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>
32 #include "hcconn_ssl.h"
33 #include "tcp_connect.h"
37 #define CONFFILE SYSCONFDIR "/popproxy.conf"
47 server_conn_new (char *server, char *port, int ssl)
53 fd = hc_tcp_connect (server, port);
56 g_warning ("Could not connect to server at %s:%s.", server, port);
59 conn = hc_conn_new (NULL, NULL);
60 r = hc_conn_set_driver_channel (conn, fd);
69 ssl_conn = hc_conn_new (NULL, NULL);
70 r = hc_conn_set_driver_ssl_client (ssl_conn, conn);
73 hc_conn_close (ssl_conn);
81 client_conn_new (int fd)
87 conn = hc_conn_new (NULL, NULL);
88 r = hc_conn_set_driver_channel (conn, fd);
95 ssl_conn = hc_conn_new (NULL, NULL);
96 hc_conn_set_driver_ssl_server (ssl_conn, conn);
99 hc_conn_close (ssl_conn);
100 hc_conn_close (conn);
103 pop_conn = hc_conn_new (NULL, NULL);
104 r = hc_conn_set_driver_pop (pop_conn, ssl_conn);
107 hc_conn_close (pop_conn);
108 hc_conn_close (ssl_conn);
115 push_other (HCConn *conn, HCEvent event, gpointer data)
122 while ((r = hc_conn_read (conn, buffer, sizeof (buffer))) > 0)
123 hc_conn_write (data, buffer, r);
126 hc_conn_close (conn);
127 hc_conn_close (data);
133 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
137 struct pop_address *address = data;
140 g_critical ("Server has received an error event.");
144 /* FIXME: Should be independent of address type. */
145 g_message ("Received connection from %s.",
146 inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
148 server_conn = server_conn_new (address->server, address->port,
150 if (server_conn == NULL)
154 client_conn = client_conn_new (fd);
155 if (client_conn == NULL)
157 hc_conn_close (server_conn);
161 hc_conn_set_callback (client_conn, push_other, server_conn);
162 hc_conn_set_callback (server_conn, push_other, client_conn);
166 static gchar *configfile;
167 static gboolean foreground;
169 static GOptionEntry opt_entries[] =
171 { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
172 "Configuration file location", "file" },
173 { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground,
174 "Run in foreground", 0 },
178 int main (int argc, char **argv)
181 GOptionContext *opt_ctx;
187 gchar *server_address;
193 struct pop_address pop_address;
195 gnutls_global_init ();
197 configfile = CONFFILE;
198 opt_ctx = g_option_context_new ("");
199 g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
202 if (!g_option_context_parse (opt_ctx, &argc, &argv, &error))
204 g_critical ("Could not parse command line options: %s.",
206 g_error_free (error);
209 g_option_context_free (opt_ctx);
211 keyfile = g_key_file_new ();
214 if (g_key_file_load_from_file (keyfile, configfile,
215 G_KEY_FILE_NONE, &error) == FALSE)
217 g_critical ("Could not load configuration file %s: %s.",
218 configfile, error->message);
219 g_error_free (error);
224 certfile = g_key_file_get_string (keyfile, "global", "certfile",
226 if (certfile == NULL && error != NULL)
228 g_critical ("No certification file specified: %s.",
230 g_error_free (error);
234 ssl_keyfile = g_key_file_get_string (keyfile, "global", "keyfile",
236 if (ssl_keyfile == NULL && error != NULL)
238 ssl_keyfile = g_strdup (certfile);
239 g_error_free (error);
244 conf_address = g_key_file_get_string (keyfile, "global", "address",
246 if (conf_address == NULL && error != NULL)
248 conf_address = g_strdup ("0.0.0.0");
249 g_error_free (error);
252 port = g_key_file_get_string (keyfile, "global", "port", &error);
253 if (port == NULL && error != NULL)
255 port = g_strdup ("110");
256 g_error_free (error);
259 server_address = g_key_file_get_string (keyfile, "global", "server",
261 if (server_address == NULL && error != NULL)
263 server_address = g_strdup ("127.0.0.1");
264 g_error_free (error);
267 server_port = g_key_file_get_string (keyfile, "global", "server_port",
269 if (server_port == NULL && error != NULL)
271 server_port = g_strdup ("995");
272 g_error_free (error);
275 server_ssl = g_key_file_get_boolean (keyfile, "global", "server_ssl",
277 if (server_ssl == 0 && error != NULL)
280 g_error_free (error);
284 policy = g_key_file_get_string (keyfile, "global", "policy",
286 if (policy == NULL && error != NULL)
288 policy = g_strdup ("deny");
289 g_error_free (error);
292 if (!strcmp (policy, "allow"))
293 ACCESS_DEFAULT = ACCESS_ALLOW;
297 pop_address.server = server_address;
298 pop_address.port = server_port;
299 pop_address.ssl = server_ssl;
301 server_fd = hc_tcp_server (port);
304 g_critical ("Could not create server.");
307 hc_server_add_watch (server_fd, new_client, &pop_address);
311 g_message ("Listening at %s:%s.", conf_address, port);
312 if (ACCESS_DEFAULT == ACCESS_ALLOW)
313 g_message ("Authorizing users by default.");
318 g_free (conf_address);
321 hc_conn_ssl_server_init_credentials (certfile, ssl_keyfile);
324 g_free (ssl_keyfile);
326 g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
328 gnutls_global_deinit ();
330 g_free (server_address);
331 g_free (server_port);