bind.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2009 Red Hat, Inc.
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. * All rights reserved.
  6. *
  7. * Contributors:
  8. * Hewlett-Packard Development Company, L.P.
  9. * Bugfix for bug #193297
  10. * Bugfix for bug #201275
  11. *
  12. * License: GPL (version 3 or any later version).
  13. * See LICENSE for details.
  14. * END COPYRIGHT BLOCK **/
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. /* bind.c - decode an ldap bind operation and pass it to a backend db */
  19. /*
  20. * Copyright (c) 1995 Regents of the University of Michigan.
  21. * All rights reserved.
  22. *
  23. * Redistribution and use in source and binary forms are permitted
  24. * provided that this notice is preserved and that due credit is given
  25. * to the University of Michigan at Ann Arbor. The name of the University
  26. * may not be used to endorse or promote products derived from this
  27. * software without specific prior written permission. This software
  28. * is provided ``as is'' without express or implied warranty.
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include "slap.h"
  35. #include "fe.h"
  36. #include "pratom.h"
  37. #include <sasl.h>
  38. static void log_bind_access(
  39. Slapi_PBlock *pb,
  40. const char* dn,
  41. ber_tag_t method,
  42. int version,
  43. const char *saslmech,
  44. const char *msg
  45. );
  46. /*
  47. * Function: is_root_dn_pw
  48. *
  49. * Returns: 1 if the password for the root dn is correct.
  50. * 0 otherwise.
  51. * dn must be normalized
  52. *
  53. */
  54. static int
  55. is_root_dn_pw( const char *dn, const Slapi_Value *cred )
  56. {
  57. int rv= 0;
  58. char *rootpw = config_get_rootpw();
  59. if ( rootpw == NULL || !slapi_dn_isroot( dn ) )
  60. {
  61. rv = 0;
  62. }
  63. else
  64. {
  65. Slapi_Value rdnpwbv;
  66. Slapi_Value *rdnpwvals[2];
  67. slapi_value_init_string(&rdnpwbv,rootpw);
  68. rdnpwvals[ 0 ] = &rdnpwbv;
  69. rdnpwvals[ 1 ] = NULL;
  70. rv = slapi_pw_find_sv( rdnpwvals, cred ) == 0;
  71. value_done(&rdnpwbv);
  72. }
  73. slapi_ch_free_string( &rootpw );
  74. return rv;
  75. }
  76. void
  77. do_bind( Slapi_PBlock *pb )
  78. {
  79. BerElement *ber = pb->pb_op->o_ber;
  80. int err, isroot;
  81. ber_tag_t method = LBER_DEFAULT;
  82. ber_int_t version = -1;
  83. int auth_response_requested = 0;
  84. int pw_response_requested = 0;
  85. char *rawdn = NULL;
  86. const char *dn = NULL;
  87. char *saslmech = NULL;
  88. struct berval cred = {0};
  89. Slapi_Backend *be = NULL;
  90. ber_tag_t ber_rc;
  91. int rc = 0;
  92. Slapi_DN *sdn = NULL;
  93. int bind_sdn_in_pb = 0; /* is sdn set in the pb? */
  94. Slapi_Entry *referral;
  95. char errorbuf[BUFSIZ];
  96. char **supported, **pmech;
  97. char authtypebuf[256]; /* >26 (strlen(SLAPD_AUTH_SASL)+SASL_MECHNAMEMAX+1) */
  98. Slapi_Entry *bind_target_entry = NULL;
  99. int auto_bind = 0;
  100. int minssf = 0;
  101. int minssf_exclude_rootdse = 0;
  102. Slapi_DN *original_sdn = NULL;
  103. LDAPDebug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
  104. /*
  105. * Parse the bind request. It looks like this:
  106. *
  107. * BindRequest ::= SEQUENCE {
  108. * version INTEGER, -- version
  109. * name DistinguishedName, -- dn
  110. * authentication CHOICE {
  111. * simple [0] OCTET STRING, -- passwd
  112. * krbv42ldap [1] OCTET STRING, -- not used
  113. * krbv42dsa [2] OCTET STRING, -- not used
  114. * sasl [3] SaslCredentials -- v3 only
  115. * }
  116. * }
  117. *
  118. * Saslcredentials ::= SEQUENCE {
  119. * mechanism LDAPString,
  120. * credentials OCTET STRING
  121. * }
  122. */
  123. ber_rc = ber_scanf( ber, "{iat", &version, &rawdn, &method );
  124. if ( ber_rc == LBER_ERROR ) {
  125. LDAPDebug( LDAP_DEBUG_ANY,
  126. "ber_scanf failed (op=Bind; params=Version,DN,Method)\n",
  127. 0, 0, 0 );
  128. log_bind_access (pb, "???", method, version, saslmech, "decoding error");
  129. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  130. "decoding error", 0, NULL );
  131. slapi_ch_free_string(&rawdn);
  132. return;
  133. }
  134. /* Check if we should be performing strict validation. */
  135. if (rawdn && config_get_dn_validate_strict()) {
  136. /* check that the dn is formatted correctly */
  137. rc = slapi_dn_syntax_check(pb, rawdn, 1);
  138. if (rc) { /* syntax check failed */
  139. op_shared_log_error_access(pb, "BIND", rawdn,
  140. "strict: invalid bind dn");
  141. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX,
  142. NULL, "invalid bind dn", 0, NULL);
  143. slapi_ch_free_string(&rawdn);
  144. return;
  145. }
  146. }
  147. sdn = slapi_sdn_new_dn_passin(rawdn);
  148. dn = slapi_sdn_get_dn(sdn);
  149. if (rawdn && (strlen(rawdn) > 0) && (NULL == dn)) {
  150. /* normalization failed */
  151. op_shared_log_error_access(pb, "BIND", rawdn, "invalid bind dn");
  152. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX, NULL,
  153. "invalid bind dn", 0, NULL);
  154. slapi_sdn_free(&sdn);
  155. return;
  156. }
  157. LDAPDebug( LDAP_DEBUG_TRACE, "BIND dn=\"%s\" method=%d version=%d\n",
  158. dn?dn:"empty", method, version );
  159. /* target spec is used to decide which plugins are applicable for the operation */
  160. operation_set_target_spec (pb->pb_op, sdn);
  161. switch ( method ) {
  162. case LDAP_AUTH_SASL:
  163. if ( version < LDAP_VERSION3 ) {
  164. LDAPDebug( LDAP_DEBUG_ANY,
  165. "got SASL credentials from LDAPv2 client\n",
  166. 0, 0, 0 );
  167. log_bind_access (pb, dn?dn:"empty", method, version, saslmech, "SASL credentials only in LDAPv3");
  168. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  169. "SASL credentials only in LDAPv3", 0, NULL );
  170. goto free_and_return;
  171. }
  172. /* Get the SASL mechanism */
  173. ber_rc = ber_scanf( ber, "{a", &saslmech );
  174. /* Get the (optional) SASL credentials */
  175. if ( ber_rc != LBER_ERROR ) {
  176. /* Credentials are optional in SASL bind */
  177. ber_len_t clen;
  178. if (( ber_peek_tag( ber, &clen )) == LBER_OCTETSTRING ) {
  179. ber_rc = ber_scanf( ber, "o}}", &cred );
  180. if (cred.bv_len == 0) {
  181. slapi_ch_free_string(&cred.bv_val);
  182. }
  183. } else {
  184. ber_rc = ber_scanf( ber, "}}" );
  185. }
  186. }
  187. break;
  188. case LDAP_AUTH_KRBV41:
  189. /* FALLTHROUGH */
  190. case LDAP_AUTH_KRBV42:
  191. if ( version >= LDAP_VERSION3 ) {
  192. static char *kmsg =
  193. "LDAPv2-style kerberos authentication received "
  194. "on LDAPv3 connection.";
  195. LDAPDebug( LDAP_DEBUG_ANY, kmsg, 0, 0, 0 );
  196. log_bind_access (pb, dn?dn:"empty", method, version, saslmech, kmsg);
  197. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  198. kmsg, 0, NULL );
  199. goto free_and_return;
  200. }
  201. /* FALLTHROUGH */
  202. case LDAP_AUTH_SIMPLE:
  203. ber_rc = ber_scanf( ber, "o}", &cred );
  204. if (cred.bv_len == 0) {
  205. slapi_ch_free_string(&cred.bv_val);
  206. }
  207. break;
  208. default:
  209. log_bind_access (pb, dn?dn:"empty", method, version, saslmech, "Unknown bind method");
  210. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  211. "Unknown bind method", 0, NULL );
  212. goto free_and_return;
  213. }
  214. if ( ber_rc == LBER_ERROR ) {
  215. LDAPDebug( LDAP_DEBUG_ANY,
  216. "ber_scanf failed (op=Bind; params=Credentials)\n",
  217. 0, 0, 0 );
  218. log_bind_access (pb, dn?dn:"empty", method, version, saslmech, "decoding error");
  219. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  220. "decoding error", 0, NULL );
  221. goto free_and_return;
  222. }
  223. /*
  224. * in LDAPv3 there can be optional control extensions on
  225. * the end of an LDAPMessage. we need to read them in and
  226. * pass them to the backend.
  227. * We also check for the presence of an "Authentication Request
  228. * Control" and set a flag so we know later whether we need to send
  229. * an "Authentication Response Control" with Success responses.
  230. */
  231. {
  232. LDAPControl **reqctrls;
  233. if (( err = get_ldapmessage_controls( pb, ber, &reqctrls ))
  234. != 0 ) {
  235. log_bind_access (pb, dn?dn:"empty", method,
  236. version, saslmech, "failed to parse LDAP controls");
  237. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  238. goto free_and_return;
  239. }
  240. auth_response_requested = slapi_control_present( reqctrls,
  241. LDAP_CONTROL_AUTH_REQUEST, NULL, NULL );
  242. slapi_pblock_get (pb, SLAPI_PWPOLICY, &pw_response_requested);
  243. }
  244. PR_EnterMonitor(pb->pb_conn->c_mutex);
  245. bind_credentials_clear( pb->pb_conn, PR_FALSE, /* do not lock conn */
  246. PR_FALSE /* do not clear external creds. */ );
  247. #if defined(ENABLE_AUTOBIND)
  248. /* LDAPI might have auto bind on, binding as anon should
  249. mean bind as self in this case
  250. */
  251. /* You are "bound" when the SSL connection is made,
  252. but the client still passes a BIND SASL/EXTERNAL request.
  253. */
  254. if((LDAP_AUTH_SASL == method) &&
  255. (0 == strcasecmp (saslmech, LDAP_SASL_EXTERNAL)) &&
  256. (0 == dn || 0 == dn[0]) && pb->pb_conn->c_unix_local)
  257. {
  258. slapd_bind_local_user(pb->pb_conn);
  259. if(pb->pb_conn->c_dn)
  260. {
  261. auto_bind = 1; /* flag the bind method */
  262. dn = slapi_ch_strdup(pb->pb_conn->c_dn);
  263. slapi_sdn_free(&sdn);
  264. sdn = slapi_sdn_new_dn_passin(dn);
  265. }
  266. }
  267. #endif /* ENABLE_AUTOBIND */
  268. /* Clear the password policy flag that forbid operation
  269. * other than Bind, Modify, Unbind :
  270. * With a new bind, the flag should be reset so that the new
  271. * bound user can work properly
  272. */
  273. pb->pb_conn->c_needpw = 0;
  274. PR_ExitMonitor(pb->pb_conn->c_mutex);
  275. log_bind_access(pb, dn?dn:"empty", method, version, saslmech, NULL);
  276. switch ( version ) {
  277. case LDAP_VERSION2:
  278. if (method == LDAP_AUTH_SIMPLE
  279. && (config_get_force_sasl_external() ||
  280. ((dn == NULL || *dn == '\0') && cred.bv_len == 0))
  281. && pb->pb_conn->c_external_dn != NULL) {
  282. /* Treat this like a SASL EXTERNAL Bind: */
  283. method = LDAP_AUTH_SASL;
  284. saslmech = slapi_ch_strdup (LDAP_SASL_EXTERNAL);
  285. /* This enables a client to establish an identity by sending
  286. * a certificate in the SSL handshake, and also use LDAPv2
  287. * (by sending this type of Bind request).
  288. */
  289. }
  290. break;
  291. case LDAP_VERSION3:
  292. if ((method == LDAP_AUTH_SIMPLE) &&
  293. config_get_force_sasl_external() &&
  294. (pb->pb_conn->c_external_dn != NULL)) {
  295. /* Treat this like a SASL EXTERNAL Bind: */
  296. method = LDAP_AUTH_SASL;
  297. saslmech = slapi_ch_strdup (LDAP_SASL_EXTERNAL);
  298. /* This enables a client to establish an identity by sending
  299. * a certificate in the SSL handshake, and also use LDAPv2
  300. * (by sending this type of Bind request).
  301. */
  302. }
  303. break;
  304. default:
  305. LDAPDebug( LDAP_DEBUG_TRACE, "bind: unknown LDAP protocol version %d\n",
  306. version, 0, 0 );
  307. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL,
  308. "version not supported", 0, NULL );
  309. goto free_and_return;
  310. }
  311. LDAPDebug( LDAP_DEBUG_TRACE, "do_bind: version %d method 0x%x dn %s\n",
  312. version, method, dn );
  313. pb->pb_conn->c_ldapversion = version;
  314. isroot = slapi_dn_isroot( slapi_sdn_get_ndn(sdn) );
  315. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
  316. slapi_pblock_set( pb, SLAPI_BIND_TARGET_SDN, (void*)sdn );
  317. bind_sdn_in_pb = 1; /* pb now owns sdn */
  318. slapi_pblock_set( pb, SLAPI_BIND_METHOD, &method );
  319. slapi_pblock_set( pb, SLAPI_BIND_SASLMECHANISM, saslmech );
  320. slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, &cred );
  321. if (method != LDAP_AUTH_SASL) {
  322. /*
  323. * RFC2251: client may abort a sasl bind negotiation by sending
  324. * an authentication choice other than sasl.
  325. */
  326. pb->pb_conn->c_flags &= ~CONN_FLAG_SASL_CONTINUE;
  327. }
  328. switch ( method ) {
  329. case LDAP_AUTH_SASL:
  330. /*
  331. * All SASL auth methods are categorized as strong binds,
  332. * although they are not necessarily stronger than simple.
  333. */
  334. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsStrongAuthBinds);
  335. if ( saslmech == NULL || *saslmech == '\0' ) {
  336. send_ldap_result( pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  337. "SASL mechanism absent", 0, NULL );
  338. goto free_and_return;
  339. }
  340. if (strlen(saslmech) > SASL_MECHNAMEMAX) {
  341. send_ldap_result( pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  342. "SASL mechanism name is too long", 0, NULL );
  343. goto free_and_return;
  344. }
  345. supported = slapi_get_supported_saslmechanisms_copy();
  346. if ( (pmech = supported) != NULL ) while (1) {
  347. if (*pmech == NULL) {
  348. /*
  349. * As we call the safe function, we receive a strdup'd saslmechanisms
  350. * charray. Therefore, we need to remove it instead of NULLing it
  351. */
  352. charray_free(supported);
  353. pmech = supported = NULL;
  354. break;
  355. }
  356. if (!strcasecmp (saslmech, *pmech)) break;
  357. ++pmech;
  358. }
  359. if (!pmech) {
  360. /* now check the sasl library */
  361. /* ids_sasl_check_bind takes care of calling bind
  362. * pre-op plugins after it knows the target DN */
  363. ids_sasl_check_bind(pb);
  364. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  365. goto free_and_return;
  366. }
  367. else {
  368. charray_free(supported); /* Avoid leaking */
  369. }
  370. if (!strcasecmp (saslmech, LDAP_SASL_EXTERNAL)) {
  371. /* call preop plugins */
  372. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) != 0){
  373. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, "", 0, NULL);
  374. goto free_and_return;
  375. }
  376. #if defined(ENABLE_AUTOBIND)
  377. if (1 == auto_bind) {
  378. /* Already AUTO-BOUND */
  379. break;
  380. }
  381. #endif
  382. /*
  383. * if this is not an SSL connection, fail and return an
  384. * inappropriateAuth error.
  385. */
  386. if ( 0 == ( pb->pb_conn->c_flags & CONN_FLAG_SSL )) {
  387. send_ldap_result( pb, LDAP_INAPPROPRIATE_AUTH, NULL,
  388. "SASL EXTERNAL bind requires an SSL connection",
  389. 0, NULL );
  390. /* call postop plugins */
  391. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  392. goto free_and_return;
  393. }
  394. /*
  395. * Check for the client certificate.
  396. */
  397. if( NULL == pb->pb_conn->c_client_cert){
  398. send_ldap_result( pb, LDAP_INAPPROPRIATE_AUTH, NULL,
  399. "missing client certificate", 0, NULL );
  400. /* call postop plugins */
  401. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  402. goto free_and_return;
  403. }
  404. /*
  405. * if the client sent us a certificate but we could not map it
  406. * to an LDAP DN, fail and return an invalidCredentials error.
  407. */
  408. if ( NULL == pb->pb_conn->c_external_dn ) {
  409. send_ldap_result( pb, LDAP_INVALID_CREDENTIALS, NULL,
  410. "client certificate mapping failed", 0, NULL );
  411. /* call postop plugins */
  412. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  413. goto free_and_return;
  414. }
  415. if (!isroot) {
  416. /* check if the account is locked */
  417. bind_target_entry = get_entry(pb, pb->pb_conn->c_external_dn);
  418. if ( bind_target_entry && slapi_check_account_lock(pb, bind_target_entry,
  419. pw_response_requested, 1 /*check password policy*/, 1 /*send ldap result*/) == 1) {
  420. /* call postop plugins */
  421. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  422. goto free_and_return;
  423. }
  424. }
  425. /*
  426. * copy external credentials into connection structure
  427. */
  428. bind_credentials_set( pb->pb_conn,
  429. pb->pb_conn->c_external_authtype,
  430. pb->pb_conn->c_external_dn,
  431. NULL, NULL, NULL , NULL);
  432. if ( auth_response_requested ) {
  433. slapi_add_auth_response_control( pb, pb->pb_conn->c_external_dn );
  434. }
  435. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  436. /* call postop plugins */
  437. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  438. goto free_and_return;
  439. }
  440. break;
  441. case LDAP_AUTH_SIMPLE:
  442. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsSimpleAuthBinds);
  443. /* Check if the minimum SSF requirement has been met. */
  444. minssf = config_get_minssf();
  445. /*
  446. * If nsslapd-minssf-exclude-rootdse is on, we have to go to the
  447. * next step and check if the operation is against rootdse or not.
  448. * Once found it's not on rootdse, return LDAP_UNWILLING_TO_PERFORM
  449. * there.
  450. */
  451. minssf_exclude_rootdse = config_get_minssf_exclude_rootdse();
  452. if (!minssf_exclude_rootdse && (pb->pb_conn->c_sasl_ssf < minssf) &&
  453. (pb->pb_conn->c_ssl_ssf < minssf) &&
  454. (pb->pb_conn->c_local_ssf < minssf)) {
  455. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  456. "Minimum SSF not met.", 0, NULL);
  457. /* increment BindSecurityErrorcount */
  458. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  459. goto free_and_return;
  460. }
  461. /* accept null binds */
  462. if (dn == NULL || *dn == '\0') {
  463. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsAnonymousBinds);
  464. /* by definition anonymous is also unauthenticated so increment
  465. that counter */
  466. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  467. /* Refuse the operation if anonymous access is disabled. We need to allow
  468. * an anonymous bind through if only root DSE anonymous access is set too. */
  469. if (config_get_anon_access_switch() == SLAPD_ANON_ACCESS_OFF) {
  470. send_ldap_result(pb, LDAP_INAPPROPRIATE_AUTH, NULL,
  471. "Anonymous access is not allowed", 0, NULL);
  472. /* increment BindSecurityErrorcount */
  473. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  474. goto free_and_return;
  475. }
  476. /* set the bind credentials so anonymous limits are set */
  477. bind_credentials_set( pb->pb_conn, SLAPD_AUTH_NONE,
  478. NULL, NULL, NULL, NULL , NULL);
  479. /* call preop plugins */
  480. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) == 0){
  481. if ( auth_response_requested ) {
  482. slapi_add_auth_response_control( pb, "" );
  483. }
  484. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  485. /* call postop plugins */
  486. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  487. } else {
  488. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, "", 0, NULL);
  489. }
  490. goto free_and_return;
  491. /* Check if unauthenticated binds are allowed. */
  492. } else if ( cred.bv_len == 0 ) {
  493. /* Increment unauthenticated bind counter */
  494. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  495. /* Refuse the operation if anonymous access is disabled. */
  496. if (config_get_anon_access_switch() != SLAPD_ANON_ACCESS_ON) {
  497. send_ldap_result(pb, LDAP_INAPPROPRIATE_AUTH, NULL,
  498. "Anonymous access is not allowed", 0, NULL);
  499. /* increment BindSecurityErrorcount */
  500. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  501. goto free_and_return;
  502. }
  503. /* Refuse the operation if unauthenticated binds are disabled. */
  504. if (!config_get_unauth_binds_switch()) {
  505. /* As stated in RFC 4513, a server SHOULD by default fail
  506. * Unauthenticated Bind requests with a resultCode of
  507. * unwillingToPerform. */
  508. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  509. "Unauthenticated binds are not allowed", 0, NULL);
  510. /* increment BindSecurityErrorcount */
  511. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  512. goto free_and_return;
  513. }
  514. /* Check if simple binds are allowed over an insecure channel. We only check
  515. * this for authenticated binds. */
  516. } else if (config_get_require_secure_binds() == 1) {
  517. Connection *conn = NULL;
  518. int sasl_ssf = 0;
  519. int local_ssf = 0;
  520. /* Allow simple binds only for SSL/TLS established connections
  521. * or connections using SASL privacy layers */
  522. conn = pb->pb_conn;
  523. if ( slapi_pblock_get(pb, SLAPI_CONN_SASL_SSF, &sasl_ssf) != 0) {
  524. slapi_log_error( SLAPI_LOG_PLUGIN, "do_bind",
  525. "Could not get SASL SSF from connection\n" );
  526. sasl_ssf = 0;
  527. }
  528. if ( slapi_pblock_get(pb, SLAPI_CONN_LOCAL_SSF, &local_ssf) != 0) {
  529. slapi_log_error( SLAPI_LOG_PLUGIN, "do_bind",
  530. "Could not get local SSF from connection\n" );
  531. local_ssf = 0;
  532. }
  533. if (((conn->c_flags & CONN_FLAG_SSL) != CONN_FLAG_SSL) &&
  534. (sasl_ssf <= 1) && (local_ssf <= 1)) {
  535. send_ldap_result(pb, LDAP_CONFIDENTIALITY_REQUIRED, NULL,
  536. "Operation requires a secure connection",
  537. 0, NULL);
  538. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  539. goto free_and_return;
  540. }
  541. }
  542. break;
  543. default:
  544. break;
  545. }
  546. /*
  547. * handle binds as the manager here, pass others to the backend
  548. */
  549. if ( isroot && method == LDAP_AUTH_SIMPLE ) {
  550. if (cred.bv_len != 0) {
  551. /* a passwd was supplied -- check it */
  552. Slapi_Value cv;
  553. slapi_value_init_berval(&cv,&cred);
  554. /*
  555. * Call pre bind root dn plugin for checking root dn access control.
  556. *
  557. * Do this before checking the password so that we give a consistent error,
  558. * regardless if the password is correct or not. Or else it would still be
  559. * possible to brute force guess the password even though access would still
  560. * be denied.
  561. */
  562. if (plugin_call_plugins(pb, SLAPI_PLUGIN_INTERNAL_PRE_BIND_FN) != 0){
  563. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  564. "RootDN access control violation", 0, NULL );
  565. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  566. value_done(&cv);
  567. goto free_and_return;
  568. }
  569. /*
  570. * Check the dn and password
  571. */
  572. if ( is_root_dn_pw( slapi_sdn_get_ndn(sdn), &cv )) {
  573. /*
  574. * right dn and passwd - authorize
  575. */
  576. bind_credentials_set( pb->pb_conn, SLAPD_AUTH_SIMPLE, slapi_ch_strdup(slapi_sdn_get_ndn(sdn)),
  577. NULL, NULL, NULL , NULL);
  578. } else {
  579. /*
  580. * right dn, wrong passwd - reject with invalid credentials
  581. */
  582. send_ldap_result( pb, LDAP_INVALID_CREDENTIALS, NULL, NULL, 0, NULL );
  583. /* increment BindSecurityErrorcount */
  584. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  585. value_done(&cv);
  586. goto free_and_return;
  587. }
  588. value_done(&cv);
  589. }
  590. /* call preop plugin */
  591. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) == 0){
  592. if ( auth_response_requested ) {
  593. slapi_add_auth_response_control( pb,
  594. ( cred.bv_len == 0 ) ? "" :
  595. slapi_sdn_get_ndn(sdn));
  596. }
  597. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  598. /* call postop plugins */
  599. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  600. } else {
  601. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, "", 0, NULL);
  602. }
  603. goto free_and_return;
  604. }
  605. /* We could be serving multiple database backends. Select the appropriate one */
  606. if (slapi_mapping_tree_select(pb, &be, &referral, errorbuf) != LDAP_SUCCESS) {
  607. send_nobackend_ldap_result( pb );
  608. be = NULL;
  609. goto free_and_return;
  610. }
  611. if (referral) {
  612. send_referrals_from_entry(pb,referral);
  613. slapi_entry_free(referral);
  614. goto free_and_return;
  615. }
  616. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  617. /* not root dn - pass to the backend */
  618. if ( be->be_bind != NULL ) {
  619. original_sdn = slapi_sdn_dup(sdn);
  620. /*
  621. * call the pre-bind plugins. if they succeed, call
  622. * the backend bind function. then call the post-bind
  623. * plugins.
  624. */
  625. if ( plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) == 0 ) {
  626. int sdn_updated = 0;
  627. rc = 0;
  628. /* Check if a pre_bind plugin mapped the DN to another backend */
  629. Slapi_DN *pb_sdn;
  630. slapi_pblock_get(pb, SLAPI_BIND_TARGET_SDN, &pb_sdn);
  631. if (!pb_sdn) {
  632. PR_snprintf(errorbuf, sizeof(errorbuf), "Pre-bind plug-in set NULL dn\n");
  633. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, errorbuf, 0, NULL);
  634. goto free_and_return;
  635. } else if ((pb_sdn != sdn) || (sdn_updated = slapi_sdn_compare(original_sdn, pb_sdn))) {
  636. /*
  637. * Slapi_DN set in pblock was changed by a pre bind plug-in.
  638. * It is a plug-in's responsibility to free the original Slapi_DN.
  639. */
  640. sdn = pb_sdn;
  641. dn = slapi_sdn_get_dn(sdn);
  642. if (!dn) {
  643. PR_snprintf(errorbuf, sizeof(errorbuf), "Pre-bind plug-in set corrupted dn\n");
  644. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, errorbuf, 0, NULL);
  645. goto free_and_return;
  646. }
  647. if (!sdn_updated) { /* pb_sdn != sdn; need to compare the dn's. */
  648. sdn_updated = slapi_sdn_compare(original_sdn, sdn);
  649. }
  650. if (sdn_updated) { /* call slapi_be_select only when the DN is updated. */
  651. slapi_be_Unlock(be);
  652. be = slapi_be_select_exact(sdn);
  653. if (be) {
  654. slapi_be_Rlock(be);
  655. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  656. } else {
  657. PR_snprintf(errorbuf, sizeof(errorbuf), "No matching backend for %s\n", dn);
  658. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, errorbuf, 0, NULL);
  659. goto free_and_return;
  660. }
  661. }
  662. }
  663. /*
  664. * Is this account locked ?
  665. * could be locked through the account inactivation
  666. * or by the password policy
  667. *
  668. * rc=0: account not locked
  669. * rc=1: account locked, can not bind, result has been sent
  670. * rc!=0 and rc!=1: error. Result was not sent, lets be_bind
  671. * deal with it.
  672. *
  673. */
  674. /* get the entry now, so that we can give it to slapi_check_account_lock and reslimit_update_from_dn */
  675. if (! slapi_be_is_flag_set(be, SLAPI_BE_FLAG_REMOTE_DATA)) {
  676. bind_target_entry = get_entry(pb, slapi_sdn_get_ndn(sdn));
  677. rc = slapi_check_account_lock ( pb, bind_target_entry, pw_response_requested, 1, 1);
  678. }
  679. slapi_pblock_set( pb, SLAPI_PLUGIN, be->be_database );
  680. set_db_default_result_handlers(pb);
  681. if ( (rc != 1) &&
  682. (auto_bind ||
  683. (((rc = (*be->be_bind)( pb )) == SLAPI_BIND_SUCCESS) ||
  684. (rc == SLAPI_BIND_ANONYMOUS))) ) {
  685. long t;
  686. char* authtype = NULL;
  687. /* rc is SLAPI_BIND_SUCCESS or SLAPI_BIND_ANONYMOUS */
  688. if(auto_bind) {
  689. rc = SLAPI_BIND_SUCCESS;
  690. }
  691. switch ( method ) {
  692. case LDAP_AUTH_SIMPLE:
  693. if (cred.bv_len != 0) {
  694. authtype = SLAPD_AUTH_SIMPLE;
  695. }
  696. #if defined(ENABLE_AUTOBIND)
  697. else if(auto_bind) {
  698. authtype = SLAPD_AUTH_OS;
  699. }
  700. #endif /* ENABLE_AUTOBIND */
  701. else {
  702. authtype = SLAPD_AUTH_NONE;
  703. }
  704. break;
  705. case LDAP_AUTH_SASL:
  706. /* authtype = SLAPD_AUTH_SASL && saslmech: */
  707. PR_snprintf(authtypebuf, sizeof(authtypebuf), "%s%s", SLAPD_AUTH_SASL, saslmech);
  708. authtype = authtypebuf;
  709. break;
  710. default:
  711. break;
  712. }
  713. if ( rc == SLAPI_BIND_SUCCESS ) {
  714. int myrc = 0;
  715. if (!auto_bind) {
  716. /*
  717. * There could be a race that bind_target_entry was not added
  718. * when bind_target_entry was retrieved before be_bind, but it
  719. * was in be_bind. Since be_bind returned SLAPI_BIND_SUCCESS,
  720. * the entry is in the DS. So, we need to retrieve it once more.
  721. */
  722. if (!slapi_be_is_flag_set(be, SLAPI_BE_FLAG_REMOTE_DATA) &&
  723. !bind_target_entry) {
  724. bind_target_entry = get_entry(pb, slapi_sdn_get_ndn(sdn));
  725. if (bind_target_entry) {
  726. myrc = slapi_check_account_lock(pb, bind_target_entry,
  727. pw_response_requested, 1, 1);
  728. if (1 == myrc) { /* account is locked */
  729. goto account_locked;
  730. }
  731. } else {
  732. send_ldap_result(pb, LDAP_NO_SUCH_OBJECT, NULL, "", 0, NULL);
  733. goto free_and_return;
  734. }
  735. }
  736. bind_credentials_set(pb->pb_conn, authtype,
  737. slapi_ch_strdup(slapi_sdn_get_ndn(sdn)),
  738. NULL, NULL, NULL, bind_target_entry);
  739. if (!slapi_be_is_flag_set(be, SLAPI_BE_FLAG_REMOTE_DATA)) {
  740. /* check if need new password before sending
  741. the bind success result */
  742. myrc = need_new_pw(pb, &t, bind_target_entry, pw_response_requested);
  743. switch (myrc) {
  744. case 1:
  745. (void)slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
  746. break;
  747. case 2:
  748. (void)slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRING, t);
  749. break;
  750. default:
  751. break;
  752. }
  753. }
  754. }
  755. if (auth_response_requested) {
  756. slapi_add_auth_response_control(pb, slapi_sdn_get_ndn(sdn));
  757. }
  758. if (-1 == myrc) {
  759. /* need_new_pw failed; need_new_pw already send_ldap_result in it. */
  760. goto free_and_return;
  761. }
  762. } else { /* anonymous */
  763. /* set bind creds here so anonymous limits are set */
  764. bind_credentials_set(pb->pb_conn, authtype, NULL, NULL, NULL, NULL, NULL);
  765. if ( auth_response_requested ) {
  766. slapi_add_auth_response_control(pb, "");
  767. }
  768. }
  769. } else {
  770. account_locked:
  771. if(cred.bv_len == 0) {
  772. /* its an UnAuthenticated Bind, DN specified but no pw */
  773. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsUnAuthBinds);
  774. }else{
  775. /* password must have been invalid */
  776. /* increment BindSecurityError count */
  777. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsBindSecurityErrors);
  778. }
  779. }
  780. /*
  781. * if rc != SLAPI_BIND_SUCCESS and != SLAPI_BIND_ANONYMOUS,
  782. * the result has already been sent by the backend. otherwise,
  783. * we assume it is success and send it here to avoid a race
  784. * condition where the client could be told by the
  785. * backend that the bind succeeded before we set the
  786. * c_dn field in the connection structure here in
  787. * the front end.
  788. */
  789. if ( rc == SLAPI_BIND_SUCCESS || rc == SLAPI_BIND_ANONYMOUS) {
  790. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL,
  791. 0, NULL );
  792. }
  793. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &rc );
  794. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  795. } else {
  796. /* even though preop failed, we should still call the post-op plugins */
  797. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_BIND_FN );
  798. }
  799. } else {
  800. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  801. "Function not implemented", 0, NULL );
  802. }
  803. free_and_return:;
  804. slapi_sdn_free(&original_sdn);
  805. if (be) {
  806. slapi_be_Unlock(be);
  807. }
  808. if (bind_sdn_in_pb) {
  809. slapi_pblock_get(pb, SLAPI_BIND_TARGET_SDN, &sdn);
  810. }
  811. slapi_sdn_free(&sdn);
  812. slapi_ch_free_string( &saslmech );
  813. slapi_ch_free( (void **)&cred.bv_val );
  814. if ( bind_target_entry != NULL )
  815. slapi_entry_free(bind_target_entry);
  816. }
  817. /*
  818. * register all of the LDAPv3 SASL mechanisms we know about.
  819. */
  820. void
  821. init_saslmechanisms( void )
  822. {
  823. ids_sasl_init();
  824. slapi_register_supported_saslmechanism( LDAP_SASL_EXTERNAL );
  825. }
  826. static void
  827. log_bind_access (
  828. Slapi_PBlock *pb,
  829. const char* dn,
  830. ber_tag_t method,
  831. int version,
  832. const char *saslmech,
  833. const char *msg
  834. )
  835. {
  836. if (method == LDAP_AUTH_SASL && saslmech && msg) {
  837. slapi_log_access( LDAP_DEBUG_STATS,
  838. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  839. "method=sasl version=%d mech=%s, %s\n",
  840. pb->pb_conn->c_connid, pb->pb_op->o_opid, dn,
  841. version, saslmech, msg );
  842. } else if (method == LDAP_AUTH_SASL && saslmech) {
  843. slapi_log_access( LDAP_DEBUG_STATS,
  844. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  845. "method=sasl version=%d mech=%s\n",
  846. pb->pb_conn->c_connid, pb->pb_op->o_opid, dn,
  847. version, saslmech );
  848. } else if (msg) {
  849. slapi_log_access( LDAP_DEBUG_STATS,
  850. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  851. "method=%ld version=%d, %s\n",
  852. pb->pb_conn->c_connid, pb->pb_op->o_opid, dn,
  853. method, version, msg );
  854. } else {
  855. slapi_log_access( LDAP_DEBUG_STATS,
  856. "conn=%" NSPRIu64 " op=%d BIND dn=\"%s\" "
  857. "method=%ld version=%d\n",
  858. pb->pb_conn->c_connid, pb->pb_op->o_opid, dn,
  859. method, version );
  860. }
  861. }