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 ... */
51 rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
52 ( in->bv_len ? in->bv_val : "" ),
53 dc->conn, &mdn.bv_val );
55 case REWRITE_REGEXEC_OK:
56 if ( !BER_BVISNULL( &mdn ) ) {
58 mdn.bv_len = strlen( mdn.bv_val );
60 if ( dn != NULL && ndn != NULL ) {
61 rc = dnPrettyNormal( NULL, &mdn, dn, ndn, NULL );
63 } else if ( dn != NULL ) {
64 rc = dnPretty( NULL, &mdn, dn, NULL );
66 } else if ( ndn != NULL) {
67 rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
70 if ( mdn.bv_val != in->bv_val ) {
71 ch_free( mdn.bv_val );
75 /* we assume the input string is already in pretty form,
76 * and that the normalized version is already available */
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 */
95 case REWRITE_REGEXEC_UNWILLING:
97 dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
98 dc->rs->sr_text = "Operation not allowed";
100 rc = LDAP_UNWILLING_TO_PERFORM;
103 case REWRITE_REGEXEC_ERR:
105 dc->rs->sr_err = LDAP_OTHER;
106 dc->rs->sr_text = "Rewrite error";
119 * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
124 struct berval *tmpin,
130 struct berval pretty = BER_BVNULL,
136 if ( in == NULL || BER_BVISNULL( in ) ) {
141 if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
147 src = 0 + dc->normalized;
148 dst = 2 + dc->normalized;
153 src = 2 + dc->normalized;
154 dst = 0 + dc->normalized;
156 /* DN from remote server may be in arbitrary form.
157 * Pretty it so we can parse reliably.
159 if ( dc->normalized && dn == NULL ) {
160 rc = dnNormalize( 0, NULL, NULL, in, &normal, NULL );
162 } else if ( !dc->normalized && ndn == NULL ) {
163 rc = dnPretty( NULL, in, &pretty, NULL );
166 rc = dnPrettyNormal( NULL, in, &pretty, &normal, NULL );
169 if ( rc != LDAP_SUCCESS ) {
173 if ( dc->normalized && !BER_BVISNULL( &normal) ) {
176 } else if ( !dc->normalized && !BER_BVISNULL( &pretty ) ) {
182 dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
184 int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
185 int diff = in->bv_len - aliasLength;
188 /* alias is longer than dn */
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 */
196 /* At a DN Separator */
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 );
205 LDAP_LOG ( BACK_LDAP, ARGS,
206 "rwm_dn_massage: converted \"%s\" to \"%s\"\n",
207 in->bv_val, dn->bv_val, 0 );
209 Debug( LDAP_DEBUG_ARGS,
211 " converted \"%s\" to \"%s\"\n",
212 in->bv_val, dn->bv_val, 0 );
218 if ( !BER_BVISNULL( &pretty ) ) {
219 ch_free( pretty.bv_val );
222 if ( !BER_BVISNULL( &normal ) ) {
223 ch_free( normal.bv_val );
228 /* Nothing matched, just return the original DN */
229 if ( dc->normalized && BER_BVISNULL( ndn ) ) {
232 } else if ( !dc->normalized && BER_BVISNULL( dn ) ) {
238 #endif /* !ENABLE_REWRITE */
240 #endif /* SLAPD_OVER_RWM */