summaryrefslogtreecommitdiffstats
path: root/servers/slapd/back-asyncmeta/map.c
blob: b811708762f735f0f545a8bf1de3b202746cd832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* map.c - ldap backend mapping routines */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 2016-2022 The OpenLDAP Foundation.
 * Portions Copyright 2016 Symas Corporation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */

/* ACKNOWLEDGEMENTS:
 * This work was developed by Symas Corporation
 * based on back-meta module for inclusion in OpenLDAP Software.
 * This work was sponsored by Ericsson. */

/* This is an altered version */
/*
 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
 *
 * Permission is granted to anyone to use this software for any purpose
 * on any computer system, and to alter it and redistribute it, subject
 * to the following restrictions:
 *
 * 1. The author is not responsible for the consequences of use of this
 *    software, no matter how awful, even if they arise from flaws in it.
 *
 * 2. The origin of this software must not be misrepresented, either by
 *    explicit claim or by omission.  Since few users ever read sources,
 *    credits should appear in the documentation.
 *
 * 3. Altered versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.  Since few users
 *    ever read sources, credits should appear in the documentation.
 *
 * 4. This notice may not be removed or altered.
 *
 *
 *
 * Copyright 2016, Symas Corporation
 *
 * This is based on the back-meta/map.c version by Pierangelo Masarati.
 * The previously reported conditions apply to the modified code as well.
 * Changes in the original code are highlighted where required.
 * Credits for the original code go to the author, Howard Chu.
 */

#include "portable.h"

#include <stdio.h>

#include <ac/string.h>
#include <ac/socket.h>

#include "slap.h"
#include "lutil.h"
#include "../back-ldap/back-ldap.h"
#include "back-asyncmeta.h"

void
asyncmeta_referral_result_rewrite(
	a_dncookie		*dc,
	BerVarray		a_vals
)
{
	int		i, last;

	assert( dc != NULL );
	assert( a_vals != NULL );

	for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ )
		;
	last--;

	for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) {
		struct berval	dn,
				olddn = BER_BVNULL;
		int		rc;
		LDAPURLDesc	*ludp;

		rc = ldap_url_parse( a_vals[ i ].bv_val, &ludp );
		if ( rc != LDAP_URL_SUCCESS ) {
			/* leave attr untouched if massage failed */
			continue;
		}

		/* FIXME: URLs like "ldap:///dc=suffix" if passed
		 * thru ldap_url_parse() and ldap_url_desc2str()
		 * get rewritten as "ldap:///dc=suffix??base";
		 * we don't want this to occur... */
		if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
			ludp->lud_scope = LDAP_SCOPE_DEFAULT;
		}

		ber_str2bv( ludp->lud_dn, 0, 0, &olddn );

		asyncmeta_dn_massage( dc, &olddn, &dn );
		/* leave attr untouched if massage did nothing */
		if ( olddn.bv_val != dn.bv_val )
		{
			char	*newurl;

			ludp->lud_dn = dn.bv_val;
			newurl = ldap_url_desc2str( ludp );
			dc->op->o_tmpfree( dn.bv_val, dc->memctx );
			if ( newurl )
			{
				/* FIXME: leave attr untouched
				 * even if ldap_url_desc2str failed...
				 */

				ber_memfree_x( a_vals[ i ].bv_val, dc->op->o_tmpmemctx );
				ber_str2bv_x( newurl, 0, 1, &a_vals[ i ], dc->memctx );
				ber_memfree( newurl );
				ludp->lud_dn = olddn.bv_val;
			}
		}
		ldap_free_urldesc( ludp );
	}
}

void
asyncmeta_dnattr_result_rewrite(
	a_dncookie		*dc,
	BerVarray		a_vals
)
{
	struct berval	bv;
	int		i;

	assert( a_vals != NULL );

	for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
		asyncmeta_dn_massage( dc, &a_vals[i], &bv );
		if ( bv.bv_val != a_vals[i].bv_val ) {
			ber_memfree_x( a_vals[i].bv_val, dc->memctx );
			a_vals[i] = bv;
		}
	}
}

/*
 * asyncmeta_dn_massage
 *
 * Aliases the suffix.
 */
void
asyncmeta_dn_massage(
	a_dncookie *dc,
	struct berval *odn,
	struct berval *res
)
{
	struct berval pretty = {0,NULL}, *dn = odn;
	struct berval *osuff, *nsuff;
	int diff;

	assert( res );

	BER_BVZERO(res);
	if ( dn == NULL )
		return;

	/* no suffix massage configured */
	if ( !dc->target->mt_lsuffixm.bv_val ) {
		*res = *dn;
		return;
	}

	if ( dc->to_from == MASSAGE_REQ ) {
		osuff = &dc->target->mt_lsuffixm;
		nsuff = &dc->target->mt_rsuffixm;
	} else {
		osuff = &dc->target->mt_rsuffixm;
		nsuff = &dc->target->mt_lsuffixm;
		/* DN from remote server may be in arbitrary form.
		 * Pretty it so we can parse reliably.
		 */
		dnPretty( NULL, dn, &pretty, dc->op->o_tmpmemctx );
		if (pretty.bv_val) dn = &pretty;
	}

	diff = dn->bv_len - osuff->bv_len;
	/* DN is shorter than suffix - ignore */
	if ( diff < 0 ) {
ignore:
		*res = *odn;
		if (pretty.bv_val)
			dc->op->o_tmpfree( pretty.bv_val, dc->op->o_tmpmemctx );
		return;
	}

	/* DN longer than our suffix and doesn't match */
	if ( diff > 0 && !DN_SEPARATOR(dn->bv_val[diff-1]))
		goto ignore;

	/* suffix is same length as ours, but doesn't match */
	if ( strcasecmp( osuff->bv_val, &dn->bv_val[diff] ))
		goto ignore;

	res->bv_len = diff + nsuff->bv_len;
	res->bv_val = dc->op->o_tmpalloc( res->bv_len + 1, dc->memctx );
	strncpy( res->bv_val, dn->bv_val, diff );
	strcpy( &res->bv_val[diff], nsuff->bv_val );

	if (pretty.bv_val)
		dc->op->o_tmpfree( pretty.bv_val, dc->op->o_tmpmemctx );
}