7f4f5a37ce02408a59af024121b3c74982d6db3d
[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 "nethook.h"
30 #include "null.h"
31 #include "ssl.h"
32 #include "pop.h"
33
34 #include "hcconn.h"
35 #include "tcp_connect.h"
36
37 #define CONFFILE SYSCONFDIR "/popproxy.conf"
38
39 struct pop_address
40 {
41   char *server;
42   char *port;
43 };
44
45 void new_client (int fd, struct sockaddr* addr, socklen_t saddr, gpointer data)
46 {
47   HCConn *conn;
48   net_hook_t* hook;
49   struct pop_address *address = data;
50   if (fd < 0)
51     {
52       g_critical ("Server has received an error event.");
53       return;
54     }
55   g_message ("Received connection from %s.",
56              inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
57   conn = hc_conn_new (fd, NULL, NULL);
58   hook = ssl_hook_new (conn, address->server, address->port);
59   pop_hook_new (hook);
60 }
61
62 static gchar* configfile;
63
64 static GOptionEntry opt_entries[] =
65   {
66     { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
67       "Configuration file location", "file" },
68     { NULL }
69   };
70
71 int main (int argc, char** argv)
72 {
73
74   GOptionContext* opt_ctx;
75   GKeyFile *keyfile;
76   GError *error;
77   int server_fd;
78   gchar* conf_address;
79   gchar* port;
80   gchar *server_address;
81   gchar *server_port;
82   struct pop_address pop_address;
83
84   gnutls_global_init ();
85   pop_log_init ();
86
87   configfile = CONFFILE;
88   opt_ctx = g_option_context_new ("");
89   g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
90   if (!g_option_context_parse (opt_ctx, &argc, &argv, NULL))
91     {
92       g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL,
93              "Could not parse command line options.");
94     }
95   g_option_context_free (opt_ctx);
96   
97   keyfile =  g_key_file_new ();
98
99   error = NULL;
100   if (g_key_file_load_from_file (keyfile, configfile,
101                                  G_KEY_FILE_NONE, &error) == FALSE)
102     {
103       fprintf (stderr, "Could not load configuration file %s: %s.\n",
104                configfile, error->message);
105       g_error_free (error);
106       exit (1);
107     }
108
109   error = NULL;
110   conf_address = g_key_file_get_string (keyfile, "global", "address",
111                                         &error);
112   if (conf_address == NULL && error != NULL)
113     {
114       conf_address = g_strdup ("0.0.0.0");
115       g_error_free (error);
116     }
117   error = NULL;
118   port = g_key_file_get_string (keyfile, "global", "port", &error);
119   if (port == NULL && error != NULL)
120     {
121       port = g_strdup ("110");
122       g_error_free (error);
123     }
124   error = NULL;
125   server_address = g_key_file_get_string (keyfile, "global", "server",
126                                           &error);
127   if (server_address == NULL && error != NULL)
128     {
129       server_address = g_strdup ("127.0.0.1");
130       g_error_free (error);
131     }
132
133   server_port = g_key_file_get_string (keyfile, "global", "server_port",
134                                        &error);
135   if (server_port == NULL && error != NULL)
136     {
137       server_port = g_strdup ("995");
138       g_error_free (error);
139     }
140
141   pop_address.server = server_address;
142   pop_address.port = server_port;
143
144   server_fd = hc_tcp_server (port);
145   if (server_fd < 0)
146     {
147       fprintf (stderr, "Could not create server.\n");
148       exit (1);
149     }
150   hc_server_add_watch (server_fd, new_client, &pop_address);
151
152   g_message ("Listening at %s:%s.", conf_address, port);
153
154   daemon (0, 0);
155
156   g_free (conf_address);
157   g_free (port);
158
159   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
160
161   gnutls_global_deinit ();
162
163   g_free (server_address);
164   g_free (server_port);
165
166   return 0;
167
168 }