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
49 const char *addr; /* our addr */
50 const char *ssh_tflag;
56 struct event sockev[MAXSOCK];
63 struct event sighupev;
64 struct event sigintev;
65 struct event sigtermev;
66 struct event sigchldev;
67 struct event siginfoev;
69 struct timeval timeout = {600, 0}; /* 10 minutes */
70 struct event timeoutev;
81 struct bufferevent *sourcebev;
83 struct bufferevent *tobev;
87 sig_handler(int sig, short event, void *data)
98 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
107 log_info("connections: %d", conn);
114 log_debug("spawning ssh");
116 switch (ssh_pid = fork()) {
120 execl(SSH_PATH, "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);
146 killing_time(int fd, short event, void *data)
151 log_debug("timeout expired, killing ssh (%d)", ssh_pid);
152 kill(ssh_pid, SIGTERM);
157 nopcb(struct bufferevent *bev, void *d)
163 sreadcb(struct bufferevent *bev, void *d)
167 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
171 treadcb(struct bufferevent *bev, void *d)
175 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
179 errcb(struct bufferevent *bev, short event, void *d)
183 log_info("closing connection (event=%x)", event);
188 log_debug("scheduling ssh termination (%llds)",
189 (long long)timeout.tv_sec);
190 if (timeout.tv_sec != 0) {
191 evtimer_set(&timeoutev, killing_time, NULL);
192 evtimer_add(&timeoutev, &timeout);
200 struct addrinfo hints, *res, *res0;
201 int r, saved_errno, sock;
204 memset(&hints, 0, sizeof(hints));
205 hints.ai_family = AF_UNSPEC;
206 hints.ai_socktype = SOCK_STREAM;
208 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
210 log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
211 ssh_host, ssh_port, gai_strerror(r));
215 for (res = res0; res; res = res->ai_next) {
216 sock = socket(res->ai_family, res->ai_socktype,
223 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
236 log_warn("%s", cause);
243 try_to_connect(int fd, short event, void *d)
247 /* ssh may die 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");
266 evtimer_set(&c->waitev, try_to_connect, c);
267 evtimer_add(&c->waitev, &c->retry);
271 log_info("connected!");
273 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
274 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
275 if (c->sourcebev == NULL || c->tobev == NULL) {
276 log_warn("bufferevent_new");
281 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
282 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
286 do_accept(int fd, short event, void *data)
291 log_debug("incoming connection");
293 if (evtimer_pending(&timeoutev, NULL))
294 evtimer_del(&timeoutev);
296 if ((s = accept(fd, NULL, 0)) == -1)
304 if ((c = calloc(1, sizeof(*c))) == NULL) {
311 c->retry.tv_sec = BACKOFF;
312 evtimer_set(&c->waitev, try_to_connect, c);
313 evtimer_add(&c->waitev, &c->retry);
317 copysec(const char *s, char *d, size_t len)
321 if ((c = strchr(s, ':')) == NULL)
323 if ((size_t)(c - s) >= len-1)
333 struct addrinfo hints, *res, *res0;
334 int v, r, saved_errno;
336 const char *c, *h, *port, *cause;
338 if ((c = strchr(addr, ':')) == NULL) {
342 if ((c = copysec(addr, host, sizeof(host))) == NULL)
343 fatalx("name too long: %s", addr);
349 memset(&hints, 0, sizeof(hints));
350 hints.ai_family = AF_UNSPEC;
351 hints.ai_socktype = SOCK_STREAM;
352 hints.ai_flags = AI_PASSIVE;
354 r = getaddrinfo(h, port, &hints, &res0);
356 fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
358 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
359 socks[nsock] = socket(res->ai_family, res->ai_socktype,
361 if (socks[nsock] == -1) {
366 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
375 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
377 fatal("setsockopt(SO_REUSEADDR)");
380 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
382 fatal("setsockopt(SO_REUSEPORT)");
384 listen(socks[nsock], 5);
399 if (isdigit((unsigned char)*ssh_tflag)) {
400 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
401 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
406 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
408 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
413 fatal("wrong value for -B");
419 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
420 " destination\n", getprogname());
425 main(int argc, char **argv)
432 * Ensure we have fds 0-2 open so that we have no issue with
433 * calling bind_socket before daemon(3).
435 for (i = 0; i < 3; ++i) {
436 if (fstat(i, &sb) == -1) {
437 if ((fd = open("/dev/null", O_RDWR)) != -1) {
438 if (dup2(fd, i) == -1)
447 log_init(1, LOG_DAEMON);
450 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
463 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
465 fatalx("timeout is %s: %s", errstr, optarg);
477 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
484 log_init(debug, LOG_DAEMON);
485 log_setverbose(verbose);
490 signal(SIGPIPE, SIG_IGN);
494 /* initialize the timer */
495 evtimer_set(&timeoutev, killing_time, NULL);
497 signal_set(&sighupev, SIGHUP, sig_handler, NULL);
498 signal_set(&sigintev, SIGINT, sig_handler, NULL);
499 signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
500 signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
502 signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
504 signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
507 signal_add(&sighupev, NULL);
508 signal_add(&sigintev, NULL);
509 signal_add(&sigtermev, NULL);
510 signal_add(&sigchldev, NULL);
511 signal_add(&siginfoev, NULL);
513 for (i = 0; i < nsock; ++i) {
514 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
516 event_add(&sockev[i], NULL);
520 * dns, inet: bind the socket and connect to the childs.
521 * proc, exec: execute ssh on demand.
523 if (pledge("stdio dns inet proc exec", NULL) == -1)
526 log_info("starting");
530 kill(ssh_pid, SIGINT);