]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/config.c
13e49922d3dda8685e3b13895ad8031cb5b4e628
[openldap] / servers / slapd / back-ldap / config.c
1 /* config.c - ldap backend configuration file routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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 #include "lutil.h"
48
49 int
50 ldap_back_db_config(
51     BackendDB   *be,
52     const char  *fname,
53     int         lineno,
54     int         argc,
55     char        **argv
56 )
57 {
58         struct ldapinfo *li = (struct ldapinfo *) be->be_private;
59
60         if ( li == NULL ) {
61                 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
62                     fname, lineno );
63                 return( 1 );
64         }
65
66         /* server address to query (depricated, use "uri" directive) */
67         if ( strcasecmp( argv[0], "server" ) == 0 ) {
68                 if (argc != 2) {
69                         fprintf( stderr,
70         "%s: line %d: missing address in \"server <address>\" line\n",
71                             fname, lineno );
72                         return( 1 );
73                 }
74                 if (li->url != NULL)
75                         ch_free(li->url);
76                 li->url = ch_calloc(strlen(argv[1]) + 9, sizeof(char));
77                 if (li->url != NULL) {
78                         strcpy(li->url, "ldap://");
79                         strcat(li->url, argv[1]);
80                         strcat(li->url, "/");
81                 }
82
83         /* URI of server to query (preferred over "server" directive) */
84         } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
85                 if (argc != 2) {
86                         fprintf( stderr,
87         "%s: line %d: missing address in \"uri <address>\" line\n",
88                             fname, lineno );
89                         return( 1 );
90                 }
91                 if (li->url != NULL)
92                         ch_free(li->url);
93                 li->url = ch_strdup(argv[1]);
94
95         /* name to use for ldap_back_group */
96         } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
97                 if (argc != 2) {
98                         fprintf( stderr,
99         "%s: line %d: missing name in \"binddn <name>\" line\n",
100                             fname, lineno );
101                         return( 1 );
102                 }
103                 li->binddn = ch_strdup(argv[1]);
104
105         /* password to use for ldap_back_group */
106         } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
107                 if (argc != 2) {
108                         fprintf( stderr,
109         "%s: line %d: missing password in \"bindpw <password>\" line\n",
110                             fname, lineno );
111                         return( 1 );
112                 }
113                 li->bindpw = ch_strdup(argv[1]);
114         
115         /* save bind creds for referral rebinds? */
116         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
117                 if (argc != 1) {
118                         fprintf( stderr,
119         "%s: line %d: rebind-as-user takes no arguments\n",
120                             fname, lineno );
121                         return( 1 );
122                 }
123                 li->savecred = 1;
124         
125         /* dn massaging */
126         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
127                 BackendDB *tmp_be;
128                 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
129 #ifdef ENABLE_REWRITE
130                 int rc;
131 #endif /* ENABLE_REWRITE */
132                 
133                 /*
134                  * syntax:
135                  * 
136                  *      suffixmassage <suffix> <massaged suffix>
137                  *
138                  * the <suffix> field must be defined as a valid suffix
139                  * (or suffixAlias?) for the current database;
140                  * the <massaged suffix> shouldn't have already been
141                  * defined as a valid suffix or suffixAlias for the 
142                  * current server
143                  */
144                 if ( argc != 3 ) {
145                         fprintf( stderr, "%s: line %d: syntax is"
146                                        " \"suffixMassage <suffix>"
147                                        " <massaged suffix>\"\n",
148                                 fname, lineno );
149                         return( 1 );
150                 }
151                 
152                 ber_str2bv( argv[1], 0, 0, &bvnc );
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                         free( nvnc.bv_val );
166                         free( pvnc.bv_val );
167                         return( 1 );
168                 }
169
170                 ber_str2bv( argv[2], 0, 0, &brnc );
171                 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc ) != LDAP_SUCCESS ) {
172                         fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
173                                 fname, lineno, brnc.bv_val );
174                         free( nvnc.bv_val );
175                         free( pvnc.bv_val );
176                         return( 1 );
177                 }
178
179 #if 0
180                 tmp_be = select_backend( &nrnc, 0, 0 );
181                 if ( tmp_be != NULL ) {
182                         fprintf( stderr, "%s: line %d: massaged suffix"
183                                        " already in use by another backend in" 
184                                        " \"suffixMassage <suffix>"
185                                        " <massaged suffix>\"\n",
186                                 fname, lineno );
187                         free( nvnc.bv_val );
188                         free( pvnc.bv_val );
189                         free( nrnc.bv_val );
190                         free( prnc.bv_val );
191                         return( 1 );
192                 }
193 #endif
194
195 #ifdef ENABLE_REWRITE
196                 /*
197                  * The suffix massaging is emulated by means of the
198                  * rewrite capabilities
199                  * FIXME: no extra rewrite capabilities should be added
200                  * to the database
201                  */
202                 rc = suffix_massage_config( li->rwinfo, &pvnc, &nvnc, &prnc, &nrnc );
203                 free( nvnc.bv_val );
204                 free( pvnc.bv_val );
205                 free( nrnc.bv_val );
206                 free( prnc.bv_val );
207
208                 return( rc );
209
210 #else /* !ENABLE_REWRITE */
211                 ber_bvarray_add( &li->suffix_massage, &pvnc );
212                 ber_bvarray_add( &li->suffix_massage, &nvnc );
213                 
214                 ber_bvarray_add( &li->suffix_massage, &prnc );
215                 ber_bvarray_add( &li->suffix_massage, &nrnc );
216 #endif /* !ENABLE_REWRITE */
217
218         /* rewrite stuff ... */
219         } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
220 #ifdef ENABLE_REWRITE
221                 return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
222
223 #else /* !ENABLE_REWRITE */
224                 fprintf( stderr, "%s: line %d: rewrite capabilities "
225                                 "are not enabled\n", fname, lineno );
226 #endif /* !ENABLE_REWRITE */
227                 
228         /* objectclass/attribute mapping */
229         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
230                 struct ldapmap *map;
231                 struct ldapmapping *mapping;
232                 char *src, *dst;
233
234                 if ( argc < 3 || argc > 4 ) {
235                         fprintf( stderr,
236         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
237                                 fname, lineno );
238                         return( 1 );
239                 }
240
241                 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
242                         map = &li->oc_map;
243                 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
244                         map = &li->at_map;
245                 } else {
246                         fprintf( stderr, "%s: line %d: syntax is "
247                                 "\"map {objectclass | attribute} [<local> | *] "
248                                 "{<foreign> | *}\"\n",
249                                 fname, lineno );
250                         return( 1 );
251                 }
252
253                 if ( strcmp( argv[2], "*" ) == 0 ) {
254                         if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
255                                 map->drop_missing = ( argc < 4 );
256                                 return 0;
257                         }
258                         src = dst = argv[3];
259                 } else if ( argc < 4 ) {
260                         src = "";
261                         dst = argv[2];
262                 } else {
263                         src = argv[2];
264                         dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
265                 }
266
267                 if ( ( map == &li->at_map )
268                         && ( strcasecmp( src, "objectclass" ) == 0
269                                 || strcasecmp( dst, "objectclass" ) == 0 ) )
270                 {
271                         fprintf( stderr,
272                                 "%s: line %d: objectclass attribute cannot be mapped\n",
273                                 fname, lineno );
274                 }
275
276                 mapping = (struct ldapmapping *)ch_calloc( 2,
277                         sizeof(struct ldapmapping) );
278                 if ( mapping == NULL ) {
279                         fprintf( stderr,
280                                 "%s: line %d: out of memory\n",
281                                 fname, lineno );
282                         return( 1 );
283                 }
284                 ber_str2bv( src, 0, 1, &mapping->src );
285                 ber_str2bv( dst, 0, 1, &mapping->dst );
286                 mapping[1].src = mapping->dst;
287                 mapping[1].dst = mapping->src;
288
289                 if ( (*src != '\0' &&
290                           avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL) ||
291                         avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
292                 {
293                         fprintf( stderr,
294                                 "%s: line %d: duplicate mapping found (ignored)\n",
295                                 fname, lineno );
296                         return 0;
297                 }
298
299                 if ( *src != '\0' )
300                         avl_insert( &map->map, (caddr_t)mapping,
301                                                 mapping_cmp, mapping_dup );
302                 avl_insert( &map->remap, (caddr_t)&mapping[1],
303                                         mapping_cmp, mapping_dup );
304
305         /* anything else */
306         } else {
307                 fprintf( stderr, "%s: line %d: unknown directive \"%s\" "
308                         "in ldap database definition (ignored)\n",
309                     fname, lineno, argv[0] );
310         }
311         return 0;
312 }
313
314 #ifdef ENABLE_REWRITE
315 static char *
316 suffix_massage_regexize( const char *s )
317 {
318         char *res, *ptr;
319         const char *p, *r;
320         int i;
321
322         for ( i = 0, p = s; 
323                         ( r = strchr( p, ',' ) ) != NULL; 
324                         p = r + 1, i++ )
325                 ;
326
327         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
328
329         ptr = lutil_strcopy( res, "(.*)" );
330         for ( i = 0, p = s;
331                         ( r = strchr( p, ',' ) ) != NULL;
332                         p = r + 1 , i++ ) {
333                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
334                 ptr = lutil_strcopy( ptr, "[ ]?" );
335
336                 if ( r[ 1 ] == ' ' ) {
337                         r++;
338                 }
339         }
340         lutil_strcopy( ptr, p );
341
342         return res;
343 }
344
345 static char *
346 suffix_massage_patternize( const char *s )
347 {
348         ber_len_t       len;
349         char            *res;
350
351         len = strlen( s );
352
353         res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
354         if ( res == NULL ) {
355                 return NULL;
356         }
357
358         strcpy( res, "%1" );
359         strcpy( res + sizeof( "%1" ) - 1, s );
360
361         return res;
362 }
363
364 int
365 suffix_massage_config( 
366                 struct rewrite_info *info,
367                 struct berval *pvnc,
368                 struct berval *nvnc,
369                 struct berval *prnc,
370                 struct berval *nrnc
371 )
372 {
373         char *rargv[ 5 ];
374         int line = 0;
375
376         rargv[ 0 ] = "rewriteEngine";
377         rargv[ 1 ] = "on";
378         rargv[ 2 ] = NULL;
379         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
380
381         rargv[ 0 ] = "rewriteContext";
382         rargv[ 1 ] = "default";
383         rargv[ 2 ] = NULL;
384         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
385
386         rargv[ 0 ] = "rewriteRule";
387         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
388         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
389         rargv[ 3 ] = ":";
390         rargv[ 4 ] = NULL;
391         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
392         ch_free( rargv[ 1 ] );
393         ch_free( rargv[ 2 ] );
394         
395         rargv[ 0 ] = "rewriteContext";
396         rargv[ 1 ] = "searchResult";
397         rargv[ 2 ] = NULL;
398         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
399         
400         rargv[ 0 ] = "rewriteRule";
401         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
402         rargv[ 2 ] = suffix_massage_patternize( pvnc->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         /*
410          * the filter should be rewritten as
411          * 
412          * rewriteRule
413          *      "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
414          *      "%1member=%2,dc=example,dc=com%3"
415          *
416          * where "o=Foo Bar, c=US" is the virtual naming context,
417          * and "dc=example, dc=com" is the real naming context
418          */
419         rargv[ 0 ] = "rewriteContext";
420         rargv[ 1 ] = "searchFilter";
421         rargv[ 2 ] = NULL;
422         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
423
424 #if 1 /* rewrite filters */
425         {
426                 /*
427                  * Note: this is far more optimistic than desirable:
428                  * for any AVA value ending with the virtual naming
429                  * context the terminal part will be replaced by the
430                  * real naming context; a better solution would be to
431                  * walk the filter looking for DN-valued attributes,
432                  * and only rewrite those that require rewriting
433                  */
434                 char    vbuf_[BUFSIZ], *vbuf = vbuf_,
435                         rbuf_[BUFSIZ], *rbuf = rbuf_;
436                 int     len;
437
438                 len = snprintf( vbuf, sizeof( vbuf_ ), 
439                                 "(.*)%s\\)(.*)", nvnc->bv_val );
440                 if ( len == -1 ) {
441                         /* 
442                          * traditional behavior: snprintf returns -1 
443                          * if buffer is insufficient
444                          */
445                         return -1;
446
447                 } else if ( len >= (int)sizeof( vbuf_ ) ) {
448                         /* 
449                          * C99: snprintf returns the required size 
450                          */
451                         vbuf = ch_malloc( len + 1 );
452                         len = snprintf( vbuf, len,
453                                         "(.*)%s\\)(.*)", nvnc->bv_val );
454                         assert( len > 0 );
455                 }
456
457                 len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2", 
458                                 nrnc->bv_val );
459                 if ( len == -1 ) {
460                         return -1;
461
462                 } else if ( len >= (int)sizeof( rbuf_ ) ) {
463                         rbuf = ch_malloc( len + 1 );
464                         len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2", 
465                                         nrnc->bv_val );
466                         assert( len > 0 );
467                 }
468                 
469                 rargv[ 0 ] = "rewriteRule";
470                 rargv[ 1 ] = vbuf;
471                 rargv[ 2 ] = rbuf;
472                 rargv[ 3 ] = ":";
473                 rargv[ 4 ] = NULL;
474                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
475
476                 if ( vbuf != vbuf_ ) {
477                         ch_free( vbuf );
478                 }
479
480                 if ( rbuf != rbuf_ ) {
481                         ch_free( rbuf );
482                 }
483         }
484 #endif /* rewrite filters */
485
486 #if 0 /*  "matched" is not normalized */
487         rargv[ 0 ] = "rewriteContext";
488         rargv[ 1 ] = "matchedDn";
489         rargv[ 2 ] = "alias";
490         rargv[ 3 ] = "searchResult";
491         rargv[ 4 ] = NULL;
492         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
493 #else /* normalize "matched" */
494         rargv[ 0 ] = "rewriteContext";
495         rargv[ 1 ] = "matchedDn";
496         rargv[ 2 ] = NULL;
497         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
498
499         rargv[ 0 ] = "rewriteRule";
500         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
501         rargv[ 2 ] = suffix_massage_patternize( nvnc->bv_val );
502         rargv[ 3 ] = ":";
503         rargv[ 4 ] = NULL;
504         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
505         ch_free( rargv[ 1 ] );
506         ch_free( rargv[ 2 ] );
507 #endif /* normalize "matched" */
508
509         return 0;
510 }
511 #endif /* ENABLE_REWRITE */