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 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()) {
119 execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
128 killing_time(int fd, short event, void *data)
133 log_debug("timeout expired, killing ssh (%d)", ssh_pid);
134 kill(ssh_pid, SIGTERM);
139 nopcb(struct bufferevent *bev, void *d)
145 sreadcb(struct bufferevent *bev, void *d)
149 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
153 treadcb(struct bufferevent *bev, void *d)
157 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
161 errcb(struct bufferevent *bev, short event, void *d)
165 log_info("closing connection (event=%x)", event);
167 bufferevent_free(c->sourcebev);
168 bufferevent_free(c->tobev);
177 log_debug("scheduling ssh termination (%llds)",
178 (long long)timeout.tv_sec);
179 if (timeout.tv_sec != 0) {
180 evtimer_set(&timeoutev, killing_time, NULL);
181 evtimer_add(&timeoutev, &timeout);
189 struct addrinfo hints, *res, *res0;
190 int r, saved_errno, sock;
193 memset(&hints, 0, sizeof(hints));
194 hints.ai_family = AF_UNSPEC;
195 hints.ai_socktype = SOCK_STREAM;
197 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
199 fatal("getaddrinfo(\"%s\", \"%s\"): %s",
200 ssh_host, ssh_port, gai_strerror(r));
202 for (res = res0; res; res = res->ai_next) {
203 sock = socket(res->ai_family, res->ai_socktype,
210 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
223 log_warn("%s", cause);
230 try_to_connect(int fd, short event, void *d)
234 /* ssh may die in the meantime */
242 log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
243 c->ntentative, RETRIES);
245 if ((c->to = connect_to_ssh()) == -1) {
246 if (c->ntentative == RETRIES) {
247 log_warnx("giving up connecting");
253 evtimer_set(&c->waitev, try_to_connect, c);
254 evtimer_add(&c->waitev, &c->retry);
258 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
259 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
260 if (c->sourcebev == NULL || c->tobev == NULL)
261 fatal("bufferevent_new");
262 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
263 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
267 do_accept(int fd, short event, void *data)
271 log_debug("incoming connection");
273 if (evtimer_pending(&timeoutev, NULL))
274 evtimer_del(&timeoutev);
276 if ((s = accept(fd, NULL, 0)) == -1)
279 if (conn == MAXCONN) {
280 log_warnx("dropping the connection, too many already");
290 for (i = 0; i < MAXCONN; ++i) {
291 if (conns[i].source != -1)
295 conns[i].ntentative = 0;
296 conns[i].retry.tv_sec = BACKOFF;
297 conns[i].retry.tv_usec = 0;
298 evtimer_set(&conns[i].waitev, try_to_connect, &conns[i]);
299 evtimer_add(&conns[i].waitev, &conns[i].retry);
305 copysec(const char *s, char *d, size_t len)
309 if ((c = strchr(s, ':')) == NULL)
311 if ((size_t)(c - s) >= len-1)
321 struct addrinfo hints, *res, *res0;
322 int v, r, saved_errno;
324 const char *c, *h, *port, *cause;
326 if ((c = strchr(addr, ':')) == NULL) {
330 if ((c = copysec(addr, host, sizeof(host))) == NULL)
331 fatalx("name too long: %s", addr);
337 memset(&hints, 0, sizeof(hints));
338 hints.ai_family = AF_UNSPEC;
339 hints.ai_socktype = SOCK_STREAM;
340 hints.ai_flags = AI_PASSIVE;
342 r = getaddrinfo(h, port, &hints, &res0);
344 fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
346 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
347 socks[nsock] = socket(res->ai_family, res->ai_socktype,
349 if (socks[nsock] == -1) {
354 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
363 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
365 fatal("setsockopt(SO_REUSEADDR)");
368 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
370 fatal("setsockopt(SO_REUSEPORT)");
372 listen(socks[nsock], 5);
387 if (isdigit((unsigned char)*ssh_tflag)) {
388 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
389 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
394 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
396 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
401 fatal("wrong value for -B");
407 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
408 " destination\n", getprogname());
413 main(int argc, char **argv)
418 log_init(1, LOG_DAEMON);
421 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
434 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
436 fatalx("timeout is %s: %s", errstr, optarg);
448 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
453 for (i = 0; i < MAXCONN; ++i) {
454 conns[i].source = -1;
458 log_init(debug, LOG_DAEMON);
459 log_setverbose(verbose);
466 signal(SIGPIPE, SIG_IGN);
470 /* initialize the timer */
471 evtimer_set(&timeoutev, killing_time, NULL);
473 signal_set(&sighupev, SIGHUP, sig_handler, NULL);
474 signal_set(&sigintev, SIGINT, sig_handler, NULL);
475 signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
476 signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
478 signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
480 signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
483 signal_add(&sighupev, NULL);
484 signal_add(&sigintev, NULL);
485 signal_add(&sigtermev, NULL);
486 signal_add(&sigchldev, NULL);
487 signal_add(&siginfoev, NULL);
489 for (i = 0; i < nsock; ++i) {
490 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
492 event_add(&sockev[i], NULL);
496 * dns, inet: bind the socket and connect to the childs.
497 * proc, exec: execute ssh on demand.
499 if (pledge("stdio dns inet proc exec", NULL) == -1)
502 log_info("starting");
506 kill(ssh_pid, SIGINT);