From: "Adam D. Barratt" <debian-bts@adam-barratt.org.uk>
Date: Thu, 22 Jan 2004 09:27:04 +0000
Subject: 10 usermapprefix

Add UserMapPrefix option (bug#228059)  by Mark Smith & Adam D. Barratt
---
 man/spop3d.8      |    9 +++++++++
 man/spop3d.conf.5 |   10 ++++++++++
 src/configfile.h  |    1 +
 src/const.h       |    1 +
 src/main.c        |   30 ++++++++++++++++++++++++++++--
 5 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/man/spop3d.8 b/man/spop3d.8
index bb34bea..8b3d7e5 100644
--- a/man/spop3d.8
+++ b/man/spop3d.8
@@ -59,6 +59,15 @@ character
 .TP
 .B %m
 replace it with mapped user name (read VIRTUALS file)
+.TP
+.B %o
+replace it with the "original" user name. This is the user name with any
+.BR UserMapPrefix
+removed. Unless
+.BR DoMapping
+is enabled, and a
+.BR UserMapPrefix
+is supplied, this will be a blank string.
 .PP
 If specified path is not absolute it is treated as relative to user home
 directory. For example:
diff --git a/man/spop3d.conf.5 b/man/spop3d.conf.5
index 76fd136..7dd016d 100644
--- a/man/spop3d.conf.5
+++ b/man/spop3d.conf.5
@@ -149,6 +149,16 @@ Option is disabled by default.
 Specify path to user names mapping file (read VIRTUALS file).
 There is no default value.
 .TP
+.B UserMapPrefix string
+Specify a set of characters which should be prefixed to the username before
+it is looked up in the user mapping file.
+.br
+For example, given a username of "bar", and a \fBUserMapPrefix\fP of
+"foo-", there should be an entry in the user mapping file for "foo-bar".
+.br
+This option has no effect when DoMapping is disabled, and is a blank
+string by default.
+.TP
 .B AllowNonIP boolean
 If enabled user is allowed to specify virtual domain name in when logging.
 Read VIRTUALS file for details. Option is enabled by default.
diff --git a/src/configfile.h b/src/configfile.h
index 39c01e7..f297977 100644
--- a/src/configfile.h
+++ b/src/configfile.h
@@ -68,6 +68,7 @@ extern int domapping;
 extern int reqmapping;
 extern int authmappeduser;
 extern char sp_mapfile[];
+extern char sp_usermapprefix[];
 extern char mapfileowner[];
 #endif
 #ifdef NONIPVIRTUALS
diff --git a/src/const.h b/src/const.h
index 9379fd4..75de5b0 100644
--- a/src/const.h
+++ b/src/const.h
@@ -26,6 +26,7 @@
 #define MAXRESPLN 512
 #define MAXCMDLN 255
 #define MAXARGLN 40
+#define MAXPREFIX 16
 
 #define AUTH_STATE 0
 #define TRANSACTION_STATE 1
diff --git a/src/main.c b/src/main.c
index 9788030..8ec9867 100644
--- a/src/main.c
+++ b/src/main.c
@@ -177,6 +177,7 @@ struct str_option options_set[] =
 	{"DoMapping", OP_BOOLEAN, &domapping, 0, NULL},
 	{"AuthMappedUser", OP_BOOLEAN, &authmappeduser, 1, NULL},
 	{"RequiredMapping", OP_BOOLEAN, &reqmapping, 0, NULL},
+	{"UserMapPrefix", OP_STRING, &sp_usermapprefix, MAXPREFIX, NULL},
 #endif
 #ifdef NONIPVIRTUALS
 	{"AllowNonIP", OP_BOOLEAN, &allownonip, 0, NULL},
@@ -213,6 +214,9 @@ char maildrop_type[MAXMDTYPENAMELENGTH];
 char sp_mapfile[PATH_MAX];
 int domapping = 0, reqmapping = 1, authmappeduser = 1;
 char mapusername[MAXARGLN + 1];
+char origusername[MAXARGLN + 1];
+char usernamebuf[MAXARGLN + 1];
+char sp_usermapprefix[MAXPREFIX];
 #endif
 #ifdef APOP
 char apop_secret[MAXARGLN + 1];
@@ -312,6 +316,16 @@ int expand_dir(char *dir, char *homedir) {
 				strcat(filename, mapusername);
 				tmp = tmp2 + 2;
 				break;
+			case 'o':
+				*tmp2 = 0;
+				if ((strlen(filename) + strlen(tmp) + strlen(origusername) + 1) > sizeof(filename)) {
+				    breakwhile = 1;
+				    break;
+				};
+				strcat(filename, tmp);
+				strcat(filename, origusername);
+				tmp = tmp2 + 2;
+				break;
 #endif
 			case 'd':
 				if ((tmp2[2] < '1') || (tmp2[2] > '8')) {
@@ -1047,9 +1061,11 @@ int main(int argc, char **argv)
 	signal(SIGUSR1, sig_handler);
 	signal(SIGUSR2, sig_handler);
 	memset(username, 0, sizeof(username));
-	memset(password, 0, sizeof(username));
+	memset(password, 0, sizeof(password));
 #ifdef MAPPING
 	memset(mapusername, 0, sizeof(mapusername));
+	memset(origusername, 0, sizeof(origusername));
+	memset(usernamebuf, 0, sizeof(usernamebuf));
 #endif
 	alarm(0);
 	umask(0077);
@@ -1216,6 +1232,10 @@ int main(int argc, char **argv)
 #ifdef MAPPING
 			mapusername[0] = 0;
 			if (domapping) {
+				if (sp_usermapprefix[0] != '\0') {
+					snprintf(usernamebuf, MAXARGLN, "%s%s", sp_usermapprefix, username);
+					strcpy(username,usernamebuf);
+				}
 /* map_finduser() doesn't overflow mapusername - sizeof(mapusername) = MAXARGLN + 1 */
 				if (((tmp = map_finduser(sp_mapfile, username, mapusername)) < 0) && \
 				    (reqmapping)) {
@@ -1348,8 +1368,14 @@ int main(int argc, char **argv)
 			wait(NULL);
 			check_logpriority(logpriority);
 #ifdef MAPPING
-			if (domapping)
+			if (domapping) {
+				strcpy(origusername,username);
+				if (sp_usermapprefix[0] != '\0') {
+					snprintf(usernamebuf, MAXARGLN, "%s%s", sp_usermapprefix, username);
+					strcpy(username,usernamebuf);
+				}
 				userentry = getpwnam(mapusername);
+			}
 			else
 				userentry = getpwnam(username);
 #else
-- 
