posix-group-task.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #include "slapi-plugin.h"
  2. #include "nspr.h"
  3. #include <string.h>
  4. #include "posix-wsp-ident.h"
  5. #include "posix-group-func.h"
  6. typedef struct _task_data
  7. {
  8. char *dn; /* search base */
  9. char *filter_str; /* search filter */
  10. } task_data;
  11. typedef struct _cb_data
  12. {
  13. char *dn;
  14. void *txn;
  15. } cb_data;
  16. /*
  17. typedef struct _posix_group_task_data
  18. {
  19. POSIX_WinSync_Config *config;
  20. Slapi_Value *memberdn_val;
  21. Slapi_ValueSet **uidvals;
  22. void *txn;
  23. } posix_group_data_data;
  24. */
  25. Slapi_Value **
  26. valueset_get_valuearray(const Slapi_ValueSet *vs); /* stolen from proto-slap.h */
  27. /* interface function */
  28. int
  29. posix_group_task_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode,
  30. char *returntext, void *arg);
  31. Slapi_Entry *
  32. getEntry(const char *udn, char **attrs);
  33. static void
  34. posix_group_task_destructor(Slapi_Task *task);
  35. static void
  36. posix_group_fixup_task_thread(void *arg);
  37. static int
  38. posix_group_fix_memberuid_callback(Slapi_Entry *e, void *callback_data);
  39. /* extract a single value from the entry (as a string) -- if it's not in the
  40. * entry, the default will be returned (which can be NULL).
  41. * you do not need to free anything returned by this.
  42. */
  43. static const char *
  44. fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val)
  45. {
  46. Slapi_Attr *attr;
  47. Slapi_Value *val = NULL;
  48. if (slapi_entry_attr_find(e, attrname, &attr) != 0)
  49. return default_val;
  50. slapi_attr_first_value(attr, &val);
  51. return slapi_value_get_string(val);
  52. }
  53. /* e configEntry */
  54. int
  55. posix_group_task_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter, int *returncode,
  56. char *returntext, void *arg)
  57. {
  58. PRThread *thread = NULL;
  59. int rv = SLAPI_DSE_CALLBACK_OK;
  60. task_data *mytaskdata = NULL;
  61. Slapi_Task *task = NULL;
  62. const char *filter;
  63. const char *dn = 0;
  64. *returncode = LDAP_SUCCESS;
  65. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  66. "posix_group_task_add: ==>\n");
  67. /* get arg(s) */
  68. /* default: set replication basedn */
  69. if ((dn = fetch_attr(e, "basedn", slapi_sdn_get_dn(posix_winsync_config_get_suffix()))) == NULL) {
  70. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  71. rv = SLAPI_DSE_CALLBACK_ERROR;
  72. goto out;
  73. }
  74. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  75. "posix_group_task_add: retrieved basedn: %s\n", dn);
  76. if ((filter = fetch_attr(e, "filter", "(&(objectclass=ntGroup)(|(uniquemember=*)(memberuid=*)))")) == NULL) {
  77. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  78. rv = SLAPI_DSE_CALLBACK_ERROR;
  79. goto out;
  80. }
  81. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  82. "posix_group_task_add: retrieved filter: %s\n", filter);
  83. /* setup our task data */
  84. mytaskdata = (task_data*) slapi_ch_malloc(sizeof(task_data));
  85. if (mytaskdata == NULL) {
  86. *returncode = LDAP_OPERATIONS_ERROR;
  87. rv = SLAPI_DSE_CALLBACK_ERROR;
  88. goto out;
  89. }
  90. mytaskdata->dn = slapi_ch_strdup(dn);
  91. mytaskdata->filter_str = slapi_ch_strdup(filter);
  92. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  93. "posix_group_task_add: task data allocated\n");
  94. /* allocate new task now */
  95. char * ndn = slapi_entry_get_ndn(e);
  96. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  97. "posix_group_task_add: creating task object: %s\n",
  98. ndn);
  99. task = slapi_new_task(ndn);
  100. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  101. "posix_group_task_add: task object created\n");
  102. /* register our destructor for cleaning up our private data */
  103. slapi_task_set_destructor_fn(task, posix_group_task_destructor);
  104. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  105. "posix_group_task_add: task destructor set\n");
  106. /* Stash a pointer to our data in the task */
  107. slapi_task_set_data(task, mytaskdata);
  108. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  109. "posix_group_task_add: task object initialized\n");
  110. /* start the sample task as a separate thread */
  111. thread = PR_CreateThread(PR_USER_THREAD, posix_group_fixup_task_thread, (void *) task,
  112. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
  113. SLAPD_DEFAULT_THREAD_STACKSIZE);
  114. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  115. "posix_group_task_add: thread created\n");
  116. if (thread == NULL) {
  117. slapi_log_error(SLAPI_LOG_FATAL, POSIX_WINSYNC_PLUGIN_NAME,
  118. "unable to create task thread!\n");
  119. *returncode = LDAP_OPERATIONS_ERROR;
  120. rv = SLAPI_DSE_CALLBACK_ERROR;
  121. slapi_task_finish(task, *returncode);
  122. } else {
  123. rv = SLAPI_DSE_CALLBACK_OK;
  124. }
  125. out:
  126. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  127. "posix_group_task_add: <==\n");
  128. return rv;
  129. }
  130. static void
  131. posix_group_task_destructor(Slapi_Task *task)
  132. {
  133. if (task) {
  134. task_data *mydata = (task_data *) slapi_task_get_data(task);
  135. if (mydata) {
  136. slapi_ch_free_string(&mydata->dn);
  137. slapi_ch_free_string(&mydata->filter_str);
  138. /* Need to cast to avoid a compiler warning */
  139. slapi_ch_free((void **) &mydata);
  140. }
  141. }
  142. }
  143. #if 0 /* NOT USED */
  144. static int
  145. posix_group_del_memberuid_callback(Slapi_Entry *e, void *callback_data)
  146. {
  147. int rc = 0;
  148. LDAPMod mod;
  149. LDAPMod *mods[2];
  150. char *val[2];
  151. Slapi_PBlock *mod_pb = 0;
  152. cb_data *the_cb_data = (cb_data *) callback_data;
  153. mod_pb = slapi_pblock_new();
  154. mods[0] = &mod;
  155. mods[1] = 0;
  156. val[0] = 0; /* all */
  157. val[1] = 0;
  158. mod.mod_op = LDAP_MOD_DELETE;
  159. mod.mod_type = "memberuid";
  160. mod.mod_values = val;
  161. slapi_modify_internal_set_pb_ext(mod_pb, slapi_entry_get_sdn(e), mods, 0, 0,
  162. posix_winsync_get_plugin_identity(), 0);
  163. slapi_pblock_set(mod_pb, SLAPI_TXN, the_cb_data->txn);
  164. slapi_modify_internal_pb(mod_pb);
  165. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  166. slapi_pblock_destroy(mod_pb);
  167. return rc;
  168. }
  169. #endif
  170. static int
  171. posix_group_fix_memberuid(char *dn, char *filter_str, void *txn)
  172. {
  173. int rc = 0;
  174. struct _cb_data callback_data = { dn, txn };
  175. Slapi_PBlock *search_pb = slapi_pblock_new();
  176. /* char *attrs[]={"uniquemember","memberuid",NULL}; */
  177. slapi_search_internal_set_pb(search_pb, dn, LDAP_SCOPE_SUBTREE, filter_str, 0, 0, 0, 0,
  178. posix_winsync_get_plugin_identity(), 0);
  179. slapi_pblock_set(search_pb, SLAPI_TXN, txn); /* set transaction id */
  180. rc = slapi_search_internal_callback_pb(search_pb, &callback_data, 0,
  181. posix_group_fix_memberuid_callback, 0);
  182. slapi_pblock_destroy(search_pb);
  183. return rc;
  184. }
  185. /* posix_group_fix_memberuid_callback()
  186. * Add initial and/or fix up broken group list in entry
  187. *
  188. * 1. forall uniquemember search if posixAccount ? add uid : ""
  189. */
  190. static int
  191. posix_group_fix_memberuid_callback(Slapi_Entry *e, void *callback_data)
  192. {
  193. int i;
  194. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  195. "_fix_memberuid ==>\n");
  196. cb_data *the_cb_data = (cb_data *) callback_data;
  197. int rc;
  198. Slapi_Attr *muid_attr = NULL;
  199. Slapi_Value *v = NULL;
  200. Slapi_Mods *smods = slapi_mods_new();
  201. char *dn = slapi_entry_get_dn(e);
  202. Slapi_DN *sdn = slapi_entry_get_sdn(e);
  203. LDAPMod **mods = NULL;
  204. int is_posix_group = 0;
  205. if (hasObjectClass(e, "posixGroup")) {
  206. is_posix_group = 1;
  207. }
  208. /* Clean out memberuids and dsonlymemberuids without a valid referant */
  209. rc = slapi_entry_attr_find(e, "memberuid", &muid_attr);
  210. if (rc == 0 && muid_attr) {
  211. Slapi_PBlock *search_pb = slapi_pblock_new();
  212. Slapi_Attr *dsmuid_attr = NULL;
  213. Slapi_ValueSet *dsmuid_vs = NULL;
  214. char *attrs[] = { "uid", NULL };
  215. rc = slapi_entry_attr_find(e, "dsonlymemberuid", &dsmuid_attr);
  216. if (rc == 0 && dsmuid_attr) {
  217. slapi_attr_get_valueset(dsmuid_attr, &dsmuid_vs);
  218. }
  219. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  220. "_fix_memberuid scan for orphaned memberuids\n");
  221. for (i = slapi_attr_first_value(muid_attr, &v); i != -1;
  222. i = slapi_attr_next_value(muid_attr, i, &v)) {
  223. char *muid = (char *)slapi_value_get_string(v);
  224. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  225. "_fix_memberuid iterating memberuid: %s\n",
  226. muid);
  227. size_t vallen = muid ? strlen(muid) : 0;
  228. char *filter_escaped_value = slapi_escape_filter_value(muid, vallen);
  229. char *filter = slapi_ch_smprintf("(uid=%s)", filter_escaped_value);
  230. slapi_ch_free_string(&filter_escaped_value);
  231. Slapi_Entry **search_entries = NULL;
  232. slapi_search_internal_set_pb(search_pb,
  233. the_cb_data->dn,
  234. LDAP_SCOPE_SUBTREE,
  235. filter,
  236. attrs, 0, NULL, NULL,
  237. posix_winsync_get_plugin_identity(), 0);
  238. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  239. "_fix_memberuid searching %s with filter: %s\n",
  240. the_cb_data->dn, filter);
  241. rc = slapi_search_internal_pb(search_pb);
  242. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &search_entries);
  243. if (!search_entries || !search_entries[0]) {
  244. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  245. "_fix_memberuid Adding bad memberuid %s\n",
  246. slapi_value_get_string(v));
  247. slapi_mods_add_string(smods, LDAP_MOD_DELETE, "memberuid", slapi_value_get_string(v));
  248. if (dsmuid_vs && slapi_valueset_find(dsmuid_attr, dsmuid_vs, v)) {
  249. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  250. "_fix_memberuid Adding bad dsonlymemberuid %s\n",
  251. slapi_value_get_string(v));
  252. slapi_mods_add_string(smods, LDAP_MOD_DELETE, "dsonlymemberuid", slapi_value_get_string(v));
  253. }
  254. }
  255. slapi_free_search_results_internal(search_pb);
  256. slapi_pblock_init(search_pb);
  257. slapi_ch_free_string(&filter);
  258. }
  259. if (dsmuid_vs) {
  260. slapi_valueset_free(dsmuid_vs); dsmuid_vs = NULL;
  261. }
  262. slapi_pblock_destroy(search_pb); search_pb = NULL;
  263. }
  264. /* Cleanup uniquemembers without a referent, and verify memberuid otherwise */
  265. Slapi_Attr *obj_attr = NULL;
  266. rc = slapi_entry_attr_find(e, "uniquemember", &obj_attr);
  267. if (rc == 0 && obj_attr) {
  268. int fixMembership = 0;
  269. Slapi_ValueSet *bad_ums = NULL;
  270. Slapi_Value *uniqval = NULL; /* uniquemeber Attribute values */
  271. Slapi_ValueSet *uids = NULL;
  272. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  273. "_fix_memberuid scan uniquemember, group %s\n", dn);
  274. for (i = slapi_attr_first_value(obj_attr, &uniqval); i != -1;
  275. i = slapi_attr_next_value(obj_attr, i, &uniqval)) {
  276. const char *member = slapi_value_get_string(uniqval);
  277. char *attrs[] = { "uid", "objectclass", NULL };
  278. Slapi_Entry *child = getEntry(member, attrs);
  279. if (child) {
  280. slapi_entry_free(child);
  281. } else {
  282. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  283. "_fix_memberuid orphaned uniquemember found: %s\n", member);
  284. if ((strncasecmp(member, "cn=", 3) == 0) ||
  285. (strncasecmp(member, "uid=", 4) == 0)) {
  286. fixMembership = 1;
  287. }
  288. if (!bad_ums) {
  289. bad_ums = slapi_valueset_new();
  290. }
  291. slapi_valueset_add_value(bad_ums, uniqval);
  292. }
  293. if (is_posix_group) {
  294. char *uid = NULL;
  295. /* search uid for member (DN) */
  296. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME, "search %s\n", member);
  297. if ((uid = searchUid(member)) != NULL) {
  298. Slapi_Value *value = slapi_value_new();
  299. /* Search an entry having "member" as DN and get uid value from it. */
  300. slapi_value_set_string_passin(value, uid);
  301. /* add uids ValueSet */
  302. if (NULL == uids) {
  303. uids = slapi_valueset_new();
  304. }
  305. slapi_valueset_add_value(uids, value);
  306. slapi_value_free(&value);
  307. }
  308. }
  309. }
  310. /* If we found some posix members, replace the existing memberuid attribute
  311. * with the found values. */
  312. if (uids && slapi_valueset_count(uids)) {
  313. Slapi_Value *val = 0;
  314. Slapi_Mod *smod = slapi_mod_new();
  315. int hint = 0;
  316. slapi_mod_init(smod, 0);
  317. slapi_mod_set_operation(smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  318. slapi_mod_set_type(smod, "memberuid");
  319. /* Loop through all of our values and add them to smod */
  320. hint = slapi_valueset_first_value(uids, &val);
  321. while (val) {
  322. /* this makes a copy of the berval */
  323. slapi_mod_add_value(smod, slapi_value_get_berval(val));
  324. hint = slapi_valueset_next_value(uids, hint, &val);
  325. }
  326. slapi_mods_add_ldapmod(smods, slapi_mod_get_ldapmod_passout(smod));
  327. slapi_mod_free(&smod);
  328. }
  329. slapi_valueset_free(uids);
  330. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  331. "_fix_memberuid Finishing...\n");
  332. if (fixMembership && posix_winsync_config_get_mapNestedGrouping()) {
  333. Slapi_ValueSet *del_nested_vs = slapi_valueset_new();
  334. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  335. "_fix_memberuid group deleted, recalculating nesting\n");
  336. propogateDeletionsUpward(e, sdn, bad_ums, del_nested_vs, 0);
  337. slapi_valueset_free(del_nested_vs); del_nested_vs = NULL;
  338. }
  339. if (bad_ums) {
  340. slapi_mods_add_mod_values(smods, LDAP_MOD_DELETE, "uniquemember", valueset_get_valuearray(bad_ums));
  341. slapi_valueset_free(bad_ums); bad_ums = NULL;
  342. }
  343. }
  344. mods = slapi_mods_get_ldapmods_byref(smods);
  345. if (mods) {
  346. Slapi_PBlock *mod_pb = NULL;
  347. mod_pb = slapi_pblock_new();
  348. slapi_modify_internal_set_pb_ext(mod_pb, sdn, mods, 0, 0,
  349. posix_winsync_get_plugin_identity(), 0);
  350. slapi_pblock_set(mod_pb, SLAPI_TXN, the_cb_data->txn);
  351. slapi_modify_internal_pb(mod_pb);
  352. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  353. slapi_pblock_destroy(mod_pb);
  354. }
  355. slapi_mods_free(&smods);
  356. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  357. "_fix_memberuid <==\n");
  358. /*
  359. * Since Ticket #481 "expand nested posix groups",
  360. * there's a possibility the found entry does not contain
  361. * uniqueMember attribute. But "not found" error shoud not
  362. * be returned, which stops the further fixup task.
  363. */
  364. return 0;
  365. }
  366. static void
  367. posix_group_fixup_task_thread(void *arg)
  368. {
  369. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  370. "_task_thread ==>\n");
  371. Slapi_Task *task = (Slapi_Task *) arg;
  372. task_data *td = NULL;
  373. int rc = 0;
  374. /* Fetch our task data from the task */
  375. td = (task_data *) slapi_task_get_data(task);
  376. slapi_task_begin(task, 1);
  377. slapi_task_log_notice(task, "posix_group task starts (arg: %s) ...\n", td->filter_str);
  378. /* get the memberOf operation lock */
  379. memberUidLock();
  380. /* do real work */
  381. rc = posix_group_fix_memberuid(td->dn, td->filter_str, NULL /* no txn? */);
  382. /* release the memberOf operation lock */
  383. memberUidUnlock();
  384. slapi_task_log_notice(task, "posix_group task finished.");
  385. slapi_task_log_status(task, "posix_group task finished.");
  386. slapi_task_inc_progress(task);
  387. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  388. "_task_thread finishing\n");
  389. /* this will queue the destruction of the task */
  390. slapi_task_finish(task, rc);
  391. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  392. "_task_thread <==\n");
  393. }