xref: /freebsd/lib/libc/rpc/bindresvport.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 /*	$NetBSD: bindresvport.c,v 1.19 2000/07/06 03:03:59 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /* from: $OpenBSD: bindresvport.c,v 1.7 1996/07/30 16:25:47 downsj Exp $ */
34 /*
35  * Copyright (c) 1987 by Sun Microsystems, Inc.
36  *
37  * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
38  */
39 
40 #include "namespace.h"
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 
44 #include <netinet/in.h>
45 
46 #include <errno.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include <rpc/rpc.h>
51 
52 #include <string.h>
53 #include "un-namespace.h"
54 
55 /*
56  * Bind a socket to a privileged IP port
57  */
58 int
59 bindresvport(int sd, struct sockaddr_in *sin)
60 {
61 	return bindresvport_sa(sd, (struct sockaddr *)sin);
62 }
63 
64 /*
65  * Bind a socket to a privileged IP port
66  */
67 int
68 bindresvport_sa(int sd, struct sockaddr *sa)
69 {
70 	int old, error, af;
71 	struct sockaddr_storage myaddr;
72 	struct sockaddr_in *sin;
73 #ifdef INET6
74 	struct sockaddr_in6 *sin6;
75 #endif
76 	int proto, portrange, portlow;
77 	u_int16_t *portp;
78 	socklen_t salen;
79 
80 	if (sa == NULL) {
81 		salen = sizeof(myaddr);
82 		sa = (struct sockaddr *)&myaddr;
83 
84 		if (_getsockname(sd, sa, &salen) == -1)
85 			return -1;	/* errno is correctly set */
86 
87 		af = sa->sa_family;
88 		memset(sa, 0, salen);
89 	} else
90 		af = sa->sa_family;
91 
92 	switch (af) {
93 	case AF_INET:
94 		proto = IPPROTO_IP;
95 		portrange = IP_PORTRANGE;
96 		portlow = IP_PORTRANGE_LOW;
97 		sin = (struct sockaddr_in *)sa;
98 		salen = sizeof(struct sockaddr_in);
99 		portp = &sin->sin_port;
100 		break;
101 #ifdef INET6
102 	case AF_INET6:
103 		proto = IPPROTO_IPV6;
104 		portrange = IPV6_PORTRANGE;
105 		portlow = IPV6_PORTRANGE_LOW;
106 		sin6 = (struct sockaddr_in6 *)sa;
107 		salen = sizeof(struct sockaddr_in6);
108 		portp = &sin6->sin6_port;
109 		break;
110 #endif
111 	default:
112 		errno = EPFNOSUPPORT;
113 		return (-1);
114 	}
115 	sa->sa_family = af;
116 	sa->sa_len = salen;
117 
118 	if (*portp == 0) {
119 		socklen_t oldlen = sizeof(old);
120 
121 		error = _getsockopt(sd, proto, portrange, &old, &oldlen);
122 		if (error < 0)
123 			return (error);
124 
125 		error = _setsockopt(sd, proto, portrange, &portlow,
126 		    sizeof(portlow));
127 		if (error < 0)
128 			return (error);
129 	}
130 
131 	error = _bind(sd, sa, salen);
132 
133 	if (*portp == 0) {
134 		int saved_errno = errno;
135 
136 		if (error < 0) {
137 			if (_setsockopt(sd, proto, portrange, &old,
138 			    sizeof(old)) < 0)
139 				errno = saved_errno;
140 			return (error);
141 		}
142 
143 		if (sa != (struct sockaddr *)&myaddr) {
144 			/* Hmm, what did the kernel assign? */
145 			if (_getsockname(sd, sa, &salen) < 0)
146 				errno = saved_errno;
147 			return (error);
148 		}
149 	}
150 	return (error);
151 }
152