saslbind.c 35 KB

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