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 "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 unsigned 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 = BLENZ(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#if defined(__GNUC__) || defined(__clang__)
331#pragma GCC diagnostic push
332#pragma GCC diagnostic ignored "-Wsign-compare"
333#endif
334
335/*
336 * Record IP/port of client in filesystem, so that server receiving
337 * the proxy can determine true client origin.
338 */
339static void
340journal_add(const char *journal_dir, struct proxy_connection *pc, struct proxy_connection *cp)
341{
342 struct openvpn_sockaddr from, to;
343
344 socklen_t slen = sizeof(from.addr);
345 socklen_t 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 struct gc_arena gc = gc_new();
350 const char *f = print_openvpn_sockaddr(&from, &gc);
351 const char *t = print_openvpn_sockaddr(&to, &gc);
352 size_t fnlen = strlen(journal_dir) + strlen(t) + 2;
353 char *jfn = (char *)malloc(fnlen);
355 snprintf(jfn, fnlen, "%s/%s", journal_dir, t);
356 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: client origin %s -> %s", jfn, f);
357 int fd = platform_open(jfn, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP);
358 if (fd != -1)
359 {
360 if (write(fd, f, strlen(f)) != strlen(f))
361 {
362 msg(M_WARN, "PORT SHARE: writing to journal file (%s) failed", jfn);
363 }
364 close(fd);
365 cp->jfn = jfn;
366 }
367 else
368 {
369 msg(M_WARN | M_ERRNO, "PORT SHARE: unable to write journal file in %s", jfn);
370 free(jfn);
371 }
372 gc_free(&gc);
373 }
374}
375
376#if defined(__GNUC__) || defined(__clang__)
377#pragma GCC diagnostic pop
378#endif
379
380/*
381 * Cleanup function, on proxy process exit.
382 */
383static void
384proxy_list_close(struct proxy_connection **list)
385{
386 if (list)
387 {
388 struct proxy_connection *pc = *list;
389 while (pc)
390 {
391 proxy_entry_mark_for_close(pc, NULL);
392 pc = pc->next;
393 }
394 proxy_list_housekeeping(list);
395 }
396}
397
398static inline void
399proxy_connection_io_requeue(struct proxy_connection *pc, const unsigned int rwflags_new,
400 struct event_set *es)
401{
402 if (socket_defined(pc->sd) && pc->rwflags != rwflags_new)
403 {
404 /*dmsg (D_PS_PROXY_DEBUG, "PORT SHARE PROXY: requeue[%d] rwflags=%u", (int)pc->sd,
405 * rwflags_new);*/
406 event_ctl(es, pc->sd, rwflags_new, (void *)pc);
407 pc->rwflags = rwflags_new;
408 }
409}
410
411/*
412 * Create a new pair of proxy_connection entries, one for each
413 * socket file descriptor involved in the proxy. We are given
414 * the client fd, and we should derive our own server fd by connecting
415 * to the server given by server_addr/server_port. Return true
416 * on success and false on failure to connect to server.
417 */
418static bool
419proxy_entry_new(struct proxy_connection **list, struct event_set *es,
420 const struct openvpn_sockaddr server_addr, const socket_descriptor_t sd_client,
421 struct buffer *initial_data, const char *journal_dir)
422{
423 socket_descriptor_t sd_server;
424 int status;
425 struct proxy_connection *pc;
426 struct proxy_connection *cp;
427
428 /* connect to port share server */
429 if ((sd_server = socket(server_addr.addr.sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
430 {
431 msg(M_WARN | M_ERRNO, "PORT SHARE PROXY: cannot create socket");
432 return false;
433 }
434 status = openvpn_connect(sd_server, &server_addr.addr.sa, 5, NULL);
435 if (status)
436 {
437 msg(M_WARN, "PORT SHARE PROXY: connect to port-share server failed");
438 openvpn_close_socket(sd_server);
439 return false;
440 }
441 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: connect to port-share server succeeded");
442
443 set_nonblock(sd_client);
444 set_nonblock(sd_server);
445
446 /* allocate 2 new proxy_connection objects */
447 ALLOC_OBJ_CLEAR(pc, struct proxy_connection);
448 ALLOC_OBJ_CLEAR(cp, struct proxy_connection);
449
450 /* client object */
451 pc->defined = true;
452 pc->next = cp;
453 pc->counterpart = cp;
454 pc->buf = *initial_data;
455 pc->buffer_initial = true;
456 pc->rwflags = EVENT_UNDEF;
457 pc->sd = sd_client;
458
459 /* server object */
460 cp->defined = true;
461 cp->next = *list;
462 cp->counterpart = pc;
463 cp->buf = alloc_buf(PROXY_CONNECTION_BUFFER_SIZE);
464 cp->buffer_initial = false;
465 cp->rwflags = EVENT_UNDEF;
466 cp->sd = sd_server;
467
468 /* add to list */
469 *list = pc;
470
471 /* add journal entry */
472 if (journal_dir)
473 {
474 journal_add(journal_dir, pc, cp);
475 }
476
477 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: NEW CONNECTION [c=%d s=%d]", (int)sd_client,
478 (int)sd_server);
479
480 /* set initial i/o states */
481 proxy_connection_io_requeue(pc, EVENT_READ, es);
482 proxy_connection_io_requeue(cp, EVENT_READ | EVENT_WRITE, es);
483
484 return true;
485}
486
487/*
488 * This function runs in the context of the background proxy process.
489 * Receive a control message from the parent (sent by the port_share_sendmsg
490 * function above) and act on it. Return false if the proxy process should
491 * exit, true otherwise.
492 */
493static bool
494control_message_from_parent(const socket_descriptor_t sd_control, struct proxy_connection **list,
495 struct event_set *es, const struct openvpn_sockaddr server_addr,
496 const int max_initial_buf, const char *journal_dir)
497{
498 /* this buffer needs to be large enough to handle the largest buffer
499 * that might be returned by the link_socket_read call in read_incoming_link. */
500 struct buffer buf = alloc_buf(max_initial_buf);
501
502 struct msghdr mesg;
503 struct cmsghdr *h;
504 struct iovec iov[2];
505 char command = 0;
506 ssize_t status;
507 int ret = true;
508
509 CLEAR(mesg);
510
511 iov[0].iov_base = &command;
512 iov[0].iov_len = sizeof(command);
513 iov[1].iov_base = BPTR(&buf);
514 iov[1].iov_len = BCAP(&buf);
515 mesg.msg_iov = iov;
516 mesg.msg_iovlen = 2;
517
518 mesg.msg_controllen = cmsg_size();
519 mesg.msg_control = (char *)malloc(mesg.msg_controllen);
520 check_malloc_return(mesg.msg_control);
521 mesg.msg_flags = 0;
522
523 h = CMSG_FIRSTHDR(&mesg);
524 h->cmsg_len = CMSG_LEN(sizeof(socket_descriptor_t));
525 h->cmsg_level = SOL_SOCKET;
526 h->cmsg_type = SCM_RIGHTS;
527 static const socket_descriptor_t socket_undefined = SOCKET_UNDEFINED;
528 memcpy(CMSG_DATA(h), &socket_undefined, sizeof(socket_undefined));
529
530 status = recvmsg(sd_control, &mesg, MSG_NOSIGNAL);
531 if (status != -1)
532 {
533 if (h == NULL || h->cmsg_len != CMSG_LEN(sizeof(socket_descriptor_t))
534 || h->cmsg_level != SOL_SOCKET || h->cmsg_type != SCM_RIGHTS)
535 {
536 msg(M_WARN, "PORT SHARE PROXY: received unknown message");
537 }
538 else
539 {
540 socket_descriptor_t received_fd;
541 memcpy(&received_fd, CMSG_DATA(h), sizeof(received_fd));
542 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: RECEIVED sd=%d", (int)received_fd);
543
544 if (status >= 2 && command == COMMAND_REDIRECT)
545 {
546 buf.len = (int)status - 1;
547 if (proxy_entry_new(list, es, server_addr, received_fd, &buf, journal_dir))
548 {
549 CLEAR(buf); /* we gave the buffer to proxy_entry_new */
550 }
551 else
552 {
553 openvpn_close_socket(received_fd);
554 }
555 }
556 else if (status >= 1 && command == COMMAND_EXIT)
557 {
558 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: RECEIVED COMMAND_EXIT");
559 openvpn_close_socket(received_fd); /* null socket */
560 ret = false;
561 }
562 }
563 }
564 free(mesg.msg_control);
565 free_buf(&buf);
566 return ret;
567}
568
569static int
570proxy_connection_io_recv(struct proxy_connection *pc)
571{
572 /* recv data from socket */
573 const ssize_t status = recv(pc->sd, BPTR(&pc->buf), BCAP(&pc->buf), MSG_NOSIGNAL);
574 if (status < 0)
575 {
576 return (errno == EAGAIN) ? IOSTAT_EAGAIN_ON_READ : IOSTAT_READ_ERROR;
577 }
578 else
579 {
580 if (!status)
581 {
582 return IOSTAT_READ_ERROR;
583 }
584 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: read[%d] %zd", (int)pc->sd, status);
585 pc->buf.len = (int)status;
586 }
587 return IOSTAT_GOOD;
588}
589
590static int
591proxy_connection_io_send(struct proxy_connection *pc, int *bytes_sent)
592{
593 const socket_descriptor_t sd = pc->counterpart->sd;
594 const ssize_t status = send(sd, BPTR(&pc->buf), BLENZ(&pc->buf), MSG_NOSIGNAL);
595
596 if (status < 0)
597 {
598 const int e = errno;
599 return (e == EAGAIN) ? IOSTAT_EAGAIN_ON_WRITE : IOSTAT_WRITE_ERROR;
600 }
601 else
602 {
603 *bytes_sent += (int)status;
604 if (status != pc->buf.len)
605 {
606 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: partial write[%d], tried=%d got=%zd", (int)sd,
607 pc->buf.len, status);
608 buf_advance(&pc->buf, status);
609 return IOSTAT_EAGAIN_ON_WRITE;
610 }
611 else
612 {
613 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: wrote[%d] %zd", (int)sd, status);
614 pc->buf.len = 0;
615 pc->buf.offset = 0;
616 }
617 }
618
619 /* realloc send buffer after initial send */
620 if (pc->buffer_initial)
621 {
622 free_buf(&pc->buf);
623 pc->buf = alloc_buf(PROXY_CONNECTION_BUFFER_SIZE);
624 pc->buffer_initial = false;
625 }
626 return IOSTAT_GOOD;
627}
628
629/*
630 * Forward data from pc to pc->counterpart.
631 */
632
633static int
634proxy_connection_io_xfer(struct proxy_connection *pc, const int max_transfer)
635{
636 int transferred = 0;
637 while (transferred < max_transfer)
638 {
639 if (!BLEN(&pc->buf))
640 {
641 const int status = proxy_connection_io_recv(pc);
642 if (status != IOSTAT_GOOD)
643 {
644 return status;
645 }
646 }
647
648 if (BLEN(&pc->buf))
649 {
650 const int status = proxy_connection_io_send(pc, &transferred);
651 if (status != IOSTAT_GOOD)
652 {
653 return status;
654 }
655 }
656 }
657 return IOSTAT_EAGAIN_ON_READ;
658}
659
660/*
661 * Decide how the receipt of an EAGAIN status should affect our next IO queueing.
662 */
663static bool
664proxy_connection_io_status(const int status, unsigned int *rwflags_pc, unsigned int *rwflags_cp)
665{
666 switch (status)
667 {
668 case IOSTAT_EAGAIN_ON_READ:
669 *rwflags_pc |= EVENT_READ;
670 *rwflags_cp &= ~EVENT_WRITE;
671 return true;
672
673 case IOSTAT_EAGAIN_ON_WRITE:
674 *rwflags_pc &= ~EVENT_READ;
675 *rwflags_cp |= EVENT_WRITE;
676 return true;
677
678 case IOSTAT_READ_ERROR:
679 return false;
680
681 case IOSTAT_WRITE_ERROR:
682 return false;
683
684 default:
685 msg(M_FATAL, "PORT SHARE PROXY: unexpected status=%d", status);
686 }
687 return false; /* NOTREACHED */
688}
689
690/*
691 * Dispatch function for forwarding data between the two socket fds involved
692 * in the proxied connection.
693 */
694static int
695proxy_connection_io_dispatch(struct proxy_connection *pc, const unsigned int rwflags,
696 struct event_set *es)
697{
698 const int max_transfer_per_iteration = 10000;
699 struct proxy_connection *cp = pc->counterpart;
700 unsigned int rwflags_pc = pc->rwflags;
701 unsigned int rwflags_cp = cp->rwflags;
702
703 ASSERT(pc->defined && cp->defined && cp->counterpart == pc);
704
705 if (rwflags & EVENT_READ)
706 {
707 const int status = proxy_connection_io_xfer(pc, max_transfer_per_iteration);
708 if (!proxy_connection_io_status(status, &rwflags_pc, &rwflags_cp))
709 {
710 goto bad;
711 }
712 }
713 if (rwflags & EVENT_WRITE)
714 {
715 const int status = proxy_connection_io_xfer(cp, max_transfer_per_iteration);
716 if (!proxy_connection_io_status(status, &rwflags_cp, &rwflags_pc))
717 {
718 goto bad;
719 }
720 }
721 proxy_connection_io_requeue(pc, rwflags_pc, es);
722 proxy_connection_io_requeue(cp, rwflags_cp, es);
723
724 return true;
725
726bad:
727 proxy_entry_mark_for_close(pc, es);
728 return false;
729}
730
731/*
732 * This is the main function for the port share proxy background process.
733 */
734static void
735port_share_proxy(const struct openvpn_sockaddr hostaddr, const socket_descriptor_t sd_control,
736 const int max_initial_buf, const char *journal_dir)
737{
738 if (send_control(sd_control, RESPONSE_INIT_SUCCEEDED) >= 0)
739 {
740 void *sd_control_marker = (void *)1;
741 int maxevents = 256;
742 struct event_set *es;
743 struct event_set_return esr[64];
744 struct proxy_connection *list = NULL;
745 time_t last_housekeeping = 0;
746
747 msg(D_PS_PROXY, "PORT SHARE PROXY: proxy starting");
748
749 es = event_set_init(&maxevents, 0);
750 event_ctl(es, sd_control, EVENT_READ, sd_control_marker);
751 while (true)
752 {
753 int n_events;
754 struct timeval tv;
755 time_t current;
756
757 tv.tv_sec = 10;
758 tv.tv_usec = 0;
759 n_events = event_wait(es, &tv, esr, SIZE(esr));
760 /*dmsg (D_PS_PROXY_DEBUG, "PORT SHARE PROXY: event_wait returned %d", n_events);*/
761 current = time(NULL);
762 if (n_events > 0)
763 {
764 int i;
765 for (i = 0; i < n_events; ++i)
766 {
767 const struct event_set_return *e = &esr[i];
768 if (e->arg == sd_control_marker)
769 {
770 if (!control_message_from_parent(sd_control, &list, es, hostaddr,
771 max_initial_buf, journal_dir))
772 {
773 goto done;
774 }
775 }
776 else
777 {
778 struct proxy_connection *pc = (struct proxy_connection *)e->arg;
779 if (pc->defined)
780 {
781 proxy_connection_io_dispatch(pc, e->rwflags, es);
782 }
783 }
784 }
785 }
786 else if (n_events < 0)
787 {
788 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: event_wait failed");
789 }
790 if (current > last_housekeeping)
791 {
792 proxy_list_housekeeping(&list);
793 last_housekeeping = current;
794 }
795 }
796
797done:
798 proxy_list_close(&list);
799 event_free(es);
800 }
801 msg(M_INFO, "PORT SHARE PROXY: proxy exiting");
802}
803
804/*
805 * Called from the main OpenVPN process to enable the port
806 * share proxy.
807 */
808struct port_share *
809port_share_open(const char *host, const char *port, const int max_initial_buf,
810 const char *journal_dir)
811{
813 struct openvpn_sockaddr hostaddr;
814 struct port_share *ps;
815
816 ALLOC_OBJ_CLEAR(ps, struct port_share);
817 ps->foreground_fd = -1;
818 ps->background_pid = -1;
819
820 /*
821 * Get host's IP address
822 */
823 struct addrinfo *ai;
825 0, NULL, AF_UNSPEC, &ai);
826 ASSERT(status == 0);
827 ASSERT(sizeof(hostaddr.addr) >= ai->ai_addrlen);
828 memcpy(&hostaddr.addr.sa, ai->ai_addr, ai->ai_addrlen);
829 freeaddrinfo(ai);
830
832 {
833 struct gc_arena gc = gc_new();
834 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE PROXY: receiver will be %s",
835 print_openvpn_sockaddr(&hostaddr, &gc));
836 gc_free(&gc);
837 }
838
839 /*
840 * Make a socket for foreground and background processes
841 * to communicate.
842 */
843 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) == -1)
844 {
845 msg(M_WARN | M_ERRNO, "PORT SHARE: socketpair call failed");
846 goto error;
847 }
848
849 /*
850 * Fork off background proxy process.
851 */
852 pid_t pid = fork();
853
854 if (pid < 0)
855 {
856 msg(M_WARN | M_ERRNO, "PORT SHARE: fork failed");
857 goto error;
858 }
859 else if (pid)
860 {
861 /*
862 * Foreground Process
863 */
864
865 ps->background_pid = pid;
866
867 /* close our copy of child's socket */
869
870 /* don't let future subprocesses inherit child socket */
871 set_cloexec(fd[0]);
872
873 /* wait for background child process to initialize */
874 int status = recv_control(fd[0]);
876 {
877 /* note that this will cause possible EAGAIN when writing to
878 * control socket if proxy process is backlogged */
879 set_nonblock(fd[0]);
880
881 ps->foreground_fd = fd[0];
882 return ps;
883 }
884 else
885 {
886 msg(M_ERR, "PORT SHARE: unexpected init recv_control status=%d", status);
887 }
888 }
889 else
890 {
891 /*
892 * Background Process
893 */
894
895 /* Ignore most signals (the parent will receive them) */
896 set_signals();
897
898 /* Let msg know that we forked */
899 msg_forked();
900
901#ifdef ENABLE_MANAGEMENT
902 /* Don't interact with management interface */
903 management = NULL;
904#endif
905
906 /* close all parent fds except our socket back to parent */
907 close_fds_except(fd[1]);
908
909 /* no blocking on control channel back to parent */
910 set_nonblock(fd[1]);
911
912 /* execute the event loop */
913 port_share_proxy(hostaddr, fd[1], max_initial_buf, journal_dir);
914
916
917 exit(0);
918 return NULL; /* NOTREACHED */
919 }
920
921error:
922 port_share_close(ps);
923 return NULL;
924}
925
926void
927port_share_close(struct port_share *ps)
928{
929 if (ps)
930 {
931 if (ps->foreground_fd >= 0)
932 {
933 /* tell background process to exit */
934 port_share_sendmsg(ps->foreground_fd, COMMAND_EXIT, NULL, SOCKET_UNDEFINED);
935
936 /* wait for background process to exit */
937 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE: waiting for background process to exit");
938 if (ps->background_pid > 0)
939 {
940 waitpid(ps->background_pid, NULL, 0);
941 }
942 dmsg(D_PS_PROXY_DEBUG, "PORT SHARE: background process exited");
943
944 openvpn_close_socket(ps->foreground_fd);
945 ps->foreground_fd = -1;
946 }
947
948 free(ps);
949 }
950}
951
952void
953port_share_abort(struct port_share *ps)
954{
955 if (ps)
956 {
957 /* tell background process to exit */
958 if (ps->foreground_fd >= 0)
959 {
960 send_control(ps->foreground_fd, COMMAND_EXIT);
961 openvpn_close_socket(ps->foreground_fd);
962 ps->foreground_fd = -1;
963 }
964 }
965}
966
967/*
968 * Given either the first 2 or 3 bytes of an initial client -> server
969 * data payload, return true if the protocol is that of an OpenVPN
970 * client attempting to connect with an OpenVPN server.
971 */
972bool
973is_openvpn_protocol(const struct buffer *buf)
974{
975 const unsigned char *p = (const unsigned char *)BSTR(buf);
976 const int len = BLEN(buf);
977 if (len >= 3)
978 {
979 int plen = (p[0] << 8) | p[1];
980
982 {
983 /* WKc is at least 290 byte (not including metadata):
984 *
985 * 16 bit len + 256 bit HMAC + 2048 bit Kc = 2320 bit
986 *
987 * This is increased by the normal length of client handshake +
988 * tls-crypt overhead (32)
989 *
990 * For metadata tls-crypt-v2.txt does not explicitly specify
991 * an upper limit but we also have TLS_CRYPT_V2_MAX_WKC_LEN
992 * as 1024 bytes. We err on the safe side with 255 extra overhead
993 *
994 * We don't do the 2 byte check for tls-crypt-v2 because it is very
995 * unrealistic to have only 2 bytes available.
996 */
997 return (plen >= 336 && plen < (1024 + 255));
998 }
999 else
1000 {
1001 /* For non tls-crypt2 we assume the packet length to valid between
1002 * 14 and 255 */
1003 return plen >= 14 && plen <= 255
1005 }
1006 }
1007 else if (len >= 2)
1008 {
1009 int plen = (p[0] << 8) | p[1];
1010 return plen >= 14 && plen <= 255;
1011 }
1012 else
1013 {
1014 return true;
1015 }
1016}
1017
1018/*
1019 * Called from the foreground process. Send a message to the background process that it
1020 * should proxy the TCP client on sd to the host/port defined in the initial port_share_open
1021 * call.
1022 */
1023void
1024port_share_redirect(struct port_share *ps, const struct buffer *head, socket_descriptor_t sd)
1025{
1026 if (ps)
1027 {
1028 port_share_sendmsg(ps->foreground_fd, COMMAND_REDIRECT, head, sd);
1029 }
1030}
1031
1032#endif /* if PORT_SHARE */
static void set_signals(void)
Definition auth-pam.c:280
#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:262
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:129
#define BPTR(buf)
Definition buffer.h:123
static bool buf_advance(struct buffer *buf, ssize_t size)
Definition buffer.h:617
#define BLEN(buf)
Definition buffer.h:126
#define BCAP(buf)
Definition buffer.h:130
#define BLENZ(buf)
Definition buffer.h:127
static void check_malloc_return(void *p)
Definition buffer.h:1131
static void gc_free(struct gc_arena *a)
Definition buffer.h:1049
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition buffer.h:1088
static struct gc_arena gc_new(void)
Definition buffer.h:1041
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:1187
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: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:95
static bool msg_test(msglvl_t flags)
Return true if flags represent an enabled, not muted log level.
Definition error.h:258
#define M_FATAL
Definition error.h:90
#define dmsg(flags,...)
Definition error.h:172
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219
#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:513
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:967
#define MSG_NOSIGNAL
Definition socket.h:261
#define openvpn_close_socket(s)
Definition socket.h:266
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:123
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
struct gc_entry * list
First element of the linked list of gc_entry structures.
Definition buffer.h:117
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:443
#define SIGINT
Definition syshead.h:56
#define SIGTERM
Definition syshead.h:59
SOCKET socket_descriptor_t
Definition syshead.h:445
#define SIGUSR1
Definition syshead.h:57
static int socket_defined(const socket_descriptor_t sd)
Definition syshead.h:453
#define SIGUSR2
Definition syshead.h:58
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:131