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