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