OpenVPN
ps.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#if PORT_SHARE
30
31#include "event.h"
32#include "socket.h"
33#include "fdmisc.h"
34#include "crypto.h"
35#include "resolve.h"
36#include "ps.h"
37
38#include "memdbg.h"
39
40struct port_share *port_share = NULL; /* GLOBAL */
41
42/* size of i/o buffers */
43#define PROXY_CONNECTION_BUFFER_SIZE 1500
44
45/* Command codes for foreground -> background communication */
46#define COMMAND_REDIRECT 10
47#define COMMAND_EXIT 11
48
49/* Response codes for background -> foreground communication */
50#define RESPONSE_INIT_SUCCEEDED 20
51#define RESPONSE_INIT_FAILED 21
52
53/*
54 * Return values for proxy_connection_io functions
55 */
56
57#define IOSTAT_EAGAIN_ON_READ 0 /* recv returned EAGAIN */
58#define IOSTAT_EAGAIN_ON_WRITE 1 /* send returned EAGAIN */
59#define IOSTAT_READ_ERROR 2 /* the other end of our read socket (pc) was closed */
60#define IOSTAT_WRITE_ERROR 3 /* the other end of our write socket (pc->counterpart) was closed */
61#define IOSTAT_GOOD 4 /* nothing to report */
62
63/*
64 * A foreign (non-OpenVPN) connection we are proxying,
65 * usually HTTPS
66 */
67struct proxy_connection
68{
69 bool defined;
70 struct proxy_connection *next;
71 struct proxy_connection *counterpart;
72 struct buffer buf;
73 bool buffer_initial;
74 unsigned int rwflags;
75 int sd;
76 char *jfn;
77};
78
79static inline void
81{
82 if (socket_defined(sd))
83 {
85 }
86}
87
88/*
89 * Close most of parent's fds.
90 * Keep stdin/stdout/stderr, plus one
91 * other fd which is presumed to be
92 * our pipe back to parent.
93 * Admittedly, a bit of a kludge,
94 * but posix doesn't give us a kind
95 * of FD_CLOEXEC which will stop
96 * fds from crossing a fork().
97 */
98static void
100{
102 closelog();
103 for (i = 3; i <= 100; ++i)
104 {
105 if (i != keep)
106 {
108 }
109 }
110}
111
112/*
113 * Usually we ignore signals, because our parent will
114 * deal with them.
115 */
116static void
117set_signals(void)
118{
120
126}
127
128/*
129 * Socket read/write functions.
130 */
131
132static int
134{
135 unsigned char c;
136 const ssize_t size = read(fd, &c, sizeof(c));
137 if (size == sizeof(c))
138 {
139 return c;
140 }
141 else
142 {
143 return -1;
144 }
145}
146
147static int
149{
150 unsigned char c = (unsigned char)code;
151 const ssize_t size = write(fd, &c, sizeof(c));
152 if (size == sizeof(c))
153 {
154 return (int)size;
155 }
156 else
157 {
158 return -1;
159 }
160}
161
162static socklen_t
163cmsg_size(void)
164{
165 return CMSG_SPACE(sizeof(socket_descriptor_t));
166}
167
168#if defined(__GNUC__) || defined(__clang__)
169#pragma GCC diagnostic push
170#pragma GCC diagnostic ignored "-Wsign-conversion"
171#endif
172
173/*
174 * Send a command (char), data (head), and a file descriptor (sd_send) to a local process
175 * over unix socket sd. Unfortunately, there's no portable way to send file descriptors
176 * to other processes, so this code, as well as its analog (control_message_from_parent below),
177 * is Linux-specific. This function runs in the context of the main process and is used to
178 * send commands, data, and file descriptors to the background process.
179 */
180static void
181port_share_sendmsg(const socket_descriptor_t sd, const char command, const struct buffer *head,
183{
184 if (socket_defined(sd))
185 {
186 struct msghdr mesg;
187 struct cmsghdr *h;
188 struct iovec iov[2];
190 char cmd;
191 ssize_t status;
192
193 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE: sendmsg sd=%d len=%d", (int)sd_send,
194 head ? BLEN(head) : -1);
195
196 CLEAR(mesg);
197
198 cmd = command;
199
200 iov[0].iov_base = &cmd;
201 iov[0].iov_len = sizeof(cmd);
202 mesg.msg_iovlen = 1;
203
204 if (head)
205 {
206 /* sendmsg takes a const msghdr, but we can't construct that here
207 directly, so cast */
208 iov[1].iov_base = (char *)CBPTR(head);
209 iov[1].iov_len = BLENZ(head);
210 mesg.msg_iovlen = 2;
211 }
212
213 mesg.msg_iov = iov;
214
215 mesg.msg_controllen = cmsg_size();
216 mesg.msg_control = (char *)malloc(mesg.msg_controllen);
217 check_malloc_return(mesg.msg_control);
218 mesg.msg_flags = 0;
219
220 h = CMSG_FIRSTHDR(&mesg);
221 h->cmsg_level = SOL_SOCKET;
222 h->cmsg_type = SCM_RIGHTS;
223 h->cmsg_len = CMSG_LEN(sizeof(socket_descriptor_t));
224
225 if (socket_defined(sd_send))
226 {
227 memcpy(CMSG_DATA(h), &sd_send, sizeof(sd_send));
228 }
229 else
230 {
231 socketpair(PF_UNIX, SOCK_DGRAM, 0, sd_null);
232 memcpy(CMSG_DATA(h), &sd_null[0], sizeof(sd_null[0]));
233 }
234
235 status = sendmsg(sd, &mesg, MSG_NOSIGNAL);
236 if (status == -1)
237 {
239 "PORT SHARE: sendmsg failed -- unable to communicate with background process (%d,%d,%d,%d)",
240 sd, sd_send, sd_null[0], sd_null[1]);
241 }
242
243 close_socket_if_defined(sd_null[0]);
244 close_socket_if_defined(sd_null[1]);
245 free(mesg.msg_control);
246 }
247}
248
249static void
250proxy_entry_close_sd(struct proxy_connection *pc, struct event_set *es)
251{
252 if (pc->defined && socket_defined(pc->sd))
253 {
254 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: delete sd=%d", (int)pc->sd);
255 if (es)
256 {
257 event_del(es, pc->sd);
258 }
259 openvpn_close_socket(pc->sd);
260 pc->sd = SOCKET_UNDEFINED;
261 }
262}
263
264/*
265 * Mark a proxy entry and its counterpart for close.
266 */
267static void
268proxy_entry_mark_for_close(struct proxy_connection *pc, struct event_set *es)
269{
270 if (pc->defined)
271 {
272 struct proxy_connection *cp = pc->counterpart;
273 proxy_entry_close_sd(pc, es);
274 free_buf(&pc->buf);
275 pc->buffer_initial = false;
276 pc->rwflags = 0;
277 pc->defined = false;
278 if (pc->jfn)
279 {
280 unlink(pc->jfn);
281 free(pc->jfn);
282 pc->jfn = NULL;
283 }
284 if (cp && cp->defined && cp->counterpart == pc)
285 {
286 proxy_entry_mark_for_close(cp, es);
287 }
288 }
289}
290
291/*
292 * Run through the proxy entry list and delete all entries marked
293 * for close.
294 */
295static void
296proxy_list_housekeeping(struct proxy_connection **list)
297{
298 if (list)
299 {
300 struct proxy_connection *prev = NULL;
301 struct proxy_connection *pc = *list;
302
303 while (pc)
304 {
305 struct proxy_connection *next = pc->next;
306 if (!pc->defined)
307 {
308 free(pc);
309 if (prev)
310 {
311 prev->next = next;
312 }
313 else
314 {
315 *list = next;
316 }
317 }
318 else
319 {
320 prev = pc;
321 }
322 pc = next;
323 }
324 }
325}
326
327/*
328 * Record IP/port of client in filesystem, so that server receiving
329 * the proxy can determine true client origin.
330 */
331static void
332journal_add(const char *journal_dir, const struct proxy_connection *pc, struct proxy_connection *cp)
333{
334 struct openvpn_sockaddr from, to;
335
336 socklen_t slen = sizeof(from.addr);
337 socklen_t dlen = sizeof(to.addr);
338 if (!getpeername(pc->sd, (struct sockaddr *)&from.addr.sa, &slen)
339 && !getsockname(cp->sd, (struct sockaddr *)&to.addr.sa, &dlen))
340 {
341 struct gc_arena gc = gc_new();
342 const char *f = print_openvpn_sockaddr(&from, &gc);
343 const char *t = print_openvpn_sockaddr(&to, &gc);
344 size_t fnlen = strlen(journal_dir) + strlen(t) + 2;
345 char *jfn = (char *)malloc(fnlen);
347 snprintf(jfn, fnlen, "%s/%s", journal_dir, t);
348 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: client origin %s -> %s", jfn, f);
349 /* never write through a pre-existing symlink or hardlink */
350 const int flags = O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW;
351 int fd = platform_open(jfn, flags, S_IRUSR | S_IWUSR | S_IRGRP);
352 if (fd == -1 && errno == EEXIST)
353 {
354 /* stale entry, e.g. left behind by a killed proxy process */
355 unlink(jfn);
356 fd = platform_open(jfn, flags, S_IRUSR | S_IWUSR | S_IRGRP);
357 }
358 if (fd != -1)
359 {
360 const size_t write_len = strlen(f);
361 if (write(fd, f, write_len) != (ssize_t)write_len)
362 {
363 msg(M_WARN, "PORT SHARE: writing to journal file (%s) failed", jfn);
364 }
365 close(fd);
366 cp->jfn = jfn;
367 }
368 else
369 {
370 msg(M_WARN | M_ERRNO, "PORT SHARE: unable to write journal file in %s", jfn);
371 free(jfn);
372 }
373 gc_free(&gc);
374 }
375}
376
377/*
378 * Cleanup function, on proxy process exit.
379 */
380static void
381proxy_list_close(struct proxy_connection **list)
382{
383 if (list)
384 {
385 struct proxy_connection *pc = *list;
386 while (pc)
387 {
388 proxy_entry_mark_for_close(pc, NULL);
389 pc = pc->next;
390 }
391 proxy_list_housekeeping(list);
392 }
393}
394
395static inline void
396proxy_connection_io_requeue(struct proxy_connection *pc, const unsigned int rwflags_new,
397 struct event_set *es)
398{
399 if (socket_defined(pc->sd) && pc->rwflags != rwflags_new)
400 {
401 /*dmsg (D_PS_PROXY_DEBUG, "PORT SHARE PROXY: requeue[%d] rwflags=%u", (int)pc->sd,
402 * rwflags_new);*/
403 event_ctl(es, pc->sd, rwflags_new, (void *)pc);
404 pc->rwflags = rwflags_new;
405 }
406}
407
408/*
409 * Create a new pair of proxy_connection entries, one for each
410 * socket file descriptor involved in the proxy. We are given
411 * the client fd, and we should derive our own server fd by connecting
412 * to the server given by server_addr/server_port. Return true
413 * on success and false on failure to connect to server.
414 */
415static bool
416proxy_entry_new(struct proxy_connection **list, struct event_set *es,
417 const struct openvpn_sockaddr server_addr, const socket_descriptor_t sd_client,
418 const struct buffer *initial_data, const char *journal_dir)
419{
420 socket_descriptor_t sd_server;
421 int status;
422 struct proxy_connection *pc;
423 struct proxy_connection *cp;
424
425 /* connect to port share server */
426 if ((sd_server = socket(server_addr.addr.sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
427 {
428 msg(M_WARN | M_ERRNO, "PORT SHARE PROXY: cannot create socket");
429 return false;
430 }
431 status = openvpn_connect(sd_server, &server_addr.addr.sa, 5, NULL);
432 if (status)
433 {
434 msg(M_WARN, "PORT SHARE PROXY: connect to port-share server failed");
435 openvpn_close_socket(sd_server);
436 return false;
437 }
438 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: connect to port-share server succeeded");
439
440 set_nonblock(sd_client);
441 set_nonblock(sd_server);
442
443 /* allocate 2 new proxy_connection objects */
444 ALLOC_OBJ_CLEAR(pc, struct proxy_connection);
445 ALLOC_OBJ_CLEAR(cp, struct proxy_connection);
446
447 /* client object */
448 pc->defined = true;
449 pc->next = cp;
450 pc->counterpart = cp;
451 pc->buf = *initial_data;
452 pc->buffer_initial = true;
453 pc->rwflags = EVENT_UNDEF;
454 pc->sd = sd_client;
455
456 /* server object */
457 cp->defined = true;
458 cp->next = *list;
459 cp->counterpart = pc;
460 cp->buf = alloc_buf(PROXY_CONNECTION_BUFFER_SIZE);
461 cp->buffer_initial = false;
462 cp->rwflags = EVENT_UNDEF;
463 cp->sd = sd_server;
464
465 /* add to list */
466 *list = pc;
467
468 /* add journal entry */
469 if (journal_dir)
470 {
471 journal_add(journal_dir, pc, cp);
472 }
473
474 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: NEW CONNECTION [c=%d s=%d]", (int)sd_client,
475 (int)sd_server);
476
477 /* set initial i/o states */
478 proxy_connection_io_requeue(pc, EVENT_READ, es);
479 proxy_connection_io_requeue(cp, EVENT_READ | EVENT_WRITE, es);
480
481 return true;
482}
483
484/*
485 * This function runs in the context of the background proxy process.
486 * Receive a control message from the parent (sent by the port_share_sendmsg
487 * function above) and act on it. Return false if the proxy process should
488 * exit, true otherwise.
489 */
490static bool
491control_message_from_parent(const socket_descriptor_t sd_control, struct proxy_connection **list,
492 struct event_set *es, const struct openvpn_sockaddr server_addr,
493 const int max_initial_buf, const char *journal_dir)
494{
495 /* this buffer needs to be large enough to handle the largest buffer
496 * that might be returned by the link_socket_read call in read_incoming_link. */
497 struct buffer buf = alloc_buf(max_initial_buf);
498
499 struct msghdr mesg;
500 struct cmsghdr *h;
501 struct iovec iov[2];
502 char command = 0;
503 ssize_t status;
504 int ret = true;
505
506 CLEAR(mesg);
507
508 iov[0].iov_base = &command;
509 iov[0].iov_len = sizeof(command);
510 iov[1].iov_base = BPTR(&buf);
511 iov[1].iov_len = BCAP(&buf);
512 mesg.msg_iov = iov;
513 mesg.msg_iovlen = 2;
514
515 mesg.msg_controllen = cmsg_size();
516 mesg.msg_control = (char *)malloc(mesg.msg_controllen);
517 check_malloc_return(mesg.msg_control);
518 mesg.msg_flags = 0;
519
520 h = CMSG_FIRSTHDR(&mesg);
521 h->cmsg_len = CMSG_LEN(sizeof(socket_descriptor_t));
522 h->cmsg_level = SOL_SOCKET;
523 h->cmsg_type = SCM_RIGHTS;
524 static const socket_descriptor_t socket_undefined = SOCKET_UNDEFINED;
525 memcpy(CMSG_DATA(h), &socket_undefined, sizeof(socket_undefined));
526
527 status = recvmsg(sd_control, &mesg, MSG_NOSIGNAL);
528 if (status != -1)
529 {
530 if (h == NULL || h->cmsg_len != CMSG_LEN(sizeof(socket_descriptor_t))
531 || h->cmsg_level != SOL_SOCKET || h->cmsg_type != SCM_RIGHTS)
532 {
533 msg(M_WARN, "PORT SHARE PROXY: received unknown message");
534 }
535 else
536 {
537 socket_descriptor_t received_fd;
538 memcpy(&received_fd, CMSG_DATA(h), sizeof(received_fd));
539 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: RECEIVED sd=%d", (int)received_fd);
540
541 if (status >= 2 && command == COMMAND_REDIRECT)
542 {
543 buf.len = (int)status - 1;
544 if (proxy_entry_new(list, es, server_addr, received_fd, &buf, journal_dir))
545 {
546 CLEAR(buf); /* we gave the buffer to proxy_entry_new */
547 }
548 else
549 {
550 openvpn_close_socket(received_fd);
551 }
552 }
553 else if (status >= 1 && command == COMMAND_EXIT)
554 {
555 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: RECEIVED COMMAND_EXIT");
556 openvpn_close_socket(received_fd); /* null socket */
557 ret = false;
558 }
559 }
560 }
561 free(mesg.msg_control);
562 free_buf(&buf);
563 return ret;
564}
565
566static int
567proxy_connection_io_recv(struct proxy_connection *pc)
568{
569 /* recv data from socket */
570 const ssize_t status = recv(pc->sd, BPTR(&pc->buf), BCAP(&pc->buf), MSG_NOSIGNAL);
571 if (status < 0)
572 {
573 return (errno == EAGAIN) ? IOSTAT_EAGAIN_ON_READ : IOSTAT_READ_ERROR;
574 }
575 else
576 {
577 if (!status)
578 {
579 return IOSTAT_READ_ERROR;
580 }
581 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: read[%d] %zd", (int)pc->sd, status);
582 pc->buf.len = (int)status;
583 }
584 return IOSTAT_GOOD;
585}
586
587static int
588proxy_connection_io_send(struct proxy_connection *pc, int *bytes_sent)
589{
590 const socket_descriptor_t sd = pc->counterpart->sd;
591 const ssize_t status = send(sd, BPTR(&pc->buf), BLENZ(&pc->buf), MSG_NOSIGNAL);
592
593 if (status < 0)
594 {
595 const int e = errno;
596 return (e == EAGAIN) ? IOSTAT_EAGAIN_ON_WRITE : IOSTAT_WRITE_ERROR;
597 }
598 else
599 {
600 *bytes_sent += (int)status;
601 if (status != pc->buf.len)
602 {
603 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: partial write[%d], tried=%d got=%zd", (int)sd,
604 pc->buf.len, status);
605 buf_advance(&pc->buf, status);
606 return IOSTAT_EAGAIN_ON_WRITE;
607 }
608 else
609 {
610 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: wrote[%d] %zd", (int)sd, status);
611 pc->buf.len = 0;
612 pc->buf.offset = 0;
613 }
614 }
615
616 /* realloc send buffer after initial send */
617 if (pc->buffer_initial)
618 {
619 free_buf(&pc->buf);
620 pc->buf = alloc_buf(PROXY_CONNECTION_BUFFER_SIZE);
621 pc->buffer_initial = false;
622 }
623 return IOSTAT_GOOD;
624}
625
626#if defined(__GNUC__) || defined(__clang__)
627#pragma GCC diagnostic pop
628#endif
629
630/*
631 * Forward data from pc to pc->counterpart.
632 */
633
634static int
635proxy_connection_io_xfer(struct proxy_connection *pc, const int max_transfer)
636{
637 int transferred = 0;
638 while (transferred < max_transfer)
639 {
640 if (!BLEN(&pc->buf))
641 {
642 const int status = proxy_connection_io_recv(pc);
643 if (status != IOSTAT_GOOD)
644 {
645 return status;
646 }
647 }
648
649 if (BLEN(&pc->buf))
650 {
651 const int status = proxy_connection_io_send(pc, &transferred);
652 if (status != IOSTAT_GOOD)
653 {
654 return status;
655 }
656 }
657 }
658 return IOSTAT_EAGAIN_ON_READ;
659}
660
661/*
662 * Decide how the receipt of an EAGAIN status should affect our next IO queueing.
663 */
664static bool
665proxy_connection_io_status(const int status, unsigned int *rwflags_pc, unsigned int *rwflags_cp)
666{
667 switch (status)
668 {
669 case IOSTAT_EAGAIN_ON_READ:
670 *rwflags_pc |= EVENT_READ;
671 *rwflags_cp &= ~EVENT_WRITE;
672 return true;
673
674 case IOSTAT_EAGAIN_ON_WRITE:
675 *rwflags_pc &= ~EVENT_READ;
676 *rwflags_cp |= EVENT_WRITE;
677 return true;
678
679 case IOSTAT_READ_ERROR:
680 return false;
681
682 case IOSTAT_WRITE_ERROR:
683 return false;
684
685 default:
686 msg(M_FATAL, "PORT SHARE PROXY: unexpected status=%d", status);
687 }
688 return false; /* NOTREACHED */
689}
690
691/*
692 * Dispatch function for forwarding data between the two socket fds involved
693 * in the proxied connection.
694 */
695static int
696proxy_connection_io_dispatch(struct proxy_connection *pc, const unsigned int rwflags,
697 struct event_set *es)
698{
699 const int max_transfer_per_iteration = 10000;
700 struct proxy_connection *cp = pc->counterpart;
701 unsigned int rwflags_pc = pc->rwflags;
702 unsigned int rwflags_cp = cp->rwflags;
703
704 ASSERT(pc->defined && cp->defined && cp->counterpart == pc);
705
706 if (rwflags & EVENT_READ)
707 {
708 const int status = proxy_connection_io_xfer(pc, max_transfer_per_iteration);
709 if (!proxy_connection_io_status(status, &rwflags_pc, &rwflags_cp))
710 {
711 goto bad;
712 }
713 }
714 if (rwflags & EVENT_WRITE)
715 {
716 const int status = proxy_connection_io_xfer(cp, max_transfer_per_iteration);
717 if (!proxy_connection_io_status(status, &rwflags_cp, &rwflags_pc))
718 {
719 goto bad;
720 }
721 }
722 proxy_connection_io_requeue(pc, rwflags_pc, es);
723 proxy_connection_io_requeue(cp, rwflags_cp, es);
724
725 return true;
726
727bad:
728 proxy_entry_mark_for_close(pc, es);
729 return false;
730}
731
732/*
733 * This is the main function for the port share proxy background process.
734 */
735static void
736port_share_proxy(const struct openvpn_sockaddr hostaddr, const socket_descriptor_t sd_control,
737 const int max_initial_buf, const char *journal_dir)
738{
739 if (send_control(sd_control, RESPONSE_INIT_SUCCEEDED) >= 0)
740 {
741 void *sd_control_marker = (void *)1;
742 int maxevents = 256;
743 struct event_set *es;
744 struct event_set_return esr[64];
745 struct proxy_connection *list = NULL;
746 time_t last_housekeeping = 0;
747
748 msg(D_PS_PROXY, "PORT SHARE PROXY: proxy starting");
749
750 es = event_set_init(&maxevents, 0);
751 event_ctl(es, sd_control, EVENT_READ, sd_control_marker);
752 while (true)
753 {
754 int n_events;
755 struct timeval tv;
756 time_t current;
757
758 tv.tv_sec = 10;
759 tv.tv_usec = 0;
760 n_events = event_wait(es, &tv, esr, SIZE(esr));
761 /*dmsg (D_PS_PROXY_DEBUG, "PORT SHARE PROXY: event_wait returned %d", n_events);*/
762 current = time(NULL);
763 if (n_events > 0)
764 {
765 int i;
766 for (i = 0; i < n_events; ++i)
767 {
768 const struct event_set_return *e = &esr[i];
769 if (e->arg == sd_control_marker)
770 {
771 if (!control_message_from_parent(sd_control, &list, es, hostaddr,
772 max_initial_buf, journal_dir))
773 {
774 goto done;
775 }
776 }
777 else
778 {
779 struct proxy_connection *pc = (struct proxy_connection *)e->arg;
780 if (pc->defined)
781 {
782 proxy_connection_io_dispatch(pc, e->rwflags, es);
783 }
784 }
785 }
786 }
787 else if (n_events < 0)
788 {
789 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: event_wait failed");
790 }
791 if (current > last_housekeeping)
792 {
793 proxy_list_housekeeping(&list);
794 last_housekeeping = current;
795 }
796 }
797
798done:
799 proxy_list_close(&list);
800 event_free(es);
801 }
802 msg(M_INFO, "PORT SHARE PROXY: proxy exiting");
803}
804
805/*
806 * Called from the main OpenVPN process to enable the port
807 * share proxy.
808 */
809struct port_share *
810port_share_open(const char *host, const char *port, const int max_initial_buf,
811 const char *journal_dir)
812{
814 struct openvpn_sockaddr hostaddr;
815 struct port_share *ps;
816
817 ALLOC_OBJ_CLEAR(ps, struct port_share);
818 ps->foreground_fd = -1;
819 ps->background_pid = -1;
820
821 /*
822 * Get host's IP address
823 */
824 struct addrinfo *ai;
825 int ga_status = openvpn_getaddrinfo(GETADDR_RESOLVE | GETADDR_FATAL, host, port,
826 0, NULL, AF_UNSPEC, &ai);
827 ASSERT(ga_status == 0);
828 ASSERT(sizeof(hostaddr.addr) >= ai->ai_addrlen);
829 memcpy(&hostaddr.addr.sa, ai->ai_addr, ai->ai_addrlen);
830 freeaddrinfo(ai);
831
833 {
834 struct gc_arena gc = gc_new();
835 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: receiver will be %s",
836 print_openvpn_sockaddr(&hostaddr, &gc));
837 gc_free(&gc);
838 }
839
840 /*
841 * Make a socket for foreground and background processes
842 * to communicate.
843 */
844 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) == -1)
845 {
846 msg(M_WARN | M_ERRNO, "PORT SHARE: socketpair call failed");
847 goto error;
848 }
849
850 /*
851 * Fork off background proxy process.
852 */
853 pid_t pid = fork();
854
855 if (pid < 0)
856 {
857 msg(M_WARN | M_ERRNO, "PORT SHARE: fork failed");
858 goto error;
859 }
860 else if (pid)
861 {
862 /*
863 * Foreground Process
864 */
865
866 ps->background_pid = pid;
867
868 /* close our copy of child's socket */
870
871 /* don't let future subprocesses inherit child socket */
872 set_cloexec(fd[0]);
873
874 /* wait for background child process to initialize */
875 int recv_status = recv_control(fd[0]);
876 if (recv_status == RESPONSE_INIT_SUCCEEDED)
877 {
878 /* note that this will cause possible EAGAIN when writing to
879 * control socket if proxy process is backlogged */
880 set_nonblock(fd[0]);
881
882 ps->foreground_fd = fd[0];
883 return ps;
884 }
885 else
886 {
887 msg(M_ERR, "PORT SHARE: unexpected init recv_control status=%d", recv_status);
888 }
889 }
890 else
891 {
892 /*
893 * Background Process
894 */
895
896 /* Ignore most signals (the parent will receive them) */
897 set_signals();
898
899 /* Let msg know that we forked */
900 msg_forked();
901
902#ifdef ENABLE_MANAGEMENT
903 /* Don't interact with management interface */
904 management = NULL;
905#endif
906
907 /* close all parent fds except our socket back to parent */
908 close_fds_except(fd[1]);
909
910 /* no blocking on control channel back to parent */
911 set_nonblock(fd[1]);
912
913 /* execute the event loop */
914 port_share_proxy(hostaddr, fd[1], max_initial_buf, journal_dir);
915
917
918 exit(0);
919 return NULL; /* NOTREACHED */
920 }
921
922error:
923 port_share_close(ps);
924 return NULL;
925}
926
927void
928port_share_close(struct port_share *ps)
929{
930 if (ps)
931 {
932 if (ps->foreground_fd >= 0)
933 {
934 /* tell background process to exit */
935 port_share_sendmsg(ps->foreground_fd, COMMAND_EXIT, NULL, SOCKET_UNDEFINED);
936
937 /* wait for background process to exit */
938 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE: waiting for background process to exit");
939 if (ps->background_pid > 0)
940 {
941 waitpid(ps->background_pid, NULL, 0);
942 }
943 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE: background process exited");
944
945 openvpn_close_socket(ps->foreground_fd);
946 ps->foreground_fd = -1;
947 }
948
949 free(ps);
950 }
951}
952
953void
954port_share_abort(struct port_share *ps)
955{
956 if (ps)
957 {
958 /* tell background process to exit */
959 if (ps->foreground_fd >= 0)
960 {
961 send_control(ps->foreground_fd, COMMAND_EXIT);
962 openvpn_close_socket(ps->foreground_fd);
963 ps->foreground_fd = -1;
964 }
965 }
966}
967
968/*
969 * Given either the first 2 or 3 bytes of an initial client -> server
970 * data payload, return true if the protocol is that of an OpenVPN
971 * client attempting to connect with an OpenVPN server.
972 */
973bool
974is_openvpn_protocol(const struct buffer *buf)
975{
976 const unsigned char *p = (const unsigned char *)CBSTR(buf);
977 const int len = BLEN(buf);
978 if (len >= 3)
979 {
980 int plen = (p[0] << 8) | p[1];
981
983 {
984 /* WKc is at least 290 byte (not including metadata):
985 *
986 * 16 bit len + 256 bit HMAC + 2048 bit Kc = 2320 bit
987 *
988 * This is increased by the normal length of client handshake +
989 * tls-crypt overhead (32)
990 *
991 * For metadata tls-crypt-v2.txt does not explicitly specify
992 * an upper limit but we also have TLS_CRYPT_V2_MAX_WKC_LEN
993 * as 1024 bytes. We err on the safe side with 255 extra overhead
994 *
995 * We don't do the 2 byte check for tls-crypt-v2 because it is very
996 * unrealistic to have only 2 bytes available.
997 */
998 return (plen >= 336 && plen < (1024 + 255));
999 }
1000 else
1001 {
1002 /* For non tls-crypt2 we assume the packet length to valid between
1003 * 14 and 255 */
1004 return plen >= 14 && plen <= 255
1006 }
1007 }
1008 else if (len >= 2)
1009 {
1010 int plen = (p[0] << 8) | p[1];
1011 return plen >= 14 && plen <= 255;
1012 }
1013 else
1014 {
1015 return true;
1016 }
1017}
1018
1019/*
1020 * Called from the foreground process. Send a message to the background process that it
1021 * should proxy the TCP client on sd to the host/port defined in the initial port_share_open
1022 * call.
1023 */
1024void
1025port_share_redirect(const struct port_share *ps, const struct buffer *head, socket_descriptor_t sd)
1026{
1027 if (ps)
1028 {
1029 port_share_sendmsg(ps->foreground_fd, COMMAND_REDIRECT, head, sd);
1030 }
1031}
1032
1033#endif /* if PORT_SHARE */
static void set_signals(void)
Definition auth-pam.c:281
#define RESPONSE_INIT_SUCCEEDED
Definition auth-pam.c:62
static int recv_control(int fd)
Definition auth-pam.c:142
#define COMMAND_EXIT
Definition auth-pam.c:59
static int send_control(int fd, int code)
Definition auth-pam.c:158
static void close_fds_except(int keep)
Definition auth-pam.c:263
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:174
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
Definition buffer.c:60
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
#define CBPTR(buf)
Return a const pointer to the start of the buffer content.
Definition buffer.h:141
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
Definition buffer.h:1165
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
#define BCAP(buf)
Return the number of bytes available for appending to the buffer.
Definition buffer.h:161
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:153
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2144
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
#define CBSTR(buf)
Return the buffer content pointer cast to const char *.
Definition buffer.h:159
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2036
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
Data Channel Cryptography Module.
#define D_PS_PROXY
Definition errlevel.h:90
#define M_INFO
Definition errlevel.h:54
#define D_PS_PROXY_DEBUG
Definition errlevel.h:141
struct event_set * event_set_init(int *maxevents, unsigned int flags)
Definition event.c:1205
static void event_free(struct event_set *es)
Definition event.h:159
static void event_del(struct event_set *es, event_t event)
Definition event.h:174
#define EVENT_UNDEF
Definition event.h:36
static int event_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
Definition event.h:186
#define EVENT_WRITE
Definition event.h:38
#define EVENT_READ
Definition event.h:37
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition event.h:180
void set_nonblock(socket_descriptor_t fd)
Definition fdmisc.c:77
void set_cloexec(socket_descriptor_t fd)
Definition fdmisc.c:87
static SERVICE_STATUS status
Definition interactive.c:52
@ write
@ read
#define CLEAR(x)
Definition basic.h:32
#define SIZE(x)
Definition basic.h:29
static bool msg_test(msglvl_t flags)
Return true if flags represent an enabled, not muted log level.
Definition error.h:260
#define M_FATAL
Definition error.h:90
#define dmsg(flags,...)
Definition error.h:164
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
int platform_open(const char *path, int flags, int mode)
Definition platform.c:518
int openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *servname, int resolve_retry_seconds, struct signal_info *sig_info, int ai_family, struct addrinfo **res)
Definition resolve.c:102
DNS resolution.
#define GETADDR_FATAL
Definition resolve.h:39
#define GETADDR_RESOLVE
Definition resolve.h:38
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:610
#define MSG_NOSIGNAL
Definition socket.h:251
#define openvpn_close_socket(s)
Definition socket.h:256
static const char * print_openvpn_sockaddr(const struct openvpn_sockaddr *addr, struct gc_arena *gc)
Definition socket_util.h:71
#define P_OPCODE_SHIFT
Definition ssl_pkt.h:39
#define P_CONTROL_HARD_RESET_CLIENT_V2
Definition ssl_pkt.h:51
#define P_CONTROL_HARD_RESET_CLIENT_V3
Definition ssl_pkt.h:55
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
unsigned int rwflags
Definition event.h:123
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
struct gc_entry * list
First element of the linked list of gc_entry structures.
Definition buffer.h:128
union openvpn_sockaddr::@27 addr
struct sockaddr sa
Definition socket_util.h:42
#define SIGHUP
Definition syshead.h:55
#define SOCKET_UNDEFINED
Definition syshead.h:439
#define SIGINT
Definition syshead.h:56
#define SIGTERM
Definition syshead.h:59
SOCKET socket_descriptor_t
Definition syshead.h:441
#define SIGUSR1
Definition syshead.h:57
static int socket_defined(const socket_descriptor_t sd)
Definition syshead.h:449
#define SIGUSR2
Definition syshead.h:58
struct gc_arena gc
Definition test_ssl.c:138