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