posix-group-task.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. while (slapi_task_get_refcount(task) > 0) {
  136. /* Yield to wait for the fixup task finishes. */
  137. DS_Sleep (PR_MillisecondsToInterval(100));
  138. }
  139. if (mydata) {
  140. slapi_ch_free_string(&mydata->dn);
  141. slapi_ch_free_string(&mydata->filter_str);
  142. /* Need to cast to avoid a compiler warning */
  143. slapi_ch_free((void **) &mydata);
  144. }
  145. }
  146. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  147. "posix_group_task_destructor <--\n");
  148. }
  149. #if 0 /* NOT USED */
  150. static int
  151. posix_group_del_memberuid_callback(Slapi_Entry *e, void *callback_data)
  152. {
  153. int rc = 0;
  154. LDAPMod mod;
  155. LDAPMod *mods[2];
  156. char *val[2];
  157. Slapi_PBlock *mod_pb = 0;
  158. cb_data *the_cb_data = (cb_data *) callback_data;
  159. mod_pb = slapi_pblock_new();
  160. mods[0] = &mod;
  161. mods[1] = 0;
  162. val[0] = 0; /* all */
  163. val[1] = 0;
  164. mod.mod_op = LDAP_MOD_DELETE;
  165. mod.mod_type = "memberuid";
  166. mod.mod_values = val;
  167. slapi_modify_internal_set_pb_ext(mod_pb, slapi_entry_get_sdn(e), mods, 0, 0,
  168. posix_winsync_get_plugin_identity(), 0);
  169. slapi_pblock_set(mod_pb, SLAPI_TXN, the_cb_data->txn);
  170. slapi_modify_internal_pb(mod_pb);
  171. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  172. slapi_pblock_destroy(mod_pb);
  173. return rc;
  174. }
  175. #endif
  176. static int
  177. posix_group_fix_memberuid(char *dn, char *filter_str, void *txn)
  178. {
  179. int rc = 0;
  180. struct _cb_data callback_data = { dn, txn };
  181. Slapi_PBlock *search_pb = slapi_pblock_new();
  182. /* char *attrs[]={"uniquemember","memberuid",NULL}; */
  183. slapi_search_internal_set_pb(search_pb, dn, LDAP_SCOPE_SUBTREE, filter_str, 0, 0, 0, 0,
  184. posix_winsync_get_plugin_identity(), 0);
  185. slapi_pblock_set(search_pb, SLAPI_TXN, txn); /* set transaction id */
  186. rc = slapi_search_internal_callback_pb(search_pb, &callback_data, 0,
  187. posix_group_fix_memberuid_callback, 0);
  188. slapi_pblock_destroy(search_pb);
  189. return rc;
  190. }
  191. /* posix_group_fix_memberuid_callback()
  192. * Add initial and/or fix up broken group list in entry
  193. *
  194. * 1. forall uniquemember search if posixAccount ? add uid : ""
  195. */
  196. static int
  197. posix_group_fix_memberuid_callback(Slapi_Entry *e, void *callback_data)
  198. {
  199. int i;
  200. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  201. "_fix_memberuid ==>\n");
  202. cb_data *the_cb_data = (cb_data *) callback_data;
  203. int rc = 0;
  204. Slapi_Attr *muid_attr = NULL;
  205. Slapi_Value *v = NULL;
  206. Slapi_Mods *smods = NULL;
  207. char *dn = NULL;
  208. Slapi_DN *sdn = NULL;
  209. LDAPMod **mods = NULL;
  210. int is_posix_group = 0;
  211. /*
  212. * If the server is ordered to shutdown, stop the fixup and return an error.
  213. */
  214. if (slapi_is_shutting_down()) {
  215. rc = -1;
  216. goto bail;
  217. }
  218. smods = slapi_mods_new();
  219. dn = slapi_entry_get_dn(e);
  220. sdn = slapi_entry_get_sdn(e);
  221. if (hasObjectClass(e, "posixGroup")) {
  222. is_posix_group = 1;
  223. }
  224. /* Clean out memberuids and dsonlymemberuids without a valid referant */
  225. rc = slapi_entry_attr_find(e, "memberuid", &muid_attr);
  226. if (rc == 0 && muid_attr) {
  227. Slapi_PBlock *search_pb = slapi_pblock_new();
  228. Slapi_Attr *dsmuid_attr = NULL;
  229. Slapi_ValueSet *dsmuid_vs = NULL;
  230. char *attrs[] = { "uid", NULL };
  231. rc = slapi_entry_attr_find(e, "dsonlymemberuid", &dsmuid_attr);
  232. if (rc == 0 && dsmuid_attr) {
  233. slapi_attr_get_valueset(dsmuid_attr, &dsmuid_vs);
  234. }
  235. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  236. "_fix_memberuid scan for orphaned memberuids\n");
  237. for (i = slapi_attr_first_value(muid_attr, &v); i != -1;
  238. i = slapi_attr_next_value(muid_attr, i, &v)) {
  239. char *muid = (char *)slapi_value_get_string(v);
  240. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  241. "_fix_memberuid iterating memberuid: %s\n",
  242. muid);
  243. size_t vallen = muid ? strlen(muid) : 0;
  244. char *filter_escaped_value = slapi_escape_filter_value(muid, vallen);
  245. char *filter = slapi_ch_smprintf("(uid=%s)", filter_escaped_value);
  246. slapi_ch_free_string(&filter_escaped_value);
  247. Slapi_Entry **search_entries = NULL;
  248. slapi_search_internal_set_pb(search_pb,
  249. the_cb_data->dn,
  250. LDAP_SCOPE_SUBTREE,
  251. filter,
  252. attrs, 0, NULL, NULL,
  253. posix_winsync_get_plugin_identity(), 0);
  254. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  255. "_fix_memberuid searching %s with filter: %s\n",
  256. the_cb_data->dn, filter);
  257. rc = slapi_search_internal_pb(search_pb);
  258. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &search_entries);
  259. if (!search_entries || !search_entries[0]) {
  260. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  261. "_fix_memberuid Adding bad memberuid %s\n",
  262. slapi_value_get_string(v));
  263. slapi_mods_add_string(smods, LDAP_MOD_DELETE, "memberuid", slapi_value_get_string(v));
  264. if (dsmuid_vs && slapi_valueset_find(dsmuid_attr, dsmuid_vs, v)) {
  265. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  266. "_fix_memberuid Adding bad dsonlymemberuid %s\n",
  267. slapi_value_get_string(v));
  268. slapi_mods_add_string(smods, LDAP_MOD_DELETE, "dsonlymemberuid", slapi_value_get_string(v));
  269. }
  270. }
  271. slapi_free_search_results_internal(search_pb);
  272. slapi_pblock_init(search_pb);
  273. slapi_ch_free_string(&filter);
  274. }
  275. if (dsmuid_vs) {
  276. slapi_valueset_free(dsmuid_vs); dsmuid_vs = NULL;
  277. }
  278. slapi_pblock_destroy(search_pb); search_pb = NULL;
  279. }
  280. /* Cleanup uniquemembers without a referent, and verify memberuid otherwise */
  281. Slapi_Attr *obj_attr = NULL;
  282. rc = slapi_entry_attr_find(e, "uniquemember", &obj_attr);
  283. if (rc == 0 && obj_attr) {
  284. int fixMembership = 0;
  285. Slapi_ValueSet *bad_ums = NULL;
  286. Slapi_Value *uniqval = NULL; /* uniquemeber Attribute values */
  287. Slapi_ValueSet *uids = NULL;
  288. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  289. "_fix_memberuid scan uniquemember, group %s\n", dn);
  290. for (i = slapi_attr_first_value(obj_attr, &uniqval); i != -1;
  291. i = slapi_attr_next_value(obj_attr, i, &uniqval)) {
  292. const char *member = slapi_value_get_string(uniqval);
  293. char *attrs[] = { "uid", "objectclass", NULL };
  294. Slapi_Entry *child = getEntry(member, attrs);
  295. if (child) {
  296. slapi_entry_free(child);
  297. } else {
  298. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  299. "_fix_memberuid orphaned uniquemember found: %s\n", member);
  300. if ((strncasecmp(member, "cn=", 3) == 0) ||
  301. (strncasecmp(member, "uid=", 4) == 0)) {
  302. fixMembership = 1;
  303. }
  304. if (!bad_ums) {
  305. bad_ums = slapi_valueset_new();
  306. }
  307. slapi_valueset_add_value(bad_ums, uniqval);
  308. }
  309. if (is_posix_group) {
  310. char *uid = NULL;
  311. /* search uid for member (DN) */
  312. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME, "search %s\n", member);
  313. if ((uid = searchUid(member)) != NULL) {
  314. Slapi_Value *value = slapi_value_new();
  315. /* Search an entry having "member" as DN and get uid value from it. */
  316. slapi_value_set_string_passin(value, uid);
  317. /* add uids ValueSet */
  318. if (NULL == uids) {
  319. uids = slapi_valueset_new();
  320. }
  321. slapi_valueset_add_value(uids, value);
  322. slapi_value_free(&value);
  323. }
  324. }
  325. }
  326. /* If we found some posix members, replace the existing memberuid attribute
  327. * with the found values. */
  328. if (uids && slapi_valueset_count(uids)) {
  329. Slapi_Value *val = 0;
  330. Slapi_Mod *smod = slapi_mod_new();
  331. int hint = 0;
  332. slapi_mod_init(smod, 0);
  333. slapi_mod_set_operation(smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  334. slapi_mod_set_type(smod, "memberuid");
  335. /* Loop through all of our values and add them to smod */
  336. hint = slapi_valueset_first_value(uids, &val);
  337. while (val) {
  338. /* this makes a copy of the berval */
  339. slapi_mod_add_value(smod, slapi_value_get_berval(val));
  340. hint = slapi_valueset_next_value(uids, hint, &val);
  341. }
  342. slapi_mods_add_ldapmod(smods, slapi_mod_get_ldapmod_passout(smod));
  343. slapi_mod_free(&smod);
  344. }
  345. slapi_valueset_free(uids);
  346. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  347. "_fix_memberuid Finishing...\n");
  348. if (fixMembership && posix_winsync_config_get_mapNestedGrouping()) {
  349. Slapi_ValueSet *del_nested_vs = slapi_valueset_new();
  350. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  351. "_fix_memberuid group deleted, recalculating nesting\n");
  352. propogateDeletionsUpward(e, sdn, bad_ums, del_nested_vs, 0);
  353. slapi_valueset_free(del_nested_vs); del_nested_vs = NULL;
  354. }
  355. if (bad_ums) {
  356. slapi_mods_add_mod_values(smods, LDAP_MOD_DELETE, "uniquemember", valueset_get_valuearray(bad_ums));
  357. slapi_valueset_free(bad_ums); bad_ums = NULL;
  358. }
  359. }
  360. mods = slapi_mods_get_ldapmods_byref(smods);
  361. if (mods) {
  362. Slapi_PBlock *mod_pb = NULL;
  363. mod_pb = slapi_pblock_new();
  364. slapi_modify_internal_set_pb_ext(mod_pb, sdn, mods, 0, 0,
  365. posix_winsync_get_plugin_identity(), 0);
  366. slapi_pblock_set(mod_pb, SLAPI_TXN, the_cb_data->txn);
  367. slapi_modify_internal_pb(mod_pb);
  368. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  369. slapi_pblock_destroy(mod_pb);
  370. }
  371. slapi_mods_free(&smods);
  372. bail:
  373. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  374. "_fix_memberuid <==\n");
  375. /*
  376. * Since Ticket #481 "expand nested posix groups",
  377. * there's a possibility the found entry does not contain
  378. * uniqueMember attribute. But "not found" error shoud not
  379. * be returned, which stops the further fixup task.
  380. */
  381. return rc;
  382. }
  383. static void
  384. posix_group_fixup_task_thread(void *arg)
  385. {
  386. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  387. "_task_thread ==>\n");
  388. Slapi_Task *task = (Slapi_Task *) arg;
  389. task_data *td = NULL;
  390. int rc = 0;
  391. if (!task) {
  392. return; /* no task */
  393. }
  394. slapi_task_inc_refcount(task);
  395. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  396. "posix_group_fixup_task_thread --> refcount incremented.\n" );
  397. /* Fetch our task data from the task */
  398. td = (task_data *) slapi_task_get_data(task);
  399. slapi_task_begin(task, 1);
  400. slapi_task_log_notice(task, "posix_group task starts (arg: %s) ...\n", td->filter_str);
  401. /* get the memberOf operation lock */
  402. memberUidLock();
  403. /* do real work */
  404. rc = posix_group_fix_memberuid(td->dn, td->filter_str, NULL /* no txn? */);
  405. /* release the memberOf operation lock */
  406. memberUidUnlock();
  407. slapi_task_log_notice(task, "posix_group task finished.");
  408. slapi_task_log_status(task, "posix_group task finished.");
  409. slapi_task_inc_progress(task);
  410. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  411. "_task_thread finishing\n");
  412. /* this will queue the destruction of the task */
  413. slapi_task_finish(task, rc);
  414. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  415. "_task_thread <==\n");
  416. slapi_task_dec_refcount(task);
  417. slapi_log_error(SLAPI_LOG_PLUGIN, POSIX_WINSYNC_PLUGIN_NAME,
  418. "posix_group_fixup_task_thread <-- refcount decremented.\n");
  419. }