diff options
Diffstat (limited to 'usr.sbin/rpcbind/rpcbind.c')
-rw-r--r-- | usr.sbin/rpcbind/rpcbind.c | 466 |
1 files changed, 233 insertions, 233 deletions
diff --git a/usr.sbin/rpcbind/rpcbind.c b/usr.sbin/rpcbind/rpcbind.c index 182dbff7dbed..faa24a4415e2 100644 --- a/usr.sbin/rpcbind/rpcbind.c +++ b/usr.sbin/rpcbind/rpcbind.c @@ -111,36 +111,37 @@ static int terminate_wfd; #ifdef WARMSTART /* Local Variable */ -static int warmstart = 0; /* Grab an old copy of registrations. */ +static int warmstart = 0; /* Grab an old copy of registrations */ #endif #ifdef PORTMAP struct pmaplist *list_pml; /* A list of version 2 rpcbind services */ -char *udptrans; /* Name of UDP transport */ -char *tcptrans; /* Name of TCP transport */ -char *udp_uaddr; /* Universal UDP address */ -char *tcp_uaddr; /* Universal TCP address */ +const char *udptrans; /* Name of UDP transport */ +const char *tcptrans; /* Name of TCP transport */ +const char *udp_uaddr; /* Universal UDP address */ +const char *tcp_uaddr; /* Universal TCP address */ #endif -static char servname[] = "rpcbind"; -static char superuser[] = "superuser"; -static char nlname[] = "netlink"; +static const char servname[] = "rpcbind"; +const char rpcbind_superuser[] = "superuser"; +const char rpcbind_unknown[] = "unknown"; +static const char nlname[] = "netlink"; -static struct netconfig netlink_nconf = { - .nc_netid = nlname, +static const struct netconfig netlink_nconf = { + .nc_netid = __UNCONST(nlname), .nc_semantics = NC_TPI_CLTS, }; -static struct t_bind netlink_taddr = { +static const struct t_bind netlink_taddr = { .addr = { .maxlen = sizeof(nlname), .len = sizeof(nlname), - .buf = nlname, + .buf = __UNCONST(nlname), }, }; -static int init_transport(struct netconfig *); -static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *, - struct netbuf *); +static int init_transport(const struct netconfig *); +static void rbllist_add(rpcprog_t, rpcvers_t, const struct netconfig *, + struct netbuf *); static void cleanup_pidfile(void); static void terminate(int); static void parseargs(int, char *[]); @@ -192,7 +193,7 @@ main(int argc, char *argv[]) fprintf(stderr, "Sorry. You are not superuser\n"); exit(1); } - nc_handle = setnetconfig(); /* open netconfig file */ + nc_handle = setnetconfig(); /* open netconfig file */ if (nc_handle == NULL) { syslog(LOG_ERR, "could not read /etc/netconfig"); exit(1); @@ -216,7 +217,7 @@ main(int argc, char *argv[]) while ((nconf = getnetconfig(nc_handle))) { if (nconf->nc_flag & NC_VISIBLE) { - if (ipv6_only == 1 && strcmp(nconf->nc_protofmly, + if (ipv6_only == 1 && strcmp(nconf->nc_protofmly, "inet") == 0) { /* DO NOTHING */ } else @@ -296,7 +297,7 @@ main(int argc, char *argv[]) * Returns 0 if succeeds, else fails */ static int -init_transport(struct netconfig *nconf) +init_transport(const struct netconfig *nconf) { int fd = -1; struct t_bind taddr; @@ -308,8 +309,8 @@ init_transport(struct netconfig *nconf) int addrlen; int nhostsbak; int bound; - struct sockaddr *sa; u_int32_t host_addr[4]; /* IPv4 or IPv6 */ + struct sockaddr *sa; struct sockaddr_un sun; mode_t oldmask; bool local, netlink; @@ -321,17 +322,17 @@ init_transport(struct netconfig *nconf) if ((nconf->nc_semantics != NC_TPI_CLTS) && (nconf->nc_semantics != NC_TPI_COTS) && (nconf->nc_semantics != NC_TPI_COTS_ORD)) - return (1); /* not my type */ -#ifdef ND_DEBUG + return (1); /* not my type */ +#ifdef RPCBIND_DEBUG if (debugging) { - int i; - char **s; - - (void)fprintf(stderr, "%s: %ld lookup routines :\n", - nconf->nc_netid, nconf->nc_nlookups); - for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups; - i++, s++) - fprintf(stderr, "[%d] - %s\n", i, *s); + unsigned int i; + char **s; + + (void)fprintf(stderr, "%s: %ld lookup routines :\n", + nconf->nc_netid, nconf->nc_nlookups); + for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups; + i++, s++) + (void)fprintf(stderr, "[%d] - %s\n", i, *s); } #endif @@ -339,224 +340,223 @@ init_transport(struct netconfig *nconf) * XXX - using RPC library internal functions. */ if (local) { - /* - * For other transports we call this later, for each socket we - * like to bind. - */ - if ((fd = __rpc_nconf2fd(nconf)) < 0) { - int non_fatal = 0; - if (errno == EAFNOSUPPORT) - non_fatal = 1; - syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s", - nconf->nc_netid); - return (1); - } + /* + * For other transports we call this later, for each socket we + * like to bind. + */ + if ((fd = __rpc_nconf2fd(nconf)) < 0) { + syslog(errno == EAFNOSUPPORT ? LOG_DEBUG : LOG_ERR, + "cannot create socket for %s", + nconf->nc_netid); + return (1); + } } if (!__rpc_nconf2sockinfo(nconf, &si)) { - syslog(LOG_ERR, "cannot get information for %s", - nconf->nc_netid); - return (1); + syslog(LOG_ERR, "cannot get information for %s", + nconf->nc_netid); + return (1); } if (local) { - memset(&sun, 0, sizeof sun); - sun.sun_family = AF_LOCAL; - unlink(_PATH_RPCBINDSOCK); - strcpy(sun.sun_path, _PATH_RPCBINDSOCK); - sun.sun_len = SUN_LEN(&sun); - addrlen = sizeof (struct sockaddr_un); - sa = (struct sockaddr *)&sun; + memset(&sun, 0, sizeof sun); + sun.sun_family = AF_LOCAL; + unlink(_PATH_RPCBINDSOCK); + strcpy(sun.sun_path, _PATH_RPCBINDSOCK); + sun.sun_len = SUN_LEN(&sun); + addrlen = sizeof (struct sockaddr_un); + sa = (struct sockaddr *)&sun; } else if (!netlink) { - /* Get rpcbind's address on this transport */ - - memset(&hints, 0, sizeof hints); - hints.ai_flags = AI_PASSIVE; - hints.ai_family = si.si_af; - hints.ai_socktype = si.si_socktype; - hints.ai_protocol = si.si_proto; + /* Get rpcbind's address on this transport */ + memset(&hints, 0, sizeof hints); + hints.ai_flags = AI_PASSIVE; + hints.ai_family = si.si_af; + hints.ai_socktype = si.si_socktype; + hints.ai_protocol = si.si_proto; } if (!local && !netlink) { - /* - * If no hosts were specified, just bind to INADDR_ANY. - * Otherwise make sure 127.0.0.1 is added to the list. - */ - nhostsbak = nhosts + 1; - hosts = realloc(hosts, nhostsbak * sizeof(char *)); - if (nhostsbak == 1) - hosts[0] = "*"; - else { - if (hints.ai_family == AF_INET && nobind_localhost == 0) { - hosts[nhostsbak - 1] = "127.0.0.1"; - } else if (hints.ai_family == AF_INET6 && nobind_localhost == 0) { - hosts[nhostsbak - 1] = "::1"; - } else - return 1; - } - - /* - * Bind to specific IPs if asked to - */ - bound = 0; - while (nhostsbak > 0) { - --nhostsbak; /* - * XXX - using RPC library internal functions. + * If no hosts were specified, just bind to INADDR_ANY. + * Otherwise make sure 127.0.0.1 is added to the list. */ - if ((fd = __rpc_nconf2fd(nconf)) < 0) { - int non_fatal = 0; - if (errno == EAFNOSUPPORT && - nconf->nc_semantics != NC_TPI_CLTS) - non_fatal = 1; - syslog(non_fatal ? LOG_DEBUG : LOG_ERR, - "cannot create socket for %s", nconf->nc_netid); - return (1); + nhostsbak = nhosts + 1; + hosts = realloc(hosts, nhostsbak * sizeof(char *)); + if (nhostsbak == 1) + hosts[0] = "*"; + else { + if (hints.ai_family == AF_INET && + !nobind_localhost) { + hosts[nhostsbak - 1] = "127.0.0.1"; + } else if (hints.ai_family == AF_INET6 && + !nobind_localhost) { + hosts[nhostsbak - 1] = "::1"; + } else + return 1; } - switch (hints.ai_family) { - case AF_INET: - if (inet_pton(AF_INET, hosts[nhostsbak], - host_addr) == 1) { - hints.ai_flags &= AI_NUMERICHOST; - } else { + + /* + * Bind to specific IPs if asked to + */ + bound = 0; + while (nhostsbak > 0) { + --nhostsbak; /* - * Skip if we have an AF_INET6 address. + * XXX - using RPC library internal functions. */ - if (inet_pton(AF_INET6, - hosts[nhostsbak], host_addr) == 1) { - close(fd); - continue; + if ((fd = __rpc_nconf2fd(nconf)) < 0) { + int non_fatal = 0; + if (errno == EAFNOSUPPORT && + nconf->nc_semantics != NC_TPI_CLTS) + non_fatal = 1; + syslog(non_fatal ? LOG_DEBUG : LOG_ERR, + "cannot create socket for %s", nconf->nc_netid); + return (1); } - } - break; - case AF_INET6: - if (inet_pton(AF_INET6, hosts[nhostsbak], - host_addr) == 1) { - hints.ai_flags &= AI_NUMERICHOST; - } else { + switch (hints.ai_family) { + case AF_INET: + if (inet_pton(AF_INET, hosts[nhostsbak], + host_addr) == 1) { + hints.ai_flags &= AI_NUMERICHOST; + } else { + /* + * Skip if we have an AF_INET6 address. + */ + if (inet_pton(AF_INET6, + hosts[nhostsbak], host_addr) == 1) { + close(fd); + continue; + } + } + break; + case AF_INET6: + if (inet_pton(AF_INET6, hosts[nhostsbak], + host_addr) == 1) { + hints.ai_flags &= AI_NUMERICHOST; + } else { + /* + * Skip if we have an AF_INET address. + */ + if (inet_pton(AF_INET, hosts[nhostsbak], + host_addr) == 1) { + close(fd); + continue; + } + } + if (setsockopt(fd, IPPROTO_IPV6, + IPV6_V6ONLY, &on, sizeof on) < 0) { + syslog(LOG_ERR, + "can't set v6-only binding for " + "ipv6 socket: %m"); + continue; + } + break; + default: + break; + } + /* - * Skip if we have an AF_INET address. + * If no hosts were specified, just bind to INADDR_ANY */ - if (inet_pton(AF_INET, hosts[nhostsbak], - host_addr) == 1) { - close(fd); + if (strcmp("*", hosts[nhostsbak]) == 0) + hosts[nhostsbak] = NULL; + if ((aicode = getaddrinfo(hosts[nhostsbak], servname, &hints, + &res)) != 0) { + syslog(LOG_ERR, "cannot get local address for %s: %s", + nconf->nc_netid, gai_strerror(aicode)); continue; } - } - if (setsockopt(fd, IPPROTO_IPV6, - IPV6_V6ONLY, &on, sizeof on) < 0) { - syslog(LOG_ERR, - "can't set v6-only binding for " - "ipv6 socket: %m"); - continue; - } - break; - default: - break; - } + addrlen = res->ai_addrlen; + sa = (struct sockaddr *)res->ai_addr; + oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH); + if (bind(fd, sa, addrlen) != 0) { + syslog(LOG_ERR, "cannot bind %s on %s: %m", + (hosts[nhostsbak] == NULL) ? "*" : + hosts[nhostsbak], nconf->nc_netid); + if (res != NULL) + freeaddrinfo(res); + continue; + } else + bound = 1; + (void)umask(oldmask); + + /* Copy the address */ + taddr.addr.len = taddr.addr.maxlen = addrlen; + taddr.addr.buf = malloc(addrlen); + if (taddr.addr.buf == NULL) { + syslog(LOG_ERR, + "cannot allocate memory for %s address", + nconf->nc_netid); + if (res != NULL) + freeaddrinfo(res); + return 1; + } + memcpy(taddr.addr.buf, sa, addrlen); +#ifdef RPCBIND_DEBUG + if (debugging) { + /* + * for debugging print out our universal + * address + */ + char *uaddr; + struct netbuf nb; + + nb.buf = sa; + nb.len = nb.maxlen = sa->sa_len; + uaddr = taddr2uaddr(nconf, &nb); + (void)fprintf(stderr, + "rpcbind : my address is %s\n", uaddr); + (void)free(uaddr); + } +#endif - /* - * If no hosts were specified, just bind to INADDR_ANY - */ - if (strcmp("*", hosts[nhostsbak]) == 0) - hosts[nhostsbak] = NULL; - if ((aicode = getaddrinfo(hosts[nhostsbak], servname, &hints, - &res)) != 0) { - syslog(LOG_ERR, "cannot get local address for %s: %s", - nconf->nc_netid, gai_strerror(aicode)); - continue; + if (nconf->nc_semantics != NC_TPI_CLTS) + listen(fd, SOMAXCONN); + + my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, + RPC_MAXDATASIZE, RPC_MAXDATASIZE); } - addrlen = res->ai_addrlen; - sa = (struct sockaddr *)res->ai_addr; + } else if (local) { oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH); - if (bind(fd, sa, addrlen) != 0) { - syslog(LOG_ERR, "cannot bind %s on %s: %m", - (hosts[nhostsbak] == NULL) ? "*" : - hosts[nhostsbak], nconf->nc_netid); - if (res != NULL) - freeaddrinfo(res); - continue; - } else - bound = 1; - (void)umask(oldmask); + if (bind(fd, sa, addrlen) < 0) { + syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid); + if (res != NULL) + freeaddrinfo(res); + return 1; + } + (void) umask(oldmask); /* Copy the address */ taddr.addr.len = taddr.addr.maxlen = addrlen; taddr.addr.buf = malloc(addrlen); if (taddr.addr.buf == NULL) { - syslog(LOG_ERR, - "cannot allocate memory for %s address", - nconf->nc_netid); - if (res != NULL) - freeaddrinfo(res); - return 1; + syslog(LOG_ERR, "cannot allocate memory for %s address", + nconf->nc_netid); + if (res != NULL) + freeaddrinfo(res); + return 1; } memcpy(taddr.addr.buf, sa, addrlen); -#ifdef ND_DEBUG +#ifdef RPCBIND_DEBUG if (debugging) { - /* - * for debugging print out our universal - * address - */ - char *uaddr; - struct netbuf nb; - - nb.buf = sa; - nb.len = nb.maxlen = sa->sa_len; - uaddr = taddr2uaddr(nconf, &nb); - (void)fprintf(stderr, - "rpcbind : my address is %s\n", uaddr); - (void)free(uaddr); - } + /* for debugging print out our universal address */ + char *uaddr; + struct netbuf nb; + + nb.buf = sa; + nb.len = nb.maxlen = sa->sa_len; + uaddr = taddr2uaddr(nconf, &nb); + (void)fprintf(stderr, "rpcbind : my address is %s\n", + uaddr); + (void)free(uaddr); + } #endif if (nconf->nc_semantics != NC_TPI_CLTS) - listen(fd, SOMAXCONN); + listen(fd, SOMAXCONN); my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, RPC_MAXDATASIZE, RPC_MAXDATASIZE); - } - } else if (local) { - oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH); - if (bind(fd, sa, addrlen) < 0) { - syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid); - if (res != NULL) - freeaddrinfo(res); - return 1; - } - (void) umask(oldmask); - - /* Copy the address */ - taddr.addr.len = taddr.addr.maxlen = addrlen; - taddr.addr.buf = malloc(addrlen); - if (taddr.addr.buf == NULL) { - syslog(LOG_ERR, "cannot allocate memory for %s address", - nconf->nc_netid); - if (res != NULL) - freeaddrinfo(res); - return 1; - } - memcpy(taddr.addr.buf, sa, addrlen); -#ifdef ND_DEBUG - if (debugging) { - /* for debugging print out our universal address */ - char *uaddr; - struct netbuf nb; - - nb.buf = sa; - nb.len = nb.maxlen = sa->sa_len; - uaddr = taddr2uaddr(nconf, &nb); - (void) fprintf(stderr, "rpcbind : my address is %s\n", - uaddr); - (void) free(uaddr); - } -#endif - - if (nconf->nc_semantics != NC_TPI_CLTS) - listen(fd, SOMAXCONN); - - my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, - RPC_MAXDATASIZE, RPC_MAXDATASIZE); } else { assert(netlink); taddr = netlink_taddr; @@ -581,7 +581,7 @@ init_transport(struct netconfig *nconf) if (!svc_register(my_xprt, PMAPPROG, PMAPVERS, pmap_service, 0)) { syslog(LOG_ERR, "could not register on %s", - nconf->nc_netid); + nconf->nc_netid); goto error; } pml = malloc(sizeof (struct pmaplist)); @@ -597,7 +597,7 @@ init_transport(struct netconfig *nconf) free(pml); pml = NULL; syslog(LOG_ERR, - "cannot have more than one TCP transport"); + "cannot have more than one TCP transport"); goto error; } tcptrans = strdup(nconf->nc_netid); @@ -609,7 +609,7 @@ init_transport(struct netconfig *nconf) } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) { if (udptrans[0]) { syslog(LOG_ERR, - "cannot have more than one UDP transport"); + "cannot have more than one UDP transport"); goto error; } udptrans = strdup(nconf->nc_netid); @@ -626,9 +626,9 @@ init_transport(struct netconfig *nconf) list_pml = pml; /* Add version 3 information */ - pml = malloc(sizeof (struct pmaplist)); + pml = malloc(sizeof(*pml)); if (pml == NULL) { - syslog(LOG_ERR, "no memory!"); + syslog(LOG_ERR, "%m"); exit(1); } pml->pml_map = list_pml->pml_map; @@ -637,9 +637,9 @@ init_transport(struct netconfig *nconf) list_pml = pml; /* Add version 4 information */ - pml = malloc (sizeof (struct pmaplist)); + pml = malloc(sizeof(*pml)); if (pml == NULL) { - syslog(LOG_ERR, "no memory!"); + syslog(LOG_ERR, "%m"); exit(1); } pml->pml_map = list_pml->pml_map; @@ -655,7 +655,7 @@ init_transport(struct netconfig *nconf) /* version 3 registration */ if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) { syslog(LOG_ERR, "could not register %s version 3", - nconf->nc_netid); + nconf->nc_netid); goto error; } rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr); @@ -663,24 +663,24 @@ init_transport(struct netconfig *nconf) /* version 4 registration */ if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) { syslog(LOG_ERR, "could not register %s version 4", - nconf->nc_netid); + nconf->nc_netid); goto error; } rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr); /* decide if bound checking works for this transport */ status = add_bndlist(nconf, &taddr.addr); -#ifdef BIND_DEBUG +#ifdef RPCBIND_DEBUG if (debugging) { if (status < 0) { fprintf(stderr, "Error in finding bind status for %s\n", - nconf->nc_netid); + nconf->nc_netid); } else if (status == 0) { fprintf(stderr, "check binding for %s\n", - nconf->nc_netid); + nconf->nc_netid); } else if (status > 0) { fprintf(stderr, "No check binding for %s\n", - nconf->nc_netid); + nconf->nc_netid); } } #endif @@ -690,15 +690,15 @@ init_transport(struct netconfig *nconf) if (!netlink && nconf->nc_semantics == NC_TPI_CLTS) { status = create_rmtcall_fd(nconf); -#ifdef BIND_DEBUG +#ifdef RPCBIND_DEBUG if (debugging) { if (status < 0) { fprintf(stderr, "Could not create rmtcall fd for %s\n", - nconf->nc_netid); + nconf->nc_netid); } else { fprintf(stderr, "rmtcall fd for %s is %d\n", - nconf->nc_netid, status); + nconf->nc_netid, status); } } #endif @@ -761,13 +761,13 @@ listen_addr(const struct sockaddr *sa) continue; switch (sa->sa_family) { case AF_INET: - if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]), + if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]), sizeof(struct in_addr)) == 0) return (1); break; #ifdef INET6 case AF_INET6: - if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]), + if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]), sizeof(struct in6_addr)) == 0) return (1); break; @@ -780,8 +780,8 @@ listen_addr(const struct sockaddr *sa) } static void -rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf, - struct netbuf *addr) +rbllist_add(rpcprog_t prog, rpcvers_t vers, const struct netconfig *nconf, + struct netbuf *addr) { rpcblist_ptr rbl; @@ -795,7 +795,7 @@ rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf, rbl->rpcb_map.r_vers = vers; rbl->rpcb_map.r_netid = strdup(nconf->nc_netid); rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr); - rbl->rpcb_map.r_owner = strdup(superuser); + rbl->rpcb_map.r_owner = strdup(rpcbind_superuser); rbl->rpcb_next = list_rbl; /* Attach to global list */ list_rbl = rbl; } @@ -928,9 +928,9 @@ void reap(int dummy __unused) { int save_errno = errno; - + while (wait3(NULL, WNOHANG, NULL) > 0) - ; + ; errno = save_errno; } |