]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
f6d2f3832b54ccf90a440cf810316e0b5390c37d
[openldap] / servers / slapd / back-ldap / config.c
1 /* config.c - ldap backend configuration file routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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         /* save bind creds for referral rebinds? */
115         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
116                 if (argc != 1) {
117                         fprintf( stderr,
118         "%s: line %d: rebind-as-user takes no arguments\n",
119                             fname, lineno );
120                         return( 1 );
121                 }
122                 li->savecred = 1;
123         
124         /* dn massaging */
125         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
126 #ifndef ENABLE_REWRITE
127                 struct berval *bd2, *nd2;
128 #endif /* ENABLE_REWRITE */
129                 BackendDB *tmp_be;
130                 struct berval bdn, ndn;
131                 
132                 /*
133                  * syntax:
134                  * 
135                  *      suffixmassage <suffix> <massaged suffix>
136                  *
137                  * the <suffix> field must be defined as a valid suffix
138                  * (or suffixAlias?) for the current database;
139                  * the <massaged suffix> shouldn't have already been
140                  * defined as a valid suffix or suffixAlias for the 
141                  * current server
142                  */
143                 if ( argc != 3 ) {
144                         fprintf( stderr, "%s: line %d: syntax is"
145                                        " \"suffixMassage <suffix>"
146                                        " <massaged suffix>\"\n",
147                                 fname, lineno );
148                         return( 1 );
149                 }
150                 
151                 bdn.bv_val = argv[1];
152                 bdn.bv_len = strlen(bdn.bv_val);
153                 if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
154                         fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
155                                 fname, lineno, bdn.bv_val );
156                         return( 1 );
157                 }
158                 tmp_be = select_backend( &ndn, 0, 0 );
159                 ch_free( ndn.bv_val );
160                 if ( tmp_be != NULL && tmp_be != be ) {
161                         fprintf( stderr, "%s: line %d: suffix already in use"
162                                        " by another backend in"
163                                        " \"suffixMassage <suffix>"
164                                        " <massaged suffix>\"\n",
165                                 fname, lineno );
166                         return( 1 );                                            
167                 }
168
169                 bdn.bv_val = argv[2];
170                 bdn.bv_len = strlen(bdn.bv_val);
171                 if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
172                         fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
173                                 fname, lineno, bdn.bv_val );
174                         return( 1 );
175                 }
176                 tmp_be = select_backend( &ndn, 0, 0 );
177                 ch_free( ndn.bv_val );
178                 if ( tmp_be != NULL ) {
179                         fprintf( stderr, "%s: line %d: massaged suffix"
180                                        " already in use by another backend in" 
181                                        " \"suffixMassage <suffix>"
182                                        " <massaged suffix>\"\n",
183                                 fname, lineno );
184                         return( 1 );
185                 }
186
187 #ifdef ENABLE_REWRITE
188                 /*
189                  * The suffix massaging is emulated by means of the
190                  * rewrite capabilities
191                  * FIXME: no extra rewrite capabilities should be added
192                  * to the database
193                  */
194                 return suffix_massage_config( li->rwinfo, argc, argv );
195 #else /* !ENABLE_REWRITE */
196                 bd2 = ber_bvstrdup( argv[1] );
197                 ber_bvecadd( &li->suffix_massage, bd2 );
198                 nd2 = NULL;
199                 dnNormalize( NULL, bd2, &nd2 );
200                 ber_bvecadd( &li->suffix_massage, nd2 );
201                 
202                 bd2 = ber_bvstrdup( argv[2] );
203                 ber_bvecadd( &li->suffix_massage, bd2 );
204                 nd2 = NULL;
205                 dnNormalize( NULL, bd2, &nd2 );
206                 ber_bvecadd( &li->suffix_massage, nd2 );
207 #endif /* !ENABLE_REWRITE */
208
209 #ifdef ENABLE_REWRITE
210         /* rewrite stuff ... */
211         } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
212                 return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
213 #endif /* ENABLE_REWRITE */
214                 
215         /* objectclass/attribute mapping */
216         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
217                 struct ldapmap *map;
218                 struct ldapmapping *mapping;
219                 char *src, *dst;
220
221                 if ( argc < 3 || argc > 4 ) {
222                         fprintf( stderr,
223         "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
224                                 fname, lineno );
225                         return( 1 );
226                 }
227
228                 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
229                         map = &li->oc_map;
230                 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
231                         map = &li->at_map;
232                 } else {
233                         fprintf( stderr, "%s: line %d: syntax is "
234                                 "\"map {objectclass | attribute} {<source> | *} "
235                                         "[<dest> | *]\"\n",
236                                 fname, lineno );
237                         return( 1 );
238                 }
239
240                 if ( strcasecmp( argv[2], "*" ) != 0 ) {
241                         src = argv[2];
242                         if ( argc < 4 )
243                                 dst = "";
244                         else if ( strcasecmp( argv[3], "*" ) == 0 )
245                                 dst = src;
246                         else
247                                 dst = argv[3];
248                 } else {
249                         if ( argc < 4 ) {
250                                 map->drop_missing = 1;
251                                 return 0;
252                         }
253                         if ( strcasecmp( argv[3], "*" ) == 0 ) {
254                                 map->drop_missing = 0;
255                                 return 0;
256                         }
257
258                         src = argv[3];
259                         dst = src;
260                 }
261
262                 if ( ( map == &li->at_map )
263                         && ( strcasecmp( src, "objectclass" ) == 0
264                                 || strcasecmp( dst, "objectclass" ) == 0 ) )
265                 {
266                         fprintf( stderr,
267                                 "%s: line %d: objectclass attribute cannot be mapped\n",
268                                 fname, lineno );
269                 }
270
271                 mapping = (struct ldapmapping *)ch_calloc( 2,
272                         sizeof(struct ldapmapping) );
273                 if ( mapping == NULL ) {
274                         fprintf( stderr,
275                                 "%s: line %d: out of memory\n",
276                                 fname, lineno );
277                         return( 1 );
278                 }
279                 ber_str2bv( src, 0, 1, &mapping->src );
280                 ber_str2bv( dst, 0, 1, &mapping->dst );
281                 if ( *dst != 0 ) {
282                         mapping[1].src = mapping->dst;
283                         mapping[1].dst = mapping->src;
284                 } else {
285                         mapping[1].src = mapping->src;
286                         mapping[1].dst = mapping->dst;
287                 }
288
289                 if ( avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL ||
290                         avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
291                 {
292                         fprintf( stderr,
293                                 "%s: line %d: duplicate mapping found (ignored)\n",
294                                 fname, lineno );
295                         return 0;
296                 }
297
298                 avl_insert( &map->map, (caddr_t)mapping,
299                                         mapping_cmp, mapping_dup );
300                 avl_insert( &map->remap, (caddr_t)&mapping[1],
301                                         mapping_cmp, mapping_dup );
302
303         /* anything else */
304         } else {
305                 fprintf( stderr, "%s: line %d: unknown directive \"%s\" "
306                         "in ldap database definition (ignored)\n",
307                     fname, lineno, argv[0] );
308         }
309         return 0;
310 }
311
312 #ifdef ENABLE_REWRITE
313 static char *
314 suffix_massage_regexize( const char *s )
315 {
316         char *res, *ptr;
317         const char *p, *r;
318         int i;
319
320         for ( i = 0, p = s; 
321                         ( r = strchr( p, ',' ) ) != NULL; 
322                         p = r + 1, i++ )
323                 ;
324
325         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
326
327         ptr = slap_strcopy( res, "(.*)" );
328         for ( i = 0, p = s;
329                         ( r = strchr( p, ',' ) ) != NULL;
330                         p = r + 1 , i++ ) {
331                 ptr = slap_strncopy( ptr, p, r - p + 1 );
332                 ptr = slap_strcopy( ptr, "[ ]?" );
333
334                 if ( r[ 1 ] == ' ' ) {
335                         r++;
336                 }
337         }
338         slap_strcopy( ptr, p );
339
340         return res;
341 }
342
343 static char *
344 suffix_massage_patternize( const char *s, int normalize )
345 {
346         struct berval   dn = { 0, NULL }, odn = { 0, NULL };
347         int             rc;
348         char            *res;
349
350         dn.bv_val = ( char * )s;
351         dn.bv_len = strlen( s );
352
353         if ( normalize ) {
354                 rc = dnNormalize2( NULL, &dn, &odn );
355         } else {
356                 rc = dnPretty2( NULL, &dn, &odn );
357         }
358
359         if ( rc != LDAP_SUCCESS ) {
360                 return NULL;
361         }
362         
363         res = ch_calloc( sizeof( char ), odn.bv_len + sizeof( "%1" ) );
364         if ( res == NULL ) {
365                 return NULL;
366         }
367
368         strcpy( res, "%1" );
369         strcpy( res + sizeof( "%1" ) - 1, odn.bv_val );
370
371         /* FIXME: what FREE should I use? */
372         free( odn.bv_val );
373
374         return res;
375 }
376
377 int
378 suffix_massage_config( 
379                 struct rewrite_info *info,
380                 int argc,
381                 char **argv
382 )
383 {
384         char *rargv[ 5 ];
385
386         rargv[ 0 ] = "rewriteEngine";
387         rargv[ 1 ] = "on";
388         rargv[ 2 ] = NULL;
389         rewrite_parse( info, "<suffix massage>", 1, 2, rargv );
390
391         rargv[ 0 ] = "rewriteContext";
392         rargv[ 1 ] = "default";
393         rargv[ 2 ] = NULL;
394         rewrite_parse( info, "<suffix massage>", 2, 2, rargv );
395
396         rargv[ 0 ] = "rewriteRule";
397         rargv[ 1 ] = suffix_massage_regexize( argv[ 1 ] );
398         rargv[ 2 ] = suffix_massage_patternize( argv[ 2 ], 0 );
399         rargv[ 3 ] = ":";
400         rargv[ 4 ] = NULL;
401         rewrite_parse( info, "<suffix massage>", 3, 4, rargv );
402         ch_free( rargv[ 1 ] );
403         ch_free( rargv[ 2 ] );
404         
405         rargv[ 0 ] = "rewriteContext";
406         rargv[ 1 ] = "searchResult";
407         rargv[ 2 ] = NULL;
408         rewrite_parse( info, "<suffix massage>", 4, 2, rargv );
409         
410         rargv[ 0 ] = "rewriteRule";
411         rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
412         rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 0 );
413         rargv[ 3 ] = ":";
414         rargv[ 4 ] = NULL;
415         rewrite_parse( info, "<suffix massage>", 5, 4, rargv );
416         ch_free( rargv[ 1 ] );
417         ch_free( rargv[ 2 ] );
418
419         /*
420          * the filter should be rewritten as
421          * 
422          * rewriteRule
423          *      "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
424          *      "%1member=%2,dc=example,dc=com%3"
425          *
426          * where "o=Foo Bar, c=US" is the virtual naming context,
427          * and "dc=example, dc=com" is the real naming context
428          */
429         rargv[ 0 ] = "rewriteContext";
430         rargv[ 1 ] = "searchFilter";
431         rargv[ 2 ] = NULL;
432         rewrite_parse( info, "<suffix massage>", 6, 2, rargv );
433
434 #if 0 /*  matched is not normalized */
435         rargv[ 0 ] = "rewriteContext";
436         rargv[ 1 ] = "matchedDn";
437         rargv[ 2 ] = "alias";
438         rargv[ 3 ] = "searchResult";
439         rargv[ 4 ] = NULL;
440         rewrite_parse( info, "<suffix massage>", 7, 4, rargv );
441 #else /* normalize matched */
442         rargv[ 0 ] = "rewriteContext";
443         rargv[ 1 ] = "matchedDn";
444         rargv[ 2 ] = NULL;
445         rewrite_parse( info, "<suffix massage>", 7, 2, rargv );
446
447         rargv[ 0 ] = "rewriteRule";
448         rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
449         rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 1 );
450         rargv[ 3 ] = ":";
451         rargv[ 4 ] = NULL;
452         rewrite_parse( info, "<suffix massage>", 8, 4, rargv );
453         ch_free( rargv[ 1 ] );
454         ch_free( rargv[ 2 ] );
455 #endif /* normalize matched */
456
457         return 0;
458 }
459 #endif /* ENABLE_REWRITE */