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