]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmconf.c
03880721e5c02112a1bb2539591a163c4f6fd6d2
[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-2003 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         }
97
98         mapping = (struct ldapmapping *)ch_calloc( 2,
99                 sizeof(struct ldapmapping) );
100         if ( mapping == NULL ) {
101                 fprintf( stderr,
102                         "%s: line %d: out of memory\n",
103                         fname, lineno );
104                 return 1;
105         }
106         ber_str2bv( src, 0, 1, &mapping->src );
107         ber_str2bv( dst, 0, 1, &mapping->dst );
108         mapping[1].src = mapping->dst;
109         mapping[1].dst = mapping->src;
110
111         /*
112          * schema check
113          */
114         if ( is_oc ) {
115                 if ( src[0] != '\0' ) {
116                         if ( oc_bvfind( &mapping->src ) == NULL ) {
117                                 fprintf( stderr,
118         "%s: line %d: warning, source objectClass '%s' "
119         "should be defined in schema\n",
120                                         fname, lineno, src );
121
122                                 /*
123                                  * FIXME: this should become an err
124                                  */
125                         }
126                 }
127
128                 if ( oc_bvfind( &mapping->dst ) == NULL ) {
129                         fprintf( stderr,
130         "%s: line %d: warning, destination objectClass '%s' "
131         "is not defined in schema\n",
132                                 fname, lineno, dst );
133                 }
134         } else {
135                 int                     rc;
136                 const char              *text = NULL;
137                 AttributeDescription    *ad = NULL;
138
139                 if ( src[0] != '\0' ) {
140                         rc = slap_bv2ad( &mapping->src, &ad, &text );
141                         if ( rc != LDAP_SUCCESS ) {
142                                 fprintf( stderr,
143         "%s: line %d: warning, source attributeType '%s' "
144         "should be defined in schema\n",
145                                         fname, lineno, src );
146
147                                 /*
148                                  * FIXME: this should become an err
149                                  */
150                         }
151
152                         ad = NULL;
153                 }
154
155                 rc = slap_bv2ad( &mapping->dst, &ad, &text );
156                 if ( rc != LDAP_SUCCESS ) {
157                         fprintf( stderr,
158         "%s: line %d: warning, destination attributeType '%s' "
159         "is not defined in schema\n",
160                                 fname, lineno, dst );
161                 }
162         }
163
164         if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
165                         || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
166         {
167                 fprintf( stderr,
168                         "%s: line %d: duplicate mapping found (ignored)\n",
169                         fname, lineno );
170                 /* FIXME: free stuff */
171                 goto error_return;
172         }
173
174         if ( src[0] != '\0' ) {
175                 avl_insert( &map->map, (caddr_t)mapping,
176                                         mapping_cmp, mapping_dup );
177         }
178         avl_insert( &map->remap, (caddr_t)&mapping[1],
179                                 mapping_cmp, mapping_dup );
180
181         return 0;
182
183 error_return:;
184         if ( mapping ) {
185                 ch_free( mapping->src.bv_val );
186                 ch_free( mapping->dst.bv_val );
187                 ch_free( mapping );
188         }
189
190         return 1;
191 }
192
193 #ifdef ENABLE_REWRITE
194 static char *
195 rwm_suffix_massage_regexize( const char *s )
196 {
197         char *res, *ptr;
198         const char *p, *r;
199         int i;
200
201         for ( i = 0, p = s; 
202                         ( r = strchr( p, ',' ) ) != NULL; 
203                         p = r + 1, i++ )
204                 ;
205
206         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
207
208         ptr = lutil_strcopy( res, "(.*)" );
209         for ( i = 0, p = s;
210                         ( r = strchr( p, ',' ) ) != NULL;
211                         p = r + 1 , i++ ) {
212                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
213                 ptr = lutil_strcopy( ptr, "[ ]?" );
214
215                 if ( r[ 1 ] == ' ' ) {
216                         r++;
217                 }
218         }
219         lutil_strcopy( ptr, p );
220
221         return res;
222 }
223
224 static char *
225 rwm_suffix_massage_patternize( const char *s )
226 {
227         ber_len_t       len;
228         char            *res;
229
230         len = strlen( s );
231
232         res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
233         if ( res == NULL ) {
234                 return NULL;
235         }
236
237         strcpy( res, "%1" );
238         strcpy( res + sizeof( "%1" ) - 1, s );
239
240         return res;
241 }
242
243 int
244 rwm_suffix_massage_config( 
245                 struct rewrite_info *info,
246                 struct berval *pvnc,
247                 struct berval *nvnc,
248                 struct berval *prnc,
249                 struct berval *nrnc
250 )
251 {
252         char *rargv[ 5 ];
253         int line = 0;
254
255         rargv[ 0 ] = "rewriteEngine";
256         rargv[ 1 ] = "on";
257         rargv[ 2 ] = NULL;
258         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
259
260         rargv[ 0 ] = "rewriteContext";
261         rargv[ 1 ] = "default";
262         rargv[ 2 ] = NULL;
263         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
264
265         rargv[ 0 ] = "rewriteRule";
266         rargv[ 1 ] = rwm_suffix_massage_regexize( pvnc->bv_val );
267         rargv[ 2 ] = rwm_suffix_massage_patternize( prnc->bv_val );
268         rargv[ 3 ] = ":";
269         rargv[ 4 ] = NULL;
270         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
271         ch_free( rargv[ 1 ] );
272         ch_free( rargv[ 2 ] );
273         
274         rargv[ 0 ] = "rewriteContext";
275         rargv[ 1 ] = "searchResultDN";
276         rargv[ 2 ] = NULL;
277         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
278         
279         rargv[ 0 ] = "rewriteRule";
280         rargv[ 1 ] = rwm_suffix_massage_regexize( prnc->bv_val );
281         rargv[ 2 ] = rwm_suffix_massage_patternize( pvnc->bv_val );
282         rargv[ 3 ] = ":";
283         rargv[ 4 ] = NULL;
284         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
285         ch_free( rargv[ 1 ] );
286         ch_free( rargv[ 2 ] );
287
288         rargv[ 0 ] = "rewriteContext";
289         rargv[ 1 ] = "matchedDN";
290         rargv[ 2 ] = "alias";
291         rargv[ 3 ] = "searchResultDN";
292         rargv[ 4 ] = NULL;
293         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
294
295         rargv[ 0 ] = "rewriteContext";
296         rargv[ 1 ] = "searchAttrDN";
297         rargv[ 2 ] = "alias";
298         rargv[ 3 ] = "searchResultDN";
299         rargv[ 4 ] = NULL;
300         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
301
302         return 0;
303 }
304 #endif /* ENABLE_REWRITE */
305
306 #endif /* SLAPD_OVER_RWM */