automember.c 96 KB

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