]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
more about previous commit
[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-2006 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         struct ldapmapping      *mapping;
42         char                    *rargv[ 3 ];
43         metatarget_t            *mt;
44
45         *mtp = NULL;
46
47         mt = ch_calloc( sizeof( metatarget_t ), 1 );
48
49         mt->mt_rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
50         if ( mt->mt_rwmap.rwm_rw == NULL ) {
51                 ch_free( mt );
52                 return -1;
53         }
54
55
56         /*
57          * the filter rewrite as a string must be disabled
58          * by default; it can be re-enabled by adding rules;
59          * this creates an empty rewriteContext
60          */
61         rargv[ 0 ] = "rewriteContext";
62         rargv[ 1 ] = "searchFilter";
63         rargv[ 2 ] = NULL;
64         rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
65
66         rargv[ 0 ] = "rewriteContext";
67         rargv[ 1 ] = "default";
68         rargv[ 2 ] = NULL;
69         rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
70
71         ldap_back_map_init( &mt->mt_rwmap.rwm_at, &mapping );
72
73         ldap_pvt_thread_mutex_init( &mt->mt_uri_mutex );
74
75         *mtp = mt;
76
77         return 0;
78 }
79
80 static int
81 check_true_false( char *str )
82 {
83         if ( strcasecmp( str, "true" ) == 0 || strcasecmp( str, "yes" ) == 0 ) {
84                 return 1;
85         }
86
87         if ( strcasecmp( str, "false" ) == 0 || strcasecmp( str, "no" ) == 0 ) {
88                 return 0;
89         }
90
91         return -1;
92 }
93
94
95 int
96 meta_back_db_config(
97                 BackendDB       *be,
98                 const char      *fname,
99                 int             lineno,
100                 int             argc,
101                 char            **argv
102 )
103 {
104         metainfo_t      *mi = ( metainfo_t * )be->be_private;
105
106         assert( mi != NULL );
107
108         /* URI of server to query */
109         if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
110                 int             i = mi->mi_ntargets;
111 #if 0
112                 int             j;
113 #endif /* uncomment if uri MUST be a branch of suffix */
114                 LDAPURLDesc     *ludp, *tmpludp;
115                 struct berval   dn;
116                 int             rc;
117                 int             c;
118
119                 metatarget_t    *mt;
120                 
121                 switch ( argc ) {
122                 case 1:
123                         Debug( LDAP_DEBUG_ANY,
124         "%s: line %d: missing URI "
125         "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
126                                 fname, lineno, 0 );
127                         return 1;
128
129                 case 2:
130                         break;
131
132                 default:
133                         Debug( LDAP_DEBUG_ANY,
134         "%s: line %d: too many args "
135         "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
136                                 fname, lineno, 0 );
137                         return 1;
138                 }
139
140                 if ( be->be_nsuffix == NULL ) {
141                         Debug( LDAP_DEBUG_ANY,
142         "%s: line %d: the suffix must be defined before any target.\n",
143                                 fname, lineno, 0 );
144                         return 1;
145                 }
146                 
147                 ++mi->mi_ntargets;
148
149                 mi->mi_targets = ( metatarget_t ** )ch_realloc( mi->mi_targets, 
150                         sizeof( metatarget_t * ) * mi->mi_ntargets );
151                 if ( mi->mi_targets == NULL ) {
152                         Debug( LDAP_DEBUG_ANY,
153         "%s: line %d: out of memory while storing server name"
154         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
155                                 fname, lineno, 0 );
156                         return 1;
157                 }
158
159                 if ( meta_back_new_target( &mi->mi_targets[ i ] ) != 0 ) {
160                         Debug( LDAP_DEBUG_ANY,
161         "%s: line %d: unable to init server"
162         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
163                                 fname, lineno, 0 );
164                         return 1;
165                 }
166
167                 mt = mi->mi_targets[ i ];
168
169                 mt->mt_rebind_f = mi->mi_rebind_f;
170                 mt->mt_urllist_f = mi->mi_urllist_f;
171                 mt->mt_urllist_p = mt;
172
173                 mt->mt_nretries = mi->mi_nretries;
174                 mt->mt_quarantine = mi->mi_quarantine;
175                 if ( META_BACK_QUARANTINE( mi ) ) {
176                         ldap_pvt_thread_mutex_init( &mt->mt_quarantine_mutex );
177                 }
178                 mt->mt_flags = mi->mi_flags;
179                 mt->mt_version = mi->mi_version;
180                 mt->mt_network_timeout = mi->mi_network_timeout;
181                 mt->mt_bind_timeout = mi->mi_bind_timeout;
182                 for ( c = 0; c < LDAP_BACK_OP_LAST; c++ ) {
183                         mt->mt_timeout[ c ] = mi->mi_timeout[ c ];
184                 }
185
186                 /*
187                  * uri MUST be legal!
188                  */
189                 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t",
190                         LDAP_PVT_URL_PARSE_NONE ) != LDAP_SUCCESS )
191                 {
192                         Debug( LDAP_DEBUG_ANY,
193         "%s: line %d: unable to parse URI"
194         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
195                                 fname, lineno, 0 );
196                         return 1;
197                 }
198
199                 /*
200                  * uri MUST have the <dn> part!
201                  */
202                 if ( ludp->lud_dn == NULL ) {
203                         Debug( LDAP_DEBUG_ANY,
204         "%s: line %d: missing <naming context> "
205         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
206                                 fname, lineno, 0 );
207                         return 1;
208
209                 } else if ( ludp->lud_dn[ 0 ] == '\0' ) {
210                         int     j = -1;
211
212                         for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[ j ] ); j++ ) {
213                                 if ( BER_BVISEMPTY( &be->be_nsuffix[ j ] ) ) {
214                                         break;
215                                 }
216                         }
217
218                         if ( BER_BVISNULL( &be->be_nsuffix[ j ] ) ) {
219                                 Debug( LDAP_DEBUG_ANY,
220                 "%s: line %d: missing <naming context> "
221                 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
222                                         fname, lineno, 0 );
223                                 return 1;
224                         }
225                 }
226
227                 /*
228                  * copies and stores uri and suffix
229                  */
230                 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
231                 rc = dnPrettyNormal( NULL, &dn, &mt->mt_psuffix,
232                         &mt->mt_nsuffix, NULL );
233                 if( rc != LDAP_SUCCESS ) {
234                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
235                                 "target \"%s\" DN is invalid\n",
236                                 fname, lineno, argv[ 1 ] );
237                         return( 1 );
238                 }
239
240                 ludp->lud_dn[ 0 ] = '\0';
241
242                 switch ( ludp->lud_scope ) {
243                 case LDAP_SCOPE_DEFAULT:
244                         mt->mt_scope = LDAP_SCOPE_SUBTREE;
245                         break;
246
247                 case LDAP_SCOPE_SUBTREE:
248                 case LDAP_SCOPE_SUBORDINATE:
249                         mt->mt_scope = ludp->lud_scope;
250                         break;
251
252                 default:
253                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
254                                 "invalid scope for target \"%s\"\n",
255                                 fname, lineno, argv[ 1 ] );
256                         return( 1 );
257                 }
258
259                 /* check all, to apply the scope check on the first one */
260                 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
261                         if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
262                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
263                                         "multiple URIs must have "
264                                         "no DN part\n",
265                                         fname, lineno, 0 );
266                                 return( 1 );
267
268                         }
269                 }
270
271                 mt->mt_uri = ldap_url_list2urls( ludp );
272                 ldap_free_urllist( ludp );
273                 if ( mt->mt_uri == NULL) {
274                         Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
275                                 fname, lineno, 0 );
276                         return( 1 );
277                 }
278                 
279                 /*
280                  * uri MUST be a branch of suffix!
281                  */
282 #if 0 /* too strict a constraint */
283                 if ( select_backend( &mt->mt_nsuffix, 0, 0 ) != be ) {
284                         Debug( LDAP_DEBUG_ANY,
285         "%s: line %d: <naming context> of URI does not refer to current backend"
286         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
287                                 fname, lineno, 0 );
288                         return 1;
289                 }
290 #else
291                 /*
292                  * uri MUST be a branch of a suffix!
293                  */
294                 if ( select_backend( &mt->mt_nsuffix, 0, 0 ) == NULL ) {
295                         Debug( LDAP_DEBUG_ANY,
296         "%s: line %d: <naming context> of URI does not resolve to a backend"
297         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
298                                 fname, lineno, 0 );
299                         return 1;
300                 }
301 #endif
302
303         /* subtree-exclude */
304         } else if ( strcasecmp( argv[ 0 ], "subtree-exclude" ) == 0 ) {
305                 int             i = mi->mi_ntargets - 1;
306                 struct berval   dn, ndn;
307
308                 if ( i < 0 ) {
309                         Debug( LDAP_DEBUG_ANY,
310         "%s: line %d: need \"uri\" directive first\n",
311                                 fname, lineno, 0 );
312                         return 1;
313                 }
314                 
315                 switch ( argc ) {
316                 case 1:
317                         Debug( LDAP_DEBUG_ANY,
318         "%s: line %d: missing DN in \"subtree-exclude <DN>\" line\n",
319                             fname, lineno, 0 );
320                         return 1;
321
322                 case 2:
323                         break;
324
325                 default:
326                         Debug( LDAP_DEBUG_ANY,
327         "%s: line %d: too many args in \"subtree-exclude <DN>\" line\n",
328                             fname, lineno, 0 );
329                         return 1;
330                 }
331
332                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
333                 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL )
334                         != LDAP_SUCCESS )
335                 {
336                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
337                                         "subtree-exclude DN=\"%s\" is invalid\n",
338                                         fname, lineno, argv[ 1 ] );
339                         return( 1 );
340                 }
341
342                 if ( !dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_nsuffix ) ) {
343                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
344                                         "subtree-exclude DN=\"%s\" "
345                                         "must be subtree of target\n",
346                                         fname, lineno, argv[ 1 ] );
347                         ber_memfree( ndn.bv_val );
348                         return( 1 );
349                 }
350
351                 if ( mi->mi_targets[ i ]->mt_subtree_exclude != NULL ) {
352                         int             j;
353
354                         for ( j = 0; !BER_BVISNULL( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ); j++ )
355                         {
356                                 if ( dnIsSuffix( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ], &ndn ) ) {
357                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
358                                                         "subtree-exclude DN=\"%s\" "
359                                                         "is suffix of another subtree-exclude\n",
360                                                         fname, lineno, argv[ 1 ] );
361                                         /* reject, because it might be superior
362                                          * to more than one subtree-exclude */
363                                         ber_memfree( ndn.bv_val );
364                                         return( 1 );
365
366                                 } else if ( dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ) ) {
367                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
368                                                         "another subtree-exclude is suffix of "
369                                                         "subtree-exclude DN=\"%s\"\n",
370                                                         fname, lineno, argv[ 1 ] );
371                                         ber_memfree( ndn.bv_val );
372                                         return( 0 );
373                                 }
374                         }
375                 }
376
377                 ber_bvarray_add( &mi->mi_targets[ i ]->mt_subtree_exclude, &ndn );
378
379         /* default target directive */
380         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
381                 int             i = mi->mi_ntargets - 1;
382                 
383                 if ( argc == 1 ) {
384                         if ( i < 0 ) {
385                                 Debug( LDAP_DEBUG_ANY,
386         "%s: line %d: \"default-target\" alone need be"
387         " inside a \"uri\" directive\n",
388                                         fname, lineno, 0 );
389                                 return 1;
390                         }
391                         mi->mi_defaulttarget = i;
392
393                 } else {
394                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
395                                 if ( i >= 0 ) {
396                                         Debug( LDAP_DEBUG_ANY,
397         "%s: line %d: \"default-target none\""
398         " should go before uri definitions\n",
399                                                 fname, lineno, 0 );
400                                 }
401                                 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
402
403                         } else {
404                                 
405                                 if ( lutil_atoi( &mi->mi_defaulttarget, argv[ 1 ] ) != 0
406                                         || mi->mi_defaulttarget < 0
407                                         || mi->mi_defaulttarget >= i - 1 )
408                                 {
409                                         Debug( LDAP_DEBUG_ANY,
410         "%s: line %d: illegal target number %d\n",
411                                                 fname, lineno, mi->mi_defaulttarget );
412                                         return 1;
413                                 }
414                         }
415                 }
416                 
417         /* ttl of dn cache */
418         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
419                 if ( argc != 2 ) {
420                         Debug( LDAP_DEBUG_ANY,
421         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
422                                 fname, lineno, 0 );
423                         return 1;
424                 }
425                 
426                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
427                         mi->mi_cache.ttl = META_DNCACHE_FOREVER;
428
429                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
430                         mi->mi_cache.ttl = META_DNCACHE_DISABLED;
431
432                 } else {
433                         unsigned long   t;
434
435                         if ( lutil_parse_time( argv[ 1 ], &t ) != 0 ) {
436                                 Debug( LDAP_DEBUG_ANY,
437         "%s: line %d: unable to parse ttl \"%s\" in \"dncache-ttl <ttl>\" line\n",
438                                         fname, lineno, argv[ 1 ] );
439                                 return 1;
440                         }
441                         mi->mi_cache.ttl = (time_t)t;
442                 }
443
444         /* network timeout when connecting to ldap servers */
445         } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
446                 unsigned long   t;
447                 time_t          *tp = mi->mi_ntargets ?
448                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_network_timeout
449                                 : &mi->mi_network_timeout;
450
451                 if ( argc != 2 ) {
452                         Debug( LDAP_DEBUG_ANY,
453         "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
454                                 fname, lineno, 0 );
455                         return 1;
456                 }
457
458                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
459                         Debug( LDAP_DEBUG_ANY,
460         "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
461                                 fname, lineno, argv[ 1 ] );
462                         return 1;
463
464                 }
465
466                 *tp = (time_t)t;
467
468         /* idle timeout when connecting to ldap servers */
469         } else if ( strcasecmp( argv[ 0 ], "idle-timeout" ) == 0 ) {
470                 unsigned long   t;
471
472                 switch ( argc ) {
473                 case 1:
474                         Debug( LDAP_DEBUG_ANY,
475         "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
476                                 fname, lineno, 0 );
477                         return 1;
478                 case 2:
479                         break;
480                 default:
481                         Debug( LDAP_DEBUG_ANY,
482         "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
483                                 fname, lineno, 0 );
484                         return 1;
485                 }
486
487                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
488                         Debug( LDAP_DEBUG_ANY,
489         "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
490                                 fname, lineno, argv[ 1 ] );
491                         return 1;
492
493                 }
494
495                 mi->mi_idle_timeout = (time_t)t;
496
497         /* conn ttl */
498         } else if ( strcasecmp( argv[ 0 ], "conn-ttl" ) == 0 ) {
499                 unsigned long   t;
500
501                 switch ( argc ) {
502                 case 1:
503                         Debug( LDAP_DEBUG_ANY,
504         "%s: line %d: missing ttl value in \"conn-ttl <seconds>\" line\n",
505                                 fname, lineno, 0 );
506                         return 1;
507                 case 2:
508                         break;
509                 default:
510                         Debug( LDAP_DEBUG_ANY,
511         "%s: line %d: extra cruft after ttl value in \"conn-ttl <seconds>\" line\n",
512                                 fname, lineno, 0 );
513                         return 1;
514                 }
515
516                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
517                         Debug( LDAP_DEBUG_ANY,
518         "%s: line %d: unable to parse ttl \"%s\" in \"conn-ttl <seconds>\" line\n",
519                                 fname, lineno, argv[ 1 ] );
520                         return 1;
521
522                 }
523
524                 mi->mi_conn_ttl = (time_t)t;
525
526         /* bind timeout when connecting to ldap servers */
527         } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
528                 unsigned long   t;
529                 struct timeval  *tp = mi->mi_ntargets ?
530                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_bind_timeout
531                                 : &mi->mi_bind_timeout;
532
533                 switch ( argc ) {
534                 case 1:
535                         Debug( LDAP_DEBUG_ANY,
536         "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
537                                 fname, lineno, 0 );
538                         return 1;
539                 case 2:
540                         break;
541                 default:
542                         Debug( LDAP_DEBUG_ANY,
543         "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
544                                 fname, lineno, 0 );
545                         return 1;
546                 }
547
548                 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
549                         Debug( LDAP_DEBUG_ANY,
550         "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
551                                 fname, lineno, argv[ 1 ] );
552                         return 1;
553
554                 }
555
556                 tp->tv_sec = t/1000000;
557                 tp->tv_usec = t%1000000;
558
559         /* name to use for meta_back_group */
560         } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
561                         || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
562         {
563                 int             i = mi->mi_ntargets - 1;
564                 struct berval   dn;
565
566                 if ( i < 0 ) {
567                         Debug( LDAP_DEBUG_ANY,
568         "%s: line %d: need \"uri\" directive first\n",
569                                 fname, lineno, 0 );
570                         return 1;
571                 }
572                 
573                 if ( argc != 2 ) {
574                         Debug( LDAP_DEBUG_ANY,
575         "%s: line %d: missing name in \"binddn <name>\" line\n",
576                                 fname, lineno, 0 );
577                         return 1;
578                 }
579
580                 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
581                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
582                                 "\"binddn\" statement is deprecated; "
583                                 "use \"acl-authcDN\" instead\n",
584                                 fname, lineno, 0 );
585                         /* FIXME: some day we'll need to throw an error */
586                 }
587
588                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
589                 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ]->mt_binddn,
590                         NULL ) != LDAP_SUCCESS )
591                 {
592                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
593                                         "bind DN '%s' is invalid\n",
594                                         fname, lineno, argv[ 1 ] );
595                         return( 1 );
596                 }
597
598         /* password to use for meta_back_group */
599         } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
600                         || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
601         {
602                 int             i = mi->mi_ntargets - 1;
603
604                 if ( i < 0 ) {
605                         Debug( LDAP_DEBUG_ANY,
606         "%s: line %d: need \"uri\" directive first\n",
607                                 fname, lineno, 0 );
608                         return 1;
609                 }
610                 
611                 if ( argc != 2 ) {
612                         Debug( LDAP_DEBUG_ANY,
613         "%s: line %d: missing password in \"bindpw <password>\" line\n",
614                             fname, lineno, 0 );
615                         return 1;
616                 }
617
618                 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
619                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
620                                 "\"bindpw\" statement is deprecated; "
621                                 "use \"acl-passwd\" instead\n",
622                                 fname, lineno, 0 );
623                         /* FIXME: some day we'll need to throw an error */
624                 }
625
626                 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ]->mt_bindpw );
627                 
628         /* save bind creds for referral rebinds? */
629         } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
630                 if ( argc > 2 ) {
631                         Debug( LDAP_DEBUG_ANY,
632         "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
633                             fname, lineno, 0 );
634                         return( 1 );
635                 }
636
637                 if ( argc == 1 ) {
638                         Debug( LDAP_DEBUG_ANY,
639         "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
640                             fname, lineno, 0 );
641                         mi->mi_flags |= LDAP_BACK_F_SAVECRED;
642
643                 } else {
644                         switch ( check_true_false( argv[ 1 ] ) ) {
645                         case 0:
646                                 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
647                                 break;
648
649                         case 1:
650                                 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
651                                 break;
652
653                         default:
654                                 Debug( LDAP_DEBUG_ANY,
655         "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
656                                     fname, lineno, argv[ 1 ] );
657                                 return 1;
658                         }
659                 }
660
661         } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
662                 unsigned        *flagsp = mi->mi_ntargets ?
663                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
664                                 : &mi->mi_flags;
665
666                 if ( argc != 2 ) {
667                         Debug( LDAP_DEBUG_ANY,
668         "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
669                                 fname, lineno, 0 );
670                         return( 1 );
671                 }
672
673                 /* this is the default; we add it because the default might change... */
674                 switch ( check_true_false( argv[ 1 ] ) ) {
675                 case 1:
676                         *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
677                         break;
678
679                 case 0:
680                         *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
681                         break;
682
683                 default:
684                         Debug( LDAP_DEBUG_ANY,
685                 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
686                                 fname, lineno, argv[ 1 ] );
687                         return( 1 );
688                 }
689         
690         } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
691                 unsigned        *flagsp = mi->mi_ntargets ?
692                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
693                                 : &mi->mi_flags;
694
695                 if ( argc != 2 ) {
696                         Debug( LDAP_DEBUG_ANY,
697                 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
698                                 fname, lineno, 0 );
699                         return( 1 );
700                 }
701
702                 /* start */
703                 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
704                         *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
705         
706                 /* try start tls */
707                 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
708                         *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
709                         *flagsp |= LDAP_BACK_F_USE_TLS;
710         
711                 /* propagate start tls */
712                 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
713                         *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
714                 
715                 /* try start tls */
716                 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
717                         *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
718                         *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
719
720                 } else {
721                         Debug( LDAP_DEBUG_ANY,
722                 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
723                                 fname, lineno, argv[ 1 ] );
724                         return( 1 );
725                 }
726
727         } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
728                 unsigned        *flagsp = mi->mi_ntargets ?
729                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
730                                 : &mi->mi_flags;
731
732                 if ( argc != 2 ) {
733                         Debug( LDAP_DEBUG_ANY,
734                 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
735                                 fname, lineno, 0 );
736                         return( 1 );
737                 }
738
739                 switch ( check_true_false( argv[ 1 ] ) ) {
740                 case 0:
741                         *flagsp &= ~LDAP_BACK_F_T_F_MASK2;
742                         break;
743
744                 case 1:
745                         *flagsp |= LDAP_BACK_F_T_F;
746                         break;
747
748                 default:
749                         if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
750                                 *flagsp |= LDAP_BACK_F_T_F_DISCOVER;
751
752                         } else {
753                                 Debug( LDAP_DEBUG_ANY,
754         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
755                                         fname, lineno, argv[ 1 ] );
756                                 return 1;
757                         }
758                         break;
759                 }
760
761         /* onerr? */
762         } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
763                 if ( argc != 2 ) {
764                         Debug( LDAP_DEBUG_ANY,
765         "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
766                                 fname, lineno, 0 );
767                         return( 1 );
768                 }
769
770                 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
771                         mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
772
773                 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
774                         mi->mi_flags |= META_BACK_F_ONERR_STOP;
775
776                 } else {
777                         Debug( LDAP_DEBUG_ANY,
778         "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
779                                 fname, lineno, argv[ 1 ] );
780                         return 1;
781                 }
782
783         /* bind-defer? */
784         } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
785                 if ( argc != 2 ) {
786                         Debug( LDAP_DEBUG_ANY,
787         "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
788                                 fname, lineno, 0 );
789                         return( 1 );
790                 }
791
792                 switch ( check_true_false( argv[ 1 ] ) ) {
793                 case 0:
794                         mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
795                         break;
796
797                 case 1:
798                         mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
799                         break;
800
801                 default:
802                         Debug( LDAP_DEBUG_ANY,
803         "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
804                                 fname, lineno, argv[ 1 ] );
805                         return 1;
806                 }
807
808         /* single-conn? */
809         } else if ( strcasecmp( argv[ 0 ], "single-conn" ) == 0 ) {
810                 if ( argc != 2 ) {
811                         Debug( LDAP_DEBUG_ANY,
812         "%s: line %d: \"single-conn {FALSE|true}\" takes 1 argument\n",
813                                 fname, lineno, 0 );
814                         return( 1 );
815                 }
816
817                 if ( mi->mi_ntargets > 0 ) {
818                         Debug( LDAP_DEBUG_ANY,
819         "%s: line %d: \"single-conn\" must appear before target definitions\n",
820                                 fname, lineno, 0 );
821                         return( 1 );
822                 }
823
824                 switch ( check_true_false( argv[ 1 ] ) ) {
825                 case 0:
826                         mi->mi_flags &= ~LDAP_BACK_F_SINGLECONN;
827                         break;
828
829                 case 1:
830                         mi->mi_flags |= LDAP_BACK_F_SINGLECONN;
831                         break;
832
833                 default:
834                         Debug( LDAP_DEBUG_ANY,
835         "%s: line %d: \"single-conn {FALSE|true}\": invalid arg \"%s\".\n",
836                                 fname, lineno, argv[ 1 ] );
837                         return 1;
838                 }
839
840         } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
841                 unsigned        flag = 0;
842                 unsigned        *flagsp = mi->mi_ntargets ?
843                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
844                                 : &mi->mi_flags;
845
846                 if ( argc != 2 ) {
847                         Debug( LDAP_DEBUG_ANY,
848         "%s: line %d: \"cancel {abandon|ignore|exop}\" takes 1 argument\n",
849                                 fname, lineno, 0 );
850                         return( 1 );
851                 }
852
853                 if ( strcasecmp( argv[ 1 ], "abandon" ) == 0 ) {
854                         flag = LDAP_BACK_F_CANCEL_ABANDON;
855
856                 } else if ( strcasecmp( argv[ 1 ], "ignore" ) == 0 ) {
857                         flag = LDAP_BACK_F_CANCEL_IGNORE;
858
859                 } else if ( strcasecmp( argv[ 1 ], "exop" ) == 0 ) {
860                         flag = LDAP_BACK_F_CANCEL_EXOP;
861
862                 } else if ( strcasecmp( argv[ 1 ], "exop-discover" ) == 0 ) {
863                         flag = LDAP_BACK_F_CANCEL_EXOP_DISCOVER;
864
865                 } else {
866                         Debug( LDAP_DEBUG_ANY,
867         "%s: line %d: \"cancel {abandon|ignore|exop[-discover]}\": unknown mode \"%s\" \n",
868                                 fname, lineno, argv[ 1 ] );
869                         return( 1 );
870                 }
871
872                 *flagsp &= ~LDAP_BACK_F_CANCEL_MASK2;
873                 *flagsp |= flag;
874
875         } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
876                 char    *sep;
877                 time_t  *tv = mi->mi_ntargets ?
878                                 mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_timeout
879                                 : mi->mi_timeout;
880                 int     c;
881
882                 if ( argc < 2 ) {
883                         Debug( LDAP_DEBUG_ANY,
884         "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
885                                 fname, lineno, 0 );
886                         return( 1 );
887                 }
888
889                 for ( c = 1; c < argc; c++ ) {
890                         time_t          *t = NULL;
891                         unsigned long   val;
892
893                         sep = strchr( argv[ c ], '=' );
894                         if ( sep != NULL ) {
895                                 size_t  len = sep - argv[ c ];
896
897                                 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
898                                         t = &tv[ LDAP_BACK_OP_ADD ];
899                                 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
900                                         t = &tv[ LDAP_BACK_OP_DELETE ];
901                                 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
902                                         t = &tv[ LDAP_BACK_OP_MODIFY ];
903                                 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
904                                         t = &tv[ LDAP_BACK_OP_MODRDN ];
905                                 } else {
906                                         char    buf[ SLAP_TEXT_BUFLEN ];
907                                         snprintf( buf, sizeof( buf ),
908                                                 "unknown operation \"%s\" for timeout #%d",
909                                                 argv[ c ], c );
910                                         Debug( LDAP_DEBUG_ANY,
911                                                 "%s: line %d: %s.\n",
912                                                 fname, lineno, buf );
913                                         return 1;
914                                 }
915                                 sep++;
916         
917                         } else {
918                                 sep = argv[ c ];
919                         }
920         
921                         if ( lutil_parse_time( sep, &val ) != 0 ) {
922                                 Debug( LDAP_DEBUG_ANY,
923                 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
924                                         fname, lineno, sep );
925                                 return 1;
926                         }
927                 
928                         if ( t ) {
929                                 *t = (time_t)val;
930         
931                         } else {
932                                 int     i;
933         
934                                 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
935                                         tv[ i ] = (time_t)val;
936                                 }
937                         }
938                 }
939         
940         /* name to use as pseudo-root dn */
941         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
942                 int             i = mi->mi_ntargets - 1;
943                 struct berval   dn;
944
945                 if ( i < 0 ) {
946                         Debug( LDAP_DEBUG_ANY,
947         "%s: line %d: need \"uri\" directive first\n",
948                                 fname, lineno, 0 );
949                         return 1;
950                 }
951                 
952                 if ( argc != 2 ) {
953                         Debug( LDAP_DEBUG_ANY,
954         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
955                                 fname, lineno, 0 );
956                         return 1;
957                 }
958
959                 dn.bv_val = argv[ 1 ];
960                 dn.bv_len = strlen( argv[ 1 ] );
961                 if ( dnNormalize( 0, NULL, NULL, &dn,
962                         &mi->mi_targets[ i ]->mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
963                 {
964                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
965                                         "pseudoroot DN '%s' is invalid\n",
966                                         fname, lineno, argv[ 1 ] );
967                         return( 1 );
968                 }
969
970         /* password to use as pseudo-root */
971         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
972                 int             i = mi->mi_ntargets - 1;
973
974                 if ( i < 0 ) {
975                         Debug( LDAP_DEBUG_ANY,
976         "%s: line %d: need \"uri\" directive first\n",
977                                 fname, lineno, 0 );
978                         return 1;
979                 }
980                 
981                 if ( argc != 2 ) {
982                         Debug( LDAP_DEBUG_ANY,
983         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
984                             fname, lineno, 0 );
985                         return 1;
986                 }
987                 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ]->mt_pseudorootpw );
988
989         /* quarantine */
990         } else if ( strcasecmp( argv[ 0 ], "quarantine" ) == 0 ) {
991                 char                    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
992                 slap_retry_info_t       *ri = mi->mi_ntargets ?
993                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_quarantine
994                                 : &mi->mi_quarantine;
995
996                 if ( ( mi->mi_ntargets == 0 && META_BACK_QUARANTINE( mi ) )
997                         || ( mi->mi_ntargets > 0 && META_BACK_TGT_QUARANTINE( mi->mi_targets[ mi->mi_ntargets - 1 ] ) ) )
998                 {
999                         Debug( LDAP_DEBUG_ANY,
1000                                 "%s: line %d: quarantine already defined.\n",
1001                                 fname, lineno, 0 );
1002                         return 1;
1003                 }
1004
1005                 switch ( argc ) {
1006                 case 2:
1007                         break;
1008
1009                 case 1:
1010                         Debug( LDAP_DEBUG_ANY,
1011                                 "%s: line %d: missing arg in \"quarantine <pattern list>\".\n",
1012                                 fname, lineno, 0 );
1013                         return 1;
1014
1015                 default:
1016                         Debug( LDAP_DEBUG_ANY,
1017                                 "%s: line %d: extra cruft after \"quarantine <pattern list>\".\n",
1018                                 fname, lineno, 0 );
1019                         return 1;
1020                 }
1021
1022                 if ( ri != &mi->mi_quarantine ) {
1023                         ri->ri_interval = NULL;
1024                         ri->ri_num = NULL;
1025                 }
1026
1027                 if ( mi->mi_ntargets > 0 && !META_BACK_QUARANTINE( mi ) ) {
1028                         ldap_pvt_thread_mutex_init( &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_quarantine_mutex );
1029                 }
1030
1031                 if ( slap_retry_info_parse( argv[ 1 ], ri, buf, sizeof( buf ) ) ) {
1032                         Debug( LDAP_DEBUG_ANY,
1033                                 "%s line %d: %s.\n",
1034                                 fname, lineno, buf );
1035                         return 1;
1036                 }
1037         
1038         /* dn massaging */
1039         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
1040                 BackendDB       *tmp_be;
1041                 int             i = mi->mi_ntargets - 1, rc;
1042                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
1043
1044                 if ( i < 0 ) {
1045                         Debug( LDAP_DEBUG_ANY,
1046         "%s: line %d: need \"uri\" directive first\n",
1047                                 fname, lineno, 0 );
1048                         return 1;
1049                 }
1050                 
1051                 /*
1052                  * syntax:
1053                  * 
1054                  *      suffixmassage <suffix> <massaged suffix>
1055                  *
1056                  * the <suffix> field must be defined as a valid suffix
1057                  * (or suffixAlias?) for the current database;
1058                  * the <massaged suffix> shouldn't have already been
1059                  * defined as a valid suffix or suffixAlias for the 
1060                  * current server
1061                  */
1062                 if ( argc != 3 ) {
1063                         Debug( LDAP_DEBUG_ANY,
1064         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
1065                                 fname, lineno, 0 );
1066                         return 1;
1067                 }
1068
1069                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
1070                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1071                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1072                                         "suffix '%s' is invalid\n",
1073                                         fname, lineno, argv[ 1 ] );
1074                         return 1;
1075                 }
1076                 
1077                 tmp_be = select_backend( &nvnc, 0, 0 );
1078                 if ( tmp_be != NULL && tmp_be != be ) {
1079                         Debug( LDAP_DEBUG_ANY, 
1080         "%s: line %d: suffix already in use by another backend in"
1081         " \"suffixMassage <suffix> <massaged suffix>\"\n",
1082                                 fname, lineno, 0 );
1083                         free( pvnc.bv_val );
1084                         free( nvnc.bv_val );
1085                         return 1;                                               
1086                 }
1087
1088                 ber_str2bv( argv[ 2 ], 0, 0, &dn );
1089                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1090                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1091                                 "massaged suffix '%s' is invalid\n",
1092                                 fname, lineno, argv[ 2 ] );
1093                         free( pvnc.bv_val );
1094                         free( nvnc.bv_val );
1095                         return 1;
1096                 }
1097         
1098 #if 0   
1099                 tmp_be = select_backend( &nrnc, 0, 0 );
1100                 if ( tmp_be != NULL ) {
1101                         Debug( LDAP_DEBUG_ANY,
1102         "%s: line %d: massaged suffix already in use by another backend in" 
1103         " \"suffixMassage <suffix> <massaged suffix>\"\n",
1104                                 fname, lineno, 0 );
1105                         free( pvnc.bv_val );
1106                         free( nvnc.bv_val );
1107                         free( prnc.bv_val );
1108                         free( nrnc.bv_val );
1109                         return 1;
1110                 }
1111 #endif
1112                 
1113                 /*
1114                  * The suffix massaging is emulated by means of the
1115                  * rewrite capabilities
1116                  * FIXME: no extra rewrite capabilities should be added
1117                  * to the database
1118                  */
1119                 rc = suffix_massage_config( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1120                                 &pvnc, &nvnc, &prnc, &nrnc );
1121
1122                 free( pvnc.bv_val );
1123                 free( nvnc.bv_val );
1124                 free( prnc.bv_val );
1125                 free( nrnc.bv_val );
1126
1127                 return rc;
1128                 
1129         /* rewrite stuff ... */
1130         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
1131                 int             i = mi->mi_ntargets - 1;
1132
1133                 if ( i < 0 ) {
1134                         Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
1135                                 "statement outside target definition.\n",
1136                                 fname, lineno, 0 );
1137                         return 1;
1138                 }
1139                 
1140                 return rewrite_parse( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1141                                 fname, lineno, argc, argv );
1142
1143         /* objectclass/attribute mapping */
1144         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
1145                 int             i = mi->mi_ntargets - 1;
1146
1147                 if ( i < 0 ) {
1148                         Debug( LDAP_DEBUG_ANY,
1149         "%s: line %d: need \"uri\" directive first\n",
1150                                 fname, lineno, 0 );
1151                         return 1;
1152                 }
1153
1154                 return ldap_back_map_config( &mi->mi_targets[ i ]->mt_rwmap.rwm_oc, 
1155                                 &mi->mi_targets[ i ]->mt_rwmap.rwm_at,
1156                                 fname, lineno, argc, argv );
1157
1158         } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
1159                 int             i = mi->mi_ntargets - 1;
1160                 int             nretries = META_RETRY_UNDEFINED;
1161
1162                 if ( argc != 2 ) {
1163                         Debug( LDAP_DEBUG_ANY,
1164         "%s: line %d: need value in \"nretries <value>\"\n",
1165                                 fname, lineno, 0 );
1166                         return 1;
1167                 }
1168
1169                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
1170                         nretries = META_RETRY_FOREVER;
1171
1172                 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
1173                         nretries = META_RETRY_NEVER;
1174
1175                 } else {
1176                         if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
1177                                 Debug( LDAP_DEBUG_ANY,
1178         "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
1179                                         fname, lineno, argv[ 1 ] );
1180                                 return 1;
1181                         }
1182                 }
1183
1184                 if ( i < 0 ) {
1185                         mi->mi_nretries = nretries;
1186
1187                 } else {
1188                         mi->mi_targets[ i ]->mt_nretries = nretries;
1189                 }
1190
1191         } else if ( strcasecmp( argv[ 0 ], "protocol-version" ) == 0 ) {
1192                 int     *version = mi->mi_ntargets ?
1193                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_version
1194                                 : &mi->mi_version;
1195
1196                 if ( argc != 2 ) {
1197                         Debug( LDAP_DEBUG_ANY,
1198         "%s: line %d: need value in \"protocol-version <version>\"\n",
1199                                 fname, lineno, 0 );
1200                         return 1;
1201                 }
1202
1203                 if ( lutil_atoi( version, argv[ 1 ] ) != 0 ) {
1204                         Debug( LDAP_DEBUG_ANY,
1205         "%s: line %d: unable to parse version \"%s\" in \"protocol-version <version>\"\n",
1206                                 fname, lineno, argv[ 1 ] );
1207                         return 1;
1208                 }
1209
1210                 if ( *version != 0 && ( *version < LDAP_VERSION_MIN || *version > LDAP_VERSION_MAX ) ) {
1211                         Debug( LDAP_DEBUG_ANY,
1212         "%s: line %d: unsupported version \"%s\" in \"protocol-version <version>\"\n",
1213                                 fname, lineno, argv[ 1 ] );
1214                         return 1;
1215                 }
1216
1217         /* anything else */
1218         } else {
1219                 return SLAP_CONF_UNKNOWN;
1220         }
1221         return 0;
1222 }
1223
1224 int
1225 ldap_back_map_config(
1226                 struct ldapmap  *oc_map,
1227                 struct ldapmap  *at_map,
1228                 const char      *fname,
1229                 int             lineno,
1230                 int             argc,
1231                 char            **argv )
1232 {
1233         struct ldapmap          *map;
1234         struct ldapmapping      *mapping;
1235         char                    *src, *dst;
1236         int                     is_oc = 0;
1237
1238         if ( argc < 3 || argc > 4 ) {
1239                 Debug( LDAP_DEBUG_ANY,
1240         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
1241                         fname, lineno, 0 );
1242                 return 1;
1243         }
1244
1245         if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
1246                 map = oc_map;
1247                 is_oc = 1;
1248
1249         } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
1250                 map = at_map;
1251
1252         } else {
1253                 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
1254                         "\"map {objectclass | attribute} [<local> | *] "
1255                         "{<foreign> | *}\"\n",
1256                         fname, lineno, 0 );
1257                 return 1;
1258         }
1259
1260         if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
1261                 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
1262                         map->drop_missing = ( argc < 4 );
1263                         return 0;
1264                 }
1265                 src = dst = argv[ 3 ];
1266
1267         } else if ( argc < 4 ) {
1268                 src = "";
1269                 dst = argv[ 2 ];
1270
1271         } else {
1272                 src = argv[ 2 ];
1273                 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1274         }
1275
1276         if ( ( map == at_map )
1277                         && ( strcasecmp( src, "objectclass" ) == 0
1278                         || strcasecmp( dst, "objectclass" ) == 0 ) )
1279         {
1280                 Debug( LDAP_DEBUG_ANY,
1281                         "%s: line %d: objectclass attribute cannot be mapped\n",
1282                         fname, lineno, 0 );
1283         }
1284
1285         mapping = (struct ldapmapping *)ch_calloc( 2,
1286                 sizeof(struct ldapmapping) );
1287         if ( mapping == NULL ) {
1288                 Debug( LDAP_DEBUG_ANY,
1289                         "%s: line %d: out of memory\n",
1290                         fname, lineno, 0 );
1291                 return 1;
1292         }
1293         ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1294         ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1295         mapping[ 1 ].src = mapping[ 0 ].dst;
1296         mapping[ 1 ].dst = mapping[ 0 ].src;
1297
1298         /*
1299          * schema check
1300          */
1301         if ( is_oc ) {
1302                 if ( src[ 0 ] != '\0' ) {
1303                         if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1304                                 Debug( LDAP_DEBUG_ANY,
1305         "%s: line %d: warning, source objectClass '%s' "
1306         "should be defined in schema\n",
1307                                         fname, lineno, src );
1308
1309                                 /*
1310                                  * FIXME: this should become an err
1311                                  */
1312                                 goto error_return;
1313                         }
1314                 }
1315
1316                 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1317                         Debug( LDAP_DEBUG_ANY,
1318         "%s: line %d: warning, destination objectClass '%s' "
1319         "is not defined in schema\n",
1320                                 fname, lineno, dst );
1321                 }
1322         } else {
1323                 int                     rc;
1324                 const char              *text = NULL;
1325                 AttributeDescription    *ad = NULL;
1326
1327                 if ( src[ 0 ] != '\0' ) {
1328                         rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1329                         if ( rc != LDAP_SUCCESS ) {
1330                                 Debug( LDAP_DEBUG_ANY,
1331         "%s: line %d: warning, source attributeType '%s' "
1332         "should be defined in schema\n",
1333                                         fname, lineno, src );
1334
1335                                 /*
1336                                  * FIXME: this should become an err
1337                                  */
1338                                 /*
1339                                  * we create a fake "proxied" ad 
1340                                  * and add it here.
1341                                  */
1342
1343                                 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1344                                                 &ad, &text, SLAP_AD_PROXIED );
1345                                 if ( rc != LDAP_SUCCESS ) {
1346                                         char    buf[ SLAP_TEXT_BUFLEN ];
1347
1348                                         snprintf( buf, sizeof( buf ),
1349                                                 "source attributeType \"%s\": %d (%s)",
1350                                                 src, rc, text ? text : "" );
1351                                         Debug( LDAP_DEBUG_ANY,
1352                                                 "%s: line %d: %s\n",
1353                                                 fname, lineno, buf );
1354                                         goto error_return;
1355                                 }
1356                         }
1357
1358                         ad = NULL;
1359                 }
1360
1361                 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1362                 if ( rc != LDAP_SUCCESS ) {
1363                         Debug( LDAP_DEBUG_ANY,
1364         "%s: line %d: warning, destination attributeType '%s' "
1365         "is not defined in schema\n",
1366                                 fname, lineno, dst );
1367
1368                         /*
1369                          * we create a fake "proxied" ad 
1370                          * and add it here.
1371                          */
1372
1373                         rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1374                                         &ad, &text, SLAP_AD_PROXIED );
1375                         if ( rc != LDAP_SUCCESS ) {
1376                                 char    buf[ SLAP_TEXT_BUFLEN ];
1377
1378                                 snprintf( buf, sizeof( buf ),
1379                                         "source attributeType \"%s\": %d (%s)\n",
1380                                         dst, rc, text ? text : "" );
1381                                 Debug( LDAP_DEBUG_ANY,
1382                                         "%s: line %d: %s\n",
1383                                         fname, lineno, buf );
1384                                 return 1;
1385                         }
1386                 }
1387         }
1388
1389         if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1390                         || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1391         {
1392                 Debug( LDAP_DEBUG_ANY,
1393                         "%s: line %d: duplicate mapping found.\n",
1394                         fname, lineno, 0 );
1395                 goto error_return;
1396         }
1397
1398         if ( src[ 0 ] != '\0' ) {
1399                 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1400                                         mapping_cmp, mapping_dup );
1401         }
1402         avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1403                                 mapping_cmp, mapping_dup );
1404
1405         return 0;
1406
1407 error_return:;
1408         if ( mapping ) {
1409                 ch_free( mapping[ 0 ].src.bv_val );
1410                 ch_free( mapping[ 0 ].dst.bv_val );
1411                 ch_free( mapping );
1412         }
1413
1414         return 1;
1415 }
1416
1417
1418 #ifdef ENABLE_REWRITE
1419 static char *
1420 suffix_massage_regexize( const char *s )
1421 {
1422         char *res, *ptr;
1423         const char *p, *r;
1424         int i;
1425
1426         if ( s[ 0 ] == '\0' ) {
1427                 return ch_strdup( "^(.+)$" );
1428         }
1429
1430         for ( i = 0, p = s; 
1431                         ( r = strchr( p, ',' ) ) != NULL; 
1432                         p = r + 1, i++ )
1433                 ;
1434
1435         res = ch_calloc( sizeof( char ),
1436                         strlen( s )
1437                         + STRLENOF( "((.+),)?" )
1438                         + STRLENOF( "[ ]?" ) * i
1439                         + STRLENOF( "$" ) + 1 );
1440
1441         ptr = lutil_strcopy( res, "((.+),)?" );
1442         for ( i = 0, p = s;
1443                         ( r = strchr( p, ',' ) ) != NULL;
1444                         p = r + 1 , i++ ) {
1445                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1446                 ptr = lutil_strcopy( ptr, "[ ]?" );
1447
1448                 if ( r[ 1 ] == ' ' ) {
1449                         r++;
1450                 }
1451         }
1452         ptr = lutil_strcopy( ptr, p );
1453         ptr[ 0 ] = '$';
1454         ptr++;
1455         ptr[ 0 ] = '\0';
1456
1457         return res;
1458 }
1459
1460 static char *
1461 suffix_massage_patternize( const char *s, const char *p )
1462 {
1463         ber_len_t       len;
1464         char            *res, *ptr;
1465
1466         len = strlen( p );
1467
1468         if ( s[ 0 ] == '\0' ) {
1469                 len++;
1470         }
1471
1472         res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1473         if ( res == NULL ) {
1474                 return NULL;
1475         }
1476
1477         ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1478         if ( s[ 0 ] == '\0' ) {
1479                 ptr[ 0 ] = ',';
1480                 ptr++;
1481         }
1482         lutil_strcopy( ptr, p );
1483
1484         return res;
1485 }
1486
1487 int
1488 suffix_massage_config( 
1489                 struct rewrite_info *info,
1490                 struct berval *pvnc,
1491                 struct berval *nvnc,
1492                 struct berval *prnc,
1493                 struct berval *nrnc
1494 )
1495 {
1496         char *rargv[ 5 ];
1497         int line = 0;
1498
1499         rargv[ 0 ] = "rewriteEngine";
1500         rargv[ 1 ] = "on";
1501         rargv[ 2 ] = NULL;
1502         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1503
1504         rargv[ 0 ] = "rewriteContext";
1505         rargv[ 1 ] = "default";
1506         rargv[ 2 ] = NULL;
1507         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1508
1509         rargv[ 0 ] = "rewriteRule";
1510         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1511         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1512         rargv[ 3 ] = ":";
1513         rargv[ 4 ] = NULL;
1514         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1515         ch_free( rargv[ 1 ] );
1516         ch_free( rargv[ 2 ] );
1517
1518         if ( BER_BVISEMPTY( pvnc ) ) {
1519                 rargv[ 0 ] = "rewriteRule";
1520                 rargv[ 1 ] = "^$";
1521                 rargv[ 2 ] = prnc->bv_val;
1522                 rargv[ 3 ] = ":";
1523                 rargv[ 4 ] = NULL;
1524                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1525         }
1526         
1527         rargv[ 0 ] = "rewriteContext";
1528         rargv[ 1 ] = "searchEntryDN";
1529         rargv[ 2 ] = NULL;
1530         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1531
1532         rargv[ 0 ] = "rewriteRule";
1533         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1534         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1535         rargv[ 3 ] = ":";
1536         rargv[ 4 ] = NULL;
1537         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1538         ch_free( rargv[ 1 ] );
1539         ch_free( rargv[ 2 ] );
1540
1541         if ( BER_BVISEMPTY( prnc ) ) {
1542                 rargv[ 0 ] = "rewriteRule";
1543                 rargv[ 1 ] = "^$";
1544                 rargv[ 2 ] = pvnc->bv_val;
1545                 rargv[ 3 ] = ":";
1546                 rargv[ 4 ] = NULL;
1547                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1548         }
1549         
1550         /* backward compatibility */
1551         rargv[ 0 ] = "rewriteContext";
1552         rargv[ 1 ] = "searchResult";
1553         rargv[ 2 ] = "alias";
1554         rargv[ 3 ] = "searchEntryDN";
1555         rargv[ 4 ] = NULL;
1556         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1557         
1558         rargv[ 0 ] = "rewriteContext";
1559         rargv[ 1 ] = "matchedDN";
1560         rargv[ 2 ] = "alias";
1561         rargv[ 3 ] = "searchEntryDN";
1562         rargv[ 4 ] = NULL;
1563         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1564
1565         rargv[ 0 ] = "rewriteContext";
1566         rargv[ 1 ] = "searchAttrDN";
1567         rargv[ 2 ] = "alias";
1568         rargv[ 3 ] = "searchEntryDN";
1569         rargv[ 4 ] = NULL;
1570         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1571
1572         /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1573          * see servers/slapd/overlays/rwm.h for details */
1574         rargv[ 0 ] = "rewriteContext";
1575         rargv[ 1 ] = "referralAttrDN";
1576         rargv[ 2 ] = NULL;
1577         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1578
1579         rargv[ 0 ] = "rewriteContext";
1580         rargv[ 1 ] = "referralDN";
1581         rargv[ 2 ] = NULL;
1582         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1583         
1584         return 0;
1585 }
1586 #endif /* ENABLE_REWRITE */
1587