2003-12-06 17:53:41 +08:00
|
|
|
/* rwmdn.c - massages dns */
|
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2004-01-02 03:15:16 +08:00
|
|
|
* Copyright 1999-2004 The OpenLDAP Foundation.
|
2003-12-09 01:41:40 +08:00
|
|
|
* Portions Copyright 1999-2003 Howard Chu.
|
|
|
|
* Portions Copyright 2000-2003 Pierangelo Masarati.
|
2003-12-06 17:53:41 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was initially developed by the Howard Chu for inclusion
|
|
|
|
* in OpenLDAP Software and subsequently enhanced by Pierangelo
|
|
|
|
* Masarati.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
2003-12-10 07:50:10 +08:00
|
|
|
#ifdef SLAPD_OVER_RWM
|
|
|
|
|
2003-12-06 17:53:41 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
#include "rwm.h"
|
|
|
|
|
|
|
|
/* FIXME: after rewriting, we should also remap attributes ... */
|
|
|
|
|
|
|
|
#ifdef ENABLE_REWRITE
|
|
|
|
int
|
|
|
|
rwm_dn_massage(
|
|
|
|
dncookie *dc,
|
2004-07-12 06:57:51 +08:00
|
|
|
struct berval *in,
|
2003-12-06 17:53:41 +08:00
|
|
|
struct berval *dn,
|
2004-07-12 06:57:51 +08:00
|
|
|
struct berval *ndn
|
2003-12-06 17:53:41 +08:00
|
|
|
)
|
|
|
|
{
|
2004-07-12 06:57:51 +08:00
|
|
|
int rc = 0;
|
|
|
|
struct berval mdn;
|
2003-12-06 17:53:41 +08:00
|
|
|
|
2004-07-26 07:16:40 +08:00
|
|
|
assert( in );
|
|
|
|
|
|
|
|
if ( dn == NULL && ndn == NULL ) {
|
|
|
|
return LDAP_OTHER;
|
|
|
|
}
|
|
|
|
|
2004-03-11 05:41:02 +08:00
|
|
|
rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
|
2004-07-12 06:57:51 +08:00
|
|
|
( in->bv_len ? in->bv_val : "" ),
|
|
|
|
dc->conn, &mdn.bv_val );
|
2004-03-11 05:41:02 +08:00
|
|
|
switch ( rc ) {
|
2003-12-06 17:53:41 +08:00
|
|
|
case REWRITE_REGEXEC_OK:
|
2004-07-12 06:57:51 +08:00
|
|
|
if ( !BER_BVISNULL( &mdn ) ) {
|
|
|
|
|
|
|
|
mdn.bv_len = strlen( mdn.bv_val );
|
|
|
|
|
|
|
|
if ( dn != NULL && ndn != NULL ) {
|
|
|
|
rc = dnPrettyNormal( NULL, &mdn, dn, ndn, NULL );
|
|
|
|
|
|
|
|
} else if ( dn != NULL ) {
|
|
|
|
rc = dnPretty( NULL, &mdn, dn, NULL );
|
|
|
|
|
|
|
|
} else if ( ndn != NULL) {
|
|
|
|
rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( mdn.bv_val != in->bv_val ) {
|
|
|
|
ch_free( mdn.bv_val );
|
|
|
|
}
|
|
|
|
|
2003-12-06 17:53:41 +08:00
|
|
|
} else {
|
2004-07-12 06:57:51 +08:00
|
|
|
/* we assume the input string is already in pretty form,
|
|
|
|
* and that the normalized version is already available */
|
2004-07-26 07:16:40 +08:00
|
|
|
if ( dn ) {
|
|
|
|
*dn = *in;
|
|
|
|
if ( ndn ) {
|
|
|
|
BER_BVZERO( ndn );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*ndn = *in;
|
2004-07-12 06:57:51 +08:00
|
|
|
}
|
|
|
|
rc = LDAP_SUCCESS;
|
2003-12-06 17:53:41 +08:00
|
|
|
}
|
2004-07-12 06:57:51 +08:00
|
|
|
|
2003-12-06 17:53:41 +08:00
|
|
|
Debug( LDAP_DEBUG_ARGS,
|
2004-03-11 05:41:02 +08:00
|
|
|
"[rw] %s: \"%s\" -> \"%s\"\n",
|
2004-07-26 07:16:40 +08:00
|
|
|
dc->ctx, in->bv_val, dn ? dn->bv_val : ndn->bv_val );
|
2003-12-06 17:53:41 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_REGEXEC_UNWILLING:
|
|
|
|
if ( dc->rs ) {
|
|
|
|
dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
|
|
|
|
dc->rs->sr_text = "Operation not allowed";
|
|
|
|
}
|
|
|
|
rc = LDAP_UNWILLING_TO_PERFORM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_REGEXEC_ERR:
|
|
|
|
if ( dc->rs ) {
|
|
|
|
dc->rs->sr_err = LDAP_OTHER;
|
|
|
|
dc->rs->sr_text = "Rewrite error";
|
|
|
|
}
|
|
|
|
rc = LDAP_OTHER;
|
|
|
|
break;
|
|
|
|
}
|
2004-03-11 05:41:02 +08:00
|
|
|
|
2003-12-06 17:53:41 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* rwm_dn_massage
|
|
|
|
*
|
|
|
|
* Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rwm_dn_massage(
|
|
|
|
dncookie *dc,
|
2004-07-12 06:57:51 +08:00
|
|
|
struct berval *tmpin,
|
|
|
|
struct berval *dn,
|
|
|
|
struct berval *ndn
|
2003-12-06 17:53:41 +08:00
|
|
|
)
|
|
|
|
{
|
2004-07-12 06:57:51 +08:00
|
|
|
int i, src, dst;
|
|
|
|
struct berval pretty = BER_BVNULL,
|
|
|
|
normal = BER_BVNULL,
|
|
|
|
*in = tmpin;
|
2003-12-06 17:53:41 +08:00
|
|
|
|
2004-07-26 07:16:40 +08:00
|
|
|
if ( dn == NULL && ndn == NULL ) {
|
|
|
|
return LDAP_OTHER;
|
|
|
|
}
|
2003-12-06 17:53:41 +08:00
|
|
|
|
2004-07-12 06:57:51 +08:00
|
|
|
if ( in == NULL || BER_BVISNULL( in ) ) {
|
2004-07-26 07:16:40 +08:00
|
|
|
if ( dn ) {
|
|
|
|
BER_BVZERO( dn );
|
|
|
|
}
|
|
|
|
if ( ndn ) {
|
|
|
|
BER_BVZERO( ndn );
|
|
|
|
}
|
2004-07-12 06:57:51 +08:00
|
|
|
return LDAP_SUCCESS;
|
2003-12-06 17:53:41 +08:00
|
|
|
}
|
2004-07-26 07:16:40 +08:00
|
|
|
|
2003-12-06 17:53:41 +08:00
|
|
|
if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
|
2004-07-26 07:16:40 +08:00
|
|
|
if ( dn ) {
|
|
|
|
*dn = *in;
|
|
|
|
if ( ndn ) {
|
|
|
|
BER_BVZERO( ndn );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*ndn = *in;
|
|
|
|
}
|
2004-07-12 06:57:51 +08:00
|
|
|
return LDAP_SUCCESS;
|
2003-12-06 17:53:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( dc->tofrom ) {
|
|
|
|
src = 0 + dc->normalized;
|
|
|
|
dst = 2 + dc->normalized;
|
2004-07-12 06:57:51 +08:00
|
|
|
|
2003-12-06 17:53:41 +08:00
|
|
|
} else {
|
2004-07-12 06:57:51 +08:00
|
|
|
int rc;
|
|
|
|
|
2003-12-06 17:53:41 +08:00
|
|
|
src = 2 + dc->normalized;
|
|
|
|
dst = 0 + dc->normalized;
|
2004-07-12 06:57:51 +08:00
|
|
|
|
2003-12-06 17:53:41 +08:00
|
|
|
/* DN from remote server may be in arbitrary form.
|
|
|
|
* Pretty it so we can parse reliably.
|
|
|
|
*/
|
2004-07-12 06:57:51 +08:00
|
|
|
if ( dc->normalized && dn == NULL ) {
|
|
|
|
rc = dnNormalize( 0, NULL, NULL, in, &normal, NULL );
|
|
|
|
|
|
|
|
} else if ( !dc->normalized && ndn == NULL ) {
|
|
|
|
rc = dnPretty( NULL, in, &pretty, NULL );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
rc = dnPrettyNormal( NULL, in, &pretty, &normal, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2004-07-26 07:16:40 +08:00
|
|
|
if ( dc->normalized && !BER_BVISNULL( &normal ) ) {
|
2004-07-12 06:57:51 +08:00
|
|
|
in = &normal;
|
|
|
|
|
|
|
|
} else if ( !dc->normalized && !BER_BVISNULL( &pretty ) ) {
|
|
|
|
in = &pretty;
|
|
|
|
}
|
2003-12-06 17:53:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = 0;
|
|
|
|
dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
|
|
|
|
i += 4 ) {
|
|
|
|
int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
|
2004-07-12 06:57:51 +08:00
|
|
|
int diff = in->bv_len - aliasLength;
|
2003-12-06 17:53:41 +08:00
|
|
|
|
|
|
|
if ( diff < 0 ) {
|
|
|
|
/* alias is longer than dn */
|
|
|
|
continue;
|
2004-07-12 06:57:51 +08:00
|
|
|
|
|
|
|
} else if ( diff > 0 && ( !DN_SEPARATOR(in->bv_val[diff-1]))) {
|
2004-05-07 17:03:05 +08:00
|
|
|
/* FIXME: DN_SEPARATOR() is intended to work
|
|
|
|
* on a normalized/pretty DN, so that ';'
|
|
|
|
* is never used as a DN separator */
|
2003-12-06 17:53:41 +08:00
|
|
|
continue;
|
|
|
|
/* At a DN Separator */
|
|
|
|
}
|
|
|
|
|
2004-07-12 06:57:51 +08:00
|
|
|
if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val, &in->bv_val[diff] ) ) {
|
2004-07-26 07:16:40 +08:00
|
|
|
struct berval *out;
|
|
|
|
|
|
|
|
if ( dn ) {
|
|
|
|
out = dn;
|
|
|
|
} else {
|
|
|
|
out = ndn;
|
|
|
|
}
|
|
|
|
out->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
|
|
|
|
out->bv_val = ch_malloc( out->bv_len + 1 );
|
|
|
|
strncpy( out->bv_val, in->bv_val, diff );
|
|
|
|
strcpy( &out->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
|
2003-12-06 17:53:41 +08:00
|
|
|
Debug( LDAP_DEBUG_ARGS,
|
|
|
|
"rwm_dn_massage:"
|
|
|
|
" converted \"%s\" to \"%s\"\n",
|
2004-07-26 07:16:40 +08:00
|
|
|
in->bv_val, out->bv_val, 0 );
|
|
|
|
if ( dn && ndn ) {
|
|
|
|
rc = dnNormalize( 0, NULL, NULL, dn, ndn, NULL );
|
|
|
|
}
|
|
|
|
|
2003-12-06 17:53:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-07-12 06:57:51 +08:00
|
|
|
|
|
|
|
if ( !BER_BVISNULL( &pretty ) ) {
|
|
|
|
ch_free( pretty.bv_val );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !BER_BVISNULL( &normal ) ) {
|
|
|
|
ch_free( normal.bv_val );
|
2003-12-06 17:53:41 +08:00
|
|
|
}
|
2004-07-12 06:57:51 +08:00
|
|
|
|
|
|
|
in = tmpin;
|
|
|
|
|
2003-12-06 17:53:41 +08:00
|
|
|
/* Nothing matched, just return the original DN */
|
2004-07-12 06:57:51 +08:00
|
|
|
if ( dc->normalized && BER_BVISNULL( ndn ) ) {
|
|
|
|
*ndn = *in;
|
|
|
|
|
|
|
|
} else if ( !dc->normalized && BER_BVISNULL( dn ) ) {
|
|
|
|
*dn = *in;
|
2003-12-06 17:53:41 +08:00
|
|
|
}
|
|
|
|
|
2004-07-12 06:57:51 +08:00
|
|
|
return LDAP_SUCCESS;
|
2003-12-06 17:53:41 +08:00
|
|
|
}
|
|
|
|
#endif /* !ENABLE_REWRITE */
|
2003-12-10 07:50:10 +08:00
|
|
|
|
|
|
|
#endif /* SLAPD_OVER_RWM */
|