]> git.sur5r.net Git - openldap/blob - servers/slapd/referral.c
91db896f1c0bb3e4869a4546d18a912107154f5c
[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 <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                                 Debug( LDAP_DEBUG_ANY,
116                                         "referral_dn_muck: SLAP_MALLOC failed\n", 0, 0, 0 );
117                                 return NULL;
118                         }
119
120                         strncpy( muck.bv_val, ntargetDN.bv_val,
121                                 ntargetDN.bv_len-nbaseDN.bv_len );
122                         strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
123                                 nrefDN.bv_val );
124
125                         ch_free( nrefDN.bv_val );
126                         ch_free( nbaseDN.bv_val );
127                         ch_free( ntargetDN.bv_val );
128
129                         return muck.bv_val;
130                 }
131         }
132
133         ch_free( nrefDN.bv_val );
134         return ntargetDN.bv_val;
135 }
136
137
138 /* validate URL for global referral use
139  *   LDAP URLs must not have:
140  *     DN, attrs, scope, nor filter
141  *   Any non-LDAP URL is okay
142  *
143  *   XXYYZ: should return an error string
144  */
145 int validate_global_referral( const char *url )
146 {
147         int rc;
148         LDAPURLDesc *lurl;
149
150         rc = ldap_url_parse_ext( url, &lurl );
151
152         switch( rc ) {
153         case LDAP_URL_SUCCESS:
154                 break;
155
156         case LDAP_URL_ERR_BADSCHEME:
157                 /* not LDAP hence valid */
158                 return 0;
159
160         default:
161                 /* other error, bail */
162                 Debug( LDAP_DEBUG_ANY,
163                         "referral: invalid URL (%s): %s (%d)\n",
164                         url, "" /* ldap_url_error2str(rc) */, rc );
165                 return 1;
166         }
167
168         rc = 0;
169
170         if( lurl->lud_dn && *lurl->lud_dn ) {
171                 Debug( LDAP_DEBUG_ANY,
172                         "referral: URL (%s): contains DN\n",
173                         url, 0, 0 );
174                 rc = 1;
175
176         } else if( lurl->lud_attrs ) {
177                 Debug( LDAP_DEBUG_ANY,
178                         "referral: URL (%s): requests attributes\n",
179                         url, 0, 0 );
180                 rc = 1;
181
182         } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
183                 Debug( LDAP_DEBUG_ANY,
184                         "referral: URL (%s): contains explicit scope\n",
185                         url, 0, 0 );
186                 rc = 1;
187
188         } else if( lurl->lud_filter ) {
189                 Debug( LDAP_DEBUG_ANY,
190                         "referral: URL (%s): contains explicit filter\n",
191                         url, 0, 0 );
192                 rc = 1;
193         }
194
195         ldap_free_urldesc( lurl );
196         return rc;
197 }
198
199 BerVarray referral_rewrite(
200         BerVarray in,
201         struct berval *base,
202         struct berval *target,
203         int scope )
204 {
205         int i;
206         BerVarray refs;
207         struct berval *iv, *jv;
208
209         if( in == NULL ) return NULL;
210
211         for( i=0; in[i].bv_val != NULL ; i++ ) {
212                 /* just count them */
213         }
214
215         if( i < 1 ) return NULL;
216
217         refs = SLAP_MALLOC( (i+1) * sizeof( struct berval ) );
218         if( refs == NULL ) {
219                 Debug( LDAP_DEBUG_ANY,
220                         "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
221                 return NULL;
222         }
223
224         for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
225                 LDAPURLDesc *url;
226                 int rc = ldap_url_parse_ext( iv->bv_val, &url );
227
228                 if( rc == LDAP_URL_ERR_BADSCHEME ) {
229                         ber_dupbv( jv++, iv );
230                         continue;
231
232                 } else if( rc != LDAP_URL_SUCCESS ) {
233                         continue;
234                 }
235
236                 {
237                         char *dn = url->lud_dn;
238                         url->lud_dn = referral_dn_muck(
239                                 ( dn && *dn ) ? dn : NULL,
240                                 base, target );
241
242                         ldap_memfree( dn );
243                 }
244
245                 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
246                         url->lud_scope = scope;
247                 }
248
249                 jv->bv_val = ldap_url_desc2str( url );
250                 jv->bv_len = strlen( jv->bv_val );
251
252                 ldap_free_urldesc( url );
253                 jv++;
254         }
255
256         if( jv == refs ) {
257                 ch_free( refs );
258                 refs = NULL;
259
260         } else {
261                 jv->bv_val = NULL;
262         }
263
264         return refs;
265 }
266
267
268 BerVarray get_entry_referrals(
269         Operation *op,
270         Entry *e )
271 {
272         Attribute *attr;
273         BerVarray refs;
274         unsigned i;
275         struct berval *iv, *jv;
276
277         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
278
279         attr = attr_find( e->e_attrs, ad_ref );
280
281         if( attr == NULL ) return NULL;
282
283         for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
284                 /* count references */
285         }
286
287         if( i < 1 ) return NULL;
288
289         refs = SLAP_MALLOC( (i + 1) * sizeof(struct berval));
290         if( refs == NULL ) {
291                 Debug( LDAP_DEBUG_ANY,
292                         "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
293                 return NULL;
294         }
295
296         for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
297                 unsigned k;
298                 ber_dupbv( jv, iv );
299
300                 /* trim the label */
301                 for( k=0; k<jv->bv_len; k++ ) {
302                         if( isspace( (unsigned char) jv->bv_val[k] ) ) {
303                                 jv->bv_val[k] = '\0';
304                                 jv->bv_len = k;
305                                 break;
306                         }
307                 }
308
309                 if(     jv->bv_len > 0 ) {
310                         jv++;
311                 } else {
312                         free( jv->bv_val );
313                 }
314         }
315
316         if( jv == refs ) {
317                 free( refs );
318                 refs = NULL;
319
320         } else {
321                 jv->bv_val = NULL;
322         }
323
324         /* we should check that a referral value exists... */
325         return refs;
326 }
327
328
329 int get_alias_dn(
330         Entry *e,
331         struct berval *ndn,
332         int *err,
333         const char **text )
334 {       
335         Attribute *a;
336         AttributeDescription *aliasedObjectName
337                 = slap_schema.si_ad_aliasedObjectName;
338
339         a = attr_find( e->e_attrs, aliasedObjectName );
340
341         if( a == NULL ) {
342                 /*
343                  * there was an aliasedobjectname defined but no data.
344                  */
345                 *err = LDAP_ALIAS_PROBLEM;
346                 *text = "alias missing aliasedObjectName attribute";
347                 return -1;
348         }
349
350         /* 
351          * aliasedObjectName should be SINGLE-VALUED with a single value. 
352          */                     
353         if ( a->a_vals[0].bv_val == NULL ) {
354                 /*
355                  * there was an aliasedobjectname defined but no data.
356                  */
357                 *err = LDAP_ALIAS_PROBLEM;
358                 *text = "alias missing aliasedObjectName value";
359                 return -1;
360         }
361
362         if( a->a_nvals[1].bv_val != NULL ) {
363                 *err = LDAP_ALIAS_PROBLEM;
364                 *text = "alias has multivalued aliasedObjectName";
365                 return -1;
366         }
367
368         *ndn = a->a_nvals[0];
369
370         return 0;
371 }
372