dovecot-1.1: expire-tool: Use mail_uid and mail_gid settings if ...

dovecot at dovecot.org dovecot at dovecot.org
Mon May 18 03:04:57 EEST 2009


details:   http://hg.dovecot.org/dovecot-1.1/rev/4ce7a60f3edd
changeset: 8273:4ce7a60f3edd
user:      Timo Sirainen <tss at iki.fi>
date:      Sun May 17 20:04:52 2009 -0400
description:
expire-tool: Use mail_uid and mail_gid settings if userdb doesn't return uid/gid.

diffstat:

3 files changed, 64 insertions(+), 11 deletions(-)
src/plugins/expire/auth-client.c   |   33 ++++++++++++++++++++----------
src/plugins/expire/expire-plugin.h |    3 ++
src/plugins/expire/expire-tool.c   |   39 ++++++++++++++++++++++++++++++++++++

diffs (147 lines):

diff -r 76c363b717a1 -r 4ce7a60f3edd src/plugins/expire/auth-client.c
--- a/src/plugins/expire/auth-client.c	Sun May 17 17:31:40 2009 -0400
+++ b/src/plugins/expire/auth-client.c	Sun May 17 20:04:52 2009 -0400
@@ -7,6 +7,7 @@
 #include "ostream.h"
 #include "env-util.h"
 #include "restrict-access.h"
+#include "expire-plugin.h"
 #include "auth-client.h"
 
 #include <stdlib.h>
@@ -92,19 +93,15 @@ static void auth_parse_input(struct auth
 {
 	const char *const *tmp, *key, *value;
 	uid_t uid = (uid_t)-1;
+	gid_t gid = (gid_t)-1;
 	int home_found = FALSE;
 
 	for (tmp = t_strsplit(args, "\t"); *tmp != NULL; tmp++) {
 		if (strncmp(*tmp, "uid=", 4) == 0)
 			uid = strtoul(*tmp + 4, NULL, 10);
-		else if (strncmp(*tmp, "gid=", 4) == 0) {
-			gid_t gid = strtoul(*tmp + 4, NULL, 10);
-
-			if (conn->orig_uid == 0 || getegid() != gid) {
-				env_put(t_strconcat("RESTRICT_SETGID=",
-						    *tmp + 4, NULL));
-			}
-		} else if (strncmp(*tmp, "chroot=", 7) == 0) {
+		else if (strncmp(*tmp, "gid=", 4) == 0)
+			gid = strtoul(*tmp + 4, NULL, 10);
+		else if (strncmp(*tmp, "chroot=", 7) == 0) {
 			env_put(t_strconcat("RESTRICT_CHROOT=",
 					    *tmp + 7, NULL));
 		} else if (strncmp(*tmp, "home=", 5) == 0) {
@@ -125,10 +122,24 @@ static void auth_parse_input(struct auth
 		return;
 	}
 
+	if (uid == (uid_t)-1)
+		uid = global_mail_uid;
 	if (uid == (uid_t)-1) {
-		i_error("userdb(%s) didn't return uid", conn->current_user);
-		return;
-	}
+		i_error("userdb(%s) didn't return uid and mail_uid not set",
+			conn->current_user);
+		return;
+	}
+
+	if (gid == (gid_t)-1)
+		gid = global_mail_gid;
+	if (gid == (gid_t)-1) {
+		i_error("userdb(%s) didn't return gid and mail_gid not set",
+			conn->current_user);
+		return;
+	}
+
+	if (conn->orig_uid == 0 || getegid() != gid)
+		env_put(t_strconcat("RESTRICT_SETGID=", *tmp + 4, NULL));
 
 	if (uid != conn->current_uid && conn->current_uid != 0) {
 		if (seteuid(0) != 0)
diff -r 76c363b717a1 -r 4ce7a60f3edd src/plugins/expire/expire-plugin.h
--- a/src/plugins/expire/expire-plugin.h	Sun May 17 17:31:40 2009 -0400
+++ b/src/plugins/expire/expire-plugin.h	Sun May 17 20:04:52 2009 -0400
@@ -1,5 +1,8 @@
 #ifndef EXPIRE_PLUGIN_H
 #define EXPIRE_PLUGIN_H
+
+extern uid_t global_mail_uid;
+extern gid_t global_mail_gid;
 
 void expire_plugin_init(void);
 void expire_plugin_deinit(void);
diff -r 76c363b717a1 -r 4ce7a60f3edd src/plugins/expire/expire-tool.c
--- a/src/plugins/expire/expire-tool.c	Sun May 17 17:31:40 2009 -0400
+++ b/src/plugins/expire/expire-tool.c	Sun May 17 20:04:52 2009 -0400
@@ -12,8 +12,11 @@
 #include "mail-namespace.h"
 #include "auth-client.h"
 #include "expire-env.h"
+#include "expire-plugin.h"
 
 #include <stdlib.h>
+#include <pwd.h>
+#include <grp.h>
 
 /* ugly, but automake doesn't like having it built as both static and
    dynamic object.. */
@@ -29,6 +32,9 @@ struct expire_context {
 	struct mail_namespace *ns;
 	bool testrun;
 };
+
+uid_t global_mail_uid;
+gid_t global_mail_gid;
 
 static int user_init(struct expire_context *ctx, const char *user)
 {
@@ -173,6 +179,37 @@ mailbox_delete_old_mails(struct expire_c
 	return ret < 0 ? -1 : 0;
 }
 
+static void expire_get_global_mail_ids(void)
+{
+	const struct passwd *pw;
+	const struct group *gr;
+	const char *str;
+
+	str = getenv("MAIL_UID");
+	if (str == NULL)
+		global_mail_uid = (uid_t)-1;
+	else if (is_numeric(str, '\0'))
+		global_mail_uid = strtoul(str, NULL, 10);
+	else {
+		pw = getpwnam(str);
+		if (pw == NULL)
+			i_fatal("mail_uid: User %s doesn't exist", str);
+		global_mail_uid = pw->pw_uid;
+	}
+
+	str = getenv("MAIL_GID");
+	if (str == NULL)
+		global_mail_gid = (gid_t)-1;
+	else if (is_numeric(str, '\0'))
+		global_mail_gid = strtoul(str, NULL, 10);
+	else {
+		gr = getgrnam(str);
+		if (gr == NULL)
+			i_fatal("mail_gid: Group %s doesn't exist", str);
+		global_mail_gid = gr->gr_gid;
+	}
+}
+
 static void expire_run(bool testrun)
 {
 	struct expire_context ctx;
@@ -195,6 +232,8 @@ static void expire_run(bool testrun)
 		i_fatal("expire and expire_altmove settings not set");
 	if (getenv("EXPIRE_DICT") == NULL)
 		i_fatal("expire_dict setting not set");
+
+	expire_get_global_mail_ids();
 
 	auth_socket = getenv("AUTH_SOCKET_PATH");
 	if (auth_socket == NULL)


More information about the dovecot-cvs mailing list