]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmdn.c
9348c70f119ad1cfa80e1fc44af7b6ebdcc36e53
[openldap] / servers / slapd / overlays / rwmdn.c
1 /* rwmdn.c - massages dns */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2007 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24
25 #include "portable.h"
26
27 #ifdef SLAPD_OVER_RWM
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/socket.h>
33
34 #include "slap.h"
35 #include "rwm.h"
36
37 /* FIXME: after rewriting, we should also remap attributes ...  */
38
39 /*
40  * massages "in" and normalizes it into "ndn"
41  *
42  * "ndn" may be untouched if no massaging occurred and its value was not null
43  */
44 int
45 rwm_dn_massage_normalize(
46         dncookie *dc,
47         struct berval *in,
48         struct berval *ndn )
49 {
50         int             rc;
51         struct berval   mdn = BER_BVNULL;
52         
53         /* massage and normalize a DN */
54         rc = rwm_dn_massage( dc, in, &mdn );
55         if ( rc != LDAP_SUCCESS ) {
56                 return rc;
57         }
58
59         if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( ndn ) ) {
60                 return rc;
61         }
62
63         rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
64
65         if ( mdn.bv_val != in->bv_val ) {
66                 ch_free( mdn.bv_val );
67         }
68
69         return rc;
70 }
71
72 /*
73  * massages "in" and prettifies it into "pdn"
74  *
75  * "pdn" may be untouched if no massaging occurred and its value was not null
76  */
77 int
78 rwm_dn_massage_pretty(
79         dncookie *dc,
80         struct berval *in,
81         struct berval *pdn )
82 {
83         int             rc;
84         struct berval   mdn = BER_BVNULL;
85         
86         /* massage and pretty a DN */
87         rc = rwm_dn_massage( dc, in, &mdn );
88         if ( rc != LDAP_SUCCESS ) {
89                 return rc;
90         }
91
92         if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
93                 return rc;
94         }
95
96         rc = dnPretty( NULL, &mdn, pdn, NULL );
97
98         if ( mdn.bv_val != in->bv_val ) {
99                 ch_free( mdn.bv_val );
100         }
101
102         return rc;
103 }
104
105 /*
106  * massages "in" and prettifies and normalizes it into "pdn" and "ndn"
107  *
108  * "pdn" may be untouched if no massaging occurred and its value was not null;
109  * "ndn" may be untouched if no massaging occurred and its value was not null;
110  * if no massage occurred and "ndn" value was not null, it is filled
111  * with the normaized value of "pdn", much like ndn = dnNormalize( pdn )
112  */
113 int
114 rwm_dn_massage_pretty_normalize(
115         dncookie *dc,
116         struct berval *in,
117         struct berval *pdn,
118         struct berval *ndn )
119 {
120         int             rc;
121         struct berval   mdn = BER_BVNULL;
122         
123         /* massage, pretty and normalize a DN */
124         rc = rwm_dn_massage( dc, in, &mdn );
125         if ( rc != LDAP_SUCCESS ) {
126                 return rc;
127         }
128
129         if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
130                 if ( BER_BVISNULL( ndn ) ) {
131                         rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
132                 }
133                 return rc;
134         }
135
136         rc = dnPrettyNormal( NULL, &mdn, pdn, ndn, NULL );
137
138         if ( mdn.bv_val != in->bv_val ) {
139                 ch_free( mdn.bv_val );
140         }
141
142         return rc;
143 }
144
145 #ifdef ENABLE_REWRITE
146 /*
147  * massages "in" into "dn"
148  * 
149  * "dn" may contain the value of "in" if no massage occurred
150  */
151 int
152 rwm_dn_massage(
153         dncookie *dc,
154         struct berval *in,
155         struct berval *dn
156 )
157 {
158         int             rc = 0;
159         struct berval   mdn;
160         static char     *dmy = "";
161
162         assert( dc != NULL );
163         assert( in != NULL );
164         assert( dn != NULL );
165
166         rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
167                         ( in->bv_val ? in->bv_val : dmy ), 
168                         dc->conn, &mdn.bv_val );
169         switch ( rc ) {
170         case REWRITE_REGEXEC_OK:
171                 if ( !BER_BVISNULL( &mdn ) && mdn.bv_val != in->bv_val ) {
172                         mdn.bv_len = strlen( mdn.bv_val );
173                         *dn = mdn;
174                 } else {
175                         *dn = *in;
176                 }
177                 rc = LDAP_SUCCESS;
178
179                 Debug( LDAP_DEBUG_ARGS,
180                         "[rw] %s: \"%s\" -> \"%s\"\n",
181                         dc->ctx, in->bv_val, dn->bv_val );
182                 break;
183                 
184         case REWRITE_REGEXEC_UNWILLING:
185                 if ( dc->rs ) {
186                         dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
187                         dc->rs->sr_text = "Operation not allowed";
188                 }
189                 rc = LDAP_UNWILLING_TO_PERFORM;
190                 break;
191                 
192         case REWRITE_REGEXEC_ERR:
193                 if ( dc->rs ) {
194                         dc->rs->sr_err = LDAP_OTHER;
195                         dc->rs->sr_text = "Rewrite error";
196                 }
197                 rc = LDAP_OTHER;
198                 break;
199         }
200
201         if ( mdn.bv_val == dmy ) {
202                 BER_BVZERO( &mdn );
203         }
204
205         if ( dn->bv_val == dmy ) {
206                 BER_BVZERO( dn );
207         }
208
209         return rc;
210 }
211
212 #else /* ! ENABLE_REWRITE */
213 /*
214  * rwm_dn_massage
215  * 
216  * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
217  */
218 int
219 rwm_dn_massage(
220         dncookie *dc,
221         struct berval *in,
222         struct berval *dn
223 )
224 {
225         int             i, src, dst;
226         struct berval   tmpin;
227
228         assert( dc != NULL );
229         assert( in != NULL );
230         assert( dn != NULL );
231
232         BER_BVZERO( dn );
233
234         if ( BER_BVISNULL( in ) ) {
235                 return LDAP_SUCCESS;
236         }
237
238         if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
239                 *dn = *in;
240                 return LDAP_SUCCESS;
241         }
242
243         if ( dc->tofrom ) {
244                 src = 0 + dc->normalized;
245                 dst = 2 + dc->normalized;
246
247                 tmpin = *in;
248
249         } else {
250                 int     rc;
251
252                 src = 2 + dc->normalized;
253                 dst = 0 + dc->normalized;
254
255                 /* DN from remote server may be in arbitrary form.
256                  * Pretty it so we can parse reliably.
257                  */
258                 if ( dc->normalized ) {
259                         rc = dnNormalize( 0, NULL, NULL, in, &tmpin, NULL );
260
261                 } else {
262                         rc = dnPretty( NULL, in, &tmpin, NULL );
263                 }
264
265                 if ( rc != LDAP_SUCCESS ) {
266                         return rc;
267                 }
268         }
269
270         for ( i = 0;
271                         !BER_BVISNULL( &dc->rwmap->rwm_suffix_massage[i] );
272                         i += 4 )
273         {
274                 int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
275                 int diff = tmpin.bv_len - aliasLength;
276
277                 if ( diff < 0 ) {
278                         /* alias is longer than dn */
279                         continue;
280
281                 } else if ( diff > 0 && ( !DN_SEPARATOR(tmpin.bv_val[diff-1])))
282                 {
283                         /* FIXME: DN_SEPARATOR() is intended to work
284                          * on a normalized/pretty DN, so that ';'
285                          * is never used as a DN separator */
286                         continue;
287                         /* At a DN Separator */
288                 }
289
290                 if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val,
291                                         &tmpin.bv_val[diff] ) )
292                 {
293                         dn->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
294                         dn->bv_val = ch_malloc( dn->bv_len + 1 );
295                         strncpy( dn->bv_val, tmpin.bv_val, diff );
296                         strcpy( &dn->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
297                         Debug( LDAP_DEBUG_ARGS,
298                                 "rwm_dn_massage:"
299                                 " converted \"%s\" to \"%s\"\n",
300                                 in->bv_val, dn->bv_val, 0 );
301
302                         break;
303                 }
304         }
305
306         if ( tmpin.bv_val != in->bv_val ) {
307                 ch_free( tmpin.bv_val );
308         }
309
310         /* Nothing matched, just return the original DN */
311         if ( BER_BVISNULL( dn ) ) {
312                 *dn = *in;
313         }
314
315         return LDAP_SUCCESS;
316 }
317 #endif /* ! ENABLE_REWRITE */
318
319 #endif /* SLAPD_OVER_RWM */