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