2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3 ** Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <gnutls/gnutls.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
33 #include "hcconn_ssl.h"
34 #include "tcp_connect.h"
38 #define CONFFILE SYSCONFDIR "/popproxy.conf"
48 server_conn_new (char *server, char *port, int ssl)
54 fd = hc_tcp_connect (server, port);
57 g_warning ("Could not connect to server at %s:%s.", server, port);
60 conn = hc_conn_new (NULL, NULL);
61 r = hc_conn_set_driver_channel (conn, fd);
70 ssl_conn = hc_conn_new (NULL, NULL);
71 r = hc_conn_set_driver_ssl_client (ssl_conn, conn);
74 hc_conn_close (ssl_conn);
82 client_conn_new (int fd)
88 conn = hc_conn_new (NULL, NULL);
89 r = hc_conn_set_driver_channel (conn, fd);
96 ssl_conn = hc_conn_new (NULL, NULL);
97 hc_conn_set_driver_ssl_server (ssl_conn, conn);
100 hc_conn_close (ssl_conn);
101 hc_conn_close (conn);
104 pop_conn = hc_conn_new (NULL, NULL);
105 r = hc_conn_set_driver_pop (pop_conn, ssl_conn);
108 hc_conn_close (pop_conn);
109 hc_conn_close (ssl_conn);
116 push_other (HCConn *conn, HCEvent event, gpointer data)
123 while ((r = hc_conn_read (conn, buffer, sizeof (buffer))) > 0)
124 hc_conn_write (data, buffer, r);
127 hc_conn_close (conn);
128 hc_conn_close (data);
134 ssl_connected (HCConn *client_conn, HCEvent event, gpointer data)
136 struct pop_address *address = data;
138 if (event != HC_EVENT_CONNECT)
140 g_debug ("Did not get connect event when trying to handshake:"
142 hc_conn_close (client_conn);
145 server_conn = server_conn_new (address->server, address->port,
147 if (server_conn == NULL)
149 g_debug ("Failure to create connection to server.");
150 hc_conn_close (client_conn);
153 hc_conn_set_callback (client_conn, push_other, server_conn);
154 hc_conn_set_callback (server_conn, push_other, client_conn);
158 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
161 struct pop_address *address = data;
164 g_critical ("Server has received an error event.");
168 /* FIXME: Should be independent of address type. */
169 g_message ("Received connection from %s.",
170 inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
172 client_conn = client_conn_new (fd);
173 if (client_conn == NULL)
179 hc_conn_set_callback (client_conn, ssl_connected, address);
181 ssl_connected (client_conn, HC_EVENT_CONNECT, address);
185 static gchar *configfile;
186 static gboolean foreground;
188 static GOptionEntry opt_entries[] =
190 { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
191 "Configuration file location", "file" },
192 { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground,
193 "Run in foreground", 0 },
197 int main (int argc, char **argv)
200 GOptionContext *opt_ctx;
206 gchar *server_address;
212 struct pop_address pop_address;
214 signal (SIGPIPE, SIG_IGN);
216 gnutls_global_init ();
218 configfile = CONFFILE;
219 opt_ctx = g_option_context_new ("");
220 g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
223 if (!g_option_context_parse (opt_ctx, &argc, &argv, &error))
225 g_critical ("Could not parse command line options: %s.",
227 g_error_free (error);
230 g_option_context_free (opt_ctx);
232 keyfile = g_key_file_new ();
235 if (g_key_file_load_from_file (keyfile, configfile,
236 G_KEY_FILE_NONE, &error) == FALSE)
238 g_critical ("Could not load configuration file %s: %s.",
239 configfile, error->message);
240 g_error_free (error);
245 certfile = g_key_file_get_string (keyfile, "global", "certfile",
247 if (certfile == NULL && error != NULL)
249 g_critical ("No certification file specified: %s.",
251 g_error_free (error);
255 ssl_keyfile = g_key_file_get_string (keyfile, "global", "keyfile",
257 if (ssl_keyfile == NULL && error != NULL)
259 ssl_keyfile = g_strdup (certfile);
260 g_error_free (error);
265 conf_address = g_key_file_get_string (keyfile, "global", "address",
267 if (conf_address == NULL && error != NULL)
269 conf_address = g_strdup ("0.0.0.0");
270 g_error_free (error);
273 port = g_key_file_get_string (keyfile, "global", "port", &error);
274 if (port == NULL && error != NULL)
276 port = g_strdup ("110");
277 g_error_free (error);
280 server_address = g_key_file_get_string (keyfile, "global", "server",
282 if (server_address == NULL && error != NULL)
284 server_address = g_strdup ("127.0.0.1");
285 g_error_free (error);
288 server_port = g_key_file_get_string (keyfile, "global", "server_port",
290 if (server_port == NULL && error != NULL)
292 server_port = g_strdup ("995");
293 g_error_free (error);
296 server_ssl = g_key_file_get_boolean (keyfile, "global", "server_ssl",
298 if (server_ssl == 0 && error != NULL)
301 g_error_free (error);
305 policy = g_key_file_get_string (keyfile, "global", "policy",
307 if (policy == NULL && error != NULL)
309 policy = g_strdup ("deny");
310 g_error_free (error);
313 if (!strcmp (policy, "allow"))
314 ACCESS_DEFAULT = ACCESS_ALLOW;
318 pop_address.server = server_address;
319 pop_address.port = server_port;
320 pop_address.ssl = server_ssl;
322 server_fd = hc_tcp_server (port);
325 g_critical ("Could not create server.");
328 hc_server_add_watch (server_fd, new_client, &pop_address);
332 g_message ("Listening at %s:%s.", conf_address, port);
333 if (ACCESS_DEFAULT == ACCESS_ALLOW)
334 g_message ("Authorizing users by default.");
339 g_free (conf_address);
342 hc_conn_ssl_server_init_credentials (certfile, ssl_keyfile);
345 g_free (ssl_keyfile);
347 g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
349 gnutls_global_deinit ();
351 g_free (server_address);
352 g_free (server_port);