saslbind.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. *
  43. * END COPYRIGHT BLOCK **/
  44. #ifdef HAVE_CONFIG_H
  45. # include <config.h>
  46. #endif
  47. #include <slap.h>
  48. #include <fe.h>
  49. #include <sasl.h>
  50. #include <saslplug.h>
  51. #ifndef _WIN32
  52. #include <unistd.h>
  53. #endif
  54. static char *serverfqdn;
  55. /*
  56. * utility functions needed by the sasl library
  57. */
  58. void *nssasl_mutex_alloc(void)
  59. {
  60. return PR_NewLock();
  61. }
  62. int nssasl_mutex_lock(void *mutex)
  63. {
  64. PR_Lock(mutex);
  65. return SASL_OK;
  66. }
  67. int nssasl_mutex_unlock(void *mutex)
  68. {
  69. if (PR_Unlock(mutex) == PR_SUCCESS) return SASL_OK;
  70. return SASL_FAIL;
  71. }
  72. void nssasl_mutex_free(void *mutex)
  73. {
  74. PR_DestroyLock(mutex);
  75. }
  76. void nssasl_free(void *ptr)
  77. {
  78. slapi_ch_free(&ptr);
  79. }
  80. static Slapi_ComponentId *sasl_component_id = NULL;
  81. static void generate_component_id()
  82. {
  83. if (NULL == sasl_component_id) {
  84. sasl_component_id = generate_componentid(NULL /* Not a plugin */,
  85. COMPONENT_SASL);
  86. }
  87. }
  88. static Slapi_ComponentId *sasl_get_component_id()
  89. {
  90. return sasl_component_id;
  91. }
  92. /*
  93. * sasl library callbacks
  94. */
  95. /*
  96. * We've added this auxprop stuff as a workaround for RHDS bug 166229
  97. * and FDS bug 166081. The problem is that sasldb is configured and
  98. * enabled by default, but we don't want or need to use it. What
  99. * happens after canon_user is that sasl looks up any auxiliary
  100. * properties of that user. If you don't tell sasl which auxprop
  101. * plug-in to use, it tries all of them, including sasldb. In order
  102. * to avoid this, we create a "dummy" auxprop plug-in with the name
  103. * "iDS" and tell sasl to use this plug-in for auxprop lookups.
  104. * The reason we don't need auxprops is because when we grab the user's
  105. * entry from the internal database, at the same time we get any other
  106. * properties we need - it's more efficient that way.
  107. */
  108. static void ids_auxprop_lookup(void *glob_context,
  109. sasl_server_params_t *sparams,
  110. unsigned flags,
  111. const char *user,
  112. unsigned ulen)
  113. {
  114. /* do nothing - we don't need auxprops - we just do this to avoid
  115. sasldb_auxprop_lookup */
  116. }
  117. static sasl_auxprop_plug_t ids_auxprop_plugin = {
  118. 0, /* Features */
  119. 0, /* spare */
  120. NULL, /* glob_context */
  121. NULL, /* auxprop_free */
  122. ids_auxprop_lookup, /* auxprop_lookup */
  123. "iDS", /* name */
  124. NULL /* auxprop_store */
  125. };
  126. int ids_auxprop_plug_init(const sasl_utils_t *utils,
  127. int max_version,
  128. int *out_version,
  129. sasl_auxprop_plug_t **plug,
  130. const char *plugname)
  131. {
  132. if(!out_version || !plug) return SASL_BADPARAM;
  133. if(max_version < SASL_AUXPROP_PLUG_VERSION) return SASL_BADVERS;
  134. *out_version = SASL_AUXPROP_PLUG_VERSION;
  135. *plug = &ids_auxprop_plugin;
  136. return SASL_OK;
  137. }
  138. static int ids_sasl_getopt(
  139. void *context,
  140. const char *plugin_name,
  141. const char *option,
  142. const char **result,
  143. unsigned *len
  144. )
  145. {
  146. unsigned tmplen;
  147. LDAPDebug(LDAP_DEBUG_TRACE, "ids_sasl_getopt: plugin=%s option=%s\n",
  148. plugin_name ? plugin_name : "", option, 0);
  149. if (len == NULL) len = &tmplen;
  150. *result = NULL;
  151. *len = 0;
  152. if (strcasecmp(option, "enable") == 0) {
  153. *result = "USERDB/DIGEST-MD5,GSSAPI/GSSAPI";
  154. } else if (strcasecmp(option, "has_plain_passwords") == 0) {
  155. *result = "yes";
  156. } else if (strcasecmp(option, "LOG_LEVEL") == 0) {
  157. if (LDAPDebugLevelIsSet(LDAP_DEBUG_TRACE)) {
  158. *result = "6"; /* SASL_LOG_TRACE */
  159. }
  160. } else if (strcasecmp(option, "auxprop_plugin") == 0) {
  161. *result = "iDS";
  162. }
  163. if (*result) *len = strlen(*result);
  164. return SASL_OK;
  165. }
  166. static int ids_sasl_log(
  167. void *context,
  168. int level,
  169. const char *message
  170. )
  171. {
  172. switch (level) {
  173. case SASL_LOG_ERR: /* log unusual errors (default) */
  174. slapi_log_error(SLAPI_LOG_FATAL, "sasl", "%s\n", message);
  175. break;
  176. case SASL_LOG_FAIL: /* log all authentication failures */
  177. case SASL_LOG_WARN: /* log non-fatal warnings */
  178. case SASL_LOG_NOTE: /* more verbose than LOG_WARN */
  179. case SASL_LOG_DEBUG: /* more verbose than LOG_NOTE */
  180. case SASL_LOG_TRACE: /* traces of internal protocols */
  181. case SASL_LOG_PASS: /* traces of internal protocols, including
  182. * passwords */
  183. LDAPDebug(LDAP_DEBUG_TRACE, "sasl(%d): %s\n", level, message, 0);
  184. break;
  185. case SASL_LOG_NONE: /* don't log anything */
  186. default:
  187. break;
  188. }
  189. return SASL_OK;
  190. }
  191. static int ids_sasl_proxy_policy(
  192. sasl_conn_t *conn,
  193. void *context,
  194. const char *requested_user, int rlen,
  195. const char *auth_identity, int alen,
  196. const char *def_realm, int urlen,
  197. struct propctx *propctx
  198. )
  199. {
  200. int retVal = SASL_OK;
  201. /* do not permit sasl proxy authorization */
  202. /* if the auth_identity is null or empty string, allow the sasl request to go thru */
  203. if ( (auth_identity != NULL ) && ( strlen(auth_identity) > 0 ) ) {
  204. Slapi_DN authId , reqUser;
  205. slapi_sdn_init_dn_byref(&authId,auth_identity);
  206. slapi_sdn_init_dn_byref(&reqUser,requested_user);
  207. if (slapi_sdn_compare((const Slapi_DN *)&reqUser,(const Slapi_DN *) &authId) != 0) {
  208. LDAPDebug(LDAP_DEBUG_TRACE,
  209. "sasl proxy auth not permitted authid=%s user=%s\n",
  210. auth_identity, requested_user, 0);
  211. retVal = SASL_NOAUTHZ;
  212. }
  213. slapi_sdn_done(&authId);
  214. slapi_sdn_done(&reqUser);
  215. }
  216. return retVal;
  217. }
  218. static void ids_sasl_user_search(
  219. char *basedn,
  220. int scope,
  221. char *filter,
  222. LDAPControl **ctrls,
  223. char **attrs,
  224. int attrsonly,
  225. Slapi_Entry **ep,
  226. int *foundp
  227. )
  228. {
  229. Slapi_Entry **entries = NULL;
  230. Slapi_PBlock *pb = NULL;
  231. int i, ret;
  232. LDAPDebug(LDAP_DEBUG_TRACE, "sasl user search basedn=\"%s\" filter=\"%s\"\n", basedn, filter, 0);
  233. /* TODO: set size and time limits */
  234. pb = slapi_pblock_new();
  235. if (!pb) {
  236. LDAPDebug(LDAP_DEBUG_TRACE, "null pblock for search_internal_pb\n", 0, 0, 0);
  237. goto out;
  238. }
  239. slapi_search_internal_set_pb(pb, basedn, scope, filter, attrs, attrsonly, ctrls,
  240. NULL, sasl_get_component_id(), 0);
  241. slapi_search_internal_pb(pb);
  242. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  243. if (ret != LDAP_SUCCESS) {
  244. LDAPDebug(LDAP_DEBUG_TRACE, "sasl user search failed basedn=\"%s\" "
  245. "filter=\"%s\": %s\n",
  246. basedn, filter, ldap_err2string(ret));
  247. goto out;
  248. }
  249. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  250. if ((entries == NULL) || (entries[0] == NULL)) {
  251. LDAPDebug(LDAP_DEBUG_TRACE, "sasl user search found no entries\n",
  252. 0, 0, 0);
  253. goto out;
  254. }
  255. for (i = 0; entries[i]; i++) {
  256. (*foundp)++;
  257. if (*ep != NULL) {
  258. slapi_entry_free(*ep);
  259. }
  260. *ep = slapi_entry_dup(entries[i]);
  261. LDAPDebug(LDAP_DEBUG_TRACE, "sasl user search found dn=%s\n",
  262. slapi_entry_get_dn(*ep), 0, 0);
  263. }
  264. out:
  265. if (pb) {
  266. slapi_free_search_results_internal(pb);
  267. slapi_pblock_destroy(pb);
  268. pb = NULL;
  269. }
  270. return;
  271. }
  272. /*
  273. * Search for an entry representing the sasl user.
  274. */
  275. static Slapi_Entry *ids_sasl_user_to_entry(
  276. sasl_conn_t *conn,
  277. void *context,
  278. const char *user,
  279. const char *user_realm
  280. )
  281. {
  282. int found = 0;
  283. int attrsonly = 0, scope = LDAP_SCOPE_SUBTREE;
  284. LDAPControl **ctrls = NULL;
  285. Slapi_Entry *entry = NULL;
  286. char **attrs = NULL;
  287. int regexmatch = 0;
  288. char *base = NULL;
  289. char *filter = NULL;
  290. /* Check for wildcards in the authid and realm. If we encounter one,
  291. * just fail the mapping without performing a costly internal search. */
  292. if (user && strchr(user, '*')) {
  293. LDAPDebug(LDAP_DEBUG_TRACE, "sasl user search encountered a wildcard in "
  294. "the authid. Not attempting to map to entry. (authid=%s)\n", user, 0, 0);
  295. return NULL;
  296. } else if (user_realm && strchr(user_realm, '*')) {
  297. LDAPDebug(LDAP_DEBUG_TRACE, "sasl user search encountered a wildcard in "
  298. "the realm. Not attempting to map to entry. (realm=%s)\n", user_realm, 0, 0);
  299. return NULL;
  300. }
  301. /* New regex-based identity mapping */
  302. regexmatch = sasl_map_domap((char*)user, (char*)user_realm, &base, &filter);
  303. if (regexmatch) {
  304. ids_sasl_user_search(base, scope, filter,
  305. ctrls, attrs, attrsonly,
  306. &entry, &found);
  307. if (found == 1) {
  308. LDAPDebug(LDAP_DEBUG_TRACE, "sasl user search found this entry: dn:%s, "
  309. "matching filter=%s\n", entry->e_sdn.dn, filter, 0);
  310. } else if (found == 0) {
  311. LDAPDebug(LDAP_DEBUG_TRACE, "sasl user search found no entries matching "
  312. "filter=%s\n", filter, 0, 0);
  313. } else {
  314. LDAPDebug(LDAP_DEBUG_TRACE, "sasl user search found more than one entry "
  315. "matching filter=%s\n", filter, 0, 0);
  316. if (entry) {
  317. slapi_entry_free(entry);
  318. entry = NULL;
  319. }
  320. }
  321. /* Free the filter etc */
  322. slapi_ch_free_string(&base);
  323. slapi_ch_free_string(&filter);
  324. }
  325. return entry;
  326. }
  327. static char *buf2str(const char *buf, unsigned buflen)
  328. {
  329. char *ret;
  330. ret = (char*)slapi_ch_malloc(buflen+1);
  331. memcpy(ret, buf, buflen);
  332. ret[buflen] = '\0';
  333. return ret;
  334. }
  335. /* Note that in this sasl1 API, when it says 'authid' it really means 'authzid'. */
  336. static int ids_sasl_canon_user(
  337. sasl_conn_t *conn,
  338. void *context,
  339. const char *userbuf, unsigned ulen,
  340. unsigned flags, const char *user_realm,
  341. char *out_user, unsigned out_umax, unsigned *out_ulen
  342. )
  343. {
  344. struct propctx *propctx = sasl_auxprop_getctx(conn);
  345. Slapi_Entry *entry = NULL;
  346. Slapi_DN *sdn = NULL;
  347. char *pw = NULL;
  348. char *user = NULL;
  349. char *mech = NULL;
  350. const char *dn;
  351. int isroot = 0;
  352. char *clear = NULL;
  353. int returnvalue = SASL_FAIL;
  354. user = buf2str(userbuf, ulen);
  355. if (user == NULL) {
  356. goto fail;
  357. }
  358. LDAPDebug(LDAP_DEBUG_TRACE,
  359. "ids_sasl_canon_user(user=%s, realm=%s)\n",
  360. user, user_realm ? user_realm : "", 0);
  361. sasl_getprop(conn, SASL_MECHNAME, (const void**)&mech);
  362. if (mech == NULL) {
  363. LDAPDebug0Args(LDAP_DEBUG_TRACE, "Unable to read SASL mechanism while "
  364. "canonifying user.\n")
  365. goto fail;
  366. }
  367. if (strncasecmp(user, "dn:", 3) == 0) {
  368. sdn = slapi_sdn_new();
  369. slapi_sdn_set_dn_byval(sdn, user+3);
  370. isroot = slapi_dn_isroot(slapi_sdn_get_ndn(sdn));
  371. }
  372. if (isroot) {
  373. /* special case directory manager */
  374. dn = slapi_sdn_get_ndn(sdn);
  375. pw = config_get_rootpw();
  376. *out_ulen = PR_snprintf(out_user, out_umax, "dn: %s", dn);
  377. } else if (strcasecmp(mech, "ANONYMOUS") == 0) {
  378. /* SASL doesn't allow us to set the username to an empty string,
  379. * so we just set it to anonymous. */
  380. dn = "anonymous";
  381. PL_strncpyz(out_user, dn, out_umax);
  382. /* the length of out_user needs to be set for Cyrus SASL */
  383. *out_ulen = strlen(out_user);
  384. } else {
  385. /* map the sasl username into an entry */
  386. entry = ids_sasl_user_to_entry(conn, context, user, user_realm);
  387. if (entry == NULL) {
  388. /* Specific return value is supposed to be set instead of
  389. an generic error (SASL_FAIL) for Cyrus SASL */
  390. returnvalue = SASL_NOAUTHZ;
  391. goto fail;
  392. }
  393. dn = slapi_entry_get_ndn(entry);
  394. pw = slapi_entry_attr_get_charptr(entry, "userpassword");
  395. *out_ulen = PR_snprintf(out_user, out_umax, "dn: %s", dn);
  396. }
  397. /* Need to set dn property to an empty string for the ANONYMOUS mechanism. This
  398. * property determines what the bind identity will be if authentication succeeds. */
  399. if (strcasecmp(mech, "ANONYMOUS") == 0) {
  400. if (prop_set(propctx, "dn", "", -1) != 0) {
  401. LDAPDebug(LDAP_DEBUG_TRACE, "prop_set(dn) failed\n", 0, 0, 0);
  402. goto fail;
  403. }
  404. } else if (prop_set(propctx, "dn", dn, -1) != 0) {
  405. LDAPDebug(LDAP_DEBUG_TRACE, "prop_set(dn) failed\n", 0, 0, 0);
  406. goto fail;
  407. }
  408. /* We need to check if the first character of pw is an opening
  409. * brace since strstr will simply return it's first argument if
  410. * it is an empty string. */
  411. if (pw && (*pw == '{')) {
  412. if (strchr( pw, '}' )) {
  413. /* This password is stored in a non-cleartext format.
  414. * Any SASL mechanism that actually needs the
  415. * password is going to fail. We should print a warning
  416. * to aid in troubleshooting. */
  417. LDAPDebug(LDAP_DEBUG_TRACE, "Warning: Detected a sasl bind attempt by an "
  418. "entry whose password is stored in a non-cleartext format. This "
  419. "will not work for mechanisms which require a cleartext password "
  420. "such as DIGEST-MD5 and CRAM-MD5.\n", 0, 0, 0);
  421. } else {
  422. /* This password doesn't have a storage prefix but
  423. * just happens to start with the '{' character. We'll
  424. * assume that it's just a cleartext password without
  425. * the proper storage prefix. */
  426. clear = pw;
  427. }
  428. } else {
  429. /* This password has no storage prefix, or the password is empty */
  430. clear = pw;
  431. }
  432. if (clear) {
  433. /* older versions of sasl do not have SASL_AUX_PASSWORD_PROP, so omit it */
  434. #ifdef SASL_AUX_PASSWORD_PROP
  435. if (prop_set(propctx, SASL_AUX_PASSWORD_PROP, clear, -1) != 0) {
  436. /* Failure is benign here because some mechanisms don't support this property */
  437. /*LDAPDebug(LDAP_DEBUG_TRACE, "prop_set(userpassword) failed\n", 0, 0, 0);
  438. goto fail */ ;
  439. }
  440. #endif /* SASL_AUX_PASSWORD_PROP */
  441. if (prop_set(propctx, SASL_AUX_PASSWORD, clear, -1) != 0) {
  442. /* Failure is benign here because some mechanisms don't support this property */
  443. /*LDAPDebug(LDAP_DEBUG_TRACE, "prop_set(userpassword) failed\n", 0, 0, 0);
  444. goto fail */ ;
  445. }
  446. }
  447. slapi_entry_free(entry);
  448. slapi_ch_free((void**)&user);
  449. slapi_ch_free((void**)&pw);
  450. slapi_sdn_free(&sdn);
  451. return SASL_OK;
  452. fail:
  453. slapi_entry_free(entry);
  454. slapi_ch_free((void**)&user);
  455. slapi_ch_free((void**)&pw);
  456. slapi_sdn_free(&sdn);
  457. return returnvalue;
  458. }
  459. static int ids_sasl_getpluginpath(sasl_conn_t *conn, const char **path)
  460. {
  461. /* Try to get path from config, otherwise check for SASL_PATH environment
  462. * variable. If neither of these are set, default to /usr/lib64/sasl2 on
  463. * 64-bit Linux machines, and /usr/lib/sasl2 on all other platforms.
  464. */
  465. char *pluginpath = config_get_saslpath();
  466. if ((!pluginpath) || (*pluginpath == '\0')) {
  467. if (!(pluginpath = getenv("SASL_PATH"))) {
  468. #if defined(LINUX) && defined(__LP64__)
  469. pluginpath = "/usr/lib64/sasl2";
  470. #else
  471. pluginpath = "/usr/lib/sasl2";
  472. #endif
  473. }
  474. }
  475. *path = pluginpath;
  476. return SASL_OK;
  477. }
  478. static sasl_callback_t ids_sasl_callbacks[] =
  479. {
  480. {
  481. SASL_CB_GETOPT,
  482. (IFP) ids_sasl_getopt,
  483. NULL
  484. },
  485. {
  486. SASL_CB_LOG,
  487. (IFP) ids_sasl_log,
  488. NULL
  489. },
  490. {
  491. SASL_CB_PROXY_POLICY,
  492. (IFP) ids_sasl_proxy_policy,
  493. NULL
  494. },
  495. {
  496. SASL_CB_CANON_USER,
  497. (IFP) ids_sasl_canon_user,
  498. NULL
  499. },
  500. {
  501. SASL_CB_GETPATH,
  502. (IFP) ids_sasl_getpluginpath,
  503. NULL
  504. },
  505. {
  506. SASL_CB_LIST_END,
  507. (IFP) NULL,
  508. NULL
  509. }
  510. };
  511. static const char *dn_propnames[] = { "dn", 0 };
  512. /*
  513. * initialize the sasl library
  514. */
  515. int ids_sasl_init(void)
  516. {
  517. static int inited = 0;
  518. int result;
  519. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_init\n", 0, 0, 0 );
  520. PR_ASSERT(inited == 0);
  521. inited = 1;
  522. serverfqdn = get_localhost_DNS();
  523. LDAPDebug(LDAP_DEBUG_TRACE, "sasl service fqdn is: %s\n",
  524. serverfqdn, 0, 0);
  525. /* get component ID for internal operations */
  526. generate_component_id();
  527. /* Set SASL memory allocation callbacks */
  528. sasl_set_alloc(
  529. (sasl_malloc_t *)slapi_ch_malloc,
  530. (sasl_calloc_t *)slapi_ch_calloc,
  531. (sasl_realloc_t *)slapi_ch_realloc,
  532. (sasl_free_t *)nssasl_free );
  533. /* Set SASL mutex callbacks */
  534. sasl_set_mutex(
  535. (sasl_mutex_alloc_t *)nssasl_mutex_alloc,
  536. (sasl_mutex_lock_t *)nssasl_mutex_lock,
  537. (sasl_mutex_unlock_t *)nssasl_mutex_unlock,
  538. (sasl_mutex_free_t *)nssasl_mutex_free);
  539. result = sasl_server_init(ids_sasl_callbacks, "iDS");
  540. if (result != SASL_OK) {
  541. LDAPDebug(LDAP_DEBUG_TRACE, "failed to initialize sasl library\n",
  542. 0, 0, 0);
  543. return result;
  544. }
  545. result = sasl_auxprop_add_plugin("iDS", ids_auxprop_plug_init);
  546. LDAPDebug( LDAP_DEBUG_TRACE, "<= ids_sasl_init\n", 0, 0, 0 );
  547. return result;
  548. }
  549. /*
  550. * create a sasl server connection
  551. */
  552. void ids_sasl_server_new(Connection *conn)
  553. {
  554. int rc;
  555. sasl_conn_t *sasl_conn = NULL;
  556. struct propctx *propctx;
  557. sasl_security_properties_t secprops = {0};
  558. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_server_new (%s)\n", serverfqdn, 0, 0 );
  559. rc = sasl_server_new("ldap",
  560. serverfqdn,
  561. NULL, /* user_realm */
  562. NULL, /* iplocalport */
  563. NULL, /* ipremoteport */
  564. ids_sasl_callbacks,
  565. SASL_SUCCESS_DATA,
  566. &sasl_conn);
  567. if (rc != SASL_OK) {
  568. LDAPDebug(LDAP_DEBUG_ANY, "sasl_server_new: %s\n",
  569. sasl_errstring(rc, NULL, NULL), 0, 0);
  570. }
  571. if (rc == SASL_OK) {
  572. propctx = sasl_auxprop_getctx(sasl_conn);
  573. if (propctx != NULL) {
  574. prop_request(propctx, dn_propnames);
  575. }
  576. }
  577. /* Enable security for this connection */
  578. secprops.maxbufsize = 2048; /* DBDB: hack */
  579. secprops.max_ssf = 0xffffffff;
  580. secprops.min_ssf = config_get_minssf();
  581. /* If anonymous access is disabled, set the appropriate flag */
  582. if (config_get_anon_access_switch() != SLAPD_ANON_ACCESS_ON) {
  583. secprops.security_flags = SASL_SEC_NOANONYMOUS;
  584. }
  585. rc = sasl_setprop(sasl_conn, SASL_SEC_PROPS, &secprops);
  586. if (rc != SASL_OK) {
  587. LDAPDebug(LDAP_DEBUG_ANY, "sasl_setprop: %s\n",
  588. sasl_errstring(rc, NULL, NULL), 0, 0);
  589. }
  590. conn->c_sasl_conn = sasl_conn;
  591. conn->c_sasl_ssf = 0;
  592. LDAPDebug( LDAP_DEBUG_TRACE, "<= ids_sasl_server_new\n", 0, 0, 0 );
  593. return;
  594. }
  595. /*
  596. * return sasl mechanisms available on this connection.
  597. * caller must free returned charray.
  598. */
  599. char **ids_sasl_listmech(Slapi_PBlock *pb)
  600. {
  601. char **ret, **others;
  602. const char *str;
  603. char *dupstr;
  604. sasl_conn_t *sasl_conn;
  605. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_listmech\n", 0, 0, 0 );
  606. PR_ASSERT(pb);
  607. /* hard-wired mechanisms and slapi plugin registered mechanisms */
  608. ret = slapi_get_supported_saslmechanisms_copy();
  609. if (pb->pb_conn == NULL) return ret;
  610. sasl_conn = (sasl_conn_t*)pb->pb_conn->c_sasl_conn;
  611. if (sasl_conn == NULL) return ret;
  612. /* sasl library mechanisms are connection dependent */
  613. PR_Lock(pb->pb_conn->c_mutex);
  614. if (sasl_listmech(sasl_conn,
  615. NULL, /* username */
  616. "", ",", "",
  617. &str, NULL, NULL) == SASL_OK) {
  618. LDAPDebug(LDAP_DEBUG_TRACE, "sasl library mechs: %s\n", str, 0, 0);
  619. /* merge into result set */
  620. dupstr = slapi_ch_strdup(str);
  621. others = slapi_str2charray_ext(dupstr, ",", 0 /* don't list duplicate mechanisms */);
  622. charray_merge(&ret, others, 1);
  623. charray_free(others);
  624. slapi_ch_free((void**)&dupstr);
  625. }
  626. PR_Unlock(pb->pb_conn->c_mutex);
  627. LDAPDebug( LDAP_DEBUG_TRACE, "<= ids_sasl_listmech\n", 0, 0, 0 );
  628. return ret;
  629. }
  630. /*
  631. * Determine whether a given sasl mechanism is supported by
  632. * this sasl connection. Returns true/false.
  633. * NOTE: caller must lock pb->pb_conn->c_mutex
  634. */
  635. static int
  636. ids_sasl_mech_supported(Slapi_PBlock *pb, const char *mech)
  637. {
  638. int i, ret = 0;
  639. char **mechs;
  640. char *dupstr;
  641. const char *str;
  642. int sasl_result = 0;
  643. sasl_conn_t *sasl_conn = (sasl_conn_t *)pb->pb_conn->c_sasl_conn;
  644. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_mech_supported\n", 0, 0, 0 );
  645. /* sasl_listmech is not thread-safe - caller must lock pb_conn */
  646. sasl_result = sasl_listmech(sasl_conn,
  647. NULL, /* username */
  648. "", ",", "",
  649. &str, NULL, NULL);
  650. if (sasl_result != SASL_OK) {
  651. return 0;
  652. }
  653. dupstr = slapi_ch_strdup(str);
  654. mechs = slapi_str2charray(dupstr, ",");
  655. for (i = 0; mechs[i] != NULL; i++) {
  656. if (strcasecmp(mech, mechs[i]) == 0) {
  657. ret = 1;
  658. break;
  659. }
  660. }
  661. charray_free(mechs);
  662. slapi_ch_free((void**)&dupstr);
  663. LDAPDebug( LDAP_DEBUG_TRACE, "<= ids_sasl_mech_supported\n", 0, 0, 0 );
  664. return ret;
  665. }
  666. /*
  667. * do a sasl bind and return a result
  668. */
  669. void ids_sasl_check_bind(Slapi_PBlock *pb)
  670. {
  671. int rc, isroot;
  672. long t;
  673. sasl_conn_t *sasl_conn;
  674. struct propctx *propctx;
  675. sasl_ssf_t *ssfp;
  676. char *activemech = NULL, *mech = NULL;
  677. char *username, *dn = NULL;
  678. const char *normdn = NULL;
  679. Slapi_DN *sdn = NULL;
  680. const char *sdata, *errstr;
  681. unsigned slen;
  682. int continuing = 0;
  683. int pwresponse_requested = 0;
  684. LDAPControl **ctrls;
  685. struct berval bvr, *cred;
  686. struct propval dnval[2];
  687. char authtype[256]; /* >26 (strlen(SLAPD_AUTH_SASL)+SASL_MECHNAMEMAX+1) */
  688. Slapi_Entry *bind_target_entry = NULL, *referral = NULL;
  689. Slapi_Backend *be = NULL;
  690. char errorbuf[BUFSIZ];
  691. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_check_bind\n", 0, 0, 0 );
  692. PR_ASSERT(pb);
  693. PR_ASSERT(pb->pb_conn);
  694. PR_Lock(pb->pb_conn->c_mutex); /* BIG LOCK */
  695. continuing = pb->pb_conn->c_flags & CONN_FLAG_SASL_CONTINUE;
  696. pb->pb_conn->c_flags &= ~CONN_FLAG_SASL_CONTINUE; /* reset flag */
  697. sasl_conn = (sasl_conn_t*)pb->pb_conn->c_sasl_conn;
  698. if (sasl_conn == NULL) {
  699. PR_Unlock(pb->pb_conn->c_mutex);
  700. send_ldap_result( pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  701. "sasl library unavailable", 0, NULL );
  702. return;
  703. }
  704. slapi_pblock_get(pb, SLAPI_BIND_SASLMECHANISM, &mech);
  705. slapi_pblock_get(pb, SLAPI_BIND_CREDENTIALS, &cred);
  706. slapi_pblock_get(pb, SLAPI_PWPOLICY, &pwresponse_requested);
  707. PR_ASSERT(mech);
  708. PR_ASSERT(cred);
  709. /* Work around a bug in the sasl library. We've told the
  710. * library that CRAM-MD5 is disabled, but it gives us a
  711. * different error code to SASL_NOMECH. Must be called
  712. * while holding the pb_conn lock
  713. */
  714. if (!ids_sasl_mech_supported(pb, mech)) {
  715. rc = SASL_NOMECH;
  716. goto sasl_check_result;
  717. }
  718. /* can't do any harm */
  719. if (cred->bv_len == 0) cred->bv_val = NULL;
  720. if (continuing) {
  721. /*
  722. * RFC 2251: a client may abort a sasl bind negotiation by
  723. * sending a bindrequest with a different value in the
  724. * mechanism field.
  725. */
  726. sasl_getprop(sasl_conn, SASL_MECHNAME, (const void**)&activemech);
  727. if (activemech == NULL) {
  728. LDAPDebug(LDAP_DEBUG_TRACE, "could not get active sasl mechanism\n", 0, 0, 0);
  729. goto sasl_start;
  730. }
  731. if (strcasecmp(activemech, mech) != 0) {
  732. LDAPDebug(LDAP_DEBUG_TRACE, "sasl mechanisms differ: active=%s current=%s\n", 0, 0, 0);
  733. goto sasl_start;
  734. }
  735. rc = sasl_server_step(sasl_conn,
  736. cred->bv_val, cred->bv_len,
  737. &sdata, &slen);
  738. goto sasl_check_result;
  739. }
  740. sasl_start:
  741. /* Check if we are already authenticated via sasl. If so,
  742. * dispose of the current sasl_conn and create a new one
  743. * using the new mechanism. We also need to do this if the
  744. * mechanism changed in the middle of the SASL authentication
  745. * process. */
  746. if ((pb->pb_conn->c_flags & CONN_FLAG_SASL_COMPLETE) || continuing) {
  747. Slapi_Operation *operation;
  748. slapi_pblock_get( pb, SLAPI_OPERATION, &operation);
  749. slapi_log_error(SLAPI_LOG_CONNS, "ids_sasl_check_bind",
  750. "cleaning up sasl IO conn=%" NSPRIu64 " op=%d complete=%d continuing=%d\n",
  751. pb->pb_conn->c_connid, operation->o_opid,
  752. (pb->pb_conn->c_flags & CONN_FLAG_SASL_COMPLETE), continuing);
  753. /* reset flag */
  754. pb->pb_conn->c_flags &= ~CONN_FLAG_SASL_COMPLETE;
  755. /* remove any SASL I/O from the connection */
  756. connection_set_io_layer_cb(pb->pb_conn, NULL, sasl_io_cleanup, NULL);
  757. /* dispose of sasl_conn and create a new sasl_conn */
  758. sasl_dispose(&sasl_conn);
  759. ids_sasl_server_new(pb->pb_conn);
  760. sasl_conn = (sasl_conn_t*)pb->pb_conn->c_sasl_conn;
  761. if (sasl_conn == NULL) {
  762. send_ldap_result( pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  763. "sasl library unavailable", 0, NULL );
  764. PR_Unlock(pb->pb_conn->c_mutex); /* BIG LOCK */
  765. return;
  766. }
  767. }
  768. rc = sasl_server_start(sasl_conn, mech,
  769. cred->bv_val, cred->bv_len,
  770. &sdata, &slen);
  771. sasl_check_result:
  772. switch (rc) {
  773. case SASL_OK: /* complete */
  774. /* retrieve the authenticated username */
  775. if (sasl_getprop(sasl_conn, SASL_USERNAME,
  776. (const void**)&username) != SASL_OK) {
  777. PR_Unlock(pb->pb_conn->c_mutex); /* BIG LOCK */
  778. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  779. "could not obtain sasl username", 0, NULL);
  780. break;
  781. }
  782. LDAPDebug(LDAP_DEBUG_TRACE, "sasl authenticated mech=%s user=%s\n",
  783. mech, username, 0);
  784. /*
  785. * Retrieve the DN corresponding to the authenticated user.
  786. * This should have been set by the user canon callback
  787. * in an auxiliary property called "dn".
  788. */
  789. propctx = sasl_auxprop_getctx(sasl_conn);
  790. if (prop_getnames(propctx, dn_propnames, dnval) == 1) {
  791. if (dnval[0].values && dnval[0].values[0]) {
  792. dn = slapi_ch_smprintf("%s", dnval[0].values[0]);
  793. }
  794. }
  795. if (dn == NULL) {
  796. PR_Unlock(pb->pb_conn->c_mutex); /* BIG LOCK */
  797. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  798. "could not get auth dn from sasl", 0, NULL);
  799. break;
  800. }
  801. /* clean up already set TARGET */
  802. slapi_pblock_get(pb, SLAPI_BIND_TARGET_SDN, &sdn);
  803. slapi_sdn_free(&sdn);
  804. sdn = slapi_sdn_new_dn_passin(dn);
  805. normdn = slapi_sdn_get_dn(sdn);
  806. slapi_pblock_set( pb, SLAPI_BIND_TARGET_SDN, sdn );
  807. if ((sasl_getprop(sasl_conn, SASL_SSF,
  808. (const void**)&ssfp) == SASL_OK) && (*ssfp > 0)) {
  809. LDAPDebug(LDAP_DEBUG_TRACE, "sasl ssf=%u\n", (unsigned)*ssfp, 0, 0);
  810. } else {
  811. *ssfp = 0;
  812. }
  813. /* Set a flag to signify that sasl bind is complete */
  814. pb->pb_conn->c_flags |= CONN_FLAG_SASL_COMPLETE;
  815. /* note - we set this here in case there are pre-bind
  816. plugins that want to know what the negotiated
  817. ssf is - but this happens before we actually set
  818. up the socket for SASL encryption - so one
  819. consequence is that we attempt to do sasl
  820. encryption on the connection after the pre-bind
  821. plugin has been called, and sasl encryption fails
  822. and the operation returns an error */
  823. pb->pb_conn->c_sasl_ssf = (unsigned)*ssfp;
  824. /* set the connection bind credentials */
  825. PR_snprintf(authtype, sizeof(authtype), "%s%s", SLAPD_AUTH_SASL, mech);
  826. /* normdn is consumed by bind_credentials_set_nolock */
  827. bind_credentials_set_nolock(pb->pb_conn, authtype,
  828. slapi_ch_strdup(normdn),
  829. NULL, NULL, NULL, bind_target_entry);
  830. PR_Unlock(pb->pb_conn->c_mutex); /* BIG LOCK */
  831. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) != 0){
  832. break;
  833. }
  834. isroot = slapi_dn_isroot(normdn);
  835. if (!isroot )
  836. {
  837. /* check if the account is locked */
  838. bind_target_entry = get_entry(pb, normdn);
  839. if ( bind_target_entry == NULL )
  840. {
  841. goto out;
  842. }
  843. if ( slapi_check_account_lock(pb, bind_target_entry, pwresponse_requested, 1, 1) == 1) {
  844. goto out;
  845. }
  846. }
  847. /* set the auth response control if requested */
  848. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &ctrls);
  849. if (slapi_control_present(ctrls, LDAP_CONTROL_AUTH_REQUEST,
  850. NULL, NULL)) {
  851. slapi_add_auth_response_control(pb, normdn);
  852. }
  853. if (slapi_mapping_tree_select(pb, &be, &referral, errorbuf) != LDAP_SUCCESS) {
  854. send_nobackend_ldap_result( pb );
  855. be = NULL;
  856. LDAPDebug( LDAP_DEBUG_TRACE, "<= ids_sasl_check_bind\n", 0, 0, 0 );
  857. return;
  858. }
  859. if (referral) {
  860. send_referrals_from_entry(pb,referral);
  861. goto out;
  862. }
  863. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  864. slapi_pblock_set( pb, SLAPI_PLUGIN, be->be_database );
  865. set_db_default_result_handlers(pb);
  866. /* check password expiry */
  867. if (!isroot) {
  868. int pwrc;
  869. pwrc = need_new_pw(pb, &t, bind_target_entry, pwresponse_requested);
  870. switch (pwrc) {
  871. case 1:
  872. slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
  873. break;
  874. case 2:
  875. slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRING, t);
  876. break;
  877. case -1:
  878. goto out;
  879. default:
  880. break;
  881. }
  882. }
  883. /* attach the sasl data */
  884. if (slen != 0) {
  885. bvr.bv_val = (char*)sdata;
  886. bvr.bv_len = slen;
  887. slapi_pblock_set(pb, SLAPI_BIND_RET_SASLCREDS, &bvr);
  888. }
  889. /* see if we negotiated a security layer */
  890. if (*ssfp > 0) {
  891. /* Enable SASL I/O on the connection */
  892. PR_Lock(pb->pb_conn->c_mutex);
  893. connection_set_io_layer_cb(pb->pb_conn, sasl_io_enable, NULL, NULL);
  894. PR_Unlock(pb->pb_conn->c_mutex);
  895. }
  896. /* send successful result */
  897. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  898. /* remove the sasl data from the pblock */
  899. slapi_pblock_set(pb, SLAPI_BIND_RET_SASLCREDS, NULL);
  900. break;
  901. case SASL_CONTINUE: /* another step needed */
  902. pb->pb_conn->c_flags |= CONN_FLAG_SASL_CONTINUE;
  903. PR_Unlock(pb->pb_conn->c_mutex); /* BIG LOCK */
  904. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) != 0){
  905. break;
  906. }
  907. /* attach the sasl data */
  908. bvr.bv_val = (char*)sdata;
  909. bvr.bv_len = slen;
  910. slapi_pblock_set(pb, SLAPI_BIND_RET_SASLCREDS, &bvr);
  911. /* send continuation result */
  912. send_ldap_result( pb, LDAP_SASL_BIND_IN_PROGRESS, NULL,
  913. NULL, 0, NULL );
  914. /* remove the sasl data from the pblock */
  915. slapi_pblock_set(pb, SLAPI_BIND_RET_SASLCREDS, NULL);
  916. break;
  917. case SASL_NOMECH:
  918. PR_Unlock(pb->pb_conn->c_mutex); /* BIG LOCK */
  919. send_ldap_result(pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  920. "sasl mechanism not supported", 0, NULL);
  921. break;
  922. default: /* other error */
  923. errstr = sasl_errdetail(sasl_conn);
  924. PR_Unlock(pb->pb_conn->c_mutex); /* BIG LOCK */
  925. send_ldap_result(pb, LDAP_INVALID_CREDENTIALS, NULL,
  926. (char*)errstr, 0, NULL);
  927. break;
  928. }
  929. out:
  930. if (referral)
  931. slapi_entry_free(referral);
  932. if (be)
  933. slapi_be_Unlock(be);
  934. if (bind_target_entry)
  935. slapi_entry_free(bind_target_entry);
  936. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_check_bind\n", 0, 0, 0 );
  937. return;
  938. }