1 /* rwmconf.c - rewrite/map configuration file routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2004 The OpenLDAP Foundation.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * Portions Copyright 2000-2003 Pierangelo Masarati.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
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>.
19 * This work was initially developed by the Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
30 #include <ac/string.h>
31 #include <ac/socket.h>
39 struct ldapmap *oc_map,
40 struct ldapmap *at_map,
47 struct ldapmapping *mapping;
51 if ( argc < 3 || argc > 4 ) {
53 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
58 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
62 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
66 fprintf( stderr, "%s: line %d: syntax is "
67 "\"map {objectclass | attribute} [<local> | *] "
68 "{<foreign> | *}\"\n",
73 if ( strcmp( argv[2], "*" ) == 0 ) {
74 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
75 map->drop_missing = ( argc < 4 );
80 } else if ( argc < 4 ) {
86 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
89 if ( ( map == at_map )
90 && ( strcasecmp( src, "objectclass" ) == 0
91 || strcasecmp( dst, "objectclass" ) == 0 ) )
94 "%s: line %d: objectclass attribute cannot be mapped\n",
99 mapping = (struct ldapmapping *)ch_calloc( 2,
100 sizeof(struct ldapmapping) );
101 if ( mapping == NULL ) {
103 "%s: line %d: out of memory\n",
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;
112 mapping[0].m_flags = RWMMAP_F_NONE;
113 mapping[1].m_flags = RWMMAP_F_NONE;
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 ) {
123 "%s: line %d: warning, source objectClass '%s' "
124 "should be defined in schema\n",
125 fname, lineno, src );
128 * FIXME: this should become an err
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;
135 mapping[1].m_dst_oc = mapping[0].m_src_oc;
138 mapping[0].m_dst_oc = oc_bvfind( &mapping[0].m_dst );
139 if ( mapping[0].m_dst_oc == NULL ) {
141 "%s: line %d: warning, destination objectClass '%s' "
142 "is not defined in schema\n",
143 fname, lineno, dst );
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;
150 mapping[1].m_src_oc = mapping[0].m_dst_oc;
152 mapping[0].m_flags |= RWMMAP_F_IS_OC;
153 mapping[1].m_flags |= RWMMAP_F_IS_OC;
157 const char *text = NULL;
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 ) {
164 "%s: line %d: warning, source attributeType '%s' "
165 "should be defined in schema\n",
166 fname, lineno, src );
169 * FIXME: this should become an err
171 * FIXME: or, we should create a fake ad
175 mapping[0].m_src_ad = ch_malloc( sizeof( AttributeDescription ) );
176 memset( mapping[0].m_src_ad, 0, sizeof( AttributeDescription ) );
177 mapping[0].m_src_ad->ad_cname = mapping[0].m_src;
178 mapping[1].m_flags |= RWMMAP_F_FREE_SRC;
180 mapping[1].m_dst_ad = mapping[0].m_src_ad;
183 rc = slap_bv2ad( &mapping[0].m_dst, &mapping[0].m_dst_ad, &text );
184 if ( rc != LDAP_SUCCESS ) {
186 "%s: line %d: warning, destination attributeType '%s' "
187 "is not defined in schema\n",
188 fname, lineno, dst );
190 mapping[0].m_dst_ad = ch_malloc( sizeof( AttributeDescription ) );
191 memset( mapping[0].m_dst_ad, 0, sizeof( AttributeDescription ) );
192 mapping[0].m_dst_ad->ad_cname = mapping[0].m_dst;
193 mapping[1].m_flags |= RWMMAP_F_FREE_SRC;
195 mapping[1].m_src_ad = mapping[0].m_dst_ad;
198 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, rwm_mapping_cmp ) != NULL)
199 || avl_find( map->remap, (caddr_t)&mapping[1], rwm_mapping_cmp ) != NULL)
202 "%s: line %d: duplicate mapping found (ignored)\n",
204 /* FIXME: free stuff */
208 if ( src[0] != '\0' ) {
209 avl_insert( &map->map, (caddr_t)&mapping[0],
210 rwm_mapping_cmp, rwm_mapping_dup );
212 avl_insert( &map->remap, (caddr_t)&mapping[1],
213 rwm_mapping_cmp, rwm_mapping_dup );
219 rwm_mapping_free( mapping );
225 #ifdef ENABLE_REWRITE
227 rwm_suffix_massage_regexize( const char *s )
234 ( r = strchr( p, ',' ) ) != NULL;
238 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
240 ptr = lutil_strcopy( res, "(.*)" );
242 ( r = strchr( p, ',' ) ) != NULL;
244 ptr = lutil_strncopy( ptr, p, r - p + 1 );
245 ptr = lutil_strcopy( ptr, "[ ]?" );
247 if ( r[ 1 ] == ' ' ) {
251 lutil_strcopy( ptr, p );
257 rwm_suffix_massage_patternize( const char *s )
264 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
270 strcpy( res + sizeof( "%1" ) - 1, s );
276 rwm_suffix_massage_config(
277 struct rewrite_info *info,
287 rargv[ 0 ] = "rewriteEngine";
290 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
292 rargv[ 0 ] = "rewriteContext";
293 rargv[ 1 ] = "default";
295 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
297 rargv[ 0 ] = "rewriteRule";
298 rargv[ 1 ] = rwm_suffix_massage_regexize( pvnc->bv_val );
299 rargv[ 2 ] = rwm_suffix_massage_patternize( prnc->bv_val );
302 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
303 ch_free( rargv[ 1 ] );
304 ch_free( rargv[ 2 ] );
306 rargv[ 0 ] = "rewriteContext";
307 rargv[ 1 ] = "searchResultDN";
309 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
311 rargv[ 0 ] = "rewriteRule";
312 rargv[ 1 ] = rwm_suffix_massage_regexize( prnc->bv_val );
313 rargv[ 2 ] = rwm_suffix_massage_patternize( pvnc->bv_val );
316 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
317 ch_free( rargv[ 1 ] );
318 ch_free( rargv[ 2 ] );
320 rargv[ 0 ] = "rewriteContext";
321 rargv[ 1 ] = "matchedDN";
322 rargv[ 2 ] = "alias";
323 rargv[ 3 ] = "searchResultDN";
325 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
327 rargv[ 0 ] = "rewriteContext";
328 rargv[ 1 ] = "searchAttrDN";
329 rargv[ 2 ] = "alias";
330 rargv[ 3 ] = "searchResultDN";
332 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
336 #endif /* ENABLE_REWRITE */
338 #endif /* SLAPD_OVER_RWM */