replutil.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /*
  7. * replutil.c - various utility functions common to all replication methods.
  8. */
  9. #include <nspr.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <sys/types.h>
  13. #include <errno.h>
  14. #ifndef _WIN32
  15. #include <sys/file.h>
  16. #include <sys/socket.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #endif
  20. #ifdef OS_solaris
  21. #include <dlfcn.h> /* needed for dlopen and dlsym */
  22. #endif /* solaris: dlopen */
  23. #include <time.h>
  24. #ifdef LINUX
  25. #include <errno.h> /* weird use of errno */
  26. #endif
  27. #include "slapi-plugin.h"
  28. #include "repl5.h"
  29. #include "repl.h"
  30. typedef int (*open_fn)(const char *path, int flags, ...);
  31. /* this is set during replication plugin initialization from the plugin entry */
  32. static char *replpluginpath = NULL;
  33. static PRBool is_chain_on_update_setup(const Slapi_DN *replroot);
  34. /*
  35. * All standard changeLogEntry attributes (initialized in get_cleattrs)
  36. */
  37. static char *cleattrs[ 10 ] = { NULL, NULL, NULL, NULL, NULL, NULL,
  38. NULL, NULL, NULL };
  39. /*
  40. * Function: get_cleattrs
  41. *
  42. * Returns: an array of pointers to attribute names.
  43. *
  44. * Arguments: None.
  45. *
  46. * Description: Initializes, if necessary, and returns an array of char *s
  47. * with attribute names used for retrieving changeLogEntry
  48. * entries from the directory.
  49. */
  50. char **
  51. get_cleattrs()
  52. {
  53. if ( cleattrs[ 0 ] == NULL ) {
  54. cleattrs[ 0 ] = type_objectclass;
  55. cleattrs[ 1 ] = attr_changenumber;
  56. cleattrs[ 2 ] = attr_targetdn;
  57. cleattrs[ 3 ] = attr_changetype;
  58. cleattrs[ 4 ] = attr_newrdn;
  59. cleattrs[ 5 ] = attr_deleteoldrdn;
  60. cleattrs[ 6 ] = attr_changes;
  61. cleattrs[ 7 ] = attr_newsuperior;
  62. cleattrs[ 8 ] = attr_changetime;
  63. cleattrs[ 9 ] = NULL;
  64. }
  65. return cleattrs;
  66. }
  67. /*
  68. * Function: add_bval2mods
  69. *
  70. * Description: same as add_val2mods, but sticks in a bval instead.
  71. * val can be null.
  72. */
  73. void
  74. add_bval2mods(LDAPMod **mod, char *type, char *val, int mod_op)
  75. {
  76. *mod = (LDAPMod *) slapi_ch_calloc(1, sizeof (LDAPMod));
  77. memset (*mod, 0, sizeof(LDAPMod));
  78. (*mod)->mod_op = mod_op | LDAP_MOD_BVALUES;
  79. (*mod)->mod_type = slapi_ch_strdup(type);
  80. if (val != NULL){
  81. (*mod)->mod_bvalues = (struct berval **) slapi_ch_calloc(2, sizeof(struct berval *));
  82. (*mod)->mod_bvalues[0] = (struct berval *) slapi_ch_malloc (sizeof(struct berval));
  83. (*mod)->mod_bvalues[1] = NULL;
  84. (*mod)->mod_bvalues[0]->bv_len = strlen(val);
  85. (*mod)->mod_bvalues[0]->bv_val = slapi_ch_strdup(val);
  86. } else {
  87. (*mod)->mod_bvalues = NULL;
  88. }
  89. }
  90. char*
  91. copy_berval (struct berval* from)
  92. {
  93. char* s = slapi_ch_malloc (from->bv_len + 1);
  94. memcpy (s, from->bv_val, from->bv_len);
  95. s [from->bv_len] = '\0';
  96. return s;
  97. }
  98. /*
  99. * Function: entry_print
  100. * Arguments: e - entry to print
  101. * Returns: nothing
  102. * Description: Prints the contents of an Slapi_Entry struct. Used for debugging.
  103. */
  104. void
  105. entry_print( Slapi_Entry *e )
  106. {
  107. int sz;
  108. char *p;
  109. printf( "Slapi_Entry dump:\n" );
  110. if ( e == NULL ) {
  111. printf( "Slapi_Entry is NULL\n" );
  112. return;
  113. }
  114. if (( p = slapi_entry2str( e, &sz )) == NULL ) {
  115. printf( "slapi_entry2str returned NULL\n" );
  116. return;
  117. }
  118. puts( p );
  119. fflush( stdout );
  120. free( p );
  121. return;
  122. }
  123. /* NSPR supports large file, but, according to dboreham, it does not work.
  124. The backed has its own functions to deal with large files. I thought
  125. about making them slapi function, but I don't think it makes sense because
  126. server should only export function which have to do with its operation
  127. and copying files is not one of them. So, instead, I made a copy of it in the
  128. replication module. I will switch it to NSPR once that stuff works.
  129. */
  130. int copyfile(char* source, char * destination, int overwrite, int mode)
  131. {
  132. #if defined _WIN32
  133. return (0 == CopyFile(source,destination,overwrite ? FALSE : TRUE));
  134. #else
  135. #ifdef DB_USE_64LFS
  136. #define OPEN_FUNCTION dblayer_open_large
  137. #else
  138. #define OPEN_FUNCTION open
  139. #endif
  140. int source_fd = -1;
  141. int dest_fd = -1;
  142. char *buffer = NULL;
  143. int return_value = -1;
  144. int bytes_to_write = 0;
  145. /* malloc the buffer */
  146. buffer = (char*) malloc(64*1024);
  147. if (NULL == buffer)
  148. {
  149. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "copy file: memory allocation failed\n");
  150. goto error;
  151. }
  152. /* Open source file */
  153. source_fd = OPEN_FUNCTION(source,O_RDONLY,0);
  154. if (-1 == source_fd)
  155. {
  156. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  157. "copyfile: failed to open source file %s\n", source);
  158. goto error;
  159. }
  160. /* Open destination file */
  161. dest_fd = OPEN_FUNCTION(destination,O_CREAT | O_WRONLY, mode);
  162. if (-1 == dest_fd)
  163. {
  164. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  165. "copyfile: failed to open destination file %s\n", destination);
  166. goto error;
  167. }
  168. /* Loop round reading data and writing it */
  169. while (1)
  170. {
  171. return_value = read(source_fd,buffer,64*1024);
  172. if (return_value <= 0)
  173. {
  174. /* means error or EOF */
  175. break;
  176. }
  177. bytes_to_write = return_value;
  178. return_value = write(dest_fd,buffer,bytes_to_write);
  179. if (return_value != bytes_to_write)
  180. {
  181. /* means error */
  182. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  183. "copyfile: failed to write to destination file %s\n");
  184. return_value = -1;
  185. break;
  186. }
  187. }
  188. error:
  189. if (source_fd != -1)
  190. {
  191. close(source_fd);
  192. }
  193. if (dest_fd != -1)
  194. {
  195. close(dest_fd);
  196. }
  197. if (NULL != buffer)
  198. {
  199. free(buffer);
  200. }
  201. return return_value;
  202. #endif
  203. }
  204. /* convert time from string like 1h (1 hour) to corresponding time in seconds */
  205. time_t
  206. age_str2time (const char *age)
  207. {
  208. char *maxage;
  209. char unit;
  210. time_t ageval;
  211. if (age == NULL || age[0] == '\0' || strcmp (age, "0") == 0)
  212. {
  213. return 0;
  214. }
  215. maxage = slapi_ch_strdup ( age );
  216. unit = maxage[ strlen( maxage ) - 1 ];
  217. maxage[ strlen( maxage ) - 1 ] = '\0';
  218. ageval = strntoul( maxage, strlen( maxage ), 10 );
  219. if ( maxage)
  220. {
  221. slapi_ch_free ( (void **) &maxage );
  222. }
  223. switch ( unit )
  224. {
  225. case 's':
  226. break;
  227. case 'm':
  228. ageval *= 60;
  229. break;
  230. case 'h':
  231. ageval *= ( 60 * 60 );
  232. break;
  233. case 'd':
  234. ageval *= ( 24 * 60 * 60 );
  235. break;
  236. case 'w':
  237. ageval *= ( 7 * 24 * 60 * 60 );
  238. break;
  239. default:
  240. slapi_log_error( SLAPI_LOG_PLUGIN, repl_plugin_name,
  241. "age_str2time: unknown unit \"%c\" "
  242. "for maxiumum changelog age\n", unit );
  243. ageval = -1;
  244. }
  245. return ageval;
  246. }
  247. const char*
  248. changeType2Str (int type)
  249. {
  250. switch (type)
  251. {
  252. case T_ADDCT: return T_ADDCTSTR;
  253. case T_MODIFYCT: return T_MODIFYCTSTR;
  254. case T_MODRDNCT: return T_MODRDNCTSTR;
  255. case T_DELETECT: return T_DELETECTSTR;
  256. default: return NULL;
  257. }
  258. }
  259. int
  260. str2ChangeType (const char *str)
  261. {
  262. if (strcasecmp (str, T_ADDCTSTR) == 0)
  263. return T_ADDCT;
  264. if (strcasecmp (str, T_MODIFYCTSTR) == 0)
  265. return T_MODIFYCT;
  266. if (strcasecmp (str, T_MODRDNCTSTR) == 0)
  267. return T_MODRDNCT;
  268. if (strcasecmp (str, T_DELETECTSTR) == 0)
  269. return T_DELETECT;
  270. return -1;
  271. }
  272. lenstr *
  273. make_changes_string(LDAPMod **ldm, char **includeattrs)
  274. {
  275. lenstr *l;
  276. int i, j, len;
  277. int skip;
  278. /* loop through the LDAPMod struct and construct the changes attribute */
  279. l = lenstr_new();
  280. for ( i = 0; ldm[ i ] != NULL; i++ ) {
  281. /* If a list of explicit attributes was given, only add those */
  282. if ( NULL != includeattrs ) {
  283. skip = 1;
  284. for ( j = 0; includeattrs[ j ] != NULL; j++ ) {
  285. if ( strcasecmp( includeattrs[ j ], ldm[ i ]->mod_type ) == 0 ) {
  286. skip = 0;
  287. break;
  288. }
  289. }
  290. if ( skip ) {
  291. continue;
  292. }
  293. }
  294. switch ( ldm[ i ]->mod_op & ~LDAP_MOD_BVALUES ) {
  295. case LDAP_MOD_ADD:
  296. addlenstr( l, "add: " );
  297. addlenstr( l, ldm[ i ]->mod_type );
  298. addlenstr( l, "\n" );
  299. break;
  300. case LDAP_MOD_DELETE:
  301. addlenstr( l, "delete: " );
  302. addlenstr( l, ldm[ i ]->mod_type );
  303. addlenstr( l, "\n" );
  304. break;
  305. case LDAP_MOD_REPLACE:
  306. addlenstr( l, "replace: " );
  307. addlenstr( l, ldm[ i ]->mod_type );
  308. addlenstr( l, "\n" );
  309. break;
  310. }
  311. for ( j = 0; ldm[ i ]->mod_bvalues != NULL &&
  312. ldm[ i ]->mod_bvalues[ j ] != NULL; j++ ) {
  313. char *buf = NULL;
  314. char *bufp = NULL;
  315. len = strlen( ldm[ i ]->mod_type );
  316. len = LDIF_SIZE_NEEDED( len,
  317. ldm[ i ]->mod_bvalues[ j ]->bv_len ) + 1;
  318. buf = slapi_ch_malloc( len );
  319. bufp = buf;
  320. ldif_put_type_and_value( &bufp, ldm[ i ]->mod_type,
  321. ldm[ i ]->mod_bvalues[ j ]->bv_val,
  322. ldm[ i ]->mod_bvalues[ j ]->bv_len );
  323. *bufp = '\0';
  324. addlenstr( l, buf );
  325. free( buf );
  326. }
  327. addlenstr( l, "-\n" );
  328. }
  329. return l;
  330. }
  331. /* note that the string get modified by ldif_parse*** functions */
  332. Slapi_Mods *
  333. parse_changes_string(char *str)
  334. {
  335. int rc;
  336. Slapi_Mods *mods;
  337. Slapi_Mod mod;
  338. char *line, *next;
  339. char *type, *value;
  340. int vlen;
  341. struct berval bv;
  342. /* allocate mods array */
  343. mods = slapi_mods_new ();
  344. if (mods == NULL)
  345. return NULL;
  346. slapi_mods_init (mods, 16); /* JCMREPL - ONREPL : 16 bigger than needed? */
  347. /* parse mods */
  348. next = str;
  349. line = ldif_getline (&next);
  350. while (line)
  351. {
  352. slapi_mod_init (&mod, 0);
  353. while (line)
  354. {
  355. char * errmsg = NULL;
  356. if (strcasecmp (line, "-") == 0)
  357. {
  358. if (slapi_mod_isvalid (&mod))
  359. {
  360. slapi_mods_add_smod (mods, &mod);
  361. /* JCMREPL - ONREPL - slapi_mod_done(&mod) ??? */
  362. }
  363. else
  364. {
  365. /* ONREPL - need to cleanup */
  366. }
  367. line = ldif_getline (&next);
  368. break;
  369. }
  370. rc = ldif_parse_line(line, &type, &value, &vlen, &errmsg);
  371. if (rc != 0)
  372. {
  373. /* ONREPL - log warning */
  374. if ( errmsg != NULL ) {
  375. slapi_log_error( SLAPI_LOG_PARSE, repl_plugin_name, "%s", errmsg );
  376. slapi_ch_free( (void**)&errmsg );
  377. }
  378. slapi_log_error( SLAPI_LOG_REPL, repl_plugin_name,
  379. "Failed to parse the ldif line.\n");
  380. continue;
  381. }
  382. if (strcasecmp (type, "add") == 0)
  383. {
  384. slapi_mod_set_operation (&mod, LDAP_MOD_ADD | LDAP_MOD_BVALUES);
  385. }
  386. else if (strcasecmp (type, "delete") == 0)
  387. {
  388. slapi_mod_set_operation (&mod, LDAP_MOD_DELETE | LDAP_MOD_BVALUES);
  389. }
  390. else if (strcasecmp (type, "replace") == 0)
  391. {
  392. slapi_mod_set_operation (&mod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  393. }
  394. else /* attr: value pair */
  395. {
  396. /* adding first value */
  397. if (slapi_mod_get_type (&mod) == NULL)
  398. {
  399. slapi_mod_set_type (&mod, type);
  400. }
  401. bv.bv_val = value;
  402. bv.bv_len = vlen;
  403. slapi_mod_add_value (&mod, &bv);
  404. }
  405. line = ldif_getline (&next);
  406. }
  407. }
  408. return mods;
  409. }
  410. static void* g_plg_identity [PLUGIN_MAX];
  411. void*
  412. repl_get_plugin_identity (int pluginID)
  413. {
  414. PR_ASSERT (pluginID < PLUGIN_MAX);
  415. return g_plg_identity [pluginID];
  416. }
  417. void
  418. repl_set_plugin_identity (int pluginID, void *identity)
  419. {
  420. PR_ASSERT (pluginID < PLUGIN_MAX);
  421. g_plg_identity [pluginID] = identity;
  422. }
  423. /* this function validates operation parameters */
  424. PRBool
  425. IsValidOperation (const slapi_operation_parameters *op)
  426. {
  427. if (op == NULL)
  428. {
  429. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  430. "IsValidOperation: NULL operation\n");
  431. return PR_FALSE;
  432. }
  433. if (op->csn == NULL)
  434. {
  435. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  436. "IsValidOperation: NULL operation CSN\n");
  437. return PR_FALSE;
  438. }
  439. if (op->target_address.uniqueid == NULL)
  440. {
  441. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  442. "IsValidOperation: NULL entry uniqueid\n");
  443. return PR_FALSE;
  444. }
  445. if (op->target_address.dn == NULL)
  446. {
  447. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  448. "IsValidOperation: NULL entry DN\n");
  449. return PR_FALSE;
  450. }
  451. switch (op->operation_type)
  452. {
  453. case SLAPI_OPERATION_ADD: if (op->p.p_add.target_entry == NULL)
  454. {
  455. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  456. "IsValidOperation: NULL entry for add operation\n");
  457. return PR_FALSE;
  458. }
  459. else
  460. break;
  461. case SLAPI_OPERATION_MODIFY: if (op->p.p_modify.modify_mods == NULL ||
  462. op->p.p_modify.modify_mods[0] == NULL)
  463. {
  464. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  465. "IsValidOperation: NULL mods for modify operation\n");
  466. return PR_FALSE;
  467. }
  468. else
  469. break;
  470. case SLAPI_OPERATION_MODRDN: if (op->p.p_modrdn.modrdn_mods == NULL ||
  471. op->p.p_modrdn.modrdn_mods[0] == NULL)
  472. {
  473. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  474. "IsValidOperation: NULL mods for modrdn operation\n");
  475. return PR_FALSE;
  476. }
  477. if (op->p.p_modrdn.modrdn_newrdn == NULL)
  478. {
  479. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  480. "IsValidOperation: NULL new rdn for modrdn operation\n");
  481. return PR_FALSE;
  482. }
  483. else
  484. break;
  485. case SLAPI_OPERATION_DELETE: break;
  486. default: return PR_FALSE;
  487. }
  488. return PR_TRUE;
  489. }
  490. const char *
  491. map_repl_root_to_dbid(Slapi_DN *repl_root)
  492. {
  493. const char *return_ptr;
  494. PR_ASSERT(NULL != repl_root);
  495. if (NULL != repl_root)
  496. {
  497. /* XXXggood get per-database ID here, when code available */
  498. }
  499. return_ptr = get_server_dataversion(); /* XXXggood temporary hack until we have per-database instance dbids */
  500. return return_ptr;
  501. }
  502. PRBool
  503. is_ruv_tombstone_entry (Slapi_Entry *e)
  504. {
  505. char *dn;
  506. char *match;
  507. PR_ASSERT (e);
  508. dn = slapi_entry_get_dn (e);
  509. PR_ASSERT (dn);
  510. /* tombstone has rdn: nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff */
  511. match = strstr (dn, RUV_STORAGE_ENTRY_UNIQUEID);
  512. return (match != NULL);
  513. }
  514. LDAPControl* create_managedsait_control ()
  515. {
  516. LDAPControl *control;
  517. control = (LDAPControl*)slapi_ch_malloc (sizeof (LDAPControl));
  518. control->ldctl_oid = slapi_ch_strdup (LDAP_CONTROL_MANAGEDSAIT);
  519. control->ldctl_value.bv_val = NULL;
  520. control->ldctl_value.bv_len = 0;
  521. control->ldctl_iscritical = '\0';
  522. return control;
  523. }
  524. LDAPControl* create_backend_control (Slapi_DN *sdn)
  525. {
  526. LDAPControl *control = NULL;
  527. const char *be_name = slapi_mtn_get_backend_name(sdn);
  528. if (NULL != be_name) {
  529. control = (LDAPControl*)slapi_ch_malloc (sizeof (LDAPControl));
  530. control->ldctl_oid = slapi_ch_strdup ("2.16.840.1.113730.3.4.14");
  531. control->ldctl_value.bv_val = strdup(be_name);
  532. control->ldctl_value.bv_len = strlen (be_name);
  533. control->ldctl_iscritical = 1;
  534. }
  535. return control;
  536. }
  537. /*
  538. * HREF_CHAR_ACCEPTABLE was copied from slapd/referral.c
  539. * which was copied from libldap/tmplout.c.
  540. */
  541. /* Note: an identical function is in ../../slapd/referral.c */
  542. #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
  543. ( c >= '@' && c <= 'Z' ) || \
  544. ( c == '_' ) || \
  545. ( c >= 'a' && c <= 'z' ))
  546. /*
  547. * Function: strcat_escaped
  548. *
  549. * Returns: nothing
  550. *
  551. * Description: Appends string s2 to s1, URL-escaping (%HH) unsafe
  552. * characters in s2 as appropriate. This function was
  553. * copied from slapd/referral.c.
  554. * which was copied from libldap/tmplout.c.
  555. * added const qualifier
  556. *
  557. * Author: MCS
  558. */
  559. /*
  560. * append s2 to s1, URL-escaping (%HH) unsafe characters
  561. */
  562. /* Note: an identical function is in ../../slapd/referral.c */
  563. static void
  564. strcat_escaped( char *s1, const char *s2 )
  565. {
  566. char *p, *q;
  567. char *hexdig = "0123456789ABCDEF";
  568. p = s1 + strlen( s1 );
  569. for ( q = (char*)s2; *q != '\0'; ++q ) {
  570. if ( HREF_CHAR_ACCEPTABLE( *q )) {
  571. *p++ = *q;
  572. } else {
  573. *p++ = '%';
  574. *p++ = hexdig[ 0x0F & ((*(unsigned char*)q) >> 4) ];
  575. *p++ = hexdig[ 0x0F & *q ];
  576. }
  577. }
  578. *p = '\0';
  579. }
  580. /*
  581. This function appends the replication root to the purl referrals found
  582. in the given ruv and the other given referrals, merges the lists, and sets the
  583. referrals in the mapping tree node corresponding to the given sdn, which is the
  584. repl_root
  585. This function also sets the mapping tree state (e.g. disabled, backend, referral,
  586. referral on update) - the mapping tree has very specific rules about how states
  587. can be set in the presence of referrals - specifically:
  588. 1) the nsslapd-referral attribute must be set before changing the state to referral
  589. or referral on update
  590. 2) the state must be set to backend or disabled before removing referrals
  591. */
  592. void
  593. repl_set_mtn_state_and_referrals(
  594. const Slapi_DN *repl_root_sdn,
  595. const char *mtn_state,
  596. const RUV *ruv,
  597. char **ruv_referrals,
  598. char **other_referrals
  599. )
  600. {
  601. int rc = 0;
  602. int ii = 0;
  603. char **referrals_to_set = NULL;
  604. PRBool chain_on_update = is_chain_on_update_setup(repl_root_sdn);
  605. /* Fix for blackflag bug 601440: We want the new behaviour of DS,
  606. ** going forward, to now be that if the nsds5replicareferral attrib
  607. ** has values, it should be the only values in nsslapd-referral (as
  608. ** opposed to older behaviour of concatenating with RUV-based
  609. ** referrals). [email protected]
  610. */
  611. if (other_referrals) {
  612. /* use the referrals passed in, instead of RUV-based referrals */
  613. charray_merge(&referrals_to_set, other_referrals, 1);
  614. /* Do copies. referrals is freed at the end */
  615. }
  616. else
  617. {
  618. /* use the referrals from the RUV */
  619. ruv_referrals= (ruv ? ruv_get_referrals(ruv) : ruv_referrals);
  620. if (ruv_referrals) {
  621. charray_merge(&referrals_to_set, ruv_referrals, 1);
  622. if (ruv) /* free referrals from ruv_get_referrals() */
  623. charray_free(ruv_referrals);
  624. }
  625. }
  626. /* next, add the repl root dn to each referral if not present */
  627. for (ii = 0; referrals_to_set && referrals_to_set[ii]; ++ii) {
  628. struct ldap_url_desc *lud = NULL;
  629. int myrc = ldap_url_parse(referrals_to_set[ii], &lud);
  630. /* see if the dn is already in the referral URL */
  631. if (myrc == LDAP_URL_ERR_NODN || !lud || !lud->lud_dn) {
  632. /* add the dn */
  633. int len = strlen(referrals_to_set[ii]);
  634. const char *cdn = slapi_sdn_get_dn(repl_root_sdn);
  635. char *tmpref = NULL;
  636. int need_slash = 0;
  637. if (referrals_to_set[ii][len-1] != '/') {
  638. len++; /* add another one for the slash */
  639. need_slash = 1;
  640. }
  641. len += (strlen(cdn) * 3) + 2; /* 3 for %HH possible per char */
  642. tmpref = slapi_ch_malloc(len);
  643. sprintf(tmpref, "%s%s", referrals_to_set[ii], (need_slash ? "/" : ""));
  644. strcat_escaped(tmpref, cdn);
  645. slapi_ch_free((void **)&referrals_to_set[ii]);
  646. referrals_to_set[ii] = tmpref;
  647. }
  648. if (lud)
  649. ldap_free_urldesc(lud);
  650. }
  651. if (!referrals_to_set) { /* deleting referrals */
  652. /* Set state before */
  653. if (!chain_on_update) {
  654. slapi_mtn_set_state(repl_root_sdn, (char *)mtn_state);
  655. }
  656. /* We should delete referral only if we want to set the
  657. replica database in backend state mode */
  658. /* if chain on update mode, go ahead and set the referrals anyway */
  659. if (strcasecmp(mtn_state, STATE_BACKEND) == 0 || chain_on_update) {
  660. rc = slapi_mtn_set_referral(repl_root_sdn, referrals_to_set);
  661. if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
  662. /* we will get no such attribute (16) if we try to set the referrals to NULL if
  663. there are no referrals - not an error */
  664. rc = LDAP_SUCCESS;
  665. }
  666. }
  667. } else { /* Replacing */
  668. rc = slapi_mtn_set_referral(repl_root_sdn, referrals_to_set);
  669. if (rc == LDAP_SUCCESS && !chain_on_update){
  670. slapi_mtn_set_state(repl_root_sdn, (char *)mtn_state);
  671. }
  672. }
  673. if (rc != LDAP_SUCCESS) {
  674. char ebuf[BUFSIZ];
  675. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "repl_set_mtn_referrals: could "
  676. "not set referrals for replica %s: %d\n",
  677. escape_string(slapi_sdn_get_dn(repl_root_sdn), ebuf), rc);
  678. }
  679. charray_free(referrals_to_set);
  680. return;
  681. }
  682. /*
  683. * This function allows to use a local backend in conjunction with
  684. * a chaining backend
  685. * The local ldbm backend is the replication consumer database
  686. * (e.g. on a hub or consumer) - it is read-only except for supplier updates
  687. * The chaining backend points to the supplier(s)
  688. * This distribution logic forwards the update request to the chaining
  689. * backend, and sends the search request to the local ldbm database
  690. *
  691. * To be able to use it one must define one ldbm backend and one chaining
  692. * backend in the mapping tree node - the ldbm backend will usually
  693. * already be there
  694. *
  695. */
  696. int
  697. repl_chain_on_update(Slapi_PBlock *pb, Slapi_DN * target_dn,
  698. char **mtn_be_names, int be_count,
  699. Slapi_DN * node_dn, int *mtn_be_states)
  700. {
  701. char * requestor_dn;
  702. unsigned long op_type;
  703. Slapi_Operation *op;
  704. int repl_op = 0;
  705. int local_backend = -1; /* index of local backend */
  706. int chaining_backend = -1; /* index of chain backend */
  707. PRBool local_online = PR_FALSE; /* true if the local db is online */
  708. PRBool chain_online = PR_FALSE; /* true if the chain db is online */
  709. int ii;
  710. int opid, connid;
  711. slapi_pblock_get(pb, SLAPI_CONN_ID, &connid);
  712. slapi_pblock_get(pb, SLAPI_OPERATION_ID, &opid);
  713. /* first, we have to decide which backend is the local backend
  714. * and which is the chaining one
  715. * also find out if any are not online (e.g. during import)
  716. */
  717. for (ii = 0; ii < be_count; ++ii)
  718. {
  719. Slapi_Backend *be = slapi_be_select_by_instance_name(mtn_be_names[ii]);
  720. if (slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA))
  721. {
  722. chaining_backend = ii;
  723. if (mtn_be_states[ii] == SLAPI_BE_STATE_ON)
  724. {
  725. chain_online = PR_TRUE;
  726. }
  727. }
  728. else
  729. {
  730. local_backend = ii;
  731. if (mtn_be_states[ii] == SLAPI_BE_STATE_ON)
  732. {
  733. local_online = PR_TRUE;
  734. }
  735. }
  736. /*
  737. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%d op=%d be "
  738. "%s is the %s backend and is %s\n",
  739. connid, opid,
  740. mtn_be_names[ii], (chaining_backend == ii) ? "chaining" : "local",
  741. (mtn_be_states[ii] == SLAPI_BE_STATE_ON) ? "online" : "offline");
  742. */
  743. }
  744. /* if no chaining backends are defined, just use the local one */
  745. if (chaining_backend == -1) {
  746. return local_backend;
  747. }
  748. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  749. /* All internal operations go to the local backend */
  750. if (operation_is_flag_set(op, OP_FLAG_INTERNAL)) {
  751. return local_backend;
  752. }
  753. /* Check the operation type
  754. * read-only operation will go to the local backend if online
  755. */
  756. op_type = slapi_op_get_type(op);
  757. if (local_online &&
  758. ((op_type == SLAPI_OPERATION_SEARCH) ||
  759. (op_type == SLAPI_OPERATION_BIND) ||
  760. (op_type == SLAPI_OPERATION_UNBIND) ||
  761. (op_type == SLAPI_OPERATION_COMPARE))) {
  762. /*
  763. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%d op=%d op is "
  764. "%d: using local backend\n",
  765. connid, opid, op_type);
  766. */
  767. return local_backend;
  768. }
  769. /* if the operation is done by directory manager
  770. * use local database even for updates because it is an administrative
  771. * operation
  772. * remarks : one could also use an update DN in the same way
  773. * to let update operation go to the local backend when they are done
  774. * by specific administrator user but let all the other user
  775. * go to the read-write replica
  776. * also - I don't think it is possible to chain directory manager
  777. */
  778. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &requestor_dn);
  779. if (slapi_dn_isroot(requestor_dn)) {
  780. /*
  781. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%d op=%d requestor "
  782. "is root: using local backend\n", connid, opid);
  783. */
  784. return local_backend;
  785. }
  786. /* if the operation is a replicated operation
  787. * use local database even for updates to avoid infinite loops
  788. */
  789. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  790. if (repl_op) {
  791. /*
  792. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%d op=%d op is "
  793. "replicated: using local backend\n", connid, opid);
  794. */
  795. return local_backend;
  796. }
  797. /* all other case (update while not directory manager) :
  798. * or any normal non replicated client operation while local is disabled (import) :
  799. * use the chaining backend
  800. */
  801. /*
  802. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%d op=%d using "
  803. "chaining backend\n", connid, opid);
  804. */
  805. return chaining_backend;
  806. }
  807. int
  808. repl_enable_chain_on_update(Slapi_DN *suffix)
  809. {
  810. /* Submit a Modify operation to add the distribution function to the mapping tree
  811. node for the given suffix */
  812. slapi_mods smods;
  813. Slapi_Operation *op = NULL;
  814. int operation_result;
  815. Slapi_PBlock *pb= slapi_pblock_new();
  816. char *mtnnodedn;
  817. slapi_mods_init(&smods,2);
  818. /* need path and file name of the replication plugin here */
  819. slapi_mods_add_string(&smods, LDAP_MOD_ADD, "nsslapd-distribution-plugin", replpluginpath);
  820. slapi_mods_add_string(&smods, LDAP_MOD_ADD, "nsslapd-distribution-funct", "repl_chain_on_update");
  821. /* need DN of mapping tree node here */
  822. mtnnodedn = slapi_get_mapping_tree_node_configdn(suffix);
  823. slapi_modify_internal_set_pb(
  824. pb,
  825. mtnnodedn,
  826. slapi_mods_get_ldapmods_byref(&smods), /* JCM cast */
  827. NULL, /*Controls*/
  828. NULL, /*uniqueid*/
  829. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  830. 0);
  831. slapi_modify_internal_pb(pb);
  832. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &operation_result);
  833. slapi_ch_free_string(&mtnnodedn);
  834. slapi_pblock_destroy(pb);
  835. switch(operation_result)
  836. {
  837. case LDAP_SUCCESS:
  838. /* OK, everything is fine. */
  839. break;
  840. default:
  841. PR_ASSERT(0); /* JCMREPL FOR DEBUGGING */
  842. }
  843. slapi_mods_done(&smods);
  844. return operation_result;
  845. }
  846. int
  847. repl_disable_chain_on_update(Slapi_DN *suffix)
  848. {
  849. /* Submit a Modify operation to remove the distribution function from the mapping tree
  850. node for the given suffix */
  851. slapi_mods smods;
  852. Slapi_Operation *op = NULL;
  853. int operation_result;
  854. Slapi_PBlock *pb= slapi_pblock_new();
  855. char *mtnnodedn;
  856. slapi_mods_init(&smods,2);
  857. slapi_mods_add_modbvps(&smods, LDAP_MOD_DELETE, "nsslapd-distribution-plugin", NULL);
  858. slapi_mods_add_modbvps(&smods, LDAP_MOD_DELETE, "nsslapd-distribution-funct", NULL);
  859. /* need DN of mapping tree node here */
  860. mtnnodedn = slapi_get_mapping_tree_node_configdn(suffix);
  861. slapi_modify_internal_set_pb(
  862. pb,
  863. mtnnodedn,
  864. slapi_mods_get_ldapmods_byref(&smods), /* JCM cast */
  865. NULL, /*Controls*/
  866. NULL, /*uniqueid*/
  867. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  868. 0);
  869. slapi_modify_internal_pb(pb);
  870. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &operation_result);
  871. slapi_ch_free_string(&mtnnodedn);
  872. slapi_pblock_destroy(pb);
  873. switch(operation_result)
  874. {
  875. case LDAP_SUCCESS:
  876. /* OK, everything is fine. */
  877. break;
  878. default:
  879. PR_ASSERT(0); /* JCMREPL FOR DEBUGGING */
  880. }
  881. slapi_mods_done(&smods);
  882. return operation_result;
  883. }
  884. static PRBool
  885. is_chain_on_update_setup(const Slapi_DN *replroot)
  886. {
  887. /* Do an internal search of the mapping tree node to see if chain on update is setup
  888. for this replica
  889. - has two backends
  890. - has a distribution function
  891. - has a distribution plugin
  892. - one of the backends is a ldbm database
  893. - one of the backends is a chaining database
  894. */
  895. static char* attrs[] = { "nsslapd-backend",
  896. "nsslapd-distribution-plugin", "nsslapd-distribution-funct",
  897. NULL };
  898. int operation_result;
  899. Slapi_PBlock *pb= slapi_pblock_new();
  900. char *mtnnodedn = slapi_get_mapping_tree_node_configdn(replroot);
  901. PRBool retval = PR_FALSE;
  902. slapi_search_internal_set_pb(
  903. pb,
  904. mtnnodedn,
  905. LDAP_SCOPE_BASE,
  906. "objectclass=*",
  907. attrs, /*attrs*/
  908. 0, /*attrsonly*/
  909. NULL, /*Controls*/
  910. NULL, /*uniqueid*/
  911. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  912. 0);
  913. slapi_search_internal_pb(pb);
  914. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &operation_result);
  915. switch(operation_result)
  916. {
  917. case LDAP_SUCCESS:
  918. {
  919. Slapi_Entry **entries= NULL;
  920. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  921. if(entries!=NULL && entries[0]!=NULL)
  922. {
  923. Slapi_Entry *e = entries[0];
  924. char **backends = slapi_entry_attr_get_charray(e, "nsslapd-backend");
  925. char *plg = slapi_entry_attr_get_charptr(e, "nsslapd-distribution-plugin");
  926. char *func = slapi_entry_attr_get_charptr(e, "nsslapd-distribution-funct");
  927. if (backends && backends[0] && backends[1] && plg && func)
  928. {
  929. /* all the necessary attrs are present - check to see if we
  930. have one chaining backend */
  931. Slapi_Backend *be0 = slapi_be_select_by_instance_name(backends[0]);
  932. Slapi_Backend *be1 = slapi_be_select_by_instance_name(backends[1]);
  933. PRBool foundchain0 = slapi_be_is_flag_set(be0,SLAPI_BE_FLAG_REMOTE_DATA);
  934. PRBool foundchain1 = slapi_be_is_flag_set(be1,SLAPI_BE_FLAG_REMOTE_DATA);
  935. retval = (foundchain0 || foundchain1) &&
  936. !(foundchain0 && foundchain1); /* 1 (but not both) backend is chaining */
  937. }
  938. slapi_ch_array_free(backends);
  939. slapi_ch_free_string(&plg);
  940. slapi_ch_free_string(&func);
  941. }
  942. else /* could not find mapping tree entry - assume not set up */
  943. {
  944. }
  945. }
  946. break;
  947. default: /* search error - assume not set up */
  948. break;
  949. }
  950. slapi_ch_free_string(&mtnnodedn);
  951. slapi_free_search_results_internal(pb);
  952. slapi_pblock_destroy(pb);
  953. return retval;
  954. }
  955. void
  956. repl_set_repl_plugin_path(const char *path)
  957. {
  958. replpluginpath = slapi_ch_strdup(path);
  959. }