]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
1cff2f1cc22872ff1666b39d4d28495d9ce2434a
[openldap] / servers / slapd / back-ldap / config.c
1 /* config.c - ldap backend configuration file routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2005 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/ctype.h>
30 #include <ac/socket.h>
31
32 #include "slap.h"
33 #include "config.h"
34 #include "back-ldap.h"
35 #include "lutil.h"
36 #include "ldif.h"
37 #undef ldap_debug
38 /* for advanced URL parsing */
39 #include "../../../libraries/libldap/ldap-int.h"
40
41 static SLAP_EXTOP_MAIN_FN ldap_back_exop_whoami;
42
43 static ConfigDriver ldap_back_cf_gen;
44
45 enum {
46         LDAP_BACK_CFG_URI = 1,
47         LDAP_BACK_CFG_TLS,
48         LDAP_BACK_CFG_ACL_AUTHCDN,
49         LDAP_BACK_CFG_ACL_PASSWD,
50         LDAP_BACK_CFG_ACL_METHOD,
51         LDAP_BACK_CFG_ACL_BIND,
52         LDAP_BACK_CFG_IDASSERT_MODE,
53         LDAP_BACK_CFG_IDASSERT_AUTHCDN,
54         LDAP_BACK_CFG_IDASSERT_PASSWD,
55         LDAP_BACK_CFG_IDASSERT_AUTHZFROM,
56         LDAP_BACK_CFG_IDASSERT_METHOD,
57         LDAP_BACK_CFG_IDASSERT_BIND,
58         LDAP_BACK_CFG_REBIND,
59         LDAP_BACK_CFG_CHASE,
60         LDAP_BACK_CFG_T_F,
61         LDAP_BACK_CFG_WHOAMI,
62         LDAP_BACK_CFG_REWRITE
63 };
64
65 static ConfigTable ldapcfg[] = {
66         { "uri", "uri", 2, 2, 0,
67                 ARG_STRING|ARG_MAGIC|LDAP_BACK_CFG_URI,
68                 ldap_back_cf_gen, "( OLcfgDbAt:0.14 "
69                         "NAME 'olcDbURI' "
70                         "DESC 'URI (list) for remote DSA' "
71                         "SYNTAX OMsDirectoryString "
72                         "SINGLE-VALUE )",
73                 NULL, NULL },
74         { "tls", "what", 2, 2, 0,
75                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_TLS,
76                 ldap_back_cf_gen, "( OLcfgDbAt:3.1 "
77                         "NAME 'olcDbStartTLS' "
78                         "DESC 'StartTLS' "
79                         "SYNTAX OMsDirectoryString "
80                         "SINGLE-VALUE )",
81                 NULL, NULL },
82         { "acl-authcDN", "DN", 2, 2, 0,
83                 ARG_DN|ARG_MAGIC|LDAP_BACK_CFG_ACL_AUTHCDN,
84                 ldap_back_cf_gen, "( OLcfgDbAt:3.2 "
85                         "NAME 'olcDbACLAuthcDn' "
86                         "DESC 'Remote ACL administrative identity' "
87                         "OBSOLETE "
88                         "SYNTAX OMsDN "
89                         "SINGLE-VALUE )",
90                 NULL, NULL },
91         /* deprecated, will be removed; aliases "acl-authcDN" */
92         { "binddn", "DN", 2, 2, 0,
93                 ARG_DN|ARG_MAGIC|LDAP_BACK_CFG_ACL_AUTHCDN,
94                 ldap_back_cf_gen, NULL, NULL, NULL },
95         { "acl-passwd", "cred", 2, 2, 0,
96                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_ACL_PASSWD,
97                 ldap_back_cf_gen, "( OLcfgDbAt:3.3 "
98                         "NAME 'olcDbACLPasswd' "
99                         "DESC 'Remote ACL administrative identity credentials' "
100                         "OBSOLETE "
101                         "SYNTAX OMsDirectoryString "
102                         "SINGLE-VALUE )",
103                 NULL, NULL },
104         /* deprecated, will be removed; aliases "acl-passwd" */
105         { "bindpw", "cred", 2, 2, 0,
106                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_ACL_PASSWD,
107                 ldap_back_cf_gen, NULL, NULL, NULL },
108         /* deprecated, will be removed; aliases "acl-bind" */
109         { "acl-method", "args", 2, 0, 0,
110                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_ACL_METHOD,
111                 ldap_back_cf_gen, NULL, NULL, NULL },
112         { "acl-bind", "args", 2, 0, 0,
113                 ARG_MAGIC|LDAP_BACK_CFG_ACL_BIND,
114                 ldap_back_cf_gen, "( OLcfgDbAt:3.4 "
115                         "NAME 'olcDbACLBind' "
116                         "DESC 'Remote ACL administrative identity auth bind configuration' "
117                         "SYNTAX OMsDirectoryString "
118                         "SINGLE-VALUE )",
119                 NULL, NULL },
120         { "idassert-authcDN", "DN", 2, 2, 0,
121                 ARG_DN|ARG_MAGIC|LDAP_BACK_CFG_IDASSERT_AUTHCDN,
122                 ldap_back_cf_gen, "( OLcfgDbAt:3.5 "
123                         "NAME 'olcDbIDAssertAuthcDn' "
124                         "DESC 'Remote Identity Assertion administrative identity' "
125                         "OBSOLETE "
126                         "SYNTAX OMsDN "
127                         "SINGLE-VALUE )",
128                 NULL, NULL },
129         /* deprecated, will be removed; partially aliases "idassert-authcDN" */
130         { "proxyauthzdn", "DN", 2, 2, 0,
131                 ARG_DN|ARG_MAGIC|LDAP_BACK_CFG_IDASSERT_AUTHCDN,
132                 ldap_back_cf_gen, NULL, NULL, NULL },
133         { "idassert-passwd", "cred", 2, 2, 0,
134                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_IDASSERT_PASSWD,
135                 ldap_back_cf_gen, "( OLcfgDbAt:3.6 "
136                         "NAME 'olcDbIDAssertPasswd' "
137                         "DESC 'Remote Identity Assertion administrative identity credentials' "
138                         "OBSOLETE "
139                         "SYNTAX OMsDirectoryString "
140                         "SINGLE-VALUE )",
141                 NULL, NULL },
142         /* deprecated, will be removed; partially aliases "idassert-passwd" */
143         { "proxyauthzpw", "cred", 2, 2, 0,
144                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_IDASSERT_PASSWD,
145                 ldap_back_cf_gen, NULL, NULL, NULL },
146         { "idassert-bind", "args", 2, 0, 0,
147                 ARG_MAGIC|LDAP_BACK_CFG_IDASSERT_BIND,
148                 ldap_back_cf_gen, "( OLcfgDbAt:3.7 "
149                         "NAME 'olcDbIDAssertBind' "
150                         "DESC 'Remote Identity Assertion administrative identity auth bind configuration' "
151                         "SYNTAX OMsDirectoryString "
152                         "SINGLE-VALUE )",
153                 NULL, NULL },
154         { "idassert-method", "args", 2, 0, 0,
155                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_IDASSERT_METHOD,
156                 ldap_back_cf_gen, NULL, NULL, NULL },
157         { "idassert-mode", "mode>|u:<user>|[dn:]<DN", 2, 0, 0,
158                 ARG_STRING|ARG_MAGIC|LDAP_BACK_CFG_IDASSERT_MODE,
159                 ldap_back_cf_gen, "( OLcfgDbAt:3.8 "
160                         "NAME 'olcDbIDAssertMode' "
161                         "DESC 'Remote Identity Assertion mode' "
162                         "OBSOLETE "
163                         "SYNTAX OMsDirectoryString "
164                         "SINGLE-VALUE)",
165                 NULL, NULL },
166         { "idassert-authzFrom", "authzRule", 2, 2, 0,
167                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_IDASSERT_AUTHZFROM,
168                 ldap_back_cf_gen, "( OLcfgDbAt:3.9 "
169                         "NAME 'olcDbIDAssertAuthzFrom' "
170                         "DESC 'Remote Identity Assertion authz rules' "
171                         "SYNTAX OMsDirectoryString "
172                         "X-ORDERED 'VALUES' )",
173                 NULL, NULL },
174         { "rebind-as-user", "NO|yes", 1, 2, 0,
175                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_REBIND,
176                 ldap_back_cf_gen, "( OLcfgDbAt:3.10 "
177                         "NAME 'olcDbRebindAsUser' "
178                         "DESC 'Rebind as user' "
179                         "SYNTAX OMsDirectoryString "
180                         "SINGLE-VALUE )",
181                 NULL, NULL },
182         { "chase-referrals", "YES|no", 2, 2, 0,
183                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_CHASE,
184                 ldap_back_cf_gen, "( OLcfgDbAt:3.11 "
185                         "NAME 'olcDbChaseReferrals' "
186                         "DESC 'Chase referrals' "
187                         "SYNTAX OMsDirectoryString "
188                         "SINGLE-VALUE )",
189                 NULL, NULL },
190         { "t-f-support", "NO|yes|discover", 2, 2, 0,
191                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_T_F,
192                 ldap_back_cf_gen, "( OLcfgDbAt:3.12 "
193                         "NAME 'olcDbTFSupport' "
194                         "DESC 'Absolute filters support' "
195                         "SYNTAX OMsDirectoryString "
196                         "SINGLE-VALUE )",
197                 NULL, NULL },
198         { "proxy-whoami", "NO|yes", 1, 2, 0,
199                 ARG_BERVAL|ARG_MAGIC|LDAP_BACK_CFG_WHOAMI,
200                 ldap_back_cf_gen, "( OLcfgDbAt:3.13 "
201                         "NAME 'olcDbProxyWhoAmI' "
202                         "DESC 'Proxy whoAmI exop' "
203                         "SYNTAX OMsDirectoryString "
204                         "SINGLE-VALUE )",
205                 NULL, NULL },
206         { "suffixmassage", "[virtual]> <real", 2, 3, 0,
207                 ARG_STRING|ARG_MAGIC|LDAP_BACK_CFG_REWRITE,
208                 ldap_back_cf_gen, NULL, NULL, NULL },
209         { "map", "attribute|objectClass> [*|<local>] *|<remote", 3, 4, 0,
210                 ARG_STRING|ARG_MAGIC|LDAP_BACK_CFG_REWRITE,
211                 ldap_back_cf_gen, NULL, NULL, NULL },
212         { "rewrite", "<arglist>", 2, 4, STRLENOF( "rewrite" ),
213                 ARG_STRING|ARG_MAGIC|LDAP_BACK_CFG_REWRITE,
214                 ldap_back_cf_gen, NULL, NULL, NULL },
215         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
216                 NULL, NULL, NULL, NULL }
217 };
218
219 static ConfigOCs ldapocs[] = {
220         { "( OLcfgDbOc:3.1 "
221                 "NAME 'olcLDAPConfig' "
222                 "DESC 'LDAP backend configuration' "
223                 "SUP olcDatabaseConfig "
224                 "MUST olcDbURI "
225                 "MAY ( olcDbStartTLS "
226                         "$ olcDbACLAuthcDn "
227                         "$ olcDbACLPasswd "
228                         "$ olcDbACLBind "
229                         "$ olcDbIDAssertAuthcDn "
230                         "$ olcDbIDAssertPasswd "
231                         "$ olcDbIDAssertBind "
232                         "$ olcDbIDAssertMode "
233                         "$ olcDbIDAssertAuthzFrom "
234                         "$ olcDbRebindAsUser "
235                         "$ olcDbChaseReferrals "
236                         "$ olcDbTFSupport "
237                         "$ olcDbProxyWhoAmI "
238                 ") )",
239                         Cft_Database, ldapcfg},
240         { NULL, 0, NULL }
241 };
242
243 #define LDAP_BACK_C_NO                  (0x0U)
244 #define LDAP_BACK_C_YES                 (0x1U)
245 static slap_verbmasks yn_mode[] = {
246         { BER_BVC( "yes" ),             LDAP_BACK_C_YES},
247         { BER_BVC( "no" ),              LDAP_BACK_C_NO },
248         { BER_BVNULL,                   0 }
249 };
250
251 static slap_verbmasks idassert_mode[] = {
252         { BER_BVC("self"),              LDAP_BACK_IDASSERT_SELF },
253         { BER_BVC("anonymous"),         LDAP_BACK_IDASSERT_ANONYMOUS },
254         { BER_BVC("none"),              LDAP_BACK_IDASSERT_NOASSERT },
255         { BER_BVC("legacy"),            LDAP_BACK_IDASSERT_LEGACY },
256         { BER_BVNULL,                   0 }
257 };
258
259 static slap_verbmasks tls_mode[] = {
260         { BER_BVC( "propagate" ),       LDAP_BACK_F_TLS_PROPAGATE_MASK },
261         { BER_BVC( "try-propagate" ),   LDAP_BACK_F_PROPAGATE_TLS },
262         { BER_BVC( "start" ),           LDAP_BACK_F_TLS_USE_MASK },
263         { BER_BVC( "try-start" ),       LDAP_BACK_F_USE_TLS },
264         { BER_BVC( "none" ),            LDAP_BACK_C_NO },
265         { BER_BVNULL,                   0 }
266 };
267
268 static slap_verbmasks t_f_mode[] = {
269         { BER_BVC( "yes" ),             LDAP_BACK_F_SUPPORT_T_F },
270         { BER_BVC( "discover" ),        LDAP_BACK_F_SUPPORT_T_F_DISCOVER },
271         { BER_BVC( "no" ),              LDAP_BACK_C_NO },
272         { BER_BVNULL,                   0 }
273 };
274
275 static int
276 ldap_back_cf_gen( ConfigArgs *c )
277 {
278         struct ldapinfo *li = ( struct ldapinfo * )c->be->be_private;
279         int             rc;
280         int             i;
281
282         if ( c->op == SLAP_CONFIG_EMIT ) {
283                 struct berval   bv = BER_BVNULL;
284                 rc = 0;
285
286                 switch( c->type ) {
287                 case LDAP_BACK_CFG_URI:
288                         if ( li->url != NULL ) {
289                                 c->value_string = ch_strdup( li->url );
290
291                         } else {
292                                 rc = 1;
293                         }
294                         break;
295
296                 case LDAP_BACK_CFG_TLS:
297                         enum_to_verb( tls_mode, ( li->flags & LDAP_BACK_F_TLS_MASK ), &bv );
298                         if ( BER_BVISNULL( &bv ) ) {
299                                 /* there's something wrong... */
300                                 assert( 0 );
301                                 rc = 1;
302
303                         } else {
304                                 c->value_bv = bv;
305                         }
306                         break;
307
308                 case LDAP_BACK_CFG_ACL_AUTHCDN:
309                 case LDAP_BACK_CFG_ACL_PASSWD:
310                 case LDAP_BACK_CFG_ACL_METHOD:
311                         /* handled by LDAP_BACK_CFG_ACL_BIND */
312                         rc = 1;
313                         break;
314
315                 case LDAP_BACK_CFG_ACL_BIND: {
316                         int     i;
317
318                         bindconf_unparse( &li->acl_sb, &bv );
319
320                         for ( i = 0; isspace( bv.bv_val[ i ] ); i++ )
321                                 /* count spaces */ ;
322
323                         if ( i ) {
324                                 bv.bv_len -= i;
325                                 AC_MEMCPY( bv.bv_val, &bv.bv_val[ i ],
326                                         bv.bv_len + 1 );
327                         }
328
329                         ber_bvarray_add( &c->rvalue_vals, &bv );
330                         break;
331                 }
332
333                 case LDAP_BACK_CFG_IDASSERT_MODE:
334                 case LDAP_BACK_CFG_IDASSERT_AUTHCDN:
335                 case LDAP_BACK_CFG_IDASSERT_PASSWD:
336                 case LDAP_BACK_CFG_IDASSERT_METHOD:
337                         /* handled by LDAP_BACK_CFG_IDASSERT_BIND */
338                         rc = 1;
339                         break;
340
341                 case LDAP_BACK_CFG_IDASSERT_AUTHZFROM: {
342                         int             i;
343
344                         if ( li->idassert_authz == NULL ) {
345                                 rc = 1;
346                                 break;
347                         }
348
349                         for ( i = 0; !BER_BVISNULL( &li->idassert_authz[ i ] ); i++ )
350                         {
351                                 value_add_one( &c->rvalue_vals, &li->idassert_authz[ i ] );
352                         }
353                         break;
354                 }
355
356                 case LDAP_BACK_CFG_IDASSERT_BIND: {
357                         int             i;
358                         struct berval   bc = BER_BVNULL;
359                         char            *ptr;
360
361                         if ( li->idassert_authmethod != LDAP_AUTH_NONE ) {
362                                 ber_len_t       len;
363
364                                 switch ( li->idassert_mode ) {
365                                 case LDAP_BACK_IDASSERT_OTHERID:
366                                 case LDAP_BACK_IDASSERT_OTHERDN:
367                                         break;
368
369                                 default: {
370                                         struct berval   mode = BER_BVNULL;
371
372                                         enum_to_verb( idassert_mode, li->idassert_mode, &mode );
373                                         if ( BER_BVISNULL( &mode ) ) {
374                                                 /* there's something wrong... */
375                                                 assert( 0 );
376                                                 rc = 1;
377         
378                                         } else {
379                                                 bv.bv_len = STRLENOF( "mode=" ) + mode.bv_len;
380                                                 bv.bv_val = ch_malloc( bv.bv_len + 1 );
381
382                                                 ptr = lutil_strcopy( bv.bv_val, "mode=" );
383                                                 ptr = lutil_strcopy( ptr, mode.bv_val );
384                                         }
385                                         break;
386                                 }
387                                 }
388
389                                 if ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) {
390                                         len = bv.bv_len + STRLENOF( "authz=native" );
391
392                                         if ( !BER_BVISEMPTY( &bv ) ) {
393                                                 len += STRLENOF( " " );
394                                         }
395
396                                         bv.bv_val = ch_realloc( bv.bv_val, len + 1 );
397
398                                         ptr = &bv.bv_val[ bv.bv_len ];
399
400                                         if ( !BER_BVISEMPTY( &bv ) ) {
401                                                 ptr = lutil_strcopy( ptr, " " );
402                                         }
403
404                                         (void)lutil_strcopy( ptr, "authz=native" );
405                                 }
406
407                                 len = bv.bv_len + STRLENOF( "flags=non-prescriptive,override" );
408                                 /* flags */
409                                 if ( !BER_BVISEMPTY( &bv ) ) {
410                                         len += STRLENOF( " " );
411                                 }
412
413                                 bv.bv_val = ch_realloc( bv.bv_val, len + 1 );
414
415                                 ptr = &bv.bv_val[ bv.bv_len ];
416
417                                 if ( !BER_BVISEMPTY( &bv ) ) {
418                                         ptr = lutil_strcopy( ptr, " " );
419                                 }
420
421                                 ptr = lutil_strcopy( ptr, "flags=" );
422
423                                 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
424                                         ptr = lutil_strcopy( ptr, "prescriptive" );
425                                 } else {
426                                         ptr = lutil_strcopy( ptr, "non-prescriptive" );
427                                 }
428
429                                 if ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
430                                         ptr = lutil_strcopy( ptr, ",override" );
431                                 }
432
433                                 bv.bv_len = ( ptr - bv.bv_val );
434                                 /* end-of-flags */
435                         }
436
437                         bindconf_unparse( &li->idassert_sb, &bc );
438
439                         if ( !BER_BVISNULL( &bv ) ) {
440                                 ber_len_t       len = bv.bv_len + bc.bv_len;
441
442                                 bv.bv_val = ch_realloc( bv.bv_val, len + 1 );
443
444                                 assert( bc.bv_val[ 0 ] == ' ' );
445
446                                 ptr = lutil_strcopy( &bv.bv_val[ bv.bv_len ], bc.bv_val );
447                                 free( bc.bv_val );
448                                 bv.bv_len = ptr - bv.bv_val;
449
450                         } else {
451                                 for ( i = 0; isspace( bc.bv_val[ i ] ); i++ )
452                                         /* count spaces */ ;
453
454                                 if ( i ) {
455                                         bc.bv_len -= i;
456                                         AC_MEMCPY( bc.bv_val, &bc.bv_val[ i ], bc.bv_len + 1 );
457                                 }
458
459                                 bv = bc;
460                         }
461                         
462                         ber_bvarray_add( &c->rvalue_vals, &bv );
463
464                         break;
465                 }
466
467                 case LDAP_BACK_CFG_REBIND:
468                         enum_to_verb( yn_mode, ( ( li->flags & LDAP_BACK_F_SAVECRED ) == LDAP_BACK_F_SAVECRED ), &bv );
469                         if ( BER_BVISNULL( &bv ) ) {
470                                 /* there's something wrong... */
471                                 assert( 0 );
472                                 rc = 1;
473
474                         } else {
475                                 c->value_bv = bv;
476                         }
477                         break;
478
479                 case LDAP_BACK_CFG_CHASE:
480                         enum_to_verb( yn_mode, ( ( li->flags & LDAP_BACK_F_CHASE_REFERRALS ) == LDAP_BACK_F_CHASE_REFERRALS ), &bv );
481                         if ( BER_BVISNULL( &bv ) ) {
482                                 /* there's something wrong... */
483                                 assert( 0 );
484                                 rc = 1;
485
486                         } else {
487                                 c->value_bv = bv;
488                         }
489                         break;
490
491                 case LDAP_BACK_CFG_T_F:
492                         enum_to_verb( t_f_mode, ( ( li->flags & LDAP_BACK_F_SUPPORT_T_F_MASK ) == LDAP_BACK_F_SUPPORT_T_F_MASK ), &bv );
493                         if ( BER_BVISNULL( &bv ) ) {
494                                 /* there's something wrong... */
495                                 assert( 0 );
496                                 rc = 1;
497
498                         } else {
499                                 c->value_bv = bv;
500                         }
501                         break;
502
503                 case LDAP_BACK_CFG_WHOAMI:
504                         enum_to_verb( yn_mode, ( ( li->flags & LDAP_BACK_F_PROXY_WHOAMI ) == LDAP_BACK_F_PROXY_WHOAMI ), &bv );
505                         if ( BER_BVISNULL( &bv ) ) {
506                                 /* there's something wrong... */
507                                 assert( 0 );
508                                 rc = 1;
509
510                         } else {
511                                 c->value_bv = bv;
512                         }
513                         break;
514
515                 default:
516                         /* FIXME: we need to handle all... */
517                         assert( 0 );
518                         break;
519                 }
520                 return rc;
521
522         } else if ( c->op == LDAP_MOD_DELETE ) {
523                 rc = 0;
524                 switch( c->type ) {
525                 case LDAP_BACK_CFG_URI:
526                         if ( li->url != NULL ) {
527                                 ch_free( li->url );
528                                 li->url = NULL;
529                         }
530
531                         if ( li->lud != NULL ) {
532                                 ldap_free_urllist( li->lud );
533                                 li->lud = NULL;
534                         }
535                         
536                         /* better cleanup the cached connections... */
537                         /* NOTE: don't worry about locking: if we got here,
538                          * other threads are suspended. */
539                         avl_free( li->conntree, ldap_back_conn_free );
540                         li->conntree = NULL;
541                         
542                         break;
543
544                 case LDAP_BACK_CFG_TLS:
545                 case LDAP_BACK_CFG_ACL_AUTHCDN:
546                 case LDAP_BACK_CFG_ACL_PASSWD:
547                 case LDAP_BACK_CFG_ACL_METHOD:
548                         /* handled by LDAP_BACK_CFG_ACL_BIND */
549                         rc = 1;
550                         break;
551
552                 case LDAP_BACK_CFG_ACL_BIND:
553                         bindconf_free( &li->acl_sb );
554                         break;
555
556                 case LDAP_BACK_CFG_IDASSERT_MODE:
557                 case LDAP_BACK_CFG_IDASSERT_AUTHCDN:
558                 case LDAP_BACK_CFG_IDASSERT_PASSWD:
559                 case LDAP_BACK_CFG_IDASSERT_METHOD:
560                         /* handled by LDAP_BACK_CFG_IDASSERT_BIND */
561                         rc = 1;
562                         break;
563
564                 case LDAP_BACK_CFG_IDASSERT_AUTHZFROM:
565                         if ( li->idassert_authz != NULL ) {
566                                 ber_bvarray_free( li->idassert_authz );
567                                 li->idassert_authz = NULL;
568                         }
569                         break;
570
571                 case LDAP_BACK_CFG_IDASSERT_BIND:
572                         bindconf_free( &li->idassert_sb );
573                         break;
574
575                 case LDAP_BACK_CFG_REBIND:
576                 case LDAP_BACK_CFG_CHASE:
577                 case LDAP_BACK_CFG_T_F:
578                 case LDAP_BACK_CFG_WHOAMI:
579                         rc = 1;
580                         break;
581
582                 default:
583                         /* FIXME: we need to handle all... */
584                         assert( 0 );
585                         break;
586                 }
587                 return rc;
588
589         }
590
591         switch( c->type ) {
592         case LDAP_BACK_CFG_URI: {
593                 LDAPURLDesc     *tmpludp;
594 #if 0
595                 char            **urllist;
596 #endif
597                 int             urlrc, i;
598
599                 if ( c->argc != 2 ) {
600                         fprintf( stderr, "%s: line %d: "
601                                         "missing uri "
602                                         "in \"uri <uri>\" line\n",
603                                         c->fname, c->lineno );
604                         return 1;
605                 }
606
607                 if ( li->url != NULL ) {
608                         ch_free( li->url );
609                 }
610
611                 if ( li->lud != NULL ) {
612                         ldap_free_urllist( li->lud );
613                 }
614
615 #if 0
616                 /* PARANOID: DN and more are not required nor allowed */
617                 urlrc = ldap_url_parselist_ext( &li->lud, c->value_string, "\t" );
618 #else
619                 urlrc = ldap_url_parselist( &li->lud, c->value_string );
620 #endif
621                 if ( urlrc != LDAP_URL_SUCCESS ) {
622                         char    *why;
623
624                         switch ( urlrc ) {
625                         case LDAP_URL_ERR_MEM:
626                                 why = "no memory";
627                                 break;
628                         case LDAP_URL_ERR_PARAM:
629                                 why = "parameter is bad";
630                                 break;
631                         case LDAP_URL_ERR_BADSCHEME:
632                                 why = "URL doesn't begin with \"[c]ldap[si]://\"";
633                                 break;
634                         case LDAP_URL_ERR_BADENCLOSURE:
635                                 why = "URL is missing trailing \">\"";
636                                 break;
637                         case LDAP_URL_ERR_BADURL:
638                                 why = "URL is bad";
639                         case LDAP_URL_ERR_BADHOST:
640                                 why = "host/port is bad";
641                                 break;
642                         case LDAP_URL_ERR_BADATTRS:
643                                 why = "bad (or missing) attributes";
644                                 break;
645                         case LDAP_URL_ERR_BADSCOPE:
646                                 why = "scope string is invalid (or missing)";
647                                 break;
648                         case LDAP_URL_ERR_BADFILTER:
649                                 why = "bad or missing filter";
650                                 break;
651                         case LDAP_URL_ERR_BADEXTS:
652                                 why = "bad or missing extensions";
653                                 break;
654                         default:
655                                 why = "unknown reason";
656                                 break;
657                         }
658                         fprintf( stderr, "%s: line %d: "
659                                         "unable to parse uri \"%s\" "
660                                         "in \"uri <uri>\" line: %s\n",
661                                         c->fname, c->lineno, c->value_string, why );
662                         return 1;
663                 }
664
665                 for ( i = 0, tmpludp = li->lud;
666                                 tmpludp;
667                                 i++, tmpludp = tmpludp->lud_next )
668                 {
669                         if ( ( tmpludp->lud_dn != NULL
670                                                 && tmpludp->lud_dn[0] != '\0' )
671                                         || tmpludp->lud_attrs != NULL
672                                         /* || tmpludp->lud_scope != LDAP_SCOPE_DEFAULT */
673                                         || tmpludp->lud_filter != NULL
674                                         || tmpludp->lud_exts != NULL )
675                         {
676                                 fprintf( stderr, "%s: line %d: "
677                                                 "warning, only protocol, "
678                                                 "host and port allowed "
679                                                 "in \"uri <uri>\" statement "
680                                                 "for uri #%d of \"%s\"\n",
681                                                 c->fname, c->lineno, i, c->value_string );
682                         }
683                 }
684
685 #if 0
686                 for ( i = 0, tmpludp = li->lud;
687                                 tmpludp;
688                                 i++, tmpludp = tmpludp->lud_next )
689                         /* just count */
690                         ;
691                 urllist = ch_calloc( sizeof( char * ), i + 1 );
692
693                 for ( i = 0, tmpludp = li->lud;
694                                 tmpludp;
695                                 i++, tmpludp = tmpludp->lud_next )
696                 {
697                         LDAPURLDesc     tmplud;
698                         ber_len_t       oldlen = 0, len;
699
700                         tmplud = *tmpludp;
701                         tmplud.lud_dn = "";
702                         tmplud.lud_attrs = NULL;
703                         tmplud.lud_filter = NULL;
704                         if ( !ldap_is_ldapi_url( tmplud.lud_scheme ) ) {
705                                 tmplud.lud_exts = NULL;
706                                 tmplud.lud_crit_exts = 0;
707                         }
708
709                         urllist[ i ]  = ldap_url_desc2str( &tmplud );
710
711                         if ( urllist[ i ] == NULL ) {
712                                 fprintf( stderr, "%s: line %d: "
713                                         "unable to rebuild uri "
714                                         "in \"uri <uri>\" statement "
715                                         "for \"%s\"\n",
716                                         c->fname, c->lineno, argv[ 1 ] );
717                                 return 1;
718                         }
719                 }
720
721                 li->url = ldap_charray2str( urllist, " " );
722                 ldap_charray_free( urllist );
723 #else
724                 li->url = c->value_string;
725 #endif
726                 break;
727         }
728
729         case LDAP_BACK_CFG_TLS:
730                 i = verb_to_mask( c->argv[1], tls_mode );
731                 if ( BER_BVISNULL( &tls_mode[i].word ) ) {
732                         return 1;
733                 }
734                 li->flags &= ~LDAP_BACK_F_TLS_MASK;
735                 li->flags |= tls_mode[i].mask;
736                 break;
737
738         case LDAP_BACK_CFG_ACL_AUTHCDN:
739                 switch ( li->acl_authmethod ) {
740                 case LDAP_AUTH_NONE:
741                         li->acl_authmethod = LDAP_AUTH_SIMPLE;
742                         break;
743
744                 case LDAP_AUTH_SIMPLE:
745                         break;
746
747                 default:
748                         fprintf( stderr, "%s: line %d: "
749                                 "\"acl-authcDN <DN>\" incompatible "
750                                 "with auth method %d.",
751                                 c->fname, c->lineno, li->acl_authmethod );
752                         return 1;
753                 }
754                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
755                         free( li->acl_authcDN.bv_val );
756                 }
757                 li->acl_authcDN = c->value_ndn;
758                 break;
759
760         case LDAP_BACK_CFG_ACL_PASSWD:
761                 switch ( li->acl_authmethod ) {
762                 case LDAP_AUTH_NONE:
763                         li->acl_authmethod = LDAP_AUTH_SIMPLE;
764                         break;
765
766                 case LDAP_AUTH_SIMPLE:
767                         break;
768
769                 default:
770                         fprintf( stderr, "%s: line %d: "
771                                 "\"acl-passwd <cred>\" incompatible "
772                                 "with auth method %d.",
773                                 c->fname, c->lineno, li->acl_authmethod );
774                         return 1;
775                 }
776                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
777                         free( li->acl_passwd.bv_val );
778                 }
779                 li->acl_passwd = c->value_bv;
780                 break;
781
782         case LDAP_BACK_CFG_ACL_METHOD:
783         case LDAP_BACK_CFG_ACL_BIND:
784                 for ( i = 1; i < c->argc; i++ ) {
785                         if ( bindconf_parse( c->argv[ i ], &li->acl_sb ) ) {
786                                 return 1;
787                         }
788                 }
789                 break;
790
791         case LDAP_BACK_CFG_IDASSERT_MODE:
792                 i = verb_to_mask( c->argv[1], idassert_mode );
793                 if ( BER_BVISNULL( &idassert_mode[i].word ) ) {
794                         if ( strncasecmp( c->argv[1], "u:", STRLENOF( "u:" ) ) == 0 ) {
795                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
796                                 ber_str2bv( c->argv[1], 0, 1, &li->idassert_authzID );
797                                 li->idassert_authzID.bv_val[ 0 ] = 'u';
798                                 
799                         } else {
800                                 struct berval   id, ndn;
801
802                                 ber_str2bv( c->argv[1], 0, 0, &id );
803
804                                 if ( strncasecmp( c->argv[1], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
805                                         id.bv_val += STRLENOF( "dn:" );
806                                         id.bv_len -= STRLENOF( "dn:" );
807                                 }
808
809                                 rc = dnNormalize( 0, NULL, NULL, &id, &ndn, NULL );
810                                 if ( rc != LDAP_SUCCESS ) {
811                                         Debug( LDAP_DEBUG_ANY,
812                                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
813                                                 c->fname, c->lineno, c->argv[1] );
814                                         return 1;
815                                 }
816
817                                 li->idassert_authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
818                                 li->idassert_authzID.bv_val = ch_malloc( li->idassert_authzID.bv_len + 1 );
819                                 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
820                                 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], ndn.bv_val, ndn.bv_len + 1 );
821                                 ch_free( ndn.bv_val );
822
823                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
824                         }
825
826                 } else {
827                         li->idassert_mode = idassert_mode[i].mask;
828                 }
829
830                 if ( c->argc > 2 ) {
831                         int     i;
832
833                         for ( i = 2; i < c->argc; i++ ) {
834                                 if ( strcasecmp( c->argv[ i ], "override" ) == 0 ) {
835                                         li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
836
837                                 } else if ( strcasecmp( c->argv[ i ], "prescriptive" ) == 0 ) {
838                                         li->idassert_flags |= LDAP_BACK_AUTH_PRESCRIPTIVE;
839
840                                 } else if ( strcasecmp( c->argv[ i ], "non-prescriptive" ) == 0 ) {
841                                         li->idassert_flags &= ( ~LDAP_BACK_AUTH_PRESCRIPTIVE );
842
843                                 } else {
844                                         Debug( LDAP_DEBUG_ANY,
845                                                 "%s: line %d: unknown flag #%d "
846                                                 "in \"idassert-mode <args> "
847                                                 "[<flags>]\" line.\n",
848                                                 c->fname, c->lineno, i - 2 );
849                                         return 1;
850                                 }
851                         }
852                 }
853                 break;
854
855         case LDAP_BACK_CFG_IDASSERT_AUTHCDN:
856                 switch ( li->idassert_authmethod ) {
857                 case LDAP_AUTH_NONE:
858                         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
859                         break;
860
861                 case LDAP_AUTH_SIMPLE:
862                         break;
863
864                 default:
865                         fprintf( stderr, "%s: line %d: "
866                                 "\"idassert-authcDN <DN>\" incompatible "
867                                 "with auth method %d.",
868                                 c->fname, c->lineno, li->idassert_authmethod );
869                         return 1;
870                 }
871                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
872                         free( li->idassert_authcDN.bv_val );
873                 }
874                 li->idassert_authcDN = c->value_ndn;
875                 break;
876
877         case LDAP_BACK_CFG_IDASSERT_PASSWD:
878                 switch ( li->idassert_authmethod ) {
879                 case LDAP_AUTH_NONE:
880                         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
881                         break;
882
883                 case LDAP_AUTH_SIMPLE:
884                         break;
885
886                 default:
887                         fprintf( stderr, "%s: line %d: "
888                                 "\"idassert-passwd <cred>\" incompatible "
889                                 "with auth method %d.",
890                                 c->fname, c->lineno, li->idassert_authmethod );
891                         return 1;
892                 }
893                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
894                         free( li->idassert_passwd.bv_val );
895                 }
896                 li->idassert_passwd = c->value_bv;
897                 break;
898
899         case LDAP_BACK_CFG_IDASSERT_AUTHZFROM:
900                 ber_bvarray_add( &li->idassert_authz, &c->value_bv );
901                 break;
902
903         case LDAP_BACK_CFG_IDASSERT_METHOD:
904                 /* no longer supported */
905                 fprintf( stderr, "%s: %d: "
906                         "\"idassert-method <args>\": "
907                         "no longer supported; use \"idassert-bind\".\n",
908                         c->fname, c->lineno );
909                 return 1;
910
911         case LDAP_BACK_CFG_IDASSERT_BIND:
912                 for ( i = 1; i < c->argc; i++ ) {
913                         if ( strncasecmp( c->argv[ i ], "mode=", STRLENOF( "mode=" ) ) == 0 ) {
914                                 char    *argvi = c->argv[ i ] + STRLENOF( "mode=" );
915                                 int     j;
916
917                                 j = verb_to_mask( argvi, idassert_mode );
918                                 if ( BER_BVISNULL( &idassert_mode[ j ].word ) ) {
919                                         fprintf( stderr, "%s: %d: "
920                                                 "\"idassert-bind <args>\": "
921                                                 "unknown mode \"%s\".\n",
922                                                 c->fname, c->lineno, argvi );
923                                         return 1;
924                                 }
925
926                                 li->idassert_mode = idassert_mode[ j ].mask;
927
928                         } else if ( strncasecmp( c->argv[ i ], "authz=", STRLENOF( "authz=" ) ) == 0 ) {
929                                 char    *argvi = c->argv[ i ] + STRLENOF( "authz=" );
930
931                                 if ( strcasecmp( argvi, "native" ) == 0 ) {
932                                         if ( li->idassert_authmethod != LDAP_AUTH_SASL ) {
933                                                 fprintf( stderr, "%s: %d: "
934                                                         "\"idassert-bind <args>\": "
935                                                         "authz=\"native\" incompatible "
936                                                         "with auth method.\n",
937                                                         c->fname, c->lineno );
938                                                 return 1;
939                                         }
940                                         li->idassert_flags |= LDAP_BACK_AUTH_NATIVE_AUTHZ;
941
942                                 } else if ( strcasecmp( argvi, "proxyAuthz" ) == 0 ) {
943                                         li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
944
945                                 } else {
946                                         fprintf( stderr, "%s: %d: "
947                                                 "\"idassert-bind <args>\": "
948                                                 "unknown authz \"%s\".\n",
949                                                 c->fname, c->lineno, argvi );
950                                         return 1;
951                                 }
952
953                         } else if ( strncasecmp( c->argv[ i ], "flags=", STRLENOF( "flags=" ) ) == 0 ) {
954                                 char    *argvi = c->argv[ i ] + STRLENOF( "flags=" );
955                                 char    **flags = ldap_str2charray( argvi, "," );
956                                 int     j;
957
958                                 if ( flags == NULL ) {
959                                         fprintf( stderr, "%s: %d: "
960                                                 "\"idassert-bind <args>\": "
961                                                 "unable to parse flags \"%s\".\n",
962                                                 c->fname, c->lineno, argvi );
963                                         return 1;
964                                 }
965
966                                 for ( j = 0; flags[ j ] != NULL; j++ ) {
967                                         if ( strcasecmp( flags[ j ], "override" ) == 0 ) {
968                                                 li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
969
970                                         } else if ( strcasecmp( flags[ j ], "prescriptive" ) == 0 ) {
971                                                 li->idassert_flags |= LDAP_BACK_AUTH_PRESCRIPTIVE;
972
973                                         } else if ( strcasecmp( flags[ j ], "non-prescriptive" ) == 0 ) {
974                                                 li->idassert_flags &= ( ~LDAP_BACK_AUTH_PRESCRIPTIVE );
975
976                                         } else {
977                                                 fprintf( stderr, "%s: %d: "
978                                                         "\"idassert-bind <args>\": "
979                                                         "unknown flag \"%s\".\n",
980                                                         c->fname, c->lineno, flags[ j ] );
981                                                 return 1;
982                                         }
983                                 }
984
985                                 ldap_charray_free( flags );
986
987                         } else if ( bindconf_parse( c->argv[ i ], &li->idassert_sb ) ) {
988                                 return 1;
989                         }
990                 }
991                 break;
992
993         case LDAP_BACK_CFG_REBIND: {
994                 int     dorebind = 0;
995
996                 if ( c->argc == 1 ) {
997                         /* legacy */
998                         dorebind = 1;
999
1000                 } else {
1001                         i = verb_to_mask( c->argv[1], yn_mode );
1002                         if ( BER_BVISNULL( &yn_mode[i].word ) ) {
1003                                 return 1;
1004                         }
1005                         if ( yn_mode[i].mask & LDAP_BACK_C_YES ) {
1006                                 dorebind = 1;
1007                         }
1008                 }
1009
1010                 if ( dorebind ) {
1011                         li->flags |= LDAP_BACK_F_SAVECRED;
1012
1013                 } else {
1014                         li->flags &= ~LDAP_BACK_F_SAVECRED;
1015                 }
1016                 break;
1017         }
1018
1019         case LDAP_BACK_CFG_CHASE: {
1020                 int     dochase = 0;
1021
1022                 if ( c->argc == 1 ) {
1023                         /* legacy */
1024                         dochase = 1;
1025
1026                 } else {
1027                         i = verb_to_mask( c->argv[1], yn_mode );
1028                         if ( BER_BVISNULL( &yn_mode[i].word ) ) {
1029                                 return 1;
1030                         }
1031                         if ( yn_mode[i].mask & LDAP_BACK_C_YES ) {
1032                                 dochase = 1;
1033                         }
1034                 }
1035
1036                 if ( dochase ) {
1037                         li->flags |= LDAP_BACK_F_CHASE_REFERRALS;
1038
1039                 } else {
1040                         li->flags &= ~LDAP_BACK_F_CHASE_REFERRALS;
1041                 }
1042                 break;
1043         }
1044
1045         case LDAP_BACK_CFG_T_F:
1046                 i = verb_to_mask( c->argv[1], t_f_mode );
1047                 if ( BER_BVISNULL( &t_f_mode[i].word ) ) {
1048                         return 1;
1049                 }
1050                 li->flags &= ~LDAP_BACK_F_SUPPORT_T_F_MASK;
1051                 li->flags |= t_f_mode[i].mask;
1052                 break;
1053
1054         case LDAP_BACK_CFG_WHOAMI: {
1055                 int     dowhoami = 0;
1056
1057                 if ( c->argc == 1 ) {
1058                         /* legacy */
1059                         dowhoami = 1;
1060
1061                 } else {
1062                         i = verb_to_mask( c->argv[1], yn_mode );
1063                         if ( BER_BVISNULL( &yn_mode[i].word ) ) {
1064                                 return 1;
1065                         }
1066                         if ( yn_mode[i].mask & LDAP_BACK_C_YES ) {
1067                                 dowhoami = 1;
1068                         }
1069                 }
1070
1071                 if ( dowhoami ) {
1072                         li->flags |= LDAP_BACK_F_PROXY_WHOAMI;
1073
1074                         load_extop( (struct berval *)&slap_EXOP_WHOAMI,
1075                                         0, ldap_back_exop_whoami );
1076
1077                 } else {
1078                         li->flags &= ~LDAP_BACK_F_PROXY_WHOAMI;
1079                 }
1080                 break;
1081         }
1082
1083         case LDAP_BACK_CFG_REWRITE:
1084                 fprintf( stderr, "%s: line %d: "
1085                         "rewrite/remap capabilities have been moved "
1086                         "to the \"rwm\" overlay; see slapo-rwm(5) "
1087                         "for details (hint: add \"overlay rwm\" "
1088                         "and prefix all directives with \"rwm-\").\n",
1089                         c->fname, c->lineno );
1090                 return 1;
1091                 
1092         default:
1093                 /* FIXME: try to catch inconsistencies */
1094                 assert( 0 );
1095                 break;
1096         }
1097
1098         return 0;
1099 }
1100
1101 int
1102 ldap_back_init_cf( BackendInfo *bi )
1103 {
1104         int                     rc;
1105         AttributeDescription    *ad = NULL;
1106         const char              *text;
1107
1108         bi->bi_cf_ocs = ldapocs;
1109
1110         rc = config_register_schema( ldapcfg, ldapocs );
1111         if ( rc ) {
1112                 return rc;
1113         }
1114
1115         /* setup olcDbAclPasswd and olcDbIDAssertPasswd 
1116          * to be base64-encoded when written in LDIF form;
1117          * basically, we don't care if it fails */
1118         rc = slap_str2ad( "olcDbACLPasswd", &ad, &text );
1119         if ( rc ) {
1120                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
1121                         "warning, unable to get \"olcDbACLPasswd\" "
1122                         "attribute description: %d: %s\n",
1123                         rc, text, 0 );
1124         } else {
1125                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
1126                         ad->ad_type->sat_oid );
1127         }
1128
1129         ad = NULL;
1130         rc = slap_str2ad( "olcDbIDAssertPasswd", &ad, &text );
1131         if ( rc ) {
1132                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
1133                         "warning, unable to get \"olcDbIDAssertPasswd\" "
1134                         "attribute description: %d: %s\n",
1135                         rc, text, 0 );
1136         } else {
1137                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
1138                         ad->ad_type->sat_oid );
1139         }
1140
1141         return 0;
1142 }
1143
1144
1145 static int
1146 parse_idassert( BackendDB *be, const char *fname, int lineno,
1147                 int argc, char **argv );
1148
1149 static int
1150 parse_acl_auth( BackendDB *be, const char *fname, int lineno,
1151                 int argc, char **argv );
1152
1153 int
1154 ldap_back_db_config(
1155                 BackendDB       *be,
1156                 const char      *fname,
1157                 int             lineno,
1158                 int             argc,
1159                 char            **argv )
1160 {
1161         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
1162
1163         if ( li == NULL ) {
1164                 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
1165                                 fname, lineno );
1166                 return 1;
1167         }
1168
1169         /* server address to query (no longer supported, use "uri" directive) */
1170         if ( strcasecmp( argv[0], "server" ) == 0 ) {
1171                 fprintf( stderr,
1172         "%s: line %d: \"server <address>\" directive is no longer supported.\n",
1173                                         fname, lineno );
1174                 return 1;
1175
1176         /* URI of server to query (obsoletes "server" directive) */
1177         } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
1178                 LDAPURLDesc     *tmpludp;
1179 #if 0
1180                 char            **urllist;
1181 #endif
1182                 int             urlrc, i;
1183
1184                 if ( argc != 2 ) {
1185                         fprintf( stderr, "%s: line %d: "
1186                                         "missing uri "
1187                                         "in \"uri <uri>\" line\n",
1188                                         fname, lineno );
1189                         return 1;
1190                 }
1191                 if ( li->url != NULL ) {
1192                         ch_free( li->url );
1193                 }
1194                 if ( li->lud != NULL ) {
1195                         ldap_free_urllist( li->lud );
1196                 }
1197
1198 #if 0
1199                 /* PARANOID: DN and more are not required nor allowed */
1200                 urlrc = ldap_url_parselist_ext( &li->lud, argv[ 1 ], "\t" );
1201 #else
1202                 urlrc = ldap_url_parselist( &li->lud, argv[ 1 ] );
1203 #endif
1204                 if ( urlrc != LDAP_URL_SUCCESS ) {
1205                         char    *why;
1206
1207                         switch ( urlrc ) {
1208                         case LDAP_URL_ERR_MEM:
1209                                 why = "no memory";
1210                                 break;
1211                         case LDAP_URL_ERR_PARAM:
1212                                 why = "parameter is bad";
1213                                 break;
1214                         case LDAP_URL_ERR_BADSCHEME:
1215                                 why = "URL doesn't begin with \"[c]ldap[si]://\"";
1216                                 break;
1217                         case LDAP_URL_ERR_BADENCLOSURE:
1218                                 why = "URL is missing trailing \">\"";
1219                                 break;
1220                         case LDAP_URL_ERR_BADURL:
1221                                 why = "URL is bad";
1222                         case LDAP_URL_ERR_BADHOST:
1223                                 why = "host/port is bad";
1224                                 break;
1225                         case LDAP_URL_ERR_BADATTRS:
1226                                 why = "bad (or missing) attributes";
1227                                 break;
1228                         case LDAP_URL_ERR_BADSCOPE:
1229                                 why = "scope string is invalid (or missing)";
1230                                 break;
1231                         case LDAP_URL_ERR_BADFILTER:
1232                                 why = "bad or missing filter";
1233                                 break;
1234                         case LDAP_URL_ERR_BADEXTS:
1235                                 why = "bad or missing extensions";
1236                                 break;
1237                         default:
1238                                 why = "unknown reason";
1239                                 break;
1240                         }
1241                         fprintf( stderr, "%s: line %d: "
1242                                         "unable to parse uri \"%s\" "
1243                                         "in \"uri <uri>\" line: %s\n",
1244                                         fname, lineno, argv[ 1 ], why );
1245                         return 1;
1246                 }
1247
1248                 for ( i = 0, tmpludp = li->lud;
1249                                 tmpludp;
1250                                 i++, tmpludp = tmpludp->lud_next )
1251                 {
1252                         if ( ( tmpludp->lud_dn != NULL
1253                                                 && tmpludp->lud_dn[0] != '\0' )
1254                                         || tmpludp->lud_attrs != NULL
1255                                         /* || tmpludp->lud_scope != LDAP_SCOPE_DEFAULT */
1256                                         || tmpludp->lud_filter != NULL
1257                                         || tmpludp->lud_exts != NULL )
1258                         {
1259                                 fprintf( stderr, "%s: line %d: "
1260                                                 "warning, only protocol, "
1261                                                 "host and port allowed "
1262                                                 "in \"uri <uri>\" statement "
1263                                                 "for uri #%d of \"%s\"\n",
1264                                                 fname, lineno, i, argv[1] );
1265                         }
1266                 }
1267
1268 #if 0
1269                 for ( i = 0, tmpludp = li->lud;
1270                                 tmpludp;
1271                                 i++, tmpludp = tmpludp->lud_next )
1272                         /* just count */
1273                         ;
1274                 urllist = ch_calloc( sizeof( char * ), i + 1 );
1275
1276                 for ( i = 0, tmpludp = li->lud;
1277                                 tmpludp;
1278                                 i++, tmpludp = tmpludp->lud_next )
1279                 {
1280                         LDAPURLDesc     tmplud;
1281                         ber_len_t       oldlen = 0, len;
1282
1283                         tmplud = *tmpludp;
1284                         tmplud.lud_dn = "";
1285                         tmplud.lud_attrs = NULL;
1286                         tmplud.lud_filter = NULL;
1287                         if ( !ldap_is_ldapi_url( tmplud.lud_scheme ) ) {
1288                                 tmplud.lud_exts = NULL;
1289                                 tmplud.lud_crit_exts = 0;
1290                         }
1291
1292                         urllist[ i ]  = ldap_url_desc2str( &tmplud );
1293
1294                         if ( urllist[ i ] == NULL ) {
1295                                 fprintf( stderr, "%s: line %d: "
1296                                         "unable to rebuild uri "
1297                                         "in \"uri <uri>\" statement "
1298                                         "for \"%s\"\n",
1299                                         fname, lineno, argv[ 1 ] );
1300                                 return 1;
1301                         }
1302                 }
1303
1304                 li->url = ldap_charray2str( urllist, " " );
1305                 ldap_charray_free( urllist );
1306 #else
1307                 li->url = ch_strdup( argv[ 1 ] );
1308 #endif
1309
1310         } else if ( strcasecmp( argv[0], "tls" ) == 0 ) {
1311                 if ( argc != 2 ) {
1312                         fprintf( stderr,
1313                 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
1314                                         fname, lineno );
1315                         return( 1 );
1316                 }
1317
1318                 /* none */
1319                 if ( strcasecmp( argv[1], "none" ) == 0 ) {
1320                         li->flags &= ~LDAP_BACK_F_TLS_MASK;
1321         
1322                 /* try start tls */
1323                 } else if ( strcasecmp( argv[1], "start" ) == 0 ) {
1324                         li->flags |= LDAP_BACK_F_TLS_USE_MASK;
1325         
1326                 /* try start tls */
1327                 } else if ( strcasecmp( argv[1], "try-start" ) == 0 ) {
1328                         li->flags &= ~LDAP_BACK_F_TLS_CRITICAL;
1329                         li->flags |= LDAP_BACK_F_USE_TLS;
1330         
1331                 /* propagate start tls */
1332                 } else if ( strcasecmp( argv[1], "propagate" ) == 0 ) {
1333                         li->flags |= LDAP_BACK_F_TLS_PROPAGATE_MASK;
1334                 
1335                 /* try start tls */
1336                 } else if ( strcasecmp( argv[1], "try-propagate" ) == 0 ) {
1337                         li->flags &= ~LDAP_BACK_F_TLS_CRITICAL;
1338                         li->flags |= LDAP_BACK_F_PROPAGATE_TLS;
1339
1340                 } else {
1341                         fprintf( stderr,
1342                 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
1343                                         fname, lineno, argv[1] );
1344                         return( 1 );
1345                 }
1346         
1347         /* remote ACL stuff... */
1348         } else if ( strncasecmp( argv[0], "acl-", STRLENOF( "acl-" ) ) == 0
1349                         || strncasecmp( argv[0], "bind", STRLENOF( "bind" ) ) == 0 )
1350         {
1351                 /* NOTE: "bind{DN,pw}" was initially used; it's now
1352                  * deprected and undocumented, it can be dropped at some
1353                  * point, since nobody should be really using it */
1354                 return parse_acl_auth( be, fname, lineno, argc, argv );
1355
1356         /* identity assertion stuff... */
1357         } else if ( strncasecmp( argv[0], "idassert-", STRLENOF( "idassert-" ) ) == 0
1358                         || strncasecmp( argv[0], "proxyauthz", STRLENOF( "proxyauthz" ) ) == 0 )
1359         {
1360                 /* NOTE: "proxyauthz{DN,pw}" was initially used; it's now
1361                  * deprected and undocumented, it can be dropped at some
1362                  * point, since nobody should be really using it */
1363                 return parse_idassert( be, fname, lineno, argc, argv );
1364
1365         /* save bind creds for referral rebinds? */
1366         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
1367                 switch ( argc ) {
1368                 case 1:
1369                         fprintf( stderr,
1370         "%s: line %d: \"rebind-as-user {NO|yes}\": use without args is deprecated.\n",
1371                                 fname, lineno );
1372         
1373                         li->flags |= LDAP_BACK_F_SAVECRED;
1374                         break;
1375
1376                 case 2:
1377                         if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1378                                 li->flags |= LDAP_BACK_F_SAVECRED;
1379
1380                         } else if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1381                                 li->flags &= ~LDAP_BACK_F_SAVECRED;
1382
1383                         } else {
1384                                 fprintf( stderr,
1385         "%s: line %d: \"rebind-as-user {NO|yes}\": unknown argument \"%s\".\n",
1386                                         fname, lineno, argv[ 1 ] );
1387                                 return( 1 );
1388                         }
1389                         break;
1390
1391                 default:
1392                         fprintf( stderr,
1393         "%s: line %d: \"rebind-as-user {NO|yes}\" needs 1 argument.\n",
1394                                         fname, lineno );
1395                         return( 1 );
1396                 }
1397
1398         } else if ( strcasecmp( argv[0], "chase-referrals" ) == 0 ) {
1399                 if ( argc != 2 ) {
1400                         fprintf( stderr,
1401         "%s: line %d: \"chase-referrals {YES|no}\" needs 1 argument.\n",
1402                                         fname, lineno );
1403                         return( 1 );
1404                 }
1405
1406                 /* this is the default; we add it because the default might change... */
1407                 if ( strcasecmp( argv[1], "yes" ) == 0 ) {
1408                         li->flags |= LDAP_BACK_F_CHASE_REFERRALS;
1409
1410                 } else if ( strcasecmp( argv[1], "no" ) == 0 ) {
1411                         li->flags &= ~LDAP_BACK_F_CHASE_REFERRALS;
1412
1413                 } else {
1414                         fprintf( stderr,
1415                 "%s: line %d: \"chase-referrals {YES|no}\": unknown argument \"%s\".\n",
1416                                         fname, lineno, argv[1] );
1417                         return( 1 );
1418                 }
1419         
1420         } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
1421                 if ( argc != 2 ) {
1422                         fprintf( stderr,
1423                 "%s: line %d: \"t-f-support {NO|yes|discover}\" needs 1 argument.\n",
1424                                         fname, lineno );
1425                         return( 1 );
1426                 }
1427
1428                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1429                         li->flags &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
1430
1431                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1432                         li->flags |= LDAP_BACK_F_SUPPORT_T_F;
1433
1434                 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1435                         li->flags |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
1436
1437                 } else {
1438                         fprintf( stderr,
1439         "%s: line %d: \"t-f-support {NO|yes|discover}\": unknown argument \"%s\".\n",
1440                                 fname, lineno, argv[ 1 ] );
1441                         return 1;
1442                 }
1443
1444         /* intercept exop_who_am_i? */
1445         } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
1446                 int     doload_extop = 0;
1447
1448                 switch ( argc ) {
1449                 case 1:
1450                         fprintf( stderr,
1451         "%s: line %d: \"proxy-whoami {NO|yes}\": use without args is deprecated.\n",
1452                                 fname, lineno );
1453         
1454                         doload_extop = 1;
1455                         break;
1456
1457                 case 2:
1458                         if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1459                                 doload_extop = 1;
1460
1461                         } else if ( strcasecmp( argv[ 1 ], "no" ) != 0 ) {
1462                                 fprintf( stderr,
1463         "%s: line %d: \"proxy-whoami {NO|yes}\": unknown argument \"%s\".\n",
1464                                         fname, lineno, argv[ 1 ] );
1465                                 return( 1 );
1466                         }
1467                         break;
1468
1469                 default:
1470                         fprintf( stderr,
1471         "%s: line %d: \"proxy-whoami {NO|yes}\" needs 1 argument.\n",
1472                                         fname, lineno );
1473                         return( 1 );
1474                 }
1475
1476                 if ( doload_extop ) {
1477                         li->flags |= LDAP_BACK_F_PROXY_WHOAMI;
1478
1479                         load_extop( (struct berval *)&slap_EXOP_WHOAMI,
1480                                         0, ldap_back_exop_whoami );
1481                 }
1482
1483         /* FIXME: legacy: intercept old rewrite/remap directives
1484          * and try to start the rwm overlay */
1485         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0
1486                         || strcasecmp( argv[0], "map" ) == 0
1487                         || strncasecmp( argv[0], "rewrite", STRLENOF( "rewrite" ) ) == 0 )
1488         {
1489 #if 0
1490                 fprintf( stderr, "%s: line %d: "
1491                         "rewrite/remap capabilities have been moved "
1492                         "to the \"rwm\" overlay; see slapo-rwm(5) "
1493                         "for details.  I'm trying to do my best "
1494                         "to preserve backwards compatibility...\n",
1495                         fname, lineno );
1496
1497                 if ( li->rwm_started == 0 ) {
1498                         if ( overlay_config( be, "rwm" ) ) {
1499                                 fprintf( stderr, "%s: line %d: "
1500                                         "unable to configure the \"rwm\" "
1501                                         "overlay, required by directive "
1502                                         "\"%s\".\n",
1503                                         fname, lineno, argv[0] );
1504 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1505                                 fprintf( stderr, "\thint: try loading the \"rwm.la\" dynamic module.\n" );
1506 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1507                                 return( 1 );
1508                         }
1509
1510                         fprintf( stderr, "%s: line %d: back-ldap: "
1511                                 "automatically starting \"rwm\" overlay, "
1512                                 "triggered by \"%s\" directive.\n",
1513                                 fname, lineno, argv[ 0 ] );
1514
1515                 /* this is the default; we add it because the default might change... */
1516                         li->rwm_started = 1;
1517
1518                         return ( *be->bd_info->bi_db_config )( be, fname, lineno, argc, argv );
1519                 }
1520 #else
1521                 fprintf( stderr, "%s: line %d: "
1522                         "rewrite/remap capabilities have been moved "
1523                         "to the \"rwm\" overlay; see slapo-rwm(5) "
1524                         "for details (hint: add \"overlay rwm\" "
1525                         "and prefix all directives with \"rwm-\").\n",
1526                         fname, lineno );
1527 #endif
1528
1529                 return 1;
1530         
1531         /* anything else */
1532         } else {
1533                 return SLAP_CONF_UNKNOWN;
1534         }
1535
1536         return 0;
1537 }
1538
1539 static int
1540 ldap_back_exop_whoami(
1541                 Operation       *op,
1542                 SlapReply       *rs )
1543 {
1544         struct berval *bv = NULL;
1545
1546         if ( op->oq_extended.rs_reqdata != NULL ) {
1547                 /* no request data should be provided */
1548                 rs->sr_text = "no request data expected";
1549                 return rs->sr_err = LDAP_PROTOCOL_ERROR;
1550         }
1551
1552         rs->sr_err = backend_check_restrictions( op, rs, 
1553                         (struct berval *)&slap_EXOP_WHOAMI );
1554         if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
1555
1556         /* if auth'd by back-ldap and request is proxied, forward it */
1557         if ( op->o_conn->c_authz_backend && !strcmp(op->o_conn->c_authz_backend->be_type, "ldap" ) && !dn_match(&op->o_ndn, &op->o_conn->c_ndn)) {
1558                 struct ldapconn *lc;
1559
1560                 LDAPControl c, *ctrls[2] = {NULL, NULL};
1561                 LDAPMessage *res;
1562                 Operation op2 = *op;
1563                 ber_int_t msgid;
1564                 int doretry = 1;
1565
1566                 ctrls[0] = &c;
1567                 op2.o_ndn = op->o_conn->c_ndn;
1568                 lc = ldap_back_getconn(&op2, rs, LDAP_BACK_SENDERR);
1569                 if (!lc || !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR )) {
1570                         return -1;
1571                 }
1572                 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1573                 c.ldctl_iscritical = 1;
1574                 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
1575                 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
1576                 strcpy(c.ldctl_value.bv_val, "dn:");
1577                 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
1578
1579 retry:
1580                 rs->sr_err = ldap_whoami(lc->lc_ld, ctrls, NULL, &msgid);
1581                 if (rs->sr_err == LDAP_SUCCESS) {
1582                         if (ldap_result(lc->lc_ld, msgid, 1, NULL, &res) == -1) {
1583                                 ldap_get_option(lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1584                                         &rs->sr_err);
1585                                 if ( rs->sr_err == LDAP_SERVER_DOWN && doretry ) {
1586                                         doretry = 0;
1587                                         if ( ldap_back_retry( lc, op, rs, LDAP_BACK_SENDERR ) )
1588                                                 goto retry;
1589                                 }
1590                                 ldap_back_freeconn( op, lc );
1591                                 lc = NULL;
1592
1593                         } else {
1594                                 rs->sr_err = ldap_parse_whoami(lc->lc_ld, res, &bv);
1595                                 ldap_msgfree(res);
1596                         }
1597                 }
1598                 ch_free(c.ldctl_value.bv_val);
1599                 if (rs->sr_err != LDAP_SUCCESS) {
1600                         rs->sr_err = slap_map_api2result( rs );
1601                 }
1602
1603                 if ( lc != NULL ) {
1604                         ldap_back_release_conn( &op2, rs, lc );
1605                 }
1606
1607         } else {
1608         /* else just do the same as before */
1609                 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
1610                 if ( !BER_BVISEMPTY( &op->o_dn ) ) {
1611                         bv->bv_len = op->o_dn.bv_len + STRLENOF("dn:");
1612                         bv->bv_val = ch_malloc( bv->bv_len + 1 );
1613                         AC_MEMCPY( bv->bv_val, "dn:", STRLENOF("dn:") );
1614                         AC_MEMCPY( &bv->bv_val[STRLENOF("dn:")], op->o_dn.bv_val,
1615                                 op->o_dn.bv_len );
1616                         bv->bv_val[bv->bv_len] = '\0';
1617                 } else {
1618                         bv->bv_len = 0;
1619                         bv->bv_val = NULL;
1620                 }
1621         }
1622
1623         rs->sr_rspdata = bv;
1624         return rs->sr_err;
1625 }
1626
1627
1628 static int
1629 parse_idassert(
1630     BackendDB   *be,
1631     const char  *fname,
1632     int         lineno,
1633     int         argc,
1634     char        **argv
1635 )
1636 {
1637         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
1638
1639         /* identity assertion mode */
1640         if ( strcasecmp( argv[0], "idassert-mode" ) == 0 ) {
1641                 if ( argc < 2 ) {
1642                         Debug( LDAP_DEBUG_ANY,
1643                                 "%s: line %d: illegal args number %d in \"idassert-mode <args> [<flag> [...]]\" line.\n",
1644                                 fname, lineno, argc );
1645                         return 1;
1646                 }
1647
1648                 if ( strcasecmp( argv[1], "legacy" ) == 0 ) {
1649                         /* will proxyAuthz as client's identity only if bound */
1650                         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
1651
1652                 } else if ( strcasecmp( argv[1], "self" ) == 0 ) {
1653                         /* will proxyAuthz as client's identity */
1654                         li->idassert_mode = LDAP_BACK_IDASSERT_SELF;
1655
1656                 } else if ( strcasecmp( argv[1], "anonymous" ) == 0 ) {
1657                         /* will proxyAuthz as anonymous */
1658                         li->idassert_mode = LDAP_BACK_IDASSERT_ANONYMOUS;
1659
1660                 } else if ( strcasecmp( argv[1], "none" ) == 0 ) {
1661                         /* will not proxyAuthz */
1662                         li->idassert_mode = LDAP_BACK_IDASSERT_NOASSERT;
1663
1664                 } else {
1665                         struct berval   id;
1666                         int             rc;
1667
1668                         /* will proxyAuthz as argv[1] */
1669                         ber_str2bv( argv[1], 0, 0, &id );
1670
1671                         if ( strncasecmp( id.bv_val, "u:", STRLENOF( "u:" ) ) == 0 ) {
1672                                 /* force lowercase... */
1673                                 id.bv_val[0] = 'u';
1674                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
1675                                 ber_dupbv( &li->idassert_authzID, &id );
1676
1677                         } else {
1678                                 struct berval   dn;
1679
1680                                 /* default is DN? */
1681                                 if ( strncasecmp( id.bv_val, "dn:", STRLENOF( "dn:" ) ) == 0 ) {
1682                                         id.bv_val += STRLENOF( "dn:" );
1683                                         id.bv_len -= STRLENOF( "dn:" );
1684                                 }
1685
1686                                 rc = dnNormalize( 0, NULL, NULL, &id, &dn, NULL );
1687                                 if ( rc != LDAP_SUCCESS ) {
1688                                         Debug( LDAP_DEBUG_ANY,
1689                                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
1690                                                 fname, lineno, argv[1] );
1691                                         return 1;
1692                                 }
1693
1694                                 li->idassert_authzID.bv_len = STRLENOF( "dn:" ) + dn.bv_len;
1695                                 li->idassert_authzID.bv_val = ch_malloc( li->idassert_authzID.bv_len + 1 );
1696                                 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1697                                 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], dn.bv_val, dn.bv_len + 1 );
1698                                 ch_free( dn.bv_val );
1699
1700                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
1701                         }
1702                 }
1703
1704                 for ( argc -= 2, argv += 2; argc--; argv++ ) {
1705                         if ( strcasecmp( argv[0], "override" ) == 0 ) {
1706                                 li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
1707
1708                         } else {
1709                                 Debug( LDAP_DEBUG_ANY,
1710                                         "%s: line %d: unknown flag \"%s\" "
1711                                         "in \"idassert-mode <args> "
1712                                         "[<flags>]\" line.\n",
1713                                         fname, lineno, argv[0] );
1714                                 return 1;
1715                         }
1716                 }
1717
1718         /* name to use for proxyAuthz propagation */
1719         } else if ( strcasecmp( argv[0], "idassert-authcdn" ) == 0
1720                         || strcasecmp( argv[0], "proxyauthzdn" ) == 0 )
1721         {
1722                 struct berval   dn;
1723                 int             rc;
1724
1725                 /* FIXME: "proxyauthzdn" is no longer documented, and
1726                  * temporarily supported for backwards compatibility */
1727
1728                 if ( argc != 2 ) {
1729                         fprintf( stderr,
1730         "%s: line %d: missing name in \"%s <name>\" line\n",
1731                             fname, lineno, argv[0] );
1732                         return( 1 );
1733                 }
1734
1735                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
1736                         fprintf( stderr, "%s: line %d: "
1737                                         "authcDN already defined; replacing...\n",
1738                                         fname, lineno );
1739                         ch_free( li->idassert_authcDN.bv_val );
1740                 }
1741                 
1742                 ber_str2bv( argv[1], 0, 0, &dn );
1743                 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
1744                 if ( rc != LDAP_SUCCESS ) {
1745                         Debug( LDAP_DEBUG_ANY,
1746                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
1747                                 fname, lineno, argv[1] );
1748                         return 1;
1749                 }
1750
1751         /* password to use for proxyAuthz propagation */
1752         } else if ( strcasecmp( argv[0], "idassert-passwd" ) == 0
1753                         || strcasecmp( argv[0], "proxyauthzpw" ) == 0 )
1754         {
1755                 /* FIXME: "proxyauthzpw" is no longer documented, and
1756                  * temporarily supported for backwards compatibility */
1757
1758                 if ( argc != 2 ) {
1759                         fprintf( stderr,
1760         "%s: line %d: missing password in \"%s <password>\" line\n",
1761                             fname, lineno, argv[0] );
1762                         return( 1 );
1763                 }
1764
1765                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
1766                         fprintf( stderr, "%s: line %d: "
1767                                         "passwd already defined; replacing...\n",
1768                                         fname, lineno );
1769                         ch_free( li->idassert_passwd.bv_val );
1770                 }
1771                 
1772                 ber_str2bv( argv[1], 0, 1, &li->idassert_passwd );
1773
1774         /* rules to accept identity assertion... */
1775         } else if ( strcasecmp( argv[0], "idassert-authzFrom" ) == 0 ) {
1776                 struct berval   rule;
1777
1778                 ber_str2bv( argv[1], 0, 1, &rule );
1779
1780                 ber_bvarray_add( &li->idassert_authz, &rule );
1781
1782         } else if ( strcasecmp( argv[0], "idassert-method" ) == 0 ) {
1783                 char    *argv1;
1784
1785                 if ( argc < 2 ) {
1786                         fprintf( stderr,
1787         "%s: line %d: missing method in \"%s <method>\" line\n",
1788                             fname, lineno, argv[0] );
1789                         return( 1 );
1790                 }
1791
1792                 argv1 = argv[1];
1793                 if ( strncasecmp( argv1, "bindmethod=", STRLENOF( "bindmethod=" ) ) == 0 ) {
1794                         argv1 += STRLENOF( "bindmethod=" );
1795                 }
1796
1797                 if ( strcasecmp( argv1, "none" ) == 0 ) {
1798                         /* FIXME: is this at all useful? */
1799                         li->idassert_authmethod = LDAP_AUTH_NONE;
1800
1801                         if ( argc != 2 ) {
1802                                 fprintf( stderr,
1803         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
1804                                         fname, lineno, argv[0], argv[1] );
1805                         }
1806
1807                 } else if ( strcasecmp( argv1, "simple" ) == 0 ) {
1808                         int     arg;
1809
1810                         for ( arg = 2; arg < argc; arg++ ) {
1811                                 if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
1812                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
1813                                         struct berval   dn;
1814                                         int             rc;
1815
1816                                         if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
1817                                                 fprintf( stderr, "%s: line %d: "
1818                                                                 "SASL authcDN already defined; replacing...\n",
1819                                                                 fname, lineno );
1820                                                 ch_free( li->idassert_authcDN.bv_val );
1821                                         }
1822                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
1823                                                 val += STRLENOF( "dn:" );
1824                                         }
1825
1826                                         ber_str2bv( val, 0, 0, &dn );
1827                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
1828                                         if ( rc != LDAP_SUCCESS ) {
1829                                                 Debug( LDAP_DEBUG_ANY,
1830                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
1831                                                         fname, lineno, val );
1832                                                 return 1;
1833                                         }
1834
1835                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
1836                                         char    *val = argv[arg] + STRLENOF( "cred=" );
1837
1838                                         if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
1839                                                 fprintf( stderr, "%s: line %d: "
1840                                                                 "SASL cred already defined; replacing...\n",
1841                                                                 fname, lineno );
1842                                                 ch_free( li->idassert_passwd.bv_val );
1843                                         }
1844                                         ber_str2bv( val, 0, 1, &li->idassert_passwd );
1845
1846                                 } else {
1847                                         fprintf( stderr, "%s: line %d: "
1848                                                         "unknown parameter %s\n",
1849                                                         fname, lineno, argv[arg] );
1850                                         return 1;
1851                                 }
1852                         }
1853
1854                         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
1855
1856                 } else if ( strcasecmp( argv1, "sasl" ) == 0 ) {
1857 #ifdef HAVE_CYRUS_SASL
1858                         int     arg;
1859
1860                         for ( arg = 2; arg < argc; arg++ ) {
1861                                 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
1862                                         char    *val = argv[arg] + STRLENOF( "mech=" );
1863
1864                                         if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
1865                                                 fprintf( stderr, "%s: line %d: "
1866                                                                 "SASL mech already defined; replacing...\n",
1867                                                                 fname, lineno );
1868                                                 ch_free( li->idassert_sasl_mech.bv_val );
1869                                         }
1870                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_mech );
1871
1872                                 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
1873                                         char    *val = argv[arg] + STRLENOF( "realm=" );
1874
1875                                         if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
1876                                                 fprintf( stderr, "%s: line %d: "
1877                                                                 "SASL realm already defined; replacing...\n",
1878                                                                 fname, lineno );
1879                                                 ch_free( li->idassert_sasl_realm.bv_val );
1880                                         }
1881                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_realm );
1882
1883                                 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
1884                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
1885                                         struct berval   dn;
1886                                         int             rc;
1887
1888                                         if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
1889                                                 fprintf( stderr, "%s: line %d: "
1890                                                                 "SASL authcDN already defined; replacing...\n",
1891                                                                 fname, lineno );
1892                                                 ch_free( li->idassert_authcDN.bv_val );
1893                                         }
1894                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
1895                                                 val += STRLENOF( "dn:" );
1896                                         }
1897
1898                                         ber_str2bv( val, 0, 0, &dn );
1899                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
1900                                         if ( rc != LDAP_SUCCESS ) {
1901                                                 Debug( LDAP_DEBUG_ANY,
1902                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
1903                                                         fname, lineno, val );
1904                                                 return 1;
1905                                         }
1906
1907                                 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
1908                                         char    *val = argv[arg] + STRLENOF( "authcid=" );
1909
1910                                         if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
1911                                                 fprintf( stderr, "%s: line %d: "
1912                                                                 "SASL authcID already defined; replacing...\n",
1913                                                                 fname, lineno );
1914                                                 ch_free( li->idassert_authcID.bv_val );
1915                                         }
1916                                         if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
1917                                                 val += STRLENOF( "u:" );
1918                                         }
1919                                         ber_str2bv( val, 0, 1, &li->idassert_authcID );
1920
1921                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
1922                                         char    *val = argv[arg] + STRLENOF( "cred=" );
1923
1924                                         if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
1925                                                 fprintf( stderr, "%s: line %d: "
1926                                                                 "SASL cred already defined; replacing...\n",
1927                                                                 fname, lineno );
1928                                                 ch_free( li->idassert_passwd.bv_val );
1929                                         }
1930                                         ber_str2bv( val, 0, 1, &li->idassert_passwd );
1931
1932                                 } else if ( strncasecmp( argv[arg], "authz=", STRLENOF( "authz=" ) ) == 0 ) {
1933                                         char    *val = argv[arg] + STRLENOF( "authz=" );
1934
1935                                         if ( strcasecmp( val, "proxyauthz" ) == 0 ) {
1936                                                 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
1937
1938                                         } else if ( strcasecmp( val, "native" ) == 0 ) {
1939                                                 li->idassert_flags |= LDAP_BACK_AUTH_NATIVE_AUTHZ;
1940
1941                                         } else {
1942                                                 fprintf( stderr, "%s: line %d: "
1943                                                         "unknown authz mode \"%s\"\n",
1944                                                         fname, lineno, val );
1945                                                 return 1;
1946                                         }
1947
1948                                 } else {
1949                                         fprintf( stderr, "%s: line %d: "
1950                                                         "unknown SASL parameter %s\n",
1951                                                         fname, lineno, argv[arg] );
1952                                         return 1;
1953                                 }
1954                         }
1955
1956                         li->idassert_authmethod = LDAP_AUTH_SASL;
1957
1958 #else /* !HAVE_CYRUS_SASL */
1959                         fprintf( stderr, "%s: line %d: "
1960                                         "compile --with-cyrus-sasl to enable SASL auth\n",
1961                                         fname, lineno );
1962                         return 1;
1963 #endif /* !HAVE_CYRUS_SASL */
1964
1965                 } else {
1966                         fprintf( stderr, "%s: line %d: "
1967                                         "unhandled idassert-method method %s\n",
1968                                         fname, lineno, argv[1] );
1969                         return 1;
1970                 }
1971
1972         } else {
1973                 return SLAP_CONF_UNKNOWN;
1974         }
1975
1976         return 0;
1977 }
1978
1979 static int
1980 parse_acl_auth(
1981     BackendDB   *be,
1982     const char  *fname,
1983     int         lineno,
1984     int         argc,
1985     char        **argv
1986 )
1987 {
1988         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
1989
1990         /* name to use for remote ACL access */
1991         if ( strcasecmp( argv[0], "acl-authcdn" ) == 0
1992                         || strcasecmp( argv[0], "binddn" ) == 0 )
1993         {
1994                 struct berval   dn;
1995                 int             rc;
1996
1997                 /* FIXME: "binddn" is no longer documented, and
1998                  * temporarily supported for backwards compatibility */
1999
2000                 if ( argc != 2 ) {
2001                         fprintf( stderr,
2002         "%s: line %d: missing name in \"%s <name>\" line\n",
2003                             fname, lineno, argv[0] );
2004                         return( 1 );
2005                 }
2006
2007                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
2008                         fprintf( stderr, "%s: line %d: "
2009                                         "authcDN already defined; replacing...\n",
2010                                         fname, lineno );
2011                         ch_free( li->acl_authcDN.bv_val );
2012                 }
2013                 
2014                 ber_str2bv( argv[1], 0, 0, &dn );
2015                 rc = dnNormalize( 0, NULL, NULL, &dn, &li->acl_authcDN, NULL );
2016                 if ( rc != LDAP_SUCCESS ) {
2017                         Debug( LDAP_DEBUG_ANY,
2018                                 "%s: line %d: acl ID \"%s\" is not a valid DN\n",
2019                                 fname, lineno, argv[1] );
2020                         return 1;
2021                 }
2022
2023         /* password to use for remote ACL access */
2024         } else if ( strcasecmp( argv[0], "acl-passwd" ) == 0
2025                         || strcasecmp( argv[0], "bindpw" ) == 0 )
2026         {
2027                 /* FIXME: "bindpw" is no longer documented, and
2028                  * temporarily supported for backwards compatibility */
2029
2030                 if ( argc != 2 ) {
2031                         fprintf( stderr,
2032         "%s: line %d: missing password in \"%s <password>\" line\n",
2033                             fname, lineno, argv[0] );
2034                         return( 1 );
2035                 }
2036
2037                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
2038                         fprintf( stderr, "%s: line %d: "
2039                                         "passwd already defined; replacing...\n",
2040                                         fname, lineno );
2041                         ch_free( li->acl_passwd.bv_val );
2042                 }
2043                 
2044                 ber_str2bv( argv[1], 0, 1, &li->acl_passwd );
2045
2046         } else if ( strcasecmp( argv[0], "acl-method" ) == 0 ) {
2047                 char    *argv1;
2048
2049                 if ( argc < 2 ) {
2050                         fprintf( stderr,
2051         "%s: line %d: missing method in \"%s <method>\" line\n",
2052                             fname, lineno, argv[0] );
2053                         return( 1 );
2054                 }
2055
2056                 argv1 = argv[1];
2057                 if ( strncasecmp( argv1, "bindmethod=", STRLENOF( "bindmethod=" ) ) == 0 ) {
2058                         argv1 += STRLENOF( "bindmethod=" );
2059                 }
2060
2061                 if ( strcasecmp( argv1, "none" ) == 0 ) {
2062                         /* FIXME: is this at all useful? */
2063                         li->acl_authmethod = LDAP_AUTH_NONE;
2064
2065                         if ( argc != 2 ) {
2066                                 fprintf( stderr,
2067         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
2068                                         fname, lineno, argv[0], argv[1] );
2069                         }
2070
2071                 } else if ( strcasecmp( argv1, "simple" ) == 0 ) {
2072                         li->acl_authmethod = LDAP_AUTH_SIMPLE;
2073
2074                         if ( argc != 2 ) {
2075                                 fprintf( stderr,
2076         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
2077                                         fname, lineno, argv[0], argv[1] );
2078                         }
2079
2080                 } else if ( strcasecmp( argv1, "sasl" ) == 0 ) {
2081 #ifdef HAVE_CYRUS_SASL
2082                         int     arg;
2083
2084                         for ( arg = 2; arg < argc; arg++ ) {
2085                                 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
2086                                         char    *val = argv[arg] + STRLENOF( "mech=" );
2087
2088                                         if ( !BER_BVISNULL( &li->acl_sasl_mech ) ) {
2089                                                 fprintf( stderr, "%s: line %d: "
2090                                                                 "SASL mech already defined; replacing...\n",
2091                                                                 fname, lineno );
2092                                                 ch_free( li->acl_sasl_mech.bv_val );
2093                                         }
2094                                         ber_str2bv( val, 0, 1, &li->acl_sasl_mech );
2095
2096                                 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
2097                                         char    *val = argv[arg] + STRLENOF( "realm=" );
2098
2099                                         if ( !BER_BVISNULL( &li->acl_sasl_realm ) ) {
2100                                                 fprintf( stderr, "%s: line %d: "
2101                                                                 "SASL realm already defined; replacing...\n",
2102                                                                 fname, lineno );
2103                                                 ch_free( li->acl_sasl_realm.bv_val );
2104                                         }
2105                                         ber_str2bv( val, 0, 1, &li->acl_sasl_realm );
2106
2107                                 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
2108                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
2109                                         struct berval   dn;
2110                                         int             rc;
2111
2112                                         if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
2113                                                 fprintf( stderr, "%s: line %d: "
2114                                                                 "SASL authcDN already defined; replacing...\n",
2115                                                                 fname, lineno );
2116                                                 ch_free( li->acl_authcDN.bv_val );
2117                                         }
2118                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
2119                                                 val += STRLENOF( "dn:" );
2120                                         }
2121
2122                                         ber_str2bv( val, 0, 0, &dn );
2123                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->acl_authcDN, NULL );
2124                                         if ( rc != LDAP_SUCCESS ) {
2125                                                 Debug( LDAP_DEBUG_ANY,
2126                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
2127                                                         fname, lineno, val );
2128                                                 return 1;
2129                                         }
2130
2131                                 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
2132                                         char    *val = argv[arg] + STRLENOF( "authcid=" );
2133
2134                                         if ( !BER_BVISNULL( &li->acl_authcID ) ) {
2135                                                 fprintf( stderr, "%s: line %d: "
2136                                                                 "SASL authcID already defined; replacing...\n",
2137                                                                 fname, lineno );
2138                                                 ch_free( li->acl_authcID.bv_val );
2139                                         }
2140                                         if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
2141                                                 val += STRLENOF( "u:" );
2142                                         }
2143                                         ber_str2bv( val, 0, 1, &li->acl_authcID );
2144
2145                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
2146                                         char    *val = argv[arg] + STRLENOF( "cred=" );
2147
2148                                         if ( !BER_BVISNULL( &li->acl_passwd ) ) {
2149                                                 fprintf( stderr, "%s: line %d: "
2150                                                                 "SASL cred already defined; replacing...\n",
2151                                                                 fname, lineno );
2152                                                 ch_free( li->acl_passwd.bv_val );
2153                                         }
2154                                         ber_str2bv( val, 0, 1, &li->acl_passwd );
2155
2156                                 } else {
2157                                         fprintf( stderr, "%s: line %d: "
2158                                                         "unknown SASL parameter %s\n",
2159                                                         fname, lineno, argv[arg] );
2160                                         return 1;
2161                                 }
2162                         }
2163
2164                         li->acl_authmethod = LDAP_AUTH_SASL;
2165
2166 #else /* !HAVE_CYRUS_SASL */
2167                         fprintf( stderr, "%s: line %d: "
2168                                         "compile --with-cyrus-sasl to enable SASL auth\n",
2169                                         fname, lineno );
2170                         return 1;
2171 #endif /* !HAVE_CYRUS_SASL */
2172
2173                 } else {
2174                         fprintf( stderr, "%s: line %d: "
2175                                         "unhandled acl-method method %s\n",
2176                                         fname, lineno, argv[1] );
2177                         return 1;
2178                 }
2179
2180         } else {
2181                 return SLAP_CONF_UNKNOWN;
2182         }
2183
2184         return 0;
2185 }
2186