1 /* rwmconf.c - rewrite/map configuration file routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2010 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;
52 if ( argc < 3 || argc > 4 ) {
53 Debug( LDAP_DEBUG_ANY,
54 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
59 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
63 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
67 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
68 "\"map {objectclass | attribute} [<local> | *] "
69 "{<foreign> | *}\"\n",
74 if ( !is_oc && map->map == NULL ) {
75 /* only init if required */
76 if ( rwm_map_init( map, &mapping ) != LDAP_SUCCESS ) {
81 if ( strcmp( argv[2], "*" ) == 0 ) {
82 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
83 map->drop_missing = ( argc < 4 );
88 } else if ( argc < 4 ) {
94 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
97 if ( ( map == at_map )
98 && ( strcasecmp( src, "objectclass" ) == 0
99 || strcasecmp( dst, "objectclass" ) == 0 ) )
101 Debug( LDAP_DEBUG_ANY,
102 "%s: line %d: objectclass attribute cannot be mapped\n",
107 mapping = (struct ldapmapping *)ch_calloc( 2,
108 sizeof(struct ldapmapping) );
109 if ( mapping == NULL ) {
110 Debug( LDAP_DEBUG_ANY,
111 "%s: line %d: out of memory\n",
115 ber_str2bv( src, 0, 1, &mapping[0].m_src );
116 ber_str2bv( dst, 0, 1, &mapping[0].m_dst );
117 mapping[1].m_src = mapping[0].m_dst;
118 mapping[1].m_dst = mapping[0].m_src;
120 mapping[0].m_flags = RWMMAP_F_NONE;
121 mapping[1].m_flags = RWMMAP_F_NONE;
127 if ( src[0] != '\0' ) {
128 mapping[0].m_src_oc = oc_bvfind( &mapping[0].m_src );
129 if ( mapping[0].m_src_oc == NULL ) {
130 Debug( LDAP_DEBUG_ANY,
131 "%s: line %d: warning, source objectClass '%s' "
132 "should be defined in schema\n",
133 fname, lineno, src );
136 * FIXME: this should become an err
138 mapping[0].m_src_oc = ch_malloc( sizeof( ObjectClass ) );
139 memset( mapping[0].m_src_oc, 0, sizeof( ObjectClass ) );
140 mapping[0].m_src_oc->soc_cname = mapping[0].m_src;
141 mapping[0].m_flags |= RWMMAP_F_FREE_SRC;
143 mapping[1].m_dst_oc = mapping[0].m_src_oc;
146 mapping[0].m_dst_oc = oc_bvfind( &mapping[0].m_dst );
147 if ( mapping[0].m_dst_oc == NULL ) {
148 Debug( LDAP_DEBUG_ANY,
149 "%s: line %d: warning, destination objectClass '%s' "
150 "is not defined in schema\n",
151 fname, lineno, dst );
153 mapping[0].m_dst_oc = oc_bvfind_undef( &mapping[0].m_dst );
154 if ( mapping[0].m_dst_oc == NULL ) {
155 Debug( LDAP_DEBUG_ANY, "%s: line %d: unable to mimic destination objectClass '%s'\n",
156 fname, lineno, dst );
160 mapping[1].m_src_oc = mapping[0].m_dst_oc;
162 mapping[0].m_flags |= RWMMAP_F_IS_OC;
163 mapping[1].m_flags |= RWMMAP_F_IS_OC;
167 const char *text = NULL;
169 if ( src[0] != '\0' ) {
170 rc = slap_bv2ad( &mapping[0].m_src,
171 &mapping[0].m_src_ad, &text );
172 if ( rc != LDAP_SUCCESS ) {
173 Debug( LDAP_DEBUG_ANY,
174 "%s: line %d: warning, source attributeType '%s' "
175 "should be defined in schema\n",
176 fname, lineno, src );
179 * we create a fake "proxied" ad
183 rc = slap_bv2undef_ad( &mapping[0].m_src,
184 &mapping[0].m_src_ad, &text,
186 if ( rc != LDAP_SUCCESS ) {
188 snprintf( prefix, sizeof(prefix),
189 "%s: line %d: source attributeType '%s': %d",
190 fname, lineno, src, rc );
191 Debug( LDAP_DEBUG_ANY, "%s (%s)\n",
192 prefix, text ? text : "null", 0 );
197 mapping[1].m_dst_ad = mapping[0].m_src_ad;
200 rc = slap_bv2ad( &mapping[0].m_dst, &mapping[0].m_dst_ad, &text );
201 if ( rc != LDAP_SUCCESS ) {
202 Debug( LDAP_DEBUG_ANY,
203 "%s: line %d: warning, destination attributeType '%s' "
204 "is not defined in schema\n",
205 fname, lineno, dst );
207 rc = slap_bv2undef_ad( &mapping[0].m_dst,
208 &mapping[0].m_dst_ad, &text,
210 if ( rc != LDAP_SUCCESS ) {
212 snprintf( prefix, sizeof(prefix),
213 "%s: line %d: destination attributeType '%s': %d",
214 fname, lineno, dst, rc );
215 Debug( LDAP_DEBUG_ANY, "%s (%s)\n",
216 prefix, text ? text : "null", 0 );
220 mapping[1].m_src_ad = mapping[0].m_dst_ad;
223 if ( ( src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, rwm_mapping_cmp ) != NULL)
224 || avl_find( map->remap, (caddr_t)&mapping[1], rwm_mapping_cmp ) != NULL)
226 Debug( LDAP_DEBUG_ANY,
227 "%s: line %d: duplicate mapping found.\n",
229 /* FIXME: free stuff */
233 if ( src[0] != '\0' ) {
234 avl_insert( &map->map, (caddr_t)&mapping[0],
235 rwm_mapping_cmp, rwm_mapping_dup );
237 avl_insert( &map->remap, (caddr_t)&mapping[1],
238 rwm_mapping_cmp, rwm_mapping_dup );
245 rwm_mapping_free( mapping );
252 rwm_suffix_massage_regexize( const char *s )
258 if ( s[0] == '\0' ) {
259 return ch_strdup( "^(.+)$" );
263 ( r = strchr( p, ',' ) ) != NULL;
267 res = ch_calloc( sizeof( char ), strlen( s )
268 + STRLENOF( "((.+),)?" )
269 + STRLENOF( "[ ]?" ) * i
270 + STRLENOF( "$" ) + 1 );
272 ptr = lutil_strcopy( res, "((.+),)?" );
274 ( r = strchr( p, ',' ) ) != NULL;
276 ptr = lutil_strncopy( ptr, p, r - p + 1 );
277 ptr = lutil_strcopy( ptr, "[ ]?" );
279 if ( r[ 1 ] == ' ' ) {
283 ptr = lutil_strcopy( ptr, p );
291 rwm_suffix_massage_patternize( const char *s, const char *p )
298 if ( s[ 0 ] == '\0' ) {
302 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
307 ptr = lutil_strcopy( res, ( p[0] == '\0' ? "%2" : "%1" ) );
308 if ( s[ 0 ] == '\0' ) {
312 lutil_strcopy( ptr, p );
318 rwm_suffix_massage_config(
319 struct rewrite_info *info,
329 rargv[ 0 ] = "rewriteEngine";
332 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
334 rargv[ 0 ] = "rewriteContext";
335 rargv[ 1 ] = "default";
337 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
339 rargv[ 0 ] = "rewriteRule";
340 rargv[ 1 ] = rwm_suffix_massage_regexize( pvnc->bv_val );
341 rargv[ 2 ] = rwm_suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
344 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
345 ch_free( rargv[ 1 ] );
346 ch_free( rargv[ 2 ] );
348 if ( BER_BVISEMPTY( pvnc ) ) {
349 rargv[ 0 ] = "rewriteRule";
351 rargv[ 2 ] = prnc->bv_val;
354 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
357 rargv[ 0 ] = "rewriteContext";
358 rargv[ 1 ] = "searchEntryDN";
360 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
362 rargv[ 0 ] = "rewriteRule";
363 rargv[ 1 ] = rwm_suffix_massage_regexize( prnc->bv_val );
364 rargv[ 2 ] = rwm_suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
367 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
368 ch_free( rargv[ 1 ] );
369 ch_free( rargv[ 2 ] );
371 if ( BER_BVISEMPTY( prnc ) ) {
372 rargv[ 0 ] = "rewriteRule";
374 rargv[ 2 ] = pvnc->bv_val;
377 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
380 rargv[ 0 ] = "rewriteContext";
381 rargv[ 1 ] = "matchedDN";
382 rargv[ 2 ] = "alias";
383 rargv[ 3 ] = "searchEntryDN";
385 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
387 #ifdef RWM_REFERRAL_REWRITE
388 /* FIXME: we don't want this on by default, do we? */
389 rargv[ 0 ] = "rewriteContext";
390 rargv[ 1 ] = "referralDN";
391 rargv[ 2 ] = "alias";
392 rargv[ 3 ] = "searchEntryDN";
394 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
395 #else /* ! RWM_REFERRAL_REWRITE */
396 rargv[ 0 ] = "rewriteContext";
397 rargv[ 1 ] = "referralAttrDN";
399 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
401 rargv[ 0 ] = "rewriteContext";
402 rargv[ 1 ] = "referralDN";
404 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
405 #endif /* ! RWM_REFERRAL_REWRITE */
407 rargv[ 0 ] = "rewriteContext";
408 rargv[ 1 ] = "searchAttrDN";
409 rargv[ 2 ] = "alias";
410 rargv[ 3 ] = "searchEntryDN";
412 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
417 #endif /* SLAPD_OVER_RWM */