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