bind.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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 its anonymous is also UnAuthenticated so increment
  394. that counter */
  395. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  396. /* call preop plugins */
  397. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) == 0){
  398. if ( auth_response_requested ) {
  399. slapi_add_auth_response_control( pb, "" );
  400. }
  401. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  402. /* call postop plugins */
  403. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  404. }
  405. goto free_and_return;
  406. } else if ( cred.bv_len == 0 ) {
  407. /* Increment unauthenticated bind counter */
  408. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  409. /* Refuse the operation if unauthenticated binds are disabled. */
  410. if (!config_get_unauth_binds_switch()) {
  411. /* As stated in RFC 4513, a server SHOULD by default fail
  412. * Unauthenticated Bind requests with a resultCode of
  413. * unwillingToPerform. */
  414. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  415. "Unauthenticated binds are not allowed", 0, NULL);
  416. /* increment BindSecurityErrorcount */
  417. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  418. goto free_and_return;
  419. }
  420. }
  421. break;
  422. default:
  423. break;
  424. }
  425. /*
  426. * handle binds as the manager here, pass others to the backend
  427. */
  428. if ( isroot && method == LDAP_AUTH_SIMPLE ) {
  429. if (cred.bv_len != 0) {
  430. /* a passwd was supplied -- check it */
  431. Slapi_Value cv;
  432. slapi_value_init_berval(&cv,&cred);
  433. /* right dn and passwd - authorize */
  434. if ( is_root_dn_pw( slapi_sdn_get_ndn(&sdn), &cv )) {
  435. bind_credentials_set( pb->pb_conn, SLAPD_AUTH_SIMPLE,
  436. slapi_ch_strdup( slapi_sdn_get_ndn(&sdn) ),
  437. NULL, NULL, NULL , NULL);
  438. /* right dn, wrong passwd - reject with invalid creds */
  439. } else {
  440. send_ldap_result( pb, LDAP_INVALID_CREDENTIALS, NULL,
  441. NULL, 0, NULL );
  442. /* increment BindSecurityErrorcount */
  443. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  444. value_done(&cv);
  445. goto free_and_return;
  446. }
  447. value_done(&cv);
  448. }
  449. /* call preop plugin */
  450. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) == 0){
  451. if ( auth_response_requested ) {
  452. slapi_add_auth_response_control( pb,
  453. ( cred.bv_len == 0 ) ? "" :
  454. slapi_sdn_get_ndn(&sdn));
  455. }
  456. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  457. /* call postop plugins */
  458. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  459. }
  460. goto free_and_return;
  461. }
  462. /* We could be serving multiple database backends. Select the appropriate one */
  463. if (slapi_mapping_tree_select(pb, &be, &referral, errorbuf) != LDAP_SUCCESS) {
  464. send_nobackend_ldap_result( pb );
  465. be = NULL;
  466. goto free_and_return;
  467. }
  468. if (referral)
  469. {
  470. send_referrals_from_entry(pb,referral);
  471. slapi_entry_free(referral);
  472. goto free_and_return;
  473. }
  474. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  475. /* not root dn - pass to the backend */
  476. if ( be->be_bind != NULL ) {
  477. /*
  478. * call the pre-bind plugins. if they succeed, call
  479. * the backend bind function. then call the post-bind
  480. * plugins.
  481. */
  482. if ( plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN )
  483. == 0 ) {
  484. int rc = 0;
  485. /*
  486. * Is this account locked ?
  487. * could be locked through the account inactivation
  488. * or by the password policy
  489. *
  490. * rc=0: account not locked
  491. * rc=1: account locked, can not bind, result has been sent
  492. * rc!=0 and rc!=1: error. Result was not sent, lets be_bind
  493. * deal with it.
  494. *
  495. */
  496. /* get the entry now, so that we can give it to check_account_lock and reslimit_update_from_dn */
  497. if (! slapi_be_is_flag_set(be, SLAPI_BE_FLAG_REMOTE_DATA)) {
  498. bind_target_entry = get_entry(pb, slapi_sdn_get_ndn(&sdn));
  499. rc = check_account_lock ( pb, bind_target_entry, pw_response_requested,0);
  500. }
  501. slapi_pblock_set( pb, SLAPI_PLUGIN, be->be_database );
  502. set_db_default_result_handlers(pb);
  503. if ( (rc != 1) && (auto_bind || (((rc = (*be->be_bind)( pb ))
  504. == SLAPI_BIND_SUCCESS ) || rc
  505. == SLAPI_BIND_ANONYMOUS ))) {
  506. long t;
  507. {
  508. char* authtype = NULL;
  509. if(auto_bind)
  510. rc = SLAPI_BIND_SUCCESS;
  511. switch ( method ) {
  512. case LDAP_AUTH_SIMPLE:
  513. if (cred.bv_len != 0) {
  514. authtype = SLAPD_AUTH_SIMPLE;
  515. }
  516. #if defined(ENABLE_AUTOBIND)
  517. else if(auto_bind) {
  518. authtype = SLAPD_AUTH_OS;
  519. }
  520. #endif /* ENABLE_AUTOBIND */
  521. break;
  522. case LDAP_AUTH_SASL:
  523. /* authtype = SLAPD_AUTH_SASL && saslmech: */
  524. PR_snprintf(authtypebuf, sizeof(authtypebuf), "%s%s", SLAPD_AUTH_SASL, saslmech);
  525. authtype = authtypebuf;
  526. break;
  527. default: /* ??? */
  528. break;
  529. }
  530. if ( rc == SLAPI_BIND_SUCCESS ) {
  531. if(!auto_bind)
  532. bind_credentials_set( pb->pb_conn,
  533. authtype, slapi_ch_strdup(
  534. slapi_sdn_get_ndn(&sdn)),
  535. NULL, NULL, NULL, bind_target_entry );
  536. if ( auth_response_requested ) {
  537. slapi_add_auth_response_control( pb,
  538. slapi_sdn_get_ndn(&sdn));
  539. }
  540. } else { /* anonymous */
  541. if ( auth_response_requested ) {
  542. slapi_add_auth_response_control( pb,
  543. "" );
  544. }
  545. }
  546. }
  547. if ( 0 == auto_bind && rc != SLAPI_BIND_ANONYMOUS &&
  548. ! slapi_be_is_flag_set(be,
  549. SLAPI_BE_FLAG_REMOTE_DATA)) {
  550. /* check if need new password before sending
  551. the bind success result */
  552. switch ( need_new_pw (pb, &t, bind_target_entry, pw_response_requested )) {
  553. case 1:
  554. (void)slapi_add_pwd_control ( pb,
  555. LDAP_CONTROL_PWEXPIRED, 0);
  556. break;
  557. case 2:
  558. (void)slapi_add_pwd_control ( pb,
  559. LDAP_CONTROL_PWEXPIRING, t);
  560. break;
  561. case -1:
  562. goto free_and_return;
  563. default:
  564. break;
  565. }
  566. } /* end if */
  567. }else{
  568. if(cred.bv_len == 0) {
  569. /* its an UnAuthenticated Bind, DN specified but no pw */
  570. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  571. }else{
  572. /* password must have been invalid */
  573. /* increment BindSecurityError count */
  574. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  575. }
  576. }
  577. /*
  578. * if rc != SLAPI_BIND_SUCCESS and != SLAPI_BIND_ANONYMOUS,
  579. * the result has already been sent by the backend. otherwise,
  580. * we assume it is success and send it here to avoid a race
  581. * condition where the client could be told by the
  582. * backend that the bind succeeded before we set the
  583. * c_dn field in the connection structure here in
  584. * the front end.
  585. */
  586. if ( rc == SLAPI_BIND_SUCCESS || rc == SLAPI_BIND_ANONYMOUS) {
  587. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL,
  588. 0, NULL );
  589. }
  590. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &rc );
  591. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  592. }
  593. } else {
  594. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  595. "Function not implemented", 0, NULL );
  596. }
  597. free_and_return:;
  598. if (be)
  599. slapi_be_Unlock(be);
  600. slapi_sdn_done(&sdn);
  601. slapi_ch_free_string( &saslmech );
  602. slapi_ch_free( (void **)&cred.bv_val );
  603. if ( bind_target_entry != NULL )
  604. slapi_entry_free(bind_target_entry);
  605. }
  606. /*
  607. * register all of the LDAPv3 SASL mechanisms we know about.
  608. */
  609. void
  610. init_saslmechanisms( void )
  611. {
  612. ids_sasl_init();
  613. slapi_register_supported_saslmechanism( LDAP_SASL_EXTERNAL );
  614. }
  615. static void
  616. log_bind_access (
  617. Slapi_PBlock *pb,
  618. const char* dn,
  619. int method,
  620. int version,
  621. const char *saslmech,
  622. const char *msg
  623. )
  624. {
  625. char ebuf[ BUFSIZ ];
  626. const char *edn;
  627. edn = escape_string( dn, ebuf );
  628. if (method == LDAP_AUTH_SASL && saslmech && msg) {
  629. slapi_log_access( LDAP_DEBUG_STATS,
  630. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  631. "method=sasl version=%d mech=%s, %s\n",
  632. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  633. version, saslmech, msg );
  634. } else if (method == LDAP_AUTH_SASL && saslmech) {
  635. slapi_log_access( LDAP_DEBUG_STATS,
  636. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  637. "method=sasl version=%d mech=%s\n",
  638. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  639. version, saslmech );
  640. } else if (msg) {
  641. slapi_log_access( LDAP_DEBUG_STATS,
  642. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  643. "method=%d version=%d, %s\n",
  644. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  645. method, version, msg );
  646. } else {
  647. slapi_log_access( LDAP_DEBUG_STATS,
  648. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  649. "method=%d version=%d\n",
  650. pb->pb_conn->c_connid, pb->pb_op->o_opid, edn,
  651. method, version );
  652. }
  653. }
  654. void
  655. slapi_add_auth_response_control( Slapi_PBlock *pb, const char *binddn )
  656. {
  657. LDAPControl arctrl;
  658. char dnbuf_fixedsize[ 512 ], *dnbuf, *dnbuf_dynamic = NULL;
  659. size_t dnlen;
  660. if ( NULL == binddn ) {
  661. binddn = "";
  662. }
  663. dnlen = strlen( binddn );
  664. /*
  665. * According to draft-weltman-ldapv3-auth-response-03.txt section
  666. * 4 (Authentication Response Control):
  667. *
  668. * The controlType is "2.16.840.1.113730.3.4.15". If the bind request
  669. * succeeded and resulted in an identity (not anonymous), the
  670. * controlValue contains the authorization identity [AUTH] granted to
  671. * the requestor. If the bind request resulted in anonymous
  672. * authentication, the controlValue field is a string of zero length.
  673. *
  674. * [AUTH] is a reference to RFC 2829, which in section 9 defines
  675. * authorization identity as:
  676. *
  677. *
  678. * The authorization identity is a string in the UTF-8 character set,
  679. * corresponding to the following ABNF [7]:
  680. *
  681. * ; Specific predefined authorization (authz) id schemes are
  682. * ; defined below -- new schemes may be defined in the future.
  683. *
  684. * authzId = dnAuthzId / uAuthzId
  685. *
  686. * ; distinguished-name-based authz id.
  687. * dnAuthzId = "dn:" dn
  688. * dn = utf8string ; with syntax defined in RFC 2253
  689. *
  690. * ; unspecified userid, UTF-8 encoded.
  691. * uAuthzId = "u:" userid
  692. * userid = utf8string ; syntax unspecified
  693. *
  694. * A utf8string is defined to be the UTF-8 encoding of one or more ISO
  695. * 10646 characters.
  696. *
  697. * We always map identities to DNs, so we always use the dnAuthzId form.
  698. */
  699. arctrl.ldctl_oid = LDAP_CONTROL_AUTH_RESPONSE;
  700. arctrl.ldctl_iscritical = 0;
  701. if ( dnlen == 0 ) { /* anonymous -- return zero length value */
  702. arctrl.ldctl_value.bv_val = "";
  703. arctrl.ldctl_value.bv_len = 0;
  704. } else { /* mapped to a DN -- return "dn:<DN>" */
  705. if ( 3 + dnlen < sizeof( dnbuf_fixedsize )) {
  706. dnbuf = dnbuf_fixedsize;
  707. } else {
  708. dnbuf = dnbuf_dynamic = slapi_ch_malloc( 4 + dnlen );
  709. }
  710. strcpy( dnbuf, "dn:" );
  711. strcpy( dnbuf + 3, binddn );
  712. arctrl.ldctl_value.bv_val = dnbuf;
  713. arctrl.ldctl_value.bv_len = 3 + dnlen;
  714. }
  715. if ( slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, &arctrl ) != 0 ) {
  716. slapi_log_error( SLAPI_LOG_FATAL, "bind",
  717. "unable to add authentication response control" );
  718. }
  719. if ( NULL != dnbuf_dynamic ) {
  720. slapi_ch_free_string( &dnbuf_dynamic );
  721. }
  722. }