]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmconf.c
cleanup
[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 1999-2004 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #ifdef SLAPD_OVER_RWM
27
28 #include <stdio.h>
29
30 #include <ac/string.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34 #include "rwm.h"
35 #include "lutil.h"
36
37 int
38 rwm_map_config(
39                 struct ldapmap  *oc_map,
40                 struct ldapmap  *at_map,
41                 const char      *fname,
42                 int             lineno,
43                 int             argc,
44                 char            **argv )
45 {
46         struct ldapmap          *map;
47         struct ldapmapping      *mapping;
48         char                    *src, *dst;
49         int                     is_oc = 0;
50
51         if ( argc < 3 || argc > 4 ) {
52                 fprintf( stderr,
53         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
54                         fname, lineno );
55                 return 1;
56         }
57
58         if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
59                 map = oc_map;
60                 is_oc = 1;
61
62         } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
63                 map = at_map;
64
65         } else {
66                 fprintf( stderr, "%s: line %d: syntax is "
67                         "\"map {objectclass | attribute} [<local> | *] "
68                         "{<foreign> | *}\"\n",
69                         fname, lineno );
70                 return 1;
71         }
72
73         if ( strcmp( argv[2], "*" ) == 0 ) {
74                 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
75                         map->drop_missing = ( argc < 4 );
76                         return 0;
77                 }
78                 src = dst = argv[3];
79
80         } else if ( argc < 4 ) {
81                 src = "";
82                 dst = argv[2];
83
84         } else {
85                 src = argv[2];
86                 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
87         }
88
89         if ( ( map == at_map )
90                         && ( strcasecmp( src, "objectclass" ) == 0
91                         || strcasecmp( dst, "objectclass" ) == 0 ) )
92         {
93                 fprintf( stderr,
94                         "%s: line %d: objectclass attribute cannot be mapped\n",
95                         fname, lineno );
96                 return 1;
97         }
98
99         mapping = (struct ldapmapping *)ch_calloc( 2,
100                 sizeof(struct ldapmapping) );
101         if ( mapping == NULL ) {
102                 fprintf( stderr,
103                         "%s: line %d: out of memory\n",
104                         fname, lineno );
105                 return 1;
106         }
107         ber_str2bv( src, 0, 1, &mapping[0].m_src );
108         ber_str2bv( dst, 0, 1, &mapping[0].m_dst );
109         mapping[1].m_src = mapping[0].m_dst;
110         mapping[1].m_dst = mapping[0].m_src;
111
112         mapping[0].m_flags = RWMMAP_F_NONE;
113         mapping[1].m_flags = RWMMAP_F_NONE;
114
115         /*
116          * schema check
117          */
118         if ( is_oc ) {
119                 if ( src[0] != '\0' ) {
120                         mapping[0].m_src_oc = oc_bvfind( &mapping[0].m_src );
121                         if ( mapping[0].m_src_oc == NULL ) {
122                                 fprintf( stderr,
123         "%s: line %d: warning, source objectClass '%s' "
124         "should be defined in schema\n",
125                                         fname, lineno, src );
126
127                                 /*
128                                  * FIXME: this should become an err
129                                  */
130                                 mapping[0].m_src_oc = ch_malloc( sizeof( ObjectClass ) );
131                                 memset( mapping[0].m_src_oc, 0, sizeof( ObjectClass ) );
132                                 mapping[0].m_src_oc->soc_cname = mapping[0].m_src;
133                                 mapping[0].m_flags |= RWMMAP_F_FREE_SRC;
134                         }
135                         mapping[1].m_dst_oc = mapping[0].m_src_oc;
136                 }
137
138                 mapping[0].m_dst_oc = oc_bvfind( &mapping[0].m_dst );
139                 if ( mapping[0].m_dst_oc == NULL ) {
140                         fprintf( stderr,
141         "%s: line %d: warning, destination objectClass '%s' "
142         "is not defined in schema\n",
143                                 fname, lineno, dst );
144
145                         mapping[0].m_dst_oc = ch_malloc( sizeof( ObjectClass ) );
146                         memset( mapping[0].m_dst_oc, 0, sizeof( ObjectClass ) );
147                         mapping[0].m_dst_oc->soc_cname = mapping[0].m_dst;
148                         mapping[0].m_flags |= RWMMAP_F_FREE_DST;
149                 }
150                 mapping[1].m_src_oc = mapping[0].m_dst_oc;
151
152                 mapping[0].m_flags |= RWMMAP_F_IS_OC;
153                 mapping[1].m_flags |= RWMMAP_F_IS_OC;
154
155         } else {
156                 int                     rc;
157                 const char              *text = NULL;
158
159                 if ( src[0] != '\0' ) {
160                         rc = slap_bv2ad( &mapping[0].m_src,
161                                         &mapping[0].m_src_ad, &text );
162                         if ( rc != LDAP_SUCCESS ) {
163                                 fprintf( stderr,
164         "%s: line %d: warning, source attributeType '%s' "
165         "should be defined in schema\n",
166                                         fname, lineno, src );
167
168                                 /*
169                                  * FIXME: this should become an err
170                                  *
171                                  * FIXME: or, we should create a fake ad
172                                  * and add it here.
173                                  */
174
175                                 rc = slap_bv2undef_ad( &mapping[0].m_src,
176                                                 &mapping[0].m_src_ad, &text );
177                                 if ( rc != LDAP_SUCCESS ) {
178                                         fprintf( stderr,
179         "%s: line %d: source attributeType '%s': %d (%s)\n",
180                                                 fname, lineno, src, rc, text ? text : "null" );
181                                         return 1;
182                                 }
183
184                         }
185                         mapping[1].m_dst_ad = mapping[0].m_src_ad;
186                 }
187
188                 rc = slap_bv2ad( &mapping[0].m_dst, &mapping[0].m_dst_ad, &text );
189                 if ( rc != LDAP_SUCCESS ) {
190                         fprintf( stderr,
191         "%s: line %d: warning, destination attributeType '%s' "
192         "is not defined in schema\n",
193                                 fname, lineno, dst );
194
195                         rc = slap_bv2undef_ad( &mapping[0].m_dst,
196                                         &mapping[0].m_dst_ad, &text );
197                         if ( rc != LDAP_SUCCESS ) {
198                                 fprintf( stderr,
199         "%s: line %d: destination attributeType '%s': %d (%s)\n",
200                                         fname, lineno, src, rc, text ? text : "null" );
201                                 return 1;
202                         }
203                 }
204                 mapping[1].m_src_ad = mapping[0].m_dst_ad;
205         }
206
207         if ( ( src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, rwm_mapping_cmp ) != NULL)
208                         || avl_find( map->remap, (caddr_t)&mapping[1], rwm_mapping_cmp ) != NULL)
209         {
210                 fprintf( stderr,
211                         "%s: line %d: duplicate mapping found (ignored)\n",
212                         fname, lineno );
213                 /* FIXME: free stuff */
214                 goto error_return;
215         }
216
217         if ( src[0] != '\0' ) {
218                 avl_insert( &map->map, (caddr_t)&mapping[0],
219                                         rwm_mapping_cmp, rwm_mapping_dup );
220         }
221         avl_insert( &map->remap, (caddr_t)&mapping[1],
222                                 rwm_mapping_cmp, rwm_mapping_dup );
223
224         return 0;
225
226 error_return:;
227         if ( mapping ) {
228                 rwm_mapping_free( mapping );
229         }
230
231         return 1;
232 }
233
234 #ifdef ENABLE_REWRITE
235 static char *
236 rwm_suffix_massage_regexize( const char *s )
237 {
238         char *res, *ptr;
239         const char *p, *r;
240         int i;
241
242         for ( i = 0, p = s; 
243                         ( r = strchr( p, ',' ) ) != NULL; 
244                         p = r + 1, i++ )
245                 ;
246
247         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
248
249         ptr = lutil_strcopy( res, "(.*)" );
250         for ( i = 0, p = s;
251                         ( r = strchr( p, ',' ) ) != NULL;
252                         p = r + 1 , i++ ) {
253                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
254                 ptr = lutil_strcopy( ptr, "[ ]?" );
255
256                 if ( r[ 1 ] == ' ' ) {
257                         r++;
258                 }
259         }
260         lutil_strcopy( ptr, p );
261
262         return res;
263 }
264
265 static char *
266 rwm_suffix_massage_patternize( const char *s )
267 {
268         ber_len_t       len;
269         char            *res;
270
271         len = strlen( s );
272
273         res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
274         if ( res == NULL ) {
275                 return NULL;
276         }
277
278         strcpy( res, "%1" );
279         strcpy( res + STRLENOF( "%1" ), s );
280
281         return res;
282 }
283
284 int
285 rwm_suffix_massage_config( 
286                 struct rewrite_info *info,
287                 struct berval *pvnc,
288                 struct berval *nvnc,
289                 struct berval *prnc,
290                 struct berval *nrnc
291 )
292 {
293         char *rargv[ 5 ];
294         int line = 0;
295
296         rargv[ 0 ] = "rewriteEngine";
297         rargv[ 1 ] = "on";
298         rargv[ 2 ] = NULL;
299         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
300
301         rargv[ 0 ] = "rewriteContext";
302         rargv[ 1 ] = "default";
303         rargv[ 2 ] = NULL;
304         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
305
306         rargv[ 0 ] = "rewriteRule";
307         rargv[ 1 ] = rwm_suffix_massage_regexize( pvnc->bv_val );
308         rargv[ 2 ] = rwm_suffix_massage_patternize( prnc->bv_val );
309         rargv[ 3 ] = ":";
310         rargv[ 4 ] = NULL;
311         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
312         ch_free( rargv[ 1 ] );
313         ch_free( rargv[ 2 ] );
314         
315         rargv[ 0 ] = "rewriteContext";
316         rargv[ 1 ] = "searchResult";
317         rargv[ 2 ] = NULL;
318         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
319         
320         rargv[ 0 ] = "rewriteRule";
321         rargv[ 1 ] = rwm_suffix_massage_regexize( prnc->bv_val );
322         rargv[ 2 ] = rwm_suffix_massage_patternize( pvnc->bv_val );
323         rargv[ 3 ] = ":";
324         rargv[ 4 ] = NULL;
325         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
326         ch_free( rargv[ 1 ] );
327         ch_free( rargv[ 2 ] );
328
329         rargv[ 0 ] = "rewriteContext";
330         rargv[ 1 ] = "matchedDN";
331         rargv[ 2 ] = "alias";
332         rargv[ 3 ] = "searchResult";
333         rargv[ 4 ] = NULL;
334         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
335
336         rargv[ 0 ] = "rewriteContext";
337         rargv[ 1 ] = "searchAttrDN";
338         rargv[ 2 ] = "alias";
339         rargv[ 3 ] = "searchResult";
340         rargv[ 4 ] = NULL;
341         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
342
343         return 0;
344 }
345 #endif /* ENABLE_REWRITE */
346
347 #endif /* SLAPD_OVER_RWM */