dovecot-1.3: Pass remote/local IPs to mail_users. Standalone mai...

dovecot at dovecot.org dovecot at dovecot.org
Tue Apr 21 02:11:58 EEST 2009


details:   http://hg.dovecot.org/dovecot-1.3/rev/bf448752f6c4
changeset: 9147:bf448752f6c4
user:      Timo Sirainen <tss at iki.fi>
date:      Mon Apr 20 19:07:24 2009 -0400
description:
Pass remote/local IPs to mail_users. Standalone mail programs now log with mail_log_prefix.

diffstat:

13 files changed, 145 insertions(+), 60 deletions(-)
src/imap/main.c                         |   18 +++--
src/lda/main.c                          |    7 +-
src/lib-storage/mail-storage-service.c  |  101 ++++++++++++++++++++-----------
src/lib-storage/mail-storage-service.h  |   13 +++
src/lib-storage/mail-storage-settings.c |    4 +
src/lib-storage/mail-storage-settings.h |    2 
src/lib-storage/mail-user.c             |    4 -
src/lib/failures.c                      |    1 
src/lmtp/commands.c                     |    8 ++
src/plugins/convert/convert-tool.c      |    8 +-
src/plugins/expire/expire-tool.c        |    6 +
src/pop3/main.c                         |   21 ++++--
src/util/doveadm.c                      |   12 ++-

diffs (truncated from 589 to 300 lines):

diff -r 1e26166a3cf8 -r bf448752f6c4 src/imap/main.c
--- a/src/imap/main.c	Mon Apr 20 18:07:46 2009 -0400
+++ b/src/imap/main.c	Mon Apr 20 19:07:24 2009 -0400
@@ -138,10 +138,11 @@ int main(int argc, char *argv[], char *e
 	};
 	enum master_service_flags service_flags = 0;
 	enum mail_storage_service_flags storage_service_flags = 0;
+	struct mail_storage_service_input input;
 	struct mail_user *mail_user;
 	const struct imap_settings *set;
-	const char *user;
 	bool dump_capability;
+	const char *value;
 	int c;
 
 #ifdef DEBUG
@@ -172,18 +173,23 @@ int main(int argc, char *argv[], char *e
 			i_fatal("Unknown argument: %c", c);
 	}
 
-	user = getenv("USER");
-	if (user == NULL) {
+	memset(&input, 0, sizeof(input));
+	input.username = getenv("USER");
+	if (input.username == NULL) {
 		if (IS_STANDALONE())
-			user = getlogin();
-		if (user == NULL)
+			input.username = getlogin();
+		if (input.username == NULL)
 			i_fatal("USER environment missing");
 	}
+	if ((value = getenv("IP")) != NULL)
+		net_addr2ip(value, &input.remote_ip);
+	if ((value = getenv("LOCAL_IP")) != NULL)
+		net_addr2ip(value, &input.local_ip);
 
 	/* plugins may want to add commands, so this needs to be called early */
 	commands_init();
 
-	mail_user = mail_storage_service_init_user(service, user, set_roots,
+	mail_user = mail_storage_service_init_user(service, &input, set_roots,
 						   storage_service_flags);
 	set = mail_storage_service_get_settings(service);
 	restrict_access_allow_coredumps(TRUE);
diff -r 1e26166a3cf8 -r bf448752f6c4 src/lda/main.c
--- a/src/lda/main.c	Mon Apr 20 18:07:46 2009 -0400
+++ b/src/lda/main.c	Mon Apr 20 19:07:24 2009 -0400
@@ -183,6 +183,7 @@ int main(int argc, char *argv[])
 	struct mail_deliver_context ctx;
 	enum mail_storage_service_flags service_flags = 0;
 	const char *user, *errstr, *path, *getopt_str;
+	struct mail_storage_service_input service_input;
 	struct mail_user *raw_mail_user;
 	struct mail_namespace *raw_ns;
 	struct mail_namespace_settings raw_ns_set;
@@ -317,8 +318,12 @@ int main(int argc, char *argv[])
 			"destination user parameter (-d user) not given");
 	}
 
+	memset(&service_input, 0, sizeof(service_input));
+	service_input.username = user;
+
 	service_flags |= MAIL_STORAGE_SERVICE_FLAG_DISALLOW_ROOT;
-	ctx.dest_user = mail_storage_service_init_user(service, user, set_roots,
+	ctx.dest_user = mail_storage_service_init_user(service, &service_input,
+						       set_roots,
 						       service_flags);
 	ctx.set = mail_storage_service_get_settings(service);
         duplicate_init(mail_user_set_get_storage_set(ctx.dest_user->set));
diff -r 1e26166a3cf8 -r bf448752f6c4 src/lib-storage/mail-storage-service.c
--- a/src/lib-storage/mail-storage-service.c	Mon Apr 20 18:07:46 2009 -0400
+++ b/src/lib-storage/mail-storage-service.c	Mon Apr 20 19:07:24 2009 -0400
@@ -31,7 +31,8 @@ struct mail_storage_service_multi_ctx {
 
 struct mail_storage_service_multi_user {
 	pool_t pool;
-	const char *user;
+	struct mail_storage_service_input input;
+
 	const char *system_groups_user;
 	const struct mail_user_settings *user_set;
 	struct setting_parser_context *set_parser;
@@ -178,7 +179,6 @@ service_auth_userdb_lookup(struct settin
 	if (ret > 0 && strcmp(*user, orig_user) != 0) {
 		if (mail_user_set_get_storage_set(user_set)->mail_debug)
 			i_info("changed username to %s", *user);
-		i_set_failure_prefix(t_strdup_printf("%s(%s): ", name, *user));
 	}
 
 	auth_master_deinit(&conn);
@@ -336,7 +336,8 @@ mail_storage_service_init_settings(struc
 
 static int
 mail_storage_service_init_post(struct master_service *service,
-			       const char *user, const char *home,
+			       const struct mail_storage_service_input *input,
+			       const char *home,
 			       const struct mail_user_settings *user_set,
 			       bool setuid_root, struct mail_user **mail_user_r,
 			       const char **error_r)
@@ -367,9 +368,10 @@ mail_storage_service_init_post(struct ma
 		}
 	}
 
-	mail_user = mail_user_alloc(user, user_set);
+	mail_user = mail_user_alloc(input->username, user_set);
 	mail_user_set_home(mail_user, *home == '\0' ? NULL : home);
-	mail_user_set_vars(mail_user, geteuid(), service->name, NULL, NULL);
+	mail_user_set_vars(mail_user, geteuid(), service->name,
+			   &input->local_ip, &input->remote_ip);
 	if (mail_user_init(mail_user, error_r) < 0) {
 		mail_user_unref(&mail_user);
 		return -1;
@@ -383,13 +385,16 @@ mail_storage_service_init_post(struct ma
 }
 
 static const struct var_expand_table *
-get_var_expand_table(struct master_service *service, const char *user)
+get_var_expand_table(struct master_service *service,
+		     struct mail_storage_service_input *input)
 {
 	static struct var_expand_table static_tab[] = {
 		{ 'u', NULL, "user" },
 		{ 'n', NULL, "username" },
 		{ 'd', NULL, "domain" },
 		{ 's', NULL, "service" },
+		{ 'l', NULL, "lip" },
+		{ 'r', NULL, "rip" },
 		{ 'p', NULL, "pid" },
 		{ 'i', NULL, "uid" },
 		{ '\0', NULL, NULL }
@@ -399,19 +404,21 @@ get_var_expand_table(struct master_servi
 	tab = t_malloc(sizeof(static_tab));
 	memcpy(tab, static_tab, sizeof(static_tab));
 
-	tab[0].value = user;
-	tab[1].value = t_strcut(user, '@');
-	tab[2].value = strchr(user, '@');
+	tab[0].value = input->username;
+	tab[1].value = t_strcut(input->username, '@');
+	tab[2].value = strchr(input->username, '@');
 	if (tab[2].value != NULL) tab[2].value++;
 	tab[3].value = service->name;
-	tab[4].value = my_pid;
-	tab[5].value = dec2str(geteuid());
+	tab[4].value = net_ip2addr(&input->local_ip);
+	tab[5].value = net_ip2addr(&input->remote_ip);
+	tab[6].value = my_pid;
+	tab[7].value = dec2str(geteuid());
 	return tab;
 }
 
 static const char *
-user_expand_varstr(struct master_service *service, const char *user,
-		   const char *str)
+user_expand_varstr(struct master_service *service,
+		   struct mail_storage_service_input *input, const char *str)
 {
 	string_t *ret;
 
@@ -421,21 +428,43 @@ user_expand_varstr(struct master_service
 	i_assert(*str == SETTING_STRVAR_UNEXPANDED[0]);
 
 	ret = t_str_new(256);
-	var_expand(ret, str + 1, get_var_expand_table(service, user));
+	var_expand(ret, str + 1, get_var_expand_table(service, input));
 	return str_c(ret);
 }
 
+static void
+mail_storage_service_init_log(struct master_service *service,
+			      struct mail_storage_service_input *input)
+{
+	const struct mail_user_settings *user_set;
+	void **sets;
+
+	sets = master_service_settings_get_others(service);
+	user_set = sets[0];
+
+	T_BEGIN {
+		string_t *str;
+
+		str = t_str_new(256);
+		var_expand(str, user_set->mail_log_prefix,
+			   get_var_expand_table(service, input));
+		master_service_init_log(service, str_c(str));
+	} T_END;
+}
+
 struct mail_user *
-mail_storage_service_init_user(struct master_service *service, const char *user,
+mail_storage_service_init_user(struct master_service *service,
+			       const struct mail_storage_service_input *_input,
 			       const struct setting_parser_info *set_roots[],
 			       enum mail_storage_service_flags flags)
 {
+	struct mail_storage_service_input input = *_input;
 	const struct master_service_settings *set;
 	const struct mail_user_settings *user_set;
 	const struct mail_storage_settings *mail_set;
 	struct mail_user *mail_user;
 	void **sets;
-	const char *orig_user, *home, *system_groups_user, *error;
+	const char *user, *orig_user, *home, *system_groups_user, *error;
 	unsigned int len;
 	bool userdb_lookup;
 
@@ -446,8 +475,7 @@ mail_storage_service_init_user(struct ma
 		set_keyval(service->set_parser, "mail_debug", "yes");
 
 	/* now that we've read settings, we can set up logging */
-	master_service_init_log(service,
-		t_strdup_printf("%s(%s): ", service->name, user));
+	mail_storage_service_init_log(service, &input);
 
 	set = master_service_settings_get(service);
 	sets = master_service_settings_get_others(service);
@@ -457,17 +485,21 @@ mail_storage_service_init_user(struct ma
 	if (userdb_lookup) {
 		/* userdb lookup may change settings, do it as soon as
 		   possible. */
-		orig_user = user;
+		orig_user = user = input.username;
 		if (service_auth_userdb_lookup(service->set_parser,
 					       service->name, user_set, &user,
 					       &system_groups_user,
 					       &error) <= 0)
 			i_fatal("%s", error);
+		input.username = user;
+
+		/* set up logging again in case username changed */
+		mail_storage_service_init_log(service, &input);
 	}
 
 	/* variable strings are expanded in mail_user_init(),
 	   but we need the home sooner so do it separately here. */
-	home = user_expand_varstr(service, user, user_set->mail_home);
+	home = user_expand_varstr(service, &input, user_set->mail_home);
 
 	if (!userdb_lookup) {
 		system_groups_user = NULL;
@@ -500,8 +532,8 @@ mail_storage_service_init_user(struct ma
 	dict_drivers_register_builtin();
 	module_dir_init(modules);
 	mail_users_init(user_set->auth_socket_path, mail_set->mail_debug);
-	if (mail_storage_service_init_post(service, user, home, user_set, FALSE,
-					   &mail_user, &error) < 0)
+	if (mail_storage_service_init_post(service, &input, home, user_set,
+					   FALSE, &mail_user, &error) < 0)
 		i_fatal("%s", error);
 	return mail_user;
 }
@@ -552,36 +584,37 @@ mail_storage_service_multi_init(struct m
 }
 
 int mail_storage_service_multi_lookup(struct mail_storage_service_multi_ctx *ctx,
-				      const char *username, pool_t pool,
+				      const struct mail_storage_service_input *input,
+				      pool_t pool,
 				      struct mail_storage_service_multi_user **user_r,
 				      const char **error_r)
 {
 	struct mail_storage_service_multi_user *user;
-	const char *orig_user;
+	const char *orig_user, *username;
 	void **sets;
 	int ret;
 
 	user = p_new(pool, struct mail_storage_service_multi_user, 1);
 	memset(user_r, 0, sizeof(user_r));
 	user->pool = pool;
-	user->user = username;
+	user->input = *input;
+	user->input.username = p_strdup(pool, input->username);
 
 	user->set_parser = settings_parser_dup(ctx->service->set_parser, pool);
 	sets = settings_parser_get_list(user->set_parser);
 	user->user_set = sets[1];
 
 	if ((ctx->flags & MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP) != 0) {
-		orig_user = user->user;
+		orig_user = username = user->input.username;
 		ret = service_auth_userdb_lookup(user->set_parser,
 						 ctx->service->name,
-						 user->user_set,
-						 &user->user,
+						 user->user_set, &username,
 						 &user->system_groups_user,
 						 error_r);
 		if (ret <= 0)
 			return ret;
-	}
-	user->user = p_strdup(pool, user->user);
+		user->input.username = p_strdup(pool, username);
+	}
 	*user_r = user;
 	return 1;
 }
@@ -597,8 +630,10 @@ int mail_storage_service_multi_next(stru


More information about the dovecot-cvs mailing list