]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
use Paged Results when talking to targets (in response to ITS#6664)
[openldap] / servers / slapd / back-meta / config.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2010 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29
30 #include "slap.h"
31 #include "lutil.h"
32 #include "../back-ldap/back-ldap.h"
33 #undef ldap_debug       /* silence a warning in ldap-int.h */
34 #include "../../../libraries/libldap/ldap-int.h"
35 #include "back-meta.h"
36
37 static int
38 meta_back_new_target( 
39         metatarget_t    **mtp )
40 {
41         char                    *rargv[ 3 ];
42         metatarget_t            *mt;
43
44         *mtp = NULL;
45
46         mt = ch_calloc( sizeof( metatarget_t ), 1 );
47
48         mt->mt_rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
49         if ( mt->mt_rwmap.rwm_rw == NULL ) {
50                 ch_free( mt );
51                 return -1;
52         }
53
54         /*
55          * the filter rewrite as a string must be disabled
56          * by default; it can be re-enabled by adding rules;
57          * this creates an empty rewriteContext
58          */
59         rargv[ 0 ] = "rewriteContext";
60         rargv[ 1 ] = "searchFilter";
61         rargv[ 2 ] = NULL;
62         rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
63
64         rargv[ 0 ] = "rewriteContext";
65         rargv[ 1 ] = "default";
66         rargv[ 2 ] = NULL;
67         rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
68
69         ldap_pvt_thread_mutex_init( &mt->mt_uri_mutex );
70
71         mt->mt_idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
72         mt->mt_idassert_authmethod = LDAP_AUTH_NONE;
73         mt->mt_idassert_tls = SB_TLS_DEFAULT;
74
75         /* by default, use proxyAuthz control on each operation */
76         mt->mt_idassert_flags = LDAP_BACK_AUTH_PRESCRIPTIVE;
77
78         *mtp = mt;
79
80         return 0;
81 }
82
83 static int
84 check_true_false( char *str )
85 {
86         if ( strcasecmp( str, "true" ) == 0 || strcasecmp( str, "yes" ) == 0 ) {
87                 return 1;
88         }
89
90         if ( strcasecmp( str, "false" ) == 0 || strcasecmp( str, "no" ) == 0 ) {
91                 return 0;
92         }
93
94         return -1;
95 }
96
97
98 int
99 meta_back_db_config(
100                 BackendDB       *be,
101                 const char      *fname,
102                 int             lineno,
103                 int             argc,
104                 char            **argv
105 )
106 {
107         metainfo_t      *mi = ( metainfo_t * )be->be_private;
108
109         assert( mi != NULL );
110
111         /* URI of server to query */
112         if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
113                 int             i = mi->mi_ntargets;
114                 LDAPURLDesc     *ludp;
115                 struct berval   dn;
116                 int             rc;
117                 int             c;
118
119                 metatarget_t    *mt;
120
121                 char            **uris = NULL;
122                 
123                 if ( argc == 1 ) {
124                         Debug( LDAP_DEBUG_ANY,
125         "%s: line %d: missing URI "
126         "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
127                                 fname, lineno, 0 );
128                         return 1;
129                 }
130
131                 if ( be->be_nsuffix == NULL ) {
132                         Debug( LDAP_DEBUG_ANY,
133         "%s: line %d: the suffix must be defined before any target.\n",
134                                 fname, lineno, 0 );
135                         return 1;
136                 }
137                 
138                 ++mi->mi_ntargets;
139
140                 mi->mi_targets = ( metatarget_t ** )ch_realloc( mi->mi_targets, 
141                         sizeof( metatarget_t * ) * mi->mi_ntargets );
142                 if ( mi->mi_targets == NULL ) {
143                         Debug( LDAP_DEBUG_ANY,
144         "%s: line %d: out of memory while storing server name"
145         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
146                                 fname, lineno, 0 );
147                         return 1;
148                 }
149
150                 if ( meta_back_new_target( &mi->mi_targets[ i ] ) != 0 ) {
151                         Debug( LDAP_DEBUG_ANY,
152         "%s: line %d: unable to init server"
153         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
154                                 fname, lineno, 0 );
155                         return 1;
156                 }
157
158                 mt = mi->mi_targets[ i ];
159
160                 mt->mt_rebind_f = mi->mi_rebind_f;
161                 mt->mt_urllist_f = mi->mi_urllist_f;
162                 mt->mt_urllist_p = mt;
163
164                 mt->mt_nretries = mi->mi_nretries;
165                 mt->mt_quarantine = mi->mi_quarantine;
166                 if ( META_BACK_QUARANTINE( mi ) ) {
167                         ldap_pvt_thread_mutex_init( &mt->mt_quarantine_mutex );
168                 }
169                 mt->mt_flags = mi->mi_flags;
170                 mt->mt_version = mi->mi_version;
171 #ifdef SLAPD_META_CLIENT_PR
172                 mt->mt_ps = mi->mi_ps;
173 #endif /* SLAPD_META_CLIENT_PR */
174                 mt->mt_network_timeout = mi->mi_network_timeout;
175                 mt->mt_bind_timeout = mi->mi_bind_timeout;
176                 for ( c = 0; c < SLAP_OP_LAST; c++ ) {
177                         mt->mt_timeout[ c ] = mi->mi_timeout[ c ];
178                 }
179
180                 for ( c = 1; c < argc; c++ ) {
181                         char    **tmpuris = ldap_str2charray( argv[ c ], "\t" );
182
183                         if ( tmpuris == NULL ) {
184                                 Debug( LDAP_DEBUG_ANY,
185         "%s: line %d: unable to parse URIs #%d"
186         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
187                                 fname, lineno, c - 1 );
188                                 return 1;
189                         }
190
191                         if ( c == 0 ) {
192                                 uris = tmpuris;
193
194                         } else {
195                                 ldap_charray_merge( &uris, tmpuris );
196                                 ldap_charray_free( tmpuris );
197                         }
198                 }
199
200                 for ( c = 0; uris[ c ] != NULL; c++ ) {
201                         char *tmpuri = NULL;
202
203                         /*
204                          * uri MUST be legal!
205                          */
206                         if ( ldap_url_parselist_ext( &ludp, uris[ c ], "\t",
207                                         LDAP_PVT_URL_PARSE_NONE ) != LDAP_SUCCESS
208                                 || ludp->lud_next != NULL )
209                         {
210                                 Debug( LDAP_DEBUG_ANY,
211                 "%s: line %d: unable to parse URI #%d"
212                 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
213                                         fname, lineno, c );
214                                 ldap_charray_free( uris );
215                                 return 1;
216                         }
217
218                         if ( c == 0 ) {
219
220                                 /*
221                                  * uri MUST have the <dn> part!
222                                  */
223                                 if ( ludp->lud_dn == NULL ) {
224                                         Debug( LDAP_DEBUG_ANY,
225                         "%s: line %d: missing <naming context> "
226                         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
227                                                 fname, lineno, 0 );
228                                         ldap_free_urllist( ludp );
229                                         ldap_charray_free( uris );
230                                         return 1;
231                                 }
232
233                                 /*
234                                  * copies and stores uri and suffix
235                                  */
236                                 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
237                                 rc = dnPrettyNormal( NULL, &dn, &mt->mt_psuffix,
238                                         &mt->mt_nsuffix, NULL );
239                                 if ( rc != LDAP_SUCCESS ) {
240                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
241                                                 "target \"%s\" DN is invalid\n",
242                                                 fname, lineno, argv[ 1 ] );
243                                         ldap_free_urllist( ludp );
244                                         ldap_charray_free( uris );
245                                         return( 1 );
246                                 }
247
248                                 ludp->lud_dn[ 0 ] = '\0';
249
250                                 switch ( ludp->lud_scope ) {
251                                 case LDAP_SCOPE_DEFAULT:
252                                         mt->mt_scope = LDAP_SCOPE_SUBTREE;
253                                         break;
254
255                                 case LDAP_SCOPE_SUBTREE:
256                                 case LDAP_SCOPE_SUBORDINATE:
257                                         mt->mt_scope = ludp->lud_scope;
258                                         break;
259
260                                 default:
261                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
262                                                 "invalid scope for target \"%s\"\n",
263                                                 fname, lineno, argv[ 1 ] );
264                                         ldap_free_urllist( ludp );
265                                         ldap_charray_free( uris );
266                                         return( 1 );
267                                 }
268
269                         } else {
270                                 /* check all, to apply the scope check on the first one */
271                                 if ( ludp->lud_dn != NULL && ludp->lud_dn[ 0 ] != '\0' ) {
272                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
273                                                 "multiple URIs must have "
274                                                 "no DN part\n",
275                                                 fname, lineno, 0 );
276                                         ldap_free_urllist( ludp );
277                                         ldap_charray_free( uris );
278                                         return( 1 );
279
280                                 }
281                         }
282
283                         tmpuri = ldap_url_list2urls( ludp );
284                         ldap_free_urllist( ludp );
285                         if ( tmpuri == NULL ) {
286                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
287                                         fname, lineno, 0 );
288                                 ldap_charray_free( uris );
289                                 return( 1 );
290                         }
291                         ldap_memfree( uris[ c ] );
292                         uris[ c ] = tmpuri;
293                 }
294
295                 mt->mt_uri = ldap_charray2str( uris, " " );
296                 ldap_charray_free( uris );
297                 if ( mt->mt_uri == NULL) {
298                         Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
299                                 fname, lineno, 0 );
300                         return( 1 );
301                 }
302                 
303                 /*
304                  * uri MUST be a branch of suffix!
305                  */
306                 for ( c = 0; !BER_BVISNULL( &be->be_nsuffix[ c ] ); c++ ) {
307                         if ( dnIsSuffix( &mt->mt_nsuffix, &be->be_nsuffix[ c ] ) ) {
308                                 break;
309                         }
310                 }
311
312                 if ( BER_BVISNULL( &be->be_nsuffix[ c ] ) ) {
313                         Debug( LDAP_DEBUG_ANY,
314         "%s: line %d: <naming context> of URI must be within the naming context of this database.\n",
315                                 fname, lineno, 0 );
316                         return 1;
317                 }
318
319         /* subtree-exclude */
320         } else if ( strcasecmp( argv[ 0 ], "subtree-exclude" ) == 0 ) {
321                 int             i = mi->mi_ntargets - 1;
322                 struct berval   dn, ndn;
323
324                 if ( i < 0 ) {
325                         Debug( LDAP_DEBUG_ANY,
326         "%s: line %d: need \"uri\" directive first\n",
327                                 fname, lineno, 0 );
328                         return 1;
329                 }
330                 
331                 switch ( argc ) {
332                 case 1:
333                         Debug( LDAP_DEBUG_ANY,
334         "%s: line %d: missing DN in \"subtree-exclude <DN>\" line\n",
335                             fname, lineno, 0 );
336                         return 1;
337
338                 case 2:
339                         break;
340
341                 default:
342                         Debug( LDAP_DEBUG_ANY,
343         "%s: line %d: too many args in \"subtree-exclude <DN>\" line\n",
344                             fname, lineno, 0 );
345                         return 1;
346                 }
347
348                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
349                 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL )
350                         != LDAP_SUCCESS )
351                 {
352                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
353                                         "subtree-exclude DN=\"%s\" is invalid\n",
354                                         fname, lineno, argv[ 1 ] );
355                         return( 1 );
356                 }
357
358                 if ( !dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_nsuffix ) ) {
359                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
360                                         "subtree-exclude DN=\"%s\" "
361                                         "must be subtree of target\n",
362                                         fname, lineno, argv[ 1 ] );
363                         ber_memfree( ndn.bv_val );
364                         return( 1 );
365                 }
366
367                 if ( mi->mi_targets[ i ]->mt_subtree_exclude != NULL ) {
368                         int             j;
369
370                         for ( j = 0; !BER_BVISNULL( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ); j++ )
371                         {
372                                 if ( dnIsSuffix( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ], &ndn ) ) {
373                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
374                                                         "subtree-exclude DN=\"%s\" "
375                                                         "is suffix of another subtree-exclude\n",
376                                                         fname, lineno, argv[ 1 ] );
377                                         /* reject, because it might be superior
378                                          * to more than one subtree-exclude */
379                                         ber_memfree( ndn.bv_val );
380                                         return( 1 );
381
382                                 } else if ( dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ) ) {
383                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
384                                                         "another subtree-exclude is suffix of "
385                                                         "subtree-exclude DN=\"%s\"\n",
386                                                         fname, lineno, argv[ 1 ] );
387                                         ber_memfree( ndn.bv_val );
388                                         return( 0 );
389                                 }
390                         }
391                 }
392
393                 ber_bvarray_add( &mi->mi_targets[ i ]->mt_subtree_exclude, &ndn );
394
395         /* default target directive */
396         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
397                 int             i = mi->mi_ntargets - 1;
398                 
399                 if ( argc == 1 ) {
400                         if ( i < 0 ) {
401                                 Debug( LDAP_DEBUG_ANY,
402         "%s: line %d: \"default-target\" alone need be"
403         " inside a \"uri\" directive\n",
404                                         fname, lineno, 0 );
405                                 return 1;
406                         }
407                         mi->mi_defaulttarget = i;
408
409                 } else {
410                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
411                                 if ( i >= 0 ) {
412                                         Debug( LDAP_DEBUG_ANY,
413         "%s: line %d: \"default-target none\""
414         " should go before uri definitions\n",
415                                                 fname, lineno, 0 );
416                                 }
417                                 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
418
419                         } else {
420                                 
421                                 if ( lutil_atoi( &mi->mi_defaulttarget, argv[ 1 ] ) != 0
422                                         || mi->mi_defaulttarget < 0
423                                         || mi->mi_defaulttarget >= i - 1 )
424                                 {
425                                         Debug( LDAP_DEBUG_ANY,
426         "%s: line %d: illegal target number %d\n",
427                                                 fname, lineno, mi->mi_defaulttarget );
428                                         return 1;
429                                 }
430                         }
431                 }
432                 
433         /* ttl of dn cache */
434         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
435                 if ( argc != 2 ) {
436                         Debug( LDAP_DEBUG_ANY,
437         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
438                                 fname, lineno, 0 );
439                         return 1;
440                 }
441                 
442                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
443                         mi->mi_cache.ttl = META_DNCACHE_FOREVER;
444
445                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
446                         mi->mi_cache.ttl = META_DNCACHE_DISABLED;
447
448                 } else {
449                         unsigned long   t;
450
451                         if ( lutil_parse_time( argv[ 1 ], &t ) != 0 ) {
452                                 Debug( LDAP_DEBUG_ANY,
453         "%s: line %d: unable to parse ttl \"%s\" in \"dncache-ttl <ttl>\" line\n",
454                                         fname, lineno, argv[ 1 ] );
455                                 return 1;
456                         }
457                         mi->mi_cache.ttl = (time_t)t;
458                 }
459
460         /* network timeout when connecting to ldap servers */
461         } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
462                 unsigned long   t;
463                 time_t          *tp = mi->mi_ntargets ?
464                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_network_timeout
465                                 : &mi->mi_network_timeout;
466
467                 if ( argc != 2 ) {
468                         Debug( LDAP_DEBUG_ANY,
469         "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
470                                 fname, lineno, 0 );
471                         return 1;
472                 }
473
474                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
475                         Debug( LDAP_DEBUG_ANY,
476         "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
477                                 fname, lineno, argv[ 1 ] );
478                         return 1;
479
480                 }
481
482                 *tp = (time_t)t;
483
484         /* idle timeout when connecting to ldap servers */
485         } else if ( strcasecmp( argv[ 0 ], "idle-timeout" ) == 0 ) {
486                 unsigned long   t;
487
488                 switch ( argc ) {
489                 case 1:
490                         Debug( LDAP_DEBUG_ANY,
491         "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
492                                 fname, lineno, 0 );
493                         return 1;
494                 case 2:
495                         break;
496                 default:
497                         Debug( LDAP_DEBUG_ANY,
498         "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
499                                 fname, lineno, 0 );
500                         return 1;
501                 }
502
503                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
504                         Debug( LDAP_DEBUG_ANY,
505         "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
506                                 fname, lineno, argv[ 1 ] );
507                         return 1;
508
509                 }
510
511                 mi->mi_idle_timeout = (time_t)t;
512
513         /* conn ttl */
514         } else if ( strcasecmp( argv[ 0 ], "conn-ttl" ) == 0 ) {
515                 unsigned long   t;
516
517                 switch ( argc ) {
518                 case 1:
519                         Debug( LDAP_DEBUG_ANY,
520         "%s: line %d: missing ttl value in \"conn-ttl <seconds>\" line\n",
521                                 fname, lineno, 0 );
522                         return 1;
523                 case 2:
524                         break;
525                 default:
526                         Debug( LDAP_DEBUG_ANY,
527         "%s: line %d: extra cruft after ttl value in \"conn-ttl <seconds>\" line\n",
528                                 fname, lineno, 0 );
529                         return 1;
530                 }
531
532                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
533                         Debug( LDAP_DEBUG_ANY,
534         "%s: line %d: unable to parse ttl \"%s\" in \"conn-ttl <seconds>\" line\n",
535                                 fname, lineno, argv[ 1 ] );
536                         return 1;
537
538                 }
539
540                 mi->mi_conn_ttl = (time_t)t;
541
542         /* bind timeout when connecting to ldap servers */
543         } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
544                 unsigned long   t;
545                 struct timeval  *tp = mi->mi_ntargets ?
546                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_bind_timeout
547                                 : &mi->mi_bind_timeout;
548
549                 switch ( argc ) {
550                 case 1:
551                         Debug( LDAP_DEBUG_ANY,
552         "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
553                                 fname, lineno, 0 );
554                         return 1;
555                 case 2:
556                         break;
557                 default:
558                         Debug( LDAP_DEBUG_ANY,
559         "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
560                                 fname, lineno, 0 );
561                         return 1;
562                 }
563
564                 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
565                         Debug( LDAP_DEBUG_ANY,
566         "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
567                                 fname, lineno, argv[ 1 ] );
568                         return 1;
569
570                 }
571
572                 tp->tv_sec = t/1000000;
573                 tp->tv_usec = t%1000000;
574
575         /* name to use for meta_back_group */
576         } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
577                         || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
578         {
579                 int             i = mi->mi_ntargets - 1;
580                 struct berval   dn;
581
582                 if ( i < 0 ) {
583                         Debug( LDAP_DEBUG_ANY,
584         "%s: line %d: need \"uri\" directive first\n",
585                                 fname, lineno, 0 );
586                         return 1;
587                 }
588                 
589                 if ( argc != 2 ) {
590                         Debug( LDAP_DEBUG_ANY,
591         "%s: line %d: missing name in \"binddn <name>\" line\n",
592                                 fname, lineno, 0 );
593                         return 1;
594                 }
595
596                 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
597                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
598                                 "\"binddn\" statement is deprecated; "
599                                 "use \"acl-authcDN\" instead\n",
600                                 fname, lineno, 0 );
601                         /* FIXME: some day we'll need to throw an error */
602                 }
603
604                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
605                 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ]->mt_binddn,
606                         NULL ) != LDAP_SUCCESS )
607                 {
608                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
609                                         "bind DN '%s' is invalid\n",
610                                         fname, lineno, argv[ 1 ] );
611                         return( 1 );
612                 }
613
614         /* password to use for meta_back_group */
615         } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
616                         || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
617         {
618                 int             i = mi->mi_ntargets - 1;
619
620                 if ( i < 0 ) {
621                         Debug( LDAP_DEBUG_ANY,
622         "%s: line %d: need \"uri\" directive first\n",
623                                 fname, lineno, 0 );
624                         return 1;
625                 }
626
627                 if ( argc != 2 ) {
628                         Debug( LDAP_DEBUG_ANY,
629         "%s: line %d: missing password in \"bindpw <password>\" line\n",
630                             fname, lineno, 0 );
631                         return 1;
632                 }
633
634                 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
635                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
636                                 "\"bindpw\" statement is deprecated; "
637                                 "use \"acl-passwd\" instead\n",
638                                 fname, lineno, 0 );
639                         /* FIXME: some day we'll need to throw an error */
640                 }
641
642                 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ]->mt_bindpw );
643                 
644         /* save bind creds for referral rebinds? */
645         } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
646                 unsigned        *flagsp = mi->mi_ntargets ?
647                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
648                                 : &mi->mi_flags;
649
650                 if ( argc > 2 ) {
651                         Debug( LDAP_DEBUG_ANY,
652         "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
653                             fname, lineno, 0 );
654                         return( 1 );
655                 }
656
657                 if ( argc == 1 ) {
658                         Debug( LDAP_DEBUG_ANY,
659         "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
660                             fname, lineno, 0 );
661                         *flagsp |= LDAP_BACK_F_SAVECRED;
662
663                 } else {
664                         switch ( check_true_false( argv[ 1 ] ) ) {
665                         case 0:
666                                 *flagsp &= ~LDAP_BACK_F_SAVECRED;
667                                 break;
668
669                         case 1:
670                                 *flagsp |= LDAP_BACK_F_SAVECRED;
671                                 break;
672
673                         default:
674                                 Debug( LDAP_DEBUG_ANY,
675         "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
676                                     fname, lineno, argv[ 1 ] );
677                                 return 1;
678                         }
679                 }
680
681         } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
682                 unsigned        *flagsp = mi->mi_ntargets ?
683                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
684                                 : &mi->mi_flags;
685
686                 if ( argc != 2 ) {
687                         Debug( LDAP_DEBUG_ANY,
688         "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
689                                 fname, lineno, 0 );
690                         return( 1 );
691                 }
692
693                 /* this is the default; we add it because the default might change... */
694                 switch ( check_true_false( argv[ 1 ] ) ) {
695                 case 1:
696                         *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
697                         break;
698
699                 case 0:
700                         *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
701                         break;
702
703                 default:
704                         Debug( LDAP_DEBUG_ANY,
705                 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
706                                 fname, lineno, argv[ 1 ] );
707                         return( 1 );
708                 }
709         
710         } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
711                 unsigned        *flagsp = mi->mi_ntargets ?
712                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
713                                 : &mi->mi_flags;
714
715                 /* start */
716                 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
717                         *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
718         
719                 /* try start tls */
720                 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
721                         *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
722                         *flagsp |= LDAP_BACK_F_USE_TLS;
723         
724                 /* propagate start tls */
725                 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
726                         *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
727                 
728                 /* try start tls */
729                 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
730                         *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
731                         *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
732
733                 } else {
734                         Debug( LDAP_DEBUG_ANY,
735                 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
736                                 fname, lineno, argv[ 1 ] );
737                         return( 1 );
738                 }
739
740                 if ( argc > 2 ) {
741                         metatarget_t    *mt = NULL;
742                         int             i;
743
744                         if ( mi->mi_ntargets - 1 < 0 ) {
745                                 Debug( LDAP_DEBUG_ANY,
746                 "%s: line %d: need \"uri\" directive first\n",
747                                         fname, lineno, 0 );
748                                 return 1;
749                         }
750
751                         mt = mi->mi_targets[ mi->mi_ntargets - 1 ];
752
753                         for ( i = 2; i < argc; i++ ) {
754                                 if ( bindconf_tls_parse( argv[i], &mt->mt_tls ))
755                                         return 1;
756                         }
757                         bindconf_tls_defaults( &mt->mt_tls );
758                 }
759
760         } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
761                 unsigned        *flagsp = mi->mi_ntargets ?
762                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
763                                 : &mi->mi_flags;
764
765                 if ( argc != 2 ) {
766                         Debug( LDAP_DEBUG_ANY,
767                 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
768                                 fname, lineno, 0 );
769                         return( 1 );
770                 }
771
772                 switch ( check_true_false( argv[ 1 ] ) ) {
773                 case 0:
774                         *flagsp &= ~LDAP_BACK_F_T_F_MASK2;
775                         break;
776
777                 case 1:
778                         *flagsp |= LDAP_BACK_F_T_F;
779                         break;
780
781                 default:
782                         if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
783                                 *flagsp |= LDAP_BACK_F_T_F_DISCOVER;
784
785                         } else {
786                                 Debug( LDAP_DEBUG_ANY,
787         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
788                                         fname, lineno, argv[ 1 ] );
789                                 return 1;
790                         }
791                         break;
792                 }
793
794         /* onerr? */
795         } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
796                 if ( argc != 2 ) {
797                         Debug( LDAP_DEBUG_ANY,
798         "%s: line %d: \"onerr {CONTINUE|report|stop}\" takes 1 argument\n",
799                                 fname, lineno, 0 );
800                         return( 1 );
801                 }
802
803                 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
804                         mi->mi_flags &= ~META_BACK_F_ONERR_MASK;
805
806                 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
807                         mi->mi_flags |= META_BACK_F_ONERR_STOP;
808
809                 } else if ( strcasecmp( argv[ 1 ], "report" ) == 0 ) {
810                         mi->mi_flags |= META_BACK_F_ONERR_REPORT;
811
812                 } else {
813                         Debug( LDAP_DEBUG_ANY,
814         "%s: line %d: \"onerr {CONTINUE|report|stop}\": invalid arg \"%s\".\n",
815                                 fname, lineno, argv[ 1 ] );
816                         return 1;
817                 }
818
819         /* bind-defer? */
820         } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0
821                 || strcasecmp( argv[ 0 ], "root-bind-defer" ) == 0 )
822         {
823                 if ( argc != 2 ) {
824                         Debug( LDAP_DEBUG_ANY,
825         "%s: line %d: \"[pseudo]root-bind-defer {TRUE|false}\" takes 1 argument\n",
826                                 fname, lineno, 0 );
827                         return( 1 );
828                 }
829
830                 switch ( check_true_false( argv[ 1 ] ) ) {
831                 case 0:
832                         mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
833                         break;
834
835                 case 1:
836                         mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
837                         break;
838
839                 default:
840                         Debug( LDAP_DEBUG_ANY,
841         "%s: line %d: \"[pseudo]root-bind-defer {TRUE|false}\": invalid arg \"%s\".\n",
842                                 fname, lineno, argv[ 1 ] );
843                         return 1;
844                 }
845
846         /* single-conn? */
847         } else if ( strcasecmp( argv[ 0 ], "single-conn" ) == 0 ) {
848                 if ( argc != 2 ) {
849                         Debug( LDAP_DEBUG_ANY,
850         "%s: line %d: \"single-conn {FALSE|true}\" takes 1 argument\n",
851                                 fname, lineno, 0 );
852                         return( 1 );
853                 }
854
855                 if ( mi->mi_ntargets > 0 ) {
856                         Debug( LDAP_DEBUG_ANY,
857         "%s: line %d: \"single-conn\" must appear before target definitions\n",
858                                 fname, lineno, 0 );
859                         return( 1 );
860                 }
861
862                 switch ( check_true_false( argv[ 1 ] ) ) {
863                 case 0:
864                         mi->mi_flags &= ~LDAP_BACK_F_SINGLECONN;
865                         break;
866
867                 case 1:
868                         mi->mi_flags |= LDAP_BACK_F_SINGLECONN;
869                         break;
870
871                 default:
872                         Debug( LDAP_DEBUG_ANY,
873         "%s: line %d: \"single-conn {FALSE|true}\": invalid arg \"%s\".\n",
874                                 fname, lineno, argv[ 1 ] );
875                         return 1;
876                 }
877
878         /* use-temporaries? */
879         } else if ( strcasecmp( argv[ 0 ], "use-temporary-conn" ) == 0 ) {
880                 if ( argc != 2 ) {
881                         Debug( LDAP_DEBUG_ANY,
882         "%s: line %d: \"use-temporary-conn {FALSE|true}\" takes 1 argument\n",
883                                 fname, lineno, 0 );
884                         return( 1 );
885                 }
886
887                 if ( mi->mi_ntargets > 0 ) {
888                         Debug( LDAP_DEBUG_ANY,
889         "%s: line %d: \"use-temporary-conn\" must appear before target definitions\n",
890                                 fname, lineno, 0 );
891                         return( 1 );
892                 }
893
894                 switch ( check_true_false( argv[ 1 ] ) ) {
895                 case 0:
896                         mi->mi_flags &= ~LDAP_BACK_F_USE_TEMPORARIES;
897                         break;
898
899                 case 1:
900                         mi->mi_flags |= LDAP_BACK_F_USE_TEMPORARIES;
901                         break;
902
903                 default:
904                         Debug( LDAP_DEBUG_ANY,
905         "%s: line %d: \"use-temporary-conn {FALSE|true}\": invalid arg \"%s\".\n",
906                                 fname, lineno, argv[ 1 ] );
907                         return 1;
908                 }
909
910         /* privileged connections pool max size ? */
911         } else if ( strcasecmp( argv[ 0 ], "conn-pool-max" ) == 0 ) {
912                 if ( argc != 2 ) {
913                         Debug( LDAP_DEBUG_ANY,
914         "%s: line %d: \"conn-pool-max <n>\" takes 1 argument\n",
915                                 fname, lineno, 0 );
916                         return( 1 );
917                 }
918
919                 if ( mi->mi_ntargets > 0 ) {
920                         Debug( LDAP_DEBUG_ANY,
921         "%s: line %d: \"conn-pool-max\" must appear before target definitions\n",
922                                 fname, lineno, 0 );
923                         return( 1 );
924                 }
925
926                 if ( lutil_atoi( &mi->mi_conn_priv_max, argv[1] )
927                         || mi->mi_conn_priv_max < LDAP_BACK_CONN_PRIV_MIN
928                         || mi->mi_conn_priv_max > LDAP_BACK_CONN_PRIV_MAX )
929                 {
930                         Debug( LDAP_DEBUG_ANY,
931         "%s: line %d: \"conn-pool-max <n>\": invalid arg \"%s\".\n",
932                                 fname, lineno, argv[ 1 ] );
933                         return 1;
934                 }
935
936         } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
937                 unsigned        flag = 0;
938                 unsigned        *flagsp = mi->mi_ntargets ?
939                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
940                                 : &mi->mi_flags;
941
942                 if ( argc != 2 ) {
943                         Debug( LDAP_DEBUG_ANY,
944         "%s: line %d: \"cancel {abandon|ignore|exop}\" takes 1 argument\n",
945                                 fname, lineno, 0 );
946                         return( 1 );
947                 }
948
949                 if ( strcasecmp( argv[ 1 ], "abandon" ) == 0 ) {
950                         flag = LDAP_BACK_F_CANCEL_ABANDON;
951
952                 } else if ( strcasecmp( argv[ 1 ], "ignore" ) == 0 ) {
953                         flag = LDAP_BACK_F_CANCEL_IGNORE;
954
955                 } else if ( strcasecmp( argv[ 1 ], "exop" ) == 0 ) {
956                         flag = LDAP_BACK_F_CANCEL_EXOP;
957
958                 } else if ( strcasecmp( argv[ 1 ], "exop-discover" ) == 0 ) {
959                         flag = LDAP_BACK_F_CANCEL_EXOP_DISCOVER;
960
961                 } else {
962                         Debug( LDAP_DEBUG_ANY,
963         "%s: line %d: \"cancel {abandon|ignore|exop[-discover]}\": unknown mode \"%s\" \n",
964                                 fname, lineno, argv[ 1 ] );
965                         return( 1 );
966                 }
967
968                 *flagsp &= ~LDAP_BACK_F_CANCEL_MASK2;
969                 *flagsp |= flag;
970
971         } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
972                 char    *sep;
973                 time_t  *tv = mi->mi_ntargets ?
974                                 mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_timeout
975                                 : mi->mi_timeout;
976                 int     c;
977
978                 if ( argc < 2 ) {
979                         Debug( LDAP_DEBUG_ANY,
980         "%s: line %d: \"timeout [{add|bind|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
981                                 fname, lineno, 0 );
982                         return( 1 );
983                 }
984
985                 for ( c = 1; c < argc; c++ ) {
986                         time_t          *t = NULL;
987                         unsigned long   val;
988
989                         sep = strchr( argv[ c ], '=' );
990                         if ( sep != NULL ) {
991                                 size_t  len = sep - argv[ c ];
992
993                                 if ( strncasecmp( argv[ c ], "bind", len ) == 0 ) {
994                                         t = &tv[ SLAP_OP_BIND ];
995                                 /* unbind makes little sense */
996                                 } else if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
997                                         t = &tv[ SLAP_OP_ADD ];
998                                 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
999                                         t = &tv[ SLAP_OP_DELETE ];
1000                                 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
1001                                         t = &tv[ SLAP_OP_MODRDN ];
1002                                 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
1003                                         t = &tv[ SLAP_OP_MODIFY ];
1004                                 } else if ( strncasecmp( argv[ c ], "compare", len ) == 0 ) {
1005                                         t = &tv[ SLAP_OP_COMPARE ];
1006                                 } else if ( strncasecmp( argv[ c ], "search", len ) == 0 ) {
1007                                         t = &tv[ SLAP_OP_SEARCH ];
1008                                 /* abandon makes little sense */
1009 #if 0                           /* not implemented yet */
1010                                 } else if ( strncasecmp( argv[ c ], "extended", len ) == 0 ) {
1011                                         t = &tv[ SLAP_OP_EXTENDED ];
1012 #endif
1013                                 } else {
1014                                         char    buf[ SLAP_TEXT_BUFLEN ];
1015                                         snprintf( buf, sizeof( buf ),
1016                                                 "unknown/unhandled operation \"%s\" for timeout #%d",
1017                                                 argv[ c ], c - 1 );
1018                                         Debug( LDAP_DEBUG_ANY,
1019                                                 "%s: line %d: %s.\n",
1020                                                 fname, lineno, buf );
1021                                         return 1;
1022                                 }
1023                                 sep++;
1024         
1025                         } else {
1026                                 sep = argv[ c ];
1027                         }
1028         
1029                         if ( lutil_parse_time( sep, &val ) != 0 ) {
1030                                 Debug( LDAP_DEBUG_ANY,
1031                 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
1032                                         fname, lineno, sep );
1033                                 return 1;
1034                         }
1035                 
1036                         if ( t ) {
1037                                 *t = (time_t)val;
1038         
1039                         } else {
1040                                 int     i;
1041         
1042                                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
1043                                         tv[ i ] = (time_t)val;
1044                                 }
1045                         }
1046                 }
1047         
1048         /* name to use as pseudo-root dn */
1049         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
1050                 int             i = mi->mi_ntargets - 1;
1051
1052                 if ( i < 0 ) {
1053                         Debug( LDAP_DEBUG_ANY,
1054         "%s: line %d: need \"uri\" directive first\n",
1055                                 fname, lineno, 0 );
1056                         return 1;
1057                 }
1058                 
1059                 if ( argc != 2 ) {
1060                         Debug( LDAP_DEBUG_ANY,
1061         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
1062                                 fname, lineno, 0 );
1063                         return 1;
1064                 }
1065
1066                 /*
1067                  * exact replacement:
1068                  *
1069
1070 idassert-bind   bindmethod=simple
1071                 binddn=<pseudorootdn>
1072                 credentials=<pseudorootpw>
1073                 mode=none
1074                 flags=non-prescriptive
1075 idassert-authzFrom      "dn:<rootdn>"
1076
1077                  * so that only when authc'd as <rootdn> the proxying occurs
1078                  * rebinding as the <pseudorootdn> without proxyAuthz.
1079                  */
1080
1081                 Debug( LDAP_DEBUG_ANY,
1082                         "%s: line %d: \"pseudorootdn\", \"pseudorootpw\" are no longer supported; "
1083                         "use \"idassert-bind\" and \"idassert-authzFrom\" instead.\n",
1084                         fname, lineno, 0 );
1085
1086                 {
1087                         char    binddn[ SLAP_TEXT_BUFLEN ];
1088                         char    *cargv[] = {
1089                                 "idassert-bind",
1090                                 "bindmethod=simple",
1091                                 NULL,
1092                                 "mode=none",
1093                                 "flags=non-prescriptive",
1094                                 NULL
1095                         };
1096                         int     cargc = 5;
1097                         int     rc;
1098
1099                         if ( BER_BVISNULL( &be->be_rootndn ) ) {
1100                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"pseudorootpw\": \"rootdn\" must be defined first.\n",
1101                                         fname, lineno, 0 );
1102                                 return 1;
1103                         }
1104
1105                         if ( sizeof( binddn ) <= (unsigned) snprintf( binddn,
1106                                         sizeof( binddn ), "binddn=%s", argv[ 1 ] ))
1107                         {
1108                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"pseudorootdn\" too long.\n",
1109                                         fname, lineno, 0 );
1110                                 return 1;
1111                         }
1112                         cargv[ 2 ] = binddn;
1113
1114                         rc = mi->mi_ldap_extra->idassert_parse_cf( fname, lineno, cargc, cargv, &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert );
1115                         if ( rc == 0 ) {
1116                                 struct berval   bv;
1117
1118                                 if ( mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert_authz != NULL ) {
1119                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: \"idassert-authzFrom\" already defined (discarded).\n",
1120                                                 fname, lineno, 0 );
1121                                         ber_bvarray_free( mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert_authz );
1122                                         mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert_authz = NULL;
1123                                 }
1124
1125                                 assert( !BER_BVISNULL( &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert_authcDN ) );
1126
1127                                 bv.bv_len = STRLENOF( "dn:" ) + be->be_rootndn.bv_len;
1128                                 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1129                                 AC_MEMCPY( bv.bv_val, "dn:", STRLENOF( "dn:" ) );
1130                                 AC_MEMCPY( &bv.bv_val[ STRLENOF( "dn:" ) ], be->be_rootndn.bv_val, be->be_rootndn.bv_len + 1 );
1131
1132                                 ber_bvarray_add( &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert_authz, &bv );
1133                         }
1134
1135                         return rc;
1136                 }
1137
1138         /* password to use as pseudo-root */
1139         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
1140                 int             i = mi->mi_ntargets - 1;
1141
1142                 if ( i < 0 ) {
1143                         Debug( LDAP_DEBUG_ANY,
1144         "%s: line %d: need \"uri\" directive first\n",
1145                                 fname, lineno, 0 );
1146                         return 1;
1147                 }
1148                 
1149                 if ( argc != 2 ) {
1150                         Debug( LDAP_DEBUG_ANY,
1151         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
1152                             fname, lineno, 0 );
1153                         return 1;
1154                 }
1155
1156                 Debug( LDAP_DEBUG_ANY,
1157                         "%s: line %d: \"pseudorootdn\", \"pseudorootpw\" are no longer supported; "
1158                         "use \"idassert-bind\" and \"idassert-authzFrom\" instead.\n",
1159                         fname, lineno, 0 );
1160
1161                 if ( BER_BVISNULL( &mi->mi_targets[ i ]->mt_idassert_authcDN ) ) {
1162                         Debug( LDAP_DEBUG_ANY, "%s: line %d: \"pseudorootpw\": \"pseudorootdn\" must be defined first.\n",
1163                                 fname, lineno, 0 );
1164                         return 1;
1165                 }
1166
1167                 if ( !BER_BVISNULL( &mi->mi_targets[ i ]->mt_idassert_passwd ) ) {
1168                         memset( mi->mi_targets[ i ]->mt_idassert_passwd.bv_val, 0,
1169                                 mi->mi_targets[ i ]->mt_idassert_passwd.bv_len );
1170                         ber_memfree( mi->mi_targets[ i ]->mt_idassert_passwd.bv_val );
1171                 }
1172                 ber_str2bv( argv[ 1 ], 0, 1, &mi->mi_targets[ i ]->mt_idassert_passwd );
1173
1174         /* idassert-bind */
1175         } else if ( strcasecmp( argv[ 0 ], "idassert-bind" ) == 0 ) {
1176                 if ( mi->mi_ntargets == 0 ) {
1177                         Debug( LDAP_DEBUG_ANY,
1178                                 "%s: line %d: \"idassert-bind\" "
1179                                 "must appear inside a target specification.\n",
1180                                 fname, lineno, 0 );
1181                         return 1;
1182                 }
1183
1184                 return mi->mi_ldap_extra->idassert_parse_cf( fname, lineno, argc, argv, &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert );
1185
1186         /* idassert-authzFrom */
1187         } else if ( strcasecmp( argv[ 0 ], "idassert-authzFrom" ) == 0 ) {
1188                 if ( mi->mi_ntargets == 0 ) {
1189                         Debug( LDAP_DEBUG_ANY,
1190                                 "%s: line %d: \"idassert-bind\" "
1191                                 "must appear inside a target specification.\n",
1192                                 fname, lineno, 0 );
1193                         return 1;
1194                 }
1195
1196                 switch ( argc ) {
1197                 case 2:
1198                         break;
1199
1200                 case 1:
1201                         Debug( LDAP_DEBUG_ANY,
1202                                 "%s: line %d: missing <id> in \"idassert-authzFrom <id>\".\n",
1203                                 fname, lineno, 0 );
1204                         return 1;
1205
1206                 default:
1207                         Debug( LDAP_DEBUG_ANY,
1208                                 "%s: line %d: extra cruft after <id> in \"idassert-authzFrom <id>\".\n",
1209                                 fname, lineno, 0 );
1210                         return 1;
1211                 }
1212
1213                 return mi->mi_ldap_extra->idassert_authzfrom_parse_cf( fname, lineno, argv[ 1 ], &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert );
1214
1215         /* quarantine */
1216         } else if ( strcasecmp( argv[ 0 ], "quarantine" ) == 0 ) {
1217                 char                    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1218                 slap_retry_info_t       *ri = mi->mi_ntargets ?
1219                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_quarantine
1220                                 : &mi->mi_quarantine;
1221
1222                 if ( ( mi->mi_ntargets == 0 && META_BACK_QUARANTINE( mi ) )
1223                         || ( mi->mi_ntargets > 0 && META_BACK_TGT_QUARANTINE( mi->mi_targets[ mi->mi_ntargets - 1 ] ) ) )
1224                 {
1225                         Debug( LDAP_DEBUG_ANY,
1226                                 "%s: line %d: quarantine already defined.\n",
1227                                 fname, lineno, 0 );
1228                         return 1;
1229                 }
1230
1231                 switch ( argc ) {
1232                 case 2:
1233                         break;
1234
1235                 case 1:
1236                         Debug( LDAP_DEBUG_ANY,
1237                                 "%s: line %d: missing arg in \"quarantine <pattern list>\".\n",
1238                                 fname, lineno, 0 );
1239                         return 1;
1240
1241                 default:
1242                         Debug( LDAP_DEBUG_ANY,
1243                                 "%s: line %d: extra cruft after \"quarantine <pattern list>\".\n",
1244                                 fname, lineno, 0 );
1245                         return 1;
1246                 }
1247
1248                 if ( ri != &mi->mi_quarantine ) {
1249                         ri->ri_interval = NULL;
1250                         ri->ri_num = NULL;
1251                 }
1252
1253                 if ( mi->mi_ntargets > 0 && !META_BACK_QUARANTINE( mi ) ) {
1254                         ldap_pvt_thread_mutex_init( &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_quarantine_mutex );
1255                 }
1256
1257                 if ( mi->mi_ldap_extra->retry_info_parse( argv[ 1 ], ri, buf, sizeof( buf ) ) ) {
1258                         Debug( LDAP_DEBUG_ANY,
1259                                 "%s line %d: %s.\n",
1260                                 fname, lineno, buf );
1261                         return 1;
1262                 }
1263
1264                 if ( mi->mi_ntargets == 0 ) {
1265                         mi->mi_flags |= LDAP_BACK_F_QUARANTINE;
1266
1267                 } else {
1268                         mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags |= LDAP_BACK_F_QUARANTINE;
1269                 }
1270
1271 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1272         /* session tracking request */
1273         } else if ( strcasecmp( argv[ 0 ], "session-tracking-request" ) == 0 ) {
1274                 unsigned        *flagsp = mi->mi_ntargets ?
1275                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
1276                                 : &mi->mi_flags;
1277
1278                 if ( argc != 2 ) {
1279                         Debug( LDAP_DEBUG_ANY,
1280         "%s: line %d: \"session-tracking-request {TRUE|false}\" needs 1 argument.\n",
1281                                 fname, lineno, 0 );
1282                         return( 1 );
1283                 }
1284
1285                 /* this is the default; we add it because the default might change... */
1286                 switch ( check_true_false( argv[ 1 ] ) ) {
1287                 case 1:
1288                         *flagsp |= LDAP_BACK_F_ST_REQUEST;
1289                         break;
1290
1291                 case 0:
1292                         *flagsp &= ~LDAP_BACK_F_ST_REQUEST;
1293                         break;
1294
1295                 default:
1296                         Debug( LDAP_DEBUG_ANY,
1297                 "%s: line %d: \"session-tracking-request {TRUE|false}\": unknown argument \"%s\".\n",
1298                                 fname, lineno, argv[ 1 ] );
1299                         return( 1 );
1300                 }
1301 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
1302         
1303         /* dn massaging */
1304         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
1305                 BackendDB       *tmp_bd;
1306                 int             i = mi->mi_ntargets - 1, c, rc;
1307                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
1308
1309                 if ( i < 0 ) {
1310                         Debug( LDAP_DEBUG_ANY,
1311         "%s: line %d: need \"uri\" directive first\n",
1312                                 fname, lineno, 0 );
1313                         return 1;
1314                 }
1315                 
1316                 /*
1317                  * syntax:
1318                  * 
1319                  *      suffixmassage <suffix> <massaged suffix>
1320                  *
1321                  * the <suffix> field must be defined as a valid suffix
1322                  * (or suffixAlias?) for the current database;
1323                  * the <massaged suffix> shouldn't have already been
1324                  * defined as a valid suffix or suffixAlias for the 
1325                  * current server
1326                  */
1327                 if ( argc != 3 ) {
1328                         Debug( LDAP_DEBUG_ANY,
1329         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
1330                                 fname, lineno, 0 );
1331                         return 1;
1332                 }
1333
1334                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
1335                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1336                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1337                                         "suffix \"%s\" is invalid\n",
1338                                         fname, lineno, argv[ 1 ] );
1339                         return 1;
1340                 }
1341
1342                 for ( c = 0; !BER_BVISNULL( &be->be_nsuffix[ c ] ); c++ ) {
1343                         if ( dnIsSuffix( &nvnc, &be->be_nsuffix[ 0 ] ) ) {
1344                                 break;
1345                         }
1346                 }
1347
1348                 if ( BER_BVISNULL( &be->be_nsuffix[ c ] ) ) {
1349                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1350         "<suffix> \"%s\" must be within the database naming context, in "
1351         "\"suffixMassage <suffix> <massaged suffix>\"\n",
1352                                 fname, lineno, pvnc.bv_val );
1353                         free( pvnc.bv_val );
1354                         free( nvnc.bv_val );
1355                         return 1;                                               
1356                 }
1357
1358                 ber_str2bv( argv[ 2 ], 0, 0, &dn );
1359                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1360                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1361                                 "massaged suffix \"%s\" is invalid\n",
1362                                 fname, lineno, argv[ 2 ] );
1363                         free( pvnc.bv_val );
1364                         free( nvnc.bv_val );
1365                         return 1;
1366                 }
1367         
1368                 tmp_bd = select_backend( &nrnc, 0 );
1369                 if ( tmp_bd != NULL && tmp_bd->be_private == be->be_private ) {
1370                         Debug( LDAP_DEBUG_ANY, 
1371         "%s: line %d: warning: <massaged suffix> \"%s\" resolves to this database, in "
1372         "\"suffixMassage <suffix> <massaged suffix>\"\n",
1373                                 fname, lineno, prnc.bv_val );
1374                 }
1375
1376                 /*
1377                  * The suffix massaging is emulated by means of the
1378                  * rewrite capabilities
1379                  */
1380                 rc = suffix_massage_config( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1381                                 &pvnc, &nvnc, &prnc, &nrnc );
1382
1383                 free( pvnc.bv_val );
1384                 free( nvnc.bv_val );
1385                 free( prnc.bv_val );
1386                 free( nrnc.bv_val );
1387
1388                 return rc;
1389                 
1390         /* rewrite stuff ... */
1391         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
1392                 int             i = mi->mi_ntargets - 1;
1393
1394                 if ( i < 0 ) {
1395                         Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
1396                                 "statement outside target definition.\n",
1397                                 fname, lineno, 0 );
1398                         return 1;
1399                 }
1400                 
1401                 return rewrite_parse( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1402                                 fname, lineno, argc, argv );
1403
1404         /* objectclass/attribute mapping */
1405         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
1406                 int             i = mi->mi_ntargets - 1;
1407
1408                 if ( i < 0 ) {
1409                         Debug( LDAP_DEBUG_ANY,
1410         "%s: line %d: need \"uri\" directive first\n",
1411                                 fname, lineno, 0 );
1412                         return 1;
1413                 }
1414
1415                 return ldap_back_map_config( &mi->mi_targets[ i ]->mt_rwmap.rwm_oc, 
1416                                 &mi->mi_targets[ i ]->mt_rwmap.rwm_at,
1417                                 fname, lineno, argc, argv );
1418
1419         } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
1420                 int             i = mi->mi_ntargets - 1;
1421                 int             nretries = META_RETRY_UNDEFINED;
1422
1423                 if ( argc != 2 ) {
1424                         Debug( LDAP_DEBUG_ANY,
1425         "%s: line %d: need value in \"nretries <value>\"\n",
1426                                 fname, lineno, 0 );
1427                         return 1;
1428                 }
1429
1430                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
1431                         nretries = META_RETRY_FOREVER;
1432
1433                 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
1434                         nretries = META_RETRY_NEVER;
1435
1436                 } else {
1437                         if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
1438                                 Debug( LDAP_DEBUG_ANY,
1439         "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
1440                                         fname, lineno, argv[ 1 ] );
1441                                 return 1;
1442                         }
1443                 }
1444
1445                 if ( i < 0 ) {
1446                         mi->mi_nretries = nretries;
1447
1448                 } else {
1449                         mi->mi_targets[ i ]->mt_nretries = nretries;
1450                 }
1451
1452         } else if ( strcasecmp( argv[ 0 ], "protocol-version" ) == 0 ) {
1453                 int     *version = mi->mi_ntargets ?
1454                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_version
1455                                 : &mi->mi_version;
1456
1457                 if ( argc != 2 ) {
1458                         Debug( LDAP_DEBUG_ANY,
1459         "%s: line %d: need value in \"protocol-version <version>\"\n",
1460                                 fname, lineno, 0 );
1461                         return 1;
1462                 }
1463
1464                 if ( lutil_atoi( version, argv[ 1 ] ) != 0 ) {
1465                         Debug( LDAP_DEBUG_ANY,
1466         "%s: line %d: unable to parse version \"%s\" in \"protocol-version <version>\"\n",
1467                                 fname, lineno, argv[ 1 ] );
1468                         return 1;
1469                 }
1470
1471                 if ( *version != 0 && ( *version < LDAP_VERSION_MIN || *version > LDAP_VERSION_MAX ) ) {
1472                         Debug( LDAP_DEBUG_ANY,
1473         "%s: line %d: unsupported version \"%s\" in \"protocol-version <version>\"\n",
1474                                 fname, lineno, argv[ 1 ] );
1475                         return 1;
1476                 }
1477
1478         /* do not return search references */
1479         } else if ( strcasecmp( argv[ 0 ], "norefs" ) == 0 ) {
1480                 unsigned        *flagsp = mi->mi_ntargets ?
1481                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
1482                                 : &mi->mi_flags;
1483
1484                 if ( argc != 2 ) {
1485                         Debug( LDAP_DEBUG_ANY,
1486         "%s: line %d: \"norefs {TRUE|false}\" needs 1 argument.\n",
1487                                 fname, lineno, 0 );
1488                         return( 1 );
1489                 }
1490
1491                 /* this is the default; we add it because the default might change... */
1492                 switch ( check_true_false( argv[ 1 ] ) ) {
1493                 case 1:
1494                         *flagsp |= LDAP_BACK_F_NOREFS;
1495                         break;
1496
1497                 case 0:
1498                         *flagsp &= ~LDAP_BACK_F_NOREFS;
1499                         break;
1500
1501                 default:
1502                         Debug( LDAP_DEBUG_ANY,
1503                 "%s: line %d: \"norefs {TRUE|false}\": unknown argument \"%s\".\n",
1504                                 fname, lineno, argv[ 1 ] );
1505                         return( 1 );
1506                 }
1507
1508         /* do not propagate undefined search filters */
1509         } else if ( strcasecmp( argv[ 0 ], "noundeffilter" ) == 0 ) {
1510                 unsigned        *flagsp = mi->mi_ntargets ?
1511                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
1512                                 : &mi->mi_flags;
1513
1514                 if ( argc != 2 ) {
1515                         Debug( LDAP_DEBUG_ANY,
1516         "%s: line %d: \"noundeffilter {TRUE|false}\" needs 1 argument.\n",
1517                                 fname, lineno, 0 );
1518                         return( 1 );
1519                 }
1520
1521                 /* this is the default; we add it because the default might change... */
1522                 switch ( check_true_false( argv[ 1 ] ) ) {
1523                 case 1:
1524                         *flagsp |= LDAP_BACK_F_NOUNDEFFILTER;
1525                         break;
1526
1527                 case 0:
1528                         *flagsp &= ~LDAP_BACK_F_NOUNDEFFILTER;
1529                         break;
1530
1531                 default:
1532                         Debug( LDAP_DEBUG_ANY,
1533                 "%s: line %d: \"noundeffilter {TRUE|false}\": unknown argument \"%s\".\n",
1534                                 fname, lineno, argv[ 1 ] );
1535                         return( 1 );
1536                 }
1537
1538 #ifdef SLAPD_META_CLIENT_PR
1539         } else if ( strcasecmp( argv[ 0 ], "client-pr" ) == 0 ) {
1540                 int *ps = mi->mi_ntargets ?
1541                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_ps
1542                                 : &mi->mi_ps;
1543
1544                 if ( argc != 2 ) {
1545                         Debug( LDAP_DEBUG_ANY,
1546         "%s: line %d: \"client-pr {accept-unsolicited|disable|<size>}\" needs 1 argument.\n",
1547                                 fname, lineno, 0 );
1548                         return( 1 );
1549                 }
1550
1551                 if ( strcasecmp( argv[ 1 ], "accept-unsolicited" ) == 0 ) {
1552                         *ps = META_CLIENT_PR_ACCEPT_UNSOLICITED;
1553
1554                 } else if ( strcasecmp( argv[ 1 ], "disable" ) == 0 ) {
1555                         *ps = META_CLIENT_PR_DISABLE;
1556
1557                 } else if ( lutil_atoi( ps, argv[ 1 ] ) || *ps < -1 ) {
1558                         Debug( LDAP_DEBUG_ANY,
1559         "%s: line %d: \"client-pr {accept-unsolicited|disable|<size>}\" invalid arg \"%s\".\n",
1560                                 fname, lineno, argv[ 1 ] );
1561                         return( 1 );
1562                 }
1563 #endif /* SLAPD_META_CLIENT_PR */
1564
1565         /* anything else */
1566         } else {
1567                 return SLAP_CONF_UNKNOWN;
1568         }
1569         return 0;
1570 }
1571
1572 int
1573 ldap_back_map_config(
1574                 struct ldapmap  *oc_map,
1575                 struct ldapmap  *at_map,
1576                 const char      *fname,
1577                 int             lineno,
1578                 int             argc,
1579                 char            **argv )
1580 {
1581         struct ldapmap          *map;
1582         struct ldapmapping      *mapping;
1583         char                    *src, *dst;
1584         int                     is_oc = 0;
1585
1586         if ( argc < 3 || argc > 4 ) {
1587                 Debug( LDAP_DEBUG_ANY,
1588         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
1589                         fname, lineno, 0 );
1590                 return 1;
1591         }
1592
1593         if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
1594                 map = oc_map;
1595                 is_oc = 1;
1596
1597         } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
1598                 map = at_map;
1599
1600         } else {
1601                 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
1602                         "\"map {objectclass | attribute} [<local> | *] "
1603                         "{<foreign> | *}\"\n",
1604                         fname, lineno, 0 );
1605                 return 1;
1606         }
1607
1608         if ( !is_oc && map->map == NULL ) {
1609                 /* only init if required */
1610                 ldap_back_map_init( map, &mapping );
1611         }
1612
1613         if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
1614                 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
1615                         map->drop_missing = ( argc < 4 );
1616                         goto success_return;
1617                 }
1618                 src = dst = argv[ 3 ];
1619
1620         } else if ( argc < 4 ) {
1621                 src = "";
1622                 dst = argv[ 2 ];
1623
1624         } else {
1625                 src = argv[ 2 ];
1626                 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1627         }
1628
1629         if ( ( map == at_map )
1630                 && ( strcasecmp( src, "objectclass" ) == 0
1631                         || strcasecmp( dst, "objectclass" ) == 0 ) )
1632         {
1633                 Debug( LDAP_DEBUG_ANY,
1634                         "%s: line %d: objectclass attribute cannot be mapped\n",
1635                         fname, lineno, 0 );
1636         }
1637
1638         mapping = (struct ldapmapping *)ch_calloc( 2,
1639                 sizeof(struct ldapmapping) );
1640         if ( mapping == NULL ) {
1641                 Debug( LDAP_DEBUG_ANY,
1642                         "%s: line %d: out of memory\n",
1643                         fname, lineno, 0 );
1644                 return 1;
1645         }
1646         ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1647         ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1648         mapping[ 1 ].src = mapping[ 0 ].dst;
1649         mapping[ 1 ].dst = mapping[ 0 ].src;
1650
1651         /*
1652          * schema check
1653          */
1654         if ( is_oc ) {
1655                 if ( src[ 0 ] != '\0' ) {
1656                         if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1657                                 Debug( LDAP_DEBUG_ANY,
1658         "%s: line %d: warning, source objectClass '%s' "
1659         "should be defined in schema\n",
1660                                         fname, lineno, src );
1661
1662                                 /*
1663                                  * FIXME: this should become an err
1664                                  */
1665                                 goto error_return;
1666                         }
1667                 }
1668
1669                 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1670                         Debug( LDAP_DEBUG_ANY,
1671         "%s: line %d: warning, destination objectClass '%s' "
1672         "is not defined in schema\n",
1673                                 fname, lineno, dst );
1674                 }
1675         } else {
1676                 int                     rc;
1677                 const char              *text = NULL;
1678                 AttributeDescription    *ad = NULL;
1679
1680                 if ( src[ 0 ] != '\0' ) {
1681                         rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1682                         if ( rc != LDAP_SUCCESS ) {
1683                                 Debug( LDAP_DEBUG_ANY,
1684         "%s: line %d: warning, source attributeType '%s' "
1685         "should be defined in schema\n",
1686                                         fname, lineno, src );
1687
1688                                 /*
1689                                  * FIXME: this should become an err
1690                                  */
1691                                 /*
1692                                  * we create a fake "proxied" ad 
1693                                  * and add it here.
1694                                  */
1695
1696                                 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1697                                                 &ad, &text, SLAP_AD_PROXIED );
1698                                 if ( rc != LDAP_SUCCESS ) {
1699                                         char    buf[ SLAP_TEXT_BUFLEN ];
1700
1701                                         snprintf( buf, sizeof( buf ),
1702                                                 "source attributeType \"%s\": %d (%s)",
1703                                                 src, rc, text ? text : "" );
1704                                         Debug( LDAP_DEBUG_ANY,
1705                                                 "%s: line %d: %s\n",
1706                                                 fname, lineno, buf );
1707                                         goto error_return;
1708                                 }
1709                         }
1710
1711                         ad = NULL;
1712                 }
1713
1714                 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1715                 if ( rc != LDAP_SUCCESS ) {
1716                         Debug( LDAP_DEBUG_ANY,
1717         "%s: line %d: warning, destination attributeType '%s' "
1718         "is not defined in schema\n",
1719                                 fname, lineno, dst );
1720
1721                         /*
1722                          * we create a fake "proxied" ad 
1723                          * and add it here.
1724                          */
1725
1726                         rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1727                                         &ad, &text, SLAP_AD_PROXIED );
1728                         if ( rc != LDAP_SUCCESS ) {
1729                                 char    buf[ SLAP_TEXT_BUFLEN ];
1730
1731                                 snprintf( buf, sizeof( buf ),
1732                                         "source attributeType \"%s\": %d (%s)\n",
1733                                         dst, rc, text ? text : "" );
1734                                 Debug( LDAP_DEBUG_ANY,
1735                                         "%s: line %d: %s\n",
1736                                         fname, lineno, buf );
1737                                 return 1;
1738                         }
1739                 }
1740         }
1741
1742         if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1743                         || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1744         {
1745                 Debug( LDAP_DEBUG_ANY,
1746                         "%s: line %d: duplicate mapping found.\n",
1747                         fname, lineno, 0 );
1748                 goto error_return;
1749         }
1750
1751         if ( src[ 0 ] != '\0' ) {
1752                 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1753                                         mapping_cmp, mapping_dup );
1754         }
1755         avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1756                                 mapping_cmp, mapping_dup );
1757
1758 success_return:;
1759         return 0;
1760
1761 error_return:;
1762         if ( mapping ) {
1763                 ch_free( mapping[ 0 ].src.bv_val );
1764                 ch_free( mapping[ 0 ].dst.bv_val );
1765                 ch_free( mapping );
1766         }
1767
1768         return 1;
1769 }
1770
1771
1772 #ifdef ENABLE_REWRITE
1773 static char *
1774 suffix_massage_regexize( const char *s )
1775 {
1776         char *res, *ptr;
1777         const char *p, *r;
1778         int i;
1779
1780         if ( s[ 0 ] == '\0' ) {
1781                 return ch_strdup( "^(.+)$" );
1782         }
1783
1784         for ( i = 0, p = s; 
1785                         ( r = strchr( p, ',' ) ) != NULL; 
1786                         p = r + 1, i++ )
1787                 ;
1788
1789         res = ch_calloc( sizeof( char ),
1790                         strlen( s )
1791                         + STRLENOF( "((.+),)?" )
1792                         + STRLENOF( "[ ]?" ) * i
1793                         + STRLENOF( "$" ) + 1 );
1794
1795         ptr = lutil_strcopy( res, "((.+),)?" );
1796         for ( i = 0, p = s;
1797                         ( r = strchr( p, ',' ) ) != NULL;
1798                         p = r + 1 , i++ ) {
1799                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1800                 ptr = lutil_strcopy( ptr, "[ ]?" );
1801
1802                 if ( r[ 1 ] == ' ' ) {
1803                         r++;
1804                 }
1805         }
1806         ptr = lutil_strcopy( ptr, p );
1807         ptr[ 0 ] = '$';
1808         ptr++;
1809         ptr[ 0 ] = '\0';
1810
1811         return res;
1812 }
1813
1814 static char *
1815 suffix_massage_patternize( const char *s, const char *p )
1816 {
1817         ber_len_t       len;
1818         char            *res, *ptr;
1819
1820         len = strlen( p );
1821
1822         if ( s[ 0 ] == '\0' ) {
1823                 len++;
1824         }
1825
1826         res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1827         if ( res == NULL ) {
1828                 return NULL;
1829         }
1830
1831         ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1832         if ( s[ 0 ] == '\0' ) {
1833                 ptr[ 0 ] = ',';
1834                 ptr++;
1835         }
1836         lutil_strcopy( ptr, p );
1837
1838         return res;
1839 }
1840
1841 int
1842 suffix_massage_config( 
1843                 struct rewrite_info *info,
1844                 struct berval *pvnc,
1845                 struct berval *nvnc,
1846                 struct berval *prnc,
1847                 struct berval *nrnc
1848 )
1849 {
1850         char *rargv[ 5 ];
1851         int line = 0;
1852
1853         rargv[ 0 ] = "rewriteEngine";
1854         rargv[ 1 ] = "on";
1855         rargv[ 2 ] = NULL;
1856         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1857
1858         rargv[ 0 ] = "rewriteContext";
1859         rargv[ 1 ] = "default";
1860         rargv[ 2 ] = NULL;
1861         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1862
1863         rargv[ 0 ] = "rewriteRule";
1864         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1865         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1866         rargv[ 3 ] = ":";
1867         rargv[ 4 ] = NULL;
1868         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1869         ch_free( rargv[ 1 ] );
1870         ch_free( rargv[ 2 ] );
1871
1872         if ( BER_BVISEMPTY( pvnc ) ) {
1873                 rargv[ 0 ] = "rewriteRule";
1874                 rargv[ 1 ] = "^$";
1875                 rargv[ 2 ] = prnc->bv_val;
1876                 rargv[ 3 ] = ":";
1877                 rargv[ 4 ] = NULL;
1878                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1879         }
1880         
1881         rargv[ 0 ] = "rewriteContext";
1882         rargv[ 1 ] = "searchEntryDN";
1883         rargv[ 2 ] = NULL;
1884         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1885
1886         rargv[ 0 ] = "rewriteRule";
1887         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1888         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1889         rargv[ 3 ] = ":";
1890         rargv[ 4 ] = NULL;
1891         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1892         ch_free( rargv[ 1 ] );
1893         ch_free( rargv[ 2 ] );
1894
1895         if ( BER_BVISEMPTY( prnc ) ) {
1896                 rargv[ 0 ] = "rewriteRule";
1897                 rargv[ 1 ] = "^$";
1898                 rargv[ 2 ] = pvnc->bv_val;
1899                 rargv[ 3 ] = ":";
1900                 rargv[ 4 ] = NULL;
1901                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1902         }
1903         
1904         /* backward compatibility */
1905         rargv[ 0 ] = "rewriteContext";
1906         rargv[ 1 ] = "searchResult";
1907         rargv[ 2 ] = "alias";
1908         rargv[ 3 ] = "searchEntryDN";
1909         rargv[ 4 ] = NULL;
1910         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1911         
1912         rargv[ 0 ] = "rewriteContext";
1913         rargv[ 1 ] = "matchedDN";
1914         rargv[ 2 ] = "alias";
1915         rargv[ 3 ] = "searchEntryDN";
1916         rargv[ 4 ] = NULL;
1917         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1918
1919         rargv[ 0 ] = "rewriteContext";
1920         rargv[ 1 ] = "searchAttrDN";
1921         rargv[ 2 ] = "alias";
1922         rargv[ 3 ] = "searchEntryDN";
1923         rargv[ 4 ] = NULL;
1924         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1925
1926         /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1927          * see servers/slapd/overlays/rwm.h for details */
1928         rargv[ 0 ] = "rewriteContext";
1929         rargv[ 1 ] = "referralAttrDN";
1930         rargv[ 2 ] = NULL;
1931         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1932
1933         rargv[ 0 ] = "rewriteContext";
1934         rargv[ 1 ] = "referralDN";
1935         rargv[ 2 ] = NULL;
1936         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1937         
1938         return 0;
1939 }
1940 #endif /* ENABLE_REWRITE */
1941