3 * Replication Engine which uses the LDAP Sync protocol
5 /* Copyright (c) 2003 by International Business Machines, Inc.
7 * International Business Machines, Inc. (hereinafter called IBM) grants
8 * permission under its copyrights to use, copy, modify, and distribute this
9 * Software with or without fee, provided that the above copyright notice and
10 * all paragraphs of this notice appear in all copies, and that the name of IBM
11 * not be used in connection with the marketing of any product incorporating
12 * the Software or modifications thereof, without specific, written prior
15 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
27 #include <ac/string.h>
28 #include <ac/socket.h>
34 #include "lutil_ldap.h"
41 syncrepl_message_to_entry ( LDAP *, Operation *, LDAPMessage *,
42 Modifications **, int*, struct berval *, struct berval * );
45 syncrepl_entry( LDAP *, Operation*, Entry*, Modifications*,
46 int, struct berval*, struct berval*, int );
49 syncrepl_del_nonpresent( LDAP *, Operation * );
52 syncrepl_add_glue( LDAP *, Operation*, Entry*, Modifications*, int,
53 struct berval*, struct berval* );
56 syncrepl_updateCookie( LDAP *, Operation *, struct berval *, struct berval * );
59 slap_mods_check_syncrepl( Operation *, Modifications **,
60 const char **, char *, size_t, void *ctx );
63 slap_mods_opattrs_syncrepl( Operation *, Modifications *, Modifications **,
64 const char **, char *, size_t );
67 slap_mods2entry_syncrepl( Modifications *, Entry **, int,
68 const char **, char *, size_t );
70 /* callback functions */
71 static int cookie_callback( struct slap_op *, struct slap_rep * );
72 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
73 static int null_callback( struct slap_op *, struct slap_rep * );
75 static AttributeDescription **add_descs;
76 static AttributeDescription **add_descs_lastmod;
77 static AttributeDescription **del_descs;
78 static AttributeDescription **del_descs_lastmod;
80 struct runqueue_s syncrepl_rq;
85 add_descs = ch_malloc( 2 * sizeof( AttributeDescription * ));
86 add_descs[0] = slap_schema.si_ad_objectClass;
89 add_descs_lastmod = ch_malloc( 7 * sizeof( AttributeDescription * ));
90 add_descs_lastmod[0] = slap_schema.si_ad_objectClass;
91 add_descs_lastmod[1] = slap_schema.si_ad_creatorsName;
92 add_descs_lastmod[2] = slap_schema.si_ad_modifiersName;
93 add_descs_lastmod[3] = slap_schema.si_ad_createTimestamp;
94 add_descs_lastmod[4] = slap_schema.si_ad_modifyTimestamp;
95 add_descs_lastmod[5] = slap_schema.si_ad_entryCSN;
96 add_descs_lastmod[6] = NULL;
98 del_descs = ch_malloc( 9 * sizeof( AttributeDescription * ));
99 del_descs[0] = slap_schema.si_ad_structuralObjectClass;
100 del_descs[1] = slap_schema.si_ad_subschemaSubentry;
101 del_descs[2] = slap_schema.si_ad_hasSubordinates;
102 del_descs[3] = slap_schema.si_ad_creatorsName;
103 del_descs[4] = slap_schema.si_ad_modifiersName;
104 del_descs[5] = slap_schema.si_ad_createTimestamp;
105 del_descs[6] = slap_schema.si_ad_modifyTimestamp;
106 del_descs[7] = slap_schema.si_ad_entryCSN;
109 del_descs_lastmod = ch_malloc( 4 * sizeof( AttributeDescription * ));
110 del_descs_lastmod[0] = slap_schema.si_ad_structuralObjectClass;
111 del_descs_lastmod[1] = slap_schema.si_ad_subschemaSubentry;
112 del_descs_lastmod[2] = slap_schema.si_ad_hasSubordinates;
113 del_descs_lastmod[3] = NULL;
122 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
124 SlapReply rs = {REP_RESULT};
127 LDAPControl **sctrls = NULL;
128 LDAPControl **rctrls = NULL;
129 LDAPControl *rctrlp = NULL;
130 BerElement *sync_ber = NULL;
131 struct berval *sync_bvalp = NULL;
133 BerElement *ctrl_ber = NULL;
134 BerElement *res_ber = NULL;
137 LDAPMessage *res = NULL;
138 LDAPMessage *msg = NULL;
142 int nresponses, nreferences, nextended, npartial;
143 int nresponses_psearch;
145 int cancel_msgid = -1;
147 struct berval *retdata = NULL;
149 int sync_info_arrived = 0;
153 struct berval syncUUID = { 0, NULL };
154 struct berval syncCookie = { 0, NULL };
159 int syncinfo_arrived = 0;
160 int cancel_response = 0;
163 AttributeDescription** descs = NULL;
175 struct berval base_bv = { 0, NULL };
176 struct berval pbase = { 0, NULL };
177 struct berval nbase = { 0, NULL };
178 struct berval sub_bv = { 0, NULL };
179 struct berval psubrdn = { 0, NULL };
180 struct berval nsubrdn = { 0, NULL };
181 struct berval psub = { 0, NULL };
182 struct berval nsub = { 0, NULL };
184 Modifications *modlist = NULL;
185 Modifications *ml, *mlnext;
186 char *def_filter_str = NULL;
189 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
191 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
197 if ( abs(si->type) != LDAP_SYNC_REFRESH_ONLY &&
198 abs(si->type) != LDAP_SYNC_REFRESH_AND_PERSIST ) {
202 /* Init connection to master */
204 if ( ldap_is_ldap_url( si->masteruri )) {
205 rc = ldap_initialize( &ld, si->masteruri );
206 if ( rc != LDAP_SUCCESS ) {
208 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
209 "ldap_initialize failed (%s)\n",
210 si->masteruri, 0, 0 );
212 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
213 "ldap_initialize failed (%s)\n",
214 si->masteruri, 0, 0 );
218 ld = ldap_init( si->mastername, si->masterport );
221 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
222 "ldap_init failed (%s:%s)\n",
223 si->mastername, si->masterport, 0 );
225 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
226 "ldap_init failed (%s:%s)\n",
227 si->mastername, si->masterport, 0 );
232 op.o_protocol = LDAP_VERSION3;
233 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &op.o_protocol );
238 rc = ldap_start_tls_s( ld, NULL, NULL );
239 if( rc != LDAP_SUCCESS ) {
241 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
242 "%s: ldap_start_tls failed (%d)\n",
243 si->tls == TLS_CRITICAL ? "Error" : "Warning",
246 Debug( LDAP_DEBUG_ANY,
247 "%s: ldap_start_tls failed (%d)\n",
248 si->tls == TLS_CRITICAL ? "Error" : "Warning",
251 if( si->tls == TLS_CRITICAL )
256 if ( si->bindmethod == LDAP_AUTH_SASL ) {
257 #ifdef HAVE_CYRUS_SASL
260 if ( si->secprops != NULL ) {
261 int err = ldap_set_option( ld,
262 LDAP_OPT_X_SASL_SECPROPS, si->secprops);
264 if( err != LDAP_OPT_SUCCESS ) {
266 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
267 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
268 si->mastername, si->secprops, 0 );
270 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
271 "(%s,SECPROPS,\"%s\") failed!\n",
272 si->mastername, si->secprops, NULL );
278 defaults = lutil_sasl_defaults( ld,
285 rc = ldap_sasl_interactive_bind_s( ld,
293 if ( rc != LDAP_SUCCESS ) {
295 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
296 "ldap_sasl_interactive_bind_s failed (%d)\n",
299 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
300 "ldap_sasl_interactive_bind_s failed (%d)\n",
305 #else /* HAVE_CYRUS_SASL */
306 fprintf( stderr, "not compiled with SASL support\n" );
310 rc = ldap_bind_s( ld, si->binddn, si->passwd, si->bindmethod );
311 if ( rc != LDAP_SUCCESS ) {
313 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
314 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
316 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
317 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
325 op.o_tmpmemctx = NULL; /* FIXME : to use per-thread mem context */
326 op.o_tag = LDAP_REQ_SEARCH;
327 op.o_dn = si->updatedn;
328 op.o_ndn = si->updatedn;
330 op.o_time = slap_get_time();
331 op.o_managedsait = 1;
332 op.o_threadctx = si->ctx;
335 op.o_connid = op.o_conn->c_connid;
336 op.ors_scope = LDAP_SCOPE_BASE;
337 op.ors_deref = LDAP_DEREF_NEVER;
340 op.ors_attrsonly = 0;
342 op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
343 ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
347 conn.c_send_ldap_result = slap_send_ldap_result;
348 conn.c_send_search_entry = slap_send_search_entry;
349 conn.c_send_search_reference = slap_send_search_reference;
351 /* get syncrepl cookie of shadow replica from subentry */
352 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
353 dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
355 sprintf( substr, "cn=syncrepl%d", si->id );
356 ber_str2bv( substr, strlen(substr), 1, &sub_bv );
357 dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn, op.o_tmpmemctx );
359 build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
360 build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
362 ch_free( base_bv.bv_val );
363 ch_free( pbase.bv_val );
364 ch_free( nbase.bv_val );
365 ch_free( sub_bv.bv_val );
366 ch_free( psubrdn.bv_val );
367 ch_free( nsubrdn.bv_val );
369 /* set callback function */
370 cb.sc_response = cookie_callback;
373 /* search subentry to retrieve cookie */
374 si->syncCookie = NULL;
375 be->be_search( &op, &rs );
377 ch_free( op.o_req_dn.bv_val );
378 ch_free( op.o_req_ndn.bv_val );
379 filter_free( op.ors_filter );
380 ch_free( op.ors_filterstr.bv_val );
382 psub = be->be_nsuffix[0];
384 /* setup LDAP SYNC control */
385 sync_ber = ber_alloc_t( LBER_USE_DER );
386 ber_set_option( sync_ber, LBER_OPT_BER_MEMCTX, op.o_tmpmemctx );
388 if ( si->syncCookie ) {
389 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
391 ber_printf( sync_ber, "{e}", abs(si->type) );
394 if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
395 ber_free( sync_ber, 1 );
398 ber_free( sync_ber, 1 );
400 sctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), op.o_tmpmemctx );
402 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
403 c[0].ldctl_value = (*sync_bvalp);
404 c[0].ldctl_iscritical = si->type < 0;
408 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
409 c[1].ldctl_value.bv_val = si->authzId;
410 c[1].ldctl_value.bv_len = strlen( si->authzId );
411 c[1].ldctl_iscritical = 1;
419 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, sctrls );
421 ber_bvfree( sync_bvalp );
424 if ( err != LDAP_OPT_SUCCESS )
425 fprintf( stderr, "Could not set controls : %d\n", err );
427 /* Delete Attributes */
428 if ( si->lastmod == LASTMOD_REQ ) {
429 descs = del_descs_lastmod;
434 for ( i = 0; descs[i] != NULL; i++ ) {
435 for ( j = 0; si->attrs[j] != NULL; j++ ) {
436 if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
437 ch_free( si->attrs[j] );
438 for ( k = j; si->attrs[k] != NULL; k++ ) {
439 si->attrs[k] = si->attrs[k+1];
447 for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
449 if ( si->lastmod == LASTMOD_REQ ) {
450 descs = add_descs_lastmod;
455 for ( i = 0; descs[i] != NULL; i++ ) {
456 tmp = ( char ** ) ch_realloc( si->attrs,
457 ( n + 2 ) * sizeof( char * ));
460 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
462 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
466 si->attrs[ n++ ] = ch_strdup ( descs[i]->ad_cname.bv_val );
467 si->attrs[ n ] = NULL;
470 /* Send LDAP SYNC search */
472 rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
473 si->attrs, si->attrsonly, NULL, NULL,
476 if( rc != LDAP_SUCCESS ) {
477 fprintf( stderr, "syncrepl: ldap_search_ext: %s (%d)\n",
478 ldap_err2string( rc ), rc );
482 while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
484 for ( msg = ldap_first_message( ld, res );
486 msg = ldap_next_message( ld, msg ) )
488 switch( ldap_msgtype( msg ) ) {
489 case LDAP_RES_SEARCH_ENTRY:
490 entry = syncrepl_message_to_entry( ld, &op, msg,
491 &modlist, &syncstate, &syncUUID, &syncCookie );
492 rc_efree = syncrepl_entry( ld, &op, entry, modlist,
493 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
494 if ( syncCookie.bv_len ) {
495 syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
499 for ( ml = modlist; ml != NULL; ml = mlnext ) {
500 mlnext = ml->sml_next;
505 case LDAP_RES_SEARCH_REFERENCE:
507 LDAP_LOG( OPERATION, ERR,
508 "do_syncrepl : reference received\n", 0, 0, 0 );
510 Debug( LDAP_DEBUG_ANY,
511 "do_syncrepl : reference received\n", 0, 0, 0 );
515 case LDAP_RES_SEARCH_RESULT:
516 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
519 ctrl_ber = ber_alloc_t( LBER_USE_DER );
520 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op.o_tmpmemctx );
521 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
522 ber_reset( ctrl_ber, 1 );
524 ber_scanf( ctrl_ber, "{" );
525 if ( ber_peek_tag( ctrl_ber, &len )
526 == LDAP_SYNC_TAG_COOKIE ) {
527 ber_scanf( ctrl_ber, "o", &syncCookie );
530 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
531 if ( cancel_response ) {
532 if ( syncCookie.bv_len ) {
533 ber_bvfree( si->syncCookie );
534 si->syncCookie = ber_dupbv( NULL, &syncCookie );
537 ber_free( ctrl_ber, 1 );
542 ber_free( ctrl_ber, 1 );
546 if ( syncCookie.bv_len ) {
547 syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
549 syncrepl_del_nonpresent( ld, &op );
551 ber_free( ctrl_ber, 1 );
556 case LDAP_RES_INTERMEDIATE_RESP:
557 ldap_parse_intermediate_resp_result( ld, msg,
558 &retoid, &retdata, 0 );
559 if ( !strcmp( retoid, LDAP_SYNC_INFO ) ) {
560 sync_info_arrived = 1;
561 res_ber = ber_init( retdata );
562 ber_scanf( res_ber, "{e", &syncstate );
564 if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
565 syncrepl_del_nonpresent( ld, &op );
566 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
568 LDAP_LOG( OPERATION, ERR,
569 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
571 Debug( LDAP_DEBUG_ANY,
572 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
576 if ( ber_peek_tag( res_ber, &len )
577 == LDAP_SYNC_TAG_COOKIE ) {
578 ber_scanf( res_ber, "o}", &syncCookie );
579 if ( syncCookie.bv_len ) {
580 ber_bvfree( si->syncCookie );
581 si->syncCookie = ber_dupbv( NULL, &syncCookie );
584 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
586 LDAP_LOG( OPERATION, ERR,
587 "do_syncrepl : cookie required\n", 0, 0, 0 );
589 Debug( LDAP_DEBUG_ANY,
590 "do_syncrepl : cookie required\n", 0, 0, 0 );
595 ldap_memfree( retoid );
596 ber_bvfree( retdata );
597 ber_free( res_ber, 1 );
601 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
602 " unknown intermediate "
603 "response\n", 0, 0, 0 );
605 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
606 "unknown intermediate "
607 "response\n", 0, 0, 0 );
609 ldap_memfree( retoid );
610 ber_bvfree( retdata );
616 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
617 "unknown message\n", 0, 0, 0 );
619 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
620 "unknown message\n", 0, 0, 0 );
631 LDAP_LOG( OPERATION, ERR,
632 "do_syncrepl : unknown result\n", 0, 0, 0 );
634 Debug( LDAP_DEBUG_ANY,
635 "do_syncrepl : unknown result\n", 0, 0, 0 );
641 if ( syncCookie.bv_val )
642 ch_free( syncCookie.bv_val );
643 if ( syncUUID.bv_val )
644 ch_free( syncUUID.bv_val );
653 syncrepl_message_to_entry(
657 Modifications **modlist,
659 struct berval *syncUUID,
660 struct berval *syncCookie
664 BerElement *ber = NULL;
666 struct berval bv = {0, NULL};
669 Modifications **modtail = modlist;
670 Backend *be = op->o_bd;
673 char txtbuf[SLAP_TEXT_BUFLEN];
674 size_t textlen = sizeof txtbuf;
676 struct berval **bvals = NULL;
678 struct berval bdn = {0, NULL};
680 struct berval empty_bv = { 0, NULL };
684 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
688 LDAPControl** rctrls = NULL;
689 BerElement* ctrl_ber;
693 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
695 LDAP_LOG( OPERATION, ERR,
696 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
698 Debug( LDAP_DEBUG_ANY,
699 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
704 op->o_tag = LDAP_REQ_ADD;
706 rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
708 if ( rc != LDAP_SUCCESS ) {
710 LDAP_LOG( OPERATION, ERR,
711 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
713 Debug( LDAP_DEBUG_ANY,
714 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
719 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
720 dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, op->o_tmpmemctx );
724 for ( rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues);
726 rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues))
728 if ( tmp.sml_type.bv_val == NULL ) break;
730 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
732 mod->sml_op = LDAP_MOD_REPLACE;
733 mod->sml_next = NULL;
734 mod->sml_desc = NULL;
735 mod->sml_type = tmp.sml_type;
736 mod->sml_bvalues = tmp.sml_bvalues;
737 mod->sml_nvalues = tmp.sml_bvalues;
740 modtail = &mod->sml_next;
743 if ( ber_scanf( ber, "}") == LBER_ERROR ) {
745 LDAP_LOG( OPERATION, ERR,
746 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
748 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
755 tmpber = ldap_get_message_ber( msg );
756 ber = ber_dup( tmpber );
758 ber_scanf( ber, "{xx" );
760 rc = ldap_int_get_controls( ber, &rctrls );
762 if ( rc != LDAP_SUCCESS ) {
764 LDAP_LOG( OPERATION, ERR,
765 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
767 Debug( LDAP_DEBUG_ANY,
768 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
775 ctrl_ber = ber_alloc_t( LBER_USE_DER );
776 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op->o_tmpmemctx );
777 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
778 ber_reset( ctrl_ber, 1 );
779 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
780 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
781 ber_scanf( ctrl_ber, "o}", syncCookie );
783 ber_free( ctrl_ber, 1 );
786 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
787 " rctrls absent\n", 0, 0, 0 );
789 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
790 " rctrls absent\n", 0, 0, 0 );
794 if ( *syncstate == LDAP_SYNC_PRESENT ) {
799 if ( *modlist == NULL ) {
801 LDAP_LOG( OPERATION, ERR,
802 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
804 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
809 rc = slap_mods_check_syncrepl( op, modlist, &text, txtbuf, textlen, NULL );
811 if ( rc != LDAP_SUCCESS ) {
813 LDAP_LOG( OPERATION, ERR,
814 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
816 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
822 rc = slap_mods_opattrs_syncrepl( op, *modlist, modtail,
823 &text,txtbuf, textlen );
825 if( rc != LDAP_SUCCESS ) {
827 LDAP_LOG( OPERATION, ERR,
828 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
830 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
836 rc = slap_mods2entry_syncrepl( *modlist, &e, 1, &text, txtbuf, textlen );
837 if( rc != LDAP_SUCCESS ) {
839 LDAP_LOG( OPERATION, ERR,
840 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
842 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
855 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
857 const struct berval *uuid1 = v_uuid1;
858 const struct berval *uuid2 = v_uuid2;
859 int rc = uuid1->bv_len - uuid2->bv_len;
861 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
869 Modifications* modlist,
871 struct berval* syncUUID,
872 struct berval* syncCookie,
876 Backend *be = op->o_bd;
877 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
879 struct berval csn_bv = {0, NULL};
880 struct berval *syncuuid_bv = NULL;
881 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
883 SlapReply rs = {REP_RESULT};
886 #if 0 /* FIXME : UUID search required first */
888 struct berval filterstr_bv;
895 ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
896 syncuuid_bv = ber_dupbv( NULL, syncUUID );
897 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
898 syncuuid_cmp, avl_dup_error );
901 if ( syncstate == LDAP_SYNC_PRESENT ) {
908 if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
909 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
912 #if 0 /* FIXME : UUID search required first */
913 filterstr = (char *) sl_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1, op->o_tmpmemctx );
914 strcpy( filterstr, "entryUUID=" );
915 strcat( filterstr, syncUUID->bv_val );
919 si->syncUUID = syncUUID;
921 #if 0 /* FIXME : UUID search required first */
922 filter = str2filter( filterstr );
923 ber_str2bv( filterstr, strlen(filterstr), 1, &filterstr_bv );
924 ch_free( filterstr );
927 op->o_req_dn = e->e_name;
928 op->o_req_ndn = e->e_nname;
930 op->o_callback = &cb;
931 cb.sc_response = null_callback;
934 switch ( syncstate ) {
936 case LDAP_SYNC_MODIFY :
938 op->o_tag = LDAP_REQ_MODIFY;
939 op->orm_modlist = modlist;
940 rc = be->be_modify( op, &rs );
941 if ( rc != LDAP_SUCCESS ) {
942 if ( rc == LDAP_REFERRAL ||
943 rc == LDAP_NO_SUCH_OBJECT ||
944 rc == DB_NOTFOUND ) {
945 op->o_tag = LDAP_REQ_ADD;
947 rc = be->be_add( op, &rs );
948 if ( rc != LDAP_SUCCESS ) {
949 if ( rc == LDAP_ALREADY_EXISTS ) {
951 } else if ( rc == LDAP_REFERRAL ||
952 rc == LDAP_NO_SUCH_OBJECT ||
953 rc == DB_NOTFOUND ) {
954 syncrepl_add_glue(ld, op, e,
956 syncUUID, syncCookie);
959 LDAP_LOG( OPERATION, ERR,
960 "be_add failed (%d)\n",
963 Debug( LDAP_DEBUG_ANY,
964 "be_add failed (%d)\n",
973 LDAP_LOG( OPERATION, ERR,
974 "be_modify failed (%d)\n", rc, 0, 0 );
976 Debug( LDAP_DEBUG_ANY,
977 "be_modify failed (%d)\n", rc, 0, 0 );
984 case LDAP_SYNC_DELETE :
985 op->o_tag = LDAP_REQ_DELETE;
986 be->be_delete( op, &rs );
991 LDAP_LOG( OPERATION, ERR,
992 "unknown syncstate\n", 0, 0, 0 );
994 Debug( LDAP_DEBUG_ANY,
995 "unknown syncstate\n", 0, 0, 0 );
1002 syncrepl_del_nonpresent(
1007 Backend* be = op->o_bd;
1008 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1010 struct berval base_bv = {0, NULL};
1012 SlapReply rs = {REP_RESULT};
1013 struct berval filterstr_bv = {0, NULL};
1014 struct nonpresent_entry *np_list, *np_prev;
1016 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
1017 dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1018 ch_free( base_bv.bv_val );
1020 filter = str2filter( si->filterstr );
1022 cb.sc_response = nonpresent_callback;
1025 op->o_callback = &cb;
1026 op->o_tag = LDAP_REQ_SEARCH;
1027 op->ors_scope = si->scope;
1028 op->ors_deref = LDAP_DEREF_NEVER;
1029 op->ors_slimit = -1;
1030 op->ors_tlimit = -1;
1031 op->ors_attrsonly = 0;
1032 op->ors_attrs = NULL;
1033 op->ors_filter = filter;
1034 ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1036 be->be_search( op, &rs );
1038 if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1039 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1040 while ( np_list != NULL ) {
1041 LDAP_LIST_REMOVE( np_list, np_link );
1043 np_list = LDAP_LIST_NEXT( np_list, np_link );
1044 op->o_tag = LDAP_REQ_DELETE;
1045 op->o_callback = &cb;
1046 cb.sc_response = null_callback;
1048 op->o_req_dn = *np_prev->dn;
1049 op->o_req_ndn = *np_prev->ndn;
1050 op->o_bd->be_delete( op, &rs );
1051 ber_bvfree( np_prev->dn );
1052 ber_bvfree( np_prev->ndn );
1053 op->o_req_dn.bv_val = NULL;
1054 op->o_req_ndn.bv_val = NULL;
1059 if ( op->o_req_dn.bv_val )
1060 ch_free( op->o_req_dn.bv_val );
1061 if ( op->o_req_ndn.bv_val )
1062 ch_free( op->o_req_ndn.bv_val );
1063 filter_free( op->ors_filter );
1064 ch_free( op->ors_filterstr.bv_val );
1073 Modifications* modlist,
1075 struct berval* syncUUID,
1076 struct berval* syncCookie
1079 Backend *be = op->o_bd;
1080 syncinfo_t *si = op->o_callback->sc_private;
1081 struct berval uuid_bv = {0, NULL};
1085 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1088 struct berval dn = {0, NULL};
1089 struct berval pdn = {0, NULL};
1090 struct berval ndn = {0, NULL};
1091 struct berval rdn = {0, NULL};
1093 SlapReply rs = {REP_RESULT};
1094 Connection *conn = op->o_conn;
1096 op->o_tag = LDAP_REQ_ADD;
1097 op->o_callback = &cb;
1098 cb.sc_response = null_callback;
1101 ber_dupbv( &dn, &e->e_nname );
1102 ber_dupbv( &pdn, &e->e_nname );
1104 while ( !be_issuffix ( be, &pdn )) {
1105 dnParent( &dn, &pdn );
1106 ch_free( dn.bv_val );
1107 ber_dupbv( &dn, &pdn );
1111 for ( i = 0; i <= levels; i++ ) {
1112 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1113 ch_free( dn.bv_val );
1114 ch_free( pdn.bv_val );
1115 ber_dupbv( &dn, &e->e_nname );
1116 ber_dupbv( &pdn, &e->e_nname );
1118 for ( k = 0; k < j; k++ ) {
1119 dnParent( &dn, &pdn );
1120 ch_free( dn.bv_val );
1121 ber_dupbv( &dn, &pdn );
1124 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1125 ber_dupbv( &glue->e_name, &pdn );
1126 ber_dupbv( &glue->e_nname, &ndn );
1127 ch_free( dn.bv_val );
1128 ch_free( pdn.bv_val );
1129 ch_free( ndn.bv_val );
1131 a = ch_calloc( 1, sizeof( Attribute ));
1132 a->a_desc = slap_schema.si_ad_objectClass;
1133 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1134 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1135 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1136 a->a_vals[2].bv_len = 0;
1137 a->a_vals[2].bv_val = NULL;
1138 a->a_next = glue->e_attrs;
1141 a = ch_calloc( 1, sizeof( Attribute ));
1142 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1143 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1144 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1145 a->a_vals[1].bv_len = 0;
1146 a->a_vals[1].bv_val = NULL;
1147 a->a_next = glue->e_attrs;
1150 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1151 op->o_req_dn = e->e_name;
1152 op->o_req_ndn = e->e_nname;
1154 rc = be->be_add ( op, &rs );
1155 if ( rc == LDAP_SUCCESS )
1156 be_entry_release_w( op, e );
1161 op->o_req_dn = glue->e_name;
1162 op->o_req_ndn = glue->e_nname;
1164 rc = be->be_add ( op, &rs );
1165 if ( rc == LDAP_SUCCESS ) {
1166 be_entry_release_w( op, glue );
1168 /* incl. ALREADY EXIST */
1178 syncrepl_updateCookie(
1182 struct berval *syncCookie
1185 Backend *be = op->o_bd;
1186 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1188 Modifications *mlnext;
1190 Modifications *modlist;
1191 Modifications **modtail = &modlist;
1193 struct berval* ocbva = NULL;
1194 struct berval* cnbva = NULL;
1195 struct berval* ssbva = NULL;
1196 struct berval* scbva = NULL;
1201 char txtbuf[SLAP_TEXT_BUFLEN];
1202 size_t textlen = sizeof txtbuf;
1207 struct berval sub_bv = { 0, NULL };
1208 struct berval psubrdn = { 0, NULL };
1211 SlapReply rs = {REP_RESULT};
1213 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1214 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1215 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1216 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1218 /* update in memory cookie */
1219 if ( si->syncCookie != NULL ) {
1220 ber_bvfree( si->syncCookie );
1222 si->syncCookie = ber_dupbv( NULL, syncCookie );
1223 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1224 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1225 ber_str2bv( "syncConsumerSubentry",
1226 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1227 ocbva[3].bv_len = 0;
1228 ocbva[3].bv_val = NULL;
1230 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1231 mod->sml_op = LDAP_MOD_REPLACE;
1232 mod->sml_next = NULL;
1233 mod->sml_desc = NULL;
1234 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1235 mod->sml_bvalues = ocbva;
1236 mod->sml_nvalues = ocbva;
1238 modtail = &mod->sml_next;
1240 sprintf( substr, "syncrepl%d", si->id );
1241 sprintf( rdnstr, "cn=%s", substr );
1242 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1243 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1244 cnbva[1].bv_len = 0;
1245 cnbva[1].bv_val = NULL;
1246 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1247 mod->sml_op = LDAP_MOD_REPLACE;
1248 mod->sml_next = NULL;
1249 mod->sml_desc = NULL;
1250 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1251 mod->sml_bvalues = cnbva;
1252 mod->sml_nvalues = cnbva;
1254 modtail = &mod->sml_next;
1256 ber_dupbv( &scbva[0], si->syncCookie );
1257 scbva[1].bv_len = 0;
1258 scbva[1].bv_val = NULL;
1259 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1260 mod->sml_op = LDAP_MOD_REPLACE;
1261 mod->sml_next = NULL;
1262 mod->sml_desc = NULL;
1263 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1264 1, &mod->sml_type );
1265 mod->sml_bvalues = scbva;
1266 mod->sml_nvalues = scbva;
1268 modtail = &mod->sml_next;
1270 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1271 ssbva[1].bv_len = 0;
1272 ssbva[1].bv_val = NULL;
1273 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1274 mod->sml_op = LDAP_MOD_REPLACE;
1275 mod->sml_next = NULL;
1276 mod->sml_desc = NULL;
1277 ber_str2bv( "subtreeSpecification",
1278 strlen("subtreeSpecification"), 1, &mod->sml_type );
1279 mod->sml_bvalues = ssbva;
1280 mod->sml_nvalues = ssbva;
1282 modtail = &mod->sml_next;
1284 rc = slap_mods_check_syncrepl( op, &modlist, &text, txtbuf, textlen, NULL );
1286 if ( rc != LDAP_SUCCESS ) {
1288 LDAP_LOG( OPERATION, ERR,
1289 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1291 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1296 op->o_tag = LDAP_REQ_ADD;
1297 rc = slap_mods_opattrs_syncrepl( op, modlist, modtail, &text,txtbuf, textlen );
1299 if( rc != LDAP_SUCCESS ) {
1301 LDAP_LOG( OPERATION, ERR,
1302 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1304 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1309 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1311 build_new_dn( &sub_bv, pdn, &psubrdn );
1312 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, op->o_tmpmemctx );
1313 ch_free( sub_bv.bv_val );
1314 ch_free( psubrdn.bv_val );
1318 rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
1320 if( rc != LDAP_SUCCESS ) {
1322 LDAP_LOG( OPERATION, ERR,
1323 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1325 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1330 cb.sc_response = null_callback;
1333 op->o_callback = &cb;
1334 op->o_req_dn = e->e_name;
1335 op->o_req_ndn = e->e_nname;
1337 /* update persistent cookie */
1338 update_cookie_retry:
1339 op->o_tag = LDAP_REQ_MODIFY;
1340 op->orm_modlist = modlist;
1341 rc = be->be_modify( op, &rs );
1342 if ( rc != LDAP_SUCCESS ) {
1343 if ( rc == LDAP_REFERRAL ||
1344 rc == LDAP_NO_SUCH_OBJECT ||
1345 rc == DB_NOTFOUND ) {
1346 op->o_tag = LDAP_REQ_ADD;
1348 rc = be->be_add( op, &rs );
1349 if ( rc != LDAP_SUCCESS ) {
1350 if ( rc == LDAP_ALREADY_EXISTS ) {
1351 goto update_cookie_retry;
1352 } else if ( rc == LDAP_REFERRAL ||
1353 rc == LDAP_NO_SUCH_OBJECT ||
1354 rc == DB_NOTFOUND ) {
1356 LDAP_LOG( OPERATION, ERR,
1357 "cookie will be non-persistent\n",
1360 Debug( LDAP_DEBUG_ANY,
1361 "cookie will be non-persistent\n",
1366 LDAP_LOG( OPERATION, ERR,
1367 "be_add failed (%d)\n",
1370 Debug( LDAP_DEBUG_ANY,
1371 "be_add failed (%d)\n",
1380 LDAP_LOG( OPERATION, ERR,
1381 "be_modify failed (%d)\n", rc, 0, 0 );
1383 Debug( LDAP_DEBUG_ANY,
1384 "be_modify failed (%d)\n", rc, 0, 0 );
1394 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1395 mlnext = ml->sml_next;
1404 int slap_mods_check_syncrepl(
1406 Modifications **mlp,
1413 Backend *be = op->o_bd;
1414 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1415 AttributeDescription** descs;
1417 Modifications *prevml = NULL;
1418 Modifications *nextml = NULL;
1419 Modifications *ml = *mlp;
1421 while ( ml != NULL ) {
1422 AttributeDescription *ad = NULL;
1424 /* convert to attribute description */
1425 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1427 if( rc != LDAP_SUCCESS ) {
1428 snprintf( textbuf, textlen, "%s: %s",
1429 ml->sml_type.bv_val, *text );
1436 if ( si->lastmod == LASTMOD_REQ ) {
1437 descs = del_descs_lastmod;
1442 for ( i = 0; descs[i] != NULL; i++ ) {
1443 if ( ad == descs[i] ) {
1444 if ( prevml == NULL ) {
1445 mlp = &ml->sml_next;
1448 prevml->sml_next = ml->sml_next;
1450 slap_mod_free( &ml->sml_mod, 0 );
1451 nextml = ml->sml_next;
1458 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1459 && !slap_ad_is_binary( ad )) {
1460 /* attribute requires binary transfer */
1461 snprintf( textbuf, textlen,
1462 "%s: requires ;binary transfer",
1463 ml->sml_type.bv_val );
1465 return LDAP_UNDEFINED_TYPE;
1468 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1469 && slap_ad_is_binary( ad )) {
1470 /* attribute requires binary transfer */
1471 snprintf( textbuf, textlen,
1472 "%s: disallows ;binary transfer",
1473 ml->sml_type.bv_val );
1475 return LDAP_UNDEFINED_TYPE;
1478 if( slap_ad_is_tag_range( ad )) {
1479 /* attribute requires binary transfer */
1480 snprintf( textbuf, textlen,
1481 "%s: inappropriate use of tag range option",
1482 ml->sml_type.bv_val );
1484 return LDAP_UNDEFINED_TYPE;
1487 if ( is_at_obsolete( ad->ad_type ) &&
1488 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1490 * attribute is obsolete,
1491 * only allow replace/delete with no values
1493 snprintf( textbuf, textlen,
1494 "%s: attribute is obsolete",
1495 ml->sml_type.bv_val );
1497 return LDAP_CONSTRAINT_VIOLATION;
1503 if( ml->sml_values != NULL ) {
1505 slap_syntax_validate_func *validate =
1506 ad->ad_type->sat_syntax->ssyn_validate;
1507 slap_syntax_transform_func *pretty =
1508 ad->ad_type->sat_syntax->ssyn_pretty;
1510 if( !pretty && !validate ) {
1511 *text = "no validator for syntax";
1512 snprintf( textbuf, textlen,
1513 "%s: no validator for syntax %s",
1514 ml->sml_type.bv_val,
1515 ad->ad_type->sat_syntax->ssyn_oid );
1517 return LDAP_INVALID_SYNTAX;
1521 * check that each value is valid per syntax
1522 * and pretty if appropriate
1524 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1525 struct berval pval = {0, NULL};
1527 rc = pretty( ad->ad_type->sat_syntax,
1528 &ml->sml_values[nvals], &pval, ctx );
1530 rc = validate( ad->ad_type->sat_syntax,
1531 &ml->sml_values[nvals] );
1535 snprintf( textbuf, textlen,
1536 "%s: value #%ld invalid per syntax",
1537 ml->sml_type.bv_val, (long) nvals );
1539 return LDAP_INVALID_SYNTAX;
1543 ber_memfree( ml->sml_values[nvals].bv_val );
1544 ml->sml_values[nvals] = pval;
1549 * a rough single value check... an additional check is needed
1550 * to catch add of single value to existing single valued attribute
1552 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1553 && nvals > 1 && is_at_single_value( ad->ad_type )) {
1554 snprintf( textbuf, textlen,
1555 "%s: multiple values provided",
1556 ml->sml_type.bv_val );
1558 return LDAP_CONSTRAINT_VIOLATION;
1561 if( nvals && ad->ad_type->sat_equality &&
1562 ad->ad_type->sat_equality->smr_normalize ) {
1563 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1564 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1565 rc = ad->ad_type->sat_equality->smr_normalize( 0,
1566 ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1567 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1570 LDAP_LOG( OPERATION, DETAIL1,
1571 "str2entry: NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1573 Debug( LDAP_DEBUG_ANY,
1574 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1576 snprintf( textbuf, textlen,
1577 "%s: value #%ld normalization failed",
1578 ml->sml_type.bv_val, (long) nvals );
1583 ml->sml_nvalues[nvals].bv_val = NULL;
1584 ml->sml_nvalues[nvals].bv_len = 0;
1591 return LDAP_SUCCESS;
1595 int slap_mods_opattrs_syncrepl(
1597 Modifications *mods,
1598 Modifications **modtail,
1600 char *textbuf, size_t textlen )
1602 struct berval name = {0, NULL};
1603 struct berval timestamp = {0, NULL};
1604 struct berval csn = {0, NULL};
1605 struct berval nname = {0, NULL};
1606 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1607 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1609 Backend *be = op->o_bd;
1610 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1612 int mop = LDAP_MOD_REPLACE;
1614 assert( modtail != NULL );
1615 assert( *modtail == NULL );
1617 if( si->lastmod == LASTMOD_GEN ) {
1619 time_t now = slap_get_time();
1621 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1622 ltm = gmtime( &now );
1623 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1625 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1626 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1627 csn.bv_val = csnbuf;
1629 timestamp.bv_val = timebuf;
1630 timestamp.bv_len = strlen(timebuf);
1632 if( op->o_dn.bv_len == 0 ) {
1633 name.bv_val = SLAPD_ANONYMOUS;
1634 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1642 if( op->o_tag == LDAP_REQ_ADD ) {
1643 struct berval tmpval = {0, NULL};
1645 if( global_schemacheck ) {
1646 int rc = mods_structural_class( mods, &tmpval,
1647 text, textbuf, textlen );
1648 if( rc != LDAP_SUCCESS ) {
1652 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1654 mod->sml_type.bv_val = NULL;
1655 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1656 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1657 ber_dupbv( &mod->sml_values[0], &tmpval );
1658 mod->sml_values[1].bv_len = 0;
1659 mod->sml_values[1].bv_val = NULL;
1660 assert( mod->sml_values[0].bv_val );
1661 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1662 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1663 mod->sml_nvalues[1].bv_len = 0;
1664 mod->sml_nvalues[1].bv_val = NULL;
1665 assert( mod->sml_nvalues[0].bv_val );
1667 modtail = &mod->sml_next;
1670 if( si->lastmod == LASTMOD_GEN ) {
1671 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1673 mod->sml_type.bv_val = NULL;
1674 mod->sml_desc = slap_schema.si_ad_creatorsName;
1675 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1676 ber_dupbv( &mod->sml_values[0], &name );
1677 mod->sml_values[1].bv_len = 0;
1678 mod->sml_values[1].bv_val = NULL;
1679 assert( mod->sml_values[0].bv_val );
1680 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1681 ber_dupbv( &mod->sml_nvalues[0], &nname );
1682 mod->sml_nvalues[1].bv_len = 0;
1683 mod->sml_nvalues[1].bv_val = NULL;
1684 assert( mod->sml_nvalues[0].bv_val );
1686 modtail = &mod->sml_next;
1688 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1690 mod->sml_type.bv_val = NULL;
1691 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1692 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1693 ber_dupbv( &mod->sml_values[0], ×tamp );
1694 mod->sml_values[1].bv_len = 0;
1695 mod->sml_values[1].bv_val = NULL;
1696 assert( mod->sml_values[0].bv_val );
1697 mod->sml_nvalues = NULL;
1699 modtail = &mod->sml_next;
1703 if( si->lastmod == LASTMOD_GEN ) {
1704 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1706 mod->sml_type.bv_val = NULL;
1707 mod->sml_desc = slap_schema.si_ad_entryCSN;
1708 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1709 ber_dupbv( &mod->sml_values[0], &csn );
1710 mod->sml_values[1].bv_len = 0;
1711 mod->sml_values[1].bv_val = NULL;
1712 assert( mod->sml_values[0].bv_val );
1713 mod->sml_nvalues = NULL;
1715 modtail = &mod->sml_next;
1717 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1719 mod->sml_type.bv_val = NULL;
1720 mod->sml_desc = slap_schema.si_ad_modifiersName;
1721 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1722 ber_dupbv( &mod->sml_values[0], &name );
1723 mod->sml_values[1].bv_len = 0;
1724 mod->sml_values[1].bv_val = NULL;
1725 assert( mod->sml_values[0].bv_val );
1726 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1727 ber_dupbv( &mod->sml_nvalues[0], &nname );
1728 mod->sml_nvalues[1].bv_len = 0;
1729 mod->sml_nvalues[1].bv_val = NULL;
1730 assert( mod->sml_nvalues[0].bv_val );
1732 modtail = &mod->sml_next;
1734 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1736 mod->sml_type.bv_val = NULL;
1737 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1738 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1739 ber_dupbv( &mod->sml_values[0], ×tamp );
1740 mod->sml_values[1].bv_len = 0;
1741 mod->sml_values[1].bv_val = NULL;
1742 assert( mod->sml_values[0].bv_val );
1743 mod->sml_nvalues = NULL;
1745 modtail = &mod->sml_next;
1749 return LDAP_SUCCESS;
1754 int slap_mods2entry_syncrepl(
1755 Modifications *mods,
1759 char *textbuf, size_t textlen )
1761 Attribute **tail = &(*e)->e_attrs;
1762 assert( *tail == NULL );
1766 for( ; mods != NULL; mods = mods->sml_next ) {
1769 assert( mods->sml_desc != NULL );
1771 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1773 if( attr != NULL ) {
1774 #define SLURPD_FRIENDLY
1775 #ifdef SLURPD_FRIENDLY
1779 snprintf( textbuf, textlen,
1780 "attribute '%s' provided more than once",
1781 mods->sml_desc->ad_cname.bv_val );
1782 return LDAP_TYPE_OR_VALUE_EXISTS;
1785 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1788 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1793 attr->a_vals = ch_realloc( attr->a_vals,
1794 sizeof( struct berval ) * (i+j) );
1796 /* should check for duplicates */
1798 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1799 sizeof( struct berval ) * j );
1801 if( attr->a_nvals ) {
1802 attr->a_nvals = ch_realloc( attr->a_nvals,
1803 sizeof( struct berval ) * (i+j) );
1805 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1806 sizeof( struct berval ) * j );
1808 /* trim the mods array */
1809 ch_free( mods->sml_nvalues );
1810 mods->sml_nvalues = NULL;
1815 snprintf( textbuf, textlen,
1816 "attribute '%s' provided more than once",
1817 mods->sml_desc->ad_cname.bv_val );
1818 return LDAP_TYPE_OR_VALUE_EXISTS;
1822 if( mods->sml_values[1].bv_val != NULL ) {
1823 /* check for duplicates */
1825 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1827 /* check if the values we're adding already exist */
1828 if( mr == NULL || !mr->smr_match ) {
1829 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1830 /* test asserted values against themselves */
1831 for( j = 0; j < i; j++ ) {
1832 if ( bvmatch( &mods->sml_bvalues[i],
1833 &mods->sml_bvalues[j] ) ) {
1834 /* value exists already */
1835 snprintf( textbuf, textlen,
1836 "%s: value #%d provided more than once",
1837 mods->sml_desc->ad_cname.bv_val, j );
1838 return LDAP_TYPE_OR_VALUE_EXISTS;
1845 const char *text = NULL;
1846 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1848 rc = modify_check_duplicates( mods->sml_desc, mr,
1849 NULL, mods->sml_bvalues, 0,
1850 &text, textbuf, sizeof( textbuf ) );
1852 if ( rc != LDAP_SUCCESS ) {
1858 attr = ch_calloc( 1, sizeof(Attribute) );
1860 /* move ad to attr structure */
1861 attr->a_desc = mods->sml_desc;
1863 /* move values to attr structure */
1864 /* should check for duplicates */
1865 attr->a_vals = mods->sml_values;
1867 attr->a_nvals = mods->sml_nvalues;
1870 tail = &attr->a_next;
1873 return LDAP_SUCCESS;
1877 avl_ber_bvfree( void *bv )
1882 if ( ((struct berval *)bv)->bv_val != NULL ) {
1883 ber_memfree ( ((struct berval *)bv)->bv_val );
1885 ber_memfree ( (char *) bv );
1894 syncinfo_t *si = op->o_callback->sc_private;
1897 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1899 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1902 si->syncCookie = NULL;
1904 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1906 return LDAP_SUCCESS;
1910 nonpresent_callback(
1915 syncinfo_t *si = op->o_callback->sc_private;
1918 struct berval* present_uuid = NULL;
1920 SlapReply rs_cb = {REP_RESULT};
1921 struct nonpresent_entry *np_entry;
1923 if ( rs->sr_type == REP_RESULT ) {
1924 count = avl_free( si->presentlist, avl_ber_bvfree );
1925 return LDAP_SUCCESS;
1926 } else if ( rs->sr_type == REP_SEARCH ) {
1927 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1932 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1934 if ( present_uuid == NULL ) {
1935 np_entry = (struct nonpresent_entry *)
1936 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1937 np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1938 np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1939 LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1941 avl_delete( &si->presentlist,
1942 &a->a_vals[0], syncuuid_cmp );
1944 return LDAP_SUCCESS;
1946 return LDAP_SUCCESS;
1957 if ( rs->sr_err != LDAP_SUCCESS &&
1958 rs->sr_err != LDAP_REFERRAL &&
1959 rs->sr_err != LDAP_ALREADY_EXISTS &&
1960 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1961 rs->sr_err != DB_NOTFOUND ) {
1963 LDAP_LOG( OPERATION, ERR,
1964 "null_callback : error code 0x%x\n",
1967 Debug( LDAP_DEBUG_ANY,
1968 "null_callback : error code 0x%x\n",
1972 return LDAP_SUCCESS;