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 <err.h>
23 b542d800 2021-11-20 op #include <errno.h>
24 b542d800 2021-11-20 op #include <event.h>
25 b542d800 2021-11-20 op #include <limits.h>
26 b542d800 2021-11-20 op #include <netdb.h>
27 b542d800 2021-11-20 op #include <signal.h>
28 b542d800 2021-11-20 op #include <stdio.h>
29 b542d800 2021-11-20 op #include <stdlib.h>
30 b542d800 2021-11-20 op #include <string.h>
31 b542d800 2021-11-20 op #include <unistd.h>
33 b542d800 2021-11-20 op #include <stdint.h>
35 b542d800 2021-11-20 op #ifndef SSH_PATH
36 b542d800 2021-11-20 op #define SSH_PATH "/usr/bin/ssh"
39 b542d800 2021-11-20 op #define MAXSOCK 4
40 b542d800 2021-11-20 op #define MAXCONN 16
41 1b3b9dc3 2021-11-23 op #define BACKOFF 1
42 1b3b9dc3 2021-11-23 op #define RETRIES 8
44 72c20227 2021-11-21 op #ifndef __OpenBSD__
45 72c20227 2021-11-21 op #define pledge(p, e) 0
48 b542d800 2021-11-20 op const char *addr; /* our addr */
49 79313907 2021-11-23 op const char *ssh_tflag;
50 b542d800 2021-11-20 op const char *ssh_dest;
52 1a869946 2021-11-23 op char ssh_host[256];
53 1a869946 2021-11-23 op char ssh_port[16];
55 b542d800 2021-11-20 op struct event sockev[MAXSOCK];
56 b542d800 2021-11-20 op int socks[MAXSOCK];
59 b542d800 2021-11-20 op struct event sighupev;
60 b542d800 2021-11-20 op struct event sigintev;
61 b542d800 2021-11-20 op struct event sigtermev;
62 b542d800 2021-11-20 op struct event sigchldev;
63 b542d800 2021-11-20 op struct event siginfoev;
65 85b8fe26 2021-11-25 op struct timeval timeout = {120, 0};
66 b542d800 2021-11-20 op struct event timeoutev;
68 b542d800 2021-11-20 op pid_t ssh_pid = -1;
73 1b3b9dc3 2021-11-23 op int ntentative;
74 1b3b9dc3 2021-11-23 op struct timeval retry;
75 1b3b9dc3 2021-11-23 op struct event waitev;
77 f0531366 2021-11-21 op struct bufferevent *sourcebev;
79 f0531366 2021-11-21 op struct bufferevent *tobev;
80 b542d800 2021-11-20 op } conns[MAXCONN];
83 b542d800 2021-11-20 op terminate(int fd, short event, void *data)
85 b542d800 2021-11-20 op event_loopbreak();
89 b542d800 2021-11-20 op chld(int fd, short event, void *data)
93 c2e630e5 2021-11-24 op if (waitpid(ssh_pid, &status, WNOHANG) == -1)
94 b542d800 2021-11-20 op err(1, "waitpid");
100 b542d800 2021-11-20 op info(int fd, short event, void *data)
102 b542d800 2021-11-20 op warnx("connections: %d", conn);
106 b542d800 2021-11-20 op spawn_ssh(void)
108 b542d800 2021-11-20 op warnx("spawning ssh...");
110 b542d800 2021-11-20 op switch (ssh_pid = fork()) {
112 b542d800 2021-11-20 op err(1, "fork");
114 79313907 2021-11-23 op execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
116 b542d800 2021-11-20 op err(1, "exec");
123 b542d800 2021-11-20 op killing_time(int fd, short event, void *data)
125 b542d800 2021-11-20 op if (ssh_pid == -1)
128 b542d800 2021-11-20 op warnx("killing time!");
129 b542d800 2021-11-20 op kill(ssh_pid, SIGTERM);
130 b542d800 2021-11-20 op ssh_pid = -1;
134 b542d800 2021-11-20 op nopcb(struct bufferevent *bev, void *d)
140 b542d800 2021-11-20 op sreadcb(struct bufferevent *bev, void *d)
142 b542d800 2021-11-20 op struct conn *c = d;
144 b542d800 2021-11-20 op bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
148 b542d800 2021-11-20 op treadcb(struct bufferevent *bev, void *d)
150 b542d800 2021-11-20 op struct conn *c = d;
152 b542d800 2021-11-20 op bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
156 b542d800 2021-11-20 op errcb(struct bufferevent *bev, short event, void *d)
158 b542d800 2021-11-20 op struct conn *c = d;
160 b542d800 2021-11-20 op warnx("in errcb, closing connection");
162 b542d800 2021-11-20 op bufferevent_free(c->sourcebev);
163 b542d800 2021-11-20 op bufferevent_free(c->tobev);
165 b542d800 2021-11-20 op close(c->source);
166 b542d800 2021-11-20 op close(c->to);
168 b542d800 2021-11-20 op c->source = -1;
171 b542d800 2021-11-20 op if (--conn == 0) {
172 ca125bba 2021-11-21 op warnx("scheduling ssh termination (%llds)",
173 ca125bba 2021-11-21 op (long long)timeout.tv_sec);
174 85b8fe26 2021-11-25 op if (timeout.tv_sec != 0) {
175 85b8fe26 2021-11-25 op evtimer_set(&timeoutev, killing_time, NULL);
176 85b8fe26 2021-11-25 op evtimer_add(&timeoutev, &timeout);
182 b542d800 2021-11-20 op connect_to_ssh(void)
184 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
185 b542d800 2021-11-20 op int r, saved_errno, sock;
186 1a869946 2021-11-23 op const char *cause;
188 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
189 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
190 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
192 1a869946 2021-11-23 op r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
194 1a869946 2021-11-23 op errx(1, "getaddrinfo(\"%s\", \"%s\"): %s",
195 1a869946 2021-11-23 op ssh_host, ssh_port, gai_strerror(r));
197 b542d800 2021-11-20 op for (res = res0; res; res = res->ai_next) {
198 b542d800 2021-11-20 op sock = socket(res->ai_family, res->ai_socktype,
199 b542d800 2021-11-20 op res->ai_protocol);
200 b542d800 2021-11-20 op if (sock == -1) {
201 b542d800 2021-11-20 op cause = "socket";
205 b542d800 2021-11-20 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
206 b542d800 2021-11-20 op cause = "connect";
207 b542d800 2021-11-20 op saved_errno = errno;
209 b542d800 2021-11-20 op errno = saved_errno;
217 b542d800 2021-11-20 op if (sock == -1)
218 048e42ec 2021-11-24 op warn("%s", cause);
220 b542d800 2021-11-20 op freeaddrinfo(res0);
225 1b3b9dc3 2021-11-23 op try_to_connect(int fd, short event, void *d)
227 1b3b9dc3 2021-11-23 op struct conn *c = d;
229 1b3b9dc3 2021-11-23 op /* ssh may die in the meantime */
230 1b3b9dc3 2021-11-23 op if (ssh_pid == -1) {
231 1b3b9dc3 2021-11-23 op close(c->source);
232 1b3b9dc3 2021-11-23 op c->source = -1;
236 1b3b9dc3 2021-11-23 op c->ntentative++;
237 1b3b9dc3 2021-11-23 op warnx("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
238 1b3b9dc3 2021-11-23 op c->ntentative, RETRIES);
240 1b3b9dc3 2021-11-23 op if ((c->to = connect_to_ssh()) == -1) {
241 1b3b9dc3 2021-11-23 op if (c->ntentative == RETRIES) {
242 1b3b9dc3 2021-11-23 op warnx("giving up");
243 1b3b9dc3 2021-11-23 op close(c->source);
244 1b3b9dc3 2021-11-23 op c->source = -1;
248 1b3b9dc3 2021-11-23 op evtimer_set(&c->waitev, try_to_connect, c);
249 1b3b9dc3 2021-11-23 op evtimer_add(&c->waitev, &c->retry);
253 1b3b9dc3 2021-11-23 op c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
254 1b3b9dc3 2021-11-23 op c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
255 1b3b9dc3 2021-11-23 op if (c->sourcebev == NULL || c->tobev == NULL)
256 1b3b9dc3 2021-11-23 op err(1, "bufferevent_new");
257 1b3b9dc3 2021-11-23 op bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
258 1b3b9dc3 2021-11-23 op bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
262 b542d800 2021-11-20 op do_accept(int fd, short event, void *data)
266 b542d800 2021-11-20 op warnx("handling connection");
268 b542d800 2021-11-20 op if (evtimer_pending(&timeoutev, NULL))
269 b542d800 2021-11-20 op evtimer_del(&timeoutev);
271 b542d800 2021-11-20 op if ((s = accept(fd, NULL, 0)) == -1)
272 b542d800 2021-11-20 op err(1, "accept");
274 b542d800 2021-11-20 op if (conn == MAXCONN) {
282 b542d800 2021-11-20 op if (ssh_pid == -1)
285 b542d800 2021-11-20 op for (i = 0; i < MAXCONN; ++i) {
286 1b3b9dc3 2021-11-23 op if (conns[i].source != -1)
289 1b3b9dc3 2021-11-23 op conns[i].source = s;
290 1b3b9dc3 2021-11-23 op conns[i].ntentative = 0;
291 1b3b9dc3 2021-11-23 op conns[i].retry.tv_sec = BACKOFF;
292 1b3b9dc3 2021-11-23 op conns[i].retry.tv_usec = 0;
293 1b3b9dc3 2021-11-23 op evtimer_set(&conns[i].waitev, try_to_connect, &conns[i]);
294 1b3b9dc3 2021-11-23 op evtimer_add(&conns[i].waitev, &conns[i].retry);
299 1a869946 2021-11-23 op static const char *
300 1a869946 2021-11-23 op copysec(const char *s, char *d, size_t len)
302 1a869946 2021-11-23 op const char *c;
304 1a869946 2021-11-23 op if ((c = strchr(s, ':')) == NULL)
306 1a869946 2021-11-23 op if ((size_t)(c - s) >= len-1)
308 1a869946 2021-11-23 op memset(d, 0, len);
309 1a869946 2021-11-23 op memcpy(d, s, c - s);
314 b542d800 2021-11-20 op bind_socket(void)
316 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
317 b542d800 2021-11-20 op int r, saved_errno;
318 b542d800 2021-11-20 op char host[64];
319 b542d800 2021-11-20 op const char *c, *h, *port, *cause;
321 b542d800 2021-11-20 op if ((c = strchr(addr, ':')) == NULL) {
325 1a869946 2021-11-23 op if ((c = copysec(addr, host, sizeof(host))) == NULL)
326 b542d800 2021-11-20 op errx(1, "ENAMETOOLONG");
332 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
333 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
334 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
335 b542d800 2021-11-20 op hints.ai_flags = AI_PASSIVE;
337 b542d800 2021-11-20 op r = getaddrinfo(h, port, &hints, &res0);
339 b542d800 2021-11-20 op errx(1, "getaddrinfo(%s): %s",
340 b542d800 2021-11-20 op addr, gai_strerror(r));
342 b542d800 2021-11-20 op for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
343 b542d800 2021-11-20 op socks[nsock] = socket(res->ai_family, res->ai_socktype,
344 b542d800 2021-11-20 op res->ai_protocol);
345 b542d800 2021-11-20 op if (socks[nsock] == -1) {
346 b542d800 2021-11-20 op cause = "socket";
350 b542d800 2021-11-20 op if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
351 b542d800 2021-11-20 op cause = "bind";
352 b542d800 2021-11-20 op saved_errno = errno;
353 b542d800 2021-11-20 op close(socks[nsock]);
354 b542d800 2021-11-20 op errno = saved_errno;
358 b542d800 2021-11-20 op listen(socks[nsock], 5);
362 b542d800 2021-11-20 op if (nsock == 0)
363 b542d800 2021-11-20 op err(1, "%s", cause);
365 b542d800 2021-11-20 op freeaddrinfo(res0);
369 1a869946 2021-11-23 op parse_tflag(void)
371 1a869946 2021-11-23 op const char *c;
373 79313907 2021-11-23 op if (isdigit(*ssh_tflag)) {
374 1a869946 2021-11-23 op strlcpy(ssh_host, "localhost", sizeof(ssh_host));
375 79313907 2021-11-23 op if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
380 79313907 2021-11-23 op if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
382 1a869946 2021-11-23 op if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
387 1a869946 2021-11-23 op errx(1, "wrong value for -B");
390 9ac6c545 2021-11-23 op static void __dead
393 9ac6c545 2021-11-23 op fprintf(stderr, "usage: %s -B sshaddr -b addr [-t timeout]"
394 9ac6c545 2021-11-23 op " destination\n", getprogname());
399 b542d800 2021-11-20 op main(int argc, char **argv)
402 b542d800 2021-11-20 op const char *errstr;
404 b542d800 2021-11-20 op while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
405 b542d800 2021-11-20 op switch (ch) {
407 79313907 2021-11-23 op ssh_tflag = optarg;
408 1a869946 2021-11-23 op parse_tflag();
411 b542d800 2021-11-20 op addr = optarg;
414 85b8fe26 2021-11-25 op timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
415 b542d800 2021-11-20 op if (errstr != NULL)
416 b542d800 2021-11-20 op errx(1, "timeout is %s: %s", errstr, optarg);
422 b542d800 2021-11-20 op argc -= optind;
423 b542d800 2021-11-20 op argv += optind;
425 79313907 2021-11-23 op if (argc != 1 || addr == NULL || ssh_tflag == NULL)
428 b542d800 2021-11-20 op ssh_dest = argv[0];
430 b542d800 2021-11-20 op for (i = 0; i < MAXCONN; ++i) {
431 b542d800 2021-11-20 op conns[i].source = -1;
432 b542d800 2021-11-20 op conns[i].to = -1;
435 b542d800 2021-11-20 op bind_socket();
437 b542d800 2021-11-20 op signal(SIGPIPE, SIG_IGN);
439 b542d800 2021-11-20 op event_init();
441 b542d800 2021-11-20 op /* initialize the timer */
442 b542d800 2021-11-20 op evtimer_set(&timeoutev, killing_time, NULL);
444 b542d800 2021-11-20 op signal_set(&sighupev, SIGHUP, terminate, NULL);
445 b542d800 2021-11-20 op signal_set(&sigintev, SIGINT, terminate, NULL);
446 b542d800 2021-11-20 op signal_set(&sigtermev, SIGTERM, terminate, NULL);
447 b542d800 2021-11-20 op signal_set(&sigchldev, SIGCHLD, chld, NULL);
448 b542d800 2021-11-20 op signal_set(&siginfoev, SIGINFO, info, NULL);
450 b542d800 2021-11-20 op signal_add(&sighupev, NULL);
451 b542d800 2021-11-20 op signal_add(&sigintev, NULL);
452 b542d800 2021-11-20 op signal_add(&sigtermev, NULL);
453 b542d800 2021-11-20 op signal_add(&sigchldev, NULL);
454 b542d800 2021-11-20 op signal_add(&siginfoev, NULL);
456 b542d800 2021-11-20 op for (i = 0; i < nsock; ++i) {
457 b542d800 2021-11-20 op event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
458 b542d800 2021-11-20 op do_accept, NULL);
459 b542d800 2021-11-20 op event_add(&sockev[i], NULL);
463 b542d800 2021-11-20 op * dns, inet: bind the socket and connect to the childs.
464 b542d800 2021-11-20 op * proc, exec: execute ssh on demand.
466 b542d800 2021-11-20 op if (pledge("stdio dns inet proc exec", NULL) == -1)
467 b542d800 2021-11-20 op err(1, "pledge");
469 b542d800 2021-11-20 op warnx("lift off!");
470 b542d800 2021-11-20 op event_dispatch();
472 b542d800 2021-11-20 op if (ssh_pid != -1)
473 b542d800 2021-11-20 op kill(ssh_pid, SIGINT);