bind.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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( (void **) &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. } else {
  177. rc = ber_scanf( ber, "}}" );
  178. }
  179. }
  180. break;
  181. case LDAP_AUTH_KRBV41:
  182. /* FALLTHROUGH */
  183. case LDAP_AUTH_KRBV42:
  184. if ( version >= LDAP_VERSION3 ) {
  185. static char *kmsg =
  186. "LDAPv2-style kerberos authentication received "
  187. "on LDAPv3 connection.";
  188. LDAPDebug( LDAP_DEBUG_ANY, kmsg, 0, 0, 0 );
  189. log_bind_access (pb, slapi_sdn_get_dn (&sdn), method, version, saslmech, kmsg);
  190. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  191. kmsg, 0, NULL );
  192. goto free_and_return;
  193. }
  194. /* FALLTHROUGH */
  195. case LDAP_AUTH_SIMPLE:
  196. rc = ber_scanf( ber, "o}", &cred );
  197. break;
  198. default:
  199. log_bind_access (pb, slapi_sdn_get_dn (&sdn), method, version, saslmech, "Unknown bind method");
  200. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  201. "Unknown bind method", 0, NULL );
  202. goto free_and_return;
  203. }
  204. if ( rc == LBER_ERROR ) {
  205. LDAPDebug( LDAP_DEBUG_ANY,
  206. "ber_scanf failed (op=Bind; params=Credentials)\n",
  207. 0, 0, 0 );
  208. log_bind_access (pb, slapi_sdn_get_dn (&sdn), method, version, saslmech, "decoding error");
  209. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  210. "decoding error", 0, NULL );
  211. goto free_and_return;
  212. }
  213. /*
  214. * in LDAPv3 there can be optional control extensions on
  215. * the end of an LDAPMessage. we need to read them in and
  216. * pass them to the backend.
  217. * We also check for the presence of an "Authentication Request
  218. * Control" and set a flag so we know later whether we need to send
  219. * an "Authentication Response Control" with Success responses.
  220. */
  221. {
  222. LDAPControl **reqctrls;
  223. if (( err = get_ldapmessage_controls( pb, ber, &reqctrls ))
  224. != 0 ) {
  225. log_bind_access (pb, slapi_sdn_get_dn (&sdn), method,
  226. version, saslmech, "failed to parse LDAP controls");
  227. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  228. goto free_and_return;
  229. }
  230. auth_response_requested = slapi_control_present( reqctrls,
  231. LDAP_CONTROL_AUTH_REQUEST, NULL, NULL );
  232. slapi_pblock_get (pb, SLAPI_PWPOLICY, &pw_response_requested);
  233. }
  234. PR_Lock( pb->pb_conn->c_mutex );
  235. /* According to RFC2251,
  236. * "if the bind fails, the connection will be treated as anonymous".
  237. */
  238. bind_credentials_clear( pb->pb_conn, PR_FALSE, /* do not lock conn */
  239. PR_FALSE /* do not clear external creds. */ );
  240. /* LDAPI might have auto bind on, binding as anon should
  241. mean bind as self in this case
  242. */
  243. #if defined(ENABLE_AUTOBIND)
  244. if((0 == dn || 0 == dn[0]) && pb->pb_conn->c_unix_local)
  245. {
  246. slapd_bind_local_user(pb->pb_conn);
  247. if(pb->pb_conn->c_dn)
  248. {
  249. auto_bind = 1; /* flag the bind method */
  250. dn = slapi_ch_strdup(pb->pb_conn->c_dn);
  251. slapi_sdn_init_dn_passin(&sdn,dn);
  252. }
  253. }
  254. #endif /* ENABLE_AUTOBIND */
  255. /* Clear the password policy flag that forbid operation
  256. * other than Bind, Modify, Unbind :
  257. * With a new bind, the flag should be reset so that the new
  258. * bound user can work properly
  259. */
  260. pb->pb_conn->c_needpw = 0;
  261. PR_Unlock( pb->pb_conn->c_mutex );
  262. log_bind_access(pb, dn, method, version, saslmech, NULL);
  263. switch ( version ) {
  264. case LDAP_VERSION2:
  265. if (method == LDAP_AUTH_SIMPLE
  266. && (dn == NULL || *dn == '\0') && cred.bv_len == 0
  267. && pb->pb_conn->c_external_dn != NULL) {
  268. /* Treat this like a SASL EXTERNAL Bind: */
  269. method = LDAP_AUTH_SASL;
  270. saslmech = slapi_ch_strdup (LDAP_SASL_EXTERNAL);
  271. /* This enables a client to establish an identity by sending
  272. * a certificate in the SSL handshake, and also use LDAPv2
  273. * (by sending this type of Bind request).
  274. */
  275. }
  276. break;
  277. case LDAP_VERSION3:
  278. break;
  279. default:
  280. LDAPDebug( LDAP_DEBUG_TRACE, "bind: unknown LDAP protocol version %d\n",
  281. version, 0, 0 );
  282. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  283. "version not supported", 0, NULL );
  284. goto free_and_return;
  285. }
  286. LDAPDebug( LDAP_DEBUG_TRACE, "do_bind: version %d method 0x%x dn %s\n",
  287. version, method, dn );
  288. pb->pb_conn->c_ldapversion = version;
  289. isroot = slapi_dn_isroot( slapi_sdn_get_ndn(&sdn) );
  290. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
  291. slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void*)slapi_sdn_get_ndn(&sdn) );
  292. slapi_pblock_set( pb, SLAPI_BIND_METHOD, &method );
  293. slapi_pblock_set( pb, SLAPI_BIND_SASLMECHANISM, saslmech );
  294. slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, &cred );
  295. if (method != LDAP_AUTH_SASL) {
  296. /*
  297. * RFC2251: client may abort a sasl bind negotiation by sending
  298. * an authentication choice other than sasl.
  299. */
  300. pb->pb_conn->c_flags &= ~CONN_FLAG_SASL_CONTINUE;
  301. }
  302. switch ( method ) {
  303. case LDAP_AUTH_SASL:
  304. /*
  305. * All SASL auth methods are categorized as strong binds,
  306. * although they are not necessarily stronger than simple.
  307. */
  308. PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsStrongAuthBinds);
  309. if ( saslmech == NULL || *saslmech == '\0' ) {
  310. send_ldap_result( pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  311. "SASL mechanism absent", 0, NULL );
  312. goto free_and_return;
  313. }
  314. if (strlen(saslmech) > SASL_MECHNAMEMAX) {
  315. send_ldap_result( pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  316. "SASL mechanism name is too long", 0, NULL );
  317. goto free_and_return;
  318. }
  319. supported = slapi_get_supported_saslmechanisms_copy();
  320. if ( (pmech = supported) != NULL ) while (1) {
  321. if (*pmech == NULL) {
  322. /* As we call the safe function, we receive a strdup'd saslmechanisms
  323. charray. Therefore, we need to remove it instead of NULLing it */
  324. charray_free(supported);
  325. pmech = supported = NULL;
  326. break;
  327. }
  328. if (!strcasecmp (saslmech, *pmech)) break;
  329. ++pmech;
  330. }
  331. if (!pmech) {
  332. /* now check the sasl library */
  333. ids_sasl_check_bind(pb);
  334. goto free_and_return;
  335. }
  336. else {
  337. charray_free(supported); /* Avoid leaking */
  338. }
  339. if (!strcasecmp (saslmech, LDAP_SASL_EXTERNAL)) {
  340. /*
  341. * if this is not an SSL connection, fail and return an
  342. * inappropriateAuth error.
  343. */
  344. if ( 0 == ( pb->pb_conn->c_flags & CONN_FLAG_SSL )) {
  345. send_ldap_result( pb, LDAP_INAPPROPRIATE_AUTH, NULL,
  346. "SASL EXTERNAL bind requires an SSL connection",
  347. 0, NULL );
  348. goto free_and_return;
  349. }
  350. /*
  351. * if the client sent us a certificate but we could not map it
  352. * to an LDAP DN, fail and return an invalidCredentials error.
  353. */
  354. if ( NULL != pb->pb_conn->c_client_cert &&
  355. NULL == pb->pb_conn->c_external_dn ) {
  356. send_ldap_result( pb, LDAP_INVALID_CREDENTIALS, NULL,
  357. "client certificate mapping failed", 0, NULL );
  358. goto free_and_return;
  359. }
  360. /*
  361. * copy external credentials into connection structure
  362. */
  363. bind_credentials_set( pb->pb_conn,
  364. pb->pb_conn->c_external_authtype,
  365. pb->pb_conn->c_external_dn,
  366. NULL, NULL, NULL , NULL);
  367. if ( auth_response_requested ) {
  368. slapi_add_auth_response_control( pb, pb->pb_conn->c_external_dn );
  369. }
  370. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  371. goto free_and_return;
  372. }
  373. break;
  374. case LDAP_AUTH_SIMPLE:
  375. PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsSimpleAuthBinds);
  376. /* accept null binds */
  377. if (dn == NULL || *dn == '\0') {
  378. PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsAnonymousBinds);
  379. /* by definition its anonymous is also UnAuthenticated so increment
  380. that counter */
  381. PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  382. /* call preop plugins */
  383. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) == 0){
  384. if ( auth_response_requested ) {
  385. slapi_add_auth_response_control( pb, "" );
  386. }
  387. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  388. /* call postop plugins */
  389. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  390. }
  391. goto free_and_return;
  392. }
  393. break;
  394. default:
  395. break;
  396. }
  397. /*
  398. * handle binds as the manager here, pass others to the backend
  399. */
  400. if ( isroot && method == LDAP_AUTH_SIMPLE ) {
  401. if ( cred.bv_len == 0 ) {
  402. /* unauthenticated bind */
  403. PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  404. } else {
  405. /* a passwd was supplied -- check it */
  406. Slapi_Value cv;
  407. slapi_value_init_berval(&cv,&cred);
  408. if ( is_root_dn_pw( slapi_sdn_get_ndn(&sdn), &cv )) {
  409. /* right dn and passwd - authorize */
  410. bind_credentials_set( pb->pb_conn, SLAPD_AUTH_SIMPLE,
  411. slapi_ch_strdup( slapi_sdn_get_ndn(&sdn) ),
  412. NULL, NULL, NULL , NULL);
  413. /* right dn, wrong passwd - reject with invalid creds */
  414. } else {
  415. send_ldap_result( pb, LDAP_INVALID_CREDENTIALS, NULL,
  416. NULL, 0, NULL );
  417. /* increment BindSecurityErrorcount */
  418. PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  419. value_done(&cv);
  420. goto free_and_return;
  421. }
  422. value_done(&cv);
  423. }
  424. /* call preop plugin */
  425. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) == 0){
  426. if ( auth_response_requested ) {
  427. slapi_add_auth_response_control( pb,
  428. ( cred.bv_len == 0 ) ? "" :
  429. slapi_sdn_get_ndn(&sdn));
  430. }
  431. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  432. /* call postop plugins */
  433. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  434. }
  435. goto free_and_return;
  436. }
  437. /* We could be serving multiple database backends. Select the appropriate one */
  438. if (slapi_mapping_tree_select(pb, &be, &referral, errorbuf) != LDAP_SUCCESS) {
  439. send_nobackend_ldap_result( pb );
  440. be = NULL;
  441. goto free_and_return;
  442. }
  443. if (referral)
  444. {
  445. send_referrals_from_entry(pb,referral);
  446. slapi_entry_free(referral);
  447. goto free_and_return;
  448. }
  449. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  450. /* not root dn - pass to the backend */
  451. if ( be->be_bind != NULL ) {
  452. /*
  453. * call the pre-bind plugins. if they succeed, call
  454. * the backend bind function. then call the post-bind
  455. * plugins.
  456. */
  457. if ( plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN )
  458. == 0 ) {
  459. int rc = 0;
  460. /*
  461. * Is this account locked ?
  462. * could be locked through the account inactivation
  463. * or by the password policy
  464. *
  465. * rc=0: account not locked
  466. * rc=1: account locked, can not bind, result has been sent
  467. * rc!=0 and rc!=1: error. Result was not sent, lets be_bind
  468. * deal with it.
  469. *
  470. */
  471. /* get the entry now, so that we can give it to check_account_lock and reslimit_update_from_dn */
  472. if (! slapi_be_is_flag_set(be, SLAPI_BE_FLAG_REMOTE_DATA)) {
  473. bind_target_entry = get_entry(pb, slapi_sdn_get_ndn(&sdn));
  474. rc = check_account_lock ( pb, bind_target_entry, pw_response_requested,0);
  475. }
  476. slapi_pblock_set( pb, SLAPI_PLUGIN, be->be_database );
  477. set_db_default_result_handlers(pb);
  478. if ( (rc != 1) && (auto_bind || (((rc = (*be->be_bind)( pb ))
  479. == SLAPI_BIND_SUCCESS ) || rc
  480. == SLAPI_BIND_ANONYMOUS ))) {
  481. long t;
  482. {
  483. char* authtype = NULL;
  484. if(auto_bind)
  485. rc = SLAPI_BIND_SUCCESS;
  486. switch ( method ) {
  487. case LDAP_AUTH_SIMPLE:
  488. if (cred.bv_len != 0) {
  489. authtype = SLAPD_AUTH_SIMPLE;
  490. }
  491. #if defined(ENABLE_AUTOBIND)
  492. else if(auto_bind) {
  493. authtype = SLAPD_AUTH_OS;
  494. }
  495. #endif /* ENABLE_AUTOBIND */
  496. break;
  497. case LDAP_AUTH_SASL:
  498. /* authtype = SLAPD_AUTH_SASL && saslmech: */
  499. PR_snprintf(authtypebuf, sizeof(authtypebuf), "%s%s", SLAPD_AUTH_SASL, saslmech);
  500. authtype = authtypebuf;
  501. break;
  502. default: /* ??? */
  503. break;
  504. }
  505. if ( rc == SLAPI_BIND_SUCCESS ) {
  506. if(!auto_bind)
  507. bind_credentials_set( pb->pb_conn,
  508. authtype, slapi_ch_strdup(
  509. slapi_sdn_get_ndn(&sdn)),
  510. NULL, NULL, NULL, bind_target_entry );
  511. if ( auth_response_requested ) {
  512. slapi_add_auth_response_control( pb,
  513. slapi_sdn_get_ndn(&sdn));
  514. }
  515. } else { /* anonymous */
  516. if ( auth_response_requested ) {
  517. slapi_add_auth_response_control( pb,
  518. "" );
  519. }
  520. }
  521. }
  522. if ( 0 == auto_bind && rc != SLAPI_BIND_ANONYMOUS &&
  523. ! slapi_be_is_flag_set(be,
  524. SLAPI_BE_FLAG_REMOTE_DATA)) {
  525. /* check if need new password before sending
  526. the bind success result */
  527. switch ( need_new_pw (pb, &t, bind_target_entry, pw_response_requested )) {
  528. case 1:
  529. (void)slapi_add_pwd_control ( pb,
  530. LDAP_CONTROL_PWEXPIRED, 0);
  531. break;
  532. case 2:
  533. (void)slapi_add_pwd_control ( pb,
  534. LDAP_CONTROL_PWEXPIRING, t);
  535. break;
  536. case -1:
  537. goto free_and_return;
  538. default:
  539. break;
  540. }
  541. } /* end if */
  542. }else{
  543. if(cred.bv_len == 0) {
  544. /* its an UnAuthenticated Bind, DN specified but no pw */
  545. PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  546. }else{
  547. /* password must have been invalid */
  548. /* increment BindSecurityError count */
  549. PR_AtomicIncrement(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  550. }
  551. }
  552. /*
  553. * if rc != SLAPI_BIND_SUCCESS and != SLAPI_BIND_ANONYMOUS,
  554. * the result has already been sent by the backend. otherwise,
  555. * we assume it is success and send it here to avoid a race
  556. * condition where the client could be told by the
  557. * backend that the bind succeeded before we set the
  558. * c_dn field in the connection structure here in
  559. * the front end.
  560. */
  561. if ( rc == SLAPI_BIND_SUCCESS || rc == SLAPI_BIND_ANONYMOUS) {
  562. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL,
  563. 0, NULL );
  564. }
  565. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &rc );
  566. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  567. }
  568. } else {
  569. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  570. "Function not implemented", 0, NULL );
  571. }
  572. free_and_return:;
  573. if (be)
  574. slapi_be_Unlock(be);
  575. slapi_sdn_done(&sdn);
  576. if ( saslmech != NULL ) {
  577. free( saslmech );
  578. }
  579. if ( cred.bv_val != NULL ) {
  580. free( cred.bv_val );
  581. }
  582. if ( bind_target_entry != NULL )
  583. slapi_entry_free(bind_target_entry);
  584. }
  585. /*
  586. * register all of the LDAPv3 SASL mechanisms we know about.
  587. */
  588. void
  589. init_saslmechanisms( void )
  590. {
  591. ids_sasl_init();
  592. slapi_register_supported_saslmechanism( LDAP_SASL_EXTERNAL );
  593. }
  594. static void
  595. log_bind_access (
  596. Slapi_PBlock *pb,
  597. const char* dn,
  598. int method,
  599. int version,
  600. const char *saslmech,
  601. const char *msg
  602. )
  603. {
  604. char ebuf[ BUFSIZ ];
  605. const char *edn;
  606. edn = escape_string( dn, ebuf );
  607. if (method == LDAP_AUTH_SASL && saslmech && msg) {
  608. slapi_log_access( LDAP_DEBUG_STATS,
  609. "conn=%d op=%d BIND dn=\"%s\" "
  610. "method=sasl version=%d mech=%s, %s\n",
  611. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  612. version, saslmech, msg );
  613. } else if (method == LDAP_AUTH_SASL && saslmech) {
  614. slapi_log_access( LDAP_DEBUG_STATS,
  615. "conn=%d op=%d BIND dn=\"%s\" "
  616. "method=sasl version=%d mech=%s\n",
  617. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  618. version, saslmech );
  619. } else if (msg) {
  620. slapi_log_access( LDAP_DEBUG_STATS,
  621. "conn=%d op=%d BIND dn=\"%s\" "
  622. "method=%d version=%d, %s\n",
  623. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  624. method, version, msg );
  625. } else {
  626. slapi_log_access( LDAP_DEBUG_STATS,
  627. "conn=%d op=%d BIND dn=\"%s\" "
  628. "method=%d version=%d\n",
  629. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  630. method, version );
  631. }
  632. }
  633. void
  634. slapi_add_auth_response_control( Slapi_PBlock *pb, const char *binddn )
  635. {
  636. LDAPControl arctrl;
  637. char dnbuf_fixedsize[ 512 ], *dnbuf, *dnbuf_dynamic = NULL;
  638. size_t dnlen;
  639. if ( NULL == binddn ) {
  640. binddn = "";
  641. }
  642. dnlen = strlen( binddn );
  643. /*
  644. * According to draft-weltman-ldapv3-auth-response-03.txt section
  645. * 4 (Authentication Response Control):
  646. *
  647. * The controlType is "2.16.840.1.113730.3.4.15". If the bind request
  648. * succeeded and resulted in an identity (not anonymous), the
  649. * controlValue contains the authorization identity [AUTH] granted to
  650. * the requestor. If the bind request resulted in anonymous
  651. * authentication, the controlValue field is a string of zero length.
  652. *
  653. * [AUTH] is a reference to RFC 2829, which in section 9 defines
  654. * authorization identity as:
  655. *
  656. *
  657. * The authorization identity is a string in the UTF-8 character set,
  658. * corresponding to the following ABNF [7]:
  659. *
  660. * ; Specific predefined authorization (authz) id schemes are
  661. * ; defined below -- new schemes may be defined in the future.
  662. *
  663. * authzId = dnAuthzId / uAuthzId
  664. *
  665. * ; distinguished-name-based authz id.
  666. * dnAuthzId = "dn:" dn
  667. * dn = utf8string ; with syntax defined in RFC 2253
  668. *
  669. * ; unspecified userid, UTF-8 encoded.
  670. * uAuthzId = "u:" userid
  671. * userid = utf8string ; syntax unspecified
  672. *
  673. * A utf8string is defined to be the UTF-8 encoding of one or more ISO
  674. * 10646 characters.
  675. *
  676. * We always map identities to DNs, so we always use the dnAuthzId form.
  677. */
  678. arctrl.ldctl_oid = LDAP_CONTROL_AUTH_RESPONSE;
  679. arctrl.ldctl_iscritical = 0;
  680. if ( dnlen == 0 ) { /* anonymous -- return zero length value */
  681. arctrl.ldctl_value.bv_val = "";
  682. arctrl.ldctl_value.bv_len = 0;
  683. } else { /* mapped to a DN -- return "dn:<DN>" */
  684. if ( 3 + dnlen < sizeof( dnbuf_fixedsize )) {
  685. dnbuf = dnbuf_fixedsize;
  686. } else {
  687. dnbuf = dnbuf_dynamic = slapi_ch_malloc( 4 + dnlen );
  688. }
  689. strcpy( dnbuf, "dn:" );
  690. strcpy( dnbuf + 3, binddn );
  691. arctrl.ldctl_value.bv_val = dnbuf;
  692. arctrl.ldctl_value.bv_len = 3 + dnlen;
  693. }
  694. if ( slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, &arctrl ) != 0 ) {
  695. slapi_log_error( SLAPI_LOG_FATAL, "bind",
  696. "unable to add authentication response control" );
  697. }
  698. if ( NULL != dnbuf_dynamic ) {
  699. slapi_ch_free( (void **)&dnbuf_dynamic );
  700. }
  701. }