]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
Plug mutex/rwlock leaks (destroy them)
[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-2010 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         char                    *rargv[ 3 ];
42         metatarget_t            *mt;
43
44         *mtp = NULL;
45
46         mt = ch_calloc( sizeof( metatarget_t ), 1 );
47
48         mt->mt_rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
49         if ( mt->mt_rwmap.rwm_rw == NULL ) {
50                 ch_free( mt );
51                 return -1;
52         }
53
54         /*
55          * the filter rewrite as a string must be disabled
56          * by default; it can be re-enabled by adding rules;
57          * this creates an empty rewriteContext
58          */
59         rargv[ 0 ] = "rewriteContext";
60         rargv[ 1 ] = "searchFilter";
61         rargv[ 2 ] = NULL;
62         rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
63
64         rargv[ 0 ] = "rewriteContext";
65         rargv[ 1 ] = "default";
66         rargv[ 2 ] = NULL;
67         rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
68
69         ldap_pvt_thread_mutex_init( &mt->mt_uri_mutex );
70
71         mt->mt_idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
72         mt->mt_idassert_authmethod = LDAP_AUTH_NONE;
73         mt->mt_idassert_tls = SB_TLS_DEFAULT;
74
75         /* by default, use proxyAuthz control on each operation */
76         mt->mt_idassert_flags = LDAP_BACK_AUTH_PRESCRIPTIVE;
77
78         *mtp = mt;
79
80         return 0;
81 }
82
83 static int
84 check_true_false( char *str )
85 {
86         if ( strcasecmp( str, "true" ) == 0 || strcasecmp( str, "yes" ) == 0 ) {
87                 return 1;
88         }
89
90         if ( strcasecmp( str, "false" ) == 0 || strcasecmp( str, "no" ) == 0 ) {
91                 return 0;
92         }
93
94         return -1;
95 }
96
97
98 int
99 meta_back_db_config(
100                 BackendDB       *be,
101                 const char      *fname,
102                 int             lineno,
103                 int             argc,
104                 char            **argv
105 )
106 {
107         metainfo_t      *mi = ( metainfo_t * )be->be_private;
108
109         assert( mi != NULL );
110
111         /* URI of server to query */
112         if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
113                 int             i = mi->mi_ntargets;
114                 LDAPURLDesc     *ludp;
115                 struct berval   dn;
116                 int             rc;
117                 int             c;
118
119                 metatarget_t    *mt;
120
121                 char            **uris = NULL;
122                 
123                 if ( argc == 1 ) {
124                         Debug( LDAP_DEBUG_ANY,
125         "%s: line %d: missing URI "
126         "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
127                                 fname, lineno, 0 );
128                         return 1;
129                 }
130
131                 if ( be->be_nsuffix == NULL ) {
132                         Debug( LDAP_DEBUG_ANY,
133         "%s: line %d: the suffix must be defined before any target.\n",
134                                 fname, lineno, 0 );
135                         return 1;
136                 }
137                 
138                 ++mi->mi_ntargets;
139
140                 mi->mi_targets = ( metatarget_t ** )ch_realloc( mi->mi_targets, 
141                         sizeof( metatarget_t * ) * mi->mi_ntargets );
142                 if ( mi->mi_targets == NULL ) {
143                         Debug( LDAP_DEBUG_ANY,
144         "%s: line %d: out of memory while storing server name"
145         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
146                                 fname, lineno, 0 );
147                         return 1;
148                 }
149
150                 if ( meta_back_new_target( &mi->mi_targets[ i ] ) != 0 ) {
151                         Debug( LDAP_DEBUG_ANY,
152         "%s: line %d: unable to init server"
153         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
154                                 fname, lineno, 0 );
155                         return 1;
156                 }
157
158                 mt = mi->mi_targets[ i ];
159
160                 mt->mt_rebind_f = mi->mi_rebind_f;
161                 mt->mt_urllist_f = mi->mi_urllist_f;
162                 mt->mt_urllist_p = mt;
163
164                 mt->mt_nretries = mi->mi_nretries;
165                 mt->mt_quarantine = mi->mi_quarantine;
166                 if ( META_BACK_QUARANTINE( mi ) ) {
167                         ldap_pvt_thread_mutex_init( &mt->mt_quarantine_mutex );
168                 }
169                 mt->mt_flags = mi->mi_flags;
170                 mt->mt_version = mi->mi_version;
171                 mt->mt_network_timeout = mi->mi_network_timeout;
172                 mt->mt_bind_timeout = mi->mi_bind_timeout;
173                 for ( c = 0; c < SLAP_OP_LAST; c++ ) {
174                         mt->mt_timeout[ c ] = mi->mi_timeout[ c ];
175                 }
176
177                 for ( c = 1; c < argc; c++ ) {
178                         char    **tmpuris = ldap_str2charray( argv[ c ], "\t" );
179
180                         if ( tmpuris == NULL ) {
181                                 Debug( LDAP_DEBUG_ANY,
182         "%s: line %d: unable to parse URIs #%d"
183         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
184                                 fname, lineno, c - 1 );
185                                 return 1;
186                         }
187
188                         if ( c == 0 ) {
189                                 uris = tmpuris;
190
191                         } else {
192                                 ldap_charray_merge( &uris, tmpuris );
193                                 ldap_charray_free( tmpuris );
194                         }
195                 }
196
197                 for ( c = 0; uris[ c ] != NULL; c++ ) {
198                         char *tmpuri = NULL;
199
200                         /*
201                          * uri MUST be legal!
202                          */
203                         if ( ldap_url_parselist_ext( &ludp, uris[ c ], "\t",
204                                         LDAP_PVT_URL_PARSE_NONE ) != LDAP_SUCCESS
205                                 || ludp->lud_next != NULL )
206                         {
207                                 Debug( LDAP_DEBUG_ANY,
208                 "%s: line %d: unable to parse URI #%d"
209                 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
210                                         fname, lineno, c );
211                                 ldap_charray_free( uris );
212                                 return 1;
213                         }
214
215                         if ( c == 0 ) {
216
217                                 /*
218                                  * uri MUST have the <dn> part!
219                                  */
220                                 if ( ludp->lud_dn == NULL ) {
221                                         Debug( LDAP_DEBUG_ANY,
222                         "%s: line %d: missing <naming context> "
223                         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
224                                                 fname, lineno, 0 );
225                                         ldap_free_urllist( ludp );
226                                         ldap_charray_free( uris );
227                                         return 1;
228                                 }
229
230                                 /*
231                                  * copies and stores uri and suffix
232                                  */
233                                 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
234                                 rc = dnPrettyNormal( NULL, &dn, &mt->mt_psuffix,
235                                         &mt->mt_nsuffix, NULL );
236                                 if ( rc != LDAP_SUCCESS ) {
237                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
238                                                 "target \"%s\" DN is invalid\n",
239                                                 fname, lineno, argv[ 1 ] );
240                                         ldap_free_urllist( ludp );
241                                         ldap_charray_free( uris );
242                                         return( 1 );
243                                 }
244
245                                 ludp->lud_dn[ 0 ] = '\0';
246
247                                 switch ( ludp->lud_scope ) {
248                                 case LDAP_SCOPE_DEFAULT:
249                                         mt->mt_scope = LDAP_SCOPE_SUBTREE;
250                                         break;
251
252                                 case LDAP_SCOPE_SUBTREE:
253                                 case LDAP_SCOPE_SUBORDINATE:
254                                         mt->mt_scope = ludp->lud_scope;
255                                         break;
256
257                                 default:
258                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
259                                                 "invalid scope for target \"%s\"\n",
260                                                 fname, lineno, argv[ 1 ] );
261                                         ldap_free_urllist( ludp );
262                                         ldap_charray_free( uris );
263                                         return( 1 );
264                                 }
265
266                         } else {
267                                 /* check all, to apply the scope check on the first one */
268                                 if ( ludp->lud_dn != NULL && ludp->lud_dn[ 0 ] != '\0' ) {
269                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
270                                                 "multiple URIs must have "
271                                                 "no DN part\n",
272                                                 fname, lineno, 0 );
273                                         ldap_free_urllist( ludp );
274                                         ldap_charray_free( uris );
275                                         return( 1 );
276
277                                 }
278                         }
279
280                         tmpuri = ldap_url_list2urls( ludp );
281                         ldap_free_urllist( ludp );
282                         if ( tmpuri == NULL ) {
283                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
284                                         fname, lineno, 0 );
285                                 ldap_charray_free( uris );
286                                 return( 1 );
287                         }
288                         ldap_memfree( uris[ c ] );
289                         uris[ c ] = tmpuri;
290                 }
291
292                 mt->mt_uri = ldap_charray2str( uris, " " );
293                 ldap_charray_free( uris );
294                 if ( mt->mt_uri == NULL) {
295                         Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
296                                 fname, lineno, 0 );
297                         return( 1 );
298                 }
299                 
300                 /*
301                  * uri MUST be a branch of suffix!
302                  */
303                 for ( c = 0; !BER_BVISNULL( &be->be_nsuffix[ c ] ); c++ ) {
304                         if ( dnIsSuffix( &mt->mt_nsuffix, &be->be_nsuffix[ c ] ) ) {
305                                 break;
306                         }
307                 }
308
309                 if ( BER_BVISNULL( &be->be_nsuffix[ c ] ) ) {
310                         Debug( LDAP_DEBUG_ANY,
311         "%s: line %d: <naming context> of URI must be within the naming context of this database.\n",
312                                 fname, lineno, 0 );
313                         return 1;
314                 }
315
316         /* subtree-exclude */
317         } else if ( strcasecmp( argv[ 0 ], "subtree-exclude" ) == 0 ) {
318                 int             i = mi->mi_ntargets - 1;
319                 struct berval   dn, ndn;
320
321                 if ( i < 0 ) {
322                         Debug( LDAP_DEBUG_ANY,
323         "%s: line %d: need \"uri\" directive first\n",
324                                 fname, lineno, 0 );
325                         return 1;
326                 }
327                 
328                 switch ( argc ) {
329                 case 1:
330                         Debug( LDAP_DEBUG_ANY,
331         "%s: line %d: missing DN in \"subtree-exclude <DN>\" line\n",
332                             fname, lineno, 0 );
333                         return 1;
334
335                 case 2:
336                         break;
337
338                 default:
339                         Debug( LDAP_DEBUG_ANY,
340         "%s: line %d: too many args in \"subtree-exclude <DN>\" line\n",
341                             fname, lineno, 0 );
342                         return 1;
343                 }
344
345                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
346                 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL )
347                         != LDAP_SUCCESS )
348                 {
349                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
350                                         "subtree-exclude DN=\"%s\" is invalid\n",
351                                         fname, lineno, argv[ 1 ] );
352                         return( 1 );
353                 }
354
355                 if ( !dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_nsuffix ) ) {
356                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
357                                         "subtree-exclude DN=\"%s\" "
358                                         "must be subtree of target\n",
359                                         fname, lineno, argv[ 1 ] );
360                         ber_memfree( ndn.bv_val );
361                         return( 1 );
362                 }
363
364                 if ( mi->mi_targets[ i ]->mt_subtree_exclude != NULL ) {
365                         int             j;
366
367                         for ( j = 0; !BER_BVISNULL( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ); j++ )
368                         {
369                                 if ( dnIsSuffix( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ], &ndn ) ) {
370                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
371                                                         "subtree-exclude DN=\"%s\" "
372                                                         "is suffix of another subtree-exclude\n",
373                                                         fname, lineno, argv[ 1 ] );
374                                         /* reject, because it might be superior
375                                          * to more than one subtree-exclude */
376                                         ber_memfree( ndn.bv_val );
377                                         return( 1 );
378
379                                 } else if ( dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ) ) {
380                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
381                                                         "another subtree-exclude is suffix of "
382                                                         "subtree-exclude DN=\"%s\"\n",
383                                                         fname, lineno, argv[ 1 ] );
384                                         ber_memfree( ndn.bv_val );
385                                         return( 0 );
386                                 }
387                         }
388                 }
389
390                 ber_bvarray_add( &mi->mi_targets[ i ]->mt_subtree_exclude, &ndn );
391
392         /* default target directive */
393         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
394                 int             i = mi->mi_ntargets - 1;
395                 
396                 if ( argc == 1 ) {
397                         if ( i < 0 ) {
398                                 Debug( LDAP_DEBUG_ANY,
399         "%s: line %d: \"default-target\" alone need be"
400         " inside a \"uri\" directive\n",
401                                         fname, lineno, 0 );
402                                 return 1;
403                         }
404                         mi->mi_defaulttarget = i;
405
406                 } else {
407                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
408                                 if ( i >= 0 ) {
409                                         Debug( LDAP_DEBUG_ANY,
410         "%s: line %d: \"default-target none\""
411         " should go before uri definitions\n",
412                                                 fname, lineno, 0 );
413                                 }
414                                 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
415
416                         } else {
417                                 
418                                 if ( lutil_atoi( &mi->mi_defaulttarget, argv[ 1 ] ) != 0
419                                         || mi->mi_defaulttarget < 0
420                                         || mi->mi_defaulttarget >= i - 1 )
421                                 {
422                                         Debug( LDAP_DEBUG_ANY,
423         "%s: line %d: illegal target number %d\n",
424                                                 fname, lineno, mi->mi_defaulttarget );
425                                         return 1;
426                                 }
427                         }
428                 }
429                 
430         /* ttl of dn cache */
431         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
432                 if ( argc != 2 ) {
433                         Debug( LDAP_DEBUG_ANY,
434         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
435                                 fname, lineno, 0 );
436                         return 1;
437                 }
438                 
439                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
440                         mi->mi_cache.ttl = META_DNCACHE_FOREVER;
441
442                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
443                         mi->mi_cache.ttl = META_DNCACHE_DISABLED;
444
445                 } else {
446                         unsigned long   t;
447
448                         if ( lutil_parse_time( argv[ 1 ], &t ) != 0 ) {
449                                 Debug( LDAP_DEBUG_ANY,
450         "%s: line %d: unable to parse ttl \"%s\" in \"dncache-ttl <ttl>\" line\n",
451                                         fname, lineno, argv[ 1 ] );
452                                 return 1;
453                         }
454                         mi->mi_cache.ttl = (time_t)t;
455                 }
456
457         /* network timeout when connecting to ldap servers */
458         } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
459                 unsigned long   t;
460                 time_t          *tp = mi->mi_ntargets ?
461                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_network_timeout
462                                 : &mi->mi_network_timeout;
463
464                 if ( argc != 2 ) {
465                         Debug( LDAP_DEBUG_ANY,
466         "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
467                                 fname, lineno, 0 );
468                         return 1;
469                 }
470
471                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
472                         Debug( LDAP_DEBUG_ANY,
473         "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
474                                 fname, lineno, argv[ 1 ] );
475                         return 1;
476
477                 }
478
479                 *tp = (time_t)t;
480
481         /* idle timeout when connecting to ldap servers */
482         } else if ( strcasecmp( argv[ 0 ], "idle-timeout" ) == 0 ) {
483                 unsigned long   t;
484
485                 switch ( argc ) {
486                 case 1:
487                         Debug( LDAP_DEBUG_ANY,
488         "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
489                                 fname, lineno, 0 );
490                         return 1;
491                 case 2:
492                         break;
493                 default:
494                         Debug( LDAP_DEBUG_ANY,
495         "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
496                                 fname, lineno, 0 );
497                         return 1;
498                 }
499
500                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
501                         Debug( LDAP_DEBUG_ANY,
502         "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
503                                 fname, lineno, argv[ 1 ] );
504                         return 1;
505
506                 }
507
508                 mi->mi_idle_timeout = (time_t)t;
509
510         /* conn ttl */
511         } else if ( strcasecmp( argv[ 0 ], "conn-ttl" ) == 0 ) {
512                 unsigned long   t;
513
514                 switch ( argc ) {
515                 case 1:
516                         Debug( LDAP_DEBUG_ANY,
517         "%s: line %d: missing ttl value in \"conn-ttl <seconds>\" line\n",
518                                 fname, lineno, 0 );
519                         return 1;
520                 case 2:
521                         break;
522                 default:
523                         Debug( LDAP_DEBUG_ANY,
524         "%s: line %d: extra cruft after ttl value in \"conn-ttl <seconds>\" line\n",
525                                 fname, lineno, 0 );
526                         return 1;
527                 }
528
529                 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
530                         Debug( LDAP_DEBUG_ANY,
531         "%s: line %d: unable to parse ttl \"%s\" in \"conn-ttl <seconds>\" line\n",
532                                 fname, lineno, argv[ 1 ] );
533                         return 1;
534
535                 }
536
537                 mi->mi_conn_ttl = (time_t)t;
538
539         /* bind timeout when connecting to ldap servers */
540         } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
541                 unsigned long   t;
542                 struct timeval  *tp = mi->mi_ntargets ?
543                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_bind_timeout
544                                 : &mi->mi_bind_timeout;
545
546                 switch ( argc ) {
547                 case 1:
548                         Debug( LDAP_DEBUG_ANY,
549         "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
550                                 fname, lineno, 0 );
551                         return 1;
552                 case 2:
553                         break;
554                 default:
555                         Debug( LDAP_DEBUG_ANY,
556         "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
557                                 fname, lineno, 0 );
558                         return 1;
559                 }
560
561                 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
562                         Debug( LDAP_DEBUG_ANY,
563         "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
564                                 fname, lineno, argv[ 1 ] );
565                         return 1;
566
567                 }
568
569                 tp->tv_sec = t/1000000;
570                 tp->tv_usec = t%1000000;
571
572         /* name to use for meta_back_group */
573         } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
574                         || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
575         {
576                 int             i = mi->mi_ntargets - 1;
577                 struct berval   dn;
578
579                 if ( i < 0 ) {
580                         Debug( LDAP_DEBUG_ANY,
581         "%s: line %d: need \"uri\" directive first\n",
582                                 fname, lineno, 0 );
583                         return 1;
584                 }
585                 
586                 if ( argc != 2 ) {
587                         Debug( LDAP_DEBUG_ANY,
588         "%s: line %d: missing name in \"binddn <name>\" line\n",
589                                 fname, lineno, 0 );
590                         return 1;
591                 }
592
593                 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
594                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
595                                 "\"binddn\" statement is deprecated; "
596                                 "use \"acl-authcDN\" instead\n",
597                                 fname, lineno, 0 );
598                         /* FIXME: some day we'll need to throw an error */
599                 }
600
601                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
602                 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ]->mt_binddn,
603                         NULL ) != LDAP_SUCCESS )
604                 {
605                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
606                                         "bind DN '%s' is invalid\n",
607                                         fname, lineno, argv[ 1 ] );
608                         return( 1 );
609                 }
610
611         /* password to use for meta_back_group */
612         } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
613                         || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
614         {
615                 int             i = mi->mi_ntargets - 1;
616
617                 if ( i < 0 ) {
618                         Debug( LDAP_DEBUG_ANY,
619         "%s: line %d: need \"uri\" directive first\n",
620                                 fname, lineno, 0 );
621                         return 1;
622                 }
623
624                 if ( argc != 2 ) {
625                         Debug( LDAP_DEBUG_ANY,
626         "%s: line %d: missing password in \"bindpw <password>\" line\n",
627                             fname, lineno, 0 );
628                         return 1;
629                 }
630
631                 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
632                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
633                                 "\"bindpw\" statement is deprecated; "
634                                 "use \"acl-passwd\" instead\n",
635                                 fname, lineno, 0 );
636                         /* FIXME: some day we'll need to throw an error */
637                 }
638
639                 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ]->mt_bindpw );
640                 
641         /* save bind creds for referral rebinds? */
642         } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
643                 unsigned        *flagsp = mi->mi_ntargets ?
644                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
645                                 : &mi->mi_flags;
646
647                 if ( argc > 2 ) {
648                         Debug( LDAP_DEBUG_ANY,
649         "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
650                             fname, lineno, 0 );
651                         return( 1 );
652                 }
653
654                 if ( argc == 1 ) {
655                         Debug( LDAP_DEBUG_ANY,
656         "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
657                             fname, lineno, 0 );
658                         *flagsp |= LDAP_BACK_F_SAVECRED;
659
660                 } else {
661                         switch ( check_true_false( argv[ 1 ] ) ) {
662                         case 0:
663                                 *flagsp &= ~LDAP_BACK_F_SAVECRED;
664                                 break;
665
666                         case 1:
667                                 *flagsp |= LDAP_BACK_F_SAVECRED;
668                                 break;
669
670                         default:
671                                 Debug( LDAP_DEBUG_ANY,
672         "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
673                                     fname, lineno, argv[ 1 ] );
674                                 return 1;
675                         }
676                 }
677
678         } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
679                 unsigned        *flagsp = mi->mi_ntargets ?
680                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
681                                 : &mi->mi_flags;
682
683                 if ( argc != 2 ) {
684                         Debug( LDAP_DEBUG_ANY,
685         "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
686                                 fname, lineno, 0 );
687                         return( 1 );
688                 }
689
690                 /* this is the default; we add it because the default might change... */
691                 switch ( check_true_false( argv[ 1 ] ) ) {
692                 case 1:
693                         *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
694                         break;
695
696                 case 0:
697                         *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
698                         break;
699
700                 default:
701                         Debug( LDAP_DEBUG_ANY,
702                 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
703                                 fname, lineno, argv[ 1 ] );
704                         return( 1 );
705                 }
706         
707         } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
708                 unsigned        *flagsp = mi->mi_ntargets ?
709                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
710                                 : &mi->mi_flags;
711
712                 /* start */
713                 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
714                         *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
715         
716                 /* try start tls */
717                 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
718                         *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
719                         *flagsp |= LDAP_BACK_F_USE_TLS;
720         
721                 /* propagate start tls */
722                 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
723                         *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
724                 
725                 /* try start tls */
726                 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
727                         *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
728                         *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
729
730                 } else {
731                         Debug( LDAP_DEBUG_ANY,
732                 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
733                                 fname, lineno, argv[ 1 ] );
734                         return( 1 );
735                 }
736
737                 if ( argc > 2 ) {
738                         metatarget_t    *mt = NULL;
739                         int             i;
740
741                         if ( mi->mi_ntargets - 1 < 0 ) {
742                                 Debug( LDAP_DEBUG_ANY,
743                 "%s: line %d: need \"uri\" directive first\n",
744                                         fname, lineno, 0 );
745                                 return 1;
746                         }
747
748                         mt = mi->mi_targets[ mi->mi_ntargets - 1 ];
749
750                         for ( i = 2; i < argc; i++ ) {
751                                 if ( bindconf_tls_parse( argv[i], &mt->mt_tls ))
752                                         return 1;
753                         }
754                         bindconf_tls_defaults( &mt->mt_tls );
755                 }
756
757         } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
758                 unsigned        *flagsp = mi->mi_ntargets ?
759                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
760                                 : &mi->mi_flags;
761
762                 if ( argc != 2 ) {
763                         Debug( LDAP_DEBUG_ANY,
764                 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
765                                 fname, lineno, 0 );
766                         return( 1 );
767                 }
768
769                 switch ( check_true_false( argv[ 1 ] ) ) {
770                 case 0:
771                         *flagsp &= ~LDAP_BACK_F_T_F_MASK2;
772                         break;
773
774                 case 1:
775                         *flagsp |= LDAP_BACK_F_T_F;
776                         break;
777
778                 default:
779                         if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
780                                 *flagsp |= LDAP_BACK_F_T_F_DISCOVER;
781
782                         } else {
783                                 Debug( LDAP_DEBUG_ANY,
784         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
785                                         fname, lineno, argv[ 1 ] );
786                                 return 1;
787                         }
788                         break;
789                 }
790
791         /* onerr? */
792         } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
793                 if ( argc != 2 ) {
794                         Debug( LDAP_DEBUG_ANY,
795         "%s: line %d: \"onerr {CONTINUE|report|stop}\" takes 1 argument\n",
796                                 fname, lineno, 0 );
797                         return( 1 );
798                 }
799
800                 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
801                         mi->mi_flags &= ~META_BACK_F_ONERR_MASK;
802
803                 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
804                         mi->mi_flags |= META_BACK_F_ONERR_STOP;
805
806                 } else if ( strcasecmp( argv[ 1 ], "report" ) == 0 ) {
807                         mi->mi_flags |= META_BACK_F_ONERR_REPORT;
808
809                 } else {
810                         Debug( LDAP_DEBUG_ANY,
811         "%s: line %d: \"onerr {CONTINUE|report|stop}\": invalid arg \"%s\".\n",
812                                 fname, lineno, argv[ 1 ] );
813                         return 1;
814                 }
815
816         /* bind-defer? */
817         } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0
818                 || strcasecmp( argv[ 0 ], "root-bind-defer" ) == 0 )
819         {
820                 if ( argc != 2 ) {
821                         Debug( LDAP_DEBUG_ANY,
822         "%s: line %d: \"[pseudo]root-bind-defer {TRUE|false}\" takes 1 argument\n",
823                                 fname, lineno, 0 );
824                         return( 1 );
825                 }
826
827                 switch ( check_true_false( argv[ 1 ] ) ) {
828                 case 0:
829                         mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
830                         break;
831
832                 case 1:
833                         mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
834                         break;
835
836                 default:
837                         Debug( LDAP_DEBUG_ANY,
838         "%s: line %d: \"[pseudo]root-bind-defer {TRUE|false}\": invalid arg \"%s\".\n",
839                                 fname, lineno, argv[ 1 ] );
840                         return 1;
841                 }
842
843         /* single-conn? */
844         } else if ( strcasecmp( argv[ 0 ], "single-conn" ) == 0 ) {
845                 if ( argc != 2 ) {
846                         Debug( LDAP_DEBUG_ANY,
847         "%s: line %d: \"single-conn {FALSE|true}\" takes 1 argument\n",
848                                 fname, lineno, 0 );
849                         return( 1 );
850                 }
851
852                 if ( mi->mi_ntargets > 0 ) {
853                         Debug( LDAP_DEBUG_ANY,
854         "%s: line %d: \"single-conn\" must appear before target definitions\n",
855                                 fname, lineno, 0 );
856                         return( 1 );
857                 }
858
859                 switch ( check_true_false( argv[ 1 ] ) ) {
860                 case 0:
861                         mi->mi_flags &= ~LDAP_BACK_F_SINGLECONN;
862                         break;
863
864                 case 1:
865                         mi->mi_flags |= LDAP_BACK_F_SINGLECONN;
866                         break;
867
868                 default:
869                         Debug( LDAP_DEBUG_ANY,
870         "%s: line %d: \"single-conn {FALSE|true}\": invalid arg \"%s\".\n",
871                                 fname, lineno, argv[ 1 ] );
872                         return 1;
873                 }
874
875         /* use-temporaries? */
876         } else if ( strcasecmp( argv[ 0 ], "use-temporary-conn" ) == 0 ) {
877                 if ( argc != 2 ) {
878                         Debug( LDAP_DEBUG_ANY,
879         "%s: line %d: \"use-temporary-conn {FALSE|true}\" takes 1 argument\n",
880                                 fname, lineno, 0 );
881                         return( 1 );
882                 }
883
884                 if ( mi->mi_ntargets > 0 ) {
885                         Debug( LDAP_DEBUG_ANY,
886         "%s: line %d: \"use-temporary-conn\" must appear before target definitions\n",
887                                 fname, lineno, 0 );
888                         return( 1 );
889                 }
890
891                 switch ( check_true_false( argv[ 1 ] ) ) {
892                 case 0:
893                         mi->mi_flags &= ~LDAP_BACK_F_USE_TEMPORARIES;
894                         break;
895
896                 case 1:
897                         mi->mi_flags |= LDAP_BACK_F_USE_TEMPORARIES;
898                         break;
899
900                 default:
901                         Debug( LDAP_DEBUG_ANY,
902         "%s: line %d: \"use-temporary-conn {FALSE|true}\": invalid arg \"%s\".\n",
903                                 fname, lineno, argv[ 1 ] );
904                         return 1;
905                 }
906
907         /* privileged connections pool max size ? */
908         } else if ( strcasecmp( argv[ 0 ], "conn-pool-max" ) == 0 ) {
909                 if ( argc != 2 ) {
910                         Debug( LDAP_DEBUG_ANY,
911         "%s: line %d: \"conn-pool-max <n>\" takes 1 argument\n",
912                                 fname, lineno, 0 );
913                         return( 1 );
914                 }
915
916                 if ( mi->mi_ntargets > 0 ) {
917                         Debug( LDAP_DEBUG_ANY,
918         "%s: line %d: \"conn-pool-max\" must appear before target definitions\n",
919                                 fname, lineno, 0 );
920                         return( 1 );
921                 }
922
923                 if ( lutil_atoi( &mi->mi_conn_priv_max, argv[1] )
924                         || mi->mi_conn_priv_max < LDAP_BACK_CONN_PRIV_MIN
925                         || mi->mi_conn_priv_max > LDAP_BACK_CONN_PRIV_MAX )
926                 {
927                         Debug( LDAP_DEBUG_ANY,
928         "%s: line %d: \"conn-pool-max <n>\": invalid arg \"%s\".\n",
929                                 fname, lineno, argv[ 1 ] );
930                         return 1;
931                 }
932
933         } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
934                 unsigned        flag = 0;
935                 unsigned        *flagsp = mi->mi_ntargets ?
936                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
937                                 : &mi->mi_flags;
938
939                 if ( argc != 2 ) {
940                         Debug( LDAP_DEBUG_ANY,
941         "%s: line %d: \"cancel {abandon|ignore|exop}\" takes 1 argument\n",
942                                 fname, lineno, 0 );
943                         return( 1 );
944                 }
945
946                 if ( strcasecmp( argv[ 1 ], "abandon" ) == 0 ) {
947                         flag = LDAP_BACK_F_CANCEL_ABANDON;
948
949                 } else if ( strcasecmp( argv[ 1 ], "ignore" ) == 0 ) {
950                         flag = LDAP_BACK_F_CANCEL_IGNORE;
951
952                 } else if ( strcasecmp( argv[ 1 ], "exop" ) == 0 ) {
953                         flag = LDAP_BACK_F_CANCEL_EXOP;
954
955                 } else if ( strcasecmp( argv[ 1 ], "exop-discover" ) == 0 ) {
956                         flag = LDAP_BACK_F_CANCEL_EXOP_DISCOVER;
957
958                 } else {
959                         Debug( LDAP_DEBUG_ANY,
960         "%s: line %d: \"cancel {abandon|ignore|exop[-discover]}\": unknown mode \"%s\" \n",
961                                 fname, lineno, argv[ 1 ] );
962                         return( 1 );
963                 }
964
965                 *flagsp &= ~LDAP_BACK_F_CANCEL_MASK2;
966                 *flagsp |= flag;
967
968         } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
969                 char    *sep;
970                 time_t  *tv = mi->mi_ntargets ?
971                                 mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_timeout
972                                 : mi->mi_timeout;
973                 int     c;
974
975                 if ( argc < 2 ) {
976                         Debug( LDAP_DEBUG_ANY,
977         "%s: line %d: \"timeout [{add|bind|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
978                                 fname, lineno, 0 );
979                         return( 1 );
980                 }
981
982                 for ( c = 1; c < argc; c++ ) {
983                         time_t          *t = NULL;
984                         unsigned long   val;
985
986                         sep = strchr( argv[ c ], '=' );
987                         if ( sep != NULL ) {
988                                 size_t  len = sep - argv[ c ];
989
990                                 if ( strncasecmp( argv[ c ], "bind", len ) == 0 ) {
991                                         t = &tv[ SLAP_OP_BIND ];
992                                 /* unbind makes little sense */
993                                 } else if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
994                                         t = &tv[ SLAP_OP_ADD ];
995                                 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
996                                         t = &tv[ SLAP_OP_DELETE ];
997                                 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
998                                         t = &tv[ SLAP_OP_MODRDN ];
999                                 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
1000                                         t = &tv[ SLAP_OP_MODIFY ];
1001                                 } else if ( strncasecmp( argv[ c ], "compare", len ) == 0 ) {
1002                                         t = &tv[ SLAP_OP_COMPARE ];
1003                                 } else if ( strncasecmp( argv[ c ], "search", len ) == 0 ) {
1004                                         t = &tv[ SLAP_OP_SEARCH ];
1005                                 /* abandon makes little sense */
1006 #if 0                           /* not implemented yet */
1007                                 } else if ( strncasecmp( argv[ c ], "extended", len ) == 0 ) {
1008                                         t = &tv[ SLAP_OP_EXTENDED ];
1009 #endif
1010                                 } else {
1011                                         char    buf[ SLAP_TEXT_BUFLEN ];
1012                                         snprintf( buf, sizeof( buf ),
1013                                                 "unknown/unhandled operation \"%s\" for timeout #%d",
1014                                                 argv[ c ], c - 1 );
1015                                         Debug( LDAP_DEBUG_ANY,
1016                                                 "%s: line %d: %s.\n",
1017                                                 fname, lineno, buf );
1018                                         return 1;
1019                                 }
1020                                 sep++;
1021         
1022                         } else {
1023                                 sep = argv[ c ];
1024                         }
1025         
1026                         if ( lutil_parse_time( sep, &val ) != 0 ) {
1027                                 Debug( LDAP_DEBUG_ANY,
1028                 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
1029                                         fname, lineno, sep );
1030                                 return 1;
1031                         }
1032                 
1033                         if ( t ) {
1034                                 *t = (time_t)val;
1035         
1036                         } else {
1037                                 int     i;
1038         
1039                                 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
1040                                         tv[ i ] = (time_t)val;
1041                                 }
1042                         }
1043                 }
1044         
1045         /* name to use as pseudo-root dn */
1046         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
1047                 int             i = mi->mi_ntargets - 1;
1048
1049                 if ( i < 0 ) {
1050                         Debug( LDAP_DEBUG_ANY,
1051         "%s: line %d: need \"uri\" directive first\n",
1052                                 fname, lineno, 0 );
1053                         return 1;
1054                 }
1055                 
1056                 if ( argc != 2 ) {
1057                         Debug( LDAP_DEBUG_ANY,
1058         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
1059                                 fname, lineno, 0 );
1060                         return 1;
1061                 }
1062
1063                 /*
1064                  * exact replacement:
1065                  *
1066
1067 idassert-bind   bindmethod=simple
1068                 binddn=<pseudorootdn>
1069                 credentials=<pseudorootpw>
1070                 mode=none
1071                 flags=non-prescriptive
1072 idassert-authzFrom      "dn:<rootdn>"
1073
1074                  * so that only when authc'd as <rootdn> the proxying occurs
1075                  * rebinding as the <pseudorootdn> without proxyAuthz.
1076                  */
1077
1078                 Debug( LDAP_DEBUG_ANY,
1079                         "%s: line %d: \"pseudorootdn\", \"pseudorootpw\" are no longer supported; "
1080                         "use \"idassert-bind\" and \"idassert-authzFrom\" instead.\n",
1081                         fname, lineno, 0 );
1082
1083                 {
1084                         char    binddn[ SLAP_TEXT_BUFLEN ];
1085                         char    *cargv[] = {
1086                                 "idassert-bind",
1087                                 "bindmethod=simple",
1088                                 NULL,
1089                                 "mode=none",
1090                                 "flags=non-prescriptive",
1091                                 NULL
1092                         };
1093                         int     cargc = 5;
1094                         int     rc;
1095
1096                         if ( BER_BVISNULL( &be->be_rootndn ) ) {
1097                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"pseudorootpw\": \"rootdn\" must be defined first.\n",
1098                                         fname, lineno, 0 );
1099                                 return 1;
1100                         }
1101
1102                         if ( sizeof( binddn ) <= (unsigned) snprintf( binddn,
1103                                         sizeof( binddn ), "binddn=%s", argv[ 1 ] ))
1104                         {
1105                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"pseudorootdn\" too long.\n",
1106                                         fname, lineno, 0 );
1107                                 return 1;
1108                         }
1109                         cargv[ 2 ] = binddn;
1110
1111                         rc = mi->mi_ldap_extra->idassert_parse_cf( fname, lineno, cargc, cargv, &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert );
1112                         if ( rc == 0 ) {
1113                                 struct berval   bv;
1114
1115                                 if ( mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert_authz != NULL ) {
1116                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: \"idassert-authzFrom\" already defined (discarded).\n",
1117                                                 fname, lineno, 0 );
1118                                         ber_bvarray_free( mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert_authz );
1119                                         mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert_authz = NULL;
1120                                 }
1121
1122                                 assert( !BER_BVISNULL( &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert_authcDN ) );
1123
1124                                 bv.bv_len = STRLENOF( "dn:" ) + be->be_rootndn.bv_len;
1125                                 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1126                                 AC_MEMCPY( bv.bv_val, "dn:", STRLENOF( "dn:" ) );
1127                                 AC_MEMCPY( &bv.bv_val[ STRLENOF( "dn:" ) ], be->be_rootndn.bv_val, be->be_rootndn.bv_len + 1 );
1128
1129                                 ber_bvarray_add( &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert_authz, &bv );
1130                         }
1131
1132                         return rc;
1133                 }
1134
1135         /* password to use as pseudo-root */
1136         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
1137                 int             i = mi->mi_ntargets - 1;
1138
1139                 if ( i < 0 ) {
1140                         Debug( LDAP_DEBUG_ANY,
1141         "%s: line %d: need \"uri\" directive first\n",
1142                                 fname, lineno, 0 );
1143                         return 1;
1144                 }
1145                 
1146                 if ( argc != 2 ) {
1147                         Debug( LDAP_DEBUG_ANY,
1148         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
1149                             fname, lineno, 0 );
1150                         return 1;
1151                 }
1152
1153                 Debug( LDAP_DEBUG_ANY,
1154                         "%s: line %d: \"pseudorootdn\", \"pseudorootpw\" are no longer supported; "
1155                         "use \"idassert-bind\" and \"idassert-authzFrom\" instead.\n",
1156                         fname, lineno, 0 );
1157
1158                 if ( BER_BVISNULL( &mi->mi_targets[ i ]->mt_idassert_authcDN ) ) {
1159                         Debug( LDAP_DEBUG_ANY, "%s: line %d: \"pseudorootpw\": \"pseudorootdn\" must be defined first.\n",
1160                                 fname, lineno, 0 );
1161                         return 1;
1162                 }
1163
1164                 if ( !BER_BVISNULL( &mi->mi_targets[ i ]->mt_idassert_passwd ) ) {
1165                         memset( mi->mi_targets[ i ]->mt_idassert_passwd.bv_val, 0,
1166                                 mi->mi_targets[ i ]->mt_idassert_passwd.bv_len );
1167                         ber_memfree( mi->mi_targets[ i ]->mt_idassert_passwd.bv_val );
1168                 }
1169                 ber_str2bv( argv[ 1 ], 0, 1, &mi->mi_targets[ i ]->mt_idassert_passwd );
1170
1171         /* idassert-bind */
1172         } else if ( strcasecmp( argv[ 0 ], "idassert-bind" ) == 0 ) {
1173                 if ( mi->mi_ntargets == 0 ) {
1174                         Debug( LDAP_DEBUG_ANY,
1175                                 "%s: line %d: \"idassert-bind\" "
1176                                 "must appear inside a target specification.\n",
1177                                 fname, lineno, 0 );
1178                         return 1;
1179                 }
1180
1181                 return mi->mi_ldap_extra->idassert_parse_cf( fname, lineno, argc, argv, &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert );
1182
1183         /* idassert-authzFrom */
1184         } else if ( strcasecmp( argv[ 0 ], "idassert-authzFrom" ) == 0 ) {
1185                 if ( mi->mi_ntargets == 0 ) {
1186                         Debug( LDAP_DEBUG_ANY,
1187                                 "%s: line %d: \"idassert-bind\" "
1188                                 "must appear inside a target specification.\n",
1189                                 fname, lineno, 0 );
1190                         return 1;
1191                 }
1192
1193                 switch ( argc ) {
1194                 case 2:
1195                         break;
1196
1197                 case 1:
1198                         Debug( LDAP_DEBUG_ANY,
1199                                 "%s: line %d: missing <id> in \"idassert-authzFrom <id>\".\n",
1200                                 fname, lineno, 0 );
1201                         return 1;
1202
1203                 default:
1204                         Debug( LDAP_DEBUG_ANY,
1205                                 "%s: line %d: extra cruft after <id> in \"idassert-authzFrom <id>\".\n",
1206                                 fname, lineno, 0 );
1207                         return 1;
1208                 }
1209
1210                 return mi->mi_ldap_extra->idassert_authzfrom_parse_cf( fname, lineno, argv[ 1 ], &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_idassert );
1211
1212         /* quarantine */
1213         } else if ( strcasecmp( argv[ 0 ], "quarantine" ) == 0 ) {
1214                 char                    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1215                 slap_retry_info_t       *ri = mi->mi_ntargets ?
1216                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_quarantine
1217                                 : &mi->mi_quarantine;
1218
1219                 if ( ( mi->mi_ntargets == 0 && META_BACK_QUARANTINE( mi ) )
1220                         || ( mi->mi_ntargets > 0 && META_BACK_TGT_QUARANTINE( mi->mi_targets[ mi->mi_ntargets - 1 ] ) ) )
1221                 {
1222                         Debug( LDAP_DEBUG_ANY,
1223                                 "%s: line %d: quarantine already defined.\n",
1224                                 fname, lineno, 0 );
1225                         return 1;
1226                 }
1227
1228                 switch ( argc ) {
1229                 case 2:
1230                         break;
1231
1232                 case 1:
1233                         Debug( LDAP_DEBUG_ANY,
1234                                 "%s: line %d: missing arg in \"quarantine <pattern list>\".\n",
1235                                 fname, lineno, 0 );
1236                         return 1;
1237
1238                 default:
1239                         Debug( LDAP_DEBUG_ANY,
1240                                 "%s: line %d: extra cruft after \"quarantine <pattern list>\".\n",
1241                                 fname, lineno, 0 );
1242                         return 1;
1243                 }
1244
1245                 if ( ri != &mi->mi_quarantine ) {
1246                         ri->ri_interval = NULL;
1247                         ri->ri_num = NULL;
1248                 }
1249
1250                 if ( mi->mi_ntargets > 0 && !META_BACK_QUARANTINE( mi ) ) {
1251                         ldap_pvt_thread_mutex_init( &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_quarantine_mutex );
1252                 }
1253
1254                 if ( mi->mi_ldap_extra->retry_info_parse( argv[ 1 ], ri, buf, sizeof( buf ) ) ) {
1255                         Debug( LDAP_DEBUG_ANY,
1256                                 "%s line %d: %s.\n",
1257                                 fname, lineno, buf );
1258                         return 1;
1259                 }
1260
1261                 if ( mi->mi_ntargets == 0 ) {
1262                         mi->mi_flags |= LDAP_BACK_F_QUARANTINE;
1263
1264                 } else {
1265                         mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags |= LDAP_BACK_F_QUARANTINE;
1266                 }
1267
1268 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1269         /* session tracking request */
1270         } else if ( strcasecmp( argv[ 0 ], "session-tracking-request" ) == 0 ) {
1271                 unsigned        *flagsp = mi->mi_ntargets ?
1272                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
1273                                 : &mi->mi_flags;
1274
1275                 if ( argc != 2 ) {
1276                         Debug( LDAP_DEBUG_ANY,
1277         "%s: line %d: \"session-tracking-request {TRUE|false}\" needs 1 argument.\n",
1278                                 fname, lineno, 0 );
1279                         return( 1 );
1280                 }
1281
1282                 /* this is the default; we add it because the default might change... */
1283                 switch ( check_true_false( argv[ 1 ] ) ) {
1284                 case 1:
1285                         *flagsp |= LDAP_BACK_F_ST_REQUEST;
1286                         break;
1287
1288                 case 0:
1289                         *flagsp &= ~LDAP_BACK_F_ST_REQUEST;
1290                         break;
1291
1292                 default:
1293                         Debug( LDAP_DEBUG_ANY,
1294                 "%s: line %d: \"session-tracking-request {TRUE|false}\": unknown argument \"%s\".\n",
1295                                 fname, lineno, argv[ 1 ] );
1296                         return( 1 );
1297                 }
1298 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
1299         
1300         /* dn massaging */
1301         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
1302                 BackendDB       *tmp_bd;
1303                 int             i = mi->mi_ntargets - 1, c, rc;
1304                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
1305
1306                 if ( i < 0 ) {
1307                         Debug( LDAP_DEBUG_ANY,
1308         "%s: line %d: need \"uri\" directive first\n",
1309                                 fname, lineno, 0 );
1310                         return 1;
1311                 }
1312                 
1313                 /*
1314                  * syntax:
1315                  * 
1316                  *      suffixmassage <suffix> <massaged suffix>
1317                  *
1318                  * the <suffix> field must be defined as a valid suffix
1319                  * (or suffixAlias?) for the current database;
1320                  * the <massaged suffix> shouldn't have already been
1321                  * defined as a valid suffix or suffixAlias for the 
1322                  * current server
1323                  */
1324                 if ( argc != 3 ) {
1325                         Debug( LDAP_DEBUG_ANY,
1326         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
1327                                 fname, lineno, 0 );
1328                         return 1;
1329                 }
1330
1331                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
1332                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1333                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1334                                         "suffix \"%s\" is invalid\n",
1335                                         fname, lineno, argv[ 1 ] );
1336                         return 1;
1337                 }
1338
1339                 for ( c = 0; !BER_BVISNULL( &be->be_nsuffix[ c ] ); c++ ) {
1340                         if ( dnIsSuffix( &nvnc, &be->be_nsuffix[ 0 ] ) ) {
1341                                 break;
1342                         }
1343                 }
1344
1345                 if ( BER_BVISNULL( &be->be_nsuffix[ c ] ) ) {
1346                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1347         "<suffix> \"%s\" must be within the database naming context, in "
1348         "\"suffixMassage <suffix> <massaged suffix>\"\n",
1349                                 fname, lineno, pvnc.bv_val );
1350                         free( pvnc.bv_val );
1351                         free( nvnc.bv_val );
1352                         return 1;                                               
1353                 }
1354
1355                 ber_str2bv( argv[ 2 ], 0, 0, &dn );
1356                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1357                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1358                                 "massaged suffix \"%s\" is invalid\n",
1359                                 fname, lineno, argv[ 2 ] );
1360                         free( pvnc.bv_val );
1361                         free( nvnc.bv_val );
1362                         return 1;
1363                 }
1364         
1365                 tmp_bd = select_backend( &nrnc, 0 );
1366                 if ( tmp_bd != NULL && tmp_bd->be_private == be->be_private ) {
1367                         Debug( LDAP_DEBUG_ANY, 
1368         "%s: line %d: warning: <massaged suffix> \"%s\" resolves to this database, in "
1369         "\"suffixMassage <suffix> <massaged suffix>\"\n",
1370                                 fname, lineno, prnc.bv_val );
1371                 }
1372
1373                 /*
1374                  * The suffix massaging is emulated by means of the
1375                  * rewrite capabilities
1376                  */
1377                 rc = suffix_massage_config( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1378                                 &pvnc, &nvnc, &prnc, &nrnc );
1379
1380                 free( pvnc.bv_val );
1381                 free( nvnc.bv_val );
1382                 free( prnc.bv_val );
1383                 free( nrnc.bv_val );
1384
1385                 return rc;
1386                 
1387         /* rewrite stuff ... */
1388         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
1389                 int             i = mi->mi_ntargets - 1;
1390
1391                 if ( i < 0 ) {
1392                         Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
1393                                 "statement outside target definition.\n",
1394                                 fname, lineno, 0 );
1395                         return 1;
1396                 }
1397                 
1398                 return rewrite_parse( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1399                                 fname, lineno, argc, argv );
1400
1401         /* objectclass/attribute mapping */
1402         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
1403                 int             i = mi->mi_ntargets - 1;
1404
1405                 if ( i < 0 ) {
1406                         Debug( LDAP_DEBUG_ANY,
1407         "%s: line %d: need \"uri\" directive first\n",
1408                                 fname, lineno, 0 );
1409                         return 1;
1410                 }
1411
1412                 return ldap_back_map_config( &mi->mi_targets[ i ]->mt_rwmap.rwm_oc, 
1413                                 &mi->mi_targets[ i ]->mt_rwmap.rwm_at,
1414                                 fname, lineno, argc, argv );
1415
1416         } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
1417                 int             i = mi->mi_ntargets - 1;
1418                 int             nretries = META_RETRY_UNDEFINED;
1419
1420                 if ( argc != 2 ) {
1421                         Debug( LDAP_DEBUG_ANY,
1422         "%s: line %d: need value in \"nretries <value>\"\n",
1423                                 fname, lineno, 0 );
1424                         return 1;
1425                 }
1426
1427                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
1428                         nretries = META_RETRY_FOREVER;
1429
1430                 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
1431                         nretries = META_RETRY_NEVER;
1432
1433                 } else {
1434                         if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
1435                                 Debug( LDAP_DEBUG_ANY,
1436         "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
1437                                         fname, lineno, argv[ 1 ] );
1438                                 return 1;
1439                         }
1440                 }
1441
1442                 if ( i < 0 ) {
1443                         mi->mi_nretries = nretries;
1444
1445                 } else {
1446                         mi->mi_targets[ i ]->mt_nretries = nretries;
1447                 }
1448
1449         } else if ( strcasecmp( argv[ 0 ], "protocol-version" ) == 0 ) {
1450                 int     *version = mi->mi_ntargets ?
1451                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_version
1452                                 : &mi->mi_version;
1453
1454                 if ( argc != 2 ) {
1455                         Debug( LDAP_DEBUG_ANY,
1456         "%s: line %d: need value in \"protocol-version <version>\"\n",
1457                                 fname, lineno, 0 );
1458                         return 1;
1459                 }
1460
1461                 if ( lutil_atoi( version, argv[ 1 ] ) != 0 ) {
1462                         Debug( LDAP_DEBUG_ANY,
1463         "%s: line %d: unable to parse version \"%s\" in \"protocol-version <version>\"\n",
1464                                 fname, lineno, argv[ 1 ] );
1465                         return 1;
1466                 }
1467
1468                 if ( *version != 0 && ( *version < LDAP_VERSION_MIN || *version > LDAP_VERSION_MAX ) ) {
1469                         Debug( LDAP_DEBUG_ANY,
1470         "%s: line %d: unsupported version \"%s\" in \"protocol-version <version>\"\n",
1471                                 fname, lineno, argv[ 1 ] );
1472                         return 1;
1473                 }
1474
1475         /* do not return search references */
1476         } else if ( strcasecmp( argv[ 0 ], "norefs" ) == 0 ) {
1477                 unsigned        *flagsp = mi->mi_ntargets ?
1478                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
1479                                 : &mi->mi_flags;
1480
1481                 if ( argc != 2 ) {
1482                         Debug( LDAP_DEBUG_ANY,
1483         "%s: line %d: \"norefs {TRUE|false}\" needs 1 argument.\n",
1484                                 fname, lineno, 0 );
1485                         return( 1 );
1486                 }
1487
1488                 /* this is the default; we add it because the default might change... */
1489                 switch ( check_true_false( argv[ 1 ] ) ) {
1490                 case 1:
1491                         *flagsp |= LDAP_BACK_F_NOREFS;
1492                         break;
1493
1494                 case 0:
1495                         *flagsp &= ~LDAP_BACK_F_NOREFS;
1496                         break;
1497
1498                 default:
1499                         Debug( LDAP_DEBUG_ANY,
1500                 "%s: line %d: \"norefs {TRUE|false}\": unknown argument \"%s\".\n",
1501                                 fname, lineno, argv[ 1 ] );
1502                         return( 1 );
1503                 }
1504
1505         /* do not propagate undefined search filters */
1506         } else if ( strcasecmp( argv[ 0 ], "noundeffilter" ) == 0 ) {
1507                 unsigned        *flagsp = mi->mi_ntargets ?
1508                                 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
1509                                 : &mi->mi_flags;
1510
1511                 if ( argc != 2 ) {
1512                         Debug( LDAP_DEBUG_ANY,
1513         "%s: line %d: \"noundeffilter {TRUE|false}\" needs 1 argument.\n",
1514                                 fname, lineno, 0 );
1515                         return( 1 );
1516                 }
1517
1518                 /* this is the default; we add it because the default might change... */
1519                 switch ( check_true_false( argv[ 1 ] ) ) {
1520                 case 1:
1521                         *flagsp |= LDAP_BACK_F_NOUNDEFFILTER;
1522                         break;
1523
1524                 case 0:
1525                         *flagsp &= ~LDAP_BACK_F_NOUNDEFFILTER;
1526                         break;
1527
1528                 default:
1529                         Debug( LDAP_DEBUG_ANY,
1530                 "%s: line %d: \"noundeffilter {TRUE|false}\": unknown argument \"%s\".\n",
1531                                 fname, lineno, argv[ 1 ] );
1532                         return( 1 );
1533                 }
1534
1535         /* anything else */
1536         } else {
1537                 return SLAP_CONF_UNKNOWN;
1538         }
1539         return 0;
1540 }
1541
1542 int
1543 ldap_back_map_config(
1544                 struct ldapmap  *oc_map,
1545                 struct ldapmap  *at_map,
1546                 const char      *fname,
1547                 int             lineno,
1548                 int             argc,
1549                 char            **argv )
1550 {
1551         struct ldapmap          *map;
1552         struct ldapmapping      *mapping;
1553         char                    *src, *dst;
1554         int                     is_oc = 0;
1555
1556         if ( argc < 3 || argc > 4 ) {
1557                 Debug( LDAP_DEBUG_ANY,
1558         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
1559                         fname, lineno, 0 );
1560                 return 1;
1561         }
1562
1563         if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
1564                 map = oc_map;
1565                 is_oc = 1;
1566
1567         } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
1568                 map = at_map;
1569
1570         } else {
1571                 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
1572                         "\"map {objectclass | attribute} [<local> | *] "
1573                         "{<foreign> | *}\"\n",
1574                         fname, lineno, 0 );
1575                 return 1;
1576         }
1577
1578         if ( !is_oc && map->map == NULL ) {
1579                 /* only init if required */
1580                 ldap_back_map_init( map, &mapping );
1581         }
1582
1583         if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
1584                 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
1585                         map->drop_missing = ( argc < 4 );
1586                         goto success_return;
1587                 }
1588                 src = dst = argv[ 3 ];
1589
1590         } else if ( argc < 4 ) {
1591                 src = "";
1592                 dst = argv[ 2 ];
1593
1594         } else {
1595                 src = argv[ 2 ];
1596                 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1597         }
1598
1599         if ( ( map == at_map )
1600                 && ( strcasecmp( src, "objectclass" ) == 0
1601                         || strcasecmp( dst, "objectclass" ) == 0 ) )
1602         {
1603                 Debug( LDAP_DEBUG_ANY,
1604                         "%s: line %d: objectclass attribute cannot be mapped\n",
1605                         fname, lineno, 0 );
1606         }
1607
1608         mapping = (struct ldapmapping *)ch_calloc( 2,
1609                 sizeof(struct ldapmapping) );
1610         if ( mapping == NULL ) {
1611                 Debug( LDAP_DEBUG_ANY,
1612                         "%s: line %d: out of memory\n",
1613                         fname, lineno, 0 );
1614                 return 1;
1615         }
1616         ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1617         ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1618         mapping[ 1 ].src = mapping[ 0 ].dst;
1619         mapping[ 1 ].dst = mapping[ 0 ].src;
1620
1621         /*
1622          * schema check
1623          */
1624         if ( is_oc ) {
1625                 if ( src[ 0 ] != '\0' ) {
1626                         if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1627                                 Debug( LDAP_DEBUG_ANY,
1628         "%s: line %d: warning, source objectClass '%s' "
1629         "should be defined in schema\n",
1630                                         fname, lineno, src );
1631
1632                                 /*
1633                                  * FIXME: this should become an err
1634                                  */
1635                                 goto error_return;
1636                         }
1637                 }
1638
1639                 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1640                         Debug( LDAP_DEBUG_ANY,
1641         "%s: line %d: warning, destination objectClass '%s' "
1642         "is not defined in schema\n",
1643                                 fname, lineno, dst );
1644                 }
1645         } else {
1646                 int                     rc;
1647                 const char              *text = NULL;
1648                 AttributeDescription    *ad = NULL;
1649
1650                 if ( src[ 0 ] != '\0' ) {
1651                         rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1652                         if ( rc != LDAP_SUCCESS ) {
1653                                 Debug( LDAP_DEBUG_ANY,
1654         "%s: line %d: warning, source attributeType '%s' "
1655         "should be defined in schema\n",
1656                                         fname, lineno, src );
1657
1658                                 /*
1659                                  * FIXME: this should become an err
1660                                  */
1661                                 /*
1662                                  * we create a fake "proxied" ad 
1663                                  * and add it here.
1664                                  */
1665
1666                                 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1667                                                 &ad, &text, SLAP_AD_PROXIED );
1668                                 if ( rc != LDAP_SUCCESS ) {
1669                                         char    buf[ SLAP_TEXT_BUFLEN ];
1670
1671                                         snprintf( buf, sizeof( buf ),
1672                                                 "source attributeType \"%s\": %d (%s)",
1673                                                 src, rc, text ? text : "" );
1674                                         Debug( LDAP_DEBUG_ANY,
1675                                                 "%s: line %d: %s\n",
1676                                                 fname, lineno, buf );
1677                                         goto error_return;
1678                                 }
1679                         }
1680
1681                         ad = NULL;
1682                 }
1683
1684                 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1685                 if ( rc != LDAP_SUCCESS ) {
1686                         Debug( LDAP_DEBUG_ANY,
1687         "%s: line %d: warning, destination attributeType '%s' "
1688         "is not defined in schema\n",
1689                                 fname, lineno, dst );
1690
1691                         /*
1692                          * we create a fake "proxied" ad 
1693                          * and add it here.
1694                          */
1695
1696                         rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1697                                         &ad, &text, SLAP_AD_PROXIED );
1698                         if ( rc != LDAP_SUCCESS ) {
1699                                 char    buf[ SLAP_TEXT_BUFLEN ];
1700
1701                                 snprintf( buf, sizeof( buf ),
1702                                         "source attributeType \"%s\": %d (%s)\n",
1703                                         dst, rc, text ? text : "" );
1704                                 Debug( LDAP_DEBUG_ANY,
1705                                         "%s: line %d: %s\n",
1706                                         fname, lineno, buf );
1707                                 return 1;
1708                         }
1709                 }
1710         }
1711
1712         if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1713                         || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1714         {
1715                 Debug( LDAP_DEBUG_ANY,
1716                         "%s: line %d: duplicate mapping found.\n",
1717                         fname, lineno, 0 );
1718                 goto error_return;
1719         }
1720
1721         if ( src[ 0 ] != '\0' ) {
1722                 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1723                                         mapping_cmp, mapping_dup );
1724         }
1725         avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1726                                 mapping_cmp, mapping_dup );
1727
1728 success_return:;
1729         return 0;
1730
1731 error_return:;
1732         if ( mapping ) {
1733                 ch_free( mapping[ 0 ].src.bv_val );
1734                 ch_free( mapping[ 0 ].dst.bv_val );
1735                 ch_free( mapping );
1736         }
1737
1738         return 1;
1739 }
1740
1741
1742 #ifdef ENABLE_REWRITE
1743 static char *
1744 suffix_massage_regexize( const char *s )
1745 {
1746         char *res, *ptr;
1747         const char *p, *r;
1748         int i;
1749
1750         if ( s[ 0 ] == '\0' ) {
1751                 return ch_strdup( "^(.+)$" );
1752         }
1753
1754         for ( i = 0, p = s; 
1755                         ( r = strchr( p, ',' ) ) != NULL; 
1756                         p = r + 1, i++ )
1757                 ;
1758
1759         res = ch_calloc( sizeof( char ),
1760                         strlen( s )
1761                         + STRLENOF( "((.+),)?" )
1762                         + STRLENOF( "[ ]?" ) * i
1763                         + STRLENOF( "$" ) + 1 );
1764
1765         ptr = lutil_strcopy( res, "((.+),)?" );
1766         for ( i = 0, p = s;
1767                         ( r = strchr( p, ',' ) ) != NULL;
1768                         p = r + 1 , i++ ) {
1769                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1770                 ptr = lutil_strcopy( ptr, "[ ]?" );
1771
1772                 if ( r[ 1 ] == ' ' ) {
1773                         r++;
1774                 }
1775         }
1776         ptr = lutil_strcopy( ptr, p );
1777         ptr[ 0 ] = '$';
1778         ptr++;
1779         ptr[ 0 ] = '\0';
1780
1781         return res;
1782 }
1783
1784 static char *
1785 suffix_massage_patternize( const char *s, const char *p )
1786 {
1787         ber_len_t       len;
1788         char            *res, *ptr;
1789
1790         len = strlen( p );
1791
1792         if ( s[ 0 ] == '\0' ) {
1793                 len++;
1794         }
1795
1796         res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1797         if ( res == NULL ) {
1798                 return NULL;
1799         }
1800
1801         ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1802         if ( s[ 0 ] == '\0' ) {
1803                 ptr[ 0 ] = ',';
1804                 ptr++;
1805         }
1806         lutil_strcopy( ptr, p );
1807
1808         return res;
1809 }
1810
1811 int
1812 suffix_massage_config( 
1813                 struct rewrite_info *info,
1814                 struct berval *pvnc,
1815                 struct berval *nvnc,
1816                 struct berval *prnc,
1817                 struct berval *nrnc
1818 )
1819 {
1820         char *rargv[ 5 ];
1821         int line = 0;
1822
1823         rargv[ 0 ] = "rewriteEngine";
1824         rargv[ 1 ] = "on";
1825         rargv[ 2 ] = NULL;
1826         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1827
1828         rargv[ 0 ] = "rewriteContext";
1829         rargv[ 1 ] = "default";
1830         rargv[ 2 ] = NULL;
1831         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1832
1833         rargv[ 0 ] = "rewriteRule";
1834         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1835         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1836         rargv[ 3 ] = ":";
1837         rargv[ 4 ] = NULL;
1838         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1839         ch_free( rargv[ 1 ] );
1840         ch_free( rargv[ 2 ] );
1841
1842         if ( BER_BVISEMPTY( pvnc ) ) {
1843                 rargv[ 0 ] = "rewriteRule";
1844                 rargv[ 1 ] = "^$";
1845                 rargv[ 2 ] = prnc->bv_val;
1846                 rargv[ 3 ] = ":";
1847                 rargv[ 4 ] = NULL;
1848                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1849         }
1850         
1851         rargv[ 0 ] = "rewriteContext";
1852         rargv[ 1 ] = "searchEntryDN";
1853         rargv[ 2 ] = NULL;
1854         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1855
1856         rargv[ 0 ] = "rewriteRule";
1857         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1858         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1859         rargv[ 3 ] = ":";
1860         rargv[ 4 ] = NULL;
1861         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1862         ch_free( rargv[ 1 ] );
1863         ch_free( rargv[ 2 ] );
1864
1865         if ( BER_BVISEMPTY( prnc ) ) {
1866                 rargv[ 0 ] = "rewriteRule";
1867                 rargv[ 1 ] = "^$";
1868                 rargv[ 2 ] = pvnc->bv_val;
1869                 rargv[ 3 ] = ":";
1870                 rargv[ 4 ] = NULL;
1871                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1872         }
1873         
1874         /* backward compatibility */
1875         rargv[ 0 ] = "rewriteContext";
1876         rargv[ 1 ] = "searchResult";
1877         rargv[ 2 ] = "alias";
1878         rargv[ 3 ] = "searchEntryDN";
1879         rargv[ 4 ] = NULL;
1880         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1881         
1882         rargv[ 0 ] = "rewriteContext";
1883         rargv[ 1 ] = "matchedDN";
1884         rargv[ 2 ] = "alias";
1885         rargv[ 3 ] = "searchEntryDN";
1886         rargv[ 4 ] = NULL;
1887         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1888
1889         rargv[ 0 ] = "rewriteContext";
1890         rargv[ 1 ] = "searchAttrDN";
1891         rargv[ 2 ] = "alias";
1892         rargv[ 3 ] = "searchEntryDN";
1893         rargv[ 4 ] = NULL;
1894         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1895
1896         /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1897          * see servers/slapd/overlays/rwm.h for details */
1898         rargv[ 0 ] = "rewriteContext";
1899         rargv[ 1 ] = "referralAttrDN";
1900         rargv[ 2 ] = NULL;
1901         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1902
1903         rargv[ 0 ] = "rewriteContext";
1904         rargv[ 1 ] = "referralDN";
1905         rargv[ 2 ] = NULL;
1906         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1907         
1908         return 0;
1909 }
1910 #endif /* ENABLE_REWRITE */
1911