changeset 12890:6f0396e35fd9

login-common API redesign so that the library doesn't refer to nonexistent variables.
author Timo Sirainen <tss@iki.fi>
date Sat, 30 Apr 2011 13:17:53 +0300
parents bd869a7053c5
children 385664bd01be
files src/imap-login/client.c src/login-common/client-common-auth.c src/login-common/client-common.c src/login-common/client-common.h src/login-common/login-common.h src/login-common/login-settings.c src/login-common/main.c src/login-common/sasl-server.c src/pop3-login/client.c
diffstat 9 files changed, 79 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/src/imap-login/client.c	Tue Apr 05 12:44:21 2011 +0300
+++ b/src/imap-login/client.c	Sat Apr 30 13:17:53 2011 +0300
@@ -28,18 +28,6 @@
 /* Disconnect client when it sends too many bad commands */
 #define CLIENT_MAX_BAD_COMMANDS 10
 
-const struct login_binary login_binary = {
-	.protocol = "imap",
-	.process_name = "imap-login",
-	.default_port = 143,
-	.default_ssl_port = 993
-};
-
-void login_process_preinit(void)
-{
-	login_set_roots = imap_login_setting_roots;
-}
-
 /* Skip incoming data until newline is found,
    returns TRUE if newline was found. */
 bool client_skip_line(struct imap_client *client)
@@ -439,16 +427,21 @@
 	} T_END;
 }
 
-void clients_init(void)
+static void imap_login_preinit(void)
+{
+	login_set_roots = imap_login_setting_roots;
+}
+
+static void imap_login_init(void)
 {
 }
 
-void clients_deinit(void)
+static void imap_login_deinit(void)
 {
 	clients_destroy_all();
 }
 
-struct client_vfuncs client_vfuncs = {
+static struct client_vfuncs imap_client_vfuncs = {
 	imap_client_alloc,
 	imap_client_create,
 	imap_client_destroy,
@@ -462,3 +455,20 @@
 	imap_proxy_reset,
 	imap_proxy_parse_line
 };
+
+static const struct login_binary imap_login_binary = {
+	.protocol = "imap",
+	.process_name = "imap-login",
+	.default_port = 143,
+	.default_ssl_port = 993,
+
+	.client_vfuncs = &imap_client_vfuncs,
+	.preinit = imap_login_preinit,
+	.init = imap_login_init,
+	.deinit = imap_login_deinit
+};
+
+int main(int argc, char *argv[])
+{
+	return login_binary_run(&imap_login_binary, argc, argv);
+}
--- a/src/login-common/client-common-auth.c	Tue Apr 05 12:44:21 2011 +0300
+++ b/src/login-common/client-common-auth.c	Sat Apr 30 13:17:53 2011 +0300
@@ -96,7 +96,7 @@
 			if (strcmp(value, "any-cert") == 0)
 				reply_r->ssl_flags |= PROXY_SSL_FLAG_ANY_CERT;
 			if (reply_r->port == 0)
-				reply_r->port = login_binary.default_ssl_port;
+				reply_r->port = login_binary->default_ssl_port;
 		} else if (strcmp(key, "starttls") == 0) {
 			reply_r->ssl_flags |= PROXY_SSL_FLAG_YES |
 				PROXY_SSL_FLAG_STARTTLS;
@@ -108,7 +108,7 @@
 			i_debug("Ignoring unknown passdb extra field: %s", key);
 	}
 	if (reply_r->port == 0)
-		reply_r->port = login_binary.default_port;
+		reply_r->port = login_binary->default_port;
 
 	if (reply_r->destuser == NULL)
 		reply_r->destuser = client->virtual_user;
@@ -488,7 +488,7 @@
 
 	client_ref(client);
 	client->auth_initializing = TRUE;
-	sasl_server_auth_begin(client, login_binary.protocol, mech_name,
+	sasl_server_auth_begin(client, login_binary->protocol, mech_name,
 			       init_resp, sasl_callback);
 	client->auth_initializing = FALSE;
 	if (!client->authenticating)
--- a/src/login-common/client-common.c	Tue Apr 05 12:44:21 2011 +0300
+++ b/src/login-common/client-common.c	Sat Apr 30 13:17:53 2011 +0300
@@ -47,8 +47,8 @@
 
 	i_assert(fd != -1);
 
-	client = client_vfuncs.alloc(pool);
-	client->v = client_vfuncs;
+	client = login_binary->client_vfuncs->alloc(pool);
+	client->v = *login_binary->client_vfuncs;
 	if (client->v.auth_send_challenge == NULL)
 		client->v.auth_send_challenge = client_auth_send_challenge;
 	if (client->v.auth_parse_response == NULL)
@@ -371,7 +371,7 @@
 		for (i = 0; i < 3; i++)
 			tab[i].value = str_sanitize(tab[i].value, 80);
 	}
-	tab[3].value = login_binary.protocol;
+	tab[3].value = login_binary->protocol;
 	tab[4].value = getenv("HOME");
 	tab[5].value = net_ip2addr(&client->local_ip);
 	tab[6].value = net_ip2addr(&client->ip);
--- a/src/login-common/client-common.h	Tue Apr 05 12:44:21 2011 +0300
+++ b/src/login-common/client-common.h	Sat Apr 30 13:17:53 2011 +0300
@@ -127,7 +127,6 @@
 };
 
 extern struct client *clients;
-extern struct client_vfuncs client_vfuncs;
 
 struct client *
 client_create(int fd, bool ssl, pool_t pool,
@@ -174,7 +173,4 @@
 void client_destroy_oldest(void);
 void clients_destroy_all(void);
 
-void clients_init(void);
-void clients_deinit(void);
-
 #endif
--- a/src/login-common/login-common.h	Tue Apr 05 12:44:21 2011 +0300
+++ b/src/login-common/login-common.h	Sat Apr 30 13:17:53 2011 +0300
@@ -22,9 +22,14 @@
 	unsigned int default_port;
 	/* e.g. 993, 995. if there is no ssl port, use 0. */
 	unsigned int default_ssl_port;
+
+	const struct client_vfuncs *client_vfuncs;
+	void (*preinit)(void);
+	void (*init)(void);
+	void (*deinit)(void);
 };
 
-extern const struct login_binary login_binary;
+extern const struct login_binary *login_binary;
 extern struct auth_client *auth_client;
 extern struct master_auth *master_auth;
 extern bool closing_down;
@@ -36,6 +41,7 @@
 void login_refresh_proctitle(void);
 void login_client_destroyed(void);
 
-void login_process_preinit(void);
+int login_binary_run(const struct login_binary *binary,
+		     int argc, char *argv[]);
 
 #endif
--- a/src/login-common/login-settings.c	Tue Apr 05 12:44:21 2011 +0300
+++ b/src/login-common/login-settings.c	Sat Apr 30 13:17:53 2011 +0300
@@ -193,8 +193,8 @@
 
 	memset(&input, 0, sizeof(input));
 	input.roots = login_set_roots;
-	input.module = login_binary.process_name;
-	input.service = login_binary.protocol;
+	input.module = login_binary->process_name;
+	input.service = login_binary->protocol;
 	input.local_name = local_name;
 
 	if (local_ip != NULL)
--- a/src/login-common/main.c	Tue Apr 05 12:44:21 2011 +0300
+++ b/src/login-common/main.c	Sat Apr 30 13:17:53 2011 +0300
@@ -31,6 +31,7 @@
 	struct access_lookup *access;
 };
 
+const struct login_binary *login_binary;
 struct auth_client *auth_client;
 struct master_auth *master_auth;
 bool closing_down;
@@ -183,7 +184,7 @@
 		return;
 	}
 	lookup->access = access_lookup(*lookup->next_socket, lookup->conn.fd,
-				       login_binary.protocol,
+				       login_binary->protocol,
 				       login_access_callback, lookup);
 	if (lookup->access == NULL)
 		login_access_lookup_free(lookup);
@@ -303,9 +304,9 @@
 	auth_client = auth_client_init(login_socket, (unsigned int)getpid(),
 				       FALSE);
         auth_client_set_connect_notify(auth_client, auth_connect_notify, NULL);
-	master_auth = master_auth_init(master_service, login_binary.protocol);
+	master_auth = master_auth_init(master_service, login_binary->protocol);
 
-	clients_init();
+	login_binary->init();
 	login_proxy_init("proxy-notify");
 }
 
@@ -314,7 +315,7 @@
 	ssl_proxy_deinit();
 	login_proxy_deinit();
 
-	clients_deinit();
+	login_binary->deinit();
 	auth_client_deinit(&auth_client);
 	master_auth_deinit(&master_auth);
 
@@ -325,7 +326,8 @@
 	login_settings_deinit();
 }
 
-int main(int argc, char *argv[])
+int login_binary_run(const struct login_binary *binary,
+		     int argc, char *argv[])
 {
 	enum master_service_flags service_flags =
 		MASTER_SERVICE_FLAG_KEEP_CONFIG_OPEN |
@@ -335,10 +337,12 @@
 	const char *login_socket = DEFAULT_LOGIN_SOCKET;
 	int c;
 
-	master_service = master_service_init(login_binary.process_name,
+	login_binary = binary;
+
+	master_service = master_service_init(login_binary->process_name,
 					     service_flags, &argc, &argv, "DS");
 	master_service_init_log(master_service, t_strconcat(
-		login_binary.process_name, ": ", NULL));
+		login_binary->process_name, ": ", NULL));
 
 	while ((c = master_getopt(master_service)) > 0) {
 		switch (c) {
@@ -355,7 +359,7 @@
 	if (argv[optind] != NULL)
 		login_socket = argv[optind];
 
-	login_process_preinit();
+	login_binary->preinit();
 
 	set_pool = pool_alloconly_create("global login settings", 4096);
 	global_login_settings =
--- a/src/login-common/sasl-server.c	Tue Apr 05 12:44:21 2011 +0300
+++ b/src/login-common/sasl-server.c	Sat Apr 30 13:17:53 2011 +0300
@@ -191,7 +191,7 @@
 		return;
 	}
 
-	query = t_strconcat("LOOKUP\t", login_binary.protocol, "/",
+	query = t_strconcat("LOOKUP\t", login_binary->protocol, "/",
 			    net_ip2addr(&client->ip), "/",
 			    str_tabescape(client->virtual_user), NULL);
 	anvil_client_query(anvil, query, anvil_lookup_callback, req);
--- a/src/pop3-login/client.c	Tue Apr 05 12:44:21 2011 +0300
+++ b/src/pop3-login/client.c	Sat Apr 30 13:17:53 2011 +0300
@@ -22,18 +22,6 @@
 /* Disconnect client when it sends too many bad commands */
 #define CLIENT_MAX_BAD_COMMANDS 10
 
-const struct login_binary login_binary = {
-	.protocol = "pop3",
-	.process_name = "pop3-login",
-	.default_port = 110,
-	.default_ssl_port = 995
-};
-
-void login_process_preinit(void)
-{
-	login_set_roots = pop3_login_setting_roots;
-}
-
 static bool cmd_stls(struct pop3_client *client)
 {
 	client_cmd_starttls(&client->common);
@@ -224,18 +212,23 @@
 	/* do nothing. pop3 connections typically die pretty quick anyway. */
 }
 
-void clients_init(void)
+static void pop3_login_preinit(void)
+{
+	login_set_roots = pop3_login_setting_roots;
+}
+
+static void pop3_login_init(void)
 {
 	/* override the default login_die() */
 	master_service_set_die_callback(master_service, pop3_login_die);
 }
 
-void clients_deinit(void)
+static void pop3_login_deinit(void)
 {
 	clients_destroy_all();
 }
 
-struct client_vfuncs client_vfuncs = {
+static struct client_vfuncs pop3_client_vfuncs = {
 	pop3_client_alloc,
 	pop3_client_create,
 	pop3_client_destroy,
@@ -249,3 +242,20 @@
 	pop3_proxy_reset,
 	pop3_proxy_parse_line
 };
+
+static const struct login_binary pop3_login_binary = {
+	.protocol = "pop3",
+	.process_name = "pop3-login",
+	.default_port = 110,
+	.default_ssl_port = 995,
+
+	.client_vfuncs = &pop3_client_vfuncs,
+	.preinit = pop3_login_preinit,
+	.init = pop3_login_init,
+	.deinit = pop3_login_deinit
+};
+
+int main(int argc, char *argv[])
+{
+	return login_binary_run(&pop3_login_binary, argc, argv);
+}