saslbind.c 31 KB

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