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 int rport; /* ssh port */
49 const char *addr; /* our addr */
50 const char *ssh_tflag;
56 struct event sockev[MAXSOCK];
60 struct event sighupev;
61 struct event sigintev;
62 struct event sigtermev;
63 struct event sigchldev;
64 struct event siginfoev;
66 struct timeval timeout;
67 struct event timeoutev;
78 struct bufferevent *sourcebev;
80 struct bufferevent *tobev;
84 terminate(int fd, short event, void *data)
90 chld(int fd, short event, void *data)
95 if ((pid = waitpid(ssh_pid, &status, WNOHANG)) == -1)
102 info(int fd, short event, void *data)
104 warnx("connections: %d", conn);
110 warnx("spawning ssh...");
112 switch (ssh_pid = fork()) {
116 execl(SSH_PATH, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
125 killing_time(int fd, short event, void *data)
130 warnx("killing time!");
131 kill(ssh_pid, SIGTERM);
136 nopcb(struct bufferevent *bev, void *d)
142 sreadcb(struct bufferevent *bev, void *d)
146 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
150 treadcb(struct bufferevent *bev, void *d)
154 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
158 errcb(struct bufferevent *bev, short event, void *d)
162 warnx("in errcb, closing connection");
164 bufferevent_free(c->sourcebev);
165 bufferevent_free(c->tobev);
174 warnx("scheduling ssh termination (%llds)",
175 (long long)timeout.tv_sec);
176 evtimer_set(&timeoutev, killing_time, NULL);
177 evtimer_add(&timeoutev, &timeout);
184 struct addrinfo hints, *res, *res0;
185 int r, saved_errno, sock;
188 memset(&hints, 0, sizeof(hints));
189 hints.ai_family = AF_UNSPEC;
190 hints.ai_socktype = SOCK_STREAM;
192 r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
194 errx(1, "getaddrinfo(\"%s\", \"%s\"): %s",
195 ssh_host, ssh_port, gai_strerror(r));
197 for (res = res0; res; res = res->ai_next) {
198 sock = socket(res->ai_family, res->ai_socktype,
205 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
225 try_to_connect(int fd, short event, void *d)
229 /* ssh may die in the meantime */
237 warnx("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
238 c->ntentative, RETRIES);
240 if ((c->to = connect_to_ssh()) == -1) {
241 if (c->ntentative == RETRIES) {
248 evtimer_set(&c->waitev, try_to_connect, c);
249 evtimer_add(&c->waitev, &c->retry);
253 c->sourcebev = bufferevent_new(c->source, sreadcb, nopcb, errcb, c);
254 c->tobev = bufferevent_new(c->to, treadcb, nopcb, errcb, c);
255 if (c->sourcebev == NULL || c->tobev == NULL)
256 err(1, "bufferevent_new");
257 bufferevent_enable(c->sourcebev, EV_READ|EV_WRITE);
258 bufferevent_enable(c->tobev, EV_READ|EV_WRITE);
262 do_accept(int fd, short event, void *data)
266 warnx("handling connection");
268 if (evtimer_pending(&timeoutev, NULL))
269 evtimer_del(&timeoutev);
271 if ((s = accept(fd, NULL, 0)) == -1)
274 if (conn == MAXCONN) {
285 for (i = 0; i < MAXCONN; ++i) {
286 if (conns[i].source != -1)
290 conns[i].ntentative = 0;
291 conns[i].retry.tv_sec = BACKOFF;
292 conns[i].retry.tv_usec = 0;
293 evtimer_set(&conns[i].waitev, try_to_connect, &conns[i]);
294 evtimer_add(&conns[i].waitev, &conns[i].retry);
302 fprintf(stderr, "usage: %s -B sshaddr -b addr [-t timeout]"
303 " destination\n", getprogname());
308 copysec(const char *s, char *d, size_t len)
312 if ((c = strchr(s, ':')) == NULL)
314 if ((size_t)(c - s) >= len-1)
324 struct addrinfo hints, *res, *res0;
327 const char *c, *h, *port, *cause;
329 if ((c = strchr(addr, ':')) == NULL) {
333 if ((c = copysec(addr, host, sizeof(host))) == NULL)
334 errx(1, "ENAMETOOLONG");
340 memset(&hints, 0, sizeof(hints));
341 hints.ai_family = AF_UNSPEC;
342 hints.ai_socktype = SOCK_STREAM;
343 hints.ai_flags = AI_PASSIVE;
345 r = getaddrinfo(h, port, &hints, &res0);
347 errx(1, "getaddrinfo(%s): %s",
348 addr, gai_strerror(r));
350 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
351 socks[nsock] = socket(res->ai_family, res->ai_socktype,
353 if (socks[nsock] == -1) {
358 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
366 listen(socks[nsock], 5);
381 if (isdigit(*ssh_tflag)) {
382 strlcpy(ssh_host, "localhost", sizeof(ssh_host));
383 if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
388 if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
390 if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
395 errx(1, "wrong value for -B");
399 main(int argc, char **argv)
404 while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
414 tout = strtonum(optarg, 1, INT_MAX, &errstr);
416 errx(1, "timeout is %s: %s", errstr, optarg);
425 if (argc != 1 || addr == NULL || ssh_tflag == NULL)
431 timeout.tv_sec = tout;
436 for (i = 0; i < MAXCONN; ++i) {
437 conns[i].source = -1;
443 signal(SIGPIPE, SIG_IGN);
447 /* initialize the timer */
448 evtimer_set(&timeoutev, killing_time, NULL);
450 signal_set(&sighupev, SIGHUP, terminate, NULL);
451 signal_set(&sigintev, SIGINT, terminate, NULL);
452 signal_set(&sigtermev, SIGTERM, terminate, NULL);
453 signal_set(&sigchldev, SIGCHLD, chld, NULL);
454 signal_set(&siginfoev, SIGINFO, info, NULL);
456 signal_add(&sighupev, NULL);
457 signal_add(&sigintev, NULL);
458 signal_add(&sigtermev, NULL);
459 signal_add(&sigchldev, NULL);
460 signal_add(&siginfoev, NULL);
462 for (i = 0; i < nsock; ++i) {
463 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
465 event_add(&sockev[i], NULL);
469 * dns, inet: bind the socket and connect to the childs.
470 * proc, exec: execute ssh on demand.
472 if (pledge("stdio dns inet proc exec", NULL) == -1)
479 kill(ssh_pid, SIGINT);