1
0

referint.c 36 KB

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