1 /* referral.c - muck with referrals */
4 * Copyright 1998-2003 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(
29 struct berval * baseDN,
30 struct berval * 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->bv_val ) : NULL;
44 bvin.bv_val = (char *)refDN;
45 bvin.bv_len = strlen( refDN );
47 rc = dnPretty2( NULL, &bvin, &nrefDN, NULL );
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->bv_val );
62 rc = dnPretty2( NULL, targetDN, &ntargetDN, NULL );
63 if( rc != LDAP_SUCCESS ) {
64 /* Invalid targetDN */
65 ch_free( nrefDN.bv_val );
70 rc = dnPretty2( NULL, baseDN, &nbaseDN, NULL );
71 if( rc != LDAP_SUCCESS ) {
73 ch_free( nrefDN.bv_val );
74 ch_free( ntargetDN.bv_val );
78 if( dn_match( &nbaseDN, &nrefDN ) ) {
79 ch_free( nrefDN.bv_val );
80 ch_free( nbaseDN.bv_val );
81 return ntargetDN.bv_val;
87 if( ntargetDN.bv_len < nbaseDN.bv_len ) {
88 ch_free( nrefDN.bv_val );
89 ch_free( nbaseDN.bv_val );
90 return ntargetDN.bv_val;
94 &ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
97 /* target not subordinate to base */
98 ch_free( nrefDN.bv_val );
99 ch_free( nbaseDN.bv_val );
100 return ntargetDN.bv_val;
103 muck.bv_len = ntargetDN.bv_len + nrefDN.bv_len - nbaseDN.bv_len;
104 muck.bv_val = SLAP_MALLOC( muck.bv_len + 1 );
105 if( muck.bv_val == NULL ) {
107 LDAP_LOG( OPERATION, CRIT,
108 "referral_dn_muck: SLAP_MALLOC failed\n", 0, 0, 0 );
110 Debug( LDAP_DEBUG_ANY,
111 "referral_dn_muck: SLAP_MALLOC failed\n", 0, 0, 0 );
116 strncpy( muck.bv_val, ntargetDN.bv_val,
117 ntargetDN.bv_len-nbaseDN.bv_len );
118 strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
121 ch_free( nrefDN.bv_val );
122 ch_free( nbaseDN.bv_val );
123 ch_free( ntargetDN.bv_val );
129 ch_free( nrefDN.bv_val );
130 return ntargetDN.bv_val;
134 /* validate URL for global referral use
135 * LDAP URLs must not have:
136 * DN, attrs, scope, nor filter
137 * Any non-LDAP URL is okay
139 * XXYYZ: should return an error string
141 int validate_global_referral( const char *url )
146 rc = ldap_url_parse_ext( url, &lurl );
149 case LDAP_URL_SUCCESS:
152 case LDAP_URL_ERR_BADSCHEME:
153 /* not LDAP hence valid */
157 /* other error, bail */
159 LDAP_LOG( CONFIG, CRIT,
160 "referral: invalid URL (%s): %s (%d)\n",
161 url, "" /* ldap_url_error2str(rc) */, rc );
163 Debug( LDAP_DEBUG_ANY,
164 "referral: invalid URL (%s): %s (%d)\n",
165 url, "" /* ldap_url_error2str(rc) */, rc );
172 if( lurl->lud_dn && *lurl->lud_dn ) {
174 LDAP_LOG( CONFIG, CRIT, "referral: URL (%s): contains DN\n", url, 0, 0 );
176 Debug( LDAP_DEBUG_ANY,
177 "referral: URL (%s): contains DN\n",
182 } else if( lurl->lud_attrs ) {
184 LDAP_LOG( CONFIG, CRIT,
185 "referral: URL (%s): requests attributes\n", url, 0, 0 );
187 Debug( LDAP_DEBUG_ANY,
188 "referral: URL (%s): requests attributes\n",
193 } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
195 LDAP_LOG( CONFIG, CRIT,
196 "referral: URL (%s): contains explicit scope\n", url, 0, 0 );
198 Debug( LDAP_DEBUG_ANY,
199 "referral: URL (%s): contains explicit scope\n",
204 } else if( lurl->lud_filter ) {
206 LDAP_LOG( CONFIG, CRIT,
207 "referral: URL (%s): contains explicit filter\n", url, 0, 0 );
209 Debug( LDAP_DEBUG_ANY,
210 "referral: URL (%s): contains explicit filter\n",
216 ldap_free_urldesc( lurl );
220 BerVarray referral_rewrite(
223 struct berval *target,
228 struct berval *iv, *jv;
230 if( in == NULL ) return NULL;
232 for( i=0; in[i].bv_val != NULL ; i++ ) {
233 /* just count them */
236 if( i < 1 ) return NULL;
238 refs = SLAP_MALLOC( (i+1) * sizeof( struct berval ) );
241 LDAP_LOG( OPERATION, CRIT,
242 "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
244 Debug( LDAP_DEBUG_ANY,
245 "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
250 for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
252 int rc = ldap_url_parse_ext( iv->bv_val, &url );
254 if( rc == LDAP_URL_ERR_BADSCHEME ) {
255 ber_dupbv( jv++, iv );
258 } else if( rc != LDAP_URL_SUCCESS ) {
263 char *dn = url->lud_dn;
264 url->lud_dn = referral_dn_muck(
265 ( dn && *dn ) ? dn : NULL,
271 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
272 url->lud_scope = scope;
275 jv->bv_val = ldap_url_desc2str( url );
276 jv->bv_len = strlen( jv->bv_val );
278 ldap_free_urldesc( url );
294 BerVarray get_entry_referrals(
301 struct berval *iv, *jv;
303 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
305 attr = attr_find( e->e_attrs, ad_ref );
307 if( attr == NULL ) return NULL;
309 for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
310 /* count references */
313 if( i < 1 ) return NULL;
315 refs = SLAP_MALLOC( (i + 1) * sizeof(struct berval));
318 LDAP_LOG( OPERATION, CRIT,
319 "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
321 Debug( LDAP_DEBUG_ANY,
322 "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
327 for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
332 for( k=0; k<jv->bv_len; k++ ) {
333 if( isspace( (unsigned char) jv->bv_val[k] ) ) {
334 jv->bv_val[k] = '\0';
340 if( jv->bv_len > 0 ) {
355 /* we should check that a referral value exists... */
367 AttributeDescription *aliasedObjectName
368 = slap_schema.si_ad_aliasedObjectName;
370 a = attr_find( e->e_attrs, aliasedObjectName );
374 * there was an aliasedobjectname defined but no data.
376 *err = LDAP_ALIAS_PROBLEM;
377 *text = "alias missing aliasedObjectName attribute";
382 * aliasedObjectName should be SINGLE-VALUED with a single value.
384 if ( a->a_vals[0].bv_val == NULL ) {
386 * there was an aliasedobjectname defined but no data.
388 *err = LDAP_ALIAS_PROBLEM;
389 *text = "alias missing aliasedObjectName value";
393 if( a->a_nvals[1].bv_val != NULL ) {
394 *err = LDAP_ALIAS_PROBLEM;
395 *text = "alias has multivalued aliasedObjectName";
399 *ndn = a->a_nvals[0];