1 /* referral.c - muck with referrals */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/socket.h>
23 #include <ac/string.h>
26 #include <ac/unistd.h>
33 * This routine generates the DN appropriate to return in
36 static char * referral_dn_muck(
38 struct berval * baseDN,
39 struct berval * targetDN )
43 struct berval nrefDN = BER_BVNULL;
44 struct berval nbaseDN = BER_BVNULL;
45 struct berval ntargetDN = BER_BVNULL;
48 /* no base, return target */
49 return targetDN ? ch_strdup( targetDN->bv_val ) : NULL;
53 bvin.bv_val = (char *)refDN;
54 bvin.bv_len = strlen( refDN );
56 rc = dnPretty( NULL, &bvin, &nrefDN, NULL );
57 if( rc != LDAP_SUCCESS ) {
64 /* continuation reference
65 * if refDN present return refDN
68 return nrefDN.bv_len ? nrefDN.bv_val : ch_strdup( baseDN->bv_val );
71 rc = dnPretty( NULL, targetDN, &ntargetDN, NULL );
72 if( rc != LDAP_SUCCESS ) {
73 /* Invalid targetDN */
74 ch_free( nrefDN.bv_val );
79 rc = dnPretty( NULL, baseDN, &nbaseDN, NULL );
80 if( rc != LDAP_SUCCESS ) {
82 ch_free( nrefDN.bv_val );
83 ch_free( ntargetDN.bv_val );
87 if( dn_match( &nbaseDN, &nrefDN ) ) {
88 ch_free( nrefDN.bv_val );
89 ch_free( nbaseDN.bv_val );
90 return ntargetDN.bv_val;
96 if( ntargetDN.bv_len < nbaseDN.bv_len ) {
97 ch_free( nrefDN.bv_val );
98 ch_free( nbaseDN.bv_val );
99 return ntargetDN.bv_val;
103 &ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
106 /* target not subordinate to base */
107 ch_free( nrefDN.bv_val );
108 ch_free( nbaseDN.bv_val );
109 return ntargetDN.bv_val;
112 muck.bv_len = ntargetDN.bv_len + nrefDN.bv_len - nbaseDN.bv_len;
113 muck.bv_val = SLAP_MALLOC( muck.bv_len + 1 );
114 if( muck.bv_val == NULL ) {
115 Debug( LDAP_DEBUG_ANY,
116 "referral_dn_muck: SLAP_MALLOC failed\n", 0, 0, 0 );
120 strncpy( muck.bv_val, ntargetDN.bv_val,
121 ntargetDN.bv_len-nbaseDN.bv_len );
122 strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
125 ch_free( nrefDN.bv_val );
126 ch_free( nbaseDN.bv_val );
127 ch_free( ntargetDN.bv_val );
133 ch_free( nrefDN.bv_val );
134 return ntargetDN.bv_val;
138 /* validate URL for global referral use
139 * LDAP URLs must not have:
140 * DN, attrs, scope, nor filter
141 * Any non-LDAP URL is okay
143 * XXYYZ: should return an error string
145 int validate_global_referral( const char *url )
150 rc = ldap_url_parse_ext( url, &lurl );
153 case LDAP_URL_SUCCESS:
156 case LDAP_URL_ERR_BADSCHEME:
157 /* not LDAP hence valid */
161 /* other error, bail */
162 Debug( LDAP_DEBUG_ANY,
163 "referral: invalid URL (%s): %s (%d)\n",
164 url, "" /* ldap_url_error2str(rc) */, rc );
170 if( lurl->lud_dn && *lurl->lud_dn ) {
171 Debug( LDAP_DEBUG_ANY,
172 "referral: URL (%s): contains DN\n",
176 } else if( lurl->lud_attrs ) {
177 Debug( LDAP_DEBUG_ANY,
178 "referral: URL (%s): requests attributes\n",
182 } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
183 Debug( LDAP_DEBUG_ANY,
184 "referral: URL (%s): contains explicit scope\n",
188 } else if( lurl->lud_filter ) {
189 Debug( LDAP_DEBUG_ANY,
190 "referral: URL (%s): contains explicit filter\n",
195 ldap_free_urldesc( lurl );
199 BerVarray referral_rewrite(
202 struct berval *target,
207 struct berval *iv, *jv;
209 if( in == NULL ) return NULL;
211 for( i=0; in[i].bv_val != NULL ; i++ ) {
212 /* just count them */
215 if( i < 1 ) return NULL;
217 refs = SLAP_MALLOC( (i+1) * sizeof( struct berval ) );
219 Debug( LDAP_DEBUG_ANY,
220 "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
224 for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
226 int rc = ldap_url_parse_ext( iv->bv_val, &url );
228 if( rc == LDAP_URL_ERR_BADSCHEME ) {
229 ber_dupbv( jv++, iv );
232 } else if( rc != LDAP_URL_SUCCESS ) {
237 char *dn = url->lud_dn;
238 url->lud_dn = referral_dn_muck(
239 ( dn && *dn ) ? dn : NULL,
245 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
246 url->lud_scope = scope;
249 jv->bv_val = ldap_url_desc2str( url );
250 jv->bv_len = strlen( jv->bv_val );
252 ldap_free_urldesc( url );
268 BerVarray get_entry_referrals(
275 struct berval *iv, *jv;
277 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
279 attr = attr_find( e->e_attrs, ad_ref );
281 if( attr == NULL ) return NULL;
283 for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
284 /* count references */
287 if( i < 1 ) return NULL;
289 refs = SLAP_MALLOC( (i + 1) * sizeof(struct berval));
291 Debug( LDAP_DEBUG_ANY,
292 "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
296 for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
301 for( k=0; k<jv->bv_len; k++ ) {
302 if( isspace( (unsigned char) jv->bv_val[k] ) ) {
303 jv->bv_val[k] = '\0';
309 if( jv->bv_len > 0 ) {
324 /* we should check that a referral value exists... */
336 AttributeDescription *aliasedObjectName
337 = slap_schema.si_ad_aliasedObjectName;
339 a = attr_find( e->e_attrs, aliasedObjectName );
343 * there was an aliasedobjectname defined but no data.
345 *err = LDAP_ALIAS_PROBLEM;
346 *text = "alias missing aliasedObjectName attribute";
351 * aliasedObjectName should be SINGLE-VALUED with a single value.
353 if ( a->a_vals[0].bv_val == NULL ) {
355 * there was an aliasedobjectname defined but no data.
357 *err = LDAP_ALIAS_PROBLEM;
358 *text = "alias missing aliasedObjectName value";
362 if( a->a_nvals[1].bv_val != NULL ) {
363 *err = LDAP_ALIAS_PROBLEM;
364 *text = "alias has multivalued aliasedObjectName";
368 *ndn = a->a_nvals[0];