2 b542d800 2021-11-20 op * Copyright (c) 2021 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 b542d800 2021-11-20 op #include <sys/socket.h>
19 b542d800 2021-11-20 op #include <sys/wait.h>
21 1a869946 2021-11-23 op #include <ctype.h>
22 b542d800 2021-11-20 op #include <errno.h>
23 b542d800 2021-11-20 op #include <event.h>
24 b542d800 2021-11-20 op #include <limits.h>
25 b542d800 2021-11-20 op #include <netdb.h>
26 b542d800 2021-11-20 op #include <signal.h>
27 b542d800 2021-11-20 op #include <stdio.h>
28 b542d800 2021-11-20 op #include <stdlib.h>
29 b542d800 2021-11-20 op #include <string.h>
30 3376f40a 2021-11-25 op #include <syslog.h>
31 b542d800 2021-11-20 op #include <unistd.h>
33 3376f40a 2021-11-25 op #include "log.h"
35 b542d800 2021-11-20 op #ifndef SSH_PATH
36 e783915a 2021-11-25 op #define SSH_PATH "/usr/bin/ssh"
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 72c20227 2021-11-21 op #ifndef __OpenBSD__
44 72c20227 2021-11-21 op #define pledge(p, e) 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 329392cb 2022-07-05 op event_loopbreak();
96 329392cb 2022-07-05 op if (waitpid(ssh_pid, &status, WNOHANG) == -1)
97 329392cb 2022-07-05 op fatal("waitpid");
100 329392cb 2022-07-05 op #ifdef SIGINFO
101 329392cb 2022-07-05 op case SIGINFO:
103 329392cb 2022-07-05 op case SIGUSR1:
105 329392cb 2022-07-05 op log_info("connections: %d", conn);
110 b542d800 2021-11-20 op spawn_ssh(void)
112 3376f40a 2021-11-25 op log_debug("spawning ssh");
114 b542d800 2021-11-20 op switch (ssh_pid = fork()) {
116 3376f40a 2021-11-25 op fatal("fork");
118 79313907 2021-11-23 op execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
120 3376f40a 2021-11-25 op fatal("exec");
127 b542d800 2021-11-20 op killing_time(int fd, short event, void *data)
129 b542d800 2021-11-20 op if (ssh_pid == -1)
132 3376f40a 2021-11-25 op log_debug("timeout expired, killing ssh (%d)", ssh_pid);
133 b542d800 2021-11-20 op kill(ssh_pid, SIGTERM);
134 b542d800 2021-11-20 op ssh_pid = -1;
138 b542d800 2021-11-20 op nopcb(struct bufferevent *bev, void *d)
144 b542d800 2021-11-20 op sreadcb(struct bufferevent *bev, void *d)
146 b542d800 2021-11-20 op struct conn *c = d;
148 b542d800 2021-11-20 op bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
152 b542d800 2021-11-20 op treadcb(struct bufferevent *bev, void *d)
154 b542d800 2021-11-20 op struct conn *c = d;
156 b542d800 2021-11-20 op bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
160 b542d800 2021-11-20 op errcb(struct bufferevent *bev, short event, void *d)
162 b542d800 2021-11-20 op struct conn *c = d;
164 3376f40a 2021-11-25 op log_info("closing connection (event=%x)", event);
166 b542d800 2021-11-20 op bufferevent_free(c->sourcebev);
167 b542d800 2021-11-20 op bufferevent_free(c->tobev);
169 b542d800 2021-11-20 op close(c->source);
170 b542d800 2021-11-20 op close(c->to);
174 b542d800 2021-11-20 op if (--conn == 0) {
175 3376f40a 2021-11-25 op log_debug("scheduling ssh termination (%llds)",
176 ca125bba 2021-11-21 op (long long)timeout.tv_sec);
177 85b8fe26 2021-11-25 op if (timeout.tv_sec != 0) {
178 85b8fe26 2021-11-25 op evtimer_set(&timeoutev, killing_time, NULL);
179 85b8fe26 2021-11-25 op evtimer_add(&timeoutev, &timeout);
185 b542d800 2021-11-20 op connect_to_ssh(void)
187 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
188 b542d800 2021-11-20 op int r, saved_errno, sock;
189 1a869946 2021-11-23 op const char *cause;
191 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
192 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
193 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
195 1a869946 2021-11-23 op r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
197 3376f40a 2021-11-25 op fatal("getaddrinfo(\"%s\", \"%s\"): %s",
198 1a869946 2021-11-23 op ssh_host, ssh_port, gai_strerror(r));
200 b542d800 2021-11-20 op for (res = res0; res; res = res->ai_next) {
201 b542d800 2021-11-20 op sock = socket(res->ai_family, res->ai_socktype,
202 b542d800 2021-11-20 op res->ai_protocol);
203 b542d800 2021-11-20 op if (sock == -1) {
204 b542d800 2021-11-20 op cause = "socket";
208 b542d800 2021-11-20 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
209 b542d800 2021-11-20 op cause = "connect";
210 b542d800 2021-11-20 op saved_errno = errno;
212 b542d800 2021-11-20 op errno = saved_errno;
220 b542d800 2021-11-20 op if (sock == -1)
221 3376f40a 2021-11-25 op log_warn("%s", cause);
223 b542d800 2021-11-20 op freeaddrinfo(res0);
228 1b3b9dc3 2021-11-23 op try_to_connect(int fd, short event, void *d)
230 1b3b9dc3 2021-11-23 op struct conn *c = d;
232 1b3b9dc3 2021-11-23 op /* ssh may die in the meantime */
233 1b3b9dc3 2021-11-23 op if (ssh_pid == -1) {
234 1b3b9dc3 2021-11-23 op close(c->source);
235 1b3b9dc3 2021-11-23 op c->source = -1;
239 1b3b9dc3 2021-11-23 op c->ntentative++;
240 61461d5b 2021-12-01 op log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
241 1b3b9dc3 2021-11-23 op c->ntentative, RETRIES);
243 1b3b9dc3 2021-11-23 op if ((c->to = connect_to_ssh()) == -1) {
244 1b3b9dc3 2021-11-23 op if (c->ntentative == RETRIES) {
245 3376f40a 2021-11-25 op log_warnx("giving up connecting");
246 1b3b9dc3 2021-11-23 op close(c->source);
247 1b3b9dc3 2021-11-23 op c->source = -1;
251 1b3b9dc3 2021-11-23 op evtimer_set(&c->waitev, try_to_connect, c);
252 1b3b9dc3 2021-11-23 op evtimer_add(&c->waitev, &c->retry);
256 1b3b9dc3 2021-11-23 op c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
257 1b3b9dc3 2021-11-23 op c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
258 1b3b9dc3 2021-11-23 op if (c->sourcebev == NULL || c->tobev == NULL)
259 3376f40a 2021-11-25 op fatal("bufferevent_new");
260 1b3b9dc3 2021-11-23 op bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
261 1b3b9dc3 2021-11-23 op bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
265 b542d800 2021-11-20 op do_accept(int fd, short event, void *data)
267 82c452fa 2022-07-05 op struct conn *c;
270 3376f40a 2021-11-25 op log_debug("incoming connection");
272 b542d800 2021-11-20 op if (evtimer_pending(&timeoutev, NULL))
273 b542d800 2021-11-20 op evtimer_del(&timeoutev);
275 b542d800 2021-11-20 op if ((s = accept(fd, NULL, 0)) == -1)
276 3376f40a 2021-11-25 op fatal("accept");
280 b542d800 2021-11-20 op if (ssh_pid == -1)
283 82c452fa 2022-07-05 op if ((c = calloc(1, sizeof(*c))) == NULL) {
284 82c452fa 2022-07-05 op log_warn("calloc");
289 82c452fa 2022-07-05 op c->source = s;
290 82c452fa 2022-07-05 op c->retry.tv_sec = BACKOFF;
291 82c452fa 2022-07-05 op evtimer_set(&c->waitev, try_to_connect, c);
292 82c452fa 2022-07-05 op evtimer_add(&c->waitev, &c->retry);
295 1a869946 2021-11-23 op static const char *
296 1a869946 2021-11-23 op copysec(const char *s, char *d, size_t len)
298 1a869946 2021-11-23 op const char *c;
300 1a869946 2021-11-23 op if ((c = strchr(s, ':')) == NULL)
302 1a869946 2021-11-23 op if ((size_t)(c - s) >= len-1)
304 1a869946 2021-11-23 op memset(d, 0, len);
305 1a869946 2021-11-23 op memcpy(d, s, c - s);
310 b542d800 2021-11-20 op bind_socket(void)
312 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
313 4a797971 2021-11-25 op int v, r, saved_errno;
314 b542d800 2021-11-20 op char host[64];
315 b542d800 2021-11-20 op const char *c, *h, *port, *cause;
317 b542d800 2021-11-20 op if ((c = strchr(addr, ':')) == NULL) {
321 1a869946 2021-11-23 op if ((c = copysec(addr, host, sizeof(host))) == NULL)
322 3376f40a 2021-11-25 op fatalx("name too long: %s", addr);
328 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
329 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
330 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
331 b542d800 2021-11-20 op hints.ai_flags = AI_PASSIVE;
333 b542d800 2021-11-20 op r = getaddrinfo(h, port, &hints, &res0);
335 3376f40a 2021-11-25 op fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
337 b542d800 2021-11-20 op for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
338 b542d800 2021-11-20 op socks[nsock] = socket(res->ai_family, res->ai_socktype,
339 b542d800 2021-11-20 op res->ai_protocol);
340 b542d800 2021-11-20 op if (socks[nsock] == -1) {
341 b542d800 2021-11-20 op cause = "socket";
345 b542d800 2021-11-20 op if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
346 b542d800 2021-11-20 op cause = "bind";
347 b542d800 2021-11-20 op saved_errno = errno;
348 b542d800 2021-11-20 op close(socks[nsock]);
349 b542d800 2021-11-20 op errno = saved_errno;
354 4a797971 2021-11-25 op if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
355 4a797971 2021-11-25 op sizeof(v)) == -1)
356 4a797971 2021-11-25 op fatal("setsockopt(SO_REUSEADDR)");
359 4a797971 2021-11-25 op if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
360 4a797971 2021-11-25 op sizeof(v)) == -1)
361 4a797971 2021-11-25 op fatal("setsockopt(SO_REUSEPORT)");
363 b542d800 2021-11-20 op listen(socks[nsock], 5);
367 b542d800 2021-11-20 op if (nsock == 0)
368 3376f40a 2021-11-25 op fatal("%s", cause);
370 b542d800 2021-11-20 op freeaddrinfo(res0);
374 1a869946 2021-11-23 op parse_tflag(void)
376 1a869946 2021-11-23 op const char *c;
378 1d9a178e 2022-07-05 op if (isdigit((unsigned char)*ssh_tflag)) {
379 1a869946 2021-11-23 op strlcpy(ssh_host, "localhost", sizeof(ssh_host));
380 79313907 2021-11-23 op if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
385 79313907 2021-11-23 op if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
387 1a869946 2021-11-23 op if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
392 3376f40a 2021-11-25 op fatal("wrong value for -B");
395 9ac6c545 2021-11-23 op static void __dead
398 3376f40a 2021-11-25 op fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
399 9ac6c545 2021-11-23 op " destination\n", getprogname());
404 b542d800 2021-11-20 op main(int argc, char **argv)
407 b542d800 2021-11-20 op const char *errstr;
409 3376f40a 2021-11-25 op log_init(1, LOG_DAEMON);
410 3376f40a 2021-11-25 op log_setverbose(1);
412 3376f40a 2021-11-25 op while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
413 b542d800 2021-11-20 op switch (ch) {
415 79313907 2021-11-23 op ssh_tflag = optarg;
416 1a869946 2021-11-23 op parse_tflag();
419 b542d800 2021-11-20 op addr = optarg;
425 85b8fe26 2021-11-25 op timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
426 b542d800 2021-11-20 op if (errstr != NULL)
427 3376f40a 2021-11-25 op fatalx("timeout is %s: %s", errstr, optarg);
436 b542d800 2021-11-20 op argc -= optind;
437 b542d800 2021-11-20 op argv += optind;
439 79313907 2021-11-23 op if (argc != 1 || addr == NULL || ssh_tflag == NULL)
442 b542d800 2021-11-20 op ssh_dest = argv[0];
444 3376f40a 2021-11-25 op log_init(debug, LOG_DAEMON);
445 3376f40a 2021-11-25 op log_setverbose(verbose);
448 3376f40a 2021-11-25 op daemon(1, 0);
450 b542d800 2021-11-20 op bind_socket();
452 b542d800 2021-11-20 op signal(SIGPIPE, SIG_IGN);
454 b542d800 2021-11-20 op event_init();
456 b542d800 2021-11-20 op /* initialize the timer */
457 b542d800 2021-11-20 op evtimer_set(&timeoutev, killing_time, NULL);
459 329392cb 2022-07-05 op signal_set(&sighupev, SIGHUP, sig_handler, NULL);
460 329392cb 2022-07-05 op signal_set(&sigintev, SIGINT, sig_handler, NULL);
461 329392cb 2022-07-05 op signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
462 329392cb 2022-07-05 op signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
463 047fe0f8 2021-11-27 op #ifdef SIGINFO
464 329392cb 2022-07-05 op signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
466 329392cb 2022-07-05 op signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
469 b542d800 2021-11-20 op signal_add(&sighupev, NULL);
470 b542d800 2021-11-20 op signal_add(&sigintev, NULL);
471 b542d800 2021-11-20 op signal_add(&sigtermev, NULL);
472 b542d800 2021-11-20 op signal_add(&sigchldev, NULL);
473 b542d800 2021-11-20 op signal_add(&siginfoev, NULL);
475 b542d800 2021-11-20 op for (i = 0; i < nsock; ++i) {
476 b542d800 2021-11-20 op event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
477 b542d800 2021-11-20 op do_accept, NULL);
478 b542d800 2021-11-20 op event_add(&sockev[i], NULL);
482 b542d800 2021-11-20 op * dns, inet: bind the socket and connect to the childs.
483 b542d800 2021-11-20 op * proc, exec: execute ssh on demand.
485 b542d800 2021-11-20 op if (pledge("stdio dns inet proc exec", NULL) == -1)
486 3376f40a 2021-11-25 op fatal("pledge");
488 3376f40a 2021-11-25 op log_info("starting");
489 b542d800 2021-11-20 op event_dispatch();
491 b542d800 2021-11-20 op if (ssh_pid != -1)
492 b542d800 2021-11-20 op kill(ssh_pid, SIGINT);