replutil.c 35 KB

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