saslbind.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. } else if (strcasecmp(mech, "ANONYMOUS") == 0) {
  377. /* SASL doesn't allow us to set the username to an empty string,
  378. * so we just set it to anonymous. */
  379. dn = "anonymous";
  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. }
  392. /* Need to set dn property to an empty string for the ANONYMOUS mechanism. This
  393. * property determines what the bind identity will be if authentication succeeds. */
  394. if (strcasecmp(mech, "ANONYMOUS") == 0) {
  395. if (prop_set(propctx, "dn", "", -1) != 0) {
  396. LDAPDebug(LDAP_DEBUG_TRACE, "prop_set(dn) failed\n", 0, 0, 0);
  397. goto fail;
  398. }
  399. } else if (prop_set(propctx, "dn", dn, -1) != 0) {
  400. LDAPDebug(LDAP_DEBUG_TRACE, "prop_set(dn) failed\n", 0, 0, 0);
  401. goto fail;
  402. }
  403. /* We need to check if the first character of pw is an opening
  404. * brace since strstr will simply return it's first argument if
  405. * it is an empty string. */
  406. if (pw && (*pw == '{')) {
  407. if (strchr( pw, '}' )) {
  408. /* This password is stored in a non-cleartext format.
  409. * Any SASL mechanism that actually needs the
  410. * password is going to fail. We should print a warning
  411. * to aid in troubleshooting. */
  412. LDAPDebug(LDAP_DEBUG_TRACE, "Warning: Detected a sasl bind attempt by an "
  413. "entry whose password is stored in a non-cleartext format. This "
  414. "will not work for mechanisms which require a cleartext password "
  415. "such as DIGEST-MD5 and CRAM-MD5.\n", 0, 0, 0);
  416. } else {
  417. /* This password doesn't have a storage prefix but
  418. * just happens to start with the '{' character. We'll
  419. * assume that it's just a cleartext password without
  420. * the proper storage prefix. */
  421. clear = pw;
  422. }
  423. } else {
  424. /* This password has no storage prefix, or the password is empty */
  425. clear = pw;
  426. }
  427. if (clear) {
  428. /* older versions of sasl do not have SASL_AUX_PASSWORD_PROP, so omit it */
  429. #ifdef SASL_AUX_PASSWORD_PROP
  430. if (prop_set(propctx, SASL_AUX_PASSWORD_PROP, clear, -1) != 0) {
  431. /* Failure is benign here because some mechanisms don't support this property */
  432. /*LDAPDebug(LDAP_DEBUG_TRACE, "prop_set(userpassword) failed\n", 0, 0, 0);
  433. goto fail */ ;
  434. }
  435. #endif /* SASL_AUX_PASSWORD_PROP */
  436. if (prop_set(propctx, SASL_AUX_PASSWORD, clear, -1) != 0) {
  437. /* Failure is benign here because some mechanisms don't support this property */
  438. /*LDAPDebug(LDAP_DEBUG_TRACE, "prop_set(userpassword) failed\n", 0, 0, 0);
  439. goto fail */ ;
  440. }
  441. }
  442. /* TODO: canonicalize */
  443. PL_strncpyz(out_user, dn, out_umax);
  444. /* the length of out_user needs to be set for Cyrus SASL */
  445. *out_ulen = strlen(out_user);
  446. slapi_entry_free(entry);
  447. slapi_ch_free((void**)&user);
  448. slapi_ch_free((void**)&pw);
  449. slapi_sdn_free(&sdn);
  450. return SASL_OK;
  451. fail:
  452. slapi_entry_free(entry);
  453. slapi_ch_free((void**)&user);
  454. slapi_ch_free((void**)&pw);
  455. slapi_sdn_free(&sdn);
  456. return returnvalue;
  457. }
  458. static int ids_sasl_getpluginpath(sasl_conn_t *conn, const char **path)
  459. {
  460. /* Try to get path from config, otherwise check for SASL_PATH environment
  461. * variable. If neither of these are set, default to /usr/lib64/sasl2 on
  462. * 64-bit Linux machines, and /usr/lib/sasl2 on all other platforms.
  463. */
  464. char *pluginpath = config_get_saslpath();
  465. if ((!pluginpath) || (*pluginpath == '\0')) {
  466. if (!(pluginpath = getenv("SASL_PATH"))) {
  467. #if defined(LINUX) && defined(__LP64__)
  468. pluginpath = "/usr/lib64/sasl2";
  469. #else
  470. pluginpath = "/usr/lib/sasl2";
  471. #endif
  472. }
  473. }
  474. *path = pluginpath;
  475. return SASL_OK;
  476. }
  477. static sasl_callback_t ids_sasl_callbacks[] =
  478. {
  479. {
  480. SASL_CB_GETOPT,
  481. (IFP) ids_sasl_getopt,
  482. NULL
  483. },
  484. {
  485. SASL_CB_LOG,
  486. (IFP) ids_sasl_log,
  487. NULL
  488. },
  489. {
  490. SASL_CB_PROXY_POLICY,
  491. (IFP) ids_sasl_proxy_policy,
  492. NULL
  493. },
  494. {
  495. SASL_CB_CANON_USER,
  496. (IFP) ids_sasl_canon_user,
  497. NULL
  498. },
  499. {
  500. SASL_CB_GETPATH,
  501. (IFP) ids_sasl_getpluginpath,
  502. NULL
  503. },
  504. {
  505. SASL_CB_LIST_END,
  506. (IFP) NULL,
  507. NULL
  508. }
  509. };
  510. static const char *dn_propnames[] = { "dn", 0 };
  511. /*
  512. * initialize the sasl library
  513. */
  514. int ids_sasl_init(void)
  515. {
  516. static int inited = 0;
  517. int result;
  518. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_init\n", 0, 0, 0 );
  519. PR_ASSERT(inited == 0);
  520. inited = 1;
  521. serverfqdn = get_localhost_DNS();
  522. LDAPDebug(LDAP_DEBUG_TRACE, "sasl service fqdn is: %s\n",
  523. serverfqdn, 0, 0);
  524. /* get component ID for internal operations */
  525. generate_component_id();
  526. /* Set SASL memory allocation callbacks */
  527. sasl_set_alloc(
  528. (sasl_malloc_t *)slapi_ch_malloc,
  529. (sasl_calloc_t *)slapi_ch_calloc,
  530. (sasl_realloc_t *)slapi_ch_realloc,
  531. (sasl_free_t *)nssasl_free );
  532. /* Set SASL mutex callbacks */
  533. sasl_set_mutex(
  534. (sasl_mutex_alloc_t *)nssasl_mutex_alloc,
  535. (sasl_mutex_lock_t *)nssasl_mutex_lock,
  536. (sasl_mutex_unlock_t *)nssasl_mutex_unlock,
  537. (sasl_mutex_free_t *)nssasl_mutex_free);
  538. result = sasl_server_init(ids_sasl_callbacks, "iDS");
  539. if (result != SASL_OK) {
  540. LDAPDebug(LDAP_DEBUG_TRACE, "failed to initialize sasl library\n",
  541. 0, 0, 0);
  542. return result;
  543. }
  544. result = sasl_auxprop_add_plugin("iDS", ids_auxprop_plug_init);
  545. LDAPDebug( LDAP_DEBUG_TRACE, "<= ids_sasl_init\n", 0, 0, 0 );
  546. return result;
  547. }
  548. /*
  549. * create a sasl server connection
  550. */
  551. void ids_sasl_server_new(Connection *conn)
  552. {
  553. int rc;
  554. sasl_conn_t *sasl_conn = NULL;
  555. struct propctx *propctx;
  556. sasl_security_properties_t secprops = {0};
  557. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_server_new (%s)\n", serverfqdn, 0, 0 );
  558. rc = sasl_server_new("ldap",
  559. serverfqdn,
  560. NULL, /* user_realm */
  561. NULL, /* iplocalport */
  562. NULL, /* ipremoteport */
  563. ids_sasl_callbacks,
  564. SASL_SUCCESS_DATA,
  565. &sasl_conn);
  566. if (rc != SASL_OK) {
  567. LDAPDebug(LDAP_DEBUG_ANY, "sasl_server_new: %s\n",
  568. sasl_errstring(rc, NULL, NULL), 0, 0);
  569. }
  570. if (rc == SASL_OK) {
  571. propctx = sasl_auxprop_getctx(sasl_conn);
  572. if (propctx != NULL) {
  573. prop_request(propctx, dn_propnames);
  574. }
  575. }
  576. /* Enable security for this connection */
  577. secprops.maxbufsize = 2048; /* DBDB: hack */
  578. secprops.max_ssf = 0xffffffff;
  579. secprops.min_ssf = config_get_minssf();
  580. /* If anonymous access is disabled, set the appropriate flag */
  581. if (!config_get_anon_access_switch()) {
  582. secprops.security_flags = SASL_SEC_NOANONYMOUS;
  583. }
  584. rc = sasl_setprop(sasl_conn, SASL_SEC_PROPS, &secprops);
  585. if (rc != SASL_OK) {
  586. LDAPDebug(LDAP_DEBUG_ANY, "sasl_setprop: %s\n",
  587. sasl_errstring(rc, NULL, NULL), 0, 0);
  588. }
  589. conn->c_sasl_conn = sasl_conn;
  590. conn->c_sasl_ssf = 0;
  591. LDAPDebug( LDAP_DEBUG_TRACE, "<= ids_sasl_server_new\n", 0, 0, 0 );
  592. return;
  593. }
  594. /*
  595. * return sasl mechanisms available on this connection.
  596. * caller must free returned charray.
  597. */
  598. char **ids_sasl_listmech(Slapi_PBlock *pb)
  599. {
  600. char **ret, **others;
  601. const char *str;
  602. char *dupstr;
  603. sasl_conn_t *sasl_conn;
  604. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_listmech\n", 0, 0, 0 );
  605. PR_ASSERT(pb);
  606. /* hard-wired mechanisms and slapi plugin registered mechanisms */
  607. ret = slapi_get_supported_saslmechanisms_copy();
  608. if (pb->pb_conn == NULL) return ret;
  609. sasl_conn = (sasl_conn_t*)pb->pb_conn->c_sasl_conn;
  610. if (sasl_conn == NULL) return ret;
  611. /* sasl library mechanisms are connection dependent */
  612. PR_Lock(pb->pb_conn->c_mutex);
  613. if (sasl_listmech(sasl_conn,
  614. NULL, /* username */
  615. "", ",", "",
  616. &str, NULL, NULL) == SASL_OK) {
  617. LDAPDebug(LDAP_DEBUG_TRACE, "sasl library mechs: %s\n", str, 0, 0);
  618. /* merge into result set */
  619. dupstr = slapi_ch_strdup(str);
  620. others = slapi_str2charray_ext(dupstr, ",", 0 /* don't list duplicate mechanisms */);
  621. charray_merge(&ret, others, 1);
  622. charray_free(others);
  623. slapi_ch_free((void**)&dupstr);
  624. }
  625. PR_Unlock(pb->pb_conn->c_mutex);
  626. LDAPDebug( LDAP_DEBUG_TRACE, "<= ids_sasl_listmech\n", 0, 0, 0 );
  627. return ret;
  628. }
  629. /*
  630. * Determine whether a given sasl mechanism is supported by
  631. * this sasl connection. Returns true/false.
  632. */
  633. static int
  634. ids_sasl_mech_supported(Slapi_PBlock *pb, sasl_conn_t *sasl_conn, const char *mech)
  635. {
  636. int i, ret = 0;
  637. char **mechs;
  638. char *dupstr;
  639. const char *str;
  640. int sasl_result = 0;
  641. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_mech_supported\n", 0, 0, 0 );
  642. /* sasl_listmech is not thread-safe, so we lock here */
  643. PR_Lock(pb->pb_conn->c_mutex);
  644. sasl_result = sasl_listmech(sasl_conn,
  645. NULL, /* username */
  646. "", ",", "",
  647. &str, NULL, NULL);
  648. PR_Unlock(pb->pb_conn->c_mutex);
  649. if (sasl_result != SASL_OK) {
  650. return 0;
  651. }
  652. dupstr = slapi_ch_strdup(str);
  653. mechs = slapi_str2charray(dupstr, ",");
  654. for (i = 0; mechs[i] != NULL; i++) {
  655. if (strcasecmp(mech, mechs[i]) == 0) {
  656. ret = 1;
  657. break;
  658. }
  659. }
  660. charray_free(mechs);
  661. slapi_ch_free((void**)&dupstr);
  662. LDAPDebug( LDAP_DEBUG_TRACE, "<= ids_sasl_mech_supported\n", 0, 0, 0 );
  663. return ret;
  664. }
  665. /*
  666. * do a sasl bind and return a result
  667. */
  668. void ids_sasl_check_bind(Slapi_PBlock *pb)
  669. {
  670. int rc, isroot;
  671. long t;
  672. sasl_conn_t *sasl_conn;
  673. struct propctx *propctx;
  674. sasl_ssf_t *ssfp;
  675. char *activemech = NULL, *mech = NULL;
  676. char *username, *dn = NULL;
  677. const char *sdata, *errstr;
  678. unsigned slen;
  679. int continuing = 0;
  680. int pwresponse_requested = 0;
  681. LDAPControl **ctrls;
  682. struct berval bvr, *cred;
  683. struct propval dnval[2];
  684. char authtype[256]; /* >26 (strlen(SLAPD_AUTH_SASL)+SASL_MECHNAMEMAX+1) */
  685. Slapi_Entry *bind_target_entry = NULL, *referral = NULL;
  686. Slapi_Backend *be = NULL;
  687. char errorbuf[BUFSIZ];
  688. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_check_bind\n", 0, 0, 0 );
  689. PR_ASSERT(pb);
  690. PR_ASSERT(pb->pb_conn);
  691. continuing = pb->pb_conn->c_flags & CONN_FLAG_SASL_CONTINUE;
  692. pb->pb_conn->c_flags &= ~CONN_FLAG_SASL_CONTINUE; /* reset flag */
  693. sasl_conn = (sasl_conn_t*)pb->pb_conn->c_sasl_conn;
  694. if (sasl_conn == NULL) {
  695. send_ldap_result( pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  696. "sasl library unavailable", 0, NULL );
  697. return;
  698. }
  699. slapi_pblock_get(pb, SLAPI_BIND_SASLMECHANISM, &mech);
  700. slapi_pblock_get(pb, SLAPI_BIND_CREDENTIALS, &cred);
  701. slapi_pblock_get(pb, SLAPI_PWPOLICY, &pwresponse_requested);
  702. PR_ASSERT(mech);
  703. PR_ASSERT(cred);
  704. /* Work around a bug in the sasl library. We've told the
  705. * library that CRAM-MD5 is disabled, but it gives us a
  706. * different error code to SASL_NOMECH.
  707. */
  708. if (!ids_sasl_mech_supported(pb, sasl_conn, mech)) {
  709. rc = SASL_NOMECH;
  710. goto sasl_check_result;
  711. }
  712. /* can't do any harm */
  713. if (cred->bv_len == 0) cred->bv_val = NULL;
  714. if (continuing) {
  715. /*
  716. * RFC 2251: a client may abort a sasl bind negotiation by
  717. * sending a bindrequest with a different value in the
  718. * mechanism field.
  719. */
  720. sasl_getprop(sasl_conn, SASL_MECHNAME, (const void**)&activemech);
  721. if (activemech == NULL) {
  722. LDAPDebug(LDAP_DEBUG_TRACE, "could not get active sasl mechanism\n", 0, 0, 0);
  723. goto sasl_start;
  724. }
  725. if (strcasecmp(activemech, mech) != 0) {
  726. LDAPDebug(LDAP_DEBUG_TRACE, "sasl mechanisms differ: active=%s current=%s\n", 0, 0, 0);
  727. goto sasl_start;
  728. }
  729. rc = sasl_server_step(sasl_conn,
  730. cred->bv_val, cred->bv_len,
  731. &sdata, &slen);
  732. goto sasl_check_result;
  733. }
  734. sasl_start:
  735. /* Check if we are already authenticated via sasl. If so,
  736. * dispose of the current sasl_conn and create a new one
  737. * using the new mechanism. We also need to do this if the
  738. * mechanism changed in the middle of the SASL authentication
  739. * process. */
  740. if ((pb->pb_conn->c_flags & CONN_FLAG_SASL_COMPLETE) || continuing) {
  741. /* reset flag */
  742. pb->pb_conn->c_flags &= ~CONN_FLAG_SASL_COMPLETE;
  743. /* Lock the connection mutex */
  744. PR_Lock(pb->pb_conn->c_mutex);
  745. /* remove any SASL I/O from the connection */
  746. sasl_io_cleanup(pb->pb_conn);
  747. /* dispose of sasl_conn and create a new sasl_conn */
  748. sasl_dispose(&sasl_conn);
  749. ids_sasl_server_new(pb->pb_conn);
  750. sasl_conn = (sasl_conn_t*)pb->pb_conn->c_sasl_conn;
  751. /* Unlock the connection mutex */
  752. PR_Unlock(pb->pb_conn->c_mutex);
  753. if (sasl_conn == NULL) {
  754. send_ldap_result( pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  755. "sasl library unavailable", 0, NULL );
  756. return;
  757. }
  758. }
  759. rc = sasl_server_start(sasl_conn, mech,
  760. cred->bv_val, cred->bv_len,
  761. &sdata, &slen);
  762. sasl_check_result:
  763. switch (rc) {
  764. case SASL_OK: /* complete */
  765. /* Set a flag to signify that sasl bind is complete */
  766. pb->pb_conn->c_flags |= CONN_FLAG_SASL_COMPLETE;
  767. /* retrieve the authenticated username */
  768. if (sasl_getprop(sasl_conn, SASL_USERNAME,
  769. (const void**)&username) != SASL_OK) {
  770. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  771. "could not obtain sasl username", 0, NULL);
  772. break;
  773. }
  774. LDAPDebug(LDAP_DEBUG_TRACE, "sasl authenticated mech=%s user=%s\n",
  775. mech, username, 0);
  776. /*
  777. * Retrieve the DN corresponding to the authenticated user.
  778. * This should have been set by the user canon callback
  779. * in an auxiliary property called "dn".
  780. */
  781. propctx = sasl_auxprop_getctx(sasl_conn);
  782. if (prop_getnames(propctx, dn_propnames, dnval) == 1) {
  783. if (dnval[0].values && dnval[0].values[0]) {
  784. dn = slapi_ch_strdup(dnval[0].values[0]);
  785. }
  786. }
  787. if (dn == NULL) {
  788. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  789. "could not get auth dn from sasl", 0, NULL);
  790. break;
  791. }
  792. slapi_pblock_set( pb, SLAPI_BIND_TARGET, slapi_ch_strdup( dn ) );
  793. if (plugin_call_plugins( pb, SLAPI_PLUGIN_PRE_BIND_FN ) != 0){
  794. break;
  795. }
  796. isroot = slapi_dn_isroot(dn);
  797. if (!isroot )
  798. {
  799. /* check if the account is locked */
  800. bind_target_entry = get_entry(pb, dn);
  801. if ( bind_target_entry == NULL )
  802. {
  803. break;
  804. }
  805. if ( slapi_check_account_lock(pb, bind_target_entry, pwresponse_requested, 1, 1) == 1) {
  806. slapi_entry_free(bind_target_entry);
  807. break;
  808. }
  809. }
  810. /* see if we negotiated a security layer */
  811. if ((sasl_getprop(sasl_conn, SASL_SSF,
  812. (const void**)&ssfp) == SASL_OK) && (*ssfp > 0)) {
  813. LDAPDebug(LDAP_DEBUG_TRACE, "sasl ssf=%u\n", (unsigned)*ssfp, 0, 0);
  814. /* Enable SASL I/O on the connection now */
  815. if (0 != sasl_io_enable(pb->pb_conn) ) {
  816. send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  817. "failed to enable sasl i/o",
  818. 0, NULL);
  819. }
  820. /* Set the SSF in the connection */
  821. pb->pb_conn->c_sasl_ssf = (unsigned)*ssfp;
  822. }
  823. /* set the connection bind credentials */
  824. PR_snprintf(authtype, sizeof(authtype), "%s%s", SLAPD_AUTH_SASL, mech);
  825. bind_credentials_set(pb->pb_conn, authtype, dn,
  826. NULL, NULL, NULL, bind_target_entry);
  827. /* set the auth response control if requested */
  828. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &ctrls);
  829. if (slapi_control_present(ctrls, LDAP_CONTROL_AUTH_REQUEST,
  830. NULL, NULL)) {
  831. slapi_add_auth_response_control(pb, dn);
  832. }
  833. if (slapi_mapping_tree_select(pb, &be, &referral, errorbuf) != LDAP_SUCCESS) {
  834. send_nobackend_ldap_result( pb );
  835. be = NULL;
  836. LDAPDebug( LDAP_DEBUG_TRACE, "<= ids_sasl_check_bind\n", 0, 0, 0 );
  837. return;
  838. }
  839. if (referral) {
  840. send_referrals_from_entry(pb,referral);
  841. goto out;
  842. }
  843. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  844. slapi_pblock_set( pb, SLAPI_PLUGIN, be->be_database );
  845. set_db_default_result_handlers(pb);
  846. /* check password expiry */
  847. if (!isroot) {
  848. int pwrc;
  849. pwrc = need_new_pw(pb, &t, bind_target_entry, pwresponse_requested);
  850. if ( bind_target_entry != NULL ) {
  851. slapi_entry_free(bind_target_entry);
  852. bind_target_entry = NULL;
  853. }
  854. switch (pwrc) {
  855. case 1:
  856. slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
  857. break;
  858. case 2:
  859. slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRING, t);
  860. break;
  861. case -1:
  862. goto out;
  863. default:
  864. break;
  865. }
  866. }
  867. /* attach the sasl data */
  868. if (slen != 0) {
  869. bvr.bv_val = (char*)sdata;
  870. bvr.bv_len = slen;
  871. slapi_pblock_set(pb, SLAPI_BIND_RET_SASLCREDS, &bvr);
  872. }
  873. /* send successful result */
  874. send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  875. /* TODO: enable sasl security layer */
  876. /* remove the sasl data from the pblock */
  877. slapi_pblock_set(pb, SLAPI_BIND_RET_SASLCREDS, NULL);
  878. break;
  879. case SASL_CONTINUE: /* another step needed */
  880. pb->pb_conn->c_flags |= CONN_FLAG_SASL_CONTINUE;
  881. /* attach the sasl data */
  882. bvr.bv_val = (char*)sdata;
  883. bvr.bv_len = slen;
  884. slapi_pblock_set(pb, SLAPI_BIND_RET_SASLCREDS, &bvr);
  885. /* send continuation result */
  886. send_ldap_result( pb, LDAP_SASL_BIND_IN_PROGRESS, NULL,
  887. NULL, 0, NULL );
  888. /* remove the sasl data from the pblock */
  889. slapi_pblock_set(pb, SLAPI_BIND_RET_SASLCREDS, NULL);
  890. break;
  891. case SASL_NOMECH:
  892. send_ldap_result(pb, LDAP_AUTH_METHOD_NOT_SUPPORTED, NULL,
  893. "sasl mechanism not supported", 0, NULL);
  894. break;
  895. default: /* other error */
  896. errstr = sasl_errdetail(sasl_conn);
  897. send_ldap_result(pb, LDAP_INVALID_CREDENTIALS, NULL,
  898. (char*)errstr, 0, NULL);
  899. break;
  900. }
  901. out:
  902. if (referral)
  903. slapi_entry_free(referral);
  904. if (be)
  905. slapi_be_Unlock(be);
  906. LDAPDebug( LDAP_DEBUG_TRACE, "=> ids_sasl_check_bind\n", 0, 0, 0 );
  907. return;
  908. }