]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/alias.c
Import nextid cleanup from devel.
[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 #ifdef SLAPD_ALIAS_DEREF
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <ac/socket.h>          /* Get struct sockaddr for slap.h */
20 #include "slap.h"
21 #include "back-ldbm.h"
22 #include "proto-back-ldbm.h"
23
24 /*
25  * given an alias object, dereference it to its end point.
26  * entry returned has reader lock 
27  */
28 Entry *derefAlias_r ( Backend     *be,
29                     Connection  *conn,
30                     Operation   *op,
31                     Entry       *e)
32 {
33   Attribute *a;
34   int       depth;
35   char      **pastAliases;
36   char      *matched;
37
38   Debug( LDAP_DEBUG_TRACE, "<= checking for alias for dn %s\n", e->e_dn, 0, 0 );
39
40   /*
41    * try to deref fully, up to a maximum depth.  If the max depth exceeded
42    * then send an error
43    */
44   for ( depth = 0;
45         ( ( a = attr_find( e->e_attrs, "aliasedobjectname" ) ) != NULL) &&
46           ( depth < be->be_maxDerefDepth );
47         ++depth) 
48   {
49
50     /* 
51      * make sure there is a defined aliasedobjectname.  
52      * can only have one value so just use first value (0) in the attr list. 
53      */     
54     if (a->a_vals[0] && a->a_vals[0]->bv_val) {
55       char *newDN, *oldDN;
56
57       Debug( LDAP_DEBUG_TRACE, "<= %s is an alias for %s\n", 
58              e->e_dn, a->a_vals[0]->bv_val, 0 );
59       newDN = ch_strdup (a->a_vals[0]->bv_val);
60       oldDN = ch_strdup (e->e_dn);
61
62       /*
63        * ok, so what happens if there is an alias in the DN of a dereferenced
64        * alias object?  
65        */
66       if ( (e = dn2entry_r( be, newDN, &matched )) == NULL ) {
67
68         /* could not deref return error  */
69         Debug( LDAP_DEBUG_TRACE, 
70                "<= %s is a dangling alias to %s\n", 
71                oldDN, newDN, 0 );
72         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM, "",
73                           "Dangling Alias" );
74
75                         if(matched != NULL) free(matched);
76       }
77       free (newDN);
78       free (oldDN);
79     }
80     else {
81       /*
82        * there was an aliasedobjectname defined but no data.
83        * this can't happen, right?
84        */
85         Debug( LDAP_DEBUG_TRACE, 
86                "<= %s has no data in aliasedobjectname attribute\n", 
87                e->e_dn, 0, 0 );
88         send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM, "",
89                           "Alias missing aliasedobjectname" );
90     }
91   }
92
93   /*
94    * warn if we pulled out due to exceeding the maximum deref depth
95    */
96   if ( depth >= be->be_maxDerefDepth ) {
97     Debug( LDAP_DEBUG_TRACE, 
98            "<= %s exceeded maximum deref depth %d\n", 
99            e->e_dn, be->be_maxDerefDepth, 0 );
100     send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM, "",
101                         "Maximum alias dereference depth exceeded" );
102   }
103
104   return e;
105 }
106
107 /*
108  * given a DN fully deref it and return the real DN or original DN if it fails
109  */
110 char *derefDN ( Backend     *be,
111                 Connection  *conn,
112                 Operation   *op,
113                 char        *dn
114 )
115 {
116   struct ldbminfo *li = (struct ldbminfo *) be->be_private;
117   char  *matched = 0;
118   char  *newDN = NULL;
119   int   depth, i;
120   Entry         *eMatched;
121   Entry         *eDeref;
122   Entry         *eNew;
123   
124
125   Debug( LDAP_DEBUG_TRACE, 
126          "<= dereferencing dn: \"%s\"\n", 
127          dn, 0, 0 );
128   
129   newDN = ch_strdup ( dn );
130
131   /* while we don't have a matched dn, deref the DN */
132   for ( depth = 0;
133         ( (eMatched = dn2entry_r( be, newDN, &matched )) == NULL) &&
134           (depth < be->be_maxDerefDepth);
135         ++depth ) {
136     
137     if ((matched != NULL) && *matched) {        
138       char *submatch;
139       
140       /* 
141        * make sure there actually is an entry for the matched part 
142        */
143       if ( (eMatched = dn2entry_r( be, matched, &submatch )) != NULL) {
144         char  *remainder; /* part before the aliased part */
145         int  rlen = strlen(newDN) - strlen(matched);
146         
147         Debug( LDAP_DEBUG_TRACE, "<= matched %s\n", matched, 0, 0 );
148         
149         remainder = ch_malloc (rlen + 1);
150         strncpy ( remainder, newDN, rlen );
151         remainder[rlen] = '\0';
152         
153         Debug( LDAP_DEBUG_TRACE, "<= remainder %s\n", remainder, 0, 0 );
154         
155         if ((eNew = derefAlias_r( be, conn, op, eMatched )) == NULL) {
156           free (matched);
157           matched = NULL;
158           free (newDN);
159           newDN = NULL;
160           free (remainder);
161           remainder = NULL;
162           break; /*  no associated entry, dont deref */
163         }
164         else {
165
166           Debug( LDAP_DEBUG_TRACE, "<= l&g we have %s vs %s \n", matched, eNew->e_dn, 0 );
167
168           i = strcasecmp (matched, eNew->e_dn);
169           /* free reader lock */
170           cache_return_entry_r(&li->li_cache, eNew);
171           if (! i) {
172             /* newDN same as old so not an alias, no need to go further */
173             free (newDN);
174             newDN = NULL;
175             free (matched);
176             matched = NULL;
177             free (remainder);
178             break;
179           }
180
181           /* 
182            * we have dereferenced the aliased part so put
183            * the new dn together
184            */
185           free (newDN);
186           newDN = ch_malloc (strlen(eMatched->e_dn) + rlen + 1);
187           strcpy (newDN, remainder);
188           strcat (newDN, eMatched->e_dn);
189           Debug( LDAP_DEBUG_TRACE, "<= expanded to %s\n", newDN, 0, 0 );
190
191           free (matched);
192           matched = NULL;
193           free (remainder);
194         }
195         /* free reader lock */
196         cache_return_entry_r(&li->li_cache, eMatched);
197       }
198       else {
199         if(submatch != NULL) free(submatch);
200         break; /* there was no entry for the matched part */
201       }
202     }
203     else {
204       break; /* there was no matched part */
205     }
206   }
207   
208   if(eMatched != NULL) {
209     /* free reader lock */
210     cache_return_entry_r(&li->li_cache, eMatched);
211   }
212
213   /*
214    * the final part of the DN might be an alias 
215    * so try to dereference it.
216    */
217   if ( (eNew = dn2entry_r( be, newDN, &matched )) != NULL) {
218     if ((eDeref = derefAlias_r( be, conn, op, eNew )) != NULL) {
219       free (newDN);
220       newDN = ch_strdup (eDeref->e_dn);
221       /* free reader lock */
222       cache_return_entry_r(&li->li_cache, eDeref);
223     }
224     /* free reader lock */
225     cache_return_entry_r(&li->li_cache, eNew);
226   }
227   
228   /*
229    * warn if we exceeded the max depth as the resulting DN may not be dereferenced
230    */
231   if (depth >= be->be_maxDerefDepth) {
232     Debug( LDAP_DEBUG_TRACE, 
233            "<= max deref depth exceeded in derefDN for \"%s\", result \"%s\"\n", 
234            dn, newDN, 0 );
235     send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM, "",
236                       "Maximum alias dereference depth exceeded for base" );
237   }
238
239   if (newDN == NULL) {
240     newDN = ch_strdup ( dn );
241   }
242   
243   Debug( LDAP_DEBUG_TRACE, "<= returning deref DN of \"%s\"\n", newDN, 0, 0 ); 
244   if (matched != NULL) free(matched);
245
246   return newDN;
247 }
248
249 #endif /* SLAPD_ALIAS_DEREF */