]> git.sur5r.net Git - openldap/blob - servers/slapd/referral.c
039e1957f3c00554564c8e81be3e87fcf7e94d07
[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-2004 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 "slap.h"
29
30 /*
31  * This routine generates the DN appropriate to return in
32  * an LDAP referral.
33  */
34 static char * referral_dn_muck(
35         const char * refDN,
36         struct berval * baseDN,
37         struct berval * targetDN )
38 {
39         int rc;
40         struct berval bvin;
41         struct berval nrefDN = BER_BVNULL;
42         struct berval nbaseDN = BER_BVNULL;
43         struct berval ntargetDN = BER_BVNULL;
44
45         if( !baseDN ) {
46                 /* no base, return target */
47                 return targetDN ? ch_strdup( targetDN->bv_val ) : NULL;
48         }
49
50         if( refDN ) {
51                 bvin.bv_val = (char *)refDN;
52                 bvin.bv_len = strlen( refDN );
53
54                 rc = dnPretty( NULL, &bvin, &nrefDN, NULL );
55                 if( rc != LDAP_SUCCESS ) {
56                         /* Invalid refDN */
57                         return NULL;
58                 }
59         }
60
61         if( !targetDN ) {
62                 /* continuation reference
63                  *      if refDN present return refDN
64                  *  else return baseDN
65                  */
66                 return nrefDN.bv_len ? nrefDN.bv_val : ch_strdup( baseDN->bv_val );
67         }
68
69         rc = dnPretty( NULL, targetDN, &ntargetDN, NULL );
70         if( rc != LDAP_SUCCESS ) {
71                 /* Invalid targetDN */
72                 ch_free( nrefDN.bv_val );
73                 return NULL;
74         }
75
76         if( nrefDN.bv_len ) {
77                 rc = dnPretty( NULL, baseDN, &nbaseDN, NULL );
78                 if( rc != LDAP_SUCCESS ) {
79                         /* Invalid baseDN */
80                         ch_free( nrefDN.bv_val );
81                         ch_free( ntargetDN.bv_val );
82                         return NULL;
83                 }
84
85                 if( dn_match( &nbaseDN, &nrefDN ) ) {
86                         ch_free( nrefDN.bv_val );
87                         ch_free( nbaseDN.bv_val );
88                         return ntargetDN.bv_val;
89                 }
90
91                 {
92                         struct berval muck;
93
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;
98                         }
99
100                         rc = strcasecmp(
101                                 &ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
102                                 nbaseDN.bv_val );
103                         if( rc ) {
104                                 /* target not subordinate to base */
105                                 ch_free( nrefDN.bv_val );
106                                 ch_free( nbaseDN.bv_val );
107                                 return ntargetDN.bv_val;
108                         }
109
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 );
115                                 return NULL;
116                         }
117
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],
121                                 nrefDN.bv_val );
122
123                         ch_free( nrefDN.bv_val );
124                         ch_free( nbaseDN.bv_val );
125                         ch_free( ntargetDN.bv_val );
126
127                         return muck.bv_val;
128                 }
129         }
130
131         ch_free( nrefDN.bv_val );
132         return ntargetDN.bv_val;
133 }
134
135
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
140  *
141  *   XXYYZ: should return an error string
142  */
143 int validate_global_referral( const char *url )
144 {
145         int rc;
146         LDAPURLDesc *lurl;
147
148         rc = ldap_url_parse_ext( url, &lurl );
149
150         switch( rc ) {
151         case LDAP_URL_SUCCESS:
152                 break;
153
154         case LDAP_URL_ERR_BADSCHEME:
155                 /* not LDAP hence valid */
156                 return 0;
157
158         default:
159                 /* other error, bail */
160                 Debug( LDAP_DEBUG_ANY,
161                         "referral: invalid URL (%s): %s (%d)\n",
162                         url, "" /* ldap_url_error2str(rc) */, rc );
163                 return 1;
164         }
165
166         rc = 0;
167
168         if( lurl->lud_dn && *lurl->lud_dn ) {
169                 Debug( LDAP_DEBUG_ANY,
170                         "referral: URL (%s): contains DN\n",
171                         url, 0, 0 );
172                 rc = 1;
173
174         } else if( lurl->lud_attrs ) {
175                 Debug( LDAP_DEBUG_ANY,
176                         "referral: URL (%s): requests attributes\n",
177                         url, 0, 0 );
178                 rc = 1;
179
180         } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
181                 Debug( LDAP_DEBUG_ANY,
182                         "referral: URL (%s): contains explicit scope\n",
183                         url, 0, 0 );
184                 rc = 1;
185
186         } else if( lurl->lud_filter ) {
187                 Debug( LDAP_DEBUG_ANY,
188                         "referral: URL (%s): contains explicit filter\n",
189                         url, 0, 0 );
190                 rc = 1;
191         }
192
193         ldap_free_urldesc( lurl );
194         return rc;
195 }
196
197 BerVarray referral_rewrite(
198         BerVarray in,
199         struct berval *base,
200         struct berval *target,
201         int scope )
202 {
203         int i;
204         BerVarray refs;
205         struct berval *iv, *jv;
206
207         if( in == NULL ) return NULL;
208
209         for( i=0; in[i].bv_val != NULL ; i++ ) {
210                 /* just count them */
211         }
212
213         if( i < 1 ) return NULL;
214
215         refs = SLAP_MALLOC( (i+1) * sizeof( struct berval ) );
216         if( refs == NULL ) {
217                 Debug( LDAP_DEBUG_ANY,
218                         "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
219                 return NULL;
220         }
221
222         for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
223                 LDAPURLDesc *url;
224                 int rc = ldap_url_parse_ext( iv->bv_val, &url );
225
226                 if( rc == LDAP_URL_ERR_BADSCHEME ) {
227                         ber_dupbv( jv++, iv );
228                         continue;
229
230                 } else if( rc != LDAP_URL_SUCCESS ) {
231                         continue;
232                 }
233
234                 {
235                         char *dn = url->lud_dn;
236                         url->lud_dn = referral_dn_muck(
237                                 ( dn && *dn ) ? dn : NULL,
238                                 base, target );
239
240                         ldap_memfree( dn );
241                 }
242
243                 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
244                         url->lud_scope = scope;
245                 }
246
247                 jv->bv_val = ldap_url_desc2str( url );
248                 jv->bv_len = strlen( jv->bv_val );
249
250                 ldap_free_urldesc( url );
251                 jv++;
252         }
253
254         if( jv == refs ) {
255                 ch_free( refs );
256                 refs = NULL;
257
258         } else {
259                 jv->bv_val = NULL;
260         }
261
262         return refs;
263 }
264
265
266 BerVarray get_entry_referrals(
267         Operation *op,
268         Entry *e )
269 {
270         Attribute *attr;
271         BerVarray refs;
272         unsigned i;
273         struct berval *iv, *jv;
274
275         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
276
277         attr = attr_find( e->e_attrs, ad_ref );
278
279         if( attr == NULL ) return NULL;
280
281         for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
282                 /* count references */
283         }
284
285         if( i < 1 ) return NULL;
286
287         refs = SLAP_MALLOC( (i + 1) * sizeof(struct berval));
288         if( refs == NULL ) {
289                 Debug( LDAP_DEBUG_ANY,
290                         "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
291                 return NULL;
292         }
293
294         for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
295                 unsigned k;
296                 ber_dupbv( jv, iv );
297
298                 /* trim the label */
299                 for( k=0; k<jv->bv_len; k++ ) {
300                         if( isspace( (unsigned char) jv->bv_val[k] ) ) {
301                                 jv->bv_val[k] = '\0';
302                                 jv->bv_len = k;
303                                 break;
304                         }
305                 }
306
307                 if(     jv->bv_len > 0 ) {
308                         jv++;
309                 } else {
310                         free( jv->bv_val );
311                 }
312         }
313
314         if( jv == refs ) {
315                 free( refs );
316                 refs = NULL;
317
318         } else {
319                 jv->bv_val = NULL;
320         }
321
322         /* we should check that a referral value exists... */
323         return refs;
324 }
325
326
327 int get_alias_dn(
328         Entry *e,
329         struct berval *ndn,
330         int *err,
331         const char **text )
332 {       
333         Attribute *a;
334         AttributeDescription *aliasedObjectName
335                 = slap_schema.si_ad_aliasedObjectName;
336
337         a = attr_find( e->e_attrs, aliasedObjectName );
338
339         if( a == NULL ) {
340                 /*
341                  * there was an aliasedobjectname defined but no data.
342                  */
343                 *err = LDAP_ALIAS_PROBLEM;
344                 *text = "alias missing aliasedObjectName attribute";
345                 return -1;
346         }
347
348         /* 
349          * aliasedObjectName should be SINGLE-VALUED with a single value. 
350          */                     
351         if ( a->a_vals[0].bv_val == NULL ) {
352                 /*
353                  * there was an aliasedobjectname defined but no data.
354                  */
355                 *err = LDAP_ALIAS_PROBLEM;
356                 *text = "alias missing aliasedObjectName value";
357                 return -1;
358         }
359
360         if( a->a_nvals[1].bv_val != NULL ) {
361                 *err = LDAP_ALIAS_PROBLEM;
362                 *text = "alias has multivalued aliasedObjectName";
363                 return -1;
364         }
365
366         *ndn = a->a_nvals[0];
367
368         return 0;
369 }
370