1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* proxy.c
*
* Binds to local port explicitly before forwarding TCP connections.
*
* Copyright (C) 2005-2011 NTT DATA CORPORATION
*
* Version: 2.5.0 2011/09/29
*
* This tool is intended to limit local port numbers that clients
* will use when connecting to servers, so that servers can enforce
* client's port number based access control.
*
* To compile this program run the following command
*
* gcc -Wall -O3 -o path_to_output proxy.c
* chown 0:0 path_to_output
*
* where the path_to_output is the location you want to place the binary.
*
* To use this program for ssh, create a file named "useproxy"
* containing a line
*
* ProxyCommand path_to_output %h %p min_port max_port
*
* where the min_port and max_port are the local port range
* the sshd will accept.
*
* Use -F option like
*
* ssh -F useproxy example.com
*
* when you run ssh.
*
* You may append to ~/.ssh/config or /etc/ssh/ssh_config
* if you want to use this tool by default.
*
* You need to turn SUID bit on (or give CAP_NET_BIND_SERVICE capability) like
*
* chmod 4755 path_to_output
*
* if you want to allow non root user to use local port less than 1024.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License v2 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
static in_addr_t get_host_by_name_alias(const char *strHostName)
{
in_addr_t IP;
IP = inet_addr(strHostName);
if (IP == INADDR_NONE) {
struct hostent *hp = gethostbyname(strHostName);
if (hp)
IP = *(in_addr_t *) hp->h_addr_list[0];
}
return IP;
}
int main(int argc, char *argv[])
{
const int remote = socket(PF_INET, SOCK_STREAM, 0);
unsigned int port;
struct sockaddr_in addr;
in_addr_t forward_connect_ip = INADDR_NONE;
unsigned short int forward_connect_port = 0;
unsigned short int forward_bind_port_min = 0;
unsigned short int forward_bind_port_max = 0;
if (argc != 5) {
fprintf(stderr, "Usage: %s forward_connect_host "
"forward_connect_port forward_bind_port_min "
"forward_bind_port_max\n", argv[0]);
return 1;
}
forward_bind_port_min = atoi(argv[3]);
forward_bind_port_max = atoi(argv[4]);
for (port = forward_bind_port_min; port <= forward_bind_port_max;
port++) {
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
if (!bind(remote, (struct sockaddr *) &addr, sizeof(addr)))
break;
}
if (port > forward_bind_port_max) {
fprintf(stderr, "ERROR: No local ports available.\n");
return 1;
}
{ /* Drop root privileges. */
const gid_t gid = -1;
setgroups(1, &gid);
setgid(-1);
setuid(-1);
}
forward_connect_ip = ntohl(get_host_by_name_alias(argv[1]));
forward_connect_port = atoi(argv[2]);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(forward_connect_ip);
addr.sin_port = htons(forward_connect_port);
if (connect(remote, (struct sockaddr *) &addr, sizeof(addr)) ||
fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK) ||
fcntl(1, F_SETFL, fcntl(1, F_GETFL) | O_NONBLOCK) ||
fcntl(remote, F_SETFL, fcntl(remote, F_GETFL) | O_NONBLOCK)) {
fprintf(stderr, "ERROR: Connecting to %u.%u.%u.%u : %s\n",
(unsigned char) (forward_connect_ip >> 24),
(unsigned char) (forward_connect_ip >> 16),
(unsigned char) (forward_connect_ip >> 8),
(unsigned char) forward_connect_ip, strerror(errno));
return 1;
}
while (1) {
fd_set rfds;
fd_set wfds;
static char local_buf[4096];
static char remote_buf[4096];
static int local_len = 0;
static int remote_len = 0;
static int local_eof = 0;
static int remote_eof = 0;
int len;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
if (local_eof == 0 && local_len < sizeof(local_buf))
FD_SET(0, &rfds);
if (remote_eof == 0 && remote_len < sizeof(remote_buf))
FD_SET(remote, &rfds);
if (local_len)
FD_SET(remote, &wfds);
if (remote_len)
FD_SET(1, &wfds);
select(remote + 1, &rfds, &wfds, NULL, NULL);
if (FD_ISSET(0, &rfds)) {
len = read(0, local_buf + local_len,
sizeof(local_buf) - local_len);
if (len > 0)
local_len += len;
else if (len == 0)
local_eof = 1;
}
if (FD_ISSET(remote, &rfds)) {
len = read(remote, remote_buf + remote_len,
sizeof(remote_buf) - remote_len);
if (len > 0)
remote_len += len;
else if (len == 0)
remote_eof = 1;
}
if (FD_ISSET(remote, &wfds)) {
len = write(remote, local_buf, local_len);
if (len > 0) {
local_len -= len;
memmove(local_buf, local_buf + len, local_len);
}
}
if (FD_ISSET(1, &wfds)) {
len = write(1, remote_buf, remote_len);
if (len > 0) {
remote_len -= len;
memmove(remote_buf, remote_buf + len,
remote_len);
}
}
if (local_len == 0 && local_eof == 1) {
shutdown(remote, SHUT_WR);
local_eof = 2;
}
if (remote_len == 0 && remote_eof == 1) {
shutdown(1, SHUT_WR);
remote_eof = 2;
}
if (local_eof == 2 && remote_eof == 2)
break;
}
return 0;
}
|