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");
89 ssh_pid = -1;
90 }
92 static void
93 info(int fd, short event, void *data)
94 {
95 warnx("connections: %d", conn);
96 }
98 static void
99 spawn_ssh(void)
101 warnx("spawning ssh...");
103 switch (ssh_pid = fork()) {
104 case -1:
105 err(1, "fork");
106 case 0:
107 execl(SSH_PATH, "ssh", "-L", ssh_tunnel_flag,
108 "-NTq", ssh_dest, NULL);
109 err(1, "exec");
110 default:
111 /* TODO: wait just a bit to let ssh to do its things */
112 sleep(5);
116 static void
117 killing_time(int fd, short event, void *data)
119 if (ssh_pid == -1)
120 return;
122 warnx("killing time!");
123 kill(ssh_pid, SIGTERM);
124 ssh_pid = -1;
127 static void
128 nopcb(struct bufferevent *bev, void *d)
130 return;
133 static void
134 sreadcb(struct bufferevent *bev, void *d)
136 struct conn *c = d;
138 bufferevent_write_buffer(c->tobev, EVBUFFER_INPUT(bev));
141 static void
142 treadcb(struct bufferevent *bev, void *d)
144 struct conn *c = d;
146 bufferevent_write_buffer(c->sourcebev, EVBUFFER_INPUT(bev));
149 static void
150 errcb(struct bufferevent *bev, short event, void *d)
152 struct conn *c = d;
154 warnx("in errcb, closing connection");
156 bufferevent_free(c->sourcebev);
157 bufferevent_free(c->tobev);
159 close(c->source);
160 close(c->to);
162 c->source = -1;
163 c->to = -1;
165 if (--conn == 0) {
166 warnx("scheduling ssh termination (%llds)",
167 (long long)timeout.tv_sec);
168 evtimer_set(&timeoutev, killing_time, NULL);
169 evtimer_add(&timeoutev, &timeout);
173 static int
174 connect_to_ssh(void)
176 struct addrinfo hints, *res, *res0;
177 int r, saved_errno, sock;
178 char port[16];
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;
194 /* XXX: hardcoded */
195 r = getaddrinfo("localhost", port, &hints, &res0);
196 if (r != 0)
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,
202 res->ai_protocol);
203 if (sock == -1) {
204 cause = "socket";
205 continue;
208 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
209 cause = "connect";
210 saved_errno = errno;
211 close(sock);
212 errno = saved_errno;
213 sock = -1;
214 continue;
217 break;
220 if (sock == -1)
221 err(1, "%s", cause);
223 freeaddrinfo(res0);
224 return sock;
227 static void
228 do_accept(int fd, short event, void *data)
230 int s, sock, i;
232 warnx("handling connection");
234 if (evtimer_pending(&timeoutev, NULL))
235 evtimer_del(&timeoutev);
237 if ((s = accept(fd, NULL, 0)) == -1)
238 err(1, "accept");
240 if (conn == MAXCONN) {
241 /* oops */
242 close(s);
243 return;
246 conn++;
248 if (ssh_pid == -1)
249 spawn_ssh();
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) {
256 conns[i].source = s;
257 conns[i].to = sock;
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,
266 EV_READ|EV_WRITE);
267 bufferevent_enable(conns[i].tobev,
268 EV_READ|EV_WRITE);
269 break;
274 static void __dead
275 usage(void)
277 fprintf(stderr, "usage: %s -B port:host:hostport -b addr [-t timeout]"
278 " destination\n", getprogname());
279 exit(1);
282 static void
283 bind_socket(void)
285 struct addrinfo hints, *res, *res0;
286 int r, saved_errno;
287 char host[64];
288 const char *c, *h, *port, *cause;
290 if ((c = strchr(addr, ':')) == NULL) {
291 h = NULL;
292 port = addr;
293 } else {
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);
299 h = host;
300 port = c+1;
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);
309 if (r != 0)
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,
315 res->ai_protocol);
316 if (socks[nsock] == -1) {
317 cause = "socket";
318 continue;
321 if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
322 cause = "bind";
323 saved_errno = errno;
324 close(socks[nsock]);
325 errno = saved_errno;
326 continue;
329 listen(socks[nsock], 5);
331 nsock++;
333 if (nsock == 0)
334 err(1, "%s", cause);
336 freeaddrinfo(res0);
339 int
340 main(int argc, char **argv)
342 int ch, tout, i;
343 const char *errstr;
345 while ((ch = getopt(argc, argv, "B:b:t:")) != -1) {
346 switch (ch) {
347 case 'B':
348 ssh_tunnel_flag = optarg;
349 break;
350 case 'b':
351 addr = optarg;
352 break;
353 case 't':
354 tout = strtonum(optarg, 1, INT_MAX, &errstr);
355 if (errstr != NULL)
356 errx(1, "timeout is %s: %s", errstr, optarg);
357 break;
358 default:
359 usage();
362 argc -= optind;
363 argv += optind;
365 if (argc != 1 || addr == NULL || ssh_tunnel_flag == NULL)
366 usage();
368 if (tout == 0)
369 tout = 120;
371 timeout.tv_sec = tout;
372 timeout.tv_usec = 0;
374 ssh_dest = argv[0];
376 for (i = 0; i < MAXCONN; ++i) {
377 conns[i].source = -1;
378 conns[i].to = -1;
381 bind_socket();
383 signal(SIGPIPE, SIG_IGN);
385 event_init();
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,
404 do_accept, NULL);
405 event_add(&sockev[i], NULL);
408 /*
409 * dns, inet: bind the socket and connect to the childs.
410 * proc, exec: execute ssh on demand.
411 */
412 if (pledge("stdio dns inet proc exec", NULL) == -1)
413 err(1, "pledge");
415 warnx("lift off!");
416 event_dispatch();
418 if (ssh_pid != -1)
419 kill(ssh_pid, SIGINT);
421 return 0;