referral.c revision 1.2 1 1.2 christos /* $NetBSD: referral.c,v 1.2 2020/08/11 13:15:39 christos Exp $ */
2 1.2 christos
3 1.1 lukem /* referral.c - muck with referrals */
4 1.2 christos /* $OpenLDAP$ */
5 1.1 lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 1.1 lukem *
7 1.2 christos * Copyright 1998-2020 The OpenLDAP Foundation.
8 1.1 lukem * All rights reserved.
9 1.1 lukem *
10 1.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1 lukem * modification, are permitted only as authorized by the OpenLDAP
12 1.1 lukem * Public License.
13 1.1 lukem *
14 1.1 lukem * A copy of this license is available in the file LICENSE in the
15 1.1 lukem * top-level directory of the distribution or, alternatively, at
16 1.1 lukem * <http://www.OpenLDAP.org/license.html>.
17 1.1 lukem */
18 1.1 lukem
19 1.2 christos #include <sys/cdefs.h>
20 1.2 christos __RCSID("$NetBSD: referral.c,v 1.2 2020/08/11 13:15:39 christos Exp $");
21 1.2 christos
22 1.1 lukem #include "portable.h"
23 1.1 lukem
24 1.1 lukem #include <stdio.h>
25 1.1 lukem
26 1.1 lukem #include <ac/socket.h>
27 1.1 lukem #include <ac/errno.h>
28 1.1 lukem #include <ac/string.h>
29 1.1 lukem #include <ac/ctype.h>
30 1.1 lukem #include <ac/time.h>
31 1.1 lukem #include <ac/unistd.h>
32 1.1 lukem
33 1.1 lukem #include "slap.h"
34 1.1 lukem
35 1.1 lukem /*
36 1.1 lukem * This routine generates the DN appropriate to return in
37 1.1 lukem * an LDAP referral.
38 1.1 lukem */
39 1.1 lukem static char * referral_dn_muck(
40 1.1 lukem const char * refDN,
41 1.1 lukem struct berval * baseDN,
42 1.1 lukem struct berval * targetDN )
43 1.1 lukem {
44 1.1 lukem int rc;
45 1.1 lukem struct berval bvin;
46 1.1 lukem struct berval nrefDN = BER_BVNULL;
47 1.1 lukem struct berval nbaseDN = BER_BVNULL;
48 1.1 lukem struct berval ntargetDN = BER_BVNULL;
49 1.1 lukem
50 1.1 lukem if( !baseDN ) {
51 1.1 lukem /* no base, return target */
52 1.1 lukem return targetDN ? ch_strdup( targetDN->bv_val ) : NULL;
53 1.1 lukem }
54 1.1 lukem
55 1.1 lukem if( refDN ) {
56 1.1 lukem bvin.bv_val = (char *)refDN;
57 1.1 lukem bvin.bv_len = strlen( refDN );
58 1.1 lukem
59 1.1 lukem rc = dnPretty( NULL, &bvin, &nrefDN, NULL );
60 1.1 lukem if( rc != LDAP_SUCCESS ) {
61 1.1 lukem /* Invalid refDN */
62 1.1 lukem return NULL;
63 1.1 lukem }
64 1.1 lukem }
65 1.1 lukem
66 1.1 lukem if( !targetDN ) {
67 1.1 lukem /* continuation reference
68 1.1 lukem * if refDN present return refDN
69 1.1 lukem * else return baseDN
70 1.1 lukem */
71 1.1 lukem return nrefDN.bv_len ? nrefDN.bv_val : ch_strdup( baseDN->bv_val );
72 1.1 lukem }
73 1.1 lukem
74 1.1 lukem rc = dnPretty( NULL, targetDN, &ntargetDN, NULL );
75 1.1 lukem if( rc != LDAP_SUCCESS ) {
76 1.1 lukem /* Invalid targetDN */
77 1.1 lukem ch_free( nrefDN.bv_val );
78 1.1 lukem return NULL;
79 1.1 lukem }
80 1.1 lukem
81 1.1 lukem if( nrefDN.bv_len ) {
82 1.1 lukem rc = dnPretty( NULL, baseDN, &nbaseDN, NULL );
83 1.1 lukem if( rc != LDAP_SUCCESS ) {
84 1.1 lukem /* Invalid baseDN */
85 1.1 lukem ch_free( nrefDN.bv_val );
86 1.1 lukem ch_free( ntargetDN.bv_val );
87 1.1 lukem return NULL;
88 1.1 lukem }
89 1.1 lukem
90 1.1 lukem if( dn_match( &nbaseDN, &nrefDN ) ) {
91 1.1 lukem ch_free( nrefDN.bv_val );
92 1.1 lukem ch_free( nbaseDN.bv_val );
93 1.1 lukem return ntargetDN.bv_val;
94 1.1 lukem }
95 1.1 lukem
96 1.1 lukem {
97 1.1 lukem struct berval muck;
98 1.1 lukem
99 1.1 lukem if( ntargetDN.bv_len < nbaseDN.bv_len ) {
100 1.1 lukem ch_free( nrefDN.bv_val );
101 1.1 lukem ch_free( nbaseDN.bv_val );
102 1.1 lukem return ntargetDN.bv_val;
103 1.1 lukem }
104 1.1 lukem
105 1.1 lukem rc = strcasecmp(
106 1.1 lukem &ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
107 1.1 lukem nbaseDN.bv_val );
108 1.1 lukem if( rc ) {
109 1.1 lukem /* target not subordinate to base */
110 1.1 lukem ch_free( nrefDN.bv_val );
111 1.1 lukem ch_free( nbaseDN.bv_val );
112 1.1 lukem return ntargetDN.bv_val;
113 1.1 lukem }
114 1.1 lukem
115 1.1 lukem muck.bv_len = ntargetDN.bv_len + nrefDN.bv_len - nbaseDN.bv_len;
116 1.1 lukem muck.bv_val = ch_malloc( muck.bv_len + 1 );
117 1.1 lukem
118 1.1 lukem strncpy( muck.bv_val, ntargetDN.bv_val,
119 1.1 lukem ntargetDN.bv_len-nbaseDN.bv_len );
120 1.1 lukem strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
121 1.1 lukem nrefDN.bv_val );
122 1.1 lukem
123 1.1 lukem ch_free( nrefDN.bv_val );
124 1.1 lukem ch_free( nbaseDN.bv_val );
125 1.1 lukem ch_free( ntargetDN.bv_val );
126 1.1 lukem
127 1.1 lukem return muck.bv_val;
128 1.1 lukem }
129 1.1 lukem }
130 1.1 lukem
131 1.1 lukem ch_free( nrefDN.bv_val );
132 1.1 lukem return ntargetDN.bv_val;
133 1.1 lukem }
134 1.1 lukem
135 1.1 lukem
136 1.1 lukem /* validate URL for global referral use
137 1.1 lukem * LDAP URLs must not have:
138 1.1 lukem * DN, attrs, scope, nor filter
139 1.1 lukem * Any non-LDAP URL is okay
140 1.1 lukem *
141 1.1 lukem * XXYYZ: should return an error string
142 1.1 lukem */
143 1.1 lukem int validate_global_referral( const char *url )
144 1.1 lukem {
145 1.1 lukem int rc;
146 1.1 lukem LDAPURLDesc *lurl;
147 1.1 lukem
148 1.1 lukem rc = ldap_url_parse_ext( url, &lurl, LDAP_PVT_URL_PARSE_NONE );
149 1.1 lukem
150 1.1 lukem switch( rc ) {
151 1.1 lukem case LDAP_URL_SUCCESS:
152 1.1 lukem break;
153 1.1 lukem
154 1.1 lukem case LDAP_URL_ERR_BADSCHEME:
155 1.1 lukem /* not LDAP hence valid */
156 1.1 lukem Debug( LDAP_DEBUG_CONFIG, "referral \"%s\": not LDAP.\n", url, 0, 0 );
157 1.1 lukem return 0;
158 1.1 lukem
159 1.1 lukem default:
160 1.1 lukem /* other error, bail */
161 1.1 lukem Debug( LDAP_DEBUG_ANY,
162 1.1 lukem "referral: invalid URL (%s): %s (%d)\n",
163 1.1 lukem url, "" /* ldap_url_error2str(rc) */, rc );
164 1.1 lukem return 1;
165 1.1 lukem }
166 1.1 lukem
167 1.1 lukem rc = 0;
168 1.1 lukem
169 1.1 lukem if( lurl->lud_dn && *lurl->lud_dn ) {
170 1.1 lukem Debug( LDAP_DEBUG_ANY,
171 1.1 lukem "referral: URL (%s): contains DN\n",
172 1.1 lukem url, 0, 0 );
173 1.1 lukem rc = 1;
174 1.1 lukem
175 1.1 lukem } else if( lurl->lud_attrs ) {
176 1.1 lukem Debug( LDAP_DEBUG_ANY,
177 1.1 lukem "referral: URL (%s): requests attributes\n",
178 1.1 lukem url, 0, 0 );
179 1.1 lukem rc = 1;
180 1.1 lukem
181 1.1 lukem } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
182 1.1 lukem Debug( LDAP_DEBUG_ANY,
183 1.1 lukem "referral: URL (%s): contains explicit scope\n",
184 1.1 lukem url, 0, 0 );
185 1.1 lukem rc = 1;
186 1.1 lukem
187 1.1 lukem } else if( lurl->lud_filter ) {
188 1.1 lukem Debug( LDAP_DEBUG_ANY,
189 1.1 lukem "referral: URL (%s): contains explicit filter\n",
190 1.1 lukem url, 0, 0 );
191 1.1 lukem rc = 1;
192 1.1 lukem }
193 1.1 lukem
194 1.1 lukem ldap_free_urldesc( lurl );
195 1.1 lukem return rc;
196 1.1 lukem }
197 1.1 lukem
198 1.1 lukem BerVarray referral_rewrite(
199 1.1 lukem BerVarray in,
200 1.1 lukem struct berval *base,
201 1.1 lukem struct berval *target,
202 1.1 lukem int scope )
203 1.1 lukem {
204 1.1 lukem int i;
205 1.1 lukem BerVarray refs;
206 1.1 lukem struct berval *iv, *jv;
207 1.1 lukem
208 1.1 lukem if ( in == NULL ) {
209 1.1 lukem return NULL;
210 1.1 lukem }
211 1.1 lukem
212 1.1 lukem for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
213 1.1 lukem /* just count them */
214 1.1 lukem }
215 1.1 lukem
216 1.1 lukem if ( i < 1 ) {
217 1.1 lukem return NULL;
218 1.1 lukem }
219 1.1 lukem
220 1.1 lukem refs = ch_malloc( ( i + 1 ) * sizeof( struct berval ) );
221 1.1 lukem
222 1.1 lukem for ( iv = in, jv = refs; !BER_BVISNULL( iv ); iv++ ) {
223 1.1 lukem LDAPURLDesc *url;
224 1.1 lukem char *dn;
225 1.1 lukem int rc;
226 1.1 lukem
227 1.1 lukem rc = ldap_url_parse_ext( iv->bv_val, &url, LDAP_PVT_URL_PARSE_NONE );
228 1.1 lukem if ( rc == LDAP_URL_ERR_BADSCHEME ) {
229 1.1 lukem ber_dupbv( jv++, iv );
230 1.1 lukem continue;
231 1.1 lukem
232 1.1 lukem } else if ( rc != LDAP_URL_SUCCESS ) {
233 1.1 lukem continue;
234 1.1 lukem }
235 1.1 lukem
236 1.1 lukem dn = url->lud_dn;
237 1.1 lukem url->lud_dn = referral_dn_muck( ( dn && *dn ) ? dn : NULL,
238 1.1 lukem base, target );
239 1.1 lukem ldap_memfree( dn );
240 1.1 lukem
241 1.1 lukem if ( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
242 1.1 lukem url->lud_scope = scope;
243 1.1 lukem }
244 1.1 lukem
245 1.1 lukem jv->bv_val = ldap_url_desc2str( url );
246 1.1 lukem if ( jv->bv_val != NULL ) {
247 1.1 lukem jv->bv_len = strlen( jv->bv_val );
248 1.1 lukem
249 1.1 lukem } else {
250 1.1 lukem ber_dupbv( jv, iv );
251 1.1 lukem }
252 1.1 lukem jv++;
253 1.1 lukem
254 1.1 lukem ldap_free_urldesc( url );
255 1.1 lukem }
256 1.1 lukem
257 1.1 lukem if ( jv == refs ) {
258 1.1 lukem ch_free( refs );
259 1.1 lukem refs = NULL;
260 1.1 lukem
261 1.1 lukem } else {
262 1.1 lukem BER_BVZERO( jv );
263 1.1 lukem }
264 1.1 lukem
265 1.1 lukem return refs;
266 1.1 lukem }
267 1.1 lukem
268 1.1 lukem
269 1.1 lukem BerVarray get_entry_referrals(
270 1.1 lukem Operation *op,
271 1.1 lukem Entry *e )
272 1.1 lukem {
273 1.1 lukem Attribute *attr;
274 1.1 lukem BerVarray refs;
275 1.1 lukem unsigned i;
276 1.1 lukem struct berval *iv, *jv;
277 1.1 lukem
278 1.1 lukem AttributeDescription *ad_ref = slap_schema.si_ad_ref;
279 1.1 lukem
280 1.1 lukem attr = attr_find( e->e_attrs, ad_ref );
281 1.1 lukem
282 1.1 lukem if( attr == NULL ) return NULL;
283 1.1 lukem
284 1.1 lukem for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
285 1.1 lukem /* count references */
286 1.1 lukem }
287 1.1 lukem
288 1.1 lukem if( i < 1 ) return NULL;
289 1.1 lukem
290 1.1 lukem refs = ch_malloc( (i + 1) * sizeof(struct berval));
291 1.1 lukem
292 1.1 lukem for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
293 1.1 lukem unsigned k;
294 1.1 lukem ber_dupbv( jv, iv );
295 1.1 lukem
296 1.1 lukem /* trim the label */
297 1.1 lukem for( k=0; k<jv->bv_len; k++ ) {
298 1.1 lukem if( isspace( (unsigned char) jv->bv_val[k] ) ) {
299 1.1 lukem jv->bv_val[k] = '\0';
300 1.1 lukem jv->bv_len = k;
301 1.1 lukem break;
302 1.1 lukem }
303 1.1 lukem }
304 1.1 lukem
305 1.1 lukem if( jv->bv_len > 0 ) {
306 1.1 lukem jv++;
307 1.1 lukem } else {
308 1.1 lukem free( jv->bv_val );
309 1.1 lukem }
310 1.1 lukem }
311 1.1 lukem
312 1.1 lukem if( jv == refs ) {
313 1.1 lukem free( refs );
314 1.1 lukem refs = NULL;
315 1.1 lukem
316 1.1 lukem } else {
317 1.1 lukem jv->bv_val = NULL;
318 1.1 lukem }
319 1.1 lukem
320 1.1 lukem /* we should check that a referral value exists... */
321 1.1 lukem return refs;
322 1.1 lukem }
323 1.1 lukem
324 1.1 lukem
325 1.1 lukem int get_alias_dn(
326 1.1 lukem Entry *e,
327 1.1 lukem struct berval *ndn,
328 1.1 lukem int *err,
329 1.1 lukem const char **text )
330 1.1 lukem {
331 1.1 lukem Attribute *a;
332 1.1 lukem AttributeDescription *aliasedObjectName
333 1.1 lukem = slap_schema.si_ad_aliasedObjectName;
334 1.1 lukem
335 1.1 lukem a = attr_find( e->e_attrs, aliasedObjectName );
336 1.1 lukem
337 1.1 lukem if( a == NULL ) {
338 1.1 lukem /*
339 1.1 lukem * there was an aliasedobjectname defined but no data.
340 1.1 lukem */
341 1.1 lukem *err = LDAP_ALIAS_PROBLEM;
342 1.1 lukem *text = "alias missing aliasedObjectName attribute";
343 1.1 lukem return -1;
344 1.1 lukem }
345 1.1 lukem
346 1.1 lukem /*
347 1.1 lukem * aliasedObjectName should be SINGLE-VALUED with a single value.
348 1.1 lukem */
349 1.1 lukem if ( a->a_vals[0].bv_val == NULL ) {
350 1.1 lukem /*
351 1.1 lukem * there was an aliasedobjectname defined but no data.
352 1.1 lukem */
353 1.1 lukem *err = LDAP_ALIAS_PROBLEM;
354 1.1 lukem *text = "alias missing aliasedObjectName value";
355 1.1 lukem return -1;
356 1.1 lukem }
357 1.1 lukem
358 1.1 lukem if( a->a_nvals[1].bv_val != NULL ) {
359 1.1 lukem *err = LDAP_ALIAS_PROBLEM;
360 1.1 lukem *text = "alias has multivalued aliasedObjectName";
361 1.1 lukem return -1;
362 1.1 lukem }
363 1.1 lukem
364 1.1 lukem *ndn = a->a_nvals[0];
365 1.1 lukem
366 1.1 lukem return 0;
367 1.1 lukem }
368 1.1 lukem
369