Always wait for the SSL handshake before connecting to server.
[cascardo/rnetproxy.git] / popproxy.c
1 /*
2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3 ** Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
4 **  
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.
9 **  
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.
14 **  
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.
18 **  
19 */
20
21 #include <glib.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <gnutls/gnutls.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <signal.h>
29 #include "log.h"
30 #include "pop.h"
31
32 #include "hcconn.h"
33 #include "hcconn_ssl.h"
34 #include "tcp_connect.h"
35
36 #include "usermap.h"
37
38 #define CONFFILE SYSCONFDIR "/popproxy.conf"
39
40 struct pop_address
41 {
42   char *server;
43   char *port;
44   int ssl;
45 };
46
47 static HCConn *
48 server_conn_new (char *server, char *port, int ssl)
49 {
50   int fd;
51   HCConn *conn;
52   HCConn *ssl_conn;
53   int r;
54   fd = hc_tcp_connect (server, port);
55   if (fd < 0)
56     {
57       g_warning ("Could not connect to server at %s:%s.", server, port);
58       return NULL;
59     }
60   conn = hc_conn_new (NULL, NULL);
61   r = hc_conn_set_driver_channel (conn, fd);
62   if (r != 0)
63     {
64       hc_conn_close (conn);
65       close (fd);
66       return NULL;
67     }
68   if (!ssl)
69     return conn;
70   ssl_conn = hc_conn_new (NULL, NULL);
71   r = hc_conn_set_driver_ssl_client (ssl_conn, conn);
72   if (r != 0)
73     {
74       hc_conn_close (ssl_conn);
75       hc_conn_close (conn);
76       return NULL;
77     }
78   return ssl_conn;
79 }
80
81 static HCConn *
82 client_conn_new (int fd)
83 {
84   HCConn *conn;
85   HCConn *ssl_conn;
86   HCConn *pop_conn;
87   int r;
88   conn = hc_conn_new (NULL, NULL);
89   r = hc_conn_set_driver_channel (conn, fd);
90   if (r != 0)
91     {
92       hc_conn_close (conn);
93       close (fd);
94       return NULL;
95     }
96   ssl_conn = hc_conn_new (NULL, NULL);
97   hc_conn_set_driver_ssl_server (ssl_conn, conn);
98   if (r != 0)
99     {
100       hc_conn_close (ssl_conn);
101       hc_conn_close (conn);
102       return NULL;
103     }
104   pop_conn = hc_conn_new (NULL, NULL);
105   r = hc_conn_set_driver_pop (pop_conn, ssl_conn);
106   if (r != 0)
107     {
108       hc_conn_close (pop_conn);
109       hc_conn_close (ssl_conn);
110       return NULL;
111     }
112   return pop_conn;
113 }
114
115 static void
116 push_other (HCConn *conn, HCEvent event, gpointer data)
117 {
118   char buffer[4096];
119   int r;
120   switch (event)
121     {
122     case HC_EVENT_READ:
123       while ((r = hc_conn_read (conn, buffer, sizeof (buffer))) > 0)
124         hc_conn_write (data, buffer, r);
125       break;
126     case HC_EVENT_CLOSE:
127       hc_conn_close (conn);
128       hc_conn_close (data);
129       break;
130     }
131 }
132
133 static void
134 ssl_connected (HCConn *client_conn, HCEvent event, gpointer data)
135 {
136   struct pop_address *address = data;
137   HCConn *server_conn;
138   if (event != HC_EVENT_CONNECT)
139     {
140       g_debug ("Did not get connect event when trying to handshake:"
141                " got %d", event);
142       hc_conn_close (client_conn);
143       return;
144     }
145   server_conn = server_conn_new (address->server, address->port,
146                                  address->ssl);
147   if (server_conn == NULL)
148     {
149       g_debug ("Failure to create connection to server.");
150       hc_conn_close (client_conn);
151       return;
152     }
153   hc_conn_set_callback (client_conn, push_other, server_conn);
154   hc_conn_set_callback (server_conn, push_other, client_conn);
155 }
156
157 static void
158 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
159 {
160   HCConn *client_conn;
161   struct pop_address *address = data;
162   if (fd < 0)
163     {
164       g_critical ("Server has received an error event.");
165       return;
166     }
167
168   /* FIXME: Should be independent of address type. */
169   g_message ("Received connection from %s.",
170              inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
171
172   client_conn = client_conn_new (fd);
173   if (client_conn == NULL)
174     {
175       return;
176     }
177
178   hc_conn_set_callback (client_conn, ssl_connected, address);
179
180 }
181
182 static gchar *configfile;
183 static gboolean foreground;
184
185 static GOptionEntry opt_entries[] =
186   {
187     { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
188       "Configuration file location", "file" },
189     { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground,
190       "Run in foreground", 0 },
191     { NULL }
192   };
193
194 int main (int argc, char **argv)
195 {
196
197   GOptionContext *opt_ctx;
198   GKeyFile *keyfile;
199   GError *error;
200   int server_fd;
201   gchar *conf_address;
202   gchar *port;
203   gchar *server_address;
204   gchar *server_port;
205   int server_ssl;
206   gchar *certfile;
207   gchar *ssl_keyfile;
208   gchar *policy;
209   struct pop_address pop_address;
210
211   signal (SIGPIPE, SIG_IGN);
212
213   gnutls_global_init ();
214
215   configfile = CONFFILE;
216   opt_ctx = g_option_context_new ("");
217   g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
218
219   error = NULL;
220   if (!g_option_context_parse (opt_ctx, &argc, &argv, &error))
221     {
222       g_critical ("Could not parse command line options: %s.",
223                   error->message);
224       g_error_free (error);
225       exit (1);
226     }
227   g_option_context_free (opt_ctx);
228   
229   keyfile = g_key_file_new ();
230
231   error = NULL;
232   if (g_key_file_load_from_file (keyfile, configfile,
233                                  G_KEY_FILE_NONE, &error) == FALSE)
234     {
235       g_critical ("Could not load configuration file %s: %s.",
236                   configfile, error->message);
237       g_error_free (error);
238       exit (1);
239     }
240
241   error = NULL;
242   certfile = g_key_file_get_string (keyfile, "global", "certfile",
243                                     &error);
244   if (certfile == NULL && error != NULL)
245     {
246       g_critical ("No certification file specified: %s.",
247                   error->message);
248       g_error_free (error);
249       exit (1);
250     }
251   error = NULL;
252   ssl_keyfile = g_key_file_get_string (keyfile, "global", "keyfile",
253                                        &error);
254   if (ssl_keyfile == NULL && error != NULL)
255     {
256       ssl_keyfile = g_strdup (certfile);
257       g_error_free (error);
258     }
259
260
261   error = NULL;
262   conf_address = g_key_file_get_string (keyfile, "global", "address",
263                                         &error);
264   if (conf_address == NULL && error != NULL)
265     {
266       conf_address = g_strdup ("0.0.0.0");
267       g_error_free (error);
268     }
269   error = NULL;
270   port = g_key_file_get_string (keyfile, "global", "port", &error);
271   if (port == NULL && error != NULL)
272     {
273       port = g_strdup ("110");
274       g_error_free (error);
275     }
276   error = NULL;
277   server_address = g_key_file_get_string (keyfile, "global", "server",
278                                           &error);
279   if (server_address == NULL && error != NULL)
280     {
281       server_address = g_strdup ("127.0.0.1");
282       g_error_free (error);
283     }
284   error = NULL;
285   server_port = g_key_file_get_string (keyfile, "global", "server_port",
286                                        &error);
287   if (server_port == NULL && error != NULL)
288     {
289       server_port = g_strdup ("995");
290       g_error_free (error);
291     }
292   error = NULL;
293   server_ssl = g_key_file_get_boolean (keyfile, "global", "server_ssl",
294                                        &error);
295   if (server_ssl == 0 && error != NULL)
296     {
297       server_ssl = 0;
298       g_error_free (error);
299     }
300
301   error = NULL;
302   policy = g_key_file_get_string (keyfile, "global", "policy",
303                                   &error);
304   if (policy == NULL && error != NULL)
305     {
306       policy = g_strdup ("deny");
307       g_error_free (error);
308     }
309
310   if (!strcmp (policy, "allow"))
311     ACCESS_DEFAULT = ACCESS_ALLOW;
312   g_free (policy);
313
314
315   pop_address.server = server_address;
316   pop_address.port = server_port;
317   pop_address.ssl = server_ssl;
318
319   server_fd = hc_tcp_server (port);
320   if (server_fd < 0)
321     {
322       g_critical ("Could not create server.");
323       exit (1);
324     }
325   hc_server_add_watch (server_fd, new_client, &pop_address);
326
327   pop_log_init ();
328
329   g_message ("Listening at %s:%s.", conf_address, port);
330   if (ACCESS_DEFAULT == ACCESS_ALLOW)
331     g_message ("Authorizing users by default.");
332
333   if (!foreground)
334     daemon (0, 0);
335
336   g_free (conf_address);
337   g_free (port);
338
339   hc_conn_ssl_server_init_credentials (certfile, ssl_keyfile);
340
341   g_free (certfile);
342   g_free (ssl_keyfile);
343
344   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
345
346   gnutls_global_deinit ();
347
348   g_free (server_address);
349   g_free (server_port);
350
351   return 0;
352
353 }