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