]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
db33946cb82f858e3c16189156062471c9b96225
[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;
118 #endif /* ENABLE_REWRITE */
119                 BackendDB *tmp_be;
120                 struct berval bdn, *ndn = NULL;
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 ( dnNormalize( 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                 ber_bvfree( ndn );
150                 ndn = NULL;
151                 if ( tmp_be != NULL && tmp_be != be ) {
152                         fprintf( stderr, "%s: line %d: suffix already in use"
153                                        " by another backend in"
154                                        " \"suffixMassage <suffix>"
155                                        " <massaged suffix>\"\n",
156                                 fname, lineno );
157                         return( 1 );                                            
158                 }
159
160                 bdn.bv_val = argv[2];
161                 bdn.bv_len = strlen(bdn.bv_val);
162                 if ( dnNormalize( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
163                         fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
164                                 fname, lineno, bdn.bv_val );
165                         return( 1 );
166                 }
167                 tmp_be = select_backend( ndn, 0, 0 );
168                 ber_bvfree( ndn );
169                 if ( tmp_be != NULL ) {
170                         fprintf( stderr, "%s: line %d: massaged suffix"
171                                        " already in use by another backend in" 
172                                        " \"suffixMassage <suffix>"
173                                        " <massaged suffix>\"\n",
174                                 fname, lineno );
175                         return( 1 );
176                 }
177
178 #ifdef ENABLE_REWRITE
179                 /*
180                  * The suffix massaging is emulated by means of the
181                  * rewrite capabilities
182                  * FIXME: no extra rewrite capabilities should be added
183                  * to the database
184                  */
185                 return suffix_massage_config( li->rwinfo, argc, argv );
186 #else /* !ENABLE_REWRITE */
187                 bd2 = ber_bvstrdup( argv[1] );
188                 ber_bvecadd( &li->suffix_massage, bd2 );
189                 ndn = NULL;
190                 dnNormalize( NULL, bd2, &ndn );
191                 ber_bvecadd( &li->suffix_massage, ndn );
192                 
193                 bd2 = ber_bvstrdup( argv[2] );
194                 ber_bvecadd( &li->suffix_massage, bd2 );
195                 ndn = NULL;
196                 dnNormalize( NULL, bd2, &ndn );
197                 ber_bvecadd( &li->suffix_massage, ndn );
198 #endif /* !ENABLE_REWRITE */
199
200 #ifdef ENABLE_REWRITE
201         /* rewrite stuff ... */
202         } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
203                 return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
204 #endif /* ENABLE_REWRITE */
205                 
206         /* objectclass/attribute mapping */
207         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
208                 struct ldapmap *map;
209                 struct ldapmapping *mapping;
210                 char *src, *dst;
211
212                 if ( argc < 3 || argc > 4 ) {
213                         fprintf( stderr,
214         "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
215                                 fname, lineno );
216                         return( 1 );
217                 }
218
219                 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
220                         map = &li->oc_map;
221                 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
222                         map = &li->at_map;
223                 } else {
224                         fprintf( stderr,
225         "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<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, sizeof(struct ldapmapping) );
262                 if ( mapping == NULL ) {
263                         fprintf( stderr,
264                                 "%s: line %d: out of memory\n",
265                                 fname, lineno );
266                         return( 1 );
267                 }
268                 mapping->src = ch_strdup(src);
269                 mapping->dst = ch_strdup(dst);
270                 if ( *dst != 0 ) {
271                         mapping[1].src = mapping->dst;
272                         mapping[1].dst = mapping->src;
273                 } else {
274                         mapping[1].src = mapping->src;
275                         mapping[1].dst = mapping->dst;
276                 }
277
278                 if ( avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL
279                         || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
280                 {
281                         fprintf( stderr,
282                                 "%s: line %d: duplicate mapping found (ignored)\n",
283                                 fname, lineno );
284                         return 0;
285                 }
286
287                 avl_insert( &map->map, (caddr_t)mapping,
288                                         mapping_cmp, mapping_dup );
289                 avl_insert( &map->remap, (caddr_t)&mapping[1],
290                                         mapping_cmp, mapping_dup );
291
292         /* anything else */
293         } else {
294                 fprintf( stderr,
295 "%s: line %d: unknown directive \"%s\" in ldap database definition (ignored)\n",
296                     fname, lineno, argv[0] );
297         }
298         return 0;
299 }
300
301 int
302 mapping_cmp ( const void *c1, const void *c2 )
303 {
304         struct ldapmapping *map1 = (struct ldapmapping *)c1;
305         struct ldapmapping *map2 = (struct ldapmapping *)c2;
306
307         return ( strcasecmp(map1->src, map2->src) );
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, map2->src) == 0 ) ? -1 : 0 );
317 }
318
319 char *
320 ldap_back_map ( struct ldapmap *map, char *s, int remap )
321 {
322         Avlnode *tree;
323         struct ldapmapping *mapping, fmapping;
324
325         if (remap)
326                 tree = map->remap;
327         else
328                 tree = map->map;
329
330         fmapping.src = s;
331         mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
332         if (mapping != NULL) {
333                 if ( *mapping->dst == 0 )
334                         return(NULL);
335                 return(mapping->dst);
336         }
337
338         if (map->drop_missing)
339                 return(NULL);
340
341         return(s);
342 }
343
344 char *
345 ldap_back_map_filter(
346                 struct ldapmap *at_map,
347                 struct ldapmap *oc_map,
348                 char *f,
349                 int remap
350 )
351 {
352         char *nf, *m, *p, *q, *s, c;
353         int len, extra, plen, in_quote;
354
355         if (f == NULL)
356                 return(NULL);
357
358         len = strlen(f);
359         extra = len;
360         len *= 2;
361         nf = ch_malloc( len + 1 );
362         if (nf == NULL)
363                 return(NULL);
364
365         /* this loop assumes the filter ends with one
366          * of the delimiter chars -- probably ')'.
367          */
368
369         s = nf;
370         q = NULL;
371         in_quote = 0;
372         for (p = f; (c = *p); p++) {
373                 if (c == '"') {
374                         in_quote = !in_quote;
375                         if (q != NULL) {
376                                 plen = p - q;
377                                 memcpy(s, q, plen);
378                                 s += plen;
379                                 q = NULL;
380                         }
381                         *s++ = c;
382                 } else if (in_quote) {
383                         /* ignore everything in quotes --
384                          * what about attrs in DNs?
385                          */
386                         *s++ = c;
387                 } else if (c != '(' && c != ')'
388                         && c != '=' && c != '>' && c != '<'
389                         && c != '|' && c != '&')
390                 {
391                         if (q == NULL)
392                                 q = p;
393                 } else {
394                         if (q != NULL) {
395                                 *p = 0;
396                                 m = ldap_back_map(at_map, q, remap);
397                                 if (m == NULL)
398                                         m = ldap_back_map(oc_map, q, remap);
399                                 if (m == NULL) {
400                                         m = q;
401                                 }
402                                 extra += p - q;
403                                 plen = strlen(m);
404                                 extra -= plen;
405                                 if (extra < 0) {
406                                         while (extra < 0) {
407                                                 extra += len;
408                                                 len *= 2;
409                                         }
410                                         s -= (long)nf;
411                                         nf = ch_realloc(nf, len + 1);
412                                         if (nf == NULL) {
413                                                 free(nf);
414                                                 return(NULL);
415                                         }
416                                         s += (long)nf;
417                                 }
418                                 memcpy(s, m, plen);
419                                 s += plen;
420                                 *p = c;
421                                 q = NULL;
422                         }
423                         *s++ = c;
424                 }
425         }
426         *s = 0;
427         return(nf);
428 }
429
430 char **
431 ldap_back_map_attrs(
432                 struct ldapmap *at_map,
433                 struct berval **a,
434                 int remap
435 )
436 {
437         int i, j, count;
438         char *mapped, **na;
439
440         if (a == NULL)
441                 return(NULL);
442
443         for (count = 0; a[count] != NULL; count++) {
444                 /*  */
445         }
446
447         na = (char **)ch_calloc( count + 1, sizeof(char *) );
448         if (na == NULL)
449                 return(NULL);
450
451         for (i = 0, j = 0; i < count; i++) {
452                 mapped = ldap_back_map(at_map, a[i]->bv_val, remap);
453                 if (mapped != NULL) {
454                         na[j] = mapped;
455                         j++;
456                 }
457         }
458         return(na);
459 }
460
461 #ifdef ENABLE_REWRITE
462 static char *
463 suffix_massage_regexize( const char *s )
464 {
465         char *res, *p, *r;
466         int i;
467
468         for ( i = 0, p = ( char * )s; 
469                         ( r = strchr( p, ',' ) ) != NULL; 
470                         p = r + 1, i++ )
471                 ;
472
473         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
474
475         strcpy( res, "(.*)" );
476         for ( i = 0, p = ( char * )s;
477                         ( r = strchr( p, ',' ) ) != NULL;
478                         p = r + 1 , i++ ) {
479                 strncat( res, p, r - p + 1 );
480                 strcat( res, "[ ]?" );
481
482                 if ( r[ 1 ] == ' ' ) {
483                         r++;
484                 }
485         }
486         strcat( res, p );
487
488         return res;
489 }
490
491 static char *
492 suffix_massage_patternize( const char *s, int normalize )
493 {
494         char *res;
495
496         res = ch_calloc( sizeof( char ), strlen( s ) + 3 );
497
498         sprintf( res, "%%1%s", s );
499
500         if ( normalize ) {
501                 char *out = dn_normalize( res + 2 );
502                 if ( out != res + 2 ) {
503                         strcpy( res + 2, out );
504                         free( out );
505                 }
506         }
507
508         return res;
509 }
510
511 int
512 suffix_massage_config( 
513                 struct rewrite_info *info,
514                 int argc,
515                 char **argv
516 )
517 {
518         char *rargv[ 5 ];
519
520         rargv[ 0 ] = "rewriteEngine";
521         rargv[ 1 ] = "on";
522         rargv[ 2 ] = NULL;
523         rewrite_parse( info, "<suffix massage>", 1, 2, rargv );
524
525         rargv[ 0 ] = "rewriteContext";
526         rargv[ 1 ] = "default";
527         rargv[ 2 ] = NULL;
528         rewrite_parse( info, "<suffix massage>", 2, 2, rargv );
529
530         rargv[ 0 ] = "rewriteRule";
531         rargv[ 1 ] = suffix_massage_regexize( argv[ 1 ] );
532         rargv[ 2 ] = suffix_massage_patternize( argv[ 2 ], 0 );
533         rargv[ 3 ] = ":";
534         rargv[ 4 ] = NULL;
535         rewrite_parse( info, "<suffix massage>", 3, 4, rargv );
536         ch_free( rargv[ 1 ] );
537         ch_free( rargv[ 2 ] );
538         
539         rargv[ 0 ] = "rewriteContext";
540         rargv[ 1 ] = "searchResult";
541         rargv[ 2 ] = NULL;
542         rewrite_parse( info, "<suffix massage>", 4, 2, rargv );
543         
544         rargv[ 0 ] = "rewriteRule";
545         rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
546         rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 0 );
547         rargv[ 3 ] = ":";
548         rargv[ 4 ] = NULL;
549         rewrite_parse( info, "<suffix massage>", 5, 4, rargv );
550         ch_free( rargv[ 1 ] );
551         ch_free( rargv[ 2 ] );
552
553         /*
554          * the filter should be rewritten as
555          * 
556          * rewriteRule
557          *      "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
558          *      "%1member=%2,dc=example,dc=com%3"
559          *
560          * where "o=Foo Bar, c=US" is the virtual naming context,
561          * and "dc=example, dc=com" is the real naming context
562          */
563         rargv[ 0 ] = "rewriteContext";
564         rargv[ 1 ] = "searchFilter";
565         rargv[ 2 ] = NULL;
566         rewrite_parse( info, "<suffix massage>", 6, 2, rargv );
567
568 #if 0 /*  matched is not normalized */
569         rargv[ 0 ] = "rewriteContext";
570         rargv[ 1 ] = "matchedDn";
571         rargv[ 2 ] = "alias";
572         rargv[ 3 ] = "searchResult";
573         rargv[ 4 ] = NULL;
574         rewrite_parse( info, "<suffix massage>", 7, 4, rargv );
575 #else /* normalize matched */
576         rargv[ 0 ] = "rewriteContext";
577         rargv[ 1 ] = "matchedDn";
578         rargv[ 2 ] = NULL;
579         rewrite_parse( info, "<suffix massage>", 7, 2, rargv );
580
581         rargv[ 0 ] = "rewriteRule";
582         rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
583         rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 1 );
584         rargv[ 3 ] = ":";
585         rargv[ 4 ] = NULL;
586         rewrite_parse( info, "<suffix massage>", 8, 4, rargv );
587         ch_free( rargv[ 1 ] );
588         ch_free( rargv[ 2 ] );
589 #endif /* normalize matched */
590
591         return 0;
592 }
593 #endif /* ENABLE_REWRITE */
594