saslbind.c 34 KB

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