bind.c 27 KB

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