Imported Upstream version 3.2.0
[debian/amanda] / common-src / bsdtcp-security.c
index 3c2e84b04fdcc42c2f42c20f5bebba2da01c80f8..25a32ac5c1165644b7004276017df2abbd3fb43a 100644 (file)
 #include "util.h"
 #include "event.h"
 #include "packet.h"
-#include "queue.h"
 #include "security.h"
 #include "security-util.h"
 #include "sockaddr-util.h"
 #include "stream.h"
-#include "version.h"
 
 /*
  * Number of seconds bsdtcp has to start up
@@ -68,6 +66,7 @@ const security_driver_t bsdtcp_security_driver = {
     "BSDTCP",
     bsdtcp_connect,
     bsdtcp_accept,
+    sec_get_authenticated_peer_name_hostname,
     sec_close,
     stream_sendpkt,
     stream_recvpkt,
@@ -92,7 +91,7 @@ static int newhandle = 1;
 /*
  * Local functions
  */
-static int runbsdtcp(struct sec_handle *);
+static int runbsdtcp(struct sec_handle *, in_port_t port);
 
 
 /*
@@ -110,6 +109,8 @@ bsdtcp_connect(
     struct sec_handle *rh;
     int result;
     char *canonname;
+    char *service;
+    in_port_t port;
 
     assert(fn != NULL);
     assert(hostname != NULL);
@@ -118,7 +119,7 @@ bsdtcp_connect(
 
     auth_debug(1, _("bsdtcp: bsdtcp_connect: %s\n"), hostname);
 
-    rh = alloc(sizeof(*rh));
+    rh = g_new0(struct sec_handle, 1);
     security_handleinit(&rh->sech, &bsdtcp_security_driver);
     rh->hostname = NULL;
     rh->rs = NULL;
@@ -153,13 +154,26 @@ bsdtcp_connect(
     amfree(rh->hostname);
     rh->hostname = stralloc(rh->rs->rc->hostname);
 
+    if (conf_fn) {
+       service = conf_fn("client_port", datap);
+       if (!service || strlen(service) <= 1)
+           service = "amanda";
+    } else {
+       service = "amanda";
+    }
+    port = find_port_for_service(service, "tcp");
+    if (port == 0) {
+       security_seterror(&rh->sech, _("%s/tcp unknown protocol"), service);
+       goto error;
+    }
+
     /*
      * We need to open a new connection.
      *
      * XXX need to eventually limit number of outgoing connections here.
      */
     if(rh->rc->read == -1) {
-       if (runbsdtcp(rh) < 0)
+       if (runbsdtcp(rh, port) < 0)
            goto error;
        rh->rc->refcnt++;
     }
@@ -197,8 +211,8 @@ bsdtcp_accept(
     void       (*fn)(security_handle_t *, pkt_t *),
     void       *datap)
 {
-    struct sockaddr_storage sin;
-    socklen_t len;
+    sockaddr_union sin;
+    socklen_t_equiv len;
     struct tcp_conn *rc;
     char hostname[NI_MAXHOST];
     int result;
@@ -240,21 +254,17 @@ bsdtcp_accept(
  */
 static int
 runbsdtcp(
-    struct sec_handle *        rh)
+    struct sec_handle *        rh,
+    in_port_t port)
 {
-    struct servent *   sp;
     int                        server_socket;
     in_port_t          my_port;
     struct tcp_conn *  rc = rh->rc;
 
-    if ((sp = getservbyname(AMANDA_SERVICE_NAME, "tcp")) == NULL) {
-       error(_("%s/tcp unknown protocol"), "amanda");
-    }
-
     set_root_privs(1);
 
     server_socket = stream_client_privileged(rc->hostname,
-                                    (in_port_t)(ntohs((in_port_t)sp->s_port)),
+                                    port,
                                     STREAM_BUFSIZE,
                                     STREAM_BUFSIZE,
                                     &my_port,