posix-winsync-config.c 11 KB

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