]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmconf.c
0690670c14cb90c99560a95f362e26556a8a5577
[openldap] / servers / slapd / overlays / rwmconf.c
1 /* rwmconf.c - rewrite/map configuration file routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by the Howard Chu for inclusion
18  * in OpenLDAP Software and subsequently enhanced by Pierangelo
19  * Masarati.
20  */
21 /* This is an altered version */
22 /*
23  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
24  * 
25  * Permission is granted to anyone to use this software for any purpose
26  * on any computer system, and to alter it and redistribute it, subject
27  * to the following restrictions:
28  * 
29  * 1. The author is not responsible for the consequences of use of this
30  *    software, no matter how awful, even if they arise from flaws in it.
31  * 
32  * 2. The origin of this software must not be misrepresented, either by
33  *    explicit claim or by omission.  Since few users ever read sources,
34  *    credits should appear in the documentation.
35  * 
36  * 3. Altered versions must be plainly marked as such, and must not be
37  *    misrepresented as being the original software.  Since few users
38  *    ever read sources, credits should appear in the documentation.
39  * 
40  * 4. This notice may not be removed or altered.
41  *
42  *
43  *
44  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
45  * 
46  * This software is being modified by Pierangelo Masarati.
47  * The previously reported conditions apply to the modified code as well.
48  * Changes in the original code are highlighted where required.
49  * Credits for the original code go to the author, Howard Chu.
50  */
51
52 #include "portable.h"
53
54 #include <stdio.h>
55
56 #include <ac/string.h>
57 #include <ac/socket.h>
58
59 #include "slap.h"
60 #include "rwm.h"
61 #include "lutil.h"
62
63 int
64 rwm_map_config(
65                 struct ldapmap  *oc_map,
66                 struct ldapmap  *at_map,
67                 const char      *fname,
68                 int             lineno,
69                 int             argc,
70                 char            **argv )
71 {
72         struct ldapmap          *map;
73         struct ldapmapping      *mapping;
74         char                    *src, *dst;
75         int                     is_oc = 0;
76
77         if ( argc < 3 || argc > 4 ) {
78                 fprintf( stderr,
79         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
80                         fname, lineno );
81                 return 1;
82         }
83
84         if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
85                 map = oc_map;
86                 is_oc = 1;
87
88         } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
89                 map = at_map;
90
91         } else {
92                 fprintf( stderr, "%s: line %d: syntax is "
93                         "\"map {objectclass | attribute} [<local> | *] "
94                         "{<foreign> | *}\"\n",
95                         fname, lineno );
96                 return 1;
97         }
98
99         if ( strcmp( argv[2], "*" ) == 0 ) {
100                 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
101                         map->drop_missing = ( argc < 4 );
102                         return 0;
103                 }
104                 src = dst = argv[3];
105
106         } else if ( argc < 4 ) {
107                 src = "";
108                 dst = argv[2];
109
110         } else {
111                 src = argv[2];
112                 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
113         }
114
115         if ( ( map == at_map )
116                         && ( strcasecmp( src, "objectclass" ) == 0
117                         || strcasecmp( dst, "objectclass" ) == 0 ) )
118         {
119                 fprintf( stderr,
120                         "%s: line %d: objectclass attribute cannot be mapped\n",
121                         fname, lineno );
122         }
123
124         mapping = (struct ldapmapping *)ch_calloc( 2,
125                 sizeof(struct ldapmapping) );
126         if ( mapping == NULL ) {
127                 fprintf( stderr,
128                         "%s: line %d: out of memory\n",
129                         fname, lineno );
130                 return 1;
131         }
132         ber_str2bv( src, 0, 1, &mapping->src );
133         ber_str2bv( dst, 0, 1, &mapping->dst );
134         mapping[1].src = mapping->dst;
135         mapping[1].dst = mapping->src;
136
137         /*
138          * schema check
139          */
140         if ( is_oc ) {
141                 if ( src[0] != '\0' ) {
142                         if ( oc_bvfind( &mapping->src ) == NULL ) {
143                                 fprintf( stderr,
144         "%s: line %d: warning, source objectClass '%s' "
145         "should be defined in schema\n",
146                                         fname, lineno, src );
147
148                                 /*
149                                  * FIXME: this should become an err
150                                  */
151                         }
152                 }
153
154                 if ( oc_bvfind( &mapping->dst ) == NULL ) {
155                         fprintf( stderr,
156         "%s: line %d: warning, destination objectClass '%s' "
157         "is not defined in schema\n",
158                                 fname, lineno, dst );
159                 }
160         } else {
161                 int                     rc;
162                 const char              *text = NULL;
163                 AttributeDescription    *ad = NULL;
164
165                 if ( src[0] != '\0' ) {
166                         rc = slap_bv2ad( &mapping->src, &ad, &text );
167                         if ( rc != LDAP_SUCCESS ) {
168                                 fprintf( stderr,
169         "%s: line %d: warning, source attributeType '%s' "
170         "should be defined in schema\n",
171                                         fname, lineno, src );
172
173                                 /*
174                                  * FIXME: this should become an err
175                                  */
176                         }
177
178                         ad = NULL;
179                 }
180
181                 rc = slap_bv2ad( &mapping->dst, &ad, &text );
182                 if ( rc != LDAP_SUCCESS ) {
183                         fprintf( stderr,
184         "%s: line %d: warning, destination attributeType '%s' "
185         "is not defined in schema\n",
186                                 fname, lineno, dst );
187                 }
188         }
189
190         if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
191                         || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
192         {
193                 fprintf( stderr,
194                         "%s: line %d: duplicate mapping found (ignored)\n",
195                         fname, lineno );
196                 /* FIXME: free stuff */
197                 goto error_return;
198         }
199
200         if ( src[0] != '\0' ) {
201                 avl_insert( &map->map, (caddr_t)mapping,
202                                         mapping_cmp, mapping_dup );
203         }
204         avl_insert( &map->remap, (caddr_t)&mapping[1],
205                                 mapping_cmp, mapping_dup );
206
207         return 0;
208
209 error_return:;
210         if ( mapping ) {
211                 ch_free( mapping->src.bv_val );
212                 ch_free( mapping->dst.bv_val );
213                 ch_free( mapping );
214         }
215
216         return 1;
217 }
218
219 #ifdef ENABLE_REWRITE
220 static char *
221 suffix_massage_regexize( const char *s )
222 {
223         char *res, *ptr;
224         const char *p, *r;
225         int i;
226
227         for ( i = 0, p = s; 
228                         ( r = strchr( p, ',' ) ) != NULL; 
229                         p = r + 1, i++ )
230                 ;
231
232         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
233
234         ptr = lutil_strcopy( res, "(.*)" );
235         for ( i = 0, p = s;
236                         ( r = strchr( p, ',' ) ) != NULL;
237                         p = r + 1 , i++ ) {
238                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
239                 ptr = lutil_strcopy( ptr, "[ ]?" );
240
241                 if ( r[ 1 ] == ' ' ) {
242                         r++;
243                 }
244         }
245         lutil_strcopy( ptr, p );
246
247         return res;
248 }
249
250 static char *
251 suffix_massage_patternize( const char *s )
252 {
253         ber_len_t       len;
254         char            *res;
255
256         len = strlen( s );
257
258         res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
259         if ( res == NULL ) {
260                 return NULL;
261         }
262
263         strcpy( res, "%1" );
264         strcpy( res + sizeof( "%1" ) - 1, s );
265
266         return res;
267 }
268
269 int
270 suffix_massage_config( 
271                 struct rewrite_info *info,
272                 struct berval *pvnc,
273                 struct berval *nvnc,
274                 struct berval *prnc,
275                 struct berval *nrnc
276 )
277 {
278         char *rargv[ 5 ];
279         int line = 0;
280
281         rargv[ 0 ] = "rewriteEngine";
282         rargv[ 1 ] = "on";
283         rargv[ 2 ] = NULL;
284         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
285
286         rargv[ 0 ] = "rewriteContext";
287         rargv[ 1 ] = "default";
288         rargv[ 2 ] = NULL;
289         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
290
291         rargv[ 0 ] = "rewriteRule";
292         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
293         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
294         rargv[ 3 ] = ":";
295         rargv[ 4 ] = NULL;
296         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
297         ch_free( rargv[ 1 ] );
298         ch_free( rargv[ 2 ] );
299         
300         rargv[ 0 ] = "rewriteContext";
301         rargv[ 1 ] = "searchResultDN";
302         rargv[ 2 ] = NULL;
303         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
304         
305         rargv[ 0 ] = "rewriteRule";
306         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
307         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
308         rargv[ 3 ] = ":";
309         rargv[ 4 ] = NULL;
310         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
311         ch_free( rargv[ 1 ] );
312         ch_free( rargv[ 2 ] );
313
314         rargv[ 0 ] = "rewriteContext";
315         rargv[ 1 ] = "matchedDN";
316         rargv[ 2 ] = "alias";
317         rargv[ 3 ] = "searchResultDN";
318         rargv[ 4 ] = NULL;
319         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
320
321         rargv[ 0 ] = "rewriteContext";
322         rargv[ 1 ] = "searchAttrDN";
323         rargv[ 2 ] = "alias";
324         rargv[ 3 ] = "searchResultDN";
325         rargv[ 4 ] = NULL;
326         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
327
328         return 0;
329 }
330 #endif /* ENABLE_REWRITE */