referint.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include "portable.h"
  44. #include "slapi-plugin.h"
  45. #include "slap.h"
  46. /* include NSPR header files */
  47. #include "prthread.h"
  48. #include "prlock.h"
  49. #include "prerror.h"
  50. #include "prcvar.h"
  51. #include "prio.h"
  52. /* get file mode flags for unix */
  53. #ifndef _WIN32
  54. #include <sys/stat.h>
  55. #endif
  56. #define REFERINT_PLUGIN_SUBSYSTEM "referint-plugin" /* used for logging */
  57. #ifdef _WIN32
  58. #define REFERINT_DEFAULT_FILE_MODE 0
  59. #else
  60. #define REFERINT_DEFAULT_FILE_MODE S_IRUSR | S_IWUSR
  61. #endif
  62. #define MAX_LINE 2048
  63. #define READ_BUFSIZE 4096
  64. #define MY_EOF 0
  65. /* function prototypes */
  66. int referint_postop_init( Slapi_PBlock *pb );
  67. int referint_postop_del( Slapi_PBlock *pb );
  68. int referint_postop_modrdn( Slapi_PBlock *pb );
  69. int referint_postop_start( Slapi_PBlock *pb);
  70. int referint_postop_close( Slapi_PBlock *pb);
  71. int update_integrity(char **argv, char *origDN, char *newrDN, char *newsuperior, int logChanges);
  72. void referint_thread_func(void *arg);
  73. int GetNextLine(char *dest, int size_dest, PRFileDesc *stream);
  74. void writeintegritylog(char *logfilename, char *dn, char *newrdn, char *newsuperior);
  75. int my_fgetc(PRFileDesc *stream);
  76. /* global thread control stuff */
  77. static PRLock *referint_mutex = NULL;
  78. static PRThread *referint_tid = NULL;
  79. int keeprunning = 0;
  80. static PRLock *keeprunning_mutex = NULL;
  81. static PRCondVar *keeprunning_cv = NULL;
  82. static Slapi_PluginDesc pdesc = { "referint", VENDOR, DS_PACKAGE_VERSION,
  83. "referential integrity plugin" };
  84. static void* referint_plugin_identity = NULL;
  85. #ifdef _WIN32
  86. int *module_ldap_debug = 0;
  87. void plugin_init_debug_level(int *level_ptr)
  88. {
  89. module_ldap_debug = level_ptr;
  90. }
  91. #endif
  92. int
  93. referint_postop_init( Slapi_PBlock *pb )
  94. {
  95. /*
  96. * Get plugin identity and stored it for later use
  97. * Used for internal operations
  98. */
  99. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &referint_plugin_identity);
  100. PR_ASSERT (referint_plugin_identity);
  101. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  102. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  103. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  104. (void *)&pdesc ) != 0 ||
  105. slapi_pblock_set( pb, SLAPI_PLUGIN_POST_DELETE_FN,
  106. (void *) referint_postop_del ) != 0 ||
  107. slapi_pblock_set( pb, SLAPI_PLUGIN_POST_MODRDN_FN,
  108. (void *) referint_postop_modrdn ) != 0 ||
  109. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  110. (void *) referint_postop_start ) != 0 ||
  111. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  112. (void *) referint_postop_close ) != 0)
  113. {
  114. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  115. "referint_postop_init failed\n" );
  116. return( -1 );
  117. }
  118. return( 0 );
  119. }
  120. int
  121. referint_postop_del( Slapi_PBlock *pb )
  122. {
  123. char *dn;
  124. int rc;
  125. int oprc;
  126. char **argv;
  127. int argc;
  128. int delay;
  129. int logChanges=0;
  130. int isrepop = 0;
  131. if ( slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &isrepop ) != 0 ||
  132. slapi_pblock_get( pb, SLAPI_DELETE_TARGET, &dn ) != 0 ||
  133. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0)
  134. {
  135. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  136. "referint_postop_del: could not get parameters\n" );
  137. return( -1 );
  138. }
  139. /* this plugin should only execute if the delete was successful
  140. and this is not a replicated op
  141. */
  142. if(oprc != 0 || isrepop)
  143. {
  144. return( 0 );
  145. }
  146. /* get args */
  147. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGC, &argc ) != 0) {
  148. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  149. "referint_postop failed to get argc\n" );
  150. return( -1 );
  151. }
  152. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGV, &argv ) != 0) {
  153. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  154. "referint_postop failed to get argv\n" );
  155. return( -1 );
  156. }
  157. if(argv == NULL){
  158. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  159. "referint_postop_modrdn, args are NULL\n" );
  160. return( -1 );
  161. }
  162. if (argc >= 3) {
  163. /* argv[0] will be the delay */
  164. delay = atoi(argv[0]);
  165. /* argv[2] will be wether or not to log changes */
  166. logChanges = atoi(argv[2]);
  167. if(delay == -1){
  168. /* integrity updating is off */
  169. rc = 0;
  170. }else if(delay == 0){
  171. /* no delay */
  172. /* call function to update references to entry */
  173. rc = update_integrity(argv, dn, NULL, NULL, logChanges);
  174. }else{
  175. /* write the entry to integrity log */
  176. writeintegritylog(argv[1],dn, NULL, NULL);
  177. rc = 0;
  178. }
  179. } else {
  180. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  181. "referint_postop insufficient arguments supplied\n" );
  182. return( -1 );
  183. }
  184. return( rc );
  185. }
  186. int
  187. referint_postop_modrdn( Slapi_PBlock *pb )
  188. {
  189. char *dn;
  190. char *newrdn;
  191. char *newsuperior;
  192. int oprc;
  193. int rc;
  194. char **argv;
  195. int argc = 0;
  196. int delay;
  197. int logChanges=0;
  198. int isrepop = 0;
  199. if ( slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &isrepop ) != 0 ||
  200. slapi_pblock_get( pb, SLAPI_MODRDN_TARGET, &dn ) != 0 ||
  201. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn ) != 0 ||
  202. slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR, &newsuperior ) != 0 ||
  203. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0 ){
  204. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  205. "referint_postop_modrdn: could not get parameters\n" );
  206. return( -1 );
  207. }
  208. /* this plugin should only execute if the delete was successful
  209. and this is not a replicated op
  210. */
  211. if(oprc != 0 || isrepop){
  212. return( 0 );
  213. }
  214. /* get args */
  215. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGC, &argc ) != 0) {
  216. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  217. "referint_postop failed to get argv\n" );
  218. return( -1 );
  219. }
  220. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGV, &argv ) != 0) {
  221. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  222. "referint_postop failed to get argv\n" );
  223. return( -1 );
  224. }
  225. if(argv == NULL){
  226. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  227. "referint_postop_modrdn, args are NULL\n" );
  228. return( -1 );
  229. }
  230. if (argc >= 3) {
  231. /* argv[0] will always be the delay */
  232. delay = atoi(argv[0]);
  233. /* argv[2] will be wether or not to log changes */
  234. logChanges = atoi(argv[2]);
  235. } else {
  236. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  237. "referint_postop_modrdn insufficient arguments supplied\n" );
  238. return( -1 );
  239. }
  240. if(delay == -1){
  241. /* integrity updating is off */
  242. rc = 0;
  243. }else if(delay == 0){
  244. /* no delay */
  245. /* call function to update references to entry */
  246. rc = update_integrity(argv, dn, newrdn, newsuperior, logChanges);
  247. }else{
  248. /* write the entry to integrity log */
  249. writeintegritylog(argv[1],dn, newrdn, newsuperior);
  250. rc = 0;
  251. }
  252. return( rc );
  253. }
  254. int isFatalSearchError(int search_result)
  255. {
  256. /* Make sure search result is fatal
  257. * Some conditions that happen quite often are not fatal
  258. * for example if you have two suffixes and one is null, you will always
  259. * get no such object, howerever this is not a fatal error.
  260. * Add other conditions to the if statement as they are found
  261. */
  262. /* NPCTE fix for bugid 531225, esc 0. <P.R> <30-May-2001> */
  263. switch(search_result) {
  264. case LDAP_REFERRAL:
  265. case LDAP_NO_SUCH_OBJECT: return 0 ;
  266. }
  267. return 1;
  268. /* end of NPCTE fix for bugid 531225 */
  269. }
  270. static int
  271. _do_modify(Slapi_PBlock *mod_pb, const char *entryDN, LDAPMod **mods)
  272. {
  273. int rc = 0;
  274. slapi_pblock_init(mod_pb);
  275. /* Use internal operation API */
  276. slapi_modify_internal_set_pb(mod_pb, entryDN, mods, NULL, NULL,
  277. referint_plugin_identity, 0);
  278. slapi_modify_internal_pb(mod_pb);
  279. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  280. return rc;
  281. }
  282. /*
  283. * update one attribute value per _do_modify
  284. */
  285. static int
  286. _update_one_per_mod(const char *entryDN, /* DN of the searched entry */
  287. Slapi_Attr *attr, /* referred attribute */
  288. char *attrName,
  289. char *origDN, /* original DN that was modified */
  290. char *norm_origDN, /* normalized original DN */
  291. char *newRDN, /* new RDN from modrdn */
  292. char *newsuperior, /* new superior from modrdn */
  293. Slapi_PBlock *mod_pb)
  294. {
  295. LDAPMod *list_of_mods[3];
  296. char *values_del[2];
  297. char *values_add[2];
  298. char *newDN = NULL;
  299. char **dnParts = NULL;
  300. char *sval = NULL;
  301. char *newvalue = NULL;
  302. LDAPMod attribute1, attribute2;
  303. int rc = 0;
  304. if (NULL == newRDN && NULL == newsuperior) {
  305. /* in delete mode */
  306. /* delete old dn so set that up */
  307. values_del[0] = origDN;
  308. values_del[1] = NULL;
  309. attribute1.mod_type = attrName;
  310. attribute1.mod_op = LDAP_MOD_DELETE;
  311. attribute1.mod_values = values_del;
  312. list_of_mods[0] = &attribute1;
  313. /* terminate list of mods. */
  314. list_of_mods[1] = NULL;
  315. rc = _do_modify(mod_pb, entryDN, list_of_mods);
  316. if (rc) {
  317. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  318. "_update_one_value: entry %s: deleting \"%s: %s\" failed (%d)"
  319. "\n", entryDN, attrName, origDN, rc);
  320. }
  321. } else {
  322. /* in modrdn mode */
  323. const char *superior = NULL;
  324. int nval = 0;
  325. Slapi_Value *v = NULL;
  326. /* need to put together rdn into a dn */
  327. dnParts = slapi_ldap_explode_dn( origDN, 0 );
  328. if (NULL == newRDN) {
  329. newRDN = dnParts[0];
  330. }
  331. if (newsuperior) {
  332. superior = (const char *)newsuperior;
  333. } else {
  334. /* no need to free superior */
  335. superior = slapi_dn_find_parent(origDN);
  336. }
  337. /* newRDN and superior are already normalized. */
  338. newDN = slapi_ch_smprintf("%s,%s", newRDN, superior);
  339. /*
  340. * Compare the modified dn with the value of
  341. * the target attribute of referint to find out
  342. * the modified dn is the ancestor (case 2) or
  343. * the value itself (case 1).
  344. *
  345. * E.g.,
  346. * (case 1)
  347. * modrdn: uid=A,ou=B,o=C --> uid=A',ou=B',o=C
  348. * (origDN) (newDN)
  349. * member: uid=A,ou=B,ou=C --> uid=A',ou=B',ou=C
  350. * (sval) (newDN)
  351. *
  352. * (case 2)
  353. * modrdn: ou=B,o=C --> ou=B',o=C
  354. * (origDN) (newDN)
  355. * member: uid=A,ou=B,ou=C --> uid=A,ou=B',ou=C
  356. * (sval) (sval' + newDN)
  357. */
  358. for (nval = slapi_attr_first_value(attr, &v);
  359. nval != -1;
  360. nval = slapi_attr_next_value(attr, nval, &v)) {
  361. char *p = NULL;
  362. size_t dnlen = 0;
  363. /* DN syntax, which should be a string */
  364. sval = slapi_ch_strdup(slapi_value_get_string(v));
  365. rc = slapi_dn_normalize_case_ext(sval, 0, &p, &dnlen);
  366. if (rc == 0) { /* sval is passed in; not terminated */
  367. *(p + dnlen) = '\0';
  368. sval = p;
  369. } else if (rc > 0) {
  370. slapi_ch_free_string(&sval);
  371. sval = p;
  372. }
  373. /* else: (rc < 0) Ignore the DN normalization error for now. */
  374. p = PL_strstr(sval, norm_origDN);
  375. if (p == sval) {
  376. /* (case 1) */
  377. values_del[0] = sval;
  378. values_del[1] = NULL;
  379. attribute1.mod_type = attrName;
  380. attribute1.mod_op = LDAP_MOD_DELETE;
  381. attribute1.mod_values = values_del;
  382. list_of_mods[0] = &attribute1;
  383. values_add[0] = newDN;
  384. values_add[1] = NULL;
  385. attribute2.mod_type = attrName;
  386. attribute2.mod_op = LDAP_MOD_ADD;
  387. attribute2.mod_values = values_add;
  388. list_of_mods[1] = &attribute2;
  389. list_of_mods[2] = NULL;
  390. rc = _do_modify(mod_pb, entryDN, list_of_mods);
  391. if (rc) {
  392. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  393. "_update_one_value: entry %s: replacing \"%s: %s\" "
  394. "with \"%s: %s\" failed (%d)\n",
  395. entryDN, attrName, origDN, attrName, newDN, rc);
  396. }
  397. } else if (p) {
  398. char bak;
  399. /* (case 2) */
  400. values_del[0] = sval;
  401. values_del[1] = NULL;
  402. attribute1.mod_type = attrName;
  403. attribute1.mod_op = LDAP_MOD_DELETE;
  404. attribute1.mod_values = values_del;
  405. list_of_mods[0] = &attribute1;
  406. bak = *p;
  407. *p = '\0';
  408. /* newRDN and superior are already normalized. */
  409. newvalue = slapi_ch_smprintf("%s%s", sval, newDN);
  410. *p = bak;
  411. values_add[0]=newvalue;
  412. values_add[1]=NULL;
  413. attribute2.mod_type = attrName;
  414. attribute2.mod_op = LDAP_MOD_ADD;
  415. attribute2.mod_values = values_add;
  416. list_of_mods[1] = &attribute2;
  417. list_of_mods[2] = NULL;
  418. rc = _do_modify(mod_pb, entryDN, list_of_mods);
  419. if (rc) {
  420. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  421. "_update_one_value: entry %s: replacing \"%s: %s\" "
  422. "with \"%s: %s\" failed (%d)\n",
  423. entryDN, attrName, sval, attrName, newvalue, rc);
  424. }
  425. slapi_ch_free_string(&newvalue);
  426. }
  427. /* else: value does not include the modified DN. Ignore it. */
  428. slapi_ch_free_string(&sval);
  429. }
  430. /* cleanup memory allocated for dnParts and newDN */
  431. if (dnParts){
  432. slapi_ldap_value_free(dnParts);
  433. dnParts = NULL;
  434. }
  435. slapi_ch_free_string(&newDN);
  436. }
  437. /* in case these memories have not freed */
  438. slapi_ch_free_string(&newvalue);
  439. slapi_ch_free_string(&sval);
  440. if (dnParts){
  441. slapi_ldap_value_free(dnParts);
  442. dnParts = NULL;
  443. }
  444. slapi_ch_free_string(&newDN);
  445. return rc;
  446. }
  447. /*
  448. * update multiple attribute values per _do_modify
  449. */
  450. static int
  451. _update_all_per_mod(const char *entryDN, /* DN of the searched entry */
  452. Slapi_Attr *attr, /* referred attribute */
  453. char *attrName,
  454. char *origDN, /* original DN that was modified */
  455. char *norm_origDN, /* normalized original DN */
  456. char *newRDN, /* new RDN from modrdn */
  457. char *newsuperior, /* new superior from modrdn */
  458. Slapi_PBlock *mod_pb)
  459. {
  460. Slapi_Mods *smods = NULL;
  461. char *newDN = NULL;
  462. char **dnParts = NULL;
  463. char *sval = NULL;
  464. char *newvalue = NULL;
  465. int rc = 0;
  466. int nval = 0;
  467. slapi_attr_get_numvalues(attr, &nval);
  468. if (NULL == newRDN && NULL == newsuperior) {
  469. /* in delete mode */
  470. LDAPMod *mods[2];
  471. char *values_del[2];
  472. LDAPMod attribute1;
  473. /* delete old dn so set that up */
  474. values_del[0] = origDN;
  475. values_del[1] = NULL;
  476. attribute1.mod_type = attrName;
  477. attribute1.mod_op = LDAP_MOD_DELETE;
  478. attribute1.mod_values = values_del;
  479. mods[0] = &attribute1;
  480. /* terminate list of mods. */
  481. mods[1] = NULL;
  482. rc = _do_modify(mod_pb, entryDN, mods);
  483. if (rc) {
  484. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  485. "_update_one_value: entry %s: deleting \"%s: %s\" failed (%d)"
  486. "\n", entryDN, attrName, origDN, rc);
  487. }
  488. } else {
  489. /* in modrdn mode */
  490. const char *superior = NULL;
  491. int nval = 0;
  492. Slapi_Value *v = NULL;
  493. /* need to put together rdn into a dn */
  494. dnParts = slapi_ldap_explode_dn( origDN, 0 );
  495. if (NULL == newRDN) {
  496. newRDN = dnParts[0];
  497. }
  498. if (newsuperior) {
  499. superior = (const char *)newsuperior;
  500. } else {
  501. /* no need to free superior */
  502. superior = slapi_dn_find_parent(origDN);
  503. }
  504. newDN = slapi_ch_smprintf("%s,%s", newRDN, superior);
  505. /*
  506. * Compare the modified dn with the value of
  507. * the target attribute of referint to find out
  508. * the modified dn is the ancestor (case 2) or
  509. * the value itself (case 1).
  510. *
  511. * E.g.,
  512. * (case 1)
  513. * modrdn: uid=A,ou=B,o=C --> uid=A',ou=B',o=C
  514. * (origDN) (newDN)
  515. * member: uid=A,ou=B,ou=C --> uid=A',ou=B',ou=C
  516. * (sval) (newDN)
  517. *
  518. * (case 2)
  519. * modrdn: ou=B,o=C --> ou=B',o=C
  520. * (origDN) (newDN)
  521. * member: uid=A,ou=B,ou=C --> uid=A,ou=B',ou=C
  522. * (sval) (sval' + newDN)
  523. */
  524. slapi_attr_get_numvalues(attr, &nval);
  525. smods = slapi_mods_new();
  526. slapi_mods_init(smods, 2 * nval + 1);
  527. for (nval = slapi_attr_first_value(attr, &v);
  528. nval != -1;
  529. nval = slapi_attr_next_value(attr, nval, &v)) {
  530. char *p = NULL;
  531. size_t dnlen = 0;
  532. /* DN syntax, which should be a string */
  533. sval = slapi_ch_strdup(slapi_value_get_string(v));
  534. rc = slapi_dn_normalize_case_ext(sval, 0, &p, &dnlen);
  535. if (rc == 0) { /* sval is passed in; not terminated */
  536. *(p + dnlen) = '\0';
  537. sval = p;
  538. } else if (rc > 0) {
  539. slapi_ch_free_string(&sval);
  540. sval = p;
  541. }
  542. /* else: (rc < 0) Ignore the DN normalization error for now. */
  543. p = PL_strstr(sval, norm_origDN);
  544. if (p == sval) {
  545. /* (case 1) */
  546. slapi_mods_add_string(smods, LDAP_MOD_DELETE, attrName, sval);
  547. slapi_mods_add_string(smods, LDAP_MOD_ADD, attrName, newDN);
  548. } else if (p) {
  549. /* (case 2) */
  550. slapi_mods_add_string(smods, LDAP_MOD_DELETE, attrName, sval);
  551. *p = '\0';
  552. newvalue = slapi_ch_smprintf("%s%s", sval, newDN);
  553. slapi_mods_add_string(smods, LDAP_MOD_ADD, attrName, newvalue);
  554. slapi_ch_free_string(&newvalue);
  555. }
  556. /* else: value does not include the modified DN. Ignore it. */
  557. slapi_ch_free_string(&sval);
  558. }
  559. rc = _do_modify(mod_pb, entryDN, slapi_mods_get_ldapmods_byref(smods));
  560. if (rc) {
  561. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  562. "_update_all_value: entry %s failed (%d)\n",
  563. entryDN, rc);
  564. }
  565. /* cleanup memory allocated for dnParts and newDN */
  566. if (dnParts){
  567. slapi_ldap_value_free(dnParts);
  568. dnParts = NULL;
  569. }
  570. slapi_ch_free_string(&newDN);
  571. slapi_mods_free(&smods);
  572. }
  573. return rc;
  574. }
  575. int
  576. update_integrity(char **argv, char *origDN,
  577. char *newrDN, char *newsuperior, int logChanges)
  578. {
  579. Slapi_PBlock *search_result_pb = NULL;
  580. Slapi_PBlock *mod_pb = slapi_pblock_new();
  581. Slapi_Entry **search_entries = NULL;
  582. int search_result;
  583. Slapi_DN *sdn = NULL;
  584. void *node = NULL;
  585. char *filter = NULL;
  586. int i, j;
  587. const char *search_base = NULL;
  588. char *norm_origDN = NULL;
  589. size_t dnlen = 0;
  590. int rc;
  591. if ( argv == NULL ) {
  592. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  593. "referint_postop required config file arguments missing\n" );
  594. rc = -1;
  595. goto free_and_return;
  596. }
  597. /* for now, just putting attributes to keep integrity on in conf file,
  598. until resolve the other timing mode issue */
  599. rc = slapi_dn_normalize_case_ext(origDN, 0, &norm_origDN, &dnlen);
  600. if (rc == 0) { /* origDN is passed in; not terminated */
  601. *(norm_origDN + dnlen) = '\0';
  602. norm_origDN = slapi_ch_strdup(norm_origDN);
  603. } else if (rc < 0) {
  604. /* Ignore the DN normalization error for now. */
  605. norm_origDN = slapi_ch_strdup(origDN);
  606. }
  607. search_result_pb = slapi_pblock_new();
  608. /* Search each namingContext in turn */
  609. for ( sdn = slapi_get_first_suffix( &node, 0 ); sdn != NULL;
  610. sdn = slapi_get_next_suffix( &node, 0 ))
  611. {
  612. search_base = slapi_sdn_get_dn( sdn );
  613. for(i = 3; argv[i] != NULL; i++)
  614. {
  615. char buf[BUFSIZ];
  616. size_t len = strlen(origDN);
  617. filter = slapi_ch_smprintf("(%s=*%s)", argv[i], escape_filter_value(origDN, len, buf));
  618. if ( filter ) {
  619. /* Need only the current attribute and its subtypes */
  620. char *attrs[2];
  621. attrs[0] = argv[i];
  622. attrs[1] = NULL;
  623. /* Use new search API */
  624. slapi_pblock_init(search_result_pb);
  625. slapi_search_internal_set_pb(search_result_pb, search_base,
  626. LDAP_SCOPE_SUBTREE, filter, attrs, 0 /* attrs only */,
  627. NULL, NULL, referint_plugin_identity, 0);
  628. slapi_search_internal_pb(search_result_pb);
  629. slapi_pblock_get( search_result_pb, SLAPI_PLUGIN_INTOP_RESULT,
  630. &search_result);
  631. /* if search successfull then do integrity update */
  632. if(search_result == LDAP_SUCCESS)
  633. {
  634. slapi_pblock_get(search_result_pb,
  635. SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  636. &search_entries);
  637. for(j=0; search_entries[j] != NULL; j++)
  638. {
  639. Slapi_Attr *attr = NULL;
  640. char *attrName = NULL;
  641. /* Loop over all the attributes of the entry and search
  642. * for the integrity attribute and its subtypes */
  643. for (slapi_entry_first_attr(search_entries[j], &attr); attr;
  644. slapi_entry_next_attr(search_entries[j], attr, &attr))
  645. {
  646. /* Take into account only the subtypes of the attribute
  647. * in argv[i] having the necessary value - origDN */
  648. slapi_attr_get_type(attr, &attrName);
  649. if (slapi_attr_type_cmp(argv[i], attrName,
  650. SLAPI_TYPE_CMP_SUBTYPE) == 0)
  651. {
  652. int nval = 0;
  653. slapi_attr_get_numvalues(attr, &nval);
  654. /*
  655. * We want to reduce the "modify" call as much as
  656. * possible. But if an entry contains 1000s of
  657. * attributes which need to be updated by the
  658. * referint plugin (e.g., a group containing 1000s
  659. * of members), we want to avoid to allocate too
  660. * many mods * in one "modify" call.
  661. * This is a compromise: If an attribute type has
  662. * more than 128 values, we update the attribute
  663. * value one by one. Otherwise, update all values
  664. * in one "modify" call.
  665. */
  666. if (nval > 128) {
  667. rc = _update_one_per_mod(
  668. slapi_entry_get_dn(search_entries[j]),
  669. attr, attrName,
  670. origDN, norm_origDN,
  671. newrDN, newsuperior,
  672. mod_pb);
  673. } else {
  674. rc = _update_all_per_mod(
  675. slapi_entry_get_dn(search_entries[j]),
  676. attr, attrName,
  677. origDN, norm_origDN,
  678. newrDN, newsuperior,
  679. mod_pb);
  680. }
  681. /* Should we stop if one modify returns an error? */
  682. }
  683. }
  684. }
  685. } else {
  686. if (isFatalSearchError(search_result))
  687. {
  688. /* NPCTE fix for bugid 531225, esc 0. <P.R> <30-May-2001> */
  689. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  690. "update_integrity search (base=%s filter=%s) returned "
  691. "error %d\n", search_base, filter, search_result);
  692. /* end of NPCTE fix for bugid 531225 */
  693. rc = -1;
  694. goto free_and_return;
  695. }
  696. }
  697. slapi_ch_free_string(&filter);
  698. }
  699. if (search_result_pb) {
  700. slapi_free_search_results_internal(search_result_pb);
  701. }
  702. }
  703. }
  704. /* if got here, then everything good rc = 0 */
  705. rc = 0;
  706. free_and_return:
  707. slapi_ch_free_string(&norm_origDN);
  708. /* free filter and search_results_pb */
  709. slapi_ch_free_string(&filter);
  710. slapi_pblock_destroy(mod_pb);
  711. if (search_result_pb) {
  712. slapi_free_search_results_internal(search_result_pb);
  713. slapi_pblock_destroy(search_result_pb);
  714. }
  715. return(rc);
  716. }
  717. int referint_postop_start( Slapi_PBlock *pb)
  718. {
  719. char **argv;
  720. int argc = 0;
  721. /* get args */
  722. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGC, &argc ) != 0 ) {
  723. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  724. "referint_postop failed to get argv\n" );
  725. return( -1 );
  726. }
  727. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGV, &argv ) != 0 ) {
  728. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  729. "referint_postop failed to get argv\n" );
  730. return( -1 );
  731. }
  732. if(argv == NULL){
  733. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  734. "args were null in referint_postop_start\n" );
  735. return( -1 );
  736. }
  737. /* only bother to start the thread if you are in delay mode.
  738. 0 = no delay,
  739. -1 = integrity off */
  740. if (argc >= 1) {
  741. if(atoi(argv[0]) > 0){
  742. /* initialize cv and lock */
  743. referint_mutex = PR_NewLock();
  744. keeprunning_mutex = PR_NewLock();
  745. keeprunning_cv = PR_NewCondVar(keeprunning_mutex);
  746. keeprunning =1;
  747. referint_tid = PR_CreateThread (PR_USER_THREAD,
  748. referint_thread_func,
  749. (void *)argv,
  750. PR_PRIORITY_NORMAL,
  751. PR_GLOBAL_THREAD,
  752. PR_UNJOINABLE_THREAD,
  753. SLAPD_DEFAULT_THREAD_STACKSIZE);
  754. if ( referint_tid == NULL ) {
  755. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  756. "referint_postop_start PR_CreateThread failed\n" );
  757. exit( 1 );
  758. }
  759. }
  760. } else {
  761. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  762. "referint_postop_start insufficient arguments supplied\n" );
  763. return( -1 );
  764. }
  765. return(0);
  766. }
  767. int referint_postop_close( Slapi_PBlock *pb)
  768. {
  769. /* signal the thread to exit */
  770. if (NULL != keeprunning_mutex) {
  771. PR_Lock(keeprunning_mutex);
  772. keeprunning=0;
  773. if (NULL != keeprunning_cv) {
  774. PR_NotifyCondVar(keeprunning_cv);
  775. }
  776. PR_Unlock(keeprunning_mutex);
  777. }
  778. return(0);
  779. }
  780. void
  781. referint_thread_func(void *arg)
  782. {
  783. char **plugin_argv = (char **)arg;
  784. PRFileDesc *prfd;
  785. char *logfilename;
  786. char thisline[MAX_LINE];
  787. int delay;
  788. int no_changes;
  789. char delimiter[]="\t\n";
  790. char *ptoken;
  791. char *tmpdn, *tmprdn, *tmpsuperior;
  792. int logChanges=0;
  793. char * iter = NULL;
  794. if(plugin_argv == NULL){
  795. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  796. "referint_thread_func not get args \n" );
  797. return;
  798. }
  799. delay = atoi(plugin_argv[0]);
  800. logfilename = plugin_argv[1];
  801. logChanges = atoi(plugin_argv[2]);
  802. /* keep running this thread until plugin is signalled to close */
  803. while(1){
  804. no_changes=1;
  805. while(no_changes){
  806. PR_Lock(keeprunning_mutex);
  807. if(keeprunning == 0){
  808. PR_Unlock(keeprunning_mutex);
  809. break;
  810. }
  811. PR_Unlock(keeprunning_mutex);
  812. PR_Lock(referint_mutex);
  813. if (( prfd = PR_Open( logfilename, PR_RDONLY,
  814. REFERINT_DEFAULT_FILE_MODE )) == NULL )
  815. {
  816. PR_Unlock(referint_mutex);
  817. /* go back to sleep and wait for this file */
  818. PR_Lock(keeprunning_mutex);
  819. PR_WaitCondVar(keeprunning_cv, PR_SecondsToInterval(delay));
  820. PR_Unlock(keeprunning_mutex);
  821. }else{
  822. no_changes = 0;
  823. }
  824. }
  825. /* check keep running here, because after break out of no
  826. * changes loop on shutdown, also need to break out of this
  827. * loop before trying to do the changes. The server
  828. * will pick them up on next startup as file still exists
  829. */
  830. PR_Lock(keeprunning_mutex);
  831. if(keeprunning == 0){
  832. PR_Unlock(keeprunning_mutex);
  833. break;
  834. }
  835. PR_Unlock(keeprunning_mutex);
  836. while( GetNextLine(thisline, MAX_LINE, prfd) ){
  837. ptoken = ldap_utf8strtok_r(thisline, delimiter, &iter);
  838. tmpdn = slapi_ch_calloc(strlen(ptoken) + 1, sizeof(char));
  839. strcpy(tmpdn, ptoken);
  840. ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
  841. if(!strcasecmp(ptoken, "NULL")) {
  842. tmprdn = NULL;
  843. } else {
  844. tmprdn = slapi_ch_smprintf("%s", ptoken);
  845. }
  846. ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
  847. if (!strcasecmp(ptoken, "NULL")) {
  848. tmpsuperior = NULL;
  849. } else {
  850. tmpsuperior = slapi_ch_smprintf("%s", ptoken);
  851. }
  852. update_integrity(plugin_argv, tmpdn, tmprdn, tmpsuperior, logChanges);
  853. slapi_ch_free_string(&tmpdn);
  854. slapi_ch_free_string(&tmprdn);
  855. slapi_ch_free_string(&tmpsuperior);
  856. }
  857. PR_Close(prfd);
  858. /* remove the original file */
  859. if( PR_SUCCESS != PR_Delete(logfilename) )
  860. {
  861. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  862. "referint_postop_close could not delete \"%s\"\n",
  863. logfilename );
  864. }
  865. /* unlock and let other writers back at the file */
  866. PR_Unlock(referint_mutex);
  867. /* wait on condition here */
  868. PR_Lock(keeprunning_mutex);
  869. PR_WaitCondVar(keeprunning_cv, PR_SecondsToInterval(delay));
  870. PR_Unlock(keeprunning_mutex);
  871. }
  872. /* cleanup resources allocated in start */
  873. if (NULL != keeprunning_mutex) {
  874. PR_DestroyLock(keeprunning_mutex);
  875. }
  876. if (NULL != referint_mutex) {
  877. PR_DestroyLock(referint_mutex);
  878. }
  879. if (NULL != keeprunning_cv) {
  880. PR_DestroyCondVar(keeprunning_cv);
  881. }
  882. }
  883. int my_fgetc(PRFileDesc *stream)
  884. {
  885. static char buf[READ_BUFSIZE] = "\0";
  886. static int position = READ_BUFSIZE;
  887. int retval;
  888. int err;
  889. /* check if we need to load the buffer */
  890. if( READ_BUFSIZE == position )
  891. {
  892. memset(buf, '\0', READ_BUFSIZE);
  893. if( ( err = PR_Read(stream, buf, READ_BUFSIZE) ) >= 0)
  894. {
  895. /* it read some data */;
  896. position = 0;
  897. }else{
  898. /* an error occurred */
  899. return err;
  900. }
  901. }
  902. /* try to read some data */
  903. if( '\0' == buf[position])
  904. {
  905. /* out of data, return eof */
  906. retval = MY_EOF;
  907. position = READ_BUFSIZE;
  908. }else{
  909. retval = buf[position];
  910. position++;
  911. }
  912. return retval;
  913. }
  914. int
  915. GetNextLine(char *dest, int size_dest, PRFileDesc *stream) {
  916. char nextchar ='\0';
  917. int done = 0;
  918. int i = 0;
  919. while(!done)
  920. {
  921. if( ( nextchar = my_fgetc(stream) ) != 0)
  922. {
  923. if( i < (size_dest - 1) )
  924. {
  925. dest[i] = nextchar;
  926. i++;
  927. if(nextchar == '\n')
  928. {
  929. /* end of line reached */
  930. done = 1;
  931. }
  932. }else{
  933. /* no more room in buffer */
  934. done = 1;
  935. }
  936. }else{
  937. /* error or end of file */
  938. done = 1;
  939. }
  940. }
  941. dest[i] = '\0';
  942. /* return size of string read */
  943. return i;
  944. }
  945. void
  946. writeintegritylog(char *logfilename, char *dn, char *newrdn, char *newsuperior)
  947. {
  948. PRFileDesc *prfd;
  949. char buffer[MAX_LINE];
  950. int len_to_write = 0;
  951. int rc;
  952. /* write this record to the file */
  953. /* use this lock to protect file data when update integrity is occuring */
  954. /* should hopefully not be a big issue on concurrency */
  955. PR_Lock(referint_mutex);
  956. if (( prfd = PR_Open( logfilename, PR_WRONLY | PR_CREATE_FILE | PR_APPEND,
  957. REFERINT_DEFAULT_FILE_MODE )) == NULL )
  958. {
  959. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  960. "referint_postop could not write integrity log \"%s\" "
  961. SLAPI_COMPONENT_NAME_NSPR " %d (%s)\n",
  962. logfilename, PR_GetError(), slapd_pr_strerror(PR_GetError()) );
  963. PR_Unlock(referint_mutex);
  964. return;
  965. }
  966. /* make sure we have enough room in our buffer
  967. before trying to write it
  968. */
  969. /* add length of dn + 4(two tabs, a newline, and terminating \0) */
  970. len_to_write = strlen(dn) + 4;
  971. if(newrdn == NULL)
  972. {
  973. /* add the length of "NULL" */
  974. len_to_write += 4;
  975. }else{
  976. /* add the length of the newrdn */
  977. len_to_write += strlen(newrdn);
  978. }
  979. if(NULL == newsuperior)
  980. {
  981. /* add the length of "NULL" */
  982. len_to_write += 4;
  983. }else{
  984. /* add the length of the newsuperior */
  985. len_to_write += strlen(newsuperior);
  986. }
  987. if(len_to_write > MAX_LINE )
  988. {
  989. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  990. "referint_postop could not write integrity log:"
  991. " line length exceeded. It will not be able"
  992. " to update references to this entry.\n");
  993. }else{
  994. PR_snprintf(buffer, MAX_LINE, "%s\t%s\t%s\t\n",
  995. dn,
  996. (newrdn != NULL) ? newrdn : "NULL",
  997. (newsuperior != NULL) ? newsuperior : "NULL");
  998. if (PR_Write(prfd,buffer,strlen(buffer)) < 0){
  999. slapi_log_error(SLAPI_LOG_FATAL,REFERINT_PLUGIN_SUBSYSTEM,
  1000. " writeintegritylog: PR_Write failed : The disk"
  1001. " may be full or the file is unwritable :: NSPR error - %d\n",
  1002. PR_GetError());
  1003. }
  1004. }
  1005. /* If file descriptor is closed successfully, PR_SUCCESS */
  1006. rc = PR_Close(prfd);
  1007. if (rc != PR_SUCCESS)
  1008. {
  1009. slapi_log_error(SLAPI_LOG_FATAL,REFERINT_PLUGIN_SUBSYSTEM,
  1010. " writeintegritylog: failed to close the file"
  1011. " descriptor prfd; NSPR error - %d\n",
  1012. PR_GetError());
  1013. }
  1014. PR_Unlock(referint_mutex);
  1015. }