]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
more fixes to parsing of (deprecated) config directives (ITS#3915)
[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_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_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_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_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_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_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_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_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_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_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_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_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                                 value_add_one( &c->rvalue_vals, &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                                 value_add_one( &c->rvalue_vals, &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                                 value_add_one( &c->rvalue_vals, &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                                 value_add_one( &c->rvalue_vals, &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                                 value_add_one( &c->rvalue_vals, &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                         rc = 1;
546                         break;
547
548                 case LDAP_BACK_CFG_ACL_AUTHCDN:
549                 case LDAP_BACK_CFG_ACL_PASSWD:
550                 case LDAP_BACK_CFG_ACL_METHOD:
551                         /* handled by LDAP_BACK_CFG_ACL_BIND */
552                         rc = 1;
553                         break;
554
555                 case LDAP_BACK_CFG_ACL_BIND:
556                         bindconf_free( &li->acl_sb );
557                         break;
558
559                 case LDAP_BACK_CFG_IDASSERT_MODE:
560                 case LDAP_BACK_CFG_IDASSERT_AUTHCDN:
561                 case LDAP_BACK_CFG_IDASSERT_PASSWD:
562                 case LDAP_BACK_CFG_IDASSERT_METHOD:
563                         /* handled by LDAP_BACK_CFG_IDASSERT_BIND */
564                         rc = 1;
565                         break;
566
567                 case LDAP_BACK_CFG_IDASSERT_AUTHZFROM:
568                         if ( li->idassert_authz != NULL ) {
569                                 ber_bvarray_free( li->idassert_authz );
570                                 li->idassert_authz = NULL;
571                         }
572                         break;
573
574                 case LDAP_BACK_CFG_IDASSERT_BIND:
575                         bindconf_free( &li->idassert_sb );
576                         break;
577
578                 case LDAP_BACK_CFG_REBIND:
579                 case LDAP_BACK_CFG_CHASE:
580                 case LDAP_BACK_CFG_T_F:
581                 case LDAP_BACK_CFG_WHOAMI:
582                         rc = 1;
583                         break;
584
585                 default:
586                         /* FIXME: we need to handle all... */
587                         assert( 0 );
588                         break;
589                 }
590                 return rc;
591
592         }
593
594         switch( c->type ) {
595         case LDAP_BACK_CFG_URI: {
596                 LDAPURLDesc     *tmpludp;
597 #if 0
598                 char            **urllist;
599 #endif
600                 int             urlrc, i;
601
602                 if ( c->argc != 2 ) {
603                         fprintf( stderr, "%s: line %d: "
604                                         "missing uri "
605                                         "in \"uri <uri>\" line\n",
606                                         c->fname, c->lineno );
607                         return 1;
608                 }
609
610                 if ( li->url != NULL ) {
611                         ch_free( li->url );
612                 }
613
614                 if ( li->lud != NULL ) {
615                         ldap_free_urllist( li->lud );
616                 }
617
618 #if 0
619                 /* PARANOID: DN and more are not required nor allowed */
620                 urlrc = ldap_url_parselist_ext( &li->lud, c->value_string, "\t" );
621 #else
622                 urlrc = ldap_url_parselist( &li->lud, c->value_string );
623 #endif
624                 if ( urlrc != LDAP_URL_SUCCESS ) {
625                         char    *why;
626
627                         switch ( urlrc ) {
628                         case LDAP_URL_ERR_MEM:
629                                 why = "no memory";
630                                 break;
631                         case LDAP_URL_ERR_PARAM:
632                                 why = "parameter is bad";
633                                 break;
634                         case LDAP_URL_ERR_BADSCHEME:
635                                 why = "URL doesn't begin with \"[c]ldap[si]://\"";
636                                 break;
637                         case LDAP_URL_ERR_BADENCLOSURE:
638                                 why = "URL is missing trailing \">\"";
639                                 break;
640                         case LDAP_URL_ERR_BADURL:
641                                 why = "URL is bad";
642                         case LDAP_URL_ERR_BADHOST:
643                                 why = "host/port is bad";
644                                 break;
645                         case LDAP_URL_ERR_BADATTRS:
646                                 why = "bad (or missing) attributes";
647                                 break;
648                         case LDAP_URL_ERR_BADSCOPE:
649                                 why = "scope string is invalid (or missing)";
650                                 break;
651                         case LDAP_URL_ERR_BADFILTER:
652                                 why = "bad or missing filter";
653                                 break;
654                         case LDAP_URL_ERR_BADEXTS:
655                                 why = "bad or missing extensions";
656                                 break;
657                         default:
658                                 why = "unknown reason";
659                                 break;
660                         }
661                         fprintf( stderr, "%s: line %d: "
662                                         "unable to parse uri \"%s\" "
663                                         "in \"uri <uri>\" line: %s\n",
664                                         c->fname, c->lineno, c->value_string, why );
665                         return 1;
666                 }
667
668                 for ( i = 0, tmpludp = li->lud;
669                                 tmpludp;
670                                 i++, tmpludp = tmpludp->lud_next )
671                 {
672                         if ( ( tmpludp->lud_dn != NULL
673                                                 && tmpludp->lud_dn[0] != '\0' )
674                                         || tmpludp->lud_attrs != NULL
675                                         /* || tmpludp->lud_scope != LDAP_SCOPE_DEFAULT */
676                                         || tmpludp->lud_filter != NULL
677                                         || tmpludp->lud_exts != NULL )
678                         {
679                                 fprintf( stderr, "%s: line %d: "
680                                                 "warning, only protocol, "
681                                                 "host and port allowed "
682                                                 "in \"uri <uri>\" statement "
683                                                 "for uri #%d of \"%s\"\n",
684                                                 c->fname, c->lineno, i, c->value_string );
685                         }
686                 }
687
688 #if 0
689                 for ( i = 0, tmpludp = li->lud;
690                                 tmpludp;
691                                 i++, tmpludp = tmpludp->lud_next )
692                         /* just count */
693                         ;
694                 urllist = ch_calloc( sizeof( char * ), i + 1 );
695
696                 for ( i = 0, tmpludp = li->lud;
697                                 tmpludp;
698                                 i++, tmpludp = tmpludp->lud_next )
699                 {
700                         LDAPURLDesc     tmplud;
701                         ber_len_t       oldlen = 0, len;
702
703                         tmplud = *tmpludp;
704                         tmplud.lud_dn = "";
705                         tmplud.lud_attrs = NULL;
706                         tmplud.lud_filter = NULL;
707                         if ( !ldap_is_ldapi_url( tmplud.lud_scheme ) ) {
708                                 tmplud.lud_exts = NULL;
709                                 tmplud.lud_crit_exts = 0;
710                         }
711
712                         urllist[ i ]  = ldap_url_desc2str( &tmplud );
713
714                         if ( urllist[ i ] == NULL ) {
715                                 fprintf( stderr, "%s: line %d: "
716                                         "unable to rebuild uri "
717                                         "in \"uri <uri>\" statement "
718                                         "for \"%s\"\n",
719                                         c->fname, c->lineno, argv[ 1 ] );
720                                 return 1;
721                         }
722                 }
723
724                 li->url = ldap_charray2str( urllist, " " );
725                 ldap_charray_free( urllist );
726 #else
727                 li->url = c->value_string;
728 #endif
729                 break;
730         }
731
732         case LDAP_BACK_CFG_TLS:
733                 i = verb_to_mask( c->argv[1], tls_mode );
734                 if ( BER_BVISNULL( &tls_mode[i].word ) ) {
735                         return 1;
736                 }
737                 li->flags &= ~LDAP_BACK_F_TLS_MASK;
738                 li->flags |= tls_mode[i].mask;
739                 break;
740
741         case LDAP_BACK_CFG_ACL_AUTHCDN:
742                 switch ( li->acl_authmethod ) {
743                 case LDAP_AUTH_NONE:
744                         li->acl_authmethod = LDAP_AUTH_SIMPLE;
745                         break;
746
747                 case LDAP_AUTH_SIMPLE:
748                         break;
749
750                 default:
751                         fprintf( stderr, "%s: line %d: "
752                                 "\"acl-authcDN <DN>\" incompatible "
753                                 "with auth method %d.",
754                                 c->fname, c->lineno, li->acl_authmethod );
755                         return 1;
756                 }
757                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
758                         free( li->acl_authcDN.bv_val );
759                 }
760                 li->acl_authcDN = c->value_ndn;
761                 break;
762
763         case LDAP_BACK_CFG_ACL_PASSWD:
764                 switch ( li->acl_authmethod ) {
765                 case LDAP_AUTH_NONE:
766                         li->acl_authmethod = LDAP_AUTH_SIMPLE;
767                         break;
768
769                 case LDAP_AUTH_SIMPLE:
770                         break;
771
772                 default:
773                         fprintf( stderr, "%s: line %d: "
774                                 "\"acl-passwd <cred>\" incompatible "
775                                 "with auth method %d.",
776                                 c->fname, c->lineno, li->acl_authmethod );
777                         return 1;
778                 }
779                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
780                         free( li->acl_passwd.bv_val );
781                 }
782                 ber_str2bv( c->argv[ 1 ], 0, 1, &li->acl_passwd );
783                 break;
784
785         case LDAP_BACK_CFG_ACL_METHOD:
786         case LDAP_BACK_CFG_ACL_BIND:
787                 for ( i = 1; i < c->argc; i++ ) {
788                         if ( bindconf_parse( c->argv[ i ], &li->acl_sb ) ) {
789                                 return 1;
790                         }
791                 }
792                 break;
793
794         case LDAP_BACK_CFG_IDASSERT_MODE:
795                 i = verb_to_mask( c->argv[1], idassert_mode );
796                 if ( BER_BVISNULL( &idassert_mode[i].word ) ) {
797                         if ( strncasecmp( c->argv[1], "u:", STRLENOF( "u:" ) ) == 0 ) {
798                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
799                                 ber_str2bv( c->argv[1], 0, 1, &li->idassert_authzID );
800                                 li->idassert_authzID.bv_val[ 0 ] = 'u';
801                                 
802                         } else {
803                                 struct berval   id, ndn;
804
805                                 ber_str2bv( c->argv[1], 0, 0, &id );
806
807                                 if ( strncasecmp( c->argv[1], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
808                                         id.bv_val += STRLENOF( "dn:" );
809                                         id.bv_len -= STRLENOF( "dn:" );
810                                 }
811
812                                 rc = dnNormalize( 0, NULL, NULL, &id, &ndn, NULL );
813                                 if ( rc != LDAP_SUCCESS ) {
814                                         Debug( LDAP_DEBUG_ANY,
815                                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
816                                                 c->fname, c->lineno, c->argv[1] );
817                                         return 1;
818                                 }
819
820                                 li->idassert_authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
821                                 li->idassert_authzID.bv_val = ch_malloc( li->idassert_authzID.bv_len + 1 );
822                                 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
823                                 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], ndn.bv_val, ndn.bv_len + 1 );
824                                 ch_free( ndn.bv_val );
825
826                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
827                         }
828
829                 } else {
830                         li->idassert_mode = idassert_mode[i].mask;
831                 }
832
833                 if ( c->argc > 2 ) {
834                         int     i;
835
836                         for ( i = 2; i < c->argc; i++ ) {
837                                 if ( strcasecmp( c->argv[ i ], "override" ) == 0 ) {
838                                         li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
839
840                                 } else if ( strcasecmp( c->argv[ i ], "prescriptive" ) == 0 ) {
841                                         li->idassert_flags |= LDAP_BACK_AUTH_PRESCRIPTIVE;
842
843                                 } else if ( strcasecmp( c->argv[ i ], "non-prescriptive" ) == 0 ) {
844                                         li->idassert_flags &= ( ~LDAP_BACK_AUTH_PRESCRIPTIVE );
845
846                                 } else {
847                                         Debug( LDAP_DEBUG_ANY,
848                                                 "%s: line %d: unknown flag #%d "
849                                                 "in \"idassert-mode <args> "
850                                                 "[<flags>]\" line.\n",
851                                                 c->fname, c->lineno, i - 2 );
852                                         return 1;
853                                 }
854                         }
855                 }
856                 break;
857
858         case LDAP_BACK_CFG_IDASSERT_AUTHCDN:
859                 switch ( li->idassert_authmethod ) {
860                 case LDAP_AUTH_NONE:
861                         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
862                         break;
863
864                 case LDAP_AUTH_SIMPLE:
865                         break;
866
867                 default:
868                         fprintf( stderr, "%s: line %d: "
869                                 "\"idassert-authcDN <DN>\" incompatible "
870                                 "with auth method %d.",
871                                 c->fname, c->lineno, li->idassert_authmethod );
872                         return 1;
873                 }
874                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
875                         free( li->idassert_authcDN.bv_val );
876                 }
877                 li->idassert_authcDN = c->value_ndn;
878                 break;
879
880         case LDAP_BACK_CFG_IDASSERT_PASSWD:
881                 switch ( li->idassert_authmethod ) {
882                 case LDAP_AUTH_NONE:
883                         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
884                         break;
885
886                 case LDAP_AUTH_SIMPLE:
887                         break;
888
889                 default:
890                         fprintf( stderr, "%s: line %d: "
891                                 "\"idassert-passwd <cred>\" incompatible "
892                                 "with auth method %d.",
893                                 c->fname, c->lineno, li->idassert_authmethod );
894                         return 1;
895                 }
896                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
897                         free( li->idassert_passwd.bv_val );
898                 }
899                 ber_str2bv( c->argv[ 1 ], 0, 1, &li->idassert_passwd );
900                 break;
901
902         case LDAP_BACK_CFG_IDASSERT_AUTHZFROM: {
903                 struct berval   bv;
904
905                 ber_str2bv( c->argv[ 1 ], 0, 1, &bv );
906                 ber_bvarray_add( &li->idassert_authz, &bv );
907                 } break;
908
909         case LDAP_BACK_CFG_IDASSERT_METHOD:
910                 /* no longer supported */
911                 fprintf( stderr, "%s: %d: "
912                         "\"idassert-method <args>\": "
913                         "no longer supported; use \"idassert-bind\".\n",
914                         c->fname, c->lineno );
915                 return 1;
916
917         case LDAP_BACK_CFG_IDASSERT_BIND:
918                 for ( i = 1; i < c->argc; i++ ) {
919                         if ( strncasecmp( c->argv[ i ], "mode=", STRLENOF( "mode=" ) ) == 0 ) {
920                                 char    *argvi = c->argv[ i ] + STRLENOF( "mode=" );
921                                 int     j;
922
923                                 j = verb_to_mask( argvi, idassert_mode );
924                                 if ( BER_BVISNULL( &idassert_mode[ j ].word ) ) {
925                                         fprintf( stderr, "%s: %d: "
926                                                 "\"idassert-bind <args>\": "
927                                                 "unknown mode \"%s\".\n",
928                                                 c->fname, c->lineno, argvi );
929                                         return 1;
930                                 }
931
932                                 li->idassert_mode = idassert_mode[ j ].mask;
933
934                         } else if ( strncasecmp( c->argv[ i ], "authz=", STRLENOF( "authz=" ) ) == 0 ) {
935                                 char    *argvi = c->argv[ i ] + STRLENOF( "authz=" );
936
937                                 if ( strcasecmp( argvi, "native" ) == 0 ) {
938                                         if ( li->idassert_authmethod != LDAP_AUTH_SASL ) {
939                                                 fprintf( stderr, "%s: %d: "
940                                                         "\"idassert-bind <args>\": "
941                                                         "authz=\"native\" incompatible "
942                                                         "with auth method.\n",
943                                                         c->fname, c->lineno );
944                                                 return 1;
945                                         }
946                                         li->idassert_flags |= LDAP_BACK_AUTH_NATIVE_AUTHZ;
947
948                                 } else if ( strcasecmp( argvi, "proxyAuthz" ) == 0 ) {
949                                         li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
950
951                                 } else {
952                                         fprintf( stderr, "%s: %d: "
953                                                 "\"idassert-bind <args>\": "
954                                                 "unknown authz \"%s\".\n",
955                                                 c->fname, c->lineno, argvi );
956                                         return 1;
957                                 }
958
959                         } else if ( strncasecmp( c->argv[ i ], "flags=", STRLENOF( "flags=" ) ) == 0 ) {
960                                 char    *argvi = c->argv[ i ] + STRLENOF( "flags=" );
961                                 char    **flags = ldap_str2charray( argvi, "," );
962                                 int     j;
963
964                                 if ( flags == NULL ) {
965                                         fprintf( stderr, "%s: %d: "
966                                                 "\"idassert-bind <args>\": "
967                                                 "unable to parse flags \"%s\".\n",
968                                                 c->fname, c->lineno, argvi );
969                                         return 1;
970                                 }
971
972                                 for ( j = 0; flags[ j ] != NULL; j++ ) {
973                                         if ( strcasecmp( flags[ j ], "override" ) == 0 ) {
974                                                 li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
975
976                                         } else if ( strcasecmp( flags[ j ], "prescriptive" ) == 0 ) {
977                                                 li->idassert_flags |= LDAP_BACK_AUTH_PRESCRIPTIVE;
978
979                                         } else if ( strcasecmp( flags[ j ], "non-prescriptive" ) == 0 ) {
980                                                 li->idassert_flags &= ( ~LDAP_BACK_AUTH_PRESCRIPTIVE );
981
982                                         } else {
983                                                 fprintf( stderr, "%s: %d: "
984                                                         "\"idassert-bind <args>\": "
985                                                         "unknown flag \"%s\".\n",
986                                                         c->fname, c->lineno, flags[ j ] );
987                                                 return 1;
988                                         }
989                                 }
990
991                                 ldap_charray_free( flags );
992
993                         } else if ( bindconf_parse( c->argv[ i ], &li->idassert_sb ) ) {
994                                 return 1;
995                         }
996                 }
997                 break;
998
999         case LDAP_BACK_CFG_REBIND: {
1000                 int     dorebind = 0;
1001
1002                 if ( c->argc == 1 ) {
1003                         /* legacy */
1004                         dorebind = 1;
1005
1006                 } else {
1007                         i = verb_to_mask( c->argv[1], yn_mode );
1008                         if ( BER_BVISNULL( &yn_mode[i].word ) ) {
1009                                 return 1;
1010                         }
1011                         if ( yn_mode[i].mask & LDAP_BACK_C_YES ) {
1012                                 dorebind = 1;
1013                         }
1014                 }
1015
1016                 if ( dorebind ) {
1017                         li->flags |= LDAP_BACK_F_SAVECRED;
1018
1019                 } else {
1020                         li->flags &= ~LDAP_BACK_F_SAVECRED;
1021                 }
1022                 break;
1023         }
1024
1025         case LDAP_BACK_CFG_CHASE: {
1026                 int     dochase = 0;
1027
1028                 if ( c->argc == 1 ) {
1029                         /* legacy */
1030                         dochase = 1;
1031
1032                 } else {
1033                         i = verb_to_mask( c->argv[1], yn_mode );
1034                         if ( BER_BVISNULL( &yn_mode[i].word ) ) {
1035                                 return 1;
1036                         }
1037                         if ( yn_mode[i].mask & LDAP_BACK_C_YES ) {
1038                                 dochase = 1;
1039                         }
1040                 }
1041
1042                 if ( dochase ) {
1043                         li->flags |= LDAP_BACK_F_CHASE_REFERRALS;
1044
1045                 } else {
1046                         li->flags &= ~LDAP_BACK_F_CHASE_REFERRALS;
1047                 }
1048                 break;
1049         }
1050
1051         case LDAP_BACK_CFG_T_F:
1052                 i = verb_to_mask( c->argv[1], t_f_mode );
1053                 if ( BER_BVISNULL( &t_f_mode[i].word ) ) {
1054                         return 1;
1055                 }
1056                 li->flags &= ~LDAP_BACK_F_SUPPORT_T_F_MASK;
1057                 li->flags |= t_f_mode[i].mask;
1058                 break;
1059
1060         case LDAP_BACK_CFG_WHOAMI: {
1061                 int     dowhoami = 0;
1062
1063                 if ( c->argc == 1 ) {
1064                         /* legacy */
1065                         dowhoami = 1;
1066
1067                 } else {
1068                         i = verb_to_mask( c->argv[1], yn_mode );
1069                         if ( BER_BVISNULL( &yn_mode[i].word ) ) {
1070                                 return 1;
1071                         }
1072                         if ( yn_mode[i].mask & LDAP_BACK_C_YES ) {
1073                                 dowhoami = 1;
1074                         }
1075                 }
1076
1077                 if ( dowhoami ) {
1078                         li->flags |= LDAP_BACK_F_PROXY_WHOAMI;
1079
1080                         load_extop( (struct berval *)&slap_EXOP_WHOAMI,
1081                                         0, ldap_back_exop_whoami );
1082
1083                 } else {
1084                         li->flags &= ~LDAP_BACK_F_PROXY_WHOAMI;
1085                 }
1086                 break;
1087         }
1088
1089         case LDAP_BACK_CFG_REWRITE:
1090                 fprintf( stderr, "%s: line %d: "
1091                         "rewrite/remap capabilities have been moved "
1092                         "to the \"rwm\" overlay; see slapo-rwm(5) "
1093                         "for details (hint: add \"overlay rwm\" "
1094                         "and prefix all directives with \"rwm-\").\n",
1095                         c->fname, c->lineno );
1096                 return 1;
1097                 
1098         default:
1099                 /* FIXME: try to catch inconsistencies */
1100                 assert( 0 );
1101                 break;
1102         }
1103
1104         return 0;
1105 }
1106
1107 int
1108 ldap_back_init_cf( BackendInfo *bi )
1109 {
1110         int                     rc;
1111         AttributeDescription    *ad = NULL;
1112         const char              *text;
1113
1114         bi->bi_cf_ocs = ldapocs;
1115
1116         rc = config_register_schema( ldapcfg, ldapocs );
1117         if ( rc ) {
1118                 return rc;
1119         }
1120
1121         /* setup olcDbAclPasswd and olcDbIDAssertPasswd 
1122          * to be base64-encoded when written in LDIF form;
1123          * basically, we don't care if it fails */
1124         rc = slap_str2ad( "olcDbACLPasswd", &ad, &text );
1125         if ( rc ) {
1126                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
1127                         "warning, unable to get \"olcDbACLPasswd\" "
1128                         "attribute description: %d: %s\n",
1129                         rc, text, 0 );
1130         } else {
1131                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
1132                         ad->ad_type->sat_oid );
1133         }
1134
1135         ad = NULL;
1136         rc = slap_str2ad( "olcDbIDAssertPasswd", &ad, &text );
1137         if ( rc ) {
1138                 Debug( LDAP_DEBUG_ANY, "config_back_initialize: "
1139                         "warning, unable to get \"olcDbIDAssertPasswd\" "
1140                         "attribute description: %d: %s\n",
1141                         rc, text, 0 );
1142         } else {
1143                 (void)ldif_must_b64_encode_register( ad->ad_cname.bv_val,
1144                         ad->ad_type->sat_oid );
1145         }
1146
1147         return 0;
1148 }
1149
1150
1151 static int
1152 parse_idassert( BackendDB *be, const char *fname, int lineno,
1153                 int argc, char **argv );
1154
1155 static int
1156 parse_acl_auth( BackendDB *be, const char *fname, int lineno,
1157                 int argc, char **argv );
1158
1159 int
1160 ldap_back_db_config(
1161                 BackendDB       *be,
1162                 const char      *fname,
1163                 int             lineno,
1164                 int             argc,
1165                 char            **argv )
1166 {
1167         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
1168
1169         if ( li == NULL ) {
1170                 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
1171                                 fname, lineno );
1172                 return 1;
1173         }
1174
1175         /* server address to query (no longer supported, use "uri" directive) */
1176         if ( strcasecmp( argv[0], "server" ) == 0 ) {
1177                 fprintf( stderr,
1178         "%s: line %d: \"server <address>\" directive is no longer supported.\n",
1179                                         fname, lineno );
1180                 return 1;
1181
1182         /* URI of server to query (obsoletes "server" directive) */
1183         } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
1184                 LDAPURLDesc     *tmpludp;
1185 #if 0
1186                 char            **urllist;
1187 #endif
1188                 int             urlrc, i;
1189
1190                 if ( argc != 2 ) {
1191                         fprintf( stderr, "%s: line %d: "
1192                                         "missing uri "
1193                                         "in \"uri <uri>\" line\n",
1194                                         fname, lineno );
1195                         return 1;
1196                 }
1197                 if ( li->url != NULL ) {
1198                         ch_free( li->url );
1199                 }
1200                 if ( li->lud != NULL ) {
1201                         ldap_free_urllist( li->lud );
1202                 }
1203
1204 #if 0
1205                 /* PARANOID: DN and more are not required nor allowed */
1206                 urlrc = ldap_url_parselist_ext( &li->lud, argv[ 1 ], "\t" );
1207 #else
1208                 urlrc = ldap_url_parselist( &li->lud, argv[ 1 ] );
1209 #endif
1210                 if ( urlrc != LDAP_URL_SUCCESS ) {
1211                         char    *why;
1212
1213                         switch ( urlrc ) {
1214                         case LDAP_URL_ERR_MEM:
1215                                 why = "no memory";
1216                                 break;
1217                         case LDAP_URL_ERR_PARAM:
1218                                 why = "parameter is bad";
1219                                 break;
1220                         case LDAP_URL_ERR_BADSCHEME:
1221                                 why = "URL doesn't begin with \"[c]ldap[si]://\"";
1222                                 break;
1223                         case LDAP_URL_ERR_BADENCLOSURE:
1224                                 why = "URL is missing trailing \">\"";
1225                                 break;
1226                         case LDAP_URL_ERR_BADURL:
1227                                 why = "URL is bad";
1228                         case LDAP_URL_ERR_BADHOST:
1229                                 why = "host/port is bad";
1230                                 break;
1231                         case LDAP_URL_ERR_BADATTRS:
1232                                 why = "bad (or missing) attributes";
1233                                 break;
1234                         case LDAP_URL_ERR_BADSCOPE:
1235                                 why = "scope string is invalid (or missing)";
1236                                 break;
1237                         case LDAP_URL_ERR_BADFILTER:
1238                                 why = "bad or missing filter";
1239                                 break;
1240                         case LDAP_URL_ERR_BADEXTS:
1241                                 why = "bad or missing extensions";
1242                                 break;
1243                         default:
1244                                 why = "unknown reason";
1245                                 break;
1246                         }
1247                         fprintf( stderr, "%s: line %d: "
1248                                         "unable to parse uri \"%s\" "
1249                                         "in \"uri <uri>\" line: %s\n",
1250                                         fname, lineno, argv[ 1 ], why );
1251                         return 1;
1252                 }
1253
1254                 for ( i = 0, tmpludp = li->lud;
1255                                 tmpludp;
1256                                 i++, tmpludp = tmpludp->lud_next )
1257                 {
1258                         if ( ( tmpludp->lud_dn != NULL
1259                                                 && tmpludp->lud_dn[0] != '\0' )
1260                                         || tmpludp->lud_attrs != NULL
1261                                         /* || tmpludp->lud_scope != LDAP_SCOPE_DEFAULT */
1262                                         || tmpludp->lud_filter != NULL
1263                                         || tmpludp->lud_exts != NULL )
1264                         {
1265                                 fprintf( stderr, "%s: line %d: "
1266                                                 "warning, only protocol, "
1267                                                 "host and port allowed "
1268                                                 "in \"uri <uri>\" statement "
1269                                                 "for uri #%d of \"%s\"\n",
1270                                                 fname, lineno, i, argv[1] );
1271                         }
1272                 }
1273
1274 #if 0
1275                 for ( i = 0, tmpludp = li->lud;
1276                                 tmpludp;
1277                                 i++, tmpludp = tmpludp->lud_next )
1278                         /* just count */
1279                         ;
1280                 urllist = ch_calloc( sizeof( char * ), i + 1 );
1281
1282                 for ( i = 0, tmpludp = li->lud;
1283                                 tmpludp;
1284                                 i++, tmpludp = tmpludp->lud_next )
1285                 {
1286                         LDAPURLDesc     tmplud;
1287                         ber_len_t       oldlen = 0, len;
1288
1289                         tmplud = *tmpludp;
1290                         tmplud.lud_dn = "";
1291                         tmplud.lud_attrs = NULL;
1292                         tmplud.lud_filter = NULL;
1293                         if ( !ldap_is_ldapi_url( tmplud.lud_scheme ) ) {
1294                                 tmplud.lud_exts = NULL;
1295                                 tmplud.lud_crit_exts = 0;
1296                         }
1297
1298                         urllist[ i ]  = ldap_url_desc2str( &tmplud );
1299
1300                         if ( urllist[ i ] == NULL ) {
1301                                 fprintf( stderr, "%s: line %d: "
1302                                         "unable to rebuild uri "
1303                                         "in \"uri <uri>\" statement "
1304                                         "for \"%s\"\n",
1305                                         fname, lineno, argv[ 1 ] );
1306                                 return 1;
1307                         }
1308                 }
1309
1310                 li->url = ldap_charray2str( urllist, " " );
1311                 ldap_charray_free( urllist );
1312 #else
1313                 li->url = ch_strdup( argv[ 1 ] );
1314 #endif
1315
1316         } else if ( strcasecmp( argv[0], "tls" ) == 0 ) {
1317                 if ( argc != 2 ) {
1318                         fprintf( stderr,
1319                 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
1320                                         fname, lineno );
1321                         return( 1 );
1322                 }
1323
1324                 /* none */
1325                 if ( strcasecmp( argv[1], "none" ) == 0 ) {
1326                         li->flags &= ~LDAP_BACK_F_TLS_MASK;
1327         
1328                 /* try start tls */
1329                 } else if ( strcasecmp( argv[1], "start" ) == 0 ) {
1330                         li->flags |= LDAP_BACK_F_TLS_USE_MASK;
1331         
1332                 /* try start tls */
1333                 } else if ( strcasecmp( argv[1], "try-start" ) == 0 ) {
1334                         li->flags &= ~LDAP_BACK_F_TLS_CRITICAL;
1335                         li->flags |= LDAP_BACK_F_USE_TLS;
1336         
1337                 /* propagate start tls */
1338                 } else if ( strcasecmp( argv[1], "propagate" ) == 0 ) {
1339                         li->flags |= LDAP_BACK_F_TLS_PROPAGATE_MASK;
1340                 
1341                 /* try start tls */
1342                 } else if ( strcasecmp( argv[1], "try-propagate" ) == 0 ) {
1343                         li->flags &= ~LDAP_BACK_F_TLS_CRITICAL;
1344                         li->flags |= LDAP_BACK_F_PROPAGATE_TLS;
1345
1346                 } else {
1347                         fprintf( stderr,
1348                 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
1349                                         fname, lineno, argv[1] );
1350                         return( 1 );
1351                 }
1352         
1353         /* remote ACL stuff... */
1354         } else if ( strncasecmp( argv[0], "acl-", STRLENOF( "acl-" ) ) == 0
1355                         || strncasecmp( argv[0], "bind", STRLENOF( "bind" ) ) == 0 )
1356         {
1357                 /* NOTE: "bind{DN,pw}" was initially used; it's now
1358                  * deprected and undocumented, it can be dropped at some
1359                  * point, since nobody should be really using it */
1360                 return parse_acl_auth( be, fname, lineno, argc, argv );
1361
1362         /* identity assertion stuff... */
1363         } else if ( strncasecmp( argv[0], "idassert-", STRLENOF( "idassert-" ) ) == 0
1364                         || strncasecmp( argv[0], "proxyauthz", STRLENOF( "proxyauthz" ) ) == 0 )
1365         {
1366                 /* NOTE: "proxyauthz{DN,pw}" was initially used; it's now
1367                  * deprected and undocumented, it can be dropped at some
1368                  * point, since nobody should be really using it */
1369                 return parse_idassert( be, fname, lineno, argc, argv );
1370
1371         /* save bind creds for referral rebinds? */
1372         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
1373                 switch ( argc ) {
1374                 case 1:
1375                         fprintf( stderr,
1376         "%s: line %d: \"rebind-as-user {NO|yes}\": use without args is deprecated.\n",
1377                                 fname, lineno );
1378         
1379                         li->flags |= LDAP_BACK_F_SAVECRED;
1380                         break;
1381
1382                 case 2:
1383                         if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1384                                 li->flags |= LDAP_BACK_F_SAVECRED;
1385
1386                         } else if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1387                                 li->flags &= ~LDAP_BACK_F_SAVECRED;
1388
1389                         } else {
1390                                 fprintf( stderr,
1391         "%s: line %d: \"rebind-as-user {NO|yes}\": unknown argument \"%s\".\n",
1392                                         fname, lineno, argv[ 1 ] );
1393                                 return( 1 );
1394                         }
1395                         break;
1396
1397                 default:
1398                         fprintf( stderr,
1399         "%s: line %d: \"rebind-as-user {NO|yes}\" needs 1 argument.\n",
1400                                         fname, lineno );
1401                         return( 1 );
1402                 }
1403
1404         } else if ( strcasecmp( argv[0], "chase-referrals" ) == 0 ) {
1405                 if ( argc != 2 ) {
1406                         fprintf( stderr,
1407         "%s: line %d: \"chase-referrals {YES|no}\" needs 1 argument.\n",
1408                                         fname, lineno );
1409                         return( 1 );
1410                 }
1411
1412                 /* this is the default; we add it because the default might change... */
1413                 if ( strcasecmp( argv[1], "yes" ) == 0 ) {
1414                         li->flags |= LDAP_BACK_F_CHASE_REFERRALS;
1415
1416                 } else if ( strcasecmp( argv[1], "no" ) == 0 ) {
1417                         li->flags &= ~LDAP_BACK_F_CHASE_REFERRALS;
1418
1419                 } else {
1420                         fprintf( stderr,
1421                 "%s: line %d: \"chase-referrals {YES|no}\": unknown argument \"%s\".\n",
1422                                         fname, lineno, argv[1] );
1423                         return( 1 );
1424                 }
1425         
1426         } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
1427                 if ( argc != 2 ) {
1428                         fprintf( stderr,
1429                 "%s: line %d: \"t-f-support {NO|yes|discover}\" needs 1 argument.\n",
1430                                         fname, lineno );
1431                         return( 1 );
1432                 }
1433
1434                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1435                         li->flags &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
1436
1437                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1438                         li->flags |= LDAP_BACK_F_SUPPORT_T_F;
1439
1440                 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1441                         li->flags |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
1442
1443                 } else {
1444                         fprintf( stderr,
1445         "%s: line %d: \"t-f-support {NO|yes|discover}\": unknown argument \"%s\".\n",
1446                                 fname, lineno, argv[ 1 ] );
1447                         return 1;
1448                 }
1449
1450         /* intercept exop_who_am_i? */
1451         } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
1452                 int     doload_extop = 0;
1453
1454                 switch ( argc ) {
1455                 case 1:
1456                         fprintf( stderr,
1457         "%s: line %d: \"proxy-whoami {NO|yes}\": use without args is deprecated.\n",
1458                                 fname, lineno );
1459         
1460                         doload_extop = 1;
1461                         break;
1462
1463                 case 2:
1464                         if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1465                                 doload_extop = 1;
1466
1467                         } else if ( strcasecmp( argv[ 1 ], "no" ) != 0 ) {
1468                                 fprintf( stderr,
1469         "%s: line %d: \"proxy-whoami {NO|yes}\": unknown argument \"%s\".\n",
1470                                         fname, lineno, argv[ 1 ] );
1471                                 return( 1 );
1472                         }
1473                         break;
1474
1475                 default:
1476                         fprintf( stderr,
1477         "%s: line %d: \"proxy-whoami {NO|yes}\" needs 1 argument.\n",
1478                                         fname, lineno );
1479                         return( 1 );
1480                 }
1481
1482                 if ( doload_extop ) {
1483                         li->flags |= LDAP_BACK_F_PROXY_WHOAMI;
1484
1485                         load_extop( (struct berval *)&slap_EXOP_WHOAMI,
1486                                         0, ldap_back_exop_whoami );
1487                 }
1488
1489         /* FIXME: legacy: intercept old rewrite/remap directives
1490          * and try to start the rwm overlay */
1491         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0
1492                         || strcasecmp( argv[0], "map" ) == 0
1493                         || strncasecmp( argv[0], "rewrite", STRLENOF( "rewrite" ) ) == 0 )
1494         {
1495 #if 0
1496                 fprintf( stderr, "%s: line %d: "
1497                         "rewrite/remap capabilities have been moved "
1498                         "to the \"rwm\" overlay; see slapo-rwm(5) "
1499                         "for details.  I'm trying to do my best "
1500                         "to preserve backwards compatibility...\n",
1501                         fname, lineno );
1502
1503                 if ( li->rwm_started == 0 ) {
1504                         if ( overlay_config( be, "rwm" ) ) {
1505                                 fprintf( stderr, "%s: line %d: "
1506                                         "unable to configure the \"rwm\" "
1507                                         "overlay, required by directive "
1508                                         "\"%s\".\n",
1509                                         fname, lineno, argv[0] );
1510 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1511                                 fprintf( stderr, "\thint: try loading the \"rwm.la\" dynamic module.\n" );
1512 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1513                                 return( 1 );
1514                         }
1515
1516                         fprintf( stderr, "%s: line %d: back-ldap: "
1517                                 "automatically starting \"rwm\" overlay, "
1518                                 "triggered by \"%s\" directive.\n",
1519                                 fname, lineno, argv[ 0 ] );
1520
1521                 /* this is the default; we add it because the default might change... */
1522                         li->rwm_started = 1;
1523
1524                         return ( *be->bd_info->bi_db_config )( be, fname, lineno, argc, argv );
1525                 }
1526 #else
1527                 fprintf( stderr, "%s: line %d: "
1528                         "rewrite/remap capabilities have been moved "
1529                         "to the \"rwm\" overlay; see slapo-rwm(5) "
1530                         "for details (hint: add \"overlay rwm\" "
1531                         "and prefix all directives with \"rwm-\").\n",
1532                         fname, lineno );
1533 #endif
1534
1535                 return 1;
1536         
1537         /* anything else */
1538         } else {
1539                 return SLAP_CONF_UNKNOWN;
1540         }
1541
1542         return 0;
1543 }
1544
1545 static int
1546 ldap_back_exop_whoami(
1547                 Operation       *op,
1548                 SlapReply       *rs )
1549 {
1550         struct berval *bv = NULL;
1551
1552         if ( op->oq_extended.rs_reqdata != NULL ) {
1553                 /* no request data should be provided */
1554                 rs->sr_text = "no request data expected";
1555                 return rs->sr_err = LDAP_PROTOCOL_ERROR;
1556         }
1557
1558         rs->sr_err = backend_check_restrictions( op, rs, 
1559                         (struct berval *)&slap_EXOP_WHOAMI );
1560         if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
1561
1562         /* if auth'd by back-ldap and request is proxied, forward it */
1563         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)) {
1564                 struct ldapconn *lc;
1565
1566                 LDAPControl c, *ctrls[2] = {NULL, NULL};
1567                 LDAPMessage *res;
1568                 Operation op2 = *op;
1569                 ber_int_t msgid;
1570                 int doretry = 1;
1571
1572                 ctrls[0] = &c;
1573                 op2.o_ndn = op->o_conn->c_ndn;
1574                 lc = ldap_back_getconn(&op2, rs, LDAP_BACK_SENDERR);
1575                 if (!lc || !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR )) {
1576                         return -1;
1577                 }
1578                 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1579                 c.ldctl_iscritical = 1;
1580                 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
1581                 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
1582                 strcpy(c.ldctl_value.bv_val, "dn:");
1583                 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
1584
1585 retry:
1586                 rs->sr_err = ldap_whoami(lc->lc_ld, ctrls, NULL, &msgid);
1587                 if (rs->sr_err == LDAP_SUCCESS) {
1588                         if (ldap_result(lc->lc_ld, msgid, 1, NULL, &res) == -1) {
1589                                 ldap_get_option(lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1590                                         &rs->sr_err);
1591                                 if ( rs->sr_err == LDAP_SERVER_DOWN && doretry ) {
1592                                         doretry = 0;
1593                                         if ( ldap_back_retry( lc, op, rs, LDAP_BACK_SENDERR ) )
1594                                                 goto retry;
1595                                 }
1596                                 ldap_back_freeconn( op, lc );
1597                                 lc = NULL;
1598
1599                         } else {
1600                                 rs->sr_err = ldap_parse_whoami(lc->lc_ld, res, &bv);
1601                                 ldap_msgfree(res);
1602                         }
1603                 }
1604                 ch_free(c.ldctl_value.bv_val);
1605                 if (rs->sr_err != LDAP_SUCCESS) {
1606                         rs->sr_err = slap_map_api2result( rs );
1607                 }
1608
1609                 if ( lc != NULL ) {
1610                         ldap_back_release_conn( &op2, rs, lc );
1611                 }
1612
1613         } else {
1614         /* else just do the same as before */
1615                 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
1616                 if ( !BER_BVISEMPTY( &op->o_dn ) ) {
1617                         bv->bv_len = op->o_dn.bv_len + STRLENOF("dn:");
1618                         bv->bv_val = ch_malloc( bv->bv_len + 1 );
1619                         AC_MEMCPY( bv->bv_val, "dn:", STRLENOF("dn:") );
1620                         AC_MEMCPY( &bv->bv_val[STRLENOF("dn:")], op->o_dn.bv_val,
1621                                 op->o_dn.bv_len );
1622                         bv->bv_val[bv->bv_len] = '\0';
1623                 } else {
1624                         bv->bv_len = 0;
1625                         bv->bv_val = NULL;
1626                 }
1627         }
1628
1629         rs->sr_rspdata = bv;
1630         return rs->sr_err;
1631 }
1632
1633
1634 static int
1635 parse_idassert(
1636     BackendDB   *be,
1637     const char  *fname,
1638     int         lineno,
1639     int         argc,
1640     char        **argv
1641 )
1642 {
1643         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
1644
1645         /* identity assertion mode */
1646         if ( strcasecmp( argv[0], "idassert-mode" ) == 0 ) {
1647                 if ( argc < 2 ) {
1648                         Debug( LDAP_DEBUG_ANY,
1649                                 "%s: line %d: illegal args number %d in \"idassert-mode <args> [<flag> [...]]\" line.\n",
1650                                 fname, lineno, argc );
1651                         return 1;
1652                 }
1653
1654                 if ( strcasecmp( argv[1], "legacy" ) == 0 ) {
1655                         /* will proxyAuthz as client's identity only if bound */
1656                         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
1657
1658                 } else if ( strcasecmp( argv[1], "self" ) == 0 ) {
1659                         /* will proxyAuthz as client's identity */
1660                         li->idassert_mode = LDAP_BACK_IDASSERT_SELF;
1661
1662                 } else if ( strcasecmp( argv[1], "anonymous" ) == 0 ) {
1663                         /* will proxyAuthz as anonymous */
1664                         li->idassert_mode = LDAP_BACK_IDASSERT_ANONYMOUS;
1665
1666                 } else if ( strcasecmp( argv[1], "none" ) == 0 ) {
1667                         /* will not proxyAuthz */
1668                         li->idassert_mode = LDAP_BACK_IDASSERT_NOASSERT;
1669
1670                 } else {
1671                         struct berval   id;
1672                         int             rc;
1673
1674                         /* will proxyAuthz as argv[1] */
1675                         ber_str2bv( argv[1], 0, 0, &id );
1676
1677                         if ( strncasecmp( id.bv_val, "u:", STRLENOF( "u:" ) ) == 0 ) {
1678                                 /* force lowercase... */
1679                                 id.bv_val[0] = 'u';
1680                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
1681                                 ber_dupbv( &li->idassert_authzID, &id );
1682
1683                         } else {
1684                                 struct berval   dn;
1685
1686                                 /* default is DN? */
1687                                 if ( strncasecmp( id.bv_val, "dn:", STRLENOF( "dn:" ) ) == 0 ) {
1688                                         id.bv_val += STRLENOF( "dn:" );
1689                                         id.bv_len -= STRLENOF( "dn:" );
1690                                 }
1691
1692                                 rc = dnNormalize( 0, NULL, NULL, &id, &dn, NULL );
1693                                 if ( rc != LDAP_SUCCESS ) {
1694                                         Debug( LDAP_DEBUG_ANY,
1695                                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
1696                                                 fname, lineno, argv[1] );
1697                                         return 1;
1698                                 }
1699
1700                                 li->idassert_authzID.bv_len = STRLENOF( "dn:" ) + dn.bv_len;
1701                                 li->idassert_authzID.bv_val = ch_malloc( li->idassert_authzID.bv_len + 1 );
1702                                 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1703                                 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], dn.bv_val, dn.bv_len + 1 );
1704                                 ch_free( dn.bv_val );
1705
1706                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
1707                         }
1708                 }
1709
1710                 for ( argc -= 2, argv += 2; argc--; argv++ ) {
1711                         if ( strcasecmp( argv[0], "override" ) == 0 ) {
1712                                 li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
1713
1714                         } else {
1715                                 Debug( LDAP_DEBUG_ANY,
1716                                         "%s: line %d: unknown flag \"%s\" "
1717                                         "in \"idassert-mode <args> "
1718                                         "[<flags>]\" line.\n",
1719                                         fname, lineno, argv[0] );
1720                                 return 1;
1721                         }
1722                 }
1723
1724         /* name to use for proxyAuthz propagation */
1725         } else if ( strcasecmp( argv[0], "idassert-authcdn" ) == 0
1726                         || strcasecmp( argv[0], "proxyauthzdn" ) == 0 )
1727         {
1728                 struct berval   dn;
1729                 int             rc;
1730
1731                 /* FIXME: "proxyauthzdn" is no longer documented, and
1732                  * temporarily supported for backwards compatibility */
1733
1734                 if ( argc != 2 ) {
1735                         fprintf( stderr,
1736         "%s: line %d: missing name in \"%s <name>\" line\n",
1737                             fname, lineno, argv[0] );
1738                         return( 1 );
1739                 }
1740
1741                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
1742                         fprintf( stderr, "%s: line %d: "
1743                                         "authcDN already defined; replacing...\n",
1744                                         fname, lineno );
1745                         ch_free( li->idassert_authcDN.bv_val );
1746                 }
1747                 
1748                 ber_str2bv( argv[1], 0, 0, &dn );
1749                 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
1750                 if ( rc != LDAP_SUCCESS ) {
1751                         Debug( LDAP_DEBUG_ANY,
1752                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
1753                                 fname, lineno, argv[1] );
1754                         return 1;
1755                 }
1756
1757         /* password to use for proxyAuthz propagation */
1758         } else if ( strcasecmp( argv[0], "idassert-passwd" ) == 0
1759                         || strcasecmp( argv[0], "proxyauthzpw" ) == 0 )
1760         {
1761                 /* FIXME: "proxyauthzpw" is no longer documented, and
1762                  * temporarily supported for backwards compatibility */
1763
1764                 if ( argc != 2 ) {
1765                         fprintf( stderr,
1766         "%s: line %d: missing password in \"%s <password>\" line\n",
1767                             fname, lineno, argv[0] );
1768                         return( 1 );
1769                 }
1770
1771                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
1772                         fprintf( stderr, "%s: line %d: "
1773                                         "passwd already defined; replacing...\n",
1774                                         fname, lineno );
1775                         ch_free( li->idassert_passwd.bv_val );
1776                 }
1777                 
1778                 ber_str2bv( argv[1], 0, 1, &li->idassert_passwd );
1779
1780         /* rules to accept identity assertion... */
1781         } else if ( strcasecmp( argv[0], "idassert-authzFrom" ) == 0 ) {
1782                 struct berval   rule;
1783
1784                 ber_str2bv( argv[1], 0, 1, &rule );
1785
1786                 ber_bvarray_add( &li->idassert_authz, &rule );
1787
1788         } else if ( strcasecmp( argv[0], "idassert-method" ) == 0 ) {
1789                 char    *argv1;
1790
1791                 if ( argc < 2 ) {
1792                         fprintf( stderr,
1793         "%s: line %d: missing method in \"%s <method>\" line\n",
1794                             fname, lineno, argv[0] );
1795                         return( 1 );
1796                 }
1797
1798                 argv1 = argv[1];
1799                 if ( strncasecmp( argv1, "bindmethod=", STRLENOF( "bindmethod=" ) ) == 0 ) {
1800                         argv1 += STRLENOF( "bindmethod=" );
1801                 }
1802
1803                 if ( strcasecmp( argv1, "none" ) == 0 ) {
1804                         /* FIXME: is this at all useful? */
1805                         li->idassert_authmethod = LDAP_AUTH_NONE;
1806
1807                         if ( argc != 2 ) {
1808                                 fprintf( stderr,
1809         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
1810                                         fname, lineno, argv[0], argv[1] );
1811                         }
1812
1813                 } else if ( strcasecmp( argv1, "simple" ) == 0 ) {
1814                         int     arg;
1815
1816                         for ( arg = 2; arg < argc; arg++ ) {
1817                                 if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
1818                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
1819                                         struct berval   dn;
1820                                         int             rc;
1821
1822                                         if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
1823                                                 fprintf( stderr, "%s: line %d: "
1824                                                                 "SASL authcDN already defined; replacing...\n",
1825                                                                 fname, lineno );
1826                                                 ch_free( li->idassert_authcDN.bv_val );
1827                                         }
1828                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
1829                                                 val += STRLENOF( "dn:" );
1830                                         }
1831
1832                                         ber_str2bv( val, 0, 0, &dn );
1833                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
1834                                         if ( rc != LDAP_SUCCESS ) {
1835                                                 Debug( LDAP_DEBUG_ANY,
1836                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
1837                                                         fname, lineno, val );
1838                                                 return 1;
1839                                         }
1840
1841                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
1842                                         char    *val = argv[arg] + STRLENOF( "cred=" );
1843
1844                                         if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
1845                                                 fprintf( stderr, "%s: line %d: "
1846                                                                 "SASL cred already defined; replacing...\n",
1847                                                                 fname, lineno );
1848                                                 ch_free( li->idassert_passwd.bv_val );
1849                                         }
1850                                         ber_str2bv( val, 0, 1, &li->idassert_passwd );
1851
1852                                 } else {
1853                                         fprintf( stderr, "%s: line %d: "
1854                                                         "unknown parameter %s\n",
1855                                                         fname, lineno, argv[arg] );
1856                                         return 1;
1857                                 }
1858                         }
1859
1860                         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
1861
1862                 } else if ( strcasecmp( argv1, "sasl" ) == 0 ) {
1863 #ifdef HAVE_CYRUS_SASL
1864                         int     arg;
1865
1866                         for ( arg = 2; arg < argc; arg++ ) {
1867                                 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
1868                                         char    *val = argv[arg] + STRLENOF( "mech=" );
1869
1870                                         if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
1871                                                 fprintf( stderr, "%s: line %d: "
1872                                                                 "SASL mech already defined; replacing...\n",
1873                                                                 fname, lineno );
1874                                                 ch_free( li->idassert_sasl_mech.bv_val );
1875                                         }
1876                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_mech );
1877
1878                                 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
1879                                         char    *val = argv[arg] + STRLENOF( "realm=" );
1880
1881                                         if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
1882                                                 fprintf( stderr, "%s: line %d: "
1883                                                                 "SASL realm already defined; replacing...\n",
1884                                                                 fname, lineno );
1885                                                 ch_free( li->idassert_sasl_realm.bv_val );
1886                                         }
1887                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_realm );
1888
1889                                 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
1890                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
1891                                         struct berval   dn;
1892                                         int             rc;
1893
1894                                         if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
1895                                                 fprintf( stderr, "%s: line %d: "
1896                                                                 "SASL authcDN already defined; replacing...\n",
1897                                                                 fname, lineno );
1898                                                 ch_free( li->idassert_authcDN.bv_val );
1899                                         }
1900                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
1901                                                 val += STRLENOF( "dn:" );
1902                                         }
1903
1904                                         ber_str2bv( val, 0, 0, &dn );
1905                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
1906                                         if ( rc != LDAP_SUCCESS ) {
1907                                                 Debug( LDAP_DEBUG_ANY,
1908                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
1909                                                         fname, lineno, val );
1910                                                 return 1;
1911                                         }
1912
1913                                 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
1914                                         char    *val = argv[arg] + STRLENOF( "authcid=" );
1915
1916                                         if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
1917                                                 fprintf( stderr, "%s: line %d: "
1918                                                                 "SASL authcID already defined; replacing...\n",
1919                                                                 fname, lineno );
1920                                                 ch_free( li->idassert_authcID.bv_val );
1921                                         }
1922                                         if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
1923                                                 val += STRLENOF( "u:" );
1924                                         }
1925                                         ber_str2bv( val, 0, 1, &li->idassert_authcID );
1926
1927                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
1928                                         char    *val = argv[arg] + STRLENOF( "cred=" );
1929
1930                                         if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
1931                                                 fprintf( stderr, "%s: line %d: "
1932                                                                 "SASL cred already defined; replacing...\n",
1933                                                                 fname, lineno );
1934                                                 ch_free( li->idassert_passwd.bv_val );
1935                                         }
1936                                         ber_str2bv( val, 0, 1, &li->idassert_passwd );
1937
1938                                 } else if ( strncasecmp( argv[arg], "authz=", STRLENOF( "authz=" ) ) == 0 ) {
1939                                         char    *val = argv[arg] + STRLENOF( "authz=" );
1940
1941                                         if ( strcasecmp( val, "proxyauthz" ) == 0 ) {
1942                                                 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
1943
1944                                         } else if ( strcasecmp( val, "native" ) == 0 ) {
1945                                                 li->idassert_flags |= LDAP_BACK_AUTH_NATIVE_AUTHZ;
1946
1947                                         } else {
1948                                                 fprintf( stderr, "%s: line %d: "
1949                                                         "unknown authz mode \"%s\"\n",
1950                                                         fname, lineno, val );
1951                                                 return 1;
1952                                         }
1953
1954                                 } else {
1955                                         fprintf( stderr, "%s: line %d: "
1956                                                         "unknown SASL parameter %s\n",
1957                                                         fname, lineno, argv[arg] );
1958                                         return 1;
1959                                 }
1960                         }
1961
1962                         li->idassert_authmethod = LDAP_AUTH_SASL;
1963
1964 #else /* !HAVE_CYRUS_SASL */
1965                         fprintf( stderr, "%s: line %d: "
1966                                         "compile --with-cyrus-sasl to enable SASL auth\n",
1967                                         fname, lineno );
1968                         return 1;
1969 #endif /* !HAVE_CYRUS_SASL */
1970
1971                 } else {
1972                         fprintf( stderr, "%s: line %d: "
1973                                         "unhandled idassert-method method %s\n",
1974                                         fname, lineno, argv[1] );
1975                         return 1;
1976                 }
1977
1978         } else {
1979                 return SLAP_CONF_UNKNOWN;
1980         }
1981
1982         return 0;
1983 }
1984
1985 static int
1986 parse_acl_auth(
1987     BackendDB   *be,
1988     const char  *fname,
1989     int         lineno,
1990     int         argc,
1991     char        **argv
1992 )
1993 {
1994         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
1995
1996         /* name to use for remote ACL access */
1997         if ( strcasecmp( argv[0], "acl-authcdn" ) == 0
1998                         || strcasecmp( argv[0], "binddn" ) == 0 )
1999         {
2000                 struct berval   dn;
2001                 int             rc;
2002
2003                 /* FIXME: "binddn" is no longer documented, and
2004                  * temporarily supported for backwards compatibility */
2005
2006                 if ( argc != 2 ) {
2007                         fprintf( stderr,
2008         "%s: line %d: missing name in \"%s <name>\" line\n",
2009                             fname, lineno, argv[0] );
2010                         return( 1 );
2011                 }
2012
2013                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
2014                         fprintf( stderr, "%s: line %d: "
2015                                         "authcDN already defined; replacing...\n",
2016                                         fname, lineno );
2017                         ch_free( li->acl_authcDN.bv_val );
2018                 }
2019                 
2020                 ber_str2bv( argv[1], 0, 0, &dn );
2021                 rc = dnNormalize( 0, NULL, NULL, &dn, &li->acl_authcDN, NULL );
2022                 if ( rc != LDAP_SUCCESS ) {
2023                         Debug( LDAP_DEBUG_ANY,
2024                                 "%s: line %d: acl ID \"%s\" is not a valid DN\n",
2025                                 fname, lineno, argv[1] );
2026                         return 1;
2027                 }
2028
2029         /* password to use for remote ACL access */
2030         } else if ( strcasecmp( argv[0], "acl-passwd" ) == 0
2031                         || strcasecmp( argv[0], "bindpw" ) == 0 )
2032         {
2033                 /* FIXME: "bindpw" is no longer documented, and
2034                  * temporarily supported for backwards compatibility */
2035
2036                 if ( argc != 2 ) {
2037                         fprintf( stderr,
2038         "%s: line %d: missing password in \"%s <password>\" line\n",
2039                             fname, lineno, argv[0] );
2040                         return( 1 );
2041                 }
2042
2043                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
2044                         fprintf( stderr, "%s: line %d: "
2045                                         "passwd already defined; replacing...\n",
2046                                         fname, lineno );
2047                         ch_free( li->acl_passwd.bv_val );
2048                 }
2049                 
2050                 ber_str2bv( argv[1], 0, 1, &li->acl_passwd );
2051
2052         } else if ( strcasecmp( argv[0], "acl-method" ) == 0 ) {
2053                 char    *argv1;
2054
2055                 if ( argc < 2 ) {
2056                         fprintf( stderr,
2057         "%s: line %d: missing method in \"%s <method>\" line\n",
2058                             fname, lineno, argv[0] );
2059                         return( 1 );
2060                 }
2061
2062                 argv1 = argv[1];
2063                 if ( strncasecmp( argv1, "bindmethod=", STRLENOF( "bindmethod=" ) ) == 0 ) {
2064                         argv1 += STRLENOF( "bindmethod=" );
2065                 }
2066
2067                 if ( strcasecmp( argv1, "none" ) == 0 ) {
2068                         /* FIXME: is this at all useful? */
2069                         li->acl_authmethod = LDAP_AUTH_NONE;
2070
2071                         if ( argc != 2 ) {
2072                                 fprintf( stderr,
2073         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
2074                                         fname, lineno, argv[0], argv[1] );
2075                         }
2076
2077                 } else if ( strcasecmp( argv1, "simple" ) == 0 ) {
2078                         li->acl_authmethod = LDAP_AUTH_SIMPLE;
2079
2080                         if ( argc != 2 ) {
2081                                 fprintf( stderr,
2082         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
2083                                         fname, lineno, argv[0], argv[1] );
2084                         }
2085
2086                 } else if ( strcasecmp( argv1, "sasl" ) == 0 ) {
2087 #ifdef HAVE_CYRUS_SASL
2088                         int     arg;
2089
2090                         for ( arg = 2; arg < argc; arg++ ) {
2091                                 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
2092                                         char    *val = argv[arg] + STRLENOF( "mech=" );
2093
2094                                         if ( !BER_BVISNULL( &li->acl_sasl_mech ) ) {
2095                                                 fprintf( stderr, "%s: line %d: "
2096                                                                 "SASL mech already defined; replacing...\n",
2097                                                                 fname, lineno );
2098                                                 ch_free( li->acl_sasl_mech.bv_val );
2099                                         }
2100                                         ber_str2bv( val, 0, 1, &li->acl_sasl_mech );
2101
2102                                 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
2103                                         char    *val = argv[arg] + STRLENOF( "realm=" );
2104
2105                                         if ( !BER_BVISNULL( &li->acl_sasl_realm ) ) {
2106                                                 fprintf( stderr, "%s: line %d: "
2107                                                                 "SASL realm already defined; replacing...\n",
2108                                                                 fname, lineno );
2109                                                 ch_free( li->acl_sasl_realm.bv_val );
2110                                         }
2111                                         ber_str2bv( val, 0, 1, &li->acl_sasl_realm );
2112
2113                                 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
2114                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
2115                                         struct berval   dn;
2116                                         int             rc;
2117
2118                                         if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
2119                                                 fprintf( stderr, "%s: line %d: "
2120                                                                 "SASL authcDN already defined; replacing...\n",
2121                                                                 fname, lineno );
2122                                                 ch_free( li->acl_authcDN.bv_val );
2123                                         }
2124                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
2125                                                 val += STRLENOF( "dn:" );
2126                                         }
2127
2128                                         ber_str2bv( val, 0, 0, &dn );
2129                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->acl_authcDN, NULL );
2130                                         if ( rc != LDAP_SUCCESS ) {
2131                                                 Debug( LDAP_DEBUG_ANY,
2132                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
2133                                                         fname, lineno, val );
2134                                                 return 1;
2135                                         }
2136
2137                                 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
2138                                         char    *val = argv[arg] + STRLENOF( "authcid=" );
2139
2140                                         if ( !BER_BVISNULL( &li->acl_authcID ) ) {
2141                                                 fprintf( stderr, "%s: line %d: "
2142                                                                 "SASL authcID already defined; replacing...\n",
2143                                                                 fname, lineno );
2144                                                 ch_free( li->acl_authcID.bv_val );
2145                                         }
2146                                         if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
2147                                                 val += STRLENOF( "u:" );
2148                                         }
2149                                         ber_str2bv( val, 0, 1, &li->acl_authcID );
2150
2151                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
2152                                         char    *val = argv[arg] + STRLENOF( "cred=" );
2153
2154                                         if ( !BER_BVISNULL( &li->acl_passwd ) ) {
2155                                                 fprintf( stderr, "%s: line %d: "
2156                                                                 "SASL cred already defined; replacing...\n",
2157                                                                 fname, lineno );
2158                                                 ch_free( li->acl_passwd.bv_val );
2159                                         }
2160                                         ber_str2bv( val, 0, 1, &li->acl_passwd );
2161
2162                                 } else {
2163                                         fprintf( stderr, "%s: line %d: "
2164                                                         "unknown SASL parameter %s\n",
2165                                                         fname, lineno, argv[arg] );
2166                                         return 1;
2167                                 }
2168                         }
2169
2170                         li->acl_authmethod = LDAP_AUTH_SASL;
2171
2172 #else /* !HAVE_CYRUS_SASL */
2173                         fprintf( stderr, "%s: line %d: "
2174                                         "compile --with-cyrus-sasl to enable SASL auth\n",
2175                                         fname, lineno );
2176                         return 1;
2177 #endif /* !HAVE_CYRUS_SASL */
2178
2179                 } else {
2180                         fprintf( stderr, "%s: line %d: "
2181                                         "unhandled acl-method method %s\n",
2182                                         fname, lineno, argv[1] );
2183                         return 1;
2184                 }
2185
2186         } else {
2187                 return SLAP_CONF_UNKNOWN;
2188         }
2189
2190         return 0;
2191 }
2192