xref: /titanic_44/usr/src/cmd/ssh/libopenbsd-compat/common/bindresvport.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /* This file has be modified from the original OpenBSD source */
2*7c478bd9Sstevel@tonic-gate 
3*7c478bd9Sstevel@tonic-gate /*
4*7c478bd9Sstevel@tonic-gate  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5*7c478bd9Sstevel@tonic-gate  * unrestricted use provided that this legend is included on all tape
6*7c478bd9Sstevel@tonic-gate  * media and as a part of the software program in whole or part.  Users
7*7c478bd9Sstevel@tonic-gate  * may copy or modify Sun RPC without charge, but are not authorized
8*7c478bd9Sstevel@tonic-gate  * to license or distribute it to anyone else except as part of a product or
9*7c478bd9Sstevel@tonic-gate  * program developed by the user.
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12*7c478bd9Sstevel@tonic-gate  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13*7c478bd9Sstevel@tonic-gate  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14*7c478bd9Sstevel@tonic-gate  *
15*7c478bd9Sstevel@tonic-gate  * Sun RPC is provided with no support and without any obligation on the
16*7c478bd9Sstevel@tonic-gate  * part of Sun Microsystems, Inc. to assist in its use, correction,
17*7c478bd9Sstevel@tonic-gate  * modification or enhancement.
18*7c478bd9Sstevel@tonic-gate  *
19*7c478bd9Sstevel@tonic-gate  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20*7c478bd9Sstevel@tonic-gate  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21*7c478bd9Sstevel@tonic-gate  * OR ANY PART THEREOF.
22*7c478bd9Sstevel@tonic-gate  *
23*7c478bd9Sstevel@tonic-gate  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24*7c478bd9Sstevel@tonic-gate  * or profits or other special, indirect and consequential damages, even if
25*7c478bd9Sstevel@tonic-gate  * Sun has been advised of the possibility of such damages.
26*7c478bd9Sstevel@tonic-gate  *
27*7c478bd9Sstevel@tonic-gate  * Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * 2550 Garcia Avenue
29*7c478bd9Sstevel@tonic-gate  * Mountain View, California  94043
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "includes.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #ifndef HAVE_BINDRESVPORT_SA
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
37*7c478bd9Sstevel@tonic-gate static char *rcsid = "$OpenBSD: bindresvport.c,v 1.13 2000/01/26 03:43:21 deraadt Exp $";
38*7c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1987 by Sun Microsystems, Inc.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #include "includes.h"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define STARTPORT 600
49*7c478bd9Sstevel@tonic-gate #define ENDPORT (IPPORT_RESERVED - 1)
50*7c478bd9Sstevel@tonic-gate #define NPORTS	(ENDPORT - STARTPORT + 1)
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * Bind a socket to a privileged IP port
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate int
bindresvport_sa(sd,sa)56*7c478bd9Sstevel@tonic-gate bindresvport_sa(sd, sa)
57*7c478bd9Sstevel@tonic-gate 	int sd;
58*7c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	int error, af;
61*7c478bd9Sstevel@tonic-gate 	struct sockaddr_storage myaddr;
62*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
63*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
64*7c478bd9Sstevel@tonic-gate 	u_int16_t *portp;
65*7c478bd9Sstevel@tonic-gate 	u_int16_t port;
66*7c478bd9Sstevel@tonic-gate 	socklen_t salen;
67*7c478bd9Sstevel@tonic-gate 	int i;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	if (sa == NULL) {
70*7c478bd9Sstevel@tonic-gate 		memset(&myaddr, 0, sizeof(myaddr));
71*7c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)&myaddr;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 		if (getsockname(sd, sa, &salen) == -1)
74*7c478bd9Sstevel@tonic-gate 			return -1;	/* errno is correctly set */
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 		af = sa->sa_family;
77*7c478bd9Sstevel@tonic-gate 		memset(&myaddr, 0, salen);
78*7c478bd9Sstevel@tonic-gate 	} else
79*7c478bd9Sstevel@tonic-gate 		af = sa->sa_family;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (af == AF_INET) {
82*7c478bd9Sstevel@tonic-gate 		/* LINTED */
83*7c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)sa;
84*7c478bd9Sstevel@tonic-gate 		salen = sizeof(struct sockaddr_in);
85*7c478bd9Sstevel@tonic-gate 		portp = &sin->sin_port;
86*7c478bd9Sstevel@tonic-gate 	} else if (af == AF_INET6) {
87*7c478bd9Sstevel@tonic-gate 		/* LINTED */
88*7c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)sa;
89*7c478bd9Sstevel@tonic-gate 		salen = sizeof(struct sockaddr_in6);
90*7c478bd9Sstevel@tonic-gate 		portp = &sin6->sin6_port;
91*7c478bd9Sstevel@tonic-gate 	} else {
92*7c478bd9Sstevel@tonic-gate 		errno = EPFNOSUPPORT;
93*7c478bd9Sstevel@tonic-gate 		return (-1);
94*7c478bd9Sstevel@tonic-gate 	}
95*7c478bd9Sstevel@tonic-gate 	sa->sa_family = af;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	port = ntohs(*portp);
98*7c478bd9Sstevel@tonic-gate 	if (port == 0)
99*7c478bd9Sstevel@tonic-gate 		port = (arc4random() % NPORTS) + STARTPORT;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	/* Avoid warning */
102*7c478bd9Sstevel@tonic-gate 	error = -1;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	for(i = 0; i < NPORTS; i++) {
105*7c478bd9Sstevel@tonic-gate 		*portp = htons(port);
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 		error = bind(sd, sa, salen);
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		/* Terminate on success */
110*7c478bd9Sstevel@tonic-gate 		if (error == 0)
111*7c478bd9Sstevel@tonic-gate 			break;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 		/* Terminate on errors, except "address already in use" */
114*7c478bd9Sstevel@tonic-gate 		if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL)))
115*7c478bd9Sstevel@tonic-gate 			break;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 		port++;
118*7c478bd9Sstevel@tonic-gate 		if (port > ENDPORT)
119*7c478bd9Sstevel@tonic-gate 			port = STARTPORT;
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	return (error);
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate #endif /* HAVE_BINDRESVPORT_SA */
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
128