memberof_config.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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) 2008 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * memberof_config.c - configuration-related code for memberOf plug-in
  42. *
  43. */
  44. #include <plstr.h>
  45. #include "memberof.h"
  46. #define MEMBEROF_CONFIG_FILTER "(objectclass=*)"
  47. /*
  48. * The configuration attributes are contained in the plugin entry e.g.
  49. * cn=MemberOf Plugin,cn=plugins,cn=config
  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. /*
  56. * function prototypes
  57. */
  58. static int memberof_validate_config (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  59. int *returncode, char *returntext, void *arg);
  60. static int memberof_apply_config (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  61. int *returncode, char *returntext, void *arg);
  62. static int memberof_search (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  63. int *returncode, char *returntext, void *arg)
  64. {
  65. return SLAPI_DSE_CALLBACK_OK;
  66. }
  67. /*
  68. * static variables
  69. */
  70. /* This is the main configuration which is updated from dse.ldif. The
  71. * config will be copied when it is used by the plug-in to prevent it
  72. * being changed out from under a running memberOf operation. */
  73. static MemberOfConfig theConfig;
  74. static Slapi_RWLock *memberof_config_lock = 0;
  75. static int inited = 0;
  76. static int dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  77. int *returncode, char *returntext, void *arg)
  78. {
  79. *returncode = LDAP_UNWILLING_TO_PERFORM;
  80. return SLAPI_DSE_CALLBACK_ERROR;
  81. }
  82. /*
  83. * memberof_config()
  84. *
  85. * Read configuration and create a configuration data structure.
  86. * This is called after the server has configured itself so we can
  87. * perform checks with regards to suffixes if it ever becomes
  88. * necessary.
  89. * Returns an LDAP error code (LDAP_SUCCESS if all goes well).
  90. */
  91. int
  92. memberof_config(Slapi_Entry *config_e)
  93. {
  94. int returncode = LDAP_SUCCESS;
  95. char returntext[SLAPI_DSE_RETURNTEXT_SIZE];
  96. if ( inited ) {
  97. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  98. "only one memberOf plugin instance can be used\n" );
  99. return( LDAP_PARAM_ERROR );
  100. }
  101. /* initialize the RW lock to protect the main config */
  102. memberof_config_lock = slapi_new_rwlock();
  103. /* initialize fields */
  104. if (SLAPI_DSE_CALLBACK_OK == memberof_validate_config(NULL, NULL, config_e,
  105. &returncode, returntext, NULL)) {
  106. memberof_apply_config(NULL, NULL, config_e,
  107. &returncode, returntext, NULL);
  108. }
  109. /* config DSE must be initialized before we get here */
  110. if (returncode == LDAP_SUCCESS) {
  111. const char *config_dn = slapi_entry_get_dn_const(config_e);
  112. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  113. config_dn, LDAP_SCOPE_BASE, MEMBEROF_CONFIG_FILTER,
  114. memberof_validate_config,NULL);
  115. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_POSTOP,
  116. config_dn, LDAP_SCOPE_BASE, MEMBEROF_CONFIG_FILTER,
  117. memberof_apply_config,NULL);
  118. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP,
  119. config_dn, LDAP_SCOPE_BASE, MEMBEROF_CONFIG_FILTER,
  120. dont_allow_that, NULL);
  121. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP,
  122. config_dn, LDAP_SCOPE_BASE, MEMBEROF_CONFIG_FILTER,
  123. dont_allow_that, NULL);
  124. slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP,
  125. config_dn, LDAP_SCOPE_BASE, MEMBEROF_CONFIG_FILTER,
  126. memberof_search,NULL);
  127. }
  128. inited = 1;
  129. if (returncode != LDAP_SUCCESS) {
  130. slapi_log_error(SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  131. "Error %d: %s\n", returncode, returntext);
  132. }
  133. return returncode;
  134. }
  135. /*
  136. * memberof_validate_config()
  137. *
  138. * Validate the pending changes in the e entry.
  139. */
  140. static int
  141. memberof_validate_config (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  142. int *returncode, char *returntext, void *arg)
  143. {
  144. Slapi_Attr *memberof_attr = NULL;
  145. Slapi_Attr *group_attr = NULL;
  146. char *syntaxoid = NULL;
  147. int not_dn_syntax = 0;
  148. *returncode = LDAP_UNWILLING_TO_PERFORM; /* be pessimistic */
  149. /* Make sure both the group attr and the memberOf attr
  150. * config atributes are supplied. We don't care about &attr
  151. * here, but slapi_entry_attr_find() requires us to pass it. */
  152. if (!slapi_entry_attr_find(e, MEMBEROF_GROUP_ATTR, &group_attr) &&
  153. !slapi_entry_attr_find(e, MEMBEROF_ATTR, &memberof_attr))
  154. {
  155. Slapi_Attr *test_attr = NULL;
  156. Slapi_Value *value = NULL;
  157. int hint = 0;
  158. /* Loop through each group attribute to see if the syntax is correct. */
  159. hint = slapi_attr_first_value(group_attr, &value);
  160. while (value && (not_dn_syntax == 0))
  161. {
  162. /* We need to create an attribute to find the syntax. */
  163. test_attr = slapi_attr_new();
  164. slapi_attr_init(test_attr, slapi_value_get_string(value));
  165. /* Get the syntax OID and see if it's the Distinguished Name or
  166. * Name and Optional UID syntax. */
  167. slapi_attr_get_syntax_oid_copy(test_attr, &syntaxoid );
  168. not_dn_syntax = strcmp(syntaxoid, DN_SYNTAX_OID) & strcmp(syntaxoid, NAME_OPT_UID_SYNTAX_OID);
  169. slapi_ch_free_string(&syntaxoid);
  170. /* Print an error if the current attribute is not using the Distinguished
  171. * Name syntax, otherwise get the next group attribute. */
  172. if (not_dn_syntax)
  173. {
  174. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  175. "The %s configuration attribute must be set to "
  176. "an attribute defined to use either the Distinguished "
  177. "Name or Name and Optional UID syntax. (illegal value: %s)",
  178. slapi_value_get_string(value), MEMBEROF_GROUP_ATTR);
  179. }
  180. else
  181. {
  182. hint = slapi_attr_next_value(group_attr, hint, &value);
  183. }
  184. /* Free the group attribute. */
  185. slapi_attr_free(&test_attr);
  186. }
  187. if (not_dn_syntax == 0)
  188. {
  189. /* Check the syntax of the memberof attribute. */
  190. slapi_attr_first_value(memberof_attr, &value);
  191. test_attr = slapi_attr_new();
  192. slapi_attr_init(test_attr, slapi_value_get_string(value));
  193. slapi_attr_get_syntax_oid_copy(test_attr, &syntaxoid );
  194. not_dn_syntax = strcmp(syntaxoid, DN_SYNTAX_OID);
  195. slapi_ch_free_string(&syntaxoid);
  196. slapi_attr_free(&test_attr);
  197. if (not_dn_syntax)
  198. {
  199. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  200. "The %s configuration attribute must be set to "
  201. "an attribute defined to use the Distinguished "
  202. "Name syntax. (illegal value: %s)",
  203. slapi_value_get_string(value), MEMBEROF_ATTR);
  204. }
  205. else
  206. {
  207. *returncode = LDAP_SUCCESS;
  208. }
  209. }
  210. }
  211. else
  212. {
  213. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  214. "The %s and %s configuration attributes must be provided",
  215. MEMBEROF_GROUP_ATTR, MEMBEROF_ATTR);
  216. }
  217. if (*returncode != LDAP_SUCCESS)
  218. {
  219. return SLAPI_DSE_CALLBACK_ERROR;
  220. }
  221. else
  222. {
  223. return SLAPI_DSE_CALLBACK_OK;
  224. }
  225. }
  226. /*
  227. * memberof_apply_config()
  228. *
  229. * Apply the pending changes in the e entry to our config struct.
  230. * memberof_validate_config() must have already been called.
  231. */
  232. static int
  233. memberof_apply_config (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  234. int *returncode, char *returntext, void *arg)
  235. {
  236. char **groupattrs = NULL;
  237. char *memberof_attr = NULL;
  238. char *filter_str = NULL;
  239. int num_groupattrs = 0;
  240. int groupattr_name_len = 0;
  241. char *allBackends = NULL;
  242. *returncode = LDAP_SUCCESS;
  243. groupattrs = slapi_entry_attr_get_charray(e, MEMBEROF_GROUP_ATTR);
  244. memberof_attr = slapi_entry_attr_get_charptr(e, MEMBEROF_ATTR);
  245. allBackends = slapi_entry_attr_get_charptr(e, MEMBEROF_BACKEND_ATTR);
  246. /* We want to be sure we don't change the config in the middle of
  247. * a memberOf operation, so we obtain an exclusive lock here */
  248. memberof_wlock_config();
  249. if (groupattrs)
  250. {
  251. int i = 0;
  252. slapi_ch_array_free(theConfig.groupattrs);
  253. theConfig.groupattrs = groupattrs;
  254. groupattrs = NULL; /* config now owns memory */
  255. /* We allocate a list of Slapi_Attr using the groupattrs for
  256. * convenience in our memberOf comparison functions */
  257. for (i = 0; theConfig.group_slapiattrs && theConfig.group_slapiattrs[i]; i++)
  258. {
  259. slapi_attr_free(&theConfig.group_slapiattrs[i]);
  260. }
  261. /* Count the number of groupattrs. */
  262. for (num_groupattrs = 0; theConfig.groupattrs && theConfig.groupattrs[num_groupattrs]; num_groupattrs++)
  263. {
  264. /* Add up the total length of all attribute names. We need
  265. * to know this for building the group check filter later. */
  266. groupattr_name_len += strlen(theConfig.groupattrs[num_groupattrs]);
  267. }
  268. /* Realloc the list of Slapi_Attr if necessary. */
  269. if (i < num_groupattrs)
  270. {
  271. theConfig.group_slapiattrs = (Slapi_Attr **)slapi_ch_realloc((char *)theConfig.group_slapiattrs,
  272. sizeof(Slapi_Attr *) * (num_groupattrs + 1));
  273. }
  274. /* Build the new list */
  275. for (i = 0; theConfig.groupattrs[i]; i++)
  276. {
  277. theConfig.group_slapiattrs[i] = slapi_attr_new();
  278. slapi_attr_init(theConfig.group_slapiattrs[i], theConfig.groupattrs[i]);
  279. }
  280. /* Terminate the list. */
  281. theConfig.group_slapiattrs[i] = NULL;
  282. /* The filter is based off of the groupattr, so we
  283. * update it here too. */
  284. slapi_filter_free(theConfig.group_filter, 1);
  285. if (num_groupattrs > 1)
  286. {
  287. int bytes_out = 0;
  288. int filter_str_len = groupattr_name_len + (num_groupattrs * 4) + 4;
  289. /* Allocate enough space for the filter */
  290. filter_str = slapi_ch_malloc(filter_str_len);
  291. /* Add beginning of filter. */
  292. bytes_out = snprintf(filter_str, filter_str_len - bytes_out, "(|");
  293. /* Add filter section for each groupattr. */
  294. for (i = 0; theConfig.groupattrs[i]; i++)
  295. {
  296. bytes_out += snprintf(filter_str + bytes_out, filter_str_len - bytes_out, "(%s=*)", theConfig.groupattrs[i]);
  297. }
  298. /* Add end of filter. */
  299. snprintf(filter_str + bytes_out, filter_str_len - bytes_out, ")");
  300. }
  301. else
  302. {
  303. filter_str = slapi_ch_smprintf("(%s=*)", theConfig.groupattrs[0]);
  304. }
  305. /* Log an error if we were unable to build the group filter for some
  306. * reason. If this happens, the memberOf plugin will not be able to
  307. * check if an entry is a group, causing it to not catch changes. This
  308. * shouldn't happen, but there may be some garbage configuration that
  309. * could trigger this. */
  310. if ((theConfig.group_filter = slapi_str2filter(filter_str)) == NULL)
  311. {
  312. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  313. "Unable to create the group check filter. The memberOf "
  314. "plug-in will not operate on changes to groups. Please check "
  315. "your %s configuration settings. (filter: %s)\n",
  316. MEMBEROF_GROUP_ATTR, filter_str );
  317. }
  318. slapi_ch_free_string(&filter_str);
  319. }
  320. if (memberof_attr)
  321. {
  322. slapi_ch_free_string(&theConfig.memberof_attr);
  323. theConfig.memberof_attr = memberof_attr;
  324. memberof_attr = NULL; /* config now owns memory */
  325. }
  326. if (allBackends)
  327. {
  328. if(strcasecmp(allBackends,"on")==0){
  329. theConfig.allBackends = 1;
  330. } else {
  331. theConfig.allBackends = 0;
  332. }
  333. } else {
  334. theConfig.allBackends = 0;
  335. }
  336. /* release the lock */
  337. memberof_unlock_config();
  338. slapi_ch_array_free(groupattrs);
  339. slapi_ch_free_string(&memberof_attr);
  340. slapi_ch_free_string(&allBackends);
  341. if (*returncode != LDAP_SUCCESS)
  342. {
  343. return SLAPI_DSE_CALLBACK_ERROR;
  344. }
  345. else
  346. {
  347. return SLAPI_DSE_CALLBACK_OK;
  348. }
  349. }
  350. /*
  351. * memberof_copy_config()
  352. *
  353. * Makes a copy of the config in src. This function will free the
  354. * elements of dest if they already exist. This should only be called
  355. * if you hold the memberof config lock if src was obtained with
  356. * memberof_get_config().
  357. */
  358. void
  359. memberof_copy_config(MemberOfConfig *dest, MemberOfConfig *src)
  360. {
  361. if (dest && src)
  362. {
  363. /* Check if the copy is already up to date */
  364. if (src->groupattrs)
  365. {
  366. int i = 0, j = 0;
  367. /* Copy group attributes string list. */
  368. slapi_ch_array_free(dest->groupattrs);
  369. dest->groupattrs = slapi_ch_array_dup(src->groupattrs);
  370. /* Copy group check filter. */
  371. slapi_filter_free(dest->group_filter, 1);
  372. dest->group_filter = slapi_filter_dup(src->group_filter);
  373. /* Copy group attributes Slapi_Attr list.
  374. * First free the old list. */
  375. for (i = 0; dest->group_slapiattrs && dest->group_slapiattrs[i]; i++)
  376. {
  377. slapi_attr_free(&dest->group_slapiattrs[i]);
  378. }
  379. /* Count how many values we have in the source list. */
  380. for (j = 0; src->group_slapiattrs[j]; j++)
  381. {
  382. /* Do nothing. */
  383. }
  384. /* Realloc dest if necessary. */
  385. if (i < j)
  386. {
  387. dest->group_slapiattrs = (Slapi_Attr **)slapi_ch_realloc((char *)dest->group_slapiattrs, sizeof(Slapi_Attr *) * (j + 1));
  388. }
  389. /* Copy the attributes. */
  390. for (i = 0; src->group_slapiattrs[i]; i++)
  391. {
  392. dest->group_slapiattrs[i] = slapi_attr_dup(src->group_slapiattrs[i]);
  393. }
  394. /* Terminate the array. */
  395. dest->group_slapiattrs[i] = NULL;
  396. }
  397. if (src->memberof_attr)
  398. {
  399. slapi_ch_free_string(&dest->memberof_attr);
  400. dest->memberof_attr = slapi_ch_strdup(src->memberof_attr);
  401. }
  402. if(src->allBackends)
  403. {
  404. dest->allBackends = src->allBackends;
  405. }
  406. }
  407. }
  408. /*
  409. * memberof_free_config()
  410. *
  411. * Free's the contents of a config structure.
  412. */
  413. void
  414. memberof_free_config(MemberOfConfig *config)
  415. {
  416. if (config)
  417. {
  418. int i = 0;
  419. slapi_ch_array_free(config->groupattrs);
  420. slapi_filter_free(config->group_filter, 1);
  421. for (i = 0; config->group_slapiattrs && config->group_slapiattrs[i]; i++)
  422. {
  423. slapi_attr_free(&config->group_slapiattrs[i]);
  424. }
  425. slapi_ch_free((void **)&config->group_slapiattrs);
  426. slapi_ch_free_string(&config->memberof_attr);
  427. }
  428. }
  429. /*
  430. * memberof_get_config()
  431. *
  432. * Returns a pointer to the main config. You should call
  433. * memberof_rlock_config() first so the main config doesn't
  434. * get modified out from under you.
  435. */
  436. MemberOfConfig *
  437. memberof_get_config()
  438. {
  439. return &theConfig;
  440. }
  441. /*
  442. * memberof_rlock_config()
  443. *
  444. * Gets a non-exclusive lock on the main config. This will
  445. * prevent the config from being changed out from under you
  446. * while you read it, but it will still allow other threads
  447. * to read the config at the same time.
  448. */
  449. void
  450. memberof_rlock_config()
  451. {
  452. slapi_rwlock_rdlock(memberof_config_lock);
  453. }
  454. /*
  455. * memberof_wlock_config()
  456. *
  457. * Gets an exclusive lock on the main config. This should
  458. * be called if you need to write to the main config.
  459. */
  460. void
  461. memberof_wlock_config()
  462. {
  463. slapi_rwlock_wrlock(memberof_config_lock);
  464. }
  465. /*
  466. * memberof_unlock_config()
  467. *
  468. * Unlocks the main config.
  469. */
  470. void
  471. memberof_unlock_config()
  472. {
  473. slapi_rwlock_unlock(memberof_config_lock);
  474. }
  475. int
  476. memberof_config_get_all_backends()
  477. {
  478. int all_backends;
  479. slapi_rwlock_rdlock(memberof_config_lock);
  480. all_backends = theConfig.allBackends;
  481. slapi_rwlock_unlock(memberof_config_lock);
  482. return all_backends;
  483. }