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(
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 );
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 );
63 if( rc != LDAP_SUCCESS ) {
64 /* Invalid targetDN */
65 ch_free( nrefDN.bv_val );
70 rc = dnPretty2( NULL, baseDN, &nbaseDN );
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 = ch_malloc( muck.bv_len + 1 );
106 strncpy( muck.bv_val, ntargetDN.bv_val,
107 ntargetDN.bv_len-nbaseDN.bv_len );
108 strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
111 ch_free( nrefDN.bv_val );
112 ch_free( nbaseDN.bv_val );
113 ch_free( ntargetDN.bv_val );
119 ch_free( nrefDN.bv_val );
120 return ntargetDN.bv_val;
124 /* validate URL for global referral use
125 * LDAP URLs must not have:
126 * DN, attrs, scope, nor filter
127 * Any non-LDAP URL is okay
129 * XXYYZ: should return an error string
131 int validate_global_referral( const char *url )
136 rc = ldap_url_parse_ext( url, &lurl );
139 case LDAP_URL_SUCCESS:
142 case LDAP_URL_ERR_BADSCHEME:
143 /* not LDAP hence valid */
147 /* other error, bail */
149 LDAP_LOG( CONFIG, CRIT,
150 "referral: invalid URL (%s): %s (%d)\n",
151 url, "" /* ldap_url_error2str(rc) */, rc );
153 Debug( LDAP_DEBUG_ANY,
154 "referral: invalid URL (%s): %s (%d)\n",
155 url, "" /* ldap_url_error2str(rc) */, rc );
162 if( lurl->lud_dn && *lurl->lud_dn ) {
164 LDAP_LOG( CONFIG, CRIT, "referral: URL (%s): contains DN\n", url, 0, 0 );
166 Debug( LDAP_DEBUG_ANY,
167 "referral: URL (%s): contains DN\n",
172 } else if( lurl->lud_attrs ) {
174 LDAP_LOG( CONFIG, CRIT,
175 "referral: URL (%s): requests attributes\n", url, 0, 0 );
177 Debug( LDAP_DEBUG_ANY,
178 "referral: URL (%s): requests attributes\n",
183 } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
185 LDAP_LOG( CONFIG, CRIT,
186 "referral: URL (%s): contains explicit scope\n", url, 0, 0 );
188 Debug( LDAP_DEBUG_ANY,
189 "referral: URL (%s): contains explicit scope\n",
194 } else if( lurl->lud_filter ) {
196 LDAP_LOG( CONFIG, CRIT,
197 "referral: URL (%s): contains explicit filter\n", url, 0, 0 );
199 Debug( LDAP_DEBUG_ANY,
200 "referral: URL (%s): contains explicit filter\n",
206 ldap_free_urldesc( lurl );
210 BerVarray referral_rewrite(
213 struct berval *target,
218 struct berval *iv, *jv;
220 if( in == NULL ) return NULL;
222 for( i=0; in[i].bv_val != NULL ; i++ ) {
223 /* just count them */
226 if( i < 1 ) return NULL;
228 refs = ch_malloc( (i+1) * sizeof( struct berval ) );
230 for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
232 int rc = ldap_url_parse_ext( iv->bv_val, &url );
234 if( rc == LDAP_URL_ERR_BADSCHEME ) {
235 ber_dupbv( jv++, iv );
238 } else if( rc != LDAP_URL_SUCCESS ) {
243 char *dn = url->lud_dn;
244 url->lud_dn = referral_dn_muck(
245 ( dn && *dn ) ? dn : NULL,
251 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
252 url->lud_scope = scope;
255 jv->bv_val = ldap_url_desc2str( url );
256 jv->bv_len = strlen( jv->bv_val );
258 ldap_free_urldesc( url );
274 BerVarray get_entry_referrals(
283 struct berval *iv, *jv;
285 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
287 attr = attr_find( e->e_attrs, ad_ref );
289 if( attr == NULL ) return NULL;
291 for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
292 /* count references */
295 if( i < 1 ) return NULL;
297 refs = ch_malloc( (i + 1) * sizeof(struct berval));
299 for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
304 for( k=0; k<jv->bv_len; k++ ) {
305 if( isspace( (unsigned char) jv->bv_val[k] ) ) {
306 jv->bv_val[k] = '\0';
312 if( jv->bv_len > 0 ) {
327 /* we should check that a referral value exists... */