memberof_config.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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_search (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  61. int *returncode, char *returntext, void *arg)
  62. {
  63. return SLAPI_DSE_CALLBACK_OK;
  64. }
  65. /*
  66. * static variables
  67. */
  68. /* This is the main configuration which is updated from dse.ldif. The
  69. * config will be copied when it is used by the plug-in to prevent it
  70. * being changed out from under a running memberOf operation. */
  71. static MemberOfConfig theConfig;
  72. static Slapi_RWLock *memberof_config_lock = 0;
  73. static int inited = 0;
  74. static int dont_allow_that(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  75. int *returncode, char *returntext, void *arg)
  76. {
  77. *returncode = LDAP_UNWILLING_TO_PERFORM;
  78. return SLAPI_DSE_CALLBACK_ERROR;
  79. }
  80. /*
  81. * memberof_config()
  82. *
  83. * Read configuration and create a configuration data structure.
  84. * This is called after the server has configured itself so we can
  85. * perform checks with regards to suffixes if it ever becomes
  86. * necessary.
  87. * Returns an LDAP error code (LDAP_SUCCESS if all goes well).
  88. */
  89. int
  90. memberof_config(Slapi_Entry *config_e)
  91. {
  92. int returncode = LDAP_SUCCESS;
  93. char returntext[SLAPI_DSE_RETURNTEXT_SIZE];
  94. if ( inited ) {
  95. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  96. "only one memberOf plugin instance can be used\n" );
  97. return( LDAP_PARAM_ERROR );
  98. }
  99. /* initialize the RW lock to protect the main config */
  100. memberof_config_lock = slapi_new_rwlock();
  101. /* initialize fields */
  102. if (SLAPI_DSE_CALLBACK_OK == memberof_validate_config(NULL, NULL, config_e,
  103. &returncode, returntext, NULL))
  104. {
  105. memberof_apply_config(NULL, NULL, config_e, &returncode, returntext, NULL);
  106. }
  107. /*
  108. * config DSE must be initialized before we get here we only need the dse callbacks
  109. * for the plugin entry, but not the shared config entry.
  110. */
  111. if (returncode == LDAP_SUCCESS) {
  112. const char *config_dn = slapi_sdn_get_dn(memberof_get_plugin_area());
  113. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP,
  114. config_dn, LDAP_SCOPE_BASE, MEMBEROF_CONFIG_FILTER,
  115. memberof_validate_config,NULL);
  116. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_POSTOP,
  117. config_dn, LDAP_SCOPE_BASE, MEMBEROF_CONFIG_FILTER,
  118. memberof_apply_config,NULL);
  119. slapi_config_register_callback(SLAPI_OPERATION_MODRDN, DSE_FLAG_PREOP,
  120. config_dn, LDAP_SCOPE_BASE, MEMBEROF_CONFIG_FILTER,
  121. dont_allow_that, NULL);
  122. slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP,
  123. config_dn, LDAP_SCOPE_BASE, MEMBEROF_CONFIG_FILTER,
  124. dont_allow_that, NULL);
  125. slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP,
  126. config_dn, LDAP_SCOPE_BASE, MEMBEROF_CONFIG_FILTER,
  127. memberof_search,NULL);
  128. }
  129. inited = 1;
  130. if (returncode != LDAP_SUCCESS) {
  131. slapi_log_error(SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  132. "Error %d: %s\n", returncode, returntext);
  133. }
  134. return returncode;
  135. }
  136. /*
  137. * memberof_validate_config()
  138. *
  139. * Validate the pending changes in the e entry.
  140. */
  141. static int
  142. memberof_validate_config (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  143. int *returncode, char *returntext, void *arg)
  144. {
  145. Slapi_Attr *memberof_attr = NULL;
  146. Slapi_Attr *group_attr = NULL;
  147. Slapi_DN *config_sdn = NULL;
  148. char *syntaxoid = NULL;
  149. char *config_dn = NULL;
  150. int not_dn_syntax = 0;
  151. *returncode = LDAP_UNWILLING_TO_PERFORM; /* be pessimistic */
  152. /* Make sure both the group attr and the memberOf attr
  153. * config atributes are supplied. We don't care about &attr
  154. * here, but slapi_entry_attr_find() requires us to pass it. */
  155. if (!slapi_entry_attr_find(e, MEMBEROF_GROUP_ATTR, &group_attr) &&
  156. !slapi_entry_attr_find(e, MEMBEROF_ATTR, &memberof_attr))
  157. {
  158. Slapi_Attr *test_attr = NULL;
  159. Slapi_Value *value = NULL;
  160. int hint = 0;
  161. /* Loop through each group attribute to see if the syntax is correct. */
  162. hint = slapi_attr_first_value(group_attr, &value);
  163. while (value && (not_dn_syntax == 0))
  164. {
  165. /* We need to create an attribute to find the syntax. */
  166. test_attr = slapi_attr_new();
  167. slapi_attr_init(test_attr, slapi_value_get_string(value));
  168. /* Get the syntax OID and see if it's the Distinguished Name or
  169. * Name and Optional UID syntax. */
  170. slapi_attr_get_syntax_oid_copy(test_attr, &syntaxoid );
  171. not_dn_syntax = strcmp(syntaxoid, DN_SYNTAX_OID) & strcmp(syntaxoid, NAME_OPT_UID_SYNTAX_OID);
  172. slapi_ch_free_string(&syntaxoid);
  173. /* Print an error if the current attribute is not using the Distinguished
  174. * Name syntax, otherwise get the next group attribute. */
  175. if (not_dn_syntax)
  176. {
  177. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  178. "The %s configuration attribute must be set to "
  179. "an attribute defined to use either the Distinguished "
  180. "Name or Name and Optional UID syntax. (illegal value: %s)",
  181. slapi_value_get_string(value), MEMBEROF_GROUP_ATTR);
  182. }
  183. else
  184. {
  185. hint = slapi_attr_next_value(group_attr, hint, &value);
  186. }
  187. /* Free the group attribute. */
  188. slapi_attr_free(&test_attr);
  189. }
  190. if (not_dn_syntax == 0)
  191. {
  192. /* Check the syntax of the memberof attribute. */
  193. slapi_attr_first_value(memberof_attr, &value);
  194. test_attr = slapi_attr_new();
  195. slapi_attr_init(test_attr, slapi_value_get_string(value));
  196. slapi_attr_get_syntax_oid_copy(test_attr, &syntaxoid );
  197. not_dn_syntax = strcmp(syntaxoid, DN_SYNTAX_OID);
  198. slapi_ch_free_string(&syntaxoid);
  199. slapi_attr_free(&test_attr);
  200. if (not_dn_syntax)
  201. {
  202. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  203. "The %s configuration attribute must be set to "
  204. "an attribute defined to use the Distinguished "
  205. "Name syntax. (illegal value: %s)",
  206. slapi_value_get_string(value), MEMBEROF_ATTR);
  207. goto done;
  208. }
  209. else
  210. {
  211. *returncode = LDAP_SUCCESS;
  212. }
  213. }
  214. } else {
  215. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  216. "The %s and %s configuration attributes must be provided",
  217. MEMBEROF_GROUP_ATTR, MEMBEROF_ATTR);
  218. goto done;
  219. }
  220. if ((config_dn = slapi_entry_attr_get_charptr(e, SLAPI_PLUGIN_SHARED_CONFIG_AREA))){
  221. /* Now check the shared config attribute, validate it now */
  222. Slapi_Entry *e = NULL;
  223. int rc = 0;
  224. rc = slapi_dn_syntax_check(pb, config_dn, 1);
  225. if (rc) { /* syntax check failed */
  226. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM, "memberof_validate_config: "
  227. "%s does not contain a valid DN (%s)\n",
  228. SLAPI_PLUGIN_SHARED_CONFIG_AREA, config_dn);
  229. *returncode = LDAP_INVALID_DN_SYNTAX;
  230. goto done;
  231. }
  232. config_sdn = slapi_sdn_new_dn_byval(config_dn);
  233. slapi_search_internal_get_entry(config_sdn, NULL, &e, memberof_get_plugin_id());
  234. if(e){
  235. slapi_entry_free(e);
  236. *returncode = LDAP_SUCCESS;
  237. } else {
  238. /* config area does not exist! */
  239. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  240. "The %s configuration attribute points to an entry that "
  241. "can not be found. (%s)",
  242. SLAPI_PLUGIN_SHARED_CONFIG_AREA, config_dn);
  243. *returncode = LDAP_UNWILLING_TO_PERFORM;
  244. }
  245. }
  246. done:
  247. slapi_sdn_free(&config_sdn);
  248. slapi_ch_free_string(&config_dn);
  249. if (*returncode != LDAP_SUCCESS)
  250. {
  251. return SLAPI_DSE_CALLBACK_ERROR;
  252. }
  253. else
  254. {
  255. return SLAPI_DSE_CALLBACK_OK;
  256. }
  257. }
  258. /*
  259. * memberof_apply_config()
  260. *
  261. * Apply the pending changes in the e entry to our config struct.
  262. * memberof_validate_config() must have already been called.
  263. */
  264. int
  265. memberof_apply_config (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  266. int *returncode, char *returntext, void *arg)
  267. {
  268. Slapi_Entry *config_entry = NULL;
  269. Slapi_DN *config_sdn = NULL;
  270. char **groupattrs = NULL;
  271. char *memberof_attr = NULL;
  272. char *filter_str = NULL;
  273. int num_groupattrs = 0;
  274. int groupattr_name_len = 0;
  275. char *allBackends = NULL;
  276. char *sharedcfg = NULL;
  277. *returncode = LDAP_SUCCESS;
  278. /*
  279. * Apply the config settings from the shared config entry
  280. */
  281. sharedcfg = slapi_entry_attr_get_charptr(e, SLAPI_PLUGIN_SHARED_CONFIG_AREA);
  282. if(sharedcfg){
  283. int rc = 0;
  284. rc = slapi_dn_syntax_check(pb, sharedcfg, 1);
  285. if (rc) { /* syntax check failed */
  286. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,"memberof_apply_config: "
  287. "%s does not contain a valid DN (%s)\n",
  288. SLAPI_PLUGIN_SHARED_CONFIG_AREA, sharedcfg);
  289. *returncode = LDAP_INVALID_DN_SYNTAX;
  290. goto done;
  291. }
  292. if((config_sdn = slapi_sdn_new_dn_byval(sharedcfg))){
  293. slapi_search_internal_get_entry(config_sdn, NULL, &config_entry, memberof_get_plugin_id());
  294. if(config_entry){
  295. char errtext[SLAPI_DSE_RETURNTEXT_SIZE];
  296. int err = 0;
  297. /*
  298. * If we got here, we are updating the shared config area, so we need to
  299. * validate and apply the settings from that config area.
  300. */
  301. if ( SLAPI_DSE_CALLBACK_ERROR == memberof_validate_config (pb, NULL, config_entry, &err, errtext,0))
  302. {
  303. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  304. "%s", errtext);
  305. *returncode = LDAP_UNWILLING_TO_PERFORM;
  306. goto done;
  307. }
  308. e = config_entry;
  309. } else {
  310. /* this should of been checked in preop validation */
  311. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM, "memberof_apply_config: "
  312. "Failed to locate shared config entry (%s)\n",sharedcfg);
  313. *returncode = LDAP_UNWILLING_TO_PERFORM;
  314. goto done;
  315. }
  316. }
  317. }
  318. groupattrs = slapi_entry_attr_get_charray(e, MEMBEROF_GROUP_ATTR);
  319. memberof_attr = slapi_entry_attr_get_charptr(e, MEMBEROF_ATTR);
  320. allBackends = slapi_entry_attr_get_charptr(e, MEMBEROF_BACKEND_ATTR);
  321. /* We want to be sure we don't change the config in the middle of
  322. * a memberOf operation, so we obtain an exclusive lock here */
  323. memberof_wlock_config();
  324. if (groupattrs)
  325. {
  326. int i = 0;
  327. slapi_ch_array_free(theConfig.groupattrs);
  328. theConfig.groupattrs = groupattrs;
  329. groupattrs = NULL; /* config now owns memory */
  330. /* We allocate a list of Slapi_Attr using the groupattrs for
  331. * convenience in our memberOf comparison functions */
  332. for (i = 0; theConfig.group_slapiattrs && theConfig.group_slapiattrs[i]; i++)
  333. {
  334. slapi_attr_free(&theConfig.group_slapiattrs[i]);
  335. }
  336. /* Count the number of groupattrs. */
  337. for (num_groupattrs = 0; theConfig.groupattrs && theConfig.groupattrs[num_groupattrs]; num_groupattrs++)
  338. {
  339. /* Add up the total length of all attribute names. We need
  340. * to know this for building the group check filter later. */
  341. groupattr_name_len += strlen(theConfig.groupattrs[num_groupattrs]);
  342. }
  343. /* Realloc the list of Slapi_Attr if necessary. */
  344. if (i < num_groupattrs)
  345. {
  346. theConfig.group_slapiattrs = (Slapi_Attr **)slapi_ch_realloc((char *)theConfig.group_slapiattrs,
  347. sizeof(Slapi_Attr *) * (num_groupattrs + 1));
  348. }
  349. /* Build the new list */
  350. for (i = 0; theConfig.groupattrs[i]; i++)
  351. {
  352. theConfig.group_slapiattrs[i] = slapi_attr_new();
  353. slapi_attr_init(theConfig.group_slapiattrs[i], theConfig.groupattrs[i]);
  354. }
  355. /* Terminate the list. */
  356. theConfig.group_slapiattrs[i] = NULL;
  357. /* The filter is based off of the groupattr, so we
  358. * update it here too. */
  359. slapi_filter_free(theConfig.group_filter, 1);
  360. if (num_groupattrs > 1)
  361. {
  362. int bytes_out = 0;
  363. int filter_str_len = groupattr_name_len + (num_groupattrs * 4) + 4;
  364. /* Allocate enough space for the filter */
  365. filter_str = slapi_ch_malloc(filter_str_len);
  366. /* Add beginning of filter. */
  367. bytes_out = snprintf(filter_str, filter_str_len - bytes_out, "(|");
  368. /* Add filter section for each groupattr. */
  369. for (i = 0; theConfig.groupattrs[i]; i++)
  370. {
  371. bytes_out += snprintf(filter_str + bytes_out, filter_str_len - bytes_out, "(%s=*)", theConfig.groupattrs[i]);
  372. }
  373. /* Add end of filter. */
  374. snprintf(filter_str + bytes_out, filter_str_len - bytes_out, ")");
  375. }
  376. else
  377. {
  378. filter_str = slapi_ch_smprintf("(%s=*)", theConfig.groupattrs[0]);
  379. }
  380. /* Log an error if we were unable to build the group filter for some
  381. * reason. If this happens, the memberOf plugin will not be able to
  382. * check if an entry is a group, causing it to not catch changes. This
  383. * shouldn't happen, but there may be some garbage configuration that
  384. * could trigger this. */
  385. if ((theConfig.group_filter = slapi_str2filter(filter_str)) == NULL)
  386. {
  387. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  388. "Unable to create the group check filter. The memberOf "
  389. "plug-in will not operate on changes to groups. Please check "
  390. "your %s configuration settings. (filter: %s)\n",
  391. MEMBEROF_GROUP_ATTR, filter_str );
  392. }
  393. slapi_ch_free_string(&filter_str);
  394. }
  395. if (memberof_attr)
  396. {
  397. slapi_ch_free_string(&theConfig.memberof_attr);
  398. theConfig.memberof_attr = memberof_attr;
  399. memberof_attr = NULL; /* config now owns memory */
  400. }
  401. if (allBackends)
  402. {
  403. if(strcasecmp(allBackends,"on")==0){
  404. theConfig.allBackends = 1;
  405. } else {
  406. theConfig.allBackends = 0;
  407. }
  408. } else {
  409. theConfig.allBackends = 0;
  410. }
  411. /* release the lock */
  412. memberof_unlock_config();
  413. done:
  414. slapi_ch_free_string(&sharedcfg);
  415. slapi_sdn_free(&config_sdn);
  416. if(config_entry){
  417. /* we switched the entry pointer to the shared config entry - which needs to be freed */
  418. slapi_entry_free(e);
  419. }
  420. slapi_ch_array_free(groupattrs);
  421. slapi_ch_free_string(&memberof_attr);
  422. slapi_ch_free_string(&allBackends);
  423. if (*returncode != LDAP_SUCCESS)
  424. {
  425. return SLAPI_DSE_CALLBACK_ERROR;
  426. }
  427. else
  428. {
  429. return SLAPI_DSE_CALLBACK_OK;
  430. }
  431. }
  432. /*
  433. * memberof_copy_config()
  434. *
  435. * Makes a copy of the config in src. This function will free the
  436. * elements of dest if they already exist. This should only be called
  437. * if you hold the memberof config lock if src was obtained with
  438. * memberof_get_config().
  439. */
  440. void
  441. memberof_copy_config(MemberOfConfig *dest, MemberOfConfig *src)
  442. {
  443. if (dest && src)
  444. {
  445. /* Check if the copy is already up to date */
  446. if (src->groupattrs)
  447. {
  448. int i = 0, j = 0;
  449. /* Copy group attributes string list. */
  450. slapi_ch_array_free(dest->groupattrs);
  451. dest->groupattrs = slapi_ch_array_dup(src->groupattrs);
  452. /* Copy group check filter. */
  453. slapi_filter_free(dest->group_filter, 1);
  454. dest->group_filter = slapi_filter_dup(src->group_filter);
  455. /* Copy group attributes Slapi_Attr list.
  456. * First free the old list. */
  457. for (i = 0; dest->group_slapiattrs && dest->group_slapiattrs[i]; i++)
  458. {
  459. slapi_attr_free(&dest->group_slapiattrs[i]);
  460. }
  461. /* Count how many values we have in the source list. */
  462. for (j = 0; src->group_slapiattrs[j]; j++)
  463. {
  464. /* Do nothing. */
  465. }
  466. /* Realloc dest if necessary. */
  467. if (i < j)
  468. {
  469. dest->group_slapiattrs = (Slapi_Attr **)slapi_ch_realloc((char *)dest->group_slapiattrs, sizeof(Slapi_Attr *) * (j + 1));
  470. }
  471. /* Copy the attributes. */
  472. for (i = 0; src->group_slapiattrs[i]; i++)
  473. {
  474. dest->group_slapiattrs[i] = slapi_attr_dup(src->group_slapiattrs[i]);
  475. }
  476. /* Terminate the array. */
  477. dest->group_slapiattrs[i] = NULL;
  478. }
  479. if (src->memberof_attr)
  480. {
  481. slapi_ch_free_string(&dest->memberof_attr);
  482. dest->memberof_attr = slapi_ch_strdup(src->memberof_attr);
  483. }
  484. if(src->allBackends)
  485. {
  486. dest->allBackends = src->allBackends;
  487. }
  488. }
  489. }
  490. /*
  491. * memberof_free_config()
  492. *
  493. * Free's the contents of a config structure.
  494. */
  495. void
  496. memberof_free_config(MemberOfConfig *config)
  497. {
  498. if (config)
  499. {
  500. int i = 0;
  501. slapi_ch_array_free(config->groupattrs);
  502. slapi_filter_free(config->group_filter, 1);
  503. for (i = 0; config->group_slapiattrs && config->group_slapiattrs[i]; i++)
  504. {
  505. slapi_attr_free(&config->group_slapiattrs[i]);
  506. }
  507. slapi_ch_free((void **)&config->group_slapiattrs);
  508. slapi_ch_free_string(&config->memberof_attr);
  509. }
  510. }
  511. /*
  512. * memberof_get_config()
  513. *
  514. * Returns a pointer to the main config. You should call
  515. * memberof_rlock_config() first so the main config doesn't
  516. * get modified out from under you.
  517. */
  518. MemberOfConfig *
  519. memberof_get_config()
  520. {
  521. return &theConfig;
  522. }
  523. /*
  524. * memberof_rlock_config()
  525. *
  526. * Gets a non-exclusive lock on the main config. This will
  527. * prevent the config from being changed out from under you
  528. * while you read it, but it will still allow other threads
  529. * to read the config at the same time.
  530. */
  531. void
  532. memberof_rlock_config()
  533. {
  534. slapi_rwlock_rdlock(memberof_config_lock);
  535. }
  536. /*
  537. * memberof_wlock_config()
  538. *
  539. * Gets an exclusive lock on the main config. This should
  540. * be called if you need to write to the main config.
  541. */
  542. void
  543. memberof_wlock_config()
  544. {
  545. slapi_rwlock_wrlock(memberof_config_lock);
  546. }
  547. /*
  548. * memberof_unlock_config()
  549. *
  550. * Unlocks the main config.
  551. */
  552. void
  553. memberof_unlock_config()
  554. {
  555. slapi_rwlock_unlock(memberof_config_lock);
  556. }
  557. int
  558. memberof_config_get_all_backends()
  559. {
  560. int all_backends;
  561. slapi_rwlock_rdlock(memberof_config_lock);
  562. all_backends = theConfig.allBackends;
  563. slapi_rwlock_unlock(memberof_config_lock);
  564. return all_backends;
  565. }
  566. /*
  567. * Check if we are modifying the config, or changing the shared config entry
  568. */
  569. int
  570. memberof_shared_config_validate(Slapi_PBlock *pb)
  571. {
  572. Slapi_Entry *e = 0;
  573. Slapi_DN *sdn = 0;
  574. Slapi_Mods *smods = 0;
  575. LDAPMod **mods = NULL;
  576. char returntext[SLAPI_DSE_RETURNTEXT_SIZE];
  577. int ret = SLAPI_PLUGIN_SUCCESS;
  578. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  579. if (slapi_sdn_issuffix(sdn, memberof_get_config_area()) &&
  580. slapi_sdn_compare(sdn, memberof_get_config_area()) == 0)
  581. {
  582. /*
  583. * This is the shared config entry. Apply the mods and set/validate
  584. * the config
  585. */
  586. int result = 0;
  587. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &e);
  588. if(e){
  589. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  590. smods = slapi_mods_new();
  591. slapi_mods_init_byref(smods, mods);
  592. /* Apply the mods to create the resulting entry. */
  593. if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
  594. /* we don't care about this, the update is invalid and will be caught later */
  595. goto bail;
  596. }
  597. if ( SLAPI_DSE_CALLBACK_ERROR == memberof_validate_config (pb, NULL, e, &ret, returntext,0)) {
  598. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM,
  599. "%s", returntext);
  600. ret = LDAP_UNWILLING_TO_PERFORM;
  601. }
  602. } else {
  603. slapi_log_error( SLAPI_LOG_FATAL, MEMBEROF_PLUGIN_SUBSYSTEM, "memberof_shared_config_validate: "
  604. "Unable to locate shared config entry (%s) error %d\n",
  605. slapi_sdn_get_dn(memberof_get_config_area()), result);
  606. ret = LDAP_UNWILLING_TO_PERFORM;
  607. }
  608. }
  609. bail:
  610. slapi_entry_free(e);
  611. return ret;
  612. }