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