23b17bad3a98f469146c3a88afdec17f4c651b20
[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 <gnet.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <gnutls/gnutls.h>
27 #include "log.h"
28 #include "nethook.h"
29 #include "null.h"
30 #include "ssl.h"
31 #include "pop.h"
32
33 #define CONFFILE SYSCONFDIR "/popproxy.conf"
34
35 void new_client (GServer* server, GConn* conn, gpointer data)
36 {
37   net_hook_t* hook;
38   if (conn == NULL)
39     {
40       g_critical ("Server has received an error event.");
41       return;
42     }
43   g_message ("Received connection from %s.", conn->hostname);
44   hook = ssl_hook_new (conn, data);
45   pop_hook_new (hook);
46   gnet_conn_read (conn);
47 }
48
49 static gchar* configfile;
50
51 static GOptionEntry opt_entries[] =
52   {
53     { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
54       "Configuration file location", "file" },
55     { NULL }
56   };
57
58 int main (int argc, char** argv)
59 {
60
61   GOptionContext* opt_ctx;
62   GKeyFile *keyfile;
63   GInetAddr* inetaddr;
64   GError *error;
65   gchar* conf_address;
66   gint port;
67   gchar *server_address;
68
69   gnutls_global_init ();
70   gnet_init ();
71   pop_log_init ();
72
73   configfile = CONFFILE;
74   opt_ctx = g_option_context_new ("");
75   g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
76   if (!g_option_context_parse (opt_ctx, &argc, &argv, NULL))
77     {
78       g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL,
79              "Could not parse command line options.");
80     }
81   g_option_context_free (opt_ctx);
82   
83   keyfile =  g_key_file_new ();
84
85   error = NULL;
86   if (g_key_file_load_from_file (keyfile, configfile,
87                                  G_KEY_FILE_NONE, &error) == FALSE)
88     {
89       fprintf (stderr, "Could not load configuration file %s: %s.\n",
90                configfile, error->message);
91       g_error_free (error);
92       exit (1);
93     }
94
95   error = NULL;
96   conf_address = g_key_file_get_string (keyfile, "global", "address",
97                                         &error);
98   if (conf_address == NULL && error != NULL)
99     {
100       conf_address = g_strdup ("0.0.0.0");
101       g_error_free (error);
102     }
103   error = NULL;
104   port = g_key_file_get_integer (keyfile, "global", "port", &error);
105   if (port == 0 && error != NULL)
106     {
107       port = 110;
108       g_error_free (error);
109     }
110   error = NULL;
111   server_address = g_key_file_get_string (keyfile, "global", "server",
112                                           &error);
113   if (server_address == NULL && error != NULL)
114     {
115       server_address = g_strdup ("127.0.0.1");
116       g_error_free (error);
117     }
118
119   inetaddr = gnet_inetaddr_new_nonblock (conf_address, port);
120   if (gnet_server_new (inetaddr, port,
121                        new_client, server_address) == NULL)
122     {
123       fprintf (stderr, "Could not create server.\n");
124       exit (1);
125     }
126
127   g_message ("Listening at %s:%d.", conf_address, port);
128
129   daemon (0, 0);
130
131   g_free (conf_address);
132
133   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
134
135   gnutls_global_deinit ();
136
137   g_free (server_address);
138
139   return 0;
140
141 }