X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=common-src%2Fbsd-security.c;h=ad2929219e93031d2b060aa15cae0391edfadacb;hb=c21061ed466e7ee71cb21dd35010e33f6cf76424;hp=4be79482bb6bc036871bcc539bab6b8c6d133dd2;hpb=fb2bd066c2f8b34addafe48d62550e3033a59431;p=debian%2Famanda diff --git a/common-src/bsd-security.c b/common-src/bsd-security.c index 4be7948..ad29292 100644 --- a/common-src/bsd-security.c +++ b/common-src/bsd-security.c @@ -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 @@ -74,6 +73,7 @@ const security_driver_t bsd_security_driver = { "BSD", bsd_connect, bsd_accept, + sec_get_authenticated_peer_name_hostname, bsd_close, udpbsd_sendpkt, udp_recvpkt, @@ -124,7 +124,6 @@ bsd_connect( void * datap) { struct sec_handle *bh; - struct servent *se; in_port_t port = 0; struct timeval sequence_time; int sequence; @@ -133,13 +132,14 @@ bsd_connect( struct addrinfo *res, *res_addr; char *canonname; int result_bind; + char *service; assert(hostname != NULL); (void)conf_fn; /* Quiet unused parameter warning */ (void)datap; /* Quiet unused parameter warning */ - bh = alloc(SIZEOF(*bh)); + bh = g_new0(struct sec_handle, 1); bh->proto_handle=NULL; security_handleinit(&bh->sech, &bsd_security_driver); @@ -156,15 +156,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 +270,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); @@ -386,7 +399,7 @@ bsd_stream_server( assert(bh != NULL); - bs = alloc(SIZEOF(*bs)); + bs = g_new0(struct sec_stream, 1); security_streaminit(&bs->secstr, &bsd_security_driver); bs->socket = stream_server(SU_GET_FAMILY(&bh->udp->peer), &bs->port, (size_t)STREAM_BUFSIZE, (size_t)STREAM_BUFSIZE, @@ -441,7 +454,7 @@ bsd_stream_client( assert(bh != NULL); - bs = alloc(SIZEOF(*bs)); + bs = g_new0(struct sec_stream, 1); security_streaminit(&bs->secstr, &bsd_security_driver); bs->fd = stream_client(bh->hostname, (in_port_t)id, STREAM_BUFSIZE, STREAM_BUFSIZE, &bs->port, 0); @@ -528,6 +541,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 +563,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 +595,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 +640,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); }