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)
99 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
108 log_info("connections: %d", conn);
115 log_debug("spawning ssh");
117 switch (ssh_pid = fork()) {
121 execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
130 conn_free(struct conn *c)
132 if (c->sourcebev != NULL)
133 bufferevent_free(c->sourcebev);
134 if (c->tobev != NULL)
135 bufferevent_free(c->tobev);
137 if (evtimer_pending(&c->waitev, NULL))
138 evtimer_del(&c->waitev);
148 killing_time(int fd, short event, void *data)
153 log_debug("timeout expired, killing ssh (%d)", ssh_pid);
154 kill(ssh_pid, SIGTERM);
159 nopcb(struct bufferevent *bev, void *d)
165 sreadcb(struct bufferevent *bev, void *d)
169 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
173 treadcb(struct bufferevent *bev, void *d)
177 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
181 errcb(struct bufferevent *bev, short event, void *d)
185 log_info("closing connection (event=%x)", event);
190 log_debug("scheduling ssh termination (%llds)",
191 (long long)timeout.tv_sec);
192 if (timeout.tv_sec != 0) {
193 evtimer_set(&timeoutev, killing_time, NULL);
194 evtimer_add(&timeoutev, &timeout);
202 struct addrinfo hints, *res, *res0;
203 int r, saved_errno, sock;
206 memset(&hints, 0, sizeof(hints));
207 hints.ai_family = AF_UNSPEC;
208 hints.ai_socktype = SOCK_STREAM;
210 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
212 log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
213 ssh_host, ssh_port, gai_strerror(r));
217 for (res = res0; res; res = res->ai_next) {
218 sock = socket(res->ai_family, res->ai_socktype,
225 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
238 log_warn("%s", cause);
245 try_to_connect(int fd, short event, void *d)
249 /* ssh may die in the meantime */
257 log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
258 c->ntentative, RETRIES);
260 if ((c->to = connect_to_ssh()) == -1) {
261 if (c->ntentative == RETRIES) {
262 log_warnx("giving up connecting");
268 evtimer_set(&c->waitev, try_to_connect, c);
269 evtimer_add(&c->waitev, &c->retry);
273 log_info("connected!");
275 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
276 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
277 if (c->sourcebev == NULL || c->tobev == NULL) {
278 log_warn("bufferevent_new");
283 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
284 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
288 do_accept(int fd, short event, void *data)
293 log_debug("incoming connection");
295 if (evtimer_pending(&timeoutev, NULL))
296 evtimer_del(&timeoutev);
298 if ((s = accept(fd, NULL, 0)) == -1)
306 if ((c = calloc(1, sizeof(*c))) == NULL) {
314 c->retry.tv_sec = BACKOFF;
315 evtimer_set(&c->waitev, try_to_connect, c);
316 evtimer_add(&c->waitev, &c->retry);
320 copysec(const char *s, char *d, size_t len)
324 if ((c = strchr(s, ':')) == NULL)
326 if ((size_t)(c - s) >= len-1)
336 struct addrinfo hints, *res, *res0;
337 int v, r, saved_errno;
339 const char *c, *h, *port, *cause;
341 if ((c = strchr(addr, ':')) == NULL) {
345 if ((c = copysec(addr, host, sizeof(host))) == NULL)
346 fatalx("name too long: %s", addr);
352 memset(&hints, 0, sizeof(hints));
353 hints.ai_family = AF_UNSPEC;
354 hints.ai_socktype = SOCK_STREAM;
355 hints.ai_flags = AI_PASSIVE;
357 r = getaddrinfo(h, port, &hints, &res0);
359 fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
361 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
362 socks[nsock] = socket(res->ai_family, res->ai_socktype,
364 if (socks[nsock] == -1) {
369 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
378 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
380 fatal("setsockopt(SO_REUSEADDR)");
383 if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
385 fatal("setsockopt(SO_REUSEPORT)");
387 listen(socks[nsock], 5);
402 if (isdigit((unsigned char)*ssh_tflag)) {
403 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
404 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
409 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
411 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
416 fatal("wrong value for -B");
422 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
423 " destination\n", getprogname());
428 main(int argc, char **argv)
435 * Ensure we have fds 0-2 open so that we have no issue with
436 * calling bind_socket before daemon(3).
438 for (i = 0; i < 3; ++i) {
439 if (fstat(i, &sb) == -1) {
440 if ((fd = open("/dev/null", O_RDWR)) != -1) {
441 if (dup2(fd, i) == -1)
450 log_init(1, LOG_DAEMON);
453 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
466 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
468 fatalx("timeout is %s: %s", errstr, optarg);
480 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
487 log_init(debug, LOG_DAEMON);
488 log_setverbose(verbose);
493 signal(SIGPIPE, SIG_IGN);
497 /* initialize the timer */
498 evtimer_set(&timeoutev, killing_time, NULL);
500 signal_set(&sighupev, SIGHUP, sig_handler, NULL);
501 signal_set(&sigintev, SIGINT, sig_handler, NULL);
502 signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
503 signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
505 signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
507 signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
510 signal_add(&sighupev, NULL);
511 signal_add(&sigintev, NULL);
512 signal_add(&sigtermev, NULL);
513 signal_add(&sigchldev, NULL);
514 signal_add(&siginfoev, NULL);
516 for (i = 0; i < nsock; ++i) {
517 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
519 event_add(&sockev[i], NULL);
523 * dns, inet: bind the socket and connect to the childs.
524 * proc, exec: execute ssh on demand.
526 if (pledge("stdio dns inet proc exec", NULL) == -1)
529 log_info("starting");
533 kill(ssh_pid, SIGINT);