Rename functions so we can distinguish client and server SSL support.
[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   fd = hc_tcp_connect (server, port);
49   if (fd < 0)
50     {
51       g_warning ("Could not connect to server at %s:%s.", server, port);
52       return NULL;
53     }
54   conn = hc_conn_new (NULL, NULL);
55   ssl_conn = hc_conn_new (NULL, NULL);
56   hc_conn_set_driver_channel (conn, fd);
57   hc_conn_set_driver_ssl_client (ssl_conn, conn);
58   return ssl_conn;
59 }
60
61 static void
62 push_other (HCConn *conn, HCEvent event, gpointer data)
63 {
64   char buffer[4096];
65   int r;
66   switch (event)
67     {
68     case HC_EVENT_READ:
69       while ((r = hc_conn_read (conn, buffer, sizeof (buffer))) > 0)
70         hc_conn_write (data, buffer, r);
71       break;
72     case HC_EVENT_CLOSE:
73       hc_conn_close (conn);
74       hc_conn_close (data);
75       break;
76     }
77 }
78
79 static void
80 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
81 {
82   HCConn *conn;
83   HCConn *pop_conn;
84   HCConn *server_conn;
85   struct pop_address *address = data;
86   if (fd < 0)
87     {
88       g_critical ("Server has received an error event.");
89       return;
90     }
91   g_message ("Received connection from %s.",
92              inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
93
94   server_conn = server_conn_new (address->server, address->port);
95   if (server_conn == NULL)
96     {
97       return;
98     }
99
100   conn = hc_conn_new (NULL, NULL);
101   hc_conn_set_driver_channel (conn, fd);
102   pop_conn = hc_conn_new (NULL, NULL);
103   hc_conn_set_driver_pop (pop_conn, conn);
104
105   hc_conn_set_callback (pop_conn, push_other, server_conn);
106   hc_conn_set_callback (server_conn, push_other, pop_conn);
107
108 }
109
110 static gchar *configfile;
111 static gboolean foreground;
112
113 static GOptionEntry opt_entries[] =
114   {
115     { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
116       "Configuration file location", "file" },
117     { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground,
118       "Run in foreground", 0 },
119     { NULL }
120   };
121
122 int main (int argc, char **argv)
123 {
124
125   GOptionContext *opt_ctx;
126   GKeyFile *keyfile;
127   GError *error;
128   int server_fd;
129   gchar *conf_address;
130   gchar *port;
131   gchar *server_address;
132   gchar *server_port;
133   struct pop_address pop_address;
134
135   gnutls_global_init ();
136
137   configfile = CONFFILE;
138   opt_ctx = g_option_context_new ("");
139   g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
140
141   error = NULL;
142   if (!g_option_context_parse (opt_ctx, &argc, &argv, &error))
143     {
144       g_critical ("Could not parse command line options: %s.",
145                   error->message);
146       g_error_free (error);
147       exit (1);
148     }
149   g_option_context_free (opt_ctx);
150   
151   keyfile = g_key_file_new ();
152
153   error = NULL;
154   if (g_key_file_load_from_file (keyfile, configfile,
155                                  G_KEY_FILE_NONE, &error) == FALSE)
156     {
157       g_critical ("Could not load configuration file %s: %s.",
158                   configfile, error->message);
159       g_error_free (error);
160       exit (1);
161     }
162
163   error = NULL;
164   conf_address = g_key_file_get_string (keyfile, "global", "address",
165                                         &error);
166   if (conf_address == NULL && error != NULL)
167     {
168       conf_address = g_strdup ("0.0.0.0");
169       g_error_free (error);
170     }
171   error = NULL;
172   port = g_key_file_get_string (keyfile, "global", "port", &error);
173   if (port == NULL && error != NULL)
174     {
175       port = g_strdup ("110");
176       g_error_free (error);
177     }
178   error = NULL;
179   server_address = g_key_file_get_string (keyfile, "global", "server",
180                                           &error);
181   if (server_address == NULL && error != NULL)
182     {
183       server_address = g_strdup ("127.0.0.1");
184       g_error_free (error);
185     }
186   error = NULL;
187   server_port = g_key_file_get_string (keyfile, "global", "server_port",
188                                        &error);
189   if (server_port == NULL && error != NULL)
190     {
191       server_port = g_strdup ("995");
192       g_error_free (error);
193     }
194
195   pop_address.server = server_address;
196   pop_address.port = server_port;
197
198   server_fd = hc_tcp_server (port);
199   if (server_fd < 0)
200     {
201       g_critical ("Could not create server.");
202       exit (1);
203     }
204   hc_server_add_watch (server_fd, new_client, &pop_address);
205
206   pop_log_init ();
207
208   g_message ("Listening at %s:%s.", conf_address, port);
209
210   if (!foreground)
211     daemon (0, 0);
212
213   g_free (conf_address);
214   g_free (port);
215
216   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
217
218   gnutls_global_deinit ();
219
220   g_free (server_address);
221   g_free (server_port);
222
223   return 0;
224
225 }