X-Git-Url: https://git.decadent.org.uk/gitweb/?p=nfs-utils.git;a=blobdiff_plain;f=utils%2Fstatd%2Fsm-notify.c;h=a3290aa82a84cf5d517212a7c2fa90b2dbb9bc11;hp=e49c72259e1b9a64231224d90c8e9dc24fb34f1f;hb=10e9c07a18d7c8635def61ea19adbc47f2934853;hpb=e132bb5d5ef85420bb188bceecea361d30cb2bfe diff --git a/utils/statd/sm-notify.c b/utils/statd/sm-notify.c index e49c722..a3290aa 100644 --- a/utils/statd/sm-notify.c +++ b/utils/statd/sm-notify.c @@ -29,16 +29,25 @@ #include #include +#include "sockaddr.h" #include "xlog.h" #include "nsm.h" #include "nfsrpc.h" +/* glibc before 2.3.4 */ +#ifndef AI_NUMERICSERV +#define AI_NUMERICSERV 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; + const char * mon_name; + const char * my_name; + char * notify_arg; struct addrinfo *ai; time_t last_used; time_t send_next; @@ -47,15 +56,16 @@ struct nsm_host { uint32_t xid; }; -static char nsm_hostname[256]; +static char nsm_hostname[SM_MAXSTRLEN + 1]; static int nsm_state; +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(void); +static void notify(const int sock); static int notify_host(int, struct nsm_host *); static void recv_reply(int); static void insert_host(struct nsm_host *); @@ -64,19 +74,19 @@ 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 - .ai_flags = AI_ADDRCONFIG, -#endif /* HAVE_DECL_AI_ADDRCONFIG */ - .ai_family = AF_INET, - .ai_protocol = IPPROTO_UDP, + struct addrinfo *ai = NULL; + struct addrinfo hint = { + .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; } @@ -84,9 +94,105 @@ static struct addrinfo *smn_lookup(const char *name) return ai; } +#ifdef HAVE_GETNAMEINFO +static char * +smn_get_hostname(const struct sockaddr *sap, const socklen_t salen, + const char *name) +{ + char buf[NI_MAXHOST]; + int error; + + error = getnameinfo(sap, salen, buf, sizeof(buf), NULL, 0, NI_NAMEREQD); + if (error != 0) { + xlog(L_ERROR, "my_name '%s' is unusable: %s", + name, gai_strerror(error)); + return NULL; + } + return strdup(buf); +} +#else /* !HAVE_GETNAMEINFO */ +static char * +smn_get_hostname(const struct sockaddr *sap, + __attribute__ ((unused)) const socklen_t salen, + const char *name) +{ + const struct sockaddr_in *sin = (const struct sockaddr_in *)(char *)sap; + const struct in_addr *addr = &sin->sin_addr; + struct hostent *hp; + + if (sap->sa_family != AF_INET) { + xlog(L_ERROR, "my_name '%s' is unusable: Bad address family", + name); + return NULL; + } + + hp = gethostbyaddr(addr, (socklen_t)sizeof(addr), AF_INET); + if (hp == NULL) { + xlog(L_ERROR, "my_name '%s' is unusable: %s", + name, hstrerror(h_errno)); + return NULL; + } + return strdup(hp->h_name); +} +#endif /* !HAVE_GETNAMEINFO */ + +/* + * Presentation addresses are converted to their canonical hostnames. + * If the IP address does not map to a hostname, it is an error: + * we never send a presentation address as the argument of SM_NOTIFY. + * + * If "name" is not a presentation address, it is left alone. This + * allows the administrator some flexibility if DNS isn't configured + * exactly how sm-notify prefers it. + * + * Returns NUL-terminated C string containing the result, or NULL + * if the canonical name doesn't exist or cannot be determined. + * The caller must free the result with free(3). + */ +__attribute_malloc__ +static char * +smn_verify_my_name(const char *name) +{ + struct addrinfo *ai = NULL; + struct addrinfo hint = { +#ifdef IPV6_SUPPORTED + .ai_family = AF_UNSPEC, +#else /* !IPV6_SUPPORTED */ + .ai_family = AF_INET, +#endif /* !IPV6_SUPPORTED */ + .ai_flags = AI_NUMERICHOST, + }; + char *retval; + int error; + + error = getaddrinfo(name, NULL, &hint, &ai); + switch (error) { + case 0: + /* @name was a presentation address */ + retval = smn_get_hostname(ai->ai_addr, ai->ai_addrlen, name); + freeaddrinfo(ai); + if (retval == NULL) + return NULL; + break; + case EAI_NONAME: + /* @name was not a presentation address */ + retval = strdup(name); + break; + default: + xlog(L_ERROR, "my_name '%s' is unusable: %s", + name, gai_strerror(error)); + return NULL; + } + + xlog(D_GENERAL, "Canonical name for my_name '%s': %s", + name, retval); + return retval; +} + __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; @@ -94,8 +200,24 @@ smn_alloc_host(const char *hostname, const time_t timestamp) if (host == NULL) goto out_nomem; + /* + * mon_name and my_name are preserved so sm-notify can + * find the right monitor record to remove when it is + * done processing this host. + */ host->name = strdup(hostname); - if (host->name == NULL) { + host->mon_name = (const char *)strdup(mon_name); + host->my_name = (const char *)strdup(my_name); + host->notify_arg = strdup(opt_srcaddr != NULL ? + nsm_hostname : my_name); + if (host->name == NULL || + host->mon_name == NULL || + host->my_name == NULL || + host->notify_arg == NULL) { + free(host->notify_arg); + free((void *)host->my_name); + free((void *)host->mon_name); + free(host->name); free(host); goto out_nomem; } @@ -113,10 +235,14 @@ out_nomem: static void smn_forget_host(struct nsm_host *host) { - xlog(D_CALL, "Removing %s from notify list", host->name); + xlog(D_CALL, "Removing %s (%s, %s) from notify list", + host->name, host->mon_name, host->my_name); - nsm_delete_notified_host(host->name); + nsm_delete_notified_host(host->name, host->mon_name, host->my_name); + free(host->notify_arg); + free((void *)host->my_name); + free((void *)host->mon_name); free(host->name); if (host->ai) freeaddrinfo(host->ai); @@ -127,25 +253,207 @@ 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; insert_host(host); - xlog(D_GENERAL, "Added host %s to notify list", hostname); return 1; } +#ifdef IPV6_SUPPORTED +static int smn_socket(void) +{ + int sock; + + /* + * Use an AF_INET socket if IPv6 is disabled on the + * local system. + */ + sock = socket(AF_INET6, SOCK_DGRAM, 0); + if (sock == -1) { + if (errno != EAFNOSUPPORT) { + xlog(L_ERROR, "Failed to create RPC socket: %m"); + return -1; + } + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) { + xlog(L_ERROR, "Failed to create RPC socket: %m"); + return -1; + } + } else + nsm_family = AF_INET6; + + if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) { + xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m"); + goto out_close; + } + + /* + * TI-RPC over IPv6 (udp6/tcp6) does not handle IPv4. However, + * since sm-notify open-codes all of its RPC support, it can + * use a single socket and let the local network stack provide + * the correct mapping between address families automatically. + * This is the same thing that is done in the kernel. + */ + if (nsm_family == AF_INET6) { + const int zero = 0; + socklen_t zerolen = (socklen_t)sizeof(zero); + + if (setsockopt(sock, SOL_IPV6, IPV6_V6ONLY, + (char *)&zero, zerolen) == -1) { + xlog(L_ERROR, "setsockopt(3) on RPC socket failed: %m"); + goto out_close; + } + } + + return sock; + +out_close: + (void)close(sock); + return -1; +} +#else /* !IPV6_SUPPORTED */ +static int smn_socket(void) +{ + int sock; + + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock == -1) { + xlog(L_ERROR, "Failed to create RPC socket: %m"); + return -1; + } + + if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) { + xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m"); + (void)close(sock); + return -1; + } + + return sock; +} +#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; + + /* Do not allow "node" and "service" parameters both to be NULL */ + 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 + * + * Returns a bound datagram socket file descriptor, or -1 if + * an error occurs. + */ +static int +smn_create_socket(const char *srcaddr, const char *srcport) +{ + int sock, retry_cnt = 0; + struct addrinfo *ai; + +retry: + sock = smn_socket(); + if (sock == -1) + return -1; + + 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) { + 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; + + 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((int)nfs_get_port(ai->ai_addr), "udp"); + if (se != NULL && retry_cnt < 100) { + retry_cnt++; + freeaddrinfo(ai); + (void)close(sock); + goto retry; + } + } + + freeaddrinfo(ai); + return sock; +} + int main(int argc, char **argv) { - int c; - int force = 0; + int c, sock, force = 0; char * progname; progname = strrchr(argv[0], '/'); @@ -169,7 +477,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; @@ -192,12 +500,14 @@ usage: fprintf(stderr, exit(1); } - xlog_syslog(1); if (opt_debug) { + xlog_syslog(0); xlog_stderr(1); xlog_config(D_ALL, 1); - } else + } else { + xlog_syslog(1); xlog_stderr(0); + } xlog_open(progname); xlog(L_NOTICE, "Version " VERSION " starting"); @@ -210,12 +520,15 @@ usage: fprintf(stderr, } } - if (opt_srcaddr) { - strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1); - } else - if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) { - xlog(L_ERROR, "Failed to obtain name of local host: %m"); - exit(1); + if (opt_srcaddr != NULL) { + char *name; + + name = smn_verify_my_name(opt_srcaddr); + if (name == NULL) + exit(1); + + strncpy(nsm_hostname, name, sizeof(nsm_hostname)); + free(name); } (void)nsm_retire_monitored_hosts(); @@ -242,7 +555,14 @@ usage: fprintf(stderr, close(2); } - notify(); + sock = smn_create_socket(opt_srcaddr, opt_srcport); + if (sock == -1) + exit(1); + + if (!nsm_drop_privileges(-1)) + exit(1); + + notify(sock); if (hosts) { struct nsm_host *hp; @@ -262,68 +582,13 @@ usage: fprintf(stderr, * Notify hosts */ static void -notify(void) +notify(const int sock) { - struct sockaddr_storage address; - struct sockaddr *local_addr = (struct sockaddr *)&address; time_t failtime = 0; - int sock = -1; - int retry_cnt = 0; - - retry: - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) { - xlog(L_ERROR, "Failed to create RPC socket: %m"); - exit(1); - } - fcntl(sock, F_SETFL, O_NONBLOCK); - - memset(&address, 0, sizeof(address)); - local_addr->sa_family = AF_INET; /* Default to IPv4 */ - - /* Bind source IP if provided on command line */ - if (opt_srcaddr) { - struct addrinfo *ai = smn_lookup(opt_srcaddr); - if (!ai) { - xlog(L_ERROR, - "Not a valid hostname or address: \"%s\"", - opt_srcaddr); - exit(1); - } - - /* We know it's IPv4 at this point */ - memcpy(local_addr, ai->ai_addr, ai->ai_addrlen); - - freeaddrinfo(ai); - } - - /* Use source port if provided on the command line, - * otherwise use bindresvport */ - if (opt_srcport) { - nfs_set_port(local_addr, opt_srcport); - if (bind(sock, local_addr, sizeof(struct sockaddr_in)) < 0) { - xlog(L_ERROR, "Failed to bind RPC socket: %m"); - exit(1); - } - } else { - struct servent *se; - struct sockaddr_in *sin = (struct sockaddr_in *)local_addr; - (void) bindresvport(sock, sin); - /* try to avoid known ports */ - se = getservbyport(sin->sin_port, "udp"); - if (se && retry_cnt < 100) { - retry_cnt++; - close(sock); - goto retry; - } - } if (opt_max_retry) failtime = time(NULL) + opt_max_retry; - if (!nsm_drop_privileges(-1)) - exit(1); - while (hosts) { struct pollfd pfd; time_t now = time(NULL); @@ -426,11 +691,30 @@ notify_host(int sock, struct nsm_host *host) host->xid = nsm_xmit_rpcbind(sock, sap, SM_PROG, SM_VERS); else host->xid = nsm_xmit_notify(sock, sap, salen, - SM_PROG, nsm_hostname, nsm_state); - + SM_PROG, host->notify_arg, nsm_state); + return 0; } +static void +smn_defer(struct nsm_host *host) +{ + host->xid = 0; + host->send_next = time(NULL) + NSM_MAX_TIMEOUT; + host->timeout = NSM_MAX_TIMEOUT; + insert_host(host); +} + +static void +smn_schedule(struct nsm_host *host) +{ + host->retries = 0; + host->xid = 0; + host->send_next = time(NULL); + host->timeout = NSM_TIMEOUT; + insert_host(host); +} + /* * Extract the returned port number and set up the SM_NOTIFY call. */ @@ -439,33 +723,37 @@ recv_rpcbind_reply(struct sockaddr *sap, struct nsm_host *host, XDR *xdr) { uint16_t port = nsm_recv_rpcbind(sap->sa_family, xdr); - host->send_next = time(NULL); - host->xid = 0; - if (port == 0) { /* No binding for statd... */ xlog(D_GENERAL, "No statd on host %s", host->name); - host->timeout = NSM_MAX_TIMEOUT; - host->send_next += NSM_MAX_TIMEOUT; + smn_defer(host); } else { + xlog(D_GENERAL, "Processing rpcbind reply for %s (port %u)", + host->name, port); nfs_set_port(sap, port); - if (host->timeout >= NSM_MAX_TIMEOUT / 4) - host->timeout = NSM_MAX_TIMEOUT / 4; + smn_schedule(host); } - - insert_host(host); } /* - * Successful NOTIFY call. Server returns void, so nothing - * we need to do here. + * Successful NOTIFY call. Server returns void. + * + * Try sending another SM_NOTIFY with an unqualified "my_name" + * argument. Reuse the port number. If "my_name" is already + * unqualified, we're done. */ static void recv_notify_reply(struct nsm_host *host) { - xlog(D_GENERAL, "Host %s notified successfully", host->name); + char *dot = strchr(host->notify_arg, '.'); - smn_forget_host(host); + if (dot != NULL) { + *dot = '\0'; + smn_schedule(host); + } else { + xlog(D_GENERAL, "Host %s notified successfully", host->name); + smn_forget_host(host); + } } /* @@ -510,7 +798,7 @@ out: } /* - * Insert host into sorted list + * Insert host into notification list, sorted by next send time */ static void insert_host(struct nsm_host *host) @@ -535,6 +823,7 @@ insert_host(struct nsm_host *host) host->next = *where; *where = host; + xlog(D_GENERAL, "Added host %s to notify list", host->name); } /*