diff options
author | 1999-12-16 21:30:34 +0000 | |
---|---|---|
committer | 1999-12-16 21:30:34 +0000 | |
commit | 01cdefb0b166a1bdd89a4e18f2de1c94e537891f (patch) | |
tree | e887bf2a06f874302eb057b18e3fb0ef0971c546 /lib | |
parent | Bugfix: execvp might fail for many reasons. (diff) | |
download | wireguard-openbsd-01cdefb0b166a1bdd89a4e18f2de1c94e537891f.tar.xz wireguard-openbsd-01cdefb0b166a1bdd89a4e18f2de1c94e537891f.zip |
rresvport_af() and bindresvport_af()
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/net/Makefile.inc | 7 | ||||
-rw-r--r-- | lib/libc/net/rcmd.3 | 27 | ||||
-rw-r--r-- | lib/libc/net/rcmd.c | 34 | ||||
-rw-r--r-- | lib/libc/net/rresvport.c | 114 | ||||
-rw-r--r-- | lib/libc/rpc/Makefile.inc | 5 | ||||
-rw-r--r-- | lib/libc/rpc/bindresvport.3 | 20 | ||||
-rw-r--r-- | lib/libc/rpc/bindresvport.c | 75 |
7 files changed, 209 insertions, 73 deletions
diff --git a/lib/libc/net/Makefile.inc b/lib/libc/net/Makefile.inc index 8b708aae9a1..447a96f1e19 100644 --- a/lib/libc/net/Makefile.inc +++ b/lib/libc/net/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.26 1999/12/11 08:09:11 itojun Exp $ +# $OpenBSD: Makefile.inc,v 1.27 1999/12/16 21:30:34 deraadt Exp $ # net sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net ${LIBCSRCDIR}/net @@ -15,7 +15,7 @@ SRCS+= base64.c freeaddrinfo.c gai_strerror.c getaddrinfo.c gethostnamadr.c \ ipx_addr.c ipx_ntoa.c iso_addr.c linkaddr.c ns_addr.c ns_ntoa.c \ nsap_addr.c \ net_addrcmp.c \ - rcmd.c recv.c res_comp.c res_data.c res_debug.c \ + rcmd.c rresvport.c recv.c res_comp.c res_data.c res_debug.c \ res_init.c res_mkquery.c res_query.c res_random.c res_send.c send.c \ sethostent.c ethers.c rcmdsh.c @@ -64,7 +64,8 @@ MLINKS+=iso_addr.3 iso_ntoa.3 MLINKS+=link_addr.3 link_ntoa.3 MLINKS+=ipx.3 ipx_addr.3 ipx.3 ipx_ntoa.3 MLINKS+=ns.3 ns_addr.3 ns.3 ns_ntoa.3 -MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 +MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 \ + rcmd.3 rresvport_af.3 MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \ resolver.3 res_mkquery.3 resolver.3 res_send.3 resolver.3 res_query.3 \ resolver.3 res_search.3 diff --git a/lib/libc/net/rcmd.3 b/lib/libc/net/rcmd.3 index d086e875bf6..5f6ff984591 100644 --- a/lib/libc/net/rcmd.3 +++ b/lib/libc/net/rcmd.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: rcmd.3,v 1.13 1999/07/05 04:41:00 aaron Exp $ +.\" $OpenBSD: rcmd.3,v 1.14 1999/12/16 21:30:34 deraadt Exp $ .\" .\" Copyright (c) 1983, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -47,6 +47,8 @@ .Ft int .Fn rresvport "int *port" .Ft int +.Fn rresvport_af "int *port" "int af" +.Ft int .Fn iruserok "u_int32_t raddr" "int superuser" "const char *ruser" "const char *luser" .Ft int .Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser" @@ -73,8 +75,10 @@ to run the command via .Xr rsh 1 . The .Fn rresvport -function -returns a descriptor to a socket +and +.Fn rresvport_af +functions +return a descriptor to a socket with an address in the privileged port space. The .Fn iruserok @@ -142,7 +146,9 @@ The protocol is described in detail in .Pp The .Fn rresvport -function is used to obtain a socket with a privileged +and +.Fn rresvport_af +functions are used to obtain a socket with a privileged address bound to it. This socket is suitable for use by .Fn rcmd @@ -150,8 +156,10 @@ and several other functions. Privileged Internet ports are those in the range 0 to 1023. Only the super-user is allowed to bind an address of this sort to a socket. .Fn rresvport -needs to be seeded with a port number; if that port -is not available it will find another. +and +.Fn rresvport_af +need to be seeded with a port number; if that port +is not available these functions will find another. .Pp The .Fn iruserok @@ -200,8 +208,10 @@ It returns \-1 on error and prints a diagnostic message on the standard error. .Pp The .Fn rresvport -function -returns a valid, bound socket descriptor on success. +and +.Fn rresvport_af +functions +return a valid, bound socket descriptor on success. It returns \-1 on error with the global value .Va errno set according to the reason for failure. @@ -214,6 +224,7 @@ is overloaded to mean .Xr rsh 1 , .Xr intro 2 , .Xr bindresvport 3 , +.Xr bindresvport_af 3 , .Xr rcmdsh 3 , .Xr rexec 3 , .Xr rexecd 8 , diff --git a/lib/libc/net/rcmd.c b/lib/libc/net/rcmd.c index c933f5b4475..bd920faadb5 100644 --- a/lib/libc/net/rcmd.c +++ b/lib/libc/net/rcmd.c @@ -34,7 +34,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: rcmd.c,v 1.31 1998/03/19 00:30:05 millert Exp $"; +static char *rcsid = "$OpenBSD: rcmd.c,v 1.32 1999/12/16 21:30:34 deraadt Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/param.h> @@ -253,38 +253,6 @@ bad: return (-1); } -int -rresvport(alport) - int *alport; -{ - struct sockaddr_in sin; - int s; - - bzero(&sin, sizeof sin); - sin.sin_len = sizeof(struct sockaddr_in); - sin.sin_family = AF_INET; - sin.sin_addr.s_addr = INADDR_ANY; - s = socket(AF_INET, SOCK_STREAM, 0); - if (s < 0) - return (-1); - sin.sin_port = htons((in_port_t)*alport); - if (*alport < IPPORT_RESERVED - 1) { - if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) - return (s); - if (errno != EADDRINUSE) { - (void)close(s); - return (-1); - } - } - sin.sin_port = 0; - if (bindresvport(s, &sin) == -1) { - (void)close(s); - return (-1); - } - *alport = (int)ntohs(sin.sin_port); - return (s); -} - int __check_rhosts_file = 1; char *__rcmd_errstr; diff --git a/lib/libc/net/rresvport.c b/lib/libc/net/rresvport.c new file mode 100644 index 00000000000..97a901ae0c8 --- /dev/null +++ b/lib/libc/net/rresvport.c @@ -0,0 +1,114 @@ +/* + * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. + * Copyright (c) 1983, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * This product includes software developed by Theo de Raadt. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: rresvport.c,v 1.1 1999/12/16 21:30:34 deraadt Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/socket.h> +#include <sys/stat.h> + +#include <netinet/in.h> +#include <arpa/inet.h> + +#include <signal.h> +#include <fcntl.h> +#include <netdb.h> +#include <unistd.h> +#include <pwd.h> +#include <errno.h> +#include <stdio.h> +#include <ctype.h> +#include <string.h> +#include <syslog.h> +#include <stdlib.h> +#include <netgroup.h> + +int +rresvport(alport) + int *alport; +{ + return rresvport_af(alport, AF_INET); +} + + +int +rresvport_af(alport, af) + int *alport; + int af; +{ + struct sockaddr_storage ss; + struct sockaddr *sa; + u_int16_t *portp; + int s; + + bzero(&ss, sizeof ss); + sa = (struct sockaddr *)&ss; + + switch (af) { + case AF_INET: + sa->sa_len = sizeof(struct sockaddr_in); + portp = &((struct sockaddr_in *)&sa)->sin_port; + case AF_INET6: + sa->sa_len = sizeof(struct sockaddr_in6); + portp = &((struct sockaddr_in6 *)&sa)->sin6_port; + default: + return (-1); + } + sa->sa_family = af; + + s = socket(af, SOCK_STREAM, 0); + if (s < 0) + return (-1); + + *portp = htons(*alport); + if (*alport < IPPORT_RESERVED - 1) { + if (bind(s, sa, sa->sa_len) >= 0) + return (s); + if (errno != EADDRINUSE) { + (void)close(s); + return (-1); + } + } + + *portp = 0; + if (bindresvport_af(s, sa, af) == -1) { + (void)close(s); + return (-1); + } + *alport = *portp; + return (s); +} diff --git a/lib/libc/rpc/Makefile.inc b/lib/libc/rpc/Makefile.inc index d4ea29b4c91..4df12abfb9d 100644 --- a/lib/libc/rpc/Makefile.inc +++ b/lib/libc/rpc/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.8 1998/11/20 11:18:47 d Exp $ +# $OpenBSD: Makefile.inc,v 1.9 1999/12/16 21:30:35 deraadt Exp $ # librpc sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE}/rpc ${LIBCSRCDIR}/rpc @@ -13,7 +13,8 @@ SRCS+= auth_none.c auth_unix.c authunix_prot.c bindresvport.c \ xdr_rec.c xdr_reference.c xdr_stdio.c MAN+= bindresvport.3 getrpcent.3 getrpcport.3 rpc.3 xdr.3 rpcauth.3 -MLINKS+= getrpcent.3 getrpcbyname.3 \ +MLINKS+= bindresvport.3 bindresvport_af.3 \ + getrpcent.3 getrpcbyname.3 \ getrpcent.3 getrpcbynumber.3 \ getrpcent.3 endrpcent.3 \ getrpcent.3 setrpcent.3 \ diff --git a/lib/libc/rpc/bindresvport.3 b/lib/libc/rpc/bindresvport.3 index 4960277753f..39e23eb911a 100644 --- a/lib/libc/rpc/bindresvport.3 +++ b/lib/libc/rpc/bindresvport.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: bindresvport.3,v 1.11 1999/07/09 13:35:22 aaron Exp $ +.\" $OpenBSD: bindresvport.3,v 1.12 1999/12/16 21:30:35 deraadt Exp $ .\" .Dd August 9, 1997 .Dt BINDRESVPORT 3 @@ -11,9 +11,13 @@ .Fd #include <netinet/in.h> .Ft int .Fn bindresvport "int sd" "struct sockaddr_in *sin" +.Ft int +.Fn bindresvport_af "int sd" "struct sockaddr *sa" "int af" .Sh DESCRIPTION .Fn bindresvport -is used to bind a socket descriptor to a privileged +and +.Fn bindresvport_af +are used to bind a socket descriptor to a privileged .Tn IP port, that is, a port number in the range 0-1023. .Fa sd @@ -23,10 +27,17 @@ is a socket descriptor that was returned by a call to Only root can bind to a privileged port; this call will fail for any other users. .Pp -If the value of sin->sin_port is non-zero, +If the value of +.Va sin->sin_port +is non-zero, .Fn bindresvport attempts to use the specified port. If that fails, it chooses another privileged port number automatically. +.Pp +.Fn bindresvport_af +acts in a similar way, but supports other protocols as well, such +as +.Va AF_INET6 . .Sh RETURN VALUES .Fn bindresvport returns 0 if it is successful, otherwise \-1 is returned and @@ -67,4 +78,5 @@ system or no implementation for it exists. .Sh SEE ALSO .Xr bind 2 , .Xr socket 2 , -.Xr rresvport 3 +.Xr rresvport 3 , +.Xr rresvport_af 3 diff --git a/lib/libc/rpc/bindresvport.c b/lib/libc/rpc/bindresvport.c index 003de2778cf..fe5ab3b1210 100644 --- a/lib/libc/rpc/bindresvport.c +++ b/lib/libc/rpc/bindresvport.c @@ -28,7 +28,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: bindresvport.c,v 1.9 1996/09/15 09:31:30 tholo Exp $"; +static char *rcsid = "$OpenBSD: bindresvport.c,v 1.10 1999/12/16 21:30:35 deraadt Exp $"; #endif /* LIBC_SCCS and not lint */ /* @@ -51,50 +51,79 @@ bindresvport(sd, sin) int sd; struct sockaddr_in *sin; { - int on, old, error; - struct sockaddr_in myaddr; - int sinlen = sizeof(struct sockaddr_in); + return bindresvport_af(sd, (struct sockaddr *)sin, AF_INET); +} + +/* + * Bind a socket to a privileged IP port + */ +int +bindresvport_af(sd, sa, af) + int sd; + struct sockaddr *sa; + int af; +{ + int old, error; + struct sockaddr_storage myaddr; + struct sockaddr_in *sin; + struct sockaddr_in6 *sin6; + int proto, portrange, portlow; + u_int16_t *portp; + int salen; - if (sin == (struct sockaddr_in *)0) { - sin = &myaddr; - memset(sin, 0, sinlen); - sin->sin_len = sinlen; - sin->sin_family = AF_INET; - } else if (sin->sin_family != AF_INET) { + if (sa == NULL) { + memset(&myaddr, 0, sizeof(myaddr)); + sa = (struct sockaddr *)&myaddr; + } + + if (af == AF_INET) { + proto = IPPROTO_IP; + portrange = IP_PORTRANGE; + portlow = IP_PORTRANGE_LOW; + sin = (struct sockaddr_in *)sa; + salen = sizeof(struct sockaddr_in); + portp = &sin->sin_port; + } else if (af == AF_INET6) { + proto = IPPROTO_IPV6; + portrange = IPV6_PORTRANGE; + portlow = IPV6_PORTRANGE_LOW; + sin6 = (struct sockaddr_in6 *)sa; + salen = sizeof(struct sockaddr_in6); + portp = &sin6->sin6_port; + } else { errno = EPFNOSUPPORT; return (-1); } + sa->sa_family = af; - if (sin->sin_port == 0) { + if (*portp == 0) { int oldlen = sizeof(old); - error = getsockopt(sd, IPPROTO_IP, IP_PORTRANGE, - &old, &oldlen); + + error = getsockopt(sd, proto, portrange, &old, &oldlen); if (error < 0) return(error); - on = IP_PORTRANGE_LOW; - error = setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, - &on, sizeof(on)); + error = setsockopt(sd, proto, portrange, &portlow, + sizeof(portlow)); if (error < 0) return(error); } - error = bind(sd, (struct sockaddr *)sin, sinlen); + error = bind(sd, sa, salen); - if (sin->sin_port == 0) { + if (*portp == 0) { int saved_errno = errno; if (error) { - if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, - &old, sizeof(old)) < 0) + if (setsockopt(sd, proto, portrange, &old, + sizeof(old)) < 0) errno = saved_errno; return (error); } - if (sin != &myaddr) { + if (sa != (struct sockaddr *)&myaddr) { /* Hmm, what did the kernel assign... */ - if (getsockname(sd, (struct sockaddr *)sin, - &sinlen) < 0) + if (getsockname(sd, sa, &salen) < 0) errno = saved_errno; return (error); } |