bind.c 27 KB

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