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