]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwmdn.c
4892e4365e1d4ea3230faed3b5590c90ae7edb71
[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-2004 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 the 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 #ifdef ENABLE_REWRITE
40 int
41 rwm_dn_massage(
42         dncookie *dc,
43         struct berval *in,
44         struct berval *dn,
45         struct berval *ndn
46 )
47 {
48         int             rc = 0;
49         struct berval   mdn;
50
51         assert( in );
52
53         if ( dn == NULL && ndn == NULL ) {
54                 return LDAP_OTHER;
55         }
56
57         rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
58                         ( in->bv_len ? in->bv_val : "" ), 
59                         dc->conn, &mdn.bv_val );
60         switch ( rc ) {
61         case REWRITE_REGEXEC_OK:
62                 if ( !BER_BVISNULL( &mdn ) ) {
63
64                         mdn.bv_len = strlen( mdn.bv_val );
65                         
66                         if ( dn != NULL && ndn != NULL ) {
67                                 rc = dnPrettyNormal( NULL, &mdn, dn, ndn, NULL );
68
69                         } else if ( dn != NULL ) {
70                                 rc = dnPretty( NULL, &mdn, dn, NULL );
71
72                         } else if ( ndn != NULL) {
73                                 rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
74                         }
75
76                         if ( mdn.bv_val != in->bv_val ) {
77                                 ch_free( mdn.bv_val );
78                         }
79
80                 } else {
81                         /* we assume the input string is already in pretty form,
82                          * and that the normalized version is already available */
83                         if ( dn ) {
84                                 *dn = *in;
85                                 if ( ndn ) {
86                                         BER_BVZERO( ndn );
87                                 }
88                         } else {
89                                 *ndn = *in;
90                         }
91                         rc = LDAP_SUCCESS;
92                 }
93
94 #ifdef NEW_LOGGING
95                 LDAP_LOG( BACK_LDAP, DETAIL1, 
96                         "[rw] %s: \"%s\" -> \"%s\"\n",
97                         dc->ctx, in->bv_val, dn ? dn->bv_val : ndn->bv_val );
98 #else /* !NEW_LOGGING */
99                 Debug( LDAP_DEBUG_ARGS,
100                         "[rw] %s: \"%s\" -> \"%s\"\n",
101                         dc->ctx, in->bv_val, dn ? dn->bv_val : ndn->bv_val );
102 #endif /* !NEW_LOGGING */
103                 break;
104                 
105         case REWRITE_REGEXEC_UNWILLING:
106                 if ( dc->rs ) {
107                         dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
108                         dc->rs->sr_text = "Operation not allowed";
109                 }
110                 rc = LDAP_UNWILLING_TO_PERFORM;
111                 break;
112                 
113         case REWRITE_REGEXEC_ERR:
114                 if ( dc->rs ) {
115                         dc->rs->sr_err = LDAP_OTHER;
116                         dc->rs->sr_text = "Rewrite error";
117                 }
118                 rc = LDAP_OTHER;
119                 break;
120         }
121
122         return rc;
123 }
124
125 #else
126 /*
127  * rwm_dn_massage
128  * 
129  * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
130  */
131 int
132 rwm_dn_massage(
133         dncookie *dc,
134         struct berval *tmpin,
135         struct berval *dn,
136         struct berval *ndn
137 )
138 {
139         int             i, src, dst;
140         struct berval   pretty = BER_BVNULL,
141                         normal = BER_BVNULL,
142                         *in = tmpin;
143
144         if ( dn == NULL && ndn == NULL ) {
145                 return LDAP_OTHER;
146         }
147
148         if ( in == NULL || BER_BVISNULL( in ) ) {
149                 if ( dn ) {
150                         BER_BVZERO( dn );
151                 }
152                 if ( ndn ) {
153                         BER_BVZERO( ndn );
154                 }
155                 return LDAP_SUCCESS;
156         }
157
158         if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
159                 if ( dn ) {
160                         *dn = *in;
161                         if ( ndn ) {
162                                 BER_BVZERO( ndn );
163                         }
164                 } else {
165                         *ndn = *in;
166                 }
167                 return LDAP_SUCCESS;
168         }
169
170         if ( dc->tofrom ) {
171                 src = 0 + dc->normalized;
172                 dst = 2 + dc->normalized;
173
174         } else {
175                 int     rc;
176
177                 src = 2 + dc->normalized;
178                 dst = 0 + dc->normalized;
179
180                 /* DN from remote server may be in arbitrary form.
181                  * Pretty it so we can parse reliably.
182                  */
183                 if ( dc->normalized && dn == NULL ) {
184                         rc = dnNormalize( 0, NULL, NULL, in, &normal, NULL );
185
186                 } else if ( !dc->normalized && ndn == NULL ) {
187                         rc = dnPretty( NULL, in, &pretty, NULL );
188
189                 } else {
190                         rc = dnPrettyNormal( NULL, in, &pretty, &normal, NULL );
191                 }
192
193                 if ( rc != LDAP_SUCCESS ) {
194                         return rc;
195                 }
196
197                 if ( dc->normalized && !BER_BVISNULL( &normal ) ) {
198                         in = &normal;
199
200                 } else if ( !dc->normalized && !BER_BVISNULL( &pretty ) ) {
201                         in = &pretty;
202                 }
203         }
204
205         for ( i = 0;
206                 dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
207                 i += 4 ) {
208                 int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
209                 int diff = in->bv_len - aliasLength;
210
211                 if ( diff < 0 ) {
212                         /* alias is longer than dn */
213                         continue;
214
215                 } else if ( diff > 0 && ( !DN_SEPARATOR(in->bv_val[diff-1]))) {
216                         /* FIXME: DN_SEPARATOR() is intended to work
217                          * on a normalized/pretty DN, so that ';'
218                          * is never used as a DN separator */
219                         continue;
220                         /* At a DN Separator */
221                 }
222
223                 if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val, &in->bv_val[diff] ) ) {
224                         struct berval   *out;
225
226                         if ( dn ) {
227                                 out = dn;
228                         } else {
229                                 out = ndn;
230                         }
231                         out->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
232                         out->bv_val = ch_malloc( out->bv_len + 1 );
233                         strncpy( out->bv_val, in->bv_val, diff );
234                         strcpy( &out->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
235 #ifdef NEW_LOGGING
236                         LDAP_LOG ( BACK_LDAP, ARGS, 
237                                 "rwm_dn_massage: converted \"%s\" to \"%s\"\n",
238                                 in->bv_val, out->bv_val, 0 );
239 #else
240                         Debug( LDAP_DEBUG_ARGS,
241                                 "rwm_dn_massage:"
242                                 " converted \"%s\" to \"%s\"\n",
243                                 in->bv_val, out->bv_val, 0 );
244 #endif
245                         if ( dn && ndn ) {
246                                 rc = dnNormalize( 0, NULL, NULL, dn, ndn, NULL );
247                         }
248
249                         break;
250                 }
251         }
252
253         if ( !BER_BVISNULL( &pretty ) ) {
254                 ch_free( pretty.bv_val );
255         }
256
257         if ( !BER_BVISNULL( &normal ) ) {
258                 ch_free( normal.bv_val );
259         }
260
261         in = tmpin;
262
263         /* Nothing matched, just return the original DN */
264         if ( dc->normalized && BER_BVISNULL( ndn ) ) {
265                 *ndn = *in;
266
267         } else if ( !dc->normalized && BER_BVISNULL( dn ) ) {
268                 *dn = *in;
269         }
270
271         return LDAP_SUCCESS;
272 }
273 #endif /* !ENABLE_REWRITE */
274
275 #endif /* SLAPD_OVER_RWM */