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{
919 unsigned count = 0;
921 {
923 msg(M_CLIENT, "%u", count);
924 msg(M_CLIENT, "END");
925 }
926 else
927 {
928 man_command_unsupported("remote-entry-count");
929 }
930}
931
932static void
933man_remote_entry_get(struct management *man, const char *p1, const char *p2)
934{
935 ASSERT(p1);
936
938 {
939 unsigned int count = (*man->persist.callback.remote_entry_count)(man->persist.callback.arg);
940
941 unsigned int from = (unsigned int)atoi(p1);
942 unsigned int to = p2 ? (unsigned int)atoi(p2) : from + 1;
943
944 if (!strcmp(p1, "all"))
945 {
946 from = 0;
947 to = count;
948 }
949
950 for (unsigned int i = from; i < min_uint(to, count); i++)
951 {
952 char *remote = NULL;
953 bool res =
954 (*man->persist.callback.remote_entry_get)(man->persist.callback.arg, i, &remote);
955 if (res && remote)
956 {
957 msg(M_CLIENT, "%u,%s", i, remote);
958 }
959 free(remote);
960 }
961 msg(M_CLIENT, "END");
962 }
963 else
964 {
965 man_command_unsupported("remote-entry-get");
966 }
967}
968
969static void
970man_hold(struct management *man, const char *cmd)
971{
972 if (cmd)
973 {
974 if (streq(cmd, "on"))
975 {
976 man->settings.flags |= MF_HOLD;
977 msg(M_CLIENT, "SUCCESS: hold flag set to ON");
978 }
979 else if (streq(cmd, "off"))
980 {
981 man->settings.flags &= ~MF_HOLD;
982 msg(M_CLIENT, "SUCCESS: hold flag set to OFF");
983 }
984 else if (streq(cmd, "release"))
985 {
986 man->persist.hold_release = true;
987 msg(M_CLIENT, "SUCCESS: hold release succeeded");
988 }
989 else
990 {
991 msg(M_CLIENT, "ERROR: bad hold command parameter");
992 }
993 }
994 else
995 {
996 msg(M_CLIENT, "SUCCESS: hold=%d", BOOL_CAST(man->settings.flags & MF_HOLD));
997 }
998}
999
1000#define IER_RESET 0
1001#define IER_NEW 1
1002
1003static void
1004in_extra_reset(struct man_connection *mc, const int mode)
1005{
1006 if (mc)
1007 {
1008 if (mode != IER_NEW)
1009 {
1010 mc->in_extra_cmd = IEC_UNDEF;
1011 mc->in_extra_cid = 0;
1012 mc->in_extra_kid = 0;
1013 }
1014 if (mc->in_extra)
1015 {
1017 mc->in_extra = NULL;
1018 }
1019 if (mode == IER_NEW)
1020 {
1021 mc->in_extra = buffer_list_new();
1022 }
1023 }
1024}
1025
1039static void
1040man_query_password_base64(struct management *man, const char *type)
1041{
1042 const bool needed = ((man->connection.up_query_mode == UP_QUERY_PASS
1044 && man->connection.up_query_type);
1045 if (!needed)
1046 {
1047 msg(M_CLIENT, "ERROR: no password is currently needed at this time");
1048 return;
1049 }
1050 if (!man->connection.up_query_type || !streq(man->connection.up_query_type, type))
1051 {
1052 msg(M_CLIENT, "ERROR: password of type '%s' entered, but we need one of type '%s'",
1053 type, man->connection.up_query_type);
1054 return;
1055 }
1056 struct man_connection *mc = &man->connection;
1059}
1060
1061static void
1063{
1064 switch (man->connection.in_extra_cmd)
1065 {
1066 case IEC_CLIENT_AUTH:
1067 if (man->persist.callback.client_auth)
1068 {
1069 const bool status = (*man->persist.callback.client_auth)(
1071 man->connection.in_extra_kid, true, NULL, NULL, man->connection.in_extra);
1072 man->connection.in_extra = NULL;
1073 report_command_status(status, "client-auth");
1074 }
1075 else
1076 {
1077 man_command_unsupported("client-auth");
1078 }
1079 break;
1080
1081 case IEC_PK_SIGN:
1085 man->connection.in_extra = NULL;
1086 return;
1087
1088 case IEC_CERTIFICATE:
1092 man->connection.in_extra = NULL;
1093 return;
1094
1095 case IEC_PASSWORD:
1096 {
1097 char decoded[USER_PASS_LEN];
1098 CLEAR(decoded);
1099
1102 struct buffer *buf = buffer_list_peek(man->connection.in_extra);
1103
1104 if (buf && BLEN(buf) > 0)
1105 {
1107 {
1108 msg(M_CLIENT, "ERROR: password too long");
1109 buf_clear(buf);
1110 break;
1111 }
1113 USER_PASS_LEN - 1);
1114 if (len < 0)
1115 {
1116 msg(M_CLIENT, "ERROR: could not base64-decode password");
1117 buf_clear(buf);
1118 break;
1119 }
1120 decoded[len] = '\0';
1121 buf_clear(buf);
1122 }
1123
1124 man_query_password(man, man->connection.up_query_type,
1125 decoded);
1126 secure_memzero(decoded, sizeof(decoded));
1127 break;
1128 }
1129 }
1130 in_extra_reset(&man->connection, IER_RESET);
1131}
1132
1133static bool
1134parse_cid(const char *str, unsigned long *cid)
1135{
1136 if (sscanf(str, "%lu", cid) == 1)
1137 {
1138 return true;
1139 }
1140 else
1141 {
1142 msg(M_CLIENT, "ERROR: cannot parse CID");
1143 return false;
1144 }
1145}
1146
1147static bool
1148parse_uint(const char *str, const char *what, unsigned int *uint)
1149{
1150 if (sscanf(str, "%u", uint) == 1)
1151 {
1152 return true;
1153 }
1154 else
1155 {
1156 msg(M_CLIENT, "ERROR: cannot parse %s", what);
1157 return false;
1158 }
1159}
1160
1172static void
1173man_client_pending_auth(struct management *man, const char *cid_str, const char *kid_str,
1174 const char *extra, const char *timeout_str)
1175{
1176 unsigned long cid = 0;
1177 unsigned int kid = 0;
1178 unsigned int timeout = 0;
1179 if (parse_cid(cid_str, &cid) && parse_uint(kid_str, "KID", &kid)
1180 && parse_uint(timeout_str, "TIMEOUT", &timeout))
1181 {
1182 if (man->persist.callback.client_pending_auth)
1183 {
1184 bool ret = (*man->persist.callback.client_pending_auth)(man->persist.callback.arg, cid,
1185 kid, extra, timeout);
1186
1187 if (ret)
1188 {
1189 msg(M_CLIENT, "SUCCESS: client-pending-auth command succeeded");
1190 }
1191 else
1192 {
1193 msg(M_CLIENT, "ERROR: client-pending-auth command failed."
1194 " Extra parameter might be too long");
1195 }
1196 }
1197 else
1198 {
1199 man_command_unsupported("client-pending-auth");
1200 }
1201 }
1202}
1203
1204static void
1205man_client_auth(struct management *man, const char *cid_str, const char *kid_str, const bool extra)
1206{
1207 struct man_connection *mc = &man->connection;
1208 mc->in_extra_cid = 0;
1209 mc->in_extra_kid = 0;
1210 if (parse_cid(cid_str, &mc->in_extra_cid) && parse_uint(kid_str, "KID", &mc->in_extra_kid))
1211 {
1214 if (!extra)
1215 {
1216 in_extra_dispatch(man);
1217 }
1218 }
1219}
1220
1221static void
1222man_client_deny(struct management *man, const char *cid_str, const char *kid_str,
1223 const char *reason, const char *client_reason)
1224{
1225 unsigned long cid = 0;
1226 unsigned int kid = 0;
1227 if (parse_cid(cid_str, &cid) && parse_uint(kid_str, "KID", &kid))
1228 {
1229 if (man->persist.callback.client_auth)
1230 {
1231 const bool status = (*man->persist.callback.client_auth)(
1232 man->persist.callback.arg, cid, kid, false, reason, client_reason, NULL);
1233 if (status)
1234 {
1235 msg(M_CLIENT, "SUCCESS: client-deny command succeeded");
1236 }
1237 else
1238 {
1239 msg(M_CLIENT, "ERROR: client-deny command failed");
1240 }
1241 }
1242 else
1243 {
1244 man_command_unsupported("client-deny");
1245 }
1246 }
1247}
1248
1249static void
1250man_client_kill(struct management *man, const char *cid_str, const char *kill_msg)
1251{
1252 unsigned long cid = 0;
1253 if (parse_cid(cid_str, &cid))
1254 {
1255 if (man->persist.callback.kill_by_cid)
1256 {
1257 const bool status =
1258 (*man->persist.callback.kill_by_cid)(man->persist.callback.arg, cid, kill_msg);
1259 if (status)
1260 {
1261 msg(M_CLIENT, "SUCCESS: client-kill command succeeded");
1262 }
1263 else
1264 {
1265 msg(M_CLIENT, "ERROR: client-kill command failed");
1266 }
1267 }
1268 else
1269 {
1270 man_command_unsupported("client-kill");
1271 }
1272 }
1273}
1274
1275static void
1277{
1278 if (man->persist.callback.n_clients)
1279 {
1280 const int nclients = (*man->persist.callback.n_clients)(man->persist.callback.arg);
1281 msg(M_CLIENT, "SUCCESS: nclients=%d", nclients);
1282 }
1283 else
1284 {
1285 man_command_unsupported("nclients");
1286 }
1287}
1288
1289static void
1290man_env_filter(struct management *man, const int level)
1291{
1292 man->connection.env_filter_level = level;
1293 msg(M_CLIENT, "SUCCESS: env_filter_level=%d", level);
1294}
1295
1296
1297static void
1298man_pk_sig(struct management *man, const char *cmd_name)
1299{
1300 struct man_connection *mc = &man->connection;
1301 if (mc->ext_key_state == EKS_SOLICIT)
1302 {
1306 }
1307 else
1308 {
1309 msg(M_CLIENT, "ERROR: The %s command is not currently available", cmd_name);
1310 }
1311}
1312
1313static void
1315{
1316 struct man_connection *mc = &man->connection;
1317 if (mc->ext_cert_state == EKS_SOLICIT)
1318 {
1322 }
1323 else
1324 {
1325 msg(M_CLIENT, "ERROR: The certificate command is not currently available");
1326 }
1327}
1328
1329static void
1331{
1332 int nclients = 0;
1333
1334 if (man->persist.callback.n_clients)
1335 {
1336 nclients = (*man->persist.callback.n_clients)(man->persist.callback.arg);
1337 }
1338 msg(M_CLIENT, "SUCCESS: nclients=%d,bytesin=" counter_format ",bytesout=" counter_format,
1340}
1341
1342#define MN_AT_LEAST (1 << 0)
1353static bool
1354man_need(struct management *man, const char **p, const int n, unsigned int flags)
1355{
1356 int i;
1357 ASSERT(p[0]);
1358 for (i = 1; i <= n; ++i)
1359 {
1360 if (!p[i])
1361 {
1362 msg(M_CLIENT, "ERROR: the '%s' command requires %s%d parameter%s", p[0],
1363 (flags & MN_AT_LEAST) ? "at least " : "", n, n > 1 ? "s" : "");
1364 return false;
1365 }
1366 }
1367 return true;
1368}
1369
1370static void
1371man_proxy(struct management *man, const char **p)
1372{
1373 if (man->persist.callback.proxy_cmd)
1374 {
1375 const bool status = (*man->persist.callback.proxy_cmd)(man->persist.callback.arg, p);
1376 report_command_status(status, "proxy");
1377 }
1378 else
1379 {
1380 man_command_unsupported("proxy");
1381 }
1382}
1383
1384static void
1385man_remote(struct management *man, const char **p)
1386{
1387 if (man->persist.callback.remote_cmd)
1388 {
1389 const bool status = (*man->persist.callback.remote_cmd)(man->persist.callback.arg, p);
1390 report_command_status(status, "remote");
1391 }
1392 else
1393 {
1394 man_command_unsupported("remote");
1395 }
1396}
1397
1398#ifdef TARGET_ANDROID
1399static void
1400man_network_change(struct management *man, bool samenetwork)
1401{
1402 /* Called to signal the OpenVPN that the network configuration has changed and
1403 * the client should either float or reconnect.
1404 *
1405 * The code is currently only used by ics-openvpn
1406 */
1407 if (man->persist.callback.network_change)
1408 {
1409 int fd = (*man->persist.callback.network_change)(man->persist.callback.arg, samenetwork);
1410 man->connection.fdtosend = fd;
1411 msg(M_CLIENT, "PROTECTFD: fd '%d' sent to be protected", fd);
1412 if (fd == -2)
1413 {
1414 man_signal(man, "SIGUSR1");
1415 }
1416 }
1417}
1418#endif
1419
1420static void
1421set_client_version(struct management *man, const char *version)
1422{
1423 if (version)
1424 {
1425 man->connection.client_version = atoi(version);
1426 /* Until MCV_PKSIGN_ALG, we missed to respond to this command. Acknowledge only if version is newer */
1428 {
1429 msg(M_CLIENT, "SUCCESS: Management client version set to %d", man->connection.client_version);
1430 }
1431 }
1432 else
1433 {
1434 msg(M_CLIENT, "ERROR: Invalid value specified for management client version");
1435 }
1436}
1437
1438static void
1439man_push_update(struct management *man, const char **p, const push_update_type type)
1440{
1441 bool status = false;
1442
1443 if (type == UPT_BROADCAST)
1444 {
1446 {
1447 man_command_unsupported("push-update-broad");
1448 return;
1449 }
1450
1452 }
1453 else if (type == UPT_BY_CID)
1454 {
1456 {
1457 man_command_unsupported("push-update-cid");
1458 return;
1459 }
1460
1461 unsigned long cid = 0;
1462
1463 if (!parse_cid(p[1], &cid))
1464 {
1465 msg(M_CLIENT, "ERROR: push-update-cid fail during cid parsing");
1466 return;
1467 }
1468
1469 status = (*man->persist.callback.push_update_by_cid)(man->persist.callback.arg, cid, p[2]);
1470 }
1471
1472 if (status)
1473 {
1474 msg(M_CLIENT, "SUCCESS: push-update command succeeded");
1475 return;
1476 }
1477 msg(M_CLIENT, "ERROR: push-update command failed");
1478}
1479
1480static void
1481man_dispatch_command(struct management *man, struct status_output *so, const char **p,
1482 const int nparms)
1483{
1484 struct gc_arena gc = gc_new();
1485
1486 ASSERT(p[0]);
1487 if (streq(p[0], "exit") || streq(p[0], "quit"))
1488 {
1489 man->connection.halt = true;
1490 goto done;
1491 }
1492 else if (streq(p[0], "help"))
1493 {
1494 man_help();
1495 }
1496 else if (streq(p[0], "version") && p[1])
1497 {
1498 set_client_version(man, p[1]);
1499 }
1500 else if (streq(p[0], "version"))
1501 {
1502 msg(M_CLIENT, "OpenVPN Version: %s", title_string);
1503 msg(M_CLIENT, "Management Version: %d", MANAGEMENT_VERSION);
1504 msg(M_CLIENT, "END");
1505 }
1506 else if (streq(p[0], "pid"))
1507 {
1508 msg(M_CLIENT, "SUCCESS: pid=%d", platform_getpid());
1509 }
1510 else if (streq(p[0], "nclients"))
1511 {
1513 }
1514 else if (streq(p[0], "env-filter"))
1515 {
1516 int level = 0;
1517 if (p[1])
1518 {
1519 level = atoi(p[1]);
1520 }
1521 man_env_filter(man, level);
1522 }
1523 else if (streq(p[0], "signal"))
1524 {
1525 if (man_need(man, p, 1, 0))
1526 {
1527 man_signal(man, p[1]);
1528 }
1529 }
1530#ifdef TARGET_ANDROID
1531 else if (streq(p[0], "network-change"))
1532 {
1533 bool samenetwork = false;
1534 if (p[1] && streq(p[1], "samenetwork"))
1535 {
1536 samenetwork = true;
1537 }
1538
1539 man_network_change(man, samenetwork);
1540 }
1541#endif
1542 else if (streq(p[0], "load-stats"))
1543 {
1544 man_load_stats(man);
1545 }
1546 else if (streq(p[0], "status"))
1547 {
1548 int version = 0;
1549 if (p[1])
1550 {
1551 version = atoi(p[1]);
1552 }
1553 man_status(man, version, so);
1554 }
1555 else if (streq(p[0], "kill"))
1556 {
1557 if (man_need(man, p, 1, 0))
1558 {
1559 man_kill(man, p[1]);
1560 }
1561 }
1562 else if (streq(p[0], "verb"))
1563 {
1564 if (p[1])
1565 {
1566 const int level = atoi(p[1]);
1567 if (set_debug_level(level, 0))
1568 {
1569 msg(M_CLIENT, "SUCCESS: verb level changed");
1570 }
1571 else
1572 {
1573 msg(M_CLIENT, "ERROR: verb level is out of range");
1574 }
1575 }
1576 else
1577 {
1578 msg(M_CLIENT, "SUCCESS: verb=%u", get_debug_level());
1579 }
1580 }
1581 else if (streq(p[0], "mute"))
1582 {
1583 if (p[1])
1584 {
1585 const int level = atoi(p[1]);
1586 if (set_mute_cutoff(level))
1587 {
1588 msg(M_CLIENT, "SUCCESS: mute level changed");
1589 }
1590 else
1591 {
1592 msg(M_CLIENT, "ERROR: mute level is out of range");
1593 }
1594 }
1595 else
1596 {
1597 msg(M_CLIENT, "SUCCESS: mute=%d", get_mute_cutoff());
1598 }
1599 }
1600 else if (streq(p[0], "auth-retry"))
1601 {
1602 if (p[1])
1603 {
1604 if (auth_retry_set(M_CLIENT, p[1]))
1605 {
1606 msg(M_CLIENT, "SUCCESS: auth-retry parameter changed");
1607 }
1608 else
1609 {
1610 msg(M_CLIENT, "ERROR: bad auth-retry parameter");
1611 }
1612 }
1613 else
1614 {
1615 msg(M_CLIENT, "SUCCESS: auth-retry=%s", auth_retry_print());
1616 }
1617 }
1618 else if (streq(p[0], "state"))
1619 {
1620 if (!p[1])
1621 {
1622 man_state(man, "1");
1623 }
1624 else
1625 {
1626 man_state(man, p[1]);
1627 if (p[2])
1628 {
1629 man_state(man, p[2]);
1630 }
1631 }
1632 }
1633 else if (streq(p[0], "log"))
1634 {
1635 if (man_need(man, p, 1, MN_AT_LEAST))
1636 {
1637 if (p[1])
1638 {
1639 man_log(man, p[1]);
1640 }
1641 if (p[2])
1642 {
1643 man_log(man, p[2]);
1644 }
1645 }
1646 }
1647 else if (streq(p[0], "echo"))
1648 {
1649 if (man_need(man, p, 1, MN_AT_LEAST))
1650 {
1651 if (p[1])
1652 {
1653 man_echo(man, p[1]);
1654 }
1655 if (p[2])
1656 {
1657 man_echo(man, p[2]);
1658 }
1659 }
1660 }
1661 else if (streq(p[0], "username"))
1662 {
1663 if (man_need(man, p, 2, 0))
1664 {
1665 man_query_username(man, p[1], p[2]);
1666 }
1667 }
1668 else if (streq(p[0], "password"))
1669 {
1670 if (man_need(man, p, 1, MN_AT_LEAST))
1671 {
1672 if (p[2])
1673 {
1674 man_query_password(man, p[1], p[2]);
1675 }
1677 {
1678 man_query_password_base64(man, p[1]);
1679 }
1680 else
1681 {
1682 msg(M_CLIENT, "ERROR: the 'password' command requires 2 parameters");
1683 }
1684 }
1685 }
1686 else if (streq(p[0], "forget-passwords"))
1687 {
1689 }
1690 else if (streq(p[0], "needok"))
1691 {
1692 if (man_need(man, p, 2, 0))
1693 {
1694 man_query_need_ok(man, p[1], p[2]);
1695 }
1696 }
1697 else if (streq(p[0], "needstr"))
1698 {
1699 if (man_need(man, p, 2, 0))
1700 {
1701 man_query_need_str(man, p[1], p[2]);
1702 }
1703 }
1704 else if (streq(p[0], "cr-response"))
1705 {
1706 if (man_need(man, p, 1, 0))
1707 {
1708 man_send_cc_message(man, "CR_RESPONSE", p[1]);
1709 }
1710 }
1711 else if (streq(p[0], "net"))
1712 {
1713 man_net(man);
1714 }
1715 else if (streq(p[0], "hold"))
1716 {
1717 man_hold(man, p[1]);
1718 }
1719 else if (streq(p[0], "bytecount"))
1720 {
1721 if (man_need(man, p, 1, 0))
1722 {
1723 man_bytecount(man, atoi(p[1]));
1724 }
1725 }
1726 else if (streq(p[0], "client-kill"))
1727 {
1728 if (man_need(man, p, 1, MN_AT_LEAST))
1729 {
1730 man_client_kill(man, p[1], p[2]);
1731 }
1732 }
1733 else if (streq(p[0], "client-deny"))
1734 {
1735 if (man_need(man, p, 3, MN_AT_LEAST))
1736 {
1737 man_client_deny(man, p[1], p[2], p[3], p[4]);
1738 }
1739 }
1740 else if (streq(p[0], "client-auth-nt"))
1741 {
1742 if (man_need(man, p, 2, 0))
1743 {
1744 man_client_auth(man, p[1], p[2], false);
1745 }
1746 }
1747 else if (streq(p[0], "client-auth"))
1748 {
1749 if (man_need(man, p, 2, 0))
1750 {
1751 man_client_auth(man, p[1], p[2], true);
1752 }
1753 }
1754 else if (streq(p[0], "client-pending-auth"))
1755 {
1756 if (man_need(man, p, 4, 0))
1757 {
1758 man_client_pending_auth(man, p[1], p[2], p[3], p[4]);
1759 }
1760 }
1761 else if (streq(p[0], "rsa-sig"))
1762 {
1763 man_pk_sig(man, "rsa-sig");
1764 }
1765 else if (streq(p[0], "pk-sig"))
1766 {
1767 man_pk_sig(man, "pk-sig");
1768 }
1769 else if (streq(p[0], "certificate"))
1770 {
1771 man_certificate(man);
1772 }
1773#ifdef ENABLE_PKCS11
1774 else if (streq(p[0], "pkcs11-id-count"))
1775 {
1776 man_pkcs11_id_count(man);
1777 }
1778 else if (streq(p[0], "pkcs11-id-get"))
1779 {
1780 if (man_need(man, p, 1, 0))
1781 {
1782 man_pkcs11_id_get(man, atoi(p[1]));
1783 }
1784 }
1785#endif
1786 else if (streq(p[0], "remote-entry-count"))
1787 {
1789 }
1790 else if (streq(p[0], "remote-entry-get"))
1791 {
1792 if (man_need(man, p, 1, MN_AT_LEAST))
1793 {
1794 man_remote_entry_get(man, p[1], p[2]);
1795 }
1796 }
1797 else if (streq(p[0], "proxy"))
1798 {
1799 if (man_need(man, p, 1, MN_AT_LEAST))
1800 {
1801 man_proxy(man, p);
1802 }
1803 }
1804 else if (streq(p[0], "remote"))
1805 {
1806 if (man_need(man, p, 1, MN_AT_LEAST))
1807 {
1808 man_remote(man, p);
1809 }
1810 }
1811 else if (streq(p[0], "push-update-broad"))
1812 {
1813 if (man_need(man, p, 1, 0))
1814 {
1816 }
1817 }
1818 else if (streq(p[0], "push-update-cid"))
1819 {
1820 if (man_need(man, p, 2, 0))
1821 {
1822 man_push_update(man, p, UPT_BY_CID);
1823 }
1824 }
1825#if 1
1826 else if (streq(p[0], "test"))
1827 {
1828 if (man_need(man, p, 1, 0))
1829 {
1830 int i;
1831 const int n = atoi(p[1]);
1832 for (i = 0; i < n; ++i)
1833 {
1834 msg(M_CLIENT,
1835 "[%d] The purpose of this command is to generate large amounts of output.", i);
1836 }
1837 }
1838 }
1839#endif
1840 else
1841 {
1842 msg(M_CLIENT, "ERROR: unknown command [%s], enter 'help' for more options", p[0]);
1843 }
1844
1845done:
1846 gc_free(&gc);
1847}
1848
1849#ifdef _WIN32
1850
1851static void
1853{
1854 switch (man->connection.state)
1855 {
1856 case MS_LISTEN:
1857 net_event_win32_start(&man->connection.ne32, FD_ACCEPT, man->connection.sd_top);
1858 break;
1859
1860 case MS_CC_WAIT_READ:
1861 case MS_CC_WAIT_WRITE:
1862 net_event_win32_start(&man->connection.ne32, FD_READ | FD_WRITE | FD_CLOSE,
1863 man->connection.sd_cli);
1864 break;
1865
1866 default:
1867 ASSERT(0);
1868 }
1869}
1870
1871static void
1873{
1875}
1876
1877#endif /* ifdef _WIN32 */
1878
1879static void
1881{
1882 man->connection.state_realtime = false;
1883 man->connection.log_realtime = false;
1884 man->connection.echo_realtime = false;
1886 man->connection.password_verified = false;
1887 man->connection.password_tries = 0;
1888 man->connection.halt = false;
1890}
1891
1892static void
1893man_new_connection_post(struct management *man, const char *description)
1894{
1895 struct gc_arena gc = gc_new();
1896
1898
1900
1901#ifdef _WIN32
1902 man_start_ne32(man);
1903#endif
1904
1905#if UNIX_SOCK_SUPPORT
1906 if (man->settings.flags & MF_UNIX_SOCK)
1907 {
1908 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description,
1909 sockaddr_unix_name(&man->settings.local_unix, "NULL"));
1910 }
1911 else
1912#endif
1914 {
1915 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description,
1916 print_sockaddr(man->settings.local->ai_addr, &gc));
1917 }
1918 else
1919 {
1920 struct sockaddr_storage addr;
1921 socklen_t addrlen = sizeof(addr);
1922 if (!getpeername(man->connection.sd_cli, (struct sockaddr *)&addr, &addrlen))
1923 {
1924 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description,
1925 print_sockaddr((struct sockaddr *)&addr, &gc));
1926 }
1927 else
1928 {
1929 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description, "unknown");
1930 }
1931 }
1932
1934
1935 if (!man_password_needed(man))
1936 {
1937 man_welcome(man);
1938 }
1939 man_prompt(man);
1941
1942 gc_free(&gc);
1943}
1944
1945#if UNIX_SOCK_SUPPORT
1946static bool
1947man_verify_unix_peer_uid_gid(struct management *man, const socket_descriptor_t sd)
1948{
1949 if (socket_defined(sd) && (man->settings.user.user_valid || man->settings.group.group_valid))
1950 {
1951 static const char err_prefix[] =
1952 "MANAGEMENT: unix domain socket client connection rejected --";
1953 uid_t uid;
1954 gid_t gid;
1955 if (unix_socket_get_peer_uid_gid(man->connection.sd_cli, &uid, &gid))
1956 {
1957 if (man->settings.user.user_valid && man->settings.user.uid != uid)
1958 {
1960 "%s UID of socket peer (%d) doesn't match required value (%d) as given by --management-client-user",
1961 err_prefix, uid, man->settings.user.uid);
1962 return false;
1963 }
1964 if (man->settings.group.group_valid && man->settings.group.gid != gid)
1965 {
1967 "%s GID of socket peer (%d) doesn't match required value (%d) as given by --management-client-group",
1968 err_prefix, gid, man->settings.group.gid);
1969 return false;
1970 }
1971 }
1972 else
1973 {
1974 msg(D_MANAGEMENT, "%s cannot get UID/GID of socket peer", err_prefix);
1975 return false;
1976 }
1977 }
1978 return true;
1979}
1980#endif /* if UNIX_SOCK_SUPPORT */
1981
1982static void
1984{
1985 struct link_socket_actual act;
1986 CLEAR(act);
1987
1988 /*
1989 * Accept the TCP or Unix domain socket client.
1990 */
1991#if UNIX_SOCK_SUPPORT
1992 if (man->settings.flags & MF_UNIX_SOCK)
1993 {
1994 struct sockaddr_un remote;
1995 man->connection.sd_cli = socket_accept_unix(man->connection.sd_top, &remote);
1996 if (!man_verify_unix_peer_uid_gid(man, man->connection.sd_cli))
1997 {
1998 sd_close(&man->connection.sd_cli);
1999 }
2000 }
2001 else
2002#endif
2003 {
2004 man->connection.sd_cli = socket_do_accept(man->connection.sd_top, &act, false);
2005 }
2006
2008 {
2009 man->connection.remote = act.dest;
2010
2012 {
2013#ifdef _WIN32
2014 man_stop_ne32(man);
2015#endif
2016 }
2017
2018 man_new_connection_post(man, "Client connected from");
2019 }
2020}
2021
2022static void
2024{
2025 struct gc_arena gc = gc_new();
2026
2027 /*
2028 * Initialize state
2029 */
2030 man->connection.state = MS_LISTEN;
2032
2033 /*
2034 * Initialize listening socket
2035 */
2037 {
2038#if UNIX_SOCK_SUPPORT
2039 if (man->settings.flags & MF_UNIX_SOCK)
2040 {
2042 man->connection.sd_top = create_socket_unix();
2043 socket_bind_unix(man->connection.sd_top, &man->settings.local_unix, "MANAGEMENT");
2044 }
2045 else
2046#endif
2047 {
2049 socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
2050 "MANAGEMENT", false);
2051 }
2052
2053 /*
2054 * Listen for connection
2055 */
2056 if (listen(man->connection.sd_top, 1))
2057 {
2058 msg(M_ERR, "MANAGEMENT: listen() failed");
2059 }
2060
2061 /*
2062 * Set misc socket properties
2063 */
2065
2066#if UNIX_SOCK_SUPPORT
2067 if (man->settings.flags & MF_UNIX_SOCK)
2068 {
2069 msg(D_MANAGEMENT, "MANAGEMENT: unix domain socket listening on %s",
2070 sockaddr_unix_name(&man->settings.local_unix, "NULL"));
2071 }
2072 else
2073#endif
2074 {
2075 const struct sockaddr *man_addr = man->settings.local->ai_addr;
2076 struct sockaddr_storage addr;
2077 socklen_t addrlen = sizeof(addr);
2078 if (!getsockname(man->connection.sd_top, (struct sockaddr *)&addr, &addrlen))
2079 {
2080 man_addr = (struct sockaddr *)&addr;
2081 }
2082 else
2083 {
2084 msg(M_WARN | M_ERRNO, "Failed to get the management socket address");
2085 }
2086 msg(D_MANAGEMENT, "MANAGEMENT: TCP Socket listening on %s",
2087 print_sockaddr(man_addr, &gc));
2088 }
2089 }
2090
2091#ifdef _WIN32
2092 man_start_ne32(man);
2093#endif
2094
2095 gc_free(&gc);
2096}
2097
2098static void
2100{
2101 struct gc_arena gc = gc_new();
2102 int status;
2103 int signal_received = 0;
2104
2105 /*
2106 * Initialize state
2107 */
2110
2111#if UNIX_SOCK_SUPPORT
2112 if (man->settings.flags & MF_UNIX_SOCK)
2113 {
2114 man->connection.sd_cli = create_socket_unix();
2115 status = socket_connect_unix(man->connection.sd_cli, &man->settings.local_unix);
2116 if (!status && !man_verify_unix_peer_uid_gid(man, man->connection.sd_cli))
2117 {
2118#ifdef EPERM
2119 status = EPERM;
2120#else
2121 status = 1;
2122#endif
2123 sd_close(&man->connection.sd_cli);
2124 }
2125 }
2126 else
2127#endif
2128 {
2130 status = openvpn_connect(man->connection.sd_cli, man->settings.local->ai_addr, 5,
2131 &signal_received);
2132 }
2133
2134 if (signal_received)
2135 {
2136 throw_signal(signal_received);
2137 goto done;
2138 }
2139
2140 if (status)
2141 {
2142#if UNIX_SOCK_SUPPORT
2143 if (man->settings.flags & MF_UNIX_SOCK)
2144 {
2145 msg(D_LINK_ERRORS | M_ERRNO, "MANAGEMENT: connect to unix socket %s failed",
2146 sockaddr_unix_name(&man->settings.local_unix, "NULL"));
2147 }
2148 else
2149#endif
2150 {
2151 msg(D_LINK_ERRORS | M_ERRNO, "MANAGEMENT: connect to %s failed",
2152 print_sockaddr(man->settings.local->ai_addr, &gc));
2153 }
2154 throw_signal_soft(SIGTERM, "management-connect-failed");
2155 goto done;
2156 }
2157
2158 man_new_connection_post(man, "Connected to management server at");
2159
2160done:
2161 gc_free(&gc);
2162}
2163
2164static void
2165man_reset_client_socket(struct management *man, const bool exiting)
2166{
2168 {
2169 man_bytecount_stop(man);
2170#ifdef _WIN32
2171 man_stop_ne32(man);
2172#endif
2179 msg(D_MANAGEMENT, "MANAGEMENT: Client disconnected");
2180 }
2181 if (!exiting)
2182 {
2184 {
2185 msg(D_MANAGEMENT, "MANAGEMENT: Reset authentication on disconnect");
2186 ssl_purge_auth(false);
2187 (void)ssl_clean_auth_token();
2188 }
2189
2190 if (man->settings.flags & MF_SIGNAL && !man_password_needed(man))
2191 {
2192 int mysig = man_mod_signal(man, SIGUSR1);
2193 if (mysig >= 0)
2194 {
2195 msg(D_MANAGEMENT, "MANAGEMENT: Triggering management signal");
2196 throw_signal_soft(mysig, "management-disconnect");
2197 }
2198 }
2199
2201 {
2202 msg(D_MANAGEMENT, "MANAGEMENT: Triggering management exit");
2203 throw_signal_soft(SIGTERM, "management-exit");
2204 }
2205 else
2206 {
2207 man_listen(man);
2208 }
2209 }
2210}
2211
2212static void
2213man_process_command(struct management *man, const char *line)
2214{
2215 struct gc_arena gc = gc_new();
2216 struct status_output *so;
2217 int nparms;
2218 char *parms[MAX_PARMS + 1];
2219
2220 CLEAR(parms);
2221 so = status_open(NULL, 0, -1, &man->persist.vout, 0);
2223
2224 if (man_password_needed(man))
2225 {
2226 man_check_password(man, line);
2227 }
2228 else
2229 {
2230 nparms = parse_line(line, parms, MAX_PARMS, "TCP", 0, M_CLIENT, &gc);
2231 if (parms[0] && streq(parms[0], "password"))
2232 {
2233 msg(D_MANAGEMENT_DEBUG, "MANAGEMENT: CMD 'password [...]'");
2234 }
2235 else if (!streq(line, "load-stats"))
2236 {
2237 msg(D_MANAGEMENT_DEBUG, "MANAGEMENT: CMD '%s'", line);
2238 }
2239
2240#if 0
2241 /* DEBUGGING -- print args */
2242 {
2243 int i;
2244 for (i = 0; i < nparms; ++i)
2245 {
2246 msg(M_INFO, "[%d] '%s'", i, parms[i]);
2247 }
2248 }
2249#endif
2250
2251 if (nparms > 0)
2252 {
2253 man_dispatch_command(man, so, (const char **)parms, nparms);
2254 }
2255 }
2256
2257 CLEAR(parms);
2258 status_close(so);
2259 gc_free(&gc);
2260}
2261
2262static bool
2263man_io_error(struct management *man, const char *prefix)
2264{
2265 bool crt_error = false;
2266 int err = openvpn_errno_maybe_crt(&crt_error);
2267
2268 if (!ignore_sys_error(err, crt_error))
2269 {
2270 struct gc_arena gc = gc_new();
2271 msg(D_MANAGEMENT, "MANAGEMENT: TCP %s error: %s", prefix, strerror(err));
2272 gc_free(&gc);
2273 return true;
2274 }
2275 else
2276 {
2277 return false;
2278 }
2279}
2280
2281#ifdef TARGET_ANDROID
2282static ssize_t
2283man_send_with_fd(int fd, void *ptr, size_t nbytes, int flags, int sendfd)
2284{
2285 struct msghdr msg = { 0 };
2286 struct iovec iov[1];
2287
2288 union
2289 {
2290 struct cmsghdr cm;
2291 char control[CMSG_SPACE(sizeof(int))];
2292 } control_un;
2293 struct cmsghdr *cmptr;
2294
2295 msg.msg_control = control_un.control;
2296 msg.msg_controllen = sizeof(control_un.control);
2297
2298 cmptr = CMSG_FIRSTHDR(&msg);
2299 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
2300 cmptr->cmsg_level = SOL_SOCKET;
2301 cmptr->cmsg_type = SCM_RIGHTS;
2302 *((int *)CMSG_DATA(cmptr)) = sendfd;
2303
2304 msg.msg_name = NULL;
2305 msg.msg_namelen = 0;
2306
2307 iov[0].iov_base = ptr;
2308 iov[0].iov_len = nbytes;
2309 msg.msg_iov = iov;
2310 msg.msg_iovlen = 1;
2311
2312 return (sendmsg(fd, &msg, flags));
2313}
2314
2315static ssize_t
2316man_recv_with_fd(int fd, void *ptr, size_t nbytes, int flags, int *recvfd)
2317{
2318 struct msghdr msghdr = { 0 };
2319 struct iovec iov[1];
2320 ssize_t n;
2321
2322 union
2323 {
2324 struct cmsghdr cm;
2325 char control[CMSG_SPACE(sizeof(int))];
2326 } control_un;
2327 struct cmsghdr *cmptr;
2328
2329 msghdr.msg_control = control_un.control;
2330 msghdr.msg_controllen = sizeof(control_un.control);
2331
2332 msghdr.msg_name = NULL;
2333 msghdr.msg_namelen = 0;
2334
2335 iov[0].iov_base = ptr;
2336 iov[0].iov_len = nbytes;
2337 msghdr.msg_iov = iov;
2338 msghdr.msg_iovlen = 1;
2339
2340 if ((n = recvmsg(fd, &msghdr, flags)) <= 0)
2341 {
2342 return (n);
2343 }
2344
2345 if ((cmptr = CMSG_FIRSTHDR(&msghdr)) != NULL && cmptr->cmsg_len == CMSG_LEN(sizeof(int)))
2346 {
2347 if (cmptr->cmsg_level != SOL_SOCKET)
2348 {
2349 msg(M_ERR, "control level != SOL_SOCKET");
2350 }
2351 if (cmptr->cmsg_type != SCM_RIGHTS)
2352 {
2353 msg(M_ERR, "control type != SCM_RIGHTS");
2354 }
2355 *recvfd = *((int *)CMSG_DATA(cmptr));
2356 }
2357 else
2358 {
2359 *recvfd = -1; /* descriptor was not passed */
2360 }
2361 return (n);
2362}
2363
2364/*
2365 * The android control method will instruct the GUI part of openvpn to do
2366 * the route/ifconfig/open tun command. See doc/android.txt for details.
2367 */
2368bool
2369management_android_control(struct management *man, const char *command, const char *msg)
2370{
2371 if (!man)
2372 {
2373 msg(M_FATAL, "Required management interface not available.");
2374 }
2375 struct user_pass up;
2376 CLEAR(up);
2377 strncpy(up.username, msg, sizeof(up.username) - 1);
2378
2380 return strcmp("ok", up.password) == 0;
2381}
2382
2383/*
2384 * In Android 4.4 it is not possible to open a new tun device and then close the
2385 * old tun device without breaking the whole VPNService stack until the device
2386 * is rebooted. This management method ask the UI what method should be taken to
2387 * ensure the optimal solution for the situation
2388 */
2389int
2390managment_android_persisttun_action(struct management *man)
2391{
2392 struct user_pass up;
2393 CLEAR(up);
2394 strcpy(up.username, "tunmethod");
2396 (void *)0);
2397 if (!strcmp("NOACTION", up.password))
2398 {
2399 return ANDROID_KEEP_OLD_TUN;
2400 }
2401 else if (!strcmp("OPEN_BEFORE_CLOSE", up.password))
2402 {
2403 return ANDROID_OPEN_BEFORE_CLOSE;
2404 }
2405 else
2406 {
2407 msg(M_ERR, "Got unrecognised '%s' from management for PERSIST_TUN_ACTION query",
2408 up.password);
2409 }
2410
2411 ASSERT(0);
2412 return ANDROID_OPEN_BEFORE_CLOSE;
2413}
2414
2415
2416#endif /* ifdef TARGET_ANDROID */
2417
2418static ssize_t
2420{
2421 /*
2422 * read command line from socket
2423 */
2424 unsigned char buf[256];
2425 ssize_t len = 0;
2426
2427#ifdef TARGET_ANDROID
2428 int fd;
2429 len = man_recv_with_fd(man->connection.sd_cli, buf, sizeof(buf), MSG_NOSIGNAL, &fd);
2430 if (fd >= 0)
2431 {
2432 man->connection.lastfdreceived = fd;
2433 }
2434#else /* ifdef TARGET_ANDROID */
2435 len = recv(man->connection.sd_cli, (void *)buf, sizeof(buf), MSG_NOSIGNAL);
2436#endif
2437
2438 if (len == 0)
2439 {
2440 man_reset_client_socket(man, false);
2441 }
2442 else if (len > 0)
2443 {
2444 bool processed_command = false;
2445
2446 ASSERT(len <= (ssize_t)sizeof(buf));
2447 command_line_add(man->connection.in, buf, (size_t)len);
2448
2449 /*
2450 * Reset output object
2451 */
2453
2454 /*
2455 * process command line if complete
2456 */
2457 {
2458 const char *line;
2459 while ((line = command_line_get(man->connection.in)))
2460 {
2461 if (man->connection.in_extra)
2462 {
2463 if (!strcmp(line, "END"))
2464 {
2465 in_extra_dispatch(man);
2466 }
2467 else
2468 {
2470 }
2471 }
2472 else
2473 {
2474 man_process_command(man, (char *)line);
2475 }
2476 if (man->connection.halt)
2477 {
2478 break;
2479 }
2481 processed_command = true;
2482 }
2483 }
2484
2485 /*
2486 * Reset output state to MS_CC_WAIT_(READ|WRITE)
2487 */
2488 if (man->connection.halt)
2489 {
2490 man_reset_client_socket(man, false);
2491 len = 0;
2492 }
2493 else
2494 {
2495 if (processed_command)
2496 {
2497 man_prompt(man);
2498 }
2500 }
2501 }
2502 else /* len < 0 */
2503 {
2504 if (man_io_error(man, "recv"))
2505 {
2506 man_reset_client_socket(man, false);
2507 }
2508 }
2509 return len;
2510}
2511
2512static ssize_t
2514{
2515 const int size_hint = 1024;
2516 ssize_t sent = 0;
2517 const struct buffer *buf;
2518
2519 buffer_list_aggregate(man->connection.out, size_hint);
2520 buf = buffer_list_peek(man->connection.out);
2521 if (buf && BLEN(buf))
2522 {
2523 const int len = min_int(size_hint, BLEN(buf));
2524#ifdef TARGET_ANDROID
2525 if (man->connection.fdtosend > 0)
2526 {
2527 sent = man_send_with_fd(man->connection.sd_cli, BPTR(buf), len, MSG_NOSIGNAL,
2528 man->connection.fdtosend);
2529 man->connection.fdtosend = -1;
2530 }
2531 else
2532#endif
2533 {
2534 sent = send(man->connection.sd_cli, (const void *)BPTR(buf), len, MSG_NOSIGNAL);
2535 }
2536 if (sent >= 0)
2537 {
2538 buffer_list_advance(man->connection.out, sent);
2539 }
2540 else
2541 {
2542 if (man_io_error(man, "send"))
2543 {
2545 }
2546 }
2547 }
2548
2549 /*
2550 * Reset output state to MS_CC_WAIT_(READ|WRITE)
2551 */
2553
2554 return sent;
2555}
2556
2557static void
2559{
2560 CLEAR(*mc);
2561
2562 /* set initial state */
2563 mc->state = MS_INITIAL;
2564
2565 /* clear socket descriptors */
2566 mc->sd_top = SOCKET_UNDEFINED;
2567 mc->sd_cli = SOCKET_UNDEFINED;
2568}
2569
2570static void
2571man_persist_init(struct management *man, const int log_history_cache, const int echo_buffer_size,
2572 const int state_buffer_size)
2573{
2574 struct man_persist *mp = &man->persist;
2575 if (!mp->defined)
2576 {
2577 CLEAR(*mp);
2578
2579 /* initialize log history store */
2580 mp->log = log_history_init(log_history_cache);
2581
2582 /*
2583 * Initialize virtual output object, so that functions
2584 * which write to a virtual_output object can be redirected
2585 * here to the management object.
2586 */
2588 mp->vout.arg = man;
2591
2592 /*
2593 * Initialize --echo list
2594 */
2595 man->persist.echo = log_history_init(echo_buffer_size);
2596
2597 /*
2598 * Initialize --state list
2599 */
2600 man->persist.state = log_history_init(state_buffer_size);
2601
2602 mp->defined = true;
2603 }
2604}
2605
2606static void
2608{
2609 if (mp->log)
2610 {
2613 }
2614
2615 if (mp->echo)
2616 {
2618 }
2619
2620 if (mp->state)
2621 {
2623 }
2624
2625 CLEAR(*mp);
2626}
2627
2628static void
2629man_settings_init(struct man_settings *ms, const char *addr, const char *port,
2630 const char *pass_file, const char *client_user, const char *client_group,
2631 const int log_history_cache, const int echo_buffer_size,
2632 const int state_buffer_size, const int remap_sigusr1, const unsigned int flags)
2633{
2634 if (!ms->defined)
2635 {
2636 CLEAR(*ms);
2637
2638 ms->flags = flags;
2639
2640 /*
2641 * Get username/password
2642 */
2643 if (pass_file)
2644 {
2645 get_user_pass(&ms->up, pass_file, "Management", GET_USER_PASS_PASSWORD_ONLY);
2646 }
2647
2648#if UNIX_SOCK_SUPPORT
2649 /*
2650 * lookup client UID/GID if specified
2651 */
2652 if (client_user)
2653 {
2654 ASSERT(platform_user_get(client_user, &ms->user));
2655 msg(D_MANAGEMENT, "MANAGEMENT: client_uid=%d", ms->user.uid);
2656 }
2657 if (client_group)
2658 {
2659 ASSERT(platform_group_get(client_group, &ms->group));
2660 msg(D_MANAGEMENT, "MANAGEMENT: client_gid=%d", ms->group.gid);
2661 }
2662
2663 if (ms->flags & MF_UNIX_SOCK)
2664 {
2665 sockaddr_unix_init(&ms->local_unix, addr);
2666 }
2667 else
2668#endif
2669 {
2670 /*
2671 * Run management over tunnel, or
2672 * separate channel?
2673 */
2674 if (streq(addr, "tunnel") && !(flags & MF_CONNECT_AS_CLIENT))
2675 {
2676 ms->management_over_tunnel = true;
2677 }
2678 else
2679 {
2680 int status;
2682
2683 if (!(flags & MF_CONNECT_AS_CLIENT))
2684 {
2685 resolve_flags |= GETADDR_PASSIVE;
2686 }
2687
2688 status =
2689 openvpn_getaddrinfo(resolve_flags, addr, port, 0, NULL, AF_UNSPEC, &ms->local);
2690 ASSERT(status == 0);
2691 }
2692 }
2693
2694 /*
2695 * Log history and echo buffer may need to be resized
2696 */
2697 ms->log_history_cache = log_history_cache;
2698 ms->echo_buffer_size = echo_buffer_size;
2699 ms->state_buffer_size = state_buffer_size;
2700
2701 /*
2702 * Set remap sigusr1 flags
2703 */
2704 if (remap_sigusr1 == SIGHUP)
2705 {
2707 }
2708 else if (remap_sigusr1 == SIGTERM)
2709 {
2711 }
2712
2713 ms->defined = true;
2714 }
2715}
2716
2717static void
2719{
2720 if (ms->local)
2721 {
2722 freeaddrinfo(ms->local);
2723 }
2724 CLEAR(*ms);
2725}
2726
2727
2728static void
2730{
2731 if (man->connection.state == MS_INITIAL)
2732 {
2733#ifdef _WIN32
2734 /*
2735 * This object is a sort of TCP/IP helper
2736 * for Windows.
2737 */
2739#endif
2740
2741 /*
2742 * Allocate helper objects for command line input and
2743 * command output from/to the socket.
2744 */
2745 man->connection.in = command_line_new(1024);
2747
2748 /*
2749 * Initialize event set for standalone usage, when we are
2750 * running outside of the primary event loop.
2751 */
2752 {
2753 int maxevents = 1;
2754 man->connection.es = event_set_init(&maxevents, EVENT_METHOD_FAST);
2755 }
2756
2757 man->connection.client_version = MCV_DEFAULT; /* default version */
2758
2759 /*
2760 * Listen/connect socket
2761 */
2763 {
2764 man_connect(man);
2765 }
2766 else
2767 {
2768 man_listen(man);
2769 }
2770 }
2771}
2772
2773static void
2775{
2776 struct man_connection *mc = &man->connection;
2777
2778 event_free(mc->es);
2779#ifdef _WIN32
2781#endif
2782 if (socket_defined(mc->sd_top))
2783 {
2784 man_close_socket(man, mc->sd_top);
2786 }
2787 if (socket_defined(mc->sd_cli))
2788 {
2789 man_close_socket(man, mc->sd_cli);
2790 }
2791
2792 command_line_free(mc->in);
2793 buffer_list_free(mc->out);
2794
2796
2800}
2801
2802struct management *
2804{
2805 struct management *man;
2806 ALLOC_OBJ_CLEAR(man, struct management);
2807
2810
2812
2813 return man;
2814}
2815
2816bool
2817management_open(struct management *man, const char *addr, const char *port, const char *pass_file,
2818 const char *client_user, const char *client_group, const int log_history_cache,
2819 const int echo_buffer_size, const int state_buffer_size, const int remap_sigusr1,
2820 const unsigned int flags)
2821{
2822 bool ret = false;
2823
2824 /*
2825 * Save the settings only if they have not
2826 * been saved before.
2827 */
2828 man_settings_init(&man->settings, addr, port, pass_file, client_user, client_group,
2829 log_history_cache, echo_buffer_size, state_buffer_size, remap_sigusr1, flags);
2830
2831 /*
2832 * The log is initially sized to MANAGEMENT_LOG_HISTORY_INITIAL_SIZE,
2833 * but may be changed here. Ditto for echo and state buffers.
2834 */
2838
2839 /*
2840 * If connection object is uninitialized and we are not doing
2841 * over-the-tunnel management, then open (listening) connection.
2842 */
2843 if (man->connection.state == MS_INITIAL)
2844 {
2846 {
2848 ret = true;
2849 }
2850 }
2851
2852 return ret;
2853}
2854
2855void
2857{
2858 man_output_list_push_finalize(man); /* flush output queue */
2862 free(man);
2863}
2864
2865void
2867{
2868 man->persist.standalone_disabled = true;
2869 man->persist.callback = *cb;
2870}
2871
2872void
2874{
2875 man->persist.standalone_disabled = false;
2876 man->persist.hold_release = false;
2877 CLEAR(man->persist.callback);
2878 man_output_list_push_finalize(man); /* flush output queue */
2879}
2880
2881void
2882management_set_state(struct management *man, const int state, const char *detail,
2883 const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6,
2884 const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
2885{
2886 if (man->persist.state
2887 && (!(man->settings.flags & MF_SERVER) || state < OPENVPN_STATE_CLIENT_BASE))
2888 {
2889 struct gc_arena gc = gc_new();
2890 struct log_entry e;
2891 const char *out = NULL;
2892
2893 update_time();
2894 CLEAR(e);
2895 e.timestamp = now;
2896 e.u.state = state;
2897 e.string = detail;
2898 if (tun_local_ip)
2899 {
2900 e.local_ip = *tun_local_ip;
2901 }
2902 if (tun_local_ip6)
2903 {
2904 e.local_ip6 = *tun_local_ip6;
2905 }
2906 if (local)
2907 {
2908 e.local_sock = *local;
2909 }
2910 if (remote)
2911 {
2912 e.remote_sock = *remote;
2913 }
2914
2915 log_history_add(man->persist.state, &e);
2916
2917 if (man->connection.state_realtime)
2918 {
2919 out = log_entry_print(&e,
2923 &gc);
2924 }
2925
2926 if (out)
2927 {
2928 man_output_list_push(man, out);
2929 }
2930
2931 gc_free(&gc);
2932 }
2933}
2934
2935static bool
2936env_filter_match(const char *env_str, const int env_filter_level)
2937{
2938 static const char *env_names[] = { "username=",
2939 "password=",
2940 "X509_0_CN=",
2941 "tls_serial_",
2942 "untrusted_ip=",
2943 "ifconfig_local=",
2944 "ifconfig_netmask=",
2945 "daemon_start_time=",
2946 "daemon_pid=",
2947 "dev=",
2948 "ifconfig_pool_remote_ip=",
2949 "ifconfig_pool_netmask=",
2950 "time_duration=",
2951 "bytes_sent=",
2952 "bytes_received=",
2953 "session_id=",
2954 "session_state=" };
2955
2956 if (env_filter_level == 0)
2957 {
2958 return true;
2959 }
2960 else if (env_filter_level <= 1 && !strncmp(env_str, "X509_", 5))
2961 {
2962 return true;
2963 }
2964 else if (env_filter_level <= 2)
2965 {
2966 size_t i;
2967 for (i = 0; i < SIZE(env_names); ++i)
2968 {
2969 const char *en = env_names[i];
2970 const size_t len = strlen(en);
2971 if (!strncmp(env_str, en, len))
2972 {
2973 return true;
2974 }
2975 }
2976 return false;
2977 }
2978 return false;
2979}
2980
2981static void
2982man_output_env(const struct env_set *es, const bool tail, const int env_filter_level,
2983 const char *prefix)
2984{
2985 if (es)
2986 {
2987 struct env_item *e;
2988 for (e = es->list; e != NULL; e = e->next)
2989 {
2990 if (e->string && (!env_filter_level || env_filter_match(e->string, env_filter_level)))
2991 {
2992 msg(M_CLIENT, ">%s:ENV,%s", prefix, e->string);
2993 }
2994 }
2995 }
2996 if (tail)
2997 {
2998 msg(M_CLIENT, ">%s:ENV,END", prefix);
2999 }
3000}
3001
3002static void
3003man_output_extra_env(struct management *man, const char *prefix)
3004{
3005 struct gc_arena gc = gc_new();
3006 struct env_set *es = env_set_create(&gc);
3007 if (man->persist.callback.n_clients)
3008 {
3009 const int nclients = (*man->persist.callback.n_clients)(man->persist.callback.arg);
3010 setenv_int(es, "n_clients", nclients);
3011 }
3012 man_output_env(es, false, man->connection.env_filter_level, prefix);
3013 gc_free(&gc);
3014}
3015
3016void
3017management_up_down(struct management *man, const char *updown, const struct env_set *es)
3018{
3019 if (man->settings.flags & MF_UP_DOWN)
3020 {
3021 msg(M_CLIENT, ">UPDOWN:%s", updown);
3022 man_output_env(es, true, 0, "UPDOWN");
3023 }
3024}
3025
3026void
3027management_notify(struct management *man, const char *severity, const char *type, const char *text)
3028{
3029 msg(M_CLIENT, ">NOTIFY:%s,%s,%s", severity, type, text);
3030}
3031
3032void
3033management_notify_generic(struct management *man, const char *str)
3034{
3035 msg(M_CLIENT, "%s", str);
3036}
3037
3038static void
3040{
3041 char line[256];
3043 {
3044 const char *peer_info =
3045 (*man->persist.callback.get_peer_info)(man->persist.callback.arg, mdac->cid);
3046 if (peer_info)
3047 {
3048 struct buffer buf;
3049 buf_set_read(&buf, (const uint8_t *)peer_info, strlen(peer_info));
3050 while (buf_parse(&buf, '\n', line, sizeof(line)))
3051 {
3052 chomp(line);
3054 {
3055 msg(M_CLIENT, ">CLIENT:ENV,%s", line);
3056 }
3057 else
3058 {
3059 msg(D_MANAGEMENT, "validation failed on peer_info line received from client");
3060 }
3061 }
3062 }
3063 }
3064}
3065
3066void
3067management_notify_client_needing_auth(struct management *management, const unsigned int mda_key_id,
3068 struct man_def_auth_context *mdac, const struct env_set *es)
3069{
3070 if (!(mdac->flags & DAF_CONNECTION_CLOSED))
3071 {
3072 const char *mode = "CONNECT";
3073 if (mdac->flags & DAF_CONNECTION_ESTABLISHED)
3074 {
3075 mode = "REAUTH";
3076 }
3077 msg(M_CLIENT, ">CLIENT:%s,%lu,%u", mode, mdac->cid, mda_key_id);
3080 {
3082 }
3084 mdac->flags |= DAF_INITIAL_AUTH;
3085 }
3086}
3087
3088void
3090 const struct env_set *es, const char *response)
3091{
3092 struct gc_arena gc;
3093 if (management)
3094 {
3095 gc = gc_new();
3096
3097 msg(M_CLIENT, ">CLIENT:CR_RESPONSE,%lu,%u,%s", mdac->cid, mda_key_id, response);
3100 {
3102 }
3104 gc_free(&gc);
3105 }
3106}
3107
3108void
3110 const struct env_set *es)
3111{
3113 msg(M_CLIENT, ">CLIENT:ESTABLISHED,%lu", mdac->cid);
3116}
3117
3118void
3120 const struct env_set *es)
3121{
3122 if ((mdac->flags & DAF_INITIAL_AUTH) && !(mdac->flags & DAF_CONNECTION_CLOSED))
3123 {
3124 msg(M_CLIENT, ">CLIENT:DISCONNECT,%lu", mdac->cid);
3127 }
3128}
3129
3130void
3132 const struct mroute_addr *addr, const bool primary)
3133{
3134 struct gc_arena gc = gc_new();
3135 if ((mdac->flags & DAF_INITIAL_AUTH) && !(mdac->flags & DAF_CONNECTION_CLOSED))
3136 {
3137 msg(M_CLIENT, ">CLIENT:ADDRESS,%lu,%s,%d", mdac->cid,
3138 mroute_addr_print_ex(addr, MAPF_SUBNET, &gc), BOOL_CAST(primary));
3139 }
3140 gc_free(&gc);
3141}
3142
3143void
3144management_echo(struct management *man, const char *string, const bool pull)
3145{
3146 if (man->persist.echo)
3147 {
3148 struct gc_arena gc = gc_new();
3149 struct log_entry e;
3150 const char *out = NULL;
3151
3152 update_time();
3153 CLEAR(e);
3154 e.timestamp = now;
3155 e.string = string;
3156 e.u.intval = BOOL_CAST(pull);
3157
3158 log_history_add(man->persist.echo, &e);
3159
3160 if (man->connection.echo_realtime)
3161 {
3162 out = log_entry_print(&e,
3165 &gc);
3166 }
3167
3168 if (out)
3169 {
3170 man_output_list_push(man, out);
3171 }
3172
3173 gc_free(&gc);
3174 }
3175}
3176
3177void
3178management_post_tunnel_open(struct management *man, const in_addr_t tun_local_ip)
3179{
3180 /*
3181 * If we are running management over the tunnel,
3182 * this is the place to initialize the connection.
3183 */
3185 {
3186 /* listen on our local TUN/TAP IP address */
3187 struct in_addr ia;
3188 int ret;
3189 char buf[INET_ADDRSTRLEN];
3190
3191 ia.s_addr = htonl(tun_local_ip);
3192 inet_ntop(AF_INET, &ia, buf, sizeof(buf));
3193 ret =
3194 openvpn_getaddrinfo(GETADDR_PASSIVE, buf, NULL, 0, NULL, AF_INET, &man->settings.local);
3195 ASSERT(ret == 0);
3197 }
3198}
3199
3200void
3202{
3204 {
3206 }
3207}
3208
3209void
3210management_auth_failure(struct management *man, const char *type, const char *reason)
3211{
3212 if (reason)
3213 {
3214 msg(M_CLIENT, ">PASSWORD:Verification Failed: '%s' ['%s']", type, reason);
3215 }
3216 else
3217 {
3218 msg(M_CLIENT, ">PASSWORD:Verification Failed: '%s'", type);
3219 }
3220}
3221
3222void
3223management_auth_token(struct management *man, const char *token)
3224{
3225 msg(M_CLIENT, ">PASSWORD:Auth-Token:%s", token);
3226}
3227
3228static inline bool
3229man_persist_state(unsigned int *persistent, const int n)
3230{
3231 if (persistent)
3232 {
3233 if (*persistent == (unsigned int)n)
3234 {
3235 return false;
3236 }
3237 *persistent = n;
3238 }
3239 return true;
3240}
3241
3242#ifdef _WIN32
3243
3244void
3245management_socket_set(struct management *man, struct event_set *es, void *arg,
3246 unsigned int *persistent)
3247{
3248 if (man->connection.state != MS_INITIAL)
3249 {
3252
3253 switch (man->connection.state)
3254 {
3255 case MS_LISTEN:
3256 if (man_persist_state(persistent, 1))
3257 {
3258 event_ctl(es, ev, EVENT_READ, arg);
3259 }
3260 break;
3261
3262 case MS_CC_WAIT_READ:
3263 if (man_persist_state(persistent, 2))
3264 {
3265 event_ctl(es, ev, EVENT_READ, arg);
3266 }
3267 break;
3268
3269 case MS_CC_WAIT_WRITE:
3270 if (man_persist_state(persistent, 3))
3271 {
3272 event_ctl(es, ev, EVENT_READ | EVENT_WRITE, arg);
3273 }
3274 break;
3275
3276 default:
3277 ASSERT(0);
3278 }
3279 }
3280}
3281
3282void
3284{
3285 if (man->connection.state != MS_INITIAL)
3286 {
3287 long net_events;
3290
3291 if (net_events & FD_CLOSE)
3292 {
3293 man_reset_client_socket(man, false);
3294 }
3295 else
3296 {
3297 if (man->connection.state == MS_LISTEN)
3298 {
3299 if (net_events & FD_ACCEPT)
3300 {
3301 man_accept(man);
3303 }
3304 }
3305 else if (man->connection.state == MS_CC_WAIT_READ
3307 {
3308 if (net_events & FD_READ)
3309 {
3310 while (man_read(man) > 0)
3311 {
3312 }
3314 }
3315
3316 if (net_events & FD_WRITE)
3317 {
3318 ssize_t status = man_write(man);
3319 if (status < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
3320 {
3322 }
3323 }
3324 }
3325 }
3326 }
3327}
3328
3329#else /* ifdef _WIN32 */
3330
3331void
3332management_socket_set(struct management *man, struct event_set *es, void *arg,
3333 unsigned int *persistent)
3334{
3335 switch (man->connection.state)
3336 {
3337 case MS_LISTEN:
3338 if (man_persist_state(persistent, 1))
3339 {
3341 }
3342 break;
3343
3344 case MS_CC_WAIT_READ:
3345 if (man_persist_state(persistent, 2))
3346 {
3348 }
3349 break;
3350
3351 case MS_CC_WAIT_WRITE:
3352 if (man_persist_state(persistent, 3))
3353 {
3355 }
3356 break;
3357
3358 case MS_INITIAL:
3359 break;
3360
3361 default:
3362 ASSERT(0);
3363 }
3364}
3365
3366void
3367management_io(struct management *man)
3368{
3369 switch (man->connection.state)
3370 {
3371 case MS_LISTEN:
3372 man_accept(man);
3373 break;
3374
3375 case MS_CC_WAIT_READ:
3376 man_read(man);
3377 break;
3378
3379 case MS_CC_WAIT_WRITE:
3380 man_write(man);
3381 break;
3382
3383 case MS_INITIAL:
3384 break;
3385
3386 default:
3387 ASSERT(0);
3388 }
3389}
3390
3391#endif /* ifdef _WIN32 */
3392
3393static inline bool
3395{
3397}
3398
3399static bool
3400man_check_for_signals(volatile int *signal_received)
3401{
3402 if (signal_received)
3403 {
3404 get_signal(signal_received);
3405 if (*signal_received)
3406 {
3407 return true;
3408 }
3409 }
3410 return false;
3411}
3412
3413/*
3414 * Wait for socket I/O when outside primary event loop
3415 */
3416static int
3417man_block(struct management *man, volatile int *signal_received, const time_t expire)
3418{
3419 struct timeval tv;
3420 struct event_set_return esr;
3421 int status = -1;
3422
3423 if (man_standalone_ok(man))
3424 {
3425 /* expire time can be already overdue, for this case init zero
3426 * timeout to avoid waiting first time and exit loop early with
3427 * either obtained event or timeout.
3428 */
3429 tv.tv_usec = 0;
3430 tv.tv_sec = 0;
3431
3432 while (true)
3433 {
3435 management_socket_set(man, man->connection.es, NULL, NULL);
3436 if (man_check_for_signals(signal_received))
3437 {
3438 status = -1;
3439 break;
3440 }
3441 status = event_wait(man->connection.es, &tv, &esr, 1);
3442 update_time();
3443 if (man_check_for_signals(signal_received))
3444 {
3445 status = -1;
3446 break;
3447 }
3448
3449 if (status > 0)
3450 {
3451 break;
3452 }
3453 else if (expire && now >= expire)
3454 {
3455 /* set SIGINT signal if expiration time exceeded */
3456 status = 0;
3457 if (signal_received)
3458 {
3459 *signal_received = SIGINT;
3460 }
3461 break;
3462 }
3463
3464 /* wait one second more */
3465 tv.tv_sec = 1;
3466 tv.tv_usec = 0;
3467 }
3468 }
3469 return status;
3470}
3471
3472/*
3473 * Perform management socket output outside primary event loop
3474 */
3475static void
3476man_output_standalone(struct management *man, volatile int *signal_received)
3477{
3478 if (man_standalone_ok(man))
3479 {
3480 while (man->connection.state == MS_CC_WAIT_WRITE)
3481 {
3482 management_io(man);
3483 if (man->connection.state == MS_CC_WAIT_WRITE)
3484 {
3485 man_block(man, signal_received, 0);
3486 }
3487 if (signal_received && *signal_received)
3488 {
3489 break;
3490 }
3491 }
3492 }
3493}
3494
3495/*
3496 * Process management event loop outside primary event loop
3497 */
3498static int
3499man_standalone_event_loop(struct management *man, volatile int *signal_received,
3500 const time_t expire)
3501{
3502 int status = -1;
3503 if (man_standalone_ok(man))
3504 {
3505 status = man_block(man, signal_received, expire);
3506 if (status > 0)
3507 {
3508 management_io(man);
3509 }
3510 }
3511 return status;
3512}
3513
3514#define MWCC_PASSWORD_WAIT (1 << 0)
3515#define MWCC_HOLD_WAIT (1 << 1)
3516#define MWCC_OTHER_WAIT (1 << 2)
3517
3518/*
3519 * Block until client connects
3520 */
3521static void
3522man_wait_for_client_connection(struct management *man, volatile int *signal_received,
3523 const time_t expire, unsigned int flags)
3524{
3526 if (man->connection.state == MS_LISTEN)
3527 {
3528 if (flags & MWCC_PASSWORD_WAIT)
3529 {
3530 msg(D_MANAGEMENT, "Need password(s) from management interface, waiting...");
3531 }
3532 if (flags & MWCC_HOLD_WAIT)
3533 {
3534 msg(D_MANAGEMENT, "Need hold release from management interface, waiting...");
3535 }
3536 if (flags & MWCC_OTHER_WAIT)
3537 {
3538 msg(D_MANAGEMENT, "Need information from management interface, waiting...");
3539 }
3540 do
3541 {
3542 man_standalone_event_loop(man, signal_received, expire);
3543 if (signal_received && *signal_received)
3544 {
3545 break;
3546 }
3547 } while (man->connection.state == MS_LISTEN || man_password_needed(man));
3548 }
3549}
3550
3551/*
3552 * Process the management event loop for sec seconds
3553 */
3554void
3556{
3557 if (man_standalone_ok(man))
3558 {
3559 volatile int signal_received = 0;
3560 const bool standalone_disabled_save = man->persist.standalone_disabled;
3561 time_t expire = 0;
3562
3563 /* This is so M_CLIENT messages will be correctly passed through msg() */
3564 man->persist.standalone_disabled = false;
3565
3566 /* set expire time */
3567 update_time();
3568 if (sec >= 0)
3569 {
3570 expire = now + sec;
3571 }
3572
3573 /* if no client connection, wait for one */
3574 man_wait_for_client_connection(man, &signal_received, expire, 0);
3575 if (signal_received)
3576 {
3577 return;
3578 }
3579
3580 /* run command processing event loop */
3581 do
3582 {
3583 man_standalone_event_loop(man, &signal_received, expire);
3584 if (!signal_received)
3585 {
3586 man_check_for_signals(&signal_received);
3587 }
3588 if (signal_received)
3589 {
3590 return;
3591 }
3592 update_time();
3593 } while (expire && expire > now);
3594
3595 /* revert state */
3596 man->persist.standalone_disabled = standalone_disabled_save;
3597 }
3598 else if (sec > 0)
3599 {
3600 sleep(sec);
3601 }
3602}
3603
3604/*
3605 * Get a username/password from management channel in standalone mode.
3606 */
3607bool
3608management_query_user_pass(struct management *man, struct user_pass *up, const char *type,
3609 const unsigned int flags, const char *static_challenge)
3610{
3611 struct gc_arena gc = gc_new();
3612 bool ret = false;
3613
3614 if (man_standalone_ok(man))
3615 {
3616 volatile int signal_received = 0;
3617 const bool standalone_disabled_save = man->persist.standalone_disabled;
3618 struct buffer alert_msg = alloc_buf_gc(128, &gc);
3619 const char *alert_type = NULL;
3620 const char *prefix = NULL;
3621 unsigned int up_query_mode = 0;
3622 const char *sc = NULL;
3623 ret = true;
3624 /* This is so M_CLIENT messages will be correctly passed through msg() */
3625 man->persist.standalone_disabled = false;
3626 man->persist.special_state_msg = NULL;
3627
3628 CLEAR(man->connection.up_query);
3629
3630 if (flags & GET_USER_PASS_NEED_OK)
3631 {
3632 up_query_mode = UP_QUERY_NEED_OK;
3633 prefix = "NEED-OK";
3634 alert_type = "confirmation";
3635 }
3636 else if (flags & GET_USER_PASS_NEED_STR)
3637 {
3638 up_query_mode = UP_QUERY_NEED_STR;
3639 prefix = "NEED-STR";
3640 alert_type = "string";
3641 }
3642 else if (flags & GET_USER_PASS_PASSWORD_ONLY)
3643 {
3644 up_query_mode = UP_QUERY_PASS;
3645 prefix = "PASSWORD";
3646 alert_type = "password";
3647 }
3648 else if ((man->connection.client_version >= MCV_USERNAME_ONLY) && (flags & GET_USER_PASS_USERNAME_ONLY))
3649 {
3650 up_query_mode = UP_QUERY_USERNAME;
3651 prefix = "PASSWORD";
3652 alert_type = "username";
3653 }
3654 else
3655 {
3656 up_query_mode = UP_QUERY_USER_PASS;
3657 prefix = "PASSWORD";
3658 alert_type = "username/password";
3659 if (static_challenge)
3660 {
3662 }
3663 }
3664 buf_printf(&alert_msg, ">%s:Need '%s' %s", prefix, type, alert_type);
3665
3667 {
3668 buf_printf(&alert_msg, " MSG:%s", up->username);
3669 }
3670
3671 if (sc)
3672 {
3673 buf_printf(&alert_msg, " SC:%d,%s",
3676 sc);
3677 }
3678
3680 if (signal_received)
3681 {
3682 ret = false;
3683 }
3684
3685 if (ret)
3686 {
3687 man->persist.special_state_msg = BSTR(&alert_msg);
3688 msg(M_CLIENT, "%s", man->persist.special_state_msg);
3689
3690 /* tell command line parser which info we need */
3691 man->connection.up_query_mode = up_query_mode;
3692 man->connection.up_query_type = type;
3693
3694 /* run command processing event loop until we get our username/password/response */
3695 do
3696 {
3697 man_standalone_event_loop(man, &signal_received, 0);
3698 if (!signal_received)
3699 {
3700 man_check_for_signals(&signal_received);
3701 }
3702 if (signal_received)
3703 {
3704 ret = false;
3705 break;
3706 }
3707 } while (!man->connection.up_query.defined);
3708 }
3709
3710 /* revert state */
3711 man->connection.up_query_mode = UP_QUERY_DISABLED;
3712 man->connection.up_query_type = NULL;
3713 man->persist.standalone_disabled = standalone_disabled_save;
3714 man->persist.special_state_msg = NULL;
3715
3716 /* pass through blank passwords */
3717 if (!strcmp(man->connection.up_query.password, blank_up))
3718 {
3719 CLEAR(man->connection.up_query.password);
3720 }
3721
3722 /*
3723 * Transfer u/p to return object, zero any record
3724 * we hold in the management object.
3725 */
3726 if (ret)
3727 {
3728 /* preserve caller's settings */
3729 man->connection.up_query.nocache = up->nocache;
3730 *up = man->connection.up_query;
3731 }
3732 secure_memzero(&man->connection.up_query, sizeof(man->connection.up_query));
3733 }
3734
3735 gc_free(&gc);
3736 return ret;
3737}
3738
3739static int
3740management_query_multiline(struct management *man, const char *b64_data, const char *prompt,
3741 const char *cmd, int *state, struct buffer_list **input)
3742{
3743 struct gc_arena gc = gc_new();
3744 int ret = 0;
3745 volatile int signal_received = 0;
3746 struct buffer alert_msg = clear_buf();
3747 const bool standalone_disabled_save = man->persist.standalone_disabled;
3748 struct man_connection *mc = &man->connection;
3749
3750 if (man_standalone_ok(man))
3751 {
3752 /* This is so M_CLIENT messages will be correctly passed through msg() */
3753 man->persist.standalone_disabled = false;
3754 man->persist.special_state_msg = NULL;
3755
3756 *state = EKS_SOLICIT;
3757
3758 if (b64_data)
3759 {
3760 alert_msg = alloc_buf_gc(strlen(b64_data) + strlen(prompt) + 3, &gc);
3761 buf_printf(&alert_msg, ">%s:%s", prompt, b64_data);
3762 }
3763 else
3764 {
3765 alert_msg = alloc_buf_gc(strlen(prompt) + 3, &gc);
3766 buf_printf(&alert_msg, ">%s", prompt);
3767 }
3768
3769 man_wait_for_client_connection(man, &signal_received, 0, MWCC_OTHER_WAIT);
3770
3771 if (signal_received)
3772 {
3773 goto done;
3774 }
3775
3776 man->persist.special_state_msg = BSTR(&alert_msg);
3778
3779 /* run command processing event loop until we get our signature */
3780 do
3781 {
3782 man_standalone_event_loop(man, &signal_received, 0);
3783 if (!signal_received)
3784 {
3785 man_check_for_signals(&signal_received);
3786 }
3787 if (signal_received)
3788 {
3789 goto done;
3790 }
3791 } while (*state != EKS_READY);
3792
3793 ret = 1;
3794 }
3795
3796done:
3797 if (*state == EKS_READY && ret)
3798 {
3799 msg(M_CLIENT, "SUCCESS: %s command succeeded", cmd);
3800 }
3801 else if (*state == EKS_INPUT || *state == EKS_READY)
3802 {
3803 msg(M_CLIENT, "ERROR: %s command failed", cmd);
3804 }
3805
3806 /* revert state */
3807 man->persist.standalone_disabled = standalone_disabled_save;
3808 man->persist.special_state_msg = NULL;
3810 *state = EKS_UNDEF;
3811
3812 gc_free(&gc);
3813 return ret;
3814}
3815
3816static char *
3817/* returns allocated base64 signature */
3819 const char *prompt, const char *cmd, int *state,
3820 struct buffer_list **input)
3821{
3822 int ok;
3823 char *result = NULL;
3824 const struct buffer *buf;
3825
3826 ok = management_query_multiline(man, b64_data, prompt, cmd, state, input);
3827 if (ok && buffer_list_defined(*input))
3828 {
3830 buf = buffer_list_peek(*input);
3831 if (buf && BLEN(buf) > 0)
3832 {
3833 result = (char *)malloc(BLENZ(buf) + 1);
3835 memcpy(result, buf->data, BLENZ(buf));
3836 result[BLEN(buf)] = '\0';
3837 }
3838 }
3839
3841 *input = NULL;
3842
3843 return result;
3844}
3845
3846static char *
3847/* returns allocated base64 signature */
3848management_query_multiline_flatten(struct management *man, const char *b64_data, const char *prompt,
3849 const char *cmd, int *state, struct buffer_list **input)
3850{
3851 int ok;
3852 char *result = NULL;
3853 const struct buffer *buf;
3854
3855 ok = management_query_multiline(man, b64_data, prompt, cmd, state, input);
3856 if (ok && buffer_list_defined(*input))
3857 {
3859 buf = buffer_list_peek(*input);
3860 if (buf && BLEN(buf) > 0)
3861 {
3862 result = (char *)malloc(BLENZ(buf) + 1);
3864 memcpy(result, buf->data, BLENZ(buf));
3865 result[BLEN(buf)] = '\0';
3866 }
3867 }
3868
3870 *input = NULL;
3871
3872 return result;
3873}
3874
3875char *
3876/* returns allocated base64 signature */
3877management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
3878{
3879 const char *prompt = "PK_SIGN";
3880 const char *desc = "pk-sign";
3882
3883 if (man->connection.client_version <= MCV_DEFAULT)
3884 {
3885 prompt = "RSA_SIGN";
3886 desc = "rsa-sign";
3887 }
3888
3890 if (man->connection.client_version >= MCV_PKSIGN_ALG)
3891 {
3892 buf_write(&buf_data, ",", (int)strlen(","));
3894 }
3895 char *ret = management_query_multiline_flatten(man, (char *)buf_bptr(&buf_data), prompt, desc,
3896 &man->connection.ext_key_state,
3897 &man->connection.ext_key_input);
3899 return ret;
3900}
3901
3902char *
3904{
3905 const char prompt_1[] = "NEED-CERTIFICATE:";
3906 struct buffer buf_prompt = alloc_buf(strlen(cert_name) + 20);
3908 buf_write(&buf_prompt, cert_name, strlen(cert_name) + 1); /* +1 for \0 */
3909
3910 char *result;
3912 management, NULL, (char *)buf_bptr(&buf_prompt), "certificate",
3913 &man->connection.ext_cert_state, &man->connection.ext_cert_input);
3915 return result;
3916}
3917
3918/*
3919 * Return true if management_hold() would block
3920 */
3921bool
3923{
3924 return (man->settings.flags & MF_HOLD) && !man->persist.hold_release && man_standalone_ok(man);
3925}
3926
3927/*
3928 * If the hold flag is enabled, hibernate until a management client releases the hold.
3929 * Return true if the caller should not sleep for an additional time interval.
3930 */
3931bool
3933{
3935 {
3936 volatile int signal_received = 0;
3937 const bool standalone_disabled_save = man->persist.standalone_disabled;
3938 struct gc_arena gc = gc_new();
3939
3941 false; /* This is so M_CLIENT messages will be correctly passed through msg() */
3942 man->persist.special_state_msg = NULL;
3944
3945 man_wait_for_client_connection(man, &signal_received, 0, MWCC_HOLD_WAIT);
3946
3947 if (!signal_received)
3948 {
3949 struct buffer out = alloc_buf_gc(128, &gc);
3950 buf_printf(&out, ">HOLD:Waiting for hold release:%d", holdtime);
3951 man->persist.special_state_msg = BSTR(&out);
3952 msg(M_CLIENT, "%s", man->persist.special_state_msg);
3953
3954 /* run command processing event loop until we get our username/password */
3955 do
3956 {
3957 man_standalone_event_loop(man, &signal_received, 0);
3958 if (!signal_received)
3959 {
3960 man_check_for_signals(&signal_received);
3961 }
3962 if (signal_received)
3963 {
3964 break;
3965 }
3966 } while (!man->persist.hold_release);
3967 }
3968
3969 /* revert state */
3970 man->persist.standalone_disabled = standalone_disabled_save;
3971 man->persist.special_state_msg = NULL;
3972 man->settings.mansig &= ~MANSIG_IGNORE_USR1_HUP;
3973
3974 gc_free(&gc);
3975 return true;
3976 }
3977 return false;
3978}
3979
3980/*
3981 * struct command_line
3982 */
3983
3984struct command_line *
3986{
3987 struct command_line *cl;
3988 ALLOC_OBJ_CLEAR(cl, struct command_line);
3989 cl->buf = alloc_buf(buf_len);
3990 cl->residual = alloc_buf(buf_len);
3991 return cl;
3992}
3993
3994void
3996{
3997 buf_clear(&cl->buf);
3998 buf_clear(&cl->residual);
3999}
4000
4001void
4003{
4004 if (!cl)
4005 {
4006 return;
4007 }
4009 free_buf(&cl->buf);
4010 free_buf(&cl->residual);
4011 free(cl);
4012}
4013
4014void
4015command_line_add(struct command_line *cl, const unsigned char *buf, const size_t len)
4016{
4017 for (size_t i = 0; i < len; ++i)
4018 {
4019 if (buf[i] && char_class(buf[i], (CC_PRINT | CC_NEWLINE)))
4020 {
4021 if (!buf_write_u8(&cl->buf, buf[i]))
4022 {
4023 buf_clear(&cl->buf);
4024 }
4025 }
4026 }
4027}
4028
4029const char *
4031{
4032 const char *ret = NULL;
4033
4034 int i = buf_substring_len(&cl->buf, '\n');
4035 if (i >= 0)
4036 {
4037 buf_copy_excess(&cl->residual, &cl->buf, i);
4038 buf_chomp(&cl->buf);
4039 ret = BSTR(&cl->buf);
4040 }
4041 return ret;
4042}
4043
4044void
4046{
4047 buf_clear(&cl->buf);
4048 buf_copy(&cl->buf, &cl->residual);
4049 buf_clear(&cl->residual);
4050}
4051
4052/*
4053 * struct log_entry
4054 */
4055
4056const char *
4057log_entry_print(const struct log_entry *e, unsigned int flags, struct gc_arena *gc)
4058{
4059 struct buffer out = alloc_buf_gc(ERR_BUF_SIZE, gc);
4060 if (flags & LOG_FATAL_NOTIFY)
4061 {
4062 buf_printf(&out, ">FATAL:");
4063 }
4064 if (flags & LOG_PRINT_LOG_PREFIX)
4065 {
4066 buf_printf(&out, ">LOG:");
4067 }
4068 if (flags & LOG_PRINT_ECHO_PREFIX)
4069 {
4070 buf_printf(&out, ">ECHO:");
4071 }
4072 if (flags & LOG_PRINT_STATE_PREFIX)
4073 {
4074 buf_printf(&out, ">STATE:");
4075 }
4076 if (flags & LOG_PRINT_INT_DATE)
4077 {
4078 buf_printf(&out, "%u,", (unsigned int)e->timestamp);
4079 }
4080 if (flags & LOG_PRINT_MSG_FLAGS)
4081 {
4082 buf_printf(&out, "%s,", msg_flags_string(e->u.msg_flags, gc));
4083 }
4084 if (flags & LOG_PRINT_STATE)
4085 {
4086 buf_printf(&out, "%s,", man_state_name(e->u.state));
4087 }
4088 if (flags & LOG_PRINT_INTVAL)
4089 {
4090 buf_printf(&out, "%d,", e->u.intval);
4091 }
4092 if (e->string)
4093 {
4094 buf_printf(&out, "%s", e->string);
4095 }
4096 if (flags & LOG_PRINT_LOCAL_IP)
4097 {
4098 buf_printf(&out, ",%s", print_in_addr_t(e->local_ip, IA_EMPTY_IF_UNDEF, gc));
4099 }
4100 if (flags & LOG_PRINT_REMOTE_IP)
4101 {
4102 buf_printf(&out, ",%s",
4103 (!addr_defined(&e->remote_sock)
4104 ? ","
4105 : print_sockaddr_ex(&e->remote_sock.addr.sa, ",",
4107 buf_printf(&out, ",%s",
4108 (!addr_defined(&e->local_sock)
4109 ? ","
4110 : print_sockaddr_ex(&e->local_sock.addr.sa, ",",
4112 }
4113 if (flags & LOG_PRINT_LOCAL_IP && !IN6_IS_ADDR_UNSPECIFIED(&e->local_ip6))
4114 {
4115 buf_printf(&out, ",%s", print_in6_addr(e->local_ip6, IA_EMPTY_IF_UNDEF, gc));
4116 }
4117 if (flags & LOG_ECHO_TO_LOG)
4118 {
4119 msg(D_MANAGEMENT, "MANAGEMENT: %s", BSTR(&out));
4120 }
4121 if (flags & LOG_PRINT_CRLF)
4122 {
4123 buf_printf(&out, "\r\n");
4124 }
4125 return BSTR(&out);
4126}
4127
4128static void
4130{
4131 /* Cast away constness of const char* */
4132 free((char *)e->string);
4133 CLEAR(*e);
4134}
4135
4136/*
4137 * struct log_history
4138 */
4139
4140static inline int
4141log_index(const struct log_history *h, int i)
4142{
4143 return modulo_add(h->base, i, h->capacity);
4144}
4145
4146static void
4148{
4149 CLEAR(*h);
4150 h->capacity = capacity;
4152}
4153
4154struct log_history *
4156{
4157 struct log_history *h;
4158 ASSERT(capacity > 0);
4159 ALLOC_OBJ(h, struct log_history);
4161 return h;
4162}
4163
4164static void
4166{
4167 int i;
4168 for (i = 0; i < h->size; ++i)
4169 {
4171 }
4172 free(h->array);
4173}
4174
4175void
4177{
4179 free(h);
4180}
4181
4182void
4183log_history_add(struct log_history *h, const struct log_entry *le)
4184{
4185 struct log_entry *e;
4186 ASSERT(h->size >= 0 && h->size <= h->capacity);
4187 if (h->size == h->capacity)
4188 {
4189 e = &h->array[h->base];
4191 h->base = log_index(h, 1);
4192 }
4193 else
4194 {
4195 e = &h->array[log_index(h, h->size)];
4196 ++h->size;
4197 }
4198
4199 *e = *le;
4200 e->string = string_alloc(le->string, NULL);
4201}
4202
4203void
4204log_history_resize(struct log_history *h, const int capacity)
4205{
4206 if (capacity != h->capacity)
4207 {
4208 struct log_history newlog;
4209 int i;
4210
4211 ASSERT(capacity > 0);
4213
4214 for (i = 0; i < h->size; ++i)
4215 {
4216 log_history_add(&newlog, &h->array[log_index(h, i)]);
4217 }
4218
4220 *h = newlog;
4221 }
4222}
4223
4224const struct log_entry *
4225log_history_ref(const struct log_history *h, const int index)
4226{
4227 if (index >= 0 && index < h->size)
4228 {
4229 return &h->array[log_index(h, (h->size - 1) - index)];
4230 }
4231 else
4232 {
4233 return NULL;
4234 }
4235}
4236
4237void
4239{
4240 if (n < 0)
4241 {
4242 return;
4243 }
4244 else if (management)
4245 {
4247 }
4248 else
4249 {
4250#ifdef _WIN32
4251 win32_sleep(n);
4252#else
4253 if (n > 0)
4254 {
4255 sleep(n);
4256 }
4257#endif
4258 }
4259}
4260
4261void
4262management_check_bytecount_client(struct context *c, struct management *man, struct timeval *timeval)
4263{
4264 if (man->persist.callback.flags & MCF_SERVER)
4265 {
4266 return;
4267 }
4268
4270 {
4271 if (dco_enabled(&c->options))
4272 {
4273 if (dco_get_peer_stats(c, true) < 0)
4274 {
4275 return;
4276 }
4277 }
4278
4281 }
4282}
4283
4284void
4285management_check_bytecount_server(struct multi_context *multi, struct timeval *timeval)
4286{
4288 {
4289 return;
4290 }
4291
4293 {
4294 /* fetch counters from dco */
4295 if (dco_enabled(&multi->top.options))
4296 {
4297 if (dco_get_peer_stats_multi(&multi->top.c1.tuntap->dco, true) < 0)
4298 {
4299 return;
4300 }
4301 }
4302
4303 /* iterate over peers and report counters for each connected peer */
4304 struct hash_iterator hi;
4305 struct hash_element *he;
4306 hash_iterator_init(multi->hash, &hi);
4307 while ((he = hash_iterator_next(&hi)))
4308 {
4309 struct multi_instance *mi = (struct multi_instance *)he->value;
4310 struct context_2 *c2 = &mi->context.c2;
4311
4313 {
4315 }
4316 }
4317 hash_iterator_free(&hi);
4318 }
4319}
4320
4321/* context_2 stats are reset on reconnect. Since client expects stats
4322 * to be preserved across reconnects, we need to save context_2
4323 * stats before tearing the tunnel down.
4324 */
4325void
4327{
4330
4331 /* no need to raise SIGUSR1 on error since we are already closing the instance */
4332 if (dco_enabled(&c->options) && (dco_get_peer_stats(c, false) == 0))
4333 {
4334 man->persist.bytes_in += c->c2.dco_read_bytes;
4336 }
4337}
4338
4339#else /* ifdef ENABLE_MANAGEMENT */
4340
4341#include "win32.h"
4342void
4343management_sleep(const int n)
4344{
4345#ifdef _WIN32
4346 win32_sleep(n);
4347#else
4348 if (n > 0)
4349 {
4350 sleep(n);
4351 }
4352#endif /* ifdef _WIN32 */
4353}
4354
4355#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
static uint8_t * buf_bptr(const struct buffer *buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:418
#define ALLOC_OBJ(dptr, type)
Allocate memory for a single object of the given type.
Definition buffer.h:1963
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:151
static struct buffer clear_buf(void)
Return an empty, undefined struct buffer (all fields zero).
Definition buffer.h:376
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:1301
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
#define ALLOC_ARRAY_CLEAR(dptr, type, n)
Allocate and zero-initialise an array of n elements of the given type.
Definition buffer.h:2015
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:1376
#define CC_NEWLINE
newline
Definition buffer.h:1650
static int buf_len(const struct buffer *buf)
Return the length of the buffer content.
Definition buffer.h:438
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:623
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:705
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1198
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1242
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:145
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:147
static void strncpynt(char *dest, const char *src, size_t maxlen)
Like strncpy() but always null-terminates the destination.
Definition buffer.h:646
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2082
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1912
#define CC_PRINT
printable (>= 32, != 127)
Definition buffer.h:1644
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:1974
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1896
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:3201
void management_auth_failure(struct management *man, const char *type, const char *reason)
Definition manage.c:3210
void log_history_close(struct log_history *h)
Definition manage.c:4176
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:4285
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:2263
static void man_log(struct management *man, const char *parm)
Definition manage.c:720
static void man_reset_client_socket(struct management *man, const bool exiting)
Definition manage.c:2165
static void man_bytecount(struct management *man, const int update_seconds)
Definition manage.c:528
void management_notify_client_close(struct management *management, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3119
static void man_connect(struct management *man)
Definition manage.c:2099
#define MWCC_OTHER_WAIT
Definition manage.c:3516
static ssize_t man_read(struct management *man)
Definition manage.c:2419
static bool man_check_for_signals(volatile int *signal_received)
Definition manage.c:3400
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:3245
void command_line_free(struct command_line *cl)
Definition manage.c:4002
static int man_block(struct management *man, volatile int *signal_received, const time_t expire)
Definition manage.c:3417
static void man_certificate(struct management *man)
Definition manage.c:1314
void command_line_next(struct command_line *cl)
Definition manage.c:4045
static void man_net(struct management *man)
Definition manage.c:854
void management_clear_callback(struct management *man)
Definition manage.c:2873
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:2571
char * management_query_cert(struct management *man, const char *cert_name)
Definition manage.c:3903
struct command_line * command_line_new(const size_t buf_len)
Definition manage.c:3985
static bool man_standalone_ok(const struct management *man)
Definition manage.c:3394
bool management_hold(struct management *man, int holdtime)
Definition manage.c:3932
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:2629
static int log_index(const struct log_history *h, int i)
Definition manage.c:4141
static void man_connection_clear(struct man_connection *mc)
Definition manage.c:2558
#define AF_DID_RESET
static void man_start_ne32(struct management *man)
Definition manage.c:1852
static void man_connection_settings_reset(struct management *man)
Definition manage.c:1880
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:3229
static bool man_need(struct management *man, 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:1354
static void man_connection_init(struct management *man)
Definition manage.c:2729
static bool env_filter_match(const char *env_str, const int env_filter_level)
Definition manage.c:2936
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:1173
static void man_connection_close(struct management *man)
Definition manage.c:2774
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:1250
struct log_history * log_history_init(const int capacity)
Definition manage.c:4155
static void man_listen(struct management *man)
Definition manage.c:2023
static void man_stop_ne32(struct management *man)
Definition manage.c:1872
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:1439
#define MWCC_PASSWORD_WAIT
Definition manage.c:3514
static void man_dispatch_command(struct management *man, struct status_output *so, const char **p, const int nparms)
Definition manage.c:1481
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:1222
static void man_echo(struct management *man, const char *parm)
Definition manage.c:727
void management_learn_addr(struct management *management, struct man_def_auth_context *mdac, const struct mroute_addr *addr, const bool primary)
Definition manage.c:3131
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:3848
static void man_proxy(struct management *man, const char **p)
Definition manage.c:1371
struct management * management_init(void)
Definition manage.c:2803
static void in_extra_reset(struct man_connection *mc, const int mode)
Definition manage.c:1004
void command_line_reset(struct command_line *cl)
Definition manage.c:3995
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:3922
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:3608
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:4183
static void log_entry_free_contents(struct log_entry *e)
Definition manage.c:4129
static int man_mod_signal(const struct management *man, const int signum)
Definition manage.c:448
#define MN_AT_LEAST
Definition manage.c:1342
void management_event_loop_n_seconds(struct management *man, int sec)
Definition manage.c:3555
static void man_persist_close(struct man_persist *mp)
Definition manage.c:2607
static void man_kill(struct management *man, const char *victim)
Definition manage.c:590
void management_close(struct management *man)
Definition manage.c:2856
static void man_pk_sig(struct management *man, const char *cmd_name)
Definition manage.c:1298
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:2882
static void man_output_list_push_finalize(struct management *man)
Definition manage.c:270
static void man_load_stats(struct management *man)
Definition manage.c:1330
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:1421
const char * log_entry_print(const struct log_entry *e, unsigned int flags, struct gc_arena *gc)
Definition manage.c:4057
void management_auth_token(struct management *man, const char *token)
Definition manage.c:3223
static void man_accept(struct management *man)
Definition manage.c:1983
static void man_client_auth(struct management *man, const char *cid_str, const char *kid_str, const bool extra)
Definition manage.c:1205
static bool parse_cid(const char *str, unsigned long *cid)
Definition manage.c:1134
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:3283
static void man_wait_for_client_connection(struct management *man, volatile int *signal_received, const time_t expire, unsigned int flags)
Definition manage.c:3522
#define IER_NEW
Definition manage.c:1001
static void man_output_peer_info_env(struct management *man, const struct man_def_auth_context *mdac)
Definition manage.c:3039
#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:2817
static void man_welcome(struct management *man)
Definition manage.c:206
void management_notify_generic(struct management *man, const char *str)
Definition manage.c:3033
void management_connection_established(struct management *management, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3109
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:3818
static void man_output_extra_env(struct management *man, const char *prefix)
Definition manage.c:3003
void man_persist_client_stats(struct management *man, struct context *c)
Definition manage.c:4326
static void man_new_connection_post(struct management *man, const char *description)
Definition manage.c:1893
static void log_history_obj_init(struct log_history *h, int capacity)
Definition manage.c:4147
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:1040
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:1000
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:1148
void management_set_callback(struct management *man, const struct management_callback *cb)
Definition manage.c:2866
void management_notify_client_needing_auth(struct management *management, const unsigned int mda_key_id, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3067
static void man_client_n_clients(struct management *man)
Definition manage.c:1276
static void man_output_standalone(struct management *man, volatile int *signal_received)
Definition manage.c:3476
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:3089
void management_echo(struct management *man, const char *string, const bool pull)
Definition manage.c:3144
static void man_remote_entry_get(struct management *man, const char *p1, const char *p2)
Definition manage.c:933
const struct log_entry * log_history_ref(const struct log_history *h, const int index)
Definition manage.c:4225
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:2718
void management_check_bytecount_client(struct context *c, struct management *man, struct timeval *timeval)
Definition manage.c:4262
static void man_env_filter(struct management *man, const int level)
Definition manage.c:1290
#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:3017
char * management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
Definition manage.c:3877
static void man_remote(struct management *man, const char **p)
Definition manage.c:1385
static void man_forget_passwords(struct management *man)
Definition manage.c:846
void command_line_add(struct command_line *cl, const unsigned char *buf, const size_t len)
Definition manage.c:4015
static int man_standalone_event_loop(struct management *man, volatile int *signal_received, const time_t expire)
Definition manage.c:3499
static void man_process_command(struct management *man, const char *line)
Definition manage.c:2213
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:3740
static void man_output_env(const struct env_set *es, const bool tail, const int env_filter_level, const char *prefix)
Definition manage.c:2982
const char * command_line_get(struct command_line *cl)
Definition manage.c:4030
static void log_history_free_contents(struct log_history *h)
Definition manage.c:4165
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
Definition manage.c:4238
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:4204
static void in_extra_dispatch(struct management *man)
Definition manage.c:1062
static void man_command_unsupported(const char *command_name)
Definition manage.c:501
void management_notify(struct management *man, const char *severity, const char *type, const char *text)
Definition manage.c:3027
#define MWCC_HOLD_WAIT
Definition manage.c:3515
void management_post_tunnel_open(struct management *man, const in_addr_t tun_local_ip)
Definition manage.c:3178
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:2513
static void man_hold(struct management *man, const char *cmd)
Definition manage.c:970
#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:748
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:929
void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int ai_family, const char *prefix, bool ipv6only)
Definition socket.c:881
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:2106
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 env_set * es
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:1549
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