posix-group-task.c 18 KB

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