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
28 #include <ac/string.h>
29 #include <ac/socket.h>
37 struct ldapmap *oc_map,
38 struct ldapmap *at_map,
45 struct ldapmapping *mapping;
49 if ( argc < 3 || argc > 4 ) {
51 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
56 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
60 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
64 fprintf( stderr, "%s: line %d: syntax is "
65 "\"map {objectclass | attribute} [<local> | *] "
66 "{<foreign> | *}\"\n",
71 if ( strcmp( argv[2], "*" ) == 0 ) {
72 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
73 map->drop_missing = ( argc < 4 );
78 } else if ( argc < 4 ) {
84 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
87 if ( ( map == at_map )
88 && ( strcasecmp( src, "objectclass" ) == 0
89 || strcasecmp( dst, "objectclass" ) == 0 ) )
92 "%s: line %d: objectclass attribute cannot be mapped\n",
96 mapping = (struct ldapmapping *)ch_calloc( 2,
97 sizeof(struct ldapmapping) );
98 if ( mapping == NULL ) {
100 "%s: line %d: out of memory\n",
104 ber_str2bv( src, 0, 1, &mapping->src );
105 ber_str2bv( dst, 0, 1, &mapping->dst );
106 mapping[1].src = mapping->dst;
107 mapping[1].dst = mapping->src;
113 if ( src[0] != '\0' ) {
114 if ( oc_bvfind( &mapping->src ) == NULL ) {
116 "%s: line %d: warning, source objectClass '%s' "
117 "should be defined in schema\n",
118 fname, lineno, src );
121 * FIXME: this should become an err
126 if ( oc_bvfind( &mapping->dst ) == NULL ) {
128 "%s: line %d: warning, destination objectClass '%s' "
129 "is not defined in schema\n",
130 fname, lineno, dst );
134 const char *text = NULL;
135 AttributeDescription *ad = NULL;
137 if ( src[0] != '\0' ) {
138 rc = slap_bv2ad( &mapping->src, &ad, &text );
139 if ( rc != LDAP_SUCCESS ) {
141 "%s: line %d: warning, source attributeType '%s' "
142 "should be defined in schema\n",
143 fname, lineno, src );
146 * FIXME: this should become an err
153 rc = slap_bv2ad( &mapping->dst, &ad, &text );
154 if ( rc != LDAP_SUCCESS ) {
156 "%s: line %d: warning, destination attributeType '%s' "
157 "is not defined in schema\n",
158 fname, lineno, dst );
162 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
163 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
166 "%s: line %d: duplicate mapping found (ignored)\n",
168 /* FIXME: free stuff */
172 if ( src[0] != '\0' ) {
173 avl_insert( &map->map, (caddr_t)mapping,
174 mapping_cmp, mapping_dup );
176 avl_insert( &map->remap, (caddr_t)&mapping[1],
177 mapping_cmp, mapping_dup );
183 ch_free( mapping->src.bv_val );
184 ch_free( mapping->dst.bv_val );
191 #ifdef ENABLE_REWRITE
193 suffix_massage_regexize( const char *s )
200 ( r = strchr( p, ',' ) ) != NULL;
204 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
206 ptr = lutil_strcopy( res, "(.*)" );
208 ( r = strchr( p, ',' ) ) != NULL;
210 ptr = lutil_strncopy( ptr, p, r - p + 1 );
211 ptr = lutil_strcopy( ptr, "[ ]?" );
213 if ( r[ 1 ] == ' ' ) {
217 lutil_strcopy( ptr, p );
223 suffix_massage_patternize( const char *s )
230 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
236 strcpy( res + sizeof( "%1" ) - 1, s );
242 suffix_massage_config(
243 struct rewrite_info *info,
253 rargv[ 0 ] = "rewriteEngine";
256 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
258 rargv[ 0 ] = "rewriteContext";
259 rargv[ 1 ] = "default";
261 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
263 rargv[ 0 ] = "rewriteRule";
264 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
265 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
268 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
269 ch_free( rargv[ 1 ] );
270 ch_free( rargv[ 2 ] );
272 rargv[ 0 ] = "rewriteContext";
273 rargv[ 1 ] = "searchResultDN";
275 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
277 rargv[ 0 ] = "rewriteRule";
278 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
279 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
282 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
283 ch_free( rargv[ 1 ] );
284 ch_free( rargv[ 2 ] );
286 rargv[ 0 ] = "rewriteContext";
287 rargv[ 1 ] = "matchedDN";
288 rargv[ 2 ] = "alias";
289 rargv[ 3 ] = "searchResultDN";
291 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
293 rargv[ 0 ] = "rewriteContext";
294 rargv[ 1 ] = "searchAttrDN";
295 rargv[ 2 ] = "alias";
296 rargv[ 3 ] = "searchResultDN";
298 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
302 #endif /* ENABLE_REWRITE */