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/signal.h>
15 #include <ac/string.h>
18 #include <ac/unistd.h>
25 * This routine generates the DN appropriate to return in
28 static char * referral_dn_muck(
31 const char * targetDN )
35 struct berval nrefDN = { 0, NULL };
36 struct berval nbaseDN = { 0, NULL };
37 struct berval ntargetDN = { 0, NULL };
40 /* no base, return target */
41 return targetDN ? ch_strdup( targetDN ) : NULL;
45 bvin.bv_val = (char *)refDN;
46 bvin.bv_len = strlen( refDN );
48 rc = dnPretty2( NULL, &bvin, &nrefDN );
49 if( rc != LDAP_SUCCESS ) {
56 /* continuation reference
57 * if refDN present return refDN
60 return nrefDN.bv_len ? nrefDN.bv_val : ch_strdup( baseDN );
63 bvin.bv_val = (char *)targetDN;
64 bvin.bv_len = strlen( targetDN );
66 rc = dnPretty2( NULL, &bvin, &ntargetDN );
67 if( rc != LDAP_SUCCESS ) {
68 /* Invalid targetDN */
69 ch_free( nrefDN.bv_val );
74 bvin.bv_val = (char *)baseDN;
75 bvin.bv_len = strlen( baseDN );
77 rc = dnPretty2( NULL, &bvin, &nbaseDN );
78 if( rc != LDAP_SUCCESS ) {
80 ch_free( nrefDN.bv_val );
81 ch_free( ntargetDN.bv_val );
85 if( dn_match( &nbaseDN, &nrefDN ) == 0 ) {
86 ch_free( nrefDN.bv_val );
87 ch_free( nbaseDN.bv_val );
88 return ntargetDN.bv_val;
94 if( ntargetDN.bv_len < nbaseDN.bv_len ) {
95 ch_free( nrefDN.bv_val );
96 ch_free( nbaseDN.bv_val );
97 return ntargetDN.bv_val;
101 &ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
104 /* target not subordinate to base */
105 ch_free( nrefDN.bv_val );
106 ch_free( nbaseDN.bv_val );
107 return ntargetDN.bv_val;
110 muck.bv_len = ntargetDN.bv_len + nrefDN.bv_len - nbaseDN.bv_len;
111 muck.bv_val = ch_malloc( muck.bv_len + 1 );
113 strncpy( muck.bv_val, ntargetDN.bv_val,
114 ntargetDN.bv_len-nbaseDN.bv_len );
115 strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
118 ch_free( nrefDN.bv_val );
119 ch_free( nbaseDN.bv_val );
120 ch_free( ntargetDN.bv_val );
126 ch_free( nrefDN.bv_val );
127 return ntargetDN.bv_val;
131 /* validate URL for global referral use
132 * LDAP URLs must not have:
133 * DN, attrs, scope, nor filter
134 * Any non-LDAP URL is okay
136 * XXYYZ: should return an error string
138 int validate_global_referral( const char *url )
143 rc = ldap_url_parse_ext( url, &lurl );
146 case LDAP_URL_SUCCESS:
149 case LDAP_URL_ERR_BADSCHEME:
150 /* not LDAP hence valid */
154 /* other error, bail */
156 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
157 "referral: invalid URL (%s): %s (%d)\n",
158 url, "" /* ldap_url_error2str(rc) */, rc ));
160 Debug( LDAP_DEBUG_ANY,
161 "referral: invalid URL (%s): %s (%d)\n",
162 url, "" /* ldap_url_error2str(rc) */, rc );
169 if( lurl->lud_dn && *lurl->lud_dn ) {
171 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
172 "referral: URL (%s): contains DN\n",
175 Debug( LDAP_DEBUG_ANY,
176 "referral: URL (%s): contains DN\n",
181 } else if( lurl->lud_attrs ) {
183 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
184 "referral: URL (%s): requests attributes\n",
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", LDAP_LEVEL_CRIT,
196 "referral: URL (%s): contains explicit scope\n",
199 Debug( LDAP_DEBUG_ANY,
200 "referral: URL (%s): contains explicit scope\n",
205 } else if( lurl->lud_filter ) {
207 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
208 "referral: URL (%s): contains explicit filter\n",
211 Debug( LDAP_DEBUG_ANY,
212 "referral: URL (%s): contains explicit filter\n",
218 ldap_free_urldesc( lurl );
222 BerVarray referral_rewrite(
225 struct berval *target,
230 struct berval *iv, *jv;
232 if( in == NULL ) return NULL;
234 for( i=0; in[i].bv_val != NULL ; i++ ) {
235 /* just count them */
238 if( i < 1 ) return NULL;
240 refs = ch_malloc( (i+1) * sizeof( struct berval ) );
242 for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
244 int rc = ldap_url_parse_ext( iv->bv_val, &url );
246 if( rc == LDAP_URL_ERR_BADSCHEME ) {
247 ber_dupbv( jv++, iv );
250 } else if( rc != LDAP_URL_SUCCESS ) {
255 char *dn = url->lud_dn;
256 url->lud_dn = referral_dn_muck(
257 ( dn && *dn ) ? dn : NULL,
258 base ? base->bv_val : NULL,
259 target ? target->bv_val : NULL );
264 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
265 url->lud_scope = scope;
268 jv->bv_val = ldap_url_desc2str( url );
269 jv->bv_len = strlen( jv->bv_val );
271 ldap_free_urldesc( url );
287 BerVarray get_entry_referrals(
296 struct berval *iv, *jv;
298 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
300 attr = attr_find( e->e_attrs, ad_ref );
302 if( attr == NULL ) return NULL;
304 for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
305 /* count references */
308 if( i < 1 ) return NULL;
310 refs = ch_malloc( (i + 1) * sizeof(struct berval));
312 for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
317 for( k=0; k<jv->bv_len; k++ ) {
318 if( isspace(jv->bv_val[k]) ) {
319 jv->bv_val[k] = '\0';
325 if( jv->bv_len > 0 ) {
340 /* we should check that a referral value exists... */