1 /* rwmconf.c - rewrite/map configuration file routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2005 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 = oc_bvfind_undef( &mapping[0].m_dst );
146 if ( mapping[0].m_dst_oc == NULL ) {
147 fprintf( stderr, "%s: line %d: unable to mimic destination objectClass '%s'\n",
148 fname, lineno, dst );
153 mapping[0].m_dst_oc = ch_malloc( sizeof( ObjectClass ) );
154 memset( mapping[0].m_dst_oc, 0, sizeof( ObjectClass ) );
155 mapping[0].m_dst_oc->soc_cname = mapping[0].m_dst;
156 mapping[0].m_flags |= RWMMAP_F_FREE_DST;
159 mapping[1].m_src_oc = mapping[0].m_dst_oc;
161 mapping[0].m_flags |= RWMMAP_F_IS_OC;
162 mapping[1].m_flags |= RWMMAP_F_IS_OC;
166 const char *text = NULL;
168 if ( src[0] != '\0' ) {
169 rc = slap_bv2ad( &mapping[0].m_src,
170 &mapping[0].m_src_ad, &text );
171 if ( rc != LDAP_SUCCESS ) {
173 "%s: line %d: warning, source attributeType '%s' "
174 "should be defined in schema\n",
175 fname, lineno, src );
178 * FIXME: this should become an err
180 * FIXME: or, we should create a fake ad
184 rc = slap_bv2undef_ad( &mapping[0].m_src,
185 &mapping[0].m_src_ad, &text );
186 if ( rc != LDAP_SUCCESS ) {
188 "%s: line %d: source attributeType '%s': %d (%s)\n",
189 fname, lineno, src, rc, text ? text : "null" );
194 mapping[1].m_dst_ad = mapping[0].m_src_ad;
197 rc = slap_bv2ad( &mapping[0].m_dst, &mapping[0].m_dst_ad, &text );
198 if ( rc != LDAP_SUCCESS ) {
200 "%s: line %d: warning, destination attributeType '%s' "
201 "is not defined in schema\n",
202 fname, lineno, dst );
204 rc = slap_bv2undef_ad( &mapping[0].m_dst,
205 &mapping[0].m_dst_ad, &text );
206 if ( rc != LDAP_SUCCESS ) {
208 "%s: line %d: destination attributeType '%s': %d (%s)\n",
209 fname, lineno, src, rc, text ? text : "null" );
213 mapping[1].m_src_ad = mapping[0].m_dst_ad;
216 if ( ( src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, rwm_mapping_cmp ) != NULL)
217 || avl_find( map->remap, (caddr_t)&mapping[1], rwm_mapping_cmp ) != NULL)
220 "%s: line %d: duplicate mapping found" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
222 /* FIXME: free stuff */
226 if ( src[0] != '\0' ) {
227 avl_insert( &map->map, (caddr_t)&mapping[0],
228 rwm_mapping_cmp, rwm_mapping_dup );
230 avl_insert( &map->remap, (caddr_t)&mapping[1],
231 rwm_mapping_cmp, rwm_mapping_dup );
237 rwm_mapping_free( mapping );
243 #ifdef ENABLE_REWRITE
245 rwm_suffix_massage_regexize( const char *s )
252 ( r = strchr( p, ',' ) ) != NULL;
256 res = ch_calloc( sizeof( char ), strlen( s )
257 + STRLENOF( "((.+),)?" )
258 + STRLENOF( "[ ]?" ) * i
259 + STRLENOF( "$" ) + 1 );
261 ptr = lutil_strcopy( res, "((.+),)?" );
263 ( r = strchr( p, ',' ) ) != NULL;
265 ptr = lutil_strncopy( ptr, p, r - p + 1 );
266 ptr = lutil_strcopy( ptr, "[ ]?" );
268 if ( r[ 1 ] == ' ' ) {
272 ptr = lutil_strcopy( ptr, p );
280 rwm_suffix_massage_patternize( const char *s )
287 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
293 strcpy( res + STRLENOF( "%1" ), s );
299 rwm_suffix_massage_config(
300 struct rewrite_info *info,
310 rargv[ 0 ] = "rewriteEngine";
313 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
315 rargv[ 0 ] = "rewriteContext";
316 rargv[ 1 ] = "default";
318 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
320 rargv[ 0 ] = "rewriteRule";
321 rargv[ 1 ] = rwm_suffix_massage_regexize( pvnc->bv_val );
322 rargv[ 2 ] = rwm_suffix_massage_patternize( prnc->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 ] = "searchEntryDN";
332 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
334 rargv[ 0 ] = "rewriteRule";
335 rargv[ 1 ] = rwm_suffix_massage_regexize( prnc->bv_val );
336 rargv[ 2 ] = rwm_suffix_massage_patternize( pvnc->bv_val );
339 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
340 ch_free( rargv[ 1 ] );
341 ch_free( rargv[ 2 ] );
343 rargv[ 0 ] = "rewriteContext";
344 rargv[ 1 ] = "matchedDN";
345 rargv[ 2 ] = "alias";
346 rargv[ 3 ] = "searchEntryDN";
348 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
350 #ifdef RWM_REFERRAL_REWRITE
351 /* FIXME: we don't want this on by default, do we? */
352 rargv[ 0 ] = "rewriteContext";
353 rargv[ 1 ] = "referralDN";
354 rargv[ 2 ] = "alias";
355 rargv[ 3 ] = "searchEntryDN";
357 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
358 #else /* ! RWM_REFERRAL_REWRITE */
359 rargv[ 0 ] = "rewriteContext";
360 rargv[ 1 ] = "referralAttrDN";
362 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
364 rargv[ 0 ] = "rewriteContext";
365 rargv[ 1 ] = "referralDN";
367 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
368 #endif /* ! RWM_REFERRAL_REWRITE */
370 rargv[ 0 ] = "rewriteContext";
371 rargv[ 1 ] = "searchAttrDN";
372 rargv[ 2 ] = "alias";
373 rargv[ 3 ] = "searchEntryDN";
375 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
379 #endif /* ENABLE_REWRITE */
381 #endif /* SLAPD_OVER_RWM */