]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
use slab memory for proxyauthz
[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    *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                 unsigned long   t;
350                 time_t          *tp = mi->mi_ntargets ?
351                                 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_network_timeout
352                                 : &mi->mi_network_timeout;
353
354                 if ( argc != 2 ) {
355                         Debug( LDAP_DEBUG_ANY,
356         "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
357                                 fname, lineno, 0 );
358                         return 1;
359                 }
360
361                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
362                         Debug( LDAP_DEBUG_ANY,
363         "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
364                                 fname, lineno, argv[ 1 ] );
365                         return 1;
366
367                 }
368
369                 *tp = (time_t)t;
370
371         /* idle timeout when connecting to ldap servers */
372         } else if ( strcasecmp( argv[ 0 ], "idle-timeout" ) == 0 ) {
373                 unsigned long   t;
374                 time_t          *tp = mi->mi_ntargets ?
375                                 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_idle_timeout
376                                 : &mi->mi_idle_timeout;
377
378                 switch ( argc ) {
379                 case 1:
380                         Debug( LDAP_DEBUG_ANY,
381         "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
382                                 fname, lineno, 0 );
383                         return 1;
384                 case 2:
385                         break;
386                 default:
387                         Debug( LDAP_DEBUG_ANY,
388         "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
389                                 fname, lineno, 0 );
390                         return 1;
391                 }
392
393                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
394                         Debug( LDAP_DEBUG_ANY,
395         "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
396                                 fname, lineno, argv[ 1 ] );
397                         return 1;
398
399                 }
400
401                 *tp = (time_t)t;
402
403         /* bind timeout when connecting to ldap servers */
404         } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
405                 unsigned long   t;
406                 struct timeval  *tp = mi->mi_ntargets ?
407                                 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_bind_timeout
408                                 : &mi->mi_bind_timeout;
409
410                 switch ( argc ) {
411                 case 1:
412                         Debug( LDAP_DEBUG_ANY,
413         "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
414                                 fname, lineno, 0 );
415                         return 1;
416                 case 2:
417                         break;
418                 default:
419                         Debug( LDAP_DEBUG_ANY,
420         "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
421                                 fname, lineno, 0 );
422                         return 1;
423                 }
424
425                 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
426                         Debug( LDAP_DEBUG_ANY,
427         "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
428                                 fname, lineno, argv[ 1 ] );
429                         return 1;
430
431                 }
432
433                 tp->tv_sec = t/1000000;
434                 tp->tv_usec = t%1000000;
435
436         /* name to use for meta_back_group */
437         } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
438                         || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
439         {
440                 int             i = mi->mi_ntargets - 1;
441                 struct berval   dn;
442
443                 if ( i < 0 ) {
444                         Debug( LDAP_DEBUG_ANY,
445         "%s: line %d: need \"uri\" directive first\n",
446                                 fname, lineno, 0 );
447                         return 1;
448                 }
449                 
450                 if ( argc != 2 ) {
451                         Debug( LDAP_DEBUG_ANY,
452         "%s: line %d: missing name in \"binddn <name>\" line\n",
453                                 fname, lineno, 0 );
454                         return 1;
455                 }
456
457                 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
458                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
459                                 "\"binddn\" statement is deprecated; "
460                                 "use \"acl-authcDN\" instead\n",
461                                 fname, lineno, 0 );
462                         /* FIXME: some day we'll need to throw an error */
463                 }
464
465                 dn.bv_val = argv[ 1 ];
466                 dn.bv_len = strlen( argv[ 1 ] );
467                 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ].mt_binddn,
468                         NULL ) != LDAP_SUCCESS )
469                 {
470                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
471                                         "bind DN '%s' is invalid\n",
472                                         fname, lineno, argv[ 1 ] );
473                         return( 1 );
474                 }
475
476         /* password to use for meta_back_group */
477         } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
478                         || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
479         {
480                 int             i = mi->mi_ntargets - 1;
481
482                 if ( i < 0 ) {
483                         Debug( LDAP_DEBUG_ANY,
484         "%s: line %d: need \"uri\" directive first\n",
485                                 fname, lineno, 0 );
486                         return 1;
487                 }
488                 
489                 if ( argc != 2 ) {
490                         Debug( LDAP_DEBUG_ANY,
491         "%s: line %d: missing password in \"bindpw <password>\" line\n",
492                             fname, lineno, 0 );
493                         return 1;
494                 }
495
496                 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
497                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
498                                 "\"bindpw\" statement is deprecated; "
499                                 "use \"acl-passwd\" instead\n",
500                                 fname, lineno, 0 );
501                         /* FIXME: some day we'll need to throw an error */
502                 }
503
504                 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_bindpw );
505                 
506         /* save bind creds for referral rebinds? */
507         } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
508                 if ( argc > 2 ) {
509                         Debug( LDAP_DEBUG_ANY,
510         "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
511                             fname, lineno, 0 );
512                         return( 1 );
513                 }
514
515                 if ( argc == 1 ) {
516                         Debug( LDAP_DEBUG_ANY,
517         "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
518                             fname, lineno, 0 );
519                         mi->mi_flags |= LDAP_BACK_F_SAVECRED;
520
521                 } else {
522                         switch ( check_true_false( argv[ 1 ] ) ) {
523                         case 0:
524                                 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
525                                 break;
526
527                         case 1:
528                                 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
529                                 break;
530
531                         default:
532                                 Debug( LDAP_DEBUG_ANY,
533         "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
534                                     fname, lineno, argv[ 1 ] );
535                                 return 1;
536                         }
537                 }
538
539         } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
540                 unsigned        *flagsp = mi->mi_ntargets ?
541                                 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
542                                 : &mi->mi_flags;
543
544                 if ( argc != 2 ) {
545                         Debug( LDAP_DEBUG_ANY,
546         "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
547                                 fname, lineno, 0 );
548                         return( 1 );
549                 }
550
551                 /* this is the default; we add it because the default might change... */
552                 switch ( check_true_false( argv[ 1 ] ) ) {
553                 case 1:
554                         *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
555                         break;
556
557                 case 0:
558                         *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
559                         break;
560
561                 default:
562                         Debug( LDAP_DEBUG_ANY,
563                 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
564                                 fname, lineno, argv[ 1 ] );
565                         return( 1 );
566                 }
567         
568         } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
569                 unsigned        *flagsp = mi->mi_ntargets ?
570                                 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
571                                 : &mi->mi_flags;
572
573                 if ( argc != 2 ) {
574                         Debug( LDAP_DEBUG_ANY,
575                 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
576                                 fname, lineno, 0 );
577                         return( 1 );
578                 }
579
580                 /* start */
581                 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
582                         *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
583         
584                 /* try start tls */
585                 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
586                         *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
587                         *flagsp |= LDAP_BACK_F_USE_TLS;
588         
589                 /* propagate start tls */
590                 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
591                         *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
592                 
593                 /* try start tls */
594                 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
595                         *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
596                         *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
597
598                 } else {
599                         Debug( LDAP_DEBUG_ANY,
600                 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
601                                 fname, lineno, argv[ 1 ] );
602                         return( 1 );
603                 }
604
605         } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
606                 unsigned        *flagsp = mi->mi_ntargets ?
607                                 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
608                                 : &mi->mi_flags;
609
610                 if ( argc != 2 ) {
611                         Debug( LDAP_DEBUG_ANY,
612                 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
613                                 fname, lineno, 0 );
614                         return( 1 );
615                 }
616
617                 switch ( check_true_false( argv[ 1 ] ) ) {
618                 case 0:
619                         *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
620                         break;
621
622                 case 1:
623                         *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
624                         break;
625
626                 default:
627                         if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
628                                 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
629
630                         } else {
631                                 Debug( LDAP_DEBUG_ANY,
632         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
633                                         fname, lineno, argv[ 1 ] );
634                                 return 1;
635                         }
636                         break;
637                 }
638
639         /* onerr? */
640         } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
641                 if ( argc != 2 ) {
642                         Debug( LDAP_DEBUG_ANY,
643         "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
644                                 fname, lineno, 0 );
645                         return( 1 );
646                 }
647
648                 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
649                         mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
650
651                 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
652                         mi->mi_flags |= META_BACK_F_ONERR_STOP;
653
654                 } else {
655                         Debug( LDAP_DEBUG_ANY,
656         "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
657                                 fname, lineno, argv[ 1 ] );
658                         return 1;
659                 }
660
661         /* bind-defer? */
662         } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
663                 if ( argc != 2 ) {
664                         Debug( LDAP_DEBUG_ANY,
665         "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
666                                 fname, lineno, 0 );
667                         return( 1 );
668                 }
669
670                 switch ( check_true_false( argv[ 1 ] ) ) {
671                 case 0:
672                         mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
673                         break;
674
675                 case 1:
676                         mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
677                         break;
678
679                 default:
680                         Debug( LDAP_DEBUG_ANY,
681         "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
682                                 fname, lineno, argv[ 1 ] );
683                         return 1;
684                 }
685
686         } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
687                 char    *sep;
688                 time_t  *tv = mi->mi_ntargets ?
689                                 mi->mi_targets[ mi->mi_ntargets - 1 ].mt_timeout
690                                 : mi->mi_timeout;
691                 int     c;
692
693                 if ( argc < 2 ) {
694                         Debug( LDAP_DEBUG_ANY,
695         "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
696                                 fname, lineno, 0 );
697                         return( 1 );
698                 }
699
700                 for ( c = 1; c < argc; c++ ) {
701                         time_t          *t = NULL;
702                         unsigned long   val;
703
704                         sep = strchr( argv[ c ], '=' );
705                         if ( sep != NULL ) {
706                                 size_t  len = sep - argv[ c ];
707
708                                 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
709                                         t = &tv[ LDAP_BACK_OP_ADD ];
710                                 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
711                                         t = &tv[ LDAP_BACK_OP_DELETE ];
712                                 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
713                                         t = &tv[ LDAP_BACK_OP_MODIFY ];
714                                 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
715                                         t = &tv[ LDAP_BACK_OP_MODRDN ];
716                                 } else {
717                                         char    buf[ SLAP_TEXT_BUFLEN ];
718                                         snprintf( buf, sizeof( buf ),
719                                                 "unknown operation \"%s\" for timeout #%d",
720                                                 argv[ c ], c );
721                                         Debug( LDAP_DEBUG_ANY,
722                                                 "%s: line %d: %s.\n",
723                                                 fname, lineno, buf );
724                                         return 1;
725                                 }
726                                 sep++;
727         
728                         } else {
729                                 sep = argv[ c ];
730                         }
731         
732                         if ( lutil_parse_time( sep, &val ) != 0 ) {
733                                 Debug( LDAP_DEBUG_ANY,
734                 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
735                                         fname, lineno, sep );
736                                 return 1;
737                         }
738                 
739                         if ( t ) {
740                                 *t = (time_t)val;
741         
742                         } else {
743                                 int     i;
744         
745                                 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
746                                         tv[ i ] = (time_t)val;
747                                 }
748                         }
749                 }
750         
751         /* name to use as pseudo-root dn */
752         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
753                 int             i = mi->mi_ntargets - 1;
754                 struct berval   dn;
755
756                 if ( i < 0 ) {
757                         Debug( LDAP_DEBUG_ANY,
758         "%s: line %d: need \"uri\" directive first\n",
759                                 fname, lineno, 0 );
760                         return 1;
761                 }
762                 
763                 if ( argc != 2 ) {
764                         Debug( LDAP_DEBUG_ANY,
765         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
766                                 fname, lineno, 0 );
767                         return 1;
768                 }
769
770                 dn.bv_val = argv[ 1 ];
771                 dn.bv_len = strlen( argv[ 1 ] );
772                 if ( dnNormalize( 0, NULL, NULL, &dn,
773                         &mi->mi_targets[ i ].mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
774                 {
775                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
776                                         "pseudoroot DN '%s' is invalid\n",
777                                         fname, lineno, argv[ 1 ] );
778                         return( 1 );
779                 }
780
781         /* password to use as pseudo-root */
782         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
783                 int             i = mi->mi_ntargets - 1;
784
785                 if ( i < 0 ) {
786                         Debug( LDAP_DEBUG_ANY,
787         "%s: line %d: need \"uri\" directive first\n",
788                                 fname, lineno, 0 );
789                         return 1;
790                 }
791                 
792                 if ( argc != 2 ) {
793                         Debug( LDAP_DEBUG_ANY,
794         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
795                             fname, lineno, 0 );
796                         return 1;
797                 }
798                 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_pseudorootpw );
799         
800         /* dn massaging */
801         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
802                 BackendDB       *tmp_be;
803                 int             i = mi->mi_ntargets - 1, rc;
804                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
805
806                 if ( i < 0 ) {
807                         Debug( LDAP_DEBUG_ANY,
808         "%s: line %d: need \"uri\" directive first\n",
809                                 fname, lineno, 0 );
810                         return 1;
811                 }
812                 
813                 /*
814                  * syntax:
815                  * 
816                  *      suffixmassage <suffix> <massaged suffix>
817                  *
818                  * the <suffix> field must be defined as a valid suffix
819                  * (or suffixAlias?) for the current database;
820                  * the <massaged suffix> shouldn't have already been
821                  * defined as a valid suffix or suffixAlias for the 
822                  * current server
823                  */
824                 if ( argc != 3 ) {
825                         Debug( LDAP_DEBUG_ANY,
826         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
827                                 fname, lineno, 0 );
828                         return 1;
829                 }
830
831                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
832                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
833                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
834                                         "suffix '%s' is invalid\n",
835                                         fname, lineno, argv[ 1 ] );
836                         return 1;
837                 }
838                 
839                 tmp_be = select_backend( &nvnc, 0, 0 );
840                 if ( tmp_be != NULL && tmp_be != be ) {
841                         Debug( LDAP_DEBUG_ANY, 
842         "%s: line %d: suffix already in use by another backend in"
843         " \"suffixMassage <suffix> <massaged suffix>\"\n",
844                                 fname, lineno, 0 );
845                         free( pvnc.bv_val );
846                         free( nvnc.bv_val );
847                         return 1;                                               
848                 }
849
850                 ber_str2bv( argv[ 2 ], 0, 0, &dn );
851                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
852                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
853                                 "massaged suffix '%s' is invalid\n",
854                                 fname, lineno, argv[ 2 ] );
855                         free( pvnc.bv_val );
856                         free( nvnc.bv_val );
857                         return 1;
858                 }
859         
860 #if 0   
861                 tmp_be = select_backend( &nrnc, 0, 0 );
862                 if ( tmp_be != NULL ) {
863                         Debug( LDAP_DEBUG_ANY,
864         "%s: line %d: massaged suffix already in use by another backend in" 
865         " \"suffixMassage <suffix> <massaged suffix>\"\n",
866                                 fname, lineno, 0 );
867                         free( pvnc.bv_val );
868                         free( nvnc.bv_val );
869                         free( prnc.bv_val );
870                         free( nrnc.bv_val );
871                         return 1;
872                 }
873 #endif
874                 
875                 /*
876                  * The suffix massaging is emulated by means of the
877                  * rewrite capabilities
878                  * FIXME: no extra rewrite capabilities should be added
879                  * to the database
880                  */
881                 rc = suffix_massage_config( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
882                                 &pvnc, &nvnc, &prnc, &nrnc );
883
884                 free( pvnc.bv_val );
885                 free( nvnc.bv_val );
886                 free( prnc.bv_val );
887                 free( nrnc.bv_val );
888
889                 return rc;
890                 
891         /* rewrite stuff ... */
892         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
893                 int             i = mi->mi_ntargets - 1;
894
895                 if ( i < 0 ) {
896                         Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
897                                 "statement outside target definition.\n",
898                                 fname, lineno, 0 );
899                         return 1;
900                 }
901                 
902                 return rewrite_parse( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
903                                 fname, lineno, argc, argv );
904
905         /* objectclass/attribute mapping */
906         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
907                 int             i = mi->mi_ntargets - 1;
908
909                 if ( i < 0 ) {
910                         Debug( LDAP_DEBUG_ANY,
911         "%s: line %d: need \"uri\" directive first\n",
912                                 fname, lineno, 0 );
913                         return 1;
914                 }
915
916                 return ldap_back_map_config( &mi->mi_targets[ i ].mt_rwmap.rwm_oc, 
917                                 &mi->mi_targets[ i ].mt_rwmap.rwm_at,
918                                 fname, lineno, argc, argv );
919
920         } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
921                 int             i = mi->mi_ntargets - 1;
922                 int             nretries = META_RETRY_UNDEFINED;
923
924                 if ( argc != 2 ) {
925                         Debug( LDAP_DEBUG_ANY,
926         "%s: line %d: need value in \"nretries <value>\"\n",
927                                 fname, lineno, 0 );
928                         return 1;
929                 }
930
931                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
932                         nretries = META_RETRY_FOREVER;
933
934                 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
935                         nretries = META_RETRY_NEVER;
936
937                 } else {
938                         if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
939                                 Debug( LDAP_DEBUG_ANY,
940         "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
941                                         fname, lineno, argv[ 1 ] );
942                                 return 1;
943                         }
944                 }
945
946                 if ( i < 0 ) {
947                         mi->mi_nretries = nretries;
948
949                 } else {
950                         mi->mi_targets[ i ].mt_nretries = nretries;
951                 }
952
953         /* anything else */
954         } else {
955                 return SLAP_CONF_UNKNOWN;
956         }
957         return 0;
958 }
959
960 int
961 ldap_back_map_config(
962                 struct ldapmap  *oc_map,
963                 struct ldapmap  *at_map,
964                 const char      *fname,
965                 int             lineno,
966                 int             argc,
967                 char            **argv )
968 {
969         struct ldapmap          *map;
970         struct ldapmapping      *mapping;
971         char                    *src, *dst;
972         int                     is_oc = 0;
973
974         if ( argc < 3 || argc > 4 ) {
975                 Debug( LDAP_DEBUG_ANY,
976         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
977                         fname, lineno, 0 );
978                 return 1;
979         }
980
981         if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
982                 map = oc_map;
983                 is_oc = 1;
984
985         } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
986                 map = at_map;
987
988         } else {
989                 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
990                         "\"map {objectclass | attribute} [<local> | *] "
991                         "{<foreign> | *}\"\n",
992                         fname, lineno, 0 );
993                 return 1;
994         }
995
996         if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
997                 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
998                         map->drop_missing = ( argc < 4 );
999                         return 0;
1000                 }
1001                 src = dst = argv[ 3 ];
1002
1003         } else if ( argc < 4 ) {
1004                 src = "";
1005                 dst = argv[ 2 ];
1006
1007         } else {
1008                 src = argv[ 2 ];
1009                 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1010         }
1011
1012         if ( ( map == at_map )
1013                         && ( strcasecmp( src, "objectclass" ) == 0
1014                         || strcasecmp( dst, "objectclass" ) == 0 ) )
1015         {
1016                 Debug( LDAP_DEBUG_ANY,
1017                         "%s: line %d: objectclass attribute cannot be mapped\n",
1018                         fname, lineno, 0 );
1019         }
1020
1021         mapping = (struct ldapmapping *)ch_calloc( 2,
1022                 sizeof(struct ldapmapping) );
1023         if ( mapping == NULL ) {
1024                 Debug( LDAP_DEBUG_ANY,
1025                         "%s: line %d: out of memory\n",
1026                         fname, lineno, 0 );
1027                 return 1;
1028         }
1029         ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1030         ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1031         mapping[ 1 ].src = mapping[ 0 ].dst;
1032         mapping[ 1 ].dst = mapping[ 0 ].src;
1033
1034         /*
1035          * schema check
1036          */
1037         if ( is_oc ) {
1038                 if ( src[ 0 ] != '\0' ) {
1039                         if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1040                                 Debug( LDAP_DEBUG_ANY,
1041         "%s: line %d: warning, source objectClass '%s' "
1042         "should be defined in schema\n",
1043                                         fname, lineno, src );
1044
1045                                 /*
1046                                  * FIXME: this should become an err
1047                                  */
1048                                 goto error_return;
1049                         }
1050                 }
1051
1052                 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1053                         Debug( LDAP_DEBUG_ANY,
1054         "%s: line %d: warning, destination objectClass '%s' "
1055         "is not defined in schema\n",
1056                                 fname, lineno, dst );
1057                 }
1058         } else {
1059                 int                     rc;
1060                 const char              *text = NULL;
1061                 AttributeDescription    *ad = NULL;
1062
1063                 if ( src[ 0 ] != '\0' ) {
1064                         rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1065                         if ( rc != LDAP_SUCCESS ) {
1066                                 Debug( LDAP_DEBUG_ANY,
1067         "%s: line %d: warning, source attributeType '%s' "
1068         "should be defined in schema\n",
1069                                         fname, lineno, src );
1070
1071                                 /*
1072                                  * FIXME: this should become an err
1073                                  */
1074                                 /*
1075                                  * we create a fake "proxied" ad 
1076                                  * and add it here.
1077                                  */
1078
1079                                 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1080                                                 &ad, &text, SLAP_AD_PROXIED );
1081                                 if ( rc != LDAP_SUCCESS ) {
1082                                         char    buf[ SLAP_TEXT_BUFLEN ];
1083
1084                                         snprintf( buf, sizeof( buf ),
1085                                                 "source attributeType \"%s\": %d (%s)",
1086                                                 src, rc, text ? text : "" );
1087                                         Debug( LDAP_DEBUG_ANY,
1088                                                 "%s: line %d: %s\n",
1089                                                 fname, lineno, buf );
1090                                         goto error_return;
1091                                 }
1092                         }
1093
1094                         ad = NULL;
1095                 }
1096
1097                 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1098                 if ( rc != LDAP_SUCCESS ) {
1099                         Debug( LDAP_DEBUG_ANY,
1100         "%s: line %d: warning, destination attributeType '%s' "
1101         "is not defined in schema\n",
1102                                 fname, lineno, dst );
1103
1104                         /*
1105                          * we create a fake "proxied" ad 
1106                          * and add it here.
1107                          */
1108
1109                         rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1110                                         &ad, &text, SLAP_AD_PROXIED );
1111                         if ( rc != LDAP_SUCCESS ) {
1112                                 char    buf[ SLAP_TEXT_BUFLEN ];
1113
1114                                 snprintf( buf, sizeof( buf ),
1115                                         "source attributeType \"%s\": %d (%s)\n",
1116                                         dst, rc, text ? text : "" );
1117                                 Debug( LDAP_DEBUG_ANY,
1118                                         "%s: line %d: %s\n",
1119                                         fname, lineno, buf );
1120                                 return 1;
1121                         }
1122                 }
1123         }
1124
1125         if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1126                         || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1127         {
1128                 Debug( LDAP_DEBUG_ANY,
1129                         "%s: line %d: duplicate mapping found" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1130                         fname, lineno, 0 );
1131                 goto error_return;
1132         }
1133
1134         if ( src[ 0 ] != '\0' ) {
1135                 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1136                                         mapping_cmp, mapping_dup );
1137         }
1138         avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1139                                 mapping_cmp, mapping_dup );
1140
1141         return 0;
1142
1143 error_return:;
1144         if ( mapping ) {
1145                 ch_free( mapping[ 0 ].src.bv_val );
1146                 ch_free( mapping[ 0 ].dst.bv_val );
1147                 ch_free( mapping );
1148         }
1149
1150         return 1;
1151 }
1152
1153
1154 #ifdef ENABLE_REWRITE
1155 static char *
1156 suffix_massage_regexize( const char *s )
1157 {
1158         char *res, *ptr;
1159         const char *p, *r;
1160         int i;
1161
1162         if ( s[ 0 ] == '\0' ) {
1163                 return ch_strdup( "^(.+)$" );
1164         }
1165
1166         for ( i = 0, p = s; 
1167                         ( r = strchr( p, ',' ) ) != NULL; 
1168                         p = r + 1, i++ )
1169                 ;
1170
1171         res = ch_calloc( sizeof( char ),
1172                         strlen( s )
1173                         + STRLENOF( "((.+),)?" )
1174                         + STRLENOF( "[ ]?" ) * i
1175                         + STRLENOF( "$" ) + 1 );
1176
1177         ptr = lutil_strcopy( res, "((.+),)?" );
1178         for ( i = 0, p = s;
1179                         ( r = strchr( p, ',' ) ) != NULL;
1180                         p = r + 1 , i++ ) {
1181                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1182                 ptr = lutil_strcopy( ptr, "[ ]?" );
1183
1184                 if ( r[ 1 ] == ' ' ) {
1185                         r++;
1186                 }
1187         }
1188         ptr = lutil_strcopy( ptr, p );
1189         ptr[ 0 ] = '$';
1190         ptr++;
1191         ptr[ 0 ] = '\0';
1192
1193         return res;
1194 }
1195
1196 static char *
1197 suffix_massage_patternize( const char *s, const char *p )
1198 {
1199         ber_len_t       len;
1200         char            *res, *ptr;
1201
1202         len = strlen( p );
1203
1204         if ( s[ 0 ] == '\0' ) {
1205                 len++;
1206         }
1207
1208         res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1209         if ( res == NULL ) {
1210                 return NULL;
1211         }
1212
1213         ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1214         if ( s[ 0 ] == '\0' ) {
1215                 ptr[ 0 ] = ',';
1216                 ptr++;
1217         }
1218         lutil_strcopy( ptr, p );
1219
1220         return res;
1221 }
1222
1223 int
1224 suffix_massage_config( 
1225                 struct rewrite_info *info,
1226                 struct berval *pvnc,
1227                 struct berval *nvnc,
1228                 struct berval *prnc,
1229                 struct berval *nrnc
1230 )
1231 {
1232         char *rargv[ 5 ];
1233         int line = 0;
1234
1235         rargv[ 0 ] = "rewriteEngine";
1236         rargv[ 1 ] = "on";
1237         rargv[ 2 ] = NULL;
1238         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1239
1240         rargv[ 0 ] = "rewriteContext";
1241         rargv[ 1 ] = "default";
1242         rargv[ 2 ] = NULL;
1243         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1244
1245         rargv[ 0 ] = "rewriteRule";
1246         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1247         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1248         rargv[ 3 ] = ":";
1249         rargv[ 4 ] = NULL;
1250         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1251         ch_free( rargv[ 1 ] );
1252         ch_free( rargv[ 2 ] );
1253
1254         if ( BER_BVISEMPTY( pvnc ) ) {
1255                 rargv[ 0 ] = "rewriteRule";
1256                 rargv[ 1 ] = "^$";
1257                 rargv[ 2 ] = prnc->bv_val;
1258                 rargv[ 3 ] = ":";
1259                 rargv[ 4 ] = NULL;
1260                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1261         }
1262         
1263         rargv[ 0 ] = "rewriteContext";
1264         rargv[ 1 ] = "searchEntryDN";
1265         rargv[ 2 ] = NULL;
1266         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1267
1268         rargv[ 0 ] = "rewriteRule";
1269         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1270         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1271         rargv[ 3 ] = ":";
1272         rargv[ 4 ] = NULL;
1273         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1274         ch_free( rargv[ 1 ] );
1275         ch_free( rargv[ 2 ] );
1276
1277         if ( BER_BVISEMPTY( prnc ) ) {
1278                 rargv[ 0 ] = "rewriteRule";
1279                 rargv[ 1 ] = "^$";
1280                 rargv[ 2 ] = pvnc->bv_val;
1281                 rargv[ 3 ] = ":";
1282                 rargv[ 4 ] = NULL;
1283                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1284         }
1285         
1286         /* backward compatibility */
1287         rargv[ 0 ] = "rewriteContext";
1288         rargv[ 1 ] = "searchResult";
1289         rargv[ 2 ] = "alias";
1290         rargv[ 3 ] = "searchEntryDN";
1291         rargv[ 4 ] = NULL;
1292         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1293         
1294         rargv[ 0 ] = "rewriteContext";
1295         rargv[ 1 ] = "matchedDN";
1296         rargv[ 2 ] = "alias";
1297         rargv[ 3 ] = "searchEntryDN";
1298         rargv[ 4 ] = NULL;
1299         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1300
1301         rargv[ 0 ] = "rewriteContext";
1302         rargv[ 1 ] = "searchAttrDN";
1303         rargv[ 2 ] = "alias";
1304         rargv[ 3 ] = "searchEntryDN";
1305         rargv[ 4 ] = NULL;
1306         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1307
1308         /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1309          * see servers/slapd/overlays/rwm.h for details */
1310         rargv[ 0 ] = "rewriteContext";
1311         rargv[ 1 ] = "referralAttrDN";
1312         rargv[ 2 ] = NULL;
1313         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1314
1315         rargv[ 0 ] = "rewriteContext";
1316         rargv[ 1 ] = "referralDN";
1317         rargv[ 2 ] = NULL;
1318         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1319         
1320         return 0;
1321 }
1322 #endif /* ENABLE_REWRITE */
1323