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"
35 #define CONFFILE SYSCONFDIR "/popproxy.conf"
44 server_conn_new (char *server, char *port)
50 fd = hc_tcp_connect (server, port);
53 g_warning ("Could not connect to server at %s:%s.", server, port);
56 conn = hc_conn_new (NULL, NULL);
57 ssl_conn = hc_conn_new (NULL, NULL);
58 r = hc_conn_set_driver_channel (conn, fd);
61 hc_conn_close (ssl_conn);
66 r = hc_conn_set_driver_ssl_client (ssl_conn, conn);
69 hc_conn_close (ssl_conn);
77 client_conn_new (int fd)
83 conn = hc_conn_new (NULL, NULL);
84 r = hc_conn_set_driver_channel (conn, fd);
91 ssl_conn = hc_conn_new (NULL, NULL);
92 hc_conn_set_driver_ssl_server (ssl_conn, conn);
95 hc_conn_close (ssl_conn);
99 pop_conn = hc_conn_new (NULL, NULL);
100 r = hc_conn_set_driver_pop (pop_conn, conn);
103 hc_conn_close (pop_conn);
104 hc_conn_close (ssl_conn);
111 push_other (HCConn *conn, HCEvent event, gpointer data)
118 while ((r = hc_conn_read (conn, buffer, sizeof (buffer))) > 0)
119 hc_conn_write (data, buffer, r);
122 hc_conn_close (conn);
123 hc_conn_close (data);
129 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
133 struct pop_address *address = data;
136 g_critical ("Server has received an error event.");
140 /* FIXME: Should be independent of address type. */
141 g_message ("Received connection from %s.",
142 inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
144 server_conn = server_conn_new (address->server, address->port);
145 if (server_conn == NULL)
149 client_conn = client_conn_new (fd);
150 if (client_conn == NULL)
152 hc_conn_close (server_conn);
156 hc_conn_set_callback (client_conn, push_other, server_conn);
157 hc_conn_set_callback (server_conn, push_other, client_conn);
161 static gchar *configfile;
162 static gboolean foreground;
164 static GOptionEntry opt_entries[] =
166 { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
167 "Configuration file location", "file" },
168 { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground,
169 "Run in foreground", 0 },
173 int main (int argc, char **argv)
176 GOptionContext *opt_ctx;
182 gchar *server_address;
186 struct pop_address pop_address;
188 gnutls_global_init ();
190 configfile = CONFFILE;
191 opt_ctx = g_option_context_new ("");
192 g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
195 if (!g_option_context_parse (opt_ctx, &argc, &argv, &error))
197 g_critical ("Could not parse command line options: %s.",
199 g_error_free (error);
202 g_option_context_free (opt_ctx);
204 keyfile = g_key_file_new ();
207 if (g_key_file_load_from_file (keyfile, configfile,
208 G_KEY_FILE_NONE, &error) == FALSE)
210 g_critical ("Could not load configuration file %s: %s.",
211 configfile, error->message);
212 g_error_free (error);
217 certfile = g_key_file_get_string (keyfile, "global", "certfile",
219 if (certfile == NULL && error != NULL)
221 g_critical ("No certification file specified: %s.",
223 g_error_free (error);
227 ssl_keyfile = g_key_file_get_string (keyfile, "global", "keyfile",
229 if (ssl_keyfile == NULL && error != NULL)
231 ssl_keyfile = g_strdup (certfile);
232 g_error_free (error);
237 conf_address = g_key_file_get_string (keyfile, "global", "address",
239 if (conf_address == NULL && error != NULL)
241 conf_address = g_strdup ("0.0.0.0");
242 g_error_free (error);
245 port = g_key_file_get_string (keyfile, "global", "port", &error);
246 if (port == NULL && error != NULL)
248 port = g_strdup ("110");
249 g_error_free (error);
252 server_address = g_key_file_get_string (keyfile, "global", "server",
254 if (server_address == NULL && error != NULL)
256 server_address = g_strdup ("127.0.0.1");
257 g_error_free (error);
260 server_port = g_key_file_get_string (keyfile, "global", "server_port",
262 if (server_port == NULL && error != NULL)
264 server_port = g_strdup ("995");
265 g_error_free (error);
268 pop_address.server = server_address;
269 pop_address.port = server_port;
271 server_fd = hc_tcp_server (port);
274 g_critical ("Could not create server.");
277 hc_server_add_watch (server_fd, new_client, &pop_address);
281 g_message ("Listening at %s:%s.", conf_address, port);
286 g_free (conf_address);
289 hc_conn_ssl_server_init_credentials (certfile, ssl_keyfile);
292 g_free (ssl_keyfile);
294 g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
296 gnutls_global_deinit ();
298 g_free (server_address);
299 g_free (server_port);