referint.c 37 KB

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