summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorderaadt <>1999-12-16 21:30:34 +0000
committerderaadt <>1999-12-16 21:30:34 +0000
commit4bbe99685ce9ae55f318adc00406e0caf4432bdf (patch)
tree8382773087d5ec817a48ab45a39f045c3d6f5539
parentac10646d45729b02230d6ba16729bb91850b3186 (diff)
downloadopenbsd-4bbe99685ce9ae55f318adc00406e0caf4432bdf.tar.gz
openbsd-4bbe99685ce9ae55f318adc00406e0caf4432bdf.tar.bz2
openbsd-4bbe99685ce9ae55f318adc00406e0caf4432bdf.zip
rresvport_af() and bindresvport_af()
-rw-r--r--src/lib/libc/net/Makefile.inc7
-rw-r--r--src/lib/libc/net/rcmd.327
-rw-r--r--src/lib/libc/net/rcmd.c34
-rw-r--r--src/lib/libc/net/rresvport.c114
4 files changed, 138 insertions, 44 deletions
diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc
index 8b708aae9a..447a96f1e1 100644
--- a/src/lib/libc/net/Makefile.inc
+++ b/src/lib/libc/net/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.26 1999/12/11 08:09:11 itojun Exp $ 1# $OpenBSD: Makefile.inc,v 1.27 1999/12/16 21:30:34 deraadt Exp $
2 2
3# net sources 3# net sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net ${LIBCSRCDIR}/net 4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net ${LIBCSRCDIR}/net
@@ -15,7 +15,7 @@ SRCS+= base64.c freeaddrinfo.c gai_strerror.c getaddrinfo.c gethostnamadr.c \
15 ipx_addr.c ipx_ntoa.c iso_addr.c linkaddr.c ns_addr.c ns_ntoa.c \ 15 ipx_addr.c ipx_ntoa.c iso_addr.c linkaddr.c ns_addr.c ns_ntoa.c \
16 nsap_addr.c \ 16 nsap_addr.c \
17 net_addrcmp.c \ 17 net_addrcmp.c \
18 rcmd.c recv.c res_comp.c res_data.c res_debug.c \ 18 rcmd.c rresvport.c recv.c res_comp.c res_data.c res_debug.c \
19 res_init.c res_mkquery.c res_query.c res_random.c res_send.c send.c \ 19 res_init.c res_mkquery.c res_query.c res_random.c res_send.c send.c \
20 sethostent.c ethers.c rcmdsh.c 20 sethostent.c ethers.c rcmdsh.c
21 21
@@ -64,7 +64,8 @@ MLINKS+=iso_addr.3 iso_ntoa.3
64MLINKS+=link_addr.3 link_ntoa.3 64MLINKS+=link_addr.3 link_ntoa.3
65MLINKS+=ipx.3 ipx_addr.3 ipx.3 ipx_ntoa.3 65MLINKS+=ipx.3 ipx_addr.3 ipx.3 ipx_ntoa.3
66MLINKS+=ns.3 ns_addr.3 ns.3 ns_ntoa.3 66MLINKS+=ns.3 ns_addr.3 ns.3 ns_ntoa.3
67MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 67MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 \
68 rcmd.3 rresvport_af.3
68MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \ 69MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \
69 resolver.3 res_mkquery.3 resolver.3 res_send.3 resolver.3 res_query.3 \ 70 resolver.3 res_mkquery.3 resolver.3 res_send.3 resolver.3 res_query.3 \
70 resolver.3 res_search.3 71 resolver.3 res_search.3
diff --git a/src/lib/libc/net/rcmd.3 b/src/lib/libc/net/rcmd.3
index d086e875bf..5f6ff98459 100644
--- a/src/lib/libc/net/rcmd.3
+++ b/src/lib/libc/net/rcmd.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: rcmd.3,v 1.13 1999/07/05 04:41:00 aaron Exp $ 1.\" $OpenBSD: rcmd.3,v 1.14 1999/12/16 21:30:34 deraadt Exp $
2.\" 2.\"
3.\" Copyright (c) 1983, 1991, 1993 3.\" Copyright (c) 1983, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -47,6 +47,8 @@
47.Ft int 47.Ft int
48.Fn rresvport "int *port" 48.Fn rresvport "int *port"
49.Ft int 49.Ft int
50.Fn rresvport_af "int *port" "int af"
51.Ft int
50.Fn iruserok "u_int32_t raddr" "int superuser" "const char *ruser" "const char *luser" 52.Fn iruserok "u_int32_t raddr" "int superuser" "const char *ruser" "const char *luser"
51.Ft int 53.Ft int
52.Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser" 54.Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser"
@@ -73,8 +75,10 @@ to run the command via
73.Xr rsh 1 . 75.Xr rsh 1 .
74The 76The
75.Fn rresvport 77.Fn rresvport
76function 78and
77returns a descriptor to a socket 79.Fn rresvport_af
80functions
81return a descriptor to a socket
78with an address in the privileged port space. 82with an address in the privileged port space.
79The 83The
80.Fn iruserok 84.Fn iruserok
@@ -142,7 +146,9 @@ The protocol is described in detail in
142.Pp 146.Pp
143The 147The
144.Fn rresvport 148.Fn rresvport
145function is used to obtain a socket with a privileged 149and
150.Fn rresvport_af
151functions are used to obtain a socket with a privileged
146address bound to it. This socket is suitable for use 152address bound to it. This socket is suitable for use
147by 153by
148.Fn rcmd 154.Fn rcmd
@@ -150,8 +156,10 @@ and several other functions. Privileged Internet ports are those
150in the range 0 to 1023. Only the super-user 156in the range 0 to 1023. Only the super-user
151is allowed to bind an address of this sort to a socket. 157is allowed to bind an address of this sort to a socket.
152.Fn rresvport 158.Fn rresvport
153needs to be seeded with a port number; if that port 159and
154is not available it will find another. 160.Fn rresvport_af
161need to be seeded with a port number; if that port
162is not available these functions will find another.
155.Pp 163.Pp
156The 164The
157.Fn iruserok 165.Fn iruserok
@@ -200,8 +208,10 @@ It returns \-1 on error and prints a diagnostic message on the standard error.
200.Pp 208.Pp
201The 209The
202.Fn rresvport 210.Fn rresvport
203function 211and
204returns a valid, bound socket descriptor on success. 212.Fn rresvport_af
213functions
214return a valid, bound socket descriptor on success.
205It returns \-1 on error with the global value 215It returns \-1 on error with the global value
206.Va errno 216.Va errno
207set according to the reason for failure. 217set according to the reason for failure.
@@ -214,6 +224,7 @@ is overloaded to mean
214.Xr rsh 1 , 224.Xr rsh 1 ,
215.Xr intro 2 , 225.Xr intro 2 ,
216.Xr bindresvport 3 , 226.Xr bindresvport 3 ,
227.Xr bindresvport_af 3 ,
217.Xr rcmdsh 3 , 228.Xr rcmdsh 3 ,
218.Xr rexec 3 , 229.Xr rexec 3 ,
219.Xr rexecd 8 , 230.Xr rexecd 8 ,
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c
index c933f5b447..bd920faadb 100644
--- a/src/lib/libc/net/rcmd.c
+++ b/src/lib/libc/net/rcmd.c
@@ -34,7 +34,7 @@
34 */ 34 */
35 35
36#if defined(LIBC_SCCS) && !defined(lint) 36#if defined(LIBC_SCCS) && !defined(lint)
37static char *rcsid = "$OpenBSD: rcmd.c,v 1.31 1998/03/19 00:30:05 millert Exp $"; 37static char *rcsid = "$OpenBSD: rcmd.c,v 1.32 1999/12/16 21:30:34 deraadt Exp $";
38#endif /* LIBC_SCCS and not lint */ 38#endif /* LIBC_SCCS and not lint */
39 39
40#include <sys/param.h> 40#include <sys/param.h>
@@ -253,38 +253,6 @@ bad:
253 return (-1); 253 return (-1);
254} 254}
255 255
256int
257rresvport(alport)
258 int *alport;
259{
260 struct sockaddr_in sin;
261 int s;
262
263 bzero(&sin, sizeof sin);
264 sin.sin_len = sizeof(struct sockaddr_in);
265 sin.sin_family = AF_INET;
266 sin.sin_addr.s_addr = INADDR_ANY;
267 s = socket(AF_INET, SOCK_STREAM, 0);
268 if (s < 0)
269 return (-1);
270 sin.sin_port = htons((in_port_t)*alport);
271 if (*alport < IPPORT_RESERVED - 1) {
272 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
273 return (s);
274 if (errno != EADDRINUSE) {
275 (void)close(s);
276 return (-1);
277 }
278 }
279 sin.sin_port = 0;
280 if (bindresvport(s, &sin) == -1) {
281 (void)close(s);
282 return (-1);
283 }
284 *alport = (int)ntohs(sin.sin_port);
285 return (s);
286}
287
288int __check_rhosts_file = 1; 256int __check_rhosts_file = 1;
289char *__rcmd_errstr; 257char *__rcmd_errstr;
290 258
diff --git a/src/lib/libc/net/rresvport.c b/src/lib/libc/net/rresvport.c
new file mode 100644
index 0000000000..97a901ae0c
--- /dev/null
+++ b/src/lib/libc/net/rresvport.c
@@ -0,0 +1,114 @@
1/*
2 * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved.
3 * Copyright (c) 1983, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * This product includes software developed by Theo de Raadt.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37static char *rcsid = "$OpenBSD: rresvport.c,v 1.1 1999/12/16 21:30:34 deraadt Exp $";
38#endif /* LIBC_SCCS and not lint */
39
40#include <sys/param.h>
41#include <sys/socket.h>
42#include <sys/stat.h>
43
44#include <netinet/in.h>
45#include <arpa/inet.h>
46
47#include <signal.h>
48#include <fcntl.h>
49#include <netdb.h>
50#include <unistd.h>
51#include <pwd.h>
52#include <errno.h>
53#include <stdio.h>
54#include <ctype.h>
55#include <string.h>
56#include <syslog.h>
57#include <stdlib.h>
58#include <netgroup.h>
59
60int
61rresvport(alport)
62 int *alport;
63{
64 return rresvport_af(alport, AF_INET);
65}
66
67
68int
69rresvport_af(alport, af)
70 int *alport;
71 int af;
72{
73 struct sockaddr_storage ss;
74 struct sockaddr *sa;
75 u_int16_t *portp;
76 int s;
77
78 bzero(&ss, sizeof ss);
79 sa = (struct sockaddr *)&ss;
80
81 switch (af) {
82 case AF_INET:
83 sa->sa_len = sizeof(struct sockaddr_in);
84 portp = &((struct sockaddr_in *)&sa)->sin_port;
85 case AF_INET6:
86 sa->sa_len = sizeof(struct sockaddr_in6);
87 portp = &((struct sockaddr_in6 *)&sa)->sin6_port;
88 default:
89 return (-1);
90 }
91 sa->sa_family = af;
92
93 s = socket(af, SOCK_STREAM, 0);
94 if (s < 0)
95 return (-1);
96
97 *portp = htons(*alport);
98 if (*alport < IPPORT_RESERVED - 1) {
99 if (bind(s, sa, sa->sa_len) >= 0)
100 return (s);
101 if (errno != EADDRINUSE) {
102 (void)close(s);
103 return (-1);
104 }
105 }
106
107 *portp = 0;
108 if (bindresvport_af(s, sa, af) == -1) {
109 (void)close(s);
110 return (-1);
111 }
112 *alport = *portp;
113 return (s);
114}