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