posix-winsync-config.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2011 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. /*
  9. $Id: posix-winsync-config.c 42 2011-06-10 08:39:50Z grzemba $
  10. $HeadURL: file:///storejet/svn/posix-winsync-plugin/trunk/posix-winsync-config.c $
  11. */
  12. #include "posix-wsp-ident.h"
  13. #include <string.h>
  14. #include "posix-group-func.h"
  15. #ifdef WINSYNC_TEST_POSIX
  16. #include <slapi-plugin.h>
  17. #include "winsync-plugin.h"
  18. #else
  19. #include <dirsrv/slapi-plugin.h>
  20. #include <dirsrv/winsync-plugin.h>
  21. #endif
  22. #define POSIX_WINSYNC_CONFIG_FILTER "(objectclass=*)"
  23. /*
  24. * static variables
  25. */
  26. /* for now, there is only one configuration and it is global to the plugin */
  27. static POSIX_WinSync_Config theConfig;
  28. static int inited = 0;
  29. /* This is called when a new agreement is created or loaded
  30. at startup.
  31. */
  32. void *
  33. posix_winsync_agmt_init(const Slapi_DN *ds_subtree, const Slapi_DN *ad_subtree)
  34. {
  35. void *cbdata = NULL;
  36. void *node = NULL;
  37. Slapi_DN *sdn = NULL;
  38. plugin_op_started();
  39. if (!get_plugin_started()) {
  40. plugin_op_finished();
  41. return NULL;
  42. }
  43. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  44. "--> posix_winsync_agmt_init [%s] [%s] -- begin\n",
  45. slapi_sdn_get_dn(ds_subtree), slapi_sdn_get_dn(ad_subtree));
  46. sdn = slapi_get_first_suffix(&node, 0);
  47. while (sdn) {
  48. /* if sdn is a parent of ds_subtree or sdn is the WinSync Subtree itself */
  49. if (slapi_sdn_isparent(sdn, ds_subtree) || !slapi_sdn_compare(sdn, ds_subtree)) {
  50. theConfig.rep_suffix = sdn;
  51. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME, "posix_winsync_agmt_init - "
  52. "Found suffix's '%s'\n",
  53. slapi_sdn_get_dn(sdn));
  54. break;
  55. }
  56. sdn = slapi_get_next_suffix(&node, 0);
  57. }
  58. if (!sdn) {
  59. char *pardn = slapi_dn_parent(slapi_sdn_get_dn(ds_subtree));
  60. slapi_log_err(SLAPI_LOG_ERR, POSIX_WINSYNC_PLUGIN_NAME, "posix_winsync_agmt_init - "
  61. "suffix not found for '%s'\n",
  62. pardn);
  63. slapi_ch_free_string(&pardn);
  64. }
  65. plugin_op_finished();
  66. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  67. "<-- posix_winsync_agmt_init -- end\n");
  68. return cbdata;
  69. }
  70. static int
  71. posix_winsync_apply_config(Slapi_PBlock *pb, Slapi_Entry *entryBefore, Slapi_Entry *e, int *returncode, char *returntext, void *arg);
  72. POSIX_WinSync_Config *
  73. posix_winsync_get_config()
  74. {
  75. return &theConfig;
  76. }
  77. PRBool
  78. posix_winsync_config_get_mapMemberUid()
  79. {
  80. return theConfig.mapMemberUID;
  81. }
  82. PRBool
  83. posix_winsync_config_get_lowercase()
  84. {
  85. return theConfig.lowercase;
  86. }
  87. PRBool
  88. posix_winsync_config_get_createMOFTask()
  89. {
  90. return theConfig.createMemberOfTask;
  91. }
  92. void
  93. posix_winsync_config_set_MOFTaskCreated()
  94. {
  95. theConfig.MOFTaskCreated = PR_TRUE;
  96. }
  97. void
  98. posix_winsync_config_reset_MOFTaskCreated()
  99. {
  100. theConfig.MOFTaskCreated = PR_FALSE;
  101. }
  102. PRBool
  103. posix_winsync_config_get_MOFTaskCreated()
  104. {
  105. return theConfig.MOFTaskCreated;
  106. }
  107. PRBool
  108. posix_winsync_config_get_msSFUSchema()
  109. {
  110. return theConfig.mssfuSchema;
  111. }
  112. PRBool
  113. posix_winsync_config_get_mapNestedGrouping()
  114. {
  115. return theConfig.mapNestedGrouping;
  116. }
  117. Slapi_DN *
  118. posix_winsync_config_get_suffix()
  119. {
  120. return theConfig.rep_suffix;
  121. }
  122. /*
  123. * Read configuration and create a configuration data structure.
  124. * This is called after the server has configured itself so we can check
  125. * schema and whatnot.
  126. * Returns an LDAP error code (LDAP_SUCCESS if all goes well).
  127. */
  128. int
  129. posix_winsync_config(Slapi_Entry *config_e)
  130. {
  131. int returncode = LDAP_SUCCESS;
  132. char returntext[SLAPI_DSE_RETURNTEXT_SIZE];
  133. /* basic init */
  134. theConfig.config_e = NULL;
  135. theConfig.lock = NULL;
  136. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME, "--> _config %s -- begin\n",
  137. slapi_entry_get_dn_const(config_e));
  138. if (inited) {
  139. slapi_log_err(SLAPI_LOG_ERR, POSIX_WINSYNC_PLUGIN_NAME,
  140. "posix_winsync_config - POSIX WinSync plug-in already configured. "
  141. "Please remove the plugin config entry [%s]\n",
  142. slapi_entry_get_dn_const(config_e));
  143. return (LDAP_PARAM_ERROR);
  144. }
  145. /* initialize fields */
  146. if ((theConfig.lock = slapi_new_mutex()) == NULL) {
  147. return (LDAP_LOCAL_ERROR);
  148. }
  149. /* init defaults */
  150. theConfig.config_e = slapi_entry_alloc();
  151. slapi_entry_init(theConfig.config_e, slapi_ch_strdup(""), NULL);
  152. theConfig.mssfuSchema = PR_FALSE;
  153. theConfig.mapMemberUID = PR_TRUE;
  154. theConfig.lowercase = PR_FALSE;
  155. theConfig.createMemberOfTask = PR_FALSE;
  156. theConfig.MOFTaskCreated = PR_FALSE;
  157. theConfig.mapNestedGrouping = PR_FALSE;
  158. posix_winsync_apply_config(NULL, NULL, config_e, &returncode, returntext, NULL);
  159. /* config DSE must be initialized before we get here */
  160. {
  161. int rc = 0;
  162. const char *config_dn = slapi_entry_get_dn_const(config_e);
  163. if (!memberUidLockInit()) {
  164. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  165. "posix_winsync_config - init Monitor failed\n");
  166. }
  167. slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_POSTOP, config_dn,
  168. LDAP_SCOPE_BASE, POSIX_WINSYNC_CONFIG_FILTER,
  169. posix_winsync_apply_config, NULL);
  170. rc = slapi_task_register_handler("memberuid task", posix_group_task_add);
  171. if (rc) {
  172. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  173. "posix_winsync_config - register memberuid task failed\n");
  174. }
  175. }
  176. inited = 1;
  177. if (returncode != LDAP_SUCCESS) {
  178. slapi_log_err(SLAPI_LOG_ERR, POSIX_WINSYNC_PLUGIN_NAME, "posix_winsync_config - Error %d: %s\n", returncode,
  179. returntext);
  180. }
  181. return returncode;
  182. }
  183. void
  184. posix_winsync_config_free()
  185. {
  186. slapi_plugin_task_unregister_handler("memberuid task", posix_group_task_add);
  187. slapi_entry_free(theConfig.config_e);
  188. theConfig.config_e = NULL;
  189. slapi_destroy_mutex(theConfig.lock);
  190. theConfig.lock = NULL;
  191. memberUidLockDestroy();
  192. inited = 0;
  193. }
  194. static int
  195. posix_winsync_apply_config(Slapi_PBlock *pb __attribute__((unused)),
  196. Slapi_Entry *entryBefore __attribute__((unused)),
  197. Slapi_Entry *e,
  198. int *returncode,
  199. char *returntext __attribute__((unused)),
  200. void *arg __attribute__((unused)))
  201. {
  202. PRBool mssfuSchema = PR_FALSE;
  203. PRBool mapMemberUID = PR_TRUE;
  204. PRBool createMemberOfTask = PR_FALSE;
  205. PRBool lowercase = PR_FALSE;
  206. Slapi_Attr *testattr = NULL;
  207. PRBool mapNestedGrouping = PR_FALSE;
  208. *returncode = LDAP_UNWILLING_TO_PERFORM; /* be pessimistic */
  209. /* get msfuSchema value */
  210. if (!slapi_entry_attr_find(e, POSIX_WINSYNC_MSSFU_SCHEMA, &testattr) && (NULL != testattr)) {
  211. mssfuSchema = slapi_entry_attr_get_bool(e, POSIX_WINSYNC_MSSFU_SCHEMA);
  212. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  213. "posix_winsync_apply_config: Config parameter %s: %d\n", POSIX_WINSYNC_MSSFU_SCHEMA,
  214. mssfuSchema);
  215. }
  216. /* get memberUid value */
  217. if (!slapi_entry_attr_find(e, POSIX_WINSYNC_MAP_MEMBERUID, &testattr) && (NULL != testattr)) {
  218. mapMemberUID = slapi_entry_attr_get_bool(e, POSIX_WINSYNC_MAP_MEMBERUID);
  219. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  220. "posix_winsync_apply_config: Config parameter %s: %d\n", POSIX_WINSYNC_MAP_MEMBERUID,
  221. mapMemberUID);
  222. }
  223. /* get create task value */
  224. if (!slapi_entry_attr_find(e, POSIX_WINSYNC_CREATE_MEMBEROFTASK, &testattr) && (NULL != testattr)) {
  225. createMemberOfTask = slapi_entry_attr_get_bool(e, POSIX_WINSYNC_CREATE_MEMBEROFTASK);
  226. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  227. "posix_winsync_apply_config: Config parameter %s: %d\n",
  228. POSIX_WINSYNC_CREATE_MEMBEROFTASK, createMemberOfTask);
  229. }
  230. /* get lower case UID in memberUID */
  231. if (!slapi_entry_attr_find(e, POSIX_WINSYNC_LOWER_CASE, &testattr) && (NULL != testattr)) {
  232. lowercase = slapi_entry_attr_get_bool(e, POSIX_WINSYNC_LOWER_CASE);
  233. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  234. "posix_winsync_apply_config: Config parameter %s: %d\n", POSIX_WINSYNC_LOWER_CASE,
  235. lowercase);
  236. }
  237. /* propogate memberuids in nested grouping */
  238. if (!slapi_entry_attr_find(e, POSIX_WINSYNC_MAP_NESTED_GROUPING, &testattr) && (NULL != testattr)) {
  239. mapNestedGrouping = slapi_entry_attr_get_bool(e, POSIX_WINSYNC_MAP_NESTED_GROUPING);
  240. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  241. "posix_winsync_apply_config: Config parameter %s: %d\n", POSIX_WINSYNC_MAP_NESTED_GROUPING,
  242. mapNestedGrouping);
  243. }
  244. /* if we got here, we have valid values for everything
  245. set the config entry */
  246. slapi_lock_mutex(theConfig.lock);
  247. slapi_entry_free(theConfig.config_e);
  248. theConfig.config_e = slapi_entry_alloc();
  249. slapi_entry_init(theConfig.config_e, slapi_ch_strdup(""), NULL);
  250. /* all of the attrs and vals have been set - set the other values */
  251. theConfig.mssfuSchema = mssfuSchema;
  252. theConfig.mapMemberUID = mapMemberUID;
  253. theConfig.createMemberOfTask = createMemberOfTask;
  254. theConfig.lowercase = lowercase;
  255. theConfig.mapNestedGrouping = mapNestedGrouping;
  256. /* success */
  257. slapi_log_err(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  258. "<-- posix_winsync_apply_config: config evaluated\n");
  259. *returncode = LDAP_SUCCESS;
  260. slapi_unlock_mutex(theConfig.lock);
  261. if (*returncode != LDAP_SUCCESS) {
  262. return SLAPI_DSE_CALLBACK_ERROR;
  263. } else {
  264. return SLAPI_DSE_CALLBACK_OK;
  265. }
  266. }