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