]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
honor T-F filters (ITS#3706) and some cleanup
[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 ( strcasecmp( argv[0], "tls" ) == 0 ) {
221                 if ( argc != 2 ) {
222                         fprintf( stderr,
223                 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
224                                         fname, lineno );
225                         return( 1 );
226                 }
227
228                 /* start */
229                 if ( strcasecmp( argv[1], "start" ) == 0 ) {
230                         li->flags |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
231         
232                 /* try start tls */
233                 } else if ( strcasecmp( argv[1], "try-start" ) == 0 ) {
234                         li->flags &= ~LDAP_BACK_F_TLS_CRITICAL;
235                         li->flags |= LDAP_BACK_F_USE_TLS;
236         
237                 /* propagate start tls */
238                 } else if ( strcasecmp( argv[1], "propagate" ) == 0 ) {
239                         li->flags |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
240                 
241                 /* try start tls */
242                 } else if ( strcasecmp( argv[1], "try-propagate" ) == 0 ) {
243                         li->flags &= ~LDAP_BACK_F_TLS_CRITICAL;
244                         li->flags |= LDAP_BACK_F_PROPAGATE_TLS;
245
246                 } else {
247                         fprintf( stderr,
248                 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
249                                         fname, lineno, argv[1] );
250                         return( 1 );
251                 }
252         
253         /* remote ACL stuff... */
254         } else if ( strncasecmp( argv[0], "acl-", STRLENOF( "acl-" ) ) == 0
255                         || strncasecmp( argv[0], "bind", STRLENOF( "bind" ) ) == 0 )
256         {
257                 /* NOTE: "bind{DN,pw}" was initially used; it's now
258                  * deprected and undocumented, it can be dropped at some
259                  * point, since nobody should be really using it */
260                 return parse_acl_auth( be, fname, lineno, argc, argv );
261
262         /* identity assertion stuff... */
263         } else if ( strncasecmp( argv[0], "idassert-", STRLENOF( "idassert-" ) ) == 0
264                         || strncasecmp( argv[0], "proxyauthz", STRLENOF( "proxyauthz" ) ) == 0 )
265         {
266                 /* NOTE: "proxyauthz{DN,pw}" was initially used; it's now
267                  * deprected and undocumented, it can be dropped at some
268                  * point, since nobody should be really using it */
269                 return parse_idassert( be, fname, lineno, argc, argv );
270
271         /* save bind creds for referral rebinds? */
272         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
273                 if ( argc != 1 ) {
274                         fprintf( stderr,
275         "%s: line %d: \"rebind-as-user\" takes no arguments\n",
276                                         fname, lineno );
277                         return( 1 );
278                 }
279                 li->flags |= LDAP_BACK_F_SAVECRED;
280
281         } else if ( strcasecmp( argv[0], "chase-referrals" ) == 0 ) {
282                 if ( argc != 2 ) {
283                         fprintf( stderr,
284         "%s: line %d: \"chase-referrals\" needs 1 argument.\n",
285                                         fname, lineno );
286                         return( 1 );
287                 }
288
289                 /* this is the default; we add it because the default might change... */
290                 if ( strcasecmp( argv[1], "yes" ) == 0 ) {
291                         li->flags |= LDAP_BACK_F_CHASE_REFERRALS;
292
293                 } else if ( strcasecmp( argv[1], "no" ) == 0 ) {
294                         li->flags &= ~LDAP_BACK_F_CHASE_REFERRALS;
295
296                 } else {
297                         fprintf( stderr,
298                 "%s: line %d: \"chase-referrals {yes|no}\": unknown argument \"%s\".\n",
299                                         fname, lineno, argv[1] );
300                         return( 1 );
301                 }
302         
303         } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
304                 if ( argc != 2 ) {
305                         fprintf( stderr,
306                 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
307                                         fname, lineno );
308                         return( 1 );
309                 }
310
311                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
312                         li->flags &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
313
314                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
315                         li->flags |= LDAP_BACK_F_SUPPORT_T_F;
316
317                 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
318                         li->flags |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
319
320                 } else {
321                         fprintf( stderr,
322         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
323                                 fname, lineno, argv[ 1 ] );
324                         return 1;
325                 }
326
327         /* intercept exop_who_am_i? */
328         } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
329                 if ( argc != 1 ) {
330                         fprintf( stderr,
331         "%s: line %d: proxy-whoami takes no arguments\n",
332                                         fname, lineno );
333                         return( 1 );
334                 }
335                 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
336                                 0, ldap_back_exop_whoami );
337
338         /* FIXME: legacy: intercept old rewrite/remap directives
339          * and try to start the rwm overlay */
340         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0
341                         || strcasecmp( argv[0], "map" ) == 0
342                         || strncasecmp( argv[0], "rewrite", STRLENOF( "rewrite" ) ) == 0 )
343         {
344                 fprintf( stderr, "%s: line %d: "
345                         "rewrite/remap capabilities have been moved "
346                         "to the \"rwm\" overlay; see slapo-rwm(5) "
347                         "for details.  I'm trying to do my best "
348                         "to preserve backwards compatibility...\n",
349                         fname, lineno );
350
351                 if ( li->rwm_started == 0 ) {
352                         if ( overlay_config( be, "rwm" ) ) {
353                                 fprintf( stderr, "%s: line %d: "
354                                         "unable to configure the \"rwm\" "
355                                         "overlay, required by directive "
356                                         "\"%s\".\n",
357                                         fname, lineno, argv[0] );
358 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
359                                 fprintf( stderr, "\thint: try loading the \"rwm.la\" dynamic module.\n" );
360 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
361                                 return( 1 );
362                         }
363
364                         fprintf( stderr, "%s: line %d: back-ldap: "
365                                 "automatically starting \"rwm\" overlay, "
366                                 "triggered by \"%s\" directive.\n",
367                                 fname, lineno, argv[ 0 ] );
368
369                 /* this is the default; we add it because the default might change... */
370                         li->rwm_started = 1;
371
372                         return ( *be->bd_info->bi_db_config )( be, fname, lineno, argc, argv );
373                 }
374
375                 return SLAP_CONF_UNKNOWN;
376         
377         /* anything else */
378         } else {
379                 return SLAP_CONF_UNKNOWN;
380         }
381
382         return 0;
383 }
384
385 static int
386 ldap_back_exop_whoami(
387                 Operation       *op,
388                 SlapReply       *rs )
389 {
390         struct berval *bv = NULL;
391
392         if ( op->oq_extended.rs_reqdata != NULL ) {
393                 /* no request data should be provided */
394                 rs->sr_text = "no request data expected";
395                 return rs->sr_err = LDAP_PROTOCOL_ERROR;
396         }
397
398         rs->sr_err = backend_check_restrictions( op, rs, 
399                         (struct berval *)&slap_EXOP_WHOAMI );
400         if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
401
402         /* if auth'd by back-ldap and request is proxied, forward it */
403         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)) {
404                 struct ldapconn *lc;
405
406                 LDAPControl c, *ctrls[2] = {NULL, NULL};
407                 LDAPMessage *res;
408                 Operation op2 = *op;
409                 ber_int_t msgid;
410                 int do_retry = 1;
411
412                 ctrls[0] = &c;
413                 op2.o_ndn = op->o_conn->c_ndn;
414                 lc = ldap_back_getconn(&op2, rs, LDAP_BACK_SENDERR);
415                 if (!lc || !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR )) {
416                         return -1;
417                 }
418                 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
419                 c.ldctl_iscritical = 1;
420                 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
421                 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
422                 strcpy(c.ldctl_value.bv_val, "dn:");
423                 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
424
425 retry:
426                 rs->sr_err = ldap_whoami(lc->lc_ld, ctrls, NULL, &msgid);
427                 if (rs->sr_err == LDAP_SUCCESS) {
428                         if (ldap_result(lc->lc_ld, msgid, 1, NULL, &res) == -1) {
429                                 ldap_get_option(lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
430                                         &rs->sr_err);
431                                 if ( rs->sr_err == LDAP_SERVER_DOWN && do_retry ) {
432                                         do_retry = 0;
433                                         if ( ldap_back_retry( lc, op, rs, LDAP_BACK_SENDERR ) )
434                                                 goto retry;
435                                 }
436                                 ldap_back_freeconn( op, lc );
437                                 lc = NULL;
438
439                         } else {
440                                 rs->sr_err = ldap_parse_whoami(lc->lc_ld, res, &bv);
441                                 ldap_msgfree(res);
442                         }
443                 }
444                 ch_free(c.ldctl_value.bv_val);
445                 if (rs->sr_err != LDAP_SUCCESS) {
446                         rs->sr_err = slap_map_api2result( rs );
447                 }
448         } else {
449         /* else just do the same as before */
450                 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
451                 if ( !BER_BVISEMPTY( &op->o_dn ) ) {
452                         bv->bv_len = op->o_dn.bv_len + sizeof("dn:") - 1;
453                         bv->bv_val = ch_malloc( bv->bv_len + 1 );
454                         AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:") - 1 );
455                         AC_MEMCPY( &bv->bv_val[sizeof("dn:") - 1], op->o_dn.bv_val,
456                                 op->o_dn.bv_len );
457                         bv->bv_val[bv->bv_len] = '\0';
458                 } else {
459                         bv->bv_len = 0;
460                         bv->bv_val = NULL;
461                 }
462         }
463
464         rs->sr_rspdata = bv;
465         return rs->sr_err;
466 }
467
468
469 static int
470 parse_idassert(
471     BackendDB   *be,
472     const char  *fname,
473     int         lineno,
474     int         argc,
475     char        **argv
476 )
477 {
478         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
479
480         /* identity assertion mode */
481         if ( strcasecmp( argv[0], "idassert-mode" ) == 0 ) {
482                 if ( argc < 2 ) {
483                         Debug( LDAP_DEBUG_ANY,
484                                 "%s: line %d: illegal args number %d in \"idassert-mode <args> [<flag> [...]]\" line.\n",
485                                 fname, lineno, argc );
486                         return 1;
487                 }
488
489                 if ( strcasecmp( argv[1], "legacy" ) == 0 ) {
490                         /* will proxyAuthz as client's identity only if bound */
491                         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
492
493                 } else if ( strcasecmp( argv[1], "self" ) == 0 ) {
494                         /* will proxyAuthz as client's identity */
495                         li->idassert_mode = LDAP_BACK_IDASSERT_SELF;
496
497                 } else if ( strcasecmp( argv[1], "anonymous" ) == 0 ) {
498                         /* will proxyAuthz as anonymous */
499                         li->idassert_mode = LDAP_BACK_IDASSERT_ANONYMOUS;
500
501                 } else if ( strcasecmp( argv[1], "none" ) == 0 ) {
502                         /* will not proxyAuthz */
503                         li->idassert_mode = LDAP_BACK_IDASSERT_NOASSERT;
504
505                 } else {
506                         struct berval   id;
507                         int             rc;
508
509                         /* will proxyAuthz as argv[1] */
510                         ber_str2bv( argv[1], 0, 0, &id );
511
512                         if ( strncasecmp( id.bv_val, "u:", STRLENOF( "u:" ) ) == 0 ) {
513                                 /* force lowercase... */
514                                 id.bv_val[0] = 'u';
515                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
516                                 ber_dupbv( &li->idassert_authzID, &id );
517
518                         } else {
519                                 struct berval   dn;
520
521                                 /* default is DN? */
522                                 if ( strncasecmp( id.bv_val, "dn:", STRLENOF( "dn:" ) ) == 0 ) {
523                                         id.bv_val += STRLENOF( "dn:" );
524                                         id.bv_len -= STRLENOF( "dn:" );
525                                 }
526
527                                 rc = dnNormalize( 0, NULL, NULL, &id, &dn, NULL );
528                                 if ( rc != LDAP_SUCCESS ) {
529                                         Debug( LDAP_DEBUG_ANY,
530                                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
531                                                 fname, lineno, argv[1] );
532                                         return 1;
533                                 }
534
535                                 li->idassert_authzID.bv_len = STRLENOF( "dn:" ) + dn.bv_len;
536                                 li->idassert_authzID.bv_val = ch_malloc( li->idassert_authzID.bv_len + 1 );
537                                 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
538                                 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], dn.bv_val, dn.bv_len + 1 );
539                                 ch_free( dn.bv_val );
540
541                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
542                         }
543                 }
544
545                 for ( argc -= 2, argv += 2; argc--; argv++ ) {
546                         if ( strcasecmp( argv[0], "override" ) == 0 ) {
547                                 li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
548
549                         } else {
550                                 Debug( LDAP_DEBUG_ANY,
551                                         "%s: line %d: unknown flag \"%s\" "
552                                         "in \"idassert-mode <args> "
553                                         "[<flags>]\" line.\n",
554                                         fname, lineno, argv[0] );
555                                 return 1;
556                         }
557                 }
558
559         /* name to use for proxyAuthz propagation */
560         } else if ( strcasecmp( argv[0], "idassert-authcdn" ) == 0
561                         || strcasecmp( argv[0], "proxyauthzdn" ) == 0 )
562         {
563                 struct berval   dn;
564                 int             rc;
565
566                 /* FIXME: "proxyauthzdn" is no longer documented, and
567                  * temporarily supported for backwards compatibility */
568
569                 if ( argc != 2 ) {
570                         fprintf( stderr,
571         "%s: line %d: missing name in \"%s <name>\" line\n",
572                             fname, lineno, argv[0] );
573                         return( 1 );
574                 }
575
576                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
577                         fprintf( stderr, "%s: line %d: "
578                                         "authcDN already defined; replacing...\n",
579                                         fname, lineno );
580                         ch_free( li->idassert_authcDN.bv_val );
581                 }
582                 
583                 ber_str2bv( argv[1], 0, 0, &dn );
584                 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
585                 if ( rc != LDAP_SUCCESS ) {
586                         Debug( LDAP_DEBUG_ANY,
587                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
588                                 fname, lineno, argv[1] );
589                         return 1;
590                 }
591
592         /* password to use for proxyAuthz propagation */
593         } else if ( strcasecmp( argv[0], "idassert-passwd" ) == 0
594                         || strcasecmp( argv[0], "proxyauthzpw" ) == 0 )
595         {
596                 /* FIXME: "proxyauthzpw" is no longer documented, and
597                  * temporarily supported for backwards compatibility */
598
599                 if ( argc != 2 ) {
600                         fprintf( stderr,
601         "%s: line %d: missing password in \"%s <password>\" line\n",
602                             fname, lineno, argv[0] );
603                         return( 1 );
604                 }
605
606                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
607                         fprintf( stderr, "%s: line %d: "
608                                         "passwd already defined; replacing...\n",
609                                         fname, lineno );
610                         ch_free( li->idassert_passwd.bv_val );
611                 }
612                 
613                 ber_str2bv( argv[1], 0, 1, &li->idassert_passwd );
614
615         /* rules to accept identity assertion... */
616         } else if ( strcasecmp( argv[0], "idassert-authzFrom" ) == 0 ) {
617                 struct berval   rule;
618
619                 ber_str2bv( argv[1], 0, 1, &rule );
620
621                 ber_bvarray_add( &li->idassert_authz, &rule );
622
623         } else if ( strcasecmp( argv[0], "idassert-method" ) == 0 ) {
624                 char    *argv1;
625
626                 if ( argc < 2 ) {
627                         fprintf( stderr,
628         "%s: line %d: missing method in \"%s <method>\" line\n",
629                             fname, lineno, argv[0] );
630                         return( 1 );
631                 }
632
633                 argv1 = argv[1];
634                 if ( strncasecmp( argv1, "bindmethod=", STRLENOF( "bindmethod=" ) ) == 0 ) {
635                         argv1 += STRLENOF( "bindmethod=" );
636                 }
637
638                 if ( strcasecmp( argv1, "none" ) == 0 ) {
639                         /* FIXME: is this at all useful? */
640                         li->idassert_authmethod = LDAP_AUTH_NONE;
641
642                         if ( argc != 2 ) {
643                                 fprintf( stderr,
644         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
645                                         fname, lineno, argv[0], argv[1] );
646                         }
647
648                 } else if ( strcasecmp( argv1, "simple" ) == 0 ) {
649                         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
650
651                         if ( argc != 2 ) {
652                                 fprintf( stderr,
653         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
654                                         fname, lineno, argv[0], argv[1] );
655                         }
656
657                 } else if ( strcasecmp( argv1, "sasl" ) == 0 ) {
658 #ifdef HAVE_CYRUS_SASL
659                         int     arg;
660
661                         for ( arg = 2; arg < argc; arg++ ) {
662                                 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
663                                         char    *val = argv[arg] + STRLENOF( "mech=" );
664
665                                         if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
666                                                 fprintf( stderr, "%s: line %d: "
667                                                                 "SASL mech already defined; replacing...\n",
668                                                                 fname, lineno );
669                                                 ch_free( li->idassert_sasl_mech.bv_val );
670                                         }
671                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_mech );
672
673                                 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
674                                         char    *val = argv[arg] + STRLENOF( "realm=" );
675
676                                         if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
677                                                 fprintf( stderr, "%s: line %d: "
678                                                                 "SASL realm already defined; replacing...\n",
679                                                                 fname, lineno );
680                                                 ch_free( li->idassert_sasl_realm.bv_val );
681                                         }
682                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_realm );
683
684                                 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
685                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
686                                         struct berval   dn;
687                                         int             rc;
688
689                                         if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
690                                                 fprintf( stderr, "%s: line %d: "
691                                                                 "SASL authcDN already defined; replacing...\n",
692                                                                 fname, lineno );
693                                                 ch_free( li->idassert_authcDN.bv_val );
694                                         }
695                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
696                                                 val += STRLENOF( "dn:" );
697                                         }
698
699                                         ber_str2bv( val, 0, 0, &dn );
700                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
701                                         if ( rc != LDAP_SUCCESS ) {
702                                                 Debug( LDAP_DEBUG_ANY,
703                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
704                                                         fname, lineno, val );
705                                                 return 1;
706                                         }
707
708                                 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
709                                         char    *val = argv[arg] + STRLENOF( "authcid=" );
710
711                                         if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
712                                                 fprintf( stderr, "%s: line %d: "
713                                                                 "SASL authcID already defined; replacing...\n",
714                                                                 fname, lineno );
715                                                 ch_free( li->idassert_authcID.bv_val );
716                                         }
717                                         if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
718                                                 val += STRLENOF( "u:" );
719                                         }
720                                         ber_str2bv( val, 0, 1, &li->idassert_authcID );
721
722                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
723                                         char    *val = argv[arg] + STRLENOF( "cred=" );
724
725                                         if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
726                                                 fprintf( stderr, "%s: line %d: "
727                                                                 "SASL cred already defined; replacing...\n",
728                                                                 fname, lineno );
729                                                 ch_free( li->idassert_passwd.bv_val );
730                                         }
731                                         ber_str2bv( val, 0, 1, &li->idassert_passwd );
732
733                                 } else if ( strncasecmp( argv[arg], "authz=", STRLENOF( "authz=" ) ) == 0 ) {
734                                         char    *val = argv[arg] + STRLENOF( "authz=" );
735
736                                         if ( strcasecmp( val, "proxyauthz" ) == 0 ) {
737                                                 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
738
739                                         } else if ( strcasecmp( val, "native" ) == 0 ) {
740                                                 li->idassert_flags |= LDAP_BACK_AUTH_NATIVE_AUTHZ;
741
742                                         } else {
743                                                 fprintf( stderr, "%s: line %d: "
744                                                         "unknown authz mode \"%s\"\n",
745                                                         fname, lineno, val );
746                                                 return 1;
747                                         }
748
749                                 } else {
750                                         fprintf( stderr, "%s: line %d: "
751                                                         "unknown SASL parameter %s\n",
752                                                         fname, lineno, argv[arg] );
753                                         return 1;
754                                 }
755                         }
756
757                         li->idassert_authmethod = LDAP_AUTH_SASL;
758
759 #else /* !HAVE_CYRUS_SASL */
760                         fprintf( stderr, "%s: line %d: "
761                                         "compile --with-cyrus-sasl to enable SASL auth\n",
762                                         fname, lineno );
763                         return 1;
764 #endif /* !HAVE_CYRUS_SASL */
765
766                 } else {
767                         fprintf( stderr, "%s: line %d: "
768                                         "unhandled idassert-method method %s\n",
769                                         fname, lineno, argv[1] );
770                         return 1;
771                 }
772
773         } else {
774                 return SLAP_CONF_UNKNOWN;
775         }
776
777         return 0;
778 }
779
780 static int
781 parse_acl_auth(
782     BackendDB   *be,
783     const char  *fname,
784     int         lineno,
785     int         argc,
786     char        **argv
787 )
788 {
789         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
790
791         /* name to use for remote ACL access */
792         if ( strcasecmp( argv[0], "acl-authcdn" ) == 0
793                         || strcasecmp( argv[0], "binddn" ) == 0 )
794         {
795                 struct berval   dn;
796                 int             rc;
797
798                 /* FIXME: "binddn" is no longer documented, and
799                  * temporarily supported for backwards compatibility */
800
801                 if ( argc != 2 ) {
802                         fprintf( stderr,
803         "%s: line %d: missing name in \"%s <name>\" line\n",
804                             fname, lineno, argv[0] );
805                         return( 1 );
806                 }
807
808                 if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
809                         fprintf( stderr, "%s: line %d: "
810                                         "authcDN already defined; replacing...\n",
811                                         fname, lineno );
812                         ch_free( li->acl_authcDN.bv_val );
813                 }
814                 
815                 ber_str2bv( argv[1], 0, 0, &dn );
816                 rc = dnNormalize( 0, NULL, NULL, &dn, &li->acl_authcDN, NULL );
817                 if ( rc != LDAP_SUCCESS ) {
818                         Debug( LDAP_DEBUG_ANY,
819                                 "%s: line %d: acl ID \"%s\" is not a valid DN\n",
820                                 fname, lineno, argv[1] );
821                         return 1;
822                 }
823
824         /* password to use for remote ACL access */
825         } else if ( strcasecmp( argv[0], "acl-passwd" ) == 0
826                         || strcasecmp( argv[0], "bindpw" ) == 0 )
827         {
828                 /* FIXME: "bindpw" is no longer documented, and
829                  * temporarily supported for backwards compatibility */
830
831                 if ( argc != 2 ) {
832                         fprintf( stderr,
833         "%s: line %d: missing password in \"%s <password>\" line\n",
834                             fname, lineno, argv[0] );
835                         return( 1 );
836                 }
837
838                 if ( !BER_BVISNULL( &li->acl_passwd ) ) {
839                         fprintf( stderr, "%s: line %d: "
840                                         "passwd already defined; replacing...\n",
841                                         fname, lineno );
842                         ch_free( li->acl_passwd.bv_val );
843                 }
844                 
845                 ber_str2bv( argv[1], 0, 1, &li->acl_passwd );
846
847         } else if ( strcasecmp( argv[0], "acl-method" ) == 0 ) {
848                 char    *argv1;
849
850                 if ( argc < 2 ) {
851                         fprintf( stderr,
852         "%s: line %d: missing method in \"%s <method>\" line\n",
853                             fname, lineno, argv[0] );
854                         return( 1 );
855                 }
856
857                 argv1 = argv[1];
858                 if ( strncasecmp( argv1, "bindmethod=", STRLENOF( "bindmethod=" ) ) == 0 ) {
859                         argv1 += STRLENOF( "bindmethod=" );
860                 }
861
862                 if ( strcasecmp( argv1, "none" ) == 0 ) {
863                         /* FIXME: is this at all useful? */
864                         li->acl_authmethod = LDAP_AUTH_NONE;
865
866                         if ( argc != 2 ) {
867                                 fprintf( stderr,
868         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
869                                         fname, lineno, argv[0], argv[1] );
870                         }
871
872                 } else if ( strcasecmp( argv1, "simple" ) == 0 ) {
873                         li->acl_authmethod = LDAP_AUTH_SIMPLE;
874
875                         if ( argc != 2 ) {
876                                 fprintf( stderr,
877         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
878                                         fname, lineno, argv[0], argv[1] );
879                         }
880
881                 } else if ( strcasecmp( argv1, "sasl" ) == 0 ) {
882 #ifdef HAVE_CYRUS_SASL
883                         int     arg;
884
885                         for ( arg = 2; arg < argc; arg++ ) {
886                                 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
887                                         char    *val = argv[arg] + STRLENOF( "mech=" );
888
889                                         if ( !BER_BVISNULL( &li->acl_sasl_mech ) ) {
890                                                 fprintf( stderr, "%s: line %d: "
891                                                                 "SASL mech already defined; replacing...\n",
892                                                                 fname, lineno );
893                                                 ch_free( li->acl_sasl_mech.bv_val );
894                                         }
895                                         ber_str2bv( val, 0, 1, &li->acl_sasl_mech );
896
897                                 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
898                                         char    *val = argv[arg] + STRLENOF( "realm=" );
899
900                                         if ( !BER_BVISNULL( &li->acl_sasl_realm ) ) {
901                                                 fprintf( stderr, "%s: line %d: "
902                                                                 "SASL realm already defined; replacing...\n",
903                                                                 fname, lineno );
904                                                 ch_free( li->acl_sasl_realm.bv_val );
905                                         }
906                                         ber_str2bv( val, 0, 1, &li->acl_sasl_realm );
907
908                                 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
909                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
910                                         struct berval   dn;
911                                         int             rc;
912
913                                         if ( !BER_BVISNULL( &li->acl_authcDN ) ) {
914                                                 fprintf( stderr, "%s: line %d: "
915                                                                 "SASL authcDN already defined; replacing...\n",
916                                                                 fname, lineno );
917                                                 ch_free( li->acl_authcDN.bv_val );
918                                         }
919                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
920                                                 val += STRLENOF( "dn:" );
921                                         }
922
923                                         ber_str2bv( val, 0, 0, &dn );
924                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->acl_authcDN, NULL );
925                                         if ( rc != LDAP_SUCCESS ) {
926                                                 Debug( LDAP_DEBUG_ANY,
927                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
928                                                         fname, lineno, val );
929                                                 return 1;
930                                         }
931
932                                 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
933                                         char    *val = argv[arg] + STRLENOF( "authcid=" );
934
935                                         if ( !BER_BVISNULL( &li->acl_authcID ) ) {
936                                                 fprintf( stderr, "%s: line %d: "
937                                                                 "SASL authcID already defined; replacing...\n",
938                                                                 fname, lineno );
939                                                 ch_free( li->acl_authcID.bv_val );
940                                         }
941                                         if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
942                                                 val += STRLENOF( "u:" );
943                                         }
944                                         ber_str2bv( val, 0, 1, &li->acl_authcID );
945
946                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
947                                         char    *val = argv[arg] + STRLENOF( "cred=" );
948
949                                         if ( !BER_BVISNULL( &li->acl_passwd ) ) {
950                                                 fprintf( stderr, "%s: line %d: "
951                                                                 "SASL cred already defined; replacing...\n",
952                                                                 fname, lineno );
953                                                 ch_free( li->acl_passwd.bv_val );
954                                         }
955                                         ber_str2bv( val, 0, 1, &li->acl_passwd );
956
957                                 } else {
958                                         fprintf( stderr, "%s: line %d: "
959                                                         "unknown SASL parameter %s\n",
960                                                         fname, lineno, argv[arg] );
961                                         return 1;
962                                 }
963                         }
964
965                         li->acl_authmethod = LDAP_AUTH_SASL;
966
967 #else /* !HAVE_CYRUS_SASL */
968                         fprintf( stderr, "%s: line %d: "
969                                         "compile --with-cyrus-sasl to enable SASL auth\n",
970                                         fname, lineno );
971                         return 1;
972 #endif /* !HAVE_CYRUS_SASL */
973
974                 } else {
975                         fprintf( stderr, "%s: line %d: "
976                                         "unhandled acl-method method %s\n",
977                                         fname, lineno, argv[1] );
978                         return 1;
979                 }
980
981         } else {
982                 return SLAP_CONF_UNKNOWN;
983         }
984
985         return 0;
986 }
987