]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
6cd07ba40a502df64e5b3463706e8a6282b44774
[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/socket.h>
30
31 #include "slap.h"
32 #include "back-ldap.h"
33 #include "lutil.h"
34 #undef ldap_debug
35 /* for advanced URL parsing */
36 #include "../../../libraries/libldap/ldap-int.h"
37
38 static SLAP_EXTOP_MAIN_FN ldap_back_exop_whoami;
39
40 static int
41 parse_idassert( BackendDB *be, const char *fname, int lineno,
42                 int argc, char **argv );
43
44 static int
45 parse_acl_auth( BackendDB *be, const char *fname, int lineno,
46                 int argc, char **argv );
47
48 int
49 ldap_back_db_config(
50                 BackendDB       *be,
51                 const char      *fname,
52                 int             lineno,
53                 int             argc,
54                 char            **argv )
55 {
56         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
57
58         if ( li == NULL ) {
59                 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
60                                 fname, lineno );
61                 return 1;
62         }
63
64         /* server address to query (depricated, use "uri" directive) */
65         if ( strcasecmp( argv[0], "server" ) == 0 ) {
66                 ber_len_t       l;
67
68                 fprintf( stderr,
69         "%s: line %d: \"server <address>\" directive is deprecated\n",
70                                         fname, lineno );
71
72                 if ( argc != 2 ) {
73                         fprintf( stderr,
74         "%s: line %d: missing address in \"server <address>\" line\n",
75                                         fname, lineno );
76                         return 1;
77                 }
78                 if ( li->url != NULL ) {
79                         ch_free( li->url );
80                 }
81
82                 l = strlen( argv[1] ) + STRLENOF( "ldap:///") + 1;
83                 li->url = ch_calloc( l, sizeof( char ) );
84                 if ( li->url == NULL ) {
85                         fprintf( stderr, "%s: line %d: malloc failed\n" );
86                         return 1;
87                 }
88
89                 snprintf( li->url, l, "ldap://%s/", argv[1] );
90
91         /* URI of server to query (preferred over "server" directive) */
92         } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
93                 LDAPURLDesc     *tmpludp;
94                 int             urlrc, i;
95
96                 if ( argc != 2 ) {
97                         fprintf( stderr, "%s: line %d: "
98                                         "missing uri "
99                                         "in \"uri <uri>\" line\n",
100                                         fname, lineno );
101                         return 1;
102                 }
103                 if ( li->url != NULL ) {
104                         ch_free( li->url );
105                 }
106                 if ( li->lud != NULL ) {
107                         ldap_free_urllist( li->lud );
108                 }
109
110 #if 0
111                 /* PARANOID: DN and more are not required nor allowed */
112                 urlrc = ldap_url_parselist_ext( &li->lud, argv[ 1 ], "\t" );
113 #else
114                 urlrc =  ldap_url_parselist( &li->lud, argv[ 1 ] );
115 #endif
116                 if ( urlrc != LDAP_URL_SUCCESS ) {
117                         char    *why;
118
119                         switch ( urlrc ) {
120                         case LDAP_URL_ERR_MEM:
121                                 why = "no memory";
122                                 break;
123                         case LDAP_URL_ERR_PARAM:
124                                 why = "parameter is bad";
125                                 break;
126                         case LDAP_URL_ERR_BADSCHEME:
127                                 why = "URL doesn't begin with \"[c]ldap[si]://\"";
128                                 break;
129                         case LDAP_URL_ERR_BADENCLOSURE:
130                                 why = "URL is missing trailing \">\"";
131                                 break;
132                         case LDAP_URL_ERR_BADURL:
133                                 why = "URL is bad";
134                         case LDAP_URL_ERR_BADHOST:
135                                 why = "host/port is bad";
136                                 break;
137                         case LDAP_URL_ERR_BADATTRS:
138                                 why = "bad (or missing) attributes";
139                                 break;
140                         case LDAP_URL_ERR_BADSCOPE:
141                                 why = "scope string is invalid (or missing)";
142                                 break;
143                         case LDAP_URL_ERR_BADFILTER:
144                                 why = "bad or missing filter";
145                                 break;
146                         case LDAP_URL_ERR_BADEXTS:
147                                 why = "bad or missing extensions";
148                                 break;
149                         default:
150                                 why = "unknown reason";
151                                 break;
152                         }
153                         fprintf( stderr, "%s: line %d: "
154                                         "unable to parse uri \"%s\" "
155                                         "in \"uri <uri>\" line: %s\n",
156                                         fname, lineno, argv[ 1 ], why );
157                         return 1;
158                 }
159
160                 for ( i = 0, tmpludp = li->lud;
161                                 tmpludp;
162                                 i++, tmpludp = tmpludp->lud_next )
163                 {
164                         if ( ( tmpludp->lud_dn != NULL
165                                                 && tmpludp->lud_dn[0] != '\0' )
166                                         || tmpludp->lud_attrs != NULL
167                                         || tmpludp->lud_filter != NULL
168                                         || tmpludp->lud_exts != NULL )
169                         {
170                                 fprintf( stderr, "%s: line %d: "
171                                                 "warning, only protocol, "
172                                                 "host and port allowed "
173                                                 "in \"uri <uri>\" statement "
174                                                 "for uri #%d of \"%s\"\n",
175                                                 fname, lineno, i, argv[1] );
176                         }
177                 }
178
179 #if 0
180                 for ( tmpludp = li->lud; tmpludp; tmpludp = tmpludp->lud_next ) {
181                         LDAPURLDesc     tmplud;
182                         char            *tmpurl;
183                         ber_len_t       oldlen = 0, len;
184
185                         tmplud = *tmpludp;
186                         tmplud.lud_dn = "";
187                         tmplud.lud_attrs = NULL;
188                         tmplud.lud_filter = NULL;
189                         if ( !ldap_is_ldapi_url( argv[ 1 ] ) ) {
190                                 tmplud.lud_exts = NULL;
191                                 tmplud.lud_crit_exts = 0;
192                         }
193
194                         tmpurl = ldap_url_desc2str( &tmplud );
195
196                         if ( tmpurl == NULL ) {
197                                 fprintf( stderr, "%s: line %d: "
198                                         "unable to rebuild uri "
199                                         "in \"uri <uri>\" statement "
200                                         "for \"%s\"\n",
201                                         fname, lineno, argv[ 1 ] );
202                                 return 1;
203                         }
204
205                         len = strlen( tmpurl );
206                         if ( li->url ) {
207                                 oldlen = strlen( li->url ) + STRLENOF( " " );
208                         }
209                         li->url = ch_realloc( li->url, oldlen + len + 1);
210                         if ( oldlen ) {
211                                 li->url[oldlen - 1] = " ";
212                         }
213                         AC_MEMCPY( &li->url[oldlen], tmpurl, len + 1 );
214                         ch_free( tmpurl );
215                 }
216 #else
217                 li->url = ch_strdup( argv[ 1 ] );
218 #endif
219
220         } else if ( strncasecmp( argv[0], "tls-", STRLENOF( "tls-" ) ) == 0 ) {
221
222                 /* start tls */
223                 if ( strcasecmp( argv[0], "tls-start" ) == 0 ) {
224                         if ( argc != 1 ) {
225                                 fprintf( stderr,
226                 "%s: line %d: tls-start takes no arguments\n",
227                                                 fname, lineno );
228                                 return( 1 );
229                         }
230                         li->flags |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
231         
232                 /* try start tls */
233                 } else if ( strcasecmp( argv[0], "tls-try-start" ) == 0 ) {
234                         if ( argc != 1 ) {
235                                 fprintf( stderr,
236                 "%s: line %d: tls-try-start takes no arguments\n",
237                                                 fname, lineno );
238                                 return( 1 );
239                         }
240                         li->flags &= ~LDAP_BACK_F_TLS_CRITICAL;
241                         li->flags |= LDAP_BACK_F_USE_TLS;
242         
243                 /* propagate start tls */
244                 } else if ( strcasecmp( argv[0], "tls-propagate" ) == 0 ) {
245                         if ( argc != 1 ) {
246                                 fprintf( stderr,
247                 "%s: line %d: tls-propagate takes no arguments\n",
248                                                 fname, lineno );
249                                 return( 1 );
250                         }
251                         li->flags |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
252                 
253                 /* try start tls */
254                 } else if ( strcasecmp( argv[0], "tls-try-propagate" ) == 0 ) {
255                         if ( argc != 1 ) {
256                                 fprintf( stderr,
257                 "%s: line %d: tls-try-propagate takes no arguments\n",
258                                                 fname, lineno );
259                                 return( 1 );
260                         }
261                         li->flags &= ~LDAP_BACK_F_TLS_CRITICAL;
262                         li->flags |= LDAP_BACK_F_PROPAGATE_TLS;
263                 }
264         
265         /* remote ACL stuff... */
266         } else if ( strncasecmp( argv[0], "acl-", STRLENOF( "acl-" ) ) == 0
267                         || strncasecmp( argv[0], "bind", STRLENOF( "bind" ) ) == 0 )
268         {
269                 /* NOTE: "bind{DN,pw}" was initially used; it's now
270                  * deprected and undocumented, it can be dropped at some
271                  * point, since nobody should be really using it */
272                 return parse_acl_auth( be, fname, lineno, argc, argv );
273
274         /* identity assertion stuff... */
275         } else if ( strncasecmp( argv[0], "idassert-", STRLENOF( "idassert-" ) ) == 0
276                         || strncasecmp( argv[0], "proxyauthz", STRLENOF( "proxyauthz" ) ) == 0 )
277         {
278                 /* NOTE: "proxyauthz{DN,pw}" was initially used; it's now
279                  * deprected and undocumented, it can be dropped at some
280                  * point, since nobody should be really using it */
281                 return parse_idassert( be, fname, lineno, argc, argv );
282
283         /* save bind creds for referral rebinds? */
284         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
285                 if ( argc != 1 ) {
286                         fprintf( stderr,
287         "%s: line %d: \"rebind-as-user\" takes no arguments\n",
288                                         fname, lineno );
289                         return( 1 );
290                 }
291                 li->flags |= LDAP_BACK_F_SAVECRED;
292
293         } else if ( strcasecmp( argv[0], "chase-referrals" ) == 0 ) {
294                 if ( argc != 1 ) {
295                         fprintf( stderr,
296         "%s: line %d: \"chase-referrals\" takes no arguments\n",
297                                         fname, lineno );
298                         return( 1 );
299                 }
300
301                 li->flags |= LDAP_BACK_F_CHASE_REFERRALS;
302
303         } else if ( strcasecmp( argv[0], "dont-chase-referrals" ) == 0 ) {
304                 if ( argc != 1 ) {
305                         fprintf( stderr,
306         "%s: line %d: \"dont-chase-referrals\" takes no arguments\n",
307                                         fname, lineno );
308                         return( 1 );
309                 }
310
311                 li->flags &= ~LDAP_BACK_F_CHASE_REFERRALS;
312
313         /* intercept exop_who_am_i? */
314         } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
315                 if ( argc != 1 ) {
316                         fprintf( stderr,
317         "%s: line %d: proxy-whoami takes no arguments\n",
318                                         fname, lineno );
319                         return( 1 );
320                 }
321                 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
322                                 0, ldap_back_exop_whoami );
323
324         /* FIXME: legacy: intercept old rewrite/remap directives
325          * and try to start the rwm overlay */
326         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0
327                         || strcasecmp( argv[0], "map" ) == 0
328                         || strncasecmp( argv[0], "rewrite", STRLENOF( "rewrite" ) ) == 0 )
329         {
330                 fprintf( stderr, "%s: line %d: "
331                         "rewrite/remap capabilities have been moved "
332                         "to the \"rwm\" overlay; see slapo-rwm(5) "
333                         "for details.  I'm trying to do my best "
334                         "to preserve backwards compatibility...\n",
335                         fname, lineno );
336
337                 if ( li->rwm_started == 0 ) {
338                         if ( overlay_config( be, "rwm" ) ) {
339                                 fprintf( stderr, "%s: line %d: "
340                                         "unable to configure the \"rwm\" "
341                                         "overlay, required by directive "
342                                         "\"%s\".\n",
343                                         fname, lineno, argv[0] );
344 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
345                                 fprintf( stderr, "\thint: try loading the \"rwm.la\" dynamic module.\n" );
346 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
347                                 return( 1 );
348                         }
349
350                         fprintf( stderr, "%s: line %d: back-ldap: "
351                                 "automatically starting \"rwm\" overlay, "
352                                 "triggered by \"%s\" directive.\n",
353                                 fname, lineno, argv[ 0 ] );
354
355                         li->rwm_started = 1;
356
357                         return ( *be->bd_info->bi_db_config )( be, fname, lineno, argc, argv );
358                 }
359
360                 return SLAP_CONF_UNKNOWN;
361         
362         /* anything else */
363         } else {
364                 return SLAP_CONF_UNKNOWN;
365         }
366
367         return 0;
368 }
369
370 static int
371 ldap_back_exop_whoami(
372                 Operation       *op,
373                 SlapReply       *rs )
374 {
375         struct berval *bv = NULL;
376
377         if ( op->oq_extended.rs_reqdata != NULL ) {
378                 /* no request data should be provided */
379                 rs->sr_text = "no request data expected";
380                 return rs->sr_err = LDAP_PROTOCOL_ERROR;
381         }
382
383         rs->sr_err = backend_check_restrictions( op, rs, 
384                         (struct berval *)&slap_EXOP_WHOAMI );
385         if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
386
387         /* if auth'd by back-ldap and request is proxied, forward it */
388         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)) {
389                 struct ldapconn *lc;
390
391                 LDAPControl c, *ctrls[2] = {NULL, NULL};
392                 LDAPMessage *res;
393                 Operation op2 = *op;
394                 ber_int_t msgid;
395                 int do_retry = 1;
396
397                 ctrls[0] = &c;
398                 op2.o_ndn = op->o_conn->c_ndn;
399                 lc = ldap_back_getconn(&op2, rs, LDAP_BACK_SENDERR);
400                 if (!lc || !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR )) {
401                         return -1;
402                 }
403                 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
404                 c.ldctl_iscritical = 1;
405                 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
406                 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
407                 strcpy(c.ldctl_value.bv_val, "dn:");
408                 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
409
410 retry:
411                 rs->sr_err = ldap_whoami(lc->lc_ld, ctrls, NULL, &msgid);
412                 if (rs->sr_err == LDAP_SUCCESS) {
413                         if (ldap_result(lc->lc_ld, msgid, 1, NULL, &res) == -1) {
414                                 ldap_get_option(lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
415                                         &rs->sr_err);
416                                 if ( rs->sr_err == LDAP_SERVER_DOWN && do_retry ) {
417                                         do_retry = 0;
418                                         if ( ldap_back_retry( lc, op, rs, LDAP_BACK_SENDERR ) )
419                                                 goto retry;
420                                 }
421                                 ldap_back_freeconn( op, lc );
422                                 lc = NULL;
423
424                         } else {
425                                 rs->sr_err = ldap_parse_whoami(lc->lc_ld, res, &bv);
426                                 ldap_msgfree(res);
427                         }
428                 }
429                 ch_free(c.ldctl_value.bv_val);
430                 if (rs->sr_err != LDAP_SUCCESS) {
431                         rs->sr_err = slap_map_api2result( rs );
432                 }
433         } else {
434         /* else just do the same as before */
435                 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
436                 if ( !BER_BVISEMPTY( &op->o_dn ) ) {
437                         bv->bv_len = op->o_dn.bv_len + sizeof("dn:") - 1;
438                         bv->bv_val = ch_malloc( bv->bv_len + 1 );
439                         AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:") - 1 );
440                         AC_MEMCPY( &bv->bv_val[sizeof("dn:") - 1], op->o_dn.bv_val,
441                                 op->o_dn.bv_len );
442                         bv->bv_val[bv->bv_len] = '\0';
443                 } else {
444                         bv->bv_len = 0;
445                         bv->bv_val = NULL;
446                 }
447         }
448
449         rs->sr_rspdata = bv;
450         return rs->sr_err;
451 }
452
453
454 static int
455 parse_idassert(
456     BackendDB   *be,
457     const char  *fname,
458     int         lineno,
459     int         argc,
460     char        **argv
461 )
462 {
463         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
464
465         /* identity assertion mode */
466         if ( strcasecmp( argv[0], "idassert-mode" ) == 0 ) {
467                 if ( argc < 2 ) {
468                         Debug( LDAP_DEBUG_ANY,
469                                 "%s: line %d: illegal args number %d in \"idassert-mode <args> [<flag> [...]]\" line.\n",
470                                 fname, lineno, argc );
471                         return 1;
472                 }
473
474                 if ( strcasecmp( argv[1], "legacy" ) == 0 ) {
475                         /* will proxyAuthz as client's identity only if bound */
476                         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
477
478                 } else if ( strcasecmp( argv[1], "self" ) == 0 ) {
479                         /* will proxyAuthz as client's identity */
480                         li->idassert_mode = LDAP_BACK_IDASSERT_SELF;
481
482                 } else if ( strcasecmp( argv[1], "anonymous" ) == 0 ) {
483                         /* will proxyAuthz as anonymous */
484                         li->idassert_mode = LDAP_BACK_IDASSERT_ANONYMOUS;
485
486                 } else if ( strcasecmp( argv[1], "none" ) == 0 ) {
487                         /* will not proxyAuthz */
488                         li->idassert_mode = LDAP_BACK_IDASSERT_NOASSERT;
489
490                 } else {
491                         struct berval   id;
492                         int             rc;
493
494                         /* will proxyAuthz as argv[1] */
495                         ber_str2bv( argv[1], 0, 0, &id );
496
497                         if ( strncasecmp( id.bv_val, "u:", STRLENOF( "u:" ) ) == 0 ) {
498                                 /* force lowercase... */
499                                 id.bv_val[0] = 'u';
500                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
501                                 ber_dupbv( &li->idassert_authzID, &id );
502
503                         } else {
504                                 struct berval   dn;
505
506                                 /* default is DN? */
507                                 if ( strncasecmp( id.bv_val, "dn:", STRLENOF( "dn:" ) ) == 0 ) {
508                                         id.bv_val += STRLENOF( "dn:" );
509                                         id.bv_len -= STRLENOF( "dn:" );
510                                 }
511
512                                 rc = dnNormalize( 0, NULL, NULL, &id, &dn, NULL );
513                                 if ( rc != LDAP_SUCCESS ) {
514                                         Debug( LDAP_DEBUG_ANY,
515                                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
516                                                 fname, lineno, argv[1] );
517                                         return 1;
518                                 }
519
520                                 li->idassert_authzID.bv_len = STRLENOF( "dn:" ) + dn.bv_len;
521                                 li->idassert_authzID.bv_val = ch_malloc( li->idassert_authzID.bv_len + 1 );
522                                 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
523                                 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], dn.bv_val, dn.bv_len + 1 );
524                                 ch_free( dn.bv_val );
525
526                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
527                         }
528                 }
529
530                 for ( argc -= 2, argv += 2; argc--; argv++ ) {
531                         if ( strcasecmp( argv[0], "override" ) == 0 ) {
532                                 li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
533
534                         } else {
535                                 Debug( LDAP_DEBUG_ANY,
536                                         "%s: line %d: unknown flag \"%s\" "
537                                         "in \"idassert-mode <args> "
538                                         "[<flags>]\" line.\n",
539                                         fname, lineno, argv[0] );
540                                 return 1;
541                         }
542                 }
543
544         /* name to use for proxyAuthz propagation */
545         } else if ( strcasecmp( argv[0], "idassert-authcdn" ) == 0
546                         || strcasecmp( argv[0], "proxyauthzdn" ) == 0 )
547         {
548                 struct berval   dn;
549                 int             rc;
550
551                 /* FIXME: "proxyauthzdn" is no longer documented, and
552                  * temporarily supported for backwards compatibility */
553
554                 if ( argc != 2 ) {
555                         fprintf( stderr,
556         "%s: line %d: missing name in \"%s <name>\" line\n",
557                             fname, lineno, argv[0] );
558                         return( 1 );
559                 }
560
561                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
562                         fprintf( stderr, "%s: line %d: "
563                                         "authcDN already defined; replacing...\n",
564                                         fname, lineno );
565                         ch_free( li->idassert_authcDN.bv_val );
566                 }
567                 
568                 ber_str2bv( argv[1], 0, 0, &dn );
569                 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
570                 if ( rc != LDAP_SUCCESS ) {
571                         Debug( LDAP_DEBUG_ANY,
572                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
573                                 fname, lineno, argv[1] );
574                         return 1;
575                 }
576
577         /* password to use for proxyAuthz propagation */
578         } else if ( strcasecmp( argv[0], "idassert-passwd" ) == 0
579                         || strcasecmp( argv[0], "proxyauthzpw" ) == 0 )
580         {
581                 /* FIXME: "proxyauthzpw" is no longer documented, and
582                  * temporarily supported for backwards compatibility */
583
584                 if ( argc != 2 ) {
585                         fprintf( stderr,
586         "%s: line %d: missing password in \"%s <password>\" line\n",
587                             fname, lineno, argv[0] );
588                         return( 1 );
589                 }
590
591                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
592                         fprintf( stderr, "%s: line %d: "
593                                         "passwd already defined; replacing...\n",
594                                         fname, lineno );
595                         ch_free( li->idassert_passwd.bv_val );
596                 }
597                 
598                 ber_str2bv( argv[1], 0, 1, &li->idassert_passwd );
599
600         /* rules to accept identity assertion... */
601         } else if ( strcasecmp( argv[0], "idassert-authzFrom" ) == 0 ) {
602                 struct berval   rule;
603
604                 ber_str2bv( argv[1], 0, 1, &rule );
605
606                 ber_bvarray_add( &li->idassert_authz, &rule );
607
608         } else if ( strcasecmp( argv[0], "idassert-method" ) == 0 ) {
609                 if ( argc < 2 ) {
610                         fprintf( stderr,
611         "%s: line %d: missing method in \"%s <method>\" line\n",
612                             fname, lineno, argv[0] );
613                         return( 1 );
614                 }
615
616                 if ( strcasecmp( argv[1], "none" ) == 0 ) {
617                         /* FIXME: is this at all useful? */
618                         li->idassert_authmethod = LDAP_AUTH_NONE;
619
620                         if ( argc != 2 ) {
621                                 fprintf( stderr,
622         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
623                                         fname, lineno, argv[0], argv[1] );
624                         }
625
626                 } else if ( strcasecmp( argv[1], "simple" ) == 0 ) {
627                         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
628
629                         if ( argc != 2 ) {
630                                 fprintf( stderr,
631         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
632                                         fname, lineno, argv[0], argv[1] );
633                         }
634
635                 } else if ( strcasecmp( argv[1], "sasl" ) == 0 ) {
636 #ifdef HAVE_CYRUS_SASL
637                         int     arg;
638
639                         for ( arg = 2; arg < argc; arg++ ) {
640                                 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
641                                         char    *val = argv[arg] + STRLENOF( "mech=" );
642
643                                         if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
644                                                 fprintf( stderr, "%s: line %d: "
645                                                                 "SASL mech already defined; replacing...\n",
646                                                                 fname, lineno );
647                                                 ch_free( li->idassert_sasl_mech.bv_val );
648                                         }
649                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_mech );
650
651                                 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
652                                         char    *val = argv[arg] + STRLENOF( "realm=" );
653
654                                         if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
655                                                 fprintf( stderr, "%s: line %d: "
656                                                                 "SASL realm already defined; replacing...\n",
657                                                                 fname, lineno );
658                                                 ch_free( li->idassert_sasl_realm.bv_val );
659                                         }
660                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_realm );
661
662                                 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
663                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
664                                         struct berval   dn;
665                                         int             rc;
666
667                                         if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
668                                                 fprintf( stderr, "%s: line %d: "
669                                                                 "SASL authcDN already defined; replacing...\n",
670                                                                 fname, lineno );
671                                                 ch_free( li->idassert_authcDN.bv_val );
672                                         }
673                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
674                                                 val += STRLENOF( "dn:" );
675                                         }
676
677                                         ber_str2bv( val, 0, 0, &dn );
678                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
679                                         if ( rc != LDAP_SUCCESS ) {
680                                                 Debug( LDAP_DEBUG_ANY,
681                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
682                                                         fname, lineno, val );
683                                                 return 1;
684                                         }
685
686                                 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
687                                         char    *val = argv[arg] + STRLENOF( "authcid=" );
688
689                                         if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
690                                                 fprintf( stderr, "%s: line %d: "
691                                                                 "SASL authcID already defined; replacing...\n",
692                                                                 fname, lineno );
693                                                 ch_free( li->idassert_authcID.bv_val );
694                                         }
695                                         if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
696                                                 val += STRLENOF( "u:" );
697                                         }
698                                         ber_str2bv( val, 0, 1, &li->idassert_authcID );
699
700                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
701                                         char    *val = argv[arg] + STRLENOF( "cred=" );
702
703                                         if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
704                                                 fprintf( stderr, "%s: line %d: "
705                                                                 "SASL cred already defined; replacing...\n",
706                                                                 fname, lineno );
707                                                 ch_free( li->idassert_passwd.bv_val );
708                                         }
709                                         ber_str2bv( val, 0, 1, &li->idassert_passwd );
710
711                                 } else if ( strncasecmp( argv[arg], "authz=", STRLENOF( "authz=" ) ) == 0 ) {
712                                         char    *val = argv[arg] + STRLENOF( "authz=" );
713
714                                         if ( strcasecmp( val, "proxyauthz" ) == 0 ) {
715                                                 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
716
717                                         } else if ( strcasecmp( val, "native" ) == 0 ) {
718                                                 li->idassert_flags |= LDAP_BACK_AUTH_NATIVE_AUTHZ;
719
720                                         } else {
721                                                 fprintf( stderr, "%s: line %d: "
722                                                         "unknown authz mode \"%s\"\n",
723                                                         fname, lineno, val );
724                                                 return 1;
725                                         }
726
727                                 } else {
728                                         fprintf( stderr, "%s: line %d: "
729                                                         "unknown SASL parameter %s\n",
730                                                         fname, lineno, argv[arg] );
731                                         return 1;
732                                 }
733                         }
734
735                         li->idassert_authmethod = LDAP_AUTH_SASL;
736
737 #else /* !HAVE_CYRUS_SASL */
738                         fprintf( stderr, "%s: line %d: "
739                                         "compile --with-cyrus-sasl to enable SASL auth\n",
740                                         fname, lineno );
741                         return 1;
742 #endif /* !HAVE_CYRUS_SASL */
743
744                 } else {
745                         fprintf( stderr, "%s: line %d: "
746                                         "unhandled idassert-method method %s\n",
747                                         fname, lineno, argv[1] );
748                         return 1;
749                 }
750
751         } else {
752                 return SLAP_CONF_UNKNOWN;
753         }
754
755         return 0;
756 }
757
758 static int
759 parse_acl_auth(
760     BackendDB   *be,
761     const char  *fname,
762     int         lineno,
763     int         argc,
764     char        **argv
765 )
766 {
767         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
768
769         /* name to use for remote ACL access */
770         if ( strcasecmp( argv[0], "acl-authcdn" ) == 0
771                         || strcasecmp( argv[0], "binddn" ) == 0 )
772         {
773                 struct berval   dn;
774                 int             rc;
775
776                 /* FIXME: "binddn" is no longer documented, and
777                  * temporarily supported for backwards compatibility */
778
779                 if ( argc != 2 ) {
780                         fprintf( stderr,
781         "%s: line %d: missing name in \"%s <name>\" line\n",
782                             fname, lineno, argv[0] );
783                         return( 1 );
784                 }
785
786                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
787                         fprintf( stderr, "%s: line %d: "
788                                         "authcDN already defined; replacing...\n",
789                                         fname, lineno );
790                         ch_free( li->acl_authcDN.bv_val );
791                 }
792                 
793                 ber_str2bv( argv[1], 0, 0, &dn );
794                 rc = dnNormalize( 0, NULL, NULL, &dn, &li->acl_authcDN, NULL );
795                 if ( rc != LDAP_SUCCESS ) {
796                         Debug( LDAP_DEBUG_ANY,
797                                 "%s: line %d: acl ID \"%s\" is not a valid DN\n",
798                                 fname, lineno, argv[1] );
799                         return 1;
800                 }
801
802         /* password to use for remote ACL access */
803         } else if ( strcasecmp( argv[0], "acl-passwd" ) == 0
804                         || strcasecmp( argv[0], "bindpw" ) == 0 )
805         {
806                 /* FIXME: "bindpw" is no longer documented, and
807                  * temporarily supported for backwards compatibility */
808
809                 if ( argc != 2 ) {
810                         fprintf( stderr,
811         "%s: line %d: missing password in \"%s <password>\" line\n",
812                             fname, lineno, argv[0] );
813                         return( 1 );
814                 }
815
816                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
817                         fprintf( stderr, "%s: line %d: "
818                                         "passwd already defined; replacing...\n",
819                                         fname, lineno );
820                         ch_free( li->acl_passwd.bv_val );
821                 }
822                 
823                 ber_str2bv( argv[1], 0, 1, &li->acl_passwd );
824
825         } else if ( strcasecmp( argv[0], "acl-method" ) == 0 ) {
826                 if ( argc < 2 ) {
827                         fprintf( stderr,
828         "%s: line %d: missing method in \"%s <method>\" line\n",
829                             fname, lineno, argv[0] );
830                         return( 1 );
831                 }
832
833                 if ( strcasecmp( argv[1], "none" ) == 0 ) {
834                         /* FIXME: is this at all useful? */
835                         li->acl_authmethod = LDAP_AUTH_NONE;
836
837                         if ( argc != 2 ) {
838                                 fprintf( stderr,
839         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
840                                         fname, lineno, argv[0], argv[1] );
841                         }
842
843                 } else if ( strcasecmp( argv[1], "simple" ) == 0 ) {
844                         li->acl_authmethod = LDAP_AUTH_SIMPLE;
845
846                         if ( argc != 2 ) {
847                                 fprintf( stderr,
848         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
849                                         fname, lineno, argv[0], argv[1] );
850                         }
851
852                 } else if ( strcasecmp( argv[1], "sasl" ) == 0 ) {
853 #ifdef HAVE_CYRUS_SASL
854                         int     arg;
855
856                         for ( arg = 2; arg < argc; arg++ ) {
857                                 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
858                                         char    *val = argv[arg] + STRLENOF( "mech=" );
859
860                                         if ( !BER_BVISNULL( &li->acl_sasl_mech ) ) {
861                                                 fprintf( stderr, "%s: line %d: "
862                                                                 "SASL mech already defined; replacing...\n",
863                                                                 fname, lineno );
864                                                 ch_free( li->acl_sasl_mech.bv_val );
865                                         }
866                                         ber_str2bv( val, 0, 1, &li->acl_sasl_mech );
867
868                                 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
869                                         char    *val = argv[arg] + STRLENOF( "realm=" );
870
871                                         if ( !BER_BVISNULL( &li->acl_sasl_realm ) ) {
872                                                 fprintf( stderr, "%s: line %d: "
873                                                                 "SASL realm already defined; replacing...\n",
874                                                                 fname, lineno );
875                                                 ch_free( li->acl_sasl_realm.bv_val );
876                                         }
877                                         ber_str2bv( val, 0, 1, &li->acl_sasl_realm );
878
879                                 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
880                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
881                                         struct berval   dn;
882                                         int             rc;
883
884                                         if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
885                                                 fprintf( stderr, "%s: line %d: "
886                                                                 "SASL authcDN already defined; replacing...\n",
887                                                                 fname, lineno );
888                                                 ch_free( li->acl_authcDN.bv_val );
889                                         }
890                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
891                                                 val += STRLENOF( "dn:" );
892                                         }
893
894                                         ber_str2bv( val, 0, 0, &dn );
895                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->acl_authcDN, NULL );
896                                         if ( rc != LDAP_SUCCESS ) {
897                                                 Debug( LDAP_DEBUG_ANY,
898                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
899                                                         fname, lineno, val );
900                                                 return 1;
901                                         }
902
903                                 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
904                                         char    *val = argv[arg] + STRLENOF( "authcid=" );
905
906                                         if ( !BER_BVISNULL( &li->acl_authcID ) ) {
907                                                 fprintf( stderr, "%s: line %d: "
908                                                                 "SASL authcID already defined; replacing...\n",
909                                                                 fname, lineno );
910                                                 ch_free( li->acl_authcID.bv_val );
911                                         }
912                                         if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
913                                                 val += STRLENOF( "u:" );
914                                         }
915                                         ber_str2bv( val, 0, 1, &li->acl_authcID );
916
917                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
918                                         char    *val = argv[arg] + STRLENOF( "cred=" );
919
920                                         if ( !BER_BVISNULL( &li->acl_passwd ) ) {
921                                                 fprintf( stderr, "%s: line %d: "
922                                                                 "SASL cred already defined; replacing...\n",
923                                                                 fname, lineno );
924                                                 ch_free( li->acl_passwd.bv_val );
925                                         }
926                                         ber_str2bv( val, 0, 1, &li->acl_passwd );
927
928                                 } else {
929                                         fprintf( stderr, "%s: line %d: "
930                                                         "unknown SASL parameter %s\n",
931                                                         fname, lineno, argv[arg] );
932                                         return 1;
933                                 }
934                         }
935
936                         li->acl_authmethod = LDAP_AUTH_SASL;
937
938 #else /* !HAVE_CYRUS_SASL */
939                         fprintf( stderr, "%s: line %d: "
940                                         "compile --with-cyrus-sasl to enable SASL auth\n",
941                                         fname, lineno );
942                         return 1;
943 #endif /* !HAVE_CYRUS_SASL */
944
945                 } else {
946                         fprintf( stderr, "%s: line %d: "
947                                         "unhandled acl-method method %s\n",
948                                         fname, lineno, argv[1] );
949                         return 1;
950                 }
951
952         } else {
953                 return SLAP_CONF_UNKNOWN;
954         }
955
956         return 0;
957 }
958