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.
17 #include <sys/types.h>
19 #include <sys/socket.h>
38 #define SSH_PATH "/usr/bin/ssh"
46 #define pledge(p, e) 0
47 #define unveil(p, m) 0
50 const char *addr; /* our addr */
51 const char *ssh_tflag;
57 struct event sockev[MAXSOCK];
64 struct event sighupev;
65 struct event sigintev;
66 struct event sigtermev;
67 struct event sigchldev;
68 struct event siginfoev;
70 struct timeval timeout = {600, 0}; /* 10 minutes */
71 struct event timeoutev;
82 struct bufferevent *sourcebev;
84 struct bufferevent *tobev;
88 sig_handler(int sig, short event, void *data)
100 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
109 log_info("connections: %d", conn);
116 log_debug("spawning ssh");
118 switch (ssh_pid = fork()) {
123 execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
132 conn_free(struct conn *c)
134 if (c->sourcebev != NULL)
135 bufferevent_free(c->sourcebev);
136 if (c->tobev != NULL)
137 bufferevent_free(c->tobev);
139 if (evtimer_pending(&c->waitev, NULL))
140 evtimer_del(&c->waitev);
150 killing_time(int fd, short event, void *data)
155 log_debug("timeout expired, killing ssh (%d)", ssh_pid);
156 kill(ssh_pid, SIGTERM);
161 nopcb(struct bufferevent *bev, void *d)
167 sreadcb(struct bufferevent *bev, void *d)
171 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
175 treadcb(struct bufferevent *bev, void *d)
179 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
183 errcb(struct bufferevent *bev, short event, void *d)
187 log_info("closing connection (event=%x)", event);
192 log_debug("scheduling ssh termination (%llds)",
193 (long long)timeout.tv_sec);
194 if (timeout.tv_sec != 0) {
195 evtimer_set(&timeoutev, killing_time, NULL);
196 evtimer_add(&timeoutev, &timeout);
204 struct addrinfo hints, *res, *res0;
205 int r, saved_errno, sock;
208 memset(&hints, 0, sizeof(hints));
209 hints.ai_family = AF_UNSPEC;
210 hints.ai_socktype = SOCK_STREAM;
212 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
214 log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
215 ssh_host, ssh_port, gai_strerror(r));
219 for (res = res0; res; res = res->ai_next) {
220 sock = socket(res->ai_family, res->ai_socktype,
227 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
240 log_warn("%s", cause);
247 try_to_connect(int fd, short event, void *d)
251 /* ssh may have died in the meantime */
258 log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
259 c->ntentative, RETRIES);
261 if ((c->to = connect_to_ssh()) == -1) {
262 if (c->ntentative == RETRIES) {
263 log_warnx("giving up connecting");
268 evtimer_set(&c->waitev, try_to_connect, c);
269 evtimer_add(&c->waitev, &c->retry);
273 log_info("connected!");
275 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
276 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
277 if (c->sourcebev == NULL || c->tobev == NULL) {
278 log_warn("bufferevent_new");
283 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
284 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
288 do_accept(int fd, short event, void *data)
293 log_debug("incoming connection");
295 if ((s = accept(fd, NULL, 0)) == -1) {
300 if (ssh_pid == -1 && spawn_ssh() == -1) {
305 if ((c = calloc(1, sizeof(*c))) == NULL) {
312 if (evtimer_pending(&timeoutev, NULL))
313 evtimer_del(&timeoutev);
317 c->retry.tv_sec = BACKOFF;
318 evtimer_set(&c->waitev, try_to_connect, c);
319 evtimer_add(&c->waitev, &c->retry);
323 copysec(const char *s, char *d, size_t len)
327 if ((c = strchr(s, ':')) == NULL)
329 if ((size_t)(c - s) >= len-1)
339 struct addrinfo hints, *res, *res0;
340 int v, r, saved_errno;
342 const char *c, *h, *port, *cause;
344 if ((c = strchr(addr, ':')) == NULL) {
348 if ((c = copysec(addr, host, sizeof(host))) == NULL)
349 fatalx("name too long: %s", addr);
355 memset(&hints, 0, sizeof(hints));
356 hints.ai_family = AF_UNSPEC;
357 hints.ai_socktype = SOCK_STREAM;
358 hints.ai_flags = AI_PASSIVE;
360 r = getaddrinfo(h, port, &hints, &res0);
362 fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
364 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
365 socks[nsock] = socket(res->ai_family, res->ai_socktype,
367 if (socks[nsock] == -1) {
372 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
381 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
383 fatal("setsockopt(SO_REUSEADDR)");
386 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
388 fatal("setsockopt(SO_REUSEPORT)");
390 listen(socks[nsock], 5);
405 if (isdigit((unsigned char)*ssh_tflag)) {
406 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
407 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
412 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
414 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
419 fatalx("wrong value for -B");
425 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
426 " destination\n", getprogname());
431 main(int argc, char **argv)
438 * Ensure we have fds 0-2 open so that we have no issue with
439 * calling bind_socket before daemon(3).
441 for (i = 0; i < 3; ++i) {
442 if (fstat(i, &sb) == -1) {
443 if ((fd = open("/dev/null", O_RDWR)) != -1) {
444 if (dup2(fd, i) == -1)
453 log_init(1, LOG_DAEMON);
456 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
469 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
471 fatalx("timeout is %s: %s", errstr, optarg);
483 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
490 log_init(debug, LOG_DAEMON);
491 log_setverbose(verbose);
496 signal(SIGPIPE, SIG_IGN);
500 /* initialize the timer */
501 evtimer_set(&timeoutev, killing_time, NULL);
503 signal_set(&sighupev, SIGHUP, sig_handler, NULL);
504 signal_set(&sigintev, SIGINT, sig_handler, NULL);
505 signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
506 signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
508 signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
510 signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
513 signal_add(&sighupev, NULL);
514 signal_add(&sigintev, NULL);
515 signal_add(&sigtermev, NULL);
516 signal_add(&sigchldev, NULL);
517 signal_add(&siginfoev, NULL);
519 for (i = 0; i < nsock; ++i) {
520 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
522 event_add(&sockev[i], NULL);
525 if (unveil(SSH_PATH, "x") == -1)
526 fatal("unveil(%s)", SSH_PATH);
529 * dns, inet: bind the socket and connect to the childs.
530 * proc, exec: execute ssh on demand.
532 if (pledge("stdio dns inet proc exec", NULL) == -1)
535 log_info("starting");
539 kill(ssh_pid, SIGINT);