1
0

automember.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672
  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. int rc = 0;
  2041. td = (task_data *)slapi_task_get_data(task);
  2042. slapi_task_begin(task, 1);
  2043. slapi_task_log_notice(task, "Automember export task starting. Exporting changes to (%s)", td->ldif_out);
  2044. slapi_task_log_status(task, "Automember export task starting. Exporting changes to (%s)", td->ldif_out);
  2045. /* make sure we can open the ldif file */
  2046. if (( ldif_fd = PR_Open( td->ldif_out, PR_CREATE_FILE | PR_WRONLY, DEFAULT_FILE_MODE )) == NULL ){
  2047. rc = PR_GetOSError();
  2048. slapi_task_log_notice(task, "Automember export task could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2049. td->ldif_out, rc, slapi_system_strerror(rc));
  2050. slapi_task_log_status(task, "Automember export task could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2051. td->ldif_out, rc, slapi_system_strerror(rc) );
  2052. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2053. "Could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2054. td->ldif_out, rc, slapi_system_strerror(rc) );
  2055. result = SLAPI_DSE_CALLBACK_ERROR;
  2056. goto out;
  2057. }
  2058. /*
  2059. * Set the bind dn in the local thread data
  2060. */
  2061. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  2062. /*
  2063. * Search the database
  2064. */
  2065. search_pb = slapi_pblock_new();
  2066. slapi_search_internal_set_pb_ext(search_pb, td->base_dn, td->scope, td->filter_str, NULL,
  2067. 0, NULL, NULL, automember_get_plugin_id(), 0);
  2068. slapi_search_internal_pb(search_pb);
  2069. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2070. if (LDAP_SUCCESS != result){
  2071. slapi_task_log_notice(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_task_log_status(task, "Automember task failed to search on base (%s) filter (%s) error (%d)\n",
  2074. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2075. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2076. "Task: unable to search on base (%s) filter (%s) error (%d)\n",
  2077. slapi_sdn_get_dn(td->base_dn), td->filter_str, result);
  2078. goto out;
  2079. }
  2080. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2081. /*
  2082. * Grab the config read lock, and loop over the entries
  2083. */
  2084. automember_config_read_lock();
  2085. for (i = 0; entries && (entries[i] != NULL); i++){
  2086. /* make sure the plugin is still up, as this loop could run for awhile */
  2087. if (!g_plugin_started) {
  2088. automember_config_unlock();
  2089. result = -1;
  2090. goto out;
  2091. }
  2092. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  2093. list = PR_LIST_HEAD(g_automember_config);
  2094. while (list != g_automember_config) {
  2095. config = (struct configEntry *)list;
  2096. if (slapi_dn_issuffix(slapi_sdn_get_dn(td->base_dn), config->scope) &&
  2097. (slapi_filter_test_simple(entries[i], config->filter) == 0))
  2098. {
  2099. automember_update_membership(config, entries[i], ldif_fd);
  2100. }
  2101. list = PR_NEXT_LINK(list);
  2102. }
  2103. }
  2104. }
  2105. automember_config_unlock();
  2106. slapi_free_search_results_internal(search_pb);
  2107. out:
  2108. if(ldif_fd){
  2109. PR_Close(ldif_fd);
  2110. }
  2111. if(result){
  2112. /* error */
  2113. slapi_task_log_notice(task, "Automember export task aborted. Error (%d)", result);
  2114. slapi_task_log_status(task, "Automember export task aborted. Error (%d)", result);
  2115. } else {
  2116. slapi_task_log_notice(task, "Automember export task finished. Processed (%d) entries.", i);
  2117. slapi_task_log_status(task, "Automember export task finished. Processed (%d) entries.", i);
  2118. }
  2119. slapi_task_inc_progress(task);
  2120. slapi_task_finish(task, result);
  2121. }
  2122. /*
  2123. * Export an ldif of the changes that would be made from the entries
  2124. * in the provided ldif file
  2125. *
  2126. * task entry:
  2127. *
  2128. * dn: cn=my map task, cn=automember map updates,cn=tasks,cn=config
  2129. * objectClass: top
  2130. * objectClass: extensibleObject
  2131. * cn: my export task
  2132. * ldif_in: /tmp/entries.ldif
  2133. * ldif_out: /tmp/automem-updates.ldif
  2134. */
  2135. static int
  2136. automember_task_add_map_entries(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *eAfter,
  2137. int *returncode, char *returntext, void *arg)
  2138. {
  2139. int rv = SLAPI_DSE_CALLBACK_OK;
  2140. task_data *mytaskdata = NULL;
  2141. Slapi_Task *task = NULL;
  2142. PRThread *thread = NULL;
  2143. char *bind_dn;
  2144. const char *ldif_out;
  2145. const char *ldif_in;
  2146. *returncode = LDAP_SUCCESS;
  2147. /*
  2148. * Make sure the plugin is started
  2149. */
  2150. if(!g_plugin_started){
  2151. *returncode = LDAP_OPERATIONS_ERROR;
  2152. rv = SLAPI_DSE_CALLBACK_ERROR;
  2153. goto out;
  2154. }
  2155. /*
  2156. * Get the params
  2157. */
  2158. if((ldif_in = fetch_attr(e, "ldif_in", 0)) == NULL){
  2159. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2160. rv = SLAPI_DSE_CALLBACK_ERROR;
  2161. goto out;
  2162. }
  2163. if((ldif_out = fetch_attr(e, "ldif_out", 0)) == NULL){
  2164. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  2165. rv = SLAPI_DSE_CALLBACK_ERROR;
  2166. goto out;
  2167. }
  2168. /*
  2169. * Setup the task data
  2170. */
  2171. mytaskdata = (task_data*)slapi_ch_malloc(sizeof(task_data));
  2172. if (mytaskdata == NULL){
  2173. *returncode = LDAP_OPERATIONS_ERROR;
  2174. rv = SLAPI_DSE_CALLBACK_ERROR;
  2175. goto out;
  2176. }
  2177. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  2178. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  2179. mytaskdata->ldif_out = slapi_ch_strdup(ldif_out);
  2180. mytaskdata->ldif_in = slapi_ch_strdup(ldif_in);
  2181. task = slapi_new_task(slapi_entry_get_ndn(e));
  2182. slapi_task_set_destructor_fn(task, automember_task_map_destructor);
  2183. slapi_task_set_data(task, mytaskdata);
  2184. /*
  2185. * Start the task as a separate thread
  2186. */
  2187. thread = PR_CreateThread(PR_USER_THREAD, automember_map_task_thread,
  2188. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2189. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2190. if (thread == NULL){
  2191. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2192. "unable to create map task thread!\n");
  2193. *returncode = LDAP_OPERATIONS_ERROR;
  2194. rv = SLAPI_DSE_CALLBACK_ERROR;
  2195. slapi_task_finish(task, *returncode);
  2196. } else {
  2197. rv = SLAPI_DSE_CALLBACK_OK;
  2198. }
  2199. out:
  2200. return rv;
  2201. }
  2202. /*
  2203. * Read in the text entries from ldif_in, and convert them to slapi_entries.
  2204. * Then, write to ldif_out what the updates would be if these entries were added
  2205. */
  2206. void automember_map_task_thread(void *arg){
  2207. Slapi_Task *task = (Slapi_Task *)arg;
  2208. Slapi_Entry *e = NULL;
  2209. int result = SLAPI_DSE_CALLBACK_OK;
  2210. struct configEntry *config = NULL;
  2211. PRCList *list = NULL;
  2212. task_data *td = NULL;
  2213. PRFileDesc *ldif_fd_out = NULL;
  2214. char *entrystr = NULL;
  2215. char *errstr = NULL;
  2216. #if defined(USE_OPENLDAP)
  2217. int buflen = 0;
  2218. LDIFFP *ldif_fd_in = NULL;
  2219. #else
  2220. PRFileDesc *ldif_fd_in = NULL;
  2221. #endif
  2222. int lineno = 0;
  2223. int rc = 0;
  2224. td = (task_data *)slapi_task_get_data(task);
  2225. slapi_task_begin(task, 1);
  2226. slapi_task_log_notice(task, "Automember map task starting... Reading entries from (%s)"
  2227. " and writing the updates to (%s)",td->ldif_in, td->ldif_out);
  2228. slapi_task_log_status(task, "Automember map task starting... Reading entries from (%s)"
  2229. " and writing the updates to (%s)",td->ldif_in, td->ldif_out);
  2230. /* make sure we can open the ldif files */
  2231. if(( ldif_fd_out = PR_Open( td->ldif_out, PR_CREATE_FILE | PR_WRONLY, DEFAULT_FILE_MODE )) == NULL ){
  2232. rc = PR_GetOSError();
  2233. slapi_task_log_notice(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2234. td->ldif_out, rc, slapi_system_strerror(rc));
  2235. slapi_task_log_status(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2236. td->ldif_out, rc, slapi_system_strerror(rc));
  2237. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2238. "Could not open ldif file \"%s\" for writing, error %d (%s)\n",
  2239. td->ldif_out, rc, slapi_system_strerror(rc) );
  2240. result = SLAPI_DSE_CALLBACK_ERROR;
  2241. goto out;
  2242. }
  2243. #if defined(USE_OPENLDAP)
  2244. if(( ldif_fd_in = ldif_open(td->ldif_in, "r")) == NULL ){
  2245. rc = errno;
  2246. errstr = strerror(rc);
  2247. #else
  2248. if(( ldif_fd_in = PR_Open( td->ldif_in, PR_RDONLY, DEFAULT_FILE_MODE )) == NULL ){
  2249. rc = PR_GetOSError();
  2250. errstr = slapi_system_strerror(rc);
  2251. #endif
  2252. slapi_task_log_notice(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2253. td->ldif_in, rc, errstr);
  2254. slapi_task_log_status(task, "The ldif file %s could not be accessed, error %d (%s). Aborting task.\n",
  2255. td->ldif_in, rc, errstr);
  2256. slapi_log_error( SLAPI_LOG_FATAL, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2257. "Could not open ldif file \"%s\" for reading, error %d (%s)\n",
  2258. td->ldif_in, rc, errstr );
  2259. result = SLAPI_DSE_CALLBACK_ERROR;
  2260. goto out;
  2261. }
  2262. /*
  2263. * Convert each LDIF entry to a slapi_entry
  2264. */
  2265. automember_config_read_lock();
  2266. #if defined(USE_OPENLDAP)
  2267. while (ldif_read_record(ldif_fd_in, &lineno, &entrystr, &buflen)){
  2268. buflen = 0;
  2269. #else
  2270. while ((entrystr = ldif_get_entry(ldif_fd_in, &lineno)) != NULL){
  2271. #endif
  2272. e = slapi_str2entry( entrystr, 0 );
  2273. if ( e != NULL ){
  2274. if (!g_plugin_started) {
  2275. automember_config_unlock();
  2276. result = -1;
  2277. goto out;
  2278. }
  2279. if (!PR_CLIST_IS_EMPTY(g_automember_config)) {
  2280. list = PR_LIST_HEAD(g_automember_config);
  2281. while (list != g_automember_config) {
  2282. config = (struct configEntry *)list;
  2283. if (slapi_dn_issuffix(slapi_entry_get_dn_const(e), config->scope) &&
  2284. (slapi_filter_test_simple(e, config->filter) == 0))
  2285. {
  2286. automember_update_membership(config, e, ldif_fd_out);
  2287. }
  2288. list = PR_NEXT_LINK(list);
  2289. }
  2290. }
  2291. slapi_entry_free(e);
  2292. } else {
  2293. /* invalid entry */
  2294. slapi_task_log_notice(task, "Automember map task, skipping invalid entry.");
  2295. slapi_task_log_status(task, "Automember map task, skipping invalid entry.");
  2296. }
  2297. slapi_ch_free((void **)&entrystr);
  2298. }
  2299. automember_config_unlock();
  2300. out:
  2301. if(ldif_fd_out){
  2302. PR_Close(ldif_fd_out);
  2303. }
  2304. if(ldif_fd_in){
  2305. #if defined(USE_OPENLDAP)
  2306. ldif_close(ldif_fd_in);
  2307. #else
  2308. PR_Close(ldif_fd_in);
  2309. #endif
  2310. }
  2311. slapi_task_inc_progress(task);
  2312. slapi_task_finish(task, result);
  2313. }
  2314. /*
  2315. * automember_modrdn_post_op()
  2316. *
  2317. * Reloads config if config entries move
  2318. * into or out of our config scope.
  2319. */
  2320. static int
  2321. automember_modrdn_post_op(Slapi_PBlock *pb)
  2322. {
  2323. Slapi_DN *old_sdn = NULL;
  2324. Slapi_DN *new_sdn = NULL;
  2325. Slapi_Entry *post_e = NULL;
  2326. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2327. "--> automember_modrdn_post_op\n");
  2328. /* Just bail if we aren't ready to service requests yet. */
  2329. if (!g_plugin_started || !automember_oktodo(pb))
  2330. return 0;
  2331. /* Reload config if an existing config entry was renamed,
  2332. * or if the new dn brings an entry into the scope of the
  2333. * config entries. */
  2334. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  2335. if (post_e) {
  2336. new_sdn = slapi_entry_get_sdn(post_e);
  2337. } else {
  2338. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2339. "automember_modrdn_post_op: Error "
  2340. "retrieving post-op entry\n");
  2341. return 0;
  2342. }
  2343. if ((old_sdn = automember_get_sdn(pb))) {
  2344. if (automember_dn_is_config(old_sdn) || automember_dn_is_config(new_sdn))
  2345. automember_load_config();
  2346. } else {
  2347. slapi_log_error(SLAPI_LOG_PLUGIN, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2348. "automember_modrdn_post_op: Error "
  2349. "retrieving dn\n");
  2350. }
  2351. slapi_log_error(SLAPI_LOG_TRACE, AUTOMEMBER_PLUGIN_SUBSYSTEM,
  2352. "<-- automember_modrdn_post_op\n");
  2353. return 0;
  2354. }