replutil.c 34 KB

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