]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
Changed struct berval ** to BVarray
[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,
224         "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
225                                 fname, lineno );
226                         return( 1 );
227                 }
228
229                 if ( strcasecmp( argv[2], "*" ) != 0 ) {
230                         src = argv[2];
231                         if ( argc < 4 )
232                                 dst = "";
233                         else if ( strcasecmp( argv[3], "*" ) == 0 )
234                                 dst = src;
235                         else
236                                 dst = argv[3];
237                 } else {
238                         if ( argc < 4 ) {
239                                 map->drop_missing = 1;
240                                 return 0;
241                         }
242                         if ( strcasecmp( argv[3], "*" ) == 0 ) {
243                                 map->drop_missing = 0;
244                                 return 0;
245                         }
246
247                         src = argv[3];
248                         dst = src;
249                 }
250
251                 if ( ( map == &li->at_map )
252                         && ( strcasecmp( src, "objectclass" ) == 0
253                                 || strcasecmp( dst, "objectclass" ) == 0 ) )
254                 {
255                         fprintf( stderr,
256                                 "%s: line %d: objectclass attribute cannot be mapped\n",
257                                 fname, lineno );
258                 }
259
260                 mapping = (struct ldapmapping *)ch_calloc( 2, sizeof(struct ldapmapping) );
261                 if ( mapping == NULL ) {
262                         fprintf( stderr,
263                                 "%s: line %d: out of memory\n",
264                                 fname, lineno );
265                         return( 1 );
266                 }
267                 ber_str2bv( src, 0, 1, &mapping->src );
268                 ber_str2bv( dst, 0, 1, &mapping->dst );
269                 if ( *dst != 0 ) {
270                         mapping[1].src = mapping->dst;
271                         mapping[1].dst = mapping->src;
272                 } else {
273                         mapping[1].src = mapping->src;
274                         mapping[1].dst = mapping->dst;
275                 }
276
277                 if ( avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL
278                         || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
279                 {
280                         fprintf( stderr,
281                                 "%s: line %d: duplicate mapping found (ignored)\n",
282                                 fname, lineno );
283                         return 0;
284                 }
285
286                 avl_insert( &map->map, (caddr_t)mapping,
287                                         mapping_cmp, mapping_dup );
288                 avl_insert( &map->remap, (caddr_t)&mapping[1],
289                                         mapping_cmp, mapping_dup );
290
291         /* anything else */
292         } else {
293                 fprintf( stderr,
294 "%s: line %d: unknown directive \"%s\" in ldap database definition (ignored)\n",
295                     fname, lineno, argv[0] );
296         }
297         return 0;
298 }
299
300 int
301 mapping_cmp ( const void *c1, const void *c2 )
302 {
303         struct ldapmapping *map1 = (struct ldapmapping *)c1;
304         struct ldapmapping *map2 = (struct ldapmapping *)c2;
305         int rc = map1->src.bv_len - map2->src.bv_len;
306         if (rc) return rc;
307         return ( strcasecmp(map1->src.bv_val, map2->src.bv_val) );
308 }
309
310 int
311 mapping_dup ( void *c1, void *c2 )
312 {
313         struct ldapmapping *map1 = (struct ldapmapping *)c1;
314         struct ldapmapping *map2 = (struct ldapmapping *)c2;
315
316         return( ( strcasecmp(map1->src.bv_val, map2->src.bv_val) == 0 ) ? -1 : 0 );
317 }
318
319 void
320 ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
321         int remap )
322 {
323         Avlnode *tree;
324         struct ldapmapping *mapping, fmapping;
325
326         if (remap)
327                 tree = map->remap;
328         else
329                 tree = map->map;
330
331         bv->bv_len = 0;
332         bv->bv_val = NULL;
333         fmapping.src = *s;
334         mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
335         if (mapping != NULL) {
336                 if ( mapping->dst.bv_val )
337                         *bv = mapping->dst;
338                 return;
339         }
340
341         if (!map->drop_missing)
342                 *bv = *s;
343
344         return;
345 }
346
347 char *
348 ldap_back_map_filter(
349                 struct ldapmap *at_map,
350                 struct ldapmap *oc_map,
351                 struct berval *f,
352                 int remap
353 )
354 {
355         char *nf, *p, *q, *s, c;
356         int len, extra, plen, in_quote;
357         struct berval m, tmp;
358
359         if (f == NULL)
360                 return(NULL);
361
362         len = f->bv_len;
363         extra = len;
364         len *= 2;
365         nf = ch_malloc( len + 1 );
366         if (nf == NULL)
367                 return(NULL);
368
369         /* this loop assumes the filter ends with one
370          * of the delimiter chars -- probably ')'.
371          */
372
373         s = nf;
374         q = NULL;
375         in_quote = 0;
376         for (p = f->bv_val; (c = *p); p++) {
377                 if (c == '"') {
378                         in_quote = !in_quote;
379                         if (q != NULL) {
380                                 plen = p - q;
381                                 memcpy(s, q, plen);
382                                 s += plen;
383                                 q = NULL;
384                         }
385                         *s++ = c;
386                 } else if (in_quote) {
387                         /* ignore everything in quotes --
388                          * what about attrs in DNs?
389                          */
390                         *s++ = c;
391                 } else if (c != '(' && c != ')'
392                         && c != '=' && c != '>' && c != '<'
393                         && c != '|' && c != '&')
394                 {
395                         if (q == NULL)
396                                 q = p;
397                 } else {
398                         if (q != NULL) {
399                                 *p = 0;
400                                 tmp.bv_len = p - q;
401                                 tmp.bv_val = q;
402                                 ldap_back_map(at_map, &tmp, &m, remap);
403                                 if (m.bv_val == NULL)
404                                         ldap_back_map(oc_map, &tmp, &m, remap);
405                                 if (m.bv_val == NULL) {
406                                         m = tmp;
407                                 }
408                                 extra += p - q;
409                                 plen = m.bv_len;
410                                 extra -= plen;
411                                 if (extra < 0) {
412                                         while (extra < 0) {
413                                                 extra += len;
414                                                 len *= 2;
415                                         }
416                                         s -= (long)nf;
417                                         nf = ch_realloc(nf, len + 1);
418                                         if (nf == NULL) {
419                                                 free(nf);
420                                                 return(NULL);
421                                         }
422                                         s += (long)nf;
423                                 }
424                                 memcpy(s, m.bv_val, plen);
425                                 s += plen;
426                                 *p = c;
427                                 q = NULL;
428                         }
429                         *s++ = c;
430                 }
431         }
432         *s = 0;
433         return(nf);
434 }
435
436 char **
437 ldap_back_map_attrs(
438                 struct ldapmap *at_map,
439                 AttributeName *a,
440                 int remap
441 )
442 {
443         int i;
444         char **na;
445         AttributeName *an;
446         struct berval mapped;
447
448         if (a == NULL)
449                 return(NULL);
450
451         for (i = 0, an=a; an; an=an->an_next, i++) {
452                 /*  */
453         }
454
455         na = (char **)ch_calloc( i + 1, sizeof(char *) );
456         if (na == NULL)
457                 return(NULL);
458
459         for (i = 0, an=a; an; an=an->an_next) {
460                 ldap_back_map(at_map, &an->an_name, &mapped, remap);
461                 if (mapped.bv_val != NULL) {
462                         na[i] = mapped.bv_val;
463                         i++;
464                 }
465         }
466         return(na);
467 }
468
469 #ifdef ENABLE_REWRITE
470 static char *
471 suffix_massage_regexize( const char *s )
472 {
473         char *res, *p, *r;
474         int i;
475
476         for ( i = 0, p = ( char * )s; 
477                         ( r = strchr( p, ',' ) ) != NULL; 
478                         p = r + 1, i++ )
479                 ;
480
481         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
482
483         strcpy( res, "(.*)" );
484         for ( i = 0, p = ( char * )s;
485                         ( r = strchr( p, ',' ) ) != NULL;
486                         p = r + 1 , i++ ) {
487                 strncat( res, p, r - p + 1 );
488                 strcat( res, "[ ]?" );
489
490                 if ( r[ 1 ] == ' ' ) {
491                         r++;
492                 }
493         }
494         strcat( res, p );
495
496         return res;
497 }
498
499 static char *
500 suffix_massage_patternize( const char *s, int normalize )
501 {
502         char *res;
503
504         res = ch_calloc( sizeof( char ), strlen( s ) + 3 );
505
506         sprintf( res, "%%1%s", s );
507
508         if ( normalize ) {
509                 char *out = dn_normalize( res + 2 );
510                 if ( out != res + 2 ) {
511                         strcpy( res + 2, out );
512                         free( out );
513                 }
514         }
515
516         return res;
517 }
518
519 int
520 suffix_massage_config( 
521                 struct rewrite_info *info,
522                 int argc,
523                 char **argv
524 )
525 {
526         char *rargv[ 5 ];
527
528         rargv[ 0 ] = "rewriteEngine";
529         rargv[ 1 ] = "on";
530         rargv[ 2 ] = NULL;
531         rewrite_parse( info, "<suffix massage>", 1, 2, rargv );
532
533         rargv[ 0 ] = "rewriteContext";
534         rargv[ 1 ] = "default";
535         rargv[ 2 ] = NULL;
536         rewrite_parse( info, "<suffix massage>", 2, 2, rargv );
537
538         rargv[ 0 ] = "rewriteRule";
539         rargv[ 1 ] = suffix_massage_regexize( argv[ 1 ] );
540         rargv[ 2 ] = suffix_massage_patternize( argv[ 2 ], 0 );
541         rargv[ 3 ] = ":";
542         rargv[ 4 ] = NULL;
543         rewrite_parse( info, "<suffix massage>", 3, 4, rargv );
544         ch_free( rargv[ 1 ] );
545         ch_free( rargv[ 2 ] );
546         
547         rargv[ 0 ] = "rewriteContext";
548         rargv[ 1 ] = "searchResult";
549         rargv[ 2 ] = NULL;
550         rewrite_parse( info, "<suffix massage>", 4, 2, rargv );
551         
552         rargv[ 0 ] = "rewriteRule";
553         rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
554         rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 0 );
555         rargv[ 3 ] = ":";
556         rargv[ 4 ] = NULL;
557         rewrite_parse( info, "<suffix massage>", 5, 4, rargv );
558         ch_free( rargv[ 1 ] );
559         ch_free( rargv[ 2 ] );
560
561         /*
562          * the filter should be rewritten as
563          * 
564          * rewriteRule
565          *      "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
566          *      "%1member=%2,dc=example,dc=com%3"
567          *
568          * where "o=Foo Bar, c=US" is the virtual naming context,
569          * and "dc=example, dc=com" is the real naming context
570          */
571         rargv[ 0 ] = "rewriteContext";
572         rargv[ 1 ] = "searchFilter";
573         rargv[ 2 ] = NULL;
574         rewrite_parse( info, "<suffix massage>", 6, 2, rargv );
575
576 #if 0 /*  matched is not normalized */
577         rargv[ 0 ] = "rewriteContext";
578         rargv[ 1 ] = "matchedDn";
579         rargv[ 2 ] = "alias";
580         rargv[ 3 ] = "searchResult";
581         rargv[ 4 ] = NULL;
582         rewrite_parse( info, "<suffix massage>", 7, 4, rargv );
583 #else /* normalize matched */
584         rargv[ 0 ] = "rewriteContext";
585         rargv[ 1 ] = "matchedDn";
586         rargv[ 2 ] = NULL;
587         rewrite_parse( info, "<suffix massage>", 7, 2, rargv );
588
589         rargv[ 0 ] = "rewriteRule";
590         rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
591         rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 1 );
592         rargv[ 3 ] = ":";
593         rargv[ 4 ] = NULL;
594         rewrite_parse( info, "<suffix massage>", 8, 4, rargv );
595         ch_free( rargv[ 1 ] );
596         ch_free( rargv[ 2 ] );
597 #endif /* normalize matched */
598
599         return 0;
600 }
601 #endif /* ENABLE_REWRITE */
602