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 ) ) {
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, 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, CRIT, "referral: URL (%s): contains DN\n", url, 0, 0 );
172 Debug( LDAP_DEBUG_ANY,
173 "referral: URL (%s): contains DN\n",
178 } else if( lurl->lud_attrs ) {
180 LDAP_LOG( CONFIG, CRIT,
181 "referral: URL (%s): requests attributes\n", url, 0, 0 );
183 Debug( LDAP_DEBUG_ANY,
184 "referral: URL (%s): requests attributes\n",
189 } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
191 LDAP_LOG( CONFIG, CRIT,
192 "referral: URL (%s): contains explicit scope\n", url, 0, 0 );
194 Debug( LDAP_DEBUG_ANY,
195 "referral: URL (%s): contains explicit scope\n",
200 } else if( lurl->lud_filter ) {
202 LDAP_LOG( CONFIG, CRIT,
203 "referral: URL (%s): contains explicit filter\n", url, 0, 0 );
205 Debug( LDAP_DEBUG_ANY,
206 "referral: URL (%s): contains explicit filter\n",
212 ldap_free_urldesc( lurl );
216 BerVarray referral_rewrite(
219 struct berval *target,
224 struct berval *iv, *jv;
226 if( in == NULL ) return NULL;
228 for( i=0; in[i].bv_val != NULL ; i++ ) {
229 /* just count them */
232 if( i < 1 ) return NULL;
234 refs = ch_malloc( (i+1) * sizeof( struct berval ) );
236 for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
238 int rc = ldap_url_parse_ext( iv->bv_val, &url );
240 if( rc == LDAP_URL_ERR_BADSCHEME ) {
241 ber_dupbv( jv++, iv );
244 } else if( rc != LDAP_URL_SUCCESS ) {
249 char *dn = url->lud_dn;
250 url->lud_dn = referral_dn_muck(
251 ( dn && *dn ) ? dn : NULL,
252 base ? base->bv_val : NULL,
253 target ? target->bv_val : NULL );
258 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
259 url->lud_scope = scope;
262 jv->bv_val = ldap_url_desc2str( url );
263 jv->bv_len = strlen( jv->bv_val );
265 ldap_free_urldesc( url );
281 BerVarray get_entry_referrals(
290 struct berval *iv, *jv;
292 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
294 attr = attr_find( e->e_attrs, ad_ref );
296 if( attr == NULL ) return NULL;
298 for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
299 /* count references */
302 if( i < 1 ) return NULL;
304 refs = ch_malloc( (i + 1) * sizeof(struct berval));
306 for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
311 for( k=0; k<jv->bv_len; k++ ) {
312 if( isspace( (unsigned char) jv->bv_val[k] ) ) {
313 jv->bv_val[k] = '\0';
319 if( jv->bv_len > 0 ) {
334 /* we should check that a referral value exists... */