replutil.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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] = repl_changenumber;
  59. cleattrs[2] = repl_targetdn;
  60. cleattrs[3] = repl_changetype;
  61. cleattrs[4] = repl_newrdn;
  62. cleattrs[5] = repl_deleteoldrdn;
  63. cleattrs[6] = repl_changes;
  64. cleattrs[7] = repl_newsuperior;
  65. cleattrs[8] = repl_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. slapi_mod_init(&mod, 0);
  309. } else {
  310. /* need to cleanup */
  311. slapi_mod_done(&mod);
  312. }
  313. line = ldif_getline(&next);
  314. break;
  315. }
  316. type = bv_null;
  317. value = bv_null;
  318. rc = slapi_ldif_parse_line(line, &type, &value, &freeval);
  319. if (rc != 0) {
  320. /* ONREPL - log warning */
  321. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  322. "Failed to parse the ldif line.\n");
  323. continue;
  324. }
  325. if (strncasecmp(type.bv_val, "add", type.bv_len) == 0) {
  326. slapi_mod_set_operation(&mod, LDAP_MOD_ADD | LDAP_MOD_BVALUES);
  327. } else if (strncasecmp(type.bv_val, "delete", type.bv_len) == 0) {
  328. slapi_mod_set_operation(&mod, LDAP_MOD_DELETE | LDAP_MOD_BVALUES);
  329. } else if (strncasecmp(type.bv_val, "replace", type.bv_len) == 0) {
  330. slapi_mod_set_operation(&mod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  331. } else /* attr: value pair */
  332. {
  333. /* adding first value */
  334. if (slapi_mod_get_type(&mod) == NULL) {
  335. slapi_mod_set_type(&mod, type.bv_val);
  336. }
  337. slapi_mod_add_value(&mod, &value);
  338. }
  339. if (freeval) {
  340. slapi_ch_free_string(&value.bv_val);
  341. }
  342. line = ldif_getline(&next);
  343. }
  344. slapi_mod_done(&mod);
  345. }
  346. return mods;
  347. }
  348. static void *g_plg_identity[PLUGIN_MAX];
  349. void *
  350. repl_get_plugin_identity(int pluginID)
  351. {
  352. PR_ASSERT(pluginID < PLUGIN_MAX);
  353. return g_plg_identity[pluginID];
  354. }
  355. void
  356. repl_set_plugin_identity(int pluginID, void *identity)
  357. {
  358. PR_ASSERT(pluginID < PLUGIN_MAX);
  359. g_plg_identity[pluginID] = identity;
  360. }
  361. /* this function validates operation parameters */
  362. PRBool
  363. IsValidOperation(const slapi_operation_parameters *op)
  364. {
  365. if (op == NULL) {
  366. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  367. "IsValidOperation - NULL operation\n");
  368. return PR_FALSE;
  369. }
  370. if (op->csn == NULL) {
  371. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  372. "IsValidOperation - NULL operation CSN\n");
  373. return PR_FALSE;
  374. }
  375. if (op->target_address.uniqueid == NULL) {
  376. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  377. "IsValidOperation - NULL entry uniqueid\n");
  378. return PR_FALSE;
  379. }
  380. if (op->target_address.sdn == NULL) {
  381. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  382. "IsValidOperation - NULL entry DN\n");
  383. return PR_FALSE;
  384. }
  385. switch (op->operation_type) {
  386. case SLAPI_OPERATION_ADD:
  387. if (op->p.p_add.target_entry == NULL) {
  388. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  389. "IsValidOperation - NULL entry for add operation\n");
  390. return PR_FALSE;
  391. } else
  392. break;
  393. case SLAPI_OPERATION_MODIFY:
  394. if (op->p.p_modify.modify_mods == NULL ||
  395. op->p.p_modify.modify_mods[0] == NULL) {
  396. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  397. "IsValidOperation - NULL mods for modify operation\n");
  398. return PR_FALSE;
  399. } else
  400. break;
  401. case SLAPI_OPERATION_MODRDN:
  402. if (op->p.p_modrdn.modrdn_mods == NULL ||
  403. op->p.p_modrdn.modrdn_mods[0] == NULL) {
  404. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  405. "IsValidOperation - NULL mods for modrdn operation\n");
  406. return PR_FALSE;
  407. }
  408. if (op->p.p_modrdn.modrdn_newrdn == NULL) {
  409. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  410. "IsValidOperation - NULL new rdn for modrdn operation\n");
  411. return PR_FALSE;
  412. } else
  413. break;
  414. case SLAPI_OPERATION_DELETE:
  415. break;
  416. default:
  417. return PR_FALSE;
  418. }
  419. return PR_TRUE;
  420. }
  421. const char *
  422. map_repl_root_to_dbid(Slapi_DN *repl_root)
  423. {
  424. const char *return_ptr;
  425. PR_ASSERT(NULL != repl_root);
  426. if (NULL != repl_root) {
  427. /* XXXggood get per-database ID here, when code available */
  428. }
  429. return_ptr = get_server_dataversion(); /* XXXggood temporary hack until we have per-database instance dbids */
  430. return return_ptr;
  431. }
  432. PRBool
  433. is_ruv_tombstone_entry(Slapi_Entry *e)
  434. {
  435. char *dn;
  436. char *match;
  437. PR_ASSERT(e);
  438. dn = slapi_entry_get_dn(e);
  439. PR_ASSERT(dn);
  440. /* tombstone has rdn: nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff */
  441. match = strstr(dn, RUV_STORAGE_ENTRY_UNIQUEID);
  442. return (match != NULL);
  443. }
  444. LDAPControl *
  445. create_managedsait_control()
  446. {
  447. LDAPControl *control;
  448. control = (LDAPControl *)slapi_ch_malloc(sizeof(LDAPControl));
  449. control->ldctl_oid = slapi_ch_strdup(LDAP_CONTROL_MANAGEDSAIT);
  450. control->ldctl_value.bv_val = NULL;
  451. control->ldctl_value.bv_len = 0;
  452. control->ldctl_iscritical = '\0';
  453. return control;
  454. }
  455. LDAPControl *
  456. create_backend_control(Slapi_DN *sdn)
  457. {
  458. LDAPControl *control = NULL;
  459. const char *be_name = slapi_mtn_get_backend_name(sdn);
  460. if (NULL != be_name) {
  461. control = (LDAPControl *)slapi_ch_malloc(sizeof(LDAPControl));
  462. control->ldctl_oid = slapi_ch_strdup("2.16.840.1.113730.3.4.14");
  463. control->ldctl_value.bv_val = slapi_ch_strdup(be_name);
  464. control->ldctl_value.bv_len = strlen(be_name);
  465. control->ldctl_iscritical = 1;
  466. }
  467. return control;
  468. }
  469. /*
  470. * HREF_CHAR_ACCEPTABLE was copied from slapd/referral.c
  471. * which was copied from libldap/tmplout.c.
  472. */
  473. /* Note: an identical function is in ../../slapd/referral.c */
  474. #define HREF_CHAR_ACCEPTABLE(c) ((c >= '-' && c <= '9') || \
  475. (c >= '@' && c <= 'Z') || \
  476. (c == '_') || \
  477. (c >= 'a' && c <= 'z'))
  478. /*
  479. * Function: strcat_escaped
  480. *
  481. * Returns: nothing
  482. *
  483. * Description: Appends string s2 to s1, URL-escaping (%HH) unsafe
  484. * characters in s2 as appropriate. This function was
  485. * copied from slapd/referral.c.
  486. * which was copied from libldap/tmplout.c.
  487. * added const qualifier
  488. *
  489. * Author: MCS
  490. */
  491. /*
  492. * append s2 to s1, URL-escaping (%HH) unsafe characters
  493. */
  494. /* Note: an identical function is in ../../slapd/referral.c */
  495. static void
  496. strcat_escaped(char *s1, const char *s2)
  497. {
  498. char *p, *q;
  499. char *hexdig = "0123456789ABCDEF";
  500. p = s1 + strlen(s1);
  501. for (q = (char *)s2; *q != '\0'; ++q) {
  502. if (HREF_CHAR_ACCEPTABLE(*q)) {
  503. *p++ = *q;
  504. } else {
  505. *p++ = '%';
  506. *p++ = hexdig[0x0F & ((*(unsigned char *)q) >> 4)];
  507. *p++ = hexdig[0x0F & *q];
  508. }
  509. }
  510. *p = '\0';
  511. }
  512. /*
  513. This function appends the replication root to the purl referrals found
  514. in the given ruv and the other given referrals, merges the lists, and sets the
  515. referrals in the mapping tree node corresponding to the given sdn, which is the
  516. repl_root
  517. This function also sets the mapping tree state (e.g. disabled, backend, referral,
  518. referral on update) - the mapping tree has very specific rules about how states
  519. can be set in the presence of referrals - specifically:
  520. 1) the nsslapd-referral attribute must be set before changing the state to referral
  521. or referral on update
  522. 2) the state must be set to backend or disabled before removing referrals
  523. */
  524. void
  525. repl_set_mtn_state_and_referrals(
  526. const Slapi_DN *repl_root_sdn,
  527. const char *mtn_state,
  528. const RUV *ruv,
  529. char **ruv_referrals,
  530. char **other_referrals)
  531. {
  532. int rc = 0;
  533. int ii = 0;
  534. char **referrals_to_set = NULL;
  535. PRBool chain_on_update = is_chain_on_update_setup(repl_root_sdn);
  536. if (NULL == mtn_state) {
  537. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  538. "repl_set_mtn_state_and_referrals - Cannot set NULL state.\n");
  539. return;
  540. }
  541. /* Fix for blackflag bug 601440: We want the new behaviour of DS,
  542. ** going forward, to now be that if the nsds5replicareferral attrib
  543. ** has values, it should be the only values in nsslapd-referral (as
  544. ** opposed to older behaviour of concatenating with RUV-based
  545. ** referrals). [email protected]
  546. */
  547. if (other_referrals) {
  548. /* use the referrals passed in, instead of RUV-based referrals */
  549. charray_merge(&referrals_to_set, other_referrals, 1);
  550. /* Do copies. referrals is freed at the end */
  551. } else {
  552. /* use the referrals from the RUV */
  553. ruv_referrals = (ruv ? ruv_get_referrals(ruv) : ruv_referrals);
  554. if (ruv_referrals) {
  555. charray_merge(&referrals_to_set, ruv_referrals, 1);
  556. if (ruv) /* free referrals from ruv_get_referrals() */
  557. charray_free(ruv_referrals);
  558. }
  559. }
  560. /* next, add the repl root dn to each referral if not present */
  561. for (ii = 0; referrals_to_set && referrals_to_set[ii]; ++ii) {
  562. LDAPURLDesc *lud = NULL;
  563. (void)slapi_ldap_url_parse(referrals_to_set[ii], &lud, 0, NULL);
  564. /* see if the dn is already in the referral URL */
  565. if (!lud || !lud->lud_dn) {
  566. /* add the dn */
  567. int len = strlen(referrals_to_set[ii]);
  568. const char *cdn = slapi_sdn_get_dn(repl_root_sdn);
  569. char *tmpref = NULL;
  570. int need_slash = 0;
  571. if (referrals_to_set[ii][len - 1] != '/') {
  572. len++; /* add another one for the slash */
  573. need_slash = 1;
  574. }
  575. len += (strlen(cdn) * 3) + 2; /* 3 for %HH possible per char */
  576. tmpref = slapi_ch_malloc(len);
  577. sprintf(tmpref, "%s%s", referrals_to_set[ii], (need_slash ? "/" : ""));
  578. strcat_escaped(tmpref, cdn);
  579. slapi_ch_free((void **)&referrals_to_set[ii]);
  580. referrals_to_set[ii] = tmpref;
  581. }
  582. if (lud)
  583. ldap_free_urldesc(lud);
  584. }
  585. if (!referrals_to_set) { /* deleting referrals */
  586. /* Set state before */
  587. if (!chain_on_update) {
  588. slapi_mtn_set_state(repl_root_sdn, (char *)mtn_state);
  589. }
  590. /* We should delete referral only if we want to set the
  591. replica database in backend state mode */
  592. if (strcasecmp(mtn_state, STATE_BACKEND) == 0 || chain_on_update) {
  593. rc = slapi_mtn_set_referral(repl_root_sdn, referrals_to_set);
  594. if (rc == LDAP_NO_SUCH_ATTRIBUTE) {
  595. /* we will get no such attribute (16) if we try to set the referrals to NULL if
  596. there are no referrals - not an error */
  597. rc = LDAP_SUCCESS;
  598. }
  599. }
  600. } else { /* Replacing */
  601. rc = slapi_mtn_set_referral(repl_root_sdn, referrals_to_set);
  602. if (rc == LDAP_SUCCESS && !chain_on_update) {
  603. slapi_mtn_set_state(repl_root_sdn, (char *)mtn_state);
  604. }
  605. }
  606. if (rc != LDAP_SUCCESS && rc != LDAP_TYPE_OR_VALUE_EXISTS) {
  607. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "repl_set_mtn_state_and_referrals - "
  608. "Could not set referrals for replica %s: %d\n",
  609. slapi_sdn_get_dn(repl_root_sdn), rc);
  610. }
  611. charray_free(referrals_to_set);
  612. return;
  613. }
  614. /*
  615. * This function allows to use a local backend in conjunction with
  616. * a chaining backend
  617. * The local ldbm backend is the replication consumer database
  618. * (e.g. on a hub or consumer) - it is read-only except for supplier updates
  619. * The chaining backend points to the supplier(s)
  620. * This distribution logic forwards the update request to the chaining
  621. * backend, and sends the search request to the local ldbm database
  622. *
  623. * To be able to use it one must define one ldbm backend and one chaining
  624. * backend in the mapping tree node - the ldbm backend will usually
  625. * already be there
  626. *
  627. */
  628. int
  629. 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)
  630. {
  631. char *requestor_dn;
  632. unsigned long op_type;
  633. Slapi_Operation *op;
  634. int repl_op = 0;
  635. int local_backend = -1; /* index of local backend */
  636. int chaining_backend = -1; /* index of chain backend */
  637. #ifdef DEBUG_CHAIN_ON_UPDATE
  638. int is_internal = 0;
  639. #endif
  640. PRBool local_online = PR_FALSE; /* true if the local db is online */
  641. int ii;
  642. int opid;
  643. #ifdef DEBUG_CHAIN_ON_UPDATE
  644. PRUint64 connid = 0;
  645. #endif
  646. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  647. #ifdef DEBUG_CHAIN_ON_UPDATE
  648. if (operation_is_flag_set(op, OP_FLAG_INTERNAL)) {
  649. is_internal = 1;
  650. } else {
  651. slapi_pblock_get(pb, SLAPI_CONN_ID, &connid);
  652. }
  653. #endif
  654. slapi_pblock_get(pb, SLAPI_OPERATION_ID, &opid);
  655. /* first, we have to decide which backend is the local backend
  656. * and which is the chaining one
  657. * also find out if any are not online (e.g. during import)
  658. */
  659. for (ii = 0; ii < be_count; ++ii) {
  660. Slapi_Backend *be = slapi_be_select_by_instance_name(mtn_be_names[ii]);
  661. if (be) {
  662. if (slapi_be_is_flag_set(be, SLAPI_BE_FLAG_REMOTE_DATA)) {
  663. chaining_backend = ii;
  664. } else {
  665. local_backend = ii;
  666. if (mtn_be_states[ii] == SLAPI_BE_STATE_ON) {
  667. local_online = PR_TRUE;
  668. }
  669. }
  670. #ifdef DEBUG_CHAIN_ON_UPDATE
  671. if (is_internal) {
  672. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=-1 op=%d be "
  673. "%s is the %s backend and is %s\n",
  674. opid,
  675. mtn_be_names[ii], (chaining_backend == ii) ? "chaining" : "local",
  676. (mtn_be_states[ii] == SLAPI_BE_STATE_ON) ? "online" : "offline");
  677. } else {
  678. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=%" PRIu64 " op=%d be "
  679. "%s is the %s backend and is %s\n",
  680. connid, opid,
  681. mtn_be_names[ii], (chaining_backend == ii) ? "chaining" : "local",
  682. (mtn_be_states[ii] == SLAPI_BE_STATE_ON) ? "online" : "offline");
  683. }
  684. #endif
  685. } else {
  686. /* A chaining backend will not be found during import. We will just return the
  687. * local backend in this case, which seems like the right thing to do to allow
  688. * offline replication initialization. */
  689. #ifdef DEBUG_CHAIN_ON_UPDATE
  690. if (is_internal) {
  691. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=-1 op=%d be "
  692. "%s not found. Assuming it is the chaining backend and we are doing an import.\n",
  693. opid, mtn_be_names[ii]);
  694. } else {
  695. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=%" PRIu64 " op=%d be "
  696. "%s not found. Assuming it is the chaining backend and we are doing an import.\n",
  697. connid, opid, mtn_be_names[ii]);
  698. }
  699. #endif
  700. }
  701. }
  702. /* if no chaining backends are defined, just use the local one */
  703. if (chaining_backend == -1) {
  704. return local_backend;
  705. }
  706. /* All internal operations go to the local backend */
  707. if (operation_is_flag_set(op, OP_FLAG_INTERNAL)) {
  708. return local_backend;
  709. }
  710. /* Check the operation type
  711. * read-only operation will go to the local backend if online
  712. */
  713. op_type = slapi_op_get_type(op);
  714. if (local_online &&
  715. ((op_type == SLAPI_OPERATION_SEARCH) ||
  716. (op_type == SLAPI_OPERATION_UNBIND) ||
  717. (op_type == SLAPI_OPERATION_COMPARE))) {
  718. #ifdef DEBUG_CHAIN_ON_UPDATE
  719. if (is_internal) {
  720. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=-1 op=%d op is "
  721. "%d: using local backend\n",
  722. opid, op_type);
  723. } else {
  724. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=%" PRIu64 " op=%d op is "
  725. "%d: using local backend\n",
  726. connid, opid, op_type);
  727. }
  728. #endif
  729. return local_backend;
  730. }
  731. /* if the operation is done by directory manager
  732. * use local database even for updates because it is an administrative
  733. * operation
  734. * remarks : one could also use an update DN in the same way
  735. * to let update operation go to the local backend when they are done
  736. * by specific administrator user but let all the other user
  737. * go to the read-write replica
  738. * also - I don't think it is possible to chain directory manager
  739. */
  740. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &requestor_dn);
  741. if (slapi_dn_isroot(requestor_dn)) {
  742. #ifdef DEBUG_CHAIN_ON_UPDATE
  743. if (is_internal) {
  744. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=-1 op=%d requestor "
  745. "is root - using local backend\n",
  746. opid);
  747. } else {
  748. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=%" PRIu64 " op=%d requestor "
  749. "is root: using local backend\n",
  750. connid, opid);
  751. }
  752. #endif
  753. if (root_mode == CHAIN_ROOT_UPDATE_LOCAL)
  754. return local_backend;
  755. else if (root_mode == CHAIN_ROOT_UPDATE_REJECT)
  756. return (SLAPI_BE_NO_BACKEND);
  757. else if (root_mode == CHAIN_ROOT_UPDATE_REFERRAL)
  758. return (SLAPI_BE_REMOTE_BACKEND);
  759. }
  760. /* if the operation is a replicated operation
  761. * use local database even for updates to avoid infinite loops
  762. */
  763. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  764. if (repl_op) {
  765. #ifdef DEBUG_CHAIN_ON_UPDATE
  766. if (is_internal) {
  767. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=-1 op=%d op is "
  768. "replicated: using local backend\n",
  769. opid);
  770. } else {
  771. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=%" PRIu64 " op=%d op is "
  772. "replicated: using local backend\n",
  773. connid, opid);
  774. }
  775. #endif
  776. return local_backend;
  777. }
  778. /* if using global password policy, chain the bind request so that the
  779. supplier can update and replicate the password policy op attrs */
  780. if (op_type == SLAPI_OPERATION_BIND) {
  781. extern int config_get_pw_is_global_policy(void);
  782. if (!config_get_pw_is_global_policy()) {
  783. #ifdef DEBUG_CHAIN_ON_UPDATE
  784. if (is_internal) {
  785. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=-1 op=%d using "
  786. "local backend for local password policy\n",
  787. opid);
  788. } else {
  789. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=%" PRIu64 " op=%d using "
  790. "local backend for local password policy\n",
  791. connid, opid);
  792. }
  793. #endif
  794. return local_backend;
  795. }
  796. }
  797. /* all other cases :
  798. * or any normal non replicated client operation while local is disabled (import) :
  799. * use the chaining backend
  800. */
  801. #ifdef DEBUG_CHAIN_ON_UPDATE
  802. if (is_internal) {
  803. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update - conn=-1 op=%d using "
  804. "chaining backend\n",
  805. opid);
  806. } else {
  807. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%" PRIu64 " op=%d using "
  808. "chaining backend\n",
  809. connid, opid);
  810. }
  811. #endif
  812. return chaining_backend;
  813. }
  814. int
  815. repl_enable_chain_on_update(Slapi_DN *suffix)
  816. {
  817. /* Submit a Modify operation to add the distribution function to the mapping tree
  818. node for the given suffix */
  819. slapi_mods smods;
  820. int operation_result;
  821. Slapi_PBlock *pb = slapi_pblock_new();
  822. Slapi_DN *mtnnodesdn;
  823. slapi_mods_init(&smods, 2);
  824. /* need path and file name of the replication plugin here */
  825. slapi_mods_add_string(&smods, LDAP_MOD_ADD, "nsslapd-distribution-plugin", replpluginpath);
  826. slapi_mods_add_string(&smods, LDAP_MOD_ADD, "nsslapd-distribution-funct", "repl_chain_on_update");
  827. /* need DN of mapping tree node here */
  828. mtnnodesdn = slapi_get_mapping_tree_node_configsdn(suffix);
  829. slapi_modify_internal_set_pb_ext(
  830. pb,
  831. mtnnodesdn,
  832. slapi_mods_get_ldapmods_byref(&smods), /* JCM cast */
  833. NULL, /*Controls*/
  834. NULL, /*uniqueid*/
  835. repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
  836. 0);
  837. slapi_modify_internal_pb(pb);
  838. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &operation_result);
  839. slapi_sdn_free(&mtnnodesdn);
  840. slapi_pblock_destroy(pb);
  841. switch (operation_result) {
  842. case LDAP_SUCCESS:
  843. /* OK, everything is fine. */
  844. break;
  845. default:
  846. PR_ASSERT(0); /* JCMREPL FOR DEBUGGING */
  847. }
  848. slapi_mods_done(&smods);
  849. return operation_result;
  850. }
  851. int
  852. repl_disable_chain_on_update(Slapi_DN *suffix)
  853. {
  854. /* Submit a Modify operation to remove the distribution function from the mapping tree
  855. node for the given suffix */
  856. slapi_mods smods;
  857. int operation_result;
  858. Slapi_PBlock *pb = slapi_pblock_new();
  859. Slapi_DN *mtnnodesdn;
  860. slapi_mods_init(&smods, 2);
  861. slapi_mods_add_modbvps(&smods, LDAP_MOD_DELETE, "nsslapd-distribution-plugin", NULL);
  862. slapi_mods_add_modbvps(&smods, LDAP_MOD_DELETE, "nsslapd-distribution-funct", NULL);
  863. /* need DN of mapping tree node here */
  864. mtnnodesdn = slapi_get_mapping_tree_node_configsdn(suffix);
  865. slapi_modify_internal_set_pb_ext(
  866. pb,
  867. mtnnodesdn,
  868. slapi_mods_get_ldapmods_byref(&smods), /* JCM cast */
  869. NULL, /*Controls*/
  870. NULL, /*uniqueid*/
  871. repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
  872. 0);
  873. slapi_modify_internal_pb(pb);
  874. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &operation_result);
  875. slapi_sdn_free(&mtnnodesdn);
  876. slapi_pblock_destroy(pb);
  877. switch (operation_result) {
  878. case LDAP_SUCCESS:
  879. /* OK, everything is fine. */
  880. break;
  881. default:
  882. PR_ASSERT(0); /* JCMREPL FOR DEBUGGING */
  883. }
  884. slapi_mods_done(&smods);
  885. return operation_result;
  886. }
  887. static PRBool
  888. is_chain_on_update_setup(const Slapi_DN *replroot)
  889. {
  890. /* Do an internal search of the mapping tree node to see if chain on update is setup
  891. for this replica
  892. - has two backends
  893. - has a distribution function
  894. - has a distribution plugin
  895. - one of the backends is a ldbm database
  896. - one of the backends is a chaining database
  897. */
  898. static char *attrs[] = {"nsslapd-backend",
  899. "nsslapd-distribution-plugin", "nsslapd-distribution-funct",
  900. NULL};
  901. int operation_result;
  902. Slapi_PBlock *pb = slapi_pblock_new();
  903. char *mtnnodedn = slapi_get_mapping_tree_node_configdn(replroot);
  904. PRBool retval = PR_FALSE;
  905. slapi_search_internal_set_pb(
  906. pb,
  907. mtnnodedn,
  908. LDAP_SCOPE_BASE,
  909. "objectclass=*",
  910. attrs, /*attrs*/
  911. 0, /*attrsonly*/
  912. NULL, /*Controls*/
  913. NULL, /*uniqueid*/
  914. repl_get_plugin_identity(PLUGIN_MULTISUPPLIER_REPLICATION),
  915. 0);
  916. slapi_search_internal_pb(pb);
  917. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &operation_result);
  918. switch (operation_result) {
  919. case LDAP_SUCCESS: {
  920. Slapi_Entry **entries = NULL;
  921. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  922. if (entries != NULL && entries[0] != NULL) {
  923. Slapi_Entry *e = entries[0];
  924. char **backends = slapi_entry_attr_get_charray(e, "nsslapd-backend");
  925. const char *plg = slapi_entry_attr_get_ref(e, "nsslapd-distribution-plugin");
  926. const char *func = slapi_entry_attr_get_ref(e, "nsslapd-distribution-funct");
  927. if (backends && backends[0] && backends[1] && plg && func) {
  928. /* all the necessary attrs are present - check to see if we
  929. have one chaining backend */
  930. Slapi_Backend *be0 = slapi_be_select_by_instance_name(backends[0]);
  931. Slapi_Backend *be1 = slapi_be_select_by_instance_name(backends[1]);
  932. PRBool foundchain0 = slapi_be_is_flag_set(be0, SLAPI_BE_FLAG_REMOTE_DATA);
  933. PRBool foundchain1 = slapi_be_is_flag_set(be1, SLAPI_BE_FLAG_REMOTE_DATA);
  934. retval = (foundchain0 || foundchain1) &&
  935. !(foundchain0 && foundchain1); /* 1 (but not both) backend is chaining */
  936. }
  937. slapi_ch_array_free(backends);
  938. } else /* could not find mapping tree entry - assume not set up */
  939. {
  940. }
  941. } break;
  942. default: /* search error - assume not set up */
  943. break;
  944. }
  945. slapi_ch_free_string(&mtnnodedn);
  946. slapi_free_search_results_internal(pb);
  947. slapi_pblock_destroy(pb);
  948. return retval;
  949. }
  950. void
  951. repl_set_repl_plugin_path(const char *path)
  952. {
  953. replpluginpath = slapi_ch_strdup(path);
  954. }
  955. int
  956. repl_config_valid_num(const char *config_attr, char *config_attr_value, int64_t min, int64_t max,
  957. int *returncode, char *errortext, int64_t *retval)
  958. {
  959. int rc = 0;
  960. char *endp = NULL;
  961. int64_t val;
  962. errno = 0;
  963. val = strtol(config_attr_value, &endp, 10);
  964. if (*endp != '\0' || val < min || val > max || errno == ERANGE) {
  965. *returncode = LDAP_UNWILLING_TO_PERFORM;
  966. if (errortext){
  967. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE,
  968. "Attribute %s value (%s) is invalid, must be a number between %" PRId64 " and %" PRId64 ".",
  969. config_attr, config_attr_value, min, max);
  970. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "repl_config_valid_num - %s\n",
  971. errortext);
  972. }
  973. rc = -1;
  974. } else {
  975. *retval = val;
  976. }
  977. return rc;
  978. }