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>
43 const char *addr; /* our addr */
44 const char *ssh_tflag;
50 struct event sockev[MAXSOCK];
57 struct event sighupev;
58 struct event sigintev;
59 struct event sigtermev;
60 struct event sigchldev;
61 struct event siginfoev;
63 struct timeval timeout = {600, 0}; /* 10 minutes */
64 struct event timeoutev;
71 sig_handler(int sig, short event, void *data)
83 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
92 log_info("connections: %d", conn);
99 log_debug("spawning ssh");
101 switch (ssh_pid = fork()) {
106 execl(SSH_PROG, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
115 killing_time(int fd, short event, void *data)
120 log_debug("timeout expired, killing ssh (%d)", ssh_pid);
121 kill(ssh_pid, SIGTERM);
126 conn_free(struct conn *c)
128 if (c->sourcebev != NULL)
129 bufferevent_free(c->sourcebev);
130 if (c->tobev != NULL)
131 bufferevent_free(c->tobev);
133 if (evtimer_pending(&c->waitev, NULL))
134 evtimer_del(&c->waitev);
143 log_debug("scheduling ssh termination (%llds)",
144 (long long)timeout.tv_sec);
145 if (timeout.tv_sec != 0) {
146 evtimer_set(&timeoutev, killing_time, NULL);
147 evtimer_add(&timeoutev, &timeout);
155 struct addrinfo hints, *res, *res0;
156 int r, saved_errno, sock;
159 memset(&hints, 0, sizeof(hints));
160 hints.ai_family = AF_UNSPEC;
161 hints.ai_socktype = SOCK_STREAM;
163 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
165 log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
166 ssh_host, ssh_port, gai_strerror(r));
170 for (res = res0; res; res = res->ai_next) {
171 sock = socket(res->ai_family, res->ai_socktype,
178 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
191 log_warn("%s", cause);
198 try_to_connect(int fd, short event, void *d)
202 /* ssh may have died in the meantime */
209 log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
210 c->ntentative, RETRIES);
212 if ((c->to = connect_to_ssh()) == -1) {
213 if (c->ntentative == RETRIES) {
214 log_warnx("giving up connecting");
219 evtimer_set(&c->waitev, try_to_connect, c);
220 evtimer_add(&c->waitev, &c->retry);
224 log_info("connected!");
226 if (conn_splice(c) == -1)
231 do_accept(int fd, short event, void *data)
236 log_debug("incoming connection");
238 if ((s = accept(fd, NULL, 0)) == -1) {
243 if (ssh_pid == -1 && spawn_ssh() == -1) {
248 if ((c = calloc(1, sizeof(*c))) == NULL) {
255 if (evtimer_pending(&timeoutev, NULL))
256 evtimer_del(&timeoutev);
260 c->retry.tv_sec = BACKOFF;
261 evtimer_set(&c->waitev, try_to_connect, c);
262 evtimer_add(&c->waitev, &c->retry);
266 copysec(const char *s, char *d, size_t len)
270 if ((c = strchr(s, ':')) == NULL)
272 if ((size_t)(c - s) >= len-1)
282 struct addrinfo hints, *res, *res0;
283 int v, r, saved_errno;
285 const char *c, *h, *port, *cause;
287 if ((c = strchr(addr, ':')) == NULL) {
291 if ((c = copysec(addr, host, sizeof(host))) == NULL)
292 fatalx("name too long: %s", addr);
298 memset(&hints, 0, sizeof(hints));
299 hints.ai_family = AF_UNSPEC;
300 hints.ai_socktype = SOCK_STREAM;
301 hints.ai_flags = AI_PASSIVE;
303 r = getaddrinfo(h, port, &hints, &res0);
305 fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
307 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
308 socks[nsock] = socket(res->ai_family, res->ai_socktype,
310 if (socks[nsock] == -1) {
316 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
318 fatal("setsockopt(SO_REUSEADDR)");
321 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
323 fatal("setsockopt(SO_REUSEPORT)");
325 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
333 listen(socks[nsock], 5);
348 if (isdigit((unsigned char)*ssh_tflag)) {
349 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
350 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
355 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
357 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
362 fatalx("wrong value for -B");
368 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
369 " destination\n", getprogname());
374 main(int argc, char **argv)
381 * Ensure we have fds 0-2 open so that we have no issue with
382 * calling bind_socket before daemon(3).
384 for (i = 0; i < 3; ++i) {
385 if (fstat(i, &sb) == -1) {
386 if ((fd = open("/dev/null", O_RDWR)) != -1) {
387 if (dup2(fd, i) == -1)
396 log_init(1, LOG_DAEMON);
399 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
412 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
414 fatalx("timeout is %s: %s", errstr, optarg);
426 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
433 log_init(debug, LOG_DAEMON);
434 log_setverbose(verbose);
439 signal(SIGPIPE, SIG_IGN);
443 /* initialize the timer */
444 evtimer_set(&timeoutev, killing_time, NULL);
446 signal_set(&sighupev, SIGHUP, sig_handler, NULL);
447 signal_set(&sigintev, SIGINT, sig_handler, NULL);
448 signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
449 signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
451 signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
453 signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
456 signal_add(&sighupev, NULL);
457 signal_add(&sigintev, NULL);
458 signal_add(&sigtermev, NULL);
459 signal_add(&sigchldev, NULL);
460 signal_add(&siginfoev, NULL);
462 for (i = 0; i < nsock; ++i) {
463 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
465 event_add(&sockev[i], NULL);
468 if (unveil(SSH_PROG, "x") == -1)
469 fatal("unveil(%s)", SSH_PROG);
472 * dns, inet: bind the socket and connect to the childs.
473 * proc, exec: execute ssh on demand.
475 if (pledge("stdio dns inet proc exec", NULL) == -1)
478 log_info("starting");
482 kill(ssh_pid, SIGINT);