1 /* referral.c - muck with referrals */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/string.h>
17 #include <ac/unistd.h>
24 * This routine generates the DN appropriate to return in
27 static char * referral_dn_muck(
30 const char * targetDN )
34 struct berval nrefDN = { 0, NULL };
35 struct berval nbaseDN = { 0, NULL };
36 struct berval ntargetDN = { 0, NULL };
39 /* no base, return target */
40 return targetDN ? ch_strdup( targetDN ) : NULL;
44 bvin.bv_val = (char *)refDN;
45 bvin.bv_len = strlen( refDN );
47 rc = dnPretty2( NULL, &bvin, &nrefDN );
48 if( rc != LDAP_SUCCESS ) {
55 /* continuation reference
56 * if refDN present return refDN
59 return nrefDN.bv_len ? nrefDN.bv_val : ch_strdup( baseDN );
62 bvin.bv_val = (char *)targetDN;
63 bvin.bv_len = strlen( targetDN );
65 rc = dnPretty2( NULL, &bvin, &ntargetDN );
66 if( rc != LDAP_SUCCESS ) {
67 /* Invalid targetDN */
68 ch_free( nrefDN.bv_val );
73 bvin.bv_val = (char *)baseDN;
74 bvin.bv_len = strlen( baseDN );
76 rc = dnPretty2( NULL, &bvin, &nbaseDN );
77 if( rc != LDAP_SUCCESS ) {
79 ch_free( nrefDN.bv_val );
80 ch_free( ntargetDN.bv_val );
84 if( dn_match( &nbaseDN, &nrefDN ) == 0 ) {
85 ch_free( nrefDN.bv_val );
86 ch_free( nbaseDN.bv_val );
87 return ntargetDN.bv_val;
93 if( ntargetDN.bv_len < nbaseDN.bv_len ) {
94 ch_free( nrefDN.bv_val );
95 ch_free( nbaseDN.bv_val );
96 return ntargetDN.bv_val;
100 &ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
103 /* target not subordinate to base */
104 ch_free( nrefDN.bv_val );
105 ch_free( nbaseDN.bv_val );
106 return ntargetDN.bv_val;
109 muck.bv_len = ntargetDN.bv_len + nrefDN.bv_len - nbaseDN.bv_len;
110 muck.bv_val = ch_malloc( muck.bv_len + 1 );
112 strncpy( muck.bv_val, ntargetDN.bv_val,
113 ntargetDN.bv_len-nbaseDN.bv_len );
114 strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
117 ch_free( nrefDN.bv_val );
118 ch_free( nbaseDN.bv_val );
119 ch_free( ntargetDN.bv_val );
125 ch_free( nrefDN.bv_val );
126 return ntargetDN.bv_val;
130 /* validate URL for global referral use
131 * LDAP URLs must not have:
132 * DN, attrs, scope, nor filter
133 * Any non-LDAP URL is okay
135 * XXYYZ: should return an error string
137 int validate_global_referral( const char *url )
142 rc = ldap_url_parse_ext( url, &lurl );
145 case LDAP_URL_SUCCESS:
148 case LDAP_URL_ERR_BADSCHEME:
149 /* not LDAP hence valid */
153 /* other error, bail */
155 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
156 "referral: invalid URL (%s): %s (%d)\n",
157 url, "" /* ldap_url_error2str(rc) */, rc ));
159 Debug( LDAP_DEBUG_ANY,
160 "referral: invalid URL (%s): %s (%d)\n",
161 url, "" /* ldap_url_error2str(rc) */, rc );
168 if( lurl->lud_dn && *lurl->lud_dn ) {
170 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
171 "referral: URL (%s): contains DN\n",
174 Debug( LDAP_DEBUG_ANY,
175 "referral: URL (%s): contains DN\n",
180 } else if( lurl->lud_attrs ) {
182 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
183 "referral: URL (%s): requests attributes\n",
186 Debug( LDAP_DEBUG_ANY,
187 "referral: URL (%s): requests attributes\n",
192 } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
194 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
195 "referral: URL (%s): contains explicit scope\n",
198 Debug( LDAP_DEBUG_ANY,
199 "referral: URL (%s): contains explicit scope\n",
204 } else if( lurl->lud_filter ) {
206 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
207 "referral: URL (%s): contains explicit filter\n",
210 Debug( LDAP_DEBUG_ANY,
211 "referral: URL (%s): contains explicit filter\n",
217 ldap_free_urldesc( lurl );
221 BerVarray referral_rewrite(
224 struct berval *target,
229 struct berval *iv, *jv;
231 if( in == NULL ) return NULL;
233 for( i=0; in[i].bv_val != NULL ; i++ ) {
234 /* just count them */
237 if( i < 1 ) return NULL;
239 refs = ch_malloc( (i+1) * sizeof( struct berval ) );
241 for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
243 int rc = ldap_url_parse_ext( iv->bv_val, &url );
245 if( rc == LDAP_URL_ERR_BADSCHEME ) {
246 ber_dupbv( jv++, iv );
249 } else if( rc != LDAP_URL_SUCCESS ) {
254 char *dn = url->lud_dn;
255 url->lud_dn = referral_dn_muck(
256 ( dn && *dn ) ? dn : NULL,
257 base ? base->bv_val : NULL,
258 target ? target->bv_val : NULL );
263 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
264 url->lud_scope = scope;
267 jv->bv_val = ldap_url_desc2str( url );
268 jv->bv_len = strlen( jv->bv_val );
270 ldap_free_urldesc( url );
286 BerVarray get_entry_referrals(
295 struct berval *iv, *jv;
297 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
299 attr = attr_find( e->e_attrs, ad_ref );
301 if( attr == NULL ) return NULL;
303 for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
304 /* count references */
307 if( i < 1 ) return NULL;
309 refs = ch_malloc( (i + 1) * sizeof(struct berval));
311 for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
316 for( k=0; k<jv->bv_len; k++ ) {
317 if( isspace(jv->bv_val[k]) ) {
318 jv->bv_val[k] = '\0';
324 if( jv->bv_len > 0 ) {
339 /* we should check that a referral value exists... */