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"
44 #define pledge(p, e) 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)
96 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
105 log_info("connections: %d", conn);
112 log_debug("spawning ssh");
114 switch (ssh_pid = fork()) {
118 execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
127 killing_time(int fd, short event, void *data)
132 log_debug("timeout expired, killing ssh (%d)", ssh_pid);
133 kill(ssh_pid, SIGTERM);
138 nopcb(struct bufferevent *bev, void *d)
144 sreadcb(struct bufferevent *bev, void *d)
148 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
152 treadcb(struct bufferevent *bev, void *d)
156 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
160 errcb(struct bufferevent *bev, short event, void *d)
164 log_info("closing connection (event=%x)", event);
166 bufferevent_free(c->sourcebev);
167 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)
270 log_debug("incoming connection");
272 if (evtimer_pending(&timeoutev, NULL))
273 evtimer_del(&timeoutev);
275 if ((s = accept(fd, NULL, 0)) == -1)
283 if ((c = calloc(1, sizeof(*c))) == NULL) {
290 c->retry.tv_sec = BACKOFF;
291 evtimer_set(&c->waitev, try_to_connect, c);
292 evtimer_add(&c->waitev, &c->retry);
296 copysec(const char *s, char *d, size_t len)
300 if ((c = strchr(s, ':')) == NULL)
302 if ((size_t)(c - s) >= len-1)
312 struct addrinfo hints, *res, *res0;
313 int v, r, saved_errno;
315 const char *c, *h, *port, *cause;
317 if ((c = strchr(addr, ':')) == NULL) {
321 if ((c = copysec(addr, host, sizeof(host))) == NULL)
322 fatalx("name too long: %s", addr);
328 memset(&hints, 0, sizeof(hints));
329 hints.ai_family = AF_UNSPEC;
330 hints.ai_socktype = SOCK_STREAM;
331 hints.ai_flags = AI_PASSIVE;
333 r = getaddrinfo(h, port, &hints, &res0);
335 fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
337 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
338 socks[nsock] = socket(res->ai_family, res->ai_socktype,
340 if (socks[nsock] == -1) {
345 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
354 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
356 fatal("setsockopt(SO_REUSEADDR)");
359 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
361 fatal("setsockopt(SO_REUSEPORT)");
363 listen(socks[nsock], 5);
378 if (isdigit((unsigned char)*ssh_tflag)) {
379 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
380 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
385 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
387 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
392 fatal("wrong value for -B");
398 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
399 " destination\n", getprogname());
404 main(int argc, char **argv)
409 log_init(1, LOG_DAEMON);
412 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
425 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
427 fatalx("timeout is %s: %s", errstr, optarg);
439 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
444 log_init(debug, LOG_DAEMON);
445 log_setverbose(verbose);
452 signal(SIGPIPE, SIG_IGN);
456 /* initialize the timer */
457 evtimer_set(&timeoutev, killing_time, NULL);
459 signal_set(&sighupev, SIGHUP, sig_handler, NULL);
460 signal_set(&sigintev, SIGINT, sig_handler, NULL);
461 signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
462 signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
464 signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
466 signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
469 signal_add(&sighupev, NULL);
470 signal_add(&sigintev, NULL);
471 signal_add(&sigtermev, NULL);
472 signal_add(&sigchldev, NULL);
473 signal_add(&siginfoev, NULL);
475 for (i = 0; i < nsock; ++i) {
476 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
478 event_add(&sockev[i], NULL);
482 * dns, inet: bind the socket and connect to the childs.
483 * proc, exec: execute ssh on demand.
485 if (pledge("stdio dns inet proc exec", NULL) == -1)
488 log_info("starting");
492 kill(ssh_pid, SIGINT);