posix-winsync-config.c 9.8 KB

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