2 5955d494 2022-07-05 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
4 b542d800 2021-11-20 op * Permission to use, copy, modify, and distribute this software for any
5 b542d800 2021-11-20 op * purpose with or without fee is hereby granted, provided that the above
6 b542d800 2021-11-20 op * copyright notice and this permission notice appear in all copies.
8 b542d800 2021-11-20 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 b542d800 2021-11-20 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 b542d800 2021-11-20 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 b542d800 2021-11-20 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 b542d800 2021-11-20 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 b542d800 2021-11-20 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 b542d800 2021-11-20 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 0efd7c9a 2022-07-30 op #include "config.h"
19 b542d800 2021-11-20 op #include <sys/types.h>
20 eb1810c9 2022-07-05 op #include <sys/stat.h>
21 b542d800 2021-11-20 op #include <sys/socket.h>
22 b542d800 2021-11-20 op #include <sys/wait.h>
24 1a869946 2021-11-23 op #include <ctype.h>
25 b542d800 2021-11-20 op #include <errno.h>
26 eb1810c9 2022-07-05 op #include <fcntl.h>
27 b542d800 2021-11-20 op #include <limits.h>
28 b542d800 2021-11-20 op #include <netdb.h>
29 b542d800 2021-11-20 op #include <signal.h>
30 b542d800 2021-11-20 op #include <stdio.h>
31 b542d800 2021-11-20 op #include <stdlib.h>
32 b542d800 2021-11-20 op #include <string.h>
33 3376f40a 2021-11-25 op #include <syslog.h>
34 b542d800 2021-11-20 op #include <unistd.h>
36 3376f40a 2021-11-25 op #include "log.h"
38 7e04fdce 2021-12-01 op #define MAXSOCK 32
39 1b3b9dc3 2021-11-23 op #define BACKOFF 1
40 c7868114 2022-07-05 op #define RETRIES 16
42 72c20227 2021-11-21 op #ifndef __OpenBSD__
43 72c20227 2021-11-21 op #define pledge(p, e) 0
44 4844aeaf 2022-07-06 op #define unveil(p, m) 0
47 b542d800 2021-11-20 op const char *addr; /* our addr */
48 79313907 2021-11-23 op const char *ssh_tflag;
49 b542d800 2021-11-20 op const char *ssh_dest;
51 1a869946 2021-11-23 op char ssh_host[256];
52 1a869946 2021-11-23 op char ssh_port[16];
54 b542d800 2021-11-20 op struct event sockev[MAXSOCK];
55 b542d800 2021-11-20 op int socks[MAXSOCK];
61 b542d800 2021-11-20 op struct event sighupev;
62 b542d800 2021-11-20 op struct event sigintev;
63 b542d800 2021-11-20 op struct event sigtermev;
64 b542d800 2021-11-20 op struct event sigchldev;
65 b542d800 2021-11-20 op struct event siginfoev;
67 dd30c95b 2021-12-01 op struct timeval timeout = {600, 0}; /* 10 minutes */
68 b542d800 2021-11-20 op struct event timeoutev;
70 b542d800 2021-11-20 op pid_t ssh_pid = -1;
75 1b3b9dc3 2021-11-23 op int ntentative;
76 1b3b9dc3 2021-11-23 op struct timeval retry;
77 1b3b9dc3 2021-11-23 op struct event waitev;
79 f0531366 2021-11-21 op struct bufferevent *sourcebev;
81 f0531366 2021-11-21 op struct bufferevent *tobev;
85 329392cb 2022-07-05 op sig_handler(int sig, short event, void *data)
89 329392cb 2022-07-05 op switch (sig) {
93 803370a7 2022-07-05 op log_info("quitting");
94 329392cb 2022-07-05 op event_loopbreak();
97 329392cb 2022-07-05 op if (waitpid(ssh_pid, &status, WNOHANG) == -1)
98 329392cb 2022-07-05 op fatal("waitpid");
101 329392cb 2022-07-05 op #ifdef SIGINFO
102 329392cb 2022-07-05 op case SIGINFO:
104 329392cb 2022-07-05 op case SIGUSR1:
106 329392cb 2022-07-05 op log_info("connections: %d", conn);
111 b542d800 2021-11-20 op spawn_ssh(void)
113 3376f40a 2021-11-25 op log_debug("spawning ssh");
115 b542d800 2021-11-20 op switch (ssh_pid = fork()) {
117 98c3d897 2022-07-06 op log_warnx("fork");
120 9050e628 2022-07-30 op execl(SSH_PROG, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
122 3376f40a 2021-11-25 op fatal("exec");
129 c56ccc84 2022-07-05 op conn_free(struct conn *c)
131 c56ccc84 2022-07-05 op if (c->sourcebev != NULL)
132 c56ccc84 2022-07-05 op bufferevent_free(c->sourcebev);
133 c56ccc84 2022-07-05 op if (c->tobev != NULL)
134 c56ccc84 2022-07-05 op bufferevent_free(c->tobev);
136 c56ccc84 2022-07-05 op if (evtimer_pending(&c->waitev, NULL))
137 c56ccc84 2022-07-05 op evtimer_del(&c->waitev);
139 c56ccc84 2022-07-05 op close(c->source);
140 8ae8b89f 2022-07-05 op if (c->to != -1)
141 8ae8b89f 2022-07-05 op close(c->to);
147 b542d800 2021-11-20 op killing_time(int fd, short event, void *data)
149 b542d800 2021-11-20 op if (ssh_pid == -1)
152 3376f40a 2021-11-25 op log_debug("timeout expired, killing ssh (%d)", ssh_pid);
153 b542d800 2021-11-20 op kill(ssh_pid, SIGTERM);
154 b542d800 2021-11-20 op ssh_pid = -1;
158 b542d800 2021-11-20 op nopcb(struct bufferevent *bev, void *d)
164 b542d800 2021-11-20 op sreadcb(struct bufferevent *bev, void *d)
166 b542d800 2021-11-20 op struct conn *c = d;
168 b542d800 2021-11-20 op bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
172 b542d800 2021-11-20 op treadcb(struct bufferevent *bev, void *d)
174 b542d800 2021-11-20 op struct conn *c = d;
176 b542d800 2021-11-20 op bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
180 b542d800 2021-11-20 op errcb(struct bufferevent *bev, short event, void *d)
182 b542d800 2021-11-20 op struct conn *c = d;
184 3376f40a 2021-11-25 op log_info("closing connection (event=%x)", event);
186 c56ccc84 2022-07-05 op conn_free(c);
188 b542d800 2021-11-20 op if (--conn == 0) {
189 3376f40a 2021-11-25 op log_debug("scheduling ssh termination (%llds)",
190 ca125bba 2021-11-21 op (long long)timeout.tv_sec);
191 85b8fe26 2021-11-25 op if (timeout.tv_sec != 0) {
192 85b8fe26 2021-11-25 op evtimer_set(&timeoutev, killing_time, NULL);
193 85b8fe26 2021-11-25 op evtimer_add(&timeoutev, &timeout);
199 b542d800 2021-11-20 op connect_to_ssh(void)
201 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
202 b542d800 2021-11-20 op int r, saved_errno, sock;
203 1a869946 2021-11-23 op const char *cause;
205 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
206 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
207 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
209 1a869946 2021-11-23 op r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
210 c56ccc84 2022-07-05 op if (r != 0) {
211 c56ccc84 2022-07-05 op log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
212 1a869946 2021-11-23 op ssh_host, ssh_port, gai_strerror(r));
216 b542d800 2021-11-20 op for (res = res0; res; res = res->ai_next) {
217 b542d800 2021-11-20 op sock = socket(res->ai_family, res->ai_socktype,
218 b542d800 2021-11-20 op res->ai_protocol);
219 b542d800 2021-11-20 op if (sock == -1) {
220 b542d800 2021-11-20 op cause = "socket";
224 b542d800 2021-11-20 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
225 b542d800 2021-11-20 op cause = "connect";
226 b542d800 2021-11-20 op saved_errno = errno;
228 b542d800 2021-11-20 op errno = saved_errno;
236 b542d800 2021-11-20 op if (sock == -1)
237 3376f40a 2021-11-25 op log_warn("%s", cause);
239 b542d800 2021-11-20 op freeaddrinfo(res0);
244 1b3b9dc3 2021-11-23 op try_to_connect(int fd, short event, void *d)
246 1b3b9dc3 2021-11-23 op struct conn *c = d;
248 ab4b8022 2022-07-06 op /* ssh may have died in the meantime */
249 1b3b9dc3 2021-11-23 op if (ssh_pid == -1) {
250 ab4b8022 2022-07-06 op conn_free(c);
254 1b3b9dc3 2021-11-23 op c->ntentative++;
255 61461d5b 2021-12-01 op log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
256 1b3b9dc3 2021-11-23 op c->ntentative, RETRIES);
258 1b3b9dc3 2021-11-23 op if ((c->to = connect_to_ssh()) == -1) {
259 1b3b9dc3 2021-11-23 op if (c->ntentative == RETRIES) {
260 3376f40a 2021-11-25 op log_warnx("giving up connecting");
261 ab4b8022 2022-07-06 op conn_free(c);
265 1b3b9dc3 2021-11-23 op evtimer_set(&c->waitev, try_to_connect, c);
266 1b3b9dc3 2021-11-23 op evtimer_add(&c->waitev, &c->retry);
270 53abf42e 2022-07-05 op log_info("connected!");
272 1b3b9dc3 2021-11-23 op c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
273 1b3b9dc3 2021-11-23 op c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
274 c56ccc84 2022-07-05 op if (c->sourcebev == NULL || c->tobev == NULL) {
275 c56ccc84 2022-07-05 op log_warn("bufferevent_new");
276 c56ccc84 2022-07-05 op conn_free(c);
280 1b3b9dc3 2021-11-23 op bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
281 1b3b9dc3 2021-11-23 op bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
285 b542d800 2021-11-20 op do_accept(int fd, short event, void *data)
287 82c452fa 2022-07-05 op struct conn *c;
290 3376f40a 2021-11-25 op log_debug("incoming connection");
292 0828da2c 2022-07-06 op if ((s = accept(fd, NULL, 0)) == -1) {
293 0828da2c 2022-07-06 op log_warn("accept");
297 98c3d897 2022-07-06 op if (ssh_pid == -1 && spawn_ssh() == -1) {
302 82c452fa 2022-07-05 op if ((c = calloc(1, sizeof(*c))) == NULL) {
303 82c452fa 2022-07-05 op log_warn("calloc");
309 f56227b6 2022-07-06 op if (evtimer_pending(&timeoutev, NULL))
310 f56227b6 2022-07-06 op evtimer_del(&timeoutev);
312 82c452fa 2022-07-05 op c->source = s;
314 82c452fa 2022-07-05 op c->retry.tv_sec = BACKOFF;
315 82c452fa 2022-07-05 op evtimer_set(&c->waitev, try_to_connect, c);
316 82c452fa 2022-07-05 op evtimer_add(&c->waitev, &c->retry);
319 1a869946 2021-11-23 op static const char *
320 1a869946 2021-11-23 op copysec(const char *s, char *d, size_t len)
322 1a869946 2021-11-23 op const char *c;
324 1a869946 2021-11-23 op if ((c = strchr(s, ':')) == NULL)
326 1a869946 2021-11-23 op if ((size_t)(c - s) >= len-1)
328 1a869946 2021-11-23 op memset(d, 0, len);
329 1a869946 2021-11-23 op memcpy(d, s, c - s);
334 b542d800 2021-11-20 op bind_socket(void)
336 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
337 4a797971 2021-11-25 op int v, r, saved_errno;
338 b542d800 2021-11-20 op char host[64];
339 b542d800 2021-11-20 op const char *c, *h, *port, *cause;
341 b542d800 2021-11-20 op if ((c = strchr(addr, ':')) == NULL) {
345 1a869946 2021-11-23 op if ((c = copysec(addr, host, sizeof(host))) == NULL)
346 3376f40a 2021-11-25 op fatalx("name too long: %s", addr);
352 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
353 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
354 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
355 b542d800 2021-11-20 op hints.ai_flags = AI_PASSIVE;
357 b542d800 2021-11-20 op r = getaddrinfo(h, port, &hints, &res0);
359 3376f40a 2021-11-25 op fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
361 b542d800 2021-11-20 op for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
362 b542d800 2021-11-20 op socks[nsock] = socket(res->ai_family, res->ai_socktype,
363 b542d800 2021-11-20 op res->ai_protocol);
364 b542d800 2021-11-20 op if (socks[nsock] == -1) {
365 b542d800 2021-11-20 op cause = "socket";
369 b542d800 2021-11-20 op if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
370 b542d800 2021-11-20 op cause = "bind";
371 b542d800 2021-11-20 op saved_errno = errno;
372 b542d800 2021-11-20 op close(socks[nsock]);
373 b542d800 2021-11-20 op errno = saved_errno;
378 4a797971 2021-11-25 op if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
379 4a797971 2021-11-25 op sizeof(v)) == -1)
380 4a797971 2021-11-25 op fatal("setsockopt(SO_REUSEADDR)");
383 4a797971 2021-11-25 op if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
384 4a797971 2021-11-25 op sizeof(v)) == -1)
385 4a797971 2021-11-25 op fatal("setsockopt(SO_REUSEPORT)");
387 b542d800 2021-11-20 op listen(socks[nsock], 5);
391 b542d800 2021-11-20 op if (nsock == 0)
392 3376f40a 2021-11-25 op fatal("%s", cause);
394 b542d800 2021-11-20 op freeaddrinfo(res0);
398 8a8fe09b 2022-07-05 op parse_sshaddr(void)
400 1a869946 2021-11-23 op const char *c;
402 1d9a178e 2022-07-05 op if (isdigit((unsigned char)*ssh_tflag)) {
403 1a869946 2021-11-23 op strlcpy(ssh_host, "localhost", sizeof(ssh_host));
404 79313907 2021-11-23 op if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
409 79313907 2021-11-23 op if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
411 1a869946 2021-11-23 op if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
416 039c0887 2022-07-06 op fatalx("wrong value for -B");
419 9ac6c545 2021-11-23 op static void __dead
422 3376f40a 2021-11-25 op fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
423 9ac6c545 2021-11-23 op " destination\n", getprogname());
428 b542d800 2021-11-20 op main(int argc, char **argv)
430 eb1810c9 2022-07-05 op int ch, i, fd;
431 b542d800 2021-11-20 op const char *errstr;
432 eb1810c9 2022-07-05 op struct stat sb;
435 eb1810c9 2022-07-05 op * Ensure we have fds 0-2 open so that we have no issue with
436 eb1810c9 2022-07-05 op * calling bind_socket before daemon(3).
438 eb1810c9 2022-07-05 op for (i = 0; i < 3; ++i) {
439 eb1810c9 2022-07-05 op if (fstat(i, &sb) == -1) {
440 eb1810c9 2022-07-05 op if ((fd = open("/dev/null", O_RDWR)) != -1) {
441 eb1810c9 2022-07-05 op if (dup2(fd, i) == -1)
450 3376f40a 2021-11-25 op log_init(1, LOG_DAEMON);
451 3376f40a 2021-11-25 op log_setverbose(1);
453 3376f40a 2021-11-25 op while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
454 b542d800 2021-11-20 op switch (ch) {
456 79313907 2021-11-23 op ssh_tflag = optarg;
457 8a8fe09b 2022-07-05 op parse_sshaddr();
460 b542d800 2021-11-20 op addr = optarg;
466 85b8fe26 2021-11-25 op timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
467 b542d800 2021-11-20 op if (errstr != NULL)
468 3376f40a 2021-11-25 op fatalx("timeout is %s: %s", errstr, optarg);
477 b542d800 2021-11-20 op argc -= optind;
478 b542d800 2021-11-20 op argv += optind;
480 79313907 2021-11-23 op if (argc != 1 || addr == NULL || ssh_tflag == NULL)
483 b542d800 2021-11-20 op ssh_dest = argv[0];
485 eb1810c9 2022-07-05 op bind_socket();
487 3376f40a 2021-11-25 op log_init(debug, LOG_DAEMON);
488 3376f40a 2021-11-25 op log_setverbose(verbose);
491 3376f40a 2021-11-25 op daemon(1, 0);
493 b542d800 2021-11-20 op signal(SIGPIPE, SIG_IGN);
495 b542d800 2021-11-20 op event_init();
497 b542d800 2021-11-20 op /* initialize the timer */
498 b542d800 2021-11-20 op evtimer_set(&timeoutev, killing_time, NULL);
500 329392cb 2022-07-05 op signal_set(&sighupev, SIGHUP, sig_handler, NULL);
501 329392cb 2022-07-05 op signal_set(&sigintev, SIGINT, sig_handler, NULL);
502 329392cb 2022-07-05 op signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
503 329392cb 2022-07-05 op signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
504 047fe0f8 2021-11-27 op #ifdef SIGINFO
505 329392cb 2022-07-05 op signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
507 329392cb 2022-07-05 op signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
510 b542d800 2021-11-20 op signal_add(&sighupev, NULL);
511 b542d800 2021-11-20 op signal_add(&sigintev, NULL);
512 b542d800 2021-11-20 op signal_add(&sigtermev, NULL);
513 b542d800 2021-11-20 op signal_add(&sigchldev, NULL);
514 b542d800 2021-11-20 op signal_add(&siginfoev, NULL);
516 b542d800 2021-11-20 op for (i = 0; i < nsock; ++i) {
517 b542d800 2021-11-20 op event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
518 b542d800 2021-11-20 op do_accept, NULL);
519 b542d800 2021-11-20 op event_add(&sockev[i], NULL);
522 9050e628 2022-07-30 op if (unveil(SSH_PROG, "x") == -1)
523 9050e628 2022-07-30 op fatal("unveil(%s)", SSH_PROG);
526 b542d800 2021-11-20 op * dns, inet: bind the socket and connect to the childs.
527 b542d800 2021-11-20 op * proc, exec: execute ssh on demand.
529 b542d800 2021-11-20 op if (pledge("stdio dns inet proc exec", NULL) == -1)
530 3376f40a 2021-11-25 op fatal("pledge");
532 3376f40a 2021-11-25 op log_info("starting");
533 b542d800 2021-11-20 op event_dispatch();
535 b542d800 2021-11-20 op if (ssh_pid != -1)
536 b542d800 2021-11-20 op kill(ssh_pid, SIGINT);