2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/socket.h>
35 #define SSH_PATH "ssh"
44 #define pledge(p, e) 0
47 const char *addr; /* our addr */
48 const char *ssh_tflag;
54 struct event sockev[MAXSOCK];
58 struct event sighupev;
59 struct event sigintev;
60 struct event sigtermev;
61 struct event sigchldev;
62 struct event siginfoev;
64 struct timeval timeout = {120, 0};
65 struct event timeoutev;
76 struct bufferevent *sourcebev;
78 struct bufferevent *tobev;
82 terminate(int fd, short event, void *data)
88 chld(int fd, short event, void *data)
92 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
99 info(int fd, short event, void *data)
101 warnx("connections: %d", conn);
107 warnx("spawning ssh...");
109 switch (ssh_pid = fork()) {
113 execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
122 killing_time(int fd, short event, void *data)
127 warnx("killing time!");
128 kill(ssh_pid, SIGTERM);
133 nopcb(struct bufferevent *bev, void *d)
139 sreadcb(struct bufferevent *bev, void *d)
143 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
147 treadcb(struct bufferevent *bev, void *d)
151 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
155 errcb(struct bufferevent *bev, short event, void *d)
159 warnx("in errcb, closing connection");
161 bufferevent_free(c->sourcebev);
162 bufferevent_free(c->tobev);
171 warnx("scheduling ssh termination (%llds)",
172 (long long)timeout.tv_sec);
173 if (timeout.tv_sec != 0) {
174 evtimer_set(&timeoutev, killing_time, NULL);
175 evtimer_add(&timeoutev, &timeout);
183 struct addrinfo hints, *res, *res0;
184 int r, saved_errno, sock;
187 memset(&hints, 0, sizeof(hints));
188 hints.ai_family = AF_UNSPEC;
189 hints.ai_socktype = SOCK_STREAM;
191 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
193 errx(1, "getaddrinfo(\"%s\", \"%s\"): %s",
194 ssh_host, ssh_port, gai_strerror(r));
196 for (res = res0; res; res = res->ai_next) {
197 sock = socket(res->ai_family, res->ai_socktype,
204 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
224 try_to_connect(int fd, short event, void *d)
228 /* ssh may die in the meantime */
236 warnx("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
237 c->ntentative, RETRIES);
239 if ((c->to = connect_to_ssh()) == -1) {
240 if (c->ntentative == RETRIES) {
247 evtimer_set(&c->waitev, try_to_connect, c);
248 evtimer_add(&c->waitev, &c->retry);
252 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
253 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
254 if (c->sourcebev == NULL || c->tobev == NULL)
255 err(1, "bufferevent_new");
256 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
257 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
261 do_accept(int fd, short event, void *data)
265 warnx("handling connection");
267 if (evtimer_pending(&timeoutev, NULL))
268 evtimer_del(&timeoutev);
270 if ((s = accept(fd, NULL, 0)) == -1)
273 if (conn == MAXCONN) {
284 for (i = 0; i < MAXCONN; ++i) {
285 if (conns[i].source != -1)
289 conns[i].ntentative = 0;
290 conns[i].retry.tv_sec = BACKOFF;
291 conns[i].retry.tv_usec = 0;
292 evtimer_set(&conns[i].waitev, try_to_connect, &conns[i]);
293 evtimer_add(&conns[i].waitev, &conns[i].retry);
299 copysec(const char *s, char *d, size_t len)
303 if ((c = strchr(s, ':')) == NULL)
305 if ((size_t)(c - s) >= len-1)
315 struct addrinfo hints, *res, *res0;
318 const char *c, *h, *port, *cause;
320 if ((c = strchr(addr, ':')) == NULL) {
324 if ((c = copysec(addr, host, sizeof(host))) == NULL)
325 errx(1, "ENAMETOOLONG");
331 memset(&hints, 0, sizeof(hints));
332 hints.ai_family = AF_UNSPEC;
333 hints.ai_socktype = SOCK_STREAM;
334 hints.ai_flags = AI_PASSIVE;
336 r = getaddrinfo(h, port, &hints, &res0);
338 errx(1, "getaddrinfo(%s): %s",
339 addr, gai_strerror(r));
341 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
342 socks[nsock] = socket(res->ai_family, res->ai_socktype,
344 if (socks[nsock] == -1) {
349 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
357 listen(socks[nsock], 5);
372 if (isdigit(*ssh_tflag)) {
373 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
374 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
379 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
381 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
386 errx(1, "wrong value for -B");
392 fprintf(stderr, "usage: %s -B sshaddr -b addr [-t timeout]"
393 " destination\n", getprogname());
398 main(int argc, char **argv)
403 while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
413 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
415 errx(1, "timeout is %s: %s", errstr, optarg);
424 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
429 for (i = 0; i < MAXCONN; ++i) {
430 conns[i].source = -1;
436 signal(SIGPIPE, SIG_IGN);
440 /* initialize the timer */
441 evtimer_set(&timeoutev, killing_time, NULL);
443 signal_set(&sighupev, SIGHUP, terminate, NULL);
444 signal_set(&sigintev, SIGINT, terminate, NULL);
445 signal_set(&sigtermev, SIGTERM, terminate, NULL);
446 signal_set(&sigchldev, SIGCHLD, chld, NULL);
447 signal_set(&siginfoev, SIGINFO, info, NULL);
449 signal_add(&sighupev, NULL);
450 signal_add(&sigintev, NULL);
451 signal_add(&sigtermev, NULL);
452 signal_add(&sigchldev, NULL);
453 signal_add(&siginfoev, NULL);
455 for (i = 0; i < nsock; ++i) {
456 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
458 event_add(&sockev[i], NULL);
462 * dns, inet: bind the socket and connect to the childs.
463 * proc, exec: execute ssh on demand.
465 if (pledge("stdio dns inet proc exec", NULL) == -1)
472 kill(ssh_pid, SIGINT);