bind.c 37 KB

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