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