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