2 * Copyright (c) 2021 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>
18 #include <sys/socket.h>
36 #define SSH_PATH "/usr/bin/ssh"
45 #define pledge(p, e) 0
48 const char *addr; /* our addr */
49 const char *ssh_tflag;
55 struct event sockev[MAXSOCK];
62 struct event sighupev;
63 struct event sigintev;
64 struct event sigtermev;
65 struct event sigchldev;
66 struct event siginfoev;
68 struct timeval timeout = {600, 0}; /* 10 minutes */
69 struct event timeoutev;
80 struct bufferevent *sourcebev;
82 struct bufferevent *tobev;
86 terminate(int fd, short event, void *data)
92 chld(int fd, short event, void *data)
96 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
103 info(int fd, short event, void *data)
105 log_info("connections: %d", conn);
111 log_debug("spawning ssh");
113 switch (ssh_pid = fork()) {
117 execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
126 killing_time(int fd, short event, void *data)
131 log_debug("timeout expired, killing ssh (%d)", ssh_pid);
132 kill(ssh_pid, SIGTERM);
137 nopcb(struct bufferevent *bev, void *d)
143 sreadcb(struct bufferevent *bev, void *d)
147 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
151 treadcb(struct bufferevent *bev, void *d)
155 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
159 errcb(struct bufferevent *bev, short event, void *d)
163 log_info("closing connection (event=%x)", event);
165 bufferevent_free(c->sourcebev);
166 bufferevent_free(c->tobev);
175 log_debug("scheduling ssh termination (%llds)",
176 (long long)timeout.tv_sec);
177 if (timeout.tv_sec != 0) {
178 evtimer_set(&timeoutev, killing_time, NULL);
179 evtimer_add(&timeoutev, &timeout);
187 struct addrinfo hints, *res, *res0;
188 int r, saved_errno, sock;
191 memset(&hints, 0, sizeof(hints));
192 hints.ai_family = AF_UNSPEC;
193 hints.ai_socktype = SOCK_STREAM;
195 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
197 fatal("getaddrinfo(\"%s\", \"%s\"): %s",
198 ssh_host, ssh_port, gai_strerror(r));
200 for (res = res0; res; res = res->ai_next) {
201 sock = socket(res->ai_family, res->ai_socktype,
208 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
221 log_warn("%s", cause);
228 try_to_connect(int fd, short event, void *d)
232 /* ssh may die in the meantime */
240 log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
241 c->ntentative, RETRIES);
243 if ((c->to = connect_to_ssh()) == -1) {
244 if (c->ntentative == RETRIES) {
245 log_warnx("giving up connecting");
251 evtimer_set(&c->waitev, try_to_connect, c);
252 evtimer_add(&c->waitev, &c->retry);
256 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
257 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
258 if (c->sourcebev == NULL || c->tobev == NULL)
259 fatal("bufferevent_new");
260 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
261 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
265 do_accept(int fd, short event, void *data)
269 log_debug("incoming connection");
271 if (evtimer_pending(&timeoutev, NULL))
272 evtimer_del(&timeoutev);
274 if ((s = accept(fd, NULL, 0)) == -1)
277 if (conn == MAXCONN) {
278 log_warnx("dropping the connection, too many already");
288 for (i = 0; i < MAXCONN; ++i) {
289 if (conns[i].source != -1)
293 conns[i].ntentative = 0;
294 conns[i].retry.tv_sec = BACKOFF;
295 conns[i].retry.tv_usec = 0;
296 evtimer_set(&conns[i].waitev, try_to_connect, &conns[i]);
297 evtimer_add(&conns[i].waitev, &conns[i].retry);
303 copysec(const char *s, char *d, size_t len)
307 if ((c = strchr(s, ':')) == NULL)
309 if ((size_t)(c - s) >= len-1)
319 struct addrinfo hints, *res, *res0;
320 int v, r, saved_errno;
322 const char *c, *h, *port, *cause;
324 if ((c = strchr(addr, ':')) == NULL) {
328 if ((c = copysec(addr, host, sizeof(host))) == NULL)
329 fatalx("name too long: %s", addr);
335 memset(&hints, 0, sizeof(hints));
336 hints.ai_family = AF_UNSPEC;
337 hints.ai_socktype = SOCK_STREAM;
338 hints.ai_flags = AI_PASSIVE;
340 r = getaddrinfo(h, port, &hints, &res0);
342 fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
344 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
345 socks[nsock] = socket(res->ai_family, res->ai_socktype,
347 if (socks[nsock] == -1) {
352 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
361 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
363 fatal("setsockopt(SO_REUSEADDR)");
366 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
368 fatal("setsockopt(SO_REUSEPORT)");
370 listen(socks[nsock], 5);
385 if (isdigit(*ssh_tflag)) {
386 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
387 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
392 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
394 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
399 fatal("wrong value for -B");
405 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
406 " destination\n", getprogname());
411 main(int argc, char **argv)
416 log_init(1, LOG_DAEMON);
419 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
432 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
434 fatalx("timeout is %s: %s", errstr, optarg);
446 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
451 for (i = 0; i < MAXCONN; ++i) {
452 conns[i].source = -1;
456 log_init(debug, LOG_DAEMON);
457 log_setverbose(verbose);
464 signal(SIGPIPE, SIG_IGN);
468 /* initialize the timer */
469 evtimer_set(&timeoutev, killing_time, NULL);
471 signal_set(&sighupev, SIGHUP, terminate, NULL);
472 signal_set(&sigintev, SIGINT, terminate, NULL);
473 signal_set(&sigtermev, SIGTERM, terminate, NULL);
474 signal_set(&sigchldev, SIGCHLD, chld, NULL);
476 signal_set(&siginfoev, SIGINFO, info, NULL);
478 signal_set(&siginfoev, SIGUSR1, info, NULL);
481 signal_add(&sighupev, NULL);
482 signal_add(&sigintev, NULL);
483 signal_add(&sigtermev, NULL);
484 signal_add(&sigchldev, NULL);
485 signal_add(&siginfoev, NULL);
487 for (i = 0; i < nsock; ++i) {
488 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
490 event_add(&sockev[i], NULL);
494 * dns, inet: bind the socket and connect to the childs.
495 * proc, exec: execute ssh on demand.
497 if (pledge("stdio dns inet proc exec", NULL) == -1)
500 log_info("starting");
504 kill(ssh_pid, SIGINT);