]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
more on idassert: SASL bind/authz
[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-2004 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
35 static SLAP_EXTOP_MAIN_FN ldap_back_exop_whoami;
36
37 static int
38 parse_idassert( BackendDB *be, const char *fname, int lineno,
39                 int argc, char **argv );
40
41 int
42 ldap_back_db_config(
43     BackendDB   *be,
44     const char  *fname,
45     int         lineno,
46     int         argc,
47     char        **argv
48 )
49 {
50         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
51
52         if ( li == NULL ) {
53                 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
54                     fname, lineno );
55                 return( 1 );
56         }
57
58         /* server address to query (depricated, use "uri" directive) */
59         if ( strcasecmp( argv[0], "server" ) == 0 ) {
60                 if (argc != 2) {
61                         fprintf( stderr,
62         "%s: line %d: missing address in \"server <address>\" line\n",
63                             fname, lineno );
64                         return( 1 );
65                 }
66                 if (li->url != NULL)
67                         ch_free(li->url);
68                 li->url = ch_calloc(strlen(argv[1]) + 9, sizeof(char));
69                 if (li->url != NULL) {
70                         strcpy(li->url, "ldap://");
71                         strcat(li->url, argv[1]);
72                         strcat(li->url, "/");
73                 }
74
75         /* URI of server to query (preferred over "server" directive) */
76         } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
77                 LDAPURLDesc     tmplud;
78
79                 if (argc != 2) {
80                         fprintf( stderr, "%s: line %d: "
81                                 "missing uri "
82                                 "in \"uri <uri>\" line\n",
83                                 fname, lineno );
84                         return( 1 );
85                 }
86                 if ( li->url != NULL ) {
87                         ch_free( li->url );
88                 }
89                 if ( li->lud != NULL ) {
90                         ldap_free_urldesc( li->lud );
91                 }
92
93                 if ( ldap_url_parse( argv[ 1 ], &li->lud ) != LDAP_URL_SUCCESS ) {
94                         fprintf( stderr, "%s: line %d: "
95                                 "unable to parse uri \"%s\" "
96                                 "in \"uri <uri>\" line\n",
97                                 fname, lineno, argv[ 1 ] );
98                         return 1;
99                 }
100
101                 if ( ( li->lud->lud_dn != NULL && li->lud->lud_dn[0] != '\0' )
102                                 || li->lud->lud_attrs != NULL
103                                 || li->lud->lud_filter != NULL
104                                 || li->lud->lud_exts != NULL )
105                 {
106                         fprintf( stderr, "%s: line %d: "
107                                 "warning, only protocol, "
108                                 "host and port allowed "
109                                 "in \"uri <uri>\" line\n",
110                                 fname, lineno );
111                 }
112
113 #if 0
114                 tmplud = *lud;
115                 tmplud.lud_dn = "";
116                 tmplud.lud_attrs = NULL;
117                 tmplud.lud_filter = NULL;
118                 if ( !ldap_is_ldapi_url( argv[ 1 ] ) ) {
119                         tmplud.lud_exts = NULL;
120                         tmplud.lud_crit_exts = 0;
121                 }
122                 
123                 li->url = ldap_url_desc2str( &tmplud );
124                 if ( li->url == NULL ) {
125                         fprintf( stderr, "%s: line %d: "
126                                 "unable to rebuild uri \"%s\" "
127                                 "in \"uri <uri>\" line\n",
128                                 fname, lineno, argv[ 1 ] );
129                         return 1;
130                 }
131 #else
132                 li->url = ch_strdup( argv[ 1 ] );
133 #endif
134
135         /* name to use for ldap_back_group */
136         } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
137                 if (argc != 2) {
138                         fprintf( stderr,
139         "%s: line %d: missing name in \"binddn <name>\" line\n",
140                             fname, lineno );
141                         return( 1 );
142                 }
143                 ber_str2bv( argv[1], 0, 1, &li->binddn );
144
145         /* password to use for ldap_back_group */
146         } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
147                 if (argc != 2) {
148                         fprintf( stderr,
149         "%s: line %d: missing password in \"bindpw <password>\" line\n",
150                             fname, lineno );
151                         return( 1 );
152                 }
153                 ber_str2bv( argv[1], 0, 1, &li->bindpw );
154
155 #ifdef LDAP_BACK_PROXY_AUTHZ
156         /* identity assertion stuff... */
157         } else if ( strncasecmp( argv[0], "idassert-", STRLENOF( "idassert-" ) ) == 0
158                         || strncasecmp( argv[0], "proxyauthz", STRLENOF( "proxyauthz" ) ) == 0 ) {
159                 return parse_idassert( be, fname, lineno, argc, argv );
160 #endif /* LDAP_BACK_PROXY_AUTHZ */
161
162         /* save bind creds for referral rebinds? */
163         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
164                 if (argc != 1) {
165                         fprintf( stderr,
166         "%s: line %d: rebind-as-user takes no arguments\n",
167                             fname, lineno );
168                         return( 1 );
169                 }
170                 li->savecred = 1;
171         
172         /* intercept exop_who_am_i? */
173         } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
174                 if (argc != 1) {
175                         fprintf( stderr,
176         "%s: line %d: proxy-whoami takes no arguments\n",
177                             fname, lineno );
178                         return( 1 );
179                 }
180                 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
181                         0, ldap_back_exop_whoami );
182         
183         /* dn massaging */
184         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
185                 BackendDB *tmp_be;
186                 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
187 #ifdef ENABLE_REWRITE
188                 int rc;
189 #endif /* ENABLE_REWRITE */
190                 
191                 /*
192                  * syntax:
193                  * 
194                  *      suffixmassage <suffix> <massaged suffix>
195                  *
196                  * the <suffix> field must be defined as a valid suffix
197                  * (or suffixAlias?) for the current database;
198                  * the <massaged suffix> shouldn't have already been
199                  * defined as a valid suffix or suffixAlias for the 
200                  * current server
201                  */
202                 if ( argc != 3 ) {
203                         fprintf( stderr, "%s: line %d: syntax is"
204                                        " \"suffixMassage <suffix>"
205                                        " <massaged suffix>\"\n",
206                                 fname, lineno );
207                         return( 1 );
208                 }
209                 
210                 ber_str2bv( argv[1], 0, 0, &bvnc );
211                 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
212                         fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
213                                 fname, lineno, bvnc.bv_val );
214                         return( 1 );
215                 }
216                 tmp_be = select_backend( &nvnc, 0, 0 );
217                 if ( tmp_be != NULL && tmp_be != be ) {
218                         fprintf( stderr, "%s: line %d: suffix already in use"
219                                        " by another backend in"
220                                        " \"suffixMassage <suffix>"
221                                        " <massaged suffix>\"\n",
222                                 fname, lineno );
223                         free( nvnc.bv_val );
224                         free( pvnc.bv_val );
225                         return( 1 );
226                 }
227
228                 ber_str2bv( argv[2], 0, 0, &brnc );
229                 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
230                         fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
231                                 fname, lineno, brnc.bv_val );
232                         free( nvnc.bv_val );
233                         free( pvnc.bv_val );
234                         return( 1 );
235                 }
236
237 #if 0
238                 tmp_be = select_backend( &nrnc, 0, 0 );
239                 if ( tmp_be != NULL ) {
240                         fprintf( stderr, "%s: line %d: massaged suffix"
241                                        " already in use by another backend in" 
242                                        " \"suffixMassage <suffix>"
243                                        " <massaged suffix>\"\n",
244                                 fname, lineno );
245                         free( nvnc.bv_val );
246                         free( pvnc.bv_val );
247                         free( nrnc.bv_val );
248                         free( prnc.bv_val );
249                         return( 1 );
250                 }
251 #endif
252
253 #ifdef ENABLE_REWRITE
254                 /*
255                  * The suffix massaging is emulated by means of the
256                  * rewrite capabilities
257                  * FIXME: no extra rewrite capabilities should be added
258                  * to the database
259                  */
260                 rc = suffix_massage_config( li->rwmap.rwm_rw,
261                                 &pvnc, &nvnc, &prnc, &nrnc );
262                 free( nvnc.bv_val );
263                 free( pvnc.bv_val );
264                 free( nrnc.bv_val );
265                 free( prnc.bv_val );
266
267                 return( rc );
268
269 #else /* !ENABLE_REWRITE */
270                 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &pvnc );
271                 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nvnc );
272                 
273                 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &prnc );
274                 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nrnc );
275 #endif /* !ENABLE_REWRITE */
276
277         /* rewrite stuff ... */
278         } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
279 #ifdef ENABLE_REWRITE
280                 return rewrite_parse( li->rwmap.rwm_rw,
281                                 fname, lineno, argc, argv );
282
283 #else /* !ENABLE_REWRITE */
284                 fprintf( stderr, "%s: line %d: rewrite capabilities "
285                                 "are not enabled\n", fname, lineno );
286 #endif /* !ENABLE_REWRITE */
287                 
288         /* objectclass/attribute mapping */
289         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
290                 return ldap_back_map_config( &li->rwmap.rwm_oc,
291                                 &li->rwmap.rwm_at,
292                                 fname, lineno, argc, argv );
293
294         /* anything else */
295         } else {
296                 return SLAP_CONF_UNKNOWN;
297         }
298         return 0;
299 }
300
301 int
302 ldap_back_map_config(
303                 struct ldapmap  *oc_map,
304                 struct ldapmap  *at_map,
305                 const char      *fname,
306                 int             lineno,
307                 int             argc,
308                 char            **argv )
309 {
310         struct ldapmap          *map;
311         struct ldapmapping      *mapping;
312         char                    *src, *dst;
313         int                     is_oc = 0;
314
315         if ( argc < 3 || argc > 4 ) {
316                 fprintf( stderr,
317         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
318                         fname, lineno );
319                 return 1;
320         }
321
322         if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
323                 map = oc_map;
324                 is_oc = 1;
325
326         } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
327                 map = at_map;
328
329         } else {
330                 fprintf( stderr, "%s: line %d: syntax is "
331                         "\"map {objectclass | attribute} [<local> | *] "
332                         "{<foreign> | *}\"\n",
333                         fname, lineno );
334                 return 1;
335         }
336
337         if ( strcmp( argv[2], "*" ) == 0 ) {
338                 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
339                         map->drop_missing = ( argc < 4 );
340                         return 0;
341                 }
342                 src = dst = argv[3];
343
344         } else if ( argc < 4 ) {
345                 src = "";
346                 dst = argv[2];
347
348         } else {
349                 src = argv[2];
350                 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
351         }
352
353         if ( ( map == at_map )
354                         && ( strcasecmp( src, "objectclass" ) == 0
355                         || strcasecmp( dst, "objectclass" ) == 0 ) )
356         {
357                 fprintf( stderr,
358                         "%s: line %d: objectclass attribute cannot be mapped\n",
359                         fname, lineno );
360         }
361
362         mapping = (struct ldapmapping *)ch_calloc( 2,
363                 sizeof(struct ldapmapping) );
364         if ( mapping == NULL ) {
365                 fprintf( stderr,
366                         "%s: line %d: out of memory\n",
367                         fname, lineno );
368                 return 1;
369         }
370         ber_str2bv( src, 0, 1, &mapping->src );
371         ber_str2bv( dst, 0, 1, &mapping->dst );
372         mapping[1].src = mapping->dst;
373         mapping[1].dst = mapping->src;
374
375         /*
376          * schema check
377          */
378         if ( is_oc ) {
379                 if ( src[0] != '\0' ) {
380                         if ( oc_bvfind( &mapping->src ) == NULL ) {
381                                 fprintf( stderr,
382         "%s: line %d: warning, source objectClass '%s' "
383         "should be defined in schema\n",
384                                         fname, lineno, src );
385
386                                 /*
387                                  * FIXME: this should become an err
388                                  */
389                                 goto error_return;
390                         }
391                 }
392
393                 if ( oc_bvfind( &mapping->dst ) == NULL ) {
394                         fprintf( stderr,
395         "%s: line %d: warning, destination objectClass '%s' "
396         "is not defined in schema\n",
397                                 fname, lineno, dst );
398                 }
399         } else {
400                 int                     rc;
401                 const char              *text = NULL;
402                 AttributeDescription    *ad = NULL;
403
404                 if ( src[0] != '\0' ) {
405                         rc = slap_bv2ad( &mapping->src, &ad, &text );
406                         if ( rc != LDAP_SUCCESS ) {
407                                 fprintf( stderr,
408         "%s: line %d: warning, source attributeType '%s' "
409         "should be defined in schema\n",
410                                         fname, lineno, src );
411
412                                 /*
413                                  * FIXME: this should become an err
414                                  */
415                                 goto error_return;
416                         }
417
418                         ad = NULL;
419                 }
420
421                 rc = slap_bv2ad( &mapping->dst, &ad, &text );
422                 if ( rc != LDAP_SUCCESS ) {
423                         fprintf( stderr,
424         "%s: line %d: warning, destination attributeType '%s' "
425         "is not defined in schema\n",
426                                 fname, lineno, dst );
427                 }
428         }
429
430         if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
431                         || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
432         {
433                 fprintf( stderr,
434                         "%s: line %d: duplicate mapping found (ignored)\n",
435                         fname, lineno );
436                 goto error_return;
437         }
438
439         if ( src[0] != '\0' ) {
440                 avl_insert( &map->map, (caddr_t)mapping,
441                                         mapping_cmp, mapping_dup );
442         }
443         avl_insert( &map->remap, (caddr_t)&mapping[1],
444                                 mapping_cmp, mapping_dup );
445
446         return 0;
447
448 error_return:;
449         if ( mapping ) {
450                 ch_free( mapping->src.bv_val );
451                 ch_free( mapping->dst.bv_val );
452                 ch_free( mapping );
453         }
454
455         return 1;
456 }
457
458 static int
459 ldap_back_exop_whoami(
460         Operation *op,
461         SlapReply *rs )
462 {
463         struct berval *bv = NULL;
464
465         if ( op->oq_extended.rs_reqdata != NULL ) {
466                 /* no request data should be provided */
467                 rs->sr_text = "no request data expected";
468                 return rs->sr_err = LDAP_PROTOCOL_ERROR;
469         }
470
471         rs->sr_err = backend_check_restrictions( op, rs, 
472                         (struct berval *)&slap_EXOP_WHOAMI );
473         if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
474
475         /* if auth'd by back-ldap and request is proxied, forward it */
476         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)) {
477                 struct ldapconn *lc;
478
479                 LDAPControl c, *ctrls[2] = {NULL, NULL};
480                 LDAPMessage *res;
481                 Operation op2 = *op;
482                 ber_int_t msgid;
483
484                 ctrls[0] = &c;
485                 op2.o_ndn = op->o_conn->c_ndn;
486                 lc = ldap_back_getconn(&op2, rs);
487                 if (!lc || !ldap_back_dobind( lc, op, rs )) {
488                         return -1;
489                 }
490                 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
491                 c.ldctl_iscritical = 1;
492                 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
493                 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
494                 strcpy(c.ldctl_value.bv_val, "dn:");
495                 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
496
497                 rs->sr_err = ldap_whoami(lc->ld, ctrls, NULL, &msgid);
498                 if (rs->sr_err == LDAP_SUCCESS) {
499                         if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
500                                 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
501                                         &rs->sr_err);
502                         } else {
503                                 rs->sr_err = ldap_parse_whoami(lc->ld, res, &bv);
504                                 ldap_msgfree(res);
505                         }
506                 }
507                 ch_free(c.ldctl_value.bv_val);
508                 if (rs->sr_err != LDAP_SUCCESS) {
509                         rs->sr_err = slap_map_api2result( rs );
510                 }
511         } else {
512         /* else just do the same as before */
513                 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
514                 if( op->o_dn.bv_len ) {
515                         bv->bv_len = op->o_dn.bv_len + sizeof("dn:") - 1;
516                         bv->bv_val = ch_malloc( bv->bv_len + 1 );
517                         AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:") - 1 );
518                         AC_MEMCPY( &bv->bv_val[sizeof("dn:") - 1], op->o_dn.bv_val,
519                                 op->o_dn.bv_len );
520                         bv->bv_val[bv->bv_len] = '\0';
521                 } else {
522                         bv->bv_len = 0;
523                         bv->bv_val = NULL;
524                 }
525         }
526
527         rs->sr_rspdata = bv;
528         return rs->sr_err;
529 }
530
531
532 #ifdef ENABLE_REWRITE
533 static char *
534 suffix_massage_regexize( const char *s )
535 {
536         char *res, *ptr;
537         const char *p, *r;
538         int i;
539
540         for ( i = 0, p = s; 
541                         ( r = strchr( p, ',' ) ) != NULL; 
542                         p = r + 1, i++ )
543                 ;
544
545         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
546
547         ptr = lutil_strcopy( res, "(.*)" );
548         for ( i = 0, p = s;
549                         ( r = strchr( p, ',' ) ) != NULL;
550                         p = r + 1 , i++ ) {
551                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
552                 ptr = lutil_strcopy( ptr, "[ ]?" );
553
554                 if ( r[ 1 ] == ' ' ) {
555                         r++;
556                 }
557         }
558         lutil_strcopy( ptr, p );
559
560         return res;
561 }
562
563 static char *
564 suffix_massage_patternize( const char *s )
565 {
566         ber_len_t       len;
567         char            *res;
568
569         len = strlen( s );
570
571         res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
572         if ( res == NULL ) {
573                 return NULL;
574         }
575
576         strcpy( res, "%1" );
577         strcpy( res + sizeof( "%1" ) - 1, s );
578
579         return res;
580 }
581
582 int
583 suffix_massage_config( 
584                 struct rewrite_info *info,
585                 struct berval *pvnc,
586                 struct berval *nvnc,
587                 struct berval *prnc,
588                 struct berval *nrnc
589 )
590 {
591         char *rargv[ 5 ];
592         int line = 0;
593
594         rargv[ 0 ] = "rewriteEngine";
595         rargv[ 1 ] = "on";
596         rargv[ 2 ] = NULL;
597         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
598
599         rargv[ 0 ] = "rewriteContext";
600         rargv[ 1 ] = "default";
601         rargv[ 2 ] = NULL;
602         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
603
604         rargv[ 0 ] = "rewriteRule";
605         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
606         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
607         rargv[ 3 ] = ":";
608         rargv[ 4 ] = NULL;
609         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
610         ch_free( rargv[ 1 ] );
611         ch_free( rargv[ 2 ] );
612         
613         rargv[ 0 ] = "rewriteContext";
614         rargv[ 1 ] = "searchResult";
615         rargv[ 2 ] = NULL;
616         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
617         
618         rargv[ 0 ] = "rewriteRule";
619         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
620         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
621         rargv[ 3 ] = ":";
622         rargv[ 4 ] = NULL;
623         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
624         ch_free( rargv[ 1 ] );
625         ch_free( rargv[ 2 ] );
626
627         rargv[ 0 ] = "rewriteContext";
628         rargv[ 1 ] = "matchedDN";
629         rargv[ 2 ] = "alias";
630         rargv[ 3 ] = "searchResult";
631         rargv[ 4 ] = NULL;
632         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
633
634         rargv[ 0 ] = "rewriteContext";
635         rargv[ 1 ] = "searchAttrDN";
636         rargv[ 2 ] = "alias";
637         rargv[ 3 ] = "searchResult";
638         rargv[ 4 ] = NULL;
639         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
640
641         return 0;
642 }
643 #endif /* ENABLE_REWRITE */
644
645 #ifdef LDAP_BACK_PROXY_AUTHZ
646 static int
647 parse_idassert(
648     BackendDB   *be,
649     const char  *fname,
650     int         lineno,
651     int         argc,
652     char        **argv
653 )
654 {
655         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
656
657         /* identity assertion mode */
658         if ( strcasecmp( argv[0], "idassert-mode" ) == 0 ) {
659                 if ( argc != 2 ) {
660 #ifdef NEW_LOGGING
661                         LDAP_LOG( CONFIG, CRIT, 
662                                 "%s: line %d: illegal args number %d in \"idassert-mode <args>\" line.\n",
663                                 fname, lineno, argc );
664 #else
665                         Debug( LDAP_DEBUG_ANY,
666                                 "%s: line %d: illegal args number %d in \"idassert-mode <args>\" line.\n",
667                                 fname, lineno, argc );
668 #endif
669                         return 1;
670                 }
671
672                 if ( strcasecmp( argv[1], "legacy" ) == 0 ) {
673                         /* will proxyAuthz as client's identity only if bound */
674                         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
675
676                 } else if ( strcasecmp( argv[1], "self" ) == 0 ) {
677                         /* will proxyAuthz as client's identity */
678                         li->idassert_mode = LDAP_BACK_IDASSERT_SELF;
679
680                 } else if ( strcasecmp( argv[1], "anonymous" ) == 0 ) {
681                         /* will proxyAuthz as anonymous */
682                         li->idassert_mode = LDAP_BACK_IDASSERT_ANONYMOUS;
683
684                 } else if ( strcasecmp( argv[1], "none" ) == 0 ) {
685                         /* will not proxyAuthz */
686                         li->idassert_mode = LDAP_BACK_IDASSERT_NOASSERT;
687
688                 } else {
689                         struct berval   id;
690                         int             rc;
691
692                         /* will proxyAuthz as argv[1] */
693                         ber_str2bv( argv[1], 0, 0, &id );
694
695                         if ( strncasecmp( id.bv_val, "u:", STRLENOF( "u:" ) ) == 0 ) {
696                                 /* force lowercase... */
697                                 id.bv_val[0] = 'u';
698                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
699                                 ber_dupbv( &li->idassert_authzID, &id );
700
701                         } else {
702                                 struct berval   dn;
703
704                                 /* default is DN? */
705                                 if ( strncasecmp( id.bv_val, "dn:", STRLENOF( "dn:" ) ) == 0 ) {
706                                         id.bv_val += STRLENOF( "dn:" );
707                                         id.bv_len -= STRLENOF( "dn:" );
708                                 }
709
710                                 rc = dnNormalize( 0, NULL, NULL, &id, &dn, NULL );
711                                 if ( rc != LDAP_SUCCESS ) {
712 #ifdef NEW_LOGGING
713                                         LDAP_LOG( CONFIG, CRIT, 
714                                                 "%s: line %d: idassert ID \"%s\" is not a valid DN.\n",
715                                                 fname, lineno, argv[1] );
716 #else
717                                         Debug( LDAP_DEBUG_ANY,
718                                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
719                                                 fname, lineno, argv[1] );
720 #endif
721                                         return 1;
722                                 }
723
724                                 li->idassert_authzID.bv_val = ch_malloc( STRLENOF( "dn:" ) + dn.bv_len + 1 );
725                                 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
726                                 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], dn.bv_val, dn.bv_len + 1 );
727                                 ch_free( dn.bv_val );
728
729                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
730                         }
731                 }
732
733         /* name to use for proxyAuthz propagation */
734         } else if ( strcasecmp( argv[0], "idassert-authcdn" ) == 0
735                         || strcasecmp( argv[0], "proxyauthzdn" ) == 0 ) {
736                 if ( argc != 2 ) {
737                         fprintf( stderr,
738         "%s: line %d: missing name in \"%s <name>\" line\n",
739                             fname, lineno, argv[0] );
740                         return( 1 );
741                 }
742
743                 if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
744                         fprintf( stderr,
745         "%s: line %d: authcDN incompatible with previously defined authcID\n",
746                             fname, lineno );
747                         return( 1 );
748                 }
749
750                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
751                         fprintf( stderr, "%s: line %d: "
752                                         "authcDN already defined; replacing...\n",
753                                         fname, lineno );
754                         ch_free( li->idassert_authcDN.bv_val );
755                 }
756                 
757                 ber_str2bv( argv[1], 0, 1, &li->idassert_authcDN );
758
759         /* password to use for proxyAuthz propagation */
760         } else if ( strcasecmp( argv[0], "idassert-passwd" ) == 0
761                         || strcasecmp( argv[0], "proxyauthzpw" ) == 0 ) {
762                 if ( argc != 2 ) {
763                         fprintf( stderr,
764         "%s: line %d: missing password in \"%s <password>\" line\n",
765                             fname, lineno, argv[0] );
766                         return( 1 );
767                 }
768
769                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
770                         fprintf( stderr, "%s: line %d: "
771                                         "passwd already defined; replacing...\n",
772                                         fname, lineno );
773                         ch_free( li->idassert_passwd.bv_val );
774                 }
775                 
776                 ber_str2bv( argv[1], 0, 1, &li->idassert_passwd );
777
778         /* rules to accept identity assertion... */
779         } else if ( strcasecmp( argv[0], "idassert-authz" ) == 0 ) {
780                 struct berval   rule;
781
782                 ber_str2bv( argv[1], 0, 1, &rule );
783
784                 ber_bvarray_add( &li->idassert_authz, &rule );
785
786         } else if ( strcasecmp( argv[0], "idassert-method" ) == 0 ) {
787                 if ( argc < 2 ) {
788                         fprintf( stderr,
789         "%s: line %d: missing method in \"%s <method>\" line\n",
790                             fname, lineno, argv[0] );
791                         return( 1 );
792                 }
793
794                 if ( strcasecmp( argv[1], "none" ) == 0 ) {
795                         /* FIXME: is this useful? */
796                         li->idassert_authmethod = LDAP_AUTH_NONE;
797
798                         if ( argc != 2 ) {
799                                 fprintf( stderr,
800         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
801                                         fname, lineno, argv[0], argv[1] );
802                         }
803
804                 } else if ( strcasecmp( argv[1], "simple" ) == 0 ) {
805                         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
806
807                         if ( argc != 2 ) {
808                                 fprintf( stderr,
809         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
810                                         fname, lineno, argv[0], argv[1] );
811                         }
812
813                 } else if ( strcasecmp( argv[1], "sasl" ) == 0 ) {
814 #ifdef HAVE_CYRUS_SASL
815                         int     arg;
816
817                         for ( arg = 2; arg < argc; arg++ ) {
818                                 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
819                                         char    *val = argv[arg] + STRLENOF( "mech=" );
820
821                                         if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
822                                                 fprintf( stderr, "%s: line %d: "
823                                                                 "SASL mech already defined; replacing...\n",
824                                                                 fname, lineno );
825                                                 ch_free( li->idassert_sasl_mech.bv_val );
826                                         }
827                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_mech );
828
829                                 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
830                                         char    *val = argv[arg] + STRLENOF( "realm=" );
831
832                                         if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
833                                                 fprintf( stderr, "%s: line %d: "
834                                                                 "SASL realm already defined; replacing...\n",
835                                                                 fname, lineno );
836                                                 ch_free( li->idassert_sasl_realm.bv_val );
837                                         }
838                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_realm );
839
840                                 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
841                                         char    *val = argv[arg] + STRLENOF( "authcid=" );
842
843                                         if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
844                                                 fprintf( stderr,
845                                 "%s: line %d: SASL authcID incompatible with previously defined authcDN\n",
846                                                                 fname, lineno );
847                                                 return( 1 );
848                                         }
849
850                                         if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
851                                                 fprintf( stderr, "%s: line %d: "
852                                                                 "SASL authcID already defined; replacing...\n",
853                                                                 fname, lineno );
854                                                 ch_free( li->idassert_authcID.bv_val );
855                                         }
856                                         if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
857                                                 val += STRLENOF( "u:" );
858                                         }
859                                         ber_str2bv( val, 0, 1, &li->idassert_authcID );
860
861                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
862                                         char    *val = argv[arg] + STRLENOF( "cred=" );
863
864                                         if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
865                                                 fprintf( stderr, "%s: line %d: "
866                                                                 "SASL cred already defined; replacing...\n",
867                                                                 fname, lineno );
868                                                 ch_free( li->idassert_passwd.bv_val );
869                                         }
870                                         ber_str2bv( val, 0, 1, &li->idassert_passwd );
871
872                                 } else {
873                                         fprintf( stderr, "%s: line %d: "
874                                                         "unknown SASL parameter %s\n",
875                                                         fname, lineno, argv[arg] );
876                                         return 1;
877                                 }
878                         }
879
880                         li->idassert_authmethod = LDAP_AUTH_SASL;
881
882 #else /* !HAVE_CYRUS_SASL */
883                         fprintf( stderr, "%s: line %d: "
884                                         "compile --with-cyrus-sasl to enable SASL auth\n",
885                                         fname, lineno );
886                         return 1;
887 #endif /* !HAVE_CYRUS_SASL */
888
889                 } else {
890                         fprintf( stderr, "%s: line %d: "
891                                         "unhandled auth method %s\n",
892                                         fname, lineno );
893                         return 1;
894                 }
895
896         } else {
897                 return SLAP_CONF_UNKNOWN;
898         }
899
900         return 0;
901 }
902 #endif /* LDAP_BACK_PROXY_AUTHZ */