]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
Added dnPretty2/dnNormalize2 using preallocated destination berval
[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                 mapping->src = ch_strdup(src);
268                 mapping->dst = ch_strdup(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
306         return ( strcasecmp(map1->src, map2->src) );
307 }
308
309 int
310 mapping_dup ( void *c1, void *c2 )
311 {
312         struct ldapmapping *map1 = (struct ldapmapping *)c1;
313         struct ldapmapping *map2 = (struct ldapmapping *)c2;
314
315         return( ( strcasecmp(map1->src, map2->src) == 0 ) ? -1 : 0 );
316 }
317
318 char *
319 ldap_back_map ( struct ldapmap *map, char *s, int remap )
320 {
321         Avlnode *tree;
322         struct ldapmapping *mapping, fmapping;
323
324         if (remap)
325                 tree = map->remap;
326         else
327                 tree = map->map;
328
329         fmapping.src = s;
330         mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
331         if (mapping != NULL) {
332                 if ( *mapping->dst == 0 )
333                         return(NULL);
334                 return(mapping->dst);
335         }
336
337         if (map->drop_missing)
338                 return(NULL);
339
340         return(s);
341 }
342
343 char *
344 ldap_back_map_filter(
345                 struct ldapmap *at_map,
346                 struct ldapmap *oc_map,
347                 char *f,
348                 int remap
349 )
350 {
351         char *nf, *m, *p, *q, *s, c;
352         int len, extra, plen, in_quote;
353
354         if (f == NULL)
355                 return(NULL);
356
357         len = strlen(f);
358         extra = len;
359         len *= 2;
360         nf = ch_malloc( len + 1 );
361         if (nf == NULL)
362                 return(NULL);
363
364         /* this loop assumes the filter ends with one
365          * of the delimiter chars -- probably ')'.
366          */
367
368         s = nf;
369         q = NULL;
370         in_quote = 0;
371         for (p = f; (c = *p); p++) {
372                 if (c == '"') {
373                         in_quote = !in_quote;
374                         if (q != NULL) {
375                                 plen = p - q;
376                                 memcpy(s, q, plen);
377                                 s += plen;
378                                 q = NULL;
379                         }
380                         *s++ = c;
381                 } else if (in_quote) {
382                         /* ignore everything in quotes --
383                          * what about attrs in DNs?
384                          */
385                         *s++ = c;
386                 } else if (c != '(' && c != ')'
387                         && c != '=' && c != '>' && c != '<'
388                         && c != '|' && c != '&')
389                 {
390                         if (q == NULL)
391                                 q = p;
392                 } else {
393                         if (q != NULL) {
394                                 *p = 0;
395                                 m = ldap_back_map(at_map, q, remap);
396                                 if (m == NULL)
397                                         m = ldap_back_map(oc_map, q, remap);
398                                 if (m == NULL) {
399                                         m = q;
400                                 }
401                                 extra += p - q;
402                                 plen = strlen(m);
403                                 extra -= plen;
404                                 if (extra < 0) {
405                                         while (extra < 0) {
406                                                 extra += len;
407                                                 len *= 2;
408                                         }
409                                         s -= (long)nf;
410                                         nf = ch_realloc(nf, len + 1);
411                                         if (nf == NULL) {
412                                                 free(nf);
413                                                 return(NULL);
414                                         }
415                                         s += (long)nf;
416                                 }
417                                 memcpy(s, m, plen);
418                                 s += plen;
419                                 *p = c;
420                                 q = NULL;
421                         }
422                         *s++ = c;
423                 }
424         }
425         *s = 0;
426         return(nf);
427 }
428
429 char **
430 ldap_back_map_attrs(
431                 struct ldapmap *at_map,
432                 struct berval **a,
433                 int remap
434 )
435 {
436         int i, j, count;
437         char *mapped, **na;
438
439         if (a == NULL)
440                 return(NULL);
441
442         for (count = 0; a[count] != NULL; count++) {
443                 /*  */
444         }
445
446         na = (char **)ch_calloc( count + 1, sizeof(char *) );
447         if (na == NULL)
448                 return(NULL);
449
450         for (i = 0, j = 0; i < count; i++) {
451                 mapped = ldap_back_map(at_map, a[i]->bv_val, remap);
452                 if (mapped != NULL) {
453                         na[j] = mapped;
454                         j++;
455                 }
456         }
457         return(na);
458 }
459
460 #ifdef ENABLE_REWRITE
461 static char *
462 suffix_massage_regexize( const char *s )
463 {
464         char *res, *p, *r;
465         int i;
466
467         for ( i = 0, p = ( char * )s; 
468                         ( r = strchr( p, ',' ) ) != NULL; 
469                         p = r + 1, i++ )
470                 ;
471
472         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
473
474         strcpy( res, "(.*)" );
475         for ( i = 0, p = ( char * )s;
476                         ( r = strchr( p, ',' ) ) != NULL;
477                         p = r + 1 , i++ ) {
478                 strncat( res, p, r - p + 1 );
479                 strcat( res, "[ ]?" );
480
481                 if ( r[ 1 ] == ' ' ) {
482                         r++;
483                 }
484         }
485         strcat( res, p );
486
487         return res;
488 }
489
490 static char *
491 suffix_massage_patternize( const char *s, int normalize )
492 {
493         char *res;
494
495         res = ch_calloc( sizeof( char ), strlen( s ) + 3 );
496
497         sprintf( res, "%%1%s", s );
498
499         if ( normalize ) {
500                 char *out = dn_normalize( res + 2 );
501                 if ( out != res + 2 ) {
502                         strcpy( res + 2, out );
503                         free( out );
504                 }
505         }
506
507         return res;
508 }
509
510 int
511 suffix_massage_config( 
512                 struct rewrite_info *info,
513                 int argc,
514                 char **argv
515 )
516 {
517         char *rargv[ 5 ];
518
519         rargv[ 0 ] = "rewriteEngine";
520         rargv[ 1 ] = "on";
521         rargv[ 2 ] = NULL;
522         rewrite_parse( info, "<suffix massage>", 1, 2, rargv );
523
524         rargv[ 0 ] = "rewriteContext";
525         rargv[ 1 ] = "default";
526         rargv[ 2 ] = NULL;
527         rewrite_parse( info, "<suffix massage>", 2, 2, rargv );
528
529         rargv[ 0 ] = "rewriteRule";
530         rargv[ 1 ] = suffix_massage_regexize( argv[ 1 ] );
531         rargv[ 2 ] = suffix_massage_patternize( argv[ 2 ], 0 );
532         rargv[ 3 ] = ":";
533         rargv[ 4 ] = NULL;
534         rewrite_parse( info, "<suffix massage>", 3, 4, rargv );
535         ch_free( rargv[ 1 ] );
536         ch_free( rargv[ 2 ] );
537         
538         rargv[ 0 ] = "rewriteContext";
539         rargv[ 1 ] = "searchResult";
540         rargv[ 2 ] = NULL;
541         rewrite_parse( info, "<suffix massage>", 4, 2, rargv );
542         
543         rargv[ 0 ] = "rewriteRule";
544         rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
545         rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 0 );
546         rargv[ 3 ] = ":";
547         rargv[ 4 ] = NULL;
548         rewrite_parse( info, "<suffix massage>", 5, 4, rargv );
549         ch_free( rargv[ 1 ] );
550         ch_free( rargv[ 2 ] );
551
552         /*
553          * the filter should be rewritten as
554          * 
555          * rewriteRule
556          *      "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
557          *      "%1member=%2,dc=example,dc=com%3"
558          *
559          * where "o=Foo Bar, c=US" is the virtual naming context,
560          * and "dc=example, dc=com" is the real naming context
561          */
562         rargv[ 0 ] = "rewriteContext";
563         rargv[ 1 ] = "searchFilter";
564         rargv[ 2 ] = NULL;
565         rewrite_parse( info, "<suffix massage>", 6, 2, rargv );
566
567 #if 0 /*  matched is not normalized */
568         rargv[ 0 ] = "rewriteContext";
569         rargv[ 1 ] = "matchedDn";
570         rargv[ 2 ] = "alias";
571         rargv[ 3 ] = "searchResult";
572         rargv[ 4 ] = NULL;
573         rewrite_parse( info, "<suffix massage>", 7, 4, rargv );
574 #else /* normalize matched */
575         rargv[ 0 ] = "rewriteContext";
576         rargv[ 1 ] = "matchedDn";
577         rargv[ 2 ] = NULL;
578         rewrite_parse( info, "<suffix massage>", 7, 2, rargv );
579
580         rargv[ 0 ] = "rewriteRule";
581         rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
582         rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 1 );
583         rargv[ 3 ] = ":";
584         rargv[ 4 ] = NULL;
585         rewrite_parse( info, "<suffix massage>", 8, 4, rargv );
586         ch_free( rargv[ 1 ] );
587         ch_free( rargv[ 2 ] );
588 #endif /* normalize matched */
589
590         return 0;
591 }
592 #endif /* ENABLE_REWRITE */
593