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