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