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