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