changeset 697:7814b29d0862 HEAD

Created env_put() and env_clean() for a bit easier handling of environment variables.
author Timo Sirainen <tss@iki.fi>
date Tue, 26 Nov 2002 21:49:06 +0200
parents c7f42a8e7176
children bbc5f74c464f
files src/lib/Makefile.am src/lib/env-util.c src/lib/env-util.h src/lib/restrict-access.c src/master/auth-process.c src/master/imap-process.c src/master/login-process.c src/master/main.c
diffstat 8 files changed, 107 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/Makefile.am	Tue Nov 26 21:30:34 2002 +0200
+++ b/src/lib/Makefile.am	Tue Nov 26 21:49:06 2002 +0200
@@ -5,6 +5,7 @@
 	base64.c \
 	compat.c \
 	data-stack.c \
+	env-util.c \
 	failures.c \
 	fdpass.c \
 	file-lock.c \
@@ -50,6 +51,7 @@
 	base64.h \
 	compat.h \
 	data-stack.h \
+	env-util.h \
 	failures.h \
 	fdpass.h \
 	file-lock.h \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/env-util.c	Tue Nov 26 21:49:06 2002 +0200
@@ -0,0 +1,50 @@
+/*
+ env-util.c : Environment variable handling
+
+    Copyright (c) 2002 Timo Sirainen
+
+    Permission is hereby granted, free of charge, to any person obtaining
+    a copy of this software and associated documentation files (the
+    "Software"), to deal in the Software without restriction, including
+    without limitation the rights to use, copy, modify, merge, publish,
+    distribute, sublicense, and/or sell copies of the Software, and to
+    permit persons to whom the Software is furnished to do so, subject to
+    the following conditions:
+
+    The above copyright notice and this permission notice shall be
+    included in all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "lib.h"
+#include "env-util.h"
+
+#include <stdlib.h>
+
+static Pool pool = NULL;
+
+void env_put(const char *env)
+{
+	if (pool == NULL)
+		pool = pool_create("Environment", 1024, FALSE);
+
+	putenv(p_strdup(pool, env));
+}
+
+void env_clean(void)
+{
+	extern char **environ;
+
+	if (environ != NULL)
+		*environ = NULL;
+
+	pool_unref(pool);
+	pool = NULL;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/env-util.h	Tue Nov 26 21:49:06 2002 +0200
@@ -0,0 +1,10 @@
+#ifndef __ENV_UTIL_H
+#define __ENV_UTIL_H
+
+/* Add new environment variable. Wrapper to putenv(). Note that calls to this
+   function allocates memory which isn't free'd until env_clean() is called. */
+void env_put(const char *env);
+/* Clear all environment variables. */
+void env_clean(void);
+
+#endif
--- a/src/lib/restrict-access.c	Tue Nov 26 21:30:34 2002 +0200
+++ b/src/lib/restrict-access.c	Tue Nov 26 21:49:06 2002 +0200
@@ -23,6 +23,7 @@
 
 #include "lib.h"
 #include "restrict-access.h"
+#include "env-util.h"
 
 #include <stdlib.h>
 #include <unistd.h>
@@ -33,12 +34,12 @@
 			     const char *chroot_dir)
 {
 	if (user != NULL && *user != '\0')
-		putenv((char *) t_strconcat("USER=", user, NULL));
+		env_put(t_strconcat("USER=", user, NULL));
 	if (chroot_dir != NULL && *chroot_dir != '\0')
-		putenv((char *) t_strconcat("CHROOT=", chroot_dir, NULL));
+		env_put(t_strconcat("CHROOT=", chroot_dir, NULL));
 
-	putenv((char *) t_strdup_printf("SETUID=%ld", (long) uid));
-	putenv((char *) t_strdup_printf("SETGID=%ld", (long) gid));
+	env_put(t_strdup_printf("SETUID=%ld", (long) uid));
+	env_put(t_strdup_printf("SETGID=%ld", (long) gid));
 }
 
 void restrict_access_by_env(void)
--- a/src/master/auth-process.c	Tue Nov 26 21:30:34 2002 +0200
+++ b/src/master/auth-process.c	Tue Nov 26 21:49:06 2002 +0200
@@ -2,6 +2,7 @@
 
 #include "common.h"
 #include "ioloop.h"
+#include "env-util.h"
 #include "network.h"
 #include "obuffer.h"
 #include "restrict-access.h"
@@ -237,11 +238,11 @@
 				config->chroot);
 
 	/* set other environment */
-	putenv((char *) t_strdup_printf("AUTH_PROCESS=%d", (int) getpid()));
-	putenv((char *) t_strconcat("METHODS=", config->methods, NULL));
-	putenv((char *) t_strconcat("REALMS=", config->realms, NULL));
-	putenv((char *) t_strconcat("USERINFO=", config->userinfo, NULL));
-	putenv((char *) t_strconcat("USERINFO_ARGS=", config->userinfo_args,
+	env_put(t_strdup_printf("AUTH_PROCESS=%d", (int) getpid()));
+	env_put(t_strconcat("METHODS=", config->methods, NULL));
+	env_put(t_strconcat("REALMS=", config->realms, NULL));
+	env_put(t_strconcat("USERINFO=", config->userinfo, NULL));
+	env_put(t_strconcat("USERINFO_ARGS=", config->userinfo_args,
 				    NULL));
 	/* hide the path, it's ugly */
 	argv[0] = strrchr(config->executable, '/');
--- a/src/master/imap-process.c	Tue Nov 26 21:30:34 2002 +0200
+++ b/src/master/imap-process.c	Tue Nov 26 21:49:06 2002 +0200
@@ -1,6 +1,7 @@
 /* Copyright (C) 2002 Timo Sirainen */
 
 #include "common.h"
+#include "env-util.h"
 #include "restrict-access.h"
 
 #include <stdlib.h>
@@ -110,38 +111,36 @@
 
 	/* setup environment */
 	while (env[0] != NULL && env[1] != NULL) {
-		putenv((char *) t_strconcat(env[0], "=", env[1], NULL));
+		env_put(t_strconcat(env[0], "=", env[1], NULL));
 		env += 2;
 	}
 
-	putenv((char *) t_strconcat("HOME=", home, NULL));
-	putenv((char *) t_strconcat("MAIL_CACHE_FIELDS=",
-				    set_mail_cache_fields, NULL));
-	putenv((char *) t_strconcat("MAIL_NEVER_CACHE_FIELDS=",
-				    set_mail_never_cache_fields, NULL));
-	putenv((char *) t_strdup_printf("MAILBOX_CHECK_INTERVAL=%u",
-					set_mailbox_check_interval));
+	env_put(t_strconcat("HOME=", home, NULL));
+	env_put(t_strconcat("MAIL_CACHE_FIELDS=", set_mail_cache_fields, NULL));
+	env_put(t_strconcat("MAIL_NEVER_CACHE_FIELDS=",
+			    set_mail_never_cache_fields, NULL));
+	env_put(t_strdup_printf("MAILBOX_CHECK_INTERVAL=%u",
+				set_mailbox_check_interval));
 
 	if (set_mail_save_crlf)
-		putenv("MAIL_SAVE_CRLF=1");
+		env_put("MAIL_SAVE_CRLF=1");
 	if (set_mail_read_mmaped)
-		putenv("MAIL_READ_MMAPED=1");
+		env_put("MAIL_READ_MMAPED=1");
 	if (set_maildir_copy_with_hardlinks)
-		putenv("MAILDIR_COPY_WITH_HARDLINKS=1");
+		env_put("MAILDIR_COPY_WITH_HARDLINKS=1");
 	if (set_maildir_check_content_changes)
-		putenv("MAILDIR_CHECK_CONTENT_CHANGES=1");
+		env_put("MAILDIR_CHECK_CONTENT_CHANGES=1");
 	if (set_overwrite_incompatible_index)
-		putenv("OVERWRITE_INCOMPATIBLE_INDEX=1");
+		env_put("OVERWRITE_INCOMPATIBLE_INDEX=1");
 	if (umask(set_umask) != set_umask)
 		i_fatal("Invalid umask: %o", set_umask);
 
-	putenv((char *) t_strconcat("MBOX_LOCKS=", set_mbox_locks, NULL));
-	putenv((char *) t_strdup_printf("MBOX_LOCK_TIMEOUT=%u",
-					set_mbox_lock_timeout));
-	putenv((char *) t_strdup_printf("MBOX_DOTLOCK_CHANGE_TIMEOUT=%u",
-					set_mbox_dotlock_change_timeout));
+	env_put(t_strconcat("MBOX_LOCKS=", set_mbox_locks, NULL));
+	env_put(t_strdup_printf("MBOX_LOCK_TIMEOUT=%u", set_mbox_lock_timeout));
+	env_put(t_strdup_printf("MBOX_DOTLOCK_CHANGE_TIMEOUT=%u",
+				set_mbox_dotlock_change_timeout));
 	if (set_mbox_read_dotlock)
-		putenv("MBOX_READ_DOTLOCK=1");
+		env_put("MBOX_READ_DOTLOCK=1");
 
 	if (set_verbose_proctitle && net_ip2host(ip, host) == 0) {
 		i_snprintf(title, sizeof(title), "[%s %s]", user, host);
--- a/src/master/login-process.c	Tue Nov 26 21:30:34 2002 +0200
+++ b/src/master/login-process.c	Tue Nov 26 21:49:06 2002 +0200
@@ -5,12 +5,12 @@
 #include "network.h"
 #include "obuffer.h"
 #include "fdpass.h"
+#include "env-util.h"
 #include "restrict-access.h"
 #include "login-process.h"
 #include "auth-process.h"
 #include "master-interface.h"
 
-#include <stdlib.h>
 #include <unistd.h>
 #include <syslog.h>
 
@@ -303,23 +303,21 @@
 	}
 
 	if (!set_ssl_disable) {
-		putenv((char *) t_strconcat("SSL_CERT_FILE=",
-					    set_ssl_cert_file, NULL));
-		putenv((char *) t_strconcat("SSL_KEY_FILE=",
-					    set_ssl_key_file, NULL));
-		putenv((char *) t_strconcat("SSL_PARAM_FILE=",
-					    set_ssl_parameters_file, NULL));
+		env_put(t_strconcat("SSL_CERT_FILE=", set_ssl_cert_file, NULL));
+		env_put(t_strconcat("SSL_KEY_FILE=", set_ssl_key_file, NULL));
+		env_put(t_strconcat("SSL_PARAM_FILE=",
+				    set_ssl_parameters_file, NULL));
 	}
 
 	if (set_disable_plaintext_auth)
-		putenv("DISABLE_PLAINTEXT_AUTH=1");
+		env_put("DISABLE_PLAINTEXT_AUTH=1");
 
 	if (set_login_process_per_connection) {
-		putenv("PROCESS_PER_CONNECTION=1");
-		putenv("MAX_LOGGING_USERS=1");
+		env_put("PROCESS_PER_CONNECTION=1");
+		env_put("MAX_LOGGING_USERS=1");
 	} else {
-		putenv((char *) t_strdup_printf("MAX_LOGGING_USERS=%d",
-						set_max_logging_users));
+		env_put(t_strdup_printf("MAX_LOGGING_USERS=%d",
+					set_max_logging_users));
 	}
 
 	/* hide the path, it's ugly */
--- a/src/master/main.c	Tue Nov 26 21:30:34 2002 +0200
+++ b/src/master/main.c	Tue Nov 26 21:49:06 2002 +0200
@@ -4,6 +4,7 @@
 #include "ioloop.h"
 #include "lib-signals.h"
 #include "network.h"
+#include "env-util.h"
 
 #include "auth-process.h"
 #include "login-process.h"
@@ -46,21 +47,14 @@
 
 void clean_child_process(void)
 {
-	extern char **environ;
-
 	/* remove all environment, we don't need them */
-	if (environ != NULL)
-		*environ = NULL;
+	env_clean();
 
 	/* set the failure log */
-	if (set_log_path != NULL) {
-		putenv((char *) t_strconcat("IMAP_LOGFILE=",
-					    set_log_path, NULL));
-	}
-	if (set_log_timestamp != NULL) {
-		putenv((char *) t_strconcat("IMAP_LOGSTAMP=",
-					    set_log_timestamp, NULL));
-	}
+	if (set_log_path != NULL)
+		env_put(t_strconcat("IMAP_LOGFILE=", set_log_path, NULL));
+	if (set_log_timestamp != NULL)
+		env_put(t_strconcat("IMAP_LOGSTAMP=", set_log_timestamp, NULL));
 
 	(void)close(null_fd);
 	(void)close(imap_fd);