dovecot-2.0: Renamed "script" binary to "script-login".

dovecot at dovecot.org dovecot at dovecot.org
Wed Jun 2 19:21:26 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/addb2c6c1dfb
changeset: 11457:addb2c6c1dfb
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Jun 02 17:21:21 2010 +0100
description:
Renamed "script" binary to "script-login".

diffstat:

 .hgignore               |    2 +-
 src/util/Makefile.am    |   10 +-
 src/util/script-login.c |  204 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/script.c       |  204 ---------------------------------------------------
 4 files changed, 210 insertions(+), 210 deletions(-)

diffs (truncated from 455 to 300 lines):

diff -r 1e890076c4e9 -r addb2c6c1dfb .hgignore
--- a/.hgignore	Wed Jun 02 16:48:55 2010 +0100
+++ b/.hgignore	Wed Jun 02 17:21:21 2010 +0100
@@ -86,7 +86,7 @@
 src/util/listview
 src/util/maildirlock
 src/util/rawlog
-src/util/script
+src/util/script-login
 src/util/tcpwrap
 src/plugins/quota/rquota_xdr.c
 src/plugins/quota/rquota.h
diff -r 1e890076c4e9 -r addb2c6c1dfb src/util/Makefile.am
--- a/src/util/Makefile.am	Wed Jun 02 16:48:55 2010 +0100
+++ b/src/util/Makefile.am	Wed Jun 02 17:21:21 2010 +0100
@@ -2,7 +2,7 @@
 
 pkglibexec_PROGRAMS = \
 	rawlog \
-	script \
+	script-login \
 	$(TCPWRAP_BIN) \
 	gdbhelper \
 	listview \
@@ -27,10 +27,10 @@
 rawlog_SOURCES = \
 	rawlog.c
 
-script_LDADD = $(LIBDOVECOT_STORAGE) $(LIBDOVECOT) $(MODULE_LIBS)
-script_DEPENDENCIES = $(LIBDOVECOT_STORAGE) $(LIBDOVECOT_DEPS)
-script_SOURCES = \
-	script.c
+script_login_LDADD = $(LIBDOVECOT_STORAGE) $(LIBDOVECOT) $(MODULE_LIBS)
+script_login_DEPENDENCIES = $(LIBDOVECOT_STORAGE) $(LIBDOVECOT_DEPS)
+script_login_SOURCES = \
+	script-login.c
 
 if TCPWRAPPERS
 TCPWRAP_BIN = tcpwrap
diff -r 1e890076c4e9 -r addb2c6c1dfb src/util/script-login.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/util/script-login.c	Wed Jun 02 17:21:21 2010 +0100
@@ -0,0 +1,204 @@
+/* Copyright (c) 2009-2010 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "env-util.h"
+#include "execv-const.h"
+#include "fdpass.h"
+#include "restrict-access.h"
+#include "str.h"
+#include "strescape.h"
+#include "settings-parser.h"
+#include "mail-storage-service.h"
+#include "master-interface.h"
+#include "master-service.h"
+#include "master-service-settings.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#define ENV_USERDB_KEYS "USERDB_KEYS"
+#define SCRIPT_COMM_FD 3
+
+static const char **exec_args;
+static bool drop_privileges = FALSE;
+
+static void client_connected(struct master_service_connection *conn)
+{
+	enum mail_storage_service_flags flags =
+		MAIL_STORAGE_SERVICE_FLAG_NO_PLUGINS;
+	string_t *instr, *keys;
+	const char **args, *key, *value, *error;
+	struct mail_storage_service_ctx *service_ctx;
+	struct mail_storage_service_input input;
+	struct mail_storage_service_user *user;
+	char buf[1024];
+	unsigned int i, socket_count;
+	int fd = -1;
+	ssize_t ret;
+
+	net_set_nonblock(conn->fd, FALSE);
+	instr = t_str_new(1024);
+	ret = fd_read(conn->fd, buf, sizeof(buf), &fd);
+	while (ret > 0) {
+		str_append_n(instr, buf, ret);
+		if (buf[ret-1] == '\n') {
+			str_truncate(instr, str_len(instr)-1);
+			break;
+		}
+
+		ret = read(conn->fd, buf, sizeof(buf));
+	}
+	if (ret <= 0) {
+		if (ret < 0)
+			i_fatal("read() failed: %m");
+		else
+			i_fatal("read() failed: disconnected");
+	}
+	if (fd == -1)
+		i_fatal("client fd not received");
+
+	/* put everything to environment */
+	env_clean();
+	keys = t_str_new(256);
+	args = t_strsplit(str_c(instr), "\t");
+
+	if (str_array_length(args) < 3)
+		i_fatal("Missing input fields");
+
+	i = 0;
+	memset(&input, 0, sizeof(input));
+	input.module = "mail"; /* need to get mail_uid, mail_gid */
+	input.service = "script-login";
+	(void)net_addr2ip(args[i++], &input.local_ip);
+	(void)net_addr2ip(args[i++], &input.remote_ip);
+	input.username = args[i++];
+	input.userdb_fields = args + i;
+
+	env_put(t_strconcat("LOCAL_IP=", net_ip2addr(&input.local_ip), NULL));
+	env_put(t_strconcat("IP=", net_ip2addr(&input.remote_ip), NULL));
+	env_put(t_strconcat("USER=", input.username, NULL));
+
+	for (; args[i] != '\0'; i++) {
+		args[i] = str_tabunescape(t_strdup_noconst(args[i]));
+		value = strchr(args[i], '=');
+		if (value != NULL) {
+			key = t_str_ucase(t_strdup_until(args[i], value));
+			env_put(t_strconcat(key, value, NULL));
+			str_printfa(keys, "%s ", key);
+		}
+	}
+	env_put(t_strconcat(ENV_USERDB_KEYS"=", str_c(keys), NULL));
+
+	master_service_init_log(master_service,
+		t_strdup_printf("script-login(%s): ", input.username));
+
+	service_ctx = mail_storage_service_init(master_service, NULL, flags);
+	if (mail_storage_service_lookup(service_ctx, &input, &user, &error) <= 0)
+		i_fatal("%s", error);
+	mail_storage_service_restrict_setenv(service_ctx, user);
+
+	if (drop_privileges)
+		restrict_access_by_env(getenv("HOME"), TRUE);
+
+	if (dup2(fd, STDIN_FILENO) < 0)
+		i_fatal("dup2() failed: %m");
+	if (dup2(fd, STDOUT_FILENO) < 0)
+		i_fatal("dup2() failed: %m");
+	if (conn->fd != SCRIPT_COMM_FD) {
+		if (dup2(conn->fd, SCRIPT_COMM_FD) < 0)
+			i_fatal("dup2() failed: %m");
+	}
+
+	/* close all listener sockets */
+	socket_count = master_service_get_socket_count(master_service);
+	for (i = 0; i < socket_count; i++) {
+		if (close(MASTER_LISTEN_FD_FIRST + i) < 0)
+			i_error("close(listener) failed: %m");
+	}
+	if (close(MASTER_STATUS_FD) < 0)
+		i_error("close(status) failed: %m");
+
+	execvp_const(exec_args[0], exec_args);
+}
+
+static void script_execute_finish(void)
+{
+	const char *keys_str, *username, *const *keys, *value;
+	string_t *reply = t_str_new(512);
+	ssize_t ret;
+
+	keys_str = getenv(ENV_USERDB_KEYS);
+	if (keys_str == NULL)
+		i_fatal(ENV_USERDB_KEYS" environment missing");
+
+	username = getenv("USER");
+	if (username == NULL)
+		i_fatal("USER environment missing");
+	str_append(reply, username);
+
+	for (keys = t_strsplit_spaces(keys_str, " "); *keys != NULL; keys++) {
+		value = getenv(t_str_ucase(*keys));
+		if (value != NULL) {
+			str_append_c(reply, '\t');
+			str_tabescape_write(reply,
+					    t_strconcat(t_str_lcase(*keys), "=",
+							value, NULL));
+		}
+	}
+	str_append_c(reply, '\n');
+
+	ret = fd_send(SCRIPT_COMM_FD, STDOUT_FILENO,
+		      str_data(reply), str_len(reply));
+	if (ret < 0)
+		i_fatal("fd_send() failed: %m");
+	else if (ret != (ssize_t)str_len(reply))
+		i_fatal("fd_send() sent partial output");
+}
+
+int main(int argc, char *argv[])
+{
+	enum master_service_flags flags = 0;
+	int i, c;
+
+	if (getenv(MASTER_UID_ENV) == NULL)
+		flags |= MASTER_SERVICE_FLAG_STANDALONE;
+
+	master_service = master_service_init("script-login", flags,
+					     &argc, &argv, "d");
+	while ((c = master_getopt(master_service)) > 0) {
+		switch (c) {
+		case 'd':
+			drop_privileges = TRUE;
+			break;
+		default:
+			return FATAL_DEFAULT;
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	master_service_init_log(master_service, "script-login: ");
+	master_service_init_finish(master_service);
+	master_service_set_service_count(master_service, 1);
+
+	if ((flags & MASTER_SERVICE_FLAG_STANDALONE) != 0)
+		script_execute_finish();
+	else {
+		if (argv[0] == NULL)
+			i_fatal("Missing script-login path");
+		exec_args = i_new(const char *, argc + 2);
+		for (i = 0; i < argc; i++)
+			exec_args[i] = argv[i];
+		exec_args[i] = PKG_LIBEXECDIR"/script-login";
+		exec_args[i+1] = NULL;
+
+		if (exec_args[0][0] != '/') {
+			exec_args[0] = t_strconcat(PKG_LIBEXECDIR"/",
+						   exec_args[0], NULL);
+		}
+
+		master_service_run(master_service, client_connected);
+	}
+	master_service_deinit(&master_service);
+        return 0;
+}
diff -r 1e890076c4e9 -r addb2c6c1dfb src/util/script.c
--- a/src/util/script.c	Wed Jun 02 16:48:55 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,204 +0,0 @@
-/* Copyright (c) 2009-2010 Dovecot authors, see the included COPYING file */
-
-#include "lib.h"
-#include "env-util.h"
-#include "execv-const.h"
-#include "fdpass.h"
-#include "restrict-access.h"
-#include "str.h"
-#include "strescape.h"
-#include "settings-parser.h"
-#include "mail-storage-service.h"
-#include "master-interface.h"
-#include "master-service.h"
-#include "master-service-settings.h"
-
-#include <stdlib.h>
-#include <unistd.h>
-
-#define ENV_USERDB_KEYS "USERDB_KEYS"
-#define SCRIPT_COMM_FD 3
-
-static const char **exec_args;
-static bool drop_privileges = FALSE;
-
-static void client_connected(struct master_service_connection *conn)
-{
-	enum mail_storage_service_flags flags =
-		MAIL_STORAGE_SERVICE_FLAG_NO_PLUGINS;
-	string_t *instr, *keys;
-	const char **args, *key, *value, *error;
-	struct mail_storage_service_ctx *service_ctx;
-	struct mail_storage_service_input input;
-	struct mail_storage_service_user *user;
-	char buf[1024];
-	unsigned int i, socket_count;
-	int fd = -1;
-	ssize_t ret;
-
-	net_set_nonblock(conn->fd, FALSE);
-	instr = t_str_new(1024);
-	ret = fd_read(conn->fd, buf, sizeof(buf), &fd);
-	while (ret > 0) {
-		str_append_n(instr, buf, ret);
-		if (buf[ret-1] == '\n') {
-			str_truncate(instr, str_len(instr)-1);
-			break;
-		}
-
-		ret = read(conn->fd, buf, sizeof(buf));


More information about the dovecot-cvs mailing list