xref: /freebsd/crypto/openssh/openbsd-compat/bindresvport.c (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 /* This file has be substantially modified from the original OpenBSD source */
2 
3 /*	$OpenBSD: bindresvport.c,v 1.17 2005/12/21 01:40:22 millert Exp $	*/
4 
5 /*
6  * Copyright 1996, Jason Downs.  All rights reserved.
7  * Copyright 1998, Theo de Raadt.  All rights reserved.
8  * Copyright 2000, Damien Miller.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* OPENBSD ORIGINAL: lib/libc/rpc/bindresvport.c */
32 
33 #include "includes.h"
34 
35 #ifndef HAVE_BINDRESVPORT_SA
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #define STARTPORT 600
47 #define ENDPORT (IPPORT_RESERVED - 1)
48 #define NPORTS	(ENDPORT - STARTPORT + 1)
49 
50 /*
51  * Bind a socket to a privileged IP port
52  */
53 int
54 bindresvport_sa(int sd, struct sockaddr *sa)
55 {
56 	int error, af;
57 	struct sockaddr_storage myaddr;
58 	struct sockaddr_in *in;
59 	struct sockaddr_in6 *in6;
60 	u_int16_t *portp;
61 	u_int16_t port;
62 	socklen_t salen;
63 	int i;
64 
65 	if (sa == NULL) {
66 		memset(&myaddr, 0, sizeof(myaddr));
67 		sa = (struct sockaddr *)&myaddr;
68 		salen = sizeof(myaddr);
69 
70 		if (getsockname(sd, sa, &salen) == -1)
71 			return -1;	/* errno is correctly set */
72 
73 		af = sa->sa_family;
74 		memset(&myaddr, 0, salen);
75 	} else
76 		af = sa->sa_family;
77 
78 	if (af == AF_INET) {
79 		in = (struct sockaddr_in *)sa;
80 		salen = sizeof(struct sockaddr_in);
81 		portp = &in->sin_port;
82 	} else if (af == AF_INET6) {
83 		in6 = (struct sockaddr_in6 *)sa;
84 		salen = sizeof(struct sockaddr_in6);
85 		portp = &in6->sin6_port;
86 	} else {
87 		errno = EPFNOSUPPORT;
88 		return (-1);
89 	}
90 	sa->sa_family = af;
91 
92 	port = ntohs(*portp);
93 	if (port == 0)
94 		port = arc4random_uniform(NPORTS) + STARTPORT;
95 
96 	/* Avoid warning */
97 	error = -1;
98 
99 	for(i = 0; i < NPORTS; i++) {
100 		*portp = htons(port);
101 
102 		error = bind(sd, sa, salen);
103 
104 		/* Terminate on success */
105 		if (error == 0)
106 			break;
107 
108 		/* Terminate on errors, except "address already in use" */
109 		if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL)))
110 			break;
111 
112 		port++;
113 		if (port > ENDPORT)
114 			port = STARTPORT;
115 	}
116 
117 	return (error);
118 }
119 
120 #endif /* HAVE_BINDRESVPORT_SA */
121