X-Git-Url: https://git.decadent.org.uk/gitweb/?p=nfs-utils.git;a=blobdiff_plain;f=utils%2Fstatd%2Fsm-notify.c;h=70d94a800f4c44c8b2b686a7849c4a3bd7cba3d8;hp=0ba817a431a225cab45ab083d62688780b449af5;hb=d56192a1e4ddb962f961721b1c5d094696b2d206;hpb=f867499c8521a957776d52a9657b15fa7c2a513f diff --git a/utils/statd/sm-notify.c b/utils/statd/sm-notify.c index 0ba817a..70d94a8 100644 --- a/utils/statd/sm-notify.c +++ b/utils/statd/sm-notify.c @@ -29,16 +29,23 @@ #include #include +#include "sockaddr.h" #include "xlog.h" #include "nsm.h" #include "nfsrpc.h" +#ifndef HAVE_DECL_AI_ADDRCONFIG +#define AI_ADDRCONFIG 0 +#endif + #define NSM_TIMEOUT 2 #define NSM_MAX_TIMEOUT 120 /* don't make this too big */ struct nsm_host { struct nsm_host * next; char * name; + char * mon_name; + char * my_name; struct addrinfo *ai; time_t last_used; time_t send_next; @@ -53,8 +60,8 @@ static int nsm_family = AF_INET; static int opt_debug = 0; static _Bool opt_update_state = true; static unsigned int opt_max_retry = 15 * 60; -static char * opt_srcaddr = 0; -static uint16_t opt_srcport = 0; +static char * opt_srcaddr = NULL; +static char * opt_srcport = NULL; static void notify(const int sock); static int notify_host(int, struct nsm_host *); @@ -65,19 +72,20 @@ static int record_pid(void); static struct nsm_host * hosts = NULL; -static struct addrinfo *smn_lookup(const char *name) +__attribute_malloc__ +static struct addrinfo * +smn_lookup(const char *name) { - struct addrinfo *ai, hint = { -#if HAVE_DECL_AI_ADDRCONFIG + struct addrinfo *ai = NULL; + struct addrinfo hint = { .ai_flags = AI_ADDRCONFIG, -#endif /* HAVE_DECL_AI_ADDRCONFIG */ - .ai_family = AF_INET, - .ai_protocol = IPPROTO_UDP, + .ai_family = (nsm_family == AF_INET ? AF_INET: AF_UNSPEC), + .ai_protocol = (int)IPPROTO_UDP, }; int error; error = getaddrinfo(name, NULL, &hint, &ai); - if (error) { + if (error != 0) { xlog(D_GENERAL, "getaddrinfo(3): %s", gai_strerror(error)); return NULL; } @@ -87,7 +95,8 @@ static struct addrinfo *smn_lookup(const char *name) __attribute_malloc__ static struct nsm_host * -smn_alloc_host(const char *hostname, const time_t timestamp) +smn_alloc_host(const char *hostname, const char *mon_name, + const char *my_name, const time_t timestamp) { struct nsm_host *host; @@ -96,7 +105,14 @@ smn_alloc_host(const char *hostname, const time_t timestamp) goto out_nomem; host->name = strdup(hostname); - if (host->name == NULL) { + host->mon_name = strdup(mon_name); + host->my_name = strdup(my_name); + if (host->name == NULL || + host->mon_name == NULL || + host->my_name == NULL) { + free(host->my_name); + free(host->mon_name); + free(host->name); free(host); goto out_nomem; } @@ -118,6 +134,8 @@ static void smn_forget_host(struct nsm_host *host) nsm_delete_notified_host(host->name); + free(host->my_name); + free(host->mon_name); free(host->name); if (host->ai) freeaddrinfo(host->ai); @@ -128,12 +146,12 @@ static void smn_forget_host(struct nsm_host *host) static unsigned int smn_get_host(const char *hostname, __attribute__ ((unused)) const struct sockaddr *sap, - __attribute__ ((unused)) const struct mon *m, - const time_t timestamp) + const struct mon *m, const time_t timestamp) { struct nsm_host *host; - host = smn_alloc_host(hostname, timestamp); + host = smn_alloc_host(hostname, + m->mon_id.mon_name, m->mon_id.my_id.my_name, timestamp); if (host == NULL) return 0; @@ -215,6 +233,59 @@ static int smn_socket(void) } #endif /* !IPV6_SUPPORTED */ +/* + * If admin specified a source address or srcport, then convert those + * to a sockaddr and return it. Otherwise, return an ANYADDR address. + */ +__attribute_malloc__ +static struct addrinfo * +smn_bind_address(const char *srcaddr, const char *srcport) +{ + struct addrinfo *ai = NULL; + struct addrinfo hint = { + .ai_flags = AI_NUMERICSERV, + .ai_family = nsm_family, + .ai_protocol = (int)IPPROTO_UDP, + }; + int error; + + if (srcaddr == NULL) + hint.ai_flags |= AI_PASSIVE; + + if (srcport == NULL) + error = getaddrinfo(srcaddr, "", &hint, &ai); + else + error = getaddrinfo(srcaddr, srcport, &hint, &ai); + if (error != 0) { + xlog(L_ERROR, + "Invalid bind address or port for RPC socket: %s", + gai_strerror(error)); + return NULL; + } + + return ai; +} + +#ifdef HAVE_LIBTIRPC +static int +smn_bindresvport(int sock, struct sockaddr *sap) +{ + return bindresvport_sa(sock, sap); +} + +#else /* !HAVE_LIBTIRPC */ +static int +smn_bindresvport(int sock, struct sockaddr *sap) +{ + if (sap->sa_family != AF_INET) { + errno = EAFNOSUPPORT; + return -1; + } + + return bindresvport(sock, (struct sockaddr_in *)(char *)sap); +} +#endif /* !HAVE_LIBTIRPC */ + /* * Prepare a socket for sending RPC requests * @@ -222,59 +293,53 @@ static int smn_socket(void) * an error occurs. */ static int -smn_create_socket(const char *srcaddr, const uint16_t srcport) +smn_create_socket(const char *srcaddr, const char *srcport) { - struct sockaddr_storage address; - struct sockaddr *local_addr = (struct sockaddr *)&address; int sock, retry_cnt = 0; + struct addrinfo *ai; retry: sock = smn_socket(); if (sock == -1) return -1; - memset(&address, 0, sizeof(address)); - local_addr->sa_family = AF_INET; /* Default to IPv4 */ - - /* Bind source IP if provided on command line */ - if (srcaddr) { - struct addrinfo *ai = smn_lookup(srcaddr); - if (!ai) { - xlog(L_ERROR, - "Not a valid hostname or address: \"%s\"", - srcaddr); - (void)close(sock); - return -1; - } - - /* We know it's IPv4 at this point */ - memcpy(local_addr, ai->ai_addr, ai->ai_addrlen); - - freeaddrinfo(ai); + ai = smn_bind_address(srcaddr, srcport); + if (ai == NULL) { + (void)close(sock); + return -1; } /* Use source port if provided on the command line, * otherwise use bindresvport */ if (srcport) { - nfs_set_port(local_addr, srcport); - if (bind(sock, local_addr, sizeof(struct sockaddr_in)) < 0) { + if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) { xlog(L_ERROR, "Failed to bind RPC socket: %m"); + freeaddrinfo(ai); (void)close(sock); return -1; } } else { struct servent *se; - struct sockaddr_in *sin = (struct sockaddr_in *)local_addr; - (void) bindresvport(sock, sin); + + if (smn_bindresvport(sock, ai->ai_addr) == -1) { + xlog(L_ERROR, + "bindresvport on RPC socket failed: %m"); + freeaddrinfo(ai); + (void)close(sock); + return -1; + } + /* try to avoid known ports */ - se = getservbyport(sin->sin_port, "udp"); - if (se && retry_cnt < 100) { + se = getservbyport((int)nfs_get_port(ai->ai_addr), "udp"); + if (se != NULL && retry_cnt < 100) { retry_cnt++; - close(sock); + freeaddrinfo(ai); + (void)close(sock); goto retry; } } + freeaddrinfo(ai); return sock; } @@ -305,7 +370,7 @@ main(int argc, char **argv) opt_update_state = false; break; case 'p': - opt_srcport = atoi(optarg); + opt_srcport = optarg; break; case 'v': opt_srcaddr = optarg;