OpenVPN
manage.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/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#ifdef ENABLE_MANAGEMENT
30
31#include "error.h"
32#include "fdmisc.h"
33#include "options.h"
34#include "base64.h"
35#include "sig.h"
36#include "event.h"
37#include "otime.h"
38#include "integer.h"
39#include "misc.h"
40#include "ssl.h"
41#include "common.h"
42#include "manage.h"
43#include "openvpn.h"
44#include "dco.h"
45#include "push.h"
46#include "multi.h"
47
48#include "memdbg.h"
49
50#ifdef ENABLE_PKCS11
51#include "pkcs11.h"
52#endif
53
54#define MANAGEMENT_ECHO_PULL_INFO 0
55
56#if MANAGEMENT_ECHO_PULL_INFO
57#define MANAGEMENT_ECHO_FLAGS LOG_PRINT_INTVAL
58#else
59#define MANAGEMENT_ECHO_FLAGS 0
60#endif
61
62/*
63 * Management client versions indicating feature support in client.
64 * Append new values as needed but do not change exisiting ones.
65 */
74
75struct management *management; /* GLOBAL */
76
77/* static forward declarations */
78static void man_output_standalone(struct management *man, volatile int *signal_received);
79
80static void man_reset_client_socket(struct management *man, const bool exiting);
81
82static void
84{
85 msg(M_CLIENT, "Management Interface for %s", title_string);
86 msg(M_CLIENT, "Commands:");
87 msg(M_CLIENT, "auth-retry t : Auth failure retry mode (none,interact,nointeract).");
88 msg(M_CLIENT, "bytecount n : Show bytes in/out, update every n secs (0=off).");
89 msg(M_CLIENT, "echo [on|off] [N|all] : Like log, but only show messages in echo buffer.");
91 "cr-response response : Send a challenge response answer via CR_RESPONSE to server");
92 msg(M_CLIENT, "exit|quit : Close management session.");
93 msg(M_CLIENT, "forget-passwords : Forget passwords entered so far.");
94 msg(M_CLIENT, "help : Print this message.");
95 msg(M_CLIENT, "hold [on|off|release] : Set/show hold flag to on/off state, or");
96 msg(M_CLIENT, " release current hold and start tunnel.");
97 msg(M_CLIENT, "kill cn : Kill the client instance(s) having common name cn.");
98 msg(M_CLIENT, "kill IP:port : Kill the client instance connecting from IP:port.");
99 msg(M_CLIENT, "load-stats : Show global server load stats.");
100 msg(M_CLIENT, "log [on|off] [N|all] : Turn on/off realtime log display");
101 msg(M_CLIENT, " + show last N lines or 'all' for entire history.");
103 "mute [n] : Set log mute level to n, or show level if n is absent.");
104 msg(M_CLIENT, "needok type action : Enter confirmation for NEED-OK request of 'type',");
105 msg(M_CLIENT, " where action = 'ok' or 'cancel'.");
106 msg(M_CLIENT, "needstr type action : Enter confirmation for NEED-STR request of 'type',");
107 msg(M_CLIENT, " where action is reply string.");
108 msg(M_CLIENT, "net : (Windows only) Show network info and routing table.");
109 msg(M_CLIENT, "password type p : Enter password p for a queried OpenVPN password.");
110 msg(M_CLIENT, "password type : (version >=5) Enter password base64-encoded on");
111 msg(M_CLIENT, " subsequent lines followed by END.");
112 msg(M_CLIENT, "remote type [host port] : Override remote directive, type=ACCEPT|MOD|SKIP.");
113 msg(M_CLIENT, "remote-entry-count : Get number of available remote entries.");
114 msg(M_CLIENT, "remote-entry-get i|all [j]: Get remote entry at index = i to to j-1 or all.");
115 msg(M_CLIENT, "proxy type [host port flags] : Enter dynamic proxy server info.");
116 msg(M_CLIENT, "pid : Show process ID of the current OpenVPN process.");
117#ifdef ENABLE_PKCS11
118 msg(M_CLIENT, "pkcs11-id-count : Get number of available PKCS#11 identities.");
119 msg(M_CLIENT, "pkcs11-id-get index : Get PKCS#11 identity at index.");
120#endif
121 msg(M_CLIENT, "client-auth CID KID : Authenticate client-id/key-id CID/KID (MULTILINE)");
122 msg(M_CLIENT, "client-auth-nt CID KID : Authenticate client-id/key-id CID/KID");
124 "client-deny CID KID R [CR] : Deny auth client-id/key-id CID/KID with log reason");
125 msg(M_CLIENT, " text R and optional client reason text CR");
127 "client-pending-auth CID KID MSG timeout : Instruct OpenVPN to send AUTH_PENDING and INFO_PRE msg");
129 " to the client and wait for a final client-auth/client-deny");
130 msg(M_CLIENT, "client-kill CID [M] : Kill client instance CID with message M (def=RESTART)");
131 msg(M_CLIENT, "env-filter [level] : Set env-var filter level");
132 msg(M_CLIENT, "rsa-sig : Enter a signature in response to >RSA_SIGN challenge");
134 " Enter signature base64 on subsequent lines followed by END");
135 msg(M_CLIENT, "pk-sig : Enter a signature in response to >PK_SIGN challenge");
137 " Enter signature base64 on subsequent lines followed by END");
139 "certificate : Enter a client certificate in response to >NEED-CERT challenge");
141 " Enter certificate base64 on subsequent lines followed by END");
142 msg(M_CLIENT, "signal s : Send signal s to daemon,");
143 msg(M_CLIENT, " s = SIGHUP|SIGTERM|SIGUSR1|SIGUSR2.");
144 msg(M_CLIENT, "state [on|off] [N|all] : Like log, but show state history.");
145 msg(M_CLIENT, "status [n] : Show current daemon status info using format #n.");
146 msg(M_CLIENT, "test n : Produce n lines of output for testing/debugging.");
147 msg(M_CLIENT, "username type u : Enter username u for a queried OpenVPN username.");
148 msg(M_CLIENT, "verb [n] : Set log verbosity level to n, or show if n is absent.");
149 msg(M_CLIENT, "version [n] : Set client's version to n or show current version of daemon.");
150 msg(M_CLIENT, "push-update-broad options : Broadcast a message to update the specified options.");
151 msg(M_CLIENT, " Ex. push-update-broad \"route something, -dns\"");
152 msg(M_CLIENT, "push-update-cid CID options : Send an update message to the client identified by CID.");
153 msg(M_CLIENT, "END");
154}
155
156static const char *
157man_state_name(const int state)
158{
159 switch (state)
160 {
162 return "INITIAL";
163
165 return "CONNECTING";
166
168 return "WAIT";
169
171 return "AUTH";
172
174 return "GET_CONFIG";
175
177 return "ASSIGN_IP";
178
180 return "ADD_ROUTES";
181
183 return "CONNECTED";
184
186 return "RECONNECTING";
187
189 return "EXITING";
190
192 return "RESOLVE";
193
195 return "TCP_CONNECT";
196
198 return "AUTH_PENDING";
199
200 default:
201 return "?";
202 }
203}
204
205static void
207{
208 msg(M_CLIENT, ">INFO:OpenVPN Management Interface Version %d -- type 'help' for more info",
210 if (man->persist.special_state_msg)
211 {
213 }
214}
215
216static inline bool
218{
219 return man->settings.up.defined && !man->connection.password_verified;
220}
221
222static void
223man_check_password(struct management *man, const char *line)
224{
225 if (man_password_needed(man))
226 {
227 /* This comparison is not fixed time but since strlen(time) is based on
228 * the attacker choice, it should not give any indication of the real
229 * password length, use + 1 to include the NUL byte that terminates the
230 * string*/
231 size_t compare_len =
232 min_size(strlen(line) + 1, sizeof(man->settings.up.password));
233 if (memcmp_constant_time(line, man->settings.up.password, compare_len) == 0)
234 {
235 man->connection.password_verified = true;
236 msg(M_CLIENT, "SUCCESS: password is correct");
237 man_welcome(man);
238 }
239 else
240 {
241 man->connection.password_verified = false;
242 msg(M_CLIENT, "ERROR: bad password");
244 {
245 msg(M_WARN, "MAN: client connection rejected after %d failed password attempts",
247 man->connection.halt = true;
248 }
249 }
250 }
251}
252
253static void
255{
257 {
259 {
261 }
262 else
263 {
265 }
266 }
267}
268
269static void
271{
272 if (management_connected(man))
273 {
276 {
277 volatile int signal_received = 0;
278 man_output_standalone(man, &signal_received);
279 }
280 }
281}
282
283static void
284man_output_list_push_str(struct management *man, const char *str)
285{
286 if (management_connected(man) && str)
287 {
289 }
290}
291
292static void
293man_output_list_push(struct management *man, const char *str)
294{
295 man_output_list_push_str(man, str);
297}
298
299static void
301{
302 if (man_password_needed(man))
303 {
304 man_output_list_push(man, "ENTER PASSWORD:");
305 }
306#if 0 /* should we use prompt? */
307 else
308 {
309 man_output_list_push(man, ">");
310 }
311#endif
312}
313
314
319static void
320report_command_status(const bool status, const char *command)
321{
322 if (status)
323 {
324 msg(M_CLIENT, "SUCCESS: %s command succeeded", command);
325 }
326 else
327 {
328 msg(M_CLIENT, "ERROR: %s command failed", command);
329 }
330}
331
332static void
334{
335#if UNIX_SOCK_SUPPORT
337 {
338 socket_delete_unix(&man->settings.local_unix);
339 }
340#endif
341}
342
343static void
345{
346#ifndef _WIN32
347 /*
348 * Windows doesn't need this because the ne32 event is permanently
349 * enabled at struct management scope.
350 */
352 {
354 }
355#endif
357}
358
359static void
360virtual_output_callback_func(void *arg, const unsigned int flags, const char *str)
361{
362 struct management *man = (struct management *)arg;
363 static int recursive_level = 0; /* GLOBAL */
364
365#define AF_DID_PUSH (1 << 0)
366#define AF_DID_RESET (1 << 1)
367 if (recursive_level < 5) /* limit recursion */
368 {
369 struct gc_arena gc = gc_new();
370 struct log_entry e;
371 const char *out = NULL;
372 unsigned int action_flags = 0;
373
374 ++recursive_level;
375
376 CLEAR(e);
377 update_time();
378 e.timestamp = now;
379 e.u.msg_flags = flags;
380 e.string = str;
381
382 if (flags & M_FATAL)
383 {
384 man->persist.standalone_disabled = false;
385 }
386
387 if (flags != M_CLIENT)
388 {
389 log_history_add(man->persist.log, &e);
390 }
391
392 if (!man_password_needed(man))
393 {
394 if (flags == M_CLIENT)
395 {
396 out = log_entry_print(&e, LOG_PRINT_CRLF, &gc);
397 }
398 else if (man->connection.log_realtime)
399 {
400 out = log_entry_print(&e,
403 &gc);
404 }
405 if (out)
406 {
407 man_output_list_push_str(man, out);
408 action_flags |= AF_DID_PUSH;
409 }
410 if (flags & M_FATAL)
411 {
413 if (out)
414 {
415 man_output_list_push_str(man, out);
416 action_flags |= (AF_DID_PUSH | AF_DID_RESET);
417 }
418 }
419 }
420
421 gc_free(&gc);
422
423 if (action_flags & AF_DID_PUSH)
424 {
426 }
427 if (action_flags & AF_DID_RESET)
428 {
429 man_reset_client_socket(man, true);
430 }
431
432 --recursive_level;
433 }
434 else
435 {
436 /* cannot use msg here */
437 printf("virtual_output: message to management interface "
438 "dropped due to recursion: <%s>\n",
439 str);
440 }
441}
442
443/*
444 * Given a signal, return the signal with possible remapping applied,
445 * or -1 if the signal should be ignored.
446 */
447static int
448man_mod_signal(const struct management *man, const int signum)
449{
450 const unsigned int flags = man->settings.mansig;
451 int s = signum;
452 if (s == SIGUSR1)
453 {
454 if (flags & MANSIG_MAP_USR1_TO_HUP)
455 {
456 s = SIGHUP;
457 }
458 if (flags & MANSIG_MAP_USR1_TO_TERM)
459 {
460 s = SIGTERM;
461 }
462 }
463 if (flags & MANSIG_IGNORE_USR1_HUP)
464 {
465 if (s == SIGHUP || s == SIGUSR1)
466 {
467 s = -1;
468 }
469 }
470 return s;
471}
472
473static void
474man_signal(struct management *man, const char *name)
475{
476 const int sig = parse_signal(name);
477 if (sig >= 0)
478 {
479 const int sig_mod = man_mod_signal(man, sig);
480 if (sig_mod >= 0)
481 {
482 throw_signal(sig_mod);
483 msg(M_CLIENT, "SUCCESS: signal %s thrown", signal_name(sig_mod, true));
484 }
485 else
486 {
487 msg(M_CLIENT, "ERROR: signal '%s' is currently ignored", name);
488 if (man->persist.special_state_msg)
489 {
491 }
492 }
493 }
494 else
495 {
496 msg(M_CLIENT, "ERROR: signal '%s' is not a known signal type", name);
497 }
498}
499
500static void
501man_command_unsupported(const char *command_name)
502{
503 msg(M_CLIENT, "ERROR: The '%s' command is not supported by the current daemon mode",
504 command_name);
505}
506
507static void
508man_status(struct management *man, const int version, struct status_output *so)
509{
510 if (man->persist.callback.status)
511 {
512 (*man->persist.callback.status)(man->persist.callback.arg, version, so);
513 }
514 else
515 {
516 man_command_unsupported("status");
517 }
518}
519
520static void
526
527static void
528man_bytecount(struct management *man, const int update_seconds)
529{
530 if (update_seconds > 0)
531 {
532 man->connection.bytecount_update_seconds = update_seconds;
535 }
536 else
537 {
539 }
540
541 /* The newly received bytecount interval may be sooner than the existing
542 * coarse timer wakeup. Reset the timer to ensure it fires at the correct,
543 * earlier time.
544 */
545 if (man->persist.callback.arg)
546 {
547 struct context *c;
548
549 if (man->settings.flags & MF_SERVER)
550 {
551 struct multi_context *m = man->persist.callback.arg;
552 c = &m->top;
553 }
554 else
555 {
556 c = man->persist.callback.arg;
557 }
558
560 }
561
562 msg(M_CLIENT, "SUCCESS: bytecount interval changed");
563}
564
565static void
567{
568 char in[32];
569 char out[32];
570
571 /* do in a roundabout way to work around possible mingw or mingw-glibc bug */
572 snprintf(in, sizeof(in), counter_format, bytes_in_total);
573 snprintf(out, sizeof(out), counter_format, bytes_out_total);
574 msg(M_CLIENT, ">BYTECOUNT:%s,%s", in, out);
575}
576
577static void
578man_bytecount_output_server(const counter_type bytes_in_total, const counter_type bytes_out_total,
579 struct man_def_auth_context *mdac)
580{
581 char in[32];
582 char out[32];
583 /* do in a roundabout way to work around possible mingw or mingw-glibc bug */
584 snprintf(in, sizeof(in), counter_format, bytes_in_total);
585 snprintf(out, sizeof(out), counter_format, bytes_out_total);
586 msg(M_CLIENT, ">BYTECOUNT_CLI:%lu,%s,%s", mdac->cid, in, out);
587}
588
589static void
590man_kill(struct management *man, const char *victim)
591{
592 struct gc_arena gc = gc_new();
593
595 {
596 struct buffer buf;
597 char p1[128];
598 char p2[128];
599 char p3[128];
600 int n_killed;
601
602 buf_set_read(&buf, (uint8_t *)victim, strlen(victim) + 1);
603 buf_parse(&buf, ':', p1, sizeof(p1));
604 buf_parse(&buf, ':', p2, sizeof(p2));
605 buf_parse(&buf, ':', p3, sizeof(p3));
606
607 if (strlen(p1) && strlen(p2) && strlen(p3))
608 {
609 /* IP:port specified */
610 bool status;
611 const in_addr_t addr =
613 if (status)
614 {
615 const int port = atoi(p3);
616 const uint8_t proto = (streq(p1, "tcp")) ? PROTO_TCP_SERVER
617 : (streq(p1, "udp")) ? PROTO_UDP
618 : PROTO_NONE;
619
620 if ((port > 0 && port <= UINT16_MAX) && (proto != PROTO_NONE))
621 {
622 n_killed = (*man->persist.callback.kill_by_addr)(man->persist.callback.arg,
623 addr, (uint16_t)port, proto);
624 if (n_killed > 0)
625 {
626 msg(M_CLIENT, "SUCCESS: %d client(s) at address %s:%s:%d killed", n_killed,
627 proto2ascii(proto, AF_UNSPEC, false), print_in_addr_t(addr, 0, &gc),
628 port);
629 }
630 else
631 {
632 msg(M_CLIENT, "ERROR: client at address %s:%s:%d not found",
633 proto2ascii(proto, AF_UNSPEC, false), print_in_addr_t(addr, 0, &gc),
634 port);
635 }
636 }
637 else
638 {
639 msg(M_CLIENT, "ERROR: port number or protocol out of range: %s %s", p3, p1);
640 }
641 }
642 else
643 {
644 msg(M_CLIENT, "ERROR: error parsing IP address: %s", p2);
645 }
646 }
647 else if (strlen(p1))
648 {
649 /* common name specified */
650 n_killed = (*man->persist.callback.kill_by_cn)(man->persist.callback.arg, p1);
651 if (n_killed > 0)
652 {
653 msg(M_CLIENT, "SUCCESS: common name '%s' found, %d client(s) killed", p1, n_killed);
654 }
655 else
656 {
657 msg(M_CLIENT, "ERROR: common name '%s' not found", p1);
658 }
659 }
660 else
661 {
662 msg(M_CLIENT, "ERROR: kill parse");
663 }
664 }
665 else
666 {
668 }
669
670 gc_free(&gc);
671}
672
673/*
674 * General-purpose history command handler
675 * for the log and echo commands.
676 */
677static void
678man_history(struct management *man, const char *parm, const char *type, struct log_history *log,
679 bool *realtime, const unsigned int lep_flags)
680{
681 struct gc_arena gc = gc_new();
682 int n = 0;
683
684 if (streq(parm, "on"))
685 {
686 *realtime = true;
687 msg(M_CLIENT, "SUCCESS: real-time %s notification set to ON", type);
688 }
689 else if (streq(parm, "off"))
690 {
691 *realtime = false;
692 msg(M_CLIENT, "SUCCESS: real-time %s notification set to OFF", type);
693 }
694 else if (streq(parm, "all") || (n = atoi(parm)) > 0)
695 {
696 const int size = log_history_size(log);
697 const int start = (n ? n : size) - 1;
698 int i;
699
700 for (i = start; i >= 0; --i)
701 {
702 const struct log_entry *e = log_history_ref(log, i);
703 if (e)
704 {
705 const char *out = log_entry_print(e, lep_flags, &gc);
707 }
708 }
709 msg(M_CLIENT, "END");
710 }
711 else
712 {
713 msg(M_CLIENT, "ERROR: %s parameter must be 'on' or 'off' or some number n or 'all'", type);
714 }
715
716 gc_free(&gc);
717}
718
719static void
720man_log(struct management *man, const char *parm)
721{
722 man_history(man, parm, "log", man->persist.log, &man->connection.log_realtime,
724}
725
726static void
727man_echo(struct management *man, const char *parm)
728{
729 man_history(man, parm, "echo", man->persist.echo, &man->connection.echo_realtime,
731}
732
733static void
734man_state(struct management *man, const char *parm)
735{
736 man_history(man, parm, "state", man->persist.state, &man->connection.state_realtime,
738}
739
740static void
742{
743 switch (man->connection.up_query_mode)
744 {
746 if (strlen(man->connection.up_query.username))
747 {
748 man->connection.up_query.defined = true;
749 }
750 break;
751
753 if (!strlen(man->connection.up_query.username))
754 {
755 break;
756 }
757
758 /* fall through */
759 case UP_QUERY_PASS:
760 case UP_QUERY_NEED_OK:
762 if (strlen(man->connection.up_query.password))
763 {
764 man->connection.up_query.defined = true;
765 }
766 break;
767
769 man->connection.up_query.defined = false;
770 break;
771
772 default:
773 ASSERT(0);
774 }
775}
776
777static void
778man_query_user_pass(struct management *man, const char *type, const char *string, const bool needed,
779 const char *prompt, char *dest, int len)
780{
781 if (needed)
782 {
784 if (streq(man->connection.up_query_type, type))
785 {
786 strncpynt(dest, string, len);
787 man_up_finalize(man);
788 msg(M_CLIENT, "SUCCESS: '%s' %s entered, but not yet verified", type, prompt);
789 }
790 else
791 {
792 msg(M_CLIENT, "ERROR: %s of type '%s' entered, but we need one of type '%s'", prompt,
793 type, man->connection.up_query_type);
794 }
795 }
796 else
797 {
798 msg(M_CLIENT, "ERROR: no %s is currently needed at this time", prompt);
799 }
800}
801
802static void
803man_query_username(struct management *man, const char *type, const char *string)
804{
805 const bool needed =
808 && man->connection.up_query_type);
809 man_query_user_pass(man, type, string, needed, "username", man->connection.up_query.username,
811}
812
813static void
814man_query_password(struct management *man, const char *type, const char *string)
815{
816 const bool needed = ((man->connection.up_query_mode == UP_QUERY_PASS
818 && man->connection.up_query_type);
819 if (!string[0]) /* allow blank passwords to be passed through using the blank_up tag */
820 {
821 string = blank_up;
822 }
823 man_query_user_pass(man, type, string, needed, "password", man->connection.up_query.password,
825}
826
827static void
828man_query_need_ok(struct management *man, const char *type, const char *action)
829{
830 const bool needed =
832 man_query_user_pass(man, type, action, needed, "needok-confirmation",
834}
835
836static void
837man_query_need_str(struct management *man, const char *type, const char *action)
838{
839 const bool needed =
841 man_query_user_pass(man, type, action, needed, "needstr-string",
843}
844
845static void
847{
848 ssl_purge_auth(false);
849 (void)ssl_clean_auth_token();
850 msg(M_CLIENT, "SUCCESS: Passwords were forgotten");
851}
852
853static void
854man_net(struct management *man)
855{
856 if (man->persist.callback.show_net)
857 {
859 }
860 else
861 {
863 }
864}
865
866static void
867man_send_cc_message(struct management *man, const char *message, const char *parameters)
868{
870 {
871 const bool status = (*man->persist.callback.send_cc_message)(man->persist.callback.arg,
872 message, parameters);
873 if (status)
874 {
875 msg(M_CLIENT, "SUCCESS: command succeeded");
876 }
877 else
878 {
879 msg(M_CLIENT, "ERROR: command failed");
880 }
881 }
882 else
883 {
884 man_command_unsupported("cr-repsonse");
885 }
886}
887#ifdef ENABLE_PKCS11
888
889static void
890man_pkcs11_id_count(struct management *man)
891{
892 msg(M_CLIENT, ">PKCS11ID-COUNT:%d", pkcs11_management_id_count());
893}
894
895static void
896man_pkcs11_id_get(struct management *man, const int index)
897{
898 char *id = NULL;
899 char *base64 = NULL;
900
901 if (pkcs11_management_id_get(index, &id, &base64))
902 {
903 msg(M_CLIENT, ">PKCS11ID-ENTRY:'%d', ID:'%s', BLOB:'%s'", index, id, base64);
904 }
905 else
906 {
907 msg(M_CLIENT, ">PKCS11ID-ENTRY:'%d'", index);
908 }
909
910 free(id);
911 free(base64);
912}
913
914#endif /* ifdef ENABLE_PKCS11 */
915
916static void
918{
920 {
921 unsigned int count = (*man->persist.callback.remote_entry_count)(man->persist.callback.arg);
922 msg(M_CLIENT, "%u", count);
923 msg(M_CLIENT, "END");
924 }
925 else
926 {
927 man_command_unsupported("remote-entry-count");
928 }
929}
930
931static void
932man_remote_entry_get(struct management *man, const char *p1, const char *p2)
933{
934 ASSERT(p1);
935
937 {
938 unsigned int count = (*man->persist.callback.remote_entry_count)(man->persist.callback.arg);
939
940 unsigned int from = (unsigned int)atoi(p1);
941 unsigned int to = p2 ? (unsigned int)atoi(p2) : from + 1;
942
943 if (!strcmp(p1, "all"))
944 {
945 from = 0;
946 to = count;
947 }
948
949 for (unsigned int i = from; i < min_uint(to, count); i++)
950 {
951 char *remote = NULL;
952 bool res =
953 (*man->persist.callback.remote_entry_get)(man->persist.callback.arg, i, &remote);
954 if (res && remote)
955 {
956 msg(M_CLIENT, "%u,%s", i, remote);
957 }
958 free(remote);
959 }
960 msg(M_CLIENT, "END");
961 }
962 else
963 {
964 man_command_unsupported("remote-entry-get");
965 }
966}
967
968static void
969man_hold(struct management *man, const char *cmd)
970{
971 if (cmd)
972 {
973 if (streq(cmd, "on"))
974 {
975 man->settings.flags |= MF_HOLD;
976 msg(M_CLIENT, "SUCCESS: hold flag set to ON");
977 }
978 else if (streq(cmd, "off"))
979 {
980 man->settings.flags &= ~MF_HOLD;
981 msg(M_CLIENT, "SUCCESS: hold flag set to OFF");
982 }
983 else if (streq(cmd, "release"))
984 {
985 man->persist.hold_release = true;
986 msg(M_CLIENT, "SUCCESS: hold release succeeded");
987 }
988 else
989 {
990 msg(M_CLIENT, "ERROR: bad hold command parameter");
991 }
992 }
993 else
994 {
995 msg(M_CLIENT, "SUCCESS: hold=%d", BOOL_CAST(man->settings.flags & MF_HOLD));
996 }
997}
998
999#define IER_RESET 0
1000#define IER_NEW 1
1001
1002static void
1003in_extra_reset(struct man_connection *mc, const int mode)
1004{
1005 if (mc)
1006 {
1007 if (mode != IER_NEW)
1008 {
1009 mc->in_extra_cmd = IEC_UNDEF;
1010 mc->in_extra_cid = 0;
1011 mc->in_extra_kid = 0;
1012 }
1013 if (mc->in_extra)
1014 {
1016 mc->in_extra = NULL;
1017 }
1018 if (mode == IER_NEW)
1019 {
1020 mc->in_extra = buffer_list_new();
1021 }
1022 }
1023}
1024
1038static void
1039man_query_password_base64(struct management *man, const char *type)
1040{
1041 const bool needed = ((man->connection.up_query_mode == UP_QUERY_PASS
1043 && man->connection.up_query_type);
1044 if (!needed)
1045 {
1046 msg(M_CLIENT, "ERROR: no password is currently needed at this time");
1047 return;
1048 }
1049 if (!man->connection.up_query_type || !streq(man->connection.up_query_type, type))
1050 {
1051 msg(M_CLIENT, "ERROR: password of type '%s' entered, but we need one of type '%s'",
1052 type, man->connection.up_query_type);
1053 return;
1054 }
1055 struct man_connection *mc = &man->connection;
1058}
1059
1060static void
1062{
1063 switch (man->connection.in_extra_cmd)
1064 {
1065 case IEC_CLIENT_AUTH:
1066 if (man->persist.callback.client_auth)
1067 {
1068 const bool status = (*man->persist.callback.client_auth)(
1070 man->connection.in_extra_kid, true, NULL, NULL, man->connection.in_extra);
1071 man->connection.in_extra = NULL;
1072 report_command_status(status, "client-auth");
1073 }
1074 else
1075 {
1076 man_command_unsupported("client-auth");
1077 }
1078 break;
1079
1080 case IEC_PK_SIGN:
1084 man->connection.in_extra = NULL;
1085 return;
1086
1087 case IEC_CERTIFICATE:
1091 man->connection.in_extra = NULL;
1092 return;
1093
1094 case IEC_PASSWORD:
1095 {
1096 char decoded[USER_PASS_LEN];
1097 CLEAR(decoded);
1098
1101 struct buffer *buf = buffer_list_peek(man->connection.in_extra);
1102
1103 if (buf && BLEN(buf) > 0)
1104 {
1106 {
1107 msg(M_CLIENT, "ERROR: password too long");
1108 buf_clear(buf);
1109 break;
1110 }
1112 USER_PASS_LEN - 1);
1113 if (len < 0)
1114 {
1115 msg(M_CLIENT, "ERROR: could not base64-decode password");
1116 buf_clear(buf);
1117 break;
1118 }
1119 decoded[len] = '\0';
1120 buf_clear(buf);
1121 }
1122
1123 man_query_password(man, man->connection.up_query_type,
1124 decoded);
1125 secure_memzero(decoded, sizeof(decoded));
1126 break;
1127 }
1128 }
1129 in_extra_reset(&man->connection, IER_RESET);
1130}
1131
1132static bool
1133parse_cid(const char *str, unsigned long *cid)
1134{
1135 if (sscanf(str, "%lu", cid) == 1)
1136 {
1137 return true;
1138 }
1139 else
1140 {
1141 msg(M_CLIENT, "ERROR: cannot parse CID");
1142 return false;
1143 }
1144}
1145
1146static bool
1147parse_uint(const char *str, const char *what, unsigned int *uint)
1148{
1149 if (sscanf(str, "%u", uint) == 1)
1150 {
1151 return true;
1152 }
1153 else
1154 {
1155 msg(M_CLIENT, "ERROR: cannot parse %s", what);
1156 return false;
1157 }
1158}
1159
1171static void
1172man_client_pending_auth(struct management *man, const char *cid_str, const char *kid_str,
1173 const char *extra, const char *timeout_str)
1174{
1175 unsigned long cid = 0;
1176 unsigned int kid = 0;
1177 unsigned int timeout = 0;
1178 if (parse_cid(cid_str, &cid) && parse_uint(kid_str, "KID", &kid)
1179 && parse_uint(timeout_str, "TIMEOUT", &timeout))
1180 {
1181 if (man->persist.callback.client_pending_auth)
1182 {
1183 bool ret = (*man->persist.callback.client_pending_auth)(man->persist.callback.arg, cid,
1184 kid, extra, timeout);
1185
1186 if (ret)
1187 {
1188 msg(M_CLIENT, "SUCCESS: client-pending-auth command succeeded");
1189 }
1190 else
1191 {
1192 msg(M_CLIENT, "ERROR: client-pending-auth command failed."
1193 " Extra parameter might be too long");
1194 }
1195 }
1196 else
1197 {
1198 man_command_unsupported("client-pending-auth");
1199 }
1200 }
1201}
1202
1203static void
1204man_client_auth(struct management *man, const char *cid_str, const char *kid_str, const bool extra)
1205{
1206 struct man_connection *mc = &man->connection;
1207 mc->in_extra_cid = 0;
1208 mc->in_extra_kid = 0;
1209 if (parse_cid(cid_str, &mc->in_extra_cid) && parse_uint(kid_str, "KID", &mc->in_extra_kid))
1210 {
1213 if (!extra)
1214 {
1215 in_extra_dispatch(man);
1216 }
1217 }
1218}
1219
1220static void
1221man_client_deny(struct management *man, const char *cid_str, const char *kid_str,
1222 const char *reason, const char *client_reason)
1223{
1224 unsigned long cid = 0;
1225 unsigned int kid = 0;
1226 if (parse_cid(cid_str, &cid) && parse_uint(kid_str, "KID", &kid))
1227 {
1228 if (man->persist.callback.client_auth)
1229 {
1230 const bool status = (*man->persist.callback.client_auth)(
1231 man->persist.callback.arg, cid, kid, false, reason, client_reason, NULL);
1232 if (status)
1233 {
1234 msg(M_CLIENT, "SUCCESS: client-deny command succeeded");
1235 }
1236 else
1237 {
1238 msg(M_CLIENT, "ERROR: client-deny command failed");
1239 }
1240 }
1241 else
1242 {
1243 man_command_unsupported("client-deny");
1244 }
1245 }
1246}
1247
1248static void
1249man_client_kill(struct management *man, const char *cid_str, const char *kill_msg)
1250{
1251 unsigned long cid = 0;
1252 if (parse_cid(cid_str, &cid))
1253 {
1254 if (man->persist.callback.kill_by_cid)
1255 {
1256 const bool status =
1257 (*man->persist.callback.kill_by_cid)(man->persist.callback.arg, cid, kill_msg);
1258 if (status)
1259 {
1260 msg(M_CLIENT, "SUCCESS: client-kill command succeeded");
1261 }
1262 else
1263 {
1264 msg(M_CLIENT, "ERROR: client-kill command failed");
1265 }
1266 }
1267 else
1268 {
1269 man_command_unsupported("client-kill");
1270 }
1271 }
1272}
1273
1274static void
1276{
1277 if (man->persist.callback.n_clients)
1278 {
1279 const int nclients = (*man->persist.callback.n_clients)(man->persist.callback.arg);
1280 msg(M_CLIENT, "SUCCESS: nclients=%d", nclients);
1281 }
1282 else
1283 {
1284 man_command_unsupported("nclients");
1285 }
1286}
1287
1288static void
1289man_env_filter(struct management *man, const int level)
1290{
1291 man->connection.env_filter_level = level;
1292 msg(M_CLIENT, "SUCCESS: env_filter_level=%d", level);
1293}
1294
1295
1296static void
1297man_pk_sig(struct management *man, const char *cmd_name)
1298{
1299 struct man_connection *mc = &man->connection;
1300 if (mc->ext_key_state == EKS_SOLICIT)
1301 {
1305 }
1306 else
1307 {
1308 msg(M_CLIENT, "ERROR: The %s command is not currently available", cmd_name);
1309 }
1310}
1311
1312static void
1314{
1315 struct man_connection *mc = &man->connection;
1316 if (mc->ext_cert_state == EKS_SOLICIT)
1317 {
1321 }
1322 else
1323 {
1324 msg(M_CLIENT, "ERROR: The certificate command is not currently available");
1325 }
1326}
1327
1328static void
1330{
1331 int nclients = 0;
1332
1333 if (man->persist.callback.n_clients)
1334 {
1335 nclients = (*man->persist.callback.n_clients)(man->persist.callback.arg);
1336 }
1337 msg(M_CLIENT, "SUCCESS: nclients=%d,bytesin=" counter_format ",bytesout=" counter_format,
1339}
1340
1341#define MN_AT_LEAST (1 << 0)
1351static bool
1352man_need(const char **p, const int n, unsigned int flags)
1353{
1354 ASSERT(p[0]);
1355 for (int i = 1; i <= n; ++i)
1356 {
1357 if (!p[i])
1358 {
1359 msg(M_CLIENT, "ERROR: the '%s' command requires %s%d parameter%s", p[0],
1360 (flags & MN_AT_LEAST) ? "at least " : "", n, n > 1 ? "s" : "");
1361 return false;
1362 }
1363 }
1364 return true;
1365}
1366
1367static void
1368man_proxy(struct management *man, const char **p)
1369{
1370 if (man->persist.callback.proxy_cmd)
1371 {
1372 const bool status = (*man->persist.callback.proxy_cmd)(man->persist.callback.arg, p);
1373 report_command_status(status, "proxy");
1374 }
1375 else
1376 {
1377 man_command_unsupported("proxy");
1378 }
1379}
1380
1381static void
1382man_remote(struct management *man, const char **p)
1383{
1384 if (man->persist.callback.remote_cmd)
1385 {
1386 const bool status = (*man->persist.callback.remote_cmd)(man->persist.callback.arg, p);
1387 report_command_status(status, "remote");
1388 }
1389 else
1390 {
1391 man_command_unsupported("remote");
1392 }
1393}
1394
1395#ifdef TARGET_ANDROID
1396static void
1397man_network_change(struct management *man, bool samenetwork)
1398{
1399 /* Called to signal the OpenVPN that the network configuration has changed and
1400 * the client should either float or reconnect.
1401 *
1402 * The code is currently only used by ics-openvpn
1403 */
1404 if (man->persist.callback.network_change)
1405 {
1406 int fd = (*man->persist.callback.network_change)(man->persist.callback.arg, samenetwork);
1407 man->connection.fdtosend = fd;
1408 msg(M_CLIENT, "PROTECTFD: fd '%d' sent to be protected", fd);
1409 if (fd == -2)
1410 {
1411 man_signal(man, "SIGUSR1");
1412 }
1413 }
1414}
1415#endif
1416
1417static void
1418set_client_version(struct management *man, const char *version)
1419{
1420 if (version)
1421 {
1422 man->connection.client_version = atoi(version);
1423 /* Until MCV_PKSIGN_ALG, we missed to respond to this command. Acknowledge only if version is newer */
1425 {
1426 msg(M_CLIENT, "SUCCESS: Management client version set to %d", man->connection.client_version);
1427 }
1428 }
1429 else
1430 {
1431 msg(M_CLIENT, "ERROR: Invalid value specified for management client version");
1432 }
1433}
1434
1435static void
1436man_push_update(struct management *man, const char **p, const push_update_type type)
1437{
1438 bool status = false;
1439
1440 if (type == UPT_BROADCAST)
1441 {
1443 {
1444 man_command_unsupported("push-update-broad");
1445 return;
1446 }
1447
1449 }
1450 else if (type == UPT_BY_CID)
1451 {
1453 {
1454 man_command_unsupported("push-update-cid");
1455 return;
1456 }
1457
1458 unsigned long cid = 0;
1459
1460 if (!parse_cid(p[1], &cid))
1461 {
1462 msg(M_CLIENT, "ERROR: push-update-cid fail during cid parsing");
1463 return;
1464 }
1465
1466 status = (*man->persist.callback.push_update_by_cid)(man->persist.callback.arg, cid, p[2]);
1467 }
1468
1469 if (status)
1470 {
1471 msg(M_CLIENT, "SUCCESS: push-update command succeeded");
1472 return;
1473 }
1474 msg(M_CLIENT, "ERROR: push-update command failed");
1475}
1476
1477static void
1478man_dispatch_command(struct management *man, struct status_output *so, const char **p,
1479 const int nparms)
1480{
1481 struct gc_arena gc = gc_new();
1482
1483 ASSERT(p[0]);
1484 if (streq(p[0], "exit") || streq(p[0], "quit"))
1485 {
1486 man->connection.halt = true;
1487 goto done;
1488 }
1489 else if (streq(p[0], "help"))
1490 {
1491 man_help();
1492 }
1493 else if (streq(p[0], "version") && p[1])
1494 {
1495 set_client_version(man, p[1]);
1496 }
1497 else if (streq(p[0], "version"))
1498 {
1499 msg(M_CLIENT, "OpenVPN Version: %s", title_string);
1500 msg(M_CLIENT, "Management Version: %d", MANAGEMENT_VERSION);
1501 msg(M_CLIENT, "END");
1502 }
1503 else if (streq(p[0], "pid"))
1504 {
1505 msg(M_CLIENT, "SUCCESS: pid=%d", platform_getpid());
1506 }
1507 else if (streq(p[0], "nclients"))
1508 {
1510 }
1511 else if (streq(p[0], "env-filter"))
1512 {
1513 int level = 0;
1514 if (p[1])
1515 {
1516 level = atoi(p[1]);
1517 }
1518 man_env_filter(man, level);
1519 }
1520 else if (streq(p[0], "signal"))
1521 {
1522 if (man_need(p, 1, 0))
1523 {
1524 man_signal(man, p[1]);
1525 }
1526 }
1527#ifdef TARGET_ANDROID
1528 else if (streq(p[0], "network-change"))
1529 {
1530 bool samenetwork = false;
1531 if (p[1] && streq(p[1], "samenetwork"))
1532 {
1533 samenetwork = true;
1534 }
1535
1536 man_network_change(man, samenetwork);
1537 }
1538#endif
1539 else if (streq(p[0], "load-stats"))
1540 {
1541 man_load_stats(man);
1542 }
1543 else if (streq(p[0], "status"))
1544 {
1545 int version = 0;
1546 if (p[1])
1547 {
1548 version = atoi(p[1]);
1549 }
1550 man_status(man, version, so);
1551 }
1552 else if (streq(p[0], "kill"))
1553 {
1554 if (man_need(p, 1, 0))
1555 {
1556 man_kill(man, p[1]);
1557 }
1558 }
1559 else if (streq(p[0], "verb"))
1560 {
1561 if (p[1])
1562 {
1563 const int level = atoi(p[1]);
1564 if (set_debug_level(level, 0))
1565 {
1566 msg(M_CLIENT, "SUCCESS: verb level changed");
1567 }
1568 else
1569 {
1570 msg(M_CLIENT, "ERROR: verb level is out of range");
1571 }
1572 }
1573 else
1574 {
1575 msg(M_CLIENT, "SUCCESS: verb=%u", get_debug_level());
1576 }
1577 }
1578 else if (streq(p[0], "mute"))
1579 {
1580 if (p[1])
1581 {
1582 const int level = atoi(p[1]);
1583 if (set_mute_cutoff(level))
1584 {
1585 msg(M_CLIENT, "SUCCESS: mute level changed");
1586 }
1587 else
1588 {
1589 msg(M_CLIENT, "ERROR: mute level is out of range");
1590 }
1591 }
1592 else
1593 {
1594 msg(M_CLIENT, "SUCCESS: mute=%d", get_mute_cutoff());
1595 }
1596 }
1597 else if (streq(p[0], "auth-retry"))
1598 {
1599 if (p[1])
1600 {
1601 if (auth_retry_set(M_CLIENT, p[1]))
1602 {
1603 msg(M_CLIENT, "SUCCESS: auth-retry parameter changed");
1604 }
1605 else
1606 {
1607 msg(M_CLIENT, "ERROR: bad auth-retry parameter");
1608 }
1609 }
1610 else
1611 {
1612 msg(M_CLIENT, "SUCCESS: auth-retry=%s", auth_retry_print());
1613 }
1614 }
1615 else if (streq(p[0], "state"))
1616 {
1617 if (!p[1])
1618 {
1619 man_state(man, "1");
1620 }
1621 else
1622 {
1623 man_state(man, p[1]);
1624 if (p[2])
1625 {
1626 man_state(man, p[2]);
1627 }
1628 }
1629 }
1630 else if (streq(p[0], "log"))
1631 {
1632 if (man_need(p, 1, MN_AT_LEAST))
1633 {
1634 if (p[1])
1635 {
1636 man_log(man, p[1]);
1637 }
1638 if (p[2])
1639 {
1640 man_log(man, p[2]);
1641 }
1642 }
1643 }
1644 else if (streq(p[0], "echo"))
1645 {
1646 if (man_need(p, 1, MN_AT_LEAST))
1647 {
1648 if (p[1])
1649 {
1650 man_echo(man, p[1]);
1651 }
1652 if (p[2])
1653 {
1654 man_echo(man, p[2]);
1655 }
1656 }
1657 }
1658 else if (streq(p[0], "username"))
1659 {
1660 if (man_need(p, 2, 0))
1661 {
1662 man_query_username(man, p[1], p[2]);
1663 }
1664 }
1665 else if (streq(p[0], "password"))
1666 {
1667 if (man_need(p, 1, MN_AT_LEAST))
1668 {
1669 if (p[2])
1670 {
1671 man_query_password(man, p[1], p[2]);
1672 }
1674 {
1675 man_query_password_base64(man, p[1]);
1676 }
1677 else
1678 {
1679 msg(M_CLIENT, "ERROR: the 'password' command requires 2 parameters");
1680 }
1681 }
1682 }
1683 else if (streq(p[0], "forget-passwords"))
1684 {
1686 }
1687 else if (streq(p[0], "needok"))
1688 {
1689 if (man_need(p, 2, 0))
1690 {
1691 man_query_need_ok(man, p[1], p[2]);
1692 }
1693 }
1694 else if (streq(p[0], "needstr"))
1695 {
1696 if (man_need(p, 2, 0))
1697 {
1698 man_query_need_str(man, p[1], p[2]);
1699 }
1700 }
1701 else if (streq(p[0], "cr-response"))
1702 {
1703 if (man_need(p, 1, 0))
1704 {
1705 man_send_cc_message(man, "CR_RESPONSE", p[1]);
1706 }
1707 }
1708 else if (streq(p[0], "net"))
1709 {
1710 man_net(man);
1711 }
1712 else if (streq(p[0], "hold"))
1713 {
1714 man_hold(man, p[1]);
1715 }
1716 else if (streq(p[0], "bytecount"))
1717 {
1718 if (man_need(p, 1, 0))
1719 {
1720 man_bytecount(man, atoi(p[1]));
1721 }
1722 }
1723 else if (streq(p[0], "client-kill"))
1724 {
1725 if (man_need(p, 1, MN_AT_LEAST))
1726 {
1727 man_client_kill(man, p[1], p[2]);
1728 }
1729 }
1730 else if (streq(p[0], "client-deny"))
1731 {
1732 if (man_need(p, 3, MN_AT_LEAST))
1733 {
1734 man_client_deny(man, p[1], p[2], p[3], p[4]);
1735 }
1736 }
1737 else if (streq(p[0], "client-auth-nt"))
1738 {
1739 if (man_need(p, 2, 0))
1740 {
1741 man_client_auth(man, p[1], p[2], false);
1742 }
1743 }
1744 else if (streq(p[0], "client-auth"))
1745 {
1746 if (man_need(p, 2, 0))
1747 {
1748 man_client_auth(man, p[1], p[2], true);
1749 }
1750 }
1751 else if (streq(p[0], "client-pending-auth"))
1752 {
1753 if (man_need(p, 4, 0))
1754 {
1755 man_client_pending_auth(man, p[1], p[2], p[3], p[4]);
1756 }
1757 }
1758 else if (streq(p[0], "rsa-sig"))
1759 {
1760 man_pk_sig(man, "rsa-sig");
1761 }
1762 else if (streq(p[0], "pk-sig"))
1763 {
1764 man_pk_sig(man, "pk-sig");
1765 }
1766 else if (streq(p[0], "certificate"))
1767 {
1768 man_certificate(man);
1769 }
1770#ifdef ENABLE_PKCS11
1771 else if (streq(p[0], "pkcs11-id-count"))
1772 {
1773 man_pkcs11_id_count(man);
1774 }
1775 else if (streq(p[0], "pkcs11-id-get"))
1776 {
1777 if (man_need(p, 1, 0))
1778 {
1779 man_pkcs11_id_get(man, atoi(p[1]));
1780 }
1781 }
1782#endif
1783 else if (streq(p[0], "remote-entry-count"))
1784 {
1786 }
1787 else if (streq(p[0], "remote-entry-get"))
1788 {
1789 if (man_need(p, 1, MN_AT_LEAST))
1790 {
1791 man_remote_entry_get(man, p[1], p[2]);
1792 }
1793 }
1794 else if (streq(p[0], "proxy"))
1795 {
1796 if (man_need(p, 1, MN_AT_LEAST))
1797 {
1798 man_proxy(man, p);
1799 }
1800 }
1801 else if (streq(p[0], "remote"))
1802 {
1803 if (man_need(p, 1, MN_AT_LEAST))
1804 {
1805 man_remote(man, p);
1806 }
1807 }
1808 else if (streq(p[0], "push-update-broad"))
1809 {
1810 if (man_need(p, 1, 0))
1811 {
1813 }
1814 }
1815 else if (streq(p[0], "push-update-cid"))
1816 {
1817 if (man_need(p, 2, 0))
1818 {
1819 man_push_update(man, p, UPT_BY_CID);
1820 }
1821 }
1822#if 1
1823 else if (streq(p[0], "test"))
1824 {
1825 if (man_need(p, 1, 0))
1826 {
1827 int i;
1828 const int n = atoi(p[1]);
1829 for (i = 0; i < n; ++i)
1830 {
1831 msg(M_CLIENT,
1832 "[%d] The purpose of this command is to generate large amounts of output.", i);
1833 }
1834 }
1835 }
1836#endif
1837 else
1838 {
1839 msg(M_CLIENT, "ERROR: unknown command [%s], enter 'help' for more options", p[0]);
1840 }
1841
1842done:
1843 gc_free(&gc);
1844}
1845
1846#ifdef _WIN32
1847
1848static void
1850{
1851 switch (man->connection.state)
1852 {
1853 case MS_LISTEN:
1854 net_event_win32_start(&man->connection.ne32, FD_ACCEPT, man->connection.sd_top);
1855 break;
1856
1857 case MS_CC_WAIT_READ:
1858 case MS_CC_WAIT_WRITE:
1859 net_event_win32_start(&man->connection.ne32, FD_READ | FD_WRITE | FD_CLOSE,
1860 man->connection.sd_cli);
1861 break;
1862
1863 default:
1864 ASSERT(0);
1865 }
1866}
1867
1868static void
1870{
1872}
1873
1874#endif /* ifdef _WIN32 */
1875
1876static void
1878{
1879 man->connection.state_realtime = false;
1880 man->connection.log_realtime = false;
1881 man->connection.echo_realtime = false;
1883 man->connection.password_verified = false;
1884 man->connection.password_tries = 0;
1885 man->connection.halt = false;
1887}
1888
1889static void
1890man_new_connection_post(struct management *man, const char *description)
1891{
1892 struct gc_arena gc = gc_new();
1893
1895
1897
1898#ifdef _WIN32
1899 man_start_ne32(man);
1900#endif
1901
1902#if UNIX_SOCK_SUPPORT
1903 if (man->settings.flags & MF_UNIX_SOCK)
1904 {
1905 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description,
1906 sockaddr_unix_name(&man->settings.local_unix, "NULL"));
1907 }
1908 else
1909#endif
1911 {
1912 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description,
1913 print_sockaddr(man->settings.local->ai_addr, &gc));
1914 }
1915 else
1916 {
1917 struct sockaddr_storage addr;
1918 socklen_t addrlen = sizeof(addr);
1919 if (!getpeername(man->connection.sd_cli, (struct sockaddr *)&addr, &addrlen))
1920 {
1921 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description,
1922 print_sockaddr((struct sockaddr *)&addr, &gc));
1923 }
1924 else
1925 {
1926 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description, "unknown");
1927 }
1928 }
1929
1931
1932 if (!man_password_needed(man))
1933 {
1934 man_welcome(man);
1935 }
1936 man_prompt(man);
1938
1939 gc_free(&gc);
1940}
1941
1942#if UNIX_SOCK_SUPPORT
1943static bool
1944man_verify_unix_peer_uid_gid(struct management *man, const socket_descriptor_t sd)
1945{
1946 if (socket_defined(sd) && (man->settings.user.user_valid || man->settings.group.group_valid))
1947 {
1948 static const char err_prefix[] =
1949 "MANAGEMENT: unix domain socket client connection rejected --";
1950 uid_t uid;
1951 gid_t gid;
1952 if (unix_socket_get_peer_uid_gid(man->connection.sd_cli, &uid, &gid))
1953 {
1954 if (man->settings.user.user_valid && man->settings.user.uid != uid)
1955 {
1957 "%s UID of socket peer (%d) doesn't match required value (%d) as given by --management-client-user",
1958 err_prefix, uid, man->settings.user.uid);
1959 return false;
1960 }
1961 if (man->settings.group.group_valid && man->settings.group.gid != gid)
1962 {
1964 "%s GID of socket peer (%d) doesn't match required value (%d) as given by --management-client-group",
1965 err_prefix, gid, man->settings.group.gid);
1966 return false;
1967 }
1968 }
1969 else
1970 {
1971 msg(D_MANAGEMENT, "%s cannot get UID/GID of socket peer", err_prefix);
1972 return false;
1973 }
1974 }
1975 return true;
1976}
1977#endif /* if UNIX_SOCK_SUPPORT */
1978
1979static void
1981{
1982 struct link_socket_actual act;
1983 CLEAR(act);
1984
1985 /*
1986 * Accept the TCP or Unix domain socket client.
1987 */
1988#if UNIX_SOCK_SUPPORT
1989 if (man->settings.flags & MF_UNIX_SOCK)
1990 {
1991 struct sockaddr_un remote;
1992 man->connection.sd_cli = socket_accept_unix(man->connection.sd_top, &remote);
1993 if (!man_verify_unix_peer_uid_gid(man, man->connection.sd_cli))
1994 {
1995 sd_close(&man->connection.sd_cli);
1996 }
1997 }
1998 else
1999#endif
2000 {
2001 man->connection.sd_cli = socket_do_accept(man->connection.sd_top, &act, false);
2002 }
2003
2005 {
2006 man->connection.remote = act.dest;
2007
2009 {
2010#ifdef _WIN32
2011 man_stop_ne32(man);
2012#endif
2013 }
2014
2015 man_new_connection_post(man, "Client connected from");
2016 }
2017}
2018
2019static void
2021{
2022 struct gc_arena gc = gc_new();
2023
2024 /*
2025 * Initialize state
2026 */
2027 man->connection.state = MS_LISTEN;
2029
2030 /*
2031 * Initialize listening socket
2032 */
2034 {
2035#if UNIX_SOCK_SUPPORT
2036 if (man->settings.flags & MF_UNIX_SOCK)
2037 {
2039 man->connection.sd_top = create_socket_unix();
2040 socket_bind_unix(man->connection.sd_top, &man->settings.local_unix, "MANAGEMENT");
2041 }
2042 else
2043#endif
2044 {
2046 socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
2047 "MANAGEMENT", false);
2048 }
2049
2050 /*
2051 * Listen for connection
2052 */
2053 if (listen(man->connection.sd_top, 1))
2054 {
2055 msg(M_ERR, "MANAGEMENT: listen() failed");
2056 }
2057
2058 /*
2059 * Set misc socket properties
2060 */
2062
2063#if UNIX_SOCK_SUPPORT
2064 if (man->settings.flags & MF_UNIX_SOCK)
2065 {
2066 msg(D_MANAGEMENT, "MANAGEMENT: unix domain socket listening on %s",
2067 sockaddr_unix_name(&man->settings.local_unix, "NULL"));
2068 }
2069 else
2070#endif
2071 {
2072 const struct sockaddr *man_addr = man->settings.local->ai_addr;
2073 struct sockaddr_storage addr;
2074 socklen_t addrlen = sizeof(addr);
2075 if (!getsockname(man->connection.sd_top, (struct sockaddr *)&addr, &addrlen))
2076 {
2077 man_addr = (struct sockaddr *)&addr;
2078 }
2079 else
2080 {
2081 msg(M_WARN | M_ERRNO, "Failed to get the management socket address");
2082 }
2083 msg(D_MANAGEMENT, "MANAGEMENT: TCP Socket listening on %s",
2084 print_sockaddr(man_addr, &gc));
2085 }
2086 }
2087
2088#ifdef _WIN32
2089 man_start_ne32(man);
2090#endif
2091
2092 gc_free(&gc);
2093}
2094
2095static void
2097{
2098 struct gc_arena gc = gc_new();
2099 int status;
2100 int signal_received = 0;
2101
2102 /*
2103 * Initialize state
2104 */
2107
2108#if UNIX_SOCK_SUPPORT
2109 if (man->settings.flags & MF_UNIX_SOCK)
2110 {
2111 man->connection.sd_cli = create_socket_unix();
2112 status = socket_connect_unix(man->connection.sd_cli, &man->settings.local_unix);
2113 if (!status && !man_verify_unix_peer_uid_gid(man, man->connection.sd_cli))
2114 {
2115#ifdef EPERM
2116 status = EPERM;
2117#else
2118 status = 1;
2119#endif
2120 sd_close(&man->connection.sd_cli);
2121 }
2122 }
2123 else
2124#endif
2125 {
2127 status = openvpn_connect(man->connection.sd_cli, man->settings.local->ai_addr, 5,
2128 &signal_received);
2129 }
2130
2131 if (signal_received)
2132 {
2133 throw_signal(signal_received);
2134 goto done;
2135 }
2136
2137 if (status)
2138 {
2139#if UNIX_SOCK_SUPPORT
2140 if (man->settings.flags & MF_UNIX_SOCK)
2141 {
2142 msg(D_LINK_ERRORS | M_ERRNO, "MANAGEMENT: connect to unix socket %s failed",
2143 sockaddr_unix_name(&man->settings.local_unix, "NULL"));
2144 }
2145 else
2146#endif
2147 {
2148 msg(D_LINK_ERRORS | M_ERRNO, "MANAGEMENT: connect to %s failed",
2149 print_sockaddr(man->settings.local->ai_addr, &gc));
2150 }
2151 throw_signal_soft(SIGTERM, "management-connect-failed");
2152 goto done;
2153 }
2154
2155 man_new_connection_post(man, "Connected to management server at");
2156
2157done:
2158 gc_free(&gc);
2159}
2160
2161static void
2162man_reset_client_socket(struct management *man, const bool exiting)
2163{
2165 {
2166 man_bytecount_stop(man);
2167#ifdef _WIN32
2168 man_stop_ne32(man);
2169#endif
2176 msg(D_MANAGEMENT, "MANAGEMENT: Client disconnected");
2177 }
2178 if (!exiting)
2179 {
2181 {
2182 msg(D_MANAGEMENT, "MANAGEMENT: Reset authentication on disconnect");
2183 ssl_purge_auth(false);
2184 (void)ssl_clean_auth_token();
2185 }
2186
2187 if (man->settings.flags & MF_SIGNAL && !man_password_needed(man))
2188 {
2189 int mysig = man_mod_signal(man, SIGUSR1);
2190 if (mysig >= 0)
2191 {
2192 msg(D_MANAGEMENT, "MANAGEMENT: Triggering management signal");
2193 throw_signal_soft(mysig, "management-disconnect");
2194 }
2195 }
2196
2198 {
2199 msg(D_MANAGEMENT, "MANAGEMENT: Triggering management exit");
2200 throw_signal_soft(SIGTERM, "management-exit");
2201 }
2202 else
2203 {
2204 man_listen(man);
2205 }
2206 }
2207}
2208
2209static void
2210man_process_command(struct management *man, const char *line)
2211{
2212 struct gc_arena gc = gc_new();
2213 struct status_output *so;
2214 int nparms;
2215 char *parms[MAX_PARMS + 1];
2216
2217 CLEAR(parms);
2218 so = status_open(NULL, 0, -1, &man->persist.vout, 0);
2220
2221 if (man_password_needed(man))
2222 {
2223 man_check_password(man, line);
2224 }
2225 else
2226 {
2227 nparms = parse_line(line, parms, MAX_PARMS, "TCP", 0, M_CLIENT, &gc);
2228 if (parms[0] && streq(parms[0], "password"))
2229 {
2230 msg(D_MANAGEMENT_DEBUG, "MANAGEMENT: CMD 'password [...]'");
2231 }
2232 else if (!streq(line, "load-stats"))
2233 {
2234 msg(D_MANAGEMENT_DEBUG, "MANAGEMENT: CMD '%s'", line);
2235 }
2236
2237#if 0
2238 /* DEBUGGING -- print args */
2239 {
2240 int i;
2241 for (i = 0; i < nparms; ++i)
2242 {
2243 msg(M_INFO, "[%d] '%s'", i, parms[i]);
2244 }
2245 }
2246#endif
2247
2248 if (nparms > 0)
2249 {
2250 man_dispatch_command(man, so, (const char **)parms, nparms);
2251 }
2252 }
2253
2254 CLEAR(parms);
2255 status_close(so);
2256 gc_free(&gc);
2257}
2258
2259static bool
2260man_io_error(struct management *man, const char *prefix)
2261{
2262 bool crt_error = false;
2263 int err = openvpn_errno_maybe_crt(&crt_error);
2264
2265 if (!ignore_sys_error(err, crt_error))
2266 {
2267 struct gc_arena gc = gc_new();
2268 msg(D_MANAGEMENT, "MANAGEMENT: TCP %s error: %s", prefix, strerror(err));
2269 gc_free(&gc);
2270 return true;
2271 }
2272 else
2273 {
2274 return false;
2275 }
2276}
2277
2278#ifdef TARGET_ANDROID
2279static ssize_t
2280man_send_with_fd(int fd, const void *ptr, size_t nbytes, int flags, int sendfd)
2281{
2282 struct msghdr msg = { 0 };
2283 struct iovec iov[1];
2284
2285 union
2286 {
2287 struct cmsghdr cm;
2288 char control[CMSG_SPACE(sizeof(int))];
2289 } control_un;
2290 struct cmsghdr *cmptr;
2291
2292 msg.msg_control = control_un.control;
2293 msg.msg_controllen = sizeof(control_un.control);
2294
2295 cmptr = CMSG_FIRSTHDR(&msg);
2296 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
2297 cmptr->cmsg_level = SOL_SOCKET;
2298 cmptr->cmsg_type = SCM_RIGHTS;
2299 *((int *)CMSG_DATA(cmptr)) = sendfd;
2300
2301 msg.msg_name = NULL;
2302 msg.msg_namelen = 0;
2303
2304 /* sendmsg takes a const msghdr, but we can't construct that here
2305 directly, so cast */
2306 iov[0].iov_base = (void *)ptr;
2307 iov[0].iov_len = nbytes;
2308 msg.msg_iov = iov;
2309 msg.msg_iovlen = 1;
2310
2311 return sendmsg(fd, &msg, flags);
2312}
2313
2314static ssize_t
2315man_recv_with_fd(int fd, void *ptr, size_t nbytes, int flags, int *recvfd)
2316{
2317 struct msghdr msghdr = { 0 };
2318 struct iovec iov[1];
2319 ssize_t n;
2320
2321 union
2322 {
2323 struct cmsghdr cm;
2324 char control[CMSG_SPACE(sizeof(int))];
2325 } control_un;
2326 struct cmsghdr *cmptr;
2327
2328 msghdr.msg_control = control_un.control;
2329 msghdr.msg_controllen = sizeof(control_un.control);
2330
2331 msghdr.msg_name = NULL;
2332 msghdr.msg_namelen = 0;
2333
2334 iov[0].iov_base = ptr;
2335 iov[0].iov_len = nbytes;
2336 msghdr.msg_iov = iov;
2337 msghdr.msg_iovlen = 1;
2338
2339 if ((n = recvmsg(fd, &msghdr, flags)) <= 0)
2340 {
2341 return (n);
2342 }
2343
2344 if ((cmptr = CMSG_FIRSTHDR(&msghdr)) != NULL && cmptr->cmsg_len == CMSG_LEN(sizeof(int)))
2345 {
2346 if (cmptr->cmsg_level != SOL_SOCKET)
2347 {
2348 msg(M_ERR, "control level != SOL_SOCKET");
2349 }
2350 if (cmptr->cmsg_type != SCM_RIGHTS)
2351 {
2352 msg(M_ERR, "control type != SCM_RIGHTS");
2353 }
2354 *recvfd = *((int *)CMSG_DATA(cmptr));
2355 }
2356 else
2357 {
2358 *recvfd = -1; /* descriptor was not passed */
2359 }
2360 return (n);
2361}
2362
2363/*
2364 * The android control method will instruct the GUI part of openvpn to do
2365 * the route/ifconfig/open tun command. See doc/android.txt for details.
2366 */
2367bool
2368management_android_control(struct management *man, const char *command, const char *msg)
2369{
2370 if (!man)
2371 {
2372 msg(M_FATAL, "Required management interface not available.");
2373 }
2374 struct user_pass up;
2375 CLEAR(up);
2376 strncpy(up.username, msg, sizeof(up.username) - 1);
2377
2379 return strcmp("ok", up.password) == 0;
2380}
2381
2382/*
2383 * In Android 4.4 it is not possible to open a new tun device and then close the
2384 * old tun device without breaking the whole VPNService stack until the device
2385 * is rebooted. This management method ask the UI what method should be taken to
2386 * ensure the optimal solution for the situation
2387 */
2388int
2389managment_android_persisttun_action(struct management *man)
2390{
2391 struct user_pass up;
2392 CLEAR(up);
2393 strcpy(up.username, "tunmethod");
2395 (void *)0);
2396 if (!strcmp("NOACTION", up.password))
2397 {
2398 return ANDROID_KEEP_OLD_TUN;
2399 }
2400 else if (!strcmp("OPEN_BEFORE_CLOSE", up.password))
2401 {
2402 return ANDROID_OPEN_BEFORE_CLOSE;
2403 }
2404 else
2405 {
2406 msg(M_ERR, "Got unrecognised '%s' from management for PERSIST_TUN_ACTION query",
2407 up.password);
2408 }
2409
2410 ASSERT(0);
2411 return ANDROID_OPEN_BEFORE_CLOSE;
2412}
2413
2414
2415#endif /* ifdef TARGET_ANDROID */
2416
2417static ssize_t
2419{
2420 /*
2421 * read command line from socket
2422 */
2423 unsigned char buf[256];
2424 ssize_t len = 0;
2425
2426#ifdef TARGET_ANDROID
2427 int fd;
2428 len = man_recv_with_fd(man->connection.sd_cli, buf, sizeof(buf), MSG_NOSIGNAL, &fd);
2429 if (fd >= 0)
2430 {
2431 man->connection.lastfdreceived = fd;
2432 }
2433#else /* ifdef TARGET_ANDROID */
2434 len = recv(man->connection.sd_cli, (void *)buf, sizeof(buf), MSG_NOSIGNAL);
2435#endif
2436
2437 if (len == 0)
2438 {
2439 man_reset_client_socket(man, false);
2440 }
2441 else if (len > 0)
2442 {
2443 bool processed_command = false;
2444
2445 ASSERT(len <= (ssize_t)sizeof(buf));
2446 command_line_add(man->connection.in, buf, (size_t)len);
2447
2448 /*
2449 * Reset output object
2450 */
2452
2453 /*
2454 * process command line if complete
2455 */
2456 {
2457 const char *line;
2458 while ((line = command_line_get(man->connection.in)))
2459 {
2460 if (man->connection.in_extra)
2461 {
2462 if (!strcmp(line, "END"))
2463 {
2464 in_extra_dispatch(man);
2465 }
2466 else
2467 {
2469 }
2470 }
2471 else
2472 {
2473 man_process_command(man, (char *)line);
2474 }
2475 if (man->connection.halt)
2476 {
2477 break;
2478 }
2480 processed_command = true;
2481 }
2482 }
2483
2484 /*
2485 * Reset output state to MS_CC_WAIT_(READ|WRITE)
2486 */
2487 if (man->connection.halt)
2488 {
2489 man_reset_client_socket(man, false);
2490 len = 0;
2491 }
2492 else
2493 {
2494 if (processed_command)
2495 {
2496 man_prompt(man);
2497 }
2499 }
2500 }
2501 else /* len < 0 */
2502 {
2503 if (man_io_error(man, "recv"))
2504 {
2505 man_reset_client_socket(man, false);
2506 }
2507 }
2508 return len;
2509}
2510
2511static ssize_t
2513{
2514 const int size_hint = 1024;
2515 ssize_t sent = 0;
2516
2517 buffer_list_aggregate(man->connection.out, size_hint);
2518 const struct buffer *buf = buffer_list_peek(man->connection.out);
2519 if (buf && BLEN(buf))
2520 {
2521 const int len = min_int(size_hint, BLEN(buf));
2522#ifdef TARGET_ANDROID
2523 if (man->connection.fdtosend > 0)
2524 {
2525 sent = man_send_with_fd(man->connection.sd_cli, CBPTR(buf), len, MSG_NOSIGNAL,
2526 man->connection.fdtosend);
2527 man->connection.fdtosend = -1;
2528 }
2529 else
2530#endif
2531 {
2532 sent = send(man->connection.sd_cli, CBSTR(buf), len, MSG_NOSIGNAL);
2533 }
2534 if (sent >= 0)
2535 {
2536 buffer_list_advance(man->connection.out, sent);
2537 }
2538 else
2539 {
2540 if (man_io_error(man, "send"))
2541 {
2543 }
2544 }
2545 }
2546
2547 /*
2548 * Reset output state to MS_CC_WAIT_(READ|WRITE)
2549 */
2551
2552 return sent;
2553}
2554
2555static void
2557{
2558 CLEAR(*mc);
2559
2560 /* set initial state */
2561 mc->state = MS_INITIAL;
2562
2563 /* clear socket descriptors */
2564 mc->sd_top = SOCKET_UNDEFINED;
2565 mc->sd_cli = SOCKET_UNDEFINED;
2566}
2567
2568static void
2569man_persist_init(struct management *man, const int log_history_cache, const int echo_buffer_size,
2570 const int state_buffer_size)
2571{
2572 struct man_persist *mp = &man->persist;
2573 if (!mp->defined)
2574 {
2575 CLEAR(*mp);
2576
2577 /* initialize log history store */
2578 mp->log = log_history_init(log_history_cache);
2579
2580 /*
2581 * Initialize virtual output object, so that functions
2582 * which write to a virtual_output object can be redirected
2583 * here to the management object.
2584 */
2586 mp->vout.arg = man;
2589
2590 /*
2591 * Initialize --echo list
2592 */
2593 man->persist.echo = log_history_init(echo_buffer_size);
2594
2595 /*
2596 * Initialize --state list
2597 */
2598 man->persist.state = log_history_init(state_buffer_size);
2599
2600 mp->defined = true;
2601 }
2602}
2603
2604static void
2606{
2607 if (mp->log)
2608 {
2611 }
2612
2613 if (mp->echo)
2614 {
2616 }
2617
2618 if (mp->state)
2619 {
2621 }
2622
2623 CLEAR(*mp);
2624}
2625
2626static void
2627man_settings_init(struct man_settings *ms, const char *addr, const char *port,
2628 const char *pass_file, const char *client_user, const char *client_group,
2629 const int log_history_cache, const int echo_buffer_size,
2630 const int state_buffer_size, const int remap_sigusr1, const unsigned int flags)
2631{
2632 if (!ms->defined)
2633 {
2634 CLEAR(*ms);
2635
2636 ms->flags = flags;
2637
2638 /*
2639 * Get username/password
2640 */
2641 if (pass_file)
2642 {
2643 get_user_pass(&ms->up, pass_file, "Management", GET_USER_PASS_PASSWORD_ONLY);
2644 }
2645
2646#if UNIX_SOCK_SUPPORT
2647 /*
2648 * lookup client UID/GID if specified
2649 */
2650 if (client_user)
2651 {
2652 ASSERT(platform_user_get(client_user, &ms->user));
2653 msg(D_MANAGEMENT, "MANAGEMENT: client_uid=%d", ms->user.uid);
2654 }
2655 if (client_group)
2656 {
2657 ASSERT(platform_group_get(client_group, &ms->group));
2658 msg(D_MANAGEMENT, "MANAGEMENT: client_gid=%d", ms->group.gid);
2659 }
2660
2661 if (ms->flags & MF_UNIX_SOCK)
2662 {
2663 sockaddr_unix_init(&ms->local_unix, addr);
2664 }
2665 else
2666#endif
2667 {
2668 /*
2669 * Run management over tunnel, or
2670 * separate channel?
2671 */
2672 if (streq(addr, "tunnel") && !(flags & MF_CONNECT_AS_CLIENT))
2673 {
2674 ms->management_over_tunnel = true;
2675 }
2676 else
2677 {
2678 int status;
2680
2681 if (!(flags & MF_CONNECT_AS_CLIENT))
2682 {
2683 resolve_flags |= GETADDR_PASSIVE;
2684 }
2685
2686 status =
2687 openvpn_getaddrinfo(resolve_flags, addr, port, 0, NULL, AF_UNSPEC, &ms->local);
2688 ASSERT(status == 0);
2689 }
2690 }
2691
2692 /*
2693 * Log history and echo buffer may need to be resized
2694 */
2695 ms->log_history_cache = log_history_cache;
2696 ms->echo_buffer_size = echo_buffer_size;
2697 ms->state_buffer_size = state_buffer_size;
2698
2699 /*
2700 * Set remap sigusr1 flags
2701 */
2702 if (remap_sigusr1 == SIGHUP)
2703 {
2705 }
2706 else if (remap_sigusr1 == SIGTERM)
2707 {
2709 }
2710
2711 ms->defined = true;
2712 }
2713}
2714
2715static void
2717{
2718 if (ms->local)
2719 {
2720 freeaddrinfo(ms->local);
2721 }
2722 CLEAR(*ms);
2723}
2724
2725
2726static void
2728{
2729 if (man->connection.state == MS_INITIAL)
2730 {
2731#ifdef _WIN32
2732 /*
2733 * This object is a sort of TCP/IP helper
2734 * for Windows.
2735 */
2737#endif
2738
2739 /*
2740 * Allocate helper objects for command line input and
2741 * command output from/to the socket.
2742 */
2743 man->connection.in = command_line_new(1024);
2745
2746 /*
2747 * Initialize event set for standalone usage, when we are
2748 * running outside of the primary event loop.
2749 */
2750 {
2751 int maxevents = 1;
2752 man->connection.es = event_set_init(&maxevents, EVENT_METHOD_FAST);
2753 }
2754
2755 man->connection.client_version = MCV_DEFAULT; /* default version */
2756
2757 /*
2758 * Listen/connect socket
2759 */
2761 {
2762 man_connect(man);
2763 }
2764 else
2765 {
2766 man_listen(man);
2767 }
2768 }
2769}
2770
2771static void
2773{
2774 struct man_connection *mc = &man->connection;
2775
2776 event_free(mc->es);
2777#ifdef _WIN32
2779#endif
2780 if (socket_defined(mc->sd_top))
2781 {
2782 man_close_socket(man, mc->sd_top);
2784 }
2785 if (socket_defined(mc->sd_cli))
2786 {
2787 man_close_socket(man, mc->sd_cli);
2788 }
2789
2790 command_line_free(mc->in);
2791 buffer_list_free(mc->out);
2792
2794
2798}
2799
2800struct management *
2802{
2803 struct management *man;
2804 ALLOC_OBJ_CLEAR(man, struct management);
2805
2808
2810
2811 return man;
2812}
2813
2814bool
2815management_open(struct management *man, const char *addr, const char *port, const char *pass_file,
2816 const char *client_user, const char *client_group, const int log_history_cache,
2817 const int echo_buffer_size, const int state_buffer_size, const int remap_sigusr1,
2818 const unsigned int flags)
2819{
2820 bool ret = false;
2821
2822 /*
2823 * Save the settings only if they have not
2824 * been saved before.
2825 */
2826 man_settings_init(&man->settings, addr, port, pass_file, client_user, client_group,
2827 log_history_cache, echo_buffer_size, state_buffer_size, remap_sigusr1, flags);
2828
2829 /*
2830 * The log is initially sized to MANAGEMENT_LOG_HISTORY_INITIAL_SIZE,
2831 * but may be changed here. Ditto for echo and state buffers.
2832 */
2836
2837 /*
2838 * If connection object is uninitialized and we are not doing
2839 * over-the-tunnel management, then open (listening) connection.
2840 */
2841 if (man->connection.state == MS_INITIAL)
2842 {
2844 {
2846 ret = true;
2847 }
2848 }
2849
2850 return ret;
2851}
2852
2853void
2855{
2856 man_output_list_push_finalize(man); /* flush output queue */
2860 free(man);
2861}
2862
2863void
2865{
2866 man->persist.standalone_disabled = true;
2867 man->persist.callback = *cb;
2868}
2869
2870void
2872{
2873 man->persist.standalone_disabled = false;
2874 man->persist.hold_release = false;
2875 CLEAR(man->persist.callback);
2876 man_output_list_push_finalize(man); /* flush output queue */
2877}
2878
2879void
2880management_set_state(struct management *man, const int state, const char *detail,
2881 const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6,
2882 const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
2883{
2884 if (man->persist.state
2885 && (!(man->settings.flags & MF_SERVER) || state < OPENVPN_STATE_CLIENT_BASE))
2886 {
2887 struct gc_arena gc = gc_new();
2888 struct log_entry e;
2889 const char *out = NULL;
2890
2891 update_time();
2892 CLEAR(e);
2893 e.timestamp = now;
2894 e.u.state = state;
2895 e.string = detail;
2896 if (tun_local_ip)
2897 {
2898 e.local_ip = *tun_local_ip;
2899 }
2900 if (tun_local_ip6)
2901 {
2902 e.local_ip6 = *tun_local_ip6;
2903 }
2904 if (local)
2905 {
2906 e.local_sock = *local;
2907 }
2908 if (remote)
2909 {
2910 e.remote_sock = *remote;
2911 }
2912
2913 log_history_add(man->persist.state, &e);
2914
2915 if (man->connection.state_realtime)
2916 {
2917 out = log_entry_print(&e,
2921 &gc);
2922 }
2923
2924 if (out)
2925 {
2926 man_output_list_push(man, out);
2927 }
2928
2929 gc_free(&gc);
2930 }
2931}
2932
2933static bool
2934env_filter_match(const char *env_str, const int env_filter_level)
2935{
2936 static const char *env_names[] = { "username=",
2937 "password=",
2938 "X509_0_CN=",
2939 "tls_serial_",
2940 "untrusted_ip=",
2941 "ifconfig_local=",
2942 "ifconfig_netmask=",
2943 "daemon_start_time=",
2944 "daemon_pid=",
2945 "dev=",
2946 "ifconfig_pool_remote_ip=",
2947 "ifconfig_pool_netmask=",
2948 "time_duration=",
2949 "bytes_sent=",
2950 "bytes_received=",
2951 "session_id=",
2952 "session_state=" };
2953
2954 if (env_filter_level == 0)
2955 {
2956 return true;
2957 }
2958 else if (env_filter_level <= 1 && !strncmp(env_str, "X509_", 5))
2959 {
2960 return true;
2961 }
2962 else if (env_filter_level <= 2)
2963 {
2964 size_t i;
2965 for (i = 0; i < SIZE(env_names); ++i)
2966 {
2967 const char *en = env_names[i];
2968 const size_t len = strlen(en);
2969 if (!strncmp(env_str, en, len))
2970 {
2971 return true;
2972 }
2973 }
2974 return false;
2975 }
2976 return false;
2977}
2978
2979static void
2980man_output_env(const struct env_set *es, const bool tail, const int env_filter_level,
2981 const char *prefix)
2982{
2983 if (es)
2984 {
2985 struct env_item *e;
2986 for (e = es->list; e != NULL; e = e->next)
2987 {
2988 if (e->string && (!env_filter_level || env_filter_match(e->string, env_filter_level)))
2989 {
2990 msg(M_CLIENT, ">%s:ENV,%s", prefix, e->string);
2991 }
2992 }
2993 }
2994 if (tail)
2995 {
2996 msg(M_CLIENT, ">%s:ENV,END", prefix);
2997 }
2998}
2999
3000static void
3001man_output_extra_env(struct management *man, const char *prefix)
3002{
3003 struct gc_arena gc = gc_new();
3004 struct env_set *es = env_set_create(&gc);
3005 if (man->persist.callback.n_clients)
3006 {
3007 const int nclients = (*man->persist.callback.n_clients)(man->persist.callback.arg);
3008 setenv_int(es, "n_clients", nclients);
3009 }
3010 man_output_env(es, false, man->connection.env_filter_level, prefix);
3011 gc_free(&gc);
3012}
3013
3014void
3015management_up_down(struct management *man, const char *updown, const struct env_set *es)
3016{
3017 if (man->settings.flags & MF_UP_DOWN)
3018 {
3019 msg(M_CLIENT, ">UPDOWN:%s", updown);
3020 man_output_env(es, true, 0, "UPDOWN");
3021 }
3022}
3023
3024void
3025management_notify(const char *severity, const char *type, const char *text)
3026{
3027 msg(M_CLIENT, ">NOTIFY:%s,%s,%s", severity, type, text);
3028}
3029
3030void
3032{
3033 msg(M_CLIENT, "%s", str);
3034}
3035
3036static void
3038{
3039 char line[256];
3041 {
3042 const char *peer_info =
3043 (*man->persist.callback.get_peer_info)(man->persist.callback.arg, mdac->cid);
3044 if (peer_info)
3045 {
3046 struct buffer buf;
3047 buf_set_read(&buf, (const uint8_t *)peer_info, strlen(peer_info));
3048 while (buf_parse(&buf, '\n', line, sizeof(line)))
3049 {
3050 chomp(line);
3052 {
3053 msg(M_CLIENT, ">CLIENT:ENV,%s", line);
3054 }
3055 else
3056 {
3057 msg(D_MANAGEMENT, "validation failed on peer_info line received from client");
3058 }
3059 }
3060 }
3061 }
3062}
3063
3064void
3065management_notify_client_needing_auth(struct management *man, const unsigned int mda_key_id,
3066 struct man_def_auth_context *mdac, const struct env_set *es)
3067{
3068 if (!(mdac->flags & DAF_CONNECTION_CLOSED))
3069 {
3070 const char *mode = "CONNECT";
3071 if (mdac->flags & DAF_CONNECTION_ESTABLISHED)
3072 {
3073 mode = "REAUTH";
3074 }
3075 msg(M_CLIENT, ">CLIENT:%s,%lu,%u", mode, mdac->cid, mda_key_id);
3076 man_output_extra_env(man, "CLIENT");
3077 if (man->connection.env_filter_level > 0)
3078 {
3080 }
3081 man_output_env(es, true, man->connection.env_filter_level, "CLIENT");
3082 mdac->flags |= DAF_INITIAL_AUTH;
3083 }
3084}
3085
3086void
3088 const struct env_set *es, const char *response)
3089{
3090 struct gc_arena gc;
3091 if (management)
3092 {
3093 gc = gc_new();
3094
3095 msg(M_CLIENT, ">CLIENT:CR_RESPONSE,%lu,%u,%s", mdac->cid, mda_key_id, response);
3098 {
3100 }
3102 gc_free(&gc);
3103 }
3104}
3105
3106void
3108 const struct env_set *es)
3109{
3111 msg(M_CLIENT, ">CLIENT:ESTABLISHED,%lu", mdac->cid);
3112 man_output_extra_env(man, "CLIENT");
3113 man_output_env(es, true, man->connection.env_filter_level, "CLIENT");
3114}
3115
3116void
3118 struct man_def_auth_context *mdac,
3119 const struct env_set *es)
3120{
3121 if ((mdac->flags & DAF_INITIAL_AUTH) && !(mdac->flags & DAF_CONNECTION_CLOSED))
3122 {
3123 msg(M_CLIENT, ">CLIENT:DISCONNECT,%lu", mdac->cid);
3124 man_output_env(es, true, man->connection.env_filter_level, "CLIENT");
3126 }
3127}
3128
3129void
3131 const struct mroute_addr *addr, const bool primary)
3132{
3133 struct gc_arena gc = gc_new();
3134 if ((mdac->flags & DAF_INITIAL_AUTH) && !(mdac->flags & DAF_CONNECTION_CLOSED))
3135 {
3136 msg(M_CLIENT, ">CLIENT:ADDRESS,%lu,%s,%d", mdac->cid,
3137 mroute_addr_print_ex(addr, MAPF_SUBNET, &gc), BOOL_CAST(primary));
3138 }
3139 gc_free(&gc);
3140}
3141
3142void
3143management_echo(struct management *man, const char *string, const bool pull)
3144{
3145 if (man->persist.echo)
3146 {
3147 struct gc_arena gc = gc_new();
3148 struct log_entry e;
3149 const char *out = NULL;
3150
3151 update_time();
3152 CLEAR(e);
3153 e.timestamp = now;
3154 e.string = string;
3155 e.u.intval = BOOL_CAST(pull);
3156
3157 log_history_add(man->persist.echo, &e);
3158
3159 if (man->connection.echo_realtime)
3160 {
3161 out = log_entry_print(&e,
3164 &gc);
3165 }
3166
3167 if (out)
3168 {
3169 man_output_list_push(man, out);
3170 }
3171
3172 gc_free(&gc);
3173 }
3174}
3175
3176void
3177management_post_tunnel_open(struct management *man, const in_addr_t tun_local_ip)
3178{
3179 /*
3180 * If we are running management over the tunnel,
3181 * this is the place to initialize the connection.
3182 */
3184 {
3185 /* listen on our local TUN/TAP IP address */
3186 struct in_addr ia;
3187 int ret;
3188 char buf[INET_ADDRSTRLEN];
3189
3190 ia.s_addr = htonl(tun_local_ip);
3191 inet_ntop(AF_INET, &ia, buf, sizeof(buf));
3192 ret =
3193 openvpn_getaddrinfo(GETADDR_PASSIVE, buf, NULL, 0, NULL, AF_INET, &man->settings.local);
3194 ASSERT(ret == 0);
3196 }
3197}
3198
3199void
3201{
3203 {
3205 }
3206}
3207
3208void
3209management_auth_failure(struct management *man, const char *type, const char *reason)
3210{
3211 if (reason)
3212 {
3213 msg(M_CLIENT, ">PASSWORD:Verification Failed: '%s' ['%s']", type, reason);
3214 }
3215 else
3216 {
3217 msg(M_CLIENT, ">PASSWORD:Verification Failed: '%s'", type);
3218 }
3219}
3220
3221void
3222management_auth_token(const char *token)
3223{
3224 msg(M_CLIENT, ">PASSWORD:Auth-Token:%s", token);
3225}
3226
3227static inline bool
3228man_persist_state(unsigned int *persistent, const int n)
3229{
3230 if (persistent)
3231 {
3232 if (*persistent == (unsigned int)n)
3233 {
3234 return false;
3235 }
3236 *persistent = n;
3237 }
3238 return true;
3239}
3240
3241#ifdef _WIN32
3242
3243void
3244management_socket_set(struct management *man, struct event_set *es, void *arg,
3245 unsigned int *persistent)
3246{
3247 if (man->connection.state != MS_INITIAL)
3248 {
3251
3252 switch (man->connection.state)
3253 {
3254 case MS_LISTEN:
3255 if (man_persist_state(persistent, 1))
3256 {
3257 event_ctl(es, ev, EVENT_READ, arg);
3258 }
3259 break;
3260
3261 case MS_CC_WAIT_READ:
3262 if (man_persist_state(persistent, 2))
3263 {
3264 event_ctl(es, ev, EVENT_READ, arg);
3265 }
3266 break;
3267
3268 case MS_CC_WAIT_WRITE:
3269 if (man_persist_state(persistent, 3))
3270 {
3271 event_ctl(es, ev, EVENT_READ | EVENT_WRITE, arg);
3272 }
3273 break;
3274
3275 default:
3276 ASSERT(0);
3277 }
3278 }
3279}
3280
3281void
3283{
3284 if (man->connection.state != MS_INITIAL)
3285 {
3286 long net_events;
3289
3290 if (net_events & FD_CLOSE)
3291 {
3292 man_reset_client_socket(man, false);
3293 }
3294 else
3295 {
3296 if (man->connection.state == MS_LISTEN)
3297 {
3298 if (net_events & FD_ACCEPT)
3299 {
3300 man_accept(man);
3302 }
3303 }
3304 else if (man->connection.state == MS_CC_WAIT_READ
3306 {
3307 if (net_events & FD_READ)
3308 {
3309 while (man_read(man) > 0)
3310 {
3311 }
3313 }
3314
3315 if (net_events & FD_WRITE)
3316 {
3317 ssize_t status = man_write(man);
3318 if (status < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
3319 {
3321 }
3322 }
3323 }
3324 }
3325 }
3326}
3327
3328#else /* ifdef _WIN32 */
3329
3330void
3331management_socket_set(struct management *man, struct event_set *es, void *arg,
3332 unsigned int *persistent)
3333{
3334 switch (man->connection.state)
3335 {
3336 case MS_LISTEN:
3337 if (man_persist_state(persistent, 1))
3338 {
3339 event_ctl(es, man->connection.sd_top, EVENT_READ, arg);
3340 }
3341 break;
3342
3343 case MS_CC_WAIT_READ:
3344 if (man_persist_state(persistent, 2))
3345 {
3346 event_ctl(es, man->connection.sd_cli, EVENT_READ, arg);
3347 }
3348 break;
3349
3350 case MS_CC_WAIT_WRITE:
3351 if (man_persist_state(persistent, 3))
3352 {
3353 event_ctl(es, man->connection.sd_cli, EVENT_WRITE, arg);
3354 }
3355 break;
3356
3357 case MS_INITIAL:
3358 break;
3359
3360 default:
3361 ASSERT(0);
3362 }
3363}
3364
3365void
3366management_io(struct management *man)
3367{
3368 switch (man->connection.state)
3369 {
3370 case MS_LISTEN:
3371 man_accept(man);
3372 break;
3373
3374 case MS_CC_WAIT_READ:
3375 man_read(man);
3376 break;
3377
3378 case MS_CC_WAIT_WRITE:
3379 man_write(man);
3380 break;
3381
3382 case MS_INITIAL:
3383 break;
3384
3385 default:
3386 ASSERT(0);
3387 }
3388}
3389
3390#endif /* ifdef _WIN32 */
3391
3392static inline bool
3394{
3396}
3397
3398static bool
3399man_check_for_signals(volatile int *signal_received)
3400{
3401 if (signal_received)
3402 {
3403 get_signal(signal_received);
3404 if (*signal_received)
3405 {
3406 return true;
3407 }
3408 }
3409 return false;
3410}
3411
3412/*
3413 * Wait for socket I/O when outside primary event loop
3414 */
3415static int
3416man_block(struct management *man, volatile int *signal_received, const time_t expire)
3417{
3418 struct timeval tv;
3419 struct event_set_return esr;
3420 int status = -1;
3421
3422 if (man_standalone_ok(man))
3423 {
3424 /* expire time can be already overdue, for this case init zero
3425 * timeout to avoid waiting first time and exit loop early with
3426 * either obtained event or timeout.
3427 */
3428 tv.tv_usec = 0;
3429 tv.tv_sec = 0;
3430
3431 while (true)
3432 {
3434 management_socket_set(man, man->connection.es, NULL, NULL);
3435 if (man_check_for_signals(signal_received))
3436 {
3437 status = -1;
3438 break;
3439 }
3440 status = event_wait(man->connection.es, &tv, &esr, 1);
3441 update_time();
3442 if (man_check_for_signals(signal_received))
3443 {
3444 status = -1;
3445 break;
3446 }
3447
3448 if (status > 0)
3449 {
3450 break;
3451 }
3452 else if (expire && now >= expire)
3453 {
3454 /* set SIGINT signal if expiration time exceeded */
3455 status = 0;
3456 if (signal_received)
3457 {
3458 *signal_received = SIGINT;
3459 }
3460 break;
3461 }
3462
3463 /* wait one second more */
3464 tv.tv_sec = 1;
3465 tv.tv_usec = 0;
3466 }
3467 }
3468 return status;
3469}
3470
3471/*
3472 * Perform management socket output outside primary event loop
3473 */
3474static void
3475man_output_standalone(struct management *man, volatile int *signal_received)
3476{
3477 if (man_standalone_ok(man))
3478 {
3479 while (man->connection.state == MS_CC_WAIT_WRITE)
3480 {
3481 management_io(man);
3482 if (man->connection.state == MS_CC_WAIT_WRITE)
3483 {
3484 man_block(man, signal_received, 0);
3485 }
3486 if (signal_received && *signal_received)
3487 {
3488 break;
3489 }
3490 }
3491 }
3492}
3493
3494/*
3495 * Process management event loop outside primary event loop
3496 */
3497static int
3498man_standalone_event_loop(struct management *man, volatile int *signal_received,
3499 const time_t expire)
3500{
3501 int status = -1;
3502 if (man_standalone_ok(man))
3503 {
3504 status = man_block(man, signal_received, expire);
3505 if (status > 0)
3506 {
3507 management_io(man);
3508 }
3509 }
3510 return status;
3511}
3512
3513#define MWCC_PASSWORD_WAIT (1 << 0)
3514#define MWCC_HOLD_WAIT (1 << 1)
3515#define MWCC_OTHER_WAIT (1 << 2)
3516
3517/*
3518 * Block until client connects
3519 */
3520static void
3521man_wait_for_client_connection(struct management *man, volatile int *signal_received,
3522 const time_t expire, unsigned int flags)
3523{
3525 if (man->connection.state == MS_LISTEN)
3526 {
3527 if (flags & MWCC_PASSWORD_WAIT)
3528 {
3529 msg(D_MANAGEMENT, "Need password(s) from management interface, waiting...");
3530 }
3531 if (flags & MWCC_HOLD_WAIT)
3532 {
3533 msg(D_MANAGEMENT, "Need hold release from management interface, waiting...");
3534 }
3535 if (flags & MWCC_OTHER_WAIT)
3536 {
3537 msg(D_MANAGEMENT, "Need information from management interface, waiting...");
3538 }
3539 do
3540 {
3541 man_standalone_event_loop(man, signal_received, expire);
3542 if (signal_received && *signal_received)
3543 {
3544 break;
3545 }
3546 } while (man->connection.state == MS_LISTEN || man_password_needed(man));
3547 }
3548}
3549
3550/*
3551 * Process the management event loop for sec seconds
3552 */
3553void
3555{
3556 if (man_standalone_ok(man))
3557 {
3558 volatile int signal_received = 0;
3559 const bool standalone_disabled_save = man->persist.standalone_disabled;
3560 time_t expire = 0;
3561
3562 /* This is so M_CLIENT messages will be correctly passed through msg() */
3563 man->persist.standalone_disabled = false;
3564
3565 /* set expire time */
3566 update_time();
3567 if (sec >= 0)
3568 {
3569 expire = now + sec;
3570 }
3571
3572 /* if no client connection, wait for one */
3573 man_wait_for_client_connection(man, &signal_received, expire, 0);
3574 if (signal_received)
3575 {
3576 return;
3577 }
3578
3579 /* run command processing event loop */
3580 do
3581 {
3582 man_standalone_event_loop(man, &signal_received, expire);
3583 if (!signal_received)
3584 {
3585 man_check_for_signals(&signal_received);
3586 }
3587 if (signal_received)
3588 {
3589 return;
3590 }
3591 update_time();
3592 } while (expire && expire > now);
3593
3594 /* revert state */
3595 man->persist.standalone_disabled = standalone_disabled_save;
3596 }
3597 else if (sec > 0)
3598 {
3599 sleep(sec);
3600 }
3601}
3602
3603/*
3604 * Get a username/password from management channel in standalone mode.
3605 */
3606bool
3607management_query_user_pass(struct management *man, struct user_pass *up, const char *type,
3608 const unsigned int flags, const char *static_challenge)
3609{
3610 struct gc_arena gc = gc_new();
3611 bool ret = false;
3612
3613 if (man_standalone_ok(man))
3614 {
3615 volatile int signal_received = 0;
3616 const bool standalone_disabled_save = man->persist.standalone_disabled;
3617 struct buffer alert_msg = alloc_buf_gc(128, &gc);
3618 const char *alert_type = NULL;
3619 const char *prefix = NULL;
3620 unsigned int up_query_mode = 0;
3621 const char *sc = NULL;
3622 ret = true;
3623 /* This is so M_CLIENT messages will be correctly passed through msg() */
3624 man->persist.standalone_disabled = false;
3625 man->persist.special_state_msg = NULL;
3626
3627 CLEAR(man->connection.up_query);
3628
3629 if (flags & GET_USER_PASS_NEED_OK)
3630 {
3631 up_query_mode = UP_QUERY_NEED_OK;
3632 prefix = "NEED-OK";
3633 alert_type = "confirmation";
3634 }
3635 else if (flags & GET_USER_PASS_NEED_STR)
3636 {
3637 up_query_mode = UP_QUERY_NEED_STR;
3638 prefix = "NEED-STR";
3639 alert_type = "string";
3640 }
3641 else if (flags & GET_USER_PASS_PASSWORD_ONLY)
3642 {
3643 up_query_mode = UP_QUERY_PASS;
3644 prefix = "PASSWORD";
3645 alert_type = "password";
3646 }
3647 else if ((man->connection.client_version >= MCV_USERNAME_ONLY) && (flags & GET_USER_PASS_USERNAME_ONLY))
3648 {
3649 up_query_mode = UP_QUERY_USERNAME;
3650 prefix = "PASSWORD";
3651 alert_type = "username";
3652 }
3653 else
3654 {
3655 up_query_mode = UP_QUERY_USER_PASS;
3656 prefix = "PASSWORD";
3657 alert_type = "username/password";
3658 if (static_challenge)
3659 {
3661 }
3662 }
3663 buf_printf(&alert_msg, ">%s:Need '%s' %s", prefix, type, alert_type);
3664
3666 {
3667 buf_printf(&alert_msg, " MSG:%s", up->username);
3668 }
3669
3670 if (sc)
3671 {
3672 buf_printf(&alert_msg, " SC:%d,%s",
3675 sc);
3676 }
3677
3679 if (signal_received)
3680 {
3681 ret = false;
3682 }
3683
3684 if (ret)
3685 {
3686 man->persist.special_state_msg = BSTR(&alert_msg);
3687 msg(M_CLIENT, "%s", man->persist.special_state_msg);
3688
3689 /* tell command line parser which info we need */
3690 man->connection.up_query_mode = up_query_mode;
3691 man->connection.up_query_type = type;
3692
3693 /* run command processing event loop until we get our username/password/response */
3694 do
3695 {
3696 man_standalone_event_loop(man, &signal_received, 0);
3697 if (!signal_received)
3698 {
3699 man_check_for_signals(&signal_received);
3700 }
3701 if (signal_received)
3702 {
3703 ret = false;
3704 break;
3705 }
3706 } while (!man->connection.up_query.defined);
3707 }
3708
3709 /* revert state */
3710 man->connection.up_query_mode = UP_QUERY_DISABLED;
3711 man->connection.up_query_type = NULL;
3712 man->persist.standalone_disabled = standalone_disabled_save;
3713 man->persist.special_state_msg = NULL;
3714
3715 /* pass through blank passwords */
3716 if (!strcmp(man->connection.up_query.password, blank_up))
3717 {
3718 CLEAR(man->connection.up_query.password);
3719 }
3720
3721 /*
3722 * Transfer u/p to return object, zero any record
3723 * we hold in the management object.
3724 */
3725 if (ret)
3726 {
3727 /* preserve caller's settings */
3728 man->connection.up_query.nocache = up->nocache;
3729 *up = man->connection.up_query;
3730 }
3731 secure_memzero(&man->connection.up_query, sizeof(man->connection.up_query));
3732 }
3733
3734 gc_free(&gc);
3735 return ret;
3736}
3737
3738static int
3739management_query_multiline(struct management *man, const char *b64_data, const char *prompt,
3740 const char *cmd, int *state, struct buffer_list **input)
3741{
3742 struct gc_arena gc = gc_new();
3743 int ret = 0;
3744 volatile int signal_received = 0;
3745 const bool standalone_disabled_save = man->persist.standalone_disabled;
3746 struct man_connection *mc = &man->connection;
3747
3748 if (man_standalone_ok(man))
3749 {
3750 /* This is so M_CLIENT messages will be correctly passed through msg() */
3751 man->persist.standalone_disabled = false;
3752 man->persist.special_state_msg = NULL;
3753
3754 *state = EKS_SOLICIT;
3755
3756 struct buffer alert_msg;
3757 if (b64_data)
3758 {
3759 alert_msg = alloc_buf_gc(strlen(b64_data) + strlen(prompt) + 3, &gc);
3760 buf_printf(&alert_msg, ">%s:%s", prompt, b64_data);
3761 }
3762 else
3763 {
3764 alert_msg = alloc_buf_gc(strlen(prompt) + 3, &gc);
3765 buf_printf(&alert_msg, ">%s", prompt);
3766 }
3767
3768 man_wait_for_client_connection(man, &signal_received, 0, MWCC_OTHER_WAIT);
3769
3770 if (signal_received)
3771 {
3772 goto done;
3773 }
3774
3775 man->persist.special_state_msg = BSTR(&alert_msg);
3776 msg(M_CLIENT, "%s", man->persist.special_state_msg);
3777
3778 /* run command processing event loop until we get our signature */
3779 do
3780 {
3781 man_standalone_event_loop(man, &signal_received, 0);
3782 if (!signal_received)
3783 {
3784 man_check_for_signals(&signal_received);
3785 }
3786 if (signal_received)
3787 {
3788 goto done;
3789 }
3790 } while (*state != EKS_READY);
3791
3792 ret = 1;
3793 }
3794
3795done:
3796 if (*state == EKS_READY && ret)
3797 {
3798 msg(M_CLIENT, "SUCCESS: %s command succeeded", cmd);
3799 }
3800 else if (*state == EKS_INPUT || *state == EKS_READY)
3801 {
3802 msg(M_CLIENT, "ERROR: %s command failed", cmd);
3803 }
3804
3805 /* revert state */
3806 man->persist.standalone_disabled = standalone_disabled_save;
3807 man->persist.special_state_msg = NULL;
3809 *state = EKS_UNDEF;
3810
3811 gc_free(&gc);
3812 return ret;
3813}
3814
3815static char *
3816/* returns allocated base64 signature */
3818 const char *prompt, const char *cmd, int *state,
3819 struct buffer_list **input)
3820{
3821 int ok;
3822 char *result = NULL;
3823 const struct buffer *buf;
3824
3825 ok = management_query_multiline(man, b64_data, prompt, cmd, state, input);
3826 if (ok && buffer_list_defined(*input))
3827 {
3829 buf = buffer_list_peek(*input);
3830 if (buf && BLEN(buf) > 0)
3831 {
3832 result = (char *)malloc(BLENZ(buf) + 1);
3834 memcpy(result, buf->data, BLENZ(buf));
3835 result[BLEN(buf)] = '\0';
3836 }
3837 }
3838
3840 *input = NULL;
3841
3842 return result;
3843}
3844
3845static char *
3846/* returns allocated base64 signature */
3847management_query_multiline_flatten(struct management *man, const char *b64_data, const char *prompt,
3848 const char *cmd, int *state, struct buffer_list **input)
3849{
3850 int ok;
3851 char *result = NULL;
3852 const struct buffer *buf;
3853
3854 ok = management_query_multiline(man, b64_data, prompt, cmd, state, input);
3855 if (ok && buffer_list_defined(*input))
3856 {
3858 buf = buffer_list_peek(*input);
3859 if (buf && BLEN(buf) > 0)
3860 {
3861 result = (char *)malloc(BLENZ(buf) + 1);
3863 memcpy(result, buf->data, BLENZ(buf));
3864 result[BLEN(buf)] = '\0';
3865 }
3866 }
3867
3869 *input = NULL;
3870
3871 return result;
3872}
3873
3874char *
3875/* returns allocated base64 signature */
3876management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
3877{
3878 const char *prompt = "PK_SIGN";
3879 const char *desc = "pk-sign";
3881
3882 if (man->connection.client_version <= MCV_DEFAULT)
3883 {
3884 prompt = "RSA_SIGN";
3885 desc = "rsa-sign";
3886 }
3887
3889 if (man->connection.client_version >= MCV_PKSIGN_ALG)
3890 {
3891 buf_write(&buf_data, ",", (int)strlen(","));
3893 }
3894 char *ret = management_query_multiline_flatten(man, (char *)buf_bptr(&buf_data), prompt, desc,
3895 &man->connection.ext_key_state,
3896 &man->connection.ext_key_input);
3898 return ret;
3899}
3900
3901char *
3903{
3904 const char prompt_1[] = "NEED-CERTIFICATE:";
3905 struct buffer buf_prompt = alloc_buf(strlen(cert_name) + 20);
3907 buf_write(&buf_prompt, cert_name, strlen(cert_name) + 1); /* +1 for \0 */
3908
3909 char *result;
3911 management, NULL, (char *)buf_bptr(&buf_prompt), "certificate",
3912 &man->connection.ext_cert_state, &man->connection.ext_cert_input);
3914 return result;
3915}
3916
3917/*
3918 * Return true if management_hold() would block
3919 */
3920bool
3922{
3923 return (man->settings.flags & MF_HOLD) && !man->persist.hold_release && man_standalone_ok(man);
3924}
3925
3926/*
3927 * If the hold flag is enabled, hibernate until a management client releases the hold.
3928 * Return true if the caller should not sleep for an additional time interval.
3929 */
3930bool
3932{
3934 {
3935 volatile int signal_received = 0;
3936 const bool standalone_disabled_save = man->persist.standalone_disabled;
3937 struct gc_arena gc = gc_new();
3938
3940 false; /* This is so M_CLIENT messages will be correctly passed through msg() */
3941 man->persist.special_state_msg = NULL;
3943
3944 man_wait_for_client_connection(man, &signal_received, 0, MWCC_HOLD_WAIT);
3945
3946 if (!signal_received)
3947 {
3948 struct buffer out = alloc_buf_gc(128, &gc);
3949 buf_printf(&out, ">HOLD:Waiting for hold release:%d", holdtime);
3950 man->persist.special_state_msg = BSTR(&out);
3951 msg(M_CLIENT, "%s", man->persist.special_state_msg);
3952
3953 /* run command processing event loop until we get our username/password */
3954 do
3955 {
3956 man_standalone_event_loop(man, &signal_received, 0);
3957 if (!signal_received)
3958 {
3959 man_check_for_signals(&signal_received);
3960 }
3961 if (signal_received)
3962 {
3963 break;
3964 }
3965 } while (!man->persist.hold_release);
3966 }
3967
3968 /* revert state */
3969 man->persist.standalone_disabled = standalone_disabled_save;
3970 man->persist.special_state_msg = NULL;
3971 man->settings.mansig &= ~MANSIG_IGNORE_USR1_HUP;
3972
3973 gc_free(&gc);
3974 return true;
3975 }
3976 return false;
3977}
3978
3979/*
3980 * struct command_line
3981 */
3982
3983struct command_line *
3984command_line_new(const size_t len)
3985{
3986 struct command_line *cl;
3987 ALLOC_OBJ_CLEAR(cl, struct command_line);
3988 cl->buf = alloc_buf(len);
3989 cl->residual = alloc_buf(len);
3990 return cl;
3991}
3992
3993void
3995{
3996 buf_clear(&cl->buf);
3997 buf_clear(&cl->residual);
3998}
3999
4000void
4002{
4003 if (!cl)
4004 {
4005 return;
4006 }
4008 free_buf(&cl->buf);
4009 free_buf(&cl->residual);
4010 free(cl);
4011}
4012
4013void
4014command_line_add(struct command_line *cl, const unsigned char *buf, const size_t len)
4015{
4016 for (size_t i = 0; i < len; ++i)
4017 {
4018 if (buf[i] && char_class(buf[i], (CC_PRINT | CC_NEWLINE)))
4019 {
4020 if (!buf_write_u8(&cl->buf, buf[i]))
4021 {
4022 buf_clear(&cl->buf);
4023 }
4024 }
4025 }
4026}
4027
4028const char *
4030{
4031 const char *ret = NULL;
4032
4033 int i = buf_substring_len(&cl->buf, '\n');
4034 if (i >= 0)
4035 {
4036 buf_copy_excess(&cl->residual, &cl->buf, i);
4037 buf_chomp(&cl->buf);
4038 ret = BSTR(&cl->buf);
4039 }
4040 return ret;
4041}
4042
4043void
4045{
4046 buf_clear(&cl->buf);
4047 buf_copy(&cl->buf, &cl->residual);
4048 buf_clear(&cl->residual);
4049}
4050
4051/*
4052 * struct log_entry
4053 */
4054
4055const char *
4056log_entry_print(const struct log_entry *e, unsigned int flags, struct gc_arena *gc)
4057{
4058 struct buffer out = alloc_buf_gc(ERR_BUF_SIZE, gc);
4059 if (flags & LOG_FATAL_NOTIFY)
4060 {
4061 buf_printf(&out, ">FATAL:");
4062 }
4063 if (flags & LOG_PRINT_LOG_PREFIX)
4064 {
4065 buf_printf(&out, ">LOG:");
4066 }
4067 if (flags & LOG_PRINT_ECHO_PREFIX)
4068 {
4069 buf_printf(&out, ">ECHO:");
4070 }
4071 if (flags & LOG_PRINT_STATE_PREFIX)
4072 {
4073 buf_printf(&out, ">STATE:");
4074 }
4075 if (flags & LOG_PRINT_INT_DATE)
4076 {
4077 buf_printf(&out, "%u,", (unsigned int)e->timestamp);
4078 }
4079 if (flags & LOG_PRINT_MSG_FLAGS)
4080 {
4081 buf_printf(&out, "%s,", msg_flags_string(e->u.msg_flags, gc));
4082 }
4083 if (flags & LOG_PRINT_STATE)
4084 {
4085 buf_printf(&out, "%s,", man_state_name(e->u.state));
4086 }
4087 if (flags & LOG_PRINT_INTVAL)
4088 {
4089 buf_printf(&out, "%d,", e->u.intval);
4090 }
4091 if (e->string)
4092 {
4093 buf_printf(&out, "%s", e->string);
4094 }
4095 if (flags & LOG_PRINT_LOCAL_IP)
4096 {
4097 buf_printf(&out, ",%s", print_in_addr_t(e->local_ip, IA_EMPTY_IF_UNDEF, gc));
4098 }
4099 if (flags & LOG_PRINT_REMOTE_IP)
4100 {
4101 buf_printf(&out, ",%s",
4102 (!addr_defined(&e->remote_sock)
4103 ? ","
4104 : print_sockaddr_ex(&e->remote_sock.addr.sa, ",",
4106 buf_printf(&out, ",%s",
4107 (!addr_defined(&e->local_sock)
4108 ? ","
4109 : print_sockaddr_ex(&e->local_sock.addr.sa, ",",
4111 }
4112 if (flags & LOG_PRINT_LOCAL_IP && !IN6_IS_ADDR_UNSPECIFIED(&e->local_ip6))
4113 {
4114 buf_printf(&out, ",%s", print_in6_addr(e->local_ip6, IA_EMPTY_IF_UNDEF, gc));
4115 }
4116 if (flags & LOG_ECHO_TO_LOG)
4117 {
4118 msg(D_MANAGEMENT, "MANAGEMENT: %s", BSTR(&out));
4119 }
4120 if (flags & LOG_PRINT_CRLF)
4121 {
4122 buf_printf(&out, "\r\n");
4123 }
4124 return BSTR(&out);
4125}
4126
4127static void
4129{
4130 /* Cast away constness of const char* */
4131 free((char *)e->string);
4132 CLEAR(*e);
4133}
4134
4135/*
4136 * struct log_history
4137 */
4138
4139static inline int
4140log_index(const struct log_history *h, int i)
4141{
4142 return modulo_add(h->base, i, h->capacity);
4143}
4144
4145static void
4147{
4148 CLEAR(*h);
4149 h->capacity = capacity;
4151}
4152
4153struct log_history *
4155{
4156 struct log_history *h;
4157 ASSERT(capacity > 0);
4158 ALLOC_OBJ(h, struct log_history);
4160 return h;
4161}
4162
4163static void
4165{
4166 int i;
4167 for (i = 0; i < h->size; ++i)
4168 {
4170 }
4171 free(h->array);
4172}
4173
4174void
4176{
4178 free(h);
4179}
4180
4181void
4182log_history_add(struct log_history *h, const struct log_entry *le)
4183{
4184 struct log_entry *e;
4185 ASSERT(h->size >= 0 && h->size <= h->capacity);
4186 if (h->size == h->capacity)
4187 {
4188 e = &h->array[h->base];
4190 h->base = log_index(h, 1);
4191 }
4192 else
4193 {
4194 e = &h->array[log_index(h, h->size)];
4195 ++h->size;
4196 }
4197
4198 *e = *le;
4199 e->string = string_alloc(le->string, NULL);
4200}
4201
4202void
4203log_history_resize(struct log_history *h, const int capacity)
4204{
4205 if (capacity != h->capacity)
4206 {
4207 struct log_history newlog;
4208 int i;
4209
4210 ASSERT(capacity > 0);
4212
4213 for (i = 0; i < h->size; ++i)
4214 {
4215 log_history_add(&newlog, &h->array[log_index(h, i)]);
4216 }
4217
4219 *h = newlog;
4220 }
4221}
4222
4223const struct log_entry *
4224log_history_ref(const struct log_history *h, const int index)
4225{
4226 if (index >= 0 && index < h->size)
4227 {
4228 return &h->array[log_index(h, (h->size - 1) - index)];
4229 }
4230 else
4231 {
4232 return NULL;
4233 }
4234}
4235
4236void
4238{
4239 if (n < 0)
4240 {
4241 return;
4242 }
4243 else if (management)
4244 {
4246 }
4247 else
4248 {
4249#ifdef _WIN32
4250 win32_sleep(n);
4251#else
4252 if (n > 0)
4253 {
4254 sleep(n);
4255 }
4256#endif
4257 }
4258}
4259
4260void
4261management_check_bytecount_client(struct context *c, struct management *man, struct timeval *timeval)
4262{
4263 if (man->persist.callback.flags & MCF_SERVER)
4264 {
4265 return;
4266 }
4267
4269 {
4270 if (dco_enabled(&c->options))
4271 {
4272 if (dco_get_peer_stats(c, true) < 0)
4273 {
4274 return;
4275 }
4276 }
4277
4280 }
4281}
4282
4283void
4284management_check_bytecount_server(struct multi_context *multi, struct timeval *timeval)
4285{
4287 {
4288 return;
4289 }
4290
4292 {
4293 /* fetch counters from dco */
4294 if (dco_enabled(&multi->top.options))
4295 {
4296 if (dco_get_peer_stats_multi(&multi->top.c1.tuntap->dco, true) < 0)
4297 {
4298 return;
4299 }
4300 }
4301
4302 /* iterate over peers and report counters for each connected peer */
4303 struct hash_iterator hi;
4304 struct hash_element *he;
4305 hash_iterator_init(multi->hash, &hi);
4306 while ((he = hash_iterator_next(&hi)))
4307 {
4308 struct multi_instance *mi = (struct multi_instance *)he->value;
4309 struct context_2 *c2 = &mi->context.c2;
4310
4312 {
4314 }
4315 }
4316 hash_iterator_free(&hi);
4317 }
4318}
4319
4320/* context_2 stats are reset on reconnect. Since client expects stats
4321 * to be preserved across reconnects, we need to save context_2
4322 * stats before tearing the tunnel down.
4323 */
4324void
4326{
4329
4330 /* no need to raise SIGUSR1 on error since we are already closing the instance */
4331 if (dco_enabled(&c->options) && (dco_get_peer_stats(c, false) == 0))
4332 {
4333 man->persist.bytes_in += c->c2.dco_read_bytes;
4335 }
4336}
4337
4338#else /* ifdef ENABLE_MANAGEMENT */
4339
4340#include "win32.h"
4341void
4342management_sleep(const int n)
4343{
4344#ifdef _WIN32
4345 win32_sleep(n);
4346#else
4347 if (n > 0)
4348 {
4349 sleep(n);
4350 }
4351#endif /* ifdef _WIN32 */
4352}
4353
4354#endif /* ENABLE_MANAGEMENT */
#define OPENVPN_BASE64_DECODED_LENGTH(base64_length)
Compute the maximal number of bytes encoded in a base64 string.
Definition base64.h:41
#define OPENVPN_BASE64_LENGTH(binary_length)
Compute resulting base64 length.
Definition base64.h:38
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
Definition buffer.c:1175
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:169
void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, const char *sep)
Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len .
Definition buffer.c:1251
void buf_clear(struct buffer *buf)
Zeroise and reset a buffer.
Definition buffer.c:148
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
Definition buffer.c:1181
void buffer_list_aggregate(struct buffer_list *bl, const size_t max)
Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len .
Definition buffer.c:1296
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:226
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
Definition buffer.c:1156
void chomp(char *str)
Remove trailing newline and carriage-return characters from a string.
Definition buffer.c:584
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition buffer.c:1238
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition buffer.c:1165
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
Definition buffer.c:60
void buffer_list_advance(struct buffer_list *ol, ssize_t n)
Advance past n bytes in the head buffer, popping it if it becomes empty.
Definition buffer.c:1319
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
Extract the next token from a buffer, delimited by a given character.
Definition buffer.c:775
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:616
void buffer_list_push(struct buffer_list *ol, const char *str)
Allocates and appends a new buffer containing str as data to ol.
Definition buffer.c:1196
void buf_chomp(struct buffer *buf)
Remove trailing newline and carriage-return characters from a buffer.
Definition buffer.c:522
bool char_class(const unsigned char c, const unsigned int flags)
Test whether a character belongs to one or more character classes.
Definition buffer.c:839
int buf_substring_len(const struct buffer *buf, int delim)
Return the number of bytes in a buffer up to and including a delimiter.
Definition buffer.c:753
#define ALLOC_OBJ(dptr, type)
Allocate memory for a single object of the given type.
Definition buffer.h:2025
static uint8_t * buf_bptr(struct buffer *buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:447
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Copy the content of one buffer to the end of another.
Definition buffer.h:1363
#define ALLOC_ARRAY_CLEAR(dptr, type, n)
Allocate and zero-initialise an array of n elements of the given type.
Definition buffer.h:2077
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
Truncate src to len bytes and copy any excess to dest.
Definition buffer.h:1438
#define CC_NEWLINE
newline
Definition buffer.h:1712
static void buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
Initialise a buffer with an externally provided read-only memory region.
Definition buffer.h:685
#define CBPTR(buf)
Return a const pointer to the start of the buffer content.
Definition buffer.h:141
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:767
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1260
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1304
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:153
static void strncpynt(char *dest, const char *src, size_t maxlen)
Like strncpy() but always null-terminates the destination.
Definition buffer.h:708
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 CC_PRINT
printable (>= 32, != 127)
Definition buffer.h:1706
#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
uint64_t counter_type
Definition common.h:31
#define counter_format
Definition common.h:32
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
static int dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err)
Definition dco.h:369
static int dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err)
Definition dco.h:375
void setenv_int(struct env_set *es, const char *name, int value)
Definition env_set.c:291
struct env_set * env_set_create(struct gc_arena *gc)
Definition env_set.c:156
#define D_MANAGEMENT_DEBUG
Definition errlevel.h:137
#define M_INFO
Definition errlevel.h:54
#define D_MANAGEMENT
Definition errlevel.h:87
#define D_LINK_ERRORS
Definition errlevel.h:56
struct event_set * event_set_init(int *maxevents, unsigned int flags)
Definition event.c:1187
#define EVENT_METHOD_FAST
Definition event.h:79
static void event_free(struct event_set *es)
Definition event.h:159
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_reset(struct event_set *es)
Definition event.h:168
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
counter_type link_write_bytes_global
Definition forward.c:49
counter_type link_read_bytes_global
Definition forward.c:48
void reset_coarse_timers(struct context *c)
Definition init.c:1282
static size_t min_size(size_t x, size_t y)
Definition integer.h:79
static unsigned int min_uint(unsigned int x, unsigned int y)
Definition integer.h:66
static int min_int(int x, int y)
Definition integer.h:105
static int modulo_add(int x, int y, int mod)
Definition integer.h:185
static SERVICE_STATUS status
Definition interactive.c:52
bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry)
This is the principal function for testing and triggering recurring timers.
Definition interval.c:42
#define ETT_DEFAULT
Definition interval.h:222
static void event_timeout_init(struct event_timeout *et, interval_t n, const time_t last)
Initialises a timer struct.
Definition interval.h:172
static void event_timeout_clear(struct event_timeout *et)
Clears the timeout and reset all values to 0.
Definition interval.h:153
void hash_iterator_free(struct hash_iterator *hi)
Definition list.c:272
struct hash_element * hash_iterator_next(struct hash_iterator *hi)
Definition list.c:278
void hash_iterator_init(struct hash *hash, struct hash_iterator *hi)
Definition list.c:236
void management_pre_tunnel_close(struct management *man)
Definition manage.c:3200
void management_auth_failure(struct management *man, const char *type, const char *reason)
Definition manage.c:3209
void log_history_close(struct log_history *h)
Definition manage.c:4175
struct management * management
Definition manage.c:75
static void man_status(struct management *man, const int version, struct status_output *so)
Definition manage.c:508
void management_check_bytecount_server(struct multi_context *multi, struct timeval *timeval)
Definition manage.c:4284
static void man_up_finalize(struct management *man)
Definition manage.c:741
static bool man_io_error(struct management *man, const char *prefix)
Definition manage.c:2260
struct command_line * command_line_new(const size_t len)
Definition manage.c:3984
static void man_log(struct management *man, const char *parm)
Definition manage.c:720
void management_auth_token(const char *token)
Definition manage.c:3222
static void man_reset_client_socket(struct management *man, const bool exiting)
Definition manage.c:2162
static void man_bytecount(struct management *man, const int update_seconds)
Definition manage.c:528
static void man_connect(struct management *man)
Definition manage.c:2096
#define MWCC_OTHER_WAIT
Definition manage.c:3515
void management_notify_client_needing_auth(struct management *man, const unsigned int mda_key_id, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3065
static ssize_t man_read(struct management *man)
Definition manage.c:2418
static bool man_check_for_signals(volatile int *signal_received)
Definition manage.c:3399
static void man_bytecount_output_server(const counter_type bytes_in_total, const counter_type bytes_out_total, struct man_def_auth_context *mdac)
Definition manage.c:578
static void man_output_list_push(struct management *man, const char *str)
Definition manage.c:293
void management_socket_set(struct management *man, struct event_set *es, void *arg, unsigned int *persistent)
Definition manage.c:3244
void command_line_free(struct command_line *cl)
Definition manage.c:4001
static int man_block(struct management *man, volatile int *signal_received, const time_t expire)
Definition manage.c:3416
static void man_certificate(struct management *man)
Definition manage.c:1313
void command_line_next(struct command_line *cl)
Definition manage.c:4044
static void man_net(struct management *man)
Definition manage.c:854
void management_clear_callback(struct management *man)
Definition manage.c:2871
static void man_persist_init(struct management *man, const int log_history_cache, const int echo_buffer_size, const int state_buffer_size)
Definition manage.c:2569
char * management_query_cert(struct management *man, const char *cert_name)
Definition manage.c:3902
static bool man_standalone_ok(const struct management *man)
Definition manage.c:3393
void management_connection_established(struct management *man, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3107
bool management_hold(struct management *man, int holdtime)
Definition manage.c:3931
static void man_settings_init(struct man_settings *ms, const char *addr, const char *port, const char *pass_file, const char *client_user, const char *client_group, const int log_history_cache, const int echo_buffer_size, const int state_buffer_size, const int remap_sigusr1, const unsigned int flags)
Definition manage.c:2627
static int log_index(const struct log_history *h, int i)
Definition manage.c:4140
static void man_connection_clear(struct man_connection *mc)
Definition manage.c:2556
#define AF_DID_RESET
static void man_start_ne32(struct management *man)
Definition manage.c:1849
static void man_connection_settings_reset(struct management *man)
Definition manage.c:1877
static void man_help(void)
Definition manage.c:83
static void man_update_io_state(struct management *man)
Definition manage.c:254
static void man_bytecount_stop(struct management *man)
Definition manage.c:521
static void man_check_password(struct management *man, const char *line)
Definition manage.c:223
static bool man_persist_state(unsigned int *persistent, const int n)
Definition manage.c:3228
static void man_connection_init(struct management *man)
Definition manage.c:2727
static bool env_filter_match(const char *env_str, const int env_filter_level)
Definition manage.c:2934
static void man_client_pending_auth(struct management *man, const char *cid_str, const char *kid_str, const char *extra, const char *timeout_str)
Will send a notification to the client that succesful authentication will require an additional step ...
Definition manage.c:1172
static void man_connection_close(struct management *man)
Definition manage.c:2772
static void man_query_password(struct management *man, const char *type, const char *string)
Definition manage.c:814
static void man_client_kill(struct management *man, const char *cid_str, const char *kill_msg)
Definition manage.c:1249
struct log_history * log_history_init(const int capacity)
Definition manage.c:4154
static void man_listen(struct management *man)
Definition manage.c:2020
static void man_stop_ne32(struct management *man)
Definition manage.c:1869
static void man_forget_passwords(void)
Definition manage.c:846
static void man_query_username(struct management *man, const char *type, const char *string)
Definition manage.c:803
static void man_push_update(struct management *man, const char **p, const push_update_type type)
Definition manage.c:1436
#define MWCC_PASSWORD_WAIT
Definition manage.c:3513
static void man_dispatch_command(struct management *man, struct status_output *so, const char **p, const int nparms)
Definition manage.c:1478
static void virtual_output_callback_func(void *arg, const unsigned int flags, const char *str)
Definition manage.c:360
static void man_signal(struct management *man, const char *name)
Definition manage.c:474
static void man_prompt(struct management *man)
Definition manage.c:300
mcv
Definition manage.c:67
@ MCV_USERNAME_ONLY
Definition manage.c:71
@ MCV_PKSIGN_ALG
Definition manage.c:70
@ MCV_DEFAULT
Definition manage.c:68
@ MCV_PKSIGN
Definition manage.c:69
@ MCV_MULTILINE_PASSWORD
Definition manage.c:72
static void man_state(struct management *man, const char *parm)
Definition manage.c:734
static void man_client_deny(struct management *man, const char *cid_str, const char *kid_str, const char *reason, const char *client_reason)
Definition manage.c:1221
static void man_echo(struct management *man, const char *parm)
Definition manage.c:727
static char * management_query_multiline_flatten(struct management *man, const char *b64_data, const char *prompt, const char *cmd, int *state, struct buffer_list **input)
Definition manage.c:3847
static void man_proxy(struct management *man, const char **p)
Definition manage.c:1368
struct management * management_init(void)
Definition manage.c:2801
static void in_extra_reset(struct man_connection *mc, const int mode)
Definition manage.c:1003
void command_line_reset(struct command_line *cl)
Definition manage.c:3994
static void man_query_need_str(struct management *man, const char *type, const char *action)
Definition manage.c:837
bool management_would_hold(struct management *man)
Definition manage.c:3921
static void man_history(struct management *man, const char *parm, const char *type, struct log_history *log, bool *realtime, const unsigned int lep_flags)
Definition manage.c:678
bool management_query_user_pass(struct management *man, struct user_pass *up, const char *type, const unsigned int flags, const char *static_challenge)
Definition manage.c:3607
static void man_bytecount_output_client(counter_type bytes_in_total, counter_type bytes_out_total)
Definition manage.c:566
void log_history_add(struct log_history *h, const struct log_entry *le)
Definition manage.c:4182
void management_notify_client_close(const struct management *man, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3117
static void log_entry_free_contents(struct log_entry *e)
Definition manage.c:4128
static int man_mod_signal(const struct management *man, const int signum)
Definition manage.c:448
void management_notify_generic(const char *str)
Definition manage.c:3031
#define MN_AT_LEAST
Definition manage.c:1341
void management_event_loop_n_seconds(struct management *man, int sec)
Definition manage.c:3554
static void man_persist_close(struct man_persist *mp)
Definition manage.c:2605
static void man_kill(struct management *man, const char *victim)
Definition manage.c:590
void management_close(struct management *man)
Definition manage.c:2854
static void man_pk_sig(struct management *man, const char *cmd_name)
Definition manage.c:1297
void management_set_state(struct management *man, const int state, const char *detail, const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6, const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
Definition manage.c:2880
static void man_output_list_push_finalize(struct management *man)
Definition manage.c:270
static bool man_need(const char **p, const int n, unsigned int flags)
Checks if the correct number of arguments to a management command are present and otherwise prints an...
Definition manage.c:1352
static void man_load_stats(struct management *man)
Definition manage.c:1329
static bool man_password_needed(struct management *man)
Definition manage.c:217
static void set_client_version(struct management *man, const char *version)
Definition manage.c:1418
const char * log_entry_print(const struct log_entry *e, unsigned int flags, struct gc_arena *gc)
Definition manage.c:4056
static void man_accept(struct management *man)
Definition manage.c:1980
static void man_client_auth(struct management *man, const char *cid_str, const char *kid_str, const bool extra)
Definition manage.c:1204
static bool parse_cid(const char *str, unsigned long *cid)
Definition manage.c:1133
static void man_send_cc_message(struct management *man, const char *message, const char *parameters)
Definition manage.c:867
static void man_delete_unix_socket(struct management *man)
Definition manage.c:333
void management_io(struct management *man)
Definition manage.c:3282
static void man_wait_for_client_connection(struct management *man, volatile int *signal_received, const time_t expire, unsigned int flags)
Definition manage.c:3521
#define IER_NEW
Definition manage.c:1000
static void man_output_peer_info_env(struct management *man, const struct man_def_auth_context *mdac)
Definition manage.c:3037
#define AF_DID_PUSH
bool management_open(struct management *man, const char *addr, const char *port, const char *pass_file, const char *client_user, const char *client_group, const int log_history_cache, const int echo_buffer_size, const int state_buffer_size, const int remap_sigusr1, const unsigned int flags)
Definition manage.c:2815
static void man_welcome(struct management *man)
Definition manage.c:206
static void man_query_need_ok(struct management *man, const char *type, const char *action)
Definition manage.c:828
static char * management_query_multiline_flatten_newline(struct management *man, const char *b64_data, const char *prompt, const char *cmd, int *state, struct buffer_list **input)
Definition manage.c:3817
static void man_output_extra_env(struct management *man, const char *prefix)
Definition manage.c:3001
void man_persist_client_stats(struct management *man, struct context *c)
Definition manage.c:4325
static void man_new_connection_post(struct management *man, const char *description)
Definition manage.c:1890
static void log_history_obj_init(struct log_history *h, int capacity)
Definition manage.c:4146
static void man_query_password_base64(struct management *man, const char *type)
Enter multi-line base64 mode for receiving a password that exceeds the single-line parameter size lim...
Definition manage.c:1039
static void report_command_status(const bool status, const char *command)
Small function to report the success or failure of a command to the management interface.
Definition manage.c:320
#define IER_RESET
Definition manage.c:999
static void man_close_socket(struct management *man, const socket_descriptor_t sd)
Definition manage.c:344
static bool parse_uint(const char *str, const char *what, unsigned int *uint)
Definition manage.c:1147
void management_set_callback(struct management *man, const struct management_callback *cb)
Definition manage.c:2864
static void man_client_n_clients(struct management *man)
Definition manage.c:1275
static void man_output_standalone(struct management *man, volatile int *signal_received)
Definition manage.c:3475
void management_notify_client_cr_response(unsigned mda_key_id, const struct man_def_auth_context *mdac, const struct env_set *es, const char *response)
Definition manage.c:3087
void management_echo(struct management *man, const char *string, const bool pull)
Definition manage.c:3143
static void man_remote_entry_get(struct management *man, const char *p1, const char *p2)
Definition manage.c:932
const struct log_entry * log_history_ref(const struct log_history *h, const int index)
Definition manage.c:4224
static void man_remote_entry_count(struct management *man)
Definition manage.c:917
static void man_settings_close(struct man_settings *ms)
Definition manage.c:2716
void management_check_bytecount_client(struct context *c, struct management *man, struct timeval *timeval)
Definition manage.c:4261
void management_notify(const char *severity, const char *type, const char *text)
Definition manage.c:3025
static void man_env_filter(struct management *man, const int level)
Definition manage.c:1289
#define MANAGEMENT_ECHO_FLAGS
Definition manage.c:59
static void man_output_list_push_str(struct management *man, const char *str)
Definition manage.c:284
void management_up_down(struct management *man, const char *updown, const struct env_set *es)
Definition manage.c:3015
char * management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
Definition manage.c:3876
static void man_remote(struct management *man, const char **p)
Definition manage.c:1382
void management_learn_addr(struct man_def_auth_context *mdac, const struct mroute_addr *addr, const bool primary)
Definition manage.c:3130
void command_line_add(struct command_line *cl, const unsigned char *buf, const size_t len)
Definition manage.c:4014
static int man_standalone_event_loop(struct management *man, volatile int *signal_received, const time_t expire)
Definition manage.c:3498
static void man_process_command(struct management *man, const char *line)
Definition manage.c:2210
static int management_query_multiline(struct management *man, const char *b64_data, const char *prompt, const char *cmd, int *state, struct buffer_list **input)
Definition manage.c:3739
static void man_output_env(const struct env_set *es, const bool tail, const int env_filter_level, const char *prefix)
Definition manage.c:2980
const char * command_line_get(struct command_line *cl)
Definition manage.c:4029
static void log_history_free_contents(struct log_history *h)
Definition manage.c:4164
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
Definition manage.c:4237
static const char * man_state_name(const int state)
Definition manage.c:157
void log_history_resize(struct log_history *h, const int capacity)
Definition manage.c:4203
static void in_extra_dispatch(struct management *man)
Definition manage.c:1061
static void man_command_unsupported(const char *command_name)
Definition manage.c:501
#define MWCC_HOLD_WAIT
Definition manage.c:3514
void management_post_tunnel_open(struct management *man, const in_addr_t tun_local_ip)
Definition manage.c:3177
static void man_query_user_pass(struct management *man, const char *type, const char *string, const bool needed, const char *prompt, char *dest, int len)
Definition manage.c:778
static ssize_t man_write(struct management *man)
Definition manage.c:2512
static void man_hold(struct management *man, const char *cmd)
Definition manage.c:969
#define MANSIG_MAP_USR1_TO_TERM
Definition manage.h:257
#define MS_LISTEN
Definition manage.h:271
#define OPENVPN_STATE_ADD_ROUTES
Definition manage.h:453
#define UP_QUERY_USER_PASS
Definition manage.h:263
#define IEC_PASSWORD
Definition manage.h:300
#define OPENVPN_STATE_INITIAL
Definition manage.h:450
#define MF_FORGET_DISCONNECT
Definition manage.h:31
#define UP_QUERY_DISABLED
Definition manage.h:262
#define LOG_FATAL_NOTIFY
Definition manage.h:127
#define MS_INITIAL
Definition manage.h:270
#define MANAGEMENT_STATE_BUFFER_SIZE
Definition manage.h:57
#define MANAGEMENT_VERSION
Definition manage.h:53
#define OPENVPN_STATE_CONNECTING
Definition manage.h:451
#define EKS_SOLICIT
Definition manage.h:306
#define MS_CC_WAIT_WRITE
Definition manage.h:273
#define LOG_PRINT_REMOTE_IP
Definition manage.h:131
#define LOG_PRINT_STATE_PREFIX
Definition manage.h:119
#define OPENVPN_STATE_RECONNECTING
Definition manage.h:455
#define LOG_PRINT_INT_DATE
Definition manage.h:121
#define DAF_INITIAL_AUTH
Definition manage.h:68
#define IEC_PK_SIGN
Definition manage.h:299
#define MANSIG_MAP_USR1_TO_HUP
Definition manage.h:256
#define LOG_PRINT_ECHO_PREFIX
Definition manage.h:118
#define LOG_PRINT_LOCAL_IP
Definition manage.h:124
#define MANAGEMENT_ECHO_BUFFER_SIZE
Definition manage.h:56
#define DAF_CONNECTION_CLOSED
Definition manage.h:67
#define MCF_SERVER
Definition manage.h:175
#define OPENVPN_STATE_CLIENT_BASE
Definition manage.h:467
#define MS_CC_WAIT_READ
Definition manage.h:272
#define LOG_PRINT_STATE
Definition manage.h:123
#define OPENVPN_STATE_CONNECTED
Definition manage.h:454
#define OPENVPN_STATE_RESOLVE
Definition manage.h:462
#define LOG_ECHO_TO_LOG
Definition manage.h:133
#define UP_QUERY_NEED_STR
Definition manage.h:266
#define OPENVPN_STATE_EXITING
Definition manage.h:456
static bool management_connected(const struct management *man)
Definition manage.h:415
#define MANAGEMENT_LOG_HISTORY_INITIAL_SIZE
Definition manage.h:55
#define OPENVPN_STATE_AUTH_PENDING
Definition manage.h:464
static int log_history_size(const struct log_history *h)
Definition manage.h:156
#define IEC_UNDEF
Definition manage.h:294
#define IEC_CERTIFICATE
Definition manage.h:298
#define MANSIG_IGNORE_USR1_HUP
Definition manage.h:255
#define OPENVPN_STATE_AUTH
Definition manage.h:460
#define EKS_READY
Definition manage.h:308
#define LOG_PRINT_LOG_PREFIX
Definition manage.h:117
#define OPENVPN_STATE_WAIT
Definition manage.h:459
#define LOG_PRINT_INTVAL
Definition manage.h:129
#define LOG_PRINT_CRLF
Definition manage.h:126
#define DAF_CONNECTION_ESTABLISHED
Definition manage.h:66
#define MF_SERVER
Definition manage.h:27
#define OPENVPN_STATE_ASSIGN_IP
Definition manage.h:452
#define MANAGEMENT_N_PASSWORD_RETRIES
Definition manage.h:54
#define MF_UP_DOWN
Definition manage.h:39
#define UP_QUERY_PASS
Definition manage.h:264
#define EKS_UNDEF
Definition manage.h:305
#define MF_HOLD
Definition manage.h:29
#define IEC_CLIENT_AUTH
Definition manage.h:295
#define MF_SIGNAL
Definition manage.h:30
#define UP_QUERY_NEED_OK
Definition manage.h:265
#define MF_UNIX_SOCK
Definition manage.h:35
#define OPENVPN_STATE_GET_CONFIG
Definition manage.h:461
#define MF_CONNECT_AS_CLIENT
Definition manage.h:32
#define OPENVPN_STATE_TCP_CONNECT
Definition manage.h:463
#define LOG_PRINT_MSG_FLAGS
Definition manage.h:122
#define UP_QUERY_USERNAME
Definition manage.h:267
#define EKS_INPUT
Definition manage.h:307
bool validate_peer_info_line(char *line)
Definition misc.c:722
#define USER_PASS_LEN
Definition misc.h:67
#define GET_USER_PASS_STATIC_CHALLENGE_CONCAT
indicates password and response should be concatenated
Definition misc.h:128
#define GET_USER_PASS_PASSWORD_ONLY
Definition misc.h:115
#define GET_USER_PASS_USERNAME_ONLY
indicate that only username should be prompted for auth-user-pass
Definition misc.h:130
#define GET_USER_PASS_STATIC_CHALLENGE_ECHO
SCRV1 protocol – echo response.
Definition misc.h:123
#define GET_USER_PASS_NEED_OK
Definition misc.h:116
static const char blank_up[]
Definition misc.h:52
static bool get_user_pass(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags)
Retrieves the user credentials from various sources depending on the flags.
Definition misc.h:155
#define GET_USER_PASS_NEED_STR
Definition misc.h:118
const char * mroute_addr_print_ex(const struct mroute_addr *ma, const unsigned int flags, struct gc_arena *gc)
Definition mroute.c:378
#define MAPF_SUBNET
Definition mroute.h:155
Header file for server-mode related structures and functions.
#define BOOL_CAST(x)
Definition basic.h:26
#define CLEAR(x)
Definition basic.h:32
#define SIZE(x)
Definition basic.h:29
msglvl_t get_debug_level(void)
Definition error.c:133
bool set_mute_cutoff(const int cutoff)
Definition error.c:119
int get_mute_cutoff(void)
Definition error.c:139
bool set_debug_level(const int level, const unsigned int flags)
Definition error.c:103
const char * msg_flags_string(const msglvl_t flags, struct gc_arena *gc)
Definition error.c:748
#define M_CLIENT
Definition error.h:108
static void msg_set_virtual_output(const struct virtual_output *vo)
Definition error.h:352
#define ERR_BUF_SIZE
Definition error.h:34
#define M_FATAL
Definition error.h:90
static bool ignore_sys_error(const int err, bool crt_error)
Definition error.h:368
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
static int openvpn_errno_maybe_crt(bool *crt_error)
Definition error.h:408
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
const char * auth_retry_print(void)
Definition options.c:3430
const char title_string[]
Definition options.c:73
bool auth_retry_set(const msglvl_t msglevel, const char *option)
Definition options.c:3407
#define streq(x, y)
Definition options.h:726
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Definition options.h:961
int parse_line(const char *line, char *p[], const int n, const char *file, const int line_num, msglvl_t msglevel, struct gc_arena *gc)
#define MAX_PARMS
Definition options.h:51
time_t now
Definition otime.c:33
static void update_time(void)
Definition otime.h:84
unsigned int platform_getpid(void)
Definition platform.c:333
bool platform_user_get(const char *username, struct platform_state_user *state)
Definition platform.c:80
bool platform_group_get(const char *groupname, struct platform_state_group *state)
Definition platform.c:124
push_update_type
Definition push.h:47
@ UPT_BY_CID
Definition push.h:49
@ UPT_BROADCAST
Definition push.h:48
void throw_signal_soft(const int signum, const char *signal_text)
Throw a soft global signal.
Definition sig.c:204
const char * signal_name(const int sig, const bool upper)
Definition sig.c:91
void throw_signal(const int signum)
Throw a hard signal.
Definition sig.c:175
int parse_signal(const char *signame)
Definition sig.c:64
static void get_signal(volatile int *sig)
Copy the global signal_received (if non-zero) to the passed-in argument sig.
Definition sig.h:109
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
Definition socket.c:785
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:966
void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int ai_family, const char *prefix, bool ipv6only)
Definition socket.c:918
socket_descriptor_t create_socket_tcp(struct addrinfo *addrinfo)
Definition socket.c:536
in_addr_t getaddr(unsigned int flags, const char *hostname, int resolve_retry_seconds, bool *succeeded, struct signal_info *sig_info)
Translate an IPv4 addr or hostname from string form to in_addr_t.
Definition socket.c:195
void sd_close(socket_descriptor_t *sd)
#define MSG_NOSIGNAL
Definition socket.h:262
#define openvpn_close_socket(s)
Definition socket.h:267
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
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)
const char * print_sockaddr_ex(const struct sockaddr *sa, const char *separator, const unsigned int flags, struct gc_arena *gc)
Definition socket_util.c:38
const char * print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
#define IA_EMPTY_IF_UNDEF
Definition socket_util.h:89
static const char * print_sockaddr(const struct sockaddr *addr, struct gc_arena *gc)
Definition socket_util.h:77
#define GETADDR_MSG_VIRT_OUT
#define GETADDR_PASSIVE
#define GETADDR_FATAL
#define PS_SHOW_PORT
Definition socket_util.h:31
@ PROTO_NONE
@ PROTO_UDP
@ PROTO_TCP_SERVER
#define GETADDR_HOST_ORDER
static bool addr_defined(const struct openvpn_sockaddr *addr)
#define GETADDR_RESOLVE
#define PS_DONT_SHOW_FAMILY
Definition socket_util.h:34
#define GETADDR_WARN_ON_SIGNAL
int openvpn_base64_decode(const char *str, void *data, int size)
Definition base64.c:160
void ssl_purge_auth(const bool auth_user_pass_only)
Definition ssl.c:395
bool ssl_clean_auth_token(void)
Definition ssl.c:384
Control Channel SSL/Data channel negotiation module.
struct status_output * status_open(const char *filename, const int refresh_freq, const int msglevel, const struct virtual_output *vout, const unsigned int flags)
Definition status.c:59
bool status_close(struct status_output *so)
Definition status.c:178
A singly-linked list of buffers, with head/tail pointers for O(1) push.
Definition buffer.h:2168
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int capacity
Size in bytes of memory allocated by malloc().
Definition buffer.h:72
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:78
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
struct buffer residual
Definition manage.h:80
struct buffer buf
Definition manage.h:79
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:173
Level 2 context containing state that is reset on both SIGHUP and SIGUSR1 restarts.
Definition openvpn.h:225
counter_type link_read_bytes
Definition openvpn.h:267
counter_type link_write_bytes
Definition openvpn.h:270
counter_type dco_read_bytes
Definition openvpn.h:268
struct man_def_auth_context mda_context
Definition openvpn.h:450
counter_type dco_write_bytes
Definition openvpn.h:271
Contains all state information for one tunnel.
Definition openvpn.h:471
struct context_2 c2
Level 2 context.
Definition openvpn.h:514
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:472
struct context_1 c1
Level 1 context.
Definition openvpn.h:513
char * string
Definition env_set.h:38
struct env_item * next
Definition env_set.h:39
struct env_item * list
Definition env_set.h:45
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
void * value
Definition list.h:41
Definition manage.h:107
struct in6_addr local_ip6
Definition manage.h:111
struct openvpn_sockaddr local_sock
Definition manage.h:112
struct openvpn_sockaddr remote_sock
Definition manage.h:113
time_t timestamp
Definition manage.h:108
const char * string
Definition manage.h:109
in_addr_t local_ip
Definition manage.h:110
union log_entry_union u
Definition manage.h:114
int capacity
Definition manage.h:141
struct log_entry * array
Definition manage.h:142
struct event_set * es
Definition manage.h:313
unsigned long in_extra_cid
Definition manage.h:303
const char * up_query_type
Definition manage.h:322
bool password_verified
Definition manage.h:288
int env_filter_level
Definition manage.h:314
struct user_pass up_query
Definition manage.h:324
socket_descriptor_t sd_top
Definition manage.h:279
struct buffer_list * ext_cert_input
Definition manage.h:312
struct command_line * in
Definition manage.h:291
unsigned int in_extra_kid
Definition manage.h:304
struct openvpn_sockaddr remote
Definition manage.h:281
socket_descriptor_t sd_cli
Definition manage.h:280
struct buffer_list * ext_key_input
Definition manage.h:310
int up_query_mode
Definition manage.h:323
int bytecount_update_seconds
Definition manage.h:319
int in_extra_cmd
Definition manage.h:301
struct buffer_list * in_extra
Definition manage.h:302
bool echo_realtime
Definition manage.h:318
bool state_realtime
Definition manage.h:316
struct event_timeout bytecount_update_interval
Definition manage.h:320
int ext_cert_state
Definition manage.h:311
struct buffer_list * out
Definition manage.h:292
int password_tries
Definition manage.h:289
int client_version
Definition manage.h:330
bool log_realtime
Definition manage.h:317
struct net_event_win32 ne32
Definition manage.h:284
int ext_key_state
Definition manage.h:309
unsigned long cid
Definition manage.h:64
unsigned int flags
Definition manage.h:69
counter_type bytes_in
Definition manage.h:234
struct log_history * state
Definition manage.h:228
struct management_callback callback
Definition manage.h:225
struct log_history * log
Definition manage.h:221
struct virtual_output vout
Definition manage.h:222
bool standalone_disabled
Definition manage.h:224
bool defined
Definition manage.h:219
bool hold_release
Definition manage.h:230
struct log_history * echo
Definition manage.h:227
const char * special_state_msg
Definition manage.h:232
counter_type bytes_out
Definition manage.h:235
bool management_over_tunnel
Definition manage.h:248
struct addrinfo * local
Definition manage.h:242
unsigned int flags
Definition manage.h:241
int echo_buffer_size
Definition manage.h:251
unsigned int mansig
Definition manage.h:258
int state_buffer_size
Definition manage.h:252
bool defined
Definition manage.h:240
int log_history_cache
Definition manage.h:250
struct user_pass up
Definition manage.h:249
char *(* get_peer_info)(void *arg, const unsigned long cid)
Definition manage.h:191
bool(* client_auth)(void *arg, const unsigned long cid, const unsigned int mda_key_id, const bool auth, const char *reason, const char *client_reason, struct buffer_list *cc_config)
Definition manage.h:186
int(* kill_by_addr)(void *arg, const in_addr_t addr, const uint16_t port, const uint8_t proto)
Definition manage.h:181
void(* delete_event)(void *arg, event_t event)
Definition manage.h:182
bool(* push_update_broadcast)(void *arg, const char *options)
Definition manage.h:199
bool(* push_update_by_cid)(void *arg, unsigned long cid, const char *options)
Definition manage.h:200
int(* n_clients)(void *arg)
Definition manage.h:183
void(* status)(void *arg, const int version, struct status_output *so)
Definition manage.h:178
bool(* remote_entry_get)(void *arg, unsigned int index, char **remote)
Definition manage.h:198
unsigned int(* remote_entry_count)(void *arg)
Definition manage.h:197
bool(* send_cc_message)(void *arg, const char *message, const char *parameter)
Definition manage.h:184
bool(* proxy_cmd)(void *arg, const char **p)
Definition manage.h:192
void(* show_net)(void *arg, const msglvl_t msglevel)
Definition manage.h:179
unsigned int flags
Definition manage.h:176
bool(* remote_cmd)(void *arg, const char **p)
Definition manage.h:193
int(* kill_by_cn)(void *arg, const char *common_name)
Definition manage.h:180
bool(* kill_by_cid)(void *arg, const unsigned long cid, const char *kill_msg)
Definition manage.h:185
struct man_persist persist
Definition manage.h:335
struct man_connection connection
Definition manage.h:337
struct man_settings settings
Definition manage.h:336
Main OpenVPN server state structure.
Definition multi.h:162
struct hash * hash
VPN tunnel instances indexed by real address of the remote peer.
Definition multi.h:169
struct context top
Storage structure for process-wide configuration.
Definition multi.h:201
Server-mode state structure for one single VPN tunnel.
Definition multi.h:102
struct context context
The context structure storing state for this VPN tunnel.
Definition multi.h:142
dco_context_t dco
Definition tun.h:247
bool defined
Definition misc.h:56
char password[USER_PASS_LEN]
Definition misc.h:71
bool nocache
Definition misc.h:60
char username[USER_PASS_LEN]
Definition misc.h:70
unsigned int flags_default
Definition status.h:34
void * arg
Definition status.h:33
void(* func)(void *arg, const unsigned int flags, const char *str)
Definition status.h:35
#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 sleep(x)
Definition syshead.h:42
#define SIGUSR1
Definition syshead.h:57
uint32_t in_addr_t
Definition syshead.h:52
static int socket_defined(const socket_descriptor_t sd)
Definition syshead.h:453
struct gc_arena gc
Definition test_ssl.c:122
unsigned int msg_flags
Definition manage.h:101
int state
Definition manage.h:102
int intval
Definition manage.h:103
void net_event_win32_init(struct net_event_win32 *ne)
Definition win32.c:419
void net_event_win32_close(struct net_event_win32 *ne)
Definition win32.c:470
void net_event_win32_reset_write(struct net_event_win32 *ne)
Definition win32.c:435
void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd)
Definition win32.c:426
void win32_sleep(const int n)
Definition win32.c:1550
void net_event_win32_reset(struct net_event_win32 *ne)
Definition win32.c:453
void net_event_win32_stop(struct net_event_win32 *ne)
Definition win32.c:459
static long net_event_win32_get_event_mask(const struct net_event_win32 *ne)
Definition win32.h:142
static void net_event_win32_clear_selected_events(struct net_event_win32 *ne, long selected_events)
Definition win32.h:148
static struct rw_handle * net_event_win32_get_event(struct net_event_win32 *ne)
Definition win32.h:136