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