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