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 )
36 char *ntargetDN = NULL;
39 /* no base, return target */
40 return targetDN ? ch_strdup( targetDN ) : NULL;
44 nrefDN = dn_validate( tmp = ch_strdup( refDN ) );
53 /* continuation reference
54 * if refDN present return refDN
57 return nrefDN ? nrefDN : ch_strdup( baseDN );
60 ntargetDN = dn_validate( tmp = ch_strdup( targetDN ) );
68 nbaseDN = dn_validate( tmp = ch_strdup( baseDN ) );
77 if( strcasecmp( nbaseDN, nrefDN ) == 0 ) {
85 * FIXME: string based mucking
88 size_t reflen, baselen, targetlen, mucklen;
90 reflen = strlen( nrefDN );
91 baselen = strlen( nbaseDN );
92 targetlen = strlen( ntargetDN );
94 if( targetlen < baselen ) {
100 if( strcasecmp( &ntargetDN[targetlen-baselen], nbaseDN ) ) {
101 /* target not subordinate to base */
107 mucklen = targetlen + reflen - baselen;
108 muck = ch_malloc( 1 + mucklen );
110 strncpy( muck, ntargetDN, targetlen-baselen );
111 strcpy( &muck[targetlen-baselen], nrefDN );
115 ch_free( ntargetDN );
125 /* validate URL for global referral use
126 * LDAP URLs must not have:
127 * DN, attrs, scope, nor filter
128 * Any non-LDAP URL is okay
130 * XXYYZ: should return an error string
132 int validate_global_referral( const char *url )
137 rc = ldap_url_parse_ext( url, &lurl );
140 case LDAP_URL_SUCCESS:
143 case LDAP_URL_ERR_BADSCHEME:
144 /* not LDAP hence valid */
148 /* other error, bail */
150 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
151 "referral: invalid URL (%s): %s (%d)\n",
152 url, "" /* ldap_url_error2str(rc) */, rc ));
154 Debug( LDAP_DEBUG_ANY,
155 "referral: invalid URL (%s): %s (%d)\n",
156 url, "" /* ldap_url_error2str(rc) */, rc );
163 if( lurl->lud_dn && *lurl->lud_dn ) {
165 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
166 "referral: URL (%s): contains DN\n",
169 Debug( LDAP_DEBUG_ANY,
170 "referral: URL (%s): contains DN\n",
175 } else if( lurl->lud_attrs ) {
177 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
178 "referral: URL (%s): requests attributes\n",
181 Debug( LDAP_DEBUG_ANY,
182 "referral: URL (%s): requests attributes\n",
187 } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
189 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
190 "referral: URL (%s): contains explicit scope\n",
193 Debug( LDAP_DEBUG_ANY,
194 "referral: URL (%s): contains explicit scope\n",
199 } else if( lurl->lud_filter ) {
201 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
202 "referral: URL (%s): contains explicit filter\n",
205 Debug( LDAP_DEBUG_ANY,
206 "referral: URL (%s): contains explicit filter\n",
212 ldap_free_urldesc( lurl );
216 BVarray 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 BVarray 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(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... */