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 #define pledge(p, e) 0
44 #define unveil(p, m) 0
47 const char *addr; /* our addr */
48 const char *ssh_tflag;
54 struct event sockev[MAXSOCK];
61 struct event sighupev;
62 struct event sigintev;
63 struct event sigtermev;
64 struct event sigchldev;
65 struct event siginfoev;
67 struct timeval timeout = {600, 0}; /* 10 minutes */
68 struct event timeoutev;
79 struct bufferevent *sourcebev;
81 struct bufferevent *tobev;
85 sig_handler(int sig, short event, void *data)
97 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
106 log_info("connections: %d", conn);
113 log_debug("spawning ssh");
115 switch (ssh_pid = fork()) {
120 execl(SSH_PROG, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
129 conn_free(struct conn *c)
131 if (c->sourcebev != NULL)
132 bufferevent_free(c->sourcebev);
133 if (c->tobev != NULL)
134 bufferevent_free(c->tobev);
136 if (evtimer_pending(&c->waitev, NULL))
137 evtimer_del(&c->waitev);
147 killing_time(int fd, short event, void *data)
152 log_debug("timeout expired, killing ssh (%d)", ssh_pid);
153 kill(ssh_pid, SIGTERM);
158 nopcb(struct bufferevent *bev, void *d)
164 sreadcb(struct bufferevent *bev, void *d)
168 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
172 treadcb(struct bufferevent *bev, void *d)
176 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
180 errcb(struct bufferevent *bev, short event, void *d)
184 log_info("closing connection (event=%x)", event);
189 log_debug("scheduling ssh termination (%llds)",
190 (long long)timeout.tv_sec);
191 if (timeout.tv_sec != 0) {
192 evtimer_set(&timeoutev, killing_time, NULL);
193 evtimer_add(&timeoutev, &timeout);
201 struct addrinfo hints, *res, *res0;
202 int r, saved_errno, sock;
205 memset(&hints, 0, sizeof(hints));
206 hints.ai_family = AF_UNSPEC;
207 hints.ai_socktype = SOCK_STREAM;
209 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
211 log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
212 ssh_host, ssh_port, gai_strerror(r));
216 for (res = res0; res; res = res->ai_next) {
217 sock = socket(res->ai_family, res->ai_socktype,
224 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
237 log_warn("%s", cause);
244 try_to_connect(int fd, short event, void *d)
248 /* ssh may have died in the meantime */
255 log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
256 c->ntentative, RETRIES);
258 if ((c->to = connect_to_ssh()) == -1) {
259 if (c->ntentative == RETRIES) {
260 log_warnx("giving up connecting");
265 evtimer_set(&c->waitev, try_to_connect, c);
266 evtimer_add(&c->waitev, &c->retry);
270 log_info("connected!");
272 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
273 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
274 if (c->sourcebev == NULL || c->tobev == NULL) {
275 log_warn("bufferevent_new");
280 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
281 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
285 do_accept(int fd, short event, void *data)
290 log_debug("incoming connection");
292 if ((s = accept(fd, NULL, 0)) == -1) {
297 if (ssh_pid == -1 && spawn_ssh() == -1) {
302 if ((c = calloc(1, sizeof(*c))) == NULL) {
309 if (evtimer_pending(&timeoutev, NULL))
310 evtimer_del(&timeoutev);
314 c->retry.tv_sec = BACKOFF;
315 evtimer_set(&c->waitev, try_to_connect, c);
316 evtimer_add(&c->waitev, &c->retry);
320 copysec(const char *s, char *d, size_t len)
324 if ((c = strchr(s, ':')) == NULL)
326 if ((size_t)(c - s) >= len-1)
336 struct addrinfo hints, *res, *res0;
337 int v, r, saved_errno;
339 const char *c, *h, *port, *cause;
341 if ((c = strchr(addr, ':')) == NULL) {
345 if ((c = copysec(addr, host, sizeof(host))) == NULL)
346 fatalx("name too long: %s", addr);
352 memset(&hints, 0, sizeof(hints));
353 hints.ai_family = AF_UNSPEC;
354 hints.ai_socktype = SOCK_STREAM;
355 hints.ai_flags = AI_PASSIVE;
357 r = getaddrinfo(h, port, &hints, &res0);
359 fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
361 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
362 socks[nsock] = socket(res->ai_family, res->ai_socktype,
364 if (socks[nsock] == -1) {
369 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
378 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
380 fatal("setsockopt(SO_REUSEADDR)");
383 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
385 fatal("setsockopt(SO_REUSEPORT)");
387 listen(socks[nsock], 5);
402 if (isdigit((unsigned char)*ssh_tflag)) {
403 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
404 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
409 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
411 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
416 fatalx("wrong value for -B");
422 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
423 " destination\n", getprogname());
428 main(int argc, char **argv)
435 * Ensure we have fds 0-2 open so that we have no issue with
436 * calling bind_socket before daemon(3).
438 for (i = 0; i < 3; ++i) {
439 if (fstat(i, &sb) == -1) {
440 if ((fd = open("/dev/null", O_RDWR)) != -1) {
441 if (dup2(fd, i) == -1)
450 log_init(1, LOG_DAEMON);
453 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
466 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
468 fatalx("timeout is %s: %s", errstr, optarg);
480 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
487 log_init(debug, LOG_DAEMON);
488 log_setverbose(verbose);
493 signal(SIGPIPE, SIG_IGN);
497 /* initialize the timer */
498 evtimer_set(&timeoutev, killing_time, NULL);
500 signal_set(&sighupev, SIGHUP, sig_handler, NULL);
501 signal_set(&sigintev, SIGINT, sig_handler, NULL);
502 signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
503 signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
505 signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
507 signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
510 signal_add(&sighupev, NULL);
511 signal_add(&sigintev, NULL);
512 signal_add(&sigtermev, NULL);
513 signal_add(&sigchldev, NULL);
514 signal_add(&siginfoev, NULL);
516 for (i = 0; i < nsock; ++i) {
517 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
519 event_add(&sockev[i], NULL);
522 if (unveil(SSH_PROG, "x") == -1)
523 fatal("unveil(%s)", SSH_PROG);
526 * dns, inet: bind the socket and connect to the childs.
527 * proc, exec: execute ssh on demand.
529 if (pledge("stdio dns inet proc exec", NULL) == -1)
532 log_info("starting");
536 kill(ssh_pid, SIGINT);