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