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"
39 syncrepl_message_to_entry ( LDAP *, Operation *, LDAPMessage *,
40 Modifications **, int*, struct berval *, struct berval * );
43 syncrepl_entry( LDAP *, Operation*, Entry*, Modifications*,
44 int, struct berval*, struct berval*, int );
47 syncrepl_del_nonpresent( LDAP *, Operation * );
50 syncrepl_add_glue( LDAP *, Operation*, Entry*, Modifications*, int,
51 struct berval*, struct berval* );
54 syncrepl_updateCookie( LDAP *, Operation *, struct berval *, struct berval * );
57 slap_mods_check_syncrepl( Operation *, Modifications **,
58 const char **, char *, size_t, void *ctx );
61 slap_mods_opattrs_syncrepl( Operation *, Modifications *, Modifications **,
62 const char **, char *, size_t );
65 slap_mods2entry_syncrepl( Modifications *, Entry **, int,
66 const char **, char *, size_t );
68 /* callback functions */
69 static int cookie_callback( struct slap_op *, struct slap_rep * );
70 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
71 static int null_callback( struct slap_op *, struct slap_rep * );
73 static AttributeDescription **add_descs;
74 static AttributeDescription **add_descs_lastmod;
75 static AttributeDescription **del_descs;
76 static AttributeDescription **del_descs_lastmod;
81 add_descs = ch_malloc( 2 * sizeof( AttributeDescription * ));
82 add_descs[0] = slap_schema.si_ad_objectClass;
85 add_descs_lastmod = ch_malloc( 7 * sizeof( AttributeDescription * ));
86 add_descs_lastmod[0] = slap_schema.si_ad_objectClass;
87 add_descs_lastmod[1] = slap_schema.si_ad_creatorsName;
88 add_descs_lastmod[2] = slap_schema.si_ad_modifiersName;
89 add_descs_lastmod[3] = slap_schema.si_ad_createTimestamp;
90 add_descs_lastmod[4] = slap_schema.si_ad_modifyTimestamp;
91 add_descs_lastmod[5] = slap_schema.si_ad_entryCSN;
92 add_descs_lastmod[6] = NULL;
94 del_descs = ch_malloc( 9 * sizeof( AttributeDescription * ));
95 del_descs[0] = slap_schema.si_ad_structuralObjectClass;
96 del_descs[1] = slap_schema.si_ad_subschemaSubentry;
97 del_descs[2] = slap_schema.si_ad_hasSubordinates;
98 del_descs[3] = slap_schema.si_ad_creatorsName;
99 del_descs[4] = slap_schema.si_ad_modifiersName;
100 del_descs[5] = slap_schema.si_ad_createTimestamp;
101 del_descs[6] = slap_schema.si_ad_modifyTimestamp;
102 del_descs[7] = slap_schema.si_ad_entryCSN;
105 del_descs_lastmod = ch_malloc( 4 * sizeof( AttributeDescription * ));
106 del_descs_lastmod[0] = slap_schema.si_ad_structuralObjectClass;
107 del_descs_lastmod[1] = slap_schema.si_ad_subschemaSubentry;
108 del_descs_lastmod[2] = slap_schema.si_ad_hasSubordinates;
109 del_descs_lastmod[3] = NULL;
118 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
120 SlapReply rs = {REP_RESULT};
123 LDAPControl **sctrls = NULL;
124 LDAPControl **rctrls = NULL;
125 LDAPControl *rctrlp = NULL;
126 BerElement *sync_ber = NULL;
127 struct berval *sync_bvalp = NULL;
129 BerElement *ctrl_ber = NULL;
130 BerElement *res_ber = NULL;
133 LDAPMessage *res = NULL;
134 LDAPMessage *msg = NULL;
138 int nresponses, nreferences, nextended, npartial;
139 int nresponses_psearch;
141 int cancel_msgid = -1;
143 struct berval *retdata = NULL;
145 int sync_info_arrived = 0;
149 struct berval syncUUID = { 0, NULL };
150 struct berval syncCookie = { 0, NULL };
155 int syncinfo_arrived = 0;
156 int cancel_response = 0;
159 AttributeDescription** descs = NULL;
171 struct berval base_bv = { 0, NULL };
172 struct berval pbase = { 0, NULL };
173 struct berval nbase = { 0, NULL };
174 struct berval sub_bv = { 0, NULL };
175 struct berval psubrdn = { 0, NULL };
176 struct berval nsubrdn = { 0, NULL };
177 struct berval psub = { 0, NULL };
178 struct berval nsub = { 0, NULL };
180 Modifications *modlist = NULL;
181 Modifications *ml, *mlnext;
182 char *def_filter_str = NULL;
185 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
187 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
193 if ( abs(si->type) != LDAP_SYNC_REFRESH_ONLY &&
194 abs(si->type) != LDAP_SYNC_REFRESH_AND_PERSIST ) {
198 /* Init connection to master */
200 if ( ldap_is_ldap_url( si->masteruri )) {
201 rc = ldap_initialize( &ld, si->masteruri );
202 if ( rc != LDAP_SUCCESS ) {
204 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
205 "ldap_initialize failed (%s)\n",
206 si->masteruri, 0, 0 );
208 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
209 "ldap_initialize failed (%s)\n",
210 si->masteruri, 0, 0 );
214 ld = ldap_init( si->mastername, si->masterport );
217 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
218 "ldap_init failed (%s:%s)\n",
219 si->mastername, si->masterport, 0 );
221 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
222 "ldap_init failed (%s:%s)\n",
223 si->mastername, si->masterport, 0 );
228 op.o_protocol = LDAP_VERSION3;
229 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &op.o_protocol );
234 rc = ldap_start_tls_s( ld, NULL, NULL );
235 if( rc != LDAP_SUCCESS ) {
237 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
238 "%s: ldap_start_tls failed (%d)\n",
239 si->tls == TLS_CRITICAL ? "Error" : "Warning",
242 Debug( LDAP_DEBUG_ANY,
243 "%s: ldap_start_tls failed (%d)\n",
244 si->tls == TLS_CRITICAL ? "Error" : "Warning",
247 if( si->tls == TLS_CRITICAL )
252 if ( si->bindmethod == LDAP_AUTH_SASL ) {
253 #ifdef HAVE_CYRUS_SASL
256 if ( si->secprops != NULL ) {
257 int err = ldap_set_option( ld,
258 LDAP_OPT_X_SASL_SECPROPS, si->secprops);
260 if( err != LDAP_OPT_SUCCESS ) {
262 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
263 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
264 si->mastername, si->secprops, 0 );
266 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
267 "(%s,SECPROPS,\"%s\") failed!\n",
268 si->mastername, si->secprops, NULL );
274 defaults = lutil_sasl_defaults( ld,
281 rc = ldap_sasl_interactive_bind_s( ld,
289 if ( rc != LDAP_SUCCESS ) {
291 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
292 "ldap_sasl_interactive_bind_s failed (%d)\n",
295 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
296 "ldap_sasl_interactive_bind_s failed (%d)\n",
301 #else /* HAVE_CYRUS_SASL */
302 fprintf( stderr, "not compiled with SASL support\n" );
306 rc = ldap_bind_s( ld, si->binddn, si->passwd, si->bindmethod );
307 if ( rc != LDAP_SUCCESS ) {
309 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
310 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
312 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
313 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
321 op.o_tmpmemctx = NULL; /* FIXME : to use per-thread mem context */
322 op.o_tag = LDAP_REQ_SEARCH;
323 op.o_dn = si->updatedn;
324 op.o_ndn = si->updatedn;
326 op.o_time = slap_get_time();
327 op.o_managedsait = 1;
328 op.o_threadctx = si->ctx;
331 op.o_connid = op.o_conn->c_connid;
332 op.ors_scope = LDAP_SCOPE_BASE;
333 op.ors_deref = LDAP_DEREF_NEVER;
336 op.ors_attrsonly = 0;
338 op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
339 ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
343 conn.c_send_ldap_result = slap_send_ldap_result;
344 conn.c_send_search_entry = slap_send_search_entry;
345 conn.c_send_search_reference = slap_send_search_reference;
347 /* get syncrepl cookie of shadow replica from subentry */
348 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
349 dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
351 sprintf( substr, "cn=syncrepl%d", si->id );
352 ber_str2bv( substr, strlen(substr), 1, &sub_bv );
353 dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn, op.o_tmpmemctx );
355 build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
356 build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
358 ch_free( base_bv.bv_val );
359 ch_free( pbase.bv_val );
360 ch_free( nbase.bv_val );
361 ch_free( sub_bv.bv_val );
362 ch_free( psubrdn.bv_val );
363 ch_free( nsubrdn.bv_val );
365 /* set callback function */
366 cb.sc_response = cookie_callback;
369 /* search subentry to retrieve cookie */
370 si->syncCookie = NULL;
371 be->be_search( &op, &rs );
373 ch_free( op.o_req_dn.bv_val );
374 ch_free( op.o_req_ndn.bv_val );
375 filter_free( op.ors_filter );
376 ch_free( op.ors_filterstr.bv_val );
378 psub = be->be_nsuffix[0];
380 /* setup LDAP SYNC control */
381 sync_ber = ber_alloc_t( LBER_USE_DER );
382 ber_set_option( sync_ber, LBER_OPT_BER_MEMCTX, op.o_tmpmemctx );
384 if ( si->syncCookie ) {
385 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
387 ber_printf( sync_ber, "{e}", abs(si->type) );
390 if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
391 ber_free( sync_ber, 1 );
394 ber_free( sync_ber, 1 );
396 sctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), op.o_tmpmemctx );
398 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
399 c[0].ldctl_value = (*sync_bvalp);
400 c[0].ldctl_iscritical = si->type < 0;
404 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
405 c[1].ldctl_value.bv_val = si->authzId;
406 c[1].ldctl_value.bv_len = strlen( si->authzId );
407 c[1].ldctl_iscritical = 1;
415 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, sctrls );
417 ber_bvfree( sync_bvalp );
420 if ( err != LDAP_OPT_SUCCESS )
421 fprintf( stderr, "Could not set controls : %d\n", err );
423 /* Delete Attributes */
424 if ( si->lastmod == LASTMOD_REQ ) {
425 descs = del_descs_lastmod;
430 for ( i = 0; descs[i] != NULL; i++ ) {
431 for ( j = 0; si->attrs[j] != NULL; j++ ) {
432 if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
433 ch_free( si->attrs[j] );
434 for ( k = j; si->attrs[k] != NULL; k++ ) {
435 si->attrs[k] = si->attrs[k+1];
443 for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
445 if ( si->lastmod == LASTMOD_REQ ) {
446 descs = add_descs_lastmod;
451 for ( i = 0; descs[i] != NULL; i++ ) {
452 tmp = ( char ** ) ch_realloc( si->attrs,
453 ( n + 2 ) * sizeof( char * ));
456 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
458 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
462 si->attrs[ n++ ] = ch_strdup ( descs[i]->ad_cname.bv_val );
463 si->attrs[ n ] = NULL;
466 /* Send LDAP SYNC search */
468 rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
469 si->attrs, si->attrsonly, NULL, NULL,
472 if( rc != LDAP_SUCCESS ) {
473 fprintf( stderr, "syncrepl: ldap_search_ext: %s (%d)\n",
474 ldap_err2string( rc ), rc );
478 while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
480 for ( msg = ldap_first_message( ld, res );
482 msg = ldap_next_message( ld, msg ) )
484 switch( ldap_msgtype( msg ) ) {
485 case LDAP_RES_SEARCH_ENTRY:
486 entry = syncrepl_message_to_entry( ld, &op, msg,
487 &modlist, &syncstate, &syncUUID, &syncCookie );
488 rc_efree = syncrepl_entry( ld, &op, entry, modlist,
489 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
490 if ( syncCookie.bv_len ) {
491 syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
495 for ( ml = modlist; ml != NULL; ml = mlnext ) {
496 mlnext = ml->sml_next;
501 case LDAP_RES_SEARCH_REFERENCE:
503 LDAP_LOG( OPERATION, ERR,
504 "do_syncrepl : reference received\n", 0, 0, 0 );
506 Debug( LDAP_DEBUG_ANY,
507 "do_syncrepl : reference received\n", 0, 0, 0 );
511 case LDAP_RES_SEARCH_RESULT:
512 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
515 ctrl_ber = ber_alloc_t( LBER_USE_DER );
516 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op.o_tmpmemctx );
517 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
518 ber_reset( ctrl_ber, 1 );
520 ber_scanf( ctrl_ber, "{" );
521 if ( ber_peek_tag( ctrl_ber, &len )
522 == LDAP_SYNC_TAG_COOKIE ) {
523 ber_scanf( ctrl_ber, "o", &syncCookie );
526 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
527 if ( cancel_response ) {
528 if ( syncCookie.bv_len ) {
529 ber_bvfree( si->syncCookie );
530 si->syncCookie = ber_dupbv( NULL, &syncCookie );
533 ber_free( ctrl_ber, 1 );
538 ber_free( ctrl_ber, 1 );
542 if ( syncCookie.bv_len ) {
543 syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
545 syncrepl_del_nonpresent( ld, &op );
547 ber_free( ctrl_ber, 1 );
552 case LDAP_RES_INTERMEDIATE_RESP:
553 ldap_parse_intermediate_resp_result( ld, msg,
554 &retoid, &retdata, 0 );
555 if ( !strcmp( retoid, LDAP_SYNC_INFO ) ) {
556 sync_info_arrived = 1;
557 res_ber = ber_init( retdata );
558 ber_scanf( res_ber, "{e", &syncstate );
560 if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
561 syncrepl_del_nonpresent( ld, &op );
562 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
564 LDAP_LOG( OPERATION, ERR,
565 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
567 Debug( LDAP_DEBUG_ANY,
568 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
572 if ( ber_peek_tag( res_ber, &len )
573 == LDAP_SYNC_TAG_COOKIE ) {
574 ber_scanf( res_ber, "o}", &syncCookie );
575 if ( syncCookie.bv_len ) {
576 ber_bvfree( si->syncCookie );
577 si->syncCookie = ber_dupbv( NULL, &syncCookie );
580 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
582 LDAP_LOG( OPERATION, ERR,
583 "do_syncrepl : cookie required\n", 0, 0, 0 );
585 Debug( LDAP_DEBUG_ANY,
586 "do_syncrepl : cookie required\n", 0, 0, 0 );
591 ldap_memfree( retoid );
592 ber_bvfree( retdata );
593 ber_free( res_ber, 1 );
597 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
598 " unknown intermediate "
599 "response\n", 0, 0, 0 );
601 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
602 "unknown intermediate "
603 "response\n", 0, 0, 0 );
605 ldap_memfree( retoid );
606 ber_bvfree( retdata );
612 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
613 "unknown message\n", 0, 0, 0 );
615 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
616 "unknown message\n", 0, 0, 0 );
627 LDAP_LOG( OPERATION, ERR,
628 "do_syncrepl : unknown result\n", 0, 0, 0 );
630 Debug( LDAP_DEBUG_ANY,
631 "do_syncrepl : unknown result\n", 0, 0, 0 );
637 if ( syncCookie.bv_val )
638 ch_free( syncCookie.bv_val );
639 if ( syncUUID.bv_val )
640 ch_free( syncUUID.bv_val );
649 syncrepl_message_to_entry(
653 Modifications **modlist,
655 struct berval *syncUUID,
656 struct berval *syncCookie
660 BerElement *ber = NULL;
662 struct berval bv = {0, NULL};
665 Modifications **modtail = modlist;
666 Backend *be = op->o_bd;
669 char txtbuf[SLAP_TEXT_BUFLEN];
670 size_t textlen = sizeof txtbuf;
672 struct berval **bvals = NULL;
674 struct berval bdn = {0, NULL};
676 struct berval empty_bv = { 0, NULL };
680 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
684 LDAPControl** rctrls = NULL;
685 BerElement* ctrl_ber;
689 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
691 LDAP_LOG( OPERATION, ERR,
692 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
694 Debug( LDAP_DEBUG_ANY,
695 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
700 op->o_tag = LDAP_REQ_ADD;
702 rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
704 if ( rc != LDAP_SUCCESS ) {
706 LDAP_LOG( OPERATION, ERR,
707 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
709 Debug( LDAP_DEBUG_ANY,
710 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
715 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
716 dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, op->o_tmpmemctx );
720 for ( rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues);
722 rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues))
724 if ( tmp.sml_type.bv_val == NULL ) break;
726 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
728 mod->sml_op = LDAP_MOD_REPLACE;
729 mod->sml_next = NULL;
730 mod->sml_desc = NULL;
731 mod->sml_type = tmp.sml_type;
732 mod->sml_bvalues = tmp.sml_bvalues;
733 mod->sml_nvalues = tmp.sml_bvalues;
736 modtail = &mod->sml_next;
739 if ( ber_scanf( ber, "}") == LBER_ERROR ) {
741 LDAP_LOG( OPERATION, ERR,
742 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
744 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
751 tmpber = ldap_get_message_ber( msg );
752 ber = ber_dup( tmpber );
754 ber_scanf( ber, "{xx" );
756 rc = ldap_int_get_controls( ber, &rctrls );
758 if ( rc != LDAP_SUCCESS ) {
760 LDAP_LOG( OPERATION, ERR,
761 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
763 Debug( LDAP_DEBUG_ANY,
764 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
771 ctrl_ber = ber_alloc_t( LBER_USE_DER );
772 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op->o_tmpmemctx );
773 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
774 ber_reset( ctrl_ber, 1 );
775 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
776 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
777 ber_scanf( ctrl_ber, "o}", syncCookie );
779 ber_free( ctrl_ber, 1 );
782 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
783 " rctrls absent\n", 0, 0, 0 );
785 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
786 " rctrls absent\n", 0, 0, 0 );
790 if ( *syncstate == LDAP_SYNC_PRESENT ) {
795 if ( *modlist == NULL ) {
797 LDAP_LOG( OPERATION, ERR,
798 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
800 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
805 rc = slap_mods_check_syncrepl( op, modlist, &text, txtbuf, textlen, NULL );
807 if ( rc != LDAP_SUCCESS ) {
809 LDAP_LOG( OPERATION, ERR,
810 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
812 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
818 rc = slap_mods_opattrs_syncrepl( op, *modlist, modtail,
819 &text,txtbuf, textlen );
821 if( rc != LDAP_SUCCESS ) {
823 LDAP_LOG( OPERATION, ERR,
824 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
826 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
832 rc = slap_mods2entry_syncrepl( *modlist, &e, 1, &text, txtbuf, textlen );
833 if( rc != LDAP_SUCCESS ) {
835 LDAP_LOG( OPERATION, ERR,
836 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
838 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
851 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
853 const struct berval *uuid1 = v_uuid1;
854 const struct berval *uuid2 = v_uuid2;
855 int rc = uuid1->bv_len - uuid2->bv_len;
857 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
865 Modifications* modlist,
867 struct berval* syncUUID,
868 struct berval* syncCookie,
872 Backend *be = op->o_bd;
873 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
875 struct berval csn_bv = {0, NULL};
876 struct berval *syncuuid_bv = NULL;
877 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
879 SlapReply rs = {REP_RESULT};
882 #if 0 /* FIXME : UUID search required first */
884 struct berval filterstr_bv;
891 ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
892 syncuuid_bv = ber_dupbv( NULL, syncUUID );
893 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
894 syncuuid_cmp, avl_dup_error );
897 if ( syncstate == LDAP_SYNC_PRESENT ) {
904 if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
905 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
908 #if 0 /* FIXME : UUID search required first */
909 filterstr = (char *) sl_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1, op->o_tmpmemctx );
910 strcpy( filterstr, "entryUUID=" );
911 strcat( filterstr, syncUUID->bv_val );
915 si->syncUUID = syncUUID;
917 #if 0 /* FIXME : UUID search required first */
918 filter = str2filter( filterstr );
919 ber_str2bv( filterstr, strlen(filterstr), 1, &filterstr_bv );
920 ch_free( filterstr );
923 op->o_req_dn = e->e_name;
924 op->o_req_ndn = e->e_nname;
926 op->o_callback = &cb;
927 cb.sc_response = null_callback;
930 switch ( syncstate ) {
932 case LDAP_SYNC_MODIFY :
934 op->o_tag = LDAP_REQ_MODIFY;
935 op->orm_modlist = modlist;
936 rc = be->be_modify( op, &rs );
937 if ( rc != LDAP_SUCCESS ) {
938 if ( rc == LDAP_REFERRAL ||
939 rc == LDAP_NO_SUCH_OBJECT ||
940 rc == DB_NOTFOUND ) {
941 op->o_tag = LDAP_REQ_ADD;
943 rc = be->be_add( op, &rs );
944 if ( rc != LDAP_SUCCESS ) {
945 if ( rc == LDAP_ALREADY_EXISTS ) {
947 } else if ( rc == LDAP_REFERRAL ||
948 rc == LDAP_NO_SUCH_OBJECT ||
949 rc == DB_NOTFOUND ) {
950 syncrepl_add_glue(ld, op, e,
952 syncUUID, syncCookie);
955 LDAP_LOG( OPERATION, ERR,
956 "be_add failed (%d)\n",
959 Debug( LDAP_DEBUG_ANY,
960 "be_add failed (%d)\n",
969 LDAP_LOG( OPERATION, ERR,
970 "be_modify failed (%d)\n", rc, 0, 0 );
972 Debug( LDAP_DEBUG_ANY,
973 "be_modify failed (%d)\n", rc, 0, 0 );
980 case LDAP_SYNC_DELETE :
981 op->o_tag = LDAP_REQ_DELETE;
982 be->be_delete( op, &rs );
987 LDAP_LOG( OPERATION, ERR,
988 "unknown syncstate\n", 0, 0, 0 );
990 Debug( LDAP_DEBUG_ANY,
991 "unknown syncstate\n", 0, 0, 0 );
998 syncrepl_del_nonpresent(
1003 Backend* be = op->o_bd;
1004 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1006 struct berval base_bv = {0, NULL};
1008 SlapReply rs = {REP_RESULT};
1009 struct berval filterstr_bv = {0, NULL};
1010 struct nonpresent_entry *np_list, *np_prev;
1012 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
1013 dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1014 ch_free( base_bv.bv_val );
1016 filter = str2filter( si->filterstr );
1018 cb.sc_response = nonpresent_callback;
1021 op->o_callback = &cb;
1022 op->o_tag = LDAP_REQ_SEARCH;
1023 op->ors_scope = si->scope;
1024 op->ors_deref = LDAP_DEREF_NEVER;
1025 op->ors_slimit = -1;
1026 op->ors_tlimit = -1;
1027 op->ors_attrsonly = 0;
1028 op->ors_attrs = NULL;
1029 op->ors_filter = filter;
1030 ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1032 be->be_search( op, &rs );
1034 if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1035 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1036 while ( np_list != NULL ) {
1037 LDAP_LIST_REMOVE( np_list, np_link );
1039 np_list = LDAP_LIST_NEXT( np_list, np_link );
1040 op->o_tag = LDAP_REQ_DELETE;
1041 op->o_callback = &cb;
1042 cb.sc_response = null_callback;
1044 op->o_req_dn = *np_prev->dn;
1045 op->o_req_ndn = *np_prev->ndn;
1046 op->o_bd->be_delete( op, &rs );
1047 ber_bvfree( np_prev->dn );
1048 ber_bvfree( np_prev->ndn );
1049 op->o_req_dn.bv_val = NULL;
1050 op->o_req_ndn.bv_val = NULL;
1055 if ( op->o_req_dn.bv_val )
1056 ch_free( op->o_req_dn.bv_val );
1057 if ( op->o_req_ndn.bv_val )
1058 ch_free( op->o_req_ndn.bv_val );
1059 filter_free( op->ors_filter );
1060 ch_free( op->ors_filterstr.bv_val );
1069 Modifications* modlist,
1071 struct berval* syncUUID,
1072 struct berval* syncCookie
1075 Backend *be = op->o_bd;
1076 syncinfo_t *si = op->o_callback->sc_private;
1077 struct berval uuid_bv = {0, NULL};
1081 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1084 struct berval dn = {0, NULL};
1085 struct berval pdn = {0, NULL};
1086 struct berval ndn = {0, NULL};
1087 struct berval rdn = {0, NULL};
1089 SlapReply rs = {REP_RESULT};
1090 Connection *conn = op->o_conn;
1092 op->o_tag = LDAP_REQ_ADD;
1093 op->o_callback = &cb;
1094 cb.sc_response = null_callback;
1097 ber_dupbv( &dn, &e->e_nname );
1098 ber_dupbv( &pdn, &e->e_nname );
1100 while ( !be_issuffix ( be, &pdn )) {
1101 dnParent( &dn, &pdn );
1102 ch_free( dn.bv_val );
1103 ber_dupbv( &dn, &pdn );
1107 for ( i = 0; i <= levels; i++ ) {
1108 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1109 ch_free( dn.bv_val );
1110 ch_free( pdn.bv_val );
1111 ber_dupbv( &dn, &e->e_nname );
1112 ber_dupbv( &pdn, &e->e_nname );
1114 for ( k = 0; k < j; k++ ) {
1115 dnParent( &dn, &pdn );
1116 ch_free( dn.bv_val );
1117 ber_dupbv( &dn, &pdn );
1120 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1121 ber_dupbv( &glue->e_name, &pdn );
1122 ber_dupbv( &glue->e_nname, &ndn );
1123 ch_free( dn.bv_val );
1124 ch_free( pdn.bv_val );
1125 ch_free( ndn.bv_val );
1127 a = ch_calloc( 1, sizeof( Attribute ));
1128 a->a_desc = slap_schema.si_ad_objectClass;
1129 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1130 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1131 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1132 a->a_vals[2].bv_len = 0;
1133 a->a_vals[2].bv_val = NULL;
1134 a->a_next = glue->e_attrs;
1137 a = ch_calloc( 1, sizeof( Attribute ));
1138 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1139 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1140 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1141 a->a_vals[1].bv_len = 0;
1142 a->a_vals[1].bv_val = NULL;
1143 a->a_next = glue->e_attrs;
1146 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1147 op->o_req_dn = e->e_name;
1148 op->o_req_ndn = e->e_nname;
1150 rc = be->be_add ( op, &rs );
1151 if ( rc == LDAP_SUCCESS )
1152 be_entry_release_w( op, e );
1157 op->o_req_dn = glue->e_name;
1158 op->o_req_ndn = glue->e_nname;
1160 rc = be->be_add ( op, &rs );
1161 if ( rc == LDAP_SUCCESS ) {
1162 be_entry_release_w( op, glue );
1164 /* incl. ALREADY EXIST */
1174 syncrepl_updateCookie(
1178 struct berval *syncCookie
1181 Backend *be = op->o_bd;
1182 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1184 Modifications *mlnext;
1186 Modifications *modlist;
1187 Modifications **modtail = &modlist;
1189 struct berval* ocbva = NULL;
1190 struct berval* cnbva = NULL;
1191 struct berval* ssbva = NULL;
1192 struct berval* scbva = NULL;
1197 char txtbuf[SLAP_TEXT_BUFLEN];
1198 size_t textlen = sizeof txtbuf;
1203 struct berval sub_bv = { 0, NULL };
1204 struct berval psubrdn = { 0, NULL };
1207 SlapReply rs = {REP_RESULT};
1209 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1210 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1211 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1212 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1214 /* update in memory cookie */
1215 if ( si->syncCookie != NULL ) {
1216 ber_bvfree( si->syncCookie );
1218 si->syncCookie = ber_dupbv( NULL, syncCookie );
1219 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1220 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1221 ber_str2bv( "syncConsumerSubentry",
1222 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1223 ocbva[3].bv_len = 0;
1224 ocbva[3].bv_val = NULL;
1226 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1227 mod->sml_op = LDAP_MOD_REPLACE;
1228 mod->sml_next = NULL;
1229 mod->sml_desc = NULL;
1230 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1231 mod->sml_bvalues = ocbva;
1232 mod->sml_nvalues = ocbva;
1234 modtail = &mod->sml_next;
1236 sprintf( substr, "syncrepl%d", si->id );
1237 sprintf( rdnstr, "cn=%s", substr );
1238 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1239 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1240 cnbva[1].bv_len = 0;
1241 cnbva[1].bv_val = NULL;
1242 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1243 mod->sml_op = LDAP_MOD_REPLACE;
1244 mod->sml_next = NULL;
1245 mod->sml_desc = NULL;
1246 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1247 mod->sml_bvalues = cnbva;
1248 mod->sml_nvalues = cnbva;
1250 modtail = &mod->sml_next;
1252 ber_dupbv( &scbva[0], si->syncCookie );
1253 scbva[1].bv_len = 0;
1254 scbva[1].bv_val = NULL;
1255 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1256 mod->sml_op = LDAP_MOD_REPLACE;
1257 mod->sml_next = NULL;
1258 mod->sml_desc = NULL;
1259 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1260 1, &mod->sml_type );
1261 mod->sml_bvalues = scbva;
1262 mod->sml_nvalues = scbva;
1264 modtail = &mod->sml_next;
1266 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1267 ssbva[1].bv_len = 0;
1268 ssbva[1].bv_val = NULL;
1269 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1270 mod->sml_op = LDAP_MOD_REPLACE;
1271 mod->sml_next = NULL;
1272 mod->sml_desc = NULL;
1273 ber_str2bv( "subtreeSpecification",
1274 strlen("subtreeSpecification"), 1, &mod->sml_type );
1275 mod->sml_bvalues = ssbva;
1276 mod->sml_nvalues = ssbva;
1278 modtail = &mod->sml_next;
1280 rc = slap_mods_check_syncrepl( op, &modlist, &text, txtbuf, textlen, NULL );
1282 if ( rc != LDAP_SUCCESS ) {
1284 LDAP_LOG( OPERATION, ERR,
1285 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1287 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1292 op->o_tag = LDAP_REQ_ADD;
1293 rc = slap_mods_opattrs_syncrepl( op, modlist, modtail, &text,txtbuf, textlen );
1295 if( rc != LDAP_SUCCESS ) {
1297 LDAP_LOG( OPERATION, ERR,
1298 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1300 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1305 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1307 build_new_dn( &sub_bv, pdn, &psubrdn );
1308 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, op->o_tmpmemctx );
1309 ch_free( sub_bv.bv_val );
1310 ch_free( psubrdn.bv_val );
1314 rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
1316 if( rc != LDAP_SUCCESS ) {
1318 LDAP_LOG( OPERATION, ERR,
1319 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1321 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1326 cb.sc_response = null_callback;
1329 op->o_callback = &cb;
1330 op->o_req_dn = e->e_name;
1331 op->o_req_ndn = e->e_nname;
1333 /* update persistent cookie */
1334 update_cookie_retry:
1335 op->o_tag = LDAP_REQ_MODIFY;
1336 op->orm_modlist = modlist;
1337 rc = be->be_modify( op, &rs );
1338 if ( rc != LDAP_SUCCESS ) {
1339 if ( rc == LDAP_REFERRAL ||
1340 rc == LDAP_NO_SUCH_OBJECT ||
1341 rc == DB_NOTFOUND ) {
1342 op->o_tag = LDAP_REQ_ADD;
1344 rc = be->be_add( op, &rs );
1345 if ( rc != LDAP_SUCCESS ) {
1346 if ( rc == LDAP_ALREADY_EXISTS ) {
1347 goto update_cookie_retry;
1348 } else if ( rc == LDAP_REFERRAL ||
1349 rc == LDAP_NO_SUCH_OBJECT ||
1350 rc == DB_NOTFOUND ) {
1352 LDAP_LOG( OPERATION, ERR,
1353 "cookie will be non-persistent\n",
1356 Debug( LDAP_DEBUG_ANY,
1357 "cookie will be non-persistent\n",
1362 LDAP_LOG( OPERATION, ERR,
1363 "be_add failed (%d)\n",
1366 Debug( LDAP_DEBUG_ANY,
1367 "be_add failed (%d)\n",
1376 LDAP_LOG( OPERATION, ERR,
1377 "be_modify failed (%d)\n", rc, 0, 0 );
1379 Debug( LDAP_DEBUG_ANY,
1380 "be_modify failed (%d)\n", rc, 0, 0 );
1390 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1391 mlnext = ml->sml_next;
1400 int slap_mods_check_syncrepl(
1402 Modifications **mlp,
1409 Backend *be = op->o_bd;
1410 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1411 AttributeDescription** descs;
1413 Modifications *prevml = NULL;
1414 Modifications *nextml = NULL;
1415 Modifications *ml = *mlp;
1417 while ( ml != NULL ) {
1418 AttributeDescription *ad = NULL;
1420 /* convert to attribute description */
1421 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1423 if( rc != LDAP_SUCCESS ) {
1424 snprintf( textbuf, textlen, "%s: %s",
1425 ml->sml_type.bv_val, *text );
1432 if ( si->lastmod == LASTMOD_REQ ) {
1433 descs = del_descs_lastmod;
1438 for ( i = 0; descs[i] != NULL; i++ ) {
1439 if ( ad == descs[i] ) {
1440 if ( prevml == NULL ) {
1441 mlp = &ml->sml_next;
1444 prevml->sml_next = ml->sml_next;
1446 slap_mod_free( &ml->sml_mod, 0 );
1447 nextml = ml->sml_next;
1454 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1455 && !slap_ad_is_binary( ad )) {
1456 /* attribute requires binary transfer */
1457 snprintf( textbuf, textlen,
1458 "%s: requires ;binary transfer",
1459 ml->sml_type.bv_val );
1461 return LDAP_UNDEFINED_TYPE;
1464 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1465 && slap_ad_is_binary( ad )) {
1466 /* attribute requires binary transfer */
1467 snprintf( textbuf, textlen,
1468 "%s: disallows ;binary transfer",
1469 ml->sml_type.bv_val );
1471 return LDAP_UNDEFINED_TYPE;
1474 if( slap_ad_is_tag_range( ad )) {
1475 /* attribute requires binary transfer */
1476 snprintf( textbuf, textlen,
1477 "%s: inappropriate use of tag range option",
1478 ml->sml_type.bv_val );
1480 return LDAP_UNDEFINED_TYPE;
1483 if ( is_at_obsolete( ad->ad_type ) &&
1484 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1486 * attribute is obsolete,
1487 * only allow replace/delete with no values
1489 snprintf( textbuf, textlen,
1490 "%s: attribute is obsolete",
1491 ml->sml_type.bv_val );
1493 return LDAP_CONSTRAINT_VIOLATION;
1499 if( ml->sml_values != NULL ) {
1501 slap_syntax_validate_func *validate =
1502 ad->ad_type->sat_syntax->ssyn_validate;
1503 slap_syntax_transform_func *pretty =
1504 ad->ad_type->sat_syntax->ssyn_pretty;
1506 if( !pretty && !validate ) {
1507 *text = "no validator for syntax";
1508 snprintf( textbuf, textlen,
1509 "%s: no validator for syntax %s",
1510 ml->sml_type.bv_val,
1511 ad->ad_type->sat_syntax->ssyn_oid );
1513 return LDAP_INVALID_SYNTAX;
1517 * check that each value is valid per syntax
1518 * and pretty if appropriate
1520 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1521 struct berval pval = {0, NULL};
1523 rc = pretty( ad->ad_type->sat_syntax,
1524 &ml->sml_values[nvals], &pval, ctx );
1526 rc = validate( ad->ad_type->sat_syntax,
1527 &ml->sml_values[nvals] );
1531 snprintf( textbuf, textlen,
1532 "%s: value #%ld invalid per syntax",
1533 ml->sml_type.bv_val, (long) nvals );
1535 return LDAP_INVALID_SYNTAX;
1539 ber_memfree( ml->sml_values[nvals].bv_val );
1540 ml->sml_values[nvals] = pval;
1545 * a rough single value check... an additional check is needed
1546 * to catch add of single value to existing single valued attribute
1548 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1549 && nvals > 1 && is_at_single_value( ad->ad_type )) {
1550 snprintf( textbuf, textlen,
1551 "%s: multiple values provided",
1552 ml->sml_type.bv_val );
1554 return LDAP_CONSTRAINT_VIOLATION;
1557 if( nvals && ad->ad_type->sat_equality &&
1558 ad->ad_type->sat_equality->smr_normalize ) {
1559 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1560 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1561 rc = ad->ad_type->sat_equality->smr_normalize( 0,
1562 ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1563 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1566 LDAP_LOG( OPERATION, DETAIL1,
1567 "str2entry: NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1569 Debug( LDAP_DEBUG_ANY,
1570 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1572 snprintf( textbuf, textlen,
1573 "%s: value #%ld normalization failed",
1574 ml->sml_type.bv_val, (long) nvals );
1579 ml->sml_nvalues[nvals].bv_val = NULL;
1580 ml->sml_nvalues[nvals].bv_len = 0;
1587 return LDAP_SUCCESS;
1591 int slap_mods_opattrs_syncrepl(
1593 Modifications *mods,
1594 Modifications **modtail,
1596 char *textbuf, size_t textlen )
1598 struct berval name = {0, NULL};
1599 struct berval timestamp = {0, NULL};
1600 struct berval csn = {0, NULL};
1601 struct berval nname = {0, NULL};
1602 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1603 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1605 Backend *be = op->o_bd;
1606 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1608 int mop = LDAP_MOD_REPLACE;
1610 assert( modtail != NULL );
1611 assert( *modtail == NULL );
1613 if( si->lastmod == LASTMOD_GEN ) {
1615 time_t now = slap_get_time();
1617 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1618 ltm = gmtime( &now );
1619 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1621 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1622 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1623 csn.bv_val = csnbuf;
1625 timestamp.bv_val = timebuf;
1626 timestamp.bv_len = strlen(timebuf);
1628 if( op->o_dn.bv_len == 0 ) {
1629 name.bv_val = SLAPD_ANONYMOUS;
1630 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1638 if( op->o_tag == LDAP_REQ_ADD ) {
1639 struct berval tmpval = {0, NULL};
1641 if( global_schemacheck ) {
1642 int rc = mods_structural_class( mods, &tmpval,
1643 text, textbuf, textlen );
1644 if( rc != LDAP_SUCCESS ) {
1648 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1650 mod->sml_type.bv_val = NULL;
1651 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1652 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1653 ber_dupbv( &mod->sml_values[0], &tmpval );
1654 mod->sml_values[1].bv_len = 0;
1655 mod->sml_values[1].bv_val = NULL;
1656 assert( mod->sml_values[0].bv_val );
1657 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1658 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1659 mod->sml_nvalues[1].bv_len = 0;
1660 mod->sml_nvalues[1].bv_val = NULL;
1661 assert( mod->sml_nvalues[0].bv_val );
1663 modtail = &mod->sml_next;
1666 if( si->lastmod == LASTMOD_GEN ) {
1667 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1669 mod->sml_type.bv_val = NULL;
1670 mod->sml_desc = slap_schema.si_ad_creatorsName;
1671 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1672 ber_dupbv( &mod->sml_values[0], &name );
1673 mod->sml_values[1].bv_len = 0;
1674 mod->sml_values[1].bv_val = NULL;
1675 assert( mod->sml_values[0].bv_val );
1676 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1677 ber_dupbv( &mod->sml_nvalues[0], &nname );
1678 mod->sml_nvalues[1].bv_len = 0;
1679 mod->sml_nvalues[1].bv_val = NULL;
1680 assert( mod->sml_nvalues[0].bv_val );
1682 modtail = &mod->sml_next;
1684 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1686 mod->sml_type.bv_val = NULL;
1687 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1688 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1689 ber_dupbv( &mod->sml_values[0], ×tamp );
1690 mod->sml_values[1].bv_len = 0;
1691 mod->sml_values[1].bv_val = NULL;
1692 assert( mod->sml_values[0].bv_val );
1693 mod->sml_nvalues = NULL;
1695 modtail = &mod->sml_next;
1699 if( si->lastmod == LASTMOD_GEN ) {
1700 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1702 mod->sml_type.bv_val = NULL;
1703 mod->sml_desc = slap_schema.si_ad_entryCSN;
1704 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1705 ber_dupbv( &mod->sml_values[0], &csn );
1706 mod->sml_values[1].bv_len = 0;
1707 mod->sml_values[1].bv_val = NULL;
1708 assert( mod->sml_values[0].bv_val );
1709 mod->sml_nvalues = NULL;
1711 modtail = &mod->sml_next;
1713 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1715 mod->sml_type.bv_val = NULL;
1716 mod->sml_desc = slap_schema.si_ad_modifiersName;
1717 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1718 ber_dupbv( &mod->sml_values[0], &name );
1719 mod->sml_values[1].bv_len = 0;
1720 mod->sml_values[1].bv_val = NULL;
1721 assert( mod->sml_values[0].bv_val );
1722 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1723 ber_dupbv( &mod->sml_nvalues[0], &nname );
1724 mod->sml_nvalues[1].bv_len = 0;
1725 mod->sml_nvalues[1].bv_val = NULL;
1726 assert( mod->sml_nvalues[0].bv_val );
1728 modtail = &mod->sml_next;
1730 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1732 mod->sml_type.bv_val = NULL;
1733 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1734 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1735 ber_dupbv( &mod->sml_values[0], ×tamp );
1736 mod->sml_values[1].bv_len = 0;
1737 mod->sml_values[1].bv_val = NULL;
1738 assert( mod->sml_values[0].bv_val );
1739 mod->sml_nvalues = NULL;
1741 modtail = &mod->sml_next;
1745 return LDAP_SUCCESS;
1750 int slap_mods2entry_syncrepl(
1751 Modifications *mods,
1755 char *textbuf, size_t textlen )
1757 Attribute **tail = &(*e)->e_attrs;
1758 assert( *tail == NULL );
1762 for( ; mods != NULL; mods = mods->sml_next ) {
1765 assert( mods->sml_desc != NULL );
1767 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1769 if( attr != NULL ) {
1770 #define SLURPD_FRIENDLY
1771 #ifdef SLURPD_FRIENDLY
1775 snprintf( textbuf, textlen,
1776 "attribute '%s' provided more than once",
1777 mods->sml_desc->ad_cname.bv_val );
1778 return LDAP_TYPE_OR_VALUE_EXISTS;
1781 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1784 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1789 attr->a_vals = ch_realloc( attr->a_vals,
1790 sizeof( struct berval ) * (i+j) );
1792 /* should check for duplicates */
1794 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1795 sizeof( struct berval ) * j );
1797 if( attr->a_nvals ) {
1798 attr->a_nvals = ch_realloc( attr->a_nvals,
1799 sizeof( struct berval ) * (i+j) );
1801 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1802 sizeof( struct berval ) * j );
1804 /* trim the mods array */
1805 ch_free( mods->sml_nvalues );
1806 mods->sml_nvalues = NULL;
1811 snprintf( textbuf, textlen,
1812 "attribute '%s' provided more than once",
1813 mods->sml_desc->ad_cname.bv_val );
1814 return LDAP_TYPE_OR_VALUE_EXISTS;
1818 if( mods->sml_values[1].bv_val != NULL ) {
1819 /* check for duplicates */
1821 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1823 /* check if the values we're adding already exist */
1824 if( mr == NULL || !mr->smr_match ) {
1825 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1826 /* test asserted values against themselves */
1827 for( j = 0; j < i; j++ ) {
1828 if ( bvmatch( &mods->sml_bvalues[i],
1829 &mods->sml_bvalues[j] ) ) {
1830 /* value exists already */
1831 snprintf( textbuf, textlen,
1832 "%s: value #%d provided more than once",
1833 mods->sml_desc->ad_cname.bv_val, j );
1834 return LDAP_TYPE_OR_VALUE_EXISTS;
1841 const char *text = NULL;
1842 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1844 rc = modify_check_duplicates( mods->sml_desc, mr,
1845 NULL, mods->sml_bvalues, 0,
1846 &text, textbuf, sizeof( textbuf ) );
1848 if ( rc != LDAP_SUCCESS ) {
1854 attr = ch_calloc( 1, sizeof(Attribute) );
1856 /* move ad to attr structure */
1857 attr->a_desc = mods->sml_desc;
1859 /* move values to attr structure */
1860 /* should check for duplicates */
1861 attr->a_vals = mods->sml_values;
1863 attr->a_nvals = mods->sml_nvalues;
1866 tail = &attr->a_next;
1869 return LDAP_SUCCESS;
1873 avl_ber_bvfree( void *bv )
1878 if ( ((struct berval *)bv)->bv_val != NULL ) {
1879 ber_memfree ( ((struct berval *)bv)->bv_val );
1881 ber_memfree ( (char *) bv );
1890 syncinfo_t *si = op->o_callback->sc_private;
1893 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1895 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1898 si->syncCookie = NULL;
1900 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1902 return LDAP_SUCCESS;
1906 nonpresent_callback(
1911 syncinfo_t *si = op->o_callback->sc_private;
1914 struct berval* present_uuid = NULL;
1916 SlapReply rs_cb = {REP_RESULT};
1917 struct nonpresent_entry *np_entry;
1919 if ( rs->sr_type == REP_RESULT ) {
1920 count = avl_free( si->presentlist, avl_ber_bvfree );
1921 return LDAP_SUCCESS;
1922 } else if ( rs->sr_type == REP_SEARCH ) {
1923 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1928 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1930 if ( present_uuid == NULL ) {
1931 np_entry = (struct nonpresent_entry *)
1932 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1933 np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1934 np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1935 LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1937 avl_delete( &si->presentlist,
1938 &a->a_vals[0], syncuuid_cmp );
1940 return LDAP_SUCCESS;
1942 return LDAP_SUCCESS;
1953 if ( rs->sr_err != LDAP_SUCCESS &&
1954 rs->sr_err != LDAP_REFERRAL &&
1955 rs->sr_err != LDAP_ALREADY_EXISTS &&
1956 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1957 rs->sr_err != DB_NOTFOUND ) {
1959 LDAP_LOG( OPERATION, ERR,
1960 "null_callback : error code 0x%x\n",
1963 Debug( LDAP_DEBUG_ANY,
1964 "null_callback : error code 0x%x\n",
1968 return LDAP_SUCCESS;