referint.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. return rc;
  438. }
  439. /*
  440. * update multiple attribute values per _do_modify
  441. */
  442. static int
  443. _update_all_per_mod(const char *entryDN, /* DN of the searched entry */
  444. Slapi_Attr *attr, /* referred attribute */
  445. char *attrName,
  446. char *origDN, /* original DN that was modified */
  447. char *norm_origDN, /* normalized original DN */
  448. char *newRDN, /* new RDN from modrdn */
  449. char *newsuperior, /* new superior from modrdn */
  450. Slapi_PBlock *mod_pb)
  451. {
  452. Slapi_Mods *smods = NULL;
  453. char *newDN = NULL;
  454. char **dnParts = NULL;
  455. char *sval = NULL;
  456. char *newvalue = NULL;
  457. int rc = 0;
  458. int nval = 0;
  459. slapi_attr_get_numvalues(attr, &nval);
  460. if (NULL == newRDN && NULL == newsuperior) {
  461. /* in delete mode */
  462. LDAPMod *mods[2];
  463. char *values_del[2];
  464. LDAPMod attribute1;
  465. /* delete old dn so set that up */
  466. values_del[0] = origDN;
  467. values_del[1] = NULL;
  468. attribute1.mod_type = attrName;
  469. attribute1.mod_op = LDAP_MOD_DELETE;
  470. attribute1.mod_values = values_del;
  471. mods[0] = &attribute1;
  472. /* terminate list of mods. */
  473. mods[1] = NULL;
  474. rc = _do_modify(mod_pb, entryDN, mods);
  475. if (rc) {
  476. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  477. "_update_one_value: entry %s: deleting \"%s: %s\" failed (%d)"
  478. "\n", entryDN, attrName, origDN, rc);
  479. }
  480. } else {
  481. /* in modrdn mode */
  482. const char *superior = NULL;
  483. int nval = 0;
  484. Slapi_Value *v = NULL;
  485. /* need to put together rdn into a dn */
  486. dnParts = slapi_ldap_explode_dn( origDN, 0 );
  487. if (NULL == newRDN) {
  488. newRDN = dnParts[0];
  489. }
  490. if (newsuperior) {
  491. superior = (const char *)newsuperior;
  492. } else {
  493. /* no need to free superior */
  494. superior = slapi_dn_find_parent(origDN);
  495. }
  496. newDN = slapi_ch_smprintf("%s,%s", newRDN, superior);
  497. /*
  498. * Compare the modified dn with the value of
  499. * the target attribute of referint to find out
  500. * the modified dn is the ancestor (case 2) or
  501. * the value itself (case 1).
  502. *
  503. * E.g.,
  504. * (case 1)
  505. * modrdn: uid=A,ou=B,o=C --> uid=A',ou=B',o=C
  506. * (origDN) (newDN)
  507. * member: uid=A,ou=B,ou=C --> uid=A',ou=B',ou=C
  508. * (sval) (newDN)
  509. *
  510. * (case 2)
  511. * modrdn: ou=B,o=C --> ou=B',o=C
  512. * (origDN) (newDN)
  513. * member: uid=A,ou=B,ou=C --> uid=A,ou=B',ou=C
  514. * (sval) (sval' + newDN)
  515. */
  516. slapi_attr_get_numvalues(attr, &nval);
  517. smods = slapi_mods_new();
  518. slapi_mods_init(smods, 2 * nval + 1);
  519. for (nval = slapi_attr_first_value(attr, &v);
  520. nval != -1;
  521. nval = slapi_attr_next_value(attr, nval, &v)) {
  522. char *p = NULL;
  523. size_t dnlen = 0;
  524. /* DN syntax, which should be a string */
  525. sval = slapi_ch_strdup(slapi_value_get_string(v));
  526. rc = slapi_dn_normalize_case_ext(sval, 0, &p, &dnlen);
  527. if (rc == 0) { /* sval is passed in; not terminated */
  528. *(p + dnlen) = '\0';
  529. sval = p;
  530. } else if (rc > 0) {
  531. slapi_ch_free_string(&sval);
  532. sval = p;
  533. }
  534. /* else: (rc < 0) Ignore the DN normalization error for now. */
  535. p = PL_strstr(sval, norm_origDN);
  536. if (p == sval) {
  537. /* (case 1) */
  538. slapi_mods_add_string(smods, LDAP_MOD_DELETE, attrName, sval);
  539. slapi_mods_add_string(smods, LDAP_MOD_ADD, attrName, newDN);
  540. } else if (p) {
  541. /* (case 2) */
  542. slapi_mods_add_string(smods, LDAP_MOD_DELETE, attrName, sval);
  543. *p = '\0';
  544. newvalue = slapi_ch_smprintf("%s%s", sval, newDN);
  545. slapi_mods_add_string(smods, LDAP_MOD_ADD, attrName, newvalue);
  546. slapi_ch_free_string(&newvalue);
  547. }
  548. /* else: value does not include the modified DN. Ignore it. */
  549. slapi_ch_free_string(&sval);
  550. }
  551. rc = _do_modify(mod_pb, entryDN, slapi_mods_get_ldapmods_byref(smods));
  552. if (rc) {
  553. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  554. "_update_all_value: entry %s failed (%d)\n",
  555. entryDN, rc);
  556. }
  557. /* cleanup memory allocated for dnParts and newDN */
  558. if (dnParts){
  559. slapi_ldap_value_free(dnParts);
  560. dnParts = NULL;
  561. }
  562. slapi_ch_free_string(&newDN);
  563. slapi_mods_free(&smods);
  564. }
  565. return rc;
  566. }
  567. int
  568. update_integrity(char **argv, char *origDN,
  569. char *newrDN, char *newsuperior, int logChanges)
  570. {
  571. Slapi_PBlock *search_result_pb = NULL;
  572. Slapi_PBlock *mod_pb = slapi_pblock_new();
  573. Slapi_Entry **search_entries = NULL;
  574. int search_result;
  575. Slapi_DN *sdn = NULL;
  576. void *node = NULL;
  577. char *filter = NULL;
  578. int i, j;
  579. const char *search_base = NULL;
  580. char *norm_origDN = NULL;
  581. size_t dnlen = 0;
  582. int rc;
  583. if ( argv == NULL ) {
  584. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  585. "referint_postop required config file arguments missing\n" );
  586. rc = -1;
  587. goto free_and_return;
  588. }
  589. /* for now, just putting attributes to keep integrity on in conf file,
  590. until resolve the other timing mode issue */
  591. rc = slapi_dn_normalize_case_ext(origDN, 0, &norm_origDN, &dnlen);
  592. if (rc == 0) { /* origDN is passed in; not terminated */
  593. *(norm_origDN + dnlen) = '\0';
  594. norm_origDN = slapi_ch_strdup(norm_origDN);
  595. } else if (rc < 0) {
  596. /* Ignore the DN normalization error for now. */
  597. norm_origDN = slapi_ch_strdup(origDN);
  598. }
  599. search_result_pb = slapi_pblock_new();
  600. /* Search each namingContext in turn */
  601. for ( sdn = slapi_get_first_suffix( &node, 0 ); sdn != NULL;
  602. sdn = slapi_get_next_suffix( &node, 0 ))
  603. {
  604. search_base = slapi_sdn_get_dn( sdn );
  605. for(i = 3; argv[i] != NULL; i++)
  606. {
  607. char buf[BUFSIZ];
  608. size_t len = strlen(origDN);
  609. filter = slapi_ch_smprintf("(%s=*%s)", argv[i], escape_filter_value(origDN, len, buf));
  610. if ( filter ) {
  611. /* Need only the current attribute and its subtypes */
  612. char *attrs[2];
  613. attrs[0] = argv[i];
  614. attrs[1] = NULL;
  615. /* Use new search API */
  616. slapi_pblock_init(search_result_pb);
  617. slapi_search_internal_set_pb(search_result_pb, search_base,
  618. LDAP_SCOPE_SUBTREE, filter, attrs, 0 /* attrs only */,
  619. NULL, NULL, referint_plugin_identity, 0);
  620. slapi_search_internal_pb(search_result_pb);
  621. slapi_pblock_get( search_result_pb, SLAPI_PLUGIN_INTOP_RESULT,
  622. &search_result);
  623. /* if search successfull then do integrity update */
  624. if(search_result == LDAP_SUCCESS)
  625. {
  626. slapi_pblock_get(search_result_pb,
  627. SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  628. &search_entries);
  629. for(j=0; search_entries[j] != NULL; j++)
  630. {
  631. Slapi_Attr *attr = NULL;
  632. char *attrName = NULL;
  633. /* Loop over all the attributes of the entry and search
  634. * for the integrity attribute and its subtypes */
  635. for (slapi_entry_first_attr(search_entries[j], &attr); attr;
  636. slapi_entry_next_attr(search_entries[j], attr, &attr))
  637. {
  638. /* Take into account only the subtypes of the attribute
  639. * in argv[i] having the necessary value - origDN */
  640. slapi_attr_get_type(attr, &attrName);
  641. if (slapi_attr_type_cmp(argv[i], attrName,
  642. SLAPI_TYPE_CMP_SUBTYPE) == 0)
  643. {
  644. int nval = 0;
  645. slapi_attr_get_numvalues(attr, &nval);
  646. /*
  647. * We want to reduce the "modify" call as much as
  648. * possible. But if an entry contains 1000s of
  649. * attributes which need to be updated by the
  650. * referint plugin (e.g., a group containing 1000s
  651. * of members), we want to avoid to allocate too
  652. * many mods * in one "modify" call.
  653. * This is a compromise: If an attribute type has
  654. * more than 128 values, we update the attribute
  655. * value one by one. Otherwise, update all values
  656. * in one "modify" call.
  657. */
  658. if (nval > 128) {
  659. rc = _update_one_per_mod(
  660. slapi_entry_get_dn(search_entries[j]),
  661. attr, attrName,
  662. origDN, norm_origDN,
  663. newrDN, newsuperior,
  664. mod_pb);
  665. } else {
  666. rc = _update_all_per_mod(
  667. slapi_entry_get_dn(search_entries[j]),
  668. attr, attrName,
  669. origDN, norm_origDN,
  670. newrDN, newsuperior,
  671. mod_pb);
  672. }
  673. /* Should we stop if one modify returns an error? */
  674. }
  675. }
  676. }
  677. } else {
  678. if (isFatalSearchError(search_result))
  679. {
  680. /* NPCTE fix for bugid 531225, esc 0. <P.R> <30-May-2001> */
  681. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  682. "update_integrity search (base=%s filter=%s) returned "
  683. "error %d\n", search_base, filter, search_result);
  684. /* end of NPCTE fix for bugid 531225 */
  685. rc = -1;
  686. goto free_and_return;
  687. }
  688. }
  689. slapi_ch_free_string(&filter);
  690. }
  691. slapi_free_search_results_internal(search_result_pb);
  692. }
  693. }
  694. /* if got here, then everything good rc = 0 */
  695. rc = 0;
  696. free_and_return:
  697. slapi_ch_free_string(&norm_origDN);
  698. /* free filter and search_results_pb */
  699. slapi_ch_free_string(&filter);
  700. slapi_pblock_destroy(mod_pb);
  701. if (search_result_pb) {
  702. slapi_free_search_results_internal(search_result_pb);
  703. slapi_pblock_destroy(search_result_pb);
  704. }
  705. return(rc);
  706. }
  707. int referint_postop_start( Slapi_PBlock *pb)
  708. {
  709. char **argv;
  710. int argc = 0;
  711. /* get args */
  712. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGC, &argc ) != 0 ) {
  713. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  714. "referint_postop failed to get argv\n" );
  715. return( -1 );
  716. }
  717. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGV, &argv ) != 0 ) {
  718. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  719. "referint_postop failed to get argv\n" );
  720. return( -1 );
  721. }
  722. if(argv == NULL){
  723. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  724. "args were null in referint_postop_start\n" );
  725. return( -1 );
  726. }
  727. /* only bother to start the thread if you are in delay mode.
  728. 0 = no delay,
  729. -1 = integrity off */
  730. if (argc >= 1) {
  731. if(atoi(argv[0]) > 0){
  732. /* initialize cv and lock */
  733. referint_mutex = PR_NewLock();
  734. keeprunning_mutex = PR_NewLock();
  735. keeprunning_cv = PR_NewCondVar(keeprunning_mutex);
  736. keeprunning =1;
  737. referint_tid = PR_CreateThread (PR_USER_THREAD,
  738. referint_thread_func,
  739. (void *)argv,
  740. PR_PRIORITY_NORMAL,
  741. PR_GLOBAL_THREAD,
  742. PR_UNJOINABLE_THREAD,
  743. SLAPD_DEFAULT_THREAD_STACKSIZE);
  744. if ( referint_tid == NULL ) {
  745. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  746. "referint_postop_start PR_CreateThread failed\n" );
  747. exit( 1 );
  748. }
  749. }
  750. } else {
  751. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  752. "referint_postop_start insufficient arguments supplied\n" );
  753. return( -1 );
  754. }
  755. return(0);
  756. }
  757. int referint_postop_close( Slapi_PBlock *pb)
  758. {
  759. /* signal the thread to exit */
  760. if (NULL != keeprunning_mutex) {
  761. PR_Lock(keeprunning_mutex);
  762. keeprunning=0;
  763. if (NULL != keeprunning_cv) {
  764. PR_NotifyCondVar(keeprunning_cv);
  765. }
  766. PR_Unlock(keeprunning_mutex);
  767. }
  768. return(0);
  769. }
  770. void
  771. referint_thread_func(void *arg)
  772. {
  773. char **plugin_argv = (char **)arg;
  774. PRFileDesc *prfd;
  775. char *logfilename;
  776. char thisline[MAX_LINE];
  777. int delay;
  778. int no_changes;
  779. char delimiter[]="\t\n";
  780. char *ptoken;
  781. char *tmpdn, *tmprdn, *tmpsuperior;
  782. int logChanges=0;
  783. char * iter = NULL;
  784. if(plugin_argv == NULL){
  785. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  786. "referint_thread_func not get args \n" );
  787. return;
  788. }
  789. delay = atoi(plugin_argv[0]);
  790. logfilename = plugin_argv[1];
  791. logChanges = atoi(plugin_argv[2]);
  792. /* keep running this thread until plugin is signalled to close */
  793. while(1){
  794. no_changes=1;
  795. while(no_changes){
  796. PR_Lock(keeprunning_mutex);
  797. if(keeprunning == 0){
  798. PR_Unlock(keeprunning_mutex);
  799. break;
  800. }
  801. PR_Unlock(keeprunning_mutex);
  802. PR_Lock(referint_mutex);
  803. if (( prfd = PR_Open( logfilename, PR_RDONLY,
  804. REFERINT_DEFAULT_FILE_MODE )) == NULL )
  805. {
  806. PR_Unlock(referint_mutex);
  807. /* go back to sleep and wait for this file */
  808. PR_Lock(keeprunning_mutex);
  809. PR_WaitCondVar(keeprunning_cv, PR_SecondsToInterval(delay));
  810. PR_Unlock(keeprunning_mutex);
  811. }else{
  812. no_changes = 0;
  813. }
  814. }
  815. /* check keep running here, because after break out of no
  816. * changes loop on shutdown, also need to break out of this
  817. * loop before trying to do the changes. The server
  818. * will pick them up on next startup as file still exists
  819. */
  820. PR_Lock(keeprunning_mutex);
  821. if(keeprunning == 0){
  822. PR_Unlock(keeprunning_mutex);
  823. break;
  824. }
  825. PR_Unlock(keeprunning_mutex);
  826. while( GetNextLine(thisline, MAX_LINE, prfd) ){
  827. ptoken = ldap_utf8strtok_r(thisline, delimiter, &iter);
  828. tmpdn = slapi_ch_calloc(strlen(ptoken) + 1, sizeof(char));
  829. strcpy(tmpdn, ptoken);
  830. ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
  831. if(!strcasecmp(ptoken, "NULL")) {
  832. tmprdn = NULL;
  833. } else {
  834. tmprdn = slapi_ch_smprintf("%s", ptoken);
  835. }
  836. ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
  837. if (!strcasecmp(ptoken, "NULL")) {
  838. tmpsuperior = NULL;
  839. } else {
  840. tmpsuperior = slapi_ch_smprintf("%s", ptoken);
  841. }
  842. update_integrity(plugin_argv, tmpdn, tmprdn, tmpsuperior, logChanges);
  843. slapi_ch_free_string(&tmpdn);
  844. slapi_ch_free_string(&tmprdn);
  845. slapi_ch_free_string(&tmpsuperior);
  846. }
  847. PR_Close(prfd);
  848. /* remove the original file */
  849. if( PR_SUCCESS != PR_Delete(logfilename) )
  850. {
  851. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  852. "referint_postop_close could not delete \"%s\"\n",
  853. logfilename );
  854. }
  855. /* unlock and let other writers back at the file */
  856. PR_Unlock(referint_mutex);
  857. /* wait on condition here */
  858. PR_Lock(keeprunning_mutex);
  859. PR_WaitCondVar(keeprunning_cv, PR_SecondsToInterval(delay));
  860. PR_Unlock(keeprunning_mutex);
  861. }
  862. /* cleanup resources allocated in start */
  863. if (NULL != keeprunning_mutex) {
  864. PR_DestroyLock(keeprunning_mutex);
  865. }
  866. if (NULL != referint_mutex) {
  867. PR_DestroyLock(referint_mutex);
  868. }
  869. if (NULL != keeprunning_cv) {
  870. PR_DestroyCondVar(keeprunning_cv);
  871. }
  872. }
  873. int my_fgetc(PRFileDesc *stream)
  874. {
  875. static char buf[READ_BUFSIZE] = "\0";
  876. static int position = READ_BUFSIZE;
  877. int retval;
  878. int err;
  879. /* check if we need to load the buffer */
  880. if( READ_BUFSIZE == position )
  881. {
  882. memset(buf, '\0', READ_BUFSIZE);
  883. if( ( err = PR_Read(stream, buf, READ_BUFSIZE) ) >= 0)
  884. {
  885. /* it read some data */;
  886. position = 0;
  887. }else{
  888. /* an error occurred */
  889. return err;
  890. }
  891. }
  892. /* try to read some data */
  893. if( '\0' == buf[position])
  894. {
  895. /* out of data, return eof */
  896. retval = MY_EOF;
  897. position = READ_BUFSIZE;
  898. }else{
  899. retval = buf[position];
  900. position++;
  901. }
  902. return retval;
  903. }
  904. int
  905. GetNextLine(char *dest, int size_dest, PRFileDesc *stream) {
  906. char nextchar ='\0';
  907. int done = 0;
  908. int i = 0;
  909. while(!done)
  910. {
  911. if( ( nextchar = my_fgetc(stream) ) != 0)
  912. {
  913. if( i < (size_dest - 1) )
  914. {
  915. dest[i] = nextchar;
  916. i++;
  917. if(nextchar == '\n')
  918. {
  919. /* end of line reached */
  920. done = 1;
  921. }
  922. }else{
  923. /* no more room in buffer */
  924. done = 1;
  925. }
  926. }else{
  927. /* error or end of file */
  928. done = 1;
  929. }
  930. }
  931. dest[i] = '\0';
  932. /* return size of string read */
  933. return i;
  934. }
  935. void
  936. writeintegritylog(char *logfilename, char *dn, char *newrdn, char *newsuperior)
  937. {
  938. PRFileDesc *prfd;
  939. char buffer[MAX_LINE];
  940. int len_to_write = 0;
  941. int rc;
  942. /* write this record to the file */
  943. /* use this lock to protect file data when update integrity is occuring */
  944. /* should hopefully not be a big issue on concurrency */
  945. PR_Lock(referint_mutex);
  946. if (( prfd = PR_Open( logfilename, PR_WRONLY | PR_CREATE_FILE | PR_APPEND,
  947. REFERINT_DEFAULT_FILE_MODE )) == NULL )
  948. {
  949. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  950. "referint_postop could not write integrity log \"%s\" "
  951. SLAPI_COMPONENT_NAME_NSPR " %d (%s)\n",
  952. logfilename, PR_GetError(), slapd_pr_strerror(PR_GetError()) );
  953. PR_Unlock(referint_mutex);
  954. return;
  955. }
  956. /* make sure we have enough room in our buffer
  957. before trying to write it
  958. */
  959. /* add length of dn + 4(two tabs, a newline, and terminating \0) */
  960. len_to_write = strlen(dn) + 4;
  961. if(newrdn == NULL)
  962. {
  963. /* add the length of "NULL" */
  964. len_to_write += 4;
  965. }else{
  966. /* add the length of the newrdn */
  967. len_to_write += strlen(newrdn);
  968. }
  969. if(NULL == newsuperior)
  970. {
  971. /* add the length of "NULL" */
  972. len_to_write += 4;
  973. }else{
  974. /* add the length of the newsuperior */
  975. len_to_write += strlen(newsuperior);
  976. }
  977. if(len_to_write > MAX_LINE )
  978. {
  979. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  980. "referint_postop could not write integrity log:"
  981. " line length exceeded. It will not be able"
  982. " to update references to this entry.\n");
  983. }else{
  984. PR_snprintf(buffer, MAX_LINE, "%s\t%s\t%s\t\n",
  985. dn,
  986. (newrdn != NULL) ? newrdn : "NULL",
  987. (newsuperior != NULL) ? newsuperior : "NULL");
  988. if (PR_Write(prfd,buffer,strlen(buffer)) < 0){
  989. slapi_log_error(SLAPI_LOG_FATAL,REFERINT_PLUGIN_SUBSYSTEM,
  990. " writeintegritylog: PR_Write failed : The disk"
  991. " may be full or the file is unwritable :: NSPR error - %d\n",
  992. PR_GetError());
  993. }
  994. }
  995. /* If file descriptor is closed successfully, PR_SUCCESS */
  996. rc = PR_Close(prfd);
  997. if (rc != PR_SUCCESS)
  998. {
  999. slapi_log_error(SLAPI_LOG_FATAL,REFERINT_PLUGIN_SUBSYSTEM,
  1000. " writeintegritylog: failed to close the file"
  1001. " descriptor prfd; NSPR error - %d\n",
  1002. PR_GetError());
  1003. }
  1004. PR_Unlock(referint_mutex);
  1005. }