pam_ptconfig.c 25 KB

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