changeset 811:664de3231e33 HEAD

Added info_log_file setting and i_info() function to write to it. Default is the same as failure log. Currently only logged information is client logins.
author Timo Sirainen <tss@iki.fi>
date Fri, 20 Dec 2002 03:47:11 +0200
parents 30f6811f4952
children 9e6317fa800c
files dovecot-example.conf src/auth/main.c src/imap/main.c src/lib/failures.c src/lib/failures.h src/login/client.c src/login/main.c src/master/imap-process.c src/master/main.c src/master/settings.c src/master/settings.h
diffstat 11 files changed, 149 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/dovecot-example.conf	Fri Dec 20 01:56:23 2002 +0200
+++ b/dovecot-example.conf	Fri Dec 20 03:47:11 2002 +0200
@@ -49,6 +49,9 @@
 # Use this logfile instead of syslog()
 #log_path = 
 
+# For informational messages, use this logfile instead of the default
+#info_log_path = 
+
 # Prefix for each line written to log file. % codes are in strftime(3)
 # format. Note the extra space at the end of line.
 #log_timestamp = %b %d %H:%M:%S 
--- a/src/auth/main.c	Fri Dec 20 01:56:23 2002 +0200
+++ b/src/auth/main.c	Fri Dec 20 03:47:11 2002 +0200
@@ -45,8 +45,12 @@
 	else {
 		/* log to file or stderr */
 		i_set_failure_file(getenv("IMAP_LOGFILE"), "imap-auth");
-		i_set_failure_timestamp_format(getenv("IMAP_LOGSTAMP"));
 	}
+
+	if (getenv("IMAP_INFOLOGFILE") != NULL)
+		i_set_info_file(getenv("IMAP_INFOLOGFILE"));
+
+	i_set_failure_timestamp_format(getenv("IMAP_LOGSTAMP"));
 }
 
 static void drop_privileges(void)
--- a/src/imap/main.c	Fri Dec 20 01:56:23 2002 +0200
+++ b/src/imap/main.c	Fri Dec 20 03:47:11 2002 +0200
@@ -40,8 +40,12 @@
 	else {
 		/* log to file or stderr */
 		i_set_failure_file(getenv("IMAP_LOGFILE"), log_prefix);
-		i_set_failure_timestamp_format(getenv("IMAP_LOGSTAMP"));
 	}
+
+	if (getenv("IMAP_INFOLOGFILE") != NULL)
+		i_set_info_file(getenv("IMAP_INFOLOGFILE"));
+
+	i_set_failure_timestamp_format(getenv("IMAP_LOGSTAMP"));
 }
 
 static void drop_privileges(void)
--- a/src/lib/failures.c	Fri Dec 20 01:56:23 2002 +0200
+++ b/src/lib/failures.c	Fri Dec 20 03:47:11 2002 +0200
@@ -40,38 +40,38 @@
 
 static void default_error_handler(const char *format, va_list args);
 static void default_warning_handler(const char *format, va_list args);
+static void default_info_handler(const char *format, va_list args);
 
 /* Initialize working defaults */
 static FailureFunc panic_handler __attr_noreturn__ = default_panic_handler;
 static FatalFailureFunc fatal_handler __attr_noreturn__ = default_fatal_handler;
 static FailureFunc error_handler = default_error_handler;
 static FailureFunc warning_handler = default_warning_handler;
+static FailureFunc info_handler = default_info_handler;
 
-static FILE *log_fd = NULL;
+static FILE *log_fd = NULL, *log_info_fd = NULL;
 static char *log_prefix = NULL, *log_stamp_format = NULL;
 
-static void write_prefix(void)
+static void write_prefix(FILE *f)
 {
 	struct tm *tm;
 	char str[256];
 
-	if (log_fd == NULL)
-		log_fd = stderr;
-
 	if (log_prefix != NULL)
-		fputs(log_prefix, log_fd);
+		fputs(log_prefix, f);
 
 	if (log_stamp_format != NULL) {
 		tm = localtime(&ioloop_time);
 
 		if (strftime(str, sizeof(str), log_stamp_format, tm) > 0)
-			fputs(str, log_fd);
+			fputs(str, f);
 	}
 }
 
 static void default_panic_handler(const char *format, va_list args)
 {
-	write_prefix();
+	if (log_fd == NULL) log_fd = stderr;
+	write_prefix(log_fd);
 
 	fputs("Panic: ", log_fd);
 	vfprintf(log_fd, printf_string_fix_format(format), args);
@@ -82,7 +82,8 @@
 
 static void default_fatal_handler(int status, const char *format, va_list args)
 {
-	write_prefix();
+	if (log_fd == NULL) log_fd = stderr;
+	write_prefix(log_fd);
 
 	fputs("Fatal: ", log_fd);
 	vfprintf(log_fd, printf_string_fix_format(format), args);
@@ -98,7 +99,8 @@
 {
 	int old_errno = errno;
 
-	write_prefix();
+	if (log_fd == NULL) log_fd = stderr;
+	write_prefix(log_fd);
 
 	t_push();
 	fputs("Error: ", log_fd);
@@ -116,7 +118,8 @@
 {
 	int old_errno = errno;
 
-	write_prefix();
+	if (log_fd == NULL) log_fd = stderr;
+	write_prefix(log_fd);
 
 	t_push();
 	fputs("Warning: ", log_fd);
@@ -130,6 +133,24 @@
 	errno = old_errno;
 }
 
+static void default_info_handler(const char *format, va_list args)
+{
+	int old_errno = errno;
+
+	if (log_info_fd == NULL) log_info_fd = stderr;
+	write_prefix(log_info_fd);
+
+	t_push();
+	vfprintf(log_info_fd, printf_string_fix_format(format), args);
+	fputc('\n', log_info_fd);
+	t_pop();
+
+	if (fflush(log_info_fd) < 0)
+		exit(FATAL_LOGWRITE);
+
+	errno = old_errno;
+}
+
 void i_panic(const char *format, ...)
 {
 	va_list args;
@@ -175,6 +196,15 @@
 	va_end(args);
 }
 
+void i_info(const char *format, ...)
+{
+	va_list args;
+
+	va_start(args, format);
+	info_handler(format, args);
+	va_end(args);
+}
+
 void i_set_panic_handler(FailureFunc func __attr_noreturn__)
 {
 	if (func == NULL)
@@ -203,6 +233,13 @@
         warning_handler = func;
 }
 
+void i_set_info_handler(FailureFunc func)
+{
+	if (func == NULL)
+		func = default_info_handler;
+        info_handler = func;
+}
+
 void i_syslog_panic_handler(const char *fmt, va_list args)
 {
 	vsyslog(LOG_CRIT, fmt, args);
@@ -225,6 +262,11 @@
 	vsyslog(LOG_WARNING, fmt, args);
 }
 
+void i_syslog_info_handler(const char *fmt, va_list args)
+{
+	vsyslog(LOG_INFO, fmt, args);
+}
+
 void i_set_failure_syslog(const char *ident, int options, int facility)
 {
 	openlog(ident, options, facility);
@@ -233,28 +275,48 @@
 	i_set_fatal_handler(i_syslog_fatal_handler);
 	i_set_error_handler(i_syslog_error_handler);
 	i_set_warning_handler(i_syslog_warning_handler);
+	i_set_info_handler(i_syslog_info_handler);
+}
+
+static void open_log_file(FILE **file, const char *path)
+{
+	if (*file != NULL && *file != stderr)
+		(void)fclose(*file);
+
+	if (path == NULL)
+		*file = stderr;
+	else {
+		*file = fopen(path, "a");
+		if (*file == NULL) {
+			i_fatal_status(FATAL_LOGOPEN,
+				       "Can't open log file %s: %m", path);
+		}
+		fd_close_on_exec(fileno(*file), TRUE);
+	}
 }
 
 void i_set_failure_file(const char *path, const char *prefix)
 {
-	if (log_fd != NULL && log_fd != stderr)
-		(void)fclose(log_fd);
-
 	i_free(log_prefix);
 	log_prefix = i_strconcat(prefix, ": ", NULL);
 
-	if (path == NULL)
-		log_fd = stderr;
-	else {
-		log_fd = fopen(path, "a");
-		if (log_fd == NULL) {
-			i_fatal_status(FATAL_LOGOPEN,
-				       "Can't open log file %s: %m", path);
-		}
-		fd_close_on_exec(fileno(log_fd), TRUE);
+	open_log_file(&log_fd, path);
+
+	if (log_info_fd != NULL && log_info_fd != stderr) {
+		(void)fclose(log_info_fd);
+		log_info_fd = log_fd;
 	}
 }
 
+void i_set_info_file(const char *path)
+{
+	if (log_info_fd == log_fd)
+		log_info_fd = NULL;
+
+	open_log_file(&log_info_fd, path);
+        info_handler = default_info_handler;
+}
+
 void i_set_failure_timestamp_format(const char *fmt)
 {
 	i_free(log_stamp_format);
@@ -263,8 +325,16 @@
 
 void failures_deinit(void)
 {
+	if (log_info_fd == log_fd)
+		log_info_fd = NULL;
+
 	if (log_fd != NULL && log_fd != stderr) {
 		(void)fclose(log_fd);
 		log_fd = stderr;
 	}
+
+	if (log_info_fd != NULL && log_info_fd != stderr) {
+		(void)fclose(log_info_fd);
+		log_info_fd = stderr;
+	}
 }
--- a/src/lib/failures.h	Fri Dec 20 01:56:23 2002 +0200
+++ b/src/lib/failures.h	Fri Dec 20 03:47:11 2002 +0200
@@ -20,6 +20,7 @@
 void i_fatal(const char *format, ...) __attr_format__(1, 2) __attr_noreturn__;
 void i_error(const char *format, ...) __attr_format__(1, 2);
 void i_warning(const char *format, ...) __attr_format__(1, 2);
+void i_info(const char *format, ...) __attr_format__(1, 2);
 
 void i_fatal_status(int status, const char *format, ...)
 	__attr_format__(2, 3) __attr_noreturn__;
@@ -29,6 +30,7 @@
 void i_set_fatal_handler(FatalFailureFunc func __attr_noreturn__);
 void i_set_error_handler(FailureFunc func);
 void i_set_warning_handler(FailureFunc func);
+void i_set_info_handler(FailureFunc func);
 
 /* Send failures to syslog() */
 void i_syslog_panic_handler(const char *fmt, va_list args) __attr_noreturn__;
@@ -36,13 +38,18 @@
 	__attr_noreturn__;
 void i_syslog_error_handler(const char *fmt, va_list args);
 void i_syslog_warning_handler(const char *fmt, va_list args);
+void i_syslog_info_handler(const char *fmt, va_list args);
 
-/* Open syslog and set failure handlers to use it. */
+/* Open syslog and set failure/info handlers to use it. */
 void i_set_failure_syslog(const char *ident, int options, int facility);
 
 /* Send failures to specified log file instead of stderr. */
 void i_set_failure_file(const char *path, const char *prefix);
 
+/* Send informational messages to specified log file. i_set_failure_*()
+   functions modify the info file too, so call this function after them. */
+void i_set_info_file(const char *path);
+
 /* Prefix failures with a timestamp. fmt is in strftime() format. */
 void i_set_failure_timestamp_format(const char *fmt);
 
--- a/src/login/client.c	Fri Dec 20 01:56:23 2002 +0200
+++ b/src/login/client.c	Fri Dec 20 03:47:11 2002 +0200
@@ -376,7 +376,7 @@
 	if (host == NULL)
 		host = "??";
 
-	syslog(LOG_INFO, "%s [%s]", text, host);
+	i_info("%s [%s]", text, host);
 }
 
 static void client_hash_check_idle(void *key, void *value __attr_unused__,
--- a/src/login/main.c	Fri Dec 20 01:56:23 2002 +0200
+++ b/src/login/main.c	Fri Dec 20 03:47:11 2002 +0200
@@ -123,8 +123,12 @@
 	else {
 		/* log to file or stderr */
 		i_set_failure_file(getenv("IMAP_LOGFILE"), "imap-login");
-		i_set_failure_timestamp_format(getenv("IMAP_LOGSTAMP"));
 	}
+
+	if (getenv("IMAP_INFOLOGFILE") != NULL)
+		i_set_info_file(getenv("IMAP_INFOLOGFILE"));
+
+	i_set_failure_timestamp_format(getenv("IMAP_LOGSTAMP"));
 }
 
 static void drop_privileges(void)
--- a/src/master/imap-process.c	Fri Dec 20 01:56:23 2002 +0200
+++ b/src/master/imap-process.c	Fri Dec 20 03:47:11 2002 +0200
@@ -50,12 +50,13 @@
 	if (*dir == '\0')
 		return TRUE;
 
-	if (set_valid_chroot_dirs == '\0')
+	if (set_valid_chroot_dirs == NULL)
 		return FALSE;
 
 	chroot_dirs = t_strsplit(set_valid_chroot_dirs, ":");
 	while (*chroot_dirs != NULL) {
-		if (strncmp(dir, *chroot_dirs, strlen(*chroot_dirs)) == 0)
+		if (**chroot_dirs != '\0' &&
+		    strncmp(dir, *chroot_dirs, strlen(*chroot_dirs)) == 0)
 			return TRUE;
 		chroot_dirs++;
 	}
--- a/src/master/main.c	Fri Dec 20 01:56:23 2002 +0200
+++ b/src/master/main.c	Fri Dec 20 03:47:11 2002 +0200
@@ -52,10 +52,15 @@
 	env_clean();
 
 	/* set the failure log */
-	if (set_log_path != NULL)
+	if (set_log_path == NULL)
+		env_put("IMAP_USE_SYSLOG=1");
+	else
 		env_put(t_strconcat("IMAP_LOGFILE=", set_log_path, NULL));
-	else
-		env_put("IMAP_USE_SYSLOG=1");
+
+	if (set_info_log_path != NULL) {
+		env_put(t_strconcat("IMAP_INFOLOGFILE=",
+				    set_info_log_path, NULL));
+	}
 
 	if (set_log_timestamp != NULL)
 		env_put(t_strconcat("IMAP_LOGSTAMP=", set_log_timestamp, NULL));
@@ -148,7 +153,7 @@
 	IPADDR *ip;
 	int ret, ips_count;
 
-	if (name == NULL || *name == '\0')
+	if (name == NULL)
 		return NULL; /* defaults to "*" or "::" */
 
 	if (strcmp(name, "*") == 0) {
@@ -217,8 +222,12 @@
 	else {
 		/* log to file or stderr */
 		i_set_failure_file(set_log_path, "imap-master");
-		i_set_failure_timestamp_format(set_log_timestamp);
 	}
+
+	if (set_info_log_path != NULL)
+		i_set_info_file(set_info_log_path);
+
+	i_set_failure_timestamp_format(set_log_timestamp);
 }
 
 static void main_init(void)
--- a/src/master/settings.c	Fri Dec 20 01:56:23 2002 +0200
+++ b/src/master/settings.c	Fri Dec 20 03:47:11 2002 +0200
@@ -24,6 +24,7 @@
 
 static Setting settings[] = {
 	{ "log_path",		SET_STR, &set_log_path },
+	{ "info_log_path",	SET_STR, &set_info_log_path },
 	{ "log_timestamp",	SET_STR, &set_log_timestamp },
 
 	{ "imap_port",		SET_INT, &set_imap_port },
@@ -85,6 +86,7 @@
 
 /* common */
 char *set_log_path = NULL;
+char *set_info_log_path = NULL;
 char *set_log_timestamp = DEFAULT_FAILURE_STAMP_FORMAT;
 
 /* general */
@@ -163,8 +165,7 @@
 			i_fatal("Can't use auth executable %s: %m",
 				auth->executable);
 		}
-		if (auth->chroot != NULL && *auth->chroot != '\0' &&
-		    access(auth->chroot, X_OK) < 0) {
+		if (auth->chroot != NULL && access(auth->chroot, X_OK) < 0) {
 			i_fatal("Can't access auth chroot directory %s: %m",
 				auth->chroot);
 		}
@@ -204,13 +205,17 @@
 	}
 
 	if (set_log_path != NULL) {
-		/* log_path specifies a full file,  */
 		dir = get_directory(set_log_path);
-
 		if (access(dir, W_OK) < 0)
 			i_fatal("Can't access log directory %s: %m", dir);
 	}
 
+	if (set_info_log_path != NULL) {
+		dir = get_directory(set_info_log_path);
+		if (access(dir, W_OK) < 0)
+			i_fatal("Can't access info log directory %s: %m", dir);
+	}
+
 #ifdef HAVE_SSL
 	if (!set_ssl_disable) {
 		if (access(set_ssl_cert_file, R_OK) < 0) {
@@ -401,7 +406,8 @@
 		if (strcmp(set->name, key) == 0) {
 			switch (set->type) {
 			case SET_STR:
-				i_strdup_replace((char **) set->ptr, value);
+				i_free(*((char **)set->ptr));
+				*((char **)set->ptr) = i_strdup_empty(value);
 				break;
 			case SET_INT:
 				/* use %i so we can handle eg. 0600
--- a/src/master/settings.h	Fri Dec 20 01:56:23 2002 +0200
+++ b/src/master/settings.h	Fri Dec 20 03:47:11 2002 +0200
@@ -3,6 +3,7 @@
 
 /* common */
 extern char *set_log_path;
+extern char *set_info_log_path;
 extern char *set_log_timestamp;
 
 /* general */