Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/bindresvport.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29
30 /*
31 * Copyright (c) 1987 by Sun Microsystems, Inc.
32 *
33 * Portions Copyright(C) 1996, Jason Downs. All rights reserved.
34 */
35
36 #include <sys/types.h>
37 #include <sys/socket.h>
38
39 #include <netdb.h>
40 #include <netinet/in.h>
41
42 #include <stdio.h>
43 #include <ctype.h>
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #include <rpc/rpc.h>
50
51 #include <string.h>
52 #include <reentrant.h>
53
54 extern pthread_mutex_t port_lock;
55
56 /*
57 * Bind a socket to a privileged IP port
58 */
59 int
60 bindresvport(sd, sin)
61 int sd;
62 struct sockaddr_in *sin;
63 {
64 return bindresvport_sa(sd, (struct sockaddr *)sin);
65 }
66
67 #ifdef __linux__
68
69 #define STARTPORT 600
70 #define LOWPORT 512
71 #define ENDPORT (IPPORT_RESERVED - 1)
72 #define NPORTS (ENDPORT - STARTPORT + 1)
73
74 /*
75 * Read the file /etc/bindresvport.blacklist, so that we don't bind
76 * to these ports.
77 */
78
79 static int blacklist_read;
80 static int *list;
81 static int list_size = 0;
82
83 static void
84 load_blacklist (void)
85 {
86 FILE *fp;
87 char *buf = NULL;
88 size_t buflen = 0;
89 int size = 0, ptr = 0;
90
91 blacklist_read = 1;
92
93 fp = fopen ("/etc/bindresvport.blacklist", "r");
94 if (NULL == fp)
95 return;
96
97 while (!feof (fp))
98 {
99 unsigned long port;
100 char *tmp, *cp;
101 ssize_t n = getline (&buf, &buflen, fp);
102 if (n < 1)
103 break;
104
105 cp = buf;
106 tmp = strchr (cp, '#'); /* remove comments */
107 if (tmp)
108 *tmp = '\0';
109 while (isspace ((int)*cp)) /* remove spaces and tabs */
110 ++cp;
111 if (*cp == '\0') /* ignore empty lines */
112 continue;
113 if (cp[strlen (cp) - 1] == '\n')
114 cp[strlen (cp) - 1] = '\0';
115
116 port = strtoul (cp, &tmp, 0);
117 while (isspace(*tmp))
118 ++tmp;
119 if (*tmp != '\0' || (port == ULONG_MAX && errno == ERANGE))
120 continue;
121
122 /* Don't bother with out-of-range ports */
123 if (port < LOWPORT || port > ENDPORT)
124 continue;
125
126 if (ptr >= size)
127 {
128 size += 10;
129 list = realloc (list, size * sizeof (int));
130 if (list == NULL)
131 {
132 free (buf);
133 fclose (fp);
134 return;
135 }
136 }
137
138 list[ptr++] = port;
139 }
140
141 fclose (fp);
142
143 if (buf)
144 free (buf);
145
146 list_size = ptr;
147 }
148
149 int
150 bindresvport_sa(sd, sa)
151 int sd;
152 struct sockaddr *sa;
153 {
154 int res, af;
155 struct sockaddr_storage myaddr;
156 struct sockaddr_in *sin;
157 #ifdef INET6
158 struct sockaddr_in6 *sin6;
159 #endif
160 u_int16_t *portp;
161 static u_int16_t port;
162 static short startport = STARTPORT;
163 socklen_t salen;
164 int nports;
165 int endport = ENDPORT;
166 int i;
167
168 mutex_lock(&port_lock);
169
170 if (!blacklist_read)
171 load_blacklist();
172
173 nports = ENDPORT - startport + 1;
174
175 if (sa == NULL) {
176 salen = sizeof(myaddr);
177 sa = (struct sockaddr *)&myaddr;
178
179 if (getsockname(sd, (struct sockaddr *)&myaddr, &salen) == -1) {
180 mutex_unlock(&port_lock);
181 return -1; /* errno is correctly set */
182 }
183
184 af = myaddr.ss_family;
185 } else
186 af = sa->sa_family;
187
188 switch (af) {
189 case AF_INET:
190 sin = (struct sockaddr_in *)sa;
191 salen = sizeof(struct sockaddr_in);
192 port = ntohs(sin->sin_port);
193 portp = &sin->sin_port;
194 break;
195 #ifdef INET6
196 case AF_INET6:
197 sin6 = (struct sockaddr_in6 *)sa;
198 salen = sizeof(struct sockaddr_in6);
199 port = ntohs(sin6->sin6_port);
200 portp = &sin6->sin6_port;
201 break;
202 #endif
203 default:
204 errno = EPFNOSUPPORT;
205 mutex_unlock(&port_lock);
206 return (-1);
207 }
208 sa->sa_family = af;
209
210 if (port == 0) {
211 port = (getpid() % NPORTS) + STARTPORT;
212 }
213 res = -1;
214 errno = EADDRINUSE;
215 again:
216 for (i = 0; i < nports; ++i) {
217 int j;
218
219 /* Check if this port is not blacklisted. */
220 for (j = 0; j < list_size; j++)
221 if (port == list[j])
222 goto try_next_port;
223
224 *portp = htons(port);
225 res = bind(sd, sa, salen);
226 if (res >= 0 || errno != EADDRINUSE)
227 break;
228
229 try_next_port:
230 if (++port > endport)
231 port = startport;
232 }
233 if (i == nports && startport != LOWPORT) {
234 startport = LOWPORT;
235 endport = STARTPORT - 1;
236 nports = STARTPORT - LOWPORT;
237 port = LOWPORT + port % (STARTPORT - LOWPORT);
238 goto again;
239 }
240 mutex_unlock(&port_lock);
241
242 return (res);
243 }
244 #else
245
246 #define IP_PORTRANGE 19
247 #define IP_PORTRANGE_LOW 2
248
249 /*
250 * Bind a socket to a privileged IP port
251 */
252 int
253 bindresvport_sa(sd, sa)
254 int sd;
255 struct sockaddr *sa;
256 {
257 int old, error, af;
258 struct sockaddr_storage myaddr;
259 struct sockaddr_in *sin;
260 #ifdef INET6
261 struct sockaddr_in6 *sin6;
262 #endif
263 int proto, portrange, portlow;
264 u_int16_t *portp;
265 socklen_t salen;
266
267 if (sa == NULL) {
268 salen = sizeof(myaddr);
269 sa = (struct sockaddr *)&myaddr;
270
271 if (getsockname(sd, sa, &salen) == -1)
272 return -1; /* errno is correctly set */
273
274 af = sa->sa_family;
275 memset(sa, 0, salen);
276 } else
277 af = sa->sa_family;
278
279 switch (af) {
280 case AF_INET:
281 proto = IPPROTO_IP;
282 portrange = IP_PORTRANGE;
283 portlow = IP_PORTRANGE_LOW;
284 sin = (struct sockaddr_in *)sa;
285 salen = sizeof(struct sockaddr_in);
286 portp = &sin->sin_port;
287 break;
288 #ifdef INET6
289 case AF_INET6:
290 proto = IPPROTO_IPV6;
291 portrange = IPV6_PORTRANGE;
292 portlow = IPV6_PORTRANGE_LOW;
293 sin6 = (struct sockaddr_in6 *)sa;
294 salen = sizeof(struct sockaddr_in6);
295 portp = &sin6->sin6_port;
296 break;
297 #endif
298 default:
299 errno = EPFNOSUPPORT;
300 return (-1);
301 }
302 sa->sa_family = af;
303
304 if (*portp == 0) {
305 socklen_t oldlen = sizeof(old);
306
307 error = getsockopt(sd, proto, portrange, &old, &oldlen);
308 if (error < 0)
309 return (error);
310
311 error = setsockopt(sd, proto, portrange, &portlow,
312 sizeof(portlow));
313 if (error < 0)
314 return (error);
315 }
316
317 error = bind(sd, sa, salen);
318
319 if (*portp == 0) {
320 int saved_errno = errno;
321
322 if (error < 0) {
323 if (setsockopt(sd, proto, portrange, &old,
324 sizeof(old)) < 0)
325 errno = saved_errno;
326 return (error);
327 }
328
329 if (sa != (struct sockaddr *)&myaddr) {
330 /* Hmm, what did the kernel assign? */
331 if (getsockname(sd, sa, &salen) < 0)
332 errno = saved_errno;
333 return (error);
334 }
335 }
336 return (error);
337 }
338 #endif