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"
37 cfe57149 2022-07-30 op #include "lstun.h"
39 7e04fdce 2021-12-01 op #define MAXSOCK 32
40 1b3b9dc3 2021-11-23 op #define BACKOFF 1
41 c7868114 2022-07-05 op #define RETRIES 16
43 b542d800 2021-11-20 op const char *addr; /* our addr */
44 79313907 2021-11-23 op const char *ssh_tflag;
45 b542d800 2021-11-20 op const char *ssh_dest;
47 1a869946 2021-11-23 op char ssh_host[256];
48 1a869946 2021-11-23 op char ssh_port[16];
50 b542d800 2021-11-20 op struct event sockev[MAXSOCK];
51 b542d800 2021-11-20 op int socks[MAXSOCK];
57 b542d800 2021-11-20 op struct event sighupev;
58 b542d800 2021-11-20 op struct event sigintev;
59 b542d800 2021-11-20 op struct event sigtermev;
60 b542d800 2021-11-20 op struct event sigchldev;
61 b542d800 2021-11-20 op struct event siginfoev;
63 dd30c95b 2021-12-01 op struct timeval timeout = {600, 0}; /* 10 minutes */
64 b542d800 2021-11-20 op struct event timeoutev;
66 b542d800 2021-11-20 op pid_t ssh_pid = -1;
71 329392cb 2022-07-05 op sig_handler(int sig, short event, void *data)
75 329392cb 2022-07-05 op switch (sig) {
79 803370a7 2022-07-05 op log_info("quitting");
80 329392cb 2022-07-05 op event_loopbreak();
83 329392cb 2022-07-05 op if (waitpid(ssh_pid, &status, WNOHANG) == -1)
84 329392cb 2022-07-05 op fatal("waitpid");
87 329392cb 2022-07-05 op #ifdef SIGINFO
92 329392cb 2022-07-05 op log_info("connections: %d", conn);
97 b542d800 2021-11-20 op spawn_ssh(void)
99 3376f40a 2021-11-25 op log_debug("spawning ssh");
101 b542d800 2021-11-20 op switch (ssh_pid = fork()) {
103 98c3d897 2022-07-06 op log_warnx("fork");
106 9050e628 2022-07-30 op execl(SSH_PROG, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
108 3376f40a 2021-11-25 op fatal("exec");
115 cfe57149 2022-07-30 op killing_time(int fd, short event, void *data)
117 cfe57149 2022-07-30 op if (ssh_pid == -1)
120 cfe57149 2022-07-30 op log_debug("timeout expired, killing ssh (%d)", ssh_pid);
121 cfe57149 2022-07-30 op kill(ssh_pid, SIGTERM);
122 cfe57149 2022-07-30 op ssh_pid = -1;
126 c56ccc84 2022-07-05 op conn_free(struct conn *c)
128 c56ccc84 2022-07-05 op if (c->sourcebev != NULL)
129 c56ccc84 2022-07-05 op bufferevent_free(c->sourcebev);
130 c56ccc84 2022-07-05 op if (c->tobev != NULL)
131 c56ccc84 2022-07-05 op bufferevent_free(c->tobev);
133 c56ccc84 2022-07-05 op if (evtimer_pending(&c->waitev, NULL))
134 c56ccc84 2022-07-05 op evtimer_del(&c->waitev);
136 c56ccc84 2022-07-05 op close(c->source);
137 8ae8b89f 2022-07-05 op if (c->to != -1)
138 8ae8b89f 2022-07-05 op close(c->to);
142 b542d800 2021-11-20 op if (--conn == 0) {
143 3376f40a 2021-11-25 op log_debug("scheduling ssh termination (%llds)",
144 ca125bba 2021-11-21 op (long long)timeout.tv_sec);
145 85b8fe26 2021-11-25 op if (timeout.tv_sec != 0) {
146 85b8fe26 2021-11-25 op evtimer_set(&timeoutev, killing_time, NULL);
147 85b8fe26 2021-11-25 op evtimer_add(&timeoutev, &timeout);
153 b542d800 2021-11-20 op connect_to_ssh(void)
155 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
156 b542d800 2021-11-20 op int r, saved_errno, sock;
157 1a869946 2021-11-23 op const char *cause;
159 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
160 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
161 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
163 1a869946 2021-11-23 op r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
164 c56ccc84 2022-07-05 op if (r != 0) {
165 c56ccc84 2022-07-05 op log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
166 1a869946 2021-11-23 op ssh_host, ssh_port, gai_strerror(r));
170 b542d800 2021-11-20 op for (res = res0; res; res = res->ai_next) {
171 b542d800 2021-11-20 op sock = socket(res->ai_family, res->ai_socktype,
172 b542d800 2021-11-20 op res->ai_protocol);
173 b542d800 2021-11-20 op if (sock == -1) {
174 b542d800 2021-11-20 op cause = "socket";
178 b542d800 2021-11-20 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
179 b542d800 2021-11-20 op cause = "connect";
180 b542d800 2021-11-20 op saved_errno = errno;
182 b542d800 2021-11-20 op errno = saved_errno;
190 b542d800 2021-11-20 op if (sock == -1)
191 3376f40a 2021-11-25 op log_warn("%s", cause);
193 b542d800 2021-11-20 op freeaddrinfo(res0);
198 1b3b9dc3 2021-11-23 op try_to_connect(int fd, short event, void *d)
200 1b3b9dc3 2021-11-23 op struct conn *c = d;
202 ab4b8022 2022-07-06 op /* ssh may have died in the meantime */
203 1b3b9dc3 2021-11-23 op if (ssh_pid == -1) {
204 ab4b8022 2022-07-06 op conn_free(c);
208 1b3b9dc3 2021-11-23 op c->ntentative++;
209 61461d5b 2021-12-01 op log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
210 1b3b9dc3 2021-11-23 op c->ntentative, RETRIES);
212 1b3b9dc3 2021-11-23 op if ((c->to = connect_to_ssh()) == -1) {
213 1b3b9dc3 2021-11-23 op if (c->ntentative == RETRIES) {
214 3376f40a 2021-11-25 op log_warnx("giving up connecting");
215 ab4b8022 2022-07-06 op conn_free(c);
219 1b3b9dc3 2021-11-23 op evtimer_set(&c->waitev, try_to_connect, c);
220 1b3b9dc3 2021-11-23 op evtimer_add(&c->waitev, &c->retry);
224 53abf42e 2022-07-05 op log_info("connected!");
226 cfe57149 2022-07-30 op if (conn_splice(c) == -1)
227 c56ccc84 2022-07-05 op conn_free(c);
231 b542d800 2021-11-20 op do_accept(int fd, short event, void *data)
233 82c452fa 2022-07-05 op struct conn *c;
236 3376f40a 2021-11-25 op log_debug("incoming connection");
238 0828da2c 2022-07-06 op if ((s = accept(fd, NULL, 0)) == -1) {
239 0828da2c 2022-07-06 op log_warn("accept");
243 98c3d897 2022-07-06 op if (ssh_pid == -1 && spawn_ssh() == -1) {
248 82c452fa 2022-07-05 op if ((c = calloc(1, sizeof(*c))) == NULL) {
249 82c452fa 2022-07-05 op log_warn("calloc");
255 f56227b6 2022-07-06 op if (evtimer_pending(&timeoutev, NULL))
256 f56227b6 2022-07-06 op evtimer_del(&timeoutev);
258 82c452fa 2022-07-05 op c->source = s;
260 82c452fa 2022-07-05 op c->retry.tv_sec = BACKOFF;
261 82c452fa 2022-07-05 op evtimer_set(&c->waitev, try_to_connect, c);
262 82c452fa 2022-07-05 op evtimer_add(&c->waitev, &c->retry);
265 1a869946 2021-11-23 op static const char *
266 1a869946 2021-11-23 op copysec(const char *s, char *d, size_t len)
268 1a869946 2021-11-23 op const char *c;
270 1a869946 2021-11-23 op if ((c = strchr(s, ':')) == NULL)
272 1a869946 2021-11-23 op if ((size_t)(c - s) >= len-1)
274 1a869946 2021-11-23 op memset(d, 0, len);
275 1a869946 2021-11-23 op memcpy(d, s, c - s);
280 b542d800 2021-11-20 op bind_socket(void)
282 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
283 4a797971 2021-11-25 op int v, r, saved_errno;
284 b542d800 2021-11-20 op char host[64];
285 b542d800 2021-11-20 op const char *c, *h, *port, *cause;
287 b542d800 2021-11-20 op if ((c = strchr(addr, ':')) == NULL) {
288 d2d81217 2022-12-02 op h = "localhost";
291 1a869946 2021-11-23 op if ((c = copysec(addr, host, sizeof(host))) == NULL)
292 3376f40a 2021-11-25 op fatalx("name too long: %s", addr);
298 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
299 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
300 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
301 b542d800 2021-11-20 op hints.ai_flags = AI_PASSIVE;
303 b542d800 2021-11-20 op r = getaddrinfo(h, port, &hints, &res0);
305 3376f40a 2021-11-25 op fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
307 b542d800 2021-11-20 op for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
308 b542d800 2021-11-20 op socks[nsock] = socket(res->ai_family, res->ai_socktype,
309 b542d800 2021-11-20 op res->ai_protocol);
310 b542d800 2021-11-20 op if (socks[nsock] == -1) {
311 b542d800 2021-11-20 op cause = "socket";
316 4a797971 2021-11-25 op if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
317 4a797971 2021-11-25 op sizeof(v)) == -1)
318 4a797971 2021-11-25 op fatal("setsockopt(SO_REUSEADDR)");
321 4a797971 2021-11-25 op if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
322 4a797971 2021-11-25 op sizeof(v)) == -1)
323 4a797971 2021-11-25 op fatal("setsockopt(SO_REUSEPORT)");
325 e4189e65 2022-12-02 op if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
326 e4189e65 2022-12-02 op cause = "bind";
327 e4189e65 2022-12-02 op saved_errno = errno;
328 e4189e65 2022-12-02 op close(socks[nsock]);
329 e4189e65 2022-12-02 op errno = saved_errno;
333 2c4afbcc 2022-12-02 op if (listen(socks[nsock], 5) == -1)
334 2c4afbcc 2022-12-02 op fatal("listen");
338 b542d800 2021-11-20 op if (nsock == 0)
339 3376f40a 2021-11-25 op fatal("%s", cause);
341 b542d800 2021-11-20 op freeaddrinfo(res0);
345 8a8fe09b 2022-07-05 op parse_sshaddr(void)
347 1a869946 2021-11-23 op const char *c;
349 1d9a178e 2022-07-05 op if (isdigit((unsigned char)*ssh_tflag)) {
350 1a869946 2021-11-23 op strlcpy(ssh_host, "localhost", sizeof(ssh_host));
351 79313907 2021-11-23 op if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
356 79313907 2021-11-23 op if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
358 1a869946 2021-11-23 op if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
363 039c0887 2022-07-06 op fatalx("wrong value for -B");
366 9ac6c545 2021-11-23 op static void __dead
369 3376f40a 2021-11-25 op fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
370 9ac6c545 2021-11-23 op " destination\n", getprogname());
375 b542d800 2021-11-20 op main(int argc, char **argv)
377 eb1810c9 2022-07-05 op int ch, i, fd;
378 b542d800 2021-11-20 op const char *errstr;
379 eb1810c9 2022-07-05 op struct stat sb;
382 eb1810c9 2022-07-05 op * Ensure we have fds 0-2 open so that we have no issue with
383 eb1810c9 2022-07-05 op * calling bind_socket before daemon(3).
385 eb1810c9 2022-07-05 op for (i = 0; i < 3; ++i) {
386 eb1810c9 2022-07-05 op if (fstat(i, &sb) == -1) {
387 eb1810c9 2022-07-05 op if ((fd = open("/dev/null", O_RDWR)) != -1) {
388 eb1810c9 2022-07-05 op if (dup2(fd, i) == -1)
397 3376f40a 2021-11-25 op log_init(1, LOG_DAEMON);
398 3376f40a 2021-11-25 op log_setverbose(1);
400 3376f40a 2021-11-25 op while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
401 b542d800 2021-11-20 op switch (ch) {
403 79313907 2021-11-23 op ssh_tflag = optarg;
404 8a8fe09b 2022-07-05 op parse_sshaddr();
407 b542d800 2021-11-20 op addr = optarg;
413 85b8fe26 2021-11-25 op timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
414 b542d800 2021-11-20 op if (errstr != NULL)
415 3376f40a 2021-11-25 op fatalx("timeout is %s: %s", errstr, optarg);
424 b542d800 2021-11-20 op argc -= optind;
425 b542d800 2021-11-20 op argv += optind;
427 79313907 2021-11-23 op if (argc != 1 || addr == NULL || ssh_tflag == NULL)
430 b542d800 2021-11-20 op ssh_dest = argv[0];
432 eb1810c9 2022-07-05 op bind_socket();
434 3376f40a 2021-11-25 op log_init(debug, LOG_DAEMON);
435 3376f40a 2021-11-25 op log_setverbose(verbose);
438 3376f40a 2021-11-25 op daemon(1, 0);
440 b542d800 2021-11-20 op signal(SIGPIPE, SIG_IGN);
442 b542d800 2021-11-20 op event_init();
444 b542d800 2021-11-20 op /* initialize the timer */
445 b542d800 2021-11-20 op evtimer_set(&timeoutev, killing_time, NULL);
447 329392cb 2022-07-05 op signal_set(&sighupev, SIGHUP, sig_handler, NULL);
448 329392cb 2022-07-05 op signal_set(&sigintev, SIGINT, sig_handler, NULL);
449 329392cb 2022-07-05 op signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
450 329392cb 2022-07-05 op signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
451 047fe0f8 2021-11-27 op #ifdef SIGINFO
452 329392cb 2022-07-05 op signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
454 329392cb 2022-07-05 op signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
457 b542d800 2021-11-20 op signal_add(&sighupev, NULL);
458 b542d800 2021-11-20 op signal_add(&sigintev, NULL);
459 b542d800 2021-11-20 op signal_add(&sigtermev, NULL);
460 b542d800 2021-11-20 op signal_add(&sigchldev, NULL);
461 b542d800 2021-11-20 op signal_add(&siginfoev, NULL);
463 b542d800 2021-11-20 op for (i = 0; i < nsock; ++i) {
464 b542d800 2021-11-20 op event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
465 b542d800 2021-11-20 op do_accept, NULL);
466 b542d800 2021-11-20 op event_add(&sockev[i], NULL);
469 9050e628 2022-07-30 op if (unveil(SSH_PROG, "x") == -1)
470 9050e628 2022-07-30 op fatal("unveil(%s)", SSH_PROG);
473 b542d800 2021-11-20 op * dns, inet: bind the socket and connect to the childs.
474 b542d800 2021-11-20 op * proc, exec: execute ssh on demand.
476 b542d800 2021-11-20 op if (pledge("stdio dns inet proc exec", NULL) == -1)
477 3376f40a 2021-11-25 op fatal("pledge");
479 3376f40a 2021-11-25 op log_info("starting");
480 b542d800 2021-11-20 op event_dispatch();
482 b542d800 2021-11-20 op if (ssh_pid != -1)
483 b542d800 2021-11-20 op kill(ssh_pid, SIGINT);