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