saslbind.c 33 KB

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