1
0

replutil.c 33 KB

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