]> git.sur5r.net Git - openldap/blob - servers/slapd/referral.c
Use IPV6_V6ONLY on IPv6 sockets if available. This way we only get IPv6
[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", LDAP_LEVEL_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", LDAP_LEVEL_CRIT,
171                         "referral: URL (%s): contains DN\n",
172                         url ));
173 #else
174                 Debug( LDAP_DEBUG_ANY,
175                         "referral: URL (%s): contains DN\n",
176                         url, 0, 0 );
177 #endif
178                 rc = 1;
179
180         } else if( lurl->lud_attrs ) {
181 #ifdef NEW_LOGGING
182                 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
183                         "referral: URL (%s): requests attributes\n",
184                         url ));
185 #else
186                 Debug( LDAP_DEBUG_ANY,
187                         "referral: URL (%s): requests attributes\n",
188                         url, 0, 0 );
189 #endif
190                 rc = 1;
191
192         } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
193 #ifdef NEW_LOGGING
194                 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
195                         "referral: URL (%s): contains explicit scope\n",
196                         url ));
197 #else
198                 Debug( LDAP_DEBUG_ANY,
199                         "referral: URL (%s): contains explicit scope\n",
200                         url, 0, 0 );
201 #endif
202                 rc = 1;
203
204         } else if( lurl->lud_filter ) {
205 #ifdef NEW_LOGGING
206                 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
207                         "referral: URL (%s): contains explicit filter\n",
208                         url ));
209 #else
210                 Debug( LDAP_DEBUG_ANY,
211                         "referral: URL (%s): contains explicit filter\n",
212                         url, 0, 0 );
213 #endif
214                 rc = 1;
215         }
216
217         ldap_free_urldesc( lurl );
218         return rc;
219 }
220
221 BerVarray referral_rewrite(
222         BerVarray in,
223         struct berval *base,
224         struct berval *target,
225         int scope )
226 {
227         int i;
228         BerVarray refs;
229         struct berval *iv, *jv;
230
231         if( in == NULL ) return NULL;
232
233         for( i=0; in[i].bv_val != NULL ; i++ ) {
234                 /* just count them */
235         }
236
237         if( i < 1 ) return NULL;
238
239         refs = ch_malloc( (i+1) * sizeof( struct berval ) );
240
241         for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
242                 LDAPURLDesc *url;
243                 int rc = ldap_url_parse_ext( iv->bv_val, &url );
244
245                 if( rc == LDAP_URL_ERR_BADSCHEME ) {
246                         ber_dupbv( jv++, iv );
247                         continue;
248
249                 } else if( rc != LDAP_URL_SUCCESS ) {
250                         continue;
251                 }
252
253                 {
254                         char *dn = url->lud_dn;
255                         url->lud_dn = referral_dn_muck(
256                                 ( dn && *dn ) ? dn : NULL,
257                                 base ? base->bv_val : NULL,
258                                 target ? target->bv_val : NULL ); 
259
260                         ldap_memfree( dn );
261                 }
262
263                 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
264                         url->lud_scope = scope;
265                 }
266
267                 jv->bv_val = ldap_url_desc2str( url );
268                 jv->bv_len = strlen( jv->bv_val );
269
270                 ldap_free_urldesc( url );
271                 jv++;
272         }
273
274         if( jv == refs ) {
275                 ch_free( refs );
276                 refs = NULL;
277
278         } else {
279                 jv->bv_val = NULL;
280         }
281
282         return refs;
283 }
284
285
286 BerVarray get_entry_referrals(
287         Backend *be,
288         Connection *conn,
289         Operation *op,
290         Entry *e )
291 {
292         Attribute *attr;
293         BerVarray refs;
294         unsigned i;
295         struct berval *iv, *jv;
296
297         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
298
299         attr = attr_find( e->e_attrs, ad_ref );
300
301         if( attr == NULL ) return NULL;
302
303         for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
304                 /* count references */
305         }
306
307         if( i < 1 ) return NULL;
308
309         refs = ch_malloc( (i + 1) * sizeof(struct berval));
310
311         for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
312                 unsigned k;
313                 ber_dupbv( jv, iv );
314
315                 /* trim the label */
316                 for( k=0; k<jv->bv_len; k++ ) {
317                         if( isspace( (unsigned char) jv->bv_val[k] ) ) {
318                                 jv->bv_val[k] = '\0';
319                                 jv->bv_len = k;
320                                 break;
321                         }
322                 }
323
324                 if(     jv->bv_len > 0 ) {
325                         jv++;
326                 } else {
327                         free( jv->bv_val );
328                 }
329         }
330
331         if( jv == refs ) {
332                 free( refs );
333                 refs = NULL;
334
335         } else {
336                 jv->bv_val = NULL;
337         }
338
339         /* we should check that a referral value exists... */
340         return refs;
341 }
342