]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/alias.c
9c4f1adeacb014b5ce9855cd6964f824ab9eaeb8
[openldap] / servers / slapd / back-ldbm / alias.c
1 /*
2  * Copyright (c) 1998 Will Ballantyne, ITSD, Government of BC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to ITSD, Government of BC. The name of ITSD
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <ac/socket.h>          /* Get struct sockaddr for slap.h */
18 #include "slap.h"
19 #include "back-ldbm.h"
20 #include "proto-back-ldbm.h"
21
22 /*
23  * given an alias object, dereference it to its end point.
24  * Entry returned has reader lock or is NULL.  Starting entry is not released.
25  */
26 Entry *derefAlias_r ( Backend     *be,
27                     Connection  *conn,
28                     Operation   *op,
29                     Entry       *e)
30 {
31   struct ldbminfo *li = (struct ldbminfo *) be->be_private; /* to free cache entries */
32   Attribute *a;
33   int       depth;
34   char      *matched;
35   Entry     *origDN = e;
36
37   if (!e) return NULL;  /* be sure we have a starting entry */
38
39   Debug( LDAP_DEBUG_TRACE, "<= checking for alias for dn %s\n", e->e_dn, 0, 0 );
40
41   /*
42    * try to deref fully, up to a maximum depth.  If the max depth exceeded
43    * then send an error
44    */
45   for ( depth = 0;
46         ( ( a = attr_find( e->e_attrs, "aliasedobjectname" ) ) != NULL) &&
47           ( depth < be->be_maxDerefDepth );
48         ++depth) 
49   {
50
51     /* 
52      * make sure there is a defined aliasedobjectname.  
53      * can only have one value so just use first value (0) in the attr list. 
54      */     
55     if (a->a_vals[0] && a->a_vals[0]->bv_val) {
56       char *newDN, *oldDN;
57
58       Debug( LDAP_DEBUG_TRACE, "<= %s is an alias for %s\n", 
59              e->e_dn, a->a_vals[0]->bv_val, 0 );
60       newDN = ch_strdup (a->a_vals[0]->bv_val);
61       oldDN = ch_strdup (e->e_ndn);
62
63       /* 
64        * release past lock if not original
65        */
66       if ( (depth > 0) && e ) {
67           cache_return_entry_r(&li->li_cache, e);       
68       }
69
70       /* make sure new and old DN are not same to avoid loops */
71       dn_normalize_case (newDN);
72       if ( strcmp (newDN, oldDN) == 0 ) {
73         
74         Debug( LDAP_DEBUG_TRACE, 
75                "<= %s alias is same as current %s\n", 
76                oldDN, newDN, 0 );
77         send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
78                           "Circular alias" );
79         free (newDN);
80         free (oldDN);
81         break;
82       }
83
84       /* make sure new and original are not same to avoid deadlocks */
85       if ( strcmp (newDN, origDN->e_ndn) == 0 ) {
86         Debug( LDAP_DEBUG_TRACE, 
87                "<= %s alias is same as original %s\n", 
88                oldDN, origDN->e_ndn, 0 );
89         send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
90                           "Circular alias" );
91         free (newDN);
92         free (oldDN);
93         break;
94       }
95
96       /*
97        * ok, so what happens if there is an alias in the DN of a dereferenced
98        * alias object?  
99        */
100       if ( (e = dn2entry_r( be, newDN, &matched )) == NULL ) {
101
102         /* could not deref return error  */
103         Debug( LDAP_DEBUG_TRACE, 
104                "<= %s is a dangling alias to %s\n", 
105                oldDN, newDN, 0 );
106         send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
107                           "Dangling Alias" );
108
109         if (matched != NULL) free(matched);
110         free (newDN);
111         free (oldDN);
112         break;
113       }
114
115       free (newDN);
116       free (oldDN);
117     }
118     else {
119       /*
120        * there was an aliasedobjectname defined but no data.
121        * this can't happen, right?
122        */
123         Debug( LDAP_DEBUG_TRACE, 
124                "<= %s has no data in aliasedobjectname attribute\n", 
125                (e && e->e_dn) ? e->e_dn : "(null)", 0, 0 );
126         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM, "",
127                           "Alias missing aliasedobjectname" );
128         break;
129     }
130   }
131
132   /*
133    * warn if we pulled out due to exceeding the maximum deref depth
134    */
135   if ( depth >= be->be_maxDerefDepth ) {
136     Debug( LDAP_DEBUG_TRACE, 
137            "<= deref(\"%s\") exceeded maximum deref depth (%d) at \"%s\"\n", 
138            origDN->e_dn ? origDN->e_dn : "(null)", 
139            be->be_maxDerefDepth, 
140            (e && e->e_ndn) ? e->e_ndn : "(null)");
141     send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
142                         "Maximum alias dereference depth exceeded" );
143   }
144
145   return e;
146 }
147
148 /*
149  * given a DN fully deref it and return the real DN or original DN if it fails
150  * This involves finding the last matched part then reconstructing forward
151  * e.g. 
152  * ou=MyOU,o=MyAliasedOrg,c=MyCountry where o=MyAliasedOrg is an alias for o=MyOrg
153  * loop starts with newDN = ou=MyOU,o=MyAliasedOrg,c=MyCountry
154  *   dn2entry_r on newDN gives null entry and o=MyAliasedOrg,c=MyCountry matched
155  *   dn2entry_r on matched gives o=MyAliasedOrg,c=MyCountry entry
156  *   remainder is ou=MyOU
157  *   dereferencing o=MyAliasedOrg,c=MyCountry yields entry o=MyOrg,c=MyCountry
158  *   release lock on o=MyAliasedOrg,c=MyCountry entry
159  *   reconstructed dn is ou=MyOU,o=MyOrg,c=MyCountry
160  *   release lock on o=MyOrg,c=MyCountry entry
161  */
162 char *derefDN ( Backend     *be,
163                 Connection  *conn,
164                 Operation   *op,
165                 char        *dn
166 )
167 {
168   struct ldbminfo *li = (struct ldbminfo *) be->be_private;
169   char  *matched = 0;
170   char  *newDN = NULL;
171   int   depth, i;
172   Entry         *eMatched;
173   Entry         *eDeref;
174   Entry         *eNew;
175   
176   if (!dn) return NULL; 
177
178   Debug( LDAP_DEBUG_TRACE, 
179          "<= dereferencing dn: \"%s\"\n", 
180          dn, 0, 0 );
181   
182   newDN = ch_strdup ( dn );
183
184   /* while we don't have a matched dn, deref the DN */
185   for ( depth = 0;
186         ( (eMatched = dn2entry_r( be, newDN, &matched )) == NULL) &&
187           (depth < be->be_maxDerefDepth);
188         ++depth ) {
189     
190     if ((matched != NULL) && *matched) {        
191       char *submatch;
192    
193       /* 
194        * make sure there actually is an entry for the matched part 
195        */
196       if ( (eMatched = dn2entry_r( be, matched, &submatch )) != NULL) {
197         char  *remainder; /* part before the aliased part */
198         int  rlen = strlen(newDN) - strlen(matched);
199         
200         Debug( LDAP_DEBUG_TRACE, "<= matched %s\n", matched, 0, 0 );
201         
202         remainder = ch_malloc (rlen + 1);
203         strncpy ( remainder, newDN, rlen );
204         remainder[rlen] = '\0';
205         
206         Debug( LDAP_DEBUG_TRACE, "<= remainder %s\n", remainder, 0, 0 );
207         
208         if ((eNew = derefAlias_r( be, conn, op, eMatched )) == NULL) {
209           free (matched);
210           matched = NULL;
211           free (newDN);
212           newDN = NULL;
213           free (remainder);
214           remainder = NULL;
215           
216           cache_return_entry_r(&li->li_cache, eMatched);
217           eMatched = NULL;
218           break; /*  no associated entry, dont deref */
219         }
220         else {
221
222           Debug( LDAP_DEBUG_TRACE, "<= l&g we have %s vs %s \n", matched, eNew->e_dn, 0 );
223
224           i = strcasecmp (matched, eNew->e_dn);
225           /* free reader lock */
226           cache_return_entry_r(&li->li_cache, eNew);
227
228           free (matched);
229           matched = NULL;
230
231           if (! i) {
232             /* newDN same as old so not an alias, no need to go further */
233             free (newDN);
234             newDN = NULL;
235             free (remainder);
236
237             cache_return_entry_r(&li->li_cache, eMatched);
238             eMatched = NULL;
239             break;
240           }
241
242           /* 
243            * we have dereferenced the aliased part so put
244            * the new dn together
245            */
246           free (newDN);
247           newDN = ch_malloc (strlen(eMatched->e_dn) + rlen + 1);
248           strcpy (newDN, remainder);
249           strcat (newDN, eMatched->e_dn);
250           Debug( LDAP_DEBUG_TRACE, "<= expanded to %s\n", newDN, 0, 0 );
251
252           free (remainder);
253         }
254         /* free reader lock */
255         cache_return_entry_r(&li->li_cache, eMatched);
256       }
257       else {
258         if(submatch != NULL) free(submatch);
259         break; /* there was no entry for the matched part */
260       }
261     }
262     else {
263       break; /* there was no matched part */
264     }
265   }
266   
267   /* release lock if a match terminated the loop, there should be no
268    * outstanding locks at this point
269    */
270   if(eMatched != NULL) {
271     /* free reader lock */
272     cache_return_entry_r(&li->li_cache, eMatched);
273   }
274
275   /*
276    * the final part of the DN might be an alias so try to dereference it.
277    * e.g. if we had started with dn = o=MyAliasedOrg,c=MyCountry the dn would match
278    * and the above loop complete but we would still be left with an aliased DN.
279    */
280   if (newDN != NULL) {
281     if ( (eNew = dn2entry_r( be, newDN, &matched )) != NULL) {
282       if ((eDeref = derefAlias_r( be, conn, op, eNew )) != NULL) {
283         free (newDN);
284         newDN = ch_strdup (eDeref->e_dn);
285         /* free reader lock */
286         cache_return_entry_r(&li->li_cache, eDeref);
287       }
288       /* free reader lock */
289       cache_return_entry_r(&li->li_cache, eNew);
290     }
291   }
292   if (matched != NULL) free(matched);
293   
294   /*
295    * warn if we exceeded the max depth as the resulting DN may not be dereferenced
296    */
297   if (depth >= be->be_maxDerefDepth) {
298     if (newDN) {
299       Debug( LDAP_DEBUG_TRACE, 
300              "<= max deref depth exceeded in derefDN for \"%s\", result \"%s\"\n", 
301              dn, newDN, 0 );
302       free (newDN);
303       newDN = NULL;
304     }
305     else {
306       Debug( LDAP_DEBUG_TRACE, 
307              "<= max deref depth exceeded in derefDN for \"%s\", result NULL\n", 
308              dn, 0, 0 );
309     }
310     send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
311                       "Maximum alias dereference depth exceeded for base" );
312   }
313
314   if (newDN == NULL) {
315     newDN = ch_strdup ( dn );
316   }
317   
318   Debug( LDAP_DEBUG_TRACE, "<= returning deref DN of \"%s\"\n", newDN, 0, 0 ); 
319
320   return newDN;
321 }