FIX: use ssl connection as lower layer for pop connection, not channel
[cascardo/rnetproxy.git] / popproxy.c
index a10b61b..34a1c90 100644 (file)
 #include "hcconn_ssl.h"
 #include "tcp_connect.h"
 
+#include "usermap.h"
+
 #define CONFFILE SYSCONFDIR "/popproxy.conf"
 
 struct pop_address
 {
   char *server;
   char *port;
+  int ssl;
 };
 
 static HCConn *
-server_conn_new (char *server, char *port)
+server_conn_new (char *server, char *port, int ssl)
 {
   int fd;
   HCConn *conn;
@@ -54,15 +57,16 @@ server_conn_new (char *server, char *port)
       return NULL;
     }
   conn = hc_conn_new (NULL, NULL);
-  ssl_conn = hc_conn_new (NULL, NULL);
   r = hc_conn_set_driver_channel (conn, fd);
   if (r != 0)
     {
-      hc_conn_close (ssl_conn);
       hc_conn_close (conn);
       close (fd);
       return NULL;
     }
+  if (!ssl)
+    return conn;
+  ssl_conn = hc_conn_new (NULL, NULL);
   r = hc_conn_set_driver_ssl_client (ssl_conn, conn);
   if (r != 0)
     {
@@ -97,7 +101,7 @@ client_conn_new (int fd)
       return NULL;
     }
   pop_conn = hc_conn_new (NULL, NULL);
-  r = hc_conn_set_driver_pop (pop_conn, conn);
+  r = hc_conn_set_driver_pop (pop_conn, ssl_conn);
   if (r != 0)
     {
       hc_conn_close (pop_conn);
@@ -141,7 +145,8 @@ new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
   g_message ("Received connection from %s.",
              inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
 
-  server_conn = server_conn_new (address->server, address->port);
+  server_conn = server_conn_new (address->server, address->port,
+                                 address->ssl);
   if (server_conn == NULL)
     {
       return;
@@ -181,8 +186,10 @@ int main (int argc, char **argv)
   gchar *port;
   gchar *server_address;
   gchar *server_port;
+  int server_ssl;
   gchar *certfile;
   gchar *ssl_keyfile;
+  gchar *policy;
   struct pop_address pop_address;
 
   gnutls_global_init ();
@@ -264,9 +271,32 @@ int main (int argc, char **argv)
       server_port = g_strdup ("995");
       g_error_free (error);
     }
+  error = NULL;
+  server_ssl = g_key_file_get_boolean (keyfile, "global", "server_ssl",
+                                       &error);
+  if (server_ssl == 0 && error != NULL)
+    {
+      server_ssl = 0;
+      g_error_free (error);
+    }
+
+  error = NULL;
+  policy = g_key_file_get_string (keyfile, "global", "policy",
+                                  &error);
+  if (policy == NULL && error != NULL)
+    {
+      policy = g_strdup ("deny");
+      g_error_free (error);
+    }
+
+  if (!strcmp (policy, "allow"))
+    ACCESS_DEFAULT = ACCESS_ALLOW;
+  g_free (policy);
+
 
   pop_address.server = server_address;
   pop_address.port = server_port;
+  pop_address.ssl = server_ssl;
 
   server_fd = hc_tcp_server (port);
   if (server_fd < 0)
@@ -279,6 +309,8 @@ int main (int argc, char **argv)
   pop_log_init ();
 
   g_message ("Listening at %s:%s.", conf_address, port);
+  if (ACCESS_DEFAULT == ACCESS_ALLOW)
+    g_message ("Authorizing users by default.");
 
   if (!foreground)
     daemon (0, 0);