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 #ifndef __OpenBSD__
42 #define pledge(p, e) 0
43 #endif
45 int rport; /* ssh port */
46 const char *addr; /* our addr */
47 const char *ssh_tunnel_flag;
48 const char *ssh_dest;
50 struct event sockev[MAXSOCK];
51 int socks[MAXSOCK];
52 int nsock;
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;
63 pid_t ssh_pid = -1;
65 int conn;
67 struct conn {
68 int source;
69 struct bufferevent *sourcebev;
70 int to;
71 struct bufferevent *tobev;
72 } conns[MAXCONN];
74 static void
75 terminate(int fd, short event, void *data)
76 {
77 event_loopbreak();
78 }
80 static void
81 chld(int fd, short event, void *data)
82 {
83 int status;
84 pid_t pid;
86 if ((pid = waitpid(ssh_pid, &status, WNOHANG)) == -1)
87 err(1, "waitpid");
88 }
90 static void
91 info(int fd, short event, void *data)
92 {
93 warnx("connections: %d", conn);
94 }
96 static void
97 spawn_ssh(void)
98 {
99 warnx("spawning ssh...");
101 switch (ssh_pid = fork()) {
102 case -1:
103 err(1, "fork");
104 case 0:
105 execl(SSH_PATH, "ssh", "-L", ssh_tunnel_flag,
106 "-NTq", ssh_dest, NULL);
107 err(1, "exec");
108 default:
109 sleep(5); /* XXX: wait for ssh to bind the port... */
113 static void
114 killing_time(int fd, short event, void *data)
116 if (ssh_pid == -1)
117 return;
119 warnx("killing time!");
120 kill(ssh_pid, SIGTERM);
121 ssh_pid = -1;
124 static void
125 nopcb(struct bufferevent *bev, void *d)
127 return;
130 static void
131 sreadcb(struct bufferevent *bev, void *d)
133 struct conn *c = d;
135 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
138 static void
139 treadcb(struct bufferevent *bev, void *d)
141 struct conn *c = d;
143 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
146 static void
147 errcb(struct bufferevent *bev, short event, void *d)
149 struct conn *c = d;
151 warnx("in errcb, closing connection");
153 bufferevent_free(c->sourcebev);
154 bufferevent_free(c->tobev);
156 close(c->source);
157 close(c->to);
159 c->source = -1;
160 c->to = -1;
162 if (--conn == 0) {
163 warnx("scheduling ssh termination (%lds)",
164 timeout.tv_sec);
165 evtimer_set(&timeoutev, killing_time, NULL);
166 evtimer_add(&timeoutev, &timeout);
170 static int
171 connect_to_ssh(void)
173 struct addrinfo hints, *res, *res0;
174 int r, saved_errno, sock;
175 char port[16];
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;
191 /* XXX: hardcoded */
192 r = getaddrinfo("localhost", port, &hints, &res0);
193 if (r != 0)
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,
199 res->ai_protocol);
200 if (sock == -1) {
201 cause = "socket";
202 continue;
205 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
206 cause = "connect";
207 saved_errno = errno;
208 close(sock);
209 errno = saved_errno;
210 sock = -1;
211 continue;
214 break;
217 if (sock == -1)
218 err(1, "%s", cause);
220 freeaddrinfo(res0);
221 return sock;
224 static void
225 do_accept(int fd, short event, void *data)
227 int s, sock, i;
229 warnx("handling connection");
231 if (evtimer_pending(&timeoutev, NULL))
232 evtimer_del(&timeoutev);
234 if ((s = accept(fd, NULL, 0)) == -1)
235 err(1, "accept");
237 if (conn == MAXCONN) {
238 /* oops */
239 close(s);
240 return;
243 conn++;
245 if (ssh_pid == -1)
246 spawn_ssh();
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) {
253 conns[i].source = s;
254 conns[i].to = sock;
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,
263 EV_READ|EV_WRITE);
264 bufferevent_enable(conns[i].tobev,
265 EV_READ|EV_WRITE);
266 break;
271 static void __dead
272 usage(void)
274 fprintf(stderr, "usage: %s -B port:host:hostport -b addr [-t timeout]"
275 " destination\n", getprogname());
276 exit(1);
279 static void
280 bind_socket(void)
282 struct addrinfo hints, *res, *res0;
283 int r, saved_errno;
284 char host[64];
285 const char *c, *h, *port, *cause;
287 if ((c = strchr(addr, ':')) == NULL) {
288 h = NULL;
289 port = addr;
290 } else {
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);
296 h = host;
297 port = c+1;
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);
306 if (r != 0)
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,
312 res->ai_protocol);
313 if (socks[nsock] == -1) {
314 cause = "socket";
315 continue;
318 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
319 cause = "bind";
320 saved_errno = errno;
321 close(socks[nsock]);
322 errno = saved_errno;
323 continue;
326 listen(socks[nsock], 5);
328 nsock++;
330 if (nsock == 0)
331 err(1, "%s", cause);
333 freeaddrinfo(res0);
336 int
337 main(int argc, char **argv)
339 pid_t pid;
340 int ch, tout, i, status;
341 const char *errstr;
343 while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
344 switch (ch) {
345 case 'B':
346 ssh_tunnel_flag = optarg;
347 break;
348 case 'b':
349 addr = optarg;
350 break;
351 case 't':
352 tout = strtonum(optarg, 1, INT_MAX, &errstr);
353 if (errstr != NULL)
354 errx(1, "timeout is %s: %s", errstr, optarg);
355 break;
356 default:
357 usage();
360 argc -= optind;
361 argv += optind;
363 if (argc != 1 || addr == NULL || ssh_tunnel_flag == NULL)
364 usage();
366 if (tout == 0)
367 tout = 120;
369 timeout.tv_sec = tout;
370 timeout.tv_usec = 0;
372 ssh_dest = argv[0];
374 for (i = 0; i < MAXCONN; ++i) {
375 conns[i].source = -1;
376 conns[i].to = -1;
379 bind_socket();
381 signal(SIGPIPE, SIG_IGN);
383 event_init();
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,
402 do_accept, NULL);
403 event_add(&sockev[i], NULL);
406 /*
407 * dns, inet: bind the socket and connect to the childs.
408 * proc, exec: execute ssh on demand.
409 */
410 if (pledge("stdio dns inet proc exec", NULL) == -1)
411 err(1, "pledge");
413 warnx("lift off!");
414 event_dispatch();
416 if (ssh_pid != -1)
417 kill(ssh_pid, SIGINT);
419 return 0;