2001-01-20 05:27:20 +08:00
|
|
|
/* suffixmassage.c - massages ldap backend dns */
|
|
|
|
/* $OpenLDAP$ */
|
2003-11-27 14:35:14 +08:00
|
|
|
/* 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-11-27 14:35:14 +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.
|
|
|
|
*/
|
2001-01-20 05:27:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2003-04-29 23:02:01 +08:00
|
|
|
#include <ac/string.h>
|
2001-01-20 05:27:20 +08:00
|
|
|
#include <ac/socket.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
#include "back-ldap.h"
|
|
|
|
|
2003-04-07 18:15:18 +08:00
|
|
|
#ifdef ENABLE_REWRITE
|
|
|
|
int
|
|
|
|
ldap_back_dn_massage(
|
|
|
|
dncookie *dc,
|
|
|
|
struct berval *dn,
|
|
|
|
struct berval *res
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
2004-03-11 05:11:14 +08:00
|
|
|
rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
|
|
|
|
( dn->bv_len ? dn->bv_val : "" ), dc->conn,
|
|
|
|
&res->bv_val );
|
|
|
|
|
|
|
|
switch ( rc ) {
|
2003-04-07 18:15:18 +08:00
|
|
|
case REWRITE_REGEXEC_OK:
|
2003-04-07 20:53:00 +08:00
|
|
|
if ( res->bv_val != NULL ) {
|
2003-04-07 18:15:18 +08:00
|
|
|
res->bv_len = strlen( res->bv_val );
|
|
|
|
} else {
|
|
|
|
*res = *dn;
|
|
|
|
}
|
|
|
|
#ifdef NEW_LOGGING
|
|
|
|
LDAP_LOG( BACK_LDAP, DETAIL1,
|
2004-03-11 05:11:14 +08:00
|
|
|
"[rw] %s: \"%s\" -> \"%s\"\n",
|
|
|
|
dc->ctx, dn->bv_val, res->bv_val );
|
2003-04-07 18:15:18 +08:00
|
|
|
#else /* !NEW_LOGGING */
|
|
|
|
Debug( LDAP_DEBUG_ARGS,
|
2004-03-11 05:11:14 +08:00
|
|
|
"[rw] %s: \"%s\" -> \"%s\"\n",
|
|
|
|
dc->ctx, dn->bv_val, res->bv_val );
|
2003-04-07 18:15:18 +08:00
|
|
|
#endif /* !NEW_LOGGING */
|
2003-04-07 20:53:00 +08:00
|
|
|
rc = LDAP_SUCCESS;
|
2003-04-07 18:15:18 +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";
|
|
|
|
}
|
2003-04-07 20:53:00 +08:00
|
|
|
rc = LDAP_UNWILLING_TO_PERFORM;
|
2003-04-07 18:15:18 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REWRITE_REGEXEC_ERR:
|
|
|
|
if ( dc->rs ) {
|
|
|
|
dc->rs->sr_err = LDAP_OTHER;
|
|
|
|
dc->rs->sr_text = "Rewrite error";
|
|
|
|
}
|
2003-04-07 20:53:00 +08:00
|
|
|
rc = LDAP_OTHER;
|
2003-04-07 18:15:18 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2001-01-20 05:27:20 +08:00
|
|
|
/*
|
|
|
|
* ldap_back_dn_massage
|
|
|
|
*
|
|
|
|
* Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
|
|
|
|
*/
|
2003-04-07 18:15:18 +08:00
|
|
|
int
|
2001-01-20 05:27:20 +08:00
|
|
|
ldap_back_dn_massage(
|
2003-04-07 18:15:18 +08:00
|
|
|
dncookie *dc,
|
2003-04-07 18:51:57 +08:00
|
|
|
struct berval *odn,
|
2003-04-07 18:15:18 +08:00
|
|
|
struct berval *res
|
2001-01-20 05:27:20 +08:00
|
|
|
)
|
|
|
|
{
|
2001-12-28 12:20:08 +08:00
|
|
|
int i, src, dst;
|
2003-04-07 18:51:57 +08:00
|
|
|
struct berval pretty = {0,NULL}, *dn = odn;
|
2001-01-20 05:27:20 +08:00
|
|
|
|
2002-05-02 01:53:32 +08:00
|
|
|
assert( res );
|
|
|
|
|
2004-01-17 19:48:14 +08:00
|
|
|
res->bv_val = NULL;
|
|
|
|
res->bv_len = 0;
|
2003-04-07 18:15:18 +08:00
|
|
|
if ( dn == NULL ) {
|
|
|
|
return 0;
|
2001-01-20 05:27:20 +08:00
|
|
|
}
|
2003-04-08 03:48:10 +08:00
|
|
|
if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
|
2001-12-28 12:20:08 +08:00
|
|
|
*res = *dn;
|
2003-04-07 18:15:18 +08:00
|
|
|
return 0;
|
2001-01-20 05:27:20 +08:00
|
|
|
}
|
|
|
|
|
2003-04-07 18:15:18 +08:00
|
|
|
if ( dc->tofrom ) {
|
|
|
|
src = 0 + dc->normalized;
|
|
|
|
dst = 2 + dc->normalized;
|
2001-12-28 12:20:08 +08:00
|
|
|
} else {
|
2003-04-07 18:15:18 +08:00
|
|
|
src = 2 + dc->normalized;
|
|
|
|
dst = 0 + dc->normalized;
|
|
|
|
/* DN from remote server may be in arbitrary form.
|
|
|
|
* Pretty it so we can parse reliably.
|
|
|
|
*/
|
2003-04-30 02:13:10 +08:00
|
|
|
dnPretty( NULL, dn, &pretty, NULL );
|
2003-04-07 18:15:18 +08:00
|
|
|
if (pretty.bv_val) dn = &pretty;
|
2001-12-28 12:20:08 +08:00
|
|
|
}
|
2001-01-20 05:27:20 +08:00
|
|
|
|
2003-04-07 18:15:18 +08:00
|
|
|
for ( i = 0;
|
2003-04-08 03:48:10 +08:00
|
|
|
dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
|
2003-04-07 18:15:18 +08:00
|
|
|
i += 4 ) {
|
2003-04-08 03:48:10 +08:00
|
|
|
int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
|
2003-04-07 18:15:18 +08:00
|
|
|
int diff = dn->bv_len - aliasLength;
|
2001-01-20 05:27:20 +08:00
|
|
|
|
2003-04-07 18:15:18 +08:00
|
|
|
if ( diff < 0 ) {
|
|
|
|
/* alias is longer than dn */
|
|
|
|
continue;
|
2004-05-07 17:03:05 +08:00
|
|
|
} else if ( diff > 0 && ( !DN_SEPARATOR(dn->bv_val[diff-1]))) {
|
|
|
|
/* FIXME: DN_SEPARATOR() is intended to work
|
|
|
|
* on a normalized/pretty DN, so that ';'
|
|
|
|
* is never used as a DN separator */
|
2003-04-07 18:15:18 +08:00
|
|
|
continue;
|
|
|
|
/* At a DN Separator */
|
|
|
|
}
|
2001-01-20 05:27:20 +08:00
|
|
|
|
2004-01-17 20:25:46 +08:00
|
|
|
if ( !strcasecmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val, &dn->bv_val[diff] ) ) {
|
2003-04-08 03:48:10 +08:00
|
|
|
res->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
|
2003-04-07 18:15:18 +08:00
|
|
|
res->bv_val = ch_malloc( res->bv_len + 1 );
|
|
|
|
strncpy( res->bv_val, dn->bv_val, diff );
|
2003-04-08 03:48:10 +08:00
|
|
|
strcpy( &res->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
|
2002-04-17 04:11:09 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2003-04-08 04:16:56 +08:00
|
|
|
LDAP_LOG ( BACK_LDAP, ARGS,
|
|
|
|
"ldap_back_dn_massage: converted \"%s\" to \"%s\"\n",
|
|
|
|
dn->bv_val, res->bv_val, 0 );
|
2002-04-17 04:11:09 +08:00
|
|
|
#else
|
2003-04-07 18:15:18 +08:00
|
|
|
Debug( LDAP_DEBUG_ARGS,
|
|
|
|
"ldap_back_dn_massage:"
|
2001-01-20 05:27:20 +08:00
|
|
|
" converted \"%s\" to \"%s\"\n",
|
2003-04-07 18:15:18 +08:00
|
|
|
dn->bv_val, res->bv_val, 0 );
|
2002-04-17 04:11:09 +08:00
|
|
|
#endif
|
2003-04-07 18:15:18 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-04-07 18:51:57 +08:00
|
|
|
if (pretty.bv_val) {
|
|
|
|
ch_free(pretty.bv_val);
|
|
|
|
dn = odn;
|
|
|
|
}
|
2003-04-07 18:15:18 +08:00
|
|
|
/* Nothing matched, just return the original DN */
|
|
|
|
if (res->bv_val == NULL) {
|
|
|
|
*res = *dn;
|
|
|
|
}
|
2001-01-20 05:27:20 +08:00
|
|
|
|
2003-04-07 18:15:18 +08:00
|
|
|
return 0;
|
2001-01-20 05:27:20 +08:00
|
|
|
}
|
2001-05-12 08:51:28 +08:00
|
|
|
#endif /* !ENABLE_REWRITE */
|