Exit if not able to read and parse the configuration file.
[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
32 #define CONFFILE SYSCONFDIR "/popproxy.conf"
33
34 void new_client (GServer* server, GConn* conn, gpointer data)
35 {
36   net_hook_t* hook;
37   if (conn == NULL)
38     {
39       g_critical ("Server has received an error event.");
40       return;
41     }
42   g_message ("Received connection from %s.", conn->hostname);
43   hook = ssl_hook_new (conn, data);
44   gnet_conn_read (conn);
45 }
46
47 static gchar* configfile;
48
49 static GOptionEntry opt_entries[] =
50   {
51     { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
52       "Configuration file location", "file" },
53     { NULL }
54   };
55
56 int main (int argc, char** argv)
57 {
58
59   GOptionContext* opt_ctx;
60   GKeyFile *keyfile;
61   GInetAddr* inetaddr;
62   GError *error;
63   gchar* conf_address;
64   gint port;
65   gchar *server_address;
66
67   gnutls_global_init ();
68   gnet_init ();
69   pop_log_init ();
70
71   configfile = CONFFILE;
72   opt_ctx = g_option_context_new ("");
73   g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
74   if (!g_option_context_parse (opt_ctx, &argc, &argv, NULL))
75     {
76       g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL,
77              "Could not parse command line options.");
78     }
79   g_option_context_free (opt_ctx);
80   
81   keyfile =  g_key_file_new ();
82
83   error = NULL;
84   if (g_key_file_load_from_file (keyfile, configfile,
85                                  G_KEY_FILE_NONE, &error) == FALSE)
86     {
87       fprintf (stderr, "Could not load configuration file %s: %s.\n",
88                configfile, error->message);
89       g_error_free (error);
90       exit (1);
91     }
92
93   conf_address = g_key_file_get_string (keyfile, "global", "address", NULL);
94   port = g_key_file_get_integer (keyfile, "global", "port", NULL);
95   server_address = g_key_file_get_string (keyfile, "global", "server", NULL);
96
97   inetaddr = gnet_inetaddr_new_nonblock (conf_address, port);
98   if (gnet_server_new (inetaddr, port,
99                        new_client, server_address) == NULL)
100     {
101       fprintf (stderr, "Could not create server.\n");
102       exit (1);
103     }
104
105   g_message ("Listening at %s:%d.", conf_address, port);
106
107   daemon (0, 0);
108
109   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
110
111   gnutls_global_deinit ();
112
113   return 0;
114
115 }