1
0

replutil.c 31 KB

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