]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmconf.c
71a77924c903cbc9a41d93583bbf87173feaaefe
[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                                 goto error_return;
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                                  * we create a fake "proxied" ad 
179                                  * and add it here.
180                                  */
181
182                                 rc = slap_bv2undef_ad( &mapping[0].m_src,
183                                                 &mapping[0].m_src_ad, &text,
184                                                 SLAP_AD_PROXIED );
185                                 if ( rc != LDAP_SUCCESS ) {
186                                         fprintf( stderr,
187         "%s: line %d: source attributeType '%s': %d (%s)\n",
188                                                 fname, lineno, src, rc, text ? text : "null" );
189                                         goto error_return;
190                                 }
191
192                         }
193                         mapping[1].m_dst_ad = mapping[0].m_src_ad;
194                 }
195
196                 rc = slap_bv2ad( &mapping[0].m_dst, &mapping[0].m_dst_ad, &text );
197                 if ( rc != LDAP_SUCCESS ) {
198                         fprintf( stderr,
199         "%s: line %d: warning, destination attributeType '%s' "
200         "is not defined in schema\n",
201                                 fname, lineno, dst );
202
203                         rc = slap_bv2undef_ad( &mapping[0].m_dst,
204                                         &mapping[0].m_dst_ad, &text,
205                                         SLAP_AD_PROXIED );
206                         if ( rc != LDAP_SUCCESS ) {
207                                 fprintf( stderr,
208         "%s: line %d: destination attributeType '%s': %d (%s)\n",
209                                         fname, lineno, dst, rc, text ? text : "null" );
210                                 goto error_return;
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         if ( s[0] == '\0' ) {
252                 return ch_strdup( "^(.+)$" );
253         }
254
255         for ( i = 0, p = s; 
256                         ( r = strchr( p, ',' ) ) != NULL; 
257                         p = r + 1, i++ )
258                 ;
259
260         res = ch_calloc( sizeof( char ), strlen( s )
261                         + STRLENOF( "((.+),)?" )
262                         + STRLENOF( "[ ]?" ) * i
263                         + STRLENOF( "$" ) + 1 );
264
265         ptr = lutil_strcopy( res, "((.+),)?" );
266         for ( i = 0, p = s;
267                         ( r = strchr( p, ',' ) ) != NULL;
268                         p = r + 1 , i++ ) {
269                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
270                 ptr = lutil_strcopy( ptr, "[ ]?" );
271
272                 if ( r[ 1 ] == ' ' ) {
273                         r++;
274                 }
275         }
276         ptr = lutil_strcopy( ptr, p );
277         ptr[0] = '$';
278         ptr[1] = '\0';
279
280         return res;
281 }
282
283 static char *
284 rwm_suffix_massage_patternize( const char *s, const char *p )
285 {
286         ber_len_t       len;
287         char            *res, *ptr;
288
289         len = strlen( p );
290
291         if ( s[ 0 ] == '\0' ) {
292                 len++;
293         }
294
295         res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
296         if ( res == NULL ) {
297                 return NULL;
298         }
299
300         ptr = lutil_strcopy( res, ( p[0] == '\0' ? "%2" : "%1" ) );
301         if ( s[ 0 ] == '\0' ) {
302                 ptr[ 0 ] = ',';
303                 ptr++;
304         }
305         lutil_strcopy( ptr, p );
306
307         return res;
308 }
309
310 int
311 rwm_suffix_massage_config( 
312                 struct rewrite_info *info,
313                 struct berval *pvnc,
314                 struct berval *nvnc,
315                 struct berval *prnc,
316                 struct berval *nrnc
317 )
318 {
319         char *rargv[ 5 ];
320         int line = 0;
321
322         rargv[ 0 ] = "rewriteEngine";
323         rargv[ 1 ] = "on";
324         rargv[ 2 ] = NULL;
325         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
326
327         rargv[ 0 ] = "rewriteContext";
328         rargv[ 1 ] = "default";
329         rargv[ 2 ] = NULL;
330         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
331
332         rargv[ 0 ] = "rewriteRule";
333         rargv[ 1 ] = rwm_suffix_massage_regexize( pvnc->bv_val );
334         rargv[ 2 ] = rwm_suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
335         rargv[ 3 ] = ":";
336         rargv[ 4 ] = NULL;
337         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
338         ch_free( rargv[ 1 ] );
339         ch_free( rargv[ 2 ] );
340         
341         if ( BER_BVISEMPTY( pvnc ) ) {
342                 rargv[ 0 ] = "rewriteRule";
343                 rargv[ 1 ] = "^$";
344                 rargv[ 2 ] = prnc->bv_val;
345                 rargv[ 3 ] = ":";
346                 rargv[ 4 ] = NULL;
347                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
348         }
349
350         rargv[ 0 ] = "rewriteContext";
351         rargv[ 1 ] = "searchEntryDN";
352         rargv[ 2 ] = NULL;
353         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
354         
355         rargv[ 0 ] = "rewriteRule";
356         rargv[ 1 ] = rwm_suffix_massage_regexize( prnc->bv_val );
357         rargv[ 2 ] = rwm_suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
358         rargv[ 3 ] = ":";
359         rargv[ 4 ] = NULL;
360         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
361         ch_free( rargv[ 1 ] );
362         ch_free( rargv[ 2 ] );
363
364         if ( BER_BVISEMPTY( prnc ) ) {
365                 rargv[ 0 ] = "rewriteRule";
366                 rargv[ 1 ] = "^$";
367                 rargv[ 2 ] = pvnc->bv_val;
368                 rargv[ 3 ] = ":";
369                 rargv[ 4 ] = NULL;
370                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
371         }
372
373         rargv[ 0 ] = "rewriteContext";
374         rargv[ 1 ] = "matchedDN";
375         rargv[ 2 ] = "alias";
376         rargv[ 3 ] = "searchEntryDN";
377         rargv[ 4 ] = NULL;
378         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
379
380 #ifdef RWM_REFERRAL_REWRITE
381         /* FIXME: we don't want this on by default, do we? */
382         rargv[ 0 ] = "rewriteContext";
383         rargv[ 1 ] = "referralDN";
384         rargv[ 2 ] = "alias";
385         rargv[ 3 ] = "searchEntryDN";
386         rargv[ 4 ] = NULL;
387         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
388 #else /* ! RWM_REFERRAL_REWRITE */
389         rargv[ 0 ] = "rewriteContext";
390         rargv[ 1 ] = "referralAttrDN";
391         rargv[ 2 ] = NULL;
392         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
393
394         rargv[ 0 ] = "rewriteContext";
395         rargv[ 1 ] = "referralDN";
396         rargv[ 2 ] = NULL;
397         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
398 #endif /* ! RWM_REFERRAL_REWRITE */
399
400         rargv[ 0 ] = "rewriteContext";
401         rargv[ 1 ] = "searchAttrDN";
402         rargv[ 2 ] = "alias";
403         rargv[ 3 ] = "searchEntryDN";
404         rargv[ 4 ] = NULL;
405         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
406
407         return 0;
408 }
409 #endif /* ENABLE_REWRITE */
410
411 #endif /* SLAPD_OVER_RWM */