replutil.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. struct berval type, value;
  369. struct berval bv_null = {0, NULL};
  370. int freeval = 0;
  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. type = bv_null;
  399. value = bv_null;
  400. rc = slapi_ldif_parse_line(line, &type, &value, &freeval);
  401. if (rc != 0)
  402. {
  403. /* ONREPL - log warning */
  404. slapi_log_error( SLAPI_LOG_REPL, repl_plugin_name,
  405. "Failed to parse the ldif line.\n");
  406. continue;
  407. }
  408. if (strncasecmp (type.bv_val, "add", type.bv_len) == 0)
  409. {
  410. slapi_mod_set_operation (&mod, LDAP_MOD_ADD | LDAP_MOD_BVALUES);
  411. }
  412. else if (strncasecmp (type.bv_val, "delete", type.bv_len) == 0)
  413. {
  414. slapi_mod_set_operation (&mod, LDAP_MOD_DELETE | LDAP_MOD_BVALUES);
  415. }
  416. else if (strncasecmp (type.bv_val, "replace", type.bv_len) == 0)
  417. {
  418. slapi_mod_set_operation (&mod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  419. }
  420. else /* attr: value pair */
  421. {
  422. /* adding first value */
  423. if (slapi_mod_get_type (&mod) == NULL)
  424. {
  425. slapi_mod_set_type (&mod, type.bv_val);
  426. }
  427. slapi_mod_add_value (&mod, &value);
  428. }
  429. if (freeval) {
  430. slapi_ch_free_string(&value.bv_val);
  431. }
  432. line = ldif_getline (&next);
  433. }
  434. }
  435. return mods;
  436. }
  437. static void* g_plg_identity [PLUGIN_MAX];
  438. void*
  439. repl_get_plugin_identity (int pluginID)
  440. {
  441. PR_ASSERT (pluginID < PLUGIN_MAX);
  442. return g_plg_identity [pluginID];
  443. }
  444. void
  445. repl_set_plugin_identity (int pluginID, void *identity)
  446. {
  447. PR_ASSERT (pluginID < PLUGIN_MAX);
  448. g_plg_identity [pluginID] = identity;
  449. }
  450. /* this function validates operation parameters */
  451. PRBool
  452. IsValidOperation (const slapi_operation_parameters *op)
  453. {
  454. if (op == NULL)
  455. {
  456. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  457. "IsValidOperation: NULL operation\n");
  458. return PR_FALSE;
  459. }
  460. if (op->csn == NULL)
  461. {
  462. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  463. "IsValidOperation: NULL operation CSN\n");
  464. return PR_FALSE;
  465. }
  466. if (op->target_address.uniqueid == NULL)
  467. {
  468. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  469. "IsValidOperation: NULL entry uniqueid\n");
  470. return PR_FALSE;
  471. }
  472. if (op->target_address.sdn == NULL)
  473. {
  474. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  475. "IsValidOperation: NULL entry DN\n");
  476. return PR_FALSE;
  477. }
  478. switch (op->operation_type)
  479. {
  480. case SLAPI_OPERATION_ADD: if (op->p.p_add.target_entry == NULL)
  481. {
  482. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  483. "IsValidOperation: NULL entry for add operation\n");
  484. return PR_FALSE;
  485. }
  486. else
  487. break;
  488. case SLAPI_OPERATION_MODIFY: if (op->p.p_modify.modify_mods == NULL ||
  489. op->p.p_modify.modify_mods[0] == NULL)
  490. {
  491. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  492. "IsValidOperation: NULL mods for modify operation\n");
  493. return PR_FALSE;
  494. }
  495. else
  496. break;
  497. case SLAPI_OPERATION_MODRDN: if (op->p.p_modrdn.modrdn_mods == NULL ||
  498. op->p.p_modrdn.modrdn_mods[0] == NULL)
  499. {
  500. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  501. "IsValidOperation: NULL mods for modrdn operation\n");
  502. return PR_FALSE;
  503. }
  504. if (op->p.p_modrdn.modrdn_newrdn == NULL)
  505. {
  506. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  507. "IsValidOperation: NULL new rdn for modrdn operation\n");
  508. return PR_FALSE;
  509. }
  510. else
  511. break;
  512. case SLAPI_OPERATION_DELETE: break;
  513. default: return PR_FALSE;
  514. }
  515. return PR_TRUE;
  516. }
  517. const char *
  518. map_repl_root_to_dbid(Slapi_DN *repl_root)
  519. {
  520. const char *return_ptr;
  521. PR_ASSERT(NULL != repl_root);
  522. if (NULL != repl_root)
  523. {
  524. /* XXXggood get per-database ID here, when code available */
  525. }
  526. return_ptr = get_server_dataversion(); /* XXXggood temporary hack until we have per-database instance dbids */
  527. return return_ptr;
  528. }
  529. PRBool
  530. is_ruv_tombstone_entry (Slapi_Entry *e)
  531. {
  532. char *dn;
  533. char *match;
  534. PR_ASSERT (e);
  535. dn = slapi_entry_get_dn (e);
  536. PR_ASSERT (dn);
  537. /* tombstone has rdn: nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff */
  538. match = strstr (dn, RUV_STORAGE_ENTRY_UNIQUEID);
  539. return (match != NULL);
  540. }
  541. LDAPControl* create_managedsait_control ()
  542. {
  543. LDAPControl *control;
  544. control = (LDAPControl*)slapi_ch_malloc (sizeof (LDAPControl));
  545. control->ldctl_oid = slapi_ch_strdup (LDAP_CONTROL_MANAGEDSAIT);
  546. control->ldctl_value.bv_val = NULL;
  547. control->ldctl_value.bv_len = 0;
  548. control->ldctl_iscritical = '\0';
  549. return control;
  550. }
  551. LDAPControl* create_backend_control (Slapi_DN *sdn)
  552. {
  553. LDAPControl *control = NULL;
  554. const char *be_name = slapi_mtn_get_backend_name(sdn);
  555. if (NULL != be_name) {
  556. control = (LDAPControl*)slapi_ch_malloc (sizeof (LDAPControl));
  557. control->ldctl_oid = slapi_ch_strdup ("2.16.840.1.113730.3.4.14");
  558. control->ldctl_value.bv_val = slapi_ch_strdup(be_name);
  559. control->ldctl_value.bv_len = strlen (be_name);
  560. control->ldctl_iscritical = 1;
  561. }
  562. return control;
  563. }
  564. /*
  565. * HREF_CHAR_ACCEPTABLE was copied from slapd/referral.c
  566. * which was copied from libldap/tmplout.c.
  567. */
  568. /* Note: an identical function is in ../../slapd/referral.c */
  569. #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
  570. ( c >= '@' && c <= 'Z' ) || \
  571. ( c == '_' ) || \
  572. ( c >= 'a' && c <= 'z' ))
  573. /*
  574. * Function: strcat_escaped
  575. *
  576. * Returns: nothing
  577. *
  578. * Description: Appends string s2 to s1, URL-escaping (%HH) unsafe
  579. * characters in s2 as appropriate. This function was
  580. * copied from slapd/referral.c.
  581. * which was copied from libldap/tmplout.c.
  582. * added const qualifier
  583. *
  584. * Author: MCS
  585. */
  586. /*
  587. * append s2 to s1, URL-escaping (%HH) unsafe characters
  588. */
  589. /* Note: an identical function is in ../../slapd/referral.c */
  590. static void
  591. strcat_escaped( char *s1, const char *s2 )
  592. {
  593. char *p, *q;
  594. char *hexdig = "0123456789ABCDEF";
  595. p = s1 + strlen( s1 );
  596. for ( q = (char*)s2; *q != '\0'; ++q ) {
  597. if ( HREF_CHAR_ACCEPTABLE( *q )) {
  598. *p++ = *q;
  599. } else {
  600. *p++ = '%';
  601. *p++ = hexdig[ 0x0F & ((*(unsigned char*)q) >> 4) ];
  602. *p++ = hexdig[ 0x0F & *q ];
  603. }
  604. }
  605. *p = '\0';
  606. }
  607. /*
  608. This function appends the replication root to the purl referrals found
  609. in the given ruv and the other given referrals, merges the lists, and sets the
  610. referrals in the mapping tree node corresponding to the given sdn, which is the
  611. repl_root
  612. This function also sets the mapping tree state (e.g. disabled, backend, referral,
  613. referral on update) - the mapping tree has very specific rules about how states
  614. can be set in the presence of referrals - specifically:
  615. 1) the nsslapd-referral attribute must be set before changing the state to referral
  616. or referral on update
  617. 2) the state must be set to backend or disabled before removing referrals
  618. */
  619. void
  620. repl_set_mtn_state_and_referrals(
  621. const Slapi_DN *repl_root_sdn,
  622. const char *mtn_state,
  623. const RUV *ruv,
  624. char **ruv_referrals,
  625. char **other_referrals
  626. )
  627. {
  628. int rc = 0;
  629. int ii = 0;
  630. char **referrals_to_set = NULL;
  631. PRBool chain_on_update = is_chain_on_update_setup(repl_root_sdn);
  632. if (NULL == mtn_state) {
  633. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  634. "repl_set_mtn_referrals: cannot set NULL state.\n");
  635. return;
  636. }
  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. LDAPURLDesc *lud = NULL;
  661. (void)slapi_ldap_url_parse(referrals_to_set[ii], &lud, 0, NULL);
  662. /* see if the dn is already in the referral URL */
  663. if (!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 && rc != LDAP_TYPE_OR_VALUE_EXISTS) {
  706. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "repl_set_mtn_referrals: could "
  707. "not set referrals for replica %s: %d\n", slapi_sdn_get_dn(repl_root_sdn), rc);
  708. }
  709. charray_free(referrals_to_set);
  710. return;
  711. }
  712. /*
  713. * This function allows to use a local backend in conjunction with
  714. * a chaining backend
  715. * The local ldbm backend is the replication consumer database
  716. * (e.g. on a hub or consumer) - it is read-only except for supplier updates
  717. * The chaining backend points to the supplier(s)
  718. * This distribution logic forwards the update request to the chaining
  719. * backend, and sends the search request to the local ldbm database
  720. *
  721. * To be able to use it one must define one ldbm backend and one chaining
  722. * backend in the mapping tree node - the ldbm backend will usually
  723. * already be there
  724. *
  725. */
  726. int
  727. repl_chain_on_update(Slapi_PBlock *pb, Slapi_DN * target_dn,
  728. char **mtn_be_names, int be_count,
  729. Slapi_DN * node_dn, int *mtn_be_states)
  730. {
  731. char * requestor_dn;
  732. unsigned long op_type;
  733. Slapi_Operation *op;
  734. int repl_op = 0;
  735. int local_backend = -1; /* index of local backend */
  736. int chaining_backend = -1; /* index of chain backend */
  737. #ifdef DEBUG_CHAIN_ON_UPDATE
  738. int is_internal = 0;
  739. #endif
  740. PRBool local_online = PR_FALSE; /* true if the local db is online */
  741. int ii;
  742. int opid;
  743. #ifdef DEBUG_CHAIN_ON_UPDATE
  744. PRUint64 connid = 0;
  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. is_internal = 1;
  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 (be) {
  763. if (slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA))
  764. {
  765. chaining_backend = ii;
  766. }
  767. else
  768. {
  769. local_backend = ii;
  770. if (mtn_be_states[ii] == SLAPI_BE_STATE_ON)
  771. {
  772. local_online = PR_TRUE;
  773. }
  774. }
  775. #ifdef DEBUG_CHAIN_ON_UPDATE
  776. if (is_internal) {
  777. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=-1 op=%d be "
  778. "%s is the %s backend and is %s\n", opid,
  779. mtn_be_names[ii], (chaining_backend == ii) ? "chaining" : "local",
  780. (mtn_be_states[ii] == SLAPI_BE_STATE_ON) ? "online" : "offline");
  781. } else {
  782. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%" PRIu64 " op=%d be "
  783. "%s is the %s backend and is %s\n", connid, opid,
  784. mtn_be_names[ii], (chaining_backend == ii) ? "chaining" : "local",
  785. (mtn_be_states[ii] == SLAPI_BE_STATE_ON) ? "online" : "offline");
  786. }
  787. #endif
  788. } else {
  789. /* A chaining backend will not be found during import. We will just return the
  790. * local backend in this case, which seems like the right thing to do to allow
  791. * offline replication initialization. */
  792. #ifdef DEBUG_CHAIN_ON_UPDATE
  793. if (is_internal) {
  794. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=-1 op=%d be "
  795. "%s not found. Assuming it is the chaining backend and we are doing an import.\n",
  796. opid, mtn_be_names[ii]);
  797. } else {
  798. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%" PRIu64 " op=%d be "
  799. "%s not found. Assuming it is the chaining backend and we are doing an import.\n",
  800. connid, opid, mtn_be_names[ii]);
  801. }
  802. #endif
  803. }
  804. }
  805. /* if no chaining backends are defined, just use the local one */
  806. if (chaining_backend == -1) {
  807. return local_backend;
  808. }
  809. /* All internal operations go to the local backend */
  810. if (operation_is_flag_set(op, OP_FLAG_INTERNAL)) {
  811. return local_backend;
  812. }
  813. /* Check the operation type
  814. * read-only operation will go to the local backend if online
  815. */
  816. op_type = slapi_op_get_type(op);
  817. if (local_online &&
  818. ((op_type == SLAPI_OPERATION_SEARCH) ||
  819. (op_type == SLAPI_OPERATION_UNBIND) ||
  820. (op_type == SLAPI_OPERATION_COMPARE))) {
  821. #ifdef DEBUG_CHAIN_ON_UPDATE
  822. if (is_internal) {
  823. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=-1 op=%d op is "
  824. "%d: using local backend\n", opid, op_type);
  825. } else {
  826. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%" PRIu64 " op=%d op is "
  827. "%d: using local backend\n", connid, opid, op_type);
  828. }
  829. #endif
  830. return local_backend;
  831. }
  832. /* if the operation is done by directory manager
  833. * use local database even for updates because it is an administrative
  834. * operation
  835. * remarks : one could also use an update DN in the same way
  836. * to let update operation go to the local backend when they are done
  837. * by specific administrator user but let all the other user
  838. * go to the read-write replica
  839. * also - I don't think it is possible to chain directory manager
  840. */
  841. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &requestor_dn);
  842. if (slapi_dn_isroot(requestor_dn)) {
  843. #ifdef DEBUG_CHAIN_ON_UPDATE
  844. if (is_internal) {
  845. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=-1 op=%d requestor "
  846. "is root: using local backend\n", opid);
  847. } else {
  848. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%" PRIu64 " op=%d requestor "
  849. "is root: using local backend\n", connid, opid);
  850. }
  851. #endif
  852. return local_backend;
  853. }
  854. /* if the operation is a replicated operation
  855. * use local database even for updates to avoid infinite loops
  856. */
  857. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  858. if (repl_op) {
  859. #ifdef DEBUG_CHAIN_ON_UPDATE
  860. if (is_internal) {
  861. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=-1 op=%d op is "
  862. "replicated: using local backend\n", opid);
  863. } else {
  864. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%" PRIu64 " op=%d op is "
  865. "replicated: using local backend\n", connid, opid);
  866. }
  867. #endif
  868. return local_backend;
  869. }
  870. /* if using global password policy, chain the bind request so that the
  871. master can update and replicate the password policy op attrs */
  872. if (op_type == SLAPI_OPERATION_BIND) {
  873. extern int config_get_pw_is_global_policy();
  874. if (!config_get_pw_is_global_policy()) {
  875. #ifdef DEBUG_CHAIN_ON_UPDATE
  876. if (is_internal) {
  877. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=-1 op=%d using "
  878. "local backend for local password policy\n", opid);
  879. } else {
  880. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%" PRIu64 " op=%d using "
  881. "local backend for local password policy\n", connid, opid);
  882. }
  883. #endif
  884. return local_backend;
  885. }
  886. }
  887. /* all other case (update while not directory manager) :
  888. * or any normal non replicated client operation while local is disabled (import) :
  889. * use the chaining backend
  890. */
  891. #ifdef DEBUG_CHAIN_ON_UPDATE
  892. if (is_internal) {
  893. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=-1 op=%d using "
  894. "chaining backend\n", opid);
  895. } else {
  896. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "repl_chain_on_update: conn=%" PRIu64 " op=%d using "
  897. "chaining backend\n", connid, opid);
  898. }
  899. #endif
  900. return chaining_backend;
  901. }
  902. int
  903. repl_enable_chain_on_update(Slapi_DN *suffix)
  904. {
  905. /* Submit a Modify operation to add the distribution function to the mapping tree
  906. node for the given suffix */
  907. slapi_mods smods;
  908. int operation_result;
  909. Slapi_PBlock *pb= slapi_pblock_new();
  910. Slapi_DN *mtnnodesdn;
  911. slapi_mods_init(&smods,2);
  912. /* need path and file name of the replication plugin here */
  913. slapi_mods_add_string(&smods, LDAP_MOD_ADD, "nsslapd-distribution-plugin", replpluginpath);
  914. slapi_mods_add_string(&smods, LDAP_MOD_ADD, "nsslapd-distribution-funct", "repl_chain_on_update");
  915. /* need DN of mapping tree node here */
  916. mtnnodesdn = slapi_get_mapping_tree_node_configsdn(suffix);
  917. slapi_modify_internal_set_pb_ext(
  918. pb,
  919. mtnnodesdn,
  920. slapi_mods_get_ldapmods_byref(&smods), /* JCM cast */
  921. NULL, /*Controls*/
  922. NULL, /*uniqueid*/
  923. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  924. 0);
  925. slapi_modify_internal_pb(pb);
  926. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &operation_result);
  927. slapi_sdn_free(&mtnnodesdn);
  928. slapi_pblock_destroy(pb);
  929. switch(operation_result)
  930. {
  931. case LDAP_SUCCESS:
  932. /* OK, everything is fine. */
  933. break;
  934. default:
  935. PR_ASSERT(0); /* JCMREPL FOR DEBUGGING */
  936. }
  937. slapi_mods_done(&smods);
  938. return operation_result;
  939. }
  940. int
  941. repl_disable_chain_on_update(Slapi_DN *suffix)
  942. {
  943. /* Submit a Modify operation to remove the distribution function from the mapping tree
  944. node for the given suffix */
  945. slapi_mods smods;
  946. int operation_result;
  947. Slapi_PBlock *pb= slapi_pblock_new();
  948. Slapi_DN *mtnnodesdn;
  949. slapi_mods_init(&smods,2);
  950. slapi_mods_add_modbvps(&smods, LDAP_MOD_DELETE, "nsslapd-distribution-plugin", NULL);
  951. slapi_mods_add_modbvps(&smods, LDAP_MOD_DELETE, "nsslapd-distribution-funct", NULL);
  952. /* need DN of mapping tree node here */
  953. mtnnodesdn = slapi_get_mapping_tree_node_configsdn(suffix);
  954. slapi_modify_internal_set_pb_ext(
  955. pb,
  956. mtnnodesdn,
  957. slapi_mods_get_ldapmods_byref(&smods), /* JCM cast */
  958. NULL, /*Controls*/
  959. NULL, /*uniqueid*/
  960. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  961. 0);
  962. slapi_modify_internal_pb(pb);
  963. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &operation_result);
  964. slapi_sdn_free(&mtnnodesdn);
  965. slapi_pblock_destroy(pb);
  966. switch(operation_result)
  967. {
  968. case LDAP_SUCCESS:
  969. /* OK, everything is fine. */
  970. break;
  971. default:
  972. PR_ASSERT(0); /* JCMREPL FOR DEBUGGING */
  973. }
  974. slapi_mods_done(&smods);
  975. return operation_result;
  976. }
  977. static PRBool
  978. is_chain_on_update_setup(const Slapi_DN *replroot)
  979. {
  980. /* Do an internal search of the mapping tree node to see if chain on update is setup
  981. for this replica
  982. - has two backends
  983. - has a distribution function
  984. - has a distribution plugin
  985. - one of the backends is a ldbm database
  986. - one of the backends is a chaining database
  987. */
  988. static char* attrs[] = { "nsslapd-backend",
  989. "nsslapd-distribution-plugin", "nsslapd-distribution-funct",
  990. NULL };
  991. int operation_result;
  992. Slapi_PBlock *pb= slapi_pblock_new();
  993. char *mtnnodedn = slapi_get_mapping_tree_node_configdn(replroot);
  994. PRBool retval = PR_FALSE;
  995. slapi_search_internal_set_pb(
  996. pb,
  997. mtnnodedn,
  998. LDAP_SCOPE_BASE,
  999. "objectclass=*",
  1000. attrs, /*attrs*/
  1001. 0, /*attrsonly*/
  1002. NULL, /*Controls*/
  1003. NULL, /*uniqueid*/
  1004. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  1005. 0);
  1006. slapi_search_internal_pb(pb);
  1007. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &operation_result);
  1008. switch(operation_result)
  1009. {
  1010. case LDAP_SUCCESS:
  1011. {
  1012. Slapi_Entry **entries= NULL;
  1013. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1014. if(entries!=NULL && entries[0]!=NULL)
  1015. {
  1016. Slapi_Entry *e = entries[0];
  1017. char **backends = slapi_entry_attr_get_charray(e, "nsslapd-backend");
  1018. char *plg = slapi_entry_attr_get_charptr(e, "nsslapd-distribution-plugin");
  1019. char *func = slapi_entry_attr_get_charptr(e, "nsslapd-distribution-funct");
  1020. if (backends && backends[0] && backends[1] && plg && func)
  1021. {
  1022. /* all the necessary attrs are present - check to see if we
  1023. have one chaining backend */
  1024. Slapi_Backend *be0 = slapi_be_select_by_instance_name(backends[0]);
  1025. Slapi_Backend *be1 = slapi_be_select_by_instance_name(backends[1]);
  1026. PRBool foundchain0 = slapi_be_is_flag_set(be0,SLAPI_BE_FLAG_REMOTE_DATA);
  1027. PRBool foundchain1 = slapi_be_is_flag_set(be1,SLAPI_BE_FLAG_REMOTE_DATA);
  1028. retval = (foundchain0 || foundchain1) &&
  1029. !(foundchain0 && foundchain1); /* 1 (but not both) backend is chaining */
  1030. }
  1031. slapi_ch_array_free(backends);
  1032. slapi_ch_free_string(&plg);
  1033. slapi_ch_free_string(&func);
  1034. }
  1035. else /* could not find mapping tree entry - assume not set up */
  1036. {
  1037. }
  1038. }
  1039. break;
  1040. default: /* search error - assume not set up */
  1041. break;
  1042. }
  1043. slapi_ch_free_string(&mtnnodedn);
  1044. slapi_free_search_results_internal(pb);
  1045. slapi_pblock_destroy(pb);
  1046. return retval;
  1047. }
  1048. void
  1049. repl_set_repl_plugin_path(const char *path)
  1050. {
  1051. replpluginpath = slapi_ch_strdup(path);
  1052. }