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 */
94 Debug( LDAP_DEBUG_ARGS,
95 "[rw] %s: \"%s\" -> \"%s\"\n",
96 dc->ctx, in->bv_val, dn ? dn->bv_val : ndn->bv_val );
99 case REWRITE_REGEXEC_UNWILLING:
101 dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
102 dc->rs->sr_text = "Operation not allowed";
104 rc = LDAP_UNWILLING_TO_PERFORM;
107 case REWRITE_REGEXEC_ERR:
109 dc->rs->sr_err = LDAP_OTHER;
110 dc->rs->sr_text = "Rewrite error";
123 * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
128 struct berval *tmpin,
134 struct berval pretty = BER_BVNULL,
138 if ( dn == NULL && ndn == NULL ) {
142 if ( in == NULL || BER_BVISNULL( in ) ) {
152 if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
165 src = 0 + dc->normalized;
166 dst = 2 + dc->normalized;
171 src = 2 + dc->normalized;
172 dst = 0 + dc->normalized;
174 /* DN from remote server may be in arbitrary form.
175 * Pretty it so we can parse reliably.
177 if ( dc->normalized && dn == NULL ) {
178 rc = dnNormalize( 0, NULL, NULL, in, &normal, NULL );
180 } else if ( !dc->normalized && ndn == NULL ) {
181 rc = dnPretty( NULL, in, &pretty, NULL );
184 rc = dnPrettyNormal( NULL, in, &pretty, &normal, NULL );
187 if ( rc != LDAP_SUCCESS ) {
191 if ( dc->normalized && !BER_BVISNULL( &normal ) ) {
194 } else if ( !dc->normalized && !BER_BVISNULL( &pretty ) ) {
200 dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
202 int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
203 int diff = in->bv_len - aliasLength;
206 /* alias is longer than dn */
209 } else if ( diff > 0 && ( !DN_SEPARATOR(in->bv_val[diff-1]))) {
210 /* FIXME: DN_SEPARATOR() is intended to work
211 * on a normalized/pretty DN, so that ';'
212 * is never used as a DN separator */
214 /* At a DN Separator */
217 if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val, &in->bv_val[diff] ) ) {
225 out->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
226 out->bv_val = ch_malloc( out->bv_len + 1 );
227 strncpy( out->bv_val, in->bv_val, diff );
228 strcpy( &out->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
229 Debug( LDAP_DEBUG_ARGS,
231 " converted \"%s\" to \"%s\"\n",
232 in->bv_val, out->bv_val, 0 );
234 rc = dnNormalize( 0, NULL, NULL, dn, ndn, NULL );
241 if ( !BER_BVISNULL( &pretty ) ) {
242 ch_free( pretty.bv_val );
245 if ( !BER_BVISNULL( &normal ) ) {
246 ch_free( normal.bv_val );
251 /* Nothing matched, just return the original DN */
252 if ( dc->normalized && BER_BVISNULL( ndn ) ) {
255 } else if ( !dc->normalized && BER_BVISNULL( dn ) ) {
261 #endif /* !ENABLE_REWRITE */
263 #endif /* SLAPD_OVER_RWM */