]> git.sur5r.net Git - openldap/blob - servers/slapd/referral.c
Fix prev commit
[openldap] / servers / slapd / referral.c
1 /* referral.c - muck with referrals */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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         struct berval * baseDN,
30         struct berval * 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->bv_val ) : NULL;
41         }
42
43         if( refDN ) {
44                 bvin.bv_val = (char *)refDN;
45                 bvin.bv_len = strlen( refDN );
46
47                 rc = dnPretty( NULL, &bvin, &nrefDN, NULL );
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->bv_val );
60         }
61
62         rc = dnPretty( NULL, targetDN, &ntargetDN, NULL );
63         if( rc != LDAP_SUCCESS ) {
64                 /* Invalid targetDN */
65                 ch_free( nrefDN.bv_val );
66                 return NULL;
67         }
68
69         if( nrefDN.bv_len ) {
70                 rc = dnPretty( NULL, baseDN, &nbaseDN, NULL );
71                 if( rc != LDAP_SUCCESS ) {
72                         /* Invalid baseDN */
73                         ch_free( nrefDN.bv_val );
74                         ch_free( ntargetDN.bv_val );
75                         return NULL;
76                 }
77
78                 if( dn_match( &nbaseDN, &nrefDN ) ) {
79                         ch_free( nrefDN.bv_val );
80                         ch_free( nbaseDN.bv_val );
81                         return ntargetDN.bv_val;
82                 }
83
84                 {
85                         struct berval muck;
86
87                         if( ntargetDN.bv_len < nbaseDN.bv_len ) {
88                                 ch_free( nrefDN.bv_val );
89                                 ch_free( nbaseDN.bv_val );
90                                 return ntargetDN.bv_val;
91                         }
92
93                         rc = strcasecmp(
94                                 &ntargetDN.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
95                                 nbaseDN.bv_val );
96                         if( rc ) {
97                                 /* target not subordinate to base */
98                                 ch_free( nrefDN.bv_val );
99                                 ch_free( nbaseDN.bv_val );
100                                 return ntargetDN.bv_val;
101                         }
102
103                         muck.bv_len = ntargetDN.bv_len + nrefDN.bv_len - nbaseDN.bv_len;
104                         muck.bv_val = SLAP_MALLOC( muck.bv_len + 1 );
105                         if( muck.bv_val == NULL ) {
106 #ifdef NEW_LOGGING
107                                 LDAP_LOG( OPERATION, CRIT, 
108                                         "referral_dn_muck: SLAP_MALLOC failed\n", 0, 0, 0 );
109 #else
110                                 Debug( LDAP_DEBUG_ANY,
111                                         "referral_dn_muck: SLAP_MALLOC failed\n", 0, 0, 0 );
112 #endif
113                                 return NULL;
114                         }
115
116                         strncpy( muck.bv_val, ntargetDN.bv_val,
117                                 ntargetDN.bv_len-nbaseDN.bv_len );
118                         strcpy( &muck.bv_val[ntargetDN.bv_len-nbaseDN.bv_len],
119                                 nrefDN.bv_val );
120
121                         ch_free( nrefDN.bv_val );
122                         ch_free( nbaseDN.bv_val );
123                         ch_free( ntargetDN.bv_val );
124
125                         return muck.bv_val;
126                 }
127         }
128
129         ch_free( nrefDN.bv_val );
130         return ntargetDN.bv_val;
131 }
132
133
134 /* validate URL for global referral use
135  *   LDAP URLs must not have:
136  *     DN, attrs, scope, nor filter
137  *   Any non-LDAP URL is okay
138  *
139  *   XXYYZ: should return an error string
140  */
141 int validate_global_referral( const char *url )
142 {
143         int rc;
144         LDAPURLDesc *lurl;
145
146         rc = ldap_url_parse_ext( url, &lurl );
147
148         switch( rc ) {
149         case LDAP_URL_SUCCESS:
150                 break;
151
152         case LDAP_URL_ERR_BADSCHEME:
153                 /* not LDAP hence valid */
154                 return 0;
155
156         default:
157                 /* other error, bail */
158 #ifdef NEW_LOGGING
159                 LDAP_LOG( CONFIG, CRIT, 
160                         "referral: invalid URL (%s): %s (%d)\n",
161                         url, "" /* ldap_url_error2str(rc) */, rc );
162 #else
163                 Debug( LDAP_DEBUG_ANY,
164                         "referral: invalid URL (%s): %s (%d)\n",
165                         url, "" /* ldap_url_error2str(rc) */, rc );
166 #endif
167                 return 1;
168         }
169
170         rc = 0;
171
172         if( lurl->lud_dn && *lurl->lud_dn ) {
173 #ifdef NEW_LOGGING
174                 LDAP_LOG( CONFIG, CRIT, "referral: URL (%s): contains DN\n", url, 0, 0 );
175 #else
176                 Debug( LDAP_DEBUG_ANY,
177                         "referral: URL (%s): contains DN\n",
178                         url, 0, 0 );
179 #endif
180                 rc = 1;
181
182         } else if( lurl->lud_attrs ) {
183 #ifdef NEW_LOGGING
184                 LDAP_LOG( CONFIG, CRIT, 
185                         "referral: URL (%s): requests attributes\n", url, 0, 0 );
186 #else
187                 Debug( LDAP_DEBUG_ANY,
188                         "referral: URL (%s): requests attributes\n",
189                         url, 0, 0 );
190 #endif
191                 rc = 1;
192
193         } else if( lurl->lud_scope != LDAP_SCOPE_DEFAULT ) {
194 #ifdef NEW_LOGGING
195                 LDAP_LOG( CONFIG, CRIT, 
196                         "referral: URL (%s): contains explicit scope\n", url, 0, 0 );
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, CRIT, 
207                         "referral: URL (%s): contains explicit filter\n", url, 0, 0 );
208 #else
209                 Debug( LDAP_DEBUG_ANY,
210                         "referral: URL (%s): contains explicit filter\n",
211                         url, 0, 0 );
212 #endif
213                 rc = 1;
214         }
215
216         ldap_free_urldesc( lurl );
217         return rc;
218 }
219
220 BerVarray referral_rewrite(
221         BerVarray in,
222         struct berval *base,
223         struct berval *target,
224         int scope )
225 {
226         int i;
227         BerVarray refs;
228         struct berval *iv, *jv;
229
230         if( in == NULL ) return NULL;
231
232         for( i=0; in[i].bv_val != NULL ; i++ ) {
233                 /* just count them */
234         }
235
236         if( i < 1 ) return NULL;
237
238         refs = SLAP_MALLOC( (i+1) * sizeof( struct berval ) );
239         if( refs == NULL ) {
240 #ifdef NEW_LOGGING
241                 LDAP_LOG( OPERATION, CRIT, 
242                         "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
243 #else
244                 Debug( LDAP_DEBUG_ANY,
245                         "referral_rewrite: SLAP_MALLOC failed\n", 0, 0, 0 );
246 #endif
247                 return NULL;
248         }
249
250         for( iv=in,jv=refs; iv->bv_val != NULL ; iv++ ) {
251                 LDAPURLDesc *url;
252                 int rc = ldap_url_parse_ext( iv->bv_val, &url );
253
254                 if( rc == LDAP_URL_ERR_BADSCHEME ) {
255                         ber_dupbv( jv++, iv );
256                         continue;
257
258                 } else if( rc != LDAP_URL_SUCCESS ) {
259                         continue;
260                 }
261
262                 {
263                         char *dn = url->lud_dn;
264                         url->lud_dn = referral_dn_muck(
265                                 ( dn && *dn ) ? dn : NULL,
266                                 base, target );
267
268                         ldap_memfree( dn );
269                 }
270
271                 if( url->lud_scope == LDAP_SCOPE_DEFAULT ) {
272                         url->lud_scope = scope;
273                 }
274
275                 jv->bv_val = ldap_url_desc2str( url );
276                 jv->bv_len = strlen( jv->bv_val );
277
278                 ldap_free_urldesc( url );
279                 jv++;
280         }
281
282         if( jv == refs ) {
283                 ch_free( refs );
284                 refs = NULL;
285
286         } else {
287                 jv->bv_val = NULL;
288         }
289
290         return refs;
291 }
292
293
294 BerVarray get_entry_referrals(
295         Operation *op,
296         Entry *e )
297 {
298         Attribute *attr;
299         BerVarray refs;
300         unsigned i;
301         struct berval *iv, *jv;
302
303         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
304
305         attr = attr_find( e->e_attrs, ad_ref );
306
307         if( attr == NULL ) return NULL;
308
309         for( i=0; attr->a_vals[i].bv_val != NULL; i++ ) {
310                 /* count references */
311         }
312
313         if( i < 1 ) return NULL;
314
315         refs = SLAP_MALLOC( (i + 1) * sizeof(struct berval));
316         if( refs == NULL ) {
317 #ifdef NEW_LOGGING
318                 LDAP_LOG( OPERATION, CRIT, 
319                         "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
320 #else
321                 Debug( LDAP_DEBUG_ANY,
322                         "get_entry_referrals: SLAP_MALLOC failed\n", 0, 0, 0 );
323 #endif
324                 return NULL;
325         }
326
327         for( iv=attr->a_vals, jv=refs; iv->bv_val != NULL; iv++ ) {
328                 unsigned k;
329                 ber_dupbv( jv, iv );
330
331                 /* trim the label */
332                 for( k=0; k<jv->bv_len; k++ ) {
333                         if( isspace( (unsigned char) jv->bv_val[k] ) ) {
334                                 jv->bv_val[k] = '\0';
335                                 jv->bv_len = k;
336                                 break;
337                         }
338                 }
339
340                 if(     jv->bv_len > 0 ) {
341                         jv++;
342                 } else {
343                         free( jv->bv_val );
344                 }
345         }
346
347         if( jv == refs ) {
348                 free( refs );
349                 refs = NULL;
350
351         } else {
352                 jv->bv_val = NULL;
353         }
354
355         /* we should check that a referral value exists... */
356         return refs;
357 }
358
359
360 int get_alias_dn(
361         Entry *e,
362         struct berval *ndn,
363         int *err,
364         const char **text )
365 {       
366         Attribute *a;
367         AttributeDescription *aliasedObjectName
368                 = slap_schema.si_ad_aliasedObjectName;
369
370         a = attr_find( e->e_attrs, aliasedObjectName );
371
372         if( a == NULL ) {
373                 /*
374                  * there was an aliasedobjectname defined but no data.
375                  */
376                 *err = LDAP_ALIAS_PROBLEM;
377                 *text = "alias missing aliasedObjectName attribute";
378                 return -1;
379         }
380
381         /* 
382          * aliasedObjectName should be SINGLE-VALUED with a single value. 
383          */                     
384         if ( a->a_vals[0].bv_val == NULL ) {
385                 /*
386                  * there was an aliasedobjectname defined but no data.
387                  */
388                 *err = LDAP_ALIAS_PROBLEM;
389                 *text = "alias missing aliasedObjectName value";
390                 return -1;
391         }
392
393         if( a->a_nvals[1].bv_val != NULL ) {
394                 *err = LDAP_ALIAS_PROBLEM;
395                 *text = "alias has multivalued aliasedObjectName";
396                 return -1;
397         }
398
399         *ndn = a->a_nvals[0];
400
401         return 0;
402 }
403