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 */
37 if (!e) return NULL; /* be sure we have a starting entry */
39 Debug( LDAP_DEBUG_TRACE, "<= checking for alias for dn %s\n", e->e_dn, 0, 0 );
42 * try to deref fully, up to a maximum depth. If the max depth exceeded
46 ( ( a = attr_find( e->e_attrs, "aliasedobjectname" ) ) != NULL) &&
47 ( depth < be->be_maxDerefDepth );
52 * make sure there is a defined aliasedobjectname.
53 * can only have one value so just use first value (0) in the attr list.
55 if (a->a_vals[0] && a->a_vals[0]->bv_val) {
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);
64 * release past lock if not original
66 if ( (depth > 0) && e ) {
67 cache_return_entry_r(&li->li_cache, e);
70 /* make sure new and old DN are not same to avoid loops */
71 dn_normalize_case (newDN);
72 if ( strcmp (newDN, oldDN) == 0 ) {
74 Debug( LDAP_DEBUG_TRACE,
75 "<= %s alias is same as current %s\n",
77 send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
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, "",
97 * ok, so what happens if there is an alias in the DN of a dereferenced
100 if ( (e = dn2entry_r( be, newDN, &matched )) == NULL ) {
102 /* could not deref return error */
103 Debug( LDAP_DEBUG_TRACE,
104 "<= %s is a dangling alias to %s\n",
106 send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
109 if (matched != NULL) free(matched);
120 * there was an aliasedobjectname defined but no data.
121 * this can't happen, right?
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" );
133 * warn if we pulled out due to exceeding the maximum deref depth
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" );
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
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
162 char *derefDN ( Backend *be,
168 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
176 if (!dn) return NULL;
178 Debug( LDAP_DEBUG_TRACE,
179 "<= dereferencing dn: \"%s\"\n",
182 newDN = ch_strdup ( dn );
184 /* while we don't have a matched dn, deref the DN */
186 ( (eMatched = dn2entry_r( be, newDN, &matched )) == NULL) &&
187 (depth < be->be_maxDerefDepth);
190 if ((matched != NULL) && *matched) {
194 * make sure there actually is an entry for the matched part
196 if ( (eMatched = dn2entry_r( be, matched, &submatch )) != NULL) {
197 char *remainder; /* part before the aliased part */
198 int rlen = strlen(newDN) - strlen(matched);
200 Debug( LDAP_DEBUG_TRACE, "<= matched %s\n", matched, 0, 0 );
202 remainder = ch_malloc (rlen + 1);
203 strncpy ( remainder, newDN, rlen );
204 remainder[rlen] = '\0';
206 Debug( LDAP_DEBUG_TRACE, "<= remainder %s\n", remainder, 0, 0 );
208 if ((eNew = derefAlias_r( be, conn, op, eMatched )) == NULL) {
216 cache_return_entry_r(&li->li_cache, eMatched);
218 break; /* no associated entry, dont deref */
222 Debug( LDAP_DEBUG_TRACE, "<= l&g we have %s vs %s \n", matched, eNew->e_dn, 0 );
224 i = strcasecmp (matched, eNew->e_dn);
225 /* free reader lock */
226 cache_return_entry_r(&li->li_cache, eNew);
232 /* newDN same as old so not an alias, no need to go further */
237 cache_return_entry_r(&li->li_cache, eMatched);
243 * we have dereferenced the aliased part so put
244 * the new dn together
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 );
254 /* free reader lock */
255 cache_return_entry_r(&li->li_cache, eMatched);
258 if(submatch != NULL) free(submatch);
259 break; /* there was no entry for the matched part */
263 break; /* there was no matched part */
267 /* release lock if a match terminated the loop, there should be no
268 * outstanding locks at this point
270 if(eMatched != NULL) {
271 /* free reader lock */
272 cache_return_entry_r(&li->li_cache, eMatched);
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.
280 if ( (eNew = dn2entry_r( be, newDN, &matched )) != NULL) {
281 if ((eDeref = derefAlias_r( be, conn, op, eNew )) != NULL) {
283 newDN = ch_strdup (eDeref->e_dn);
284 /* free reader lock */
285 cache_return_entry_r(&li->li_cache, eDeref);
287 /* free reader lock */
288 cache_return_entry_r(&li->li_cache, eNew);
290 if (matched != NULL) free(matched);
293 * warn if we exceeded the max depth as the resulting DN may not be dereferenced
295 if (depth >= be->be_maxDerefDepth) {
297 Debug( LDAP_DEBUG_TRACE,
298 "<= max deref depth exceeded in derefDN for \"%s\", result \"%s\"\n",
304 Debug( LDAP_DEBUG_TRACE,
305 "<= max deref depth exceeded in derefDN for \"%s\", result NULL\n",
308 send_ldap_result( conn, op, LDAP_ALIAS_DEREF_PROBLEM, "",
309 "Maximum alias dereference depth exceeded for base" );
313 newDN = ch_strdup ( dn );
316 Debug( LDAP_DEBUG_TRACE, "<= returning deref DN of \"%s\"\n", newDN, 0, 0 );