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 conn_free(struct conn *c)
129 if (c->sourcebev != NULL)
130 bufferevent_free(c->sourcebev);
131 if (c->tobev != NULL)
132 bufferevent_free(c->tobev);
134 if (evtimer_pending(&c->waitev, NULL))
135 evtimer_del(&c->waitev);
144 killing_time(int fd, short event, void *data)
149 log_debug("timeout expired, killing ssh (%d)", ssh_pid);
150 kill(ssh_pid, SIGTERM);
155 nopcb(struct bufferevent *bev, void *d)
161 sreadcb(struct bufferevent *bev, void *d)
165 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
169 treadcb(struct bufferevent *bev, void *d)
173 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
177 errcb(struct bufferevent *bev, short event, void *d)
181 log_info("closing connection (event=%x)", event);
186 log_debug("scheduling ssh termination (%llds)",
187 (long long)timeout.tv_sec);
188 if (timeout.tv_sec != 0) {
189 evtimer_set(&timeoutev, killing_time, NULL);
190 evtimer_add(&timeoutev, &timeout);
198 struct addrinfo hints, *res, *res0;
199 int r, saved_errno, sock;
202 memset(&hints, 0, sizeof(hints));
203 hints.ai_family = AF_UNSPEC;
204 hints.ai_socktype = SOCK_STREAM;
206 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
208 log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
209 ssh_host, ssh_port, gai_strerror(r));
213 for (res = res0; res; res = res->ai_next) {
214 sock = socket(res->ai_family, res->ai_socktype,
221 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
234 log_warn("%s", cause);
241 try_to_connect(int fd, short event, void *d)
245 /* ssh may die in the meantime */
253 log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
254 c->ntentative, RETRIES);
256 if ((c->to = connect_to_ssh()) == -1) {
257 if (c->ntentative == RETRIES) {
258 log_warnx("giving up connecting");
264 evtimer_set(&c->waitev, try_to_connect, c);
265 evtimer_add(&c->waitev, &c->retry);
269 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
270 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
271 if (c->sourcebev == NULL || c->tobev == NULL) {
272 log_warn("bufferevent_new");
277 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
278 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
282 do_accept(int fd, short event, void *data)
287 log_debug("incoming connection");
289 if (evtimer_pending(&timeoutev, NULL))
290 evtimer_del(&timeoutev);
292 if ((s = accept(fd, NULL, 0)) == -1)
300 if ((c = calloc(1, sizeof(*c))) == NULL) {
307 c->retry.tv_sec = BACKOFF;
308 evtimer_set(&c->waitev, try_to_connect, c);
309 evtimer_add(&c->waitev, &c->retry);
313 copysec(const char *s, char *d, size_t len)
317 if ((c = strchr(s, ':')) == NULL)
319 if ((size_t)(c - s) >= len-1)
329 struct addrinfo hints, *res, *res0;
330 int v, r, saved_errno;
332 const char *c, *h, *port, *cause;
334 if ((c = strchr(addr, ':')) == NULL) {
338 if ((c = copysec(addr, host, sizeof(host))) == NULL)
339 fatalx("name too long: %s", addr);
345 memset(&hints, 0, sizeof(hints));
346 hints.ai_family = AF_UNSPEC;
347 hints.ai_socktype = SOCK_STREAM;
348 hints.ai_flags = AI_PASSIVE;
350 r = getaddrinfo(h, port, &hints, &res0);
352 fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
354 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
355 socks[nsock] = socket(res->ai_family, res->ai_socktype,
357 if (socks[nsock] == -1) {
362 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
371 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
373 fatal("setsockopt(SO_REUSEADDR)");
376 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
378 fatal("setsockopt(SO_REUSEPORT)");
380 listen(socks[nsock], 5);
395 if (isdigit((unsigned char)*ssh_tflag)) {
396 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
397 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
402 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
404 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
409 fatal("wrong value for -B");
415 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
416 " destination\n", getprogname());
421 main(int argc, char **argv)
426 log_init(1, LOG_DAEMON);
429 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
442 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
444 fatalx("timeout is %s: %s", errstr, optarg);
456 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
461 log_init(debug, LOG_DAEMON);
462 log_setverbose(verbose);
469 signal(SIGPIPE, SIG_IGN);
473 /* initialize the timer */
474 evtimer_set(&timeoutev, killing_time, NULL);
476 signal_set(&sighupev, SIGHUP, sig_handler, NULL);
477 signal_set(&sigintev, SIGINT, sig_handler, NULL);
478 signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
479 signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
481 signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
483 signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
486 signal_add(&sighupev, NULL);
487 signal_add(&sigintev, NULL);
488 signal_add(&sigtermev, NULL);
489 signal_add(&sigchldev, NULL);
490 signal_add(&siginfoev, NULL);
492 for (i = 0; i < nsock; ++i) {
493 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
495 event_add(&sockev[i], NULL);
499 * dns, inet: bind the socket and connect to the childs.
500 * proc, exec: execute ssh on demand.
502 if (pledge("stdio dns inet proc exec", NULL) == -1)
505 log_info("starting");
509 kill(ssh_pid, SIGINT);