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 rc = slap_bv2undef_ad( &mapping[0].m_src,
176 &mapping[0].m_src_ad, &text );
177 if ( rc != LDAP_SUCCESS ) {
179 "%s: line %d: source attributeType '%s': %d (%s)\n",
180 fname, lineno, src, rc, text ? text : "null" );
185 mapping[1].m_dst_ad = mapping[0].m_src_ad;
188 rc = slap_bv2ad( &mapping[0].m_dst, &mapping[0].m_dst_ad, &text );
189 if ( rc != LDAP_SUCCESS ) {
191 "%s: line %d: warning, destination attributeType '%s' "
192 "is not defined in schema\n",
193 fname, lineno, dst );
195 rc = slap_bv2undef_ad( &mapping[0].m_dst,
196 &mapping[0].m_dst_ad, &text );
197 if ( rc != LDAP_SUCCESS ) {
199 "%s: line %d: destination attributeType '%s': %d (%s)\n",
200 fname, lineno, src, rc, text ? text : "null" );
204 mapping[1].m_src_ad = mapping[0].m_dst_ad;
207 if ( ( src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, rwm_mapping_cmp ) != NULL)
208 || avl_find( map->remap, (caddr_t)&mapping[1], rwm_mapping_cmp ) != NULL)
211 "%s: line %d: duplicate mapping found (ignored)\n",
213 /* FIXME: free stuff */
217 if ( src[0] != '\0' ) {
218 avl_insert( &map->map, (caddr_t)&mapping[0],
219 rwm_mapping_cmp, rwm_mapping_dup );
221 avl_insert( &map->remap, (caddr_t)&mapping[1],
222 rwm_mapping_cmp, rwm_mapping_dup );
228 rwm_mapping_free( mapping );
234 #ifdef ENABLE_REWRITE
236 rwm_suffix_massage_regexize( const char *s )
243 ( r = strchr( p, ',' ) ) != NULL;
247 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
249 ptr = lutil_strcopy( res, "(.*)" );
251 ( r = strchr( p, ',' ) ) != NULL;
253 ptr = lutil_strncopy( ptr, p, r - p + 1 );
254 ptr = lutil_strcopy( ptr, "[ ]?" );
256 if ( r[ 1 ] == ' ' ) {
260 lutil_strcopy( ptr, p );
266 rwm_suffix_massage_patternize( const char *s )
273 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
279 strcpy( res + STRLENOF( "%1" ), s );
285 rwm_suffix_massage_config(
286 struct rewrite_info *info,
296 rargv[ 0 ] = "rewriteEngine";
299 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
301 rargv[ 0 ] = "rewriteContext";
302 rargv[ 1 ] = "default";
304 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
306 rargv[ 0 ] = "rewriteRule";
307 rargv[ 1 ] = rwm_suffix_massage_regexize( pvnc->bv_val );
308 rargv[ 2 ] = rwm_suffix_massage_patternize( prnc->bv_val );
311 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
312 ch_free( rargv[ 1 ] );
313 ch_free( rargv[ 2 ] );
315 rargv[ 0 ] = "rewriteContext";
316 rargv[ 1 ] = "searchEntryDN";
318 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
320 rargv[ 0 ] = "rewriteRule";
321 rargv[ 1 ] = rwm_suffix_massage_regexize( prnc->bv_val );
322 rargv[ 2 ] = rwm_suffix_massage_patternize( pvnc->bv_val );
325 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
326 ch_free( rargv[ 1 ] );
327 ch_free( rargv[ 2 ] );
329 rargv[ 0 ] = "rewriteContext";
330 rargv[ 1 ] = "matchedDN";
331 rargv[ 2 ] = "alias";
332 rargv[ 3 ] = "searchEntryDN";
334 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
336 rargv[ 0 ] = "rewriteContext";
337 rargv[ 1 ] = "searchAttrDN";
338 rargv[ 2 ] = "alias";
339 rargv[ 3 ] = "searchEntryDN";
341 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
345 #endif /* ENABLE_REWRITE */
347 #endif /* SLAPD_OVER_RWM */