1 /* rwmdn.c - massages dns */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2005 The OpenLDAP Foundation.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * Portions Copyright 2000-2003 Pierangelo Masarati.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
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>.
19 * This work was initially developed by the Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
31 #include <ac/string.h>
32 #include <ac/socket.h>
37 /* FIXME: after rewriting, we should also remap attributes ... */
40 * massages "in" and normalizes it into "ndn"
42 * "ndn" may be untouched if no massaging occurred and its value was not null
45 rwm_dn_massage_normalize(
51 struct berval mdn = BER_BVNULL;
53 /* massage and normalize a DN */
54 rc = rwm_dn_massage( dc, in, &mdn );
55 if ( rc != LDAP_SUCCESS ) {
59 if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( ndn ) ) {
63 rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
65 if ( mdn.bv_val != in->bv_val ) {
66 ch_free( mdn.bv_val );
73 * massages "in" and prettifies it into "pdn"
75 * "pdn" may be untouched if no massaging occurred and its value was not null
78 rwm_dn_massage_pretty(
84 struct berval mdn = BER_BVNULL;
86 /* massage and pretty a DN */
87 rc = rwm_dn_massage( dc, in, &mdn );
88 if ( rc != LDAP_SUCCESS ) {
92 if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
96 rc = dnPretty( NULL, &mdn, pdn, NULL );
98 if ( mdn.bv_val != in->bv_val ) {
99 ch_free( mdn.bv_val );
106 * massages "in" and prettifies and normalizes it into "pdn" and "ndn"
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 )
114 rwm_dn_massage_pretty_normalize(
121 struct berval mdn = BER_BVNULL;
123 /* massage, pretty and normalize a DN */
124 rc = rwm_dn_massage( dc, in, &mdn );
125 if ( rc != LDAP_SUCCESS ) {
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 );
136 rc = dnPrettyNormal( NULL, &mdn, pdn, ndn, NULL );
138 if ( mdn.bv_val != in->bv_val ) {
139 ch_free( mdn.bv_val );
145 #ifdef ENABLE_REWRITE
147 * massages "in" into "dn"
149 * "dn" may contain the value of "in" if no massage occurred
165 rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
166 ( in->bv_len ? in->bv_val : "" ),
167 dc->conn, &mdn.bv_val );
169 case REWRITE_REGEXEC_OK:
170 if ( !BER_BVISNULL( &mdn ) && mdn.bv_val != in->bv_val ) {
171 mdn.bv_len = strlen( mdn.bv_val );
178 Debug( LDAP_DEBUG_ARGS,
179 "[rw] %s: \"%s\" -> \"%s\"\n",
180 dc->ctx, in->bv_val, dn->bv_val );
183 case REWRITE_REGEXEC_UNWILLING:
185 dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
186 dc->rs->sr_text = "Operation not allowed";
188 rc = LDAP_UNWILLING_TO_PERFORM;
191 case REWRITE_REGEXEC_ERR:
193 dc->rs->sr_err = LDAP_OTHER;
194 dc->rs->sr_text = "Rewrite error";
203 #else /* ! ENABLE_REWRITE */
207 * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
225 if ( BER_BVISNULL( in ) ) {
229 if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
235 src = 0 + dc->normalized;
236 dst = 2 + dc->normalized;
243 src = 2 + dc->normalized;
244 dst = 0 + dc->normalized;
246 /* DN from remote server may be in arbitrary form.
247 * Pretty it so we can parse reliably.
249 if ( dc->normalized ) {
250 rc = dnNormalize( 0, NULL, NULL, in, &tmpin, NULL );
253 rc = dnPretty( NULL, in, &tmpin, NULL );
256 if ( rc != LDAP_SUCCESS ) {
262 !BER_BVISNULL( &dc->rwmap->rwm_suffix_massage[i] );
265 int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
266 int diff = tmpin.bv_len - aliasLength;
269 /* alias is longer than dn */
272 } else if ( diff > 0 && ( !DN_SEPARATOR(tmpin.bv_val[diff-1])))
274 /* FIXME: DN_SEPARATOR() is intended to work
275 * on a normalized/pretty DN, so that ';'
276 * is never used as a DN separator */
278 /* At a DN Separator */
281 if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val,
282 &tmpin.bv_val[diff] ) )
284 dn->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
285 dn->bv_val = ch_malloc( dn->bv_len + 1 );
286 strncpy( dn->bv_val, tmpin.bv_val, diff );
287 strcpy( &dn->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
288 Debug( LDAP_DEBUG_ARGS,
290 " converted \"%s\" to \"%s\"\n",
291 in->bv_val, dn->bv_val, 0 );
297 if ( tmpin.bv_val != in->bv_val ) {
298 ch_free( tmpin.bv_val );
301 /* Nothing matched, just return the original DN */
302 if ( BER_BVISNULL( dn ) ) {
308 #endif /* ! ENABLE_REWRITE */
310 #endif /* SLAPD_OVER_RWM */