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 ) {
116 LDAP_LOG( OPERATION, CRIT,
117 "referral_dn_muck: SLAP_MALLOC failed\n", 0, 0, 0 );
119 Debug( LDAP_DEBUG_ANY,
120 "referral_dn_muck: SLAP_MALLOC failed\n", 0, 0, 0 );
125 strncpy( muck.bv_val, ntargetDN.bv_val,
126 ntargetDN.bv_len-nbaseDN.bv_len );
127 strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
130 ch_free( nrefDN.bv_val );
131 ch_free( nbaseDN.bv_val );
132 ch_free( ntargetDN.bv_val );
138 ch_free( nrefDN.bv_val );
139 return ntargetDN.bv_val;
143 /* validate URL for global referral use
144 * LDAP URLs must not have:
145 * DN, attrs, scope, nor filter
146 * Any non-LDAP URL is okay
148 * XXYYZ: should return an error string
150 int validate_global_referral( const char *url )
155 rc = ldap_url_parse_ext( url, &lurl );
158 case LDAP_URL_SUCCESS:
161 case LDAP_URL_ERR_BADSCHEME:
162 /* not LDAP hence valid */
166 /* other error, bail */
168 LDAP_LOG( CONFIG, CRIT,
169 "referral: invalid URL (%s): %s (%d)\n",
170 url, "" /* ldap_url_error2str(rc) */, rc );
172 Debug( LDAP_DEBUG_ANY,
173 "referral: invalid URL (%s): %s (%d)\n",
174 url, "" /* ldap_url_error2str(rc) */, rc );
181 if( lurl->lud_dn && *lurl->lud_dn ) {
183 LDAP_LOG( CONFIG, CRIT, "referral: URL (%s): contains DN\n", url, 0, 0 );
185 Debug( LDAP_DEBUG_ANY,
186 "referral: URL (%s): contains DN\n",
191 } else if( lurl->lud_attrs ) {
193 LDAP_LOG( CONFIG, CRIT,
194 "referral: URL (%s): requests attributes\n", url, 0, 0 );
196 Debug( LDAP_DEBUG_ANY,
197 "referral: URL (%s): requests attributes\n",
202 } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
204 LDAP_LOG( CONFIG, CRIT,
205 "referral: URL (%s): contains explicit scope\n", url, 0, 0 );
207 Debug( LDAP_DEBUG_ANY,
208 "referral: URL (%s): contains explicit scope\n",
213 } else if( lurl->lud_filter ) {
215 LDAP_LOG( CONFIG, CRIT,
216 "referral: URL (%s): contains explicit filter\n", url, 0, 0 );
218 Debug( LDAP_DEBUG_ANY,
219 "referral: URL (%s): contains explicit filter\n",
225 ldap_free_urldesc( lurl );
229 BerVarray referral_rewrite(
232 struct berval *target,
237 struct berval *iv, *jv;
239 if( in == NULL ) return NULL;
241 for( i=0; in[i].bv_val != NULL ; i++ ) {
242 /* just count them */
245 if( i < 1 ) return NULL;
247 refs = SLAP_MALLOC( (i+1) * sizeof( struct berval ) );
250 LDAP_LOG( OPERATION, CRIT,
251 "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
253 Debug( LDAP_DEBUG_ANY,
254 "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
259 for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
261 int rc = ldap_url_parse_ext( iv->bv_val, &url );
263 if( rc == LDAP_URL_ERR_BADSCHEME ) {
264 ber_dupbv( jv++, iv );
267 } else if( rc != LDAP_URL_SUCCESS ) {
272 char *dn = url->lud_dn;
273 url->lud_dn = referral_dn_muck(
274 ( dn && *dn ) ? dn : NULL,
280 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
281 url->lud_scope = scope;
284 jv->bv_val = ldap_url_desc2str( url );
285 jv->bv_len = strlen( jv->bv_val );
287 ldap_free_urldesc( url );
303 BerVarray get_entry_referrals(
310 struct berval *iv, *jv;
312 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
314 attr = attr_find( e->e_attrs, ad_ref );
316 if( attr == NULL ) return NULL;
318 for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
319 /* count references */
322 if( i < 1 ) return NULL;
324 refs = SLAP_MALLOC( (i + 1) * sizeof(struct berval));
327 LDAP_LOG( OPERATION, CRIT,
328 "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
330 Debug( LDAP_DEBUG_ANY,
331 "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
336 for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
341 for( k=0; k<jv->bv_len; k++ ) {
342 if( isspace( (unsigned char) jv->bv_val[k] ) ) {
343 jv->bv_val[k] = '\0';
349 if( jv->bv_len > 0 ) {
364 /* we should check that a referral value exists... */
376 AttributeDescription *aliasedObjectName
377 = slap_schema.si_ad_aliasedObjectName;
379 a = attr_find( e->e_attrs, aliasedObjectName );
383 * there was an aliasedobjectname defined but no data.
385 *err = LDAP_ALIAS_PROBLEM;
386 *text = "alias missing aliasedObjectName attribute";
391 * aliasedObjectName should be SINGLE-VALUED with a single value.
393 if ( a->a_vals[0].bv_val == NULL ) {
395 * there was an aliasedobjectname defined but no data.
397 *err = LDAP_ALIAS_PROBLEM;
398 *text = "alias missing aliasedObjectName value";
402 if( a->a_nvals[1].bv_val != NULL ) {
403 *err = LDAP_ALIAS_PROBLEM;
404 *text = "alias has multivalued aliasedObjectName";
408 *ndn = a->a_nvals[0];