]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
f40fd8273f3aefca0f40785b4397ac9d0364218f
[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                 BackendDB *tmp_be;
127                 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
128 #ifdef ENABLE_REWRITE
129                 int rc;
130 #endif /* ENABLE_REWRITE */
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                 ber_str2bv( argv[1], 0, 0, &bvnc );
152                 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc ) != LDAP_SUCCESS ) {
153                         fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
154                                 fname, lineno, bvnc.bv_val );
155                         return( 1 );
156                 }
157                 tmp_be = select_backend( &nvnc, 0, 0 );
158                 if ( tmp_be != NULL && tmp_be != be ) {
159                         fprintf( stderr, "%s: line %d: suffix already in use"
160                                        " by another backend in"
161                                        " \"suffixMassage <suffix>"
162                                        " <massaged suffix>\"\n",
163                                 fname, lineno );
164                         free( nvnc.bv_val );
165                         free( pvnc.bv_val );
166                         return( 1 );
167                 }
168
169                 ber_str2bv( argv[2], 0, 0, &brnc );
170                 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc ) != LDAP_SUCCESS ) {
171                         fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
172                                 fname, lineno, brnc.bv_val );
173                         free( nvnc.bv_val );
174                         free( pvnc.bv_val );
175                         return( 1 );
176                 }
177
178 #if 0
179                 tmp_be = select_backend( &nrnc, 0, 0 );
180                 if ( tmp_be != NULL ) {
181                         fprintf( stderr, "%s: line %d: massaged suffix"
182                                        " already in use by another backend in" 
183                                        " \"suffixMassage <suffix>"
184                                        " <massaged suffix>\"\n",
185                                 fname, lineno );
186                         free( nvnc.bv_val );
187                         free( pvnc.bv_val );
188                         free( nrnc.bv_val );
189                         free( prnc.bv_val );
190                         return( 1 );
191                 }
192 #endif
193
194 #ifdef ENABLE_REWRITE
195                 /*
196                  * The suffix massaging is emulated by means of the
197                  * rewrite capabilities
198                  * FIXME: no extra rewrite capabilities should be added
199                  * to the database
200                  */
201                 rc = suffix_massage_config( li->rwinfo, &pvnc, &nvnc, &prnc, &nrnc );
202                 free( nvnc.bv_val );
203                 free( pvnc.bv_val );
204                 free( nrnc.bv_val );
205                 free( prnc.bv_val );
206
207                 return( rc );
208
209 #else /* !ENABLE_REWRITE */
210                 ber_bvarray_add( &li->suffix_massage, &pvnc );
211                 ber_bvarray_add( &li->suffix_massage, &nvnc );
212                 
213                 ber_bvarray_add( &li->suffix_massage, &prnc );
214                 ber_bvarray_add( &li->suffix_massage, &nrnc );
215 #endif /* !ENABLE_REWRITE */
216
217         /* rewrite stuff ... */
218         } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
219 #ifdef ENABLE_REWRITE
220                 return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
221
222 #else /* !ENABLE_REWRITE */
223                 fprintf( stderr, "%s: line %d: rewrite capabilities "
224                                 "are not enabled\n", fname, lineno );
225 #endif /* !ENABLE_REWRITE */
226                 
227         /* objectclass/attribute mapping */
228         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
229                 struct ldapmap *map;
230                 struct ldapmapping *mapping;
231                 char *src, *dst;
232
233                 if ( argc < 3 || argc > 4 ) {
234                         fprintf( stderr,
235         "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
236                                 fname, lineno );
237                         return( 1 );
238                 }
239
240                 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
241                         map = &li->oc_map;
242                 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
243                         map = &li->at_map;
244                 } else {
245                         fprintf( stderr, "%s: line %d: syntax is "
246                                 "\"map {objectclass | attribute} {<source> | *} "
247                                         "[<dest> | *]\"\n",
248                                 fname, lineno );
249                         return( 1 );
250                 }
251
252                 if ( strcasecmp( argv[2], "*" ) != 0 ) {
253                         src = argv[2];
254                         if ( argc < 4 )
255                                 dst = "";
256                         else if ( strcasecmp( argv[3], "*" ) == 0 )
257                                 dst = src;
258                         else
259                                 dst = argv[3];
260                 } else {
261                         if ( argc < 4 ) {
262                                 map->drop_missing = 1;
263                                 return 0;
264                         }
265                         if ( strcasecmp( argv[3], "*" ) == 0 ) {
266                                 map->drop_missing = 0;
267                                 return 0;
268                         }
269
270                         src = argv[3];
271                         dst = src;
272                 }
273
274                 if ( ( map == &li->at_map )
275                         && ( strcasecmp( src, "objectclass" ) == 0
276                                 || strcasecmp( dst, "objectclass" ) == 0 ) )
277                 {
278                         fprintf( stderr,
279                                 "%s: line %d: objectclass attribute cannot be mapped\n",
280                                 fname, lineno );
281                 }
282
283                 mapping = (struct ldapmapping *)ch_calloc( 2,
284                         sizeof(struct ldapmapping) );
285                 if ( mapping == NULL ) {
286                         fprintf( stderr,
287                                 "%s: line %d: out of memory\n",
288                                 fname, lineno );
289                         return( 1 );
290                 }
291                 ber_str2bv( src, 0, 1, &mapping->src );
292                 ber_str2bv( dst, 0, 1, &mapping->dst );
293                 if ( *dst != 0 ) {
294                         mapping[1].src = mapping->dst;
295                         mapping[1].dst = mapping->src;
296                 } else {
297                         mapping[1].src = mapping->src;
298                         mapping[1].dst = mapping->dst;
299                 }
300
301                 if ( avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL ||
302                         avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
303                 {
304                         fprintf( stderr,
305                                 "%s: line %d: duplicate mapping found (ignored)\n",
306                                 fname, lineno );
307                         return 0;
308                 }
309
310                 avl_insert( &map->map, (caddr_t)mapping,
311                                         mapping_cmp, mapping_dup );
312                 avl_insert( &map->remap, (caddr_t)&mapping[1],
313                                         mapping_cmp, mapping_dup );
314
315         /* anything else */
316         } else {
317                 fprintf( stderr, "%s: line %d: unknown directive \"%s\" "
318                         "in ldap database definition (ignored)\n",
319                     fname, lineno, argv[0] );
320         }
321         return 0;
322 }
323
324 #ifdef ENABLE_REWRITE
325 static char *
326 suffix_massage_regexize( const char *s )
327 {
328         char *res, *ptr;
329         const char *p, *r;
330         int i;
331
332         for ( i = 0, p = s; 
333                         ( r = strchr( p, ',' ) ) != NULL; 
334                         p = r + 1, i++ )
335                 ;
336
337         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
338
339         ptr = slap_strcopy( res, "(.*)" );
340         for ( i = 0, p = s;
341                         ( r = strchr( p, ',' ) ) != NULL;
342                         p = r + 1 , i++ ) {
343                 ptr = slap_strncopy( ptr, p, r - p + 1 );
344                 ptr = slap_strcopy( ptr, "[ ]?" );
345
346                 if ( r[ 1 ] == ' ' ) {
347                         r++;
348                 }
349         }
350         slap_strcopy( ptr, p );
351
352         return res;
353 }
354
355 static char *
356 suffix_massage_patternize( const char *s )
357 {
358         ber_len_t       len;
359         char            *res;
360
361         len = strlen( s );
362
363         res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
364         if ( res == NULL ) {
365                 return NULL;
366         }
367
368         strcpy( res, "%1" );
369         strcpy( res + sizeof( "%1" ) - 1, s );
370
371         return res;
372 }
373
374 int
375 suffix_massage_config( 
376                 struct rewrite_info *info,
377                 struct berval *pvnc,
378                 struct berval *nvnc,
379                 struct berval *prnc,
380                 struct berval *nrnc
381 )
382 {
383         char *rargv[ 5 ];
384         int line = 0;
385
386         rargv[ 0 ] = "rewriteEngine";
387         rargv[ 1 ] = "on";
388         rargv[ 2 ] = NULL;
389         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
390
391         rargv[ 0 ] = "rewriteContext";
392         rargv[ 1 ] = "default";
393         rargv[ 2 ] = NULL;
394         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
395
396         rargv[ 0 ] = "rewriteRule";
397         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
398         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
399         rargv[ 3 ] = ":";
400         rargv[ 4 ] = NULL;
401         rewrite_parse( info, "<suffix massage>", ++line, 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>", ++line, 2, rargv );
409         
410         rargv[ 0 ] = "rewriteRule";
411         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
412         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
413         rargv[ 3 ] = ":";
414         rargv[ 4 ] = NULL;
415         rewrite_parse( info, "<suffix massage>", ++line, 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>", ++line, 2, rargv );
433
434 #if 1 /* rewrite filters */
435         {
436                 /*
437                  * Note: this is far more optimistic than desirable:
438                  * for any AVA value ending with the virtual naming
439                  * context the terminal part will be replaced by the
440                  * real naming context; a better solution would be to
441                  * walk the filter looking for DN-valued attributes,
442                  * and only rewrite those that require rewriting
443                  */
444                 char vbuf[LDAP_FILT_MAXSIZ], rbuf[LDAP_FILT_MAXSIZ];
445
446                 snprintf( vbuf, sizeof( vbuf ), "(.*)%s\\)(.*)", nvnc->bv_val );
447                 snprintf( rbuf, sizeof( rbuf ), "%%1%s)%%2", nrnc->bv_val );
448                 
449                 rargv[ 0 ] = "rewriteRule";
450                 rargv[ 1 ] = vbuf;
451                 rargv[ 2 ] = rbuf;
452                 rargv[ 3 ] = ":";
453                 rargv[ 4 ] = NULL;
454                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
455         }
456 #endif /* rewrite filters */
457
458 #if 0 /*  "matched" is not normalized */
459         rargv[ 0 ] = "rewriteContext";
460         rargv[ 1 ] = "matchedDn";
461         rargv[ 2 ] = "alias";
462         rargv[ 3 ] = "searchResult";
463         rargv[ 4 ] = NULL;
464         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
465 #else /* normalize "matched" */
466         rargv[ 0 ] = "rewriteContext";
467         rargv[ 1 ] = "matchedDn";
468         rargv[ 2 ] = NULL;
469         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
470
471         rargv[ 0 ] = "rewriteRule";
472         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
473         rargv[ 2 ] = suffix_massage_patternize( nvnc->bv_val );
474         rargv[ 3 ] = ":";
475         rargv[ 4 ] = NULL;
476         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
477         ch_free( rargv[ 1 ] );
478         ch_free( rargv[ 2 ] );
479 #endif /* normalize "matched" */
480
481         return 0;
482 }
483 #endif /* ENABLE_REWRITE */