bind.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* bind.c - decode an ldap bind operation and pass it to a backend db */
  42. /*
  43. * Copyright (c) 1995 Regents of the University of Michigan.
  44. * All rights reserved.
  45. *
  46. * Redistribution and use in source and binary forms are permitted
  47. * provided that this notice is preserved and that due credit is given
  48. * to the University of Michigan at Ann Arbor. The name of the University
  49. * may not be used to endorse or promote products derived from this
  50. * software without specific prior written permission. This software
  51. * is provided ``as is'' without express or implied warranty.
  52. */
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <sys/types.h>
  56. #ifndef _WIN32
  57. #include <sys/socket.h>
  58. #endif
  59. #include "slap.h"
  60. #include "fe.h"
  61. #include "pratom.h"
  62. #include <sasl.h>
  63. static void log_bind_access(
  64. Slapi_PBlock *pb,
  65. const char* dn,
  66. int method,
  67. int version,
  68. const char *saslmech,
  69. const char *msg
  70. );
  71. /*
  72. * Function: is_root_dn_pw
  73. *
  74. * Returns: 1 if the password for the root dn is correct.
  75. * 0 otherwise.
  76. * dn must be normalized
  77. *
  78. */
  79. static int
  80. is_root_dn_pw( const char *dn, const Slapi_Value *cred )
  81. {
  82. int rv= 0;
  83. char *rootpw = config_get_rootpw();
  84. if ( rootpw == NULL || !slapi_dn_isroot( dn ) )
  85. {
  86. rv = 0;
  87. }
  88. else
  89. {
  90. Slapi_Value rdnpwbv;
  91. Slapi_Value *rdnpwvals[2];
  92. slapi_value_init_string(&rdnpwbv,rootpw);
  93. rdnpwvals[ 0 ] = &rdnpwbv;
  94. rdnpwvals[ 1 ] = NULL;
  95. rv = slapi_pw_find_sv( rdnpwvals, cred ) == 0;
  96. value_done(&rdnpwbv);
  97. }
  98. slapi_ch_free_string( &rootpw );
  99. return rv;
  100. }
  101. void
  102. do_bind( Slapi_PBlock *pb )
  103. {
  104. BerElement *ber = pb->pb_op->o_ber;
  105. int err, isroot;
  106. ber_tag_t method = LBER_DEFAULT;
  107. ber_int_t version = -1;
  108. int auth_response_requested = 0;
  109. int pw_response_requested = 0;
  110. char *dn = NULL, *saslmech = NULL;
  111. struct berval cred = {0};
  112. Slapi_Backend *be = NULL;
  113. ber_tag_t rc;
  114. Slapi_DN sdn;
  115. Slapi_Entry *referral;
  116. char errorbuf[BUFSIZ];
  117. char **supported, **pmech;
  118. char authtypebuf[256]; /* >26 (strlen(SLAPD_AUTH_SASL)+SASL_MECHNAMEMAX+1) */
  119. Slapi_Entry *bind_target_entry = NULL;
  120. int auto_bind = 0;
  121. LDAPDebug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
  122. /*
  123. * Parse the bind request. It looks like this:
  124. *
  125. * BindRequest ::= SEQUENCE {
  126. * version INTEGER, -- version
  127. * name DistinguishedName, -- dn
  128. * authentication CHOICE {
  129. * simple [0] OCTET STRING, -- passwd
  130. * krbv42ldap [1] OCTET STRING, -- not used
  131. * krbv42dsa [2] OCTET STRING, -- not used
  132. * sasl [3] SaslCredentials -- v3 only
  133. * }
  134. * }
  135. *
  136. * Saslcredentials ::= SEQUENCE {
  137. * mechanism LDAPString,
  138. * credentials OCTET STRING
  139. * }
  140. */
  141. rc = ber_scanf( ber, "{iat", &version, &dn, &method );
  142. if ( rc == LBER_ERROR ) {
  143. LDAPDebug( LDAP_DEBUG_ANY,
  144. "ber_scanf failed (op=Bind; params=Version,DN,Method)\n",
  145. 0, 0, 0 );
  146. log_bind_access (pb, "???", method, version, saslmech, "decoding error");
  147. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  148. "decoding error", 0, NULL );
  149. slapi_ch_free_string(&dn);
  150. return;
  151. }
  152. slapi_sdn_init_dn_passin(&sdn,dn);
  153. LDAPDebug( LDAP_DEBUG_TRACE, "BIND dn=\"%s\" method=%d version=%d\n",
  154. dn, method, version );
  155. /* target spec is used to decide which plugins are applicable for the operation */
  156. operation_set_target_spec (pb->pb_op, &sdn);
  157. switch ( method ) {
  158. case LDAP_AUTH_SASL:
  159. if ( version < LDAP_VERSION3 ) {
  160. LDAPDebug( LDAP_DEBUG_ANY,
  161. "got SASL credentials from LDAPv2 client\n",
  162. 0, 0, 0 );
  163. log_bind_access (pb, slapi_sdn_get_dn (&sdn), method, version, saslmech, "SASL credentials only in LDAPv3");
  164. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  165. "SASL credentials only in LDAPv3", 0, NULL );
  166. goto free_and_return;
  167. }
  168. /* Get the SASL mechanism */
  169. rc = ber_scanf( ber, "{a", &saslmech );
  170. /* Get the (optional) SASL credentials */
  171. if ( rc != LBER_ERROR ) {
  172. /* Credentials are optional in SASL bind */
  173. ber_len_t clen;
  174. if (( ber_peek_tag( ber, &clen )) == LBER_OCTETSTRING ) {
  175. rc = ber_scanf( ber, "o}}", &cred );
  176. if (cred.bv_len == 0) {
  177. slapi_ch_free_string(&cred.bv_val);
  178. }
  179. } else {
  180. rc = ber_scanf( ber, "}}" );
  181. }
  182. }
  183. break;
  184. case LDAP_AUTH_KRBV41:
  185. /* FALLTHROUGH */
  186. case LDAP_AUTH_KRBV42:
  187. if ( version >= LDAP_VERSION3 ) {
  188. static char *kmsg =
  189. "LDAPv2-style kerberos authentication received "
  190. "on LDAPv3 connection.";
  191. LDAPDebug( LDAP_DEBUG_ANY, kmsg, 0, 0, 0 );
  192. log_bind_access (pb, slapi_sdn_get_dn (&sdn), method, version, saslmech, kmsg);
  193. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  194. kmsg, 0, NULL );
  195. goto free_and_return;
  196. }
  197. /* FALLTHROUGH */
  198. case LDAP_AUTH_SIMPLE:
  199. rc = ber_scanf( ber, "o}", &cred );
  200. if (cred.bv_len == 0) {
  201. slapi_ch_free_string(&cred.bv_val);
  202. }
  203. break;
  204. default:
  205. log_bind_access (pb, slapi_sdn_get_dn (&sdn), method, version, saslmech, "Unknown bind method");
  206. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  207. "Unknown bind method", 0, NULL );
  208. goto free_and_return;
  209. }
  210. if ( rc == LBER_ERROR ) {
  211. LDAPDebug( LDAP_DEBUG_ANY,
  212. "ber_scanf failed (op=Bind; params=Credentials)\n",
  213. 0, 0, 0 );
  214. log_bind_access (pb, slapi_sdn_get_dn (&sdn), method, version, saslmech, "decoding error");
  215. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  216. "decoding error", 0, NULL );
  217. goto free_and_return;
  218. }
  219. /*
  220. * in LDAPv3 there can be optional control extensions on
  221. * the end of an LDAPMessage. we need to read them in and
  222. * pass them to the backend.
  223. * We also check for the presence of an "Authentication Request
  224. * Control" and set a flag so we know later whether we need to send
  225. * an "Authentication Response Control" with Success responses.
  226. */
  227. {
  228. LDAPControl **reqctrls;
  229. if (( err = get_ldapmessage_controls( pb, ber, &reqctrls ))
  230. != 0 ) {
  231. log_bind_access (pb, slapi_sdn_get_dn (&sdn), method,
  232. version, saslmech, "failed to parse LDAP controls");
  233. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  234. goto free_and_return;
  235. }
  236. auth_response_requested = slapi_control_present( reqctrls,
  237. LDAP_CONTROL_AUTH_REQUEST, NULL, NULL );
  238. slapi_pblock_get (pb, SLAPI_PWPOLICY, &pw_response_requested);
  239. }
  240. PR_Lock( pb->pb_conn->c_mutex );
  241. bind_credentials_clear( pb->pb_conn, PR_FALSE, /* do not lock conn */
  242. PR_FALSE /* do not clear external creds. */ );
  243. #if defined(ENABLE_AUTOBIND)
  244. /* LDAPI might have auto bind on, binding as anon should
  245. mean bind as self in this case
  246. */
  247. /* You are "bound" when the SSL connection is made,
  248. but the client still passes a BIND SASL/EXTERNAL request.
  249. */
  250. if((LDAP_AUTH_SASL == method) &&
  251. (0 == strcasecmp (saslmech, LDAP_SASL_EXTERNAL)) &&
  252. (0 == dn || 0 == dn[0]) && pb->pb_conn->c_unix_local)
  253. {
  254. slapd_bind_local_user(pb->pb_conn);
  255. if(pb->pb_conn->c_dn)
  256. {
  257. auto_bind = 1; /* flag the bind method */
  258. dn = slapi_ch_strdup(pb->pb_conn->c_dn);
  259. slapi_sdn_init_dn_passin(&sdn,dn);
  260. }
  261. }
  262. #endif /* ENABLE_AUTOBIND */
  263. /* Clear the password policy flag that forbid operation
  264. * other than Bind, Modify, Unbind :
  265. * With a new bind, the flag should be reset so that the new
  266. * bound user can work properly
  267. */
  268. pb->pb_conn->c_needpw = 0;
  269. PR_Unlock( pb->pb_conn->c_mutex );
  270. log_bind_access(pb, dn, method, version, saslmech, NULL);
  271. switch ( version ) {
  272. case LDAP_VERSION2:
  273. if (method == LDAP_AUTH_SIMPLE
  274. && (dn == NULL || *dn == '\0') && cred.bv_len == 0
  275. && pb->pb_conn->c_external_dn != NULL) {
  276. /* Treat this like a SASL EXTERNAL Bind: */
  277. method = LDAP_AUTH_SASL;
  278. saslmech = slapi_ch_strdup (LDAP_SASL_EXTERNAL);
  279. /* This enables a client to establish an identity by sending
  280. * a certificate in the SSL handshake, and also use LDAPv2
  281. * (by sending this type of Bind request).
  282. */
  283. }
  284. break;
  285. case LDAP_VERSION3:
  286. break;
  287. default:
  288. LDAPDebug( LDAP_DEBUG_TRACE, "bind: unknown LDAP protocol version %d\n",
  289. version, 0, 0 );
  290. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  291. "version not supported", 0, NULL );
  292. goto free_and_return;
  293. }
  294. LDAPDebug( LDAP_DEBUG_TRACE, "do_bind: version %d method 0x%x dn %s\n",
  295. version, method, dn );
  296. pb->pb_conn->c_ldapversion = version;
  297. isroot = slapi_dn_isroot( slapi_sdn_get_ndn(&sdn) );
  298. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
  299. slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void*)slapi_sdn_get_ndn(&sdn) );
  300. slapi_pblock_set( pb, SLAPI_BIND_METHOD, &method );
  301. slapi_pblock_set( pb, SLAPI_BIND_SASLMECHANISM, saslmech );
  302. slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, &cred );
  303. if (method != LDAP_AUTH_SASL) {
  304. /*
  305. * RFC2251: client may abort a sasl bind negotiation by sending
  306. * an authentication choice other than sasl.
  307. */
  308. pb->pb_conn->c_flags &= ~CONN_FLAG_SASL_CONTINUE;
  309. }
  310. switch ( method ) {
  311. case LDAP_AUTH_SASL:
  312. /*
  313. * All SASL auth methods are categorized as strong binds,
  314. * although they are not necessarily stronger than simple.
  315. */
  316. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsStrongAuthBinds);
  317. if ( saslmech == NULL || *saslmech == '\0' ) {
  318. send_ldap_result( pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  319. "SASL mechanism absent", 0, NULL );
  320. goto free_and_return;
  321. }
  322. if (strlen(saslmech) > SASL_MECHNAMEMAX) {
  323. send_ldap_result( pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  324. "SASL mechanism name is too long", 0, NULL );
  325. goto free_and_return;
  326. }
  327. supported = slapi_get_supported_saslmechanisms_copy();
  328. if ( (pmech = supported) != NULL ) while (1) {
  329. if (*pmech == NULL) {
  330. /* As we call the safe function, we receive a strdup'd saslmechanisms
  331. charray. Therefore, we need to remove it instead of NULLing it */
  332. charray_free(supported);
  333. pmech = supported = NULL;
  334. break;
  335. }
  336. if (!strcasecmp (saslmech, *pmech)) break;
  337. ++pmech;
  338. }
  339. if (!pmech) {
  340. /* now check the sasl library */
  341. ids_sasl_check_bind(pb);
  342. goto free_and_return;
  343. }
  344. else {
  345. charray_free(supported); /* Avoid leaking */
  346. }
  347. if (!strcasecmp (saslmech, LDAP_SASL_EXTERNAL)) {
  348. #if defined(ENABLE_AUTOBIND)
  349. if (1 == auto_bind) {
  350. /* Already AUTO-BOUND */
  351. break;
  352. }
  353. #endif
  354. /*
  355. * if this is not an SSL connection, fail and return an
  356. * inappropriateAuth error.
  357. */
  358. if ( 0 == ( pb->pb_conn->c_flags & CONN_FLAG_SSL )) {
  359. send_ldap_result( pb, LDAP_INAPPROPRIATE_AUTH, NULL,
  360. "SASL EXTERNAL bind requires an SSL connection",
  361. 0, NULL );
  362. goto free_and_return;
  363. }
  364. /*
  365. * if the client sent us a certificate but we could not map it
  366. * to an LDAP DN, fail and return an invalidCredentials error.
  367. */
  368. if ( NULL != pb->pb_conn->c_client_cert &&
  369. NULL == pb->pb_conn->c_external_dn ) {
  370. send_ldap_result( pb, LDAP_INVALID_CREDENTIALS, NULL,
  371. "client certificate mapping failed", 0, NULL );
  372. goto free_and_return;
  373. }
  374. /*
  375. * copy external credentials into connection structure
  376. */
  377. bind_credentials_set( pb->pb_conn,
  378. pb->pb_conn->c_external_authtype,
  379. pb->pb_conn->c_external_dn,
  380. NULL, NULL, NULL , NULL);
  381. if ( auth_response_requested ) {
  382. slapi_add_auth_response_control( pb, pb->pb_conn->c_external_dn );
  383. }
  384. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  385. goto free_and_return;
  386. }
  387. break;
  388. case LDAP_AUTH_SIMPLE:
  389. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsSimpleAuthBinds);
  390. /* accept null binds */
  391. if (dn == NULL || *dn == '\0') {
  392. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsAnonymousBinds);
  393. /* by definition anonymous is also unauthenticated so increment
  394. that counter */
  395. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  396. /* Refuse the operation if anonymous access is disabled. */
  397. if (!config_get_anon_access_switch()) {
  398. send_ldap_result(pb, LDAP_INAPPROPRIATE_AUTH, NULL,
  399. "Anonymous access is not allowed", 0, NULL);
  400. /* increment BindSecurityErrorcount */
  401. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  402. goto free_and_return;
  403. }
  404. /* call preop plugins */
  405. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) == 0){
  406. if ( auth_response_requested ) {
  407. slapi_add_auth_response_control( pb, "" );
  408. }
  409. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  410. /* call postop plugins */
  411. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  412. }
  413. goto free_and_return;
  414. /* Check if unauthenticated binds are allowed. */
  415. } else if ( cred.bv_len == 0 ) {
  416. /* Increment unauthenticated bind counter */
  417. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  418. /* Refuse the operation if anonymous access is disabled. */
  419. if (!config_get_anon_access_switch()) {
  420. send_ldap_result(pb, LDAP_INAPPROPRIATE_AUTH, NULL,
  421. "Anonymous access is not allowed", 0, NULL);
  422. /* increment BindSecurityErrorcount */
  423. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  424. goto free_and_return;
  425. }
  426. /* Refuse the operation if unauthenticated binds are disabled. */
  427. if (!config_get_unauth_binds_switch()) {
  428. /* As stated in RFC 4513, a server SHOULD by default fail
  429. * Unauthenticated Bind requests with a resultCode of
  430. * unwillingToPerform. */
  431. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  432. "Unauthenticated binds are not allowed", 0, NULL);
  433. /* increment BindSecurityErrorcount */
  434. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  435. goto free_and_return;
  436. }
  437. /* Check if simple binds are allowed over an insecure channel. We only check
  438. * this for authenticated binds. */
  439. } else if (config_get_require_secure_binds() == 1) {
  440. Connection *conn = NULL;
  441. int sasl_ssf = 0;
  442. /* Allow simple binds only for SSL/TLS established connections
  443. * or connections using SASL privacy layers */
  444. conn = pb->pb_conn;
  445. if ( slapi_pblock_get(pb, SLAPI_CONN_SASL_SSF, &sasl_ssf) != 0) {
  446. slapi_log_error( SLAPI_LOG_PLUGIN, "do_bind",
  447. "Could not get SASL SSF from connection\n" );
  448. sasl_ssf = 0;
  449. }
  450. if (((conn->c_flags & CONN_FLAG_SSL) != CONN_FLAG_SSL) &&
  451. (sasl_ssf <= 1) ) {
  452. send_ldap_result(pb, LDAP_CONFIDENTIALITY_REQUIRED, NULL,
  453. "Operation requires a secure connection",
  454. 0, NULL);
  455. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  456. goto free_and_return;
  457. }
  458. }
  459. break;
  460. default:
  461. break;
  462. }
  463. /*
  464. * handle binds as the manager here, pass others to the backend
  465. */
  466. if ( isroot && method == LDAP_AUTH_SIMPLE ) {
  467. if (cred.bv_len != 0) {
  468. /* a passwd was supplied -- check it */
  469. Slapi_Value cv;
  470. slapi_value_init_berval(&cv,&cred);
  471. /* right dn and passwd - authorize */
  472. if ( is_root_dn_pw( slapi_sdn_get_ndn(&sdn), &cv )) {
  473. bind_credentials_set( pb->pb_conn, SLAPD_AUTH_SIMPLE,
  474. slapi_ch_strdup( slapi_sdn_get_ndn(&sdn) ),
  475. NULL, NULL, NULL , NULL);
  476. /* right dn, wrong passwd - reject with invalid creds */
  477. } else {
  478. send_ldap_result( pb, LDAP_INVALID_CREDENTIALS, NULL,
  479. NULL, 0, NULL );
  480. /* increment BindSecurityErrorcount */
  481. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  482. value_done(&cv);
  483. goto free_and_return;
  484. }
  485. value_done(&cv);
  486. }
  487. /* call preop plugin */
  488. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) == 0){
  489. if ( auth_response_requested ) {
  490. slapi_add_auth_response_control( pb,
  491. ( cred.bv_len == 0 ) ? "" :
  492. slapi_sdn_get_ndn(&sdn));
  493. }
  494. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  495. /* call postop plugins */
  496. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  497. }
  498. goto free_and_return;
  499. }
  500. /* We could be serving multiple database backends. Select the appropriate one */
  501. if (slapi_mapping_tree_select(pb, &be, &referral, errorbuf) != LDAP_SUCCESS) {
  502. send_nobackend_ldap_result( pb );
  503. be = NULL;
  504. goto free_and_return;
  505. }
  506. if (referral)
  507. {
  508. send_referrals_from_entry(pb,referral);
  509. slapi_entry_free(referral);
  510. goto free_and_return;
  511. }
  512. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  513. /* not root dn - pass to the backend */
  514. if ( be->be_bind != NULL ) {
  515. /*
  516. * call the pre-bind plugins. if they succeed, call
  517. * the backend bind function. then call the post-bind
  518. * plugins.
  519. */
  520. if ( plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN )
  521. == 0 ) {
  522. int rc = 0;
  523. /*
  524. * Is this account locked ?
  525. * could be locked through the account inactivation
  526. * or by the password policy
  527. *
  528. * rc=0: account not locked
  529. * rc=1: account locked, can not bind, result has been sent
  530. * rc!=0 and rc!=1: error. Result was not sent, lets be_bind
  531. * deal with it.
  532. *
  533. */
  534. /* get the entry now, so that we can give it to check_account_lock and reslimit_update_from_dn */
  535. if (! slapi_be_is_flag_set(be, SLAPI_BE_FLAG_REMOTE_DATA)) {
  536. bind_target_entry = get_entry(pb, slapi_sdn_get_ndn(&sdn));
  537. rc = check_account_lock ( pb, bind_target_entry, pw_response_requested,0);
  538. }
  539. slapi_pblock_set( pb, SLAPI_PLUGIN, be->be_database );
  540. set_db_default_result_handlers(pb);
  541. if ( (rc != 1) && (auto_bind || (((rc = (*be->be_bind)( pb ))
  542. == SLAPI_BIND_SUCCESS ) || rc
  543. == SLAPI_BIND_ANONYMOUS ))) {
  544. long t;
  545. {
  546. char* authtype = NULL;
  547. if(auto_bind)
  548. rc = SLAPI_BIND_SUCCESS;
  549. switch ( method ) {
  550. case LDAP_AUTH_SIMPLE:
  551. if (cred.bv_len != 0) {
  552. authtype = SLAPD_AUTH_SIMPLE;
  553. }
  554. #if defined(ENABLE_AUTOBIND)
  555. else if(auto_bind) {
  556. authtype = SLAPD_AUTH_OS;
  557. }
  558. #endif /* ENABLE_AUTOBIND */
  559. break;
  560. case LDAP_AUTH_SASL:
  561. /* authtype = SLAPD_AUTH_SASL && saslmech: */
  562. PR_snprintf(authtypebuf, sizeof(authtypebuf), "%s%s", SLAPD_AUTH_SASL, saslmech);
  563. authtype = authtypebuf;
  564. break;
  565. default: /* ??? */
  566. break;
  567. }
  568. if ( rc == SLAPI_BIND_SUCCESS ) {
  569. if(!auto_bind)
  570. bind_credentials_set( pb->pb_conn,
  571. authtype, slapi_ch_strdup(
  572. slapi_sdn_get_ndn(&sdn)),
  573. NULL, NULL, NULL, bind_target_entry );
  574. if ( auth_response_requested ) {
  575. slapi_add_auth_response_control( pb,
  576. slapi_sdn_get_ndn(&sdn));
  577. }
  578. } else { /* anonymous */
  579. if ( auth_response_requested ) {
  580. slapi_add_auth_response_control( pb,
  581. "" );
  582. }
  583. }
  584. }
  585. if ( 0 == auto_bind && rc != SLAPI_BIND_ANONYMOUS &&
  586. ! slapi_be_is_flag_set(be,
  587. SLAPI_BE_FLAG_REMOTE_DATA)) {
  588. /* check if need new password before sending
  589. the bind success result */
  590. switch ( need_new_pw (pb, &t, bind_target_entry, pw_response_requested )) {
  591. case 1:
  592. (void)slapi_add_pwd_control ( pb,
  593. LDAP_CONTROL_PWEXPIRED, 0);
  594. break;
  595. case 2:
  596. (void)slapi_add_pwd_control ( pb,
  597. LDAP_CONTROL_PWEXPIRING, t);
  598. break;
  599. case -1:
  600. goto free_and_return;
  601. default:
  602. break;
  603. }
  604. } /* end if */
  605. }else{
  606. if(cred.bv_len == 0) {
  607. /* its an UnAuthenticated Bind, DN specified but no pw */
  608. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  609. }else{
  610. /* password must have been invalid */
  611. /* increment BindSecurityError count */
  612. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  613. }
  614. }
  615. /*
  616. * if rc != SLAPI_BIND_SUCCESS and != SLAPI_BIND_ANONYMOUS,
  617. * the result has already been sent by the backend. otherwise,
  618. * we assume it is success and send it here to avoid a race
  619. * condition where the client could be told by the
  620. * backend that the bind succeeded before we set the
  621. * c_dn field in the connection structure here in
  622. * the front end.
  623. */
  624. if ( rc == SLAPI_BIND_SUCCESS || rc == SLAPI_BIND_ANONYMOUS) {
  625. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL,
  626. 0, NULL );
  627. }
  628. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &rc );
  629. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  630. }
  631. } else {
  632. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  633. "Function not implemented", 0, NULL );
  634. }
  635. free_and_return:;
  636. if (be)
  637. slapi_be_Unlock(be);
  638. slapi_sdn_done(&sdn);
  639. slapi_ch_free_string( &saslmech );
  640. slapi_ch_free( (void **)&cred.bv_val );
  641. if ( bind_target_entry != NULL )
  642. slapi_entry_free(bind_target_entry);
  643. }
  644. /*
  645. * register all of the LDAPv3 SASL mechanisms we know about.
  646. */
  647. void
  648. init_saslmechanisms( void )
  649. {
  650. ids_sasl_init();
  651. slapi_register_supported_saslmechanism( LDAP_SASL_EXTERNAL );
  652. }
  653. static void
  654. log_bind_access (
  655. Slapi_PBlock *pb,
  656. const char* dn,
  657. int method,
  658. int version,
  659. const char *saslmech,
  660. const char *msg
  661. )
  662. {
  663. char ebuf[ BUFSIZ ];
  664. const char *edn;
  665. edn = escape_string( dn, ebuf );
  666. if (method == LDAP_AUTH_SASL && saslmech && msg) {
  667. slapi_log_access( LDAP_DEBUG_STATS,
  668. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  669. "method=sasl version=%d mech=%s, %s\n",
  670. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  671. version, saslmech, msg );
  672. } else if (method == LDAP_AUTH_SASL && saslmech) {
  673. slapi_log_access( LDAP_DEBUG_STATS,
  674. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  675. "method=sasl version=%d mech=%s\n",
  676. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  677. version, saslmech );
  678. } else if (msg) {
  679. slapi_log_access( LDAP_DEBUG_STATS,
  680. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  681. "method=%d version=%d, %s\n",
  682. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  683. method, version, msg );
  684. } else {
  685. slapi_log_access( LDAP_DEBUG_STATS,
  686. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  687. "method=%d version=%d\n",
  688. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  689. method, version );
  690. }
  691. }
  692. void
  693. slapi_add_auth_response_control( Slapi_PBlock *pb, const char *binddn )
  694. {
  695. LDAPControl arctrl;
  696. char dnbuf_fixedsize[ 512 ], *dnbuf, *dnbuf_dynamic = NULL;
  697. size_t dnlen;
  698. if ( NULL == binddn ) {
  699. binddn = "";
  700. }
  701. dnlen = strlen( binddn );
  702. /*
  703. * According to draft-weltman-ldapv3-auth-response-03.txt section
  704. * 4 (Authentication Response Control):
  705. *
  706. * The controlType is "2.16.840.1.113730.3.4.15". If the bind request
  707. * succeeded and resulted in an identity (not anonymous), the
  708. * controlValue contains the authorization identity [AUTH] granted to
  709. * the requestor. If the bind request resulted in anonymous
  710. * authentication, the controlValue field is a string of zero length.
  711. *
  712. * [AUTH] is a reference to RFC 2829, which in section 9 defines
  713. * authorization identity as:
  714. *
  715. *
  716. * The authorization identity is a string in the UTF-8 character set,
  717. * corresponding to the following ABNF [7]:
  718. *
  719. * ; Specific predefined authorization (authz) id schemes are
  720. * ; defined below -- new schemes may be defined in the future.
  721. *
  722. * authzId = dnAuthzId / uAuthzId
  723. *
  724. * ; distinguished-name-based authz id.
  725. * dnAuthzId = "dn:" dn
  726. * dn = utf8string ; with syntax defined in RFC 2253
  727. *
  728. * ; unspecified userid, UTF-8 encoded.
  729. * uAuthzId = "u:" userid
  730. * userid = utf8string ; syntax unspecified
  731. *
  732. * A utf8string is defined to be the UTF-8 encoding of one or more ISO
  733. * 10646 characters.
  734. *
  735. * We always map identities to DNs, so we always use the dnAuthzId form.
  736. */
  737. arctrl.ldctl_oid = LDAP_CONTROL_AUTH_RESPONSE;
  738. arctrl.ldctl_iscritical = 0;
  739. if ( dnlen == 0 ) { /* anonymous -- return zero length value */
  740. arctrl.ldctl_value.bv_val = "";
  741. arctrl.ldctl_value.bv_len = 0;
  742. } else { /* mapped to a DN -- return "dn:<DN>" */
  743. if ( 3 + dnlen < sizeof( dnbuf_fixedsize )) {
  744. dnbuf = dnbuf_fixedsize;
  745. } else {
  746. dnbuf = dnbuf_dynamic = slapi_ch_malloc( 4 + dnlen );
  747. }
  748. strcpy( dnbuf, "dn:" );
  749. strcpy( dnbuf + 3, binddn );
  750. arctrl.ldctl_value.bv_val = dnbuf;
  751. arctrl.ldctl_value.bv_len = 3 + dnlen;
  752. }
  753. if ( slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, &arctrl ) != 0 ) {
  754. slapi_log_error( SLAPI_LOG_FATAL, "bind",
  755. "unable to add authentication response control" );
  756. }
  757. if ( NULL != dnbuf_dynamic ) {
  758. slapi_ch_free_string( &dnbuf_dynamic );
  759. }
  760. }