2 * Copyright (c) 1998 Will Ballantyne, ITSD, Government of BC
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.
17 #include <ac/socket.h> /* Get struct sockaddr for slap.h */
19 #include "back-ldbm.h"
20 #include "proto-back-ldbm.h"
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.
26 Entry *derefAlias_r ( Backend *be,
31 struct ldbminfo *li = (struct ldbminfo *) be->be_private; /* to free cache entries */
38 if (!e) return NULL; /* be sure we have a starting entry */
40 Debug( LDAP_DEBUG_TRACE, "<= checking for alias for dn %s\n", e->e_dn, 0, 0 );
43 * try to deref fully, up to a maximum depth. If the max depth exceeded
47 ( ( a = attr_find( e->e_attrs, "aliasedobjectname" ) ) != NULL) &&
48 ( depth < be->be_maxDerefDepth );
53 * make sure there is a defined aliasedobjectname.
54 * can only have one value so just use first value (0) in the attr list.
56 if (a->a_vals[0] && a->a_vals[0]->bv_val) {
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);
65 * release past lock if not original
67 if ( (depth > 0) && e ) {
68 cache_return_entry_r(&li->li_cache, e);
71 /* make sure new and old DN are not same to avoid loops */
72 dn_normalize_case (newDN);
73 if ( strcmp (newDN, oldDN) == 0 ) {
75 Debug( LDAP_DEBUG_TRACE,
76 "<= %s alias is same as current %s\n",
78 send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
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, "",
98 * ok, so what happens if there is an alias in the DN of a dereferenced
101 if ( (e = dn2entry_r( be, newDN, &matched )) == NULL ) {
103 /* could not deref return error */
104 Debug( LDAP_DEBUG_TRACE,
105 "<= %s is a dangling alias to %s\n",
107 send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
110 if (matched != NULL) free(matched);
121 * there was an aliasedobjectname defined but no data.
122 * this can't happen, right?
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" );
134 * warn if we pulled out due to exceeding the maximum deref depth
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" );
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
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
163 char *derefDN ( Backend *be,
169 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
177 if (!dn) return NULL;
179 Debug( LDAP_DEBUG_TRACE,
180 "<= dereferencing dn: \"%s\"\n",
183 newDN = ch_strdup ( dn );
185 /* while we don't have a matched dn, deref the DN */
187 ( (eMatched = dn2entry_r( be, newDN, &matched )) == NULL) &&
188 (depth < be->be_maxDerefDepth);
191 if ((matched != NULL) && *matched) {
195 * make sure there actually is an entry for the matched part
197 if ( (eMatched = dn2entry_r( be, matched, &submatch )) != NULL) {
198 char *remainder; /* part before the aliased part */
199 int rlen = strlen(newDN) - strlen(matched);
201 Debug( LDAP_DEBUG_TRACE, "<= matched %s\n", matched, 0, 0 );
203 remainder = ch_malloc (rlen + 1);
204 strncpy ( remainder, newDN, rlen );
205 remainder[rlen] = '\0';
207 Debug( LDAP_DEBUG_TRACE, "<= remainder %s\n", remainder, 0, 0 );
209 if ((eNew = derefAlias_r( be, conn, op, eMatched )) == NULL) {
217 cache_return_entry_r(&li->li_cache, eMatched);
219 break; /* no associated entry, dont deref */
223 Debug( LDAP_DEBUG_TRACE, "<= l&g we have %s vs %s \n", matched, eNew->e_dn, 0 );
225 i = strcasecmp (matched, eNew->e_dn);
226 /* free reader lock */
227 cache_return_entry_r(&li->li_cache, eNew);
233 /* newDN same as old so not an alias, no need to go further */
238 cache_return_entry_r(&li->li_cache, eMatched);
244 * we have dereferenced the aliased part so put
245 * the new dn together
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 );
255 /* free reader lock */
256 cache_return_entry_r(&li->li_cache, eMatched);
259 if(submatch != NULL) free(submatch);
260 break; /* there was no entry for the matched part */
264 break; /* there was no matched part */
268 /* release lock if a match terminated the loop, there should be no
269 * outstanding locks at this point
271 if(eMatched != NULL) {
272 /* free reader lock */
273 cache_return_entry_r(&li->li_cache, eMatched);
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.
281 if ( (eNew = dn2entry_r( be, newDN, &matched )) != NULL) {
282 if ((eDeref = derefAlias_r( be, conn, op, eNew )) != NULL) {
284 newDN = ch_strdup (eDeref->e_dn);
285 /* free reader lock */
286 cache_return_entry_r(&li->li_cache, eDeref);
288 /* free reader lock */
289 cache_return_entry_r(&li->li_cache, eNew);
291 if (matched != NULL) free(matched);
294 * warn if we exceeded the max depth as the resulting DN may not be dereferenced
296 if (depth >= be->be_maxDerefDepth) {
298 Debug( LDAP_DEBUG_TRACE,
299 "<= max deref depth exceeded in derefDN for \"%s\", result \"%s\"\n",
305 Debug( LDAP_DEBUG_TRACE,
306 "<= max deref depth exceeded in derefDN for \"%s\", result NULL\n",
309 send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
310 "Maximum alias dereference depth exceeded for base" );
314 newDN = ch_strdup ( dn );
317 Debug( LDAP_DEBUG_TRACE, "<= returning deref DN of \"%s\"\n", newDN, 0, 0 );