Debian Codegraph @

servers/slapd/back-asyncmeta/map.c

raw
1 /* map.c - ldap backend mapping routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2016-2024 The OpenLDAP Foundation.
6 * Portions Copyright 2016 Symas Corporation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17
18 /* ACKNOWLEDGEMENTS:
19 * This work was developed by Symas Corporation
20 * based on back-meta module for inclusion in OpenLDAP Software.
21 * This work was sponsored by Ericsson. */
22
23 /* This is an altered version */
24 /*
25 * Copyright 1999, Howard Chu, All rights reserved. <[email protected]>
26 *
27 * Permission is granted to anyone to use this software for any purpose
28 * on any computer system, and to alter it and redistribute it, subject
29 * to the following restrictions:
30 *
31 * 1. The author is not responsible for the consequences of use of this
32 * software, no matter how awful, even if they arise from flaws in it.
33 *
34 * 2. The origin of this software must not be misrepresented, either by
35 * explicit claim or by omission. Since few users ever read sources,
36 * credits should appear in the documentation.
37 *
38 * 3. Altered versions must be plainly marked as such, and must not be
39 * misrepresented as being the original software. Since few users
40 * ever read sources, credits should appear in the documentation.
41 *
42 * 4. This notice may not be removed or altered.
43 *
44 *
45 *
46 * Copyright 2016, Symas Corporation
47 *
48 * This is based on the back-meta/map.c version by Pierangelo Masarati.
49 * The previously reported conditions apply to the modified code as well.
50 * Changes in the original code are highlighted where required.
51 * Credits for the original code go to the author, Howard Chu.
52 */
53
54 #include "portable.h"
55
56 #include <stdio.h>
57
58 #include <ac/string.h>
59 #include <ac/socket.h>
60
61 #include "slap.h"
62 #include "lutil.h"
63 #include "../back-ldap/back-ldap.h"
64 #include "back-asyncmeta.h"
65
66 void
67 asyncmeta_referral_result_rewrite(
68 a_dncookie *dc,
69 BerVarray a_vals
70 )
71 {
72 int i, last;
73
74 assert( dc != NULL );
75 assert( a_vals != NULL );
76
77 for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ )
78 ;
79 last--;
80
81 for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) {
82 struct berval dn,
83 olddn = BER_BVNULL;
84 int rc;
85 LDAPURLDesc *ludp;
86
87 rc = ldap_url_parse( a_vals[ i ].bv_val, &ludp );
88 if ( rc != LDAP_URL_SUCCESS ) {
89 /* leave attr untouched if massage failed */
90 continue;
91 }
92
93 /* FIXME: URLs like "ldap:///dc=suffix" if passed
94 * thru ldap_url_parse() and ldap_url_desc2str()
95 * get rewritten as "ldap:///dc=suffix??base";
96 * we don't want this to occur... */
97 if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
98 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
99 }
100
101 ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
102
103 asyncmeta_dn_massage( dc, &olddn, &dn );
104 /* leave attr untouched if massage did nothing */
105 if ( olddn.bv_val != dn.bv_val )
106 {
107 char *newurl;
108
109 ludp->lud_dn = dn.bv_val;
110 newurl = ldap_url_desc2str( ludp );
111 dc->op->o_tmpfree( dn.bv_val, dc->memctx );
112 if ( newurl )
113 {
114 /* FIXME: leave attr untouched
115 * even if ldap_url_desc2str failed...
116 */
117
118 ber_memfree_x( a_vals[ i ].bv_val, dc->op->o_tmpmemctx );
119 ber_str2bv_x( newurl, 0, 1, &a_vals[ i ], dc->memctx );
120 ber_memfree( newurl );
121 ludp->lud_dn = olddn.bv_val;
122 }
123 }
124 ldap_free_urldesc( ludp );
125 }
126 }
127
128 void
129 asyncmeta_dnattr_result_rewrite(
130 a_dncookie *dc,
131 BerVarray a_vals
132 )
133 {
134 struct berval bv;
135 int i;
136
137 assert( a_vals != NULL );
138
139 for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
140 asyncmeta_dn_massage( dc, &a_vals[i], &bv );
141 if ( bv.bv_val != a_vals[i].bv_val ) {
142 ber_memfree_x( a_vals[i].bv_val, dc->memctx );
143 a_vals[i] = bv;
144 }
145 }
146 }
147
148 /*
149 * asyncmeta_dn_massage
150 *
151 * Aliases the suffix.
152 */
153 void
154 asyncmeta_dn_massage(
155 a_dncookie *dc,
156 struct berval *odn,
157 struct berval *res
158 )
159 {
160 struct berval pretty = {0,NULL}, *dn = odn;
161 struct berval *osuff, *nsuff;
162 int diff;
163
164 assert( res );
165
166 BER_BVZERO(res);
167 if ( dn == NULL )
168 return;
169
170 /* no suffix massage configured */
171 if ( !dc->target->mt_lsuffixm.bv_val ) {
172 *res = *dn;
173 return;
174 }
175
176 if ( dc->to_from == MASSAGE_REQ ) {
177 osuff = &dc->target->mt_lsuffixm;
178 nsuff = &dc->target->mt_rsuffixm;
179 } else {
180 osuff = &dc->target->mt_rsuffixm;
181 nsuff = &dc->target->mt_lsuffixm;
182 /* DN from remote server may be in arbitrary form.
183 * Pretty it so we can parse reliably.
184 */
185 dnPretty( NULL, dn, &pretty, dc->op->o_tmpmemctx );
186 if (pretty.bv_val) dn = &pretty;
187 }
188
189 diff = dn->bv_len - osuff->bv_len;
190 /* DN is shorter than suffix - ignore */
191 if ( diff < 0 ) {
192 ignore:
193 *res = *odn;
194 if (pretty.bv_val)
195 dc->op->o_tmpfree( pretty.bv_val, dc->op->o_tmpmemctx );
196 return;
197 }
198
199 /* DN longer than our suffix and doesn't match */
200 if ( osuff->bv_len != 0 && diff > 0 && !DN_SEPARATOR(dn->bv_val[diff-1]) )
201 goto ignore;
202
203 /* suffix is same length as ours, but doesn't match */
204 if ( strcasecmp( osuff->bv_val, &dn->bv_val[diff] ))
205 goto ignore;
206
207 /* if remote suffix is empty, remove or add the dn separator*/
208 if ( nsuff->bv_len == 0 ) {
209 diff--;
210 } else if ( osuff->bv_len == 0 ) {
211 diff++;
212 }
213
214
215 res->bv_len = diff + nsuff->bv_len;
216 res->bv_val = dc->op->o_tmpalloc( res->bv_len + 1, dc->memctx );
217 strncpy( res->bv_val, dn->bv_val, diff );
218 if ( osuff->bv_len == 0 )
219 res->bv_val[diff-1] = ',';
220 strcpy( &res->bv_val[diff], nsuff->bv_val );
221
222 if (pretty.bv_val)
223 dc->op->o_tmpfree( pretty.bv_val, dc->op->o_tmpmemctx );
224 }