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