X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=common-src%2Fbsd-security.c;h=ad2929219e93031d2b060aa15cae0391edfadacb;hb=011a59f5a54864108a16af570a6b287410597cc2;hp=8953f1f9308e72bf3a951bda549a45258cef2f46;hpb=94a044f90357edefa6f4ae9f0b1d5885b0e34aee;p=debian%2Famanda diff --git a/common-src/bsd-security.c b/common-src/bsd-security.c index 8953f1f..ad29292 100644 --- a/common-src/bsd-security.c +++ b/common-src/bsd-security.c @@ -37,8 +37,8 @@ #include "packet.h" #include "security.h" #include "security-util.h" +#include "sockaddr-util.h" #include "stream.h" -#include "version.h" #ifndef SO_RCVBUF #undef DUMPER_SOCKET_BUFFERING @@ -73,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, @@ -123,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; @@ -132,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); @@ -155,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) { @@ -268,16 +270,28 @@ 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); g_snprintf(handle, 14, "000-%08x", (unsigned)newhandle++); if (udp_inithandle(bh->udp, bh, canonname, - (struct sockaddr_storage *)res_addr->ai_addr, port, handle, sequence) < 0) { + (sockaddr_union *)res_addr->ai_addr, port, handle, sequence) < 0) { (*fn)(arg, &bh->sech, S_ERROR); amfree(bh->hostname); amfree(bh); @@ -385,9 +399,9 @@ 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(bh->udp->peer.ss_family, &bs->port, + bs->socket = stream_server(SU_GET_FAMILY(&bh->udp->peer), &bs->port, (size_t)STREAM_BUFSIZE, (size_t)STREAM_BUFSIZE, 0); if (bs->socket < 0) { @@ -440,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); @@ -527,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. */ @@ -545,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); } @@ -575,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); } /* @@ -617,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); }