Blame


1 b542d800 2021-11-20 op /*
2 5955d494 2022-07-05 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
3 b542d800 2021-11-20 op *
4 b542d800 2021-11-20 op * Permission to use, copy, modify, and distribute this software for any
5 b542d800 2021-11-20 op * purpose with or without fee is hereby granted, provided that the above
6 b542d800 2021-11-20 op * copyright notice and this permission notice appear in all copies.
7 b542d800 2021-11-20 op *
8 b542d800 2021-11-20 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 b542d800 2021-11-20 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 b542d800 2021-11-20 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 b542d800 2021-11-20 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 b542d800 2021-11-20 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 b542d800 2021-11-20 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 b542d800 2021-11-20 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 b542d800 2021-11-20 op */
16 90fbce40 2021-11-25 op
17 0efd7c9a 2022-07-30 op #include "config.h"
18 0efd7c9a 2022-07-30 op
19 b542d800 2021-11-20 op #include <sys/types.h>
20 eb1810c9 2022-07-05 op #include <sys/stat.h>
21 b542d800 2021-11-20 op #include <sys/socket.h>
22 b542d800 2021-11-20 op #include <sys/wait.h>
23 b542d800 2021-11-20 op
24 1a869946 2021-11-23 op #include <ctype.h>
25 b542d800 2021-11-20 op #include <errno.h>
26 eb1810c9 2022-07-05 op #include <fcntl.h>
27 b542d800 2021-11-20 op #include <limits.h>
28 b542d800 2021-11-20 op #include <netdb.h>
29 b542d800 2021-11-20 op #include <signal.h>
30 b542d800 2021-11-20 op #include <stdio.h>
31 b542d800 2021-11-20 op #include <stdlib.h>
32 b542d800 2021-11-20 op #include <string.h>
33 3376f40a 2021-11-25 op #include <syslog.h>
34 b542d800 2021-11-20 op #include <unistd.h>
35 b542d800 2021-11-20 op
36 3376f40a 2021-11-25 op #include "log.h"
37 cfe57149 2022-07-30 op #include "lstun.h"
38 3376f40a 2021-11-25 op
39 7e04fdce 2021-12-01 op #define MAXSOCK 32
40 1b3b9dc3 2021-11-23 op #define BACKOFF 1
41 c7868114 2022-07-05 op #define RETRIES 16
42 72c20227 2021-11-21 op
43 b542d800 2021-11-20 op const char *addr; /* our addr */
44 79313907 2021-11-23 op const char *ssh_tflag;
45 b542d800 2021-11-20 op const char *ssh_dest;
46 b542d800 2021-11-20 op
47 1a869946 2021-11-23 op char ssh_host[256];
48 1a869946 2021-11-23 op char ssh_port[16];
49 1a869946 2021-11-23 op
50 b542d800 2021-11-20 op struct event sockev[MAXSOCK];
51 b542d800 2021-11-20 op int socks[MAXSOCK];
52 b542d800 2021-11-20 op int nsock;
53 b542d800 2021-11-20 op
54 3376f40a 2021-11-25 op int debug;
55 3376f40a 2021-11-25 op int verbose;
56 3376f40a 2021-11-25 op
57 b542d800 2021-11-20 op struct event sighupev;
58 b542d800 2021-11-20 op struct event sigintev;
59 b542d800 2021-11-20 op struct event sigtermev;
60 b542d800 2021-11-20 op struct event sigchldev;
61 b542d800 2021-11-20 op struct event siginfoev;
62 b542d800 2021-11-20 op
63 dd30c95b 2021-12-01 op struct timeval timeout = {600, 0}; /* 10 minutes */
64 b542d800 2021-11-20 op struct event timeoutev;
65 b542d800 2021-11-20 op
66 b542d800 2021-11-20 op pid_t ssh_pid = -1;
67 b542d800 2021-11-20 op
68 b542d800 2021-11-20 op int conn;
69 b542d800 2021-11-20 op
70 b542d800 2021-11-20 op static void
71 329392cb 2022-07-05 op sig_handler(int sig, short event, void *data)
72 b542d800 2021-11-20 op {
73 b542d800 2021-11-20 op int status;
74 b542d800 2021-11-20 op
75 329392cb 2022-07-05 op switch (sig) {
76 329392cb 2022-07-05 op case SIGHUP:
77 329392cb 2022-07-05 op case SIGINT:
78 329392cb 2022-07-05 op case SIGTERM:
79 803370a7 2022-07-05 op log_info("quitting");
80 329392cb 2022-07-05 op event_loopbreak();
81 329392cb 2022-07-05 op break;
82 329392cb 2022-07-05 op case SIGCHLD:
83 329392cb 2022-07-05 op if (waitpid(ssh_pid, &status, WNOHANG) == -1)
84 329392cb 2022-07-05 op fatal("waitpid");
85 329392cb 2022-07-05 op ssh_pid = -1;
86 329392cb 2022-07-05 op break;
87 329392cb 2022-07-05 op #ifdef SIGINFO
88 329392cb 2022-07-05 op case SIGINFO:
89 329392cb 2022-07-05 op #else
90 329392cb 2022-07-05 op case SIGUSR1:
91 329392cb 2022-07-05 op #endif
92 329392cb 2022-07-05 op log_info("connections: %d", conn);
93 329392cb 2022-07-05 op }
94 b542d800 2021-11-20 op }
95 b542d800 2021-11-20 op
96 98c3d897 2022-07-06 op static int
97 b542d800 2021-11-20 op spawn_ssh(void)
98 b542d800 2021-11-20 op {
99 3376f40a 2021-11-25 op log_debug("spawning ssh");
100 b542d800 2021-11-20 op
101 b542d800 2021-11-20 op switch (ssh_pid = fork()) {
102 b542d800 2021-11-20 op case -1:
103 98c3d897 2022-07-06 op log_warnx("fork");
104 98c3d897 2022-07-06 op return -1;
105 b542d800 2021-11-20 op case 0:
106 9050e628 2022-07-30 op execl(SSH_PROG, "ssh", "-L", ssh_tflag, "-NTq", ssh_dest,
107 79313907 2021-11-23 op NULL);
108 3376f40a 2021-11-25 op fatal("exec");
109 b542d800 2021-11-20 op default:
110 98c3d897 2022-07-06 op return 0;
111 b542d800 2021-11-20 op }
112 b542d800 2021-11-20 op }
113 b542d800 2021-11-20 op
114 b542d800 2021-11-20 op static void
115 cfe57149 2022-07-30 op killing_time(int fd, short event, void *data)
116 cfe57149 2022-07-30 op {
117 cfe57149 2022-07-30 op if (ssh_pid == -1)
118 cfe57149 2022-07-30 op return;
119 cfe57149 2022-07-30 op
120 cfe57149 2022-07-30 op log_debug("timeout expired, killing ssh (%d)", ssh_pid);
121 cfe57149 2022-07-30 op kill(ssh_pid, SIGTERM);
122 cfe57149 2022-07-30 op ssh_pid = -1;
123 cfe57149 2022-07-30 op }
124 cfe57149 2022-07-30 op
125 cfe57149 2022-07-30 op void
126 c56ccc84 2022-07-05 op conn_free(struct conn *c)
127 c56ccc84 2022-07-05 op {
128 c56ccc84 2022-07-05 op if (c->sourcebev != NULL)
129 c56ccc84 2022-07-05 op bufferevent_free(c->sourcebev);
130 c56ccc84 2022-07-05 op if (c->tobev != NULL)
131 c56ccc84 2022-07-05 op bufferevent_free(c->tobev);
132 c56ccc84 2022-07-05 op
133 c56ccc84 2022-07-05 op if (evtimer_pending(&c->waitev, NULL))
134 c56ccc84 2022-07-05 op evtimer_del(&c->waitev);
135 c56ccc84 2022-07-05 op
136 c56ccc84 2022-07-05 op close(c->source);
137 8ae8b89f 2022-07-05 op if (c->to != -1)
138 8ae8b89f 2022-07-05 op close(c->to);
139 c56ccc84 2022-07-05 op
140 c56ccc84 2022-07-05 op free(c);
141 c56ccc84 2022-07-05 op
142 b542d800 2021-11-20 op if (--conn == 0) {
143 3376f40a 2021-11-25 op log_debug("scheduling ssh termination (%llds)",
144 ca125bba 2021-11-21 op (long long)timeout.tv_sec);
145 85b8fe26 2021-11-25 op if (timeout.tv_sec != 0) {
146 85b8fe26 2021-11-25 op evtimer_set(&timeoutev, killing_time, NULL);
147 85b8fe26 2021-11-25 op evtimer_add(&timeoutev, &timeout);
148 85b8fe26 2021-11-25 op }
149 b542d800 2021-11-20 op }
150 b542d800 2021-11-20 op }
151 b542d800 2021-11-20 op
152 b542d800 2021-11-20 op static int
153 b542d800 2021-11-20 op connect_to_ssh(void)
154 b542d800 2021-11-20 op {
155 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
156 b542d800 2021-11-20 op int r, saved_errno, sock;
157 1a869946 2021-11-23 op const char *cause;
158 b542d800 2021-11-20 op
159 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
160 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
161 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
162 b542d800 2021-11-20 op
163 1a869946 2021-11-23 op r = getaddrinfo(ssh_host, ssh_port, &hints, &res0);
164 c56ccc84 2022-07-05 op if (r != 0) {
165 c56ccc84 2022-07-05 op log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
166 1a869946 2021-11-23 op ssh_host, ssh_port, gai_strerror(r));
167 c56ccc84 2022-07-05 op return -1;
168 c56ccc84 2022-07-05 op }
169 b542d800 2021-11-20 op
170 b542d800 2021-11-20 op for (res = res0; res; res = res->ai_next) {
171 b542d800 2021-11-20 op sock = socket(res->ai_family, res->ai_socktype,
172 b542d800 2021-11-20 op res->ai_protocol);
173 b542d800 2021-11-20 op if (sock == -1) {
174 b542d800 2021-11-20 op cause = "socket";
175 b542d800 2021-11-20 op continue;
176 b542d800 2021-11-20 op }
177 b542d800 2021-11-20 op
178 b542d800 2021-11-20 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
179 b542d800 2021-11-20 op cause = "connect";
180 b542d800 2021-11-20 op saved_errno = errno;
181 b542d800 2021-11-20 op close(sock);
182 b542d800 2021-11-20 op errno = saved_errno;
183 b542d800 2021-11-20 op sock = -1;
184 b542d800 2021-11-20 op continue;
185 b542d800 2021-11-20 op }
186 b542d800 2021-11-20 op
187 b542d800 2021-11-20 op break;
188 b542d800 2021-11-20 op }
189 b542d800 2021-11-20 op
190 b542d800 2021-11-20 op if (sock == -1)
191 3376f40a 2021-11-25 op log_warn("%s", cause);
192 b542d800 2021-11-20 op
193 b542d800 2021-11-20 op freeaddrinfo(res0);
194 b542d800 2021-11-20 op return sock;
195 1b3b9dc3 2021-11-23 op }
196 1b3b9dc3 2021-11-23 op
197 1b3b9dc3 2021-11-23 op static void
198 1b3b9dc3 2021-11-23 op try_to_connect(int fd, short event, void *d)
199 1b3b9dc3 2021-11-23 op {
200 1b3b9dc3 2021-11-23 op struct conn *c = d;
201 1b3b9dc3 2021-11-23 op
202 ab4b8022 2022-07-06 op /* ssh may have died in the meantime */
203 1b3b9dc3 2021-11-23 op if (ssh_pid == -1) {
204 ab4b8022 2022-07-06 op conn_free(c);
205 1b3b9dc3 2021-11-23 op return;
206 1b3b9dc3 2021-11-23 op }
207 1b3b9dc3 2021-11-23 op
208 1b3b9dc3 2021-11-23 op c->ntentative++;
209 61461d5b 2021-12-01 op log_info("trying to connect to %s:%s (%d/%d)", ssh_host, ssh_port,
210 1b3b9dc3 2021-11-23 op c->ntentative, RETRIES);
211 1b3b9dc3 2021-11-23 op
212 1b3b9dc3 2021-11-23 op if ((c->to = connect_to_ssh()) == -1) {
213 1b3b9dc3 2021-11-23 op if (c->ntentative == RETRIES) {
214 3376f40a 2021-11-25 op log_warnx("giving up connecting");
215 ab4b8022 2022-07-06 op conn_free(c);
216 1b3b9dc3 2021-11-23 op return;
217 1b3b9dc3 2021-11-23 op }
218 1b3b9dc3 2021-11-23 op
219 1b3b9dc3 2021-11-23 op evtimer_set(&c->waitev, try_to_connect, c);
220 1b3b9dc3 2021-11-23 op evtimer_add(&c->waitev, &c->retry);
221 1b3b9dc3 2021-11-23 op return;
222 1b3b9dc3 2021-11-23 op }
223 53abf42e 2022-07-05 op
224 53abf42e 2022-07-05 op log_info("connected!");
225 1b3b9dc3 2021-11-23 op
226 cfe57149 2022-07-30 op if (conn_splice(c) == -1)
227 c56ccc84 2022-07-05 op conn_free(c);
228 b542d800 2021-11-20 op }
229 b542d800 2021-11-20 op
230 b542d800 2021-11-20 op static void
231 b542d800 2021-11-20 op do_accept(int fd, short event, void *data)
232 b542d800 2021-11-20 op {
233 82c452fa 2022-07-05 op struct conn *c;
234 82c452fa 2022-07-05 op int s;
235 b542d800 2021-11-20 op
236 3376f40a 2021-11-25 op log_debug("incoming connection");
237 b542d800 2021-11-20 op
238 0828da2c 2022-07-06 op if ((s = accept(fd, NULL, 0)) == -1) {
239 0828da2c 2022-07-06 op log_warn("accept");
240 0828da2c 2022-07-06 op return;
241 0828da2c 2022-07-06 op }
242 b542d800 2021-11-20 op
243 98c3d897 2022-07-06 op if (ssh_pid == -1 && spawn_ssh() == -1) {
244 98c3d897 2022-07-06 op close(s);
245 98c3d897 2022-07-06 op return;
246 98c3d897 2022-07-06 op }
247 b542d800 2021-11-20 op
248 82c452fa 2022-07-05 op if ((c = calloc(1, sizeof(*c))) == NULL) {
249 82c452fa 2022-07-05 op log_warn("calloc");
250 82c452fa 2022-07-05 op close(s);
251 82c452fa 2022-07-05 op return;
252 b542d800 2021-11-20 op }
253 82c452fa 2022-07-05 op
254 f56227b6 2022-07-06 op conn++;
255 f56227b6 2022-07-06 op if (evtimer_pending(&timeoutev, NULL))
256 f56227b6 2022-07-06 op evtimer_del(&timeoutev);
257 f56227b6 2022-07-06 op
258 82c452fa 2022-07-05 op c->source = s;
259 8ae8b89f 2022-07-05 op c->to = -1;
260 82c452fa 2022-07-05 op c->retry.tv_sec = BACKOFF;
261 82c452fa 2022-07-05 op evtimer_set(&c->waitev, try_to_connect, c);
262 82c452fa 2022-07-05 op evtimer_add(&c->waitev, &c->retry);
263 1a869946 2021-11-23 op }
264 1a869946 2021-11-23 op
265 1a869946 2021-11-23 op static const char *
266 1a869946 2021-11-23 op copysec(const char *s, char *d, size_t len)
267 1a869946 2021-11-23 op {
268 1a869946 2021-11-23 op const char *c;
269 1a869946 2021-11-23 op
270 1a869946 2021-11-23 op if ((c = strchr(s, ':')) == NULL)
271 1a869946 2021-11-23 op return NULL;
272 1a869946 2021-11-23 op if ((size_t)(c - s) >= len-1)
273 1a869946 2021-11-23 op return NULL;
274 1a869946 2021-11-23 op memset(d, 0, len);
275 1a869946 2021-11-23 op memcpy(d, s, c - s);
276 1a869946 2021-11-23 op return c;
277 b542d800 2021-11-20 op }
278 b542d800 2021-11-20 op
279 b542d800 2021-11-20 op static void
280 b542d800 2021-11-20 op bind_socket(void)
281 b542d800 2021-11-20 op {
282 b542d800 2021-11-20 op struct addrinfo hints, *res, *res0;
283 4a797971 2021-11-25 op int v, r, saved_errno;
284 b542d800 2021-11-20 op char host[64];
285 b542d800 2021-11-20 op const char *c, *h, *port, *cause;
286 b542d800 2021-11-20 op
287 b542d800 2021-11-20 op if ((c = strchr(addr, ':')) == NULL) {
288 d2d81217 2022-12-02 op h = "localhost";
289 b542d800 2021-11-20 op port = addr;
290 b542d800 2021-11-20 op } else {
291 1a869946 2021-11-23 op if ((c = copysec(addr, host, sizeof(host))) == NULL)
292 3376f40a 2021-11-25 op fatalx("name too long: %s", addr);
293 b542d800 2021-11-20 op
294 b542d800 2021-11-20 op h = host;
295 b542d800 2021-11-20 op port = c+1;
296 b542d800 2021-11-20 op }
297 b542d800 2021-11-20 op
298 b542d800 2021-11-20 op memset(&hints, 0, sizeof(hints));
299 b542d800 2021-11-20 op hints.ai_family = AF_UNSPEC;
300 b542d800 2021-11-20 op hints.ai_socktype = SOCK_STREAM;
301 b542d800 2021-11-20 op hints.ai_flags = AI_PASSIVE;
302 b542d800 2021-11-20 op
303 b542d800 2021-11-20 op r = getaddrinfo(h, port, &hints, &res0);
304 b542d800 2021-11-20 op if (r != 0)
305 3376f40a 2021-11-25 op fatalx("getaddrinfo(%s): %s", addr, gai_strerror(r));
306 b542d800 2021-11-20 op
307 b542d800 2021-11-20 op for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
308 b542d800 2021-11-20 op socks[nsock] = socket(res->ai_family, res->ai_socktype,
309 b542d800 2021-11-20 op res->ai_protocol);
310 b542d800 2021-11-20 op if (socks[nsock] == -1) {
311 b542d800 2021-11-20 op cause = "socket";
312 b542d800 2021-11-20 op continue;
313 b542d800 2021-11-20 op }
314 b542d800 2021-11-20 op
315 b542d800 2021-11-20 op if (bind(socks[nsock], res->ai_addr, res->ai_addrlen) == -1) {
316 b542d800 2021-11-20 op cause = "bind";
317 b542d800 2021-11-20 op saved_errno = errno;
318 b542d800 2021-11-20 op close(socks[nsock]);
319 b542d800 2021-11-20 op errno = saved_errno;
320 b542d800 2021-11-20 op continue;
321 b542d800 2021-11-20 op }
322 4a797971 2021-11-25 op
323 4a797971 2021-11-25 op v = 1;
324 4a797971 2021-11-25 op if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEADDR, &v,
325 4a797971 2021-11-25 op sizeof(v)) == -1)
326 4a797971 2021-11-25 op fatal("setsockopt(SO_REUSEADDR)");
327 b542d800 2021-11-20 op
328 4a797971 2021-11-25 op v = 1;
329 4a797971 2021-11-25 op if (setsockopt(socks[nsock], SOL_SOCKET, SO_REUSEPORT, &v,
330 4a797971 2021-11-25 op sizeof(v)) == -1)
331 4a797971 2021-11-25 op fatal("setsockopt(SO_REUSEPORT)");
332 4a797971 2021-11-25 op
333 b542d800 2021-11-20 op listen(socks[nsock], 5);
334 b542d800 2021-11-20 op
335 b542d800 2021-11-20 op nsock++;
336 b542d800 2021-11-20 op }
337 b542d800 2021-11-20 op if (nsock == 0)
338 3376f40a 2021-11-25 op fatal("%s", cause);
339 b542d800 2021-11-20 op
340 b542d800 2021-11-20 op freeaddrinfo(res0);
341 b542d800 2021-11-20 op }
342 b542d800 2021-11-20 op
343 1a869946 2021-11-23 op static void
344 8a8fe09b 2022-07-05 op parse_sshaddr(void)
345 1a869946 2021-11-23 op {
346 1a869946 2021-11-23 op const char *c;
347 1a869946 2021-11-23 op
348 1d9a178e 2022-07-05 op if (isdigit((unsigned char)*ssh_tflag)) {
349 1a869946 2021-11-23 op strlcpy(ssh_host, "localhost", sizeof(ssh_host));
350 79313907 2021-11-23 op if (copysec(ssh_tflag, ssh_port, sizeof(ssh_port)) == NULL)
351 1a869946 2021-11-23 op goto err;
352 1a869946 2021-11-23 op return;
353 1a869946 2021-11-23 op }
354 1a869946 2021-11-23 op
355 79313907 2021-11-23 op if ((c = copysec(ssh_tflag, ssh_host, sizeof(ssh_host))) == NULL)
356 1a869946 2021-11-23 op goto err;
357 1a869946 2021-11-23 op if (copysec(c+1, ssh_port, sizeof(ssh_port)) == NULL)
358 1a869946 2021-11-23 op goto err;
359 1a869946 2021-11-23 op return;
360 1a869946 2021-11-23 op
361 1a869946 2021-11-23 op err:
362 039c0887 2022-07-06 op fatalx("wrong value for -B");
363 1a869946 2021-11-23 op }
364 1a869946 2021-11-23 op
365 9ac6c545 2021-11-23 op static void __dead
366 9ac6c545 2021-11-23 op usage(void)
367 9ac6c545 2021-11-23 op {
368 3376f40a 2021-11-25 op fprintf(stderr, "usage: %s [-dv] -B sshaddr -b addr [-t timeout]"
369 9ac6c545 2021-11-23 op " destination\n", getprogname());
370 9ac6c545 2021-11-23 op exit(1);
371 9ac6c545 2021-11-23 op }
372 9ac6c545 2021-11-23 op
373 b542d800 2021-11-20 op int
374 b542d800 2021-11-20 op main(int argc, char **argv)
375 b542d800 2021-11-20 op {
376 eb1810c9 2022-07-05 op int ch, i, fd;
377 b542d800 2021-11-20 op const char *errstr;
378 eb1810c9 2022-07-05 op struct stat sb;
379 b542d800 2021-11-20 op
380 eb1810c9 2022-07-05 op /*
381 eb1810c9 2022-07-05 op * Ensure we have fds 0-2 open so that we have no issue with
382 eb1810c9 2022-07-05 op * calling bind_socket before daemon(3).
383 eb1810c9 2022-07-05 op */
384 eb1810c9 2022-07-05 op for (i = 0; i < 3; ++i) {
385 eb1810c9 2022-07-05 op if (fstat(i, &sb) == -1) {
386 eb1810c9 2022-07-05 op if ((fd = open("/dev/null", O_RDWR)) != -1) {
387 eb1810c9 2022-07-05 op if (dup2(fd, i) == -1)
388 eb1810c9 2022-07-05 op exit(1);
389 eb1810c9 2022-07-05 op if (fd > i)
390 eb1810c9 2022-07-05 op close(fd);
391 eb1810c9 2022-07-05 op } else
392 eb1810c9 2022-07-05 op exit(1);
393 eb1810c9 2022-07-05 op }
394 eb1810c9 2022-07-05 op }
395 eb1810c9 2022-07-05 op
396 3376f40a 2021-11-25 op log_init(1, LOG_DAEMON);
397 3376f40a 2021-11-25 op log_setverbose(1);
398 3376f40a 2021-11-25 op
399 3376f40a 2021-11-25 op while ((ch = getopt(argc, argv, "B:b:dt:v")) != -1) {
400 b542d800 2021-11-20 op switch (ch) {
401 b542d800 2021-11-20 op case 'B':
402 79313907 2021-11-23 op ssh_tflag = optarg;
403 8a8fe09b 2022-07-05 op parse_sshaddr();
404 b542d800 2021-11-20 op break;
405 b542d800 2021-11-20 op case 'b':
406 b542d800 2021-11-20 op addr = optarg;
407 b542d800 2021-11-20 op break;
408 3376f40a 2021-11-25 op case 'd':
409 3376f40a 2021-11-25 op debug = 1;
410 3376f40a 2021-11-25 op break;
411 b542d800 2021-11-20 op case 't':
412 85b8fe26 2021-11-25 op timeout.tv_sec = strtonum(optarg, 0, INT_MAX, &errstr);
413 b542d800 2021-11-20 op if (errstr != NULL)
414 3376f40a 2021-11-25 op fatalx("timeout is %s: %s", errstr, optarg);
415 b542d800 2021-11-20 op break;
416 3376f40a 2021-11-25 op case 'v':
417 3376f40a 2021-11-25 op verbose = 1;
418 3376f40a 2021-11-25 op break;
419 b542d800 2021-11-20 op default:
420 b542d800 2021-11-20 op usage();
421 b542d800 2021-11-20 op }
422 b542d800 2021-11-20 op }
423 b542d800 2021-11-20 op argc -= optind;
424 b542d800 2021-11-20 op argv += optind;
425 b542d800 2021-11-20 op
426 79313907 2021-11-23 op if (argc != 1 || addr == NULL || ssh_tflag == NULL)
427 b542d800 2021-11-20 op usage();
428 b542d800 2021-11-20 op
429 b542d800 2021-11-20 op ssh_dest = argv[0];
430 b542d800 2021-11-20 op
431 eb1810c9 2022-07-05 op bind_socket();
432 eb1810c9 2022-07-05 op
433 3376f40a 2021-11-25 op log_init(debug, LOG_DAEMON);
434 3376f40a 2021-11-25 op log_setverbose(verbose);
435 3376f40a 2021-11-25 op
436 3376f40a 2021-11-25 op if (!debug)
437 3376f40a 2021-11-25 op daemon(1, 0);
438 3376f40a 2021-11-25 op
439 b542d800 2021-11-20 op signal(SIGPIPE, SIG_IGN);
440 b542d800 2021-11-20 op
441 b542d800 2021-11-20 op event_init();
442 b542d800 2021-11-20 op
443 b542d800 2021-11-20 op /* initialize the timer */
444 b542d800 2021-11-20 op evtimer_set(&timeoutev, killing_time, NULL);
445 b542d800 2021-11-20 op
446 329392cb 2022-07-05 op signal_set(&sighupev, SIGHUP, sig_handler, NULL);
447 329392cb 2022-07-05 op signal_set(&sigintev, SIGINT, sig_handler, NULL);
448 329392cb 2022-07-05 op signal_set(&sigtermev, SIGTERM, sig_handler, NULL);
449 329392cb 2022-07-05 op signal_set(&sigchldev, SIGCHLD, sig_handler, NULL);
450 047fe0f8 2021-11-27 op #ifdef SIGINFO
451 329392cb 2022-07-05 op signal_set(&siginfoev, SIGINFO, sig_handler, NULL);
452 047fe0f8 2021-11-27 op #else
453 329392cb 2022-07-05 op signal_set(&siginfoev, SIGUSR1, sig_handler, NULL);
454 047fe0f8 2021-11-27 op #endif
455 b542d800 2021-11-20 op
456 b542d800 2021-11-20 op signal_add(&sighupev, NULL);
457 b542d800 2021-11-20 op signal_add(&sigintev, NULL);
458 b542d800 2021-11-20 op signal_add(&sigtermev, NULL);
459 b542d800 2021-11-20 op signal_add(&sigchldev, NULL);
460 b542d800 2021-11-20 op signal_add(&siginfoev, NULL);
461 b542d800 2021-11-20 op
462 b542d800 2021-11-20 op for (i = 0; i < nsock; ++i) {
463 b542d800 2021-11-20 op event_set(&sockev[i], socks[i], EV_READ|EV_PERSIST,
464 b542d800 2021-11-20 op do_accept, NULL);
465 b542d800 2021-11-20 op event_add(&sockev[i], NULL);
466 b542d800 2021-11-20 op }
467 4844aeaf 2022-07-06 op
468 9050e628 2022-07-30 op if (unveil(SSH_PROG, "x") == -1)
469 9050e628 2022-07-30 op fatal("unveil(%s)", SSH_PROG);
470 b542d800 2021-11-20 op
471 b542d800 2021-11-20 op /*
472 b542d800 2021-11-20 op * dns, inet: bind the socket and connect to the childs.
473 b542d800 2021-11-20 op * proc, exec: execute ssh on demand.
474 b542d800 2021-11-20 op */
475 b542d800 2021-11-20 op if (pledge("stdio dns inet proc exec", NULL) == -1)
476 3376f40a 2021-11-25 op fatal("pledge");
477 b542d800 2021-11-20 op
478 3376f40a 2021-11-25 op log_info("starting");
479 b542d800 2021-11-20 op event_dispatch();
480 b542d800 2021-11-20 op
481 b542d800 2021-11-20 op if (ssh_pid != -1)
482 b542d800 2021-11-20 op kill(ssh_pid, SIGINT);
483 b542d800 2021-11-20 op
484 b542d800 2021-11-20 op return 0;
485 b542d800 2021-11-20 op }