1 /* rwmconf.c - rewrite/map configuration file routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by the Howard Chu for inclusion
18 * in OpenLDAP Software and subsequently enhanced by Pierangelo
21 /* This is an altered version */
23 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
25 * Permission is granted to anyone to use this software for any purpose
26 * on any computer system, and to alter it and redistribute it, subject
27 * to the following restrictions:
29 * 1. The author is not responsible for the consequences of use of this
30 * software, no matter how awful, even if they arise from flaws in it.
32 * 2. The origin of this software must not be misrepresented, either by
33 * explicit claim or by omission. Since few users ever read sources,
34 * credits should appear in the documentation.
36 * 3. Altered versions must be plainly marked as such, and must not be
37 * misrepresented as being the original software. Since few users
38 * ever read sources, credits should appear in the documentation.
40 * 4. This notice may not be removed or altered.
44 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
46 * This software is being modified by Pierangelo Masarati.
47 * The previously reported conditions apply to the modified code as well.
48 * Changes in the original code are highlighted where required.
49 * Credits for the original code go to the author, Howard Chu.
56 #include <ac/string.h>
57 #include <ac/socket.h>
65 struct ldapmap *oc_map,
66 struct ldapmap *at_map,
73 struct ldapmapping *mapping;
77 if ( argc < 3 || argc > 4 ) {
79 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
84 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
88 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
92 fprintf( stderr, "%s: line %d: syntax is "
93 "\"map {objectclass | attribute} [<local> | *] "
94 "{<foreign> | *}\"\n",
99 if ( strcmp( argv[2], "*" ) == 0 ) {
100 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
101 map->drop_missing = ( argc < 4 );
106 } else if ( argc < 4 ) {
112 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
115 if ( ( map == at_map )
116 && ( strcasecmp( src, "objectclass" ) == 0
117 || strcasecmp( dst, "objectclass" ) == 0 ) )
120 "%s: line %d: objectclass attribute cannot be mapped\n",
124 mapping = (struct ldapmapping *)ch_calloc( 2,
125 sizeof(struct ldapmapping) );
126 if ( mapping == NULL ) {
128 "%s: line %d: out of memory\n",
132 ber_str2bv( src, 0, 1, &mapping->src );
133 ber_str2bv( dst, 0, 1, &mapping->dst );
134 mapping[1].src = mapping->dst;
135 mapping[1].dst = mapping->src;
141 if ( src[0] != '\0' ) {
142 if ( oc_bvfind( &mapping->src ) == NULL ) {
144 "%s: line %d: warning, source objectClass '%s' "
145 "should be defined in schema\n",
146 fname, lineno, src );
149 * FIXME: this should become an err
154 if ( oc_bvfind( &mapping->dst ) == NULL ) {
156 "%s: line %d: warning, destination objectClass '%s' "
157 "is not defined in schema\n",
158 fname, lineno, dst );
162 const char *text = NULL;
163 AttributeDescription *ad = NULL;
165 if ( src[0] != '\0' ) {
166 rc = slap_bv2ad( &mapping->src, &ad, &text );
167 if ( rc != LDAP_SUCCESS ) {
169 "%s: line %d: warning, source attributeType '%s' "
170 "should be defined in schema\n",
171 fname, lineno, src );
174 * FIXME: this should become an err
181 rc = slap_bv2ad( &mapping->dst, &ad, &text );
182 if ( rc != LDAP_SUCCESS ) {
184 "%s: line %d: warning, destination attributeType '%s' "
185 "is not defined in schema\n",
186 fname, lineno, dst );
190 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
191 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
194 "%s: line %d: duplicate mapping found (ignored)\n",
196 /* FIXME: free stuff */
200 if ( src[0] != '\0' ) {
201 avl_insert( &map->map, (caddr_t)mapping,
202 mapping_cmp, mapping_dup );
204 avl_insert( &map->remap, (caddr_t)&mapping[1],
205 mapping_cmp, mapping_dup );
211 ch_free( mapping->src.bv_val );
212 ch_free( mapping->dst.bv_val );
219 #ifdef ENABLE_REWRITE
221 suffix_massage_regexize( const char *s )
228 ( r = strchr( p, ',' ) ) != NULL;
232 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
234 ptr = lutil_strcopy( res, "(.*)" );
236 ( r = strchr( p, ',' ) ) != NULL;
238 ptr = lutil_strncopy( ptr, p, r - p + 1 );
239 ptr = lutil_strcopy( ptr, "[ ]?" );
241 if ( r[ 1 ] == ' ' ) {
245 lutil_strcopy( ptr, p );
251 suffix_massage_patternize( const char *s )
258 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
264 strcpy( res + sizeof( "%1" ) - 1, s );
270 suffix_massage_config(
271 struct rewrite_info *info,
281 rargv[ 0 ] = "rewriteEngine";
284 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
286 rargv[ 0 ] = "rewriteContext";
287 rargv[ 1 ] = "default";
289 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
291 rargv[ 0 ] = "rewriteRule";
292 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
293 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
296 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
297 ch_free( rargv[ 1 ] );
298 ch_free( rargv[ 2 ] );
300 rargv[ 0 ] = "rewriteContext";
301 rargv[ 1 ] = "searchResult";
303 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
305 rargv[ 0 ] = "rewriteRule";
306 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
307 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
310 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
311 ch_free( rargv[ 1 ] );
312 ch_free( rargv[ 2 ] );
314 rargv[ 0 ] = "rewriteContext";
315 rargv[ 1 ] = "matchedDN";
316 rargv[ 2 ] = "alias";
317 rargv[ 3 ] = "searchResult";
319 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
321 rargv[ 0 ] = "rewriteContext";
322 rargv[ 1 ] = "searchAttrDN";
323 rargv[ 2 ] = "alias";
324 rargv[ 3 ] = "searchResult";
326 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
330 #endif /* ENABLE_REWRITE */