posix-winsync-config.c 11 KB

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