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"
36 /* This is for accessing ld->def_conn */
37 #define LDAP_R_COMPILE
38 #include "../../../libraries/libldap/ldap-int.h"
41 static Entry* syncrepl_message_to_entry( LDAP *, Backend*, LDAPMessage *, Modifications *, int*, struct berval *, struct berval * );
43 static int syncrepl_entry( LDAP *, Backend*, Entry*, Modifications*, int,
44 struct berval*, struct berval*, int );
46 static int syncrepl_del_nonpresent( LDAP *, Backend * );
48 static int cookie_entry_callback( Backend *, Connection *, Operation *, Entry *,
49 AttributeName *, int, LDAPControl ** );
51 static void syncrepl_add_glue( LDAP *, Connection*, Operation*, Backend*,
52 Entry*, Modifications*, int,
53 struct berval*, struct berval* );
55 static void syncrepl_updateCookie( LDAP *, Backend *, struct berval *,
58 static int null_entry_callback( Backend*, Connection*, Operation*,
59 Entry*, AttributeName*, int, LDAPControl** );
60 static void null_result_callback( Connection*, Operation*, ber_int_t, const char*,
61 const char*, BerVarray, LDAPControl**, int );
62 static int null_reference_callback( Backend*, Connection*, Operation*,
63 Entry*, BerVarray, LDAPControl**, BerVarray* );
64 static void null_response_callback( Connection*, Operation*, ber_tag_t, ber_int_t,
65 ber_int_t, const char*, const char*, BerVarray,
66 const char*, struct berval*,struct berval*,LDAPControl**);
68 static int nonpresent_entry_callback( Backend*, Connection*, Operation*,
69 Entry*, AttributeName*, int, LDAPControl** );
70 static void nonpresent_result_callback( Connection*, Operation*, ber_int_t,
71 const char*, const char*, BerVarray, LDAPControl**, int );
73 int slap_mods_check_syncrepl( Backend *, Modifications **, const char **, char *, size_t );
75 int slap_mods_opattrs_syncrepl( Backend *, Operation *, Modifications *,
76 Modifications **, const char **, char *, size_t );
78 int slap_mods2entry_syncrepl( Modifications *, Entry **, int, const char **, char *, size_t );
80 static AttributeDescription **add_descs;
81 static AttributeDescription **add_descs_lastmod;
82 static AttributeDescription **del_descs;
83 static AttributeDescription **del_descs_lastmod;
88 add_descs = ch_malloc( 2 * sizeof( AttributeDescription * ));
89 add_descs[0] = slap_schema.si_ad_objectClass;
92 add_descs_lastmod = ch_malloc( 7 * sizeof( AttributeDescription * ));
93 add_descs_lastmod[0] = slap_schema.si_ad_objectClass;
94 add_descs_lastmod[1] = slap_schema.si_ad_creatorsName;
95 add_descs_lastmod[2] = slap_schema.si_ad_modifiersName;
96 add_descs_lastmod[3] = slap_schema.si_ad_createTimestamp;
97 add_descs_lastmod[4] = slap_schema.si_ad_modifyTimestamp;
98 add_descs_lastmod[5] = slap_schema.si_ad_entryCSN;
99 add_descs_lastmod[6] = NULL;
101 del_descs = ch_malloc( 9 * sizeof( AttributeDescription * ));
102 del_descs[0] = slap_schema.si_ad_structuralObjectClass;
103 del_descs[1] = slap_schema.si_ad_subschemaSubentry;
104 del_descs[2] = slap_schema.si_ad_hasSubordinates;
105 del_descs[3] = slap_schema.si_ad_creatorsName;
106 del_descs[4] = slap_schema.si_ad_modifiersName;
107 del_descs[5] = slap_schema.si_ad_createTimestamp;
108 del_descs[6] = slap_schema.si_ad_modifyTimestamp;
109 del_descs[7] = slap_schema.si_ad_entryCSN;
112 del_descs_lastmod = ch_malloc( 4 * sizeof( AttributeDescription * ));
113 del_descs_lastmod[0] = slap_schema.si_ad_structuralObjectClass;
114 del_descs_lastmod[1] = slap_schema.si_ad_subschemaSubentry;
115 del_descs_lastmod[2] = slap_schema.si_ad_hasSubordinates;
116 del_descs_lastmod[3] = NULL;
125 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
127 LDAPControl c[2], **sctrls = NULL, **rctrls = NULL, *rctrlp;
128 BerElement *sync_ber;
129 struct berval *sync_bvalp;
131 BerElement *ctrl_ber;
135 LDAPMessage *res = NULL, *msg;
139 int nresponses, nreferences, nextended, npartial;
140 int nresponses_psearch;
142 int cancel_msgid = -1;
144 struct berval *retdata = NULL;
146 int sync_info_arrived = 0;
150 struct berval syncUUID;
151 struct berval syncCookie;
157 int syncinfo_arrived = 0;
158 int cancel_response = 0;
161 AttributeDescription** descs;
169 struct berval base_bv = { 0, NULL };
170 struct berval pbase = { 0, NULL };
171 struct berval nbase = { 0, NULL };
172 struct berval sub_bv = { 0, NULL };
173 struct berval psubrdn = { 0, NULL };
174 struct berval nsubrdn = { 0, NULL };
175 struct berval psub = { 0, NULL };
176 struct berval nsub = { 0, NULL };
178 Modifications *modlist;
180 char *def_filter_str;
182 struct berval def_filter_bv = { 0, 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 protocol = LDAP_VERSION3;
229 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &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 );
320 conn.c_send_ldap_result = slap_send_ldap_result;
321 conn.c_send_search_entry = slap_send_search_entry;
322 conn.c_send_search_result = slap_send_search_result;
323 conn.c_send_search_reference = slap_send_search_reference;
327 /* get syncrepl cookie of shadow replica from subentry */
328 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
329 dnPrettyNormal(0, &base_bv, &pbase, &nbase);
331 sprintf( substr, "cn=syncrepl%d", si->id );
332 ber_str2bv( substr, strlen(substr), 1, &sub_bv );
333 dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn );
335 build_new_dn( &psub, &pbase, &psubrdn );
336 build_new_dn( &nsub, &nbase, &nsubrdn );
338 op.o_tag = LDAP_REQ_SEARCH;
339 op.o_protocol = LDAP_VERSION3;
340 ber_dupbv( &op.o_ndn, &be->be_rootndn );
342 op.o_time = slap_get_time();
343 op.o_managedsait = 1;
344 op.o_threadctx = si->ctx;
346 /* set callback function */
347 cb.sc_sendentry = cookie_entry_callback;
348 cb.sc_sresult = null_result_callback;
349 cb.sc_response = null_response_callback;
350 cb.sc_sendreference = null_reference_callback;
354 /* search subentry to retrieve cookie */
355 si->syncCookie = NULL;
356 def_filter = str2filter( def_filter_str = "(objectClass=*)" );
357 ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
359 be->be_search( be, si->conn, &op, &psub, &nsub, LDAP_SCOPE_BASE,
360 LDAP_DEREF_NEVER, -1, -1, def_filter, &def_filter_bv,
363 /* setup LDAP SYNC control */
364 sync_ber = ber_alloc_t( LBER_USE_DER );
366 if ( si->syncCookie ) {
367 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
369 ber_printf( sync_ber, "{e}", abs(si->type) );
372 if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
373 ber_free( sync_ber, 1 );
376 ber_free( sync_ber, 1 );
378 sctrls = (LDAPControl**) ch_calloc( 3, sizeof(LDAPControl*) );
380 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
381 c[0].ldctl_value = (*sync_bvalp);
382 c[0].ldctl_iscritical = si->type < 0;
386 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
387 c[1].ldctl_value.bv_val = si->authzId;
388 c[1].ldctl_value.bv_len = strlen( si->authzId );
389 c[1].ldctl_iscritical = 1;
397 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, sctrls );
399 ber_bvfree( sync_bvalp );
402 if ( err != LDAP_OPT_SUCCESS )
403 fprintf( stderr, "Could not set controls : %d\n", err );
405 /* Delete Attributes */
406 if ( si->lastmod == LASTMOD_REQ ) {
407 descs = del_descs_lastmod;
412 for ( i = 0; descs[i] != NULL; i++ ) {
413 for ( j = 0; si->attrs[j] != NULL; j++ ) {
414 if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
415 ch_free( si->attrs[j] );
416 for ( k = j; si->attrs[k] != NULL; k++ ) {
417 si->attrs[k] = si->attrs[k+1];
425 for ( n = 0; si->attrs[ n ] != NULL; n++ )
428 if ( si->lastmod == LASTMOD_REQ ) {
429 descs = add_descs_lastmod;
434 for ( i = 0; descs[i] != NULL; i++ ) {
435 tmp = ( char ** ) ch_realloc( si->attrs,
436 ( n + 2 ) * sizeof( char * ));
439 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
441 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
445 si->attrs[ n++ ] = strndup( descs[i]->ad_cname.bv_val,
446 descs[i]->ad_cname.bv_len );
447 si->attrs[ n ] = NULL;
450 /* Send LDAP SYNC search */
452 rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
453 si->attrs, si->attrsonly, NULL, NULL,
456 if( rc != LDAP_SUCCESS ) {
457 fprintf( stderr, "syncrepl: ldap_search_ext (%d)\n",
458 ldap_err2string( rc ), rc );
462 while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
464 for ( msg = ldap_first_message( ld, res );
466 msg = ldap_next_message( ld, msg ) )
468 switch( ldap_msgtype( msg ) ) {
469 case LDAP_RES_SEARCH_ENTRY:
470 entry = syncrepl_message_to_entry( ld, be, msg, modlist, &syncstate, &syncUUID, &syncCookie );
474 ctrl_ber = ber_init( &rctrlp->ldctl_value );
475 ber_scanf( ctrl_ber, "{em",
478 if ( ber_peek_tag( ctrl_ber, &len )
479 == LDAP_SYNC_TAG_COOKIE ) {
480 ber_scanf( ctrl_ber, "m}", &syncCookie );
484 LDAP_LOG( OPERATION, ERR,"do_syncrepl: "
488 Debug( LDAP_DEBUG_ANY, "do_syncrepl :"
495 syncrepl_entry( ld, be, entry, modlist,
496 syncstate, &syncUUID,
499 if ( syncCookie.bv_len ) {
500 syncrepl_updateCookie( ld, be, &psub,
503 ber_free( ctrl_ber, 1 );
506 case LDAP_RES_SEARCH_REFERENCE:
508 LDAP_LOG( OPERATION, ERR,
509 "do_syncrepl : reference received\n", 0, 0, 0 );
511 Debug( LDAP_DEBUG_ANY,
512 "do_syncrepl : reference received\n", 0, 0, 0 );
516 case LDAP_RES_SEARCH_RESULT:
517 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
520 ctrl_ber = ber_init( &rctrlp->ldctl_value );
521 ber_scanf( ctrl_ber, "{" );
522 if ( ber_peek_tag( ctrl_ber, &len )
523 == LDAP_SYNC_TAG_COOKIE ) {
524 ber_scanf( ctrl_ber, "m", &syncCookie );
527 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
528 if ( cancel_response ) {
529 if ( syncCookie.bv_len ) {
530 ber_bvfree( si->syncCookie );
531 si->syncCookie = ber_dupbv( NULL, &syncCookie );
538 if ( syncCookie.bv_len ) {
539 syncrepl_updateCookie( ld,
548 case LDAP_RES_INTERMEDIATE_RESP:
549 ldap_parse_intermediate_resp_result( ld, msg,
550 &retoid, &retdata, 0 );
551 if ( !strcmp( retoid, LDAP_SYNC_INFO ) ) {
552 sync_info_arrived = 1;
553 res_ber = ber_init( retdata );
554 ber_scanf( res_ber, "{e", &syncstate );
556 if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
557 syncrepl_del_nonpresent( ld, be );
558 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
560 LDAP_LOG( OPERATION, ERR,
561 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
563 Debug( LDAP_DEBUG_ANY,
564 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
568 if ( ber_peek_tag( res_ber, &len )
569 == LDAP_SYNC_TAG_COOKIE ) {
570 ber_scanf( res_ber, "m}", &syncCookie );
571 if ( syncCookie.bv_len ) {
572 ber_bvfree( si->syncCookie );
573 si->syncCookie = ber_dupbv( NULL, &syncCookie );
576 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
578 LDAP_LOG( OPERATION, ERR,
579 "do_syncrepl : cookie required\n", 0, 0, 0 );
581 Debug( LDAP_DEBUG_ANY,
582 "do_syncrepl : cookie required\n", 0, 0, 0 );
587 ldap_memfree( retoid );
588 ber_bvfree( retdata );
589 ber_free( res_ber, 1 );
593 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
594 " unknown intermediate "
595 "response\n", 0, 0, 0 );
597 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
598 "unknown intermediate "
599 "response\n", 0, 0, 0 );
601 ldap_memfree( retoid );
602 ber_bvfree( retdata );
608 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
609 "unknown message\n", 0, 0, 0 );
611 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
612 "unknown message\n", 0, 0, 0 );
623 LDAP_LOG( OPERATION, ERR,
624 "do_syncrepl : unknown result\n", 0, 0, 0 );
626 Debug( LDAP_DEBUG_ANY,
627 "do_syncrepl : unknown result\n", 0, 0, 0 );
633 sleep(si->interval * 60);
635 // set alarm clock to send signal to slapd
636 // should set the signal handler beforehand
637 // the signal handler re execute do_syncrepl()
647 syncrepl_message_to_entry(
651 Modifications *modlist,
653 struct berval *syncUUID,
654 struct berval *syncCookie
658 BerElement *ber = NULL;
662 Modifications **modtail = &modlist;
665 char txtbuf[SLAP_TEXT_BUFLEN];
666 size_t textlen = sizeof txtbuf;
668 struct berval **bvals;
672 struct berval empty_bv = { 0, NULL };
677 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
681 LDAPControl** rctrls = NULL;
682 BerElement* ctrl_ber;
686 if ( msg->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
688 LDAP_LOG( OPERATION, ERR,
689 "Message type should be entry (%d)", msg->lm_msgtype, 0, 0 );
691 Debug( LDAP_DEBUG_ANY,
692 "Message type should be entry (%d)", msg->lm_msgtype, 0, 0 );
697 op.o_tag = LDAP_REQ_ADD;
698 ber_dupbv( &op.o_ndn, &be->be_rootndn );
700 rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
702 if ( rc != LDAP_SUCCESS ) {
704 LDAP_LOG( OPERATION, ERR,
705 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
707 Debug( LDAP_DEBUG_ANY,
708 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
713 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
714 dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname );
718 for ( rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues);
720 rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues))
722 if ( tmp.sml_type.bv_val == NULL ) break;
724 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
726 mod->sml_op = LDAP_MOD_ADD;
727 mod->sml_next = NULL;
728 mod->sml_desc = NULL;
729 mod->sml_type = tmp.sml_type;
730 mod->sml_bvalues = tmp.sml_bvalues;
733 modtail = &mod->sml_next;
736 if ( ber_scanf( ber, "}") == LBER_ERROR ) {
738 LDAP_LOG( OPERATION, ERR,
739 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
741 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
748 ber_scanf( ber, "{xx" );
750 rc = ldap_int_get_controls( ber, &rctrls );
752 if ( rc != LDAP_SUCCESS ) {
754 LDAP_LOG( OPERATION, ERR,
755 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
757 Debug( LDAP_DEBUG_ANY,
758 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
765 ctrl_ber = ber_init( &rctrlp->ldctl_value );
766 ber_scanf( ctrl_ber, "{em", syncstate, syncUUID );
767 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
768 ber_scanf( ctrl_ber, "m}", syncCookie );
772 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
773 " rctrls absent\n", 0, 0, 0 );
775 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
776 " rctrls absent\n", 0, 0, 0 );
780 if ( *syncstate == LDAP_SYNC_PRESENT ) {
785 if ( modlist == NULL ) {
787 LDAP_LOG( OPERATION, ERR,
788 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
790 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
795 rc = slap_mods_check_syncrepl( be, &modlist, &text, txtbuf, textlen );
797 if ( rc != LDAP_SUCCESS ) {
799 LDAP_LOG( OPERATION, ERR,
800 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
802 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
808 rc = slap_mods_opattrs_syncrepl( be, &op, modlist, modtail,
809 &text,txtbuf, textlen );
811 if( rc != LDAP_SUCCESS ) {
813 LDAP_LOG( OPERATION, ERR,
814 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
816 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
822 rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
823 if( rc != LDAP_SUCCESS ) {
825 LDAP_LOG( OPERATION, ERR,
826 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
828 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
841 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
843 const struct berval *uuid1 = v_uuid1;
844 const struct berval *uuid2 = v_uuid2;
845 int rc = uuid1->bv_len - uuid2->bv_len;
847 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
855 Modifications* modlist,
857 struct berval* syncUUID,
858 struct berval* syncCookie,
862 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
864 struct berval normdn = {0, NULL};
865 struct berval prettydn = {0, NULL};
866 struct berval pbase = {0, NULL};
867 struct berval nbase = {0, NULL};
868 struct berval base_bv = {0, NULL};
869 struct berval csn_bv = {0, NULL};
870 struct berval *syncuuid_bv = NULL;
871 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
877 struct berval filterstr_bv;
882 if ( syncstate == LDAP_SYNC_PRESENT ) {
883 syncuuid_bv = ber_dupbv( NULL, syncUUID );
884 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
885 syncuuid_cmp, avl_dup_error );
889 if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
890 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID );
893 filterstr = (char *) ch_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1 );
894 strcpy( filterstr, "entryUUID=" );
895 strcat( filterstr, syncUUID->bv_val );
898 si->syncUUID = syncUUID;
900 filter = str2filter( filterstr );
901 ber_str2bv( filterstr, strlen(filterstr), 1, &filterstr_bv );
902 ch_free( filterstr );
904 dnPrettyNormal(0, &(e->e_name), &prettydn, &normdn);
906 free(e->e_name.bv_val);
907 free(e->e_nname.bv_val);
908 e->e_name = prettydn;
911 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
912 dnPrettyNormal(0, &base_bv, &pbase, &nbase);
914 op.o_protocol = LDAP_VERSION3;
915 ber_dupbv( &op.o_ndn, &be->be_rootndn );
917 op.o_time = slap_get_time();
918 op.o_managedsait = 1;
919 op.o_threadctx = si->ctx;
921 cb.sc_sendentry = null_entry_callback;
922 cb.sc_sresult = null_result_callback;
923 cb.sc_response = null_response_callback;
924 cb.sc_sendreference = null_reference_callback;
928 switch ( syncstate ) {
930 case LDAP_SYNC_MODIFY :
932 op.o_tag = LDAP_REQ_MODIFY;
933 rc = be->be_modify( be, si->conn, &op,
934 &e->e_name, &e->e_nname, modlist );
935 if ( rc != LDAP_SUCCESS ) {
936 if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT || rc == DB_NOTFOUND ) {
937 op.o_tag = LDAP_REQ_ADD;
938 rc = be->be_add( be, si->conn, &op, e );
939 if ( rc != LDAP_SUCCESS ) {
940 if ( rc == LDAP_ALREADY_EXISTS ) {
942 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT || rc == DB_NOTFOUND ) {
943 syncrepl_add_glue(ld, si->conn,
946 syncUUID, syncCookie);
949 LDAP_LOG( OPERATION, ERR,
950 "be_add failed (%d)\n",
953 Debug( LDAP_DEBUG_ANY,
954 "be_add failed (%d)\n",
961 LDAP_LOG( OPERATION, ERR,
962 "be_modify failed (%d)\n", rc, 0, 0 );
964 Debug( LDAP_DEBUG_ANY,
965 "be_modify failed (%d)\n", rc, 0, 0 );
972 case LDAP_SYNC_DELETE :
973 be->be_delete( be, si->conn, &op, &e->e_name, &e->e_nname );
979 LDAP_LOG( OPERATION, ERR,
980 "unknown syncstate\n", 0, 0, 0 );
982 Debug( LDAP_DEBUG_ANY,
983 "unknown syncstate\n", 0, 0, 0 );
990 syncrepl_del_nonpresent(
995 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
997 struct berval pbase = {0, NULL};
998 struct berval nbase = {0, NULL};
999 struct berval base_bv = {0, NULL};
1002 struct berval filterstr_bv;
1004 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
1005 dnPrettyNormal(0, &base_bv, &pbase, &nbase);
1007 filter = str2filter( si->filterstr );
1009 op.o_tag = LDAP_REQ_SEARCH;
1010 op.o_protocol = LDAP_VERSION3;
1011 ber_dupbv( &op.o_ndn, &be->be_rootndn );
1012 op.o_callback = &cb;
1013 op.o_time = slap_get_time();
1014 op.o_managedsait = 1;
1015 op.o_threadctx = si->ctx;
1017 ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &filterstr_bv );
1019 // si->conn->c_send_ldap_result = slap_send_ldap_result;
1020 // si->conn->c_send_search_entry = slap_send_search_entry;
1021 // si->conn->c_send_search_result = slap_send_search_result;
1022 // si->conn->c_send_search_reference = slap_send_search_reference;
1024 cb.sc_sendentry = nonpresent_entry_callback;
1025 cb.sc_sresult = nonpresent_result_callback;
1026 cb.sc_response = null_response_callback;
1027 cb.sc_sendreference = null_reference_callback;
1031 be->be_search( be, si->conn, &op, &pbase, &nbase, LDAP_SCOPE_BASE,
1032 LDAP_DEREF_NEVER, -1, -1, filter, &filterstr_bv, NULL, 0 );
1036 nonpresent_entry_callback(
1041 AttributeName *attrs,
1046 syncinfo_t *si = op->o_callback->sc_private;
1048 struct berval* present_uuid = NULL;
1051 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
1056 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1058 if ( present_uuid == NULL ) {
1059 op->o_tag = LDAP_REQ_DELETE;
1060 op->o_callback = &cb;
1061 cb.sc_sendentry = null_entry_callback;
1062 cb.sc_sresult = null_result_callback;
1063 cb.sc_response = null_response_callback;
1064 cb.sc_sendreference = null_reference_callback;
1068 be->be_delete( be, si->conn, op, &e->e_name, &e->e_nname );
1070 avl_delete( &si->presentlist, &a->a_vals[0], syncuuid_cmp );
1075 avl_ber_bvfree( void *bv )
1080 if ( ((struct berval *)bv)->bv_val != NULL ) {
1081 LBER_FREE( ((struct berval *)bv)->bv_val );
1083 LBER_FREE( (char *) bv );
1087 nonpresent_result_callback(
1091 const char *matched,
1094 LDAPControl **ctrls,
1098 syncinfo_t *si = op->o_callback->sc_private;
1101 count = avl_free( si->presentlist, avl_ber_bvfree );
1107 cookie_entry_callback(
1112 AttributeName *attrs,
1117 syncinfo_t *si = op->o_callback->sc_private;
1120 a = attr_find( e->e_attrs, slap_schema.si_ad_syncreplCookie );
1123 si->syncCookie = NULL;
1125 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1127 return LDAP_SUCCESS;
1137 Modifications* modlist,
1139 struct berval* syncUUID,
1140 struct berval* syncCookie
1143 syncinfo_t *si = op->o_callback->sc_private;
1144 struct berval uuid_bv = {0, NULL};
1148 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1151 struct berval dn = {0, NULL};
1152 struct berval pdn = {0, NULL};
1153 struct berval ndn = {0, NULL};
1154 struct berval rdn = {0, NULL};
1157 op->o_tag = LDAP_REQ_ADD;
1158 op->o_callback = &cb;
1159 cb.sc_sendentry = null_entry_callback;
1160 cb.sc_sresult = null_result_callback;
1161 cb.sc_response = null_response_callback;
1162 cb.sc_sendreference = null_reference_callback;
1166 ber_dupbv( &dn, &e->e_nname );
1167 ber_dupbv( &pdn, &e->e_nname );
1168 while ( !be_issuffix ( be, &pdn )) {
1169 dnParent( &dn, &pdn );
1170 ber_dupbv( &dn, &pdn );
1174 for ( i = 0; i <= levels; i++ ) {
1175 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1176 ber_dupbv( &dn, &e->e_nname );
1177 ber_dupbv( &pdn, &e->e_nname );
1179 for ( k = 0; k < j; k++ ) {
1180 dnParent( &dn, &pdn );
1181 ber_dupbv( &dn, &pdn );
1184 dnPrettyNormal(0, &dn, &pdn, &ndn);
1185 ber_dupbv( &glue->e_name, &pdn);
1186 ber_dupbv( &glue->e_nname, &ndn);
1188 a = ch_calloc( 1, sizeof( Attribute ));
1189 a->a_desc = slap_schema.si_ad_objectClass;
1190 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1191 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1192 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1193 a->a_vals[2].bv_len = 0;
1194 a->a_vals[2].bv_val = NULL;
1195 a->a_next = glue->e_attrs;
1198 a = ch_calloc( 1, sizeof( Attribute ));
1199 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1200 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1201 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1202 a->a_vals[1].bv_len = 0;
1203 a->a_vals[1].bv_val = NULL;
1204 a->a_next = glue->e_attrs;
1207 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1208 rc = be->be_add ( be, conn, op, e );
1209 if ( rc == LDAP_SUCCESS )
1210 be_entry_release_w( be, conn, op, e );
1215 rc = be->be_add ( be, conn, op, glue );
1216 if ( rc == LDAP_SUCCESS )
1217 be_entry_release_w( be, conn, op, glue );
1227 syncrepl_updateCookie(
1231 struct berval *syncCookie
1234 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1236 Modifications *modlist;
1237 Modifications **modtail = &modlist;
1239 struct berval* ocbva;
1240 struct berval* cnbva;
1241 struct berval* ssbva;
1242 struct berval* scbva;
1246 char txtbuf[SLAP_TEXT_BUFLEN];
1247 size_t textlen = sizeof txtbuf;
1255 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1256 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1257 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1258 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1259 substr = ( char * ) ch_calloc( 64, sizeof( char ));
1261 /* update in memory cookie */
1262 if ( si->syncCookie != NULL ) {
1263 ber_bvfree( si->syncCookie );
1265 si->syncCookie = ber_dupbv( NULL, syncCookie );
1266 ber_str2bv( "top", strlen("top"), 0, &ocbva[0] );
1267 ber_str2bv( "subentry", strlen("subentry"), 0, &ocbva[1] );
1268 ber_str2bv( "syncConsumerSubentry",
1269 strlen("syncConsumerSubentry"), 0, &ocbva[2] );
1270 ocbva[3].bv_len = 0;
1271 ocbva[3].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( "objectClass", strlen("objectClass"), 0, &mod->sml_type );
1278 mod->sml_bvalues = ocbva;
1280 modtail = &mod->sml_next;
1282 sprintf( substr, "syncrepl%d", si->id );
1283 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1285 cnbva[1].bv_len = 0;
1286 cnbva[1].bv_val = NULL;
1287 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1288 mod->sml_op = LDAP_MOD_REPLACE;
1289 mod->sml_next = NULL;
1290 mod->sml_desc = NULL;
1291 ber_str2bv( "cn", strlen("cn"), 0, &mod->sml_type );
1292 mod->sml_bvalues = cnbva;
1294 modtail = &mod->sml_next;
1296 ber_dupbv( &scbva[0], si->syncCookie );
1297 scbva[1].bv_len = 0;
1298 scbva[1].bv_val = NULL;
1299 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1300 mod->sml_op = LDAP_MOD_REPLACE;
1301 mod->sml_next = NULL;
1302 mod->sml_desc = NULL;
1303 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1304 0, &mod->sml_type );
1305 mod->sml_bvalues = scbva;
1307 modtail = &mod->sml_next;
1309 ber_str2bv( " ", 1, 1, &ssbva[0] );
1310 ssbva[1].bv_len = 0;
1311 ssbva[1].bv_val = NULL;
1312 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1313 mod->sml_op = LDAP_MOD_REPLACE;
1314 mod->sml_next = NULL;
1315 mod->sml_desc = NULL;
1316 ber_str2bv( "subtreeSpecification",
1317 strlen("subtreeSpecification"), 0, &mod->sml_type );
1318 mod->sml_bvalues = ssbva;
1320 modtail = &mod->sml_next;
1322 rc = slap_mods_check_syncrepl( be, &modlist, &text, txtbuf, textlen );
1324 if ( rc != LDAP_SUCCESS ) {
1326 LDAP_LOG( OPERATION, ERR,
1327 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1329 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1334 op.o_tag = LDAP_REQ_ADD;
1335 rc = slap_mods_opattrs_syncrepl( be, &op, modlist, modtail,
1336 &text,txtbuf, textlen );
1338 if( rc != LDAP_SUCCESS ) {
1340 LDAP_LOG( OPERATION, ERR,
1341 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1343 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1348 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1349 dnPrettyNormal( NULL, pdn, &e->e_name, &e->e_nname );
1353 rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
1355 if( rc != LDAP_SUCCESS ) {
1357 LDAP_LOG( OPERATION, ERR,
1358 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1360 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1365 op.o_protocol = LDAP_VERSION3;
1366 ber_dupbv( &op.o_ndn, &be->be_rootndn );
1367 op.o_callback = &cb;
1368 op.o_time = slap_get_time();
1369 op.o_managedsait = 1;
1370 op.o_threadctx = si->ctx;
1372 cb.sc_sendentry = null_entry_callback;
1373 cb.sc_sresult = null_result_callback;
1374 cb.sc_response = null_response_callback;
1375 cb.sc_sendreference = null_reference_callback;
1380 for( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1381 mod->sml_op = LDAP_MOD_REPLACE;
1385 /* update persistent cookie */
1386 update_cookie_retry:
1387 op.o_tag = LDAP_REQ_MODIFY;
1388 rc = be->be_modify( be, si->conn, &op, &e->e_name,
1389 &e->e_nname, modlist );
1390 if ( rc != LDAP_SUCCESS ) {
1391 if ( rc == LDAP_REFERRAL ||
1392 rc == LDAP_NO_SUCH_OBJECT ||
1393 rc == DB_NOTFOUND ) {
1394 op.o_tag = LDAP_REQ_ADD;
1395 rc = be->be_add( be, si->conn, &op, e );
1396 if ( rc != LDAP_SUCCESS ) {
1397 if ( rc == LDAP_ALREADY_EXISTS ) {
1398 goto update_cookie_retry;
1399 } else if ( rc == LDAP_REFERRAL ||
1400 rc == LDAP_NO_SUCH_OBJECT ||
1401 rc == DB_NOTFOUND ) {
1403 LDAP_LOG( OPERATION, ERR,
1404 "cookie will be non-persistent\n",
1407 Debug( LDAP_DEBUG_ANY,
1408 "cookie will be non-persistent\n",
1413 LDAP_LOG( OPERATION, ERR,
1414 "be_add failed (%d)\n",
1417 Debug( LDAP_DEBUG_ANY,
1418 "be_add failed (%d)\n",
1425 LDAP_LOG( OPERATION, ERR,
1426 "be_modify failed (%d)\n", rc, 0, 0 );
1428 Debug( LDAP_DEBUG_ANY,
1429 "be_modify failed (%d)\n", rc, 0, 0 );
1436 null_entry_callback(
1441 AttributeName *attrs,
1449 null_result_callback(
1453 const char *matched,
1456 LDAPControl **ctrls,
1460 if ( err != LDAP_SUCCESS && err != LDAP_REFERRAL && err != LDAP_ALREADY_EXISTS && err != LDAP_NO_SUCH_OBJECT && err != DB_NOTFOUND ) {
1461 fprintf( stderr, "err = 0x%x\n", err );
1466 null_reference_callback(
1472 LDAPControl **ctrls,
1479 null_response_callback(
1485 const char *matched,
1489 struct berval *resdata,
1490 struct berval *sasldata,
1494 if ( err != LDAP_SUCCESS && err != LDAP_REFERRAL && err != LDAP_ALREADY_EXISTS && err != LDAP_NO_SUCH_OBJECT && err != DB_NOTFOUND ) {
1495 fprintf( stderr, "err = 0x%x\n", err );
1499 int slap_mods_check_syncrepl(
1501 Modifications **mlp,
1507 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1508 AttributeDescription** descs;
1510 Modifications *prevml = NULL;
1511 Modifications *nextml = NULL;
1512 Modifications *ml = *mlp;
1514 while ( ml != NULL ) {
1515 AttributeDescription *ad = NULL;
1517 /* convert to attribute description */
1518 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1520 if( rc != LDAP_SUCCESS ) {
1521 snprintf( textbuf, textlen, "%s: %s",
1522 ml->sml_type.bv_val, *text );
1529 if ( si->lastmod == LASTMOD_REQ ) {
1530 descs = del_descs_lastmod;
1535 for ( i = 0; descs[i] != NULL; i++ ) {
1536 if ( ad == descs[i] ) {
1537 if ( prevml == NULL ) {
1538 mlp = &ml->sml_next;
1541 prevml->sml_next = ml->sml_next;
1543 slap_mod_free( &ml->sml_mod, 0 );
1544 nextml = ml->sml_next;
1551 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1552 && !slap_ad_is_binary( ad ))
1554 /* attribute requires binary transfer */
1555 snprintf( textbuf, textlen,
1556 "%s: requires ;binary transfer",
1557 ml->sml_type.bv_val );
1559 return LDAP_UNDEFINED_TYPE;
1562 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1563 && slap_ad_is_binary( ad ))
1565 /* attribute requires binary transfer */
1566 snprintf( textbuf, textlen,
1567 "%s: disallows ;binary transfer",
1568 ml->sml_type.bv_val );
1570 return LDAP_UNDEFINED_TYPE;
1573 if( slap_ad_is_tag_range( ad )) {
1574 /* attribute requires binary transfer */
1575 snprintf( textbuf, textlen,
1576 "%s: inappropriate use of tag range option",
1577 ml->sml_type.bv_val );
1579 return LDAP_UNDEFINED_TYPE;
1582 if ( is_at_obsolete( ad->ad_type ) &&
1583 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) )
1586 * attribute is obsolete,
1587 * only allow replace/delete with no values
1589 snprintf( textbuf, textlen,
1590 "%s: attribute is obsolete",
1591 ml->sml_type.bv_val );
1593 return LDAP_CONSTRAINT_VIOLATION;
1599 if( ml->sml_values != NULL ) {
1601 slap_syntax_validate_func *validate =
1602 ad->ad_type->sat_syntax->ssyn_validate;
1603 slap_syntax_transform_func *pretty =
1604 ad->ad_type->sat_syntax->ssyn_pretty;
1606 if( !pretty && !validate ) {
1607 *text = "no validator for syntax";
1608 snprintf( textbuf, textlen,
1609 "%s: no validator for syntax %s",
1610 ml->sml_type.bv_val,
1611 ad->ad_type->sat_syntax->ssyn_oid );
1613 return LDAP_INVALID_SYNTAX;
1617 * check that each value is valid per syntax
1618 * and pretty if appropriate
1620 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1623 rc = pretty( ad->ad_type->sat_syntax,
1624 &ml->sml_values[nvals], &pval );
1626 rc = validate( ad->ad_type->sat_syntax,
1627 &ml->sml_values[nvals] );
1631 snprintf( textbuf, textlen,
1632 "%s: value #%ld invalid per syntax",
1633 ml->sml_type.bv_val, (long) nvals );
1635 return LDAP_INVALID_SYNTAX;
1639 ber_memfree( ml->sml_values[nvals].bv_val );
1640 ml->sml_values[nvals] = pval;
1645 * a rough single value check... an additional check is needed
1646 * to catch add of single value to existing single valued attribute
1648 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1649 && nvals > 1 && is_at_single_value( ad->ad_type ))
1651 snprintf( textbuf, textlen,
1652 "%s: multiple values provided",
1653 ml->sml_type.bv_val );
1655 return LDAP_CONSTRAINT_VIOLATION;
1659 if( nvals && ad->ad_type->sat_equality &&
1660 ad->ad_type->sat_equality->smr_normalize )
1662 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1663 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1664 rc = ad->ad_type->sat_equality->smr_normalize(
1666 ad->ad_type->sat_syntax,
1667 ad->ad_type->sat_equality,
1668 &ml->sml_values[nvals], &ml->sml_nvalues[nvals] );
1671 LDAP_LOG( OPERATION, DETAIL1,
1672 "str2entry: NULL (ssyn_normalize %d)\n",
1675 Debug( LDAP_DEBUG_ANY,
1676 "<= str2entry NULL (ssyn_normalize %d)\n",
1679 snprintf( textbuf, textlen,
1680 "%s: value #%ld normalization failed",
1681 ml->sml_type.bv_val, (long) nvals );
1686 ml->sml_nvalues[nvals].bv_val = NULL;
1687 ml->sml_nvalues[nvals].bv_len = 0;
1695 return LDAP_SUCCESS;
1698 int slap_mods_opattrs_syncrepl(
1701 Modifications *mods,
1702 Modifications **modtail,
1704 char *textbuf, size_t textlen )
1706 struct berval name, timestamp, csn;
1708 struct berval nname;
1710 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1711 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1713 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1715 int mop = op->o_tag == LDAP_REQ_ADD
1716 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
1718 assert( modtail != NULL );
1719 assert( *modtail == NULL );
1721 if( si->lastmod == LASTMOD_GEN ) {
1723 time_t now = slap_get_time();
1725 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1726 ltm = gmtime( &now );
1727 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1729 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1730 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1731 csn.bv_val = csnbuf;
1733 timestamp.bv_val = timebuf;
1734 timestamp.bv_len = strlen(timebuf);
1736 if( op->o_dn.bv_len == 0 ) {
1737 name.bv_val = SLAPD_ANONYMOUS;
1738 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1750 if( op->o_tag == LDAP_REQ_ADD ) {
1751 struct berval tmpval;
1753 if( global_schemacheck ) {
1754 int rc = mods_structural_class( mods, &tmpval,
1755 text, textbuf, textlen );
1756 if( rc != LDAP_SUCCESS ) {
1760 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1762 mod->sml_type.bv_val = NULL;
1763 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1765 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1766 ber_dupbv( &mod->sml_values[0], &tmpval );
1767 mod->sml_values[1].bv_len = 0;
1768 mod->sml_values[1].bv_val = NULL;
1769 assert( mod->sml_values[0].bv_val );
1772 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1773 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1774 mod->sml_nvalues[1].bv_len = 0;
1775 mod->sml_nvalues[1].bv_val = NULL;
1776 assert( mod->sml_nvalues[0].bv_val );
1779 modtail = &mod->sml_next;
1782 if( si->lastmod == LASTMOD_GEN ) {
1783 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1785 mod->sml_type.bv_val = NULL;
1786 mod->sml_desc = slap_schema.si_ad_creatorsName;
1787 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1788 ber_dupbv( &mod->sml_values[0], &name );
1789 mod->sml_values[1].bv_len = 0;
1790 mod->sml_values[1].bv_val = NULL;
1791 assert( mod->sml_values[0].bv_val );
1794 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1795 ber_dupbv( &mod->sml_nvalues[0], &nname );
1796 mod->sml_nvalues[1].bv_len = 0;
1797 mod->sml_nvalues[1].bv_val = NULL;
1798 assert( mod->sml_nvalues[0].bv_val );
1801 modtail = &mod->sml_next;
1803 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1805 mod->sml_type.bv_val = NULL;
1806 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1807 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1808 ber_dupbv( &mod->sml_values[0], ×tamp );
1809 mod->sml_values[1].bv_len = 0;
1810 mod->sml_values[1].bv_val = NULL;
1811 assert( mod->sml_values[0].bv_val );
1813 mod->sml_nvalues = NULL;
1816 modtail = &mod->sml_next;
1820 if( si->lastmod == LASTMOD_GEN ) {
1821 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1823 mod->sml_type.bv_val = NULL;
1824 mod->sml_desc = slap_schema.si_ad_entryCSN;
1825 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1826 ber_dupbv( &mod->sml_values[0], &csn );
1827 mod->sml_values[1].bv_len = 0;
1828 mod->sml_values[1].bv_val = NULL;
1829 assert( mod->sml_values[0].bv_val );
1831 mod->sml_nvalues = NULL;
1834 modtail = &mod->sml_next;
1836 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1838 mod->sml_type.bv_val = NULL;
1839 mod->sml_desc = slap_schema.si_ad_modifiersName;
1840 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1841 ber_dupbv( &mod->sml_values[0], &name );
1842 mod->sml_values[1].bv_len = 0;
1843 mod->sml_values[1].bv_val = NULL;
1844 assert( mod->sml_values[0].bv_val );
1847 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1848 ber_dupbv( &mod->sml_nvalues[0], &nname );
1849 mod->sml_nvalues[1].bv_len = 0;
1850 mod->sml_nvalues[1].bv_val = NULL;
1851 assert( mod->sml_nvalues[0].bv_val );
1854 modtail = &mod->sml_next;
1856 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1858 mod->sml_type.bv_val = NULL;
1859 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1860 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1861 ber_dupbv( &mod->sml_values[0], ×tamp );
1862 mod->sml_values[1].bv_len = 0;
1863 mod->sml_values[1].bv_val = NULL;
1864 assert( mod->sml_values[0].bv_val );
1866 mod->sml_nvalues = NULL;
1869 modtail = &mod->sml_next;
1873 return LDAP_SUCCESS;
1878 slap_mods2entry_syncrepl(
1879 Modifications *mods,
1883 char *textbuf, size_t textlen )
1885 Attribute **tail = &(*e)->e_attrs;
1886 assert( *tail == NULL );
1890 for( ; mods != NULL; mods = mods->sml_next ) {
1893 assert( mods->sml_desc != NULL );
1895 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1897 if( attr != NULL ) {
1898 #define SLURPD_FRIENDLY
1899 #ifdef SLURPD_FRIENDLY
1903 snprintf( textbuf, textlen,
1904 "attribute '%s' provided more than once",
1905 mods->sml_desc->ad_cname.bv_val );
1906 return LDAP_TYPE_OR_VALUE_EXISTS;
1909 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1912 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1917 attr->a_vals = ch_realloc( attr->a_vals,
1918 sizeof( struct berval ) * (i+j) );
1920 /* should check for duplicates */
1922 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1923 sizeof( struct berval ) * j );
1926 if( attr->a_nvals ) {
1927 attr->a_nvals = ch_realloc( attr->a_nvals,
1928 sizeof( struct berval ) * (i+j) );
1930 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1931 sizeof( struct berval ) * j );
1933 /* trim the mods array */
1934 ch_free( mods->sml_nvalues );
1935 mods->sml_nvalues = NULL;
1941 snprintf( textbuf, textlen,
1942 "attribute '%s' provided more than once",
1943 mods->sml_desc->ad_cname.bv_val );
1944 return LDAP_TYPE_OR_VALUE_EXISTS;
1948 if( mods->sml_values[1].bv_val != NULL ) {
1949 /* check for duplicates */
1951 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1953 /* check if the values we're adding already exist */
1954 if( mr == NULL || !mr->smr_match ) {
1955 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1956 /* test asserted values against themselves */
1957 for( j = 0; j < i; j++ ) {
1958 if ( bvmatch( &mods->sml_bvalues[i],
1959 &mods->sml_bvalues[j] ) ) {
1960 /* value exists already */
1961 snprintf( textbuf, textlen,
1962 "%s: value #%d provided more than once",
1963 mods->sml_desc->ad_cname.bv_val, j );
1964 return LDAP_TYPE_OR_VALUE_EXISTS;
1971 const char *text = NULL;
1972 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1974 rc = modify_check_duplicates( mods->sml_desc, mr,
1975 NULL, mods->sml_bvalues, 0,
1976 &text, textbuf, sizeof( textbuf ) );
1978 if ( rc != LDAP_SUCCESS ) {
1984 attr = ch_calloc( 1, sizeof(Attribute) );
1986 /* move ad to attr structure */
1987 attr->a_desc = mods->sml_desc;
1989 /* move values to attr structure */
1990 /* should check for duplicates */
1991 attr->a_vals = mods->sml_values;
1994 attr->a_nvals = mods->sml_nvalues;
1998 tail = &attr->a_next;
2001 return LDAP_SUCCESS;