diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 1999-12-16 21:30:36 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 1999-12-16 21:30:36 +0000 |
commit | 6d482af2eab04b3d3aef695c895761acfafa18e0 (patch) | |
tree | 988a0d2394ca3693e23c18db00b406ca3a231ac5 /lib/libc/net | |
parent | 1fa54c44ce388765511dd93bab101711fb09cbb5 (diff) |
rresvport_af() and bindresvport_af()
Diffstat (limited to 'lib/libc/net')
-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 |
4 files changed, 138 insertions, 44 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); +} |