pam_ptconfig.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * ptconfig.c - configuration-related code for Pass Through Authentication
  42. *
  43. */
  44. #include <plstr.h>
  45. #include "pam_passthru.h"
  46. #define PAM_PT_CONFIG_FILTER "(objectclass=*)"
  47. /*
  48. * The configuration attributes are contained in the plugin entry e.g.
  49. * cn=PAM Pass Through,cn=plugins,cn=config, or an alternate config area.
  50. *
  51. * Configuration is a two step process. The first pass is a validation step which
  52. * occurs pre-op - check inputs and error out if bad. The second pass actually
  53. * applies the changes to the run time config.
  54. */
  55. static Slapi_DN *_ConfigArea = NULL;
  56. /*
  57. * function prototypes
  58. */
  59. static int pam_passthru_apply_config (Slapi_Entry* e);
  60. /*
  61. * Read and load configuration. Validation will also
  62. * be performed unless skip_validate is set to non-0.
  63. * Returns PAM_PASSTHRU_SUCCESS if all is well.
  64. */
  65. int
  66. pam_passthru_load_config(int skip_validate)
  67. {
  68. int status = PAM_PASSTHRU_SUCCESS;
  69. int result;
  70. int i;
  71. int alternate = 0;
  72. Slapi_PBlock *search_pb;
  73. Slapi_Entry **entries = NULL;
  74. slapi_log_error( SLAPI_LOG_TRACE, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  75. "=> pam_passthru_load_config\n");
  76. pam_passthru_write_lock();
  77. pam_passthru_delete_config();
  78. search_pb = slapi_pblock_new();
  79. /* Find all entries in the active config area. */
  80. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_ndn(pam_passthru_get_config_area()),
  81. LDAP_SCOPE_SUBTREE, "objectclass=*",
  82. NULL, 0, NULL, NULL,
  83. pam_passthruauth_get_plugin_identity(), 0);
  84. slapi_search_internal_pb(search_pb);
  85. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  86. if (LDAP_SUCCESS != result) {
  87. status = PAM_PASSTHRU_FAILURE;
  88. goto cleanup;
  89. }
  90. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  91. &entries);
  92. if (NULL == entries || NULL == entries[0]) {
  93. status = PAM_PASSTHRU_FAILURE;
  94. goto cleanup;
  95. }
  96. /* Check if we are using an alternate config area. We do this here
  97. * so we don't have to check each every time in the loop below. */
  98. if (slapi_sdn_compare(pam_passthru_get_config_area(),
  99. pam_passthruauth_get_plugin_sdn()) != 0) {
  100. alternate = 1;
  101. }
  102. /* Validate and apply config if valid. If skip_validate is set, we skip
  103. * validation and just apply the config. This should only be done if the
  104. * configuration has already been validated. */
  105. for (i = 0; (entries[i] != NULL); i++) {
  106. /* If this is the alternate config container, skip it since
  107. * we don't consider it to be an actual config entry. */
  108. if (alternate && (slapi_sdn_compare(pam_passthru_get_config_area(),
  109. slapi_entry_get_sdn(entries[i])) == 0)) {
  110. continue;
  111. }
  112. if (skip_validate || (PAM_PASSTHRU_SUCCESS == pam_passthru_validate_config(entries[i], NULL))) {
  113. if (PAM_PASSTHRU_FAILURE == pam_passthru_apply_config(entries[i])) {
  114. slapi_log_error( SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  115. "pam_passthru_load_config: unable to apply config "
  116. "for entry \"%s\"\n", slapi_entry_get_ndn(entries[i]));
  117. }
  118. } else {
  119. slapi_log_error( SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  120. "pam_passthru_load_config: skipping invalid config "
  121. "entry \"%s\"\n", slapi_entry_get_ndn(entries[i]));
  122. }
  123. }
  124. cleanup:
  125. slapi_free_search_results_internal(search_pb);
  126. slapi_pblock_destroy(search_pb);
  127. pam_passthru_unlock();
  128. slapi_log_error(SLAPI_LOG_TRACE, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  129. "<= pam_passthru_load_config\n");
  130. return status;
  131. }
  132. static void
  133. Delete_Pam_PassthruSuffix(Pam_PassthruSuffix *one)
  134. {
  135. if (one) {
  136. slapi_sdn_free(&one->pamptsuffix_dn);
  137. slapi_ch_free((void **)&one);
  138. }
  139. }
  140. static void
  141. pam_ptconfig_free_suffixes(Pam_PassthruSuffix *list)
  142. {
  143. while (list) {
  144. Pam_PassthruSuffix *next = list->pamptsuffix_next;
  145. Delete_Pam_PassthruSuffix(list);
  146. list = next;
  147. }
  148. }
  149. /*
  150. * Free a config struct.
  151. */
  152. static void
  153. pam_passthru_free_config_entry(Pam_PassthruConfig **entry)
  154. {
  155. Pam_PassthruConfig *e = *entry;
  156. if (e == NULL) {
  157. return;
  158. }
  159. slapi_ch_free_string(&e->dn);
  160. pam_ptconfig_free_suffixes(e->pamptconfig_includes);
  161. pam_ptconfig_free_suffixes(e->pamptconfig_excludes);
  162. slapi_ch_free_string(&e->pamptconfig_pam_ident_attr);
  163. slapi_ch_free_string(&e->pamptconfig_service);
  164. slapi_ch_free_string(&e->filter_str);
  165. slapi_filter_free(e->slapi_filter, 1);
  166. slapi_ch_free((void **) entry);
  167. }
  168. /*
  169. * Free and remove a single config item from the list.
  170. */
  171. static void
  172. pam_passthru_delete_configEntry(PRCList *entry)
  173. {
  174. PR_REMOVE_LINK(entry);
  175. pam_passthru_free_config_entry((Pam_PassthruConfig **) &entry);
  176. }
  177. /*
  178. * Delete the entire config list contents.
  179. */
  180. void
  181. pam_passthru_delete_config()
  182. {
  183. PRCList *list;
  184. while (!PR_CLIST_IS_EMPTY(pam_passthru_global_config)) {
  185. list = PR_LIST_HEAD(pam_passthru_global_config);
  186. pam_passthru_delete_configEntry(list);
  187. }
  188. return;
  189. }
  190. static int
  191. missing_suffix_to_int(char *missing_suffix)
  192. {
  193. int retval = -1; /* -1 is error */
  194. if (!PL_strcasecmp(missing_suffix, PAMPT_MISSING_SUFFIX_ERROR_STRING)) {
  195. retval = PAMPT_MISSING_SUFFIX_ERROR;
  196. } else if (!PL_strcasecmp(missing_suffix, PAMPT_MISSING_SUFFIX_ALLOW_STRING)) {
  197. retval = PAMPT_MISSING_SUFFIX_ALLOW;
  198. } else if (!PL_strcasecmp(missing_suffix, PAMPT_MISSING_SUFFIX_IGNORE_STRING)) {
  199. retval = PAMPT_MISSING_SUFFIX_IGNORE;
  200. }
  201. return retval;
  202. }
  203. static PRBool
  204. check_missing_suffix_flag(int val) {
  205. if (val == PAMPT_MISSING_SUFFIX_ERROR ||
  206. val == PAMPT_MISSING_SUFFIX_ALLOW ||
  207. val == PAMPT_MISSING_SUFFIX_IGNORE) {
  208. return PR_TRUE;
  209. }
  210. return PR_FALSE;
  211. }
  212. static char *get_missing_suffix_values()
  213. {
  214. return PAMPT_MISSING_SUFFIX_ERROR_STRING ", " PAMPT_MISSING_SUFFIX_ALLOW_STRING ", "
  215. PAMPT_MISSING_SUFFIX_IGNORE_STRING;
  216. }
  217. static char *get_map_method_values()
  218. {
  219. return PAMPT_MAP_METHOD_DN_STRING " or " PAMPT_MAP_METHOD_RDN_STRING " or " PAMPT_MAP_METHOD_ENTRY_STRING;
  220. }
  221. static int
  222. meth_to_int(char **map_method, int *err)
  223. {
  224. char *end;
  225. int len;
  226. int ret = PAMPT_MAP_METHOD_NONE;
  227. *err = 0;
  228. if (!map_method || !*map_method) {
  229. return ret;
  230. }
  231. end = strchr(*map_method, ' ');
  232. if (!end) {
  233. len = strlen(*map_method);
  234. } else {
  235. len = end - *map_method;
  236. }
  237. if (!PL_strncasecmp(*map_method, PAMPT_MAP_METHOD_DN_STRING, len)) {
  238. ret = PAMPT_MAP_METHOD_DN;
  239. } else if (!PL_strncasecmp(*map_method, PAMPT_MAP_METHOD_RDN_STRING, len)) {
  240. ret = PAMPT_MAP_METHOD_RDN;
  241. } else if (!PL_strncasecmp(*map_method, PAMPT_MAP_METHOD_ENTRY_STRING, len)) {
  242. ret = PAMPT_MAP_METHOD_ENTRY;
  243. } else {
  244. *err = 1;
  245. }
  246. if (!*err) {
  247. if (end && *end) {
  248. *map_method = end + 1;
  249. } else {
  250. *map_method = NULL;
  251. }
  252. }
  253. return ret;
  254. }
  255. static int
  256. parse_map_method(char *map_method, int *one, int *two, int *three, char *returntext)
  257. {
  258. int err = PAM_PASSTHRU_SUCCESS;
  259. char **ptr = &map_method;
  260. *one = *two = *three = PAMPT_MAP_METHOD_NONE;
  261. *one = meth_to_int(ptr, &err);
  262. if (err) {
  263. if (returntext) {
  264. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  265. "The map method in the string [%s] is invalid: must be "
  266. "one of %s", map_method, get_map_method_values());
  267. } else {
  268. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  269. "The map method in the string [%s] is invalid: must be "
  270. "one of %s\n", map_method, get_map_method_values());
  271. }
  272. err = PAM_PASSTHRU_FAILURE;
  273. goto bail;
  274. }
  275. *two = meth_to_int(ptr, &err);
  276. if (err) {
  277. if (returntext) {
  278. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  279. "The map method in the string [%s] is invalid: must be "
  280. "one of %s", map_method, get_map_method_values());
  281. } else {
  282. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  283. "The map method in the string [%s] is invalid: must be "
  284. "one of %s\n", map_method, get_map_method_values());
  285. }
  286. err = PAM_PASSTHRU_FAILURE;
  287. goto bail;
  288. }
  289. *three = meth_to_int(ptr, &err);
  290. if (err) {
  291. if (returntext) {
  292. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  293. "The map method in the string [%s] is invalid: must be "
  294. "one of %s", map_method, get_map_method_values());
  295. } else {
  296. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  297. "The map method in the string [%s] is invalid: must be "
  298. "one of %s\n", map_method, get_map_method_values());
  299. }
  300. err = PAM_PASSTHRU_FAILURE;
  301. goto bail;
  302. }
  303. if ((meth_to_int(ptr, &err) != PAMPT_MAP_METHOD_NONE) || err) {
  304. if (returntext) {
  305. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  306. "Invalid extra text [%s] after last map method",
  307. ((ptr && *ptr) ? *ptr : "(null)"));
  308. } else {
  309. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  310. "Invalid extra text [%s] after last map method\n",
  311. ((ptr && *ptr) ? *ptr : "(null)"));
  312. }
  313. err = PAM_PASSTHRU_FAILURE;
  314. goto bail;
  315. }
  316. bail:
  317. return err;
  318. }
  319. static void
  320. print_suffixes()
  321. {
  322. void *cookie = NULL;
  323. Slapi_DN *sdn = NULL;
  324. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  325. "The following is the list of valid suffixes to use with "
  326. PAMPT_EXCLUDES_ATTR " and " PAMPT_INCLUDES_ATTR ":\n");
  327. for (sdn = slapi_get_first_suffix(&cookie, 1);
  328. sdn && cookie;
  329. sdn = slapi_get_next_suffix(&cookie, 1)) {
  330. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  331. "\t%s\n", slapi_sdn_get_dn(sdn));
  332. }
  333. }
  334. /*
  335. * Validate the pending changes in the e entry.
  336. * If returntext is NULL, we log messages about invalid config
  337. * to the errors log.
  338. */
  339. int
  340. pam_passthru_validate_config (Slapi_Entry* e, char *returntext)
  341. {
  342. int rc = PAM_PASSTHRU_FAILURE;
  343. char *missing_suffix_str = NULL;
  344. int missing_suffix;
  345. int ii;
  346. char **excludes = NULL;
  347. char **includes = NULL;
  348. char *pam_ident_attr = NULL;
  349. char *map_method = NULL;
  350. char *pam_filter_str = NULL;
  351. Slapi_Filter *pam_filter = NULL;
  352. /* first, get the missing_suffix flag and validate it */
  353. missing_suffix_str = slapi_entry_attr_get_charptr(e, PAMPT_MISSING_SUFFIX_ATTR);
  354. if ((missing_suffix = missing_suffix_to_int(missing_suffix_str)) < 0 ||
  355. !check_missing_suffix_flag(missing_suffix)) {
  356. if (returntext) {
  357. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  358. "Error: valid values for %s are %s",
  359. PAMPT_MISSING_SUFFIX_ATTR, get_missing_suffix_values());
  360. } else {
  361. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  362. "Error: valid values for %s are %s\n",
  363. PAMPT_MISSING_SUFFIX_ATTR, get_missing_suffix_values());
  364. }
  365. goto done;
  366. }
  367. if (missing_suffix != PAMPT_MISSING_SUFFIX_IGNORE) {
  368. char **missing_list = NULL;
  369. /* get the list of excluded suffixes */
  370. excludes = slapi_entry_attr_get_charray(e, PAMPT_EXCLUDES_ATTR);
  371. for (ii = 0; excludes && excludes[ii]; ++ii) {
  372. /* The excludes DNs are already normalized. */
  373. Slapi_DN *comp_dn = slapi_sdn_new_normdn_byref(excludes[ii]);
  374. if (!slapi_be_exist(comp_dn)) {
  375. charray_add(&missing_list, slapi_ch_strdup(excludes[ii]));
  376. }
  377. slapi_sdn_free(&comp_dn);
  378. }
  379. /* get the list of included suffixes */
  380. includes = slapi_entry_attr_get_charray(e, PAMPT_INCLUDES_ATTR);
  381. for (ii = 0; includes && includes[ii]; ++ii) {
  382. /* The includes DNs are already normalized. */
  383. Slapi_DN *comp_dn = slapi_sdn_new_normdn_byref(includes[ii]);
  384. if (!slapi_be_exist(comp_dn)) {
  385. charray_add(&missing_list, slapi_ch_strdup(includes[ii]));
  386. }
  387. slapi_sdn_free(&comp_dn);
  388. }
  389. if (missing_list) {
  390. if (returntext) {
  391. PRUint32 size =
  392. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  393. "The following suffixes listed in %s or %s are not present in this "
  394. "server: ", PAMPT_EXCLUDES_ATTR, PAMPT_INCLUDES_ATTR);
  395. for (ii = 0; missing_list[ii]; ++ii) {
  396. if (size < SLAPI_DSE_RETURNTEXT_SIZE) {
  397. size += PR_snprintf(returntext+size, SLAPI_DSE_RETURNTEXT_SIZE-size,
  398. "%s%s", (ii > 0) ? "; " : "",
  399. missing_list[ii]);
  400. }
  401. }
  402. } else {
  403. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  404. "The suffixes listed in %s or %s are not present in "
  405. "this server\n", PAMPT_EXCLUDES_ATTR, PAMPT_INCLUDES_ATTR);
  406. }
  407. slapi_ch_array_free(missing_list);
  408. missing_list = NULL;
  409. print_suffixes();
  410. if (missing_suffix != PAMPT_MISSING_SUFFIX_ERROR) {
  411. if (returntext) {
  412. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  413. "Warning: %s\n", returntext);
  414. *returntext = 0; /* log error, don't report back to user */
  415. }
  416. } else {
  417. goto done;
  418. }
  419. }
  420. }
  421. pam_ident_attr = slapi_entry_attr_get_charptr(e, PAMPT_PAM_IDENT_ATTR);
  422. map_method = slapi_entry_attr_get_charptr(e, PAMPT_MAP_METHOD_ATTR);
  423. if (map_method) {
  424. int one, two, three;
  425. if (PAM_PASSTHRU_SUCCESS !=
  426. (rc = parse_map_method(map_method, &one, &two, &three, returntext))) {
  427. goto done; /* returntext set already (or error logged) */
  428. }
  429. if (!pam_ident_attr &&
  430. ((one == PAMPT_MAP_METHOD_ENTRY) || (two == PAMPT_MAP_METHOD_ENTRY) ||
  431. (three == PAMPT_MAP_METHOD_ENTRY))) {
  432. if (returntext) {
  433. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Error: the %s method"
  434. " was specified, but no %s was given",
  435. PAMPT_MAP_METHOD_ENTRY_STRING, PAMPT_PAM_IDENT_ATTR);
  436. } else {
  437. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  438. "Error: the %s method was specified, but no %s was given\n",
  439. PAMPT_MAP_METHOD_ENTRY_STRING, PAMPT_PAM_IDENT_ATTR);
  440. }
  441. rc = PAM_PASSTHRU_FAILURE;
  442. goto done;
  443. }
  444. if ((one == PAMPT_MAP_METHOD_NONE) && (two == PAMPT_MAP_METHOD_NONE) &&
  445. (three == PAMPT_MAP_METHOD_NONE)) {
  446. if (returntext) {
  447. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Error: no method(s)"
  448. " specified for %s, should be one or more of %s",
  449. PAMPT_MAP_METHOD_ATTR, get_map_method_values());
  450. } else {
  451. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  452. "Error: no method(s) specified for %s, should be "
  453. "one or more of %s\n", PAMPT_MAP_METHOD_ATTR,
  454. get_map_method_values());
  455. }
  456. rc = PAM_PASSTHRU_FAILURE;
  457. goto done;
  458. }
  459. }
  460. /* Validate filter by converting to Slapi_Filter */
  461. pam_filter_str = slapi_entry_attr_get_charptr(e, PAMPT_FILTER_ATTR);
  462. if (pam_filter_str) {
  463. pam_filter = slapi_str2filter(pam_filter_str);
  464. if (pam_filter == NULL) {
  465. if (returntext) {
  466. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "Error: invalid "
  467. "filter specified for %s (filter: \"%s\")",
  468. PAMPT_FILTER_ATTR, pam_filter_str);
  469. } else {
  470. slapi_log_error(SLAPI_LOG_FATAL, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  471. "Error: invalid filter specified for %s "
  472. "(filter: \"%s\")\n", PAMPT_FILTER_ATTR,
  473. pam_filter_str);
  474. }
  475. rc = PAM_PASSTHRU_FAILURE;
  476. goto done;
  477. }
  478. }
  479. /* success */
  480. rc = PAM_PASSTHRU_SUCCESS;
  481. done:
  482. slapi_ch_free_string(&map_method);
  483. slapi_ch_free_string(&pam_ident_attr);
  484. slapi_ch_array_free(excludes);
  485. excludes = NULL;
  486. slapi_ch_array_free(includes);
  487. includes = NULL;
  488. slapi_ch_free_string(&missing_suffix_str);
  489. slapi_ch_free_string(&pam_filter_str);
  490. slapi_filter_free(pam_filter, 1);
  491. return rc;
  492. }
  493. static Pam_PassthruSuffix *
  494. New_Pam_PassthruSuffix(char *suffix)
  495. {
  496. Pam_PassthruSuffix *newone = NULL;
  497. if (suffix) {
  498. newone = (Pam_PassthruSuffix *)slapi_ch_malloc(sizeof(Pam_PassthruSuffix));
  499. /* The passed in suffix should already be normalized. */
  500. newone->pamptsuffix_dn = slapi_sdn_new_normdn_byval(suffix);
  501. newone->pamptsuffix_next = NULL;
  502. }
  503. return newone;
  504. }
  505. static Pam_PassthruSuffix *
  506. pam_ptconfig_add_suffixes(char **str_list)
  507. {
  508. Pam_PassthruSuffix *head = NULL;
  509. Pam_PassthruSuffix *suffixent = NULL;
  510. if (str_list && *str_list) {
  511. int ii;
  512. for (ii = 0; str_list[ii]; ++ii) {
  513. Pam_PassthruSuffix *tmp = New_Pam_PassthruSuffix(str_list[ii]);
  514. if (!suffixent) {
  515. head = suffixent = tmp;
  516. } else {
  517. suffixent->pamptsuffix_next = tmp;
  518. suffixent = suffixent->pamptsuffix_next;
  519. }
  520. }
  521. }
  522. return head;
  523. }
  524. /*
  525. Apply the pending changes in the e entry to our config struct.
  526. validate must have already been called
  527. */
  528. static int
  529. pam_passthru_apply_config (Slapi_Entry* e)
  530. {
  531. int rc = PAM_PASSTHRU_SUCCESS;
  532. char **excludes = NULL;
  533. char **includes = NULL;
  534. char *new_service = NULL;
  535. char *pam_ident_attr = NULL;
  536. char *map_method = NULL;
  537. char *dn = NULL;
  538. PRBool fallback;
  539. PRBool secure;
  540. Pam_PassthruConfig *entry = NULL;
  541. PRCList *list;
  542. Slapi_Attr *a = NULL;
  543. char *filter_str = NULL;
  544. int inserted = 0;
  545. pam_ident_attr = slapi_entry_attr_get_charptr(e, PAMPT_PAM_IDENT_ATTR);
  546. map_method = slapi_entry_attr_get_charptr(e, PAMPT_MAP_METHOD_ATTR);
  547. new_service = slapi_entry_attr_get_charptr(e, PAMPT_SERVICE_ATTR);
  548. excludes = slapi_entry_attr_get_charray(e, PAMPT_EXCLUDES_ATTR);
  549. includes = slapi_entry_attr_get_charray(e, PAMPT_INCLUDES_ATTR);
  550. fallback = slapi_entry_attr_get_bool(e, PAMPT_FALLBACK_ATTR);
  551. filter_str = slapi_entry_attr_get_charptr(e, PAMPT_FILTER_ATTR);
  552. /* Require SSL/TLS if the secure attr is not specified. We
  553. * need to check if the attribute is present to make this
  554. * determiniation. */
  555. if (slapi_entry_attr_find(e, PAMPT_SECURE_ATTR, &a) == 0) {
  556. secure = slapi_entry_attr_get_bool(e, PAMPT_SECURE_ATTR);
  557. } else {
  558. secure = PR_TRUE;
  559. }
  560. /* Allocate a config struct. */
  561. entry = (Pam_PassthruConfig *)
  562. slapi_ch_calloc(1, sizeof(Pam_PassthruConfig));
  563. if (NULL == entry) {
  564. rc = PAM_PASSTHRU_FAILURE;
  565. goto bail;
  566. }
  567. /* use the RDN method to derive the PAM identity by default*/
  568. entry->pamptconfig_map_method1 = PAMPT_MAP_METHOD_RDN;
  569. entry->pamptconfig_map_method2 = PAMPT_MAP_METHOD_NONE;
  570. entry->pamptconfig_map_method3 = PAMPT_MAP_METHOD_NONE;
  571. /* Fill in the struct. */
  572. dn = slapi_entry_get_ndn(e);
  573. if (dn) {
  574. entry->dn = slapi_ch_strdup(dn);
  575. }
  576. entry->pamptconfig_fallback = fallback;
  577. entry->pamptconfig_secure = secure;
  578. if (!entry->pamptconfig_service ||
  579. (new_service && PL_strcmp(entry->pamptconfig_service, new_service))) {
  580. slapi_ch_free_string(&entry->pamptconfig_service);
  581. entry->pamptconfig_service = new_service;
  582. new_service = NULL; /* config now owns memory */
  583. }
  584. /* get the list of excluded suffixes */
  585. pam_ptconfig_free_suffixes(entry->pamptconfig_excludes);
  586. entry->pamptconfig_excludes = pam_ptconfig_add_suffixes(excludes);
  587. /* get the list of included suffixes */
  588. pam_ptconfig_free_suffixes(entry->pamptconfig_includes);
  589. entry->pamptconfig_includes = pam_ptconfig_add_suffixes(includes);
  590. if (!entry->pamptconfig_pam_ident_attr ||
  591. (pam_ident_attr && PL_strcmp(entry->pamptconfig_pam_ident_attr, pam_ident_attr))) {
  592. slapi_ch_free_string(&entry->pamptconfig_pam_ident_attr);
  593. entry->pamptconfig_pam_ident_attr = pam_ident_attr;
  594. pam_ident_attr = NULL; /* config now owns memory */
  595. }
  596. if (map_method) {
  597. parse_map_method(map_method,
  598. &entry->pamptconfig_map_method1,
  599. &entry->pamptconfig_map_method2,
  600. &entry->pamptconfig_map_method3,
  601. NULL);
  602. }
  603. if (filter_str) {
  604. entry->filter_str = filter_str;
  605. filter_str = NULL; /* config now owns memory */
  606. entry->slapi_filter = slapi_str2filter(entry->filter_str);
  607. }
  608. /* Add config to list. We just store at the tail. */
  609. if (!PR_CLIST_IS_EMPTY(pam_passthru_global_config)) {
  610. list = PR_LIST_HEAD(pam_passthru_global_config);
  611. while (list != pam_passthru_global_config) {
  612. list = PR_NEXT_LINK(list);
  613. if (pam_passthru_global_config == list) {
  614. /* add to tail */
  615. PR_INSERT_BEFORE(&(entry->list), list);
  616. slapi_log_error(SLAPI_LOG_CONFIG, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  617. "store [%s] at tail\n", entry->dn);
  618. inserted = 1;
  619. break;
  620. }
  621. }
  622. } else {
  623. /* first entry */
  624. PR_INSERT_LINK(&(entry->list), pam_passthru_global_config);
  625. slapi_log_error(SLAPI_LOG_CONFIG, PAM_PASSTHRU_PLUGIN_SUBSYSTEM,
  626. "store [%s] at head \n", entry->dn);
  627. inserted = 1;
  628. }
  629. bail:
  630. if(!inserted){
  631. pam_passthru_free_config_entry(&entry);
  632. }
  633. slapi_ch_free_string(&new_service);
  634. slapi_ch_free_string(&map_method);
  635. slapi_ch_free_string(&pam_ident_attr);
  636. slapi_ch_free_string(&filter_str);
  637. slapi_ch_array_free(excludes);
  638. slapi_ch_array_free(includes);
  639. return rc;
  640. }
  641. static int
  642. pam_passthru_check_suffix(Pam_PassthruConfig *cfg, const Slapi_DN *bindsdn)
  643. {
  644. Pam_PassthruSuffix *try;
  645. int ret = LDAP_SUCCESS;
  646. if (!cfg->pamptconfig_includes && !cfg->pamptconfig_excludes) {
  647. goto done; /* NULL means allow */
  648. }
  649. /* exclude trumps include - if suffix is on exclude list, then
  650. deny */
  651. for (try = cfg->pamptconfig_excludes; try; try = try->pamptsuffix_next) {
  652. if (slapi_sdn_issuffix(bindsdn, try->pamptsuffix_dn)) {
  653. ret = LDAP_UNWILLING_TO_PERFORM; /* suffix is excluded */
  654. goto done;
  655. }
  656. }
  657. /* ok, now flip it - deny access unless dn is on include list */
  658. if (cfg->pamptconfig_includes) {
  659. ret = LDAP_UNWILLING_TO_PERFORM; /* suffix is excluded */
  660. for (try = cfg->pamptconfig_includes; try; try = try->pamptsuffix_next) {
  661. if (slapi_sdn_issuffix(bindsdn, try->pamptsuffix_dn)) {
  662. ret = LDAP_SUCCESS; /* suffix is included */
  663. goto done;
  664. }
  665. }
  666. }
  667. done:
  668. return ret;
  669. }
  670. /*
  671. * Find the config entry that matches the passed in bind DN
  672. */
  673. Pam_PassthruConfig *
  674. pam_passthru_get_config( Slapi_DN *bind_sdn )
  675. {
  676. PRCList *list = NULL;
  677. Pam_PassthruConfig *cfg = NULL;
  678. /* Loop through config list to see if there is a match. */
  679. if (!PR_CLIST_IS_EMPTY(pam_passthru_global_config)) {
  680. list = PR_LIST_HEAD(pam_passthru_global_config);
  681. while (list != pam_passthru_global_config) {
  682. cfg = (Pam_PassthruConfig *)list;
  683. if (pam_passthru_check_suffix( cfg, bind_sdn ) == LDAP_SUCCESS) {
  684. if (cfg->slapi_filter) {
  685. /* A filter is configured, so see if the bind entry is a match. */
  686. Slapi_Entry *test_e = NULL;
  687. /* Fetch the bind entry */
  688. slapi_search_internal_get_entry(bind_sdn, NULL, &test_e,
  689. pam_passthruauth_get_plugin_identity());
  690. /* If the entry doesn't exist, just fall through to the main server code */
  691. if (test_e) {
  692. /* Evaluate the filter. */
  693. if (LDAP_SUCCESS == slapi_filter_test_simple(test_e, cfg-> slapi_filter)) {
  694. /* This is a match. */
  695. slapi_entry_free(test_e);
  696. goto done;
  697. }
  698. slapi_entry_free(test_e);
  699. }
  700. } else {
  701. /* There is no filter to check, so this is a match. */
  702. goto done;
  703. }
  704. }
  705. cfg = NULL;
  706. list = PR_NEXT_LINK(list);
  707. }
  708. }
  709. done:
  710. return(cfg);
  711. }
  712. /*
  713. * Check if the DN is considered to be a config entry.
  714. *
  715. * If the config is stored in cn=config, the top-level plug-in
  716. * entry and it's children are considered to be config. If an
  717. * alternate plug-in config area is being used, only the children
  718. * of the alternate config container are considered to be config.
  719. *
  720. * Returns 1 if DN is a config entry.
  721. */
  722. int
  723. pam_passthru_dn_is_config(Slapi_DN *sdn)
  724. {
  725. int rc = 0;
  726. if (sdn == NULL) {
  727. goto bail;
  728. }
  729. /* Check if we're using the standard config area. */
  730. if (slapi_sdn_compare(pam_passthru_get_config_area(),
  731. pam_passthruauth_get_plugin_sdn()) == 0) {
  732. /* We're using the standard config area, so both
  733. * the container and the children are considered
  734. * to be config entries. */
  735. if (slapi_sdn_issuffix(sdn, pam_passthru_get_config_area())) {
  736. rc = 1;
  737. }
  738. } else {
  739. /* We're using an alternative config area, so only
  740. * the children are considered to be config entries. */
  741. if (slapi_sdn_issuffix(sdn, pam_passthru_get_config_area()) &&
  742. slapi_sdn_compare(sdn, pam_passthru_get_config_area())) {
  743. rc = 1;
  744. }
  745. }
  746. bail:
  747. return rc;
  748. }
  749. /*
  750. * Set the active config area.
  751. */
  752. void
  753. pam_passthru_set_config_area(Slapi_DN *sdn)
  754. {
  755. _ConfigArea = sdn;
  756. }
  757. /*
  758. * Return the active config area.
  759. */
  760. Slapi_DN *
  761. pam_passthru_get_config_area()
  762. {
  763. return _ConfigArea;
  764. }
  765. /*
  766. * Free the active config area.
  767. */
  768. void
  769. pam_passthru_free_config_area()
  770. {
  771. slapi_sdn_free(&_ConfigArea);
  772. }