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.
17 #include <sys/types.h>
18 #include <sys/socket.h>
36 #define SSH_PATH "/usr/bin/ssh"
45 #define pledge(p, e) 0
48 const char *addr; /* our addr */
49 const char *ssh_tflag;
55 struct event sockev[MAXSOCK];
59 struct event sighupev;
60 struct event sigintev;
61 struct event sigtermev;
62 struct event sigchldev;
63 struct event siginfoev;
65 struct timeval timeout = {120, 0};
66 struct event timeoutev;
77 struct bufferevent *sourcebev;
79 struct bufferevent *tobev;
83 terminate(int fd, short event, void *data)
89 chld(int fd, short event, void *data)
93 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
100 info(int fd, short event, void *data)
102 warnx("connections: %d", conn);
108 warnx("spawning ssh...");
110 switch (ssh_pid = fork()) {
114 execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
123 killing_time(int fd, short event, void *data)
128 warnx("killing time!");
129 kill(ssh_pid, SIGTERM);
134 nopcb(struct bufferevent *bev, void *d)
140 sreadcb(struct bufferevent *bev, void *d)
144 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
148 treadcb(struct bufferevent *bev, void *d)
152 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
156 errcb(struct bufferevent *bev, short event, void *d)
160 warnx("in errcb, closing connection");
162 bufferevent_free(c->sourcebev);
163 bufferevent_free(c->tobev);
172 warnx("scheduling ssh termination (%llds)",
173 (long long)timeout.tv_sec);
174 if (timeout.tv_sec != 0) {
175 evtimer_set(&timeoutev, killing_time, NULL);
176 evtimer_add(&timeoutev, &timeout);
184 struct addrinfo hints, *res, *res0;
185 int r, saved_errno, sock;
188 memset(&hints, 0, sizeof(hints));
189 hints.ai_family = AF_UNSPEC;
190 hints.ai_socktype = SOCK_STREAM;
192 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
194 errx(1, "getaddrinfo(\"%s\", \"%s\"): %s",
195 ssh_host, ssh_port, gai_strerror(r));
197 for (res = res0; res; res = res->ai_next) {
198 sock = socket(res->ai_family, res->ai_socktype,
205 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
225 try_to_connect(int fd, short event, void *d)
229 /* ssh may die in the meantime */
237 warnx("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
238 c->ntentative, RETRIES);
240 if ((c->to = connect_to_ssh()) == -1) {
241 if (c->ntentative == RETRIES) {
248 evtimer_set(&c->waitev, try_to_connect, c);
249 evtimer_add(&c->waitev, &c->retry);
253 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
254 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
255 if (c->sourcebev == NULL || c->tobev == NULL)
256 err(1, "bufferevent_new");
257 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
258 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
262 do_accept(int fd, short event, void *data)
266 warnx("handling connection");
268 if (evtimer_pending(&timeoutev, NULL))
269 evtimer_del(&timeoutev);
271 if ((s = accept(fd, NULL, 0)) == -1)
274 if (conn == MAXCONN) {
285 for (i = 0; i < MAXCONN; ++i) {
286 if (conns[i].source != -1)
290 conns[i].ntentative = 0;
291 conns[i].retry.tv_sec = BACKOFF;
292 conns[i].retry.tv_usec = 0;
293 evtimer_set(&conns[i].waitev, try_to_connect, &conns[i]);
294 evtimer_add(&conns[i].waitev, &conns[i].retry);
300 copysec(const char *s, char *d, size_t len)
304 if ((c = strchr(s, ':')) == NULL)
306 if ((size_t)(c - s) >= len-1)
316 struct addrinfo hints, *res, *res0;
319 const char *c, *h, *port, *cause;
321 if ((c = strchr(addr, ':')) == NULL) {
325 if ((c = copysec(addr, host, sizeof(host))) == NULL)
326 errx(1, "ENAMETOOLONG");
332 memset(&hints, 0, sizeof(hints));
333 hints.ai_family = AF_UNSPEC;
334 hints.ai_socktype = SOCK_STREAM;
335 hints.ai_flags = AI_PASSIVE;
337 r = getaddrinfo(h, port, &hints, &res0);
339 errx(1, "getaddrinfo(%s): %s",
340 addr, gai_strerror(r));
342 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
343 socks[nsock] = socket(res->ai_family, res->ai_socktype,
345 if (socks[nsock] == -1) {
350 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
358 listen(socks[nsock], 5);
373 if (isdigit(*ssh_tflag)) {
374 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
375 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
380 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
382 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
387 errx(1, "wrong value for -B");
393 fprintf(stderr, "usage: %s -B sshaddr -b addr [-t timeout]"
394 " destination\n", getprogname());
399 main(int argc, char **argv)
404 while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
414 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
416 errx(1, "timeout is %s: %s", errstr, optarg);
425 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
430 for (i = 0; i < MAXCONN; ++i) {
431 conns[i].source = -1;
437 signal(SIGPIPE, SIG_IGN);
441 /* initialize the timer */
442 evtimer_set(&timeoutev, killing_time, NULL);
444 signal_set(&sighupev, SIGHUP, terminate, NULL);
445 signal_set(&sigintev, SIGINT, terminate, NULL);
446 signal_set(&sigtermev, SIGTERM, terminate, NULL);
447 signal_set(&sigchldev, SIGCHLD, chld, NULL);
448 signal_set(&siginfoev, SIGINFO, info, NULL);
450 signal_add(&sighupev, NULL);
451 signal_add(&sigintev, NULL);
452 signal_add(&sigtermev, NULL);
453 signal_add(&sigchldev, NULL);
454 signal_add(&siginfoev, NULL);
456 for (i = 0; i < nsock; ++i) {
457 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
459 event_add(&sockev[i], NULL);
463 * dns, inet: bind the socket and connect to the childs.
464 * proc, exec: execute ssh on demand.
466 if (pledge("stdio dns inet proc exec", NULL) == -1)
473 kill(ssh_pid, SIGINT);