bind.c 37 KB

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