diff -upN qmail-ldap/Makefile qmail-ldap-domainalias/Makefile
--- qmail-ldap/Makefile	2007-02-14 15:16:27.000000000 +0100
+++ qmail-ldap-domainalias/Makefile	2007-03-06 11:17:43.000000000 +0100
@@ -19,7 +19,8 @@
 # -DQMQP_COMPRESS to use the QMQP on the fly compression (for clusters)
 # -DQUOTATRASH to include the Trash in the quota calculation (normaly it is not)
 # -DSMTPEXECCHECK to enable smtp DOS/Windows executable detection
-#LDAPFLAGS=-DQLDAP_CLUSTER -DEXTERNAL_TODO -DDASH_EXT -DDATA_COMPRESS -DQMQP_COMPRESS -DSMTPEXECCHECK
+# -DDOMAIN_ALIAS to enable domain aliasing support
+#LDAPFLAGS=-DQLDAP_CLUSTER -DEXTERNAL_TODO -DDASH_EXT -DDATA_COMPRESS -DQMQP_COMPRESS -DSMTPEXECCHECK -DDOMAIN_ALIAS
 
 # Perhaps you have different ldap libraries, change them here
 LDAPLIBS=-L/usr/local/lib -lldap -llber
@@ -117,14 +118,14 @@ auth_imap: \
 load auth_imap.o auth_mod.o checkpassword.o passwd.o digest_md4.o \
 digest_md5.o digest_rmd160.o digest_sha1.o base64.o read-ctrl.o getopt.a \
 control.o dirmaker.o mailmaker.o qldap.a localdelivery.o locallookup.o \
-pbsexec.o constmap.o getln.a strerr.a substdio.a stralloc.a env.a wait.a \
+pbsexec.o getln.a strerr.a substdio.a stralloc.a env.a wait.a \
 dns.o ip.o ipalloc.o ipme.o alloc.a str.a case.a fs.a error.a timeoutconn.o \
 timeoutread.o ndelay.a open.a sig.a prot.o auto_uids.o auto_qmail.o \
 dns.lib socket.lib
 	./load auth_imap auth_mod.o checkpassword.o passwd.o digest_md4.o \
 	digest_md5.o digest_rmd160.o digest_sha1.o base64.o read-ctrl.o \
 	getopt.a control.o dirmaker.o mailmaker.o qldap.a localdelivery.o \
-	locallookup.o pbsexec.o constmap.o getln.a strerr.a substdio.a \
+	locallookup.o pbsexec.o getln.a strerr.a substdio.a \
 	stralloc.a env.a wait.a dns.o ip.o ipalloc.o ipme.o alloc.a str.a \
 	case.a fs.a error.a timeoutconn.o timeoutread.o ndelay.a open.a \
 	sig.a prot.o auto_uids.o auto_qmail.o $(LDAPLIBS) $(SHADOWLIBS) \
@@ -146,14 +147,14 @@ auth_pop: \
 load auth_pop.o auth_mod.o checkpassword.o passwd.o digest_md4.o \
 digest_md5.o digest_rmd160.o digest_sha1.o base64.o read-ctrl.o getopt.a \
 control.o dirmaker.o mailmaker.o qldap.a localdelivery.o locallookup.o \
-pbsexec.o constmap.o getln.a strerr.a substdio.a stralloc.a env.a wait.a \
+pbsexec.o getln.a strerr.a substdio.a stralloc.a env.a wait.a \
 dns.o ip.o ipalloc.o ipme.o alloc.a str.a case.a fs.a error.a timeoutconn.o \
 timeoutread.o ndelay.a open.a prot.o auto_uids.o auto_qmail.o \
 dns.lib socket.lib
 	./load auth_pop auth_mod.o checkpassword.o passwd.o digest_md4.o \
 	digest_md5.o digest_rmd160.o digest_sha1.o base64.o read-ctrl.o \
 	getopt.a control.o qldap.a dirmaker.o mailmaker.o localdelivery.o \
-	locallookup.o pbsexec.o constmap.o getln.a strerr.a substdio.a \
+	locallookup.o pbsexec.o getln.a strerr.a substdio.a \
 	stralloc.a env.a wait.a dns.o ip.o ipalloc.o ipme.o alloc.a str.a \
 	case.a fs.a error.a timeoutconn.o timeoutread.o ndelay.a open.a \
 	prot.o auto_uids.o auto_qmail.o $(LDAPLIBS) $(SHADOWLIBS) \
@@ -168,11 +169,11 @@ timeoutread.h auth_mod.h
 auth_smtp: \
 load auth_smtp.o checkpassword.o passwd.o digest_md4.o digest_md5.o \
 digest_rmd160.o digest_sha1.o base64.o read-ctrl.o control.o qldap.a \
-constmap.o getln.a strerr.a substdio.a stralloc.a env.a alloc.a str.a \
+getln.a strerr.a substdio.a stralloc.a env.a alloc.a str.a \
 case.a fs.a error.a open.a prot.o auto_uids.o auto_qmail.o
 	./load auth_smtp checkpassword.o passwd.o digest_md4.o \
 	digest_md5.o digest_rmd160.o digest_sha1.o base64.o read-ctrl.o \
-	control.o qldap.a constmap.o getln.a strerr.a substdio.a stralloc.a \
+	control.o qldap.a getln.a strerr.a substdio.a stralloc.a \
 	env.a alloc.a str.a case.a fs.a error.a open.a prot.o auto_uids.o \
 	auto_qmail.o $(LDAPLIBS) $(SHADOWLIBS)
 	
@@ -1398,9 +1399,10 @@ substdio.h open.h byte.h str.h headerbod
 
 qldap.a: \
 makelib check.o output.o qldap.o qldap-cluster.o qldap-filter.o \
-qldap-debug.o qldap-errno.o auto_break.o
+qldap-debug.o qldap-errno.o auto_break.o constmap.o case_diffb.o
 	./makelib qldap.a check.o output.o qldap.o qldap-cluster.o \
-	qldap-filter.o qldap-debug.o qldap-errno.o auto_break.o
+	qldap-filter.o qldap-debug.o qldap-errno.o auto_break.o constmap.o \
+	case_diffb.o
 
 qldap.o: \
 compile qldap.c qldap.h alloc.h byte.h case.h check.h control.h error.h \
@@ -1612,12 +1614,12 @@ qmail-log.5
 
 qmail-ldaplookup: \
 load qmail-ldaplookup.o qldap.a passwd.o digest_md4.o digest_md5.o \
-digest_rmd160.o digest_sha1.o base64.o constmap.o localdelivery.o \
+digest_rmd160.o digest_sha1.o base64.o localdelivery.o \
 dirmaker.o wait.a read-ctrl.o control.o env.a getopt.a getln.a stralloc.a \
 alloc.a strerr.a error.a substdio.a open.a fs.a str.a case.a auto_usera.o \
 auto_qmail.o
 	./load qmail-ldaplookup qldap.a passwd.o digest_md4.o digest_md5.o \
-	digest_rmd160.o digest_sha1.o base64.o constmap.o localdelivery.o \
+	digest_rmd160.o digest_sha1.o base64.o localdelivery.o \
 	dirmaker.o wait.a read-ctrl.o control.o env.a getopt.a getln.a \
 	stralloc.a alloc.a strerr.a error.a substdio.a open.a fs.a str.a \
 	case.a auto_usera.o auto_qmail.o $(LDAPLIBS) $(SHADOWLIBS)
@@ -1635,9 +1637,9 @@ load qmail-lspawn.o spawn.o prot.o slurp
 sig.a strerr.a getln.a wait.a case.a cdb.a fd.a open.a stralloc.a \
 alloc.a substdio.a error.a str.a fs.a auto_qmail.o auto_uids.o \
 auto_spawn.o auto_usera.o env.a qldap.a dirmaker.o read-ctrl.o \
-localdelivery.o seek.a constmap.o
+localdelivery.o seek.a 
 	./load qmail-lspawn spawn.o prot.o slurpclose.o coe.o control.o \
-	qldap.a sig.a strerr.a constmap.o getln.a wait.a case.a cdb.a \
+	qldap.a sig.a strerr.a getln.a wait.a case.a cdb.a \
 	fd.a seek.a open.a dirmaker.o read-ctrl.o localdelivery.o env.a \
 	stralloc.a alloc.a substdio.a str.a error.a fs.a auto_qmail.o \
 	auto_uids.o auto_usera.o auto_spawn.o $(LDAPLIBS)
diff -upN qmail-ldap/qldap.c qmail-ldap-domainalias/qldap.c
--- qmail-ldap/qldap.c	2007-02-14 15:16:27.000000000 +0100
+++ qmail-ldap-domainalias/qldap.c	2007-03-06 11:18:47.000000000 +0100
@@ -52,6 +52,10 @@
 
 #include "qldap.h"
 
+#ifdef DOMAIN_ALIAS
+#include "constmap.h"
+#endif
+
 struct qldap {
 	int		state;
 #define NEW	0
@@ -81,6 +85,9 @@ unsigned int	default_uid = 0;
 unsigned int	default_gid = 0;
 unsigned long	quotasize = 0;
 unsigned long	quotacount = 0;
+#ifdef DOMAIN_ALIAS
+stralloc	domain_map_file = {0};
+#endif
 
 
 static  int qldap_close(qldap *);
@@ -210,6 +217,10 @@ qldap_ctrl_generic(void)
 		return -1;
 	logit(64, "init_ldap: control/defaultquotasize: %u\n", quotasize);
 	logit(64, "init_ldap: control/defaultquotacount: %u\n", quotacount);
+#ifdef DOMAIN_ALIAS
+	if (control_readfile(&domain_map_file, "control/aliasdomains", 0) == -1)
+		return -1;
+#endif
 
 	return 0;
 }
@@ -1055,3 +1066,33 @@ check_next_state(qldap *q, int next)
 	}
 }
 
+#ifdef DOMAIN_ALIAS
+/* Look up real domain based on alias domain name
+ * Returns NULL, if no such mapping found (for real domains, for example)
+ * Returns a static (char*) real_domain otherwise
+ * Caller must issue read_controls(...,qldap_ctrl_generic,...) BEFORE 
+ *   in order to properly initialize domain_map_file
+ * */
+char*
+qldap_domain_alias(const char* alias_domain)
+{
+	const char*	mapping;
+	static stralloc	real = {0};
+	struct constmap	domain_map;
+
+	/* caller must have issued read_controls(...,qldap_ctrl_generic,...) */
+	if(!constmap_init(&domain_map,domain_map_file.s,domain_map_file.len,1))
+		return 0;
+	mapping = constmap(&domain_map,alias_domain,str_len(alias_domain));
+
+	if(mapping && *mapping) {
+		if(!stralloc_copys(&real,mapping))
+			return 0;
+		if(!stralloc_0(&real))
+			return 0;
+		return real.s;
+	}
+	return 0;
+}
+#endif
+
diff -upN qmail-ldap/qldap-filter.c qmail-ldap-domainalias/qldap-filter.c
--- qmail-ldap/qldap-filter.c	2007-02-14 15:16:27.000000000 +0100
+++ qmail-ldap-domainalias/qldap-filter.c	2007-03-05 17:39:55.000000000 +0100
@@ -47,6 +47,8 @@ filter_escape(char *s)
 {
 	char	x;
 
+	if( !(s && *s) ) return 0;
+
 	/* pre reserve some space */
 	if (!stralloc_ready(&escapedstr, str_len(s))) return 0;
 	if (!stralloc_copys(&escapedstr, "")) return 0;
@@ -59,6 +61,35 @@ filter_escape(char *s)
 	return escapedstr.s;
 }
 
+#ifdef DOMAIN_ALIAS
+/* Returns a modified mail string if an alias_domain-real_domain mapping is found
+ * It simply returns its argument if no mapping is found
+ * On error returns NULL
+ * */
+char*
+replace_domain(const char* mail)
+{
+	static stralloc	new_mail = {0};
+	unsigned int	at;
+	char*		new_domain;
+
+	at = str_rchr(mail, '@');
+        if (mail[at] != '@') 
+		return (char*) 0;
+	
+	if (new_domain=qldap_domain_alias(mail + at + 1)) { 
+		if(!stralloc_copyb(&new_mail,mail,at + 1))
+			return (char*) 0;
+		if(!stralloc_cats(&new_mail,new_domain))
+			return (char*) 0;
+		if(!stralloc_0(&new_mail))
+			return (char*) 0;
+		return new_mail.s;	
+	}
+	return (char*) mail; /* Do nothing */
+}
+#endif
+
 static stralloc ocfilter = {0};
 extern stralloc	objectclass;
 
@@ -122,7 +153,11 @@ filter_mail(char *mail, int *done)
 	}
 
 	if (len == 0) {
+#ifdef DOMAIN_ALIAS
+		escaped = filter_escape(replace_domain(mail));
+#else
 		escaped = filter_escape(mail);
+#endif
 		if (escaped == (char *)0) return 0;
 		len = str_len(escaped);
 		at = str_rchr(escaped, '@');
diff -upN qmail-ldap/qldap.h qmail-ldap-domainalias/qldap.h
--- qmail-ldap/qldap.h	2007-02-14 15:16:27.000000000 +0100
+++ qmail-ldap-domainalias/qldap.h	2007-02-26 14:40:33.000000000 +0100
@@ -111,4 +111,6 @@ char *filter_objectclass(char *);
 char *filter_uid(char *);
 char *filter_mail(char *, int *);
 int filter_mail_ext(void);
+
+char *qldap_domain_alias(const char*);
 #endif
