2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2006-2014 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This program was originally developed by Pierangelo Masarati
17 * for inclusion in OpenLDAP Software.
21 * Proof-of-concept API that implement the client-side
22 * of the "LDAP Content Sync Operation" (RFC 4533)
31 #ifdef LDAP_SYNC_TRACE
33 ldap_sync_state2str( int state )
36 case LDAP_SYNC_PRESENT:
37 return "LDAP_SYNC_PRESENT";
40 return "LDAP_SYNC_ADD";
42 case LDAP_SYNC_MODIFY:
43 return "LDAP_SYNC_MODIFY";
45 case LDAP_SYNC_DELETE:
46 return "LDAP_SYNC_DELETE";
55 * initialize the persistent search structure
58 ldap_sync_initialize( ldap_sync_t *ls_in )
60 ldap_sync_t *ls = ls_in;
63 ls = ldap_memalloc( sizeof( ldap_sync_t ) );
68 memset( ls, 0, sizeof( ldap_sync_t ) );
70 ls->ls_scope = LDAP_SCOPE_SUBTREE;
77 * destroy the persistent search structure
80 ldap_sync_destroy( ldap_sync_t *ls, int freeit )
84 if ( ls->ls_base != NULL ) {
85 ldap_memfree( ls->ls_base );
89 if ( ls->ls_filter != NULL ) {
90 ldap_memfree( ls->ls_filter );
94 if ( ls->ls_attrs != NULL ) {
97 for ( i = 0; ls->ls_attrs[ i ] != NULL; i++ ) {
98 ldap_memfree( ls->ls_attrs[ i ] );
100 ldap_memfree( ls->ls_attrs );
104 if ( ls->ls_ld != NULL ) {
105 (void)ldap_unbind_ext( ls->ls_ld, NULL, NULL );
106 #ifdef LDAP_SYNC_TRACE
107 fprintf( stderr, "ldap_unbind_ext()\n" );
108 #endif /* LDAP_SYNC_TRACE */
112 if ( ls->ls_cookie.bv_val != NULL ) {
113 ldap_memfree( ls->ls_cookie.bv_val );
114 ls->ls_cookie.bv_val = NULL;
123 * handle the LDAP_RES_SEARCH_ENTRY response
126 ldap_sync_search_entry( ldap_sync_t *ls, LDAPMessage *res )
128 LDAPControl **ctrls = NULL;
131 BerElement *ber = NULL;
132 struct berval entryUUID = { 0 },
136 ldap_sync_refresh_t phase;
138 #ifdef LDAP_SYNC_TRACE
139 fprintf( stderr, "\tgot LDAP_RES_SEARCH_ENTRY\n" );
140 #endif /* LDAP_SYNC_TRACE */
142 assert( ls != NULL );
143 assert( res != NULL );
145 phase = ls->ls_refreshPhase;
154 * - Sync State Control is "add"
157 /* the control MUST be present */
159 /* extract controls */
160 ldap_get_entry_controls( ls->ls_ld, res, &ctrls );
161 if ( ctrls == NULL ) {
165 /* lookup the sync state control */
166 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
167 if ( strcmp( ctrls[ i ]->ldctl_oid, LDAP_CONTROL_SYNC_STATE ) == 0 ) {
172 /* control must be present; there might be other... */
173 if ( ctrls[ i ] == NULL ) {
178 ber = ber_init( &ctrls[ i ]->ldctl_value );
182 /* scan entryUUID in-place ("m") */
183 if ( ber_scanf( ber, "{em" /*"}"*/, &state, &entryUUID ) == LBER_ERROR
184 || entryUUID.bv_len == 0 )
189 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
190 /* scan cookie in-place ("m") */
191 if ( ber_scanf( ber, /*"{"*/ "m}", &cookie ) == LBER_ERROR ) {
194 if ( cookie.bv_val != NULL ) {
195 ber_bvreplace( &ls->ls_cookie, &cookie );
197 #ifdef LDAP_SYNC_TRACE
198 fprintf( stderr, "\t\tgot cookie=%s\n",
199 cookie.bv_val ? cookie.bv_val : "(null)" );
200 #endif /* LDAP_SYNC_TRACE */
204 case LDAP_SYNC_PRESENT:
205 case LDAP_SYNC_DELETE:
207 case LDAP_SYNC_MODIFY:
208 /* NOTE: ldap_sync_refresh_t is defined
209 * as the corresponding LDAP_SYNC_*
210 * for the 4 above cases */
212 #ifdef LDAP_SYNC_TRACE
213 fprintf( stderr, "\t\tgot syncState=%s\n", ldap_sync_state2str( state ) );
214 #endif /* LDAP_SYNC_TRACE */
218 #ifdef LDAP_SYNC_TRACE
219 fprintf( stderr, "\t\tgot unknown syncState=%d\n", state );
220 #endif /* LDAP_SYNC_TRACE */
224 rc = ls->ls_search_entry
225 ? ls->ls_search_entry( ls, res, &entryUUID, phase )
233 if ( ctrls != NULL ) {
234 ldap_controls_free( ctrls );
241 * handle the LDAP_RES_SEARCH_REFERENCE response
242 * (to be implemented yet)
245 ldap_sync_search_reference( ldap_sync_t *ls, LDAPMessage *res )
249 #ifdef LDAP_SYNC_TRACE
250 fprintf( stderr, "\tgot LDAP_RES_SEARCH_REFERENCE\n" );
251 #endif /* LDAP_SYNC_TRACE */
253 assert( ls != NULL );
254 assert( res != NULL );
256 if ( ls->ls_search_reference ) {
257 rc = ls->ls_search_reference( ls, res );
264 * handle the LDAP_RES_SEARCH_RESULT response
267 ldap_sync_search_result( ldap_sync_t *ls, LDAPMessage *res )
270 char *matched = NULL,
272 LDAPControl **ctrls = NULL;
274 int refreshDeletes = -1;
276 #ifdef LDAP_SYNC_TRACE
277 fprintf( stderr, "\tgot LDAP_RES_SEARCH_RESULT\n" );
278 #endif /* LDAP_SYNC_TRACE */
280 assert( ls != NULL );
281 assert( res != NULL );
283 /* should not happen in refreshAndPersist... */
284 rc = ldap_parse_result( ls->ls_ld,
285 res, &err, &matched, &msg, NULL, &ctrls, 0 );
286 #ifdef LDAP_SYNC_TRACE
288 "\tldap_parse_result(%d, \"%s\", \"%s\") == %d\n",
290 matched ? matched : "",
293 #endif /* LDAP_SYNC_TRACE */
294 if ( rc == LDAP_SUCCESS ) {
298 ls->ls_refreshPhase = LDAP_SYNC_CAPI_DONE;
303 BerElement *ber = NULL;
305 struct berval cookie = { 0 };
309 /* deal with control; then fallthru to handler */
310 if ( ctrls == NULL ) {
314 /* lookup the sync state control */
315 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
316 if ( strcmp( ctrls[ i ]->ldctl_oid,
317 LDAP_CONTROL_SYNC_DONE ) == 0 )
323 /* control must be present; there might be other... */
324 if ( ctrls[ i ] == NULL ) {
329 ber = ber_init( &ctrls[ i ]->ldctl_value );
334 if ( ber_scanf( ber, "{" /*"}"*/) == LBER_ERROR ) {
337 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
338 if ( ber_scanf( ber, "m", &cookie ) == LBER_ERROR ) {
341 if ( cookie.bv_val != NULL ) {
342 ber_bvreplace( &ls->ls_cookie, &cookie );
344 #ifdef LDAP_SYNC_TRACE
345 fprintf( stderr, "\t\tgot cookie=%s\n",
346 cookie.bv_val ? cookie.bv_val : "(null)" );
347 #endif /* LDAP_SYNC_TRACE */
351 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES ) {
352 if ( ber_scanf( ber, "b", &refreshDeletes ) == LBER_ERROR ) {
355 if ( refreshDeletes ) {
360 if ( ber_scanf( ber, /*"{"*/ "}" ) != LBER_ERROR ) {
366 if ( rc != LDAP_SUCCESS ) {
370 #ifdef LDAP_SYNC_TRACE
371 fprintf( stderr, "\t\tgot refreshDeletes=%s\n",
372 refreshDeletes ? "TRUE" : "FALSE" );
373 #endif /* LDAP_SYNC_TRACE */
375 /* FIXME: what should we do with the refreshDelete? */
376 switch ( refreshDeletes ) {
378 ls->ls_refreshPhase = LDAP_SYNC_CAPI_PRESENTS;
382 ls->ls_refreshPhase = LDAP_SYNC_CAPI_DELETES;
388 case LDAP_SYNC_REFRESH_REQUIRED:
389 /* TODO: check for Sync Done Control */
390 /* FIXME: perhaps the handler should be called
391 * also in case of failure; we'll deal with this
392 * later when implementing refreshOnly */
393 if ( ls->ls_search_result ) {
394 err = ls->ls_search_result( ls, res, refreshDeletes );
400 if ( matched != NULL ) {
401 ldap_memfree( matched );
408 if ( ctrls != NULL ) {
409 ldap_controls_free( ctrls );
412 ls->ls_refreshPhase = LDAP_SYNC_CAPI_DONE;
418 * handle the LDAP_RES_INTERMEDIATE response
421 ldap_sync_search_intermediate( ldap_sync_t *ls, LDAPMessage *res, int *refreshDone )
425 struct berval *retdata = NULL;
426 BerElement *ber = NULL;
428 ber_tag_t syncinfo_tag;
429 struct berval cookie;
430 int refreshDeletes = 0;
431 BerVarray syncUUIDs = NULL;
432 ldap_sync_refresh_t phase;
434 #ifdef LDAP_SYNC_TRACE
435 fprintf( stderr, "\tgot LDAP_RES_INTERMEDIATE\n" );
436 #endif /* LDAP_SYNC_TRACE */
438 assert( ls != NULL );
439 assert( res != NULL );
440 assert( refreshDone != NULL );
444 rc = ldap_parse_intermediate( ls->ls_ld, res,
445 &retoid, &retdata, NULL, 0 );
446 #ifdef LDAP_SYNC_TRACE
447 fprintf( stderr, "\t%sldap_parse_intermediate(%s) == %d\n",
448 rc != LDAP_SUCCESS ? "!!! " : "",
449 retoid == NULL ? "\"\"" : retoid,
451 #endif /* LDAP_SYNC_TRACE */
452 /* parsing must be successful, and yield the OID
453 * of the sync info intermediate response */
454 if ( rc != LDAP_SUCCESS ) {
460 if ( retoid == NULL || strcmp( retoid, LDAP_SYNC_INFO ) != 0 ) {
464 /* init ber using the value in the response */
465 ber = ber_init( retdata );
470 syncinfo_tag = ber_peek_tag( ber, &len );
471 switch ( syncinfo_tag ) {
472 case LDAP_TAG_SYNC_NEW_COOKIE:
473 if ( ber_scanf( ber, "m", &cookie ) == LBER_ERROR ) {
476 if ( cookie.bv_val != NULL ) {
477 ber_bvreplace( &ls->ls_cookie, &cookie );
479 #ifdef LDAP_SYNC_TRACE
480 fprintf( stderr, "\t\tgot cookie=%s\n",
481 cookie.bv_val ? cookie.bv_val : "(null)" );
482 #endif /* LDAP_SYNC_TRACE */
485 case LDAP_TAG_SYNC_REFRESH_DELETE:
486 case LDAP_TAG_SYNC_REFRESH_PRESENT:
487 if ( syncinfo_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
488 #ifdef LDAP_SYNC_TRACE
489 fprintf( stderr, "\t\tgot refreshDelete\n" );
490 #endif /* LDAP_SYNC_TRACE */
491 switch ( ls->ls_refreshPhase ) {
492 case LDAP_SYNC_CAPI_NONE:
493 case LDAP_SYNC_CAPI_PRESENTS:
494 ls->ls_refreshPhase = LDAP_SYNC_CAPI_DELETES;
498 /* TODO: impossible; handle */
503 #ifdef LDAP_SYNC_TRACE
504 fprintf( stderr, "\t\tgot refreshPresent\n" );
505 #endif /* LDAP_SYNC_TRACE */
506 switch ( ls->ls_refreshPhase ) {
507 case LDAP_SYNC_CAPI_NONE:
508 ls->ls_refreshPhase = LDAP_SYNC_CAPI_PRESENTS;
512 /* TODO: impossible; handle */
517 if ( ber_scanf( ber, "{" /*"}"*/ ) == LBER_ERROR ) {
520 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
521 if ( ber_scanf( ber, "m", &cookie ) == LBER_ERROR ) {
524 if ( cookie.bv_val != NULL ) {
525 ber_bvreplace( &ls->ls_cookie, &cookie );
527 #ifdef LDAP_SYNC_TRACE
528 fprintf( stderr, "\t\tgot cookie=%s\n",
529 cookie.bv_val ? cookie.bv_val : "(null)" );
530 #endif /* LDAP_SYNC_TRACE */
534 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDONE ) {
535 if ( ber_scanf( ber, "b", refreshDone ) == LBER_ERROR ) {
540 #ifdef LDAP_SYNC_TRACE
541 fprintf( stderr, "\t\tgot refreshDone=%s\n",
542 *refreshDone ? "TRUE" : "FALSE" );
543 #endif /* LDAP_SYNC_TRACE */
545 if ( ber_scanf( ber, /*"{"*/ "}" ) == LBER_ERROR ) {
549 if ( *refreshDone ) {
550 ls->ls_refreshPhase = LDAP_SYNC_CAPI_DONE;
553 if ( ls->ls_intermediate ) {
554 ls->ls_intermediate( ls, res, NULL, ls->ls_refreshPhase );
559 case LDAP_TAG_SYNC_ID_SET:
560 #ifdef LDAP_SYNC_TRACE
561 fprintf( stderr, "\t\tgot syncIdSet\n" );
562 #endif /* LDAP_SYNC_TRACE */
563 if ( ber_scanf( ber, "{" /*"}"*/ ) == LBER_ERROR ) {
566 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
567 if ( ber_scanf( ber, "m", &cookie ) == LBER_ERROR ) {
570 if ( cookie.bv_val != NULL ) {
571 ber_bvreplace( &ls->ls_cookie, &cookie );
573 #ifdef LDAP_SYNC_TRACE
574 fprintf( stderr, "\t\tgot cookie=%s\n",
575 cookie.bv_val ? cookie.bv_val : "(null)" );
576 #endif /* LDAP_SYNC_TRACE */
579 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES ) {
580 if ( ber_scanf( ber, "b", &refreshDeletes ) == LBER_ERROR ) {
585 if ( ber_scanf( ber, /*"{"*/ "[W]}", &syncUUIDs ) == LBER_ERROR
586 || syncUUIDs == NULL )
591 #ifdef LDAP_SYNC_TRACE
595 fprintf( stderr, "\t\tgot refreshDeletes=%s\n",
596 refreshDeletes ? "TRUE" : "FALSE" );
597 for ( i = 0; syncUUIDs[ i ].bv_val != NULL; i++ ) {
599 fprintf( stderr, "\t\t%s\n",
600 lutil_uuidstr_from_normalized(
601 syncUUIDs[ i ].bv_val, syncUUIDs[ i ].bv_len,
602 buf, sizeof( buf ) ) );
605 #endif /* LDAP_SYNC_TRACE */
607 if ( refreshDeletes ) {
608 phase = LDAP_SYNC_CAPI_DELETES_IDSET;
611 phase = LDAP_SYNC_CAPI_PRESENTS_IDSET;
614 /* FIXME: should touch ls->ls_refreshPhase? */
615 if ( ls->ls_intermediate ) {
616 ls->ls_intermediate( ls, res, syncUUIDs, phase );
619 ber_bvarray_free( syncUUIDs );
623 #ifdef LDAP_SYNC_TRACE
624 fprintf( stderr, "\t\tunknown tag!\n" );
625 #endif /* LDAP_SYNC_TRACE */
636 if ( retoid != NULL ) {
637 ldap_memfree( retoid );
640 if ( retdata != NULL ) {
641 ber_bvfree( retdata );
648 * initialize the sync
651 ldap_sync_init( ldap_sync_t *ls, int mode )
653 LDAPControl ctrl = { 0 },
655 BerElement *ber = NULL;
657 struct timeval tv = { 0 },
659 LDAPMessage *res = NULL;
661 #ifdef LDAP_SYNC_TRACE
662 fprintf( stderr, "ldap_sync_init(%s)...\n",
663 mode == LDAP_SYNC_REFRESH_AND_PERSIST ?
664 "LDAP_SYNC_REFRESH_AND_PERSIST" :
665 ( mode == LDAP_SYNC_REFRESH_ONLY ?
666 "LDAP_SYNC_REFRESH_ONLY" : "unknown" ) );
667 #endif /* LDAP_SYNC_TRACE */
669 assert( ls != NULL );
670 assert( ls->ls_ld != NULL );
672 /* support both refreshOnly and refreshAndPersist */
674 case LDAP_SYNC_REFRESH_AND_PERSIST:
675 case LDAP_SYNC_REFRESH_ONLY:
679 fprintf( stderr, "ldap_sync_init: unknown mode=%d\n", mode );
680 return LDAP_PARAM_ERROR;
683 /* check consistency of cookie and reloadHint at initial refresh */
684 if ( ls->ls_cookie.bv_val == NULL && ls->ls_reloadHint != 0 ) {
685 fprintf( stderr, "ldap_sync_init: inconsistent cookie/rhint\n" );
686 return LDAP_PARAM_ERROR;
692 /* prepare the Sync Request control */
693 ber = ber_alloc_t( LBER_USE_DER );
694 #ifdef LDAP_SYNC_TRACE
695 fprintf( stderr, "%sber_alloc_t() %s= NULL\n",
696 ber == NULL ? "!!! " : "",
697 ber == NULL ? "=" : "!" );
698 #endif /* LDAP_SYNC_TRACE */
704 ls->ls_refreshPhase = LDAP_SYNC_CAPI_NONE;
706 if ( ls->ls_cookie.bv_val != NULL ) {
707 ber_printf( ber, "{eOb}", mode,
708 &ls->ls_cookie, ls->ls_reloadHint );
711 ber_printf( ber, "{eb}", mode, ls->ls_reloadHint );
714 rc = ber_flatten2( ber, &ctrl.ldctl_value, 0 );
715 #ifdef LDAP_SYNC_TRACE
717 "%sber_flatten2() == %d\n",
720 #endif /* LDAP_SYNC_TRACE */
726 /* make the control critical, as we cannot proceed without */
727 ctrl.ldctl_oid = LDAP_CONTROL_SYNC;
728 ctrl.ldctl_iscritical = 1;
731 if ( ls->ls_timelimit ) {
732 tv.tv_sec = ls->ls_timelimit;
736 /* actually run the search */
737 rc = ldap_search_ext( ls->ls_ld,
738 ls->ls_base, ls->ls_scope, ls->ls_filter,
739 ls->ls_attrs, 0, ctrls, NULL,
740 tvp, ls->ls_sizelimit, &ls->ls_msgid );
741 #ifdef LDAP_SYNC_TRACE
743 "%sldap_search_ext(\"%s\", %d, \"%s\") == %d\n",
745 ls->ls_base, ls->ls_scope, ls->ls_filter, rc );
746 #endif /* LDAP_SYNC_TRACE */
747 if ( rc != LDAP_SUCCESS ) {
751 /* initial content/content update phase */
753 LDAPMessage *msg = NULL;
755 /* NOTE: this very short timeout is just to let
756 * ldap_result() yield long enough to get something */
760 rc = ldap_result( ls->ls_ld, ls->ls_msgid,
761 LDAP_MSG_RECEIVED, &tv, &res );
762 #ifdef LDAP_SYNC_TRACE
764 "\t%sldap_result(%d) == %d\n",
765 rc == -1 ? "!!! " : "",
767 #endif /* LDAP_SYNC_TRACE */
773 * TODO: can do something else in the meanwhile)
782 for ( msg = ldap_first_message( ls->ls_ld, res );
784 msg = ldap_next_message( ls->ls_ld, msg ) )
788 switch ( ldap_msgtype( msg ) ) {
789 case LDAP_RES_SEARCH_ENTRY:
790 rc = ldap_sync_search_entry( ls, res );
793 case LDAP_RES_SEARCH_REFERENCE:
794 rc = ldap_sync_search_reference( ls, res );
797 case LDAP_RES_SEARCH_RESULT:
798 rc = ldap_sync_search_result( ls, res );
801 case LDAP_RES_INTERMEDIATE:
802 rc = ldap_sync_search_intermediate( ls, res, &refreshDone );
803 if ( rc != LDAP_SUCCESS || refreshDone ) {
809 #ifdef LDAP_SYNC_TRACE
810 fprintf( stderr, "\tgot something unexpected...\n" );
811 #endif /* LDAP_SYNC_TRACE */
837 * initialize the refreshOnly sync
840 ldap_sync_init_refresh_only( ldap_sync_t *ls )
842 return ldap_sync_init( ls, LDAP_SYNC_REFRESH_ONLY );
846 * initialize the refreshAndPersist sync
849 ldap_sync_init_refresh_and_persist( ldap_sync_t *ls )
851 return ldap_sync_init( ls, LDAP_SYNC_REFRESH_AND_PERSIST );
855 * poll for new responses
858 ldap_sync_poll( ldap_sync_t *ls )
862 LDAPMessage *res = NULL,
866 #ifdef LDAP_SYNC_TRACE
867 fprintf( stderr, "ldap_sync_poll...\n" );
868 #endif /* LDAP_SYNC_TRACE */
870 assert( ls != NULL );
871 assert( ls->ls_ld != NULL );
873 if ( ls->ls_timeout != -1 ) {
874 tv.tv_sec = ls->ls_timeout;
879 rc = ldap_result( ls->ls_ld, ls->ls_msgid,
880 LDAP_MSG_RECEIVED, tvp, &res );
885 for ( msg = ldap_first_message( ls->ls_ld, res );
887 msg = ldap_next_message( ls->ls_ld, msg ) )
891 switch ( ldap_msgtype( msg ) ) {
892 case LDAP_RES_SEARCH_ENTRY:
893 rc = ldap_sync_search_entry( ls, res );
896 case LDAP_RES_SEARCH_REFERENCE:
897 rc = ldap_sync_search_reference( ls, res );
900 case LDAP_RES_SEARCH_RESULT:
901 rc = ldap_sync_search_result( ls, res );
904 case LDAP_RES_INTERMEDIATE:
905 rc = ldap_sync_search_intermediate( ls, res, &refreshDone );
906 if ( rc != LDAP_SUCCESS || refreshDone ) {
912 #ifdef LDAP_SYNC_TRACE
913 fprintf( stderr, "\tgot something unexpected...\n" );
914 #endif /* LDAP_SYNC_TRACE */