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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
/*
The MIT License (MIT)
Copyright (c) 2016 Wrymouth Innovation Ltd
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <errno.h>
#include <getopt.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include "crypto-gnutls.h"
static char *connectaddr = NULL;
static char *listenaddr = NULL;
static char *keyfile = NULL;
static char *certfile = NULL;
static char *cacertfile = NULL;
static char *hostname = NULL;
static int debug = 0;
static int insecure = 0;
static int nofork = 0;
static int server = 0;
static const char *defaultport = "12345";
static volatile sig_atomic_t rxsigquit = 0;
static int bindtoaddress(char *addrport)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int fd, s;
char addr[128];
snprintf(addr, sizeof(addr), "%s", addrport);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Stream socket */
hints.ai_protocol = 0; /* any protocol */
char *colon = strrchr(addr, ':');
const char *port = defaultport;
if (colon) {
*colon = 0;
port = colon + 1;
}
s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) {
fprintf(stderr, "Error in address %s: %s\n", addr,
gai_strerror(s));
return -1;
}
/* attempt to bind to each address */
for (rp = result; rp != NULL; rp = rp->ai_next) {
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (fd >= 0) {
int one = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one,
sizeof(one)) < 0) {
close(fd);
continue;
}
if (bind(fd, rp->ai_addr, rp->ai_addrlen) == 0)
break;
close(fd);
}
}
if (!rp) {
fprintf(stderr, "Error binding to %s:%s: %m\n", addr, port);
freeaddrinfo(result);
return -1;
}
freeaddrinfo(result); /* No longer needed */
if (listen(fd, 5) < 0) {
close(fd);
return -1;
}
return fd;
}
static int connecttoaddress(char *addrport)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int fd, s;
char addr[128];
snprintf(addr, sizeof(addr), "%s", addrport);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Stream socket */
hints.ai_protocol = 0; /* any protocol */
char *colon = strrchr(addr, ':');
const char *port = defaultport;
if (colon) {
*colon = 0;
port = colon + 1;
}
if (!hostname && !server)
hostname = strdup(addr);
s = getaddrinfo(addr, port, &hints, &result);
if (s != 0) {
fprintf(stderr, "Error in address %s: %s\n", addr,
gai_strerror(s));
return -1;
}
/* attempt to connect to each address */
for (rp = result; rp != NULL; rp = rp->ai_next) {
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (fd >= 0) {
if (connect(fd, rp->ai_addr, rp->ai_addrlen) == 0)
break;
close(fd);
}
}
if (!rp) {
fprintf(stderr, "Error connecting to %s:%s: %m\n", addr, port);
freeaddrinfo(result);
return -1;
}
freeaddrinfo(result); /* No longer needed */
return fd;
}
static int quitfn(void *opaque)
{
return rxsigquit;
}
static int runproxy(int acceptfd)
{
int connectfd;
if ((connectfd = connecttoaddress(connectaddr)) < 0) {
fprintf(stderr, "Could not connect\n");
close(acceptfd);
return -1;
}
tlssession_t *session = tlssession_new(server, keyfile, certfile,
cacertfile, hostname, insecure,
debug, quitfn, NULL, NULL);
if (!session) {
fprintf(stderr, "Could create TLS session\n");
close(connectfd);
close(acceptfd);
return -1;
}
int ret;
if (server)
ret = tlssession_mainloop(acceptfd, connectfd, session);
else
ret = tlssession_mainloop(connectfd, acceptfd, session);
tlssession_close(session);
close(connectfd);
close(acceptfd);
if (ret < 0) {
fprintf(stderr, "TLS proxy exited with an error\n");
return -1;
}
return 0;
}
static int runlistener(void)
{
int listenfd;
if ((listenfd = bindtoaddress(listenaddr)) < 0) {
fprintf(stderr, "Could not bind listener\n");
return -1;
}
/*
if (!nofork)
daemon (FALSE, FALSE);
*/
int fd;
while (!rxsigquit) {
do {
if ((fd = accept(listenfd, NULL, NULL)) < 0) {
if (errno != EINTR) {
fprintf(stderr, "Accept failed\n");
return -1;
}
}
} while (fd < 0 && !rxsigquit);
if (rxsigquit)
break;
if (nofork < 2) {
int ret = runproxy(fd);
if (ret < 0)
return -1;
} else {
int cpid = fork();
if (cpid == 0) {
/* we're the child */
runproxy(fd);
exit(0);
} else
close(fd);
}
}
return 0;
}
static void usage(void)
{
fprintf(stderr, "tlsproxy\n\n\
Usage:\n\
tlsproxy [OPTIONS]\n\
\n\
A TLS client or server proxy\n\
\n\
Options:\n\
-c, --connect ADDRESS Connect to ADDRESS\n\
-l, --listen ADDRESS Listen on ADDRESS\n\
-K, --key FILE Use FILE as private key\n\
-C, --cert FILE Use FILE as public key\n\
-A, --cacert FILE Use FILE as public CA cert file\n\
-H, --hostname HOSTNAME Use HOSTNAME to validate the CN of the peer\n\
rather than hostname extracted from -C option\n\
-s, --server Run the listen port encrypted rather than the\n\
connect port\n\
-i, --insecure Do not validate certificates\n\
-n, --nofork Do not fork off (aids debugging); specify twice\n\
to stop forking on accept as well\n\
-d, --debug Turn on debugging\n\
-h, --help Show this usage message\n\
\n\
\n");
}
static void processoptions(int argc, char **argv)
{
while (1) {
static const struct option longopts[] = {
{ "connect", required_argument, 0, 'c' },
{ "listen", required_argument, 0, 'l' },
{ "key", required_argument, 0, 'K' },
{ "cert", required_argument, 0, 'C' },
{ "cacert", required_argument, 0, 'A' },
{ "hostname", required_argument, 0, 'H' },
{ "server", no_argument, 0, 's' },
{ "insecure", no_argument, 0, 'i' },
{ "nofork", no_argument, 0, 'n' },
{ "debug", no_argument, 0, 'd' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
int optidx = 0;
int c = getopt_long(argc, argv, "c:l:K:C:A:H:sindh", longopts,
&optidx);
if (c == -1)
break;
switch (c) {
case 0: /* set a flag, nothing else to do */
break;
case 'c':
free(connectaddr);
connectaddr = strdup(optarg);
break;
case 'l':
free(listenaddr);
listenaddr = strdup(optarg);
break;
case 'K':
free(keyfile);
keyfile = strdup(optarg);
break;
case 'C':
free(certfile);
certfile = strdup(optarg);
break;
case 'A':
free(cacertfile);
cacertfile = strdup(optarg);
break;
case 'H':
free(hostname);
hostname = strdup(optarg);
break;
case 's':
server = 1;
break;
case 'i':
insecure = 1;
break;
case 'n':
nofork++;
break;
case 'd':
debug++;
break;
case 'h':
usage();
exit(0);
break;
default:
usage();
exit(1);
}
}
if (optind != argc || !connectaddr || !listenaddr) {
usage();
exit(1);
}
if (!certfile && keyfile)
certfile = strdup(keyfile);
}
static void handlesignal(int sig)
{
switch (sig) {
case SIGINT:
case SIGTERM:
rxsigquit++;
break;
default:
break;
}
}
static void setsignalmasks(void)
{
struct sigaction sa;
/* Set up the structure to specify the new action. */
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = handlesignal;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
sigaction(SIGPIPE, &sa, NULL);
}
int main(int argc, char **argv)
{
processoptions(argc, argv);
setsignalmasks();
if (tlssession_init())
exit(1);
runlistener();
free(connectaddr);
free(listenaddr);
free(keyfile);
free(certfile);
free(cacertfile);
free(hostname);
exit(0);
}
|