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 90fbce40 2021-11-25 op #include "compat.h"
19 b542d800 2021-11-20 op #include <sys/types.h>
20 b542d800 2021-11-20 op #include <sys/socket.h>
21 b542d800 2021-11-20 op #include <sys/wait.h>
23 1a869946 2021-11-23 op #include <ctype.h>
24 b542d800 2021-11-20 op #include <errno.h>
25 b542d800 2021-11-20 op #include <event.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 b542d800 2021-11-20 op #include <unistd.h>
34 b542d800 2021-11-20 op #ifndef SSH_PATH
35 14151a45 2021-11-25 op #define SSH_PATH "ssh"
38 b542d800 2021-11-20 op #define MAXSOCK 4
39 b542d800 2021-11-20 op #define MAXCONN 16
40 1b3b9dc3 2021-11-23 op #define BACKOFF 1
41 1b3b9dc3 2021-11-23 op #define RETRIES 8
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];
58 b542d800 2021-11-20 op struct event sighupev;
59 b542d800 2021-11-20 op struct event sigintev;
60 b542d800 2021-11-20 op struct event sigtermev;
61 b542d800 2021-11-20 op struct event sigchldev;
62 b542d800 2021-11-20 op struct event siginfoev;
64 85b8fe26 2021-11-25 op struct timeval timeout = {120, 0};
65 b542d800 2021-11-20 op struct event timeoutev;
67 b542d800 2021-11-20 op pid_t ssh_pid = -1;
72 1b3b9dc3 2021-11-23 op int ntentative;
73 1b3b9dc3 2021-11-23 op struct timeval retry;
74 1b3b9dc3 2021-11-23 op struct event waitev;
76 f0531366 2021-11-21 op struct bufferevent *sourcebev;
78 f0531366 2021-11-21 op struct bufferevent *tobev;
79 b542d800 2021-11-20 op } conns[MAXCONN];
82 b542d800 2021-11-20 op terminate(int fd, short event, void *data)
84 b542d800 2021-11-20 op event_loopbreak();
88 b542d800 2021-11-20 op chld(int fd, short event, void *data)
92 c2e630e5 2021-11-24 op if (waitpid(ssh_pid, &status, WNOHANG) == -1)
93 b542d800 2021-11-20 op err(1, "waitpid");
99 b542d800 2021-11-20 op info(int fd, short event, void *data)
101 b542d800 2021-11-20 op warnx("connections: %d", conn);
105 b542d800 2021-11-20 op spawn_ssh(void)
107 b542d800 2021-11-20 op warnx("spawning ssh...");
109 b542d800 2021-11-20 op switch (ssh_pid = fork()) {
111 b542d800 2021-11-20 op err(1, "fork");
113 79313907 2021-11-23 op execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
115 b542d800 2021-11-20 op err(1, "exec");
122 b542d800 2021-11-20 op killing_time(int fd, short event, void *data)
124 b542d800 2021-11-20 op if (ssh_pid == -1)
127 b542d800 2021-11-20 op warnx("killing time!");
128 b542d800 2021-11-20 op kill(ssh_pid, SIGTERM);
129 b542d800 2021-11-20 op ssh_pid = -1;
133 b542d800 2021-11-20 op nopcb(struct bufferevent *bev, void *d)
139 b542d800 2021-11-20 op sreadcb(struct bufferevent *bev, void *d)
141 b542d800 2021-11-20 op struct conn *c = d;
143 b542d800 2021-11-20 op bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
147 b542d800 2021-11-20 op treadcb(struct bufferevent *bev, void *d)
149 b542d800 2021-11-20 op struct conn *c = d;
151 b542d800 2021-11-20 op bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
155 b542d800 2021-11-20 op errcb(struct bufferevent *bev, short event, void *d)
157 b542d800 2021-11-20 op struct conn *c = d;
159 b542d800 2021-11-20 op warnx("in errcb, closing connection");
161 b542d800 2021-11-20 op bufferevent_free(c->sourcebev);
162 b542d800 2021-11-20 op bufferevent_free(c->tobev);
164 b542d800 2021-11-20 op close(c->source);
165 b542d800 2021-11-20 op close(c->to);
167 b542d800 2021-11-20 op c->source = -1;
170 b542d800 2021-11-20 op if (--conn == 0) {
171 ca125bba 2021-11-21 op warnx("scheduling ssh termination (%llds)",
172 ca125bba 2021-11-21 op (long long)timeout.tv_sec);
173 85b8fe26 2021-11-25 op if (timeout.tv_sec != 0) {
174 85b8fe26 2021-11-25 op evtimer_set(&timeoutev, killing_time, NULL);
175 85b8fe26 2021-11-25 op evtimer_add(&timeoutev, &timeout);
181 b542d800 2021-11-20 op connect_to_ssh(void)
183 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
184 b542d800 2021-11-20 op int r, saved_errno, sock;
185 1a869946 2021-11-23 op const char *cause;
187 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
188 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
189 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
191 1a869946 2021-11-23 op r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
193 1a869946 2021-11-23 op errx(1, "getaddrinfo(\"%s\", \"%s\"): %s",
194 1a869946 2021-11-23 op ssh_host, ssh_port, gai_strerror(r));
196 b542d800 2021-11-20 op for (res = res0; res; res = res->ai_next) {
197 b542d800 2021-11-20 op sock = socket(res->ai_family, res->ai_socktype,
198 b542d800 2021-11-20 op res->ai_protocol);
199 b542d800 2021-11-20 op if (sock == -1) {
200 b542d800 2021-11-20 op cause = "socket";
204 b542d800 2021-11-20 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
205 b542d800 2021-11-20 op cause = "connect";
206 b542d800 2021-11-20 op saved_errno = errno;
208 b542d800 2021-11-20 op errno = saved_errno;
216 b542d800 2021-11-20 op if (sock == -1)
217 048e42ec 2021-11-24 op warn("%s", cause);
219 b542d800 2021-11-20 op freeaddrinfo(res0);
224 1b3b9dc3 2021-11-23 op try_to_connect(int fd, short event, void *d)
226 1b3b9dc3 2021-11-23 op struct conn *c = d;
228 1b3b9dc3 2021-11-23 op /* ssh may die in the meantime */
229 1b3b9dc3 2021-11-23 op if (ssh_pid == -1) {
230 1b3b9dc3 2021-11-23 op close(c->source);
231 1b3b9dc3 2021-11-23 op c->source = -1;
235 1b3b9dc3 2021-11-23 op c->ntentative++;
236 1b3b9dc3 2021-11-23 op warnx("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
237 1b3b9dc3 2021-11-23 op c->ntentative, RETRIES);
239 1b3b9dc3 2021-11-23 op if ((c->to = connect_to_ssh()) == -1) {
240 1b3b9dc3 2021-11-23 op if (c->ntentative == RETRIES) {
241 1b3b9dc3 2021-11-23 op warnx("giving up");
242 1b3b9dc3 2021-11-23 op close(c->source);
243 1b3b9dc3 2021-11-23 op c->source = -1;
247 1b3b9dc3 2021-11-23 op evtimer_set(&c->waitev, try_to_connect, c);
248 1b3b9dc3 2021-11-23 op evtimer_add(&c->waitev, &c->retry);
252 1b3b9dc3 2021-11-23 op c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
253 1b3b9dc3 2021-11-23 op c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
254 1b3b9dc3 2021-11-23 op if (c->sourcebev == NULL || c->tobev == NULL)
255 1b3b9dc3 2021-11-23 op err(1, "bufferevent_new");
256 1b3b9dc3 2021-11-23 op bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
257 1b3b9dc3 2021-11-23 op bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
261 b542d800 2021-11-20 op do_accept(int fd, short event, void *data)
265 b542d800 2021-11-20 op warnx("handling connection");
267 b542d800 2021-11-20 op if (evtimer_pending(&timeoutev, NULL))
268 b542d800 2021-11-20 op evtimer_del(&timeoutev);
270 b542d800 2021-11-20 op if ((s = accept(fd, NULL, 0)) == -1)
271 b542d800 2021-11-20 op err(1, "accept");
273 b542d800 2021-11-20 op if (conn == MAXCONN) {
281 b542d800 2021-11-20 op if (ssh_pid == -1)
284 b542d800 2021-11-20 op for (i = 0; i < MAXCONN; ++i) {
285 1b3b9dc3 2021-11-23 op if (conns[i].source != -1)
288 1b3b9dc3 2021-11-23 op conns[i].source = s;
289 1b3b9dc3 2021-11-23 op conns[i].ntentative = 0;
290 1b3b9dc3 2021-11-23 op conns[i].retry.tv_sec = BACKOFF;
291 1b3b9dc3 2021-11-23 op conns[i].retry.tv_usec = 0;
292 1b3b9dc3 2021-11-23 op evtimer_set(&conns[i].waitev, try_to_connect, &conns[i]);
293 1b3b9dc3 2021-11-23 op evtimer_add(&conns[i].waitev, &conns[i].retry);
298 1a869946 2021-11-23 op static const char *
299 1a869946 2021-11-23 op copysec(const char *s, char *d, size_t len)
301 1a869946 2021-11-23 op const char *c;
303 1a869946 2021-11-23 op if ((c = strchr(s, ':')) == NULL)
305 1a869946 2021-11-23 op if ((size_t)(c - s) >= len-1)
307 1a869946 2021-11-23 op memset(d, 0, len);
308 1a869946 2021-11-23 op memcpy(d, s, c - s);
313 b542d800 2021-11-20 op bind_socket(void)
315 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
316 b542d800 2021-11-20 op int r, saved_errno;
317 b542d800 2021-11-20 op char host[64];
318 b542d800 2021-11-20 op const char *c, *h, *port, *cause;
320 b542d800 2021-11-20 op if ((c = strchr(addr, ':')) == NULL) {
324 1a869946 2021-11-23 op if ((c = copysec(addr, host, sizeof(host))) == NULL)
325 b542d800 2021-11-20 op errx(1, "ENAMETOOLONG");
331 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
332 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
333 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
334 b542d800 2021-11-20 op hints.ai_flags = AI_PASSIVE;
336 b542d800 2021-11-20 op r = getaddrinfo(h, port, &hints, &res0);
338 b542d800 2021-11-20 op errx(1, "getaddrinfo(%s): %s",
339 b542d800 2021-11-20 op addr, gai_strerror(r));
341 b542d800 2021-11-20 op for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
342 b542d800 2021-11-20 op socks[nsock] = socket(res->ai_family, res->ai_socktype,
343 b542d800 2021-11-20 op res->ai_protocol);
344 b542d800 2021-11-20 op if (socks[nsock] == -1) {
345 b542d800 2021-11-20 op cause = "socket";
349 b542d800 2021-11-20 op if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
350 b542d800 2021-11-20 op cause = "bind";
351 b542d800 2021-11-20 op saved_errno = errno;
352 b542d800 2021-11-20 op close(socks[nsock]);
353 b542d800 2021-11-20 op errno = saved_errno;
357 b542d800 2021-11-20 op listen(socks[nsock], 5);
361 b542d800 2021-11-20 op if (nsock == 0)
362 b542d800 2021-11-20 op err(1, "%s", cause);
364 b542d800 2021-11-20 op freeaddrinfo(res0);
368 1a869946 2021-11-23 op parse_tflag(void)
370 1a869946 2021-11-23 op const char *c;
372 79313907 2021-11-23 op if (isdigit(*ssh_tflag)) {
373 1a869946 2021-11-23 op strlcpy(ssh_host, "localhost", sizeof(ssh_host));
374 79313907 2021-11-23 op if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
379 79313907 2021-11-23 op if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
381 1a869946 2021-11-23 op if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
386 1a869946 2021-11-23 op errx(1, "wrong value for -B");
389 9ac6c545 2021-11-23 op static void __dead
392 9ac6c545 2021-11-23 op fprintf(stderr, "usage: %s -B sshaddr -b addr [-t timeout]"
393 9ac6c545 2021-11-23 op " destination\n", getprogname());
398 b542d800 2021-11-20 op main(int argc, char **argv)
401 b542d800 2021-11-20 op const char *errstr;
403 b542d800 2021-11-20 op while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
404 b542d800 2021-11-20 op switch (ch) {
406 79313907 2021-11-23 op ssh_tflag = optarg;
407 1a869946 2021-11-23 op parse_tflag();
410 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 b542d800 2021-11-20 op errx(1, "timeout is %s: %s", errstr, optarg);
421 b542d800 2021-11-20 op argc -= optind;
422 b542d800 2021-11-20 op argv += optind;
424 79313907 2021-11-23 op if (argc != 1 || addr == NULL || ssh_tflag == NULL)
427 b542d800 2021-11-20 op ssh_dest = argv[0];
429 b542d800 2021-11-20 op for (i = 0; i < MAXCONN; ++i) {
430 b542d800 2021-11-20 op conns[i].source = -1;
431 b542d800 2021-11-20 op conns[i].to = -1;
434 b542d800 2021-11-20 op bind_socket();
436 b542d800 2021-11-20 op signal(SIGPIPE, SIG_IGN);
438 b542d800 2021-11-20 op event_init();
440 b542d800 2021-11-20 op /* initialize the timer */
441 b542d800 2021-11-20 op evtimer_set(&timeoutev, killing_time, NULL);
443 b542d800 2021-11-20 op signal_set(&sighupev, SIGHUP, terminate, NULL);
444 b542d800 2021-11-20 op signal_set(&sigintev, SIGINT, terminate, NULL);
445 b542d800 2021-11-20 op signal_set(&sigtermev, SIGTERM, terminate, NULL);
446 b542d800 2021-11-20 op signal_set(&sigchldev, SIGCHLD, chld, NULL);
447 b542d800 2021-11-20 op signal_set(&siginfoev, SIGINFO, info, NULL);
449 b542d800 2021-11-20 op signal_add(&sighupev, NULL);
450 b542d800 2021-11-20 op signal_add(&sigintev, NULL);
451 b542d800 2021-11-20 op signal_add(&sigtermev, NULL);
452 b542d800 2021-11-20 op signal_add(&sigchldev, NULL);
453 b542d800 2021-11-20 op signal_add(&siginfoev, NULL);
455 b542d800 2021-11-20 op for (i = 0; i < nsock; ++i) {
456 b542d800 2021-11-20 op event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
457 b542d800 2021-11-20 op do_accept, NULL);
458 b542d800 2021-11-20 op event_add(&sockev[i], NULL);
462 b542d800 2021-11-20 op * dns, inet: bind the socket and connect to the childs.
463 b542d800 2021-11-20 op * proc, exec: execute ssh on demand.
465 b542d800 2021-11-20 op if (pledge("stdio dns inet proc exec", NULL) == -1)
466 b542d800 2021-11-20 op err(1, "pledge");
468 b542d800 2021-11-20 op warnx("lift off!");
469 b542d800 2021-11-20 op event_dispatch();
471 b542d800 2021-11-20 op if (ssh_pid != -1)
472 b542d800 2021-11-20 op kill(ssh_pid, SIGINT);