2 * Copyright (c) 2021, 2022 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>
21 #include <sys/socket.h>
39 #define SSH_PATH "/usr/bin/ssh"
47 #define pledge(p, e) 0
48 #define unveil(p, m) 0
51 const char *addr; /* our addr */
52 const char *ssh_tflag;
58 struct event sockev[MAXSOCK];
65 struct event sighupev;
66 struct event sigintev;
67 struct event sigtermev;
68 struct event sigchldev;
69 struct event siginfoev;
71 struct timeval timeout = {600, 0}; /* 10 minutes */
72 struct event timeoutev;
83 struct bufferevent *sourcebev;
85 struct bufferevent *tobev;
89 sig_handler(int sig, short event, void *data)
101 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
110 log_info("connections: %d", conn);
117 log_debug("spawning ssh");
119 switch (ssh_pid = fork()) {
124 execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
133 conn_free(struct conn *c)
135 if (c->sourcebev != NULL)
136 bufferevent_free(c->sourcebev);
137 if (c->tobev != NULL)
138 bufferevent_free(c->tobev);
140 if (evtimer_pending(&c->waitev, NULL))
141 evtimer_del(&c->waitev);
151 killing_time(int fd, short event, void *data)
156 log_debug("timeout expired, killing ssh (%d)", ssh_pid);
157 kill(ssh_pid, SIGTERM);
162 nopcb(struct bufferevent *bev, void *d)
168 sreadcb(struct bufferevent *bev, void *d)
172 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
176 treadcb(struct bufferevent *bev, void *d)
180 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
184 errcb(struct bufferevent *bev, short event, void *d)
188 log_info("closing connection (event=%x)", event);
193 log_debug("scheduling ssh termination (%llds)",
194 (long long)timeout.tv_sec);
195 if (timeout.tv_sec != 0) {
196 evtimer_set(&timeoutev, killing_time, NULL);
197 evtimer_add(&timeoutev, &timeout);
205 struct addrinfo hints, *res, *res0;
206 int r, saved_errno, sock;
209 memset(&hints, 0, sizeof(hints));
210 hints.ai_family = AF_UNSPEC;
211 hints.ai_socktype = SOCK_STREAM;
213 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
215 log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
216 ssh_host, ssh_port, gai_strerror(r));
220 for (res = res0; res; res = res->ai_next) {
221 sock = socket(res->ai_family, res->ai_socktype,
228 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
241 log_warn("%s", cause);
248 try_to_connect(int fd, short event, void *d)
252 /* ssh may have died in the meantime */
259 log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
260 c->ntentative, RETRIES);
262 if ((c->to = connect_to_ssh()) == -1) {
263 if (c->ntentative == RETRIES) {
264 log_warnx("giving up connecting");
269 evtimer_set(&c->waitev, try_to_connect, c);
270 evtimer_add(&c->waitev, &c->retry);
274 log_info("connected!");
276 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
277 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
278 if (c->sourcebev == NULL || c->tobev == NULL) {
279 log_warn("bufferevent_new");
284 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
285 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
289 do_accept(int fd, short event, void *data)
294 log_debug("incoming connection");
296 if ((s = accept(fd, NULL, 0)) == -1) {
301 if (ssh_pid == -1 && spawn_ssh() == -1) {
306 if ((c = calloc(1, sizeof(*c))) == NULL) {
313 if (evtimer_pending(&timeoutev, NULL))
314 evtimer_del(&timeoutev);
318 c->retry.tv_sec = BACKOFF;
319 evtimer_set(&c->waitev, try_to_connect, c);
320 evtimer_add(&c->waitev, &c->retry);
324 copysec(const char *s, char *d, size_t len)
328 if ((c = strchr(s, ':')) == NULL)
330 if ((size_t)(c - s) >= len-1)
340 struct addrinfo hints, *res, *res0;
341 int v, r, saved_errno;
343 const char *c, *h, *port, *cause;
345 if ((c = strchr(addr, ':')) == NULL) {
349 if ((c = copysec(addr, host, sizeof(host))) == NULL)
350 fatalx("name too long: %s", addr);
356 memset(&hints, 0, sizeof(hints));
357 hints.ai_family = AF_UNSPEC;
358 hints.ai_socktype = SOCK_STREAM;
359 hints.ai_flags = AI_PASSIVE;
361 r = getaddrinfo(h, port, &hints, &res0);
363 fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
365 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
366 socks[nsock] = socket(res->ai_family, res->ai_socktype,
368 if (socks[nsock] == -1) {
373 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
382 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
384 fatal("setsockopt(SO_REUSEADDR)");
387 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
389 fatal("setsockopt(SO_REUSEPORT)");
391 listen(socks[nsock], 5);
406 if (isdigit((unsigned char)*ssh_tflag)) {
407 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
408 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
413 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
415 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
420 fatalx("wrong value for -B");
426 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
427 " destination\n", getprogname());
432 main(int argc, char **argv)
439 * Ensure we have fds 0-2 open so that we have no issue with
440 * calling bind_socket before daemon(3).
442 for (i = 0; i < 3; ++i) {
443 if (fstat(i, &sb) == -1) {
444 if ((fd = open("/dev/null", O_RDWR)) != -1) {
445 if (dup2(fd, i) == -1)
454 log_init(1, LOG_DAEMON);
457 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
470 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
472 fatalx("timeout is %s: %s", errstr, optarg);
484 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
491 log_init(debug, LOG_DAEMON);
492 log_setverbose(verbose);
497 signal(SIGPIPE, SIG_IGN);
501 /* initialize the timer */
502 evtimer_set(&timeoutev, killing_time, NULL);
504 signal_set(&sighupev, SIGHUP, sig_handler, NULL);
505 signal_set(&sigintev, SIGINT, sig_handler, NULL);
506 signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
507 signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
509 signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
511 signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
514 signal_add(&sighupev, NULL);
515 signal_add(&sigintev, NULL);
516 signal_add(&sigtermev, NULL);
517 signal_add(&sigchldev, NULL);
518 signal_add(&siginfoev, NULL);
520 for (i = 0; i < nsock; ++i) {
521 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
523 event_add(&sockev[i], NULL);
526 if (unveil(SSH_PATH, "x") == -1)
527 fatal("unveil(%s)", SSH_PATH);
530 * dns, inet: bind the socket and connect to the childs.
531 * proc, exec: execute ssh on demand.
533 if (pledge("stdio dns inet proc exec", NULL) == -1)
536 log_info("starting");
540 kill(ssh_pid, SIGINT);