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