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