referint.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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 <dirlite_strings.h> /* PLUGIN_MAGIC_VENDOR_STR */
  47. #include "dirver.h"
  48. /* include NSPR header files */
  49. #include "prthread.h"
  50. #include "prlock.h"
  51. #include "prerror.h"
  52. #include "prcvar.h"
  53. #include "prio.h"
  54. /* get file mode flags for unix */
  55. #ifndef _WIN32
  56. #include <sys/stat.h>
  57. #endif
  58. #define REFERINT_PLUGIN_SUBSYSTEM "referint-plugin" /* used for logging */
  59. #ifdef _WIN32
  60. #define REFERINT_DEFAULT_FILE_MODE 0
  61. #else
  62. #define REFERINT_DEFAULT_FILE_MODE S_IRUSR | S_IWUSR
  63. #endif
  64. #define MAX_LINE 2048
  65. #define READ_BUFSIZE 4096
  66. #define MY_EOF 0
  67. /* function prototypes */
  68. int referint_postop_init( Slapi_PBlock *pb );
  69. int referint_postop_del( Slapi_PBlock *pb );
  70. int referint_postop_modrdn( Slapi_PBlock *pb );
  71. int referint_postop_start( Slapi_PBlock *pb);
  72. int referint_postop_close( Slapi_PBlock *pb);
  73. int update_integrity(char **argv, char *origDN, char *newrDN, int logChanges);
  74. void referint_thread_func(void *arg);
  75. int GetNextLine(char *dest, int size_dest, PRFileDesc *stream);
  76. void writeintegritylog(char *logfilename, char *dn, char *newrdn);
  77. int my_fgetc(PRFileDesc *stream);
  78. /* global thread control stuff */
  79. static PRLock *referint_mutex = NULL;
  80. static PRThread *referint_tid = NULL;
  81. int keeprunning = 0;
  82. static PRLock *keeprunning_mutex = NULL;
  83. static PRCondVar *keeprunning_cv = NULL;
  84. static Slapi_PluginDesc pdesc = { "referint", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT,
  85. "referential integrity plugin" };
  86. static void* referint_plugin_identity = NULL;
  87. #ifdef _WIN32
  88. int *module_ldap_debug = 0;
  89. void plugin_init_debug_level(int *level_ptr)
  90. {
  91. module_ldap_debug = level_ptr;
  92. }
  93. #endif
  94. int
  95. referint_postop_init( Slapi_PBlock *pb )
  96. {
  97. /*
  98. * Get plugin identity and stored it for later use
  99. * Used for internal operations
  100. */
  101. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &referint_plugin_identity);
  102. PR_ASSERT (referint_plugin_identity);
  103. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  104. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  105. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  106. (void *)&pdesc ) != 0 ||
  107. slapi_pblock_set( pb, SLAPI_PLUGIN_POST_DELETE_FN,
  108. (void *) referint_postop_del ) != 0 ||
  109. slapi_pblock_set( pb, SLAPI_PLUGIN_POST_MODRDN_FN,
  110. (void *) referint_postop_modrdn ) != 0 ||
  111. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  112. (void *) referint_postop_start ) != 0 ||
  113. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  114. (void *) referint_postop_close ) != 0)
  115. {
  116. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  117. "referint_postop_init failed\n" );
  118. return( -1 );
  119. }
  120. return( 0 );
  121. }
  122. int
  123. referint_postop_del( Slapi_PBlock *pb )
  124. {
  125. char *dn;
  126. int rc;
  127. int oprc;
  128. char **argv;
  129. int argc;
  130. int delay;
  131. int logChanges=0;
  132. int isrepop = 0;
  133. if ( slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &isrepop ) != 0 ||
  134. slapi_pblock_get( pb, SLAPI_DELETE_TARGET, &dn ) != 0 ||
  135. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0)
  136. {
  137. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  138. "referint_postop_del: could not get parameters\n" );
  139. return( -1 );
  140. }
  141. /* this plugin should only execute if the delete was successful
  142. and this is not a replicated op
  143. */
  144. if(oprc != 0 || isrepop)
  145. {
  146. return( 0 );
  147. }
  148. /* get args */
  149. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGC, &argc ) != 0) {
  150. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  151. "referint_postop failed to get argc\n" );
  152. return( -1 );
  153. }
  154. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGV, &argv ) != 0) {
  155. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  156. "referint_postop failed to get argv\n" );
  157. return( -1 );
  158. }
  159. if(argv == NULL){
  160. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  161. "referint_postop_modrdn, args are NULL\n" );
  162. return( -1 );
  163. }
  164. if (argc >= 3) {
  165. /* argv[0] will be the delay */
  166. delay = atoi(argv[0]);
  167. /* argv[2] will be wether or not to log changes */
  168. logChanges = atoi(argv[2]);
  169. if(delay == -1){
  170. /* integrity updating is off */
  171. rc = 0;
  172. }else if(delay == 0){
  173. /* no delay */
  174. /* call function to update references to entry */
  175. rc = update_integrity(argv, dn, NULL, logChanges);
  176. }else{
  177. /* write the entry to integrity log */
  178. writeintegritylog(argv[1],dn, NULL);
  179. rc = 0;
  180. }
  181. } else {
  182. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  183. "referint_postop insufficient arguments supplied\n" );
  184. return( -1 );
  185. }
  186. return( rc );
  187. }
  188. int
  189. referint_postop_modrdn( Slapi_PBlock *pb )
  190. {
  191. char *dn;
  192. char *newrdn;
  193. int oprc;
  194. int rc;
  195. char **argv;
  196. int argc = 0;
  197. int delay;
  198. int logChanges=0;
  199. int isrepop = 0;
  200. if ( slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &isrepop ) != 0 ||
  201. slapi_pblock_get( pb, SLAPI_MODRDN_TARGET, &dn ) != 0 ||
  202. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn ) != 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, logChanges);
  247. }else{
  248. /* write the entry to integrity log */
  249. writeintegritylog(argv[1],dn, newrdn);
  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. int update_integrity(char **argv, char *origDN, char *newrDN, int logChanges){
  271. Slapi_PBlock *search_result_pb = NULL;
  272. Slapi_PBlock *mod_result_pb = NULL;
  273. Slapi_Entry **search_entries = NULL;
  274. int search_result;
  275. Slapi_DN *sdn = NULL;
  276. void *node = NULL;
  277. LDAPMod attribute1, attribute2;
  278. const LDAPMod *list_of_mods[3];
  279. char *values_del[2];
  280. char *values_add[2];
  281. char *filter = NULL;
  282. int i, j;
  283. const char *search_base = NULL;
  284. char *newDN=NULL;
  285. char **dnParts=NULL;
  286. int dnsize;
  287. int x;
  288. int rc;
  289. if ( argv == NULL ) {
  290. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  291. "referint_postop required config file arguments missing\n" );
  292. rc = -1;
  293. goto free_and_return;
  294. }
  295. /* for now, just putting attributes to keep integrity on in conf file,
  296. until resolve the other timing mode issue */
  297. /* Search each namingContext in turn */
  298. for ( sdn = slapi_get_first_suffix( &node, 0 ); sdn != NULL;
  299. sdn = slapi_get_next_suffix( &node, 0 ))
  300. {
  301. search_base = slapi_sdn_get_dn( sdn );
  302. for(i=3; argv[i] != NULL; i++)
  303. {
  304. unsigned long filtlen = strlen(argv[i]) + (strlen(origDN) * 3 ) + 4;
  305. filter = (char *)slapi_ch_calloc( filtlen, sizeof(char ));
  306. if (( search_result = ldap_create_filter( filter, filtlen, "(%a=%e)",
  307. NULL, NULL, argv[i], origDN, NULL )) == LDAP_SUCCESS ) {
  308. /* Don't need any attribute */
  309. char * attrs[2];
  310. attrs[0]="1.1";
  311. attrs[1]=NULL;
  312. /* Use new search API */
  313. search_result_pb = slapi_pblock_new();
  314. slapi_search_internal_set_pb(search_result_pb, search_base, LDAP_SCOPE_SUBTREE,
  315. filter, attrs, 0 /* attrs only */, NULL,NULL,referint_plugin_identity,0);
  316. slapi_search_internal_pb(search_result_pb);
  317. slapi_pblock_get( search_result_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
  318. }
  319. /* if search successfull then do integrity update */
  320. if(search_result == 0)
  321. {
  322. slapi_pblock_get( search_result_pb,SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  323. &search_entries);
  324. for(j=0; search_entries[j] != NULL; j++)
  325. {
  326. /* no matter what mode in always going to delete old dn so set that up */
  327. values_del[0]= origDN;
  328. values_del[1]= NULL;
  329. attribute1.mod_type = argv[i];
  330. attribute1.mod_op = LDAP_MOD_DELETE;
  331. attribute1.mod_values = values_del;
  332. list_of_mods[0] = &attribute1;
  333. if(newrDN == NULL){
  334. /* in delete mode so terminate list of mods cause this is the only one */
  335. list_of_mods[1] = NULL;
  336. }else if(newrDN != NULL){
  337. /* in modrdn mode */
  338. /* need to put together rdn into a dn */
  339. dnParts = ldap_explode_dn( origDN, 0 );
  340. /* skip original rdn so start at 1*/
  341. dnsize = 0;
  342. for(x=1; dnParts[x] != NULL; x++)
  343. {
  344. /* +2 for space and comma adding later */
  345. dnsize += strlen(dnParts[x]) + 2;
  346. }
  347. /* add the newrDN length */
  348. dnsize += strlen(newrDN) + 1;
  349. newDN = slapi_ch_calloc(dnsize, sizeof(char));
  350. strcat(newDN, newrDN);
  351. for(x=1; dnParts[x] != NULL; x++)
  352. {
  353. strcat(newDN, ", ");
  354. strcat(newDN, dnParts[x]);
  355. }
  356. values_add[0]=newDN;
  357. values_add[1]=NULL;
  358. attribute2.mod_type = argv[i];
  359. attribute2.mod_op = LDAP_MOD_ADD;
  360. attribute2.mod_values = values_add;
  361. /* add the new dn to list of mods and terminate list of mods */
  362. list_of_mods[1] = &attribute2;
  363. list_of_mods[2] = NULL;
  364. }
  365. /* try to cleanup entry */
  366. /* Use new internal operation API */
  367. mod_result_pb=slapi_pblock_new();
  368. slapi_modify_internal_set_pb(mod_result_pb,slapi_entry_get_dn(search_entries[j]),
  369. (LDAPMod **)list_of_mods,NULL,NULL,referint_plugin_identity,0);
  370. slapi_modify_internal_pb(mod_result_pb);
  371. /* could check the result code here if want to log it or something later
  372. for now, continue no matter what result is */
  373. slapi_pblock_destroy(mod_result_pb);
  374. /* cleanup memory allocated for dnParts and newDN */
  375. if(dnParts != NULL){
  376. for(x=0; dnParts[x] != NULL; x++)
  377. {
  378. slapi_ch_free_string(&dnParts[x]);
  379. }
  380. slapi_ch_free((void **)&dnParts);
  381. }
  382. slapi_ch_free_string(&newDN);
  383. }
  384. }else{
  385. if(isFatalSearchError(search_result))
  386. {
  387. /* NPCTE fix for bugid 531225, esc 0. <P.R> <30-May-2001> */
  388. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  389. "referint_postop search (base=%s filter=%s) returned error %d\n", search_base,filter,search_result );
  390. /* end of NPCTE fix for bugid 531225 */
  391. rc = -1;
  392. goto free_and_return;
  393. }
  394. }
  395. slapi_ch_free((void**)&filter);
  396. if(search_result_pb != NULL){
  397. slapi_free_search_results_internal(search_result_pb);
  398. slapi_pblock_destroy(search_result_pb);
  399. search_result_pb= NULL;
  400. }
  401. }
  402. }
  403. /* if got here, then everything good rc = 0 */
  404. rc = 0;
  405. free_and_return:
  406. /* free filter and search_results_pb */
  407. slapi_ch_free_string(&filter);
  408. if(search_result_pb != NULL)
  409. {
  410. slapi_free_search_results_internal(search_result_pb);
  411. slapi_pblock_destroy(search_result_pb);
  412. }
  413. return(rc);
  414. }
  415. int referint_postop_start( Slapi_PBlock *pb)
  416. {
  417. char **argv;
  418. int argc = 0;
  419. /* get args */
  420. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGC, &argc ) != 0 ) {
  421. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  422. "referint_postop failed to get argv\n" );
  423. return( -1 );
  424. }
  425. if ( slapi_pblock_get( pb, SLAPI_PLUGIN_ARGV, &argv ) != 0 ) {
  426. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  427. "referint_postop failed to get argv\n" );
  428. return( -1 );
  429. }
  430. if(argv == NULL){
  431. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  432. "args were null in referint_postop_start\n" );
  433. return( -1 );
  434. }
  435. /* only bother to start the thread if you are in delay mode.
  436. 0 = no delay,
  437. -1 = integrity off */
  438. if (argc >= 1) {
  439. if(atoi(argv[0]) > 0){
  440. /* initialize cv and lock */
  441. referint_mutex = PR_NewLock();
  442. keeprunning_mutex = PR_NewLock();
  443. keeprunning_cv = PR_NewCondVar(keeprunning_mutex);
  444. keeprunning =1;
  445. referint_tid = PR_CreateThread (PR_USER_THREAD,
  446. referint_thread_func,
  447. (void *)argv,
  448. PR_PRIORITY_NORMAL,
  449. PR_GLOBAL_THREAD,
  450. PR_UNJOINABLE_THREAD,
  451. SLAPD_DEFAULT_THREAD_STACKSIZE);
  452. if ( referint_tid == NULL ) {
  453. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  454. "referint_postop_start PR_CreateThread failed\n" );
  455. exit( 1 );
  456. }
  457. }
  458. } else {
  459. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  460. "referint_postop_start insufficient arguments supplied\n" );
  461. return( -1 );
  462. }
  463. return(0);
  464. }
  465. int referint_postop_close( Slapi_PBlock *pb)
  466. {
  467. /* signal the thread to exit */
  468. if (NULL != keeprunning_mutex) {
  469. PR_Lock(keeprunning_mutex);
  470. keeprunning=0;
  471. if (NULL != keeprunning_cv) {
  472. PR_NotifyCondVar(keeprunning_cv);
  473. }
  474. PR_Unlock(keeprunning_mutex);
  475. }
  476. return(0);
  477. }
  478. void referint_thread_func(void *arg){
  479. char **plugin_argv = (char **)arg;
  480. PRFileDesc *prfd;
  481. char *logfilename;
  482. char thisline[MAX_LINE];
  483. int delay;
  484. int no_changes;
  485. char delimiter[]="\t\n";
  486. char *ptoken;
  487. char *tmpdn, *tmprdn;
  488. int logChanges=0;
  489. char * iter = NULL;
  490. if(plugin_argv == NULL){
  491. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  492. "referint_thread_func not get args \n" );
  493. return;
  494. }
  495. delay = atoi(plugin_argv[0]);
  496. logfilename = plugin_argv[1];
  497. logChanges = atoi(plugin_argv[2]);
  498. /* keep running this thread until plugin is signalled to close */
  499. while(1){
  500. no_changes=1;
  501. while(no_changes){
  502. PR_Lock(keeprunning_mutex);
  503. if(keeprunning == 0){
  504. PR_Unlock(keeprunning_mutex);
  505. break;
  506. }
  507. PR_Unlock(keeprunning_mutex);
  508. PR_Lock(referint_mutex);
  509. if (( prfd = PR_Open( logfilename, PR_RDONLY,
  510. REFERINT_DEFAULT_FILE_MODE )) == NULL )
  511. {
  512. PR_Unlock(referint_mutex);
  513. /* go back to sleep and wait for this file */
  514. PR_Lock(keeprunning_mutex);
  515. PR_WaitCondVar(keeprunning_cv, PR_SecondsToInterval(delay));
  516. PR_Unlock(keeprunning_mutex);
  517. }else{
  518. no_changes = 0;
  519. }
  520. }
  521. /* check keep running here, because after break out of no
  522. * changes loop on shutdown, also need to break out of this
  523. * loop before trying to do the changes. The server
  524. * will pick them up on next startup as file still exists
  525. */
  526. PR_Lock(keeprunning_mutex);
  527. if(keeprunning == 0){
  528. PR_Unlock(keeprunning_mutex);
  529. break;
  530. }
  531. PR_Unlock(keeprunning_mutex);
  532. while( GetNextLine(thisline, MAX_LINE, prfd) ){
  533. ptoken = ldap_utf8strtok_r(thisline, delimiter, &iter);
  534. tmpdn = slapi_ch_calloc(strlen(ptoken) + 1, sizeof(char));
  535. strcpy(tmpdn, ptoken);
  536. ptoken = ldap_utf8strtok_r (NULL, delimiter, &iter);
  537. if(!strcasecmp(ptoken, "NULL")){
  538. tmprdn = NULL;
  539. }else{
  540. tmprdn = slapi_ch_calloc(strlen(ptoken) + 1, sizeof(char));
  541. strcpy(tmprdn, ptoken);
  542. }
  543. update_integrity(plugin_argv, tmpdn, tmprdn, logChanges);
  544. slapi_ch_free((void **) &tmpdn);
  545. slapi_ch_free((void **) &tmprdn);
  546. }
  547. PR_Close(prfd);
  548. /* remove the original file */
  549. if( PR_SUCCESS != PR_Delete(logfilename) )
  550. {
  551. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  552. "referint_postop_close could not delete \"%s\"\n",
  553. logfilename );
  554. }
  555. /* unlock and let other writers back at the file */
  556. PR_Unlock(referint_mutex);
  557. /* wait on condition here */
  558. PR_Lock(keeprunning_mutex);
  559. PR_WaitCondVar(keeprunning_cv, PR_SecondsToInterval(delay));
  560. PR_Unlock(keeprunning_mutex);
  561. }
  562. /* cleanup resources allocated in start */
  563. if (NULL != keeprunning_mutex) {
  564. PR_DestroyLock(keeprunning_mutex);
  565. }
  566. if (NULL != referint_mutex) {
  567. PR_DestroyLock(referint_mutex);
  568. }
  569. if (NULL != keeprunning_cv) {
  570. PR_DestroyCondVar(keeprunning_cv);
  571. }
  572. }
  573. int my_fgetc(PRFileDesc *stream)
  574. {
  575. static char buf[READ_BUFSIZE] = "\0";
  576. static int position = READ_BUFSIZE;
  577. int retval;
  578. int err;
  579. /* check if we need to load the buffer */
  580. if( READ_BUFSIZE == position )
  581. {
  582. memset(buf, '\0', READ_BUFSIZE);
  583. if( ( err = PR_Read(stream, buf, READ_BUFSIZE) ) >= 0)
  584. {
  585. /* it read some data */;
  586. position = 0;
  587. }else{
  588. /* an error occurred */
  589. return err;
  590. }
  591. }
  592. /* try to read some data */
  593. if( '\0' == buf[position])
  594. {
  595. /* out of data, return eof */
  596. retval = MY_EOF;
  597. position = READ_BUFSIZE;
  598. }else{
  599. retval = buf[position];
  600. position++;
  601. }
  602. return retval;
  603. }
  604. int
  605. GetNextLine(char *dest, int size_dest, PRFileDesc *stream) {
  606. char nextchar ='\0';
  607. int done = 0;
  608. int i = 0;
  609. while(!done)
  610. {
  611. if( ( nextchar = my_fgetc(stream) ) != 0)
  612. {
  613. if( i < (size_dest - 1) )
  614. {
  615. dest[i] = nextchar;
  616. i++;
  617. if(nextchar == '\n')
  618. {
  619. /* end of line reached */
  620. done = 1;
  621. }
  622. }else{
  623. /* no more room in buffer */
  624. done = 1;
  625. }
  626. }else{
  627. /* error or end of file */
  628. done = 1;
  629. }
  630. }
  631. dest[i] = '\0';
  632. /* return size of string read */
  633. return i;
  634. }
  635. void writeintegritylog(char *logfilename, char *dn, char *newrdn){
  636. PRFileDesc *prfd;
  637. char buffer[MAX_LINE];
  638. int len_to_write = 0;
  639. int rc;
  640. /* write this record to the file */
  641. /* use this lock to protect file data when update integrity is occuring */
  642. /* should hopefully not be a big issue on concurrency */
  643. PR_Lock(referint_mutex);
  644. if (( prfd = PR_Open( logfilename, PR_WRONLY | PR_CREATE_FILE | PR_APPEND,
  645. REFERINT_DEFAULT_FILE_MODE )) == NULL )
  646. {
  647. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  648. "referint_postop could not write integrity log \"%s\" "
  649. SLAPI_COMPONENT_NAME_NSPR " %d (%s)\n",
  650. logfilename, PR_GetError(), slapd_pr_strerror(PR_GetError()) );
  651. PR_Unlock(referint_mutex);
  652. return;
  653. }
  654. /* make sure we have enough room in our buffer
  655. before trying to write it
  656. */
  657. /* add length of dn + 4(two tabs, a newline, and terminating \0) */
  658. len_to_write = strlen(dn) + 4;
  659. if(newrdn == NULL)
  660. {
  661. /* add the length of "NULL" */
  662. len_to_write += 4;
  663. }else{
  664. /* add the length of the newrdn */
  665. len_to_write += strlen(newrdn);
  666. }
  667. if(len_to_write > MAX_LINE )
  668. {
  669. slapi_log_error( SLAPI_LOG_FATAL, REFERINT_PLUGIN_SUBSYSTEM,
  670. "referint_postop could not write integrity log:"
  671. " line length exceeded. It will not be able"
  672. " to update references to this entry.\n");
  673. }else{
  674. PR_snprintf(buffer, MAX_LINE, "%s\t%s\t\n",
  675. dn,
  676. (newrdn != NULL) ? newrdn : "NULL");
  677. if (PR_Write(prfd,buffer,strlen(buffer)) < 0){
  678. slapi_log_error(SLAPI_LOG_FATAL,REFERINT_PLUGIN_SUBSYSTEM,
  679. " writeintegritylog: PR_Write failed : The disk"
  680. " may be full or the file is unwritable :: NSPR error - %d\n",
  681. PR_GetError());
  682. }
  683. }
  684. /* If file descriptor is closed successfully, PR_SUCCESS */
  685. rc = PR_Close(prfd);
  686. if (rc != PR_SUCCESS)
  687. {
  688. slapi_log_error(SLAPI_LOG_FATAL,REFERINT_PLUGIN_SUBSYSTEM,
  689. " writeintegritylog: failed to close the file"
  690. " descriptor prfd; NSPR error - %d\n",
  691. PR_GetError());
  692. }
  693. PR_Unlock(referint_mutex);
  694. }