1
0

automember.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674
  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 used
  26. * 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 to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2011 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * Auto Membership Plug-in
  42. */
  43. #include "automember.h"
  44. /*
  45. * Plug-in globals
  46. */
  47. static PRCList *g_automember_config = NULL;
  48. static Slapi_RWLock *g_automember_config_lock;
  49. static void *_PluginID = NULL;
  50. static Slapi_DN *_PluginDN = NULL;
  51. static Slapi_DN *_ConfigAreaDN = NULL;
  52. static int g_plugin_started = 0;
  53. static Slapi_PluginDesc pdesc = { AUTOMEMBER_FEATURE_DESC,
  54. VENDOR,
  55. DS_PACKAGE_VERSION,
  56. AUTOMEMBER_PLUGIN_DESC };
  57. /*
  58. * Plug-in management functions
  59. */
  60. int automember_init(Slapi_PBlock * pb);
  61. static int automember_start(Slapi_PBlock * pb);
  62. static int automember_close(Slapi_PBlock * pb);
  63. static int automember_postop_init(Slapi_PBlock * pb);
  64. static int automember_internal_postop_init(Slapi_PBlock *pb);
  65. /*
  66. * Operation callbacks (where the real work is done)
  67. */
  68. static int automember_mod_post_op(Slapi_PBlock *pb);
  69. static int automember_add_post_op(Slapi_PBlock *pb);
  70. static int automember_del_post_op(Slapi_PBlock *pb);
  71. static int automember_modrdn_post_op(Slapi_PBlock *pb);
  72. static int automember_pre_op(Slapi_PBlock *pb, int modop);
  73. static int automember_mod_pre_op(Slapi_PBlock *pb);
  74. static int automember_add_pre_op(Slapi_PBlock *pb);
  75. /*
  76. * Config cache management functions
  77. */
  78. static int automember_load_config();
  79. static void automember_delete_config();
  80. static int automember_parse_config_entry(Slapi_Entry * e, int apply);
  81. static void automember_free_config_entry(struct configEntry ** entry);
  82. /*
  83. * helpers
  84. */
  85. static Slapi_DN *automember_get_sdn(Slapi_PBlock *pb);
  86. static Slapi_DN *automember_get_config_area();
  87. static void automember_set_config_area(Slapi_DN *sdn);
  88. static int automember_dn_is_config(Slapi_DN *sdn);
  89. static int automember_oktodo(Slapi_PBlock *pb);
  90. static int automember_isrepl(Slapi_PBlock *pb);
  91. static void automember_parse_regex_entry(struct configEntry *config, Slapi_Entry *e);
  92. static struct automemberRegexRule *automember_parse_regex_rule(char *rule_string);
  93. static void automember_free_regex_rule(struct automemberRegexRule *rule);
  94. static int automember_parse_grouping_attr(char *value, char **grouping_attr,
  95. char **grouping_value);
  96. static void automember_update_membership(struct configEntry *config, Slapi_Entry *e, PRFileDesc *ldif_fd);
  97. static void automember_add_member_value(Slapi_Entry *member_e, const char *group_dn,
  98. char *grouping_attr, char *grouping_value, PRFileDesc *ldif_fd);
  99. const char *fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val);
  100. /*
  101. * task functions
  102. */
  103. static int automember_task_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  104. int *returncode, char *returntext, void *arg);
  105. static int automember_task_add_export_updates(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  106. int *returncode, char *returntext, void *arg);
  107. static int automember_task_add_map_entries(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  108. int *returncode, char *returntext, void *arg);
  109. void automember_rebuild_task_thread(void *arg);
  110. void automember_export_task_thread(void *arg);
  111. void automember_map_task_thread(void *arg);
  112. void automember_task_destructor(Slapi_Task *task);
  113. void automember_task_export_destructor(Slapi_Task *task);
  114. void automember_task_map_destructor(Slapi_Task *task);
  115. #define DEFAULT_FILE_MODE PR_IRUSR | PR_IWUSR
  116. /*
  117. * Config cache locking functions
  118. */
  119. void
  120. automember_config_read_lock()
  121. {
  122. slapi_rwlock_rdlock(g_automember_config_lock);
  123. }
  124. void
  125. automember_config_write_lock()
  126. {
  127. slapi_rwlock_wrlock(g_automember_config_lock);
  128. }
  129. void
  130. automember_config_unlock()
  131. {
  132. slapi_rwlock_unlock(g_automember_config_lock);
  133. }
  134. /*
  135. * Plugin identity functions
  136. */
  137. void
  138. automember_set_plugin_id(void *pluginID)
  139. {
  140. _PluginID = pluginID;
  141. }
  142. void *
  143. automember_get_plugin_id()
  144. {
  145. return _PluginID;
  146. }
  147. void
  148. automember_set_plugin_sdn(Slapi_DN *pluginDN)
  149. {
  150. _PluginDN = pluginDN;
  151. }
  152. Slapi_DN *
  153. automember_get_plugin_sdn()
  154. {
  155. return _PluginDN;
  156. }
  157. static int plugin_is_betxn = 0;
  158. /*
  159. * Plug-in initialization functions
  160. */
  161. int
  162. automember_init(Slapi_PBlock *pb)
  163. {
  164. int status = 0;
  165. char *plugin_identity = NULL;
  166. Slapi_Entry *plugin_entry = NULL;
  167. char *plugin_type = NULL;
  168. int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
  169. int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
  170. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  171. "--> automember_init\n");
  172. /* get args */
  173. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  174. plugin_entry &&
  175. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  176. plugin_type && strstr(plugin_type, "betxn")) {
  177. plugin_is_betxn = 1;
  178. preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  179. premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  180. }
  181. slapi_ch_free_string(&plugin_type);
  182. /* Store the plugin identity for later use.
  183. * Used for internal operations. */
  184. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  185. PR_ASSERT(plugin_identity);
  186. automember_set_plugin_id(plugin_identity);
  187. /* Register callbacks */
  188. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  189. SLAPI_PLUGIN_VERSION_01) != 0 ||
  190. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  191. (void *) automember_start) != 0 ||
  192. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  193. (void *) automember_close) != 0 ||
  194. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  195. (void *) &pdesc) != 0 ||
  196. slapi_pblock_set(pb, premod, (void *) automember_mod_pre_op) != 0 ||
  197. slapi_pblock_set(pb, preadd, (void *) automember_add_pre_op) != 0) {
  198. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  199. "automember_init: failed to register plugin\n");
  200. status = -1;
  201. }
  202. if (!plugin_is_betxn && !status &&
  203. slapi_register_plugin("internalpostoperation", /* op type */
  204. 1, /* Enabled */
  205. "automember_init", /* this function desc */
  206. automember_internal_postop_init, /* init func */
  207. AUTOMEMBER_INT_POSTOP_DESC, /* plugin desc */
  208. NULL, /* ? */
  209. plugin_identity /* access control */
  210. )) {
  211. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  212. "automember_init: failed to register internalpostoperation plugin\n");
  213. status = -1;
  214. }
  215. if (!status) {
  216. plugin_type = "postoperation";
  217. if (plugin_is_betxn) {
  218. plugin_type = "betxnpostoperation";
  219. }
  220. if (slapi_register_plugin(plugin_type, /* op type */
  221. 1, /* Enabled */
  222. "automember_init", /* this function desc */
  223. automember_postop_init, /* init func for post op */
  224. AUTOMEMBER_POSTOP_DESC, /* plugin desc */
  225. NULL, /* ? */
  226. plugin_identity /* access control */
  227. )
  228. ) {
  229. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  230. "automember_init: failed to register postop plugin\n");
  231. status = -1;
  232. }
  233. }
  234. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  235. "<-- automember_init\n");
  236. return status;
  237. }
  238. /* not used when using plugin as a betxn plugin - betxn plugins are called for both internal and external ops */
  239. static int
  240. automember_internal_postop_init(Slapi_PBlock *pb)
  241. {
  242. int status = 0;
  243. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  244. SLAPI_PLUGIN_VERSION_01) != 0 ||
  245. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  246. (void *) &pdesc) != 0 ||
  247. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  248. (void *) automember_add_post_op) != 0 ||
  249. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  250. (void *) automember_del_post_op) != 0 ||
  251. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  252. (void *) automember_mod_post_op) != 0 ||
  253. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  254. (void *) automember_modrdn_post_op) != 0) {
  255. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  256. "automember_internal_postop_init: failed to register plugin\n");
  257. status = -1;
  258. }
  259. return status;
  260. }
  261. static int
  262. automember_postop_init(Slapi_PBlock *pb)
  263. {
  264. int status = 0;
  265. int addfn = SLAPI_PLUGIN_POST_ADD_FN;
  266. int delfn = SLAPI_PLUGIN_POST_DELETE_FN;
  267. int modfn = SLAPI_PLUGIN_POST_MODIFY_FN;
  268. int mdnfn = SLAPI_PLUGIN_POST_MODRDN_FN;
  269. if (plugin_is_betxn) {
  270. addfn = SLAPI_PLUGIN_BE_TXN_POST_ADD_FN;
  271. delfn = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
  272. modfn = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
  273. mdnfn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
  274. }
  275. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  276. SLAPI_PLUGIN_VERSION_01) != 0 ||
  277. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  278. (void *) &pdesc) != 0 ||
  279. slapi_pblock_set(pb, addfn, (void *) automember_add_post_op) != 0 ||
  280. slapi_pblock_set(pb, delfn, (void *) automember_del_post_op) != 0 ||
  281. slapi_pblock_set(pb, modfn, (void *) automember_mod_post_op) != 0 ||
  282. slapi_pblock_set(pb, mdnfn, (void *) automember_modrdn_post_op) != 0) {
  283. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  284. "automember_postop_init: failed to register plugin\n");
  285. status = -1;
  286. }
  287. return status;
  288. }
  289. /* Stash the config area in the pblock for start functions? */
  290. /*
  291. * automember_start()
  292. *
  293. * Creates config lock and loads config cache.
  294. */
  295. static int
  296. automember_start(Slapi_PBlock * pb)
  297. {
  298. Slapi_DN *plugindn = NULL;
  299. char *config_area = NULL;
  300. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  301. "--> automember_start\n");
  302. /* Check if we're already started */
  303. if (g_plugin_started) {
  304. goto done;
  305. }
  306. slapi_task_register_handler("automember rebuild membership", automember_task_add);
  307. slapi_task_register_handler("automember export updates", automember_task_add_export_updates);
  308. slapi_task_register_handler("automember map updates", automember_task_add_map_entries);
  309. g_automember_config_lock = slapi_new_rwlock();
  310. if (!g_automember_config_lock) {
  311. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  312. "automember_start: lock creation failed\n");
  313. return -1;
  314. }
  315. /*
  316. * Get the plug-in target dn from the system
  317. * and store it for future use. */
  318. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &plugindn);
  319. if (NULL == plugindn || 0 == slapi_sdn_get_ndn_len(plugindn)) {
  320. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  321. "automember_start: unable to retrieve plugin dn\n");
  322. return -1;
  323. }
  324. automember_set_plugin_sdn(slapi_sdn_dup(plugindn));
  325. /* Set the alternate config area if one is defined. */
  326. slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_AREA, &config_area);
  327. if (config_area) {
  328. automember_set_config_area(slapi_sdn_new_normdn_byval(config_area));
  329. }
  330. /*
  331. * Load the config cache
  332. */
  333. g_automember_config = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
  334. PR_INIT_CLIST(g_automember_config);
  335. if (automember_load_config() != 0) {
  336. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  337. "automember_start: unable to load plug-in configuration\n");
  338. return -1;
  339. }
  340. g_plugin_started = 1;
  341. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  342. "auto membership plug-in: ready for service\n");
  343. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  344. "<-- automember_start\n");
  345. done:
  346. return 0;
  347. }
  348. /*
  349. * automember_close()
  350. *
  351. * Cleans up the config cache.
  352. */
  353. static int
  354. automember_close(Slapi_PBlock * pb)
  355. {
  356. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  357. "--> automember_close\n");
  358. if (!g_plugin_started) {
  359. goto done;
  360. }
  361. automember_config_write_lock();
  362. g_plugin_started = 0;
  363. automember_delete_config();
  364. automember_config_unlock();
  365. slapi_ch_free((void **)&g_automember_config);
  366. slapi_sdn_free(&_PluginDN);
  367. slapi_sdn_free(&_ConfigAreaDN);
  368. /* We explicitly don't destroy the config lock here. If we did,
  369. * there is the slight possibility that another thread that just
  370. * passed the g_plugin_started check is about to try to obtain
  371. * a reader lock. We leave the lock around so these threads
  372. * don't crash the process. If we always check the started
  373. * flag again after obtaining a reader lock, no free'd resources
  374. * will be used. */
  375. done:
  376. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  377. "<-- automember_close\n");
  378. return 0;
  379. }
  380. /*
  381. * automember_get_config()
  382. *
  383. * Get the config list.
  384. */
  385. PRCList *
  386. automember_get_config()
  387. {
  388. return g_automember_config;
  389. }
  390. /*
  391. * automember_load_config()
  392. *
  393. * Parse and load the config entries.
  394. */
  395. static int
  396. automember_load_config()
  397. {
  398. int status = 0;
  399. int result;
  400. int i;
  401. Slapi_PBlock *search_pb;
  402. Slapi_Entry **entries = NULL;
  403. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  404. "--> automember_load_config\n");
  405. /* Clear out any old config. */
  406. automember_config_write_lock();
  407. automember_delete_config();
  408. search_pb = slapi_pblock_new();
  409. /* If an alternate config area is configured, find
  410. * the config entries that are beneath it, otherwise
  411. * we load the entries beneath our top-level plug-in
  412. * config entry. */
  413. if (automember_get_config_area()) {
  414. /* Find the config entries beneath the alternate config area. */
  415. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  416. "automember_load_config: Looking for config entries "
  417. "beneath \"%s\".\n", slapi_sdn_get_ndn(automember_get_config_area()));
  418. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_ndn(automember_get_config_area()),
  419. LDAP_SCOPE_SUBTREE, AUTOMEMBER_DEFINITION_FILTER,
  420. NULL, 0, NULL, NULL, automember_get_plugin_id(), 0);
  421. } else {
  422. /* Find the config entries beneath our plugin entry. */
  423. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  424. "automember_load_config: Looking for config entries "
  425. "beneath \"%s\".\n", slapi_sdn_get_ndn(automember_get_plugin_sdn()));
  426. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_ndn(automember_get_plugin_sdn()),
  427. LDAP_SCOPE_SUBTREE, AUTOMEMBER_DEFINITION_FILTER,
  428. NULL, 0, NULL, NULL, automember_get_plugin_id(), 0);
  429. }
  430. slapi_search_internal_pb(search_pb);
  431. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  432. if (LDAP_SUCCESS != result) {
  433. if (automember_get_config_area() && (result == LDAP_NO_SUCH_OBJECT)) {
  434. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  435. "automember_load_config: Config container \"%s\" does "
  436. "not exist.\n", slapi_sdn_get_ndn(automember_get_config_area()));
  437. goto cleanup;
  438. } else {
  439. status = -1;
  440. goto cleanup;
  441. }
  442. }
  443. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  444. &entries);
  445. /* Loop through all of the entries we found and parse them. */
  446. for (i = 0; entries && (entries[i] != NULL); i++) {
  447. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  448. "automember_load_config: parsing config entry "
  449. "\"%s\".\n", slapi_entry_get_dn(entries[i]));
  450. /* We don't care about the status here because we may have
  451. * some invalid config entries, but we just want to continue
  452. * looking for valid ones. */
  453. automember_parse_config_entry(entries[i], 1);
  454. }
  455. cleanup:
  456. slapi_free_search_results_internal(search_pb);
  457. slapi_pblock_destroy(search_pb);
  458. automember_config_unlock();
  459. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  460. "<-- automember_load_config\n");
  461. return status;
  462. }
  463. /*
  464. * automember_parse_config_entry()
  465. *
  466. * Parses a single config entry. If apply is non-zero, then
  467. * we will load and start using the new config. You can simply
  468. * validate config without making any changes by setting apply
  469. * to 0.
  470. *
  471. * Returns 0 if the entry is valid and -1 if it is invalid.
  472. */
  473. static int
  474. automember_parse_config_entry(Slapi_Entry * e, int apply)
  475. {
  476. char *value = NULL;
  477. char **values = NULL;
  478. struct configEntry *entry = NULL;
  479. struct configEntry *config_entry;
  480. PRCList *list;
  481. Slapi_PBlock *search_pb = NULL;
  482. Slapi_Entry **rule_entries = NULL;
  483. char *filter_str = NULL;
  484. Slapi_DN *dn = NULL;
  485. Slapi_Filter *filter = NULL;
  486. int result;
  487. int entry_added = 0;
  488. int i = 0;
  489. int ret = 0;
  490. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  491. "--> automember_parse_config_entry\n");
  492. /* If this is the main plug-in config entry or
  493. * the config area container, just bail. */
  494. if ((slapi_sdn_compare(automember_get_plugin_sdn(), slapi_entry_get_sdn(e)) == 0) ||
  495. (automember_get_config_area() && (slapi_sdn_compare(automember_get_config_area(),
  496. slapi_entry_get_sdn(e)) == 0))) {
  497. goto bail;
  498. }
  499. /* If this entry is not an automember config definition entry, just bail. */
  500. filter_str = slapi_ch_strdup(AUTOMEMBER_DEFINITION_FILTER);
  501. filter = slapi_str2filter(filter_str);
  502. if (slapi_filter_test_simple(e, filter) != 0) {
  503. goto bail;
  504. }
  505. /* If marked as disabled, just bail. */
  506. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_DISABLED_TYPE);
  507. if (value) {
  508. slapi_ch_free_string(&value);
  509. goto bail;
  510. }
  511. entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  512. if (NULL == entry) {
  513. ret = -1;
  514. goto bail;
  515. }
  516. value = slapi_entry_get_ndn(e);
  517. if (value) {
  518. entry->dn = slapi_ch_strdup(value);
  519. } else {
  520. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  521. "automember_parse_config_entry: Error "
  522. "reading dn from config entry\n");
  523. ret = -1;
  524. goto bail;
  525. }
  526. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  527. "----------> dn [%s]\n", entry->dn);
  528. /* Load the scope */
  529. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_SCOPE_TYPE);
  530. if (value) {
  531. entry->scope = value;
  532. } else {
  533. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  534. "automember_parse_config_entry: The %s config "
  535. "setting is required for config entry \"%s\".\n",
  536. AUTOMEMBER_SCOPE_TYPE, entry->dn);
  537. ret = -1;
  538. goto bail;
  539. }
  540. /* Load the filter */
  541. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_FILTER_TYPE);
  542. if (value) {
  543. /* Convert to a Slapi_Filter to improve performance. */
  544. if (NULL == (entry->filter = slapi_str2filter(value))) {
  545. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM ,
  546. "automember_parse_config_entry: Invalid search filter in "
  547. "%s config setting for config entry \"%s\" "
  548. "(filter = \"%s\").\n", AUTOMEMBER_FILTER_TYPE, entry->dn, value);
  549. ret = -1;
  550. }
  551. slapi_ch_free_string(&value);
  552. if (ret != 0) {
  553. goto bail;
  554. }
  555. } else {
  556. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  557. "automember_parse_config_entry: The %s config "
  558. "setting is required for config entry \"%s\".\n",
  559. AUTOMEMBER_FILTER_TYPE, entry->dn);
  560. ret = -1;
  561. goto bail;
  562. }
  563. /* Load the default groups */
  564. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_DEFAULT_GROUP_TYPE);
  565. if (values) {
  566. /* Just hand off the values */
  567. entry->default_groups = values;
  568. /*
  569. * If we set the config area, we need to make sure that the default groups are not
  570. * in the config area, or else we could deadlock on updates.
  571. */
  572. if(automember_get_config_area()){
  573. for(i = 0; values && values[i]; i++){
  574. dn = slapi_sdn_new_dn_byref(values[i]);
  575. if(slapi_sdn_issuffix(dn, automember_get_config_area())){
  576. /* The groups are under the config area - not good */
  577. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  578. "automember_parse_config_entry: The default group \"%s\" can not be "
  579. "a child of the plugin config area \"%s\".\n",
  580. values[i], slapi_sdn_get_dn(automember_get_config_area()));
  581. slapi_sdn_free(&dn);
  582. ret = -1;
  583. goto bail;
  584. }
  585. slapi_sdn_free(&dn);
  586. }
  587. }
  588. values = NULL;
  589. }
  590. /* Load the grouping attr */
  591. value = slapi_entry_attr_get_charptr(e, AUTOMEMBER_GROUPING_ATTR_TYPE);
  592. if (value) {
  593. if (automember_parse_grouping_attr(value, &(entry->grouping_attr),
  594. &(entry->grouping_value)) != 0) {
  595. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  596. "automember_parse_config_entry: Invalid "
  597. "%s config setting for config entry \"%s\" "
  598. "(value: \"%s\").\n", AUTOMEMBER_GROUPING_ATTR_TYPE,
  599. entry->dn, value);
  600. ret = -1;
  601. }
  602. slapi_ch_free_string(&value);
  603. if (ret != 0) {
  604. goto bail;
  605. }
  606. } else {
  607. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  608. "automember_parse_config_entry: The %s config "
  609. "setting is required for config entry \"%s\".\n",
  610. AUTOMEMBER_GROUPING_ATTR_TYPE, entry->dn);
  611. ret = -1;
  612. goto bail;
  613. }
  614. /* Find all child regex rule entries */
  615. search_pb = slapi_pblock_new();
  616. slapi_search_internal_set_pb(search_pb, entry->dn, LDAP_SCOPE_SUBTREE,
  617. AUTOMEMBER_REGEX_RULE_FILTER, NULL, 0, NULL,
  618. NULL, automember_get_plugin_id(), 0);
  619. slapi_search_internal_pb(search_pb);
  620. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  621. /* If this is a new config entry being added, it won't exist yet
  622. * when we are simply validating config. We can just ignore no
  623. * such object errors. */
  624. if ((LDAP_SUCCESS != result) && (LDAP_NO_SUCH_OBJECT != result)) {
  625. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  626. "automember_parse_config_entry: Error searching "
  627. "for child rule entries for config \"%s\" (err=%d).",
  628. entry->dn, result);
  629. ret = -1;
  630. goto bail;
  631. }
  632. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  633. &rule_entries);
  634. /* Go through each child rule entry and parse it. */
  635. for (i = 0; rule_entries && (rule_entries[i] != NULL); i++) {
  636. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  637. "automember_parse_config_entry: parsing regex rule entry "
  638. "\"%s\".\n", slapi_entry_get_dn(rule_entries[i]));
  639. automember_parse_regex_entry(entry, rule_entries[i]);
  640. }
  641. /* If we were only called to validate config, we can
  642. * just bail out before applying the config changes */
  643. if (apply == 0) {
  644. goto bail;
  645. }
  646. /* Add the config object to the list. We order by scope. */
  647. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  648. list = PR_LIST_HEAD(g_automember_config);
  649. while (list != g_automember_config) {
  650. config_entry = (struct configEntry *) list;
  651. /* If the config entry we are adding has a scope that is
  652. * a child of the scope of the current list item, we insert
  653. * the entry before that list item. */
  654. if (slapi_dn_issuffix(entry->scope, config_entry->scope)) {
  655. PR_INSERT_BEFORE(&(entry->list), list);
  656. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  657. "store [%s] before [%s] \n", entry->dn,
  658. config_entry->dn);
  659. entry_added = 1;
  660. break;
  661. }
  662. list = PR_NEXT_LINK(list);
  663. /* If we hit the end of the list, add to the tail. */
  664. if (g_automember_config == list) {
  665. PR_INSERT_BEFORE(&(entry->list), list);
  666. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  667. "store [%s] at tail\n", entry->dn);
  668. entry_added = 1;
  669. break;
  670. }
  671. }
  672. } else {
  673. /* first entry */
  674. PR_INSERT_LINK(&(entry->list), g_automember_config);
  675. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  676. "store [%s] at head \n", entry->dn);
  677. entry_added = 1;
  678. }
  679. bail:
  680. if (0 == entry_added) {
  681. /* Don't log error if we weren't asked to apply config */
  682. if ((apply != 0) && (entry != NULL)) {
  683. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  684. "automember_parse_config_entry: Invalid config entry "
  685. "[%s] skipped\n", entry->dn);
  686. }
  687. automember_free_config_entry(&entry);
  688. } else {
  689. ret = 0;
  690. }
  691. slapi_ch_free_string(&filter_str);
  692. slapi_filter_free(filter, 1);
  693. slapi_free_search_results_internal(search_pb);
  694. slapi_pblock_destroy(search_pb);
  695. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  696. "<-- automember_parse_config_entry\n");
  697. return ret;
  698. }
  699. static void
  700. automember_free_config_entry(struct configEntry ** entry)
  701. {
  702. struct configEntry *e = *entry;
  703. if (e == NULL)
  704. return;
  705. if (e->dn) {
  706. slapi_log_error(SLAPI_LOG_CONFIG, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  707. "freeing config entry [%s]\n", e->dn);
  708. slapi_ch_free_string(&e->dn);
  709. }
  710. if (e->scope) {
  711. slapi_ch_free_string(&e->scope);
  712. }
  713. if (e->filter) {
  714. slapi_filter_free(e->filter, 1);
  715. }
  716. if (e->exclusive_rules) {
  717. PRCList *list;
  718. /* Clear out the list contents. */
  719. while (!PR_CLIST_IS_EMPTY((PRCList *)e->exclusive_rules)) {
  720. list = PR_LIST_HEAD((PRCList *)e->exclusive_rules);
  721. PR_REMOVE_LINK(list);
  722. automember_free_regex_rule((struct automemberRegexRule *)list);
  723. }
  724. /* Free the list itself. */
  725. slapi_ch_free((void **)&(e->exclusive_rules));
  726. }
  727. /* Clear out the list contents. */
  728. if (e->inclusive_rules) {
  729. PRCList *list;
  730. while (!PR_CLIST_IS_EMPTY((PRCList *)e->inclusive_rules)) {
  731. list = PR_LIST_HEAD((PRCList *)e->inclusive_rules);
  732. PR_REMOVE_LINK(list);
  733. automember_free_regex_rule((struct automemberRegexRule *)list);
  734. }
  735. /* Free the list itself. */
  736. slapi_ch_free((void **)&(e->inclusive_rules));
  737. }
  738. if (e->default_groups) {
  739. slapi_ch_array_free(e->default_groups);
  740. }
  741. if (e->grouping_attr) {
  742. slapi_ch_free_string(&e->grouping_attr);
  743. }
  744. if (e->grouping_value) {
  745. slapi_ch_free_string(&e->grouping_value);
  746. }
  747. slapi_ch_free((void **)entry);
  748. }
  749. static void
  750. automember_delete_configEntry(PRCList *entry)
  751. {
  752. PR_REMOVE_LINK(entry);
  753. automember_free_config_entry((struct configEntry **) &entry);
  754. }
  755. static void
  756. automember_delete_config()
  757. {
  758. PRCList *list;
  759. /* Delete the config cache. */
  760. while (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  761. list = PR_LIST_HEAD(g_automember_config);
  762. automember_delete_configEntry(list);
  763. }
  764. return;
  765. }
  766. static Slapi_DN *
  767. automember_get_sdn(Slapi_PBlock * pb)
  768. {
  769. Slapi_DN *sdn = 0;
  770. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  771. "--> automember_get_sdn\n");
  772. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  773. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  774. "<-- automember_get_sdn\n");
  775. return sdn;
  776. }
  777. void
  778. automember_set_config_area(Slapi_DN *sdn)
  779. {
  780. _ConfigAreaDN = sdn;
  781. }
  782. Slapi_DN *
  783. automember_get_config_area()
  784. {
  785. return _ConfigAreaDN;
  786. }
  787. /*
  788. * automember_dn_is_config()
  789. *
  790. * Checks if dn is an auto membership config entry.
  791. */
  792. static int
  793. automember_dn_is_config(Slapi_DN *sdn)
  794. {
  795. int ret = 0;
  796. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  797. "--> automember_dn_is_config\n");
  798. if (sdn == NULL) {
  799. goto bail;
  800. }
  801. /* If an alternate config area is configured, treat it's child
  802. * entries as config entries. If the alternate config area is
  803. * not configured, treat children of the top-level plug-in
  804. * config entry as our config entries. */
  805. if (automember_get_config_area()) {
  806. if (slapi_sdn_issuffix(sdn, automember_get_config_area()) &&
  807. slapi_sdn_compare(sdn, automember_get_config_area())) {
  808. ret = 1;
  809. }
  810. } else {
  811. if (slapi_sdn_issuffix(sdn, automember_get_plugin_sdn()) &&
  812. slapi_sdn_compare(sdn, automember_get_plugin_sdn())) {
  813. ret = 1;
  814. }
  815. }
  816. bail:
  817. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  818. "<-- automember_dn_is_config\n");
  819. return ret;
  820. }
  821. /*
  822. * automember_oktodo()
  823. *
  824. * Check if we want to process this operation. We need to be
  825. * sure that the operation succeeded.
  826. */
  827. static int
  828. automember_oktodo(Slapi_PBlock *pb)
  829. {
  830. int ret = 1;
  831. int oprc = 0;
  832. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  833. "--> automember_oktodo\n" );
  834. if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0) {
  835. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  836. "automember_oktodo: could not get parameters\n" );
  837. ret = -1;
  838. }
  839. /* This plugin should only execute if the operation succeeded. */
  840. if(oprc != 0) {
  841. ret = 0;
  842. }
  843. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  844. "<-- automember_oktodo\n" );
  845. return ret;
  846. }
  847. /*
  848. * automember_isrepl()
  849. *
  850. * Returns 1 if the operation associated with pb
  851. * is a replicated op. Returns 0 otherwise.
  852. */
  853. static int
  854. automember_isrepl(Slapi_PBlock *pb)
  855. {
  856. int is_repl = 0;
  857. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  858. "--> automember_isrepl\n" );
  859. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  860. slapi_log_error( SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  861. "<-- automember_isrepl\n" );
  862. return is_repl;
  863. }
  864. /*
  865. * automember_parse_regex_entry()
  866. *
  867. * Parses a rule entry and adds the regex rules to the
  868. * passed in config struct. Invalid regex rules will
  869. * be skipped and logged at the fatal log level.
  870. */
  871. static void
  872. automember_parse_regex_entry(struct configEntry *config, Slapi_Entry *e)
  873. {
  874. char *target_group = NULL;
  875. char **values = NULL;
  876. Slapi_DN *group_dn = NULL;
  877. PRCList *list;
  878. int i = 0;
  879. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  880. "--> automember_parse_regex_entry\n");
  881. /* Make sure the target group was specified. */
  882. target_group = slapi_entry_attr_get_charptr(e, AUTOMEMBER_TARGET_GROUP_TYPE);
  883. if (!target_group) {
  884. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  885. "automember_parse_regex_entry: The %s config "
  886. "setting is required for rule entry \"%s\".\n",
  887. AUTOMEMBER_TARGET_GROUP_TYPE, slapi_entry_get_ndn(e));
  888. goto bail;
  889. }
  890. /* Ensure that the target group DN is valid. */
  891. if (slapi_dn_syntax_check(NULL, target_group, 1) != 0) {
  892. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  893. "automember_parse_regex_entry: invalid target group DN "
  894. "in rule \"%s\" (dn=\"%s\").\n", slapi_entry_get_ndn(e),
  895. target_group);
  896. goto bail;
  897. }
  898. /* normalize the group dn and compare it to the configArea DN */
  899. if(automember_get_config_area()){
  900. group_dn = slapi_sdn_new_dn_byref(target_group);
  901. if(slapi_sdn_issuffix(group_dn, automember_get_config_area())){
  902. /* The target group is under the plugin config area - not good */
  903. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  904. "automember_parse_regex_entry: The target group \"%s\" can not be "
  905. "a child of the plugin config area \"%s\".\n",
  906. slapi_sdn_get_dn(group_dn), slapi_sdn_get_dn(automember_get_config_area()));
  907. slapi_sdn_free(&group_dn);
  908. goto bail;
  909. }
  910. slapi_sdn_free(&group_dn);
  911. }
  912. /* Load inclusive rules */
  913. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_INC_REGEX_TYPE);
  914. if (values) {
  915. struct automemberRegexRule *rule = NULL;
  916. /* If we haven't loaded any inclusive rules for
  917. * this config definition yet, create a new list. */
  918. if (config->inclusive_rules == NULL) {
  919. /* Create a list to hold our regex rules */
  920. config->inclusive_rules = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  921. PR_INIT_CLIST((PRCList *)config->inclusive_rules);
  922. }
  923. for (i = 0; values && values[i]; ++i) {
  924. rule = automember_parse_regex_rule(values[i]);
  925. if (rule) {
  926. /* Fill in the target group. */
  927. rule->target_group_dn = slapi_sdn_new_normdn_byval(target_group);
  928. if (!PR_CLIST_IS_EMPTY((PRCList *)config->inclusive_rules)) {
  929. list = PR_LIST_HEAD((PRCList *)config->inclusive_rules);
  930. while (list != (PRCList *)config->inclusive_rules) {
  931. struct automemberRegexRule *curr_rule = (struct automemberRegexRule *)list;
  932. /* Order rules by target group DN */
  933. if (slapi_sdn_compare(rule->target_group_dn, curr_rule->target_group_dn) < 0) {
  934. PR_INSERT_BEFORE(&(rule->list), list);
  935. break;
  936. }
  937. list = PR_NEXT_LINK(list);
  938. /* If we hit the end of the list, add to the tail. */
  939. if ((PRCList *)config->inclusive_rules == list) {
  940. PR_INSERT_BEFORE(&(rule->list), list);
  941. break;
  942. }
  943. }
  944. } else {
  945. /* Add to head of list */
  946. PR_INSERT_LINK(&(rule->list), (PRCList *)config->inclusive_rules);
  947. }
  948. } else {
  949. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  950. "automember_parse_regex_entry: Skipping invalid inclusive "
  951. "regex rule in rule entry \"%s\" (rule = \"%s\").\n",
  952. slapi_entry_get_ndn(e), values[i]);
  953. }
  954. }
  955. slapi_ch_array_free(values);
  956. values = NULL;
  957. }
  958. /* Load exclusive rules. */
  959. values = slapi_entry_attr_get_charray(e, AUTOMEMBER_EXC_REGEX_TYPE);
  960. if (values) {
  961. struct automemberRegexRule *rule = NULL;
  962. /* If we haven't loaded any exclusive rules for
  963. * this config definition yet, create a new list. */
  964. if (config->exclusive_rules == NULL) {
  965. /* Create a list to hold our regex rules */
  966. config->exclusive_rules = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  967. PR_INIT_CLIST((PRCList *)config->exclusive_rules);
  968. }
  969. for (i = 0; values && values[i]; ++i) {
  970. rule = automember_parse_regex_rule(values[i]);
  971. if (rule) {
  972. /* Fill in the target group. */
  973. rule->target_group_dn = slapi_sdn_new_normdn_byval(target_group);
  974. if (!PR_CLIST_IS_EMPTY((PRCList *)config->exclusive_rules)) {
  975. list = PR_LIST_HEAD((PRCList *)config->exclusive_rules);
  976. while (list != (PRCList *)config->exclusive_rules) {
  977. struct automemberRegexRule *curr_rule = (struct automemberRegexRule *)list;
  978. /* Order rules by target group DN */
  979. if (slapi_sdn_compare(rule->target_group_dn, curr_rule->target_group_dn) < 0) {
  980. PR_INSERT_BEFORE(&(rule->list), list);
  981. break;
  982. }
  983. list = PR_NEXT_LINK(list);
  984. /* If we hit the end of the list, add to the tail. */
  985. if ((PRCList *)config->exclusive_rules == list) {
  986. PR_INSERT_BEFORE(&(rule->list), list);
  987. break;
  988. }
  989. }
  990. } else {
  991. /* Add to head of list */
  992. PR_INSERT_LINK(&(rule->list), (PRCList *)config->exclusive_rules);
  993. }
  994. } else {
  995. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  996. "automember_parse_regex_entry: Skipping invalid exclusive "
  997. "regex rule in rule entry \"%s\" (rule = \"%s\").\n",
  998. slapi_entry_get_ndn(e), values[i]);
  999. }
  1000. }
  1001. slapi_ch_array_free(values);
  1002. values = NULL;
  1003. }
  1004. bail:
  1005. slapi_ch_free_string(&target_group);
  1006. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1007. "<-- automember_parse_regex_entry\n");
  1008. }
  1009. /*
  1010. * automember_parse_regex_rule()
  1011. *
  1012. * Parses a regex rule and returns a regex rule struct. The caller
  1013. * will need to free this struct when it is finished with it. If
  1014. * there is a problem parsing the regex rule, an error will be
  1015. * logged and NULL will be returned.
  1016. */
  1017. static struct automemberRegexRule *
  1018. automember_parse_regex_rule(char *rule_string)
  1019. {
  1020. struct automemberRegexRule *rule = NULL;
  1021. char *attr = NULL;
  1022. Slapi_Regex *regex = NULL;
  1023. const char *recomp_result = NULL;
  1024. char *p = NULL;
  1025. char *p2 = NULL;
  1026. /* A rule is in the form "attr=regex". */
  1027. /* Find the comparison attribute name. */
  1028. if ((p = strchr(rule_string, '=')) == NULL) {
  1029. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1030. "automember_parse_regex_rule: Unable to parse "
  1031. "regex rule (missing '=' delimeter).\n");
  1032. goto bail;
  1033. }
  1034. /* Make sure the attribute name is not empty. */
  1035. if (p == rule_string) {
  1036. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1037. "automember_parse_regex_rule: Unable to parse "
  1038. " regex rule (missing comparison attribute).\n");
  1039. goto bail;
  1040. }
  1041. if ((attr = strndup(rule_string, p - rule_string)) == NULL) {
  1042. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1043. "automember_parse_regex_rule: Unable to allocate "
  1044. "memory.\n");
  1045. goto bail;
  1046. }
  1047. /* Validate the attribute. */
  1048. for (p2 = attr; p2 && (*p2 != '\0'); p2++) {
  1049. if (!IS_ATTRDESC_CHAR(*p2)) {
  1050. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1051. "automember_parse_regex_rule: Invalid comparison "
  1052. "attribute name \"%s\".\n", attr);
  1053. goto bail;
  1054. }
  1055. }
  1056. /* Find the regex. */
  1057. p++;
  1058. if (*p == '\0') {
  1059. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1060. "automember_parse_regex_rule: Unable to parse "
  1061. "regex rule (missing regex).\n");
  1062. goto bail;
  1063. }
  1064. /* Compile the regex to validate it. */
  1065. regex = slapi_re_comp(p, &recomp_result);
  1066. if (!regex) {
  1067. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1068. "automember_parse_regex_rule: Unable to parse "
  1069. "regex rule (invalid regex). Error \"%s\".\n",
  1070. recomp_result?recomp_result:"unknown");
  1071. }
  1072. /* Validation has passed, so create the regex rule struct and fill it in.
  1073. * We hand off everything we have allocated. All of this will be free'd
  1074. * when the rule struct itself is freed. */
  1075. rule = (struct automemberRegexRule *)slapi_ch_calloc(1, sizeof(struct automemberRegexRule));
  1076. rule->attr = attr;
  1077. rule->regex_str = slapi_ch_strdup(p);
  1078. rule->regex = regex;
  1079. bail:
  1080. /* Cleanup if we didn't successfully create a rule. */
  1081. if (!rule) {
  1082. slapi_ch_free_string(&attr);
  1083. slapi_re_free(regex);
  1084. }
  1085. return rule;
  1086. }
  1087. /*
  1088. * automember_free_regex_rule()
  1089. *
  1090. * Frees a regex rule and all of it's contents from memory.
  1091. */
  1092. static void
  1093. automember_free_regex_rule(struct automemberRegexRule *rule)
  1094. {
  1095. if (rule) {
  1096. if (rule->target_group_dn) {
  1097. slapi_sdn_free(&(rule->target_group_dn));
  1098. }
  1099. if (rule->attr) {
  1100. slapi_ch_free_string(&(rule->attr));
  1101. }
  1102. if (rule->regex_str) {
  1103. slapi_ch_free_string(&(rule->regex_str));
  1104. }
  1105. if (rule->regex) {
  1106. slapi_re_free(rule->regex);
  1107. }
  1108. }
  1109. slapi_ch_free((void **)&rule);
  1110. }
  1111. /*
  1112. * automember_parse_grouping_attr()
  1113. *
  1114. * Parses a grouping attribute and grouping value from
  1115. * the passed in config string. Memory will be allocated
  1116. * for grouping_attr and grouping_value, so it is up to
  1117. * the called to free them when they are no longer needed.
  1118. * Returns 0 upon success and 1 upon failure.
  1119. */
  1120. static int
  1121. automember_parse_grouping_attr(char *value, char **grouping_attr, char **grouping_value)
  1122. {
  1123. int ret = 0;
  1124. char *p = NULL;
  1125. /* Clear out any existing type or value. */
  1126. slapi_ch_free_string(grouping_attr);
  1127. slapi_ch_free_string(grouping_value);
  1128. /* split out the type from the value (use the first ':') */
  1129. if ((p = strchr(value, ':')) == NULL) {
  1130. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1131. "automember_parse_grouping_attr: Value for grouping attribute "
  1132. "is not in the correct format. (value: \"%s\").\n", value);
  1133. ret = 1;
  1134. goto bail;
  1135. }
  1136. /* Ensure the type is not empty. */
  1137. if (p == value) {
  1138. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1139. "automember_parse_grouping_attr: Value for grouping attribute "
  1140. "is not in the correct format. The grouping attribute is missing. "
  1141. "(value: \"%s\").\n", value);
  1142. ret = 1;
  1143. goto bail;
  1144. }
  1145. /* Duplicate the type to be returned. */
  1146. *grouping_attr = strndup(value, p - value);
  1147. /* Advance p to point to the beginning of the value. */
  1148. p++;
  1149. while (*p == ' ') {
  1150. p++;
  1151. }
  1152. /* Ensure the value is not empty. */
  1153. if (*p == '\0') {
  1154. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1155. "automember_parse_grouping_attr: Value for grouping attribute "
  1156. "is not in the correct format. The grouping value is "
  1157. "missing. (value: \"%s\").\n", value);
  1158. ret = 1;
  1159. goto bail;
  1160. }
  1161. /* Duplicate the value to be returned. */
  1162. *grouping_value = slapi_ch_strdup(p);
  1163. /* Ensure that memory was allocated successfully. */
  1164. if ((*grouping_attr == NULL) || (*grouping_value == NULL)) {
  1165. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1166. "automember_parse_grouping_attr: Error allocating memory.\n");
  1167. ret = 1;
  1168. goto bail;
  1169. }
  1170. /* Ensure that the grouping attr is a legal attr name. */
  1171. for (p = *grouping_attr; p && (*p != '\0'); p++) {
  1172. if (!IS_ATTRDESC_CHAR(*p)) {
  1173. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1174. "automember_parse_grouping_attr: Invalid value for "
  1175. "grouping attribute. The grouping attribute type is "
  1176. "illegal. (type: \"%s\").\n", *grouping_attr);
  1177. ret = 1;
  1178. goto bail;
  1179. }
  1180. }
  1181. /* Ensure that the grouping value type is a legal attr name. */
  1182. for (p = *grouping_value; p && (*p != '\0'); p++) {
  1183. if (!IS_ATTRDESC_CHAR(*p)) {
  1184. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1185. "automember_parse_grouping_attr: Invalid value for "
  1186. "grouping attribute. The grouping value type is "
  1187. "illegal. (type: \"%s\").\n", *grouping_value);
  1188. ret = 1;
  1189. goto bail;
  1190. }
  1191. }
  1192. bail:
  1193. if (ret != 0) {
  1194. slapi_ch_free_string(grouping_attr);
  1195. slapi_ch_free_string(grouping_value);
  1196. }
  1197. return ret;
  1198. }
  1199. /*
  1200. * automember_update_membership()
  1201. *
  1202. * Determines which target groups need to be updated according to
  1203. * the rules in config, then performs the updates.
  1204. */
  1205. static void
  1206. automember_update_membership(struct configEntry *config, Slapi_Entry *e, PRFileDesc *ldif_fd)
  1207. {
  1208. PRCList *rule = NULL;
  1209. struct automemberRegexRule *curr_rule = NULL;
  1210. PRCList exclusions;
  1211. PRCList targets;
  1212. struct automemberDNListItem *dnitem = NULL;
  1213. Slapi_DN *last = NULL;
  1214. PRCList *curr_exclusion = NULL;
  1215. char **vals = NULL;
  1216. int i = 0;
  1217. if (!config || !e) {
  1218. return;
  1219. }
  1220. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1221. "automember_update_membership: Processing \"%s\" "
  1222. "definition entry for candidate entry \"%s\".\n",
  1223. config->dn, slapi_entry_get_dn(e));
  1224. /* Initialize our lists that keep track of targets. */
  1225. PR_INIT_CLIST(&exclusions);
  1226. PR_INIT_CLIST(&targets);
  1227. /* Go through exclusive rules and build an exclusion list. */
  1228. if (config->exclusive_rules) {
  1229. if (!PR_CLIST_IS_EMPTY((PRCList *)config->exclusive_rules)) {
  1230. rule = PR_LIST_HEAD((PRCList *)config->exclusive_rules);
  1231. while (rule != (PRCList *)config->exclusive_rules) {
  1232. curr_rule = (struct automemberRegexRule *)rule;
  1233. /* Regex rules are sorted by the target group DN. This means
  1234. * we can skip all rules for the last target group DN that we
  1235. * added to the exclusions list. */
  1236. if ((last == NULL) || slapi_sdn_compare(last, curr_rule->target_group_dn) != 0) {
  1237. /* Get comparison attr and loop through values. */
  1238. vals = slapi_entry_attr_get_charray(e, curr_rule->attr);
  1239. for (i = 0; vals && vals[i]; ++i) {
  1240. /* Evaluate the regex. */
  1241. if (slapi_re_exec(curr_rule->regex, vals[i], -1) == 1) {
  1242. /* Found a match. Add to end of the exclusion list
  1243. * and set last as a hint to ourselves. */
  1244. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1245. "automember_update_membership: Adding \"%s\" "
  1246. "to list of excluded groups for \"%s\" "
  1247. "(matched: \"%s=%s\").\n",
  1248. slapi_sdn_get_dn(curr_rule->target_group_dn),
  1249. slapi_entry_get_dn(e), curr_rule->attr,
  1250. curr_rule->regex_str);
  1251. dnitem = (struct automemberDNListItem *)slapi_ch_calloc(1,
  1252. sizeof(struct automemberDNListItem));
  1253. /* We are just referencing the dn from the regex rule. We
  1254. * will not free it when we clean up this list. This list
  1255. * is more short-lived than the regex rule list, so we can
  1256. * get away with this optimization. */
  1257. dnitem->dn = curr_rule->target_group_dn;
  1258. PR_APPEND_LINK(&(dnitem->list), &exclusions);
  1259. last = curr_rule->target_group_dn;
  1260. }
  1261. }
  1262. slapi_ch_array_free(vals);
  1263. vals = NULL;
  1264. }
  1265. rule = PR_NEXT_LINK(rule);
  1266. }
  1267. }
  1268. }
  1269. /* Go through inclusive rules and build the target list. */
  1270. if (config->inclusive_rules) {
  1271. if (!PR_CLIST_IS_EMPTY((PRCList *)config->inclusive_rules)) {
  1272. /* Clear out our last hint from processing exclusions. */
  1273. last = NULL;
  1274. /* Get the first excluded target for exclusion checking. */
  1275. if (!PR_CLIST_IS_EMPTY(&exclusions)) {
  1276. curr_exclusion = PR_LIST_HEAD(&exclusions);
  1277. }
  1278. rule = PR_LIST_HEAD((PRCList *)config->inclusive_rules);
  1279. while (rule != (PRCList *)config->inclusive_rules) {
  1280. curr_rule = (struct automemberRegexRule *)rule;
  1281. /* The excluded targets are stored in alphabetical order. Instead
  1282. * of going through the entire exclusion list for each inclusive
  1283. * rule, we can simply go through the exclusion list once and keep
  1284. * track of our position. If the curent exclusion comes after
  1285. * the target DN used in the current inclusive rule, it can't be
  1286. * excluded. If the current exclusion comes before the target
  1287. * in the current rule, we need to go through the exclusion list
  1288. * until we find a target that is the same or comes after the
  1289. * current rule. */
  1290. if (curr_exclusion) {
  1291. while ((curr_exclusion != &exclusions) && (slapi_sdn_compare(
  1292. ((struct automemberDNListItem *)curr_exclusion)->dn,
  1293. curr_rule->target_group_dn) < 0)) {
  1294. curr_exclusion = PR_NEXT_LINK(curr_exclusion);
  1295. }
  1296. }
  1297. /* Regex rules are sorted by the target group DN. This means
  1298. * we can skip all rules for the last target group DN that we
  1299. * added to the targets list. We also skip any rules for
  1300. * target groups that have been excluded by an exclusion rule. */
  1301. if (((curr_exclusion == NULL) || (curr_exclusion == &exclusions) ||
  1302. slapi_sdn_compare(((struct automemberDNListItem *)curr_exclusion)->dn,
  1303. curr_rule->target_group_dn) != 0) && ((last == NULL) ||
  1304. (slapi_sdn_compare(last, curr_rule->target_group_dn) != 0))) {
  1305. /* Get comparison attr and loop through values. */
  1306. vals = slapi_entry_attr_get_charray(e, curr_rule->attr);
  1307. for (i = 0; vals && vals[i]; ++i) {
  1308. /* Evaluate the regex. */
  1309. if (slapi_re_exec(curr_rule->regex, vals[i], -1) == 1) {
  1310. /* Found a match. Add to the end of the targets list
  1311. * and set last as a hint to ourselves. */
  1312. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1313. "automember_update_membership: Adding \"%s\" "
  1314. "to list of target groups for \"%s\" "
  1315. "(matched: \"%s=%s\").\n",
  1316. slapi_sdn_get_dn(curr_rule->target_group_dn),
  1317. slapi_entry_get_dn(e), curr_rule->attr,
  1318. curr_rule->regex_str);
  1319. dnitem = (struct automemberDNListItem *)slapi_ch_calloc(1,
  1320. sizeof(struct automemberDNListItem));
  1321. /* We are just referencing the dn from the regex rule. We
  1322. * will not free it when we clean up this list. This list
  1323. * is more short-lived than the regex rule list, so we can
  1324. * get away with this optimization. */
  1325. dnitem->dn = curr_rule->target_group_dn;
  1326. PR_APPEND_LINK(&(dnitem->list), &targets);
  1327. last = curr_rule->target_group_dn;
  1328. }
  1329. }
  1330. slapi_ch_array_free(vals);
  1331. vals = NULL;
  1332. }
  1333. rule = PR_NEXT_LINK(rule);
  1334. }
  1335. }
  1336. }
  1337. /* If no targets, update default groups if set. Otherwise, update
  1338. * targets. Use a helper to do the actual updates. We can just pass an
  1339. * array of target group DNs along with our entry DN, the grouping attr,
  1340. * and the grouping value. */
  1341. if (PR_CLIST_IS_EMPTY(&targets)) {
  1342. /* Add to each default group. */
  1343. for (i = 0; config->default_groups && config->default_groups[i]; i++) {
  1344. automember_add_member_value(e, config->default_groups[i],
  1345. config->grouping_attr, config->grouping_value, ldif_fd);
  1346. }
  1347. } else {
  1348. /* Update the target groups. */
  1349. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&targets);
  1350. while ((PRCList *)dnitem != &targets) {
  1351. automember_add_member_value(e, slapi_sdn_get_dn(dnitem->dn),
  1352. config->grouping_attr, config->grouping_value, ldif_fd);
  1353. dnitem = (struct automemberDNListItem *)PR_NEXT_LINK((PRCList *)dnitem);
  1354. }
  1355. }
  1356. /* Free the exclusions and targets lists. Remember that the DN's
  1357. * are not ours, so don't free them! */
  1358. while (!PR_CLIST_IS_EMPTY(&exclusions)) {
  1359. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&exclusions);
  1360. PR_REMOVE_LINK((PRCList *)dnitem);
  1361. slapi_ch_free((void**)&dnitem);
  1362. }
  1363. while (!PR_CLIST_IS_EMPTY(&targets)) {
  1364. dnitem = (struct automemberDNListItem *)PR_LIST_HEAD(&targets);
  1365. PR_REMOVE_LINK((PRCList *)dnitem);
  1366. slapi_ch_free((void**)&dnitem);
  1367. }
  1368. }
  1369. /*
  1370. * automember_add_member_value()
  1371. *
  1372. * Adds a member entry to a group.
  1373. */
  1374. static void
  1375. automember_add_member_value(Slapi_Entry *member_e, const char *group_dn, char *grouping_attr,
  1376. char *grouping_value, PRFileDesc *ldif_fd)
  1377. {
  1378. Slapi_PBlock *mod_pb = slapi_pblock_new();
  1379. int result = LDAP_SUCCESS;
  1380. LDAPMod mod;
  1381. LDAPMod *mods[2];
  1382. char *vals[2];
  1383. char *member_value = NULL;
  1384. int freeit = 0;
  1385. /* If grouping_value is dn, we need to fetch the dn instead. */
  1386. if (slapi_attr_type_cmp(grouping_value, "dn", SLAPI_TYPE_CMP_EXACT) == 0) {
  1387. member_value = slapi_entry_get_ndn(member_e);
  1388. } else {
  1389. member_value = slapi_entry_attr_get_charptr(member_e, grouping_value);
  1390. freeit = 1;
  1391. }
  1392. /*
  1393. * If ldif_fd is set, we are performing an export task. Write the changes to the
  1394. * file instead of performing them
  1395. */
  1396. if(ldif_fd){
  1397. PR_fprintf(ldif_fd, "dn: %s\n", group_dn);
  1398. PR_fprintf(ldif_fd, "changetype: modify\n");
  1399. PR_fprintf(ldif_fd, "add: %s\n", grouping_attr);
  1400. PR_fprintf(ldif_fd, "%s: %s\n", grouping_attr, member_value);
  1401. PR_fprintf(ldif_fd, "\n");
  1402. goto out;
  1403. }
  1404. if (member_value) {
  1405. /* Set up the operation. */
  1406. vals[0] = member_value;
  1407. vals[1] = 0;
  1408. mod.mod_op = LDAP_MOD_ADD;
  1409. mod.mod_type = grouping_attr;
  1410. mod.mod_values = vals;
  1411. mods[0] = &mod;
  1412. mods[1] = 0;
  1413. /* Perform the modify operation. */
  1414. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1415. "automember_add_member_value: Adding \"%s\" as "
  1416. "a \"%s\" value to group \"%s\".\n",
  1417. member_value, grouping_attr, group_dn);
  1418. slapi_modify_internal_set_pb(mod_pb, group_dn,
  1419. mods, 0, 0, automember_get_plugin_id(), 0);
  1420. slapi_modify_internal_pb(mod_pb);
  1421. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1422. if ((result != LDAP_SUCCESS) && (result != LDAP_TYPE_OR_VALUE_EXISTS)) {
  1423. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1424. "automember_add_member_value: Unable to add \"%s\" as "
  1425. "a \"%s\" value to group \"%s\" (%s).\n",
  1426. member_value, grouping_attr, group_dn,
  1427. ldap_err2string(result));
  1428. }
  1429. } else {
  1430. slapi_log_error(SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1431. "automember_add_member_value: Unable to find grouping "
  1432. "value attribute \"%s\" in entry \"%s\".\n",
  1433. grouping_value, slapi_entry_get_dn(member_e));
  1434. }
  1435. out:
  1436. /* Cleanup */
  1437. if (freeit) {
  1438. slapi_ch_free_string(&member_value);
  1439. }
  1440. slapi_pblock_destroy(mod_pb);
  1441. }
  1442. /*
  1443. * Operation callback functions
  1444. */
  1445. /*
  1446. * automember_pre_op()
  1447. *
  1448. * Checks if an operation affects the auto membership
  1449. * config and validates the config changes.
  1450. */
  1451. static int
  1452. automember_pre_op(Slapi_PBlock * pb, int modop)
  1453. {
  1454. Slapi_DN *sdn = 0;
  1455. Slapi_Entry *e = 0;
  1456. Slapi_Mods *smods = 0;
  1457. LDAPMod **mods;
  1458. int free_entry = 0;
  1459. char *errstr = NULL;
  1460. int ret = SLAPI_PLUGIN_SUCCESS;
  1461. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1462. "--> automember_pre_op\n");
  1463. /* Just bail if we aren't ready to service requests yet. */
  1464. if (!g_plugin_started)
  1465. goto bail;
  1466. if (0 == (sdn = automember_get_sdn(pb)))
  1467. goto bail;
  1468. if (automember_dn_is_config(sdn)) {
  1469. /* Validate config changes, but don't apply them.
  1470. * This allows us to reject invalid config changes
  1471. * here at the pre-op stage. Applying the config
  1472. * needs to be done at the post-op stage. */
  1473. if (LDAP_CHANGETYPE_ADD == modop) {
  1474. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  1475. /* If the entry doesn't exist, just bail and
  1476. * let the server handle it. */
  1477. if (e == NULL) {
  1478. goto bail;
  1479. }
  1480. } else if (LDAP_CHANGETYPE_MODIFY == modop) {
  1481. /* Fetch the entry being modified so we can
  1482. * create the resulting entry for validation. */
  1483. if (sdn) {
  1484. slapi_search_internal_get_entry(sdn, 0, &e, automember_get_plugin_id());
  1485. free_entry = 1;
  1486. }
  1487. /* If the entry doesn't exist, just bail and
  1488. * let the server handle it. */
  1489. if (e == NULL) {
  1490. goto bail;
  1491. }
  1492. /* Grab the mods. */
  1493. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1494. smods = slapi_mods_new();
  1495. slapi_mods_init_byref(smods, mods);
  1496. /* Apply the mods to create the resulting entry. */
  1497. if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
  1498. /* The mods don't apply cleanly, so we just let this op go
  1499. * to let the main server handle it. */
  1500. goto bailmod;
  1501. }
  1502. } else {
  1503. errstr = slapi_ch_smprintf("automember_pre_op: invalid op type %d",
  1504. modop);
  1505. ret = LDAP_PARAM_ERROR;
  1506. goto bail;
  1507. }
  1508. if (automember_parse_config_entry(e, 0) != 0) {
  1509. /* Refuse the operation if config parsing failed. */
  1510. ret = LDAP_UNWILLING_TO_PERFORM;
  1511. if (LDAP_CHANGETYPE_ADD == modop) {
  1512. errstr = slapi_ch_smprintf("Not a valid auto membership configuration entry.");
  1513. } else {
  1514. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1515. "auto membership configuration.");
  1516. }
  1517. }
  1518. }
  1519. bailmod:
  1520. /* Clean up smods. */
  1521. if (LDAP_CHANGETYPE_MODIFY == modop) {
  1522. slapi_mods_free(&smods);
  1523. }
  1524. bail:
  1525. if (free_entry && e)
  1526. slapi_entry_free(e);
  1527. if (ret) {
  1528. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1529. "automember_pre_op: operation failure [%d]\n", ret);
  1530. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  1531. slapi_ch_free((void **)&errstr);
  1532. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  1533. ret = SLAPI_PLUGIN_FAILURE;
  1534. }
  1535. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1536. "<-- automember_pre_op\n");
  1537. return ret;
  1538. }
  1539. static int
  1540. automember_add_pre_op(Slapi_PBlock * pb)
  1541. {
  1542. return automember_pre_op(pb, LDAP_CHANGETYPE_ADD);
  1543. }
  1544. static int
  1545. automember_mod_pre_op(Slapi_PBlock * pb)
  1546. {
  1547. return automember_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  1548. }
  1549. /*
  1550. * automember_mod_post_op()
  1551. *
  1552. * Reloads the auto membership config
  1553. * if config changes were made.
  1554. */
  1555. static int
  1556. automember_mod_post_op(Slapi_PBlock *pb)
  1557. {
  1558. Slapi_DN *sdn = NULL;
  1559. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1560. "--> automember_mod_post_op\n");
  1561. /* Just bail if we aren't ready to service requests yet. */
  1562. if (!g_plugin_started) {
  1563. goto bail;
  1564. }
  1565. if (automember_oktodo(pb) && (sdn = automember_get_sdn(pb))) {
  1566. /* Check if the config is being modified and reload if so. */
  1567. if (automember_dn_is_config(sdn)) {
  1568. automember_load_config();
  1569. }
  1570. }
  1571. bail:
  1572. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1573. "<-- automember_mod_post_op\n");
  1574. return SLAPI_PLUGIN_SUCCESS;
  1575. }
  1576. static int
  1577. automember_add_post_op(Slapi_PBlock *pb)
  1578. {
  1579. Slapi_Entry *e = NULL;
  1580. Slapi_DN *sdn = NULL;
  1581. struct configEntry *config = NULL;
  1582. PRCList *list = NULL;
  1583. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1584. "--> automember_add_post_op\n");
  1585. /* Just bail if we aren't ready to service requests yet. */
  1586. if (!g_plugin_started || !automember_oktodo(pb))
  1587. return SLAPI_PLUGIN_SUCCESS;
  1588. /* Reload config if a config entry was added. */
  1589. if ((sdn = automember_get_sdn(pb))) {
  1590. if (automember_dn_is_config(sdn)) {
  1591. automember_load_config();
  1592. }
  1593. } else {
  1594. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1595. "automember_add_post_op: Error "
  1596. "retrieving dn\n");
  1597. goto bail;
  1598. }
  1599. /* If replication, just bail. */
  1600. if (automember_isrepl(pb)) {
  1601. return SLAPI_PLUGIN_SUCCESS;
  1602. }
  1603. /* Get the newly added entry. */
  1604. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  1605. if (e) {
  1606. /* If the entry is a tombstone, just bail. */
  1607. Slapi_Value *tombstone =
  1608. slapi_value_new_string(SLAPI_ATTR_VALUE_TOMBSTONE);
  1609. int rc = slapi_entry_attr_has_syntax_value(e, SLAPI_ATTR_OBJECTCLASS,
  1610. tombstone);
  1611. slapi_value_free(&tombstone);
  1612. if (rc) {
  1613. return SLAPI_PLUGIN_SUCCESS;
  1614. }
  1615. /* Check if a config entry applies
  1616. * to the entry being added. */
  1617. automember_config_read_lock();
  1618. /* Bail out if the plug-in close function was just called. */
  1619. if (!g_plugin_started) {
  1620. automember_config_unlock();
  1621. return SLAPI_PLUGIN_SUCCESS;
  1622. }
  1623. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  1624. list = PR_LIST_HEAD(g_automember_config);
  1625. while (list != g_automember_config) {
  1626. config = (struct configEntry *)list;
  1627. /* Does the entry meet scope and filter requirements? */
  1628. if (slapi_dn_issuffix(slapi_sdn_get_dn(sdn), config->scope) &&
  1629. (slapi_filter_test_simple(e, config->filter) == 0)) {
  1630. /* Find out what membership changes are needed and make them. */
  1631. automember_update_membership(config, e, NULL);
  1632. }
  1633. list = PR_NEXT_LINK(list);
  1634. }
  1635. }
  1636. automember_config_unlock();
  1637. } else {
  1638. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1639. "automember_add_post_op: Error "
  1640. "retrieving post-op entry %s\n", slapi_sdn_get_dn(sdn));
  1641. }
  1642. bail:
  1643. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1644. "<-- automember_add_post_op\n");
  1645. return SLAPI_PLUGIN_SUCCESS;
  1646. }
  1647. /*
  1648. * automember_del_post_op()
  1649. *
  1650. * Removes deleted config.
  1651. */
  1652. static int
  1653. automember_del_post_op(Slapi_PBlock *pb)
  1654. {
  1655. Slapi_DN *sdn = NULL;
  1656. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1657. "--> automember_del_post_op\n");
  1658. /* Just bail if we aren't ready to service requests yet. */
  1659. if (!g_plugin_started || !automember_oktodo(pb)) {
  1660. return SLAPI_PLUGIN_SUCCESS;
  1661. }
  1662. /* Reload config if a config entry was deleted. */
  1663. if ((sdn = automember_get_sdn(pb))) {
  1664. if (automember_dn_is_config(sdn))
  1665. automember_load_config();
  1666. } else {
  1667. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1668. "automember_del_post_op: Error "
  1669. "retrieving dn\n");
  1670. }
  1671. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1672. "<-- automember_del_post_op\n");
  1673. return SLAPI_PLUGIN_SUCCESS;
  1674. }
  1675. typedef struct _task_data
  1676. {
  1677. char *filter_str;
  1678. char *ldif_out;
  1679. char *ldif_in;
  1680. Slapi_DN *base_dn;
  1681. char *bind_dn;
  1682. int scope;
  1683. } task_data;
  1684. /*
  1685. * extract a single value from the entry (as a string) -- if it's not in the
  1686. * entry, the default will be returned (which can be NULL).
  1687. * you do not need to free anything returned by this.
  1688. */
  1689. const char *
  1690. fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val)
  1691. {
  1692. Slapi_Value *val = NULL;
  1693. Slapi_Attr *attr;
  1694. if(slapi_entry_attr_find(e, attrname, &attr) != 0){
  1695. return default_val;
  1696. }
  1697. slapi_attr_first_value(attr, &val);
  1698. return slapi_value_get_string(val);
  1699. }
  1700. void
  1701. automember_task_destructor(Slapi_Task *task)
  1702. {
  1703. if (task) {
  1704. task_data *mydata = (task_data *)slapi_task_get_data(task);
  1705. if (mydata) {
  1706. slapi_ch_free_string(&mydata->bind_dn);
  1707. slapi_sdn_free(&mydata->base_dn);
  1708. slapi_ch_free_string(&mydata->filter_str);
  1709. slapi_ch_free((void **)&mydata);
  1710. }
  1711. }
  1712. }
  1713. void
  1714. automember_task_export_destructor(Slapi_Task *task)
  1715. {
  1716. if (task) {
  1717. task_data *mydata = (task_data *)slapi_task_get_data(task);
  1718. if (mydata) {
  1719. slapi_ch_free_string(&mydata->ldif_out);
  1720. slapi_ch_free_string(&mydata->bind_dn);
  1721. slapi_sdn_free(&mydata->base_dn);
  1722. slapi_ch_free_string(&mydata->filter_str);
  1723. slapi_ch_free((void **)&mydata);
  1724. }
  1725. }
  1726. }
  1727. void
  1728. automember_task_map_destructor(Slapi_Task *task)
  1729. {
  1730. if (task) {
  1731. task_data *mydata = (task_data *)slapi_task_get_data(task);
  1732. if (mydata) {
  1733. slapi_ch_free_string(&mydata->ldif_out);
  1734. slapi_ch_free_string(&mydata->ldif_in);
  1735. slapi_ch_free_string(&mydata->bind_dn);
  1736. slapi_ch_free((void **)&mydata);
  1737. }
  1738. }
  1739. }
  1740. /*
  1741. * automember_task_add
  1742. *
  1743. * This task is designed to "retro-fit" entries that existed prior to
  1744. * enabling this plugin. This can be an expensive task to run, but it's
  1745. * better than processing every modify operation in an attempt to catch
  1746. * entries that have not been processed.
  1747. *
  1748. * task entry:
  1749. *
  1750. * dn: cn=my rebuild task, cn=automember rebuild membership,cn=tasks,cn=config
  1751. * objectClass: top
  1752. * objectClass: extensibleObject
  1753. * cn: my rebuild task
  1754. * basedn: dc=example,dc=com
  1755. * filter: (uid=*)
  1756. * scope: sub
  1757. *
  1758. * basedn and filter are required. If scope is omitted, the default is sub
  1759. */
  1760. static int
  1761. automember_task_add(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1762. int *returncode, char *returntext, void *arg)
  1763. {
  1764. int rv = SLAPI_DSE_CALLBACK_OK;
  1765. task_data *mytaskdata = NULL;
  1766. Slapi_Task *task = NULL;
  1767. PRThread *thread = NULL;
  1768. char *bind_dn = NULL;
  1769. const char *base_dn;
  1770. const char *filter;
  1771. const char *scope;
  1772. *returncode = LDAP_SUCCESS;
  1773. /*
  1774. * Make sure the plugin is started
  1775. */
  1776. if(!g_plugin_started){
  1777. *returncode = LDAP_OPERATIONS_ERROR;
  1778. rv = SLAPI_DSE_CALLBACK_ERROR;
  1779. goto out;
  1780. }
  1781. /*
  1782. * Grab the task params
  1783. */
  1784. if((base_dn = fetch_attr(e, "basedn", 0)) == NULL){
  1785. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1786. rv = SLAPI_DSE_CALLBACK_ERROR;
  1787. goto out;
  1788. }
  1789. if((filter = fetch_attr(e, "filter", 0)) == NULL){
  1790. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1791. rv = SLAPI_DSE_CALLBACK_ERROR;
  1792. goto out;
  1793. }
  1794. scope = fetch_attr(e, "scope", "sub");
  1795. /*
  1796. * setup our task data
  1797. */
  1798. mytaskdata = (task_data*)slapi_ch_malloc(sizeof(task_data));
  1799. if (mytaskdata == NULL){
  1800. *returncode = LDAP_OPERATIONS_ERROR;
  1801. rv = SLAPI_DSE_CALLBACK_ERROR;
  1802. goto out;
  1803. }
  1804. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  1805. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  1806. mytaskdata->base_dn = slapi_sdn_new_dn_byval(base_dn);
  1807. mytaskdata->filter_str = slapi_ch_strdup(filter);
  1808. if(scope){
  1809. if(strcasecmp(scope,"sub")== 0){
  1810. mytaskdata->scope = 2;
  1811. } else if(strcasecmp(scope,"one")== 0){
  1812. mytaskdata->scope = 1;
  1813. } else if(strcasecmp(scope,"base")== 0){
  1814. mytaskdata->scope = 0;
  1815. } else {
  1816. /* Hmm, possible typo, use subtree */
  1817. mytaskdata->scope = 2;
  1818. }
  1819. } else {
  1820. /* subtree by default */
  1821. mytaskdata->scope = 2;
  1822. }
  1823. task = slapi_new_task(slapi_entry_get_ndn(e));
  1824. slapi_task_set_destructor_fn(task, automember_task_destructor);
  1825. slapi_task_set_data(task, mytaskdata);
  1826. /*
  1827. * Start the task as a separate thread
  1828. */
  1829. thread = PR_CreateThread(PR_USER_THREAD, automember_rebuild_task_thread,
  1830. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1831. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1832. if (thread == NULL){
  1833. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1834. "unable to create task thread!\n");
  1835. *returncode = LDAP_OPERATIONS_ERROR;
  1836. rv = SLAPI_DSE_CALLBACK_ERROR;
  1837. slapi_task_finish(task, *returncode);
  1838. } else {
  1839. rv = SLAPI_DSE_CALLBACK_OK;
  1840. }
  1841. out:
  1842. return rv;
  1843. }
  1844. /*
  1845. * automember_rebuild_task_thread()
  1846. *
  1847. * Search using the basedn, filter, and scope provided from the task data.
  1848. * Then loop of each entry, and apply the membership if applicable.
  1849. */
  1850. void automember_rebuild_task_thread(void *arg){
  1851. Slapi_Task *task = (Slapi_Task *)arg;
  1852. struct configEntry *config = NULL;
  1853. Slapi_PBlock *search_pb = NULL;
  1854. Slapi_Entry **entries = NULL;
  1855. task_data *td = NULL;
  1856. PRCList *list = NULL;
  1857. int result = 0;
  1858. int i = 0;
  1859. /*
  1860. * Fetch our task data from the task
  1861. */
  1862. td = (task_data *)slapi_task_get_data(task);
  1863. slapi_task_begin(task, 1);
  1864. slapi_task_log_notice(task, "Automember rebuild task starting (base dn: (%s) filter (%s)...\n",
  1865. slapi_sdn_get_dn(td->base_dn),td->filter_str);
  1866. slapi_task_log_status(task, "Automember rebuild task starting (base dn: (%s) filter (%s)...\n",
  1867. slapi_sdn_get_dn(td->base_dn),td->filter_str);
  1868. /*
  1869. * Set the bind dn in the local thread data
  1870. */
  1871. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  1872. /*
  1873. * Search the database
  1874. */
  1875. search_pb = slapi_pblock_new();
  1876. slapi_search_internal_set_pb_ext(search_pb, td->base_dn, td->scope, td->filter_str, NULL,
  1877. 0, NULL, NULL, automember_get_plugin_id(), 0);
  1878. slapi_search_internal_pb(search_pb);
  1879. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1880. if (LDAP_SUCCESS != result){
  1881. slapi_task_log_notice(task, "Automember rebuild membership task unable to search"
  1882. " on base (%s) filter (%s) error (%d)\n", slapi_sdn_get_dn(td->base_dn),
  1883. td->filter_str, result);
  1884. slapi_task_log_status(task, "Automember rebuild membership task unable to search"
  1885. " on base (%s) filter (%s) error (%d)\n", slapi_sdn_get_dn(td->base_dn),
  1886. td->filter_str, result);
  1887. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  1888. "Task: unable to search on base (%s) filter (%s) error (%d)\n",
  1889. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  1890. goto out;
  1891. }
  1892. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1893. /*
  1894. * Grab the config read lock, and loop over the entries
  1895. */
  1896. automember_config_read_lock();
  1897. for (i = 0; entries && (entries[i] != NULL); i++){
  1898. /* make sure the plugin is still up, as this loop could run for awhile */
  1899. if (!g_plugin_started) {
  1900. automember_config_unlock();
  1901. result = -1;
  1902. goto out;
  1903. }
  1904. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  1905. list = PR_LIST_HEAD(g_automember_config);
  1906. while (list != g_automember_config) {
  1907. config = (struct configEntry *)list;
  1908. /* Does the entry meet scope and filter requirements? */
  1909. if (slapi_dn_issuffix(slapi_entry_get_dn(entries[i]), config->scope) &&
  1910. (slapi_filter_test_simple(entries[i], config->filter) == 0))
  1911. {
  1912. automember_update_membership(config, entries[i], NULL);
  1913. }
  1914. list = PR_NEXT_LINK(list);
  1915. }
  1916. }
  1917. }
  1918. automember_config_unlock();
  1919. slapi_free_search_results_internal(search_pb);
  1920. out:
  1921. if(result){
  1922. /* error */
  1923. slapi_task_log_notice(task, "Automember rebuild task aborted. Error (%d)", result);
  1924. slapi_task_log_status(task, "Automember rebuild task aborted. Error (%d)", result);
  1925. } else {
  1926. slapi_task_log_notice(task, "Automember rebuild task finished. Processed (%d) entries.", i);
  1927. slapi_task_log_status(task, "Automember rebuild task finished. Processed (%d) entries.", i);
  1928. }
  1929. slapi_task_inc_progress(task);
  1930. slapi_task_finish(task, result);
  1931. }
  1932. /*
  1933. * Export an ldif of the changes that would be made if we ran the automember rebuild membership task
  1934. *
  1935. * task entry:
  1936. *
  1937. * dn: cn=my export task, cn=automember export updates,cn=tasks,cn=config
  1938. * objectClass: top
  1939. * objectClass: extensibleObject
  1940. * cn: my export task
  1941. * basedn: dc=example,dc=com
  1942. * filter: (uid=*)
  1943. * scope: sub
  1944. * ldif: /tmp/automem-updates.ldif
  1945. */
  1946. static int
  1947. automember_task_add_export_updates(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  1948. int *returncode, char *returntext, void *arg)
  1949. {
  1950. int rv = SLAPI_DSE_CALLBACK_OK;
  1951. task_data *mytaskdata = NULL;
  1952. Slapi_Task *task = NULL;
  1953. PRThread *thread = NULL;
  1954. char *bind_dn = NULL;
  1955. const char *base_dn = NULL;
  1956. const char *filter = NULL;
  1957. const char *ldif = NULL;
  1958. const char *scope = NULL;
  1959. *returncode = LDAP_SUCCESS;
  1960. /*
  1961. * Make sure the plugin is started
  1962. */
  1963. if(!g_plugin_started){
  1964. *returncode = LDAP_OPERATIONS_ERROR;
  1965. rv = SLAPI_DSE_CALLBACK_ERROR;
  1966. goto out;
  1967. }
  1968. if((ldif = fetch_attr(e, "ldif", 0)) == NULL){
  1969. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1970. rv = SLAPI_DSE_CALLBACK_ERROR;
  1971. goto out;
  1972. }
  1973. if((base_dn = fetch_attr(e, "basedn", 0)) == NULL){
  1974. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1975. rv = SLAPI_DSE_CALLBACK_ERROR;
  1976. goto out;
  1977. }
  1978. if((filter = fetch_attr(e, "filter", 0)) == NULL){
  1979. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  1980. rv = SLAPI_DSE_CALLBACK_ERROR;
  1981. goto out;
  1982. }
  1983. scope = fetch_attr(e, "scope", "sub");
  1984. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  1985. mytaskdata = (task_data*)slapi_ch_malloc(sizeof(task_data));
  1986. if (mytaskdata == NULL){
  1987. *returncode = LDAP_OPERATIONS_ERROR;
  1988. rv = SLAPI_DSE_CALLBACK_ERROR;
  1989. goto out;
  1990. }
  1991. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  1992. mytaskdata->ldif_out = slapi_ch_strdup(ldif);
  1993. mytaskdata->base_dn = slapi_sdn_new_dn_byval(base_dn);
  1994. mytaskdata->filter_str = slapi_ch_strdup(filter);
  1995. if(scope){
  1996. if(strcasecmp(scope,"sub")== 0){
  1997. mytaskdata->scope = 2;
  1998. } else if(strcasecmp(scope,"one")== 0){
  1999. mytaskdata->scope = 1;
  2000. } else if(strcasecmp(scope,"base")== 0){
  2001. mytaskdata->scope = 0;
  2002. } else {
  2003. /* Hmm, possible typo, use subtree */
  2004. mytaskdata->scope = 2;
  2005. }
  2006. } else {
  2007. /* subtree by default */
  2008. mytaskdata->scope = 2;
  2009. }
  2010. task = slapi_new_task(slapi_entry_get_ndn(e));
  2011. slapi_task_set_destructor_fn(task, automember_task_export_destructor);
  2012. slapi_task_set_data(task, mytaskdata);
  2013. /*
  2014. * Start the task as a separate thread
  2015. */
  2016. thread = PR_CreateThread(PR_USER_THREAD, automember_export_task_thread,
  2017. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2018. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2019. if (thread == NULL){
  2020. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2021. "unable to create export task thread!\n");
  2022. *returncode = LDAP_OPERATIONS_ERROR;
  2023. rv = SLAPI_DSE_CALLBACK_ERROR;
  2024. slapi_task_finish(task, *returncode);
  2025. } else {
  2026. rv = SLAPI_DSE_CALLBACK_OK;
  2027. }
  2028. out:
  2029. return rv;
  2030. }
  2031. void automember_export_task_thread(void *arg){
  2032. Slapi_Task *task = (Slapi_Task *)arg;
  2033. Slapi_PBlock *search_pb = NULL;
  2034. Slapi_Entry **entries = NULL;
  2035. int result = SLAPI_DSE_CALLBACK_OK;
  2036. struct configEntry *config = NULL;
  2037. PRCList *list = NULL;
  2038. task_data *td = NULL;
  2039. PRFileDesc *ldif_fd;
  2040. int i = 0;
  2041. int rc = 0;
  2042. td = (task_data *)slapi_task_get_data(task);
  2043. slapi_task_begin(task, 1);
  2044. slapi_task_log_notice(task, "Automember export task starting. Exporting changes to (%s)", td->ldif_out);
  2045. slapi_task_log_status(task, "Automember export task starting. Exporting changes to (%s)", td->ldif_out);
  2046. /* make sure we can open the ldif file */
  2047. if (( ldif_fd = PR_Open( td->ldif_out, PR_CREATE_FILE | PR_WRONLY, DEFAULT_FILE_MODE )) == NULL ){
  2048. rc = PR_GetOSError();
  2049. slapi_task_log_notice(task, "Automember export task could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2050. td->ldif_out, rc, slapi_system_strerror(rc));
  2051. slapi_task_log_status(task, "Automember export task could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2052. td->ldif_out, rc, slapi_system_strerror(rc) );
  2053. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2054. "Could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2055. td->ldif_out, rc, slapi_system_strerror(rc) );
  2056. result = SLAPI_DSE_CALLBACK_ERROR;
  2057. goto out;
  2058. }
  2059. /*
  2060. * Set the bind dn in the local thread data
  2061. */
  2062. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  2063. /*
  2064. * Search the database
  2065. */
  2066. search_pb = slapi_pblock_new();
  2067. slapi_search_internal_set_pb_ext(search_pb, td->base_dn, td->scope, td->filter_str, NULL,
  2068. 0, NULL, NULL, automember_get_plugin_id(), 0);
  2069. slapi_search_internal_pb(search_pb);
  2070. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2071. if (LDAP_SUCCESS != result){
  2072. slapi_task_log_notice(task, "Automember task failed to search on base (%s) filter (%s) error (%d)\n",
  2073. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2074. slapi_task_log_status(task, "Automember task failed to search on base (%s) filter (%s) error (%d)\n",
  2075. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2076. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2077. "Task: unable to search on base (%s) filter (%s) error (%d)\n",
  2078. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2079. goto out;
  2080. }
  2081. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2082. /*
  2083. * Grab the config read lock, and loop over the entries
  2084. */
  2085. automember_config_read_lock();
  2086. for (i = 0; entries && (entries[i] != NULL); i++){
  2087. /* make sure the plugin is still up, as this loop could run for awhile */
  2088. if (!g_plugin_started) {
  2089. automember_config_unlock();
  2090. result = -1;
  2091. goto out;
  2092. }
  2093. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  2094. list = PR_LIST_HEAD(g_automember_config);
  2095. while (list != g_automember_config) {
  2096. config = (struct configEntry *)list;
  2097. if (slapi_dn_issuffix(slapi_sdn_get_dn(td->base_dn), config->scope) &&
  2098. (slapi_filter_test_simple(entries[i], config->filter) == 0))
  2099. {
  2100. automember_update_membership(config, entries[i], ldif_fd);
  2101. }
  2102. list = PR_NEXT_LINK(list);
  2103. }
  2104. }
  2105. }
  2106. automember_config_unlock();
  2107. slapi_free_search_results_internal(search_pb);
  2108. out:
  2109. if(ldif_fd){
  2110. PR_Close(ldif_fd);
  2111. }
  2112. if(result){
  2113. /* error */
  2114. slapi_task_log_notice(task, "Automember export task aborted. Error (%d)", result);
  2115. slapi_task_log_status(task, "Automember export task aborted. Error (%d)", result);
  2116. } else {
  2117. slapi_task_log_notice(task, "Automember export task finished. Processed (%d) entries.", i);
  2118. slapi_task_log_status(task, "Automember export task finished. Processed (%d) entries.", i);
  2119. }
  2120. slapi_task_inc_progress(task);
  2121. slapi_task_finish(task, result);
  2122. }
  2123. /*
  2124. * Export an ldif of the changes that would be made from the entries
  2125. * in the provided ldif file
  2126. *
  2127. * task entry:
  2128. *
  2129. * dn: cn=my map task, cn=automember map updates,cn=tasks,cn=config
  2130. * objectClass: top
  2131. * objectClass: extensibleObject
  2132. * cn: my export task
  2133. * ldif_in: /tmp/entries.ldif
  2134. * ldif_out: /tmp/automem-updates.ldif
  2135. */
  2136. static int
  2137. automember_task_add_map_entries(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  2138. int *returncode, char *returntext, void *arg)
  2139. {
  2140. int rv = SLAPI_DSE_CALLBACK_OK;
  2141. task_data *mytaskdata = NULL;
  2142. Slapi_Task *task = NULL;
  2143. PRThread *thread = NULL;
  2144. char *bind_dn;
  2145. const char *ldif_out;
  2146. const char *ldif_in;
  2147. *returncode = LDAP_SUCCESS;
  2148. /*
  2149. * Make sure the plugin is started
  2150. */
  2151. if(!g_plugin_started){
  2152. *returncode = LDAP_OPERATIONS_ERROR;
  2153. rv = SLAPI_DSE_CALLBACK_ERROR;
  2154. goto out;
  2155. }
  2156. /*
  2157. * Get the params
  2158. */
  2159. if((ldif_in = fetch_attr(e, "ldif_in", 0)) == NULL){
  2160. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2161. rv = SLAPI_DSE_CALLBACK_ERROR;
  2162. goto out;
  2163. }
  2164. if((ldif_out = fetch_attr(e, "ldif_out", 0)) == NULL){
  2165. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2166. rv = SLAPI_DSE_CALLBACK_ERROR;
  2167. goto out;
  2168. }
  2169. /*
  2170. * Setup the task data
  2171. */
  2172. mytaskdata = (task_data*)slapi_ch_malloc(sizeof(task_data));
  2173. if (mytaskdata == NULL){
  2174. *returncode = LDAP_OPERATIONS_ERROR;
  2175. rv = SLAPI_DSE_CALLBACK_ERROR;
  2176. goto out;
  2177. }
  2178. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  2179. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  2180. mytaskdata->ldif_out = slapi_ch_strdup(ldif_out);
  2181. mytaskdata->ldif_in = slapi_ch_strdup(ldif_in);
  2182. task = slapi_new_task(slapi_entry_get_ndn(e));
  2183. slapi_task_set_destructor_fn(task, automember_task_map_destructor);
  2184. slapi_task_set_data(task, mytaskdata);
  2185. /*
  2186. * Start the task as a separate thread
  2187. */
  2188. thread = PR_CreateThread(PR_USER_THREAD, automember_map_task_thread,
  2189. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2190. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2191. if (thread == NULL){
  2192. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2193. "unable to create map task thread!\n");
  2194. *returncode = LDAP_OPERATIONS_ERROR;
  2195. rv = SLAPI_DSE_CALLBACK_ERROR;
  2196. slapi_task_finish(task, *returncode);
  2197. } else {
  2198. rv = SLAPI_DSE_CALLBACK_OK;
  2199. }
  2200. out:
  2201. return rv;
  2202. }
  2203. /*
  2204. * Read in the text entries from ldif_in, and convert them to slapi_entries.
  2205. * Then, write to ldif_out what the updates would be if these entries were added
  2206. */
  2207. void automember_map_task_thread(void *arg){
  2208. Slapi_Task *task = (Slapi_Task *)arg;
  2209. Slapi_Entry *e = NULL;
  2210. int result = SLAPI_DSE_CALLBACK_OK;
  2211. struct configEntry *config = NULL;
  2212. PRCList *list = NULL;
  2213. task_data *td = NULL;
  2214. PRFileDesc *ldif_fd_out = NULL;
  2215. char *entrystr = NULL;
  2216. char *errstr = NULL;
  2217. #if defined(USE_OPENLDAP)
  2218. int buflen = 0;
  2219. LDIFFP *ldif_fd_in = NULL;
  2220. ldif_record_lineno_t lineno = 0;
  2221. #else
  2222. PRFileDesc *ldif_fd_in = NULL;
  2223. int lineno = 0;
  2224. #endif
  2225. int rc = 0;
  2226. td = (task_data *)slapi_task_get_data(task);
  2227. slapi_task_begin(task, 1);
  2228. slapi_task_log_notice(task, "Automember map task starting... Reading entries from (%s)"
  2229. " and writing the updates to (%s)",td->ldif_in, td->ldif_out);
  2230. slapi_task_log_status(task, "Automember map task starting... Reading entries from (%s)"
  2231. " and writing the updates to (%s)",td->ldif_in, td->ldif_out);
  2232. /* make sure we can open the ldif files */
  2233. if(( ldif_fd_out = PR_Open( td->ldif_out, PR_CREATE_FILE | PR_WRONLY, DEFAULT_FILE_MODE )) == NULL ){
  2234. rc = PR_GetOSError();
  2235. slapi_task_log_notice(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2236. td->ldif_out, rc, slapi_system_strerror(rc));
  2237. slapi_task_log_status(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2238. td->ldif_out, rc, slapi_system_strerror(rc));
  2239. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2240. "Could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2241. td->ldif_out, rc, slapi_system_strerror(rc) );
  2242. result = SLAPI_DSE_CALLBACK_ERROR;
  2243. goto out;
  2244. }
  2245. #if defined(USE_OPENLDAP)
  2246. if(( ldif_fd_in = ldif_open(td->ldif_in, "r")) == NULL ){
  2247. rc = errno;
  2248. errstr = strerror(rc);
  2249. #else
  2250. if(( ldif_fd_in = PR_Open( td->ldif_in, PR_RDONLY, DEFAULT_FILE_MODE )) == NULL ){
  2251. rc = PR_GetOSError();
  2252. errstr = slapi_system_strerror(rc);
  2253. #endif
  2254. slapi_task_log_notice(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2255. td->ldif_in, rc, errstr);
  2256. slapi_task_log_status(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2257. td->ldif_in, rc, errstr);
  2258. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2259. "Could not open ldif file \"%s\" for reading, error %d (%s)\n",
  2260. td->ldif_in, rc, errstr );
  2261. result = SLAPI_DSE_CALLBACK_ERROR;
  2262. goto out;
  2263. }
  2264. /*
  2265. * Convert each LDIF entry to a slapi_entry
  2266. */
  2267. automember_config_read_lock();
  2268. #if defined(USE_OPENLDAP)
  2269. while (ldif_read_record(ldif_fd_in, &lineno, &entrystr, &buflen)){
  2270. buflen = 0;
  2271. #else
  2272. while ((entrystr = ldif_get_entry(ldif_fd_in, &lineno)) != NULL){
  2273. #endif
  2274. e = slapi_str2entry( entrystr, 0 );
  2275. if ( e != NULL ){
  2276. if (!g_plugin_started) {
  2277. automember_config_unlock();
  2278. result = -1;
  2279. goto out;
  2280. }
  2281. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  2282. list = PR_LIST_HEAD(g_automember_config);
  2283. while (list != g_automember_config) {
  2284. config = (struct configEntry *)list;
  2285. if (slapi_dn_issuffix(slapi_entry_get_dn_const(e), config->scope) &&
  2286. (slapi_filter_test_simple(e, config->filter) == 0))
  2287. {
  2288. automember_update_membership(config, e, ldif_fd_out);
  2289. }
  2290. list = PR_NEXT_LINK(list);
  2291. }
  2292. }
  2293. slapi_entry_free(e);
  2294. } else {
  2295. /* invalid entry */
  2296. slapi_task_log_notice(task, "Automember map task, skipping invalid entry.");
  2297. slapi_task_log_status(task, "Automember map task, skipping invalid entry.");
  2298. }
  2299. slapi_ch_free((void **)&entrystr);
  2300. }
  2301. automember_config_unlock();
  2302. out:
  2303. if(ldif_fd_out){
  2304. PR_Close(ldif_fd_out);
  2305. }
  2306. if(ldif_fd_in){
  2307. #if defined(USE_OPENLDAP)
  2308. ldif_close(ldif_fd_in);
  2309. #else
  2310. PR_Close(ldif_fd_in);
  2311. #endif
  2312. }
  2313. slapi_task_inc_progress(task);
  2314. slapi_task_finish(task, result);
  2315. }
  2316. /*
  2317. * automember_modrdn_post_op()
  2318. *
  2319. * Reloads config if config entries move
  2320. * into or out of our config scope.
  2321. */
  2322. static int
  2323. automember_modrdn_post_op(Slapi_PBlock *pb)
  2324. {
  2325. Slapi_DN *old_sdn = NULL;
  2326. Slapi_DN *new_sdn = NULL;
  2327. Slapi_Entry *post_e = NULL;
  2328. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2329. "--> automember_modrdn_post_op\n");
  2330. /* Just bail if we aren't ready to service requests yet. */
  2331. if (!g_plugin_started || !automember_oktodo(pb))
  2332. return 0;
  2333. /* Reload config if an existing config entry was renamed,
  2334. * or if the new dn brings an entry into the scope of the
  2335. * config entries. */
  2336. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  2337. if (post_e) {
  2338. new_sdn = slapi_entry_get_sdn(post_e);
  2339. } else {
  2340. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2341. "automember_modrdn_post_op: Error "
  2342. "retrieving post-op entry\n");
  2343. return 0;
  2344. }
  2345. if ((old_sdn = automember_get_sdn(pb))) {
  2346. if (automember_dn_is_config(old_sdn) || automember_dn_is_config(new_sdn))
  2347. automember_load_config();
  2348. } else {
  2349. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2350. "automember_modrdn_post_op: Error "
  2351. "retrieving dn\n");
  2352. }
  2353. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2354. "<-- automember_modrdn_post_op\n");
  2355. return 0;
  2356. }