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)
93 info(int fd, short event, void *data)
95 warnx("connections: %d", conn);
101 warnx("spawning ssh...");
103 switch (ssh_pid = fork()) {
107 execl(SSH_PATH, "ssh", "-L", ssh_tunnel_flag,
108 "-NTq", ssh_dest, NULL);
111 /* TODO: wait just a bit to let ssh to do its things */
117 killing_time(int fd, short event, void *data)
122 warnx("killing time!");
123 kill(ssh_pid, SIGTERM);
128 nopcb(struct bufferevent *bev, void *d)
134 sreadcb(struct bufferevent *bev, void *d)
138 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
142 treadcb(struct bufferevent *bev, void *d)
146 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
150 errcb(struct bufferevent *bev, short event, void *d)
154 warnx("in errcb, closing connection");
156 bufferevent_free(c->sourcebev);
157 bufferevent_free(c->tobev);
166 warnx("scheduling ssh termination (%llds)",
167 (long long)timeout.tv_sec);
168 evtimer_set(&timeoutev, killing_time, NULL);
169 evtimer_add(&timeoutev, &timeout);
176 struct addrinfo hints, *res, *res0;
177 int r, saved_errno, sock;
179 const char *c, *cause;
181 if ((c = strchr(ssh_tunnel_flag, ':')) == NULL)
182 errx(1, "wrong flag format: %s", ssh_tunnel_flag);
184 if ((size_t)(c - ssh_tunnel_flag) >= sizeof(port)-1)
185 errx(1, "EPORTTOOLONG");
187 memset(port, 0, sizeof(port));
188 memcpy(port, ssh_tunnel_flag, c - ssh_tunnel_flag);
190 memset(&hints, 0, sizeof(hints));
191 hints.ai_family = AF_UNSPEC;
192 hints.ai_socktype = SOCK_STREAM;
195 r = getaddrinfo("localhost", port, &hints, &res0);
197 errx(1, "getaddrinfo(\"localhost\", \"%s\"): %s",
198 port, gai_strerror(r));
200 for (res = res0; res; res = res->ai_next) {
201 sock = socket(res->ai_family, res->ai_socktype,
208 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
228 do_accept(int fd, short event, void *data)
232 warnx("handling connection");
234 if (evtimer_pending(&timeoutev, NULL))
235 evtimer_del(&timeoutev);
237 if ((s = accept(fd, NULL, 0)) == -1)
240 if (conn == MAXCONN) {
251 warnx("binding the socket to ssh");
252 sock = connect_to_ssh();
254 for (i = 0; i < MAXCONN; ++i) {
255 if (conns[i].source == -1) {
258 conns[i].sourcebev = bufferevent_new(s,
259 sreadcb, nopcb, errcb, &conns[i]);
260 conns[i].tobev = bufferevent_new(sock,
261 treadcb, nopcb, errcb, &conns[i]);
262 if (conns[i].sourcebev == NULL ||
263 conns[i].tobev == NULL)
264 err(1, "bufferevent_new");
265 bufferevent_enable(conns[i].sourcebev,
267 bufferevent_enable(conns[i].tobev,
277 fprintf(stderr, "usage: %s -B port:host:hostport -b addr [-t timeout]"
278 " destination\n", getprogname());
285 struct addrinfo hints, *res, *res0;
288 const char *c, *h, *port, *cause;
290 if ((c = strchr(addr, ':')) == NULL) {
294 if ((size_t)(c - addr) >= sizeof(host) -1)
295 errx(1, "ENAMETOOLONG");
296 memset(host, 0, sizeof(host));
297 memcpy(host, c, c - addr);
303 memset(&hints, 0, sizeof(hints));
304 hints.ai_family = AF_UNSPEC;
305 hints.ai_socktype = SOCK_STREAM;
306 hints.ai_flags = AI_PASSIVE;
308 r = getaddrinfo(h, port, &hints, &res0);
310 errx(1, "getaddrinfo(%s): %s",
311 addr, gai_strerror(r));
313 for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
314 socks[nsock] = socket(res->ai_family, res->ai_socktype,
316 if (socks[nsock] == -1) {
321 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
329 listen(socks[nsock], 5);
340 main(int argc, char **argv)
345 while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
348 ssh_tunnel_flag = optarg;
354 tout = strtonum(optarg, 1, INT_MAX, &errstr);
356 errx(1, "timeout is %s: %s", errstr, optarg);
365 if (argc != 1 || addr == NULL || ssh_tunnel_flag == NULL)
371 timeout.tv_sec = tout;
376 for (i = 0; i < MAXCONN; ++i) {
377 conns[i].source = -1;
383 signal(SIGPIPE, SIG_IGN);
387 /* initialize the timer */
388 evtimer_set(&timeoutev, killing_time, NULL);
390 signal_set(&sighupev, SIGHUP, terminate, NULL);
391 signal_set(&sigintev, SIGINT, terminate, NULL);
392 signal_set(&sigtermev, SIGTERM, terminate, NULL);
393 signal_set(&sigchldev, SIGCHLD, chld, NULL);
394 signal_set(&siginfoev, SIGINFO, info, NULL);
396 signal_add(&sighupev, NULL);
397 signal_add(&sigintev, NULL);
398 signal_add(&sigtermev, NULL);
399 signal_add(&sigchldev, NULL);
400 signal_add(&siginfoev, NULL);
402 for (i = 0; i < nsock; ++i) {
403 event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
405 event_add(&sockev[i], NULL);
409 * dns, inet: bind the socket and connect to the childs.
410 * proc, exec: execute ssh on demand.
412 if (pledge("stdio dns inet proc exec", NULL) == -1)
419 kill(ssh_pid, SIGINT);