Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/wait.h>
21 #include <err.h>
22 #include <errno.h>
23 #include <event.h>
24 #include <limits.h>
25 #include <netdb.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include <stdint.h>
34 #ifndef SSH_PATH
35 #define SSH_PATH "/usr/bin/ssh"
36 #endif
38 #define MAXSOCK 4
39 #define MAXCONN 16
41 int rport; /* ssh port */
42 const char *addr; /* our addr */
43 const char *ssh_tunnel_flag;
44 const char *ssh_dest;
46 struct event sockev[MAXSOCK];
47 int socks[MAXSOCK];
48 int nsock;
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;
59 pid_t ssh_pid = -1;
61 int conn;
63 struct conn {
64 int source;
65 struct bufferevent *sourcebev;
66 int to;
67 struct bufferevent *tobev;
68 } conns[MAXCONN];
70 static void
71 terminate(int fd, short event, void *data)
72 {
73 event_loopbreak();
74 }
76 static void
77 chld(int fd, short event, void *data)
78 {
79 int status;
80 pid_t pid;
82 if ((pid = waitpid(ssh_pid, &status, WNOHANG)) == -1)
83 err(1, "waitpid");
84 }
86 static void
87 info(int fd, short event, void *data)
88 {
89 warnx("connections: %d", conn);
90 }
92 static void
93 spawn_ssh(void)
94 {
95 warnx("spawning ssh...");
97 switch (ssh_pid = fork()) {
98 case -1:
99 err(1, "fork");
100 case 0:
101 execl(SSH_PATH, "ssh", "-L", ssh_tunnel_flag,
102 "-NTq", ssh_dest, NULL);
103 err(1, "exec");
104 default:
105 sleep(5); /* XXX: wait for ssh to bind the port... */
109 static void
110 killing_time(int fd, short event, void *data)
112 if (ssh_pid == -1)
113 return;
115 warnx("killing time!");
116 kill(ssh_pid, SIGTERM);
117 ssh_pid = -1;
120 static void
121 nopcb(struct bufferevent *bev, void *d)
123 return;
126 static void
127 sreadcb(struct bufferevent *bev, void *d)
129 struct conn *c = d;
131 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
134 static void
135 treadcb(struct bufferevent *bev, void *d)
137 struct conn *c = d;
139 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
142 static void
143 errcb(struct bufferevent *bev, short event, void *d)
145 struct conn *c = d;
147 warnx("in errcb, closing connection");
149 bufferevent_free(c->sourcebev);
150 bufferevent_free(c->tobev);
152 close(c->source);
153 close(c->to);
155 c->source = -1;
156 c->to = -1;
158 if (--conn == 0) {
159 warnx("scheduling ssh termination (%lds)",
160 timeout.tv_sec);
161 evtimer_set(&timeoutev, killing_time, NULL);
162 evtimer_add(&timeoutev, &timeout);
166 static int
167 connect_to_ssh(void)
169 struct addrinfo hints, *res, *res0;
170 int r, saved_errno, sock;
171 char port[16];
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;
187 /* XXX: hardcoded */
188 r = getaddrinfo("localhost", port, &hints, &res0);
189 if (r != 0)
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,
195 res->ai_protocol);
196 if (sock == -1) {
197 cause = "socket";
198 continue;
201 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
202 cause = "connect";
203 saved_errno = errno;
204 close(sock);
205 errno = saved_errno;
206 sock = -1;
207 continue;
210 break;
213 if (sock == -1)
214 err(1, "%s", cause);
216 freeaddrinfo(res0);
217 return sock;
220 static void
221 do_accept(int fd, short event, void *data)
223 int s, sock, i;
225 warnx("handling connection");
227 if (evtimer_pending(&timeoutev, NULL))
228 evtimer_del(&timeoutev);
230 if ((s = accept(fd, NULL, 0)) == -1)
231 err(1, "accept");
233 if (conn == MAXCONN) {
234 /* oops */
235 close(s);
236 return;
239 conn++;
241 if (ssh_pid == -1)
242 spawn_ssh();
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) {
249 conns[i].source = s;
250 conns[i].to = sock;
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,
259 EV_READ|EV_WRITE);
260 bufferevent_enable(conns[i].tobev,
261 EV_READ|EV_WRITE);
262 break;
267 static void __dead
268 usage(void)
270 fprintf(stderr, "usage: %s -B port:host:hostport -b addr [-t timeout]"
271 " destination\n", getprogname());
272 exit(1);
275 static void
276 bind_socket(void)
278 struct addrinfo hints, *res, *res0;
279 int r, saved_errno;
280 char host[64];
281 const char *c, *h, *port, *cause;
283 if ((c = strchr(addr, ':')) == NULL) {
284 h = NULL;
285 port = addr;
286 } else {
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);
292 h = host;
293 port = c+1;
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);
302 if (r != 0)
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,
308 res->ai_protocol);
309 if (socks[nsock] == -1) {
310 cause = "socket";
311 continue;
314 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
315 cause = "bind";
316 saved_errno = errno;
317 close(socks[nsock]);
318 errno = saved_errno;
319 continue;
322 listen(socks[nsock], 5);
324 nsock++;
326 if (nsock == 0)
327 err(1, "%s", cause);
329 freeaddrinfo(res0);
332 int
333 main(int argc, char **argv)
335 pid_t pid;
336 int ch, tout, i, status;
337 const char *errstr;
339 while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
340 switch (ch) {
341 case 'B':
342 ssh_tunnel_flag = optarg;
343 break;
344 case 'b':
345 addr = optarg;
346 break;
347 case 't':
348 tout = strtonum(optarg, 1, INT_MAX, &errstr);
349 if (errstr != NULL)
350 errx(1, "timeout is %s: %s", errstr, optarg);
351 break;
352 default:
353 usage();
356 argc -= optind;
357 argv += optind;
359 if (argc != 1 || addr == NULL || ssh_tunnel_flag == NULL)
360 usage();
362 if (tout == 0)
363 tout = 120;
365 timeout.tv_sec = tout;
366 timeout.tv_usec = 0;
368 ssh_dest = argv[0];
370 for (i = 0; i < MAXCONN; ++i) {
371 conns[i].source = -1;
372 conns[i].to = -1;
375 bind_socket();
377 signal(SIGPIPE, SIG_IGN);
379 event_init();
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,
398 do_accept, NULL);
399 event_add(&sockev[i], NULL);
402 /*
403 * dns, inet: bind the socket and connect to the childs.
404 * proc, exec: execute ssh on demand.
405 */
406 if (pledge("stdio dns inet proc exec", NULL) == -1)
407 err(1, "pledge");
409 warnx("lift off!");
410 event_dispatch();
412 if (ssh_pid != -1)
413 kill(ssh_pid, SIGINT);
415 return 0;