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#if defined(LIBC_SCCS) && !defined(lint)
34static char *sccsid2 = "from: @(#)bindresvport.c 1.8 88/02/08 SMI";
35static char *sccsid = "from: @(#)bindresvport.c	2.2 88/07/29 4.0 RPCSRC";
36#endif
37/* from: $OpenBSD: bindresvport.c,v 1.7 1996/07/30 16:25:47 downsj Exp $ */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD$");
40
41/*
42 * Copyright (c) 1987 by Sun Microsystems, Inc.
43 *
44 * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
45 */
46
47#include "namespace.h"
48#include <sys/types.h>
49#include <sys/socket.h>
50
51#include <netinet/in.h>
52
53#include <errno.h>
54#include <string.h>
55#include <unistd.h>
56
57#include <rpc/rpc.h>
58
59#include <string.h>
60#include "un-namespace.h"
61
62/*
63 * Bind a socket to a privileged IP port
64 */
65int
66bindresvport(int sd, struct sockaddr_in *sin)
67{
68	return bindresvport_sa(sd, (struct sockaddr *)sin);
69}
70
71/*
72 * Bind a socket to a privileged IP port
73 */
74int
75bindresvport_sa(int sd, struct sockaddr *sa)
76{
77	int old, error, af;
78	struct sockaddr_storage myaddr;
79	struct sockaddr_in *sin;
80#ifdef INET6
81	struct sockaddr_in6 *sin6;
82#endif
83	int proto, portrange, portlow;
84	u_int16_t *portp;
85	socklen_t salen;
86
87	if (sa == NULL) {
88		salen = sizeof(myaddr);
89		sa = (struct sockaddr *)&myaddr;
90
91		if (_getsockname(sd, sa, &salen) == -1)
92			return -1;	/* errno is correctly set */
93
94		af = sa->sa_family;
95		memset(sa, 0, salen);
96	} else
97		af = sa->sa_family;
98
99	switch (af) {
100	case AF_INET:
101		proto = IPPROTO_IP;
102		portrange = IP_PORTRANGE;
103		portlow = IP_PORTRANGE_LOW;
104		sin = (struct sockaddr_in *)sa;
105		salen = sizeof(struct sockaddr_in);
106		portp = &sin->sin_port;
107		break;
108#ifdef INET6
109	case AF_INET6:
110		proto = IPPROTO_IPV6;
111		portrange = IPV6_PORTRANGE;
112		portlow = IPV6_PORTRANGE_LOW;
113		sin6 = (struct sockaddr_in6 *)sa;
114		salen = sizeof(struct sockaddr_in6);
115		portp = &sin6->sin6_port;
116		break;
117#endif
118	default:
119		errno = EPFNOSUPPORT;
120		return (-1);
121	}
122	sa->sa_family = af;
123	sa->sa_len = salen;
124
125	if (*portp == 0) {
126		socklen_t oldlen = sizeof(old);
127
128		error = _getsockopt(sd, proto, portrange, &old, &oldlen);
129		if (error < 0)
130			return (error);
131
132		error = _setsockopt(sd, proto, portrange, &portlow,
133		    sizeof(portlow));
134		if (error < 0)
135			return (error);
136	}
137
138	error = _bind(sd, sa, salen);
139
140	if (*portp == 0) {
141		int saved_errno = errno;
142
143		if (error < 0) {
144			if (_setsockopt(sd, proto, portrange, &old,
145			    sizeof(old)) < 0)
146				errno = saved_errno;
147			return (error);
148		}
149
150		if (sa != (struct sockaddr *)&myaddr) {
151			/* Hmm, what did the kernel assign? */
152			if (_getsockname(sd, sa, &salen) < 0)
153				errno = saved_errno;
154			return (error);
155		}
156	}
157	return (error);
158}
159