1 /* referral.c - muck with referrals */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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>
31 * This routine generates the DN appropriate to return in
34 static char * referral_dn_muck(
36 struct berval * baseDN,
37 struct berval * targetDN )
41 struct berval nrefDN = BER_BVNULL;
42 struct berval nbaseDN = BER_BVNULL;
43 struct berval ntargetDN = BER_BVNULL;
46 /* no base, return target */
47 return targetDN ? ch_strdup( targetDN->bv_val ) : NULL;
51 bvin.bv_val = (char *)refDN;
52 bvin.bv_len = strlen( refDN );
54 rc = dnPretty( NULL, &bvin, &nrefDN, NULL );
55 if( rc != LDAP_SUCCESS ) {
62 /* continuation reference
63 * if refDN present return refDN
66 return nrefDN.bv_len ? nrefDN.bv_val : ch_strdup( baseDN->bv_val );
69 rc = dnPretty( NULL, targetDN, &ntargetDN, NULL );
70 if( rc != LDAP_SUCCESS ) {
71 /* Invalid targetDN */
72 ch_free( nrefDN.bv_val );
77 rc = dnPretty( NULL, baseDN, &nbaseDN, NULL );
78 if( rc != LDAP_SUCCESS ) {
80 ch_free( nrefDN.bv_val );
81 ch_free( ntargetDN.bv_val );
85 if( dn_match( &nbaseDN, &nrefDN ) ) {
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 = SLAP_MALLOC( muck.bv_len + 1 );
112 if( muck.bv_val == NULL ) {
113 Debug( LDAP_DEBUG_ANY,
114 "referral_dn_muck: SLAP_MALLOC failed\n", 0, 0, 0 );
118 strncpy( muck.bv_val, ntargetDN.bv_val,
119 ntargetDN.bv_len-nbaseDN.bv_len );
120 strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
123 ch_free( nrefDN.bv_val );
124 ch_free( nbaseDN.bv_val );
125 ch_free( ntargetDN.bv_val );
131 ch_free( nrefDN.bv_val );
132 return ntargetDN.bv_val;
136 /* validate URL for global referral use
137 * LDAP URLs must not have:
138 * DN, attrs, scope, nor filter
139 * Any non-LDAP URL is okay
141 * XXYYZ: should return an error string
143 int validate_global_referral( const char *url )
148 rc = ldap_url_parse_ext( url, &lurl );
151 case LDAP_URL_SUCCESS:
154 case LDAP_URL_ERR_BADSCHEME:
155 /* not LDAP hence valid */
159 /* other error, bail */
160 Debug( LDAP_DEBUG_ANY,
161 "referral: invalid URL (%s): %s (%d)\n",
162 url, "" /* ldap_url_error2str(rc) */, rc );
168 if( lurl->lud_dn && *lurl->lud_dn ) {
169 Debug( LDAP_DEBUG_ANY,
170 "referral: URL (%s): contains DN\n",
174 } else if( lurl->lud_attrs ) {
175 Debug( LDAP_DEBUG_ANY,
176 "referral: URL (%s): requests attributes\n",
180 } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
181 Debug( LDAP_DEBUG_ANY,
182 "referral: URL (%s): contains explicit scope\n",
186 } else if( lurl->lud_filter ) {
187 Debug( LDAP_DEBUG_ANY,
188 "referral: URL (%s): contains explicit filter\n",
193 ldap_free_urldesc( lurl );
197 BerVarray referral_rewrite(
200 struct berval *target,
205 struct berval *iv, *jv;
211 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
212 /* just count them */
219 refs = SLAP_MALLOC( ( i + 1 ) * sizeof( struct berval ) );
220 if ( refs == NULL ) {
221 Debug( LDAP_DEBUG_ANY,
222 "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
226 for ( iv = in, jv = refs; !BER_BVISNULL( iv ); iv++ ) {
231 rc = ldap_url_parse_ext( iv->bv_val, &url );
232 if ( rc == LDAP_URL_ERR_BADSCHEME ) {
233 ber_dupbv( jv++, iv );
236 } else if ( rc != LDAP_URL_SUCCESS ) {
241 url->lud_dn = referral_dn_muck( ( 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 if ( jv->bv_val != NULL ) {
251 jv->bv_len = strlen( jv->bv_val );
258 ldap_free_urldesc( url );
273 BerVarray get_entry_referrals(
280 struct berval *iv, *jv;
282 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
284 attr = attr_find( e->e_attrs, ad_ref );
286 if( attr == NULL ) return NULL;
288 for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
289 /* count references */
292 if( i < 1 ) return NULL;
294 refs = SLAP_MALLOC( (i + 1) * sizeof(struct berval));
296 Debug( LDAP_DEBUG_ANY,
297 "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
301 for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
306 for( k=0; k<jv->bv_len; k++ ) {
307 if( isspace( (unsigned char) jv->bv_val[k] ) ) {
308 jv->bv_val[k] = '\0';
314 if( jv->bv_len > 0 ) {
329 /* we should check that a referral value exists... */
341 AttributeDescription *aliasedObjectName
342 = slap_schema.si_ad_aliasedObjectName;
344 a = attr_find( e->e_attrs, aliasedObjectName );
348 * there was an aliasedobjectname defined but no data.
350 *err = LDAP_ALIAS_PROBLEM;
351 *text = "alias missing aliasedObjectName attribute";
356 * aliasedObjectName should be SINGLE-VALUED with a single value.
358 if ( a->a_vals[0].bv_val == NULL ) {
360 * there was an aliasedobjectname defined but no data.
362 *err = LDAP_ALIAS_PROBLEM;
363 *text = "alias missing aliasedObjectName value";
367 if( a->a_nvals[1].bv_val != NULL ) {
368 *err = LDAP_ALIAS_PROBLEM;
369 *text = "alias has multivalued aliasedObjectName";
373 *ndn = a->a_nvals[0];