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