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>
35 #define SSH_PATH "/usr/bin/ssh"
41 int rport; /* ssh port */
42 const char *addr; /* our addr */
43 const char *ssh_tunnel_flag;
46 struct event sockev[MAXSOCK];
50 struct event sighupev;
51 struct event sigintev;
52 struct event sigtermev;
53 struct event sigchldev;
54 struct event siginfoev;
56 struct timeval timeout;
57 struct event timeoutev;
65 struct bufferevent *sourcebev;
67 struct bufferevent *tobev;
71 terminate(int fd, short event, void *data)
77 chld(int fd, short event, void *data)
82 if ((pid = waitpid(ssh_pid, &status, WNOHANG)) == -1)
87 info(int fd, short event, void *data)
89 warnx("connections: %d", conn);
95 warnx("spawning ssh...");
97 switch (ssh_pid = fork()) {
101 execl(SSH_PATH, "ssh", "-L", ssh_tunnel_flag,
102 "-NTq", ssh_dest, NULL);
105 sleep(5); /* XXX: wait for ssh to bind the port... */
110 killing_time(int fd, short event, void *data)
115 warnx("killing time!");
116 kill(ssh_pid, SIGTERM);
121 nopcb(struct bufferevent *bev, void *d)
127 sreadcb(struct bufferevent *bev, void *d)
131 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
135 treadcb(struct bufferevent *bev, void *d)
139 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
143 errcb(struct bufferevent *bev, short event, void *d)
147 warnx("in errcb, closing connection");
149 bufferevent_free(c->sourcebev);
150 bufferevent_free(c->tobev);
159 warnx("scheduling ssh termination (%lds)",
161 evtimer_set(&timeoutev, killing_time, NULL);
162 evtimer_add(&timeoutev, &timeout);
169 struct addrinfo hints, *res, *res0;
170 int r, saved_errno, sock;
172 const char *c, *cause;
174 if ((c = strchr(ssh_tunnel_flag, ':')) == NULL)
175 errx(1, "wrong flag format: %s", ssh_tunnel_flag);
177 if ((size_t)(c - ssh_tunnel_flag) >= sizeof(port)-1)
178 errx(1, "EPORTTOOLONG");
180 memset(port, 0, sizeof(port));
181 memcpy(port, ssh_tunnel_flag, c - ssh_tunnel_flag);
183 memset(&hints, 0, sizeof(hints));
184 hints.ai_family = AF_UNSPEC;
185 hints.ai_socktype = SOCK_STREAM;
188 r = getaddrinfo("localhost", port, &hints, &res0);
190 errx(1, "getaddrinfo(\"localhost\", \"%s\"): %s",
191 port, gai_strerror(r));
193 for (res = res0; res; res = res->ai_next) {
194 sock = socket(res->ai_family, res->ai_socktype,
201 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
221 do_accept(int fd, short event, void *data)
225 warnx("handling connection");
227 if (evtimer_pending(&timeoutev, NULL))
228 evtimer_del(&timeoutev);
230 if ((s = accept(fd, NULL, 0)) == -1)
233 if (conn == MAXCONN) {
244 warnx("binding the socket to ssh");
245 sock = connect_to_ssh();
247 for (i = 0; i < MAXCONN; ++i) {
248 if (conns[i].source == -1) {
251 conns[i].sourcebev = bufferevent_new(s,
252 sreadcb, nopcb, errcb, &conns[i]);
253 conns[i].tobev = bufferevent_new(sock,
254 treadcb, nopcb, errcb, &conns[i]);
255 if (conns[i].sourcebev == NULL ||
256 conns[i].tobev == NULL)
257 err(1, "bufferevent_new");
258 bufferevent_enable(conns[i].sourcebev,
260 bufferevent_enable(conns[i].tobev,
270 fprintf(stderr, "usage: %s -B port:host:hostport -b addr [-t timeout]"
271 " destination\n", getprogname());
278 struct addrinfo hints, *res, *res0;
281 const char *c, *h, *port, *cause;
283 if ((c = strchr(addr, ':')) == NULL) {
287 if ((size_t)(c - addr) >= sizeof(host) -1)
288 errx(1, "ENAMETOOLONG");
289 memset(host, 0, sizeof(host));
290 memcpy(host, c, c - addr);
296 memset(&hints, 0, sizeof(hints));
297 hints.ai_family = AF_UNSPEC;
298 hints.ai_socktype = SOCK_STREAM;
299 hints.ai_flags = AI_PASSIVE;
301 r = getaddrinfo(h, port, &hints, &res0);
303 errx(1, "getaddrinfo(%s): %s",
304 addr, gai_strerror(r));
306 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
307 socks[nsock] = socket(res->ai_family, res->ai_socktype,
309 if (socks[nsock] == -1) {
314 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
322 listen(socks[nsock], 5);
333 main(int argc, char **argv)
336 int ch, tout, i, status;
339 while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
342 ssh_tunnel_flag = optarg;
348 tout = strtonum(optarg, 1, INT_MAX, &errstr);
350 errx(1, "timeout is %s: %s", errstr, optarg);
359 if (argc != 1 || addr == NULL || ssh_tunnel_flag == NULL)
365 timeout.tv_sec = tout;
370 for (i = 0; i < MAXCONN; ++i) {
371 conns[i].source = -1;
377 signal(SIGPIPE, SIG_IGN);
381 /* initialize the timer */
382 evtimer_set(&timeoutev, killing_time, NULL);
384 signal_set(&sighupev, SIGHUP, terminate, NULL);
385 signal_set(&sigintev, SIGINT, terminate, NULL);
386 signal_set(&sigtermev, SIGTERM, terminate, NULL);
387 signal_set(&sigchldev, SIGCHLD, chld, NULL);
388 signal_set(&siginfoev, SIGINFO, info, NULL);
390 signal_add(&sighupev, NULL);
391 signal_add(&sigintev, NULL);
392 signal_add(&sigtermev, NULL);
393 signal_add(&sigchldev, NULL);
394 signal_add(&siginfoev, NULL);
396 for (i = 0; i < nsock; ++i) {
397 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
399 event_add(&sockev[i], NULL);
403 * dns, inet: bind the socket and connect to the childs.
404 * proc, exec: execute ssh on demand.
406 if (pledge("stdio dns inet proc exec", NULL) == -1)
413 kill(ssh_pid, SIGINT);