automember.c 93 KB

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