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