From c12547cf3bbae157b2588b686d17084b5699accd Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Tue, 27 Jul 1999 18:43:30 +0000 Subject: [PATCH] Resurrect suffix aliasing... --- servers/slapd/Makefile.in | 4 +- servers/slapd/bind.c | 3 ++ servers/slapd/compare.c | 3 ++ servers/slapd/config.c | 63 +++++++++++++++++++++++++++++++ servers/slapd/delete.c | 3 ++ servers/slapd/modify.c | 3 ++ servers/slapd/modrdn.c | 7 +++- servers/slapd/proto-slap.h | 5 +++ servers/slapd/search.c | 3 ++ servers/slapd/slap.h | 1 + servers/slapd/suffixalias.c | 74 +++++++++++++++++++++++++++++++++++++ 11 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 servers/slapd/suffixalias.c diff --git a/servers/slapd/Makefile.in b/servers/slapd/Makefile.in index 2e0aa85782..b442e97cba 100644 --- a/servers/slapd/Makefile.in +++ b/servers/slapd/Makefile.in @@ -11,7 +11,7 @@ SRCS = main.c daemon.c connection.c search.c filter.c add.c charray.c \ phonetic.c acl.c str2filter.c aclparse.c init.c user.c \ repl.c lock.c controls.c extended.c \ schema.c schemaparse.c monitor.c configinfo.c \ - root_dse.c module.c + root_dse.c module.c suffixalias.c OBJS = main.o daemon.o connection.o search.o filter.o add.o charray.o \ attr.o entry.o config.o backend.o result.o operation.o \ dn.o compare.o modify.o delete.o modrdn.o ch_malloc.o \ @@ -19,7 +19,7 @@ OBJS = main.o daemon.o connection.o search.o filter.o add.o charray.o \ phonetic.o acl.o str2filter.o aclparse.o init.o user.o \ repl.o lock.o controls.o extended.o \ schema.o schemaparse.o monitor.o configinfo.o \ - root_dse.o module.o + root_dse.o module.o suffixalias.o LDAP_INCDIR= ../../include LDAP_LIBDIR= ../../libraries diff --git a/servers/slapd/bind.c b/servers/slapd/bind.c index 74459983bb..bbd2f9d5fd 100644 --- a/servers/slapd/bind.c +++ b/servers/slapd/bind.c @@ -281,6 +281,9 @@ do_bind( /* alias suffix */ char *edn; + /* deref suffix alias if appropriate */ + ndn = suffix_alias( be, ndn ); + if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) { ldap_pvt_thread_mutex_lock( &conn->c_mutex ); diff --git a/servers/slapd/compare.c b/servers/slapd/compare.c index f3a8ae9c13..61380982ff 100644 --- a/servers/slapd/compare.c +++ b/servers/slapd/compare.c @@ -97,6 +97,9 @@ do_compare( return 1; } + /* deref suffix alias if appropriate */ + ndn = suffix_alias( be, ndn ); + if ( be->be_compare ) { (*be->be_compare)( be, conn, op, ndn, &ava ); } else { diff --git a/servers/slapd/config.c b/servers/slapd/config.c index e4d67fedde..4805e885c8 100644 --- a/servers/slapd/config.c +++ b/servers/slapd/config.c @@ -204,6 +204,7 @@ read_config( char *fname ) /* set database suffix */ } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) { + Backend *tmp_be; if ( cargc < 2 ) { Debug( LDAP_DEBUG_ANY, "%s: line %d: missing dn in \"suffix \" line\n", @@ -218,6 +219,14 @@ read_config( char *fname ) Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix line must appear inside a database definition (ignored)\n", fname, lineno, 0 ); + } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) { + Debug( LDAP_DEBUG_ANY, +"%s: line %d: suffix already served by this backend (ignored)\n", + fname, lineno, 0 ); + } else if ( tmp_be != NULL ) { + Debug( LDAP_DEBUG_ANY, +"%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n", + fname, lineno, tmp_be->be_suffix[0] ); } else { char *dn = ch_strdup( cargv[1] ); (void) dn_normalize( dn ); @@ -227,6 +236,60 @@ read_config( char *fname ) free( dn ); } + /* set database suffixAlias */ + } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) { + Backend *tmp_be; + if ( cargc < 2 ) { + Debug( LDAP_DEBUG_ANY, +"%s: line %d: missing alias and aliased_dn in \"suffixAlias \" line\n", + fname, lineno, 0 ); + return( 1 ); + } else if ( cargc < 3 ) { + Debug( LDAP_DEBUG_ANY, +"%s: line %d: missing aliased_dn in \"suffixAlias \" line\n", + fname, lineno, 0 ); + return( 1 ); + } else if ( cargc > 3 ) { + Debug( LDAP_DEBUG_ANY, + "%s: line %d: extra cruft in suffixAlias line (ignored)\n", + fname, lineno, 0 ); + } + + if ( be == NULL ) { + Debug( LDAP_DEBUG_ANY, + "%s: line %d: suffixAlias line" + " must appear inside a database definition (ignored)\n", + fname, lineno, 0 ); + } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) { + Debug( LDAP_DEBUG_ANY, + "%s: line %d: suffixAlias served by" + " a preceeding backend \"%s\" (ignored)\n", + fname, lineno, tmp_be->be_suffix[0] ); + + } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) { + Debug( LDAP_DEBUG_ANY, + "%s: line %d: suffixAlias derefs to differnet backend" + " a preceeding backend \"%s\" (ignored)\n", + fname, lineno, tmp_be->be_suffix[0] ); + + } else { + char *alias, *aliased_dn; + + alias = ch_strdup( cargv[1] ); + (void) dn_normalize( alias ); + + aliased_dn = ch_strdup( cargv[2] ); + (void) dn_normalize( aliased_dn ); + + (void) dn_normalize_case( alias ); + (void) dn_normalize_case( aliased_dn ); + charray_add( &be->be_suffixAlias, alias ); + charray_add( &be->be_suffixAlias, aliased_dn ); + + free(alias); + free(aliased_dn); + } + /* set max deref depth */ } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) { int i; diff --git a/servers/slapd/delete.c b/servers/slapd/delete.c index aa0bbc4b21..64cbefa675 100644 --- a/servers/slapd/delete.c +++ b/servers/slapd/delete.c @@ -81,6 +81,9 @@ do_delete( return rc; } + /* deref suffix alias if appropriate */ + ndn = suffix_alias( be, ndn ); + /* * do the delete if 1 && (2 || 3) * 1) there is a delete function implemented in this backend; diff --git a/servers/slapd/modify.c b/servers/slapd/modify.c index 2bf8ce5a5c..e7c01b86ed 100644 --- a/servers/slapd/modify.c +++ b/servers/slapd/modify.c @@ -172,6 +172,9 @@ do_modify( return rc; } + /* deref suffix alias if appropriate */ + ndn = suffix_alias( be, ndn ); + /* * do the modify if 1 && (2 || 3) * 1) there is a modify function implemented in this backend; diff --git a/servers/slapd/modrdn.c b/servers/slapd/modrdn.c index 50bbaeb68a..10349d001d 100644 --- a/servers/slapd/modrdn.c +++ b/servers/slapd/modrdn.c @@ -187,7 +187,6 @@ do_modrdn( return rc; } - /* Make sure that the entry being changed and the newSuperior are in * the same backend, otherwise we return an error. */ @@ -208,8 +207,14 @@ do_modrdn( return rc; } + + /* deref suffix alias if appropriate */ + nnewSuperior = suffix_alias( be, nnewSuperior ); } + /* deref suffix alias if appropriate */ + ndn = suffix_alias( be, ndn ); + /* * do the add if 1 && (2 || 3) * 1) there is an add function implemented in this backend; diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h index adf380e809..1d0b470042 100644 --- a/servers/slapd/proto-slap.h +++ b/servers/slapd/proto-slap.h @@ -350,6 +350,11 @@ char *scherr2str LDAP_P((int code)); Filter * str2filter LDAP_P(( char *str )); +/* + * suffixalias.c + */ +char *suffix_alias LDAP_P(( Backend *be, char *ndn )); + /* * value.c */ diff --git a/servers/slapd/search.c b/servers/slapd/search.c index cc74378ba9..a24a9f68ee 100644 --- a/servers/slapd/search.c +++ b/servers/slapd/search.c @@ -200,6 +200,9 @@ do_search( goto return_results; } + /* deref the base if needed */ + base = suffix_alias( be, base ); + /* actually do the search and send the result(s) */ if ( be->be_search ) { (*be->be_search)( be, conn, op, base, scope, deref, sizelimit, diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h index 2e3488f403..be69eb46e7 100644 --- a/servers/slapd/slap.h +++ b/servers/slapd/slap.h @@ -416,6 +416,7 @@ struct slap_backend_db { /* these should be renamed from be_ to bd_ */ char **be_suffix; /* the DN suffixes of data in this backend */ char **be_nsuffix; /* the normalized DN suffixes in this backend */ + char **be_suffixAlias; /* pairs of DN suffix aliases and deref values */ char *be_root_dn; /* the magic "root" dn for this db */ char *be_root_ndn; /* the magic "root" normalized dn for this db */ char *be_root_pw; /* the magic "root" password for this db */ diff --git a/servers/slapd/suffixalias.c b/servers/slapd/suffixalias.c new file mode 100644 index 0000000000..1b11005a5d --- /dev/null +++ b/servers/slapd/suffixalias.c @@ -0,0 +1,74 @@ +/* + * Copyright 1999 The OpenLDAP Foundation, All Rights Reserved. + * + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file in the top level + * directory of this package. + */ +/* Portions + * Copyright (c) 1998 Will Ballantyne, ITSD, Government of BC + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that this notice is preserved and that due credit is given + * to ITSD, Government of BC. The name of ITSD + * may not be used to endorse or promote products derived from this + * software without specific prior written permission. This software + * is provided ``as is'' without express or implied warranty. + */ + +#include "portable.h" + +#include +#include +#include +#include "slap.h" + +/* + * given a normalized uppercased dn (or root part), + * return an aliased dn if any of the alias suffixes match + */ +char *suffix_alias( + Backend *be, + char *dn ) +{ + int i, dnLength; + + if(dn == NULL) return NULL; + if(be == NULL) return dn; + + dnLength = strlen ( dn ); + + for ( i = 0; + be->be_suffixAlias != NULL && be->be_suffixAlias[i] != NULL; + i += 2 ) + { + int aliasLength = strlen (be->be_suffixAlias[i]); + int diff = dnLength - aliasLength; + + if ( diff < 0 ) { + /* alias is longer than dn */ + continue; + } else if ( diff > 0 ) { + if ( ! DNSEPARATOR(dn[diff-1]) ) { + /* boundary is not at a DN separator */ + continue; + } + /* At a DN Separator */ + /* XXX or an escaped separator... oh well */ + } + + if (!strcmp(be->be_suffixAlias[i], &dn[diff])) { + char *oldDN = dn; + dn = ch_malloc( diff + strlen(be->be_suffixAlias[i+1]) + 1 ); + strncpy( dn, oldDN, diff ); + strcpy( &dn[diff], be->be_suffixAlias[i+1] ); + Debug( LDAP_DEBUG_ARGS, + "suffix_alias: converted \"%s\" to \"%s\"\n", + oldDN, dn, 0); + free (oldDN); + break; + } + } + + return dn; +} -- 2.39.5