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"
42 #define pledge(p, e) 0
45 int rport; /* ssh port */
46 const char *addr; /* our addr */
47 const char *ssh_tunnel_flag;
50 struct event sockev[MAXSOCK];
54 struct event sighupev;
55 struct event sigintev;
56 struct event sigtermev;
57 struct event sigchldev;
58 struct event siginfoev;
60 struct timeval timeout;
61 struct event timeoutev;
69 struct bufferevent *sourcebev;
71 struct bufferevent *tobev;
75 terminate(int fd, short event, void *data)
81 chld(int fd, short event, void *data)
86 if ((pid = waitpid(ssh_pid, &status, WNOHANG)) == -1)
91 info(int fd, short event, void *data)
93 warnx("connections: %d", conn);
99 warnx("spawning ssh...");
101 switch (ssh_pid = fork()) {
105 execl(SSH_PATH, "ssh", "-L", ssh_tunnel_flag,
106 "-NTq", ssh_dest, NULL);
109 sleep(5); /* XXX: wait for ssh to bind the port... */
114 killing_time(int fd, short event, void *data)
119 warnx("killing time!");
120 kill(ssh_pid, SIGTERM);
125 nopcb(struct bufferevent *bev, void *d)
131 sreadcb(struct bufferevent *bev, void *d)
135 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
139 treadcb(struct bufferevent *bev, void *d)
143 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
147 errcb(struct bufferevent *bev, short event, void *d)
151 warnx("in errcb, closing connection");
153 bufferevent_free(c->sourcebev);
154 bufferevent_free(c->tobev);
163 warnx("scheduling ssh termination (%lds)",
165 evtimer_set(&timeoutev, killing_time, NULL);
166 evtimer_add(&timeoutev, &timeout);
173 struct addrinfo hints, *res, *res0;
174 int r, saved_errno, sock;
176 const char *c, *cause;
178 if ((c = strchr(ssh_tunnel_flag, ':')) == NULL)
179 errx(1, "wrong flag format: %s", ssh_tunnel_flag);
181 if ((size_t)(c - ssh_tunnel_flag) >= sizeof(port)-1)
182 errx(1, "EPORTTOOLONG");
184 memset(port, 0, sizeof(port));
185 memcpy(port, ssh_tunnel_flag, c - ssh_tunnel_flag);
187 memset(&hints, 0, sizeof(hints));
188 hints.ai_family = AF_UNSPEC;
189 hints.ai_socktype = SOCK_STREAM;
192 r = getaddrinfo("localhost", port, &hints, &res0);
194 errx(1, "getaddrinfo(\"localhost\", \"%s\"): %s",
195 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 do_accept(int fd, short event, void *data)
229 warnx("handling connection");
231 if (evtimer_pending(&timeoutev, NULL))
232 evtimer_del(&timeoutev);
234 if ((s = accept(fd, NULL, 0)) == -1)
237 if (conn == MAXCONN) {
248 warnx("binding the socket to ssh");
249 sock = connect_to_ssh();
251 for (i = 0; i < MAXCONN; ++i) {
252 if (conns[i].source == -1) {
255 conns[i].sourcebev = bufferevent_new(s,
256 sreadcb, nopcb, errcb, &conns[i]);
257 conns[i].tobev = bufferevent_new(sock,
258 treadcb, nopcb, errcb, &conns[i]);
259 if (conns[i].sourcebev == NULL ||
260 conns[i].tobev == NULL)
261 err(1, "bufferevent_new");
262 bufferevent_enable(conns[i].sourcebev,
264 bufferevent_enable(conns[i].tobev,
274 fprintf(stderr, "usage: %s -B port:host:hostport -b addr [-t timeout]"
275 " destination\n", getprogname());
282 struct addrinfo hints, *res, *res0;
285 const char *c, *h, *port, *cause;
287 if ((c = strchr(addr, ':')) == NULL) {
291 if ((size_t)(c - addr) >= sizeof(host) -1)
292 errx(1, "ENAMETOOLONG");
293 memset(host, 0, sizeof(host));
294 memcpy(host, c, c - addr);
300 memset(&hints, 0, sizeof(hints));
301 hints.ai_family = AF_UNSPEC;
302 hints.ai_socktype = SOCK_STREAM;
303 hints.ai_flags = AI_PASSIVE;
305 r = getaddrinfo(h, port, &hints, &res0);
307 errx(1, "getaddrinfo(%s): %s",
308 addr, gai_strerror(r));
310 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
311 socks[nsock] = socket(res->ai_family, res->ai_socktype,
313 if (socks[nsock] == -1) {
318 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
326 listen(socks[nsock], 5);
337 main(int argc, char **argv)
340 int ch, tout, i, status;
343 while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
346 ssh_tunnel_flag = optarg;
352 tout = strtonum(optarg, 1, INT_MAX, &errstr);
354 errx(1, "timeout is %s: %s", errstr, optarg);
363 if (argc != 1 || addr == NULL || ssh_tunnel_flag == NULL)
369 timeout.tv_sec = tout;
374 for (i = 0; i < MAXCONN; ++i) {
375 conns[i].source = -1;
381 signal(SIGPIPE, SIG_IGN);
385 /* initialize the timer */
386 evtimer_set(&timeoutev, killing_time, NULL);
388 signal_set(&sighupev, SIGHUP, terminate, NULL);
389 signal_set(&sigintev, SIGINT, terminate, NULL);
390 signal_set(&sigtermev, SIGTERM, terminate, NULL);
391 signal_set(&sigchldev, SIGCHLD, chld, NULL);
392 signal_set(&siginfoev, SIGINFO, info, NULL);
394 signal_add(&sighupev, NULL);
395 signal_add(&sigintev, NULL);
396 signal_add(&sigtermev, NULL);
397 signal_add(&sigchldev, NULL);
398 signal_add(&siginfoev, NULL);
400 for (i = 0; i < nsock; ++i) {
401 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
403 event_add(&sockev[i], NULL);
407 * dns, inet: bind the socket and connect to the childs.
408 * proc, exec: execute ssh on demand.
410 if (pledge("stdio dns inet proc exec", NULL) == -1)
417 kill(ssh_pid, SIGINT);