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