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