referint.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "portable.h"
  15. #include "slapi-plugin.h"
  16. #include "slap.h"
  17. #include "prthread.h"
  18. #include "prlock.h"
  19. #include "prerror.h"
  20. #include "prcvar.h"
  21. #include "prio.h"
  22. #include <sys/stat.h>
  23. #define REFERINT_DEFAULT_FILE_MODE S_IRUSR | S_IWUSR
  24. #define REFERINT_PLUGIN_SUBSYSTEM "referint-plugin" /* used for logging */
  25. #define REFERINT_PREOP_DESC "referint preop plugin"
  26. #define REFERINT_ATTR_DELAY "referint-update-delay"
  27. #define REFERINT_ATTR_LOGCHANGES "referint-logchanges"
  28. #define REFERINT_ATTR_LOGFILE "referint-logfile"
  29. #define REFERINT_ATTR_MEMBERSHIP "referint-membership-attr"
  30. #define MAX_LINE 2048
  31. #define READ_BUFSIZE 4096
  32. #define MY_EOF 0
  33. #define STARTUP 2
  34. typedef struct referint_config {
  35. int delay;
  36. char *logfile;
  37. int logchanges;
  38. char **attrs;
  39. } referint_config;
  40. Slapi_RWLock *config_rwlock = NULL;
  41. /* function prototypes */
  42. int referint_postop_init( Slapi_PBlock *pb );
  43. int referint_postop_del( Slapi_PBlock *pb );
  44. int referint_postop_modrdn( Slapi_PBlock *pb );
  45. int referint_postop_start( Slapi_PBlock *pb);
  46. int referint_postop_close( Slapi_PBlock *pb);
  47. int update_integrity(Slapi_DN *sDN, char *newrDN, Slapi_DN *newsuperior, int logChanges);
  48. int GetNextLine(char *dest, int size_dest, PRFileDesc *stream);
  49. int my_fgetc(PRFileDesc *stream);
  50. void referint_thread_func(void *arg);
  51. void writeintegritylog(Slapi_PBlock *pb, char *logfilename, Slapi_DN *sdn, char *newrdn, Slapi_DN *newsuperior, Slapi_DN *requestorsdn);
  52. int load_config(Slapi_PBlock *pb, Slapi_Entry *config_entry, int apply);
  53. int referint_get_delay(void);
  54. int referint_get_logchanges(void);
  55. char *referint_get_logfile(void);
  56. char **referint_get_attrs(void);
  57. int referint_postop_modify(Slapi_PBlock *pb);
  58. int referint_validate_config(Slapi_PBlock *pb);
  59. static int referint_preop_init(Slapi_PBlock *pb);
  60. void referint_set_config_area(Slapi_DN *dn);
  61. Slapi_DN *referint_get_config_area(void);
  62. void referint_set_plugin_area(Slapi_DN *sdn);
  63. Slapi_DN *referint_get_plugin_area(void);
  64. int referint_sdn_config_cmp(Slapi_DN *sdn);
  65. void referint_get_config(int *delay, int *logchanges, char **logfile);
  66. /* global thread control stuff */
  67. static PRLock *referint_mutex = NULL;
  68. static PRThread *referint_tid = NULL;
  69. static PRLock *keeprunning_mutex = NULL;
  70. static PRCondVar *keeprunning_cv = NULL;
  71. static int keeprunning = 0;
  72. static referint_config *config = NULL;
  73. static Slapi_DN* _ConfigAreaDN = NULL;
  74. static Slapi_DN* _pluginDN = NULL;
  75. static Slapi_PluginDesc pdesc = { "referint", VENDOR, DS_PACKAGE_VERSION, "referential integrity plugin" };
  76. static int allow_repl = 0;
  77. static Slapi_DN **plugin_EntryScope = NULL;
  78. static Slapi_DN *plugin_ExcludeEntryScope = NULL;
  79. static Slapi_DN *plugin_ContainerScope = NULL;
  80. static void* referint_plugin_identity = NULL;
  81. static int use_txn = 0;
  82. static int premodfn = SLAPI_PLUGIN_PRE_MODIFY_FN;
  83. static void
  84. referint_lock(void)
  85. {
  86. if (use_txn) { /* no lock if betxn is enabled */
  87. return;
  88. }
  89. if (NULL == referint_mutex) {
  90. referint_mutex = PR_NewLock();
  91. }
  92. if (referint_mutex) {
  93. PR_Lock(referint_mutex);
  94. }
  95. }
  96. static void
  97. referint_unlock(void)
  98. {
  99. if (use_txn) { /* no lock if betxn is enabled */
  100. return;
  101. }
  102. if (referint_mutex) {
  103. PR_Unlock(referint_mutex);
  104. }
  105. }
  106. void
  107. referint_set_config_area(Slapi_DN *dn)
  108. {
  109. slapi_rwlock_wrlock(config_rwlock);
  110. slapi_sdn_free(&_ConfigAreaDN);
  111. _ConfigAreaDN = slapi_sdn_dup(dn);
  112. slapi_rwlock_unlock(config_rwlock);
  113. }
  114. /*
  115. * No need to lock here, because this only called from referint_sdn_config_cmp()
  116. * which does take the lock.
  117. */
  118. Slapi_DN *
  119. referint_get_config_area(void)
  120. {
  121. return _ConfigAreaDN;
  122. }
  123. /* no locking needed for the plugin DN because it is set at initialization */
  124. void
  125. referint_set_plugin_area(Slapi_DN *sdn)
  126. {
  127. slapi_sdn_free(&_pluginDN);
  128. _pluginDN = slapi_sdn_dup(sdn);
  129. }
  130. Slapi_DN *
  131. referint_get_plugin_area(void)
  132. {
  133. return _pluginDN;
  134. }
  135. int
  136. referint_postop_init( Slapi_PBlock *pb )
  137. {
  138. Slapi_Entry *plugin_entry = NULL;
  139. char *plugin_type = NULL;
  140. int delfn = SLAPI_PLUGIN_POST_DELETE_FN;
  141. int mdnfn = SLAPI_PLUGIN_POST_MODRDN_FN;
  142. int modfn = SLAPI_PLUGIN_POST_MODIFY_FN; /* for config changes */
  143. char *preop_plugin_type = "preoperation";
  144. /*
  145. * Get plugin identity and stored it for later use.
  146. * Used for internal operations.
  147. */
  148. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &referint_plugin_identity);
  149. PR_ASSERT (referint_plugin_identity);
  150. if((config = (referint_config *)slapi_ch_calloc (1, sizeof (referint_config))) == NULL){
  151. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_init - Failed to "
  152. "allocate configuration\n" );
  153. return ( -1 );
  154. }
  155. /* get the args */
  156. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  157. plugin_entry &&
  158. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  159. plugin_type && strstr(plugin_type, "betxn"))
  160. {
  161. delfn = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
  162. mdnfn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
  163. modfn = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
  164. preop_plugin_type = "betxnpreoperation";
  165. premodfn = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  166. use_txn = 1;
  167. }
  168. slapi_ch_free_string(&plugin_type);
  169. if(plugin_entry){
  170. char *plugin_attr_value;
  171. char **plugin_attr_values;
  172. plugin_attr_value = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-pluginAllowReplUpdates");
  173. if(plugin_attr_value && strcasecmp(plugin_attr_value,"on")==0){
  174. allow_repl = 1;
  175. }
  176. slapi_ch_free_string(&plugin_attr_value);
  177. plugin_attr_values = slapi_entry_attr_get_charray(plugin_entry, "nsslapd-pluginEntryScope");
  178. if(plugin_attr_values) {
  179. int i,j=0;;
  180. for (i=0; plugin_attr_values[i];i++);
  181. plugin_EntryScope = (Slapi_DN **)slapi_ch_calloc(sizeof(Slapi_DN *),i+1);
  182. for (i=0; plugin_attr_values[i];i++) {
  183. if (slapi_dn_syntax_check(NULL, plugin_attr_values[i], 1) == 1) {
  184. slapi_log_error(SLAPI_LOG_WARNING, REFERINT_PLUGIN_SUBSYSTEM,
  185. "referint_postop_init - Ignoring invalid DN used as plugin entry scope: [%s]\n",
  186. plugin_attr_values[i]);
  187. slapi_ch_free_string(&plugin_attr_values[i]);
  188. } else {
  189. plugin_EntryScope[j++] = slapi_sdn_new_dn_passin(plugin_attr_values[i]);
  190. }
  191. }
  192. slapi_ch_free((void**)&plugin_attr_values);
  193. }
  194. plugin_attr_value = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-pluginExcludeEntryScope");
  195. if(plugin_attr_value) {
  196. if (slapi_dn_syntax_check(NULL, plugin_attr_value, 1) == 1) {
  197. slapi_log_error(SLAPI_LOG_WARNING, REFERINT_PLUGIN_SUBSYSTEM,
  198. "referint_postop_init - Ignoring invalid DN used as plugin exclude entry scope: [%s]\n",
  199. plugin_attr_value);
  200. slapi_ch_free_string(&plugin_attr_value);
  201. } else {
  202. plugin_ExcludeEntryScope = slapi_sdn_new_dn_passin(plugin_attr_value);
  203. }
  204. }
  205. plugin_attr_value = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-pluginContainerScope");
  206. if(plugin_attr_value) {
  207. if (slapi_dn_syntax_check(NULL, plugin_attr_value, 1) == 1) {
  208. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  209. "referint_postop_init - Ignoring invalid DN used as plugin container scope: [%s]\n",
  210. plugin_attr_value);
  211. slapi_ch_free_string(&plugin_attr_value);
  212. } else {
  213. plugin_ContainerScope = slapi_sdn_new_dn_passin(plugin_attr_value);
  214. }
  215. }
  216. referint_set_plugin_area(slapi_entry_get_sdn(plugin_entry));
  217. }
  218. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  219. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, (void *)&pdesc ) != 0 ||
  220. slapi_pblock_set( pb, delfn, (void *) referint_postop_del ) != 0 ||
  221. slapi_pblock_set( pb, mdnfn, (void *) referint_postop_modrdn ) != 0 ||
  222. slapi_pblock_set( pb, modfn, (void *) (void *)referint_postop_modify ) != 0 ||
  223. slapi_pblock_set( pb, SLAPI_PLUGIN_START_FN, (void *) referint_postop_start ) != 0 ||
  224. slapi_pblock_set( pb, SLAPI_PLUGIN_CLOSE_FN, (void *) referint_postop_close ) != 0)
  225. {
  226. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_init - Failed\n" );
  227. return( -1 );
  228. }
  229. /*
  230. * Setup the preop plugin for config validation
  231. */
  232. if (slapi_register_plugin(preop_plugin_type, /* op type */
  233. 1, /* Enabled */
  234. "referint_preop_init", /* this function desc */
  235. referint_preop_init, /* init func */
  236. REFERINT_PREOP_DESC, /* plugin desc */
  237. NULL, /* ? */
  238. referint_plugin_identity /* access control */))
  239. {
  240. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_init - Preop registration failed\n" );
  241. return ( -1 );
  242. }
  243. return( 0 );
  244. }
  245. /*
  246. * referint-update-delay: 0
  247. * referint-logfile: /var/log/dirsrv/slapd-localhost/referint
  248. * referint-logchanges: 0
  249. * referint-membership-attr: member
  250. * referint-membership-attr: uniquemember
  251. * referint-membership-attr: owner
  252. * referint-membership-attr: seeAlso
  253. *
  254. *
  255. * Need to lock this!
  256. */
  257. int
  258. load_config(Slapi_PBlock *pb, Slapi_Entry *config_entry, int apply)
  259. {
  260. referint_config *tmp_config = NULL;
  261. char *value = NULL;
  262. char **attrs = NULL;
  263. char **argv = NULL;
  264. int new_config_present = 0;
  265. int argc = 0;
  266. int rc = SLAPI_PLUGIN_SUCCESS;
  267. if(config_entry == NULL){
  268. return rc;
  269. }
  270. slapi_rwlock_wrlock(config_rwlock);
  271. if(config == NULL){
  272. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  273. "load_config: config is NULL\n" );
  274. rc = SLAPI_PLUGIN_FAILURE;
  275. goto done;
  276. }
  277. if((tmp_config = (referint_config *)slapi_ch_calloc (1, sizeof (referint_config))) == NULL){
  278. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "load_config - Failed to "
  279. "allocate configuration\n" );
  280. rc = SLAPI_PLUGIN_FAILURE;
  281. goto done;
  282. } else {
  283. /* set these to -1 for config validation */
  284. tmp_config->delay = -1;
  285. tmp_config->logchanges = -1;
  286. }
  287. if((value = slapi_entry_attr_get_charptr(config_entry, REFERINT_ATTR_DELAY))){
  288. tmp_config->delay = atoi(value);
  289. slapi_ch_free_string(&value);
  290. new_config_present = 1;
  291. }
  292. if((value = slapi_entry_attr_get_charptr(config_entry, REFERINT_ATTR_LOGFILE))){
  293. tmp_config->logfile = value;
  294. new_config_present = 1;
  295. }
  296. if((value = slapi_entry_attr_get_charptr(config_entry, REFERINT_ATTR_LOGCHANGES))){
  297. tmp_config->logchanges = atoi(value);
  298. slapi_ch_free_string(&value);
  299. new_config_present = 1;
  300. }
  301. if((attrs = slapi_entry_attr_get_charray(config_entry, REFERINT_ATTR_MEMBERSHIP))){
  302. tmp_config->attrs = attrs;
  303. new_config_present = 1;
  304. }
  305. if(new_config_present){
  306. /* Verify we have everything we need */
  307. if(tmp_config->delay == -1){
  308. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "load_config - Plugin configuration is missing %s\n",
  309. REFERINT_ATTR_DELAY);
  310. rc = SLAPI_PLUGIN_FAILURE;
  311. } else if (!tmp_config->logfile){
  312. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "load_config - Plugin configuration is missing %s\n",
  313. REFERINT_ATTR_LOGFILE);
  314. rc = SLAPI_PLUGIN_FAILURE;
  315. } else if (tmp_config->logchanges == -1){
  316. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "load_config - Plugin configuration is missing %s\n",
  317. REFERINT_ATTR_LOGCHANGES);
  318. rc = SLAPI_PLUGIN_FAILURE;
  319. } else if (!tmp_config->attrs){
  320. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "load_config - Plugin configuration is missing %s\n",
  321. REFERINT_ATTR_MEMBERSHIP);
  322. rc = SLAPI_PLUGIN_FAILURE;
  323. }
  324. } else{
  325. /*
  326. * We are using the old plugin arg configuration, get the args
  327. */
  328. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGC, &argc ) != 0) {
  329. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  330. "load_config - Failed to get argc\n" );
  331. rc = SLAPI_PLUGIN_FAILURE;
  332. goto done;
  333. }
  334. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGV, &argv ) != 0) {
  335. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  336. "load_config - Failed to get argv\n" );
  337. rc = SLAPI_PLUGIN_FAILURE;
  338. goto done;
  339. }
  340. if(argv == NULL){
  341. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  342. "load_config - args are NULL\n" );
  343. rc = SLAPI_PLUGIN_FAILURE;
  344. goto done;
  345. }
  346. /*
  347. * Load the args and set the config struct
  348. */
  349. if (argc >= 3) {
  350. int i;
  351. tmp_config->delay = atoi(argv[0]);
  352. tmp_config->logfile = slapi_ch_strdup(argv[1]);
  353. tmp_config->logchanges = atoi(argv[2]);
  354. for(i = 3; argv[i] != NULL; i++){
  355. slapi_ch_array_add(&tmp_config->attrs, slapi_ch_strdup(argv[i]));
  356. }
  357. } else {
  358. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  359. "load_config - Insufficient arguments supplied\n" );
  360. rc = SLAPI_PLUGIN_FAILURE;
  361. goto done;
  362. }
  363. }
  364. done:
  365. if(apply && rc == SLAPI_PLUGIN_SUCCESS){
  366. slapi_ch_free_string(&config->logfile);
  367. slapi_ch_array_free(config->attrs);
  368. slapi_ch_free((void **)&config);
  369. config = tmp_config;
  370. } else if(tmp_config){
  371. slapi_ch_free_string(&tmp_config->logfile);
  372. slapi_ch_array_free(tmp_config->attrs);
  373. slapi_ch_free((void **)&tmp_config);
  374. }
  375. slapi_rwlock_unlock(config_rwlock);
  376. return rc;
  377. }
  378. int
  379. referint_postop_modify(Slapi_PBlock *pb)
  380. {
  381. Slapi_Entry *entry = NULL, *e = NULL;
  382. Slapi_Entry *config_e = NULL;
  383. Slapi_DN *config_sdn = NULL;
  384. Slapi_DN *sdn = NULL;
  385. char *config_area = NULL;
  386. int result = 0;
  387. int rc = SLAPI_PLUGIN_SUCCESS;
  388. /* check if we are updating the shared config entry */
  389. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  390. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &entry);
  391. if (referint_sdn_config_cmp(sdn) == 0 && slapi_sdn_compare(sdn, referint_get_plugin_area()))
  392. {
  393. if( SLAPI_PLUGIN_FAILURE == load_config(pb, entry, 1)){
  394. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_modify - "
  395. "Failed to update configuration.\n");
  396. return SLAPI_PLUGIN_FAILURE;
  397. }
  398. } else if (slapi_sdn_compare(sdn, referint_get_plugin_area()) == 0){
  399. /*
  400. * Check if the plugin config area is set(verify it and load its config),
  401. * otherwise reload the plugin entry config
  402. */
  403. if((config_area = slapi_entry_attr_get_charptr(entry, SLAPI_PLUGIN_SHARED_CONFIG_AREA))){
  404. rc = slapi_dn_syntax_check(pb, config_area, 1);
  405. if (rc) { /* syntax check failed */
  406. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_modify - "
  407. "%s does not contain a valid DN (%s)\n",
  408. SLAPI_PLUGIN_SHARED_CONFIG_AREA, config_area);
  409. rc = LDAP_INVALID_DN_SYNTAX;
  410. goto bail;
  411. }
  412. config_sdn = slapi_sdn_new_dn_byval(config_area);
  413. result = slapi_search_internal_get_entry(config_sdn, NULL, &e, referint_plugin_identity);
  414. if (LDAP_SUCCESS != result) {
  415. if (result == LDAP_NO_SUCH_OBJECT) {
  416. /* log an error and use the plugin entry for the config */
  417. slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
  418. "referint_postop_modify - Config entry \"%s\" does "
  419. "not exist.\n", config_area);
  420. rc = LDAP_OPERATIONS_ERROR;
  421. goto bail;
  422. }
  423. } else {
  424. if(e){
  425. config_e = e;
  426. } else {
  427. slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
  428. "referint_postop_modify - Config entry \"%s\" was not located.\n", config_area);
  429. rc = LDAP_OPERATIONS_ERROR;
  430. goto bail;
  431. }
  432. }
  433. } else {
  434. config_e = entry;
  435. }
  436. if(load_config(pb, config_e, 1) != LDAP_SUCCESS){
  437. rc = LDAP_UNWILLING_TO_PERFORM;
  438. goto bail;
  439. }
  440. referint_set_config_area(slapi_entry_get_sdn(config_e));
  441. }
  442. bail:
  443. slapi_ch_free_string(&config_area);
  444. slapi_sdn_free(&config_sdn);
  445. slapi_entry_free(e);
  446. return rc;
  447. }
  448. int
  449. referint_get_delay(void)
  450. {
  451. int delay;
  452. slapi_rwlock_rdlock(config_rwlock);
  453. delay = config->delay;
  454. slapi_rwlock_unlock(config_rwlock);
  455. return delay;
  456. }
  457. int
  458. referint_get_logchanges(void)
  459. {
  460. int log_changes;
  461. slapi_rwlock_rdlock(config_rwlock);
  462. log_changes = config->logchanges;
  463. slapi_rwlock_unlock(config_rwlock);
  464. return log_changes;
  465. }
  466. char *
  467. referint_get_logfile(void)
  468. {
  469. char *log_file;
  470. slapi_rwlock_rdlock(config_rwlock);
  471. log_file = slapi_ch_strdup(config->logfile);
  472. slapi_rwlock_unlock(config_rwlock);
  473. return log_file;
  474. }
  475. void
  476. referint_get_config(int *delay, int *logchanges, char **logfile)
  477. {
  478. slapi_rwlock_rdlock(config_rwlock);
  479. if(delay){
  480. *delay = config->delay;
  481. }
  482. if(logchanges){
  483. *logchanges = config->logchanges;
  484. }
  485. if(logfile){
  486. *logfile = slapi_ch_strdup(config->logfile);
  487. }
  488. slapi_rwlock_unlock(config_rwlock);
  489. }
  490. /*
  491. * might need to find an alternate option instead of copying
  492. */
  493. char **
  494. referint_get_attrs(void)
  495. {
  496. char **attrs = NULL;
  497. slapi_rwlock_rdlock(config_rwlock);
  498. attrs = slapi_ch_array_dup(config->attrs);
  499. slapi_rwlock_unlock(config_rwlock);
  500. return attrs;
  501. }
  502. int
  503. referint_sdn_config_cmp(Slapi_DN *sdn)
  504. {
  505. int rc = 0;
  506. slapi_rwlock_rdlock(config_rwlock);
  507. rc = slapi_sdn_compare(sdn, referint_get_config_area());
  508. slapi_rwlock_unlock(config_rwlock);
  509. return rc;
  510. }
  511. int
  512. referint_sdn_in_container_scope(Slapi_DN *sdn)
  513. {
  514. if (plugin_ContainerScope == NULL) {
  515. return(1);
  516. } else {
  517. return(slapi_sdn_issuffix(sdn, plugin_ContainerScope));
  518. }
  519. }
  520. int
  521. referint_sdn_in_entry_scope(Slapi_DN *sdn)
  522. {
  523. int rc = 0;
  524. if (plugin_ExcludeEntryScope && slapi_sdn_issuffix(sdn, plugin_ExcludeEntryScope))
  525. return (0);
  526. if (plugin_EntryScope == NULL) {
  527. /* no scope defined, all sdns match */
  528. return(1);
  529. } else {
  530. int i = 0;
  531. while (plugin_EntryScope[i]) {
  532. if (slapi_sdn_issuffix(sdn, plugin_EntryScope[i]) ) {
  533. rc = 1;
  534. break;
  535. } else {
  536. i++;
  537. }
  538. }
  539. }
  540. return (rc);
  541. }
  542. int
  543. referint_postop_del( Slapi_PBlock *pb )
  544. {
  545. Slapi_DN *sdn = NULL;
  546. char *logfile = NULL;
  547. int delay;
  548. int logChanges=0;
  549. int isrepop = 0;
  550. int oprc;
  551. int rc = SLAPI_PLUGIN_SUCCESS;
  552. if ( slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &isrepop ) != 0 ||
  553. slapi_pblock_get( pb, SLAPI_DELETE_TARGET_SDN, &sdn ) != 0 ||
  554. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0)
  555. {
  556. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  557. "referint_postop_del - Could not get parameters\n" );
  558. return SLAPI_PLUGIN_FAILURE;
  559. }
  560. /*
  561. * This plugin should only execute if the delete was successful
  562. * and this is not a replicated op(unless its allowed)
  563. */
  564. if(oprc != 0 || (isrepop && !allow_repl)){
  565. return SLAPI_PLUGIN_SUCCESS;
  566. }
  567. referint_get_config(&delay, &logChanges, NULL);
  568. if(delay == -1){
  569. /* integrity updating is off */
  570. rc = SLAPI_PLUGIN_SUCCESS;
  571. } else if(delay == 0){ /* no delay */
  572. /* call function to update references to entry */
  573. if (referint_sdn_in_entry_scope(sdn)) {
  574. rc = update_integrity(sdn, NULL, NULL, logChanges);
  575. }
  576. } else {
  577. /* write the entry to integrity log */
  578. logfile = referint_get_logfile();
  579. writeintegritylog(pb, logfile, sdn, NULL, NULL, NULL /* slapi_get_requestor_sdn(pb) */);
  580. rc = SLAPI_PLUGIN_SUCCESS;
  581. }
  582. slapi_ch_free_string(&logfile);
  583. return( rc );
  584. }
  585. int
  586. referint_postop_modrdn( Slapi_PBlock *pb )
  587. {
  588. Slapi_DN *sdn = NULL;
  589. Slapi_DN *newsuperior;
  590. char *logfile = NULL;
  591. char *newrdn;
  592. int oprc;
  593. int rc = SLAPI_PLUGIN_SUCCESS;
  594. int delay;
  595. int logChanges=0;
  596. int isrepop = 0;
  597. if ( slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &isrepop ) != 0 ||
  598. slapi_pblock_get( pb, SLAPI_MODRDN_TARGET_SDN, &sdn ) != 0 ||
  599. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn ) != 0 ||
  600. slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &newsuperior ) != 0 ||
  601. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0 )
  602. {
  603. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  604. "referint_postop_modrdn - Could not get parameters\n" );
  605. return SLAPI_PLUGIN_FAILURE;
  606. }
  607. /*
  608. * This plugin should only execute if the delete was successful
  609. * and this is not a replicated op (unless its allowed)
  610. */
  611. if(oprc != 0 || (isrepop && !allow_repl)){
  612. return SLAPI_PLUGIN_SUCCESS;
  613. }
  614. referint_get_config(&delay, &logChanges, NULL);
  615. if(delay == -1){
  616. /* integrity updating is off */
  617. rc = SLAPI_PLUGIN_SUCCESS;
  618. } else if(delay == 0){ /* no delay */
  619. /* call function to update references to entry */
  620. if (!plugin_EntryScope && !plugin_ExcludeEntryScope) {
  621. /* no scope defined, default always process referint */
  622. rc = update_integrity(sdn, newrdn, newsuperior, logChanges);
  623. } else {
  624. const char *newsuperiordn = slapi_sdn_get_dn(newsuperior);
  625. if ( (newsuperiordn == NULL && referint_sdn_in_entry_scope(sdn)) ||
  626. ( newsuperiordn && referint_sdn_in_entry_scope(newsuperior)))
  627. {
  628. /*
  629. * It is a modrdn inside the scope or into the scope,
  630. * process normal modrdn
  631. */
  632. rc = update_integrity(sdn, newrdn, newsuperior, logChanges);
  633. } else if (referint_sdn_in_entry_scope(sdn)) {
  634. /* the entry is moved out of scope, treat as delete */
  635. rc = update_integrity(sdn, NULL, NULL, logChanges);
  636. }
  637. }
  638. } else {
  639. /* write the entry to integrity log */
  640. logfile = referint_get_logfile();
  641. writeintegritylog(pb, logfile, sdn, newrdn, newsuperior, NULL /* slapi_get_requestor_sdn(pb) */);
  642. rc = SLAPI_PLUGIN_SUCCESS;
  643. }
  644. slapi_ch_free_string(&logfile);
  645. return( rc );
  646. }
  647. int isFatalSearchError(int search_result)
  648. {
  649. /* Make sure search result is fatal
  650. * Some conditions that happen quite often are not fatal
  651. * for example if you have two suffixes and one is null, you will always
  652. * get no such object, however this is not a fatal error.
  653. * Add other conditions to the if statement as they are found
  654. */
  655. switch(search_result) {
  656. case LDAP_REFERRAL:
  657. case LDAP_NO_SUCH_OBJECT: return 0 ;
  658. }
  659. return 1;
  660. }
  661. static int
  662. _do_modify(Slapi_PBlock *mod_pb, Slapi_DN *entrySDN, LDAPMod **mods)
  663. {
  664. int rc = 0;
  665. slapi_pblock_init(mod_pb);
  666. if(allow_repl){
  667. /* Must set as a replicated operation */
  668. slapi_modify_internal_set_pb_ext(mod_pb, entrySDN, mods, NULL, NULL,
  669. referint_plugin_identity, OP_FLAG_REPLICATED);
  670. } else {
  671. slapi_modify_internal_set_pb_ext(mod_pb, entrySDN, mods, NULL, NULL,
  672. referint_plugin_identity, 0);
  673. }
  674. slapi_modify_internal_pb(mod_pb);
  675. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  676. return rc;
  677. }
  678. /*
  679. * update one attribute value per _do_modify
  680. */
  681. static int
  682. _update_one_per_mod(Slapi_DN *entrySDN, /* DN of the searched entry */
  683. Slapi_Attr *attr, /* referred attribute */
  684. char *attrName,
  685. Slapi_DN *origDN, /* original DN that was modified */
  686. char *newRDN, /* new RDN from modrdn */
  687. const char *newsuperior, /* new superior from modrdn */
  688. Slapi_PBlock *mod_pb)
  689. {
  690. LDAPMod attribute1, attribute2;
  691. LDAPMod *list_of_mods[3];
  692. char *values_del[2];
  693. char *values_add[2];
  694. char *newDN = NULL;
  695. char **dnParts = NULL;
  696. char *sval = NULL;
  697. char *newvalue = NULL;
  698. char *p = NULL;
  699. size_t dnlen = 0;
  700. int rc = 0;
  701. if (NULL == newRDN && NULL == newsuperior) {
  702. /* in delete mode */
  703. /* delete old dn so set that up */
  704. values_del[0] = (char *)slapi_sdn_get_dn(origDN);
  705. values_del[1] = NULL;
  706. attribute1.mod_type = attrName;
  707. attribute1.mod_op = LDAP_MOD_DELETE;
  708. attribute1.mod_values = values_del;
  709. list_of_mods[0] = &attribute1;
  710. /* terminate list of mods. */
  711. list_of_mods[1] = NULL;
  712. rc = _do_modify(mod_pb, entrySDN, list_of_mods);
  713. if (rc) {
  714. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  715. "_update_one_per_mod - Entry %s: deleting \"%s: %s\" failed (%d)"
  716. "\n", slapi_sdn_get_dn(entrySDN), attrName, slapi_sdn_get_dn(origDN), rc);
  717. }
  718. } else {
  719. /* in modrdn mode */
  720. const char *superior = NULL;
  721. int nval = 0;
  722. Slapi_Value *v = NULL;
  723. if (NULL == origDN) {
  724. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  725. "_update_one_per_mod - NULL dn was passed\n");
  726. goto bail;
  727. }
  728. /* need to put together rdn into a dn */
  729. dnParts = slapi_ldap_explode_dn( slapi_sdn_get_dn(origDN), 0 );
  730. if (NULL == dnParts) {
  731. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  732. "_update_one_per_mod - Failed to explode dn %s\n",
  733. slapi_sdn_get_dn(origDN));
  734. goto bail;
  735. }
  736. if (NULL == newRDN) {
  737. newRDN = dnParts[0];
  738. }
  739. if (newsuperior) {
  740. superior = newsuperior;
  741. } else {
  742. /* do not free superior */
  743. superior = slapi_dn_find_parent(slapi_sdn_get_dn(origDN));
  744. }
  745. /* newRDN and superior are already normalized. */
  746. newDN = slapi_ch_smprintf("%s,%s", newRDN, superior);
  747. slapi_dn_ignore_case(newDN);
  748. /*
  749. * Compare the modified dn with the value of
  750. * the target attribute of referint to find out
  751. * the modified dn is the ancestor (case 2) or
  752. * the value itself (case 1).
  753. *
  754. * E.g.,
  755. * (case 1)
  756. * modrdn: uid=A,ou=B,o=C --> uid=A',ou=B',o=C
  757. * (origDN) (newDN)
  758. * member: uid=A,ou=B,ou=C --> uid=A',ou=B',ou=C
  759. * (sval) (newDN)
  760. *
  761. * (case 2)
  762. * modrdn: ou=B,o=C --> ou=B',o=C
  763. * (origDN) (newDN)
  764. * member: uid=A,ou=B,ou=C --> uid=A,ou=B',ou=C
  765. * (sval) (sval' + newDN)
  766. */
  767. for (nval = slapi_attr_first_value(attr, &v); nval != -1;
  768. nval = slapi_attr_next_value(attr, nval, &v)) {
  769. p = NULL;
  770. dnlen = 0;
  771. /* DN syntax, which should be a string */
  772. sval = slapi_ch_strdup(slapi_value_get_string(v));
  773. rc = slapi_dn_normalize_case_ext(sval, 0, &p, &dnlen);
  774. if (rc == 0) { /* sval is passed in; not terminated */
  775. *(p + dnlen) = '\0';
  776. sval = p;
  777. } else if (rc > 0) {
  778. slapi_ch_free_string(&sval);
  779. sval = p;
  780. }
  781. /* else: (rc < 0) Ignore the DN normalization error for now. */
  782. p = PL_strstr(sval, slapi_sdn_get_ndn(origDN));
  783. if (p == sval) {
  784. /* (case 1) */
  785. values_del[0] = sval;
  786. values_del[1] = NULL;
  787. attribute1.mod_type = attrName;
  788. attribute1.mod_op = LDAP_MOD_DELETE;
  789. attribute1.mod_values = values_del;
  790. list_of_mods[0] = &attribute1;
  791. values_add[0] = newDN;
  792. values_add[1] = NULL;
  793. attribute2.mod_type = attrName;
  794. attribute2.mod_op = LDAP_MOD_ADD;
  795. attribute2.mod_values = values_add;
  796. list_of_mods[1] = &attribute2;
  797. list_of_mods[2] = NULL;
  798. rc = _do_modify(mod_pb, entrySDN, list_of_mods);
  799. if (rc) {
  800. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  801. "_update_one_per_mod - Entry %s: replacing \"%s: %s\" "
  802. "with \"%s: %s\" failed (%d)\n",
  803. slapi_sdn_get_dn(entrySDN), attrName,
  804. slapi_sdn_get_dn(origDN), attrName, newDN, rc);
  805. }
  806. } else if (p) {
  807. char bak;
  808. /* (case 2) */
  809. values_del[0] = sval;
  810. values_del[1] = NULL;
  811. attribute1.mod_type = attrName;
  812. attribute1.mod_op = LDAP_MOD_DELETE;
  813. attribute1.mod_values = values_del;
  814. list_of_mods[0] = &attribute1;
  815. bak = *p;
  816. *p = '\0';
  817. /* newRDN and superior are already normalized. */
  818. newvalue = slapi_ch_smprintf("%s%s", sval, newDN);
  819. *p = bak;
  820. values_add[0]=newvalue;
  821. values_add[1]=NULL;
  822. attribute2.mod_type = attrName;
  823. attribute2.mod_op = LDAP_MOD_ADD;
  824. attribute2.mod_values = values_add;
  825. list_of_mods[1] = &attribute2;
  826. list_of_mods[2] = NULL;
  827. rc = _do_modify(mod_pb, entrySDN, list_of_mods);
  828. if (rc) {
  829. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  830. "_update_one_per_mod - Entry %s: replacing \"%s: %s\" "
  831. "with \"%s: %s\" failed (%d)\n",
  832. slapi_sdn_get_dn(entrySDN), attrName, sval, attrName, newvalue, rc);
  833. }
  834. slapi_ch_free_string(&newvalue);
  835. }
  836. /* else: value does not include the modified DN. Ignore it. */
  837. slapi_ch_free_string(&sval);
  838. }
  839. /* cleanup memory allocated for dnParts and newDN */
  840. if (dnParts){
  841. slapi_ldap_value_free(dnParts);
  842. dnParts = NULL;
  843. }
  844. slapi_ch_free_string(&newDN);
  845. }
  846. bail:
  847. return rc;
  848. }
  849. /*
  850. * update multiple attribute values per _do_modify
  851. */
  852. static int
  853. _update_all_per_mod(Slapi_DN *entrySDN, /* DN of the searched entry */
  854. Slapi_Attr *attr, /* referred attribute */
  855. char *attrName,
  856. Slapi_DN *origDN, /* original DN that was modified */
  857. char *newRDN, /* new RDN from modrdn */
  858. const char *newsuperior, /* new superior from modrdn */
  859. Slapi_PBlock *mod_pb)
  860. {
  861. Slapi_Mods *smods = NULL;
  862. char *newDN = NULL;
  863. char **dnParts = NULL;
  864. char *sval = NULL;
  865. char *newvalue = NULL;
  866. char *p = NULL;
  867. size_t dnlen = 0;
  868. int rc = 0;
  869. int nval = 0;
  870. slapi_attr_get_numvalues(attr, &nval);
  871. if (NULL == newRDN && NULL == newsuperior) {
  872. /* in delete mode */
  873. LDAPMod *mods[2];
  874. char *values_del[2];
  875. LDAPMod attribute1;
  876. /* delete old dn so set that up */
  877. values_del[0] = (char *)slapi_sdn_get_dn(origDN);
  878. values_del[1] = NULL;
  879. attribute1.mod_type = attrName;
  880. attribute1.mod_op = LDAP_MOD_DELETE;
  881. attribute1.mod_values = values_del;
  882. mods[0] = &attribute1;
  883. /* terminate list of mods. */
  884. mods[1] = NULL;
  885. rc = _do_modify(mod_pb, entrySDN, mods);
  886. if (rc) {
  887. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  888. "_update_all_per_mod - Entry %s: deleting \"%s: %s\" failed (%d)"
  889. "\n", slapi_sdn_get_dn(entrySDN), attrName, slapi_sdn_get_dn(origDN), rc);
  890. }
  891. } else {
  892. /* in modrdn mode */
  893. const char *superior = NULL;
  894. int nval = 0;
  895. Slapi_Value *v = NULL;
  896. if (NULL == origDN) {
  897. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  898. "_update_all_per_mod - NULL dn was passed\n");
  899. goto bail;
  900. }
  901. /* need to put together rdn into a dn */
  902. dnParts = slapi_ldap_explode_dn( slapi_sdn_get_dn(origDN), 0 );
  903. if (NULL == dnParts) {
  904. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  905. "_update_all_per_mod - Failed to explode dn %s\n",
  906. slapi_sdn_get_dn(origDN));
  907. goto bail;
  908. }
  909. if (NULL == newRDN) {
  910. newRDN = dnParts[0];
  911. }
  912. if (newsuperior) {
  913. superior = newsuperior;
  914. } else {
  915. /* do not free superior */
  916. superior = slapi_dn_find_parent(slapi_sdn_get_dn(origDN));
  917. }
  918. /* newRDN and superior are already normalized. */
  919. newDN = slapi_ch_smprintf("%s,%s", newRDN, superior);
  920. slapi_dn_ignore_case(newDN);
  921. /*
  922. * Compare the modified dn with the value of
  923. * the target attribute of referint to find out
  924. * the modified dn is the ancestor (case 2) or
  925. * the value itself (case 1).
  926. *
  927. * E.g.,
  928. * (case 1)
  929. * modrdn: uid=A,ou=B,o=C --> uid=A',ou=B',o=C
  930. * (origDN) (newDN)
  931. * member: uid=A,ou=B,ou=C --> uid=A',ou=B',ou=C
  932. * (sval) (newDN)
  933. *
  934. * (case 2)
  935. * modrdn: ou=B,o=C --> ou=B',o=C
  936. * (origDN) (newDN)
  937. * member: uid=A,ou=B,ou=C --> uid=A,ou=B',ou=C
  938. * (sval) (sval' + newDN)
  939. */
  940. slapi_attr_get_numvalues(attr, &nval);
  941. smods = slapi_mods_new();
  942. slapi_mods_init(smods, 2 * nval + 1);
  943. for (nval = slapi_attr_first_value(attr, &v);
  944. nval != -1;
  945. nval = slapi_attr_next_value(attr, nval, &v)) {
  946. p = NULL;
  947. dnlen = 0;
  948. /* DN syntax, which should be a string */
  949. sval = slapi_ch_strdup(slapi_value_get_string(v));
  950. rc = slapi_dn_normalize_case_ext(sval, 0, &p, &dnlen);
  951. if (rc == 0) { /* sval is passed in; not terminated */
  952. *(p + dnlen) = '\0';
  953. sval = p;
  954. } else if (rc > 0) {
  955. slapi_ch_free_string(&sval);
  956. sval = p;
  957. }
  958. /* else: (rc < 0) Ignore the DN normalization error for now. */
  959. p = PL_strstr(sval, slapi_sdn_get_ndn(origDN));
  960. if (p == sval) {
  961. /* (case 1) */
  962. slapi_mods_add_string(smods, LDAP_MOD_DELETE, attrName, sval);
  963. slapi_mods_add_string(smods, LDAP_MOD_ADD, attrName, newDN);
  964. } else if (p) {
  965. /* (case 2) */
  966. slapi_mods_add_string(smods, LDAP_MOD_DELETE, attrName, sval);
  967. *p = '\0';
  968. newvalue = slapi_ch_smprintf("%s%s", sval, newDN);
  969. slapi_mods_add_string(smods, LDAP_MOD_ADD, attrName, newvalue);
  970. slapi_ch_free_string(&newvalue);
  971. }
  972. /* else: value does not include the modified DN. Ignore it. */
  973. slapi_ch_free_string(&sval);
  974. }
  975. rc = _do_modify(mod_pb, entrySDN, slapi_mods_get_ldapmods_byref(smods));
  976. if (rc) {
  977. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  978. "_update_all_per_mod - Entry %s failed (%d)\n",
  979. slapi_sdn_get_dn(entrySDN), rc);
  980. }
  981. /* cleanup memory allocated for dnParts and newDN */
  982. if (dnParts){
  983. slapi_ldap_value_free(dnParts);
  984. dnParts = NULL;
  985. }
  986. slapi_ch_free_string(&newDN);
  987. slapi_mods_free(&smods);
  988. }
  989. bail:
  990. return rc;
  991. }
  992. int
  993. update_integrity(Slapi_DN *origSDN,
  994. char *newrDN, Slapi_DN *newsuperior,
  995. int logChanges)
  996. {
  997. Slapi_PBlock *search_result_pb = NULL;
  998. Slapi_PBlock *mod_pb = slapi_pblock_new();
  999. Slapi_Entry **search_entries = NULL;
  1000. Slapi_DN *sdn = NULL;
  1001. Slapi_Attr *attr = NULL;
  1002. void *node = NULL;
  1003. const char *origDN = slapi_sdn_get_dn(origSDN);
  1004. const char *search_base = NULL;
  1005. char *attrName = NULL;
  1006. char *filter = NULL;
  1007. char *attrs[2];
  1008. char **membership_attrs = NULL;
  1009. int search_result;
  1010. int nval = 0;
  1011. int i, j;
  1012. int rc = SLAPI_PLUGIN_SUCCESS;
  1013. membership_attrs = referint_get_attrs();
  1014. /*
  1015. * For now, just putting attributes to keep integrity on in conf file,
  1016. * until resolve the other timing mode issue
  1017. */
  1018. search_result_pb = slapi_pblock_new();
  1019. /* Search each namingContext in turn
  1020. * or use the defined scope(s)
  1021. */
  1022. if (plugin_ContainerScope) {
  1023. sdn = plugin_ContainerScope;
  1024. } else {
  1025. sdn = slapi_get_first_suffix( &node, 0 );
  1026. }
  1027. while (sdn)
  1028. {
  1029. Slapi_Backend *be = slapi_be_select(sdn);
  1030. search_base = slapi_sdn_get_dn( sdn );
  1031. for(i = 0; membership_attrs[i] != NULL; i++){
  1032. if(newrDN){
  1033. /* we need to check the children of the old dn, so use a wildcard */
  1034. filter = slapi_filter_sprintf("(%s=*%s%s)", membership_attrs[i], ESC_NEXT_VAL, origDN);
  1035. } else {
  1036. filter = slapi_filter_sprintf("(%s=%s%s)", membership_attrs[i], ESC_NEXT_VAL, origDN);
  1037. }
  1038. if ( filter ) {
  1039. /* Need only the current attribute and its subtypes */
  1040. attrs[0] = membership_attrs[i];
  1041. attrs[1] = NULL;
  1042. /* Use new search API */
  1043. slapi_pblock_init(search_result_pb);
  1044. slapi_pblock_set(search_result_pb, SLAPI_BACKEND, be);
  1045. slapi_search_internal_set_pb(search_result_pb, search_base,
  1046. LDAP_SCOPE_SUBTREE, filter, attrs, 0 /* attrs only */,
  1047. NULL, NULL, referint_plugin_identity, 0);
  1048. slapi_search_internal_pb(search_result_pb);
  1049. slapi_pblock_get( search_result_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
  1050. /* if search successfull then do integrity update */
  1051. if(search_result == LDAP_SUCCESS)
  1052. {
  1053. slapi_pblock_get(search_result_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  1054. &search_entries);
  1055. for(j = 0; search_entries[j] != NULL; j++){
  1056. attr = NULL;
  1057. attrName = NULL;
  1058. /*
  1059. * Loop over all the attributes of the entry and search
  1060. * for the integrity attribute and its subtypes
  1061. */
  1062. for (slapi_entry_first_attr(search_entries[j], &attr); attr;
  1063. slapi_entry_next_attr(search_entries[j], attr, &attr))
  1064. {
  1065. /*
  1066. * Take into account only the subtypes of the attribute
  1067. * in argv[i] having the necessary value - origDN
  1068. */
  1069. slapi_attr_get_type(attr, &attrName);
  1070. if (slapi_attr_type_cmp(membership_attrs[i], attrName,
  1071. SLAPI_TYPE_CMP_SUBTYPE) == 0)
  1072. {
  1073. nval = 0;
  1074. slapi_attr_get_numvalues(attr, &nval);
  1075. /*
  1076. * We want to reduce the "modify" call as much as
  1077. * possible. But if an entry contains 1000s of
  1078. * attributes which need to be updated by the
  1079. * referint plugin (e.g., a group containing 1000s
  1080. * of members), we want to avoid to allocate too
  1081. * many mods * in one "modify" call.
  1082. * This is a compromise: If an attribute type has
  1083. * more than 128 values, we update the attribute
  1084. * value one by one. Otherwise, update all values
  1085. * in one "modify" call.
  1086. */
  1087. if (nval > 128) {
  1088. rc = _update_one_per_mod(
  1089. slapi_entry_get_sdn(search_entries[j]),
  1090. attr, attrName, origSDN, newrDN,
  1091. slapi_sdn_get_dn(newsuperior),
  1092. mod_pb);
  1093. } else {
  1094. rc = _update_all_per_mod(
  1095. slapi_entry_get_sdn(search_entries[j]),
  1096. attr, attrName, origSDN, newrDN,
  1097. slapi_sdn_get_dn(newsuperior),
  1098. mod_pb);
  1099. }
  1100. if (rc) {
  1101. if (use_txn) {
  1102. /*
  1103. * We're using backend transactions,
  1104. * so we need to stop on failure.
  1105. */
  1106. rc = SLAPI_PLUGIN_FAILURE;
  1107. goto free_and_return;
  1108. } else {
  1109. rc = SLAPI_PLUGIN_SUCCESS;
  1110. }
  1111. }
  1112. }
  1113. }
  1114. }
  1115. } else {
  1116. if (isFatalSearchError(search_result)){
  1117. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  1118. "update_integrity - Search (base=%s filter=%s) returned "
  1119. "error %d\n", search_base, filter, search_result);
  1120. rc = SLAPI_PLUGIN_FAILURE;
  1121. goto free_and_return;
  1122. }
  1123. }
  1124. slapi_ch_free_string(&filter);
  1125. }
  1126. slapi_free_search_results_internal(search_result_pb);
  1127. }
  1128. if (plugin_ContainerScope) {
  1129. /* at the moment only a single scope is supported
  1130. * so the loop ends after the first iteration
  1131. */
  1132. sdn = NULL;
  1133. } else {
  1134. sdn = slapi_get_next_suffix( &node, 0 );
  1135. }
  1136. }
  1137. free_and_return:
  1138. /* free filter and search_results_pb */
  1139. slapi_ch_free_string(&filter);
  1140. slapi_ch_array_free(membership_attrs);
  1141. slapi_pblock_destroy(mod_pb);
  1142. if (search_result_pb) {
  1143. slapi_free_search_results_internal(search_result_pb);
  1144. slapi_pblock_destroy(search_result_pb);
  1145. }
  1146. return(rc);
  1147. }
  1148. int referint_postop_start( Slapi_PBlock *pb)
  1149. {
  1150. Slapi_Entry *plugin_entry = NULL;
  1151. Slapi_Entry *config_e = NULL;
  1152. Slapi_PBlock *search_pb = NULL;
  1153. Slapi_Entry *e = NULL;
  1154. Slapi_DN *config_sdn = NULL;
  1155. char *config_area = NULL;
  1156. int result = 0;
  1157. int rc = 0;
  1158. if((config_rwlock = slapi_new_rwlock()) == NULL){
  1159. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_start - Failed to "
  1160. "create rwlock.\n" );
  1161. return ( -1 );
  1162. }
  1163. slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &plugin_entry );
  1164. /* Set the alternate config area if one is defined. */
  1165. slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_AREA, &config_area);
  1166. if (config_area)
  1167. {
  1168. rc = slapi_dn_syntax_check(pb, config_area, 1);
  1169. if (rc) { /* syntax check failed */
  1170. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_postop_start - "
  1171. "%s does not contain a valid DN (%s)\n",
  1172. SLAPI_PLUGIN_SHARED_CONFIG_AREA, config_area);
  1173. rc = LDAP_INVALID_DN_SYNTAX;
  1174. goto bail;
  1175. }
  1176. config_sdn = slapi_sdn_new_dn_byval(config_area);
  1177. result = slapi_search_internal_get_entry(config_sdn, NULL, &e, referint_plugin_identity);
  1178. if (LDAP_SUCCESS != result) {
  1179. if (result == LDAP_NO_SUCH_OBJECT) {
  1180. /* log an error and use the plugin entry for the config */
  1181. slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
  1182. "referint_postop_start -Config entry \"%s\" does "
  1183. "not exist.\n", config_area);
  1184. rc = -1;
  1185. goto bail;
  1186. }
  1187. } else {
  1188. if(e){
  1189. config_e = e;
  1190. } else {
  1191. slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
  1192. "referint_postop_start - Config entry \"%s\" was not located.\n", config_area);
  1193. rc = -1;
  1194. goto bail;
  1195. }
  1196. }
  1197. } else {
  1198. config_e = plugin_entry;
  1199. }
  1200. if(load_config(pb, config_e, STARTUP) != LDAP_SUCCESS){
  1201. rc = -1;
  1202. goto bail;
  1203. }
  1204. referint_set_config_area(slapi_entry_get_sdn(config_e));
  1205. /*
  1206. * Only bother to start the thread if you are in delay mode.
  1207. * 0 = no delay,
  1208. * -1 = integrity off
  1209. */
  1210. if(referint_get_delay() > 0){
  1211. /* initialize the cv and lock */
  1212. if (!use_txn && (NULL == referint_mutex)) {
  1213. referint_mutex = PR_NewLock();
  1214. }
  1215. keeprunning_mutex = PR_NewLock();
  1216. keeprunning_cv = PR_NewCondVar(keeprunning_mutex);
  1217. keeprunning =1;
  1218. referint_tid = PR_CreateThread (PR_USER_THREAD,
  1219. referint_thread_func,
  1220. NULL,
  1221. PR_PRIORITY_NORMAL,
  1222. PR_GLOBAL_THREAD,
  1223. PR_UNJOINABLE_THREAD,
  1224. SLAPD_DEFAULT_THREAD_STACKSIZE);
  1225. if ( referint_tid == NULL ) {
  1226. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  1227. "referint_postop_start - PR_CreateThread failed\n" );
  1228. exit( 1 );
  1229. }
  1230. }
  1231. bail:
  1232. slapi_free_search_results_internal(search_pb);
  1233. slapi_pblock_destroy(search_pb);
  1234. slapi_sdn_free(&config_sdn);
  1235. slapi_entry_free(e);
  1236. return rc;
  1237. }
  1238. int referint_postop_close( Slapi_PBlock *pb)
  1239. {
  1240. /* signal the thread to exit */
  1241. if (NULL != keeprunning_mutex) {
  1242. PR_Lock(keeprunning_mutex);
  1243. keeprunning=0;
  1244. if (NULL != keeprunning_cv) {
  1245. PR_NotifyCondVar(keeprunning_cv);
  1246. }
  1247. PR_Unlock(keeprunning_mutex);
  1248. }
  1249. slapi_destroy_rwlock(config_rwlock);
  1250. config_rwlock = NULL;
  1251. slapi_ch_free_string(&config->logfile);
  1252. slapi_ch_array_free(config->attrs);
  1253. slapi_ch_free((void **)&config);
  1254. return(0);
  1255. }
  1256. void
  1257. referint_thread_func(void *arg)
  1258. {
  1259. PRFileDesc *prfd = NULL;
  1260. char *logfilename = NULL;
  1261. char thisline[MAX_LINE];
  1262. char delimiter[]="\t\n";
  1263. char *ptoken;
  1264. char *tmprdn;
  1265. char *iter = NULL;
  1266. Slapi_DN *sdn = NULL;
  1267. Slapi_DN *tmpsuperior = NULL;
  1268. int logChanges = 0;
  1269. int delay;
  1270. int no_changes;
  1271. /*
  1272. * keep running this thread until plugin is signaled to close
  1273. */
  1274. while(1){
  1275. /* refresh the config */
  1276. slapi_ch_free_string(&logfilename);
  1277. referint_get_config(&delay, &logChanges, &logfilename);
  1278. no_changes=1;
  1279. while(no_changes){
  1280. PR_Lock(keeprunning_mutex);
  1281. if(keeprunning == 0){
  1282. PR_Unlock(keeprunning_mutex);
  1283. break;
  1284. }
  1285. PR_Unlock(keeprunning_mutex);
  1286. referint_lock();
  1287. if (( prfd = PR_Open( logfilename, PR_RDONLY, REFERINT_DEFAULT_FILE_MODE )) == NULL ){
  1288. referint_unlock();
  1289. /* go back to sleep and wait for this file */
  1290. PR_Lock(keeprunning_mutex);
  1291. PR_WaitCondVar(keeprunning_cv, PR_SecondsToInterval(delay));
  1292. PR_Unlock(keeprunning_mutex);
  1293. } else {
  1294. no_changes = 0;
  1295. }
  1296. }
  1297. /*
  1298. * Check keep running here, because after break out of no
  1299. * changes loop on shutdown, also need to break out of this
  1300. * loop before trying to do the changes. The server
  1301. * will pick them up on next startup as file still exists
  1302. */
  1303. PR_Lock(keeprunning_mutex);
  1304. if(keeprunning == 0){
  1305. PR_Unlock(keeprunning_mutex);
  1306. break;
  1307. }
  1308. PR_Unlock(keeprunning_mutex);
  1309. while( GetNextLine(thisline, MAX_LINE, prfd) ){
  1310. ptoken = ldap_utf8strtok_r(thisline, delimiter, &iter);
  1311. sdn = slapi_sdn_new_normdn_byref(ptoken);
  1312. ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
  1313. if(!strcasecmp(ptoken, "NULL")) {
  1314. tmprdn = NULL;
  1315. } else {
  1316. tmprdn = slapi_ch_smprintf("%s", ptoken);
  1317. }
  1318. ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
  1319. if (!strcasecmp(ptoken, "NULL")) {
  1320. tmpsuperior = NULL;
  1321. } else {
  1322. tmpsuperior = slapi_sdn_new_normdn_byref(ptoken);
  1323. }
  1324. ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
  1325. if (strcasecmp(ptoken, "NULL") != 0) {
  1326. /* Set the bind DN in the thread data */
  1327. if(slapi_td_set_dn(slapi_ch_strdup(ptoken))){
  1328. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,"referint_thread_func - "
  1329. "Failed to set thread data\n");
  1330. }
  1331. }
  1332. update_integrity(sdn, tmprdn, tmpsuperior, logChanges);
  1333. slapi_sdn_free(&sdn);
  1334. slapi_ch_free_string(&tmprdn);
  1335. slapi_sdn_free(&tmpsuperior);
  1336. }
  1337. PR_Close(prfd);
  1338. /* remove the original file */
  1339. if( PR_SUCCESS != PR_Delete(logfilename) ){
  1340. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  1341. "referint_thread_func - Could not delete \"%s\"\n", logfilename );
  1342. }
  1343. /* unlock and let other writers back at the file */
  1344. referint_unlock();
  1345. /* wait on condition here */
  1346. PR_Lock(keeprunning_mutex);
  1347. PR_WaitCondVar(keeprunning_cv, PR_SecondsToInterval(delay));
  1348. PR_Unlock(keeprunning_mutex);
  1349. }
  1350. /* cleanup resources allocated in start */
  1351. if (NULL != keeprunning_mutex) {
  1352. PR_DestroyLock(keeprunning_mutex);
  1353. }
  1354. if (NULL != referint_mutex) {
  1355. PR_DestroyLock(referint_mutex);
  1356. }
  1357. if (NULL != keeprunning_cv) {
  1358. PR_DestroyCondVar(keeprunning_cv);
  1359. }
  1360. slapi_ch_free_string(&logfilename);
  1361. }
  1362. int my_fgetc(PRFileDesc *stream)
  1363. {
  1364. static char buf[READ_BUFSIZE] = "\0";
  1365. static int position = READ_BUFSIZE;
  1366. int retval;
  1367. int err;
  1368. /* check if we need to load the buffer */
  1369. if( READ_BUFSIZE == position )
  1370. {
  1371. memset(buf, '\0', READ_BUFSIZE);
  1372. if( ( err = PR_Read(stream, buf, READ_BUFSIZE) ) >= 0)
  1373. {
  1374. /* it read some data */;
  1375. position = 0;
  1376. }else{
  1377. /* an error occurred */
  1378. return err;
  1379. }
  1380. }
  1381. /* try to read some data */
  1382. if( '\0' == buf[position])
  1383. {
  1384. /* out of data, return eof */
  1385. retval = MY_EOF;
  1386. position = READ_BUFSIZE;
  1387. }else{
  1388. retval = buf[position];
  1389. position++;
  1390. }
  1391. return retval;
  1392. }
  1393. int
  1394. GetNextLine(char *dest, int size_dest, PRFileDesc *stream) {
  1395. char nextchar ='\0';
  1396. int done = 0;
  1397. int i = 0;
  1398. while(!done){
  1399. if( ( nextchar = my_fgetc(stream) ) != 0){
  1400. if( i < (size_dest - 1) ){
  1401. dest[i] = nextchar;
  1402. i++;
  1403. if(nextchar == '\n'){
  1404. /* end of line reached */
  1405. done = 1;
  1406. }
  1407. } else {
  1408. /* no more room in buffer */
  1409. done = 1;
  1410. }
  1411. } else {
  1412. /* error or end of file */
  1413. done = 1;
  1414. }
  1415. }
  1416. dest[i] = '\0';
  1417. /* return size of string read */
  1418. return i;
  1419. }
  1420. /*
  1421. * Write this record to the log file
  1422. */
  1423. void
  1424. writeintegritylog(Slapi_PBlock *pb, char *logfilename, Slapi_DN *sdn,
  1425. char *newrdn, Slapi_DN *newsuperior, Slapi_DN *requestorsdn)
  1426. {
  1427. PRFileDesc *prfd;
  1428. char buffer[MAX_LINE];
  1429. int len_to_write = 0;
  1430. int rc;
  1431. const char *requestordn = NULL;
  1432. const char *newsuperiordn = NULL;
  1433. size_t reqdn_len = 0;
  1434. if (!(referint_sdn_in_entry_scope(sdn) ||
  1435. (newsuperior && referint_sdn_in_entry_scope(newsuperior)))) {
  1436. return;
  1437. }
  1438. /*
  1439. * Use this lock to protect file data when update integrity is occuring.
  1440. * If betxn is enabled, this mutex is ignored; transaction itself takes
  1441. * the role.
  1442. */
  1443. referint_lock();
  1444. if (( prfd = PR_Open( logfilename, PR_WRONLY | PR_CREATE_FILE | PR_APPEND,
  1445. REFERINT_DEFAULT_FILE_MODE )) == NULL )
  1446. {
  1447. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  1448. "writeintegritylog - Could not write integrity log \"%s\" "
  1449. SLAPI_COMPONENT_NAME_NSPR " %d (%s)\n",
  1450. logfilename, PR_GetError(), slapd_pr_strerror(PR_GetError()) );
  1451. PR_Unlock(referint_mutex);
  1452. referint_unlock();
  1453. return;
  1454. }
  1455. /*
  1456. * Make sure we have enough room in our buffer before trying to write it.
  1457. * add length of dn + 5(three tabs, a newline, and terminating \0)
  1458. */
  1459. len_to_write = slapi_sdn_get_ndn_len(sdn) + 5;
  1460. newsuperiordn = slapi_sdn_get_dn(newsuperior);
  1461. if (newsuperiordn &&
  1462. !referint_sdn_in_entry_scope(newsuperior)) {
  1463. /* this is a modrdn which moves the entry out of scope, handle like a delete */
  1464. newsuperiordn = NULL;
  1465. newrdn = NULL;
  1466. }
  1467. if(newrdn == NULL){
  1468. /* add the length of "NULL" */
  1469. len_to_write += 4;
  1470. } else {
  1471. /* add the length of the newrdn */
  1472. len_to_write += strlen(newrdn);
  1473. }
  1474. if(NULL == newsuperiordn)
  1475. {
  1476. /* add the length of "NULL" */
  1477. len_to_write += 4;
  1478. } else {
  1479. /* add the length of the newsuperior */
  1480. len_to_write += slapi_sdn_get_ndn_len(newsuperior);
  1481. }
  1482. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &requestordn);
  1483. if (requestorsdn && (requestordn = slapi_sdn_get_udn(requestorsdn)) &&
  1484. (reqdn_len = strlen(requestordn))) {
  1485. len_to_write += reqdn_len;
  1486. } else {
  1487. len_to_write += 4; /* "NULL" */
  1488. }
  1489. if(len_to_write > MAX_LINE ){
  1490. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  1491. "writeintegritylog - Could not write integrity log:"
  1492. " line length exceeded. It will not be able"
  1493. " to update references to this entry.\n");
  1494. } else {
  1495. PR_snprintf(buffer, MAX_LINE, "%s\t%s\t%s\t%s\t\n", slapi_sdn_get_dn(sdn),
  1496. (newrdn != NULL) ? newrdn : "NULL",
  1497. (newsuperiordn != NULL) ? newsuperiordn : "NULL",
  1498. requestordn ? requestordn : "NULL");
  1499. if (PR_Write(prfd,buffer,strlen(buffer)) < 0){
  1500. slapi_log_error(SLAPI_LOG_ERR,REFERINT_PLUGIN_SUBSYSTEM,
  1501. " writeintegritylog - PR_Write failed : The disk"
  1502. " may be full or the file is unwritable :: NSPR error - %d\n",
  1503. PR_GetError());
  1504. }
  1505. }
  1506. /* If file descriptor is closed successfully, PR_SUCCESS */
  1507. rc = PR_Close(prfd);
  1508. if (rc != PR_SUCCESS){
  1509. slapi_log_error(SLAPI_LOG_ERR,REFERINT_PLUGIN_SUBSYSTEM,
  1510. " writeintegritylog - Failed to close the file descriptor prfd; NSPR error - %d\n",
  1511. PR_GetError());
  1512. }
  1513. referint_unlock();
  1514. }
  1515. static int
  1516. referint_preop_init(Slapi_PBlock *pb)
  1517. {
  1518. int status = 0;
  1519. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_01) != 0 ||
  1520. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, (void *) &pdesc) != 0 ||
  1521. slapi_pblock_set(pb, premodfn, (void *)referint_validate_config) != 0)
  1522. {
  1523. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM,
  1524. "referint_preop_init - Failed to register plugin\n");
  1525. status = -1;
  1526. }
  1527. return status;
  1528. }
  1529. /*
  1530. * This is our preop function to validate a config update, postop modify
  1531. * will apply the config change.
  1532. */
  1533. int
  1534. referint_validate_config(Slapi_PBlock *pb)
  1535. {
  1536. Slapi_Entry *config_e = NULL, *e = NULL;
  1537. Slapi_Entry *pre_entry = NULL;
  1538. Slapi_Entry *resulting_entry = NULL;
  1539. Slapi_DN *config_sdn = NULL;
  1540. Slapi_DN *sdn = NULL;
  1541. Slapi_Mods *smods = NULL;
  1542. LDAPMod **mods = NULL;
  1543. char *config_area = NULL;
  1544. int rc = SLAPI_PLUGIN_SUCCESS;
  1545. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  1546. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &pre_entry);
  1547. if (!pre_entry) {
  1548. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_validate_config - Null pre op entry.\n");
  1549. rc = LDAP_OPERATIONS_ERROR;
  1550. goto bail;
  1551. }
  1552. if (referint_sdn_config_cmp(sdn) == 0 && slapi_sdn_compare(sdn, referint_get_plugin_area()) ){
  1553. /*
  1554. * This is the shared config entry. Apply the mods and set/validate
  1555. * the config
  1556. */
  1557. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1558. smods = slapi_mods_new();
  1559. slapi_mods_init_byref(smods, mods);
  1560. /*
  1561. * Create a copy of the entry and apply the
  1562. * mods to create the resulting entry.
  1563. */
  1564. resulting_entry = slapi_entry_dup(pre_entry);
  1565. if (mods && (slapi_entry_apply_mods(resulting_entry, mods) != LDAP_SUCCESS)) {
  1566. /* we don't care about this, the update is invalid and will be caught later */
  1567. goto bail;
  1568. }
  1569. if ( SLAPI_PLUGIN_FAILURE == load_config(pb, resulting_entry, 0)) {
  1570. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_validate_config - "
  1571. "configuration validation failed.\n");
  1572. rc = LDAP_UNWILLING_TO_PERFORM;
  1573. goto bail;
  1574. }
  1575. } else if (slapi_sdn_compare(sdn, referint_get_plugin_area()) == 0){
  1576. /*
  1577. * Check if the plugin config area is set(verify it and load its config),
  1578. * otherwise reload the plugin entry config
  1579. */
  1580. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1581. smods = slapi_mods_new();
  1582. slapi_mods_init_byref(smods, mods);
  1583. /* Apply the mods to create the resulting entry. */
  1584. resulting_entry = slapi_entry_dup(pre_entry);
  1585. if (mods && (slapi_entry_apply_mods(resulting_entry, mods) != LDAP_SUCCESS)) {
  1586. /* we don't care about this, the update is invalid and will be caught later */
  1587. goto bail;
  1588. }
  1589. if((config_area = slapi_entry_attr_get_charptr(resulting_entry, SLAPI_PLUGIN_SHARED_CONFIG_AREA))){
  1590. rc = slapi_dn_syntax_check(pb, config_area, 1);
  1591. if (rc) { /* syntax check failed */
  1592. slapi_log_error(SLAPI_LOG_ERR, REFERINT_PLUGIN_SUBSYSTEM, "referint_validate_config - "
  1593. "%s does not contain a valid DN (%s)\n",
  1594. SLAPI_PLUGIN_SHARED_CONFIG_AREA, config_area);
  1595. rc = LDAP_INVALID_DN_SYNTAX;
  1596. goto bail;
  1597. }
  1598. config_sdn = slapi_sdn_new_dn_byval(config_area);
  1599. rc = slapi_search_internal_get_entry(config_sdn, NULL, &e, referint_plugin_identity);
  1600. if (LDAP_SUCCESS != rc) {
  1601. /* log an error and use the plugin entry for the config */
  1602. slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
  1603. "referint_validate_config - Config entry \"%s\" couild not be found, error %d\n",
  1604. config_area, rc);
  1605. rc = LDAP_OPERATIONS_ERROR;
  1606. goto bail;
  1607. } else {
  1608. if(e){
  1609. config_e = e;
  1610. } else {
  1611. slapi_log_error(SLAPI_LOG_PLUGIN, REFERINT_PLUGIN_SUBSYSTEM,
  1612. "referint_validate_config - Config entry \"%s\" was not located.\n", config_area);
  1613. rc = LDAP_OPERATIONS_ERROR;
  1614. goto bail;
  1615. }
  1616. }
  1617. } else {
  1618. config_e = resulting_entry;
  1619. }
  1620. if(load_config(pb, config_e, 0) != LDAP_SUCCESS){
  1621. rc = LDAP_UNWILLING_TO_PERFORM;
  1622. goto bail;
  1623. }
  1624. referint_set_config_area(slapi_entry_get_sdn(config_e));
  1625. }
  1626. bail:
  1627. slapi_entry_free(e);
  1628. slapi_entry_free(resulting_entry);
  1629. slapi_sdn_free(&config_sdn);
  1630. slapi_ch_free_string(&config_area);
  1631. slapi_mods_free(&smods);
  1632. return rc;
  1633. }