Merge commit 'upstream/3.1.0'
[debian/amanda] / common-src / bsd-security.c
index 4be79482bb6bc036871bcc539bab6b8c6d133dd2..be023602e9fe1a39ab494dcb4e3b9aef3823c1f0 100644 (file)
@@ -39,7 +39,6 @@
 #include "security-util.h"
 #include "sockaddr-util.h"
 #include "stream.h"
-#include "version.h"
 
 #ifndef SO_RCVBUF
 #undef DUMPER_SOCKET_BUFFERING
@@ -124,7 +123,6 @@ bsd_connect(
     void *             datap)
 {
     struct sec_handle *bh;
-    struct servent *se;
     in_port_t port = 0;
     struct timeval sequence_time;
     int sequence;
@@ -133,6 +131,7 @@ bsd_connect(
     struct addrinfo *res, *res_addr;
     char *canonname;
     int result_bind;
+    char *service;
 
     assert(hostname != NULL);
 
@@ -156,15 +155,16 @@ bsd_connect(
        security_seterror(&bh->sech,
                _("resolve_hostname(%s) did not return a canonical name\n"), hostname);
        (*fn)(arg, &bh->sech, S_ERROR);
-       return;
+       if (res) freeaddrinfo(res);
+       return;
     }
     if (res == NULL) {
        dbprintf(_("resolve_hostname(%s): no results\n"), hostname);
        security_seterror(&bh->sech,
                _("resolve_hostname(%s): no results\n"), hostname);
        (*fn)(arg, &bh->sech, S_ERROR);
-       amfree(canonname);
-       return;
+       amfree(canonname);
+       return;
     }
 
     for (res_addr = res; res_addr != NULL; res_addr = res_addr->ai_next) {
@@ -269,10 +269,22 @@ bsd_connect(
        bh->udp = &netfd4;
 
     auth_debug(1, _("Resolved hostname=%s\n"), canonname);
-    if ((se = getservbyname(AMANDA_SERVICE_NAME, "udp")) == NULL)
-       port = AMANDA_SERVICE_DEFAULT;
-    else
-       port = (in_port_t)ntohs(se->s_port);
+
+    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, "udp");
+    if (port == 0) {
+        security_seterror(&bh->sech, _("%s/udp unknown protocol"), service);
+       (*fn)(arg, &bh->sech, S_ERROR);
+        amfree(canonname);
+       return;
+    }
+
     amanda_gettimeofday(&sequence_time);
     sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
     handle=alloc(15);
@@ -528,6 +540,10 @@ bsd_stream_read(
     bs->arg = arg;
 }
 
+/* buffer for bsd_stream_read_sync function */
+static ssize_t  sync_pktlen;
+static void    *sync_pkt;
+
 /*
  * Read a chunk of data to a stream.  Blocks until completion.
  */
@@ -546,11 +562,13 @@ bsd_stream_read_sync(
     if(bs->ev_read != NULL) {
         return -1;
     }
+    sync_pktlen = 0;
+    sync_pkt = NULL;
     bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD,
                        stream_read_sync_callback, bs);
     event_wait(bs->ev_read);
-    *buf = bs->databuf;
-    return (bs->len);
+    *buf = sync_pkt;
+    return (sync_pktlen);
 }
 
 
@@ -576,8 +594,11 @@ stream_read_sync_callback(
        n = read(bs->fd, bs->databuf, sizeof(bs->databuf));
     } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
     if (n < 0)
-        security_stream_seterror(&bs->secstr, strerror(errno));
+        security_stream_seterror(&bs->secstr, "%s", strerror(errno));
     bs->len = n;
+    sync_pktlen = bs->len;
+    sync_pkt = malloc(sync_pktlen);
+    memcpy(sync_pkt, bs->databuf, sync_pktlen);
 }
 
 /*
@@ -618,7 +639,7 @@ stream_read_callback(
     } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
 
     if (n < 0)
-       security_stream_seterror(&bs->secstr, strerror(errno));
+       security_stream_seterror(&bs->secstr, "%s", strerror(errno));
 
     (*bs->fn)(bs->arg, bs->databuf, n);
 }