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_del_nonpresent( LDAP *, Operation * );
41 /* callback functions */
42 static int cookie_callback( struct slap_op *, struct slap_rep * );
43 static int dn_callback( struct slap_op *, struct slap_rep * );
44 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
45 static int null_callback( struct slap_op *, struct slap_rep * );
46 static int contextcsn_callback( Operation*, SlapReply* );
48 static AttributeDescription **sync_descs;
50 struct runqueue_s syncrepl_rq;
55 sync_descs = ch_malloc( 4 * sizeof( AttributeDescription * ));
56 sync_descs[0] = slap_schema.si_ad_objectClass;
57 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
58 sync_descs[2] = slap_schema.si_ad_entryCSN;
75 BerElement *sync_ber = NULL;
76 struct berval *sync_bvalp = NULL;
80 struct timeval timeout;
82 /* setup LDAP SYNC control */
83 sync_ber = ber_alloc_t( LBER_USE_DER );
84 ber_set_option( sync_ber, LBER_OPT_BER_MEMCTX, NULL );
86 if ( si->syncCookie ) {
87 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
89 ber_printf( sync_ber, "{e}", abs(si->type) );
92 if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
93 ber_free( sync_ber, 1 );
96 ber_free( sync_ber, 1 );
98 ctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), NULL );
100 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
101 c[0].ldctl_value = (*sync_bvalp);
102 c[0].ldctl_iscritical = si->type < 0;
106 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
107 c[1].ldctl_value.bv_val = si->authzId;
108 c[1].ldctl_value.bv_len = strlen( si->authzId );
109 c[1].ldctl_iscritical = 1;
117 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
119 ber_bvfree( sync_bvalp );
122 if ( err != LDAP_OPT_SUCCESS )
123 fprintf( stderr, "Could not set controls : %d\n", err );
125 timeout.tv_sec = si->tlimit > 0 ? si->tlimit : 1;
127 rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
128 si->attrs, si->attrsonly, sctrls, cctrls,
129 si->tlimit < 0 ? NULL : &timeout,
130 si->slimit, msgidp );
140 struct re_s* rtask = arg;
141 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
142 Backend *be = si->be;
144 SlapReply rs = {REP_RESULT};
147 LDAPControl **sctrls = NULL;
148 LDAPControl **rctrls = NULL;
149 LDAPControl *rctrlp = NULL;
150 BerElement *sync_ber = NULL;
151 struct berval *sync_bvalp = NULL;
153 BerElement *ctrl_ber = NULL;
154 BerElement *res_ber = NULL;
157 LDAPMessage *res = NULL;
158 LDAPMessage *msg = NULL;
162 int nresponses, nreferences, nextended, npartial;
163 int nresponses_psearch;
165 int cancel_msgid = -1;
167 struct berval *retdata = NULL;
169 int sync_info_arrived = 0;
173 struct berval syncUUID = { 0, NULL };
174 struct berval syncCookie = { 0, NULL };
175 struct berval syncCookie_req = { 0, NULL };
180 int syncinfo_arrived = 0;
181 int cancel_response = 0;
184 AttributeDescription** descs = NULL;
196 struct berval base_bv = { 0, NULL };
197 struct berval pbase = { 0, NULL };
198 struct berval nbase = { 0, NULL };
199 struct berval sub_bv = { 0, NULL };
200 struct berval psubrdn = { 0, NULL };
201 struct berval nsubrdn = { 0, NULL };
202 struct berval psub = { 0, NULL };
203 struct berval nsub = { 0, NULL };
205 Modifications *modlist = NULL;
206 Modifications *ml, *mlnext;
207 char *def_filter_str = NULL;
212 struct timeval *tout_p = NULL;
213 struct timeval tout = { 10, 0 };
216 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
218 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
224 if ( abs(si->type) != LDAP_SYNC_REFRESH_ONLY &&
225 abs(si->type) != LDAP_SYNC_REFRESH_AND_PERSIST ) {
229 si->sync_mode = LDAP_SYNC_STATE_MODE;
231 /* Init connection to master */
233 rc = ldap_initialize( &ld, si->provideruri );
234 if ( rc != LDAP_SUCCESS ) {
236 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
237 "ldap_initialize failed (%s)\n",
238 si->provideruri, 0, 0 );
240 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
241 "ldap_initialize failed (%s)\n",
242 si->provideruri, 0, 0 );
246 op.o_protocol = LDAP_VERSION3;
247 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &op.o_protocol );
252 rc = ldap_start_tls_s( ld, NULL, NULL );
253 if( rc != LDAP_SUCCESS ) {
255 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
256 "%s: ldap_start_tls failed (%d)\n",
257 si->tls == TLS_CRITICAL ? "Error" : "Warning",
260 Debug( LDAP_DEBUG_ANY,
261 "%s: ldap_start_tls failed (%d)\n",
262 si->tls == TLS_CRITICAL ? "Error" : "Warning",
265 if( si->tls == TLS_CRITICAL )
270 if ( si->bindmethod == LDAP_AUTH_SASL ) {
271 #ifdef HAVE_CYRUS_SASL
274 if ( si->secprops != NULL ) {
275 int err = ldap_set_option( ld,
276 LDAP_OPT_X_SASL_SECPROPS, si->secprops);
278 if( err != LDAP_OPT_SUCCESS ) {
280 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
281 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
282 si->provideruri, si->secprops, 0 );
284 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
285 "(%s,SECPROPS,\"%s\") failed!\n",
286 si->provideruri, si->secprops, NULL );
292 defaults = lutil_sasl_defaults( ld,
299 rc = ldap_sasl_interactive_bind_s( ld,
307 if ( rc != LDAP_SUCCESS ) {
309 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
310 "ldap_sasl_interactive_bind_s failed (%d)\n",
313 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
314 "ldap_sasl_interactive_bind_s failed (%d)\n",
319 #else /* HAVE_CYRUS_SASL */
320 fprintf( stderr, "not compiled with SASL support\n" );
324 rc = ldap_bind_s( ld, si->binddn, si->passwd, si->bindmethod );
325 if ( rc != LDAP_SUCCESS ) {
327 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
328 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
330 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
331 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
337 /* set thread context in syncinfo */
340 /* set memory context */
341 #define SLAB_SIZE 1048576
343 memctx = sl_mem_create( memsiz, ctx );
344 op.o_tmpmemctx = memctx;
345 op.o_tmpmfuncs = &sl_mfuncs;
348 op.o_tag = LDAP_REQ_SEARCH;
349 op.o_dn = si->updatedn;
350 op.o_ndn = si->updatedn;
352 op.o_time = slap_get_time();
353 op.o_managedsait = 1;
354 op.o_threadctx = si->ctx;
357 op.o_connid = op.o_conn->c_connid;
358 op.ors_scope = LDAP_SCOPE_BASE;
359 op.ors_deref = LDAP_DEREF_NEVER;
362 op.ors_attrsonly = 0;
364 op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
365 ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
369 conn.c_send_ldap_result = slap_send_ldap_result;
370 conn.c_send_search_entry = slap_send_search_entry;
371 conn.c_send_search_reference = slap_send_search_reference;
373 /* get syncrepl cookie of shadow replica from subentry */
374 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
375 dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
377 sprintf( substr, "cn=syncrepl%d", si->id );
378 ber_str2bv( substr, strlen(substr), 1, &sub_bv );
379 dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn, op.o_tmpmemctx );
381 build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
382 build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
384 ch_free( base_bv.bv_val );
385 ch_free( pbase.bv_val );
386 ch_free( nbase.bv_val );
387 ch_free( sub_bv.bv_val );
388 ch_free( psubrdn.bv_val );
389 ch_free( nsubrdn.bv_val );
391 /* set callback function */
392 cb.sc_response = cookie_callback;
395 /* search subentry to retrieve cookie */
396 si->syncCookie = NULL;
397 be->be_search( &op, &rs );
399 ber_dupbv( &syncCookie_req, si->syncCookie );
401 ch_free( op.o_req_dn.bv_val );
402 ch_free( op.o_req_ndn.bv_val );
403 filter_free( op.ors_filter );
404 ch_free( op.ors_filterstr.bv_val );
406 psub = be->be_nsuffix[0];
408 for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
411 /* Delete Attributes */
413 for ( i = 0; descs[i] != NULL; i++ ) {
414 for ( j = 0; si->attrs[j] != NULL; j++ ) {
415 if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
416 ch_free( si->attrs[j] );
417 for ( k = j; si->attrs[k] != NULL; k++ ) {
418 si->attrs[k] = si->attrs[k+1];
423 for ( n = 0; si->attrs[ n ] != NULL; n++ );
424 tmp = ( char ** ) ch_realloc( si->attrs, ( n + 4 ) * sizeof( char * ));
427 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
429 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
433 tmp = ( char ** ) ch_realloc( si->attrs, 5 * sizeof( char * ));
436 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
438 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
441 tmp[ n++ ] = ch_strdup( "*" );
449 for ( i = 0; descs[ i ] != NULL; i++ ) {
450 si->attrs[ n++ ] = ch_strdup ( descs[i]->ad_cname.bv_val );
451 si->attrs[ n ] = NULL;
454 rc = ldap_sync_search( si, ld, NULL, NULL, &msgid );
455 if( rc != LDAP_SUCCESS ) {
456 fprintf( stderr, "syncrepl: ldap_search_ext: %s (%d)\n",
457 ldap_err2string( rc ), rc );
461 if ( abs(si->type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
467 while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res )) >= 0 ) {
470 if ( slapd_abrupt_shutdown ) {
477 for ( msg = ldap_first_message( ld, res );
479 msg = ldap_next_message( ld, msg ) )
481 syncCookie.bv_len = 0; syncCookie.bv_val = NULL;
482 switch( ldap_msgtype( msg ) ) {
483 case LDAP_RES_SEARCH_ENTRY:
484 entry = syncrepl_message_to_entry( si, ld, &op, msg,
485 &modlist, &syncstate, &syncUUID, &syncCookie );
486 rc_efree = syncrepl_entry( si, ld, &op, entry, modlist,
487 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
488 if ( syncCookie.bv_len ) {
489 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie );
493 for ( ml = modlist; ml != NULL; ml = mlnext ) {
494 mlnext = ml->sml_next;
499 case LDAP_RES_SEARCH_REFERENCE:
501 LDAP_LOG( OPERATION, ERR,
502 "do_syncrepl : reference received\n", 0, 0, 0 );
504 Debug( LDAP_DEBUG_ANY,
505 "do_syncrepl : reference received\n", 0, 0, 0 );
509 case LDAP_RES_SEARCH_RESULT:
510 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
513 ctrl_ber = ber_alloc_t( LBER_USE_DER );
514 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op.o_tmpmemctx );
515 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
516 ber_reset( ctrl_ber, 1 );
518 ber_scanf( ctrl_ber, "{" /*"}"*/);
519 if ( ber_peek_tag( ctrl_ber, &len )
520 == LDAP_SYNC_TAG_COOKIE ) {
521 ber_scanf( ctrl_ber, "o", &syncCookie );
524 value_match( &match, slap_schema.si_ad_entryCSN,
525 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
526 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
527 &syncCookie_req, &syncCookie, &text );
528 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
529 if ( cancel_response ) {
530 if ( syncCookie.bv_len && match < 0) {
531 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie );
534 ber_free( ctrl_ber, 1 );
539 ber_free( ctrl_ber, 1 );
543 if ( syncCookie.bv_len && match < 0 ) {
544 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie);
546 if ( si->sync_mode == LDAP_SYNC_STATE_MODE && match < 0 ) {
547 syncrepl_del_nonpresent( ld, &op );
550 ber_free( ctrl_ber, 1 );
555 case LDAP_RES_INTERMEDIATE:
556 rc = ldap_parse_intermediate( ld, msg,
557 &retoid, &retdata, NULL, 0 );
558 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
559 sync_info_arrived = 1;
560 res_ber = ber_init( retdata );
561 ber_scanf( res_ber, "{e" /*"}"*/, &syncstate );
563 if ( ber_peek_tag( res_ber, &len )
564 == LDAP_SYNC_TAG_COOKIE ) {
565 ber_scanf( res_ber, /*"{"*/ "o}", &syncCookie );
567 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
569 LDAP_LOG( OPERATION, ERR,
570 "do_syncrepl : cookie required\n", 0, 0, 0 );
572 Debug( LDAP_DEBUG_ANY,
573 "do_syncrepl : cookie required\n", 0, 0, 0 );
578 value_match( &match, slap_schema.si_ad_entryCSN,
579 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
580 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
581 &syncCookie_req, &syncCookie, &text );
583 if ( syncCookie.bv_len && match < 0 ) {
584 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie);
587 if ( syncstate == LDAP_SYNC_STATE_MODE_DONE ) {
589 syncrepl_del_nonpresent( ld, &op );
591 si->sync_mode = LDAP_SYNC_LOG_MODE;
592 } else if ( syncstate == LDAP_SYNC_LOG_MODE_DONE ) {
593 si->sync_mode = LDAP_SYNC_PERSIST_MODE;
594 } else if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
595 si->sync_mode = LDAP_SYNC_PERSIST_MODE;
596 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ||
597 syncstate != LDAP_SYNC_LOG_MODE_DONE ) {
599 LDAP_LOG( OPERATION, ERR,
600 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
602 Debug( LDAP_DEBUG_ANY,
603 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
607 ldap_memfree( retoid );
608 ber_bvfree( retdata );
609 ber_free( res_ber, 1 );
613 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
614 " unknown intermediate "
615 "response\n", 0, 0, 0 );
617 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
618 "unknown intermediate response (%d)\n",
621 ldap_memfree( retoid );
622 ber_bvfree( retdata );
628 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
629 "unknown message\n", 0, 0, 0 );
631 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
632 "unknown message\n", 0, 0, 0 );
643 LDAP_LOG( OPERATION, ERR,
644 "do_syncrepl : unknown result\n", 0, 0, 0 );
646 Debug( LDAP_DEBUG_ANY,
647 "do_syncrepl : unknown result\n", 0, 0, 0 );
652 if ( syncCookie.bv_val )
653 ch_free( syncCookie.bv_val );
654 if ( syncCookie_req.bv_val )
655 ch_free( syncCookie_req.bv_val );
656 if ( syncUUID.bv_val )
657 ch_free( syncUUID.bv_val );
664 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
665 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
666 if ( si->type == LDAP_SYNC_REFRESH_ONLY ) {
667 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask );
669 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
671 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
677 syncrepl_message_to_entry(
682 Modifications **modlist,
684 struct berval *syncUUID,
685 struct berval *syncCookie
689 BerElement *ber = NULL;
691 struct berval bv = {0, NULL};
694 Modifications **modtail = modlist;
695 Backend *be = op->o_bd;
698 char txtbuf[SLAP_TEXT_BUFLEN];
699 size_t textlen = sizeof txtbuf;
701 struct berval **bvals = NULL;
703 struct berval bdn = {0, NULL};
705 struct berval empty_bv = { 0, NULL };
711 LDAPControl** rctrls = NULL;
712 BerElement* ctrl_ber;
716 Modifications *ml = NULL;
717 AttributeDescription** descs;
722 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
724 LDAP_LOG( OPERATION, ERR,
725 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
727 Debug( LDAP_DEBUG_ANY,
728 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
733 op->o_tag = LDAP_REQ_ADD;
735 rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
737 if ( rc != LDAP_SUCCESS ) {
739 LDAP_LOG( OPERATION, ERR,
740 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
742 Debug( LDAP_DEBUG_ANY,
743 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
748 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
749 dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, NULL );
753 while ( ber_remaining( ber ) ) {
754 tag = ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values );
756 if ( tag == LBER_ERROR ) break;
757 if ( tmp.sml_type.bv_val == NULL ) break;
759 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
761 mod->sml_op = LDAP_MOD_REPLACE;
762 mod->sml_next = NULL;
763 mod->sml_desc = NULL;
764 mod->sml_type = tmp.sml_type;
765 mod->sml_bvalues = tmp.sml_bvalues;
766 mod->sml_nvalues = NULL;
769 modtail = &mod->sml_next;
772 if ( ber_scanf( ber, "}") == LBER_ERROR ) {
774 LDAP_LOG( OPERATION, ERR,
775 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
777 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
784 tmpber = ldap_get_message_ber( msg );
785 ber = ber_dup( tmpber );
787 ber_scanf( ber, "{xx" );
789 rc = ldap_int_get_controls( ber, &rctrls );
791 if ( rc != LDAP_SUCCESS ) {
793 LDAP_LOG( OPERATION, ERR,
794 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
796 Debug( LDAP_DEBUG_ANY,
797 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
804 ctrl_ber = ber_alloc_t( LBER_USE_DER );
805 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
806 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
807 ber_reset( ctrl_ber, 1 );
808 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
809 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
810 ber_scanf( ctrl_ber, "o}", syncCookie );
812 ber_free( ctrl_ber, 1 );
815 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
816 " rctrls absent\n", 0, 0, 0 );
818 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
819 " rctrls absent\n", 0, 0, 0 );
823 if ( *syncstate == LDAP_SYNC_PRESENT ) {
826 } else if ( *syncstate == LDAP_SYNC_DELETE ) {
830 if ( *modlist == NULL ) {
832 LDAP_LOG( OPERATION, ERR,
833 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
835 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
841 while ( ml != NULL ) {
842 AttributeDescription *ad = NULL;
843 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, &text );
845 if( rc != LDAP_SUCCESS ) {
855 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
857 if ( rc != LDAP_SUCCESS ) {
859 LDAP_LOG( OPERATION, ERR,
860 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
862 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
868 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
869 if( rc != LDAP_SUCCESS ) {
871 LDAP_LOG( OPERATION, ERR,
872 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
874 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
887 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
889 const struct berval *uuid1 = v_uuid1;
890 const struct berval *uuid2 = v_uuid2;
891 int rc = uuid1->bv_len - uuid2->bv_len;
893 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
902 Modifications* modlist,
904 struct berval* syncUUID,
905 struct berval* syncCookie,
909 Backend *be = op->o_bd;
911 struct berval csn_bv = {0, NULL};
912 struct berval *syncuuid_bv = NULL;
913 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
915 SlapReply rs = {REP_RESULT};
916 int rc = LDAP_SUCCESS;
918 struct berval base_bv = {0, NULL};
926 ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
927 syncuuid_bv = ber_dupbv( NULL, syncUUID );
928 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
929 syncuuid_cmp, avl_dup_error );
932 if ( syncstate == LDAP_SYNC_PRESENT ) {
939 filterstr = (char *) sl_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1,
941 strcpy( filterstr, "entryUUID=" );
942 strcat( filterstr, syncUUID->bv_val );
945 si->syncUUID = syncUUID;
946 si->syncUUID_ndn = NULL;
948 filter = str2filter( filterstr );
949 ber_str2bv( filterstr, strlen(filterstr), 1, &op->ors_filterstr );
950 ch_free( filterstr );
951 op->ors_filter = filter;
952 op->ors_scope = LDAP_SCOPE_SUBTREE;
954 /* get syncrepl cookie of shadow replica from subentry */
955 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
956 dnPrettyNormal( 0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
957 ch_free( base_bv.bv_val );
959 /* set callback function */
960 op->o_callback = &cb;
961 cb.sc_response = dn_callback;
964 si->syncUUID_ndn = NULL;
966 rc = be->be_search( op, &rs );
968 ch_free( op->o_req_dn.bv_val );
969 ch_free( op->o_req_ndn.bv_val );
970 filter_free( op->ors_filter );
971 ch_free( op->ors_filterstr.bv_val );
973 cb.sc_response = null_callback;
976 if ( rc == LDAP_SUCCESS && si->syncUUID_ndn && si->sync_mode != LDAP_SYNC_LOG_MODE ) {
977 op->o_req_dn = *si->syncUUID_ndn;
978 op->o_req_ndn = *si->syncUUID_ndn;
979 op->o_tag = LDAP_REQ_DELETE;
980 rc = be->be_delete( op, &rs );
983 if ( si->syncUUID_ndn ) {
984 ber_bvfree( si->syncUUID_ndn );
987 switch ( syncstate ) {
989 case LDAP_SYNC_MODIFY :
991 if ( rc == LDAP_SUCCESS ||
992 rc == LDAP_REFERRAL ||
993 rc == LDAP_NO_SUCH_OBJECT ) {
995 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
996 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, syncUUID, op->o_tmpmemctx );
998 op->o_tag = LDAP_REQ_ADD;
1000 op->o_req_dn = e->e_name;
1001 op->o_req_ndn = e->e_nname;
1002 rc = be->be_add( op, &rs );
1004 if ( rc != LDAP_SUCCESS ) {
1005 if ( rc == LDAP_ALREADY_EXISTS ) {
1006 op->o_tag = LDAP_REQ_MODIFY;
1007 op->orm_modlist = modlist;
1008 op->o_req_dn = e->e_name;
1009 op->o_req_ndn = e->e_nname;
1010 rc = be->be_modify( op, &rs );
1012 if ( rc != LDAP_SUCCESS ) {
1014 LDAP_LOG( OPERATION, ERR,
1015 "syncrepl_entry : be_modify failed (%d)\n",
1018 Debug( LDAP_DEBUG_ANY,
1019 "syncrepl_entry : be_modify failed (%d)\n",
1025 } else if ( rc == LDAP_REFERRAL ||
1026 rc == LDAP_NO_SUCH_OBJECT ) {
1027 syncrepl_add_glue( si, ld, op, e,
1029 syncUUID, syncCookie);
1034 LDAP_LOG( OPERATION, ERR,
1035 "syncrepl_entry : be_add failed (%d)\n",
1038 Debug( LDAP_DEBUG_ANY,
1039 "syncrepl_entry : be_add failed (%d)\n",
1047 be_entry_release_w( op, e );
1052 LDAP_LOG( OPERATION, ERR,
1053 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1055 Debug( LDAP_DEBUG_ANY,
1056 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1062 case LDAP_SYNC_DELETE :
1063 if ( si->sync_mode == LDAP_SYNC_LOG_MODE ) {
1064 op->o_req_dn = *si->syncUUID_ndn;
1065 op->o_req_ndn = *si->syncUUID_ndn;
1066 op->o_tag = LDAP_REQ_DELETE;
1067 rc = be->be_delete( op, &rs );
1069 /* Already deleted otherwise */
1074 LDAP_LOG( OPERATION, ERR,
1075 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1077 Debug( LDAP_DEBUG_ANY,
1078 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1085 syncrepl_del_nonpresent(
1090 Backend* be = op->o_bd;
1091 syncinfo_t *si = op->o_si;
1093 struct berval base_bv = {0, NULL};
1095 SlapReply rs = {REP_RESULT};
1096 struct berval filterstr_bv = {0, NULL};
1097 struct nonpresent_entry *np_list, *np_prev;
1099 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
1100 dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1101 ch_free( base_bv.bv_val );
1103 filter = str2filter( si->filterstr );
1105 cb.sc_response = nonpresent_callback;
1108 op->o_callback = &cb;
1109 op->o_tag = LDAP_REQ_SEARCH;
1110 op->ors_scope = si->scope;
1111 op->ors_deref = LDAP_DEREF_NEVER;
1114 op->ors_attrsonly = 0;
1115 op->ors_attrs = NULL;
1116 op->ors_filter = filter;
1117 ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1119 be->be_search( op, &rs );
1121 if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1122 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1123 while ( np_list != NULL ) {
1124 LDAP_LIST_REMOVE( np_list, np_link );
1126 np_list = LDAP_LIST_NEXT( np_list, np_link );
1127 op->o_tag = LDAP_REQ_DELETE;
1128 op->o_callback = &cb;
1129 cb.sc_response = null_callback;
1131 op->o_req_dn = *np_prev->dn;
1132 op->o_req_ndn = *np_prev->ndn;
1133 op->o_bd->be_delete( op, &rs );
1134 ber_bvfree( np_prev->dn );
1135 ber_bvfree( np_prev->ndn );
1136 op->o_req_dn.bv_val = NULL;
1137 op->o_req_ndn.bv_val = NULL;
1142 if ( op->o_req_dn.bv_val )
1143 ch_free( op->o_req_dn.bv_val );
1144 if ( op->o_req_ndn.bv_val )
1145 ch_free( op->o_req_ndn.bv_val );
1146 filter_free( op->ors_filter );
1147 ch_free( op->ors_filterstr.bv_val );
1159 Modifications* modlist,
1161 struct berval* syncUUID,
1162 struct berval* syncCookie
1165 Backend *be = op->o_bd;
1166 struct berval uuid_bv = {0, NULL};
1170 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1173 struct berval dn = {0, NULL};
1174 struct berval pdn = {0, NULL};
1175 struct berval ndn = {0, NULL};
1176 struct berval rdn = {0, NULL};
1178 SlapReply rs = {REP_RESULT};
1179 Connection *conn = op->o_conn;
1182 op->o_tag = LDAP_REQ_ADD;
1183 op->o_callback = &cb;
1184 cb.sc_response = null_callback;
1187 ber_dupbv( &dn, &e->e_nname );
1188 ber_dupbv( &pdn, &e->e_nname );
1191 while ( !be_issuffix ( be, &pdn )) {
1192 dnParent( &dn, &pdn );
1193 dn.bv_val = pdn.bv_val;
1194 dn.bv_len = pdn.bv_len;
1199 for ( i = 0; i <= levels; i++ ) {
1200 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1201 ber_dupbv( &dn, &e->e_nname );
1205 for ( k = 0; k < j; k++ ) {
1206 dnParent( &dn, &pdn );
1207 dn.bv_val = pdn.bv_val;
1208 dn.bv_len = pdn.bv_len;
1211 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1212 ber_dupbv( &glue->e_name, &pdn );
1213 ber_dupbv( &glue->e_nname, &ndn );
1215 ch_free( pdn.bv_val );
1216 ch_free( ndn.bv_val );
1218 a = ch_calloc( 1, sizeof( Attribute ));
1219 a->a_desc = slap_schema.si_ad_objectClass;
1221 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1222 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1223 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1224 a->a_vals[2].bv_len = 0;
1225 a->a_vals[2].bv_val = NULL;
1227 a->a_nvals = ch_calloc( 3, sizeof( struct berval ));
1228 ber_str2bv( "top", strlen("top"), 1, &a->a_nvals[0] );
1229 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[1] );
1230 a->a_nvals[2].bv_len = 0;
1231 a->a_nvals[2].bv_val = NULL;
1233 a->a_next = glue->e_attrs;
1236 a = ch_calloc( 1, sizeof( Attribute ));
1237 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1239 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1240 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1241 a->a_vals[1].bv_len = 0;
1242 a->a_vals[1].bv_val = NULL;
1244 a->a_nvals = ch_calloc( 2, sizeof( struct berval ));
1245 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[0] );
1246 a->a_nvals[1].bv_len = 0;
1247 a->a_nvals[1].bv_val = NULL;
1249 a->a_next = glue->e_attrs;
1252 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1253 op->o_req_dn = e->e_name;
1254 op->o_req_ndn = e->e_nname;
1256 rc = be->be_add ( op, &rs );
1257 if ( rc == LDAP_SUCCESS )
1258 be_entry_release_w( op, e );
1263 op->o_req_dn = glue->e_name;
1264 op->o_req_ndn = glue->e_nname;
1266 rc = be->be_add ( op, &rs );
1267 if ( rc == LDAP_SUCCESS ) {
1268 be_entry_release_w( op, glue );
1270 /* incl. ALREADY EXIST */
1280 syncrepl_updateCookie(
1285 struct berval *syncCookie
1288 Backend *be = op->o_bd;
1290 Modifications *mlnext;
1292 Modifications *modlist;
1293 Modifications **modtail = &modlist;
1295 struct berval* ocbva = NULL;
1296 struct berval* cnbva = NULL;
1297 struct berval* ssbva = NULL;
1298 struct berval* scbva = NULL;
1303 char txtbuf[SLAP_TEXT_BUFLEN];
1304 size_t textlen = sizeof txtbuf;
1309 struct berval sub_bv = { 0, NULL };
1310 struct berval psubrdn = { 0, NULL };
1313 SlapReply rs = {REP_RESULT};
1315 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1316 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1317 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1318 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1320 /* update in memory cookie */
1321 if ( si->syncCookie != NULL ) {
1322 ber_bvfree( si->syncCookie );
1324 si->syncCookie = ber_dupbv( NULL, syncCookie );
1325 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1326 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1327 ber_str2bv( "syncConsumerSubentry",
1328 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1329 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1330 mod->sml_op = LDAP_MOD_REPLACE;
1331 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1332 mod->sml_bvalues = ocbva;
1334 modtail = &mod->sml_next;
1336 sprintf( substr, "syncrepl%d", si->id );
1337 sprintf( rdnstr, "cn=%s", substr );
1338 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1339 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1340 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1341 mod->sml_op = LDAP_MOD_REPLACE;
1342 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1343 mod->sml_bvalues = cnbva;
1345 modtail = &mod->sml_next;
1347 ber_dupbv( &scbva[0], si->syncCookie );
1348 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1349 mod->sml_op = LDAP_MOD_REPLACE;
1350 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1351 1, &mod->sml_type );
1352 mod->sml_bvalues = scbva;
1354 modtail = &mod->sml_next;
1356 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1357 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1358 mod->sml_op = LDAP_MOD_REPLACE;
1359 ber_str2bv( "subtreeSpecification",
1360 strlen("subtreeSpecification"), 1, &mod->sml_type );
1361 mod->sml_bvalues = ssbva;
1363 modtail = &mod->sml_next;
1365 rc = slap_mods_check( modlist, 1, &text, txtbuf, textlen, NULL );
1367 if ( rc != LDAP_SUCCESS ) {
1369 LDAP_LOG( OPERATION, ERR,
1370 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1372 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1377 op->o_tag = LDAP_REQ_ADD;
1378 rc = slap_mods_opattrs( op, modlist, modtail,
1379 &text,txtbuf, textlen );
1381 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1382 mlnext = ml->sml_next;
1383 ml->sml_op = LDAP_MOD_REPLACE;
1386 if( rc != LDAP_SUCCESS ) {
1388 LDAP_LOG( OPERATION, ERR,
1389 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1391 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1396 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1398 build_new_dn( &sub_bv, pdn, &psubrdn );
1399 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
1400 ch_free( sub_bv.bv_val );
1401 ch_free( psubrdn.bv_val );
1405 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1407 if( rc != LDAP_SUCCESS ) {
1409 LDAP_LOG( OPERATION, ERR,
1410 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1412 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1417 cb.sc_response = null_callback;
1420 op->o_callback = &cb;
1421 op->o_req_dn = e->e_name;
1422 op->o_req_ndn = e->e_nname;
1424 /* update persistent cookie */
1425 update_cookie_retry:
1426 op->o_tag = LDAP_REQ_MODIFY;
1427 op->orm_modlist = modlist;
1428 rc = be->be_modify( op, &rs );
1430 if ( rc != LDAP_SUCCESS ) {
1431 if ( rc == LDAP_REFERRAL ||
1432 rc == LDAP_NO_SUCH_OBJECT ) {
1433 op->o_tag = LDAP_REQ_ADD;
1435 rc = be->be_add( op, &rs );
1436 if ( rc != LDAP_SUCCESS ) {
1437 if ( rc == LDAP_ALREADY_EXISTS ) {
1438 goto update_cookie_retry;
1439 } else if ( rc == LDAP_REFERRAL ||
1440 rc == LDAP_NO_SUCH_OBJECT ) {
1442 LDAP_LOG( OPERATION, ERR,
1443 "cookie will be non-persistent\n",
1446 Debug( LDAP_DEBUG_ANY,
1447 "cookie will be non-persistent\n",
1452 LDAP_LOG( OPERATION, ERR,
1453 "be_add failed (%d)\n",
1456 Debug( LDAP_DEBUG_ANY,
1457 "be_add failed (%d)\n",
1462 be_entry_release_w( op, e );
1467 LDAP_LOG( OPERATION, ERR,
1468 "be_modify failed (%d)\n", rc, 0, 0 );
1470 Debug( LDAP_DEBUG_ANY,
1471 "be_modify failed (%d)\n", rc, 0, 0 );
1481 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1482 mlnext = ml->sml_next;
1490 avl_ber_bvfree( void *bv )
1495 if ( ((struct berval *)bv)->bv_val != NULL ) {
1496 ber_memfree ( ((struct berval *)bv)->bv_val );
1498 ber_memfree ( (char *) bv );
1507 syncinfo_t *si = op->o_callback->sc_private;
1510 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1512 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1515 si->syncCookie = NULL;
1517 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1519 return LDAP_SUCCESS;
1528 syncinfo_t *si = op->o_callback->sc_private;
1530 if ( rs->sr_type == REP_SEARCH ) {
1531 if ( si->syncUUID_ndn != NULL ) {
1533 LDAP_LOG( OPERATION, ERR,
1534 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1536 Debug( LDAP_DEBUG_ANY,
1537 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1540 if ( rs->sr_entry == NULL ) {
1541 si->syncUUID_ndn = NULL;
1543 si->syncUUID_ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1548 return LDAP_SUCCESS;
1552 nonpresent_callback(
1557 syncinfo_t *si = op->o_callback->sc_private;
1560 struct berval* present_uuid = NULL;
1562 SlapReply rs_cb = {REP_RESULT};
1563 struct nonpresent_entry *np_entry;
1565 if ( rs->sr_type == REP_RESULT ) {
1566 count = avl_free( si->presentlist, avl_ber_bvfree );
1567 si->presentlist = NULL;
1568 return LDAP_SUCCESS;
1569 } else if ( rs->sr_type == REP_SEARCH ) {
1570 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1575 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1577 if ( present_uuid == NULL ) {
1578 np_entry = (struct nonpresent_entry *)
1579 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1580 np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1581 np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1582 LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1584 avl_delete( &si->presentlist,
1585 &a->a_vals[0], syncuuid_cmp );
1587 return LDAP_SUCCESS;
1589 return LDAP_SUCCESS;
1600 syncinfo_t *si = op->o_callback->sc_private;
1602 if ( rs->sr_err != LDAP_SUCCESS &&
1603 rs->sr_err != LDAP_REFERRAL &&
1604 rs->sr_err != LDAP_ALREADY_EXISTS &&
1605 rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
1607 LDAP_LOG( OPERATION, ERR,
1608 "null_callback : error code 0x%x\n",
1611 Debug( LDAP_DEBUG_ANY,
1612 "null_callback : error code 0x%x\n",
1616 return LDAP_SUCCESS;
1621 str2clist( char ***out, char *in, const char *brkstr )
1630 /* find last element in list */
1631 for (i = 0; *out && *out[i]; i++);
1633 /* protect the input string from strtok */
1634 str = ch_strdup( in );
1636 if ( *str == '\0' ) {
1641 /* Count words in string */
1643 for ( s = str; *s; s++ ) {
1644 if ( strchr( brkstr, *s ) != NULL ) {
1649 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1651 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1653 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1655 *new = ch_strdup( s );