Damien Miller | f468442 | 2003-06-02 18:59:08 +1000 | [diff] [blame] | 1 | /* This file has be substantially modified from the original OpenBSD source */ |
| 2 | |
Damien Miller | a97529f | 2007-10-26 16:16:09 +1000 | [diff] [blame] | 3 | /* $OpenBSD: bindresvport.c,v 1.17 2005/12/21 01:40:22 millert Exp $ */ |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 4 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 5 | /* |
Damien Miller | f468442 | 2003-06-02 18:59:08 +1000 | [diff] [blame] | 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. |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 29 | */ |
| 30 | |
Darren Tucker | 6f15c07 | 2005-11-10 17:52:08 +1100 | [diff] [blame] | 31 | /* OPENBSD ORIGINAL: lib/libc/rpc/bindresvport.c */ |
| 32 | |
Ben Lindstrom | dd21fe9 | 2002-06-27 18:23:20 +0000 | [diff] [blame] | 33 | #include "includes.h" |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 34 | |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 35 | #ifndef HAVE_BINDRESVPORT_SA |
Damien Miller | 607aede | 2006-09-01 15:48:19 +1000 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <sys/socket.h> |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 38 | |
Damien Miller | 607aede | 2006-09-01 15:48:19 +1000 | [diff] [blame] | 39 | #include <netinet/in.h> |
Darren Tucker | 46aa3e0 | 2006-09-02 15:32:40 +1000 | [diff] [blame] | 40 | #include <arpa/inet.h> |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 41 | |
Darren Tucker | 2c1a02a | 2006-07-12 22:40:50 +1000 | [diff] [blame] | 42 | #include <errno.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 43 | #include <string.h> |
Darren Tucker | 2c1a02a | 2006-07-12 22:40:50 +1000 | [diff] [blame] | 44 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 45 | #define STARTPORT 600 |
| 46 | #define ENDPORT (IPPORT_RESERVED - 1) |
| 47 | #define NPORTS (ENDPORT - STARTPORT + 1) |
| 48 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 49 | /* |
| 50 | * Bind a socket to a privileged IP port |
| 51 | */ |
| 52 | int |
Darren Tucker | fe80d7a | 2005-11-10 17:54:46 +1100 | [diff] [blame] | 53 | bindresvport_sa(int sd, struct sockaddr *sa) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 54 | { |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 55 | int error, af; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 56 | struct sockaddr_storage myaddr; |
| 57 | struct sockaddr_in *sin; |
| 58 | struct sockaddr_in6 *sin6; |
| 59 | u_int16_t *portp; |
Damien Miller | 8dbbe6e | 2000-01-22 18:17:42 +1100 | [diff] [blame] | 60 | u_int16_t port; |
Ben Lindstrom | 0d5af60 | 2001-01-09 00:50:29 +0000 | [diff] [blame] | 61 | socklen_t salen; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 62 | int i; |
| 63 | |
| 64 | if (sa == NULL) { |
| 65 | memset(&myaddr, 0, sizeof(myaddr)); |
| 66 | sa = (struct sockaddr *)&myaddr; |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 67 | |
| 68 | if (getsockname(sd, sa, &salen) == -1) |
| 69 | return -1; /* errno is correctly set */ |
| 70 | |
| 71 | af = sa->sa_family; |
| 72 | memset(&myaddr, 0, salen); |
| 73 | } else |
| 74 | af = sa->sa_family; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 75 | |
| 76 | if (af == AF_INET) { |
| 77 | sin = (struct sockaddr_in *)sa; |
| 78 | salen = sizeof(struct sockaddr_in); |
| 79 | portp = &sin->sin_port; |
| 80 | } else if (af == AF_INET6) { |
| 81 | sin6 = (struct sockaddr_in6 *)sa; |
| 82 | salen = sizeof(struct sockaddr_in6); |
| 83 | portp = &sin6->sin6_port; |
| 84 | } else { |
| 85 | errno = EPFNOSUPPORT; |
| 86 | return (-1); |
| 87 | } |
| 88 | sa->sa_family = af; |
| 89 | |
Damien Miller | 8dbbe6e | 2000-01-22 18:17:42 +1100 | [diff] [blame] | 90 | port = ntohs(*portp); |
| 91 | if (port == 0) |
| 92 | port = (arc4random() % NPORTS) + STARTPORT; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 93 | |
Damien Miller | a64b57a | 2001-01-17 10:44:13 +1100 | [diff] [blame] | 94 | /* Avoid warning */ |
| 95 | error = -1; |
| 96 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 97 | for(i = 0; i < NPORTS; i++) { |
Damien Miller | 8dbbe6e | 2000-01-22 18:17:42 +1100 | [diff] [blame] | 98 | *portp = htons(port); |
| 99 | |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 100 | error = bind(sd, sa, salen); |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 101 | |
Damien Miller | b9b94a7 | 2000-01-17 09:52:46 +1100 | [diff] [blame] | 102 | /* Terminate on success */ |
| 103 | if (error == 0) |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 104 | break; |
| 105 | |
Damien Miller | b9b94a7 | 2000-01-17 09:52:46 +1100 | [diff] [blame] | 106 | /* Terminate on errors, except "address already in use" */ |
Damien Miller | 19fe9c7 | 2000-01-17 15:23:01 +1100 | [diff] [blame] | 107 | if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL))) |
Damien Miller | b9b94a7 | 2000-01-17 09:52:46 +1100 | [diff] [blame] | 108 | break; |
| 109 | |
Damien Miller | 8dbbe6e | 2000-01-22 18:17:42 +1100 | [diff] [blame] | 110 | port++; |
| 111 | if (port > ENDPORT) |
| 112 | port = STARTPORT; |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return (error); |
| 116 | } |
| 117 | |
Damien Miller | 2a5c1ce | 2001-01-25 10:32:00 +1100 | [diff] [blame] | 118 | #endif /* HAVE_BINDRESVPORT_SA */ |