]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
8f041a117e327b2db716d12c01a5315877f2898b
[openldap] / servers / slapd / back-ldap / config.c
1 /* config.c - ldap backend configuration file routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7 /* This is an altered version */
8 /*
9  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
10  * 
11  * Permission is granted to anyone to use this software for any purpose
12  * on any computer system, and to alter it and redistribute it, subject
13  * to the following restrictions:
14  * 
15  * 1. The author is not responsible for the consequences of use of this
16  *    software, no matter how awful, even if they arise from flaws in it.
17  * 
18  * 2. The origin of this software must not be misrepresented, either by
19  *    explicit claim or by omission.  Since few users ever read sources,
20  *    credits should appear in the documentation.
21  * 
22  * 3. Altered versions must be plainly marked as such, and must not be
23  *    misrepresented as being the original software.  Since few users
24  *    ever read sources, credits should appear in the documentation.
25  * 
26  * 4. This notice may not be removed or altered.
27  *
28  *
29  *
30  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
31  * 
32  * This software is being modified by Pierangelo Masarati.
33  * The previously reported conditions apply to the modified code as well.
34  * Changes in the original code are highlighted where required.
35  * Credits for the original code go to the author, Howard Chu.
36  */
37
38 #include "portable.h"
39
40 #include <stdio.h>
41
42 #include <ac/string.h>
43 #include <ac/socket.h>
44
45 #include "slap.h"
46 #include "back-ldap.h"
47
48 int
49 ldap_back_db_config(
50     BackendDB   *be,
51     const char  *fname,
52     int         lineno,
53     int         argc,
54     char        **argv
55 )
56 {
57         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
58
59         if ( li == NULL ) {
60                 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
61                     fname, lineno );
62                 return( 1 );
63         }
64
65         /* server address to query (depricated, use "uri" directive) */
66         if ( strcasecmp( argv[0], "server" ) == 0 ) {
67                 if (argc != 2) {
68                         fprintf( stderr,
69         "%s: line %d: missing address in \"server <address>\" line\n",
70                             fname, lineno );
71                         return( 1 );
72                 }
73                 if (li->url != NULL)
74                         ch_free(li->url);
75                 li->url = ch_calloc(strlen(argv[1]) + 9, sizeof(char));
76                 if (li->url != NULL) {
77                         strcpy(li->url, "ldap://");
78                         strcat(li->url, argv[1]);
79                         strcat(li->url, "/");
80                 }
81
82         /* URI of server to query (preferred over "server" directive) */
83         } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
84                 if (argc != 2) {
85                         fprintf( stderr,
86         "%s: line %d: missing address in \"uri <address>\" line\n",
87                             fname, lineno );
88                         return( 1 );
89                 }
90                 if (li->url != NULL)
91                         ch_free(li->url);
92                 li->url = ch_strdup(argv[1]);
93
94         /* name to use for ldap_back_group */
95         } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
96                 if (argc != 2) {
97                         fprintf( stderr,
98         "%s: line %d: missing name in \"binddn <name>\" line\n",
99                             fname, lineno );
100                         return( 1 );
101                 }
102                 li->binddn = ch_strdup(argv[1]);
103
104         /* password to use for ldap_back_group */
105         } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
106                 if (argc != 2) {
107                         fprintf( stderr,
108         "%s: line %d: missing password in \"bindpw <password>\" line\n",
109                             fname, lineno );
110                         return( 1 );
111                 }
112                 li->bindpw = ch_strdup(argv[1]);
113         
114         /* dn massaging */
115         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
116 #ifndef ENABLE_REWRITE
117                 struct berval *bd2, *nd2;
118 #endif /* ENABLE_REWRITE */
119                 BackendDB *tmp_be;
120                 struct berval bdn, ndn;
121                 
122                 /*
123                  * syntax:
124                  * 
125                  *      suffixmassage <suffix> <massaged suffix>
126                  *
127                  * the <suffix> field must be defined as a valid suffix
128                  * (or suffixAlias?) for the current database;
129                  * the <massaged suffix> shouldn't have already been
130                  * defined as a valid suffix or suffixAlias for the 
131                  * current server
132                  */
133                 if ( argc != 3 ) {
134                         fprintf( stderr, "%s: line %d: syntax is"
135                                        " \"suffixMassage <suffix>"
136                                        " <massaged suffix>\"\n",
137                                 fname, lineno );
138                         return( 1 );
139                 }
140                 
141                 bdn.bv_val = argv[1];
142                 bdn.bv_len = strlen(bdn.bv_val);
143                 if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
144                         fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
145                                 fname, lineno, bdn.bv_val );
146                         return( 1 );
147                 }
148                 tmp_be = select_backend( &ndn, 0, 0 );
149                 free( ndn.bv_val );
150                 if ( tmp_be != NULL && tmp_be != be ) {
151                         fprintf( stderr, "%s: line %d: suffix already in use"
152                                        " by another backend in"
153                                        " \"suffixMassage <suffix>"
154                                        " <massaged suffix>\"\n",
155                                 fname, lineno );
156                         return( 1 );                                            
157                 }
158
159                 bdn.bv_val = argv[2];
160                 bdn.bv_len = strlen(bdn.bv_val);
161                 if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
162                         fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
163                                 fname, lineno, bdn.bv_val );
164                         return( 1 );
165                 }
166                 tmp_be = select_backend( &ndn, 0, 0 );
167                 free( ndn.bv_val );
168                 if ( tmp_be != NULL ) {
169                         fprintf( stderr, "%s: line %d: massaged suffix"
170                                        " already in use by another backend in" 
171                                        " \"suffixMassage <suffix>"
172                                        " <massaged suffix>\"\n",
173                                 fname, lineno );
174                         return( 1 );
175                 }
176
177 #ifdef ENABLE_REWRITE
178                 /*
179                  * The suffix massaging is emulated by means of the
180                  * rewrite capabilities
181                  * FIXME: no extra rewrite capabilities should be added
182                  * to the database
183                  */
184                 return suffix_massage_config( li->rwinfo, argc, argv );
185 #else /* !ENABLE_REWRITE */
186                 bd2 = ber_bvstrdup( argv[1] );
187                 ber_bvecadd( &li->suffix_massage, bd2 );
188                 nd2 = NULL;
189                 dnNormalize( NULL, bd2, &nd2 );
190                 ber_bvecadd( &li->suffix_massage, nd2 );
191                 
192                 bd2 = ber_bvstrdup( argv[2] );
193                 ber_bvecadd( &li->suffix_massage, bd2 );
194                 nd2 = NULL;
195                 dnNormalize( NULL, bd2, &nd2 );
196                 ber_bvecadd( &li->suffix_massage, nd2 );
197 #endif /* !ENABLE_REWRITE */
198
199 #ifdef ENABLE_REWRITE
200         /* rewrite stuff ... */
201         } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
202                 return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
203 #endif /* ENABLE_REWRITE */
204                 
205         /* objectclass/attribute mapping */
206         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
207                 struct ldapmap *map;
208                 struct ldapmapping *mapping;
209                 char *src, *dst;
210
211                 if ( argc < 3 || argc > 4 ) {
212                         fprintf( stderr,
213         "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
214                                 fname, lineno );
215                         return( 1 );
216                 }
217
218                 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
219                         map = &li->oc_map;
220                 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
221                         map = &li->at_map;
222                 } else {
223                         fprintf( stderr, "%s: line %d: syntax is "
224                                 "\"map {objectclass | attribute} {<source> | *} "
225                                         "[<dest> | *]\"\n",
226                                 fname, lineno );
227                         return( 1 );
228                 }
229
230                 if ( strcasecmp( argv[2], "*" ) != 0 ) {
231                         src = argv[2];
232                         if ( argc < 4 )
233                                 dst = "";
234                         else if ( strcasecmp( argv[3], "*" ) == 0 )
235                                 dst = src;
236                         else
237                                 dst = argv[3];
238                 } else {
239                         if ( argc < 4 ) {
240                                 map->drop_missing = 1;
241                                 return 0;
242                         }
243                         if ( strcasecmp( argv[3], "*" ) == 0 ) {
244                                 map->drop_missing = 0;
245                                 return 0;
246                         }
247
248                         src = argv[3];
249                         dst = src;
250                 }
251
252                 if ( ( map == &li->at_map )
253                         && ( strcasecmp( src, "objectclass" ) == 0
254                                 || strcasecmp( dst, "objectclass" ) == 0 ) )
255                 {
256                         fprintf( stderr,
257                                 "%s: line %d: objectclass attribute cannot be mapped\n",
258                                 fname, lineno );
259                 }
260
261                 mapping = (struct ldapmapping *)ch_calloc( 2,
262                         sizeof(struct ldapmapping) );
263                 if ( mapping == NULL ) {
264                         fprintf( stderr,
265                                 "%s: line %d: out of memory\n",
266                                 fname, lineno );
267                         return( 1 );
268                 }
269                 ber_str2bv( src, 0, 1, &mapping->src );
270                 ber_str2bv( dst, 0, 1, &mapping->dst );
271                 if ( *dst != 0 ) {
272                         mapping[1].src = mapping->dst;
273                         mapping[1].dst = mapping->src;
274                 } else {
275                         mapping[1].src = mapping->src;
276                         mapping[1].dst = mapping->dst;
277                 }
278
279                 if ( avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL ||
280                         avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
281                 {
282                         fprintf( stderr,
283                                 "%s: line %d: duplicate mapping found (ignored)\n",
284                                 fname, lineno );
285                         return 0;
286                 }
287
288                 avl_insert( &map->map, (caddr_t)mapping,
289                                         mapping_cmp, mapping_dup );
290                 avl_insert( &map->remap, (caddr_t)&mapping[1],
291                                         mapping_cmp, mapping_dup );
292
293         /* anything else */
294         } else {
295                 fprintf( stderr, "%s: line %d: unknown directive \"%s\" "
296                         "in ldap database definition (ignored)\n",
297                     fname, lineno, argv[0] );
298         }
299         return 0;
300 }
301
302 int
303 mapping_cmp ( const void *c1, const void *c2 )
304 {
305         struct ldapmapping *map1 = (struct ldapmapping *)c1;
306         struct ldapmapping *map2 = (struct ldapmapping *)c2;
307         int rc = map1->src.bv_len - map2->src.bv_len;
308         if (rc) return rc;
309         return ( strcasecmp(map1->src.bv_val, map2->src.bv_val) );
310 }
311
312 int
313 mapping_dup ( void *c1, void *c2 )
314 {
315         struct ldapmapping *map1 = (struct ldapmapping *)c1;
316         struct ldapmapping *map2 = (struct ldapmapping *)c2;
317
318         return( ( strcasecmp(map1->src.bv_val, map2->src.bv_val) == 0 ) ? -1 : 0 );
319 }
320
321 void
322 ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m )
323 {
324         struct ldapmapping *mapping;
325
326         assert( m );
327
328         *m = NULL;
329         
330         mapping = (struct ldapmapping *)ch_calloc( 2, 
331                         sizeof( struct ldapmapping ) );
332         if ( mapping == NULL ) {
333                 return;
334         }
335
336         ber_str2bv( "objectclass", sizeof("objectclass")-1, 1, &mapping->src);
337         ber_dupbv( &mapping->dst, &mapping->src );
338         mapping[1].src = mapping->src;
339         mapping[1].dst = mapping->dst;
340
341         avl_insert( &lm->map, (caddr_t)mapping, 
342                         mapping_cmp, mapping_dup );
343         avl_insert( &lm->remap, (caddr_t)&mapping[1], 
344                         mapping_cmp, mapping_dup );
345         *m = mapping;
346 }
347
348 void
349 ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
350         int remap )
351 {
352         Avlnode *tree;
353         struct ldapmapping *mapping, fmapping;
354
355         if (remap)
356                 tree = map->remap;
357         else
358                 tree = map->map;
359
360         bv->bv_len = 0;
361         bv->bv_val = NULL;
362         fmapping.src = *s;
363         mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
364         if (mapping != NULL) {
365                 if ( mapping->dst.bv_val )
366                         *bv = mapping->dst;
367                 return;
368         }
369
370         if (!map->drop_missing)
371                 *bv = *s;
372
373         return;
374 }
375
376 char *
377 ldap_back_map_filter(
378                 struct ldapmap *at_map,
379                 struct ldapmap *oc_map,
380                 struct berval *f,
381                 int remap
382 )
383 {
384         char *nf, *p, *q, *s, c;
385         int len, extra, plen, in_quote;
386         struct berval m, tmp;
387
388         if (f == NULL)
389                 return(NULL);
390
391         len = f->bv_len;
392         extra = len;
393         len *= 2;
394         nf = ch_malloc( len + 1 );
395         if (nf == NULL)
396                 return(NULL);
397
398         /* this loop assumes the filter ends with one
399          * of the delimiter chars -- probably ')'.
400          */
401
402         s = nf;
403         q = NULL;
404         in_quote = 0;
405         for (p = f->bv_val; (c = *p); p++) {
406                 if (c == '"') {
407                         in_quote = !in_quote;
408                         if (q != NULL) {
409                                 plen = p - q;
410                                 AC_MEMCPY(s, q, plen);
411                                 s += plen;
412                                 q = NULL;
413                         }
414                         *s++ = c;
415                 } else if (in_quote) {
416                         /* ignore everything in quotes --
417                          * what about attrs in DNs?
418                          */
419                         *s++ = c;
420                 } else if (c != '(' && c != ')'
421                         && c != '=' && c != '>' && c != '<'
422                         && c != '|' && c != '&')
423                 {
424                         if (q == NULL)
425                                 q = p;
426                 } else {
427                         if (q != NULL) {
428                                 *p = 0;
429                                 tmp.bv_len = p - q;
430                                 tmp.bv_val = q;
431                                 ldap_back_map(at_map, &tmp, &m, remap);
432                                 if (m.bv_val == NULL)
433                                         ldap_back_map(oc_map, &tmp, &m, remap);
434                                 if (m.bv_val == NULL) {
435                                         m = tmp;
436                                 }
437                                 extra += p - q;
438                                 plen = m.bv_len;
439                                 extra -= plen;
440                                 if (extra < 0) {
441                                         while (extra < 0) {
442                                                 extra += len;
443                                                 len *= 2;
444                                         }
445                                         s -= (long)nf;
446                                         nf = ch_realloc(nf, len + 1);
447                                         if (nf == NULL) {
448                                                 free(nf);
449                                                 return(NULL);
450                                         }
451                                         s += (long)nf;
452                                 }
453                                 AC_MEMCPY(s, m.bv_val, plen);
454                                 s += plen;
455                                 *p = c;
456                                 q = NULL;
457                         }
458                         *s++ = c;
459                 }
460         }
461         *s = 0;
462         return(nf);
463 }
464
465 char **
466 ldap_back_map_attrs(
467                 struct ldapmap *at_map,
468                 AttributeName *an,
469                 int remap
470 )
471 {
472         int i;
473         char **na;
474         struct berval mapped;
475
476         if (an == NULL)
477                 return(NULL);
478
479         for (i = 0; an[i].an_name.bv_val; i++) {
480                 /*  */
481         }
482
483         na = (char **)ch_calloc( i + 1, sizeof(char *) );
484         if (na == NULL)
485                 return(NULL);
486
487         for (i = 0; an[i].an_name.bv_val; i++) {
488                 ldap_back_map(at_map, &an[i].an_name, &mapped, remap);
489                 if (mapped.bv_val != NULL) {
490                         na[i] = mapped.bv_val;
491                         i++;
492                 }
493         }
494         return(na);
495 }
496
497 #ifdef ENABLE_REWRITE
498 static char *
499 suffix_massage_regexize( const char *s )
500 {
501         char *res, *p, *r;
502         int i;
503
504         for ( i = 0, p = ( char * )s; 
505                         ( r = strchr( p, ',' ) ) != NULL; 
506                         p = r + 1, i++ )
507                 ;
508
509         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
510
511         strcpy( res, "(.*)" );
512         for ( i = 0, p = ( char * )s;
513                         ( r = strchr( p, ',' ) ) != NULL;
514                         p = r + 1 , i++ ) {
515                 strncat( res, p, r - p + 1 );
516                 strcat( res, "[ ]?" );
517
518                 if ( r[ 1 ] == ' ' ) {
519                         r++;
520                 }
521         }
522         strcat( res, p );
523
524         return res;
525 }
526
527 static char *
528 suffix_massage_patternize( const char *s, int normalize )
529 {
530         char *res;
531
532         res = ch_calloc( sizeof( char ), strlen( s ) + sizeof("%1") );
533
534         sprintf( res, "%%1%s", s );
535
536         if ( normalize ) {
537                 char *out = dn_normalize( res + (sizeof("%1")-1) );
538                 if ( out != res + 2 ) {
539                         strcpy( res + 2, out );
540                         free( out );
541                 }
542         }
543
544         return res;
545 }
546
547 int
548 suffix_massage_config( 
549                 struct rewrite_info *info,
550                 int argc,
551                 char **argv
552 )
553 {
554         char *rargv[ 5 ];
555
556         rargv[ 0 ] = "rewriteEngine";
557         rargv[ 1 ] = "on";
558         rargv[ 2 ] = NULL;
559         rewrite_parse( info, "<suffix massage>", 1, 2, rargv );
560
561         rargv[ 0 ] = "rewriteContext";
562         rargv[ 1 ] = "default";
563         rargv[ 2 ] = NULL;
564         rewrite_parse( info, "<suffix massage>", 2, 2, rargv );
565
566         rargv[ 0 ] = "rewriteRule";
567         rargv[ 1 ] = suffix_massage_regexize( argv[ 1 ] );
568         rargv[ 2 ] = suffix_massage_patternize( argv[ 2 ], 0 );
569         rargv[ 3 ] = ":";
570         rargv[ 4 ] = NULL;
571         rewrite_parse( info, "<suffix massage>", 3, 4, rargv );
572         ch_free( rargv[ 1 ] );
573         ch_free( rargv[ 2 ] );
574         
575         rargv[ 0 ] = "rewriteContext";
576         rargv[ 1 ] = "searchResult";
577         rargv[ 2 ] = NULL;
578         rewrite_parse( info, "<suffix massage>", 4, 2, rargv );
579         
580         rargv[ 0 ] = "rewriteRule";
581         rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
582         rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 0 );
583         rargv[ 3 ] = ":";
584         rargv[ 4 ] = NULL;
585         rewrite_parse( info, "<suffix massage>", 5, 4, rargv );
586         ch_free( rargv[ 1 ] );
587         ch_free( rargv[ 2 ] );
588
589         /*
590          * the filter should be rewritten as
591          * 
592          * rewriteRule
593          *      "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
594          *      "%1member=%2,dc=example,dc=com%3"
595          *
596          * where "o=Foo Bar, c=US" is the virtual naming context,
597          * and "dc=example, dc=com" is the real naming context
598          */
599         rargv[ 0 ] = "rewriteContext";
600         rargv[ 1 ] = "searchFilter";
601         rargv[ 2 ] = NULL;
602         rewrite_parse( info, "<suffix massage>", 6, 2, rargv );
603
604 #if 0 /*  matched is not normalized */
605         rargv[ 0 ] = "rewriteContext";
606         rargv[ 1 ] = "matchedDn";
607         rargv[ 2 ] = "alias";
608         rargv[ 3 ] = "searchResult";
609         rargv[ 4 ] = NULL;
610         rewrite_parse( info, "<suffix massage>", 7, 4, rargv );
611 #else /* normalize matched */
612         rargv[ 0 ] = "rewriteContext";
613         rargv[ 1 ] = "matchedDn";
614         rargv[ 2 ] = NULL;
615         rewrite_parse( info, "<suffix massage>", 7, 2, rargv );
616
617         rargv[ 0 ] = "rewriteRule";
618         rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
619         rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 1 );
620         rargv[ 3 ] = ":";
621         rargv[ 4 ] = NULL;
622         rewrite_parse( info, "<suffix massage>", 8, 4, rargv );
623         ch_free( rargv[ 1 ] );
624         ch_free( rargv[ 2 ] );
625 #endif /* normalize matched */
626
627         return 0;
628 }
629 #endif /* ENABLE_REWRITE */