]> git.sur5r.net Git - openldap/blob - servers/slapd/referral.c
Add HAVE_EPOLL 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-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 "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 ) {
208                 return NULL;
209         }
210
211         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
212                 /* just count them */
213         }
214
215         if ( i < 1 ) {
216                 return NULL;
217         }
218
219         refs = SLAP_MALLOC( ( i + 1 ) * sizeof( struct berval ) );
220         if ( refs == NULL ) {
221                 Debug( LDAP_DEBUG_ANY,
222                         "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
223                 return NULL;
224         }
225
226         for ( iv = in, jv = refs; !BER_BVISNULL( iv ); iv++ ) {
227                 LDAPURLDesc     *url;
228                 char            *dn;
229                 int             rc;
230                 
231                 rc = ldap_url_parse_ext( iv->bv_val, &url );
232                 if ( rc == LDAP_URL_ERR_BADSCHEME ) {
233                         ber_dupbv( jv++, iv );
234                         continue;
235
236                 } else if ( rc != LDAP_URL_SUCCESS ) {
237                         continue;
238                 }
239
240                 dn = url->lud_dn;
241                 url->lud_dn = referral_dn_muck( ( dn && *dn ) ? dn : NULL,
242                                 base, target );
243                 ldap_memfree( dn );
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                 if ( jv->bv_val != NULL ) {
251                         jv->bv_len = strlen( jv->bv_val );
252
253                 } else {
254                         ber_dupbv( jv, iv );
255                 }
256                 jv++;
257
258                 ldap_free_urldesc( url );
259         }
260
261         if ( jv == refs ) {
262                 ch_free( refs );
263                 refs = NULL;
264
265         } else {
266                 BER_BVZERO( jv );
267         }
268
269         return refs;
270 }
271
272
273 BerVarray get_entry_referrals(
274         Operation *op,
275         Entry *e )
276 {
277         Attribute *attr;
278         BerVarray refs;
279         unsigned i;
280         struct berval *iv, *jv;
281
282         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
283
284         attr = attr_find( e->e_attrs, ad_ref );
285
286         if( attr == NULL ) return NULL;
287
288         for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
289                 /* count references */
290         }
291
292         if( i < 1 ) return NULL;
293
294         refs = SLAP_MALLOC( (i + 1) * sizeof(struct berval));
295         if( refs == NULL ) {
296                 Debug( LDAP_DEBUG_ANY,
297                         "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
298                 return NULL;
299         }
300
301         for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
302                 unsigned k;
303                 ber_dupbv( jv, iv );
304
305                 /* trim the label */
306                 for( k=0; k<jv->bv_len; k++ ) {
307                         if( isspace( (unsigned char) jv->bv_val[k] ) ) {
308                                 jv->bv_val[k] = '\0';
309                                 jv->bv_len = k;
310                                 break;
311                         }
312                 }
313
314                 if(     jv->bv_len > 0 ) {
315                         jv++;
316                 } else {
317                         free( jv->bv_val );
318                 }
319         }
320
321         if( jv == refs ) {
322                 free( refs );
323                 refs = NULL;
324
325         } else {
326                 jv->bv_val = NULL;
327         }
328
329         /* we should check that a referral value exists... */
330         return refs;
331 }
332
333
334 int get_alias_dn(
335         Entry *e,
336         struct berval *ndn,
337         int *err,
338         const char **text )
339 {       
340         Attribute *a;
341         AttributeDescription *aliasedObjectName
342                 = slap_schema.si_ad_aliasedObjectName;
343
344         a = attr_find( e->e_attrs, aliasedObjectName );
345
346         if( a == NULL ) {
347                 /*
348                  * there was an aliasedobjectname defined but no data.
349                  */
350                 *err = LDAP_ALIAS_PROBLEM;
351                 *text = "alias missing aliasedObjectName attribute";
352                 return -1;
353         }
354
355         /* 
356          * aliasedObjectName should be SINGLE-VALUED with a single value. 
357          */                     
358         if ( a->a_vals[0].bv_val == NULL ) {
359                 /*
360                  * there was an aliasedobjectname defined but no data.
361                  */
362                 *err = LDAP_ALIAS_PROBLEM;
363                 *text = "alias missing aliasedObjectName value";
364                 return -1;
365         }
366
367         if( a->a_nvals[1].bv_val != NULL ) {
368                 *err = LDAP_ALIAS_PROBLEM;
369                 *text = "alias has multivalued aliasedObjectName";
370                 return -1;
371         }
372
373         *ndn = a->a_nvals[0];
374
375         return 0;
376 }
377