1 /* rwmdn.c - massages dns */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2004 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 ... */
53 if ( dn == NULL && ndn == NULL ) {
57 rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
58 ( in->bv_len ? in->bv_val : "" ),
59 dc->conn, &mdn.bv_val );
61 case REWRITE_REGEXEC_OK:
62 if ( !BER_BVISNULL( &mdn ) ) {
64 mdn.bv_len = strlen( mdn.bv_val );
66 if ( dn != NULL && ndn != NULL ) {
67 rc = dnPrettyNormal( NULL, &mdn, dn, ndn, NULL );
69 } else if ( dn != NULL ) {
70 rc = dnPretty( NULL, &mdn, dn, NULL );
72 } else if ( ndn != NULL) {
73 rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
76 if ( mdn.bv_val != in->bv_val ) {
77 ch_free( mdn.bv_val );
81 /* we assume the input string is already in pretty form,
82 * and that the normalized version is already available */
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 */
105 case REWRITE_REGEXEC_UNWILLING:
107 dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
108 dc->rs->sr_text = "Operation not allowed";
110 rc = LDAP_UNWILLING_TO_PERFORM;
113 case REWRITE_REGEXEC_ERR:
115 dc->rs->sr_err = LDAP_OTHER;
116 dc->rs->sr_text = "Rewrite error";
129 * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
134 struct berval *tmpin,
140 struct berval pretty = BER_BVNULL,
144 if ( dn == NULL && ndn == NULL ) {
148 if ( in == NULL || BER_BVISNULL( in ) ) {
158 if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
171 src = 0 + dc->normalized;
172 dst = 2 + dc->normalized;
177 src = 2 + dc->normalized;
178 dst = 0 + dc->normalized;
180 /* DN from remote server may be in arbitrary form.
181 * Pretty it so we can parse reliably.
183 if ( dc->normalized && dn == NULL ) {
184 rc = dnNormalize( 0, NULL, NULL, in, &normal, NULL );
186 } else if ( !dc->normalized && ndn == NULL ) {
187 rc = dnPretty( NULL, in, &pretty, NULL );
190 rc = dnPrettyNormal( NULL, in, &pretty, &normal, NULL );
193 if ( rc != LDAP_SUCCESS ) {
197 if ( dc->normalized && !BER_BVISNULL( &normal ) ) {
200 } else if ( !dc->normalized && !BER_BVISNULL( &pretty ) ) {
206 dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
208 int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
209 int diff = in->bv_len - aliasLength;
212 /* alias is longer than dn */
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 */
220 /* At a DN Separator */
223 if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val, &in->bv_val[diff] ) ) {
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 );
236 LDAP_LOG ( BACK_LDAP, ARGS,
237 "rwm_dn_massage: converted \"%s\" to \"%s\"\n",
238 in->bv_val, out->bv_val, 0 );
240 Debug( LDAP_DEBUG_ARGS,
242 " converted \"%s\" to \"%s\"\n",
243 in->bv_val, out->bv_val, 0 );
246 rc = dnNormalize( 0, NULL, NULL, dn, ndn, NULL );
253 if ( !BER_BVISNULL( &pretty ) ) {
254 ch_free( pretty.bv_val );
257 if ( !BER_BVISNULL( &normal ) ) {
258 ch_free( normal.bv_val );
263 /* Nothing matched, just return the original DN */
264 if ( dc->normalized && BER_BVISNULL( ndn ) ) {
267 } else if ( !dc->normalized && BER_BVISNULL( dn ) ) {
273 #endif /* !ENABLE_REWRITE */
275 #endif /* SLAPD_OVER_RWM */