replutil.c 32 KB

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