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 b542d800 2021-11-20 op #include <sys/types.h>
18 eb1810c9 2022-07-05 op #include <sys/stat.h>
19 b542d800 2021-11-20 op #include <sys/socket.h>
20 b542d800 2021-11-20 op #include <sys/wait.h>
22 1a869946 2021-11-23 op #include <ctype.h>
23 b542d800 2021-11-20 op #include <errno.h>
24 b542d800 2021-11-20 op #include <event.h>
25 eb1810c9 2022-07-05 op #include <fcntl.h>
26 b542d800 2021-11-20 op #include <limits.h>
27 b542d800 2021-11-20 op #include <netdb.h>
28 b542d800 2021-11-20 op #include <signal.h>
29 b542d800 2021-11-20 op #include <stdio.h>
30 b542d800 2021-11-20 op #include <stdlib.h>
31 b542d800 2021-11-20 op #include <string.h>
32 3376f40a 2021-11-25 op #include <syslog.h>
33 b542d800 2021-11-20 op #include <unistd.h>
35 3376f40a 2021-11-25 op #include "log.h"
37 b542d800 2021-11-20 op #ifndef SSH_PATH
38 e783915a 2021-11-25 op #define SSH_PATH "/usr/bin/ssh"
41 7e04fdce 2021-12-01 op #define MAXSOCK 32
42 1b3b9dc3 2021-11-23 op #define BACKOFF 1
43 c7868114 2022-07-05 op #define RETRIES 16
45 72c20227 2021-11-21 op #ifndef __OpenBSD__
46 72c20227 2021-11-21 op #define pledge(p, e) 0
49 b542d800 2021-11-20 op const char *addr; /* our addr */
50 79313907 2021-11-23 op const char *ssh_tflag;
51 b542d800 2021-11-20 op const char *ssh_dest;
53 1a869946 2021-11-23 op char ssh_host[256];
54 1a869946 2021-11-23 op char ssh_port[16];
56 b542d800 2021-11-20 op struct event sockev[MAXSOCK];
57 b542d800 2021-11-20 op int socks[MAXSOCK];
63 b542d800 2021-11-20 op struct event sighupev;
64 b542d800 2021-11-20 op struct event sigintev;
65 b542d800 2021-11-20 op struct event sigtermev;
66 b542d800 2021-11-20 op struct event sigchldev;
67 b542d800 2021-11-20 op struct event siginfoev;
69 dd30c95b 2021-12-01 op struct timeval timeout = {600, 0}; /* 10 minutes */
70 b542d800 2021-11-20 op struct event timeoutev;
72 b542d800 2021-11-20 op pid_t ssh_pid = -1;
77 1b3b9dc3 2021-11-23 op int ntentative;
78 1b3b9dc3 2021-11-23 op struct timeval retry;
79 1b3b9dc3 2021-11-23 op struct event waitev;
81 f0531366 2021-11-21 op struct bufferevent *sourcebev;
83 f0531366 2021-11-21 op struct bufferevent *tobev;
87 329392cb 2022-07-05 op sig_handler(int sig, short event, void *data)
91 329392cb 2022-07-05 op switch (sig) {
95 329392cb 2022-07-05 op event_loopbreak();
98 329392cb 2022-07-05 op if (waitpid(ssh_pid, &status, WNOHANG) == -1)
99 329392cb 2022-07-05 op fatal("waitpid");
100 329392cb 2022-07-05 op ssh_pid = -1;
102 329392cb 2022-07-05 op #ifdef SIGINFO
103 329392cb 2022-07-05 op case SIGINFO:
105 329392cb 2022-07-05 op case SIGUSR1:
107 329392cb 2022-07-05 op log_info("connections: %d", conn);
112 b542d800 2021-11-20 op spawn_ssh(void)
114 3376f40a 2021-11-25 op log_debug("spawning ssh");
116 b542d800 2021-11-20 op switch (ssh_pid = fork()) {
118 3376f40a 2021-11-25 op fatal("fork");
120 79313907 2021-11-23 op execl(SSH_PATH, "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 c56ccc84 2022-07-05 op close(c->to);
146 b542d800 2021-11-20 op killing_time(int fd, short event, void *data)
148 b542d800 2021-11-20 op if (ssh_pid == -1)
151 3376f40a 2021-11-25 op log_debug("timeout expired, killing ssh (%d)", ssh_pid);
152 b542d800 2021-11-20 op kill(ssh_pid, SIGTERM);
153 b542d800 2021-11-20 op ssh_pid = -1;
157 b542d800 2021-11-20 op nopcb(struct bufferevent *bev, void *d)
163 b542d800 2021-11-20 op sreadcb(struct bufferevent *bev, void *d)
165 b542d800 2021-11-20 op struct conn *c = d;
167 b542d800 2021-11-20 op bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
171 b542d800 2021-11-20 op treadcb(struct bufferevent *bev, void *d)
173 b542d800 2021-11-20 op struct conn *c = d;
175 b542d800 2021-11-20 op bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
179 b542d800 2021-11-20 op errcb(struct bufferevent *bev, short event, void *d)
181 b542d800 2021-11-20 op struct conn *c = d;
183 3376f40a 2021-11-25 op log_info("closing connection (event=%x)", event);
185 c56ccc84 2022-07-05 op conn_free(c);
187 b542d800 2021-11-20 op if (--conn == 0) {
188 3376f40a 2021-11-25 op log_debug("scheduling ssh termination (%llds)",
189 ca125bba 2021-11-21 op (long long)timeout.tv_sec);
190 85b8fe26 2021-11-25 op if (timeout.tv_sec != 0) {
191 85b8fe26 2021-11-25 op evtimer_set(&timeoutev, killing_time, NULL);
192 85b8fe26 2021-11-25 op evtimer_add(&timeoutev, &timeout);
198 b542d800 2021-11-20 op connect_to_ssh(void)
200 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
201 b542d800 2021-11-20 op int r, saved_errno, sock;
202 1a869946 2021-11-23 op const char *cause;
204 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
205 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
206 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
208 1a869946 2021-11-23 op r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
209 c56ccc84 2022-07-05 op if (r != 0) {
210 c56ccc84 2022-07-05 op log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
211 1a869946 2021-11-23 op ssh_host, ssh_port, gai_strerror(r));
215 b542d800 2021-11-20 op for (res = res0; res; res = res->ai_next) {
216 b542d800 2021-11-20 op sock = socket(res->ai_family, res->ai_socktype,
217 b542d800 2021-11-20 op res->ai_protocol);
218 b542d800 2021-11-20 op if (sock == -1) {
219 b542d800 2021-11-20 op cause = "socket";
223 b542d800 2021-11-20 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
224 b542d800 2021-11-20 op cause = "connect";
225 b542d800 2021-11-20 op saved_errno = errno;
227 b542d800 2021-11-20 op errno = saved_errno;
235 b542d800 2021-11-20 op if (sock == -1)
236 3376f40a 2021-11-25 op log_warn("%s", cause);
238 b542d800 2021-11-20 op freeaddrinfo(res0);
243 1b3b9dc3 2021-11-23 op try_to_connect(int fd, short event, void *d)
245 1b3b9dc3 2021-11-23 op struct conn *c = d;
247 1b3b9dc3 2021-11-23 op /* ssh may die in the meantime */
248 1b3b9dc3 2021-11-23 op if (ssh_pid == -1) {
249 1b3b9dc3 2021-11-23 op close(c->source);
250 1b3b9dc3 2021-11-23 op c->source = -1;
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 1b3b9dc3 2021-11-23 op close(c->source);
262 1b3b9dc3 2021-11-23 op c->source = -1;
266 1b3b9dc3 2021-11-23 op evtimer_set(&c->waitev, try_to_connect, c);
267 1b3b9dc3 2021-11-23 op evtimer_add(&c->waitev, &c->retry);
271 53abf42e 2022-07-05 op log_info("connected!");
273 1b3b9dc3 2021-11-23 op c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
274 1b3b9dc3 2021-11-23 op c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
275 c56ccc84 2022-07-05 op if (c->sourcebev == NULL || c->tobev == NULL) {
276 c56ccc84 2022-07-05 op log_warn("bufferevent_new");
277 c56ccc84 2022-07-05 op conn_free(c);
281 1b3b9dc3 2021-11-23 op bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
282 1b3b9dc3 2021-11-23 op bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
286 b542d800 2021-11-20 op do_accept(int fd, short event, void *data)
288 82c452fa 2022-07-05 op struct conn *c;
291 3376f40a 2021-11-25 op log_debug("incoming connection");
293 b542d800 2021-11-20 op if (evtimer_pending(&timeoutev, NULL))
294 b542d800 2021-11-20 op evtimer_del(&timeoutev);
296 b542d800 2021-11-20 op if ((s = accept(fd, NULL, 0)) == -1)
297 3376f40a 2021-11-25 op fatal("accept");
301 b542d800 2021-11-20 op if (ssh_pid == -1)
304 82c452fa 2022-07-05 op if ((c = calloc(1, sizeof(*c))) == NULL) {
305 82c452fa 2022-07-05 op log_warn("calloc");
310 82c452fa 2022-07-05 op c->source = s;
311 82c452fa 2022-07-05 op c->retry.tv_sec = BACKOFF;
312 82c452fa 2022-07-05 op evtimer_set(&c->waitev, try_to_connect, c);
313 82c452fa 2022-07-05 op evtimer_add(&c->waitev, &c->retry);
316 1a869946 2021-11-23 op static const char *
317 1a869946 2021-11-23 op copysec(const char *s, char *d, size_t len)
319 1a869946 2021-11-23 op const char *c;
321 1a869946 2021-11-23 op if ((c = strchr(s, ':')) == NULL)
323 1a869946 2021-11-23 op if ((size_t)(c - s) >= len-1)
325 1a869946 2021-11-23 op memset(d, 0, len);
326 1a869946 2021-11-23 op memcpy(d, s, c - s);
331 b542d800 2021-11-20 op bind_socket(void)
333 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
334 4a797971 2021-11-25 op int v, r, saved_errno;
335 b542d800 2021-11-20 op char host[64];
336 b542d800 2021-11-20 op const char *c, *h, *port, *cause;
338 b542d800 2021-11-20 op if ((c = strchr(addr, ':')) == NULL) {
342 1a869946 2021-11-23 op if ((c = copysec(addr, host, sizeof(host))) == NULL)
343 3376f40a 2021-11-25 op fatalx("name too long: %s", addr);
349 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
350 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
351 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
352 b542d800 2021-11-20 op hints.ai_flags = AI_PASSIVE;
354 b542d800 2021-11-20 op r = getaddrinfo(h, port, &hints, &res0);
356 3376f40a 2021-11-25 op fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
358 b542d800 2021-11-20 op for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
359 b542d800 2021-11-20 op socks[nsock] = socket(res->ai_family, res->ai_socktype,
360 b542d800 2021-11-20 op res->ai_protocol);
361 b542d800 2021-11-20 op if (socks[nsock] == -1) {
362 b542d800 2021-11-20 op cause = "socket";
366 b542d800 2021-11-20 op if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
367 b542d800 2021-11-20 op cause = "bind";
368 b542d800 2021-11-20 op saved_errno = errno;
369 b542d800 2021-11-20 op close(socks[nsock]);
370 b542d800 2021-11-20 op errno = saved_errno;
375 4a797971 2021-11-25 op if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
376 4a797971 2021-11-25 op sizeof(v)) == -1)
377 4a797971 2021-11-25 op fatal("setsockopt(SO_REUSEADDR)");
380 4a797971 2021-11-25 op if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
381 4a797971 2021-11-25 op sizeof(v)) == -1)
382 4a797971 2021-11-25 op fatal("setsockopt(SO_REUSEPORT)");
384 b542d800 2021-11-20 op listen(socks[nsock], 5);
388 b542d800 2021-11-20 op if (nsock == 0)
389 3376f40a 2021-11-25 op fatal("%s", cause);
391 b542d800 2021-11-20 op freeaddrinfo(res0);
395 1a869946 2021-11-23 op parse_tflag(void)
397 1a869946 2021-11-23 op const char *c;
399 1d9a178e 2022-07-05 op if (isdigit((unsigned char)*ssh_tflag)) {
400 1a869946 2021-11-23 op strlcpy(ssh_host, "localhost", sizeof(ssh_host));
401 79313907 2021-11-23 op if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
406 79313907 2021-11-23 op if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
408 1a869946 2021-11-23 op if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
413 3376f40a 2021-11-25 op fatal("wrong value for -B");
416 9ac6c545 2021-11-23 op static void __dead
419 3376f40a 2021-11-25 op fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
420 9ac6c545 2021-11-23 op " destination\n", getprogname());
425 b542d800 2021-11-20 op main(int argc, char **argv)
427 eb1810c9 2022-07-05 op int ch, i, fd;
428 b542d800 2021-11-20 op const char *errstr;
429 eb1810c9 2022-07-05 op struct stat sb;
432 eb1810c9 2022-07-05 op * Ensure we have fds 0-2 open so that we have no issue with
433 eb1810c9 2022-07-05 op * calling bind_socket before daemon(3).
435 eb1810c9 2022-07-05 op for (i = 0; i < 3; ++i) {
436 eb1810c9 2022-07-05 op if (fstat(i, &sb) == -1) {
437 eb1810c9 2022-07-05 op if ((fd = open("/dev/null", O_RDWR)) != -1) {
438 eb1810c9 2022-07-05 op if (dup2(fd, i) == -1)
447 3376f40a 2021-11-25 op log_init(1, LOG_DAEMON);
448 3376f40a 2021-11-25 op log_setverbose(1);
450 3376f40a 2021-11-25 op while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
451 b542d800 2021-11-20 op switch (ch) {
453 79313907 2021-11-23 op ssh_tflag = optarg;
454 1a869946 2021-11-23 op parse_tflag();
457 b542d800 2021-11-20 op addr = optarg;
463 85b8fe26 2021-11-25 op timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
464 b542d800 2021-11-20 op if (errstr != NULL)
465 3376f40a 2021-11-25 op fatalx("timeout is %s: %s", errstr, optarg);
474 b542d800 2021-11-20 op argc -= optind;
475 b542d800 2021-11-20 op argv += optind;
477 79313907 2021-11-23 op if (argc != 1 || addr == NULL || ssh_tflag == NULL)
480 b542d800 2021-11-20 op ssh_dest = argv[0];
482 eb1810c9 2022-07-05 op bind_socket();
484 3376f40a 2021-11-25 op log_init(debug, LOG_DAEMON);
485 3376f40a 2021-11-25 op log_setverbose(verbose);
488 3376f40a 2021-11-25 op daemon(1, 0);
490 b542d800 2021-11-20 op signal(SIGPIPE, SIG_IGN);
492 b542d800 2021-11-20 op event_init();
494 b542d800 2021-11-20 op /* initialize the timer */
495 b542d800 2021-11-20 op evtimer_set(&timeoutev, killing_time, NULL);
497 329392cb 2022-07-05 op signal_set(&sighupev, SIGHUP, sig_handler, NULL);
498 329392cb 2022-07-05 op signal_set(&sigintev, SIGINT, sig_handler, NULL);
499 329392cb 2022-07-05 op signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
500 329392cb 2022-07-05 op signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
501 047fe0f8 2021-11-27 op #ifdef SIGINFO
502 329392cb 2022-07-05 op signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
504 329392cb 2022-07-05 op signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
507 b542d800 2021-11-20 op signal_add(&sighupev, NULL);
508 b542d800 2021-11-20 op signal_add(&sigintev, NULL);
509 b542d800 2021-11-20 op signal_add(&sigtermev, NULL);
510 b542d800 2021-11-20 op signal_add(&sigchldev, NULL);
511 b542d800 2021-11-20 op signal_add(&siginfoev, NULL);
513 b542d800 2021-11-20 op for (i = 0; i < nsock; ++i) {
514 b542d800 2021-11-20 op event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
515 b542d800 2021-11-20 op do_accept, NULL);
516 b542d800 2021-11-20 op event_add(&sockev[i], NULL);
520 b542d800 2021-11-20 op * dns, inet: bind the socket and connect to the childs.
521 b542d800 2021-11-20 op * proc, exec: execute ssh on demand.
523 b542d800 2021-11-20 op if (pledge("stdio dns inet proc exec", NULL) == -1)
524 3376f40a 2021-11-25 op fatal("pledge");
526 3376f40a 2021-11-25 op log_info("starting");
527 b542d800 2021-11-20 op event_dispatch();
529 b542d800 2021-11-20 op if (ssh_pid != -1)
530 b542d800 2021-11-20 op kill(ssh_pid, SIGINT);