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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
38 #define SSH_PATH "ssh"
47 #define pledge(p, e) 0
50 const char *addr; /* our addr */
51 const char *ssh_tflag;
57 struct event sockev[MAXSOCK];
64 struct event sighupev;
65 struct event sigintev;
66 struct event sigtermev;
67 struct event sigchldev;
68 struct event siginfoev;
70 struct timeval timeout = {120, 0};
71 struct event timeoutev;
82 struct bufferevent *sourcebev;
84 struct bufferevent *tobev;
88 terminate(int fd, short event, void *data)
94 chld(int fd, short event, void *data)
98 if (waitpid(ssh_pid, &status, WNOHANG) == -1)
105 info(int fd, short event, void *data)
107 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_debug("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;
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) {
362 listen(socks[nsock], 5);
377 if (isdigit(*ssh_tflag)) {
378 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
379 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
384 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
386 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
391 fatal("wrong value for -B");
397 fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
398 " destination\n", getprogname());
403 main(int argc, char **argv)
408 log_init(1, LOG_DAEMON);
411 while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
424 timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
426 fatalx("timeout is %s: %s", errstr, optarg);
438 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
443 for (i = 0; i < MAXCONN; ++i) {
444 conns[i].source = -1;
448 log_init(debug, LOG_DAEMON);
449 log_setverbose(verbose);
456 signal(SIGPIPE, SIG_IGN);
460 /* initialize the timer */
461 evtimer_set(&timeoutev, killing_time, NULL);
463 signal_set(&sighupev, SIGHUP, terminate, NULL);
464 signal_set(&sigintev, SIGINT, terminate, NULL);
465 signal_set(&sigtermev, SIGTERM, terminate, NULL);
466 signal_set(&sigchldev, SIGCHLD, chld, NULL);
467 signal_set(&siginfoev, SIGINFO, info, 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);