]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmconf.c
32212d6c6dda5af33f082e3aba9705efd98f4883
[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-2005 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 = oc_bvfind_undef( &mapping[0].m_dst );
146                         if ( mapping[0].m_dst_oc == NULL ) {
147                                 fprintf( stderr, "%s: line %d: unable to mimic destination objectClass '%s'\n",
148                                         fname, lineno, dst );
149                                 return 1;
150                         }
151
152 #if 0
153                         mapping[0].m_dst_oc = ch_malloc( sizeof( ObjectClass ) );
154                         memset( mapping[0].m_dst_oc, 0, sizeof( ObjectClass ) );
155                         mapping[0].m_dst_oc->soc_cname = mapping[0].m_dst;
156                         mapping[0].m_flags |= RWMMAP_F_FREE_DST;
157 #endif
158                 }
159                 mapping[1].m_src_oc = mapping[0].m_dst_oc;
160
161                 mapping[0].m_flags |= RWMMAP_F_IS_OC;
162                 mapping[1].m_flags |= RWMMAP_F_IS_OC;
163
164         } else {
165                 int                     rc;
166                 const char              *text = NULL;
167
168                 if ( src[0] != '\0' ) {
169                         rc = slap_bv2ad( &mapping[0].m_src,
170                                         &mapping[0].m_src_ad, &text );
171                         if ( rc != LDAP_SUCCESS ) {
172                                 fprintf( stderr,
173         "%s: line %d: warning, source attributeType '%s' "
174         "should be defined in schema\n",
175                                         fname, lineno, src );
176
177                                 /*
178                                  * FIXME: this should become an err
179                                  *
180                                  * FIXME: or, we should create a fake ad
181                                  * and add it here.
182                                  */
183
184                                 rc = slap_bv2undef_ad( &mapping[0].m_src,
185                                                 &mapping[0].m_src_ad, &text );
186                                 if ( rc != LDAP_SUCCESS ) {
187                                         fprintf( stderr,
188         "%s: line %d: source attributeType '%s': %d (%s)\n",
189                                                 fname, lineno, src, rc, text ? text : "null" );
190                                         return 1;
191                                 }
192
193                         }
194                         mapping[1].m_dst_ad = mapping[0].m_src_ad;
195                 }
196
197                 rc = slap_bv2ad( &mapping[0].m_dst, &mapping[0].m_dst_ad, &text );
198                 if ( rc != LDAP_SUCCESS ) {
199                         fprintf( stderr,
200         "%s: line %d: warning, destination attributeType '%s' "
201         "is not defined in schema\n",
202                                 fname, lineno, dst );
203
204                         rc = slap_bv2undef_ad( &mapping[0].m_dst,
205                                         &mapping[0].m_dst_ad, &text );
206                         if ( rc != LDAP_SUCCESS ) {
207                                 fprintf( stderr,
208         "%s: line %d: destination attributeType '%s': %d (%s)\n",
209                                         fname, lineno, src, rc, text ? text : "null" );
210                                 return 1;
211                         }
212                 }
213                 mapping[1].m_src_ad = mapping[0].m_dst_ad;
214         }
215
216         if ( ( src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, rwm_mapping_cmp ) != NULL)
217                         || avl_find( map->remap, (caddr_t)&mapping[1], rwm_mapping_cmp ) != NULL)
218         {
219                 fprintf( stderr,
220                         "%s: line %d: duplicate mapping found" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
221                         fname, lineno );
222                 /* FIXME: free stuff */
223                 goto error_return;
224         }
225
226         if ( src[0] != '\0' ) {
227                 avl_insert( &map->map, (caddr_t)&mapping[0],
228                                         rwm_mapping_cmp, rwm_mapping_dup );
229         }
230         avl_insert( &map->remap, (caddr_t)&mapping[1],
231                                 rwm_mapping_cmp, rwm_mapping_dup );
232
233         return 0;
234
235 error_return:;
236         if ( mapping ) {
237                 rwm_mapping_free( mapping );
238         }
239
240         return 1;
241 }
242
243 #ifdef ENABLE_REWRITE
244 static char *
245 rwm_suffix_massage_regexize( const char *s )
246 {
247         char *res, *ptr;
248         const char *p, *r;
249         int i;
250
251         for ( i = 0, p = s; 
252                         ( r = strchr( p, ',' ) ) != NULL; 
253                         p = r + 1, i++ )
254                 ;
255
256         res = ch_calloc( sizeof( char ), strlen( s )
257                         + STRLENOF( "((.+),)?" )
258                         + STRLENOF( "[ ]?" ) * i
259                         + STRLENOF( "$" ) + 1 );
260
261         ptr = lutil_strcopy( res, "((.+),)?" );
262         for ( i = 0, p = s;
263                         ( r = strchr( p, ',' ) ) != NULL;
264                         p = r + 1 , i++ ) {
265                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
266                 ptr = lutil_strcopy( ptr, "[ ]?" );
267
268                 if ( r[ 1 ] == ' ' ) {
269                         r++;
270                 }
271         }
272         ptr = lutil_strcopy( ptr, p );
273         ptr[0] = '$';
274         ptr[1] = '\0';
275
276         return res;
277 }
278
279 static char *
280 rwm_suffix_massage_patternize( const char *s )
281 {
282         ber_len_t       len;
283         char            *res;
284
285         len = strlen( s );
286
287         res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
288         if ( res == NULL ) {
289                 return NULL;
290         }
291
292         strcpy( res, "%1" );
293         strcpy( res + STRLENOF( "%1" ), s );
294
295         return res;
296 }
297
298 int
299 rwm_suffix_massage_config( 
300                 struct rewrite_info *info,
301                 struct berval *pvnc,
302                 struct berval *nvnc,
303                 struct berval *prnc,
304                 struct berval *nrnc
305 )
306 {
307         char *rargv[ 5 ];
308         int line = 0;
309
310         rargv[ 0 ] = "rewriteEngine";
311         rargv[ 1 ] = "on";
312         rargv[ 2 ] = NULL;
313         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
314
315         rargv[ 0 ] = "rewriteContext";
316         rargv[ 1 ] = "default";
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( pvnc->bv_val );
322         rargv[ 2 ] = rwm_suffix_massage_patternize( prnc->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 ] = "searchEntryDN";
331         rargv[ 2 ] = NULL;
332         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
333         
334         rargv[ 0 ] = "rewriteRule";
335         rargv[ 1 ] = rwm_suffix_massage_regexize( prnc->bv_val );
336         rargv[ 2 ] = rwm_suffix_massage_patternize( pvnc->bv_val );
337         rargv[ 3 ] = ":";
338         rargv[ 4 ] = NULL;
339         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
340         ch_free( rargv[ 1 ] );
341         ch_free( rargv[ 2 ] );
342
343         rargv[ 0 ] = "rewriteContext";
344         rargv[ 1 ] = "matchedDN";
345         rargv[ 2 ] = "alias";
346         rargv[ 3 ] = "searchEntryDN";
347         rargv[ 4 ] = NULL;
348         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
349
350 #ifdef RWM_REFERRAL_REWRITE
351         /* FIXME: we don't want this on by default, do we? */
352         rargv[ 0 ] = "rewriteContext";
353         rargv[ 1 ] = "referralDN";
354         rargv[ 2 ] = "alias";
355         rargv[ 3 ] = "searchEntryDN";
356         rargv[ 4 ] = NULL;
357         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
358 #else /* ! RWM_REFERRAL_REWRITE */
359         rargv[ 0 ] = "rewriteContext";
360         rargv[ 1 ] = "referralAttrDN";
361         rargv[ 2 ] = NULL;
362         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
363
364         rargv[ 0 ] = "rewriteContext";
365         rargv[ 1 ] = "referralDN";
366         rargv[ 2 ] = NULL;
367         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
368 #endif /* ! RWM_REFERRAL_REWRITE */
369
370         rargv[ 0 ] = "rewriteContext";
371         rargv[ 1 ] = "searchAttrDN";
372         rargv[ 2 ] = "alias";
373         rargv[ 3 ] = "searchEntryDN";
374         rargv[ 4 ] = NULL;
375         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
376
377         return 0;
378 }
379 #endif /* ENABLE_REWRITE */
380
381 #endif /* SLAPD_OVER_RWM */