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