]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
1b2da1c40027a7fca9514b46a2c5767224d260ba
[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                         } else {
505                                 rs->sr_err = ldap_parse_whoami(lc->ld, res, &bv);
506                                 ldap_msgfree(res);
507                         }
508                 }
509                 ch_free(c.ldctl_value.bv_val);
510                 if (rs->sr_err != LDAP_SUCCESS) {
511                         rs->sr_err = slap_map_api2result( rs );
512                 }
513         } else {
514         /* else just do the same as before */
515                 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
516                 if( op->o_dn.bv_len ) {
517                         bv->bv_len = op->o_dn.bv_len + sizeof("dn:") - 1;
518                         bv->bv_val = ch_malloc( bv->bv_len + 1 );
519                         AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:") - 1 );
520                         AC_MEMCPY( &bv->bv_val[sizeof("dn:") - 1], op->o_dn.bv_val,
521                                 op->o_dn.bv_len );
522                         bv->bv_val[bv->bv_len] = '\0';
523                 } else {
524                         bv->bv_len = 0;
525                         bv->bv_val = NULL;
526                 }
527         }
528
529         rs->sr_rspdata = bv;
530         return rs->sr_err;
531 }
532
533
534 #ifdef ENABLE_REWRITE
535 static char *
536 suffix_massage_regexize( const char *s )
537 {
538         char *res, *ptr;
539         const char *p, *r;
540         int i;
541
542         for ( i = 0, p = s; 
543                         ( r = strchr( p, ',' ) ) != NULL; 
544                         p = r + 1, i++ )
545                 ;
546
547         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
548
549         ptr = lutil_strcopy( res, "(.*)" );
550         for ( i = 0, p = s;
551                         ( r = strchr( p, ',' ) ) != NULL;
552                         p = r + 1 , i++ ) {
553                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
554                 ptr = lutil_strcopy( ptr, "[ ]?" );
555
556                 if ( r[ 1 ] == ' ' ) {
557                         r++;
558                 }
559         }
560         lutil_strcopy( ptr, p );
561
562         return res;
563 }
564
565 static char *
566 suffix_massage_patternize( const char *s )
567 {
568         ber_len_t       len;
569         char            *res;
570
571         len = strlen( s );
572
573         res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
574         if ( res == NULL ) {
575                 return NULL;
576         }
577
578         strcpy( res, "%1" );
579         strcpy( res + sizeof( "%1" ) - 1, s );
580
581         return res;
582 }
583
584 int
585 suffix_massage_config( 
586                 struct rewrite_info *info,
587                 struct berval *pvnc,
588                 struct berval *nvnc,
589                 struct berval *prnc,
590                 struct berval *nrnc
591 )
592 {
593         char *rargv[ 5 ];
594         int line = 0;
595
596         rargv[ 0 ] = "rewriteEngine";
597         rargv[ 1 ] = "on";
598         rargv[ 2 ] = NULL;
599         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
600
601         rargv[ 0 ] = "rewriteContext";
602         rargv[ 1 ] = "default";
603         rargv[ 2 ] = NULL;
604         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
605
606         rargv[ 0 ] = "rewriteRule";
607         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
608         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
609         rargv[ 3 ] = ":";
610         rargv[ 4 ] = NULL;
611         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
612         ch_free( rargv[ 1 ] );
613         ch_free( rargv[ 2 ] );
614         
615         rargv[ 0 ] = "rewriteContext";
616         rargv[ 1 ] = "searchResult";
617         rargv[ 2 ] = NULL;
618         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
619         
620         rargv[ 0 ] = "rewriteRule";
621         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
622         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
623         rargv[ 3 ] = ":";
624         rargv[ 4 ] = NULL;
625         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
626         ch_free( rargv[ 1 ] );
627         ch_free( rargv[ 2 ] );
628
629         rargv[ 0 ] = "rewriteContext";
630         rargv[ 1 ] = "matchedDN";
631         rargv[ 2 ] = "alias";
632         rargv[ 3 ] = "searchResult";
633         rargv[ 4 ] = NULL;
634         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
635
636         rargv[ 0 ] = "rewriteContext";
637         rargv[ 1 ] = "searchAttrDN";
638         rargv[ 2 ] = "alias";
639         rargv[ 3 ] = "searchResult";
640         rargv[ 4 ] = NULL;
641         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
642
643         return 0;
644 }
645 #endif /* ENABLE_REWRITE */
646
647 #ifdef LDAP_BACK_PROXY_AUTHZ
648 static int
649 parse_idassert(
650     BackendDB   *be,
651     const char  *fname,
652     int         lineno,
653     int         argc,
654     char        **argv
655 )
656 {
657         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
658
659         /* identity assertion mode */
660         if ( strcasecmp( argv[0], "idassert-mode" ) == 0 ) {
661                 if ( argc != 2 ) {
662 #ifdef NEW_LOGGING
663                         LDAP_LOG( CONFIG, CRIT, 
664                                 "%s: line %d: illegal args number %d in \"idassert-mode <args>\" line.\n",
665                                 fname, lineno, argc );
666 #else
667                         Debug( LDAP_DEBUG_ANY,
668                                 "%s: line %d: illegal args number %d in \"idassert-mode <args>\" line.\n",
669                                 fname, lineno, argc );
670 #endif
671                         return 1;
672                 }
673
674                 if ( strcasecmp( argv[1], "legacy" ) == 0 ) {
675                         /* will proxyAuthz as client's identity only if bound */
676                         li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
677
678                 } else if ( strcasecmp( argv[1], "self" ) == 0 ) {
679                         /* will proxyAuthz as client's identity */
680                         li->idassert_mode = LDAP_BACK_IDASSERT_SELF;
681
682                 } else if ( strcasecmp( argv[1], "anonymous" ) == 0 ) {
683                         /* will proxyAuthz as anonymous */
684                         li->idassert_mode = LDAP_BACK_IDASSERT_ANONYMOUS;
685
686                 } else if ( strcasecmp( argv[1], "none" ) == 0 ) {
687                         /* will not proxyAuthz */
688                         li->idassert_mode = LDAP_BACK_IDASSERT_NOASSERT;
689
690                 } else {
691                         struct berval   id;
692                         int             rc;
693
694                         /* will proxyAuthz as argv[1] */
695                         ber_str2bv( argv[1], 0, 0, &id );
696
697                         if ( strncasecmp( id.bv_val, "u:", STRLENOF( "u:" ) ) == 0 ) {
698                                 /* force lowercase... */
699                                 id.bv_val[0] = 'u';
700                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
701                                 ber_dupbv( &li->idassert_authzID, &id );
702
703                         } else {
704                                 struct berval   dn;
705
706                                 /* default is DN? */
707                                 if ( strncasecmp( id.bv_val, "dn:", STRLENOF( "dn:" ) ) == 0 ) {
708                                         id.bv_val += STRLENOF( "dn:" );
709                                         id.bv_len -= STRLENOF( "dn:" );
710                                 }
711
712                                 rc = dnNormalize( 0, NULL, NULL, &id, &dn, NULL );
713                                 if ( rc != LDAP_SUCCESS ) {
714 #ifdef NEW_LOGGING
715                                         LDAP_LOG( CONFIG, CRIT, 
716                                                 "%s: line %d: idassert ID \"%s\" is not a valid DN.\n",
717                                                 fname, lineno, argv[1] );
718 #else
719                                         Debug( LDAP_DEBUG_ANY,
720                                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
721                                                 fname, lineno, argv[1] );
722 #endif
723                                         return 1;
724                                 }
725
726                                 li->idassert_authzID.bv_len = STRLENOF( "dn:" ) + dn.bv_len;
727                                 li->idassert_authzID.bv_val = ch_malloc( li->idassert_authzID.bv_len + 1 );
728                                 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
729                                 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], dn.bv_val, dn.bv_len + 1 );
730                                 ch_free( dn.bv_val );
731
732                                 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
733                         }
734                 }
735
736         /* name to use for proxyAuthz propagation */
737         } else if ( strcasecmp( argv[0], "idassert-authcdn" ) == 0
738                         || strcasecmp( argv[0], "proxyauthzdn" ) == 0 ) {
739                 struct berval   dn;
740                 int             rc;
741
742                 if ( argc != 2 ) {
743                         fprintf( stderr,
744         "%s: line %d: missing name in \"%s <name>\" line\n",
745                             fname, lineno, argv[0] );
746                         return( 1 );
747                 }
748
749                 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
750                         fprintf( stderr, "%s: line %d: "
751                                         "authcDN already defined; replacing...\n",
752                                         fname, lineno );
753                         ch_free( li->idassert_authcDN.bv_val );
754                 }
755                 
756                 ber_str2bv( argv[1], 0, 0, &dn );
757                 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
758                 if ( rc != LDAP_SUCCESS ) {
759 #ifdef NEW_LOGGING
760                         LDAP_LOG( CONFIG, CRIT, 
761                                 "%s: line %d: idassert ID \"%s\" is not a valid DN.\n",
762                                 fname, lineno, argv[1] );
763 #else
764                         Debug( LDAP_DEBUG_ANY,
765                                 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
766                                 fname, lineno, argv[1] );
767 #endif
768                         return 1;
769                 }
770
771         /* password to use for proxyAuthz propagation */
772         } else if ( strcasecmp( argv[0], "idassert-passwd" ) == 0
773                         || strcasecmp( argv[0], "proxyauthzpw" ) == 0 ) {
774                 if ( argc != 2 ) {
775                         fprintf( stderr,
776         "%s: line %d: missing password in \"%s <password>\" line\n",
777                             fname, lineno, argv[0] );
778                         return( 1 );
779                 }
780
781                 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
782                         fprintf( stderr, "%s: line %d: "
783                                         "passwd already defined; replacing...\n",
784                                         fname, lineno );
785                         ch_free( li->idassert_passwd.bv_val );
786                 }
787                 
788                 ber_str2bv( argv[1], 0, 1, &li->idassert_passwd );
789
790         /* rules to accept identity assertion... */
791         } else if ( strcasecmp( argv[0], "idassert-authz" ) == 0 ) {
792                 struct berval   rule;
793
794                 ber_str2bv( argv[1], 0, 1, &rule );
795
796                 ber_bvarray_add( &li->idassert_authz, &rule );
797
798         } else if ( strcasecmp( argv[0], "idassert-method" ) == 0 ) {
799                 if ( argc < 2 ) {
800                         fprintf( stderr,
801         "%s: line %d: missing method in \"%s <method>\" line\n",
802                             fname, lineno, argv[0] );
803                         return( 1 );
804                 }
805
806                 if ( strcasecmp( argv[1], "none" ) == 0 ) {
807                         /* FIXME: is this useful? */
808                         li->idassert_authmethod = LDAP_AUTH_NONE;
809
810                         if ( argc != 2 ) {
811                                 fprintf( stderr,
812         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
813                                         fname, lineno, argv[0], argv[1] );
814                         }
815
816                 } else if ( strcasecmp( argv[1], "simple" ) == 0 ) {
817                         li->idassert_authmethod = LDAP_AUTH_SIMPLE;
818
819                         if ( argc != 2 ) {
820                                 fprintf( stderr,
821         "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
822                                         fname, lineno, argv[0], argv[1] );
823                         }
824
825                 } else if ( strcasecmp( argv[1], "sasl" ) == 0 ) {
826 #ifdef HAVE_CYRUS_SASL
827                         int     arg;
828
829                         for ( arg = 2; arg < argc; arg++ ) {
830                                 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
831                                         char    *val = argv[arg] + STRLENOF( "mech=" );
832
833                                         if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
834                                                 fprintf( stderr, "%s: line %d: "
835                                                                 "SASL mech already defined; replacing...\n",
836                                                                 fname, lineno );
837                                                 ch_free( li->idassert_sasl_mech.bv_val );
838                                         }
839                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_mech );
840
841                                 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
842                                         char    *val = argv[arg] + STRLENOF( "realm=" );
843
844                                         if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
845                                                 fprintf( stderr, "%s: line %d: "
846                                                                 "SASL realm already defined; replacing...\n",
847                                                                 fname, lineno );
848                                                 ch_free( li->idassert_sasl_realm.bv_val );
849                                         }
850                                         ber_str2bv( val, 0, 1, &li->idassert_sasl_realm );
851
852                                 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
853                                         char            *val = argv[arg] + STRLENOF( "authcdn=" );
854                                         struct berval   dn;
855                                         int             rc;
856
857                                         if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
858                                                 fprintf( stderr, "%s: line %d: "
859                                                                 "SASL authcDN already defined; replacing...\n",
860                                                                 fname, lineno );
861                                                 ch_free( li->idassert_authcDN.bv_val );
862                                         }
863                                         if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
864                                                 val += STRLENOF( "dn:" );
865                                         }
866
867                                         ber_str2bv( val, 0, 0, &dn );
868                                         rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
869                                         if ( rc != LDAP_SUCCESS ) {
870 #ifdef NEW_LOGGING
871                                                 LDAP_LOG( CONFIG, CRIT, 
872                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN.\n",
873                                                         fname, lineno, val );
874 #else
875                                                 Debug( LDAP_DEBUG_ANY,
876                                                         "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
877                                                         fname, lineno, val );
878 #endif
879                                                 return 1;
880                                         }
881
882                                 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
883                                         char    *val = argv[arg] + STRLENOF( "authcid=" );
884
885                                         if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
886                                                 fprintf( stderr, "%s: line %d: "
887                                                                 "SASL authcID already defined; replacing...\n",
888                                                                 fname, lineno );
889                                                 ch_free( li->idassert_authcID.bv_val );
890                                         }
891                                         if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
892                                                 val += STRLENOF( "u:" );
893                                         }
894                                         ber_str2bv( val, 0, 1, &li->idassert_authcID );
895
896                                 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
897                                         char    *val = argv[arg] + STRLENOF( "cred=" );
898
899                                         if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
900                                                 fprintf( stderr, "%s: line %d: "
901                                                                 "SASL cred already defined; replacing...\n",
902                                                                 fname, lineno );
903                                                 ch_free( li->idassert_passwd.bv_val );
904                                         }
905                                         ber_str2bv( val, 0, 1, &li->idassert_passwd );
906
907                                 } else {
908                                         fprintf( stderr, "%s: line %d: "
909                                                         "unknown SASL parameter %s\n",
910                                                         fname, lineno, argv[arg] );
911                                         return 1;
912                                 }
913                         }
914
915                         li->idassert_authmethod = LDAP_AUTH_SASL;
916
917 #else /* !HAVE_CYRUS_SASL */
918                         fprintf( stderr, "%s: line %d: "
919                                         "compile --with-cyrus-sasl to enable SASL auth\n",
920                                         fname, lineno );
921                         return 1;
922 #endif /* !HAVE_CYRUS_SASL */
923
924                 } else {
925                         fprintf( stderr, "%s: line %d: "
926                                         "unhandled auth method %s\n",
927                                         fname, lineno );
928                         return 1;
929                 }
930
931         } else {
932                 return SLAP_CONF_UNKNOWN;
933         }
934
935         return 0;
936 }
937 #endif /* LDAP_BACK_PROXY_AUTHZ */