1 /* rwmconf.c - rewrite/map configuration file routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2003 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",
98 mapping = (struct ldapmapping *)ch_calloc( 2,
99 sizeof(struct ldapmapping) );
100 if ( mapping == NULL ) {
102 "%s: line %d: out of memory\n",
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;
115 if ( src[0] != '\0' ) {
116 if ( oc_bvfind( &mapping->src ) == NULL ) {
118 "%s: line %d: warning, source objectClass '%s' "
119 "should be defined in schema\n",
120 fname, lineno, src );
123 * FIXME: this should become an err
128 if ( oc_bvfind( &mapping->dst ) == NULL ) {
130 "%s: line %d: warning, destination objectClass '%s' "
131 "is not defined in schema\n",
132 fname, lineno, dst );
136 const char *text = NULL;
137 AttributeDescription *ad = NULL;
139 if ( src[0] != '\0' ) {
140 rc = slap_bv2ad( &mapping->src, &ad, &text );
141 if ( rc != LDAP_SUCCESS ) {
143 "%s: line %d: warning, source attributeType '%s' "
144 "should be defined in schema\n",
145 fname, lineno, src );
148 * FIXME: this should become an err
155 rc = slap_bv2ad( &mapping->dst, &ad, &text );
156 if ( rc != LDAP_SUCCESS ) {
158 "%s: line %d: warning, destination attributeType '%s' "
159 "is not defined in schema\n",
160 fname, lineno, dst );
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)
168 "%s: line %d: duplicate mapping found (ignored)\n",
170 /* FIXME: free stuff */
174 if ( src[0] != '\0' ) {
175 avl_insert( &map->map, (caddr_t)mapping,
176 mapping_cmp, mapping_dup );
178 avl_insert( &map->remap, (caddr_t)&mapping[1],
179 mapping_cmp, mapping_dup );
185 ch_free( mapping->src.bv_val );
186 ch_free( mapping->dst.bv_val );
193 #ifdef ENABLE_REWRITE
195 suffix_massage_regexize( const char *s )
202 ( r = strchr( p, ',' ) ) != NULL;
206 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
208 ptr = lutil_strcopy( res, "(.*)" );
210 ( r = strchr( p, ',' ) ) != NULL;
212 ptr = lutil_strncopy( ptr, p, r - p + 1 );
213 ptr = lutil_strcopy( ptr, "[ ]?" );
215 if ( r[ 1 ] == ' ' ) {
219 lutil_strcopy( ptr, p );
225 suffix_massage_patternize( const char *s )
232 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
238 strcpy( res + sizeof( "%1" ) - 1, s );
244 suffix_massage_config(
245 struct rewrite_info *info,
255 rargv[ 0 ] = "rewriteEngine";
258 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
260 rargv[ 0 ] = "rewriteContext";
261 rargv[ 1 ] = "default";
263 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
265 rargv[ 0 ] = "rewriteRule";
266 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
267 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
270 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
271 ch_free( rargv[ 1 ] );
272 ch_free( rargv[ 2 ] );
274 rargv[ 0 ] = "rewriteContext";
275 rargv[ 1 ] = "searchResultDN";
277 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
279 rargv[ 0 ] = "rewriteRule";
280 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
281 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
284 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
285 ch_free( rargv[ 1 ] );
286 ch_free( rargv[ 2 ] );
288 rargv[ 0 ] = "rewriteContext";
289 rargv[ 1 ] = "matchedDN";
290 rargv[ 2 ] = "alias";
291 rargv[ 3 ] = "searchResultDN";
293 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
295 rargv[ 0 ] = "rewriteContext";
296 rargv[ 1 ] = "searchAttrDN";
297 rargv[ 2 ] = "alias";
298 rargv[ 3 ] = "searchResultDN";
300 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
304 #endif /* ENABLE_REWRITE */
306 #endif /* SLAPD_OVER_RWM */