memberof_config.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 PRRWLock *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 = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "memberof_config_lock");
  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 *attr = NULL;
  145. *returncode = LDAP_UNWILLING_TO_PERFORM; /* be pessimistic */
  146. /* Make sure both the group attr and the memberOf attr
  147. * config atributes are supplied. We don't care about &attr
  148. * here, but slapi_entry_attr_find() requires us to pass it. */
  149. if (!slapi_entry_attr_find(e, MEMBEROF_GROUP_ATTR, &attr) &&
  150. !slapi_entry_attr_find(e, MEMBEROF_ATTR, &attr))
  151. {
  152. *returncode = LDAP_SUCCESS;
  153. } else {
  154. PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  155. "The %s and %s configuration attributes must be provided",
  156. MEMBEROF_GROUP_ATTR, MEMBEROF_ATTR);
  157. }
  158. if (*returncode != LDAP_SUCCESS)
  159. {
  160. return SLAPI_DSE_CALLBACK_ERROR;
  161. }
  162. else
  163. {
  164. return SLAPI_DSE_CALLBACK_OK;
  165. }
  166. }
  167. /*
  168. * memberof_apply_config()
  169. *
  170. * Apply the pending changes in the e entry to our config struct.
  171. * memberof_validate_config() must have already been called.
  172. */
  173. static int
  174. memberof_apply_config (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e,
  175. int *returncode, char *returntext, void *arg)
  176. {
  177. char *groupattr = NULL;
  178. char *memberof_attr = NULL;
  179. char *filter_str = NULL;
  180. *returncode = LDAP_SUCCESS;
  181. groupattr = slapi_entry_attr_get_charptr(e, MEMBEROF_GROUP_ATTR);
  182. memberof_attr = slapi_entry_attr_get_charptr(e, MEMBEROF_ATTR);
  183. /* We want to be sure we don't change the config in the middle of
  184. * a memberOf operation, so we obtain an exclusive lock here */
  185. memberof_wlock_config();
  186. if (!theConfig.groupattr ||
  187. (groupattr && PL_strcmp(theConfig.groupattr, groupattr))) {
  188. slapi_ch_free_string(&theConfig.groupattr);
  189. theConfig.groupattr = groupattr;
  190. groupattr = NULL; /* config now owns memory */
  191. /* We allocate a Slapi_Attr using the groupattr for
  192. * convenience in our memberOf comparison functions */
  193. slapi_attr_free(&theConfig.group_slapiattr);
  194. theConfig.group_slapiattr = slapi_attr_new();
  195. slapi_attr_init(theConfig.group_slapiattr, theConfig.groupattr);
  196. /* The filter is based off of the groupattr, so we
  197. * update it here too. */
  198. slapi_filter_free(theConfig.group_filter, 1);
  199. filter_str = slapi_ch_smprintf("(%s=*)", theConfig.groupattr);
  200. theConfig.group_filter = slapi_str2filter(filter_str);
  201. slapi_ch_free_string(&filter_str);
  202. }
  203. if (!theConfig.memberof_attr ||
  204. (memberof_attr && PL_strcmp(theConfig.memberof_attr, memberof_attr))) {
  205. slapi_ch_free_string(&theConfig.memberof_attr);
  206. theConfig.memberof_attr = memberof_attr;
  207. memberof_attr = NULL; /* config now owns memory */
  208. }
  209. /* release the lock */
  210. memberof_unlock_config();
  211. slapi_ch_free_string(&groupattr);
  212. slapi_ch_free_string(&memberof_attr);
  213. if (*returncode != LDAP_SUCCESS)
  214. {
  215. return SLAPI_DSE_CALLBACK_ERROR;
  216. }
  217. else
  218. {
  219. return SLAPI_DSE_CALLBACK_OK;
  220. }
  221. }
  222. /*
  223. * memberof_copy_config()
  224. *
  225. * Makes a copy of the config in src. This function will free the
  226. * elements of dest if they already exist. This should only be called
  227. * if you hold the memberof config lock if src was obtained with
  228. * memberof_get_config().
  229. */
  230. void
  231. memberof_copy_config(MemberOfConfig *dest, MemberOfConfig *src)
  232. {
  233. if (dest && src)
  234. {
  235. /* Check if the copy is already up to date */
  236. if (!dest->groupattr || (src->groupattr
  237. && PL_strcmp(dest->groupattr, src->groupattr)))
  238. {
  239. slapi_ch_free_string(&dest->groupattr);
  240. dest->groupattr = slapi_ch_strdup(src->groupattr);
  241. slapi_filter_free(dest->group_filter, 1);
  242. dest->group_filter = slapi_filter_dup(src->group_filter);
  243. slapi_attr_free(&dest->group_slapiattr);
  244. dest->group_slapiattr = slapi_attr_dup(src->group_slapiattr);
  245. }
  246. if (!dest->memberof_attr || (src->memberof_attr
  247. && PL_strcmp(dest->memberof_attr, src->memberof_attr)))
  248. {
  249. slapi_ch_free_string(&dest->memberof_attr);
  250. dest->memberof_attr = slapi_ch_strdup(src->memberof_attr);
  251. }
  252. }
  253. }
  254. /*
  255. * memberof_free_config()
  256. *
  257. * Free's the contents of a config structure.
  258. */
  259. void
  260. memberof_free_config(MemberOfConfig *config)
  261. {
  262. if (config)
  263. {
  264. slapi_ch_free_string(&config->groupattr);
  265. slapi_filter_free(config->group_filter, 1);
  266. slapi_attr_free(&config->group_slapiattr);
  267. slapi_ch_free_string(&config->memberof_attr);
  268. }
  269. }
  270. /*
  271. * memberof_get_config()
  272. *
  273. * Returns a pointer to the main config. You should call
  274. * memberof_rlock_config() first so the main config doesn't
  275. * get modified out from under you.
  276. */
  277. MemberOfConfig *
  278. memberof_get_config()
  279. {
  280. return &theConfig;
  281. }
  282. /*
  283. * memberof_rlock_config()
  284. *
  285. * Gets a non-exclusive lock on the main config. This will
  286. * prevent the config from being changed out from under you
  287. * while you read it, but it will still allow other threads
  288. * to read the config at the same time.
  289. */
  290. void
  291. memberof_rlock_config()
  292. {
  293. PR_RWLock_Rlock(memberof_config_lock);
  294. }
  295. /*
  296. * memberof_wlock_config()
  297. *
  298. * Gets an exclusive lock on the main config. This should
  299. * be called if you need to write to the main config.
  300. */
  301. void
  302. memberof_wlock_config()
  303. {
  304. PR_RWLock_Wlock(memberof_config_lock);
  305. }
  306. /*
  307. * memberof_unlock_config()
  308. *
  309. * Unlocks the main config.
  310. */
  311. void
  312. memberof_unlock_config()
  313. {
  314. PR_RWLock_Unlock(memberof_config_lock);
  315. }