2004-11-14 02:36:02 +08:00
|
|
|
/* suffixmassage.c - massages ldap backend dns */
|
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2009-01-22 08:40:04 +08:00
|
|
|
* Copyright 2003-2009 The OpenLDAP Foundation.
|
2004-11-14 02:36:02 +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.
|
|
|
|
*/
|
|
|
|
/* This is an altered version */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
|
|
|
|
* Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
|
|
|
|
*
|
|
|
|
* Module back-ldap, originally developed by Howard Chu
|
|
|
|
*
|
|
|
|
* has been modified by Pierangelo Masarati. The original copyright
|
|
|
|
* notice has been maintained.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose
|
|
|
|
* on any computer system, and to alter it and redistribute it, subject
|
|
|
|
* to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The author is not responsible for the consequences of use of this
|
|
|
|
* software, no matter how awful, even if they arise from flaws in it.
|
|
|
|
*
|
|
|
|
* 2. The origin of this software must not be misrepresented, either by
|
|
|
|
* explicit claim or by omission. Since few users ever read sources,
|
|
|
|
* credits should appear in the documentation.
|
|
|
|
*
|
|
|
|
* 3. Altered versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software. Since few users
|
|
|
|
* ever read sources, credits should appear in the documentation.
|
|
|
|
*
|
|
|
|
* 4. This notice may not be removed or altered.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
#include "../back-ldap/back-ldap.h"
|
|
|
|
#include "back-meta.h"
|
|
|
|
|
|
|
|
#ifdef ENABLE_REWRITE
|
|
|
|
int
|
|
|
|
ldap_back_dn_massage(
|
2005-05-04 07:42:28 +08:00
|
|
|
dncookie *dc,
|
|
|
|
struct berval *dn,
|
|
|
|
struct berval *res )
|
2004-11-14 02:36:02 +08:00
|
|
|
{
|
2005-05-04 07:42:28 +08:00
|
|
|
int rc = 0;
|
|
|
|
static char *dmy = "";
|
2004-11-14 02:36:02 +08:00
|
|
|
|
2005-05-05 07:57:55 +08:00
|
|
|
switch ( rewrite_session( dc->target->mt_rwmap.rwm_rw, dc->ctx,
|
2005-05-04 07:42:28 +08:00
|
|
|
( dn->bv_val ? dn->bv_val : dmy ),
|
2005-01-02 00:12:09 +08:00
|
|
|
dc->conn, &res->bv_val ) )
|
|
|
|
{
|
2004-11-14 02:36:02 +08:00
|
|
|
case REWRITE_REGEXEC_OK:
|
|
|
|
if ( res->bv_val != NULL ) {
|
|
|
|
res->bv_len = strlen( res->bv_val );
|
|
|
|
} else {
|
|
|
|
*res = *dn;
|
|
|
|
}
|
|
|
|
Debug( LDAP_DEBUG_ARGS,
|
2005-05-04 07:42:28 +08:00
|
|
|
"[rw] %s: \"%s\" -> \"%s\"\n",
|
|
|
|
dc->ctx,
|
|
|
|
BER_BVISNULL( dn ) ? "" : dn->bv_val,
|
|
|
|
BER_BVISNULL( res ) ? "" : res->bv_val );
|
2004-11-14 02:36:02 +08:00
|
|
|
rc = LDAP_SUCCESS;
|
|
|
|
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;
|
|
|
|
}
|
2005-05-04 07:42:28 +08:00
|
|
|
|
|
|
|
if ( res->bv_val == dmy ) {
|
|
|
|
BER_BVZERO( res );
|
|
|
|
}
|
|
|
|
|
2004-11-14 02:36:02 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* ldap_back_dn_massage
|
|
|
|
*
|
|
|
|
* Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ldap_back_dn_massage(
|
|
|
|
dncookie *dc,
|
|
|
|
struct berval *odn,
|
|
|
|
struct berval *res
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int i, src, dst;
|
|
|
|
struct berval pretty = {0,NULL}, *dn = odn;
|
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( res != NULL );
|
2004-11-14 02:36:02 +08:00
|
|
|
|
|
|
|
if ( dn == NULL ) {
|
|
|
|
res->bv_val = NULL;
|
|
|
|
res->bv_len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2005-05-05 07:57:55 +08:00
|
|
|
if ( dc->target->mt_rwmap.rwm_suffix_massage == NULL ) {
|
2004-11-14 02:36:02 +08:00
|
|
|
*res = *dn;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( dc->tofrom ) {
|
|
|
|
src = 0 + dc->normalized;
|
|
|
|
dst = 2 + dc->normalized;
|
|
|
|
} else {
|
|
|
|
src = 2 + dc->normalized;
|
|
|
|
dst = 0 + dc->normalized;
|
|
|
|
/* DN from remote server may be in arbitrary form.
|
|
|
|
* Pretty it so we can parse reliably.
|
|
|
|
*/
|
|
|
|
dnPretty( NULL, dn, &pretty, NULL );
|
|
|
|
if (pretty.bv_val) dn = &pretty;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = 0;
|
2005-05-05 07:57:55 +08:00
|
|
|
dc->target->mt_rwmap.rwm_suffix_massage[i].bv_val != NULL;
|
2004-11-14 02:36:02 +08:00
|
|
|
i += 4 ) {
|
2005-05-05 07:57:55 +08:00
|
|
|
int aliasLength = dc->target->mt_rwmap.rwm_suffix_massage[i+src].bv_len;
|
2004-11-14 02:36:02 +08:00
|
|
|
int diff = dn->bv_len - aliasLength;
|
|
|
|
|
|
|
|
if ( diff < 0 ) {
|
|
|
|
/* alias is longer than dn */
|
|
|
|
continue;
|
|
|
|
} else if ( diff > 0 && ( !DN_SEPARATOR(dn->bv_val[diff-1]))) {
|
|
|
|
/* boundary is not at a DN separator */
|
|
|
|
continue;
|
|
|
|
/* At a DN Separator */
|
|
|
|
}
|
|
|
|
|
2005-05-05 07:57:55 +08:00
|
|
|
if ( !strcmp( dc->target->mt_rwmap.rwm_suffix_massage[i+src].bv_val, &dn->bv_val[diff] ) ) {
|
|
|
|
res->bv_len = diff + dc->target->mt_rwmap.rwm_suffix_massage[i+dst].bv_len;
|
2004-11-14 02:36:02 +08:00
|
|
|
res->bv_val = ch_malloc( res->bv_len + 1 );
|
|
|
|
strncpy( res->bv_val, dn->bv_val, diff );
|
2005-05-05 07:57:55 +08:00
|
|
|
strcpy( &res->bv_val[diff], dc->target->mt_rwmap.rwm_suffix_massage[i+dst].bv_val );
|
2004-11-14 02:36:02 +08:00
|
|
|
Debug( LDAP_DEBUG_ARGS,
|
|
|
|
"ldap_back_dn_massage:"
|
|
|
|
" converted \"%s\" to \"%s\"\n",
|
2005-05-04 07:42:28 +08:00
|
|
|
BER_BVISNULL( dn ) ? "" : dn->bv_val,
|
|
|
|
BER_BVISNULL( res ) ? "" : res->bv_val, 0 );
|
2004-11-14 02:36:02 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pretty.bv_val) {
|
|
|
|
ch_free(pretty.bv_val);
|
|
|
|
dn = odn;
|
|
|
|
}
|
|
|
|
/* Nothing matched, just return the original DN */
|
|
|
|
if (res->bv_val == NULL) {
|
|
|
|
*res = *dn;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* !ENABLE_REWRITE */
|