]> git.sur5r.net Git - openldap/blob - servers/slapd/referral.c
Experimental code that uses one locker ID per thread. Seems to work OK,
[openldap] / servers / slapd / referral.c
1 /* referral.c - muck with referrals */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/errno.h>
14 #include <ac/string.h>
15 #include <ac/ctype.h>
16 #include <ac/time.h>
17 #include <ac/unistd.h>
18
19 #include <ldap_pvt.h>
20
21 #include "slap.h"
22
23 /*
24  * This routine generates the DN appropriate to return in
25  * an LDAP referral.
26  */
27 static char * referral_dn_muck(
28         const char * refDN,
29         const char * baseDN,
30         const char * targetDN )
31 {
32         int rc;
33         struct berval bvin;
34         struct berval nrefDN = { 0, NULL };
35         struct berval nbaseDN = { 0, NULL };
36         struct berval ntargetDN = { 0, NULL };
37
38         if( !baseDN ) {
39                 /* no base, return target */
40                 return targetDN ? ch_strdup( targetDN ) : NULL;
41         }
42
43         if( refDN ) {
44                 bvin.bv_val = (char *)refDN;
45                 bvin.bv_len = strlen( refDN );
46
47                 rc = dnPretty2( NULL, &bvin, &nrefDN );
48                 if( rc != LDAP_SUCCESS ) {
49                         /* Invalid refDN */
50                         return NULL;
51                 }
52         }
53
54         if( !targetDN ) {
55                 /* continuation reference
56                  *      if refDN present return refDN
57                  *  else return baseDN
58                  */
59                 return nrefDN.bv_len ? nrefDN.bv_val : ch_strdup( baseDN );
60         }
61
62         bvin.bv_val = (char *)targetDN;
63         bvin.bv_len = strlen( targetDN );
64
65         rc = dnPretty2( NULL, &bvin, &ntargetDN );
66         if( rc != LDAP_SUCCESS ) {
67                 /* Invalid targetDN */
68                 ch_free( nrefDN.bv_val );
69                 return NULL;
70         }
71
72         if( nrefDN.bv_len ) {
73                 bvin.bv_val = (char *)baseDN;
74                 bvin.bv_len = strlen( baseDN );
75
76                 rc = dnPretty2( NULL, &bvin, &nbaseDN );
77                 if( rc != LDAP_SUCCESS ) {
78                         /* Invalid baseDN */
79                         ch_free( nrefDN.bv_val );
80                         ch_free( ntargetDN.bv_val );
81                         return NULL;
82                 }
83
84                 if( dn_match( &nbaseDN, &nrefDN ) == 0 ) {
85                         ch_free( nrefDN.bv_val );
86                         ch_free( nbaseDN.bv_val );
87                         return ntargetDN.bv_val;
88                 }
89
90                 {
91                         struct berval muck;
92
93                         if( ntargetDN.bv_len < nbaseDN.bv_len ) {
94                                 ch_free( nrefDN.bv_val );
95                                 ch_free( nbaseDN.bv_val );
96                                 return ntargetDN.bv_val;
97                         }
98
99                         rc = strcasecmp(
100                                 &ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
101                                 nbaseDN.bv_val );
102                         if( rc ) {
103                                 /* target not subordinate to base */
104                                 ch_free( nrefDN.bv_val );
105                                 ch_free( nbaseDN.bv_val );
106                                 return ntargetDN.bv_val;
107                         }
108
109                         muck.bv_len = ntargetDN.bv_len + nrefDN.bv_len - nbaseDN.bv_len;
110                         muck.bv_val = ch_malloc( muck.bv_len + 1 );
111
112                         strncpy( muck.bv_val, ntargetDN.bv_val,
113                                 ntargetDN.bv_len-nbaseDN.bv_len );
114                         strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
115                                 nrefDN.bv_val );
116
117                         ch_free( nrefDN.bv_val );
118                         ch_free( nbaseDN.bv_val );
119                         ch_free( ntargetDN.bv_val );
120
121                         return muck.bv_val;
122                 }
123         }
124
125         ch_free( nrefDN.bv_val );
126         return ntargetDN.bv_val;
127 }
128
129
130 /* validate URL for global referral use
131  *   LDAP URLs must not have:
132  *     DN, attrs, scope, nor filter
133  *   Any non-LDAP URL is okay
134  *
135  *   XXYYZ: should return an error string
136  */
137 int validate_global_referral( const char *url )
138 {
139         int rc;
140         LDAPURLDesc *lurl;
141
142         rc = ldap_url_parse_ext( url, &lurl );
143
144         switch( rc ) {
145         case LDAP_URL_SUCCESS:
146                 break;
147
148         case LDAP_URL_ERR_BADSCHEME:
149                 /* not LDAP hence valid */
150                 return 0;
151
152         default:
153                 /* other error, bail */
154 #ifdef NEW_LOGGING
155                 LDAP_LOG( CONFIG, CRIT, 
156                         "referral: invalid URL (%s): %s (%d)\n",
157                         url, "" /* ldap_url_error2str(rc) */, rc );
158 #else
159                 Debug( LDAP_DEBUG_ANY,
160                         "referral: invalid URL (%s): %s (%d)\n",
161                         url, "" /* ldap_url_error2str(rc) */, rc );
162 #endif
163                 return 1;
164         }
165
166         rc = 0;
167
168         if( lurl->lud_dn && *lurl->lud_dn ) {
169 #ifdef NEW_LOGGING
170                 LDAP_LOG( CONFIG, CRIT, "referral: URL (%s): contains DN\n", url, 0, 0 );
171 #else
172                 Debug( LDAP_DEBUG_ANY,
173                         "referral: URL (%s): contains DN\n",
174                         url, 0, 0 );
175 #endif
176                 rc = 1;
177
178         } else if( lurl->lud_attrs ) {
179 #ifdef NEW_LOGGING
180                 LDAP_LOG( CONFIG, CRIT, 
181                         "referral: URL (%s): requests attributes\n", url, 0, 0 );
182 #else
183                 Debug( LDAP_DEBUG_ANY,
184                         "referral: URL (%s): requests attributes\n",
185                         url, 0, 0 );
186 #endif
187                 rc = 1;
188
189         } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
190 #ifdef NEW_LOGGING
191                 LDAP_LOG( CONFIG, CRIT, 
192                         "referral: URL (%s): contains explicit scope\n", url, 0, 0 );
193 #else
194                 Debug( LDAP_DEBUG_ANY,
195                         "referral: URL (%s): contains explicit scope\n",
196                         url, 0, 0 );
197 #endif
198                 rc = 1;
199
200         } else if( lurl->lud_filter ) {
201 #ifdef NEW_LOGGING
202                 LDAP_LOG( CONFIG, CRIT, 
203                         "referral: URL (%s): contains explicit filter\n", url, 0, 0 );
204 #else
205                 Debug( LDAP_DEBUG_ANY,
206                         "referral: URL (%s): contains explicit filter\n",
207                         url, 0, 0 );
208 #endif
209                 rc = 1;
210         }
211
212         ldap_free_urldesc( lurl );
213         return rc;
214 }
215
216 BerVarray referral_rewrite(
217         BerVarray in,
218         struct berval *base,
219         struct berval *target,
220         int scope )
221 {
222         int i;
223         BerVarray refs;
224         struct berval *iv, *jv;
225
226         if( in == NULL ) return NULL;
227
228         for( i=0; in[i].bv_val != NULL ; i++ ) {
229                 /* just count them */
230         }
231
232         if( i < 1 ) return NULL;
233
234         refs = ch_malloc( (i+1) * sizeof( struct berval ) );
235
236         for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
237                 LDAPURLDesc *url;
238                 int rc = ldap_url_parse_ext( iv->bv_val, &url );
239
240                 if( rc == LDAP_URL_ERR_BADSCHEME ) {
241                         ber_dupbv( jv++, iv );
242                         continue;
243
244                 } else if( rc != LDAP_URL_SUCCESS ) {
245                         continue;
246                 }
247
248                 {
249                         char *dn = url->lud_dn;
250                         url->lud_dn = referral_dn_muck(
251                                 ( dn && *dn ) ? dn : NULL,
252                                 base ? base->bv_val : NULL,
253                                 target ? target->bv_val : NULL ); 
254
255                         ldap_memfree( dn );
256                 }
257
258                 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
259                         url->lud_scope = scope;
260                 }
261
262                 jv->bv_val = ldap_url_desc2str( url );
263                 jv->bv_len = strlen( jv->bv_val );
264
265                 ldap_free_urldesc( url );
266                 jv++;
267         }
268
269         if( jv == refs ) {
270                 ch_free( refs );
271                 refs = NULL;
272
273         } else {
274                 jv->bv_val = NULL;
275         }
276
277         return refs;
278 }
279
280
281 BerVarray get_entry_referrals(
282         Backend *be,
283         Connection *conn,
284         Operation *op,
285         Entry *e )
286 {
287         Attribute *attr;
288         BerVarray refs;
289         unsigned i;
290         struct berval *iv, *jv;
291
292         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
293
294         attr = attr_find( e->e_attrs, ad_ref );
295
296         if( attr == NULL ) return NULL;
297
298         for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
299                 /* count references */
300         }
301
302         if( i < 1 ) return NULL;
303
304         refs = ch_malloc( (i + 1) * sizeof(struct berval));
305
306         for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
307                 unsigned k;
308                 ber_dupbv( jv, iv );
309
310                 /* trim the label */
311                 for( k=0; k<jv->bv_len; k++ ) {
312                         if( isspace( (unsigned char) jv->bv_val[k] ) ) {
313                                 jv->bv_val[k] = '\0';
314                                 jv->bv_len = k;
315                                 break;
316                         }
317                 }
318
319                 if(     jv->bv_len > 0 ) {
320                         jv++;
321                 } else {
322                         free( jv->bv_val );
323                 }
324         }
325
326         if( jv == refs ) {
327                 free( refs );
328                 refs = NULL;
329
330         } else {
331                 jv->bv_val = NULL;
332         }
333
334         /* we should check that a referral value exists... */
335         return refs;
336 }
337