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 unsigned int action_flags = 0;
372
373 ++recursive_level;
374
375 CLEAR(e);
376 update_time();
377 e.timestamp = now;
378 e.u.msg_flags = flags;
379 e.string = str;
380
381 if (flags & M_FATAL)
382 {
383 man->persist.standalone_disabled = false;
384 }
385
386 if (flags != M_CLIENT)
387 {
388 log_history_add(man->persist.log, &e);
389 }
390
391 if (!man_password_needed(man))
392 {
393 const char *out = NULL;
394
395 if (flags == M_CLIENT)
396 {
397 out = log_entry_print(&e, LOG_PRINT_CRLF, &gc);
398 }
399 else if (man->connection.log_realtime)
400 {
401 out = log_entry_print(&e,
404 &gc);
405 }
406 if (out)
407 {
408 man_output_list_push_str(man, out);
409 action_flags |= AF_DID_PUSH;
410 }
411 if (flags & M_FATAL)
412 {
414 if (out)
415 {
416 man_output_list_push_str(man, out);
417 action_flags |= (AF_DID_PUSH | AF_DID_RESET);
418 }
419 }
420 }
421
422 gc_free(&gc);
423
424 if (action_flags & AF_DID_PUSH)
425 {
427 }
428 if (action_flags & AF_DID_RESET)
429 {
430 man_reset_client_socket(man, true);
431 }
432
433 --recursive_level;
434 }
435 else
436 {
437 /* cannot use msg here */
438 printf("virtual_output: message to management interface "
439 "dropped due to recursion: <%s>\n",
440 str);
441 }
442}
443
444/*
445 * Given a signal, return the signal with possible remapping applied,
446 * or -1 if the signal should be ignored.
447 */
448static int
449man_mod_signal(const struct management *man, const int signum)
450{
451 const unsigned int flags = man->settings.mansig;
452 int s = signum;
453 if (s == SIGUSR1)
454 {
455 if (flags & MANSIG_MAP_USR1_TO_HUP)
456 {
457 s = SIGHUP;
458 }
459 if (flags & MANSIG_MAP_USR1_TO_TERM)
460 {
461 s = SIGTERM;
462 }
463 }
464 if (flags & MANSIG_IGNORE_USR1_HUP)
465 {
466 if (s == SIGHUP || s == SIGUSR1)
467 {
468 s = -1;
469 }
470 }
471 return s;
472}
473
474static void
475man_signal(struct management *man, const char *name)
476{
477 const int sig = parse_signal(name);
478 if (sig >= 0)
479 {
480 const int sig_mod = man_mod_signal(man, sig);
481 if (sig_mod >= 0)
482 {
483 throw_signal(sig_mod);
484 msg(M_CLIENT, "SUCCESS: signal %s thrown", signal_name(sig_mod, true));
485 }
486 else
487 {
488 msg(M_CLIENT, "ERROR: signal '%s' is currently ignored", name);
489 if (man->persist.special_state_msg)
490 {
492 }
493 }
494 }
495 else
496 {
497 msg(M_CLIENT, "ERROR: signal '%s' is not a known signal type", name);
498 }
499}
500
501static void
502man_command_unsupported(const char *command_name)
503{
504 msg(M_CLIENT, "ERROR: The '%s' command is not supported by the current daemon mode",
505 command_name);
506}
507
508static void
509man_status(struct management *man, const int version, struct status_output *so)
510{
511 if (man->persist.callback.status)
512 {
513 (*man->persist.callback.status)(man->persist.callback.arg, version, so);
514 }
515 else
516 {
517 man_command_unsupported("status");
518 }
519}
520
521static void
527
528static void
529man_bytecount(struct management *man, const int update_seconds)
530{
531 if (update_seconds > 0)
532 {
533 man->connection.bytecount_update_seconds = update_seconds;
536 }
537 else
538 {
540 }
541
542 /* The newly received bytecount interval may be sooner than the existing
543 * coarse timer wakeup. Reset the timer to ensure it fires at the correct,
544 * earlier time.
545 */
546 if (man->persist.callback.arg)
547 {
548 struct context *c;
549
550 if (man->settings.flags & MF_SERVER)
551 {
552 struct multi_context *m = man->persist.callback.arg;
553 c = &m->top;
554 }
555 else
556 {
557 c = man->persist.callback.arg;
558 }
559
561 }
562
563 msg(M_CLIENT, "SUCCESS: bytecount interval changed");
564}
565
566static void
568{
569 char in[32];
570 char out[32];
571
572 /* do in a roundabout way to work around possible mingw or mingw-glibc bug */
573 snprintf(in, sizeof(in), counter_format, bytes_in_total);
574 snprintf(out, sizeof(out), counter_format, bytes_out_total);
575 msg(M_CLIENT, ">BYTECOUNT:%s,%s", in, out);
576}
577
578static void
579man_bytecount_output_server(const counter_type bytes_in_total, const counter_type bytes_out_total,
580 struct man_def_auth_context *mdac)
581{
582 char in[32];
583 char out[32];
584 /* do in a roundabout way to work around possible mingw or mingw-glibc bug */
585 snprintf(in, sizeof(in), counter_format, bytes_in_total);
586 snprintf(out, sizeof(out), counter_format, bytes_out_total);
587 msg(M_CLIENT, ">BYTECOUNT_CLI:%lu,%s,%s", mdac->cid, in, out);
588}
589
590static void
591man_kill(struct management *man, const char *victim)
592{
593 struct gc_arena gc = gc_new();
594
596 {
597 struct buffer buf;
598 char p1[128];
599 char p2[128];
600 char p3[128];
601 int n_killed;
602
603 buf_set_read(&buf, (uint8_t *)victim, strlen(victim) + 1);
604 buf_parse(&buf, ':', p1, sizeof(p1));
605 buf_parse(&buf, ':', p2, sizeof(p2));
606 buf_parse(&buf, ':', p3, sizeof(p3));
607
608 if (strlen(p1) && strlen(p2) && strlen(p3))
609 {
610 /* IP:port specified */
611 bool status;
612 const in_addr_t addr =
614 if (status)
615 {
616 const int port = atoi(p3);
617 const uint8_t proto = (streq(p1, "tcp")) ? PROTO_TCP_SERVER
618 : (streq(p1, "udp")) ? PROTO_UDP
619 : PROTO_NONE;
620
621 if ((port > 0 && port <= UINT16_MAX) && (proto != PROTO_NONE))
622 {
623 n_killed = (*man->persist.callback.kill_by_addr)(man->persist.callback.arg,
624 addr, (uint16_t)port, proto);
625 if (n_killed > 0)
626 {
627 msg(M_CLIENT, "SUCCESS: %d client(s) at address %s:%s:%d killed", n_killed,
628 proto2ascii(proto, AF_UNSPEC, false), print_in_addr_t(addr, 0, &gc),
629 port);
630 }
631 else
632 {
633 msg(M_CLIENT, "ERROR: client at address %s:%s:%d not found",
634 proto2ascii(proto, AF_UNSPEC, false), print_in_addr_t(addr, 0, &gc),
635 port);
636 }
637 }
638 else
639 {
640 msg(M_CLIENT, "ERROR: port number or protocol out of range: %s %s", p3, p1);
641 }
642 }
643 else
644 {
645 msg(M_CLIENT, "ERROR: error parsing IP address: %s", p2);
646 }
647 }
648 else if (strlen(p1))
649 {
650 /* common name specified */
651 n_killed = (*man->persist.callback.kill_by_cn)(man->persist.callback.arg, p1);
652 if (n_killed > 0)
653 {
654 msg(M_CLIENT, "SUCCESS: common name '%s' found, %d client(s) killed", p1, n_killed);
655 }
656 else
657 {
658 msg(M_CLIENT, "ERROR: common name '%s' not found", p1);
659 }
660 }
661 else
662 {
663 msg(M_CLIENT, "ERROR: kill parse");
664 }
665 }
666 else
667 {
669 }
670
671 gc_free(&gc);
672}
673
674/*
675 * General-purpose history command handler
676 * for the log and echo commands.
677 */
678static void
679man_history(struct management *man, const char *parm, const char *type, struct log_history *log,
680 bool *realtime, const unsigned int lep_flags)
681{
682 struct gc_arena gc = gc_new();
683 int n = 0;
684
685 if (streq(parm, "on"))
686 {
687 *realtime = true;
688 msg(M_CLIENT, "SUCCESS: real-time %s notification set to ON", type);
689 }
690 else if (streq(parm, "off"))
691 {
692 *realtime = false;
693 msg(M_CLIENT, "SUCCESS: real-time %s notification set to OFF", type);
694 }
695 else if (streq(parm, "all") || (n = atoi(parm)) > 0)
696 {
697 const int size = log_history_size(log);
698 const int start = (n ? n : size) - 1;
699 int i;
700
701 for (i = start; i >= 0; --i)
702 {
703 const struct log_entry *e = log_history_ref(log, i);
704 if (e)
705 {
706 const char *out = log_entry_print(e, lep_flags, &gc);
708 }
709 }
710 msg(M_CLIENT, "END");
711 }
712 else
713 {
714 msg(M_CLIENT, "ERROR: %s parameter must be 'on' or 'off' or some number n or 'all'", type);
715 }
716
717 gc_free(&gc);
718}
719
720static void
721man_log(struct management *man, const char *parm)
722{
723 man_history(man, parm, "log", man->persist.log, &man->connection.log_realtime,
725}
726
727static void
728man_echo(struct management *man, const char *parm)
729{
730 man_history(man, parm, "echo", man->persist.echo, &man->connection.echo_realtime,
732}
733
734static void
735man_state(struct management *man, const char *parm)
736{
737 man_history(man, parm, "state", man->persist.state, &man->connection.state_realtime,
739}
740
741static void
743{
744 switch (man->connection.up_query_mode)
745 {
747 if (strlen(man->connection.up_query.username))
748 {
749 man->connection.up_query.defined = true;
750 }
751 break;
752
754 if (!strlen(man->connection.up_query.username))
755 {
756 break;
757 }
758
759 /* fall through */
760 case UP_QUERY_PASS:
761 case UP_QUERY_NEED_OK:
763 if (strlen(man->connection.up_query.password))
764 {
765 man->connection.up_query.defined = true;
766 }
767 break;
768
770 man->connection.up_query.defined = false;
771 break;
772
773 default:
774 ASSERT(0);
775 }
776}
777
778static void
779man_query_user_pass(struct management *man, const char *type, const char *string, const bool needed,
780 const char *prompt, char *dest, int len)
781{
782 if (needed)
783 {
785 if (streq(man->connection.up_query_type, type))
786 {
787 strncpynt(dest, string, len);
788 man_up_finalize(man);
789 msg(M_CLIENT, "SUCCESS: '%s' %s entered, but not yet verified", type, prompt);
790 }
791 else
792 {
793 msg(M_CLIENT, "ERROR: %s of type '%s' entered, but we need one of type '%s'", prompt,
794 type, man->connection.up_query_type);
795 }
796 }
797 else
798 {
799 msg(M_CLIENT, "ERROR: no %s is currently needed at this time", prompt);
800 }
801}
802
803static void
804man_query_username(struct management *man, const char *type, const char *string)
805{
806 const bool needed =
809 && man->connection.up_query_type);
810 man_query_user_pass(man, type, string, needed, "username", man->connection.up_query.username,
812}
813
814static void
815man_query_password(struct management *man, const char *type, const char *string)
816{
817 const bool needed = ((man->connection.up_query_mode == UP_QUERY_PASS
819 && man->connection.up_query_type);
820 if (!string[0]) /* allow blank passwords to be passed through using the blank_up tag */
821 {
822 string = blank_up;
823 }
824 man_query_user_pass(man, type, string, needed, "password", man->connection.up_query.password,
826}
827
828static void
829man_query_need_ok(struct management *man, const char *type, const char *action)
830{
831 const bool needed =
833 man_query_user_pass(man, type, action, needed, "needok-confirmation",
835}
836
837static void
838man_query_need_str(struct management *man, const char *type, const char *action)
839{
840 const bool needed =
842 man_query_user_pass(man, type, action, needed, "needstr-string",
844}
845
846static void
848{
849 ssl_purge_auth(false);
850 (void)ssl_clean_auth_token();
851 msg(M_CLIENT, "SUCCESS: Passwords were forgotten");
852}
853
854static void
855man_net(struct management *man)
856{
857 if (man->persist.callback.show_net)
858 {
860 }
861 else
862 {
864 }
865}
866
867static void
868man_send_cc_message(struct management *man, const char *message, const char *parameters)
869{
871 {
872 const bool status = (*man->persist.callback.send_cc_message)(man->persist.callback.arg,
873 message, parameters);
874 if (status)
875 {
876 msg(M_CLIENT, "SUCCESS: command succeeded");
877 }
878 else
879 {
880 msg(M_CLIENT, "ERROR: command failed");
881 }
882 }
883 else
884 {
885 man_command_unsupported("cr-repsonse");
886 }
887}
888#ifdef ENABLE_PKCS11
889
890static void
891man_pkcs11_id_count(struct management *man)
892{
893 msg(M_CLIENT, ">PKCS11ID-COUNT:%d", pkcs11_management_id_count());
894}
895
896static void
897man_pkcs11_id_get(struct management *man, const int index)
898{
899 char *id = NULL;
900 char *base64 = NULL;
901
902 if (pkcs11_management_id_get(index, &id, &base64))
903 {
904 msg(M_CLIENT, ">PKCS11ID-ENTRY:'%d', ID:'%s', BLOB:'%s'", index, id, base64);
905 }
906 else
907 {
908 msg(M_CLIENT, ">PKCS11ID-ENTRY:'%d'", index);
909 }
910
911 free(id);
912 free(base64);
913}
914
915#endif /* ifdef ENABLE_PKCS11 */
916
917static void
919{
921 {
922 unsigned int count = (*man->persist.callback.remote_entry_count)(man->persist.callback.arg);
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)
1352static bool
1353man_need(const char **p, const int n, unsigned int flags)
1354{
1355 ASSERT(p[0]);
1356 for (int i = 1; i <= n; ++i)
1357 {
1358 if (!p[i])
1359 {
1360 msg(M_CLIENT, "ERROR: the '%s' command requires %s%d parameter%s", p[0],
1361 (flags & MN_AT_LEAST) ? "at least " : "", n, n > 1 ? "s" : "");
1362 return false;
1363 }
1364 }
1365 return true;
1366}
1367
1368static void
1369man_proxy(struct management *man, const char **p)
1370{
1371 if (man->persist.callback.proxy_cmd)
1372 {
1373 const bool status = (*man->persist.callback.proxy_cmd)(man->persist.callback.arg, p);
1374 report_command_status(status, "proxy");
1375 }
1376 else
1377 {
1378 man_command_unsupported("proxy");
1379 }
1380}
1381
1382static void
1383man_remote(struct management *man, const char **p)
1384{
1385 if (man->persist.callback.remote_cmd)
1386 {
1387 const bool status = (*man->persist.callback.remote_cmd)(man->persist.callback.arg, p);
1388 report_command_status(status, "remote");
1389 }
1390 else
1391 {
1392 man_command_unsupported("remote");
1393 }
1394}
1395
1396#ifdef TARGET_ANDROID
1397static void
1398man_network_change(struct management *man, bool samenetwork)
1399{
1400 /* Called to signal the OpenVPN that the network configuration has changed and
1401 * the client should either float or reconnect.
1402 *
1403 * The code is currently only used by ics-openvpn
1404 */
1405 if (man->persist.callback.network_change)
1406 {
1407 int fd = (*man->persist.callback.network_change)(man->persist.callback.arg, samenetwork);
1408 man->connection.fdtosend = fd;
1409 msg(M_CLIENT, "PROTECTFD: fd '%d' sent to be protected", fd);
1410 if (fd == -2)
1411 {
1412 man_signal(man, "SIGUSR1");
1413 }
1414 }
1415}
1416#endif
1417
1418static void
1419set_client_version(struct management *man, const char *version)
1420{
1421 if (version)
1422 {
1423 man->connection.client_version = atoi(version);
1424 /* Until MCV_PKSIGN_ALG, we missed to respond to this command. Acknowledge only if version is newer */
1426 {
1427 msg(M_CLIENT, "SUCCESS: Management client version set to %d", man->connection.client_version);
1428 }
1429 }
1430 else
1431 {
1432 msg(M_CLIENT, "ERROR: Invalid value specified for management client version");
1433 }
1434}
1435
1436static void
1437man_push_update(struct management *man, const char **p, const push_update_type type)
1438{
1439 bool status = false;
1440
1441 if (type == UPT_BROADCAST)
1442 {
1444 {
1445 man_command_unsupported("push-update-broad");
1446 return;
1447 }
1448
1450 }
1451 else if (type == UPT_BY_CID)
1452 {
1454 {
1455 man_command_unsupported("push-update-cid");
1456 return;
1457 }
1458
1459 unsigned long cid = 0;
1460
1461 if (!parse_cid(p[1], &cid))
1462 {
1463 msg(M_CLIENT, "ERROR: push-update-cid fail during cid parsing");
1464 return;
1465 }
1466
1467 status = (*man->persist.callback.push_update_by_cid)(man->persist.callback.arg, cid, p[2]);
1468 }
1469
1470 if (status)
1471 {
1472 msg(M_CLIENT, "SUCCESS: push-update command succeeded");
1473 return;
1474 }
1475 msg(M_CLIENT, "ERROR: push-update command failed");
1476}
1477
1478static void
1479man_dispatch_command(struct management *man, struct status_output *so, const char **p,
1480 const int nparms)
1481{
1482 struct gc_arena gc = gc_new();
1483
1484 ASSERT(p[0]);
1485 if (streq(p[0], "exit") || streq(p[0], "quit"))
1486 {
1487 man->connection.halt = true;
1488 goto done;
1489 }
1490 else if (streq(p[0], "help"))
1491 {
1492 man_help();
1493 }
1494 else if (streq(p[0], "version") && p[1])
1495 {
1496 set_client_version(man, p[1]);
1497 }
1498 else if (streq(p[0], "version"))
1499 {
1500 msg(M_CLIENT, "OpenVPN Version: %s", title_string);
1501 msg(M_CLIENT, "Management Version: %d", MANAGEMENT_VERSION);
1502 msg(M_CLIENT, "END");
1503 }
1504 else if (streq(p[0], "pid"))
1505 {
1506 msg(M_CLIENT, "SUCCESS: pid=%u", platform_getpid());
1507 }
1508 else if (streq(p[0], "nclients"))
1509 {
1511 }
1512 else if (streq(p[0], "env-filter"))
1513 {
1514 int level = 0;
1515 if (p[1])
1516 {
1517 level = atoi(p[1]);
1518 }
1519 man_env_filter(man, level);
1520 }
1521 else if (streq(p[0], "signal"))
1522 {
1523 if (man_need(p, 1, 0))
1524 {
1525 man_signal(man, p[1]);
1526 }
1527 }
1528#ifdef TARGET_ANDROID
1529 else if (streq(p[0], "network-change"))
1530 {
1531 bool samenetwork = false;
1532 if (p[1] && streq(p[1], "samenetwork"))
1533 {
1534 samenetwork = true;
1535 }
1536
1537 man_network_change(man, samenetwork);
1538 }
1539#endif
1540 else if (streq(p[0], "load-stats"))
1541 {
1542 man_load_stats(man);
1543 }
1544 else if (streq(p[0], "status"))
1545 {
1546 int version = 0;
1547 if (p[1])
1548 {
1549 version = atoi(p[1]);
1550 }
1551 man_status(man, version, so);
1552 }
1553 else if (streq(p[0], "kill"))
1554 {
1555 if (man_need(p, 1, 0))
1556 {
1557 man_kill(man, p[1]);
1558 }
1559 }
1560 else if (streq(p[0], "verb"))
1561 {
1562 if (p[1])
1563 {
1564 const int level = atoi(p[1]);
1565 if (set_debug_level(level, 0))
1566 {
1567 msg(M_CLIENT, "SUCCESS: verb level changed");
1568 }
1569 else
1570 {
1571 msg(M_CLIENT, "ERROR: verb level is out of range");
1572 }
1573 }
1574 else
1575 {
1576 msg(M_CLIENT, "SUCCESS: verb=%u", get_debug_level());
1577 }
1578 }
1579 else if (streq(p[0], "mute"))
1580 {
1581 if (p[1])
1582 {
1583 const int level = atoi(p[1]);
1584 if (set_mute_cutoff(level))
1585 {
1586 msg(M_CLIENT, "SUCCESS: mute level changed");
1587 }
1588 else
1589 {
1590 msg(M_CLIENT, "ERROR: mute level is out of range");
1591 }
1592 }
1593 else
1594 {
1595 msg(M_CLIENT, "SUCCESS: mute=%d", get_mute_cutoff());
1596 }
1597 }
1598 else if (streq(p[0], "auth-retry"))
1599 {
1600 if (p[1])
1601 {
1602 if (auth_retry_set(M_CLIENT, p[1]))
1603 {
1604 msg(M_CLIENT, "SUCCESS: auth-retry parameter changed");
1605 }
1606 else
1607 {
1608 msg(M_CLIENT, "ERROR: bad auth-retry parameter");
1609 }
1610 }
1611 else
1612 {
1613 msg(M_CLIENT, "SUCCESS: auth-retry=%s", auth_retry_print());
1614 }
1615 }
1616 else if (streq(p[0], "state"))
1617 {
1618 if (!p[1])
1619 {
1620 man_state(man, "1");
1621 }
1622 else
1623 {
1624 man_state(man, p[1]);
1625 if (p[2])
1626 {
1627 man_state(man, p[2]);
1628 }
1629 }
1630 }
1631 else if (streq(p[0], "log"))
1632 {
1633 if (man_need(p, 1, MN_AT_LEAST))
1634 {
1635 if (p[1])
1636 {
1637 man_log(man, p[1]);
1638 }
1639 if (p[2])
1640 {
1641 man_log(man, p[2]);
1642 }
1643 }
1644 }
1645 else if (streq(p[0], "echo"))
1646 {
1647 if (man_need(p, 1, MN_AT_LEAST))
1648 {
1649 if (p[1])
1650 {
1651 man_echo(man, p[1]);
1652 }
1653 if (p[2])
1654 {
1655 man_echo(man, p[2]);
1656 }
1657 }
1658 }
1659 else if (streq(p[0], "username"))
1660 {
1661 if (man_need(p, 2, 0))
1662 {
1663 man_query_username(man, p[1], p[2]);
1664 }
1665 }
1666 else if (streq(p[0], "password"))
1667 {
1668 if (man_need(p, 1, MN_AT_LEAST))
1669 {
1670 if (p[2])
1671 {
1672 man_query_password(man, p[1], p[2]);
1673 }
1675 {
1676 man_query_password_base64(man, p[1]);
1677 }
1678 else
1679 {
1680 msg(M_CLIENT, "ERROR: the 'password' command requires 2 parameters");
1681 }
1682 }
1683 }
1684 else if (streq(p[0], "forget-passwords"))
1685 {
1687 }
1688 else if (streq(p[0], "needok"))
1689 {
1690 if (man_need(p, 2, 0))
1691 {
1692 man_query_need_ok(man, p[1], p[2]);
1693 }
1694 }
1695 else if (streq(p[0], "needstr"))
1696 {
1697 if (man_need(p, 2, 0))
1698 {
1699 man_query_need_str(man, p[1], p[2]);
1700 }
1701 }
1702 else if (streq(p[0], "cr-response"))
1703 {
1704 if (man_need(p, 1, 0))
1705 {
1706 man_send_cc_message(man, "CR_RESPONSE", p[1]);
1707 }
1708 }
1709 else if (streq(p[0], "net"))
1710 {
1711 man_net(man);
1712 }
1713 else if (streq(p[0], "hold"))
1714 {
1715 man_hold(man, p[1]);
1716 }
1717 else if (streq(p[0], "bytecount"))
1718 {
1719 if (man_need(p, 1, 0))
1720 {
1721 man_bytecount(man, atoi(p[1]));
1722 }
1723 }
1724 else if (streq(p[0], "client-kill"))
1725 {
1726 if (man_need(p, 1, MN_AT_LEAST))
1727 {
1728 man_client_kill(man, p[1], p[2]);
1729 }
1730 }
1731 else if (streq(p[0], "client-deny"))
1732 {
1733 if (man_need(p, 3, MN_AT_LEAST))
1734 {
1735 man_client_deny(man, p[1], p[2], p[3], p[4]);
1736 }
1737 }
1738 else if (streq(p[0], "client-auth-nt"))
1739 {
1740 if (man_need(p, 2, 0))
1741 {
1742 man_client_auth(man, p[1], p[2], false);
1743 }
1744 }
1745 else if (streq(p[0], "client-auth"))
1746 {
1747 if (man_need(p, 2, 0))
1748 {
1749 man_client_auth(man, p[1], p[2], true);
1750 }
1751 }
1752 else if (streq(p[0], "client-pending-auth"))
1753 {
1754 if (man_need(p, 4, 0))
1755 {
1756 man_client_pending_auth(man, p[1], p[2], p[3], p[4]);
1757 }
1758 }
1759 else if (streq(p[0], "rsa-sig"))
1760 {
1761 man_pk_sig(man, "rsa-sig");
1762 }
1763 else if (streq(p[0], "pk-sig"))
1764 {
1765 man_pk_sig(man, "pk-sig");
1766 }
1767 else if (streq(p[0], "certificate"))
1768 {
1769 man_certificate(man);
1770 }
1771#ifdef ENABLE_PKCS11
1772 else if (streq(p[0], "pkcs11-id-count"))
1773 {
1774 man_pkcs11_id_count(man);
1775 }
1776 else if (streq(p[0], "pkcs11-id-get"))
1777 {
1778 if (man_need(p, 1, 0))
1779 {
1780 man_pkcs11_id_get(man, atoi(p[1]));
1781 }
1782 }
1783#endif
1784 else if (streq(p[0], "remote-entry-count"))
1785 {
1787 }
1788 else if (streq(p[0], "remote-entry-get"))
1789 {
1790 if (man_need(p, 1, MN_AT_LEAST))
1791 {
1792 man_remote_entry_get(man, p[1], p[2]);
1793 }
1794 }
1795 else if (streq(p[0], "proxy"))
1796 {
1797 if (man_need(p, 1, MN_AT_LEAST))
1798 {
1799 man_proxy(man, p);
1800 }
1801 }
1802 else if (streq(p[0], "remote"))
1803 {
1804 if (man_need(p, 1, MN_AT_LEAST))
1805 {
1806 man_remote(man, p);
1807 }
1808 }
1809 else if (streq(p[0], "push-update-broad"))
1810 {
1811 if (man_need(p, 1, 0))
1812 {
1814 }
1815 }
1816 else if (streq(p[0], "push-update-cid"))
1817 {
1818 if (man_need(p, 2, 0))
1819 {
1820 man_push_update(man, p, UPT_BY_CID);
1821 }
1822 }
1823#if 1
1824 else if (streq(p[0], "test"))
1825 {
1826 if (man_need(p, 1, 0))
1827 {
1828 int i;
1829 const int n = atoi(p[1]);
1830 for (i = 0; i < n; ++i)
1831 {
1832 msg(M_CLIENT,
1833 "[%d] The purpose of this command is to generate large amounts of output.", i);
1834 }
1835 }
1836 }
1837#endif
1838 else
1839 {
1840 msg(M_CLIENT, "ERROR: unknown command [%s], enter 'help' for more options", p[0]);
1841 }
1842
1843done:
1844 gc_free(&gc);
1845}
1846
1847#ifdef _WIN32
1848
1849static void
1851{
1852 switch (man->connection.state)
1853 {
1854 case MS_LISTEN:
1855 net_event_win32_start(&man->connection.ne32, FD_ACCEPT, man->connection.sd_top);
1856 break;
1857
1858 case MS_CC_WAIT_READ:
1859 case MS_CC_WAIT_WRITE:
1860 net_event_win32_start(&man->connection.ne32, FD_READ | FD_WRITE | FD_CLOSE,
1861 man->connection.sd_cli);
1862 break;
1863
1864 default:
1865 ASSERT(0);
1866 }
1867}
1868
1869static void
1871{
1873}
1874
1875#endif /* ifdef _WIN32 */
1876
1877static void
1879{
1880 man->connection.state_realtime = false;
1881 man->connection.log_realtime = false;
1882 man->connection.echo_realtime = false;
1884 man->connection.password_verified = false;
1885 man->connection.password_tries = 0;
1886 man->connection.halt = false;
1888}
1889
1890static void
1891man_new_connection_post(struct management *man, const char *description)
1892{
1893 struct gc_arena gc = gc_new();
1894
1896
1898
1899#ifdef _WIN32
1900 man_start_ne32(man);
1901#endif
1902
1903#if UNIX_SOCK_SUPPORT
1904 if (man->settings.flags & MF_UNIX_SOCK)
1905 {
1906 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description,
1907 sockaddr_unix_name(&man->settings.local_unix, "NULL"));
1908 }
1909 else
1910#endif
1912 {
1913 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description,
1914 print_sockaddr(man->settings.local->ai_addr, &gc));
1915 }
1916 else
1917 {
1918 struct sockaddr_storage addr;
1919 socklen_t addrlen = sizeof(addr);
1920 if (!getpeername(man->connection.sd_cli, (struct sockaddr *)&addr, &addrlen))
1921 {
1922 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description,
1923 print_sockaddr((struct sockaddr *)&addr, &gc));
1924 }
1925 else
1926 {
1927 msg(D_MANAGEMENT, "MANAGEMENT: %s %s", description, "unknown");
1928 }
1929 }
1930
1932
1933 if (!man_password_needed(man))
1934 {
1935 man_welcome(man);
1936 }
1937 man_prompt(man);
1939
1940 gc_free(&gc);
1941}
1942
1943#if UNIX_SOCK_SUPPORT
1944static bool
1945man_verify_unix_peer_uid_gid(struct management *man, const socket_descriptor_t sd)
1946{
1947 if (socket_defined(sd) && (man->settings.user.user_valid || man->settings.group.group_valid))
1948 {
1949 static const char err_prefix[] =
1950 "MANAGEMENT: unix domain socket client connection rejected --";
1951 uid_t uid;
1952 gid_t gid;
1953 if (unix_socket_get_peer_uid_gid(man->connection.sd_cli, &uid, &gid))
1954 {
1955 if (man->settings.user.user_valid && man->settings.user.uid != uid)
1956 {
1958 "%s UID of socket peer (%d) doesn't match required value (%d) as given by --management-client-user",
1959 err_prefix, uid, man->settings.user.uid);
1960 return false;
1961 }
1962 if (man->settings.group.group_valid && man->settings.group.gid != gid)
1963 {
1965 "%s GID of socket peer (%d) doesn't match required value (%d) as given by --management-client-group",
1966 err_prefix, gid, man->settings.group.gid);
1967 return false;
1968 }
1969 }
1970 else
1971 {
1972 msg(D_MANAGEMENT, "%s cannot get UID/GID of socket peer", err_prefix);
1973 return false;
1974 }
1975 }
1976 return true;
1977}
1978#endif /* if UNIX_SOCK_SUPPORT */
1979
1980static void
1982{
1983 struct link_socket_actual act;
1984 CLEAR(act);
1985
1986 /*
1987 * Accept the TCP or Unix domain socket client.
1988 */
1989#if UNIX_SOCK_SUPPORT
1990 if (man->settings.flags & MF_UNIX_SOCK)
1991 {
1992 struct sockaddr_un remote;
1993 man->connection.sd_cli = socket_accept_unix(man->connection.sd_top, &remote);
1994 if (!man_verify_unix_peer_uid_gid(man, man->connection.sd_cli))
1995 {
1996 sd_close(&man->connection.sd_cli);
1997 }
1998 }
1999 else
2000#endif
2001 {
2002 man->connection.sd_cli = socket_do_accept(man->connection.sd_top, &act, false);
2003 }
2004
2006 {
2007 man->connection.remote = act.dest;
2008
2010 {
2011#ifdef _WIN32
2012 man_stop_ne32(man);
2013#endif
2014 }
2015
2016 man_new_connection_post(man, "Client connected from");
2017 }
2018}
2019
2020static void
2022{
2023 struct gc_arena gc = gc_new();
2024
2025 /*
2026 * Initialize state
2027 */
2028 man->connection.state = MS_LISTEN;
2030
2031 /*
2032 * Initialize listening socket
2033 */
2035 {
2036#if UNIX_SOCK_SUPPORT
2037 if (man->settings.flags & MF_UNIX_SOCK)
2038 {
2040 man->connection.sd_top = create_socket_unix();
2041 socket_bind_unix(man->connection.sd_top, &man->settings.local_unix, "MANAGEMENT");
2042 }
2043 else
2044#endif
2045 {
2047 socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
2048 "MANAGEMENT", false);
2049 }
2050
2051 /*
2052 * Listen for connection
2053 */
2054 if (listen(man->connection.sd_top, 1))
2055 {
2056 msg(M_ERR, "MANAGEMENT: listen() failed");
2057 }
2058
2059 /*
2060 * Set misc socket properties
2061 */
2063
2064#if UNIX_SOCK_SUPPORT
2065 if (man->settings.flags & MF_UNIX_SOCK)
2066 {
2067 msg(D_MANAGEMENT, "MANAGEMENT: unix domain socket listening on %s",
2068 sockaddr_unix_name(&man->settings.local_unix, "NULL"));
2069 }
2070 else
2071#endif
2072 {
2073 const struct sockaddr *man_addr = man->settings.local->ai_addr;
2074 struct sockaddr_storage addr;
2075 socklen_t addrlen = sizeof(addr);
2076 if (!getsockname(man->connection.sd_top, (struct sockaddr *)&addr, &addrlen))
2077 {
2078 man_addr = (struct sockaddr *)&addr;
2079 }
2080 else
2081 {
2082 msg(M_WARN | M_ERRNO, "Failed to get the management socket address");
2083 }
2084 msg(D_MANAGEMENT, "MANAGEMENT: TCP Socket listening on %s",
2085 print_sockaddr(man_addr, &gc));
2086 }
2087 }
2088
2089#ifdef _WIN32
2090 man_start_ne32(man);
2091#endif
2092
2093 gc_free(&gc);
2094}
2095
2096static void
2098{
2099 struct gc_arena gc = gc_new();
2100 int status;
2101 int signal_received = 0;
2102
2103 /*
2104 * Initialize state
2105 */
2108
2109#if UNIX_SOCK_SUPPORT
2110 if (man->settings.flags & MF_UNIX_SOCK)
2111 {
2112 man->connection.sd_cli = create_socket_unix();
2113 status = socket_connect_unix(man->connection.sd_cli, &man->settings.local_unix);
2114 if (!status && !man_verify_unix_peer_uid_gid(man, man->connection.sd_cli))
2115 {
2116#ifdef EPERM
2117 status = EPERM;
2118#else
2119 status = 1;
2120#endif
2121 sd_close(&man->connection.sd_cli);
2122 }
2123 }
2124 else
2125#endif
2126 {
2128 status = openvpn_connect(man->connection.sd_cli, man->settings.local->ai_addr, 5,
2129 &signal_received);
2130 }
2131
2132 if (signal_received)
2133 {
2134 throw_signal(signal_received);
2135 goto done;
2136 }
2137
2138 if (status)
2139 {
2140#if UNIX_SOCK_SUPPORT
2141 if (man->settings.flags & MF_UNIX_SOCK)
2142 {
2143 msg(D_LINK_ERRORS | M_ERRNO, "MANAGEMENT: connect to unix socket %s failed",
2144 sockaddr_unix_name(&man->settings.local_unix, "NULL"));
2145 }
2146 else
2147#endif
2148 {
2149 msg(D_LINK_ERRORS | M_ERRNO, "MANAGEMENT: connect to %s failed",
2150 print_sockaddr(man->settings.local->ai_addr, &gc));
2151 }
2152 throw_signal_soft(SIGTERM, "management-connect-failed");
2153 goto done;
2154 }
2155
2156 man_new_connection_post(man, "Connected to management server at");
2157
2158done:
2159 gc_free(&gc);
2160}
2161
2162static void
2163man_reset_client_socket(struct management *man, const bool exiting)
2164{
2166 {
2167 man_bytecount_stop(man);
2168#ifdef _WIN32
2169 man_stop_ne32(man);
2170#endif
2177 msg(D_MANAGEMENT, "MANAGEMENT: Client disconnected");
2178 }
2179 if (!exiting)
2180 {
2182 {
2183 msg(D_MANAGEMENT, "MANAGEMENT: Reset authentication on disconnect");
2184 ssl_purge_auth(false);
2185 (void)ssl_clean_auth_token();
2186 }
2187
2188 if (man->settings.flags & MF_SIGNAL && !man_password_needed(man))
2189 {
2190 int mysig = man_mod_signal(man, SIGUSR1);
2191 if (mysig >= 0)
2192 {
2193 msg(D_MANAGEMENT, "MANAGEMENT: Triggering management signal");
2194 throw_signal_soft(mysig, "management-disconnect");
2195 }
2196 }
2197
2199 {
2200 msg(D_MANAGEMENT, "MANAGEMENT: Triggering management exit");
2201 throw_signal_soft(SIGTERM, "management-exit");
2202 }
2203 else
2204 {
2205 man_listen(man);
2206 }
2207 }
2208}
2209
2210static void
2211man_process_command(struct management *man, const char *line)
2212{
2213 struct gc_arena gc = gc_new();
2214 struct status_output *so;
2215 int nparms;
2216 char *parms[MAX_PARMS + 1];
2217
2218 CLEAR(parms);
2219 so = status_open(NULL, 0, -1, &man->persist.vout, 0);
2221
2222 if (man_password_needed(man))
2223 {
2224 man_check_password(man, line);
2225 }
2226 else
2227 {
2228 nparms = parse_line(line, parms, MAX_PARMS, "TCP", 0, M_CLIENT, &gc);
2229 if (parms[0] && streq(parms[0], "password"))
2230 {
2231 msg(D_MANAGEMENT_DEBUG, "MANAGEMENT: CMD 'password [...]'");
2232 }
2233 else if (!streq(line, "load-stats"))
2234 {
2235 msg(D_MANAGEMENT_DEBUG, "MANAGEMENT: CMD '%s'", line);
2236 }
2237
2238#if 0
2239 /* DEBUGGING -- print args */
2240 {
2241 int i;
2242 for (i = 0; i < nparms; ++i)
2243 {
2244 msg(M_INFO, "[%d] '%s'", i, parms[i]);
2245 }
2246 }
2247#endif
2248
2249 if (nparms > 0)
2250 {
2251 man_dispatch_command(man, so, (const char **)parms, nparms);
2252 }
2253 }
2254
2255 CLEAR(parms);
2256 status_close(so);
2257 gc_free(&gc);
2258}
2259
2260static bool
2261man_io_error(struct management *man, const char *prefix)
2262{
2263 bool crt_error = false;
2264 int err = openvpn_errno_maybe_crt(&crt_error);
2265
2266 if (!ignore_sys_error(err, crt_error))
2267 {
2268 struct gc_arena gc = gc_new();
2269 msg(D_MANAGEMENT, "MANAGEMENT: TCP %s error: %s", prefix, strerror(err));
2270 gc_free(&gc);
2271 return true;
2272 }
2273 else
2274 {
2275 return false;
2276 }
2277}
2278
2279#ifdef TARGET_ANDROID
2280static ssize_t
2281man_send_with_fd(int fd, const void *ptr, size_t nbytes, int flags, int sendfd)
2282{
2283 struct msghdr msg = { 0 };
2284 struct iovec iov[1];
2285
2286 union
2287 {
2288 struct cmsghdr cm;
2289 char control[CMSG_SPACE(sizeof(int))];
2290 } control_un;
2291 struct cmsghdr *cmptr;
2292
2293 msg.msg_control = control_un.control;
2294 msg.msg_controllen = sizeof(control_un.control);
2295
2296 cmptr = CMSG_FIRSTHDR(&msg);
2297 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
2298 cmptr->cmsg_level = SOL_SOCKET;
2299 cmptr->cmsg_type = SCM_RIGHTS;
2300 *((int *)CMSG_DATA(cmptr)) = sendfd;
2301
2302 msg.msg_name = NULL;
2303 msg.msg_namelen = 0;
2304
2305 /* sendmsg takes a const msghdr, but we can't construct that here
2306 directly, so cast */
2307 iov[0].iov_base = (void *)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
2518 buffer_list_aggregate(man->connection.out, size_hint);
2519 const struct buffer *buf = buffer_list_peek(man->connection.out);
2520 if (buf && BLEN(buf))
2521 {
2522 const int len = min_int(size_hint, BLEN(buf));
2523#ifdef TARGET_ANDROID
2524 if (man->connection.fdtosend > 0)
2525 {
2526 sent = man_send_with_fd(man->connection.sd_cli, CBPTR(buf), len, MSG_NOSIGNAL,
2527 man->connection.fdtosend);
2528 man->connection.fdtosend = -1;
2529 }
2530 else
2531#endif
2532 {
2533 sent = send(man->connection.sd_cli, CBSTR(buf), len, MSG_NOSIGNAL);
2534 }
2535 if (sent >= 0)
2536 {
2537 buffer_list_advance(man->connection.out, sent);
2538 }
2539 else
2540 {
2541 if (man_io_error(man, "send"))
2542 {
2544 }
2545 }
2546 }
2547
2548 /*
2549 * Reset output state to MS_CC_WAIT_(READ|WRITE)
2550 */
2552
2553 return sent;
2554}
2555
2556static void
2558{
2559 CLEAR(*mc);
2560
2561 /* set initial state */
2562 mc->state = MS_INITIAL;
2563
2564 /* clear socket descriptors */
2565 mc->sd_top = SOCKET_UNDEFINED;
2566 mc->sd_cli = SOCKET_UNDEFINED;
2567}
2568
2569static void
2570man_persist_init(struct management *man, const int log_history_cache, const int echo_buffer_size,
2571 const int state_buffer_size)
2572{
2573 struct man_persist *mp = &man->persist;
2574 if (!mp->defined)
2575 {
2576 CLEAR(*mp);
2577
2578 /* initialize log history store */
2579 mp->log = log_history_init(log_history_cache);
2580
2581 /*
2582 * Initialize virtual output object, so that functions
2583 * which write to a virtual_output object can be redirected
2584 * here to the management object.
2585 */
2587 mp->vout.arg = man;
2590
2591 /*
2592 * Initialize --echo list
2593 */
2594 man->persist.echo = log_history_init(echo_buffer_size);
2595
2596 /*
2597 * Initialize --state list
2598 */
2599 man->persist.state = log_history_init(state_buffer_size);
2600
2601 mp->defined = true;
2602 }
2603}
2604
2605static void
2607{
2608 if (mp->log)
2609 {
2612 }
2613
2614 if (mp->echo)
2615 {
2617 }
2618
2619 if (mp->state)
2620 {
2622 }
2623
2624 CLEAR(*mp);
2625}
2626
2627static void
2628man_settings_init(struct man_settings *ms, const char *addr, const char *port,
2629 const char *pass_file, const char *client_user, const char *client_group,
2630 const int log_history_cache, const int echo_buffer_size,
2631 const int state_buffer_size, const int remap_sigusr1, const unsigned int flags)
2632{
2633 if (!ms->defined)
2634 {
2635 CLEAR(*ms);
2636
2637 ms->flags = flags;
2638
2639 /*
2640 * Get username/password
2641 */
2642 if (pass_file)
2643 {
2644 get_user_pass(&ms->up, pass_file, "Management", GET_USER_PASS_PASSWORD_ONLY);
2645 }
2646
2647#if UNIX_SOCK_SUPPORT
2648 /*
2649 * lookup client UID/GID if specified
2650 */
2651 if (client_user)
2652 {
2653 ASSERT(platform_user_get(client_user, &ms->user));
2654 msg(D_MANAGEMENT, "MANAGEMENT: client_uid=%d", ms->user.uid);
2655 }
2656 if (client_group)
2657 {
2658 ASSERT(platform_group_get(client_group, &ms->group));
2659 msg(D_MANAGEMENT, "MANAGEMENT: client_gid=%d", ms->group.gid);
2660 }
2661
2662 if (ms->flags & MF_UNIX_SOCK)
2663 {
2664 sockaddr_unix_init(&ms->local_unix, addr);
2665 }
2666 else
2667#endif
2668 {
2669 /*
2670 * Run management over tunnel, or
2671 * separate channel?
2672 */
2673 if (streq(addr, "tunnel") && !(flags & MF_CONNECT_AS_CLIENT))
2674 {
2675 ms->management_over_tunnel = true;
2676 }
2677 else
2678 {
2679 int status;
2681
2682 if (!(flags & MF_CONNECT_AS_CLIENT))
2683 {
2684 resolve_flags |= GETADDR_PASSIVE;
2685 }
2686
2687 status =
2688 openvpn_getaddrinfo(resolve_flags, addr, port, 0, NULL, AF_UNSPEC, &ms->local);
2689 ASSERT(status == 0);
2690 }
2691 }
2692
2693 /*
2694 * Log history and echo buffer may need to be resized
2695 */
2696 ms->log_history_cache = log_history_cache;
2697 ms->echo_buffer_size = echo_buffer_size;
2698 ms->state_buffer_size = state_buffer_size;
2699
2700 /*
2701 * Set remap sigusr1 flags
2702 */
2703 if (remap_sigusr1 == SIGHUP)
2704 {
2706 }
2707 else if (remap_sigusr1 == SIGTERM)
2708 {
2710 }
2711
2712 ms->defined = true;
2713 }
2714}
2715
2716static void
2718{
2719 if (ms->local)
2720 {
2721 freeaddrinfo(ms->local);
2722 }
2723 CLEAR(*ms);
2724}
2725
2726
2727static void
2729{
2730 if (man->connection.state == MS_INITIAL)
2731 {
2732#ifdef _WIN32
2733 /*
2734 * This object is a sort of TCP/IP helper
2735 * for Windows.
2736 */
2738#endif
2739
2740 /*
2741 * Allocate helper objects for command line input and
2742 * command output from/to the socket.
2743 */
2744 man->connection.in = command_line_new(1024);
2746
2747 /*
2748 * Initialize event set for standalone usage, when we are
2749 * running outside of the primary event loop.
2750 */
2751 {
2752 int maxevents = 1;
2753 man->connection.es = event_set_init(&maxevents, EVENT_METHOD_FAST);
2754 }
2755
2756 man->connection.client_version = MCV_DEFAULT; /* default version */
2757
2758 /*
2759 * Listen/connect socket
2760 */
2762 {
2763 man_connect(man);
2764 }
2765 else
2766 {
2767 man_listen(man);
2768 }
2769 }
2770}
2771
2772static void
2774{
2775 struct man_connection *mc = &man->connection;
2776
2777 event_free(mc->es);
2778#ifdef _WIN32
2780#endif
2781 if (socket_defined(mc->sd_top))
2782 {
2783 man_close_socket(man, mc->sd_top);
2785 }
2786 if (socket_defined(mc->sd_cli))
2787 {
2788 man_close_socket(man, mc->sd_cli);
2789 }
2790
2791 command_line_free(mc->in);
2792 buffer_list_free(mc->out);
2793
2795
2799}
2800
2801struct management *
2803{
2804 struct management *man;
2805 ALLOC_OBJ_CLEAR(man, struct management);
2806
2809
2811
2812 return man;
2813}
2814
2815bool
2816management_open(struct management *man, const char *addr, const char *port, const char *pass_file,
2817 const char *client_user, const char *client_group, const int log_history_cache,
2818 const int echo_buffer_size, const int state_buffer_size, const int remap_sigusr1,
2819 const unsigned int flags)
2820{
2821 bool ret = false;
2822
2823 /*
2824 * Save the settings only if they have not
2825 * been saved before.
2826 */
2827 man_settings_init(&man->settings, addr, port, pass_file, client_user, client_group,
2828 log_history_cache, echo_buffer_size, state_buffer_size, remap_sigusr1, flags);
2829
2830 /*
2831 * The log is initially sized to MANAGEMENT_LOG_HISTORY_INITIAL_SIZE,
2832 * but may be changed here. Ditto for echo and state buffers.
2833 */
2837
2838 /*
2839 * If connection object is uninitialized and we are not doing
2840 * over-the-tunnel management, then open (listening) connection.
2841 */
2842 if (man->connection.state == MS_INITIAL)
2843 {
2845 {
2847 ret = true;
2848 }
2849 }
2850
2851 return ret;
2852}
2853
2854void
2856{
2857 man_output_list_push_finalize(man); /* flush output queue */
2861 free(man);
2862}
2863
2864void
2866{
2867 man->persist.standalone_disabled = true;
2868 man->persist.callback = *cb;
2869}
2870
2871void
2873{
2874 man->persist.standalone_disabled = false;
2875 man->persist.hold_release = false;
2876 CLEAR(man->persist.callback);
2877 man_output_list_push_finalize(man); /* flush output queue */
2878}
2879
2880void
2881management_set_state(struct management *man, const int state, const char *detail,
2882 const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6,
2883 const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
2884{
2885 if (man->persist.state
2886 && (!(man->settings.flags & MF_SERVER) || state < OPENVPN_STATE_CLIENT_BASE))
2887 {
2888 struct gc_arena gc = gc_new();
2889 struct log_entry e;
2890 const char *out = NULL;
2891
2892 update_time();
2893 CLEAR(e);
2894 e.timestamp = now;
2895 e.u.state = state;
2896 e.string = detail;
2897 if (tun_local_ip)
2898 {
2899 e.local_ip = *tun_local_ip;
2900 }
2901 if (tun_local_ip6)
2902 {
2903 e.local_ip6 = *tun_local_ip6;
2904 }
2905 if (local)
2906 {
2907 e.local_sock = *local;
2908 }
2909 if (remote)
2910 {
2911 e.remote_sock = *remote;
2912 }
2913
2914 log_history_add(man->persist.state, &e);
2915
2916 if (man->connection.state_realtime)
2917 {
2918 out = log_entry_print(&e,
2922 &gc);
2923 }
2924
2925 if (out)
2926 {
2927 man_output_list_push(man, out);
2928 }
2929
2930 gc_free(&gc);
2931 }
2932}
2933
2934static bool
2935env_filter_match(const char *env_str, const int env_filter_level)
2936{
2937 static const char *env_names[] = { "username=",
2938 "password=",
2939 "X509_0_CN=",
2940 "tls_serial_",
2941 "untrusted_ip=",
2942 "ifconfig_local=",
2943 "ifconfig_netmask=",
2944 "daemon_start_time=",
2945 "daemon_pid=",
2946 "dev=",
2947 "ifconfig_pool_remote_ip=",
2948 "ifconfig_pool_netmask=",
2949 "time_duration=",
2950 "bytes_sent=",
2951 "bytes_received=",
2952 "session_id=",
2953 "session_state=" };
2954
2955 if (env_filter_level == 0)
2956 {
2957 return true;
2958 }
2959 else if (env_filter_level <= 1 && !strncmp(env_str, "X509_", 5))
2960 {
2961 return true;
2962 }
2963 else if (env_filter_level <= 2)
2964 {
2965 size_t i;
2966 for (i = 0; i < SIZE(env_names); ++i)
2967 {
2968 const char *en = env_names[i];
2969 const size_t len = strlen(en);
2970 if (!strncmp(env_str, en, len))
2971 {
2972 return true;
2973 }
2974 }
2975 return false;
2976 }
2977 return false;
2978}
2979
2980static void
2981man_output_env(const struct env_set *es, const bool tail, const int env_filter_level,
2982 const char *prefix)
2983{
2984 if (es)
2985 {
2986 struct env_item *e;
2987 for (e = es->list; e != NULL; e = e->next)
2988 {
2989 if (e->string && (!env_filter_level || env_filter_match(e->string, env_filter_level)))
2990 {
2991 msg(M_CLIENT, ">%s:ENV,%s", prefix, e->string);
2992 }
2993 }
2994 }
2995 if (tail)
2996 {
2997 msg(M_CLIENT, ">%s:ENV,END", prefix);
2998 }
2999}
3000
3001static void
3002man_output_extra_env(struct management *man, const char *prefix)
3003{
3004 struct gc_arena gc = gc_new();
3005 struct env_set *es = env_set_create(&gc);
3006 if (man->persist.callback.n_clients)
3007 {
3008 const int nclients = (*man->persist.callback.n_clients)(man->persist.callback.arg);
3009 setenv_int(es, "n_clients", nclients);
3010 }
3011 man_output_env(es, false, man->connection.env_filter_level, prefix);
3012 gc_free(&gc);
3013}
3014
3015void
3016management_up_down(struct management *man, const char *updown, const struct env_set *es)
3017{
3018 if (man->settings.flags & MF_UP_DOWN)
3019 {
3020 msg(M_CLIENT, ">UPDOWN:%s", updown);
3021 man_output_env(es, true, 0, "UPDOWN");
3022 }
3023}
3024
3025void
3026management_notify(const char *severity, const char *type, const char *text)
3027{
3028 msg(M_CLIENT, ">NOTIFY:%s,%s,%s", severity, type, text);
3029}
3030
3031void
3033{
3034 msg(M_CLIENT, "%s", str);
3035}
3036
3037static void
3039{
3041 {
3042 const char *peer_info =
3043 (*man->persist.callback.get_peer_info)(man->persist.callback.arg, mdac->cid);
3044 if (peer_info)
3045 {
3046 char line[256];
3047 struct buffer buf;
3048
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 *man, 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);
3078 man_output_extra_env(man, "CLIENT");
3079 if (man->connection.env_filter_level > 0)
3080 {
3082 }
3083 man_output_env(es, true, man->connection.env_filter_level, "CLIENT");
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);
3114 man_output_extra_env(man, "CLIENT");
3115 man_output_env(es, true, man->connection.env_filter_level, "CLIENT");
3116}
3117
3118void
3120 struct man_def_auth_context *mdac,
3121 const struct env_set *es)
3122{
3123 if ((mdac->flags & DAF_INITIAL_AUTH) && !(mdac->flags & DAF_CONNECTION_CLOSED))
3124 {
3125 msg(M_CLIENT, ">CLIENT:DISCONNECT,%lu", mdac->cid);
3126 man_output_env(es, true, man->connection.env_filter_level, "CLIENT");
3128 }
3129}
3130
3131void
3133 const struct mroute_addr *addr, const bool primary)
3134{
3135 struct gc_arena gc = gc_new();
3136 if ((mdac->flags & DAF_INITIAL_AUTH) && !(mdac->flags & DAF_CONNECTION_CLOSED))
3137 {
3138 msg(M_CLIENT, ">CLIENT:ADDRESS,%lu,%s,%d", mdac->cid,
3139 mroute_addr_print_ex(addr, MAPF_SUBNET, &gc), BOOL_CAST(primary));
3140 }
3141 gc_free(&gc);
3142}
3143
3144void
3145management_echo(struct management *man, const char *string, const bool pull)
3146{
3147 if (man->persist.echo)
3148 {
3149 struct gc_arena gc = gc_new();
3150 struct log_entry e;
3151 const char *out = NULL;
3152
3153 update_time();
3154 CLEAR(e);
3155 e.timestamp = now;
3156 e.string = string;
3157 e.u.intval = BOOL_CAST(pull);
3158
3159 log_history_add(man->persist.echo, &e);
3160
3161 if (man->connection.echo_realtime)
3162 {
3163 out = log_entry_print(&e,
3166 &gc);
3167 }
3168
3169 if (out)
3170 {
3171 man_output_list_push(man, out);
3172 }
3173
3174 gc_free(&gc);
3175 }
3176}
3177
3178void
3179management_post_tunnel_open(struct management *man, const in_addr_t tun_local_ip)
3180{
3181 /*
3182 * If we are running management over the tunnel,
3183 * this is the place to initialize the connection.
3184 */
3186 {
3187 /* listen on our local TUN/TAP IP address */
3188 struct in_addr ia;
3189 int ret;
3190 char buf[INET_ADDRSTRLEN];
3191
3192 ia.s_addr = htonl(tun_local_ip);
3193 inet_ntop(AF_INET, &ia, buf, sizeof(buf));
3194 ret =
3195 openvpn_getaddrinfo(GETADDR_PASSIVE, buf, NULL, 0, NULL, AF_INET, &man->settings.local);
3196 ASSERT(ret == 0);
3198 }
3199}
3200
3201void
3203{
3205 {
3207 }
3208}
3209
3210void
3211management_auth_failure(struct management *man, const char *type, const char *reason)
3212{
3213 if (reason)
3214 {
3215 msg(M_CLIENT, ">PASSWORD:Verification Failed: '%s' ['%s']", type, reason);
3216 }
3217 else
3218 {
3219 msg(M_CLIENT, ">PASSWORD:Verification Failed: '%s'", type);
3220 }
3221}
3222
3223void
3224management_auth_token(const char *token)
3225{
3226 msg(M_CLIENT, ">PASSWORD:Auth-Token:%s", token);
3227}
3228
3229static inline bool
3230man_persist_state(unsigned int *persistent, const int n)
3231{
3232 if (persistent)
3233 {
3234 if (*persistent == (unsigned int)n)
3235 {
3236 return false;
3237 }
3238 *persistent = n;
3239 }
3240 return true;
3241}
3242
3243#ifdef _WIN32
3244
3245void
3246management_socket_set(struct management *man, struct event_set *es, void *arg,
3247 unsigned int *persistent)
3248{
3249 if (man->connection.state != MS_INITIAL)
3250 {
3253
3254 switch (man->connection.state)
3255 {
3256 case MS_LISTEN:
3257 if (man_persist_state(persistent, 1))
3258 {
3259 event_ctl(es, ev, EVENT_READ, arg);
3260 }
3261 break;
3262
3263 case MS_CC_WAIT_READ:
3264 if (man_persist_state(persistent, 2))
3265 {
3266 event_ctl(es, ev, EVENT_READ, arg);
3267 }
3268 break;
3269
3270 case MS_CC_WAIT_WRITE:
3271 if (man_persist_state(persistent, 3))
3272 {
3273 event_ctl(es, ev, EVENT_READ | EVENT_WRITE, arg);
3274 }
3275 break;
3276
3277 default:
3278 ASSERT(0);
3279 }
3280 }
3281}
3282
3283void
3285{
3286 if (man->connection.state != MS_INITIAL)
3287 {
3288 long net_events;
3291
3292 if (net_events & FD_CLOSE)
3293 {
3294 man_reset_client_socket(man, false);
3295 }
3296 else
3297 {
3298 if (man->connection.state == MS_LISTEN)
3299 {
3300 if (net_events & FD_ACCEPT)
3301 {
3302 man_accept(man);
3304 }
3305 }
3306 else if (man->connection.state == MS_CC_WAIT_READ
3308 {
3309 if (net_events & FD_READ)
3310 {
3311 while (man_read(man) > 0)
3312 {
3313 }
3315 }
3316
3317 if (net_events & FD_WRITE)
3318 {
3319 ssize_t status = man_write(man);
3320 if (status < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
3321 {
3323 }
3324 }
3325 }
3326 }
3327 }
3328}
3329
3330#else /* ifdef _WIN32 */
3331
3332void
3333management_socket_set(struct management *man, struct event_set *es, void *arg,
3334 unsigned int *persistent)
3335{
3336 switch (man->connection.state)
3337 {
3338 case MS_LISTEN:
3339 if (man_persist_state(persistent, 1))
3340 {
3341 event_ctl(es, man->connection.sd_top, EVENT_READ, arg);
3342 }
3343 break;
3344
3345 case MS_CC_WAIT_READ:
3346 if (man_persist_state(persistent, 2))
3347 {
3348 event_ctl(es, man->connection.sd_cli, EVENT_READ, arg);
3349 }
3350 break;
3351
3352 case MS_CC_WAIT_WRITE:
3353 if (man_persist_state(persistent, 3))
3354 {
3355 event_ctl(es, man->connection.sd_cli, EVENT_WRITE, arg);
3356 }
3357 break;
3358
3359 case MS_INITIAL:
3360 break;
3361
3362 default:
3363 ASSERT(0);
3364 }
3365}
3366
3367void
3368management_io(struct management *man)
3369{
3370 switch (man->connection.state)
3371 {
3372 case MS_LISTEN:
3373 man_accept(man);
3374 break;
3375
3376 case MS_CC_WAIT_READ:
3377 man_read(man);
3378 break;
3379
3380 case MS_CC_WAIT_WRITE:
3381 man_write(man);
3382 break;
3383
3384 case MS_INITIAL:
3385 break;
3386
3387 default:
3388 ASSERT(0);
3389 }
3390}
3391
3392#endif /* ifdef _WIN32 */
3393
3394static inline bool
3396{
3398}
3399
3400static bool
3401man_check_for_signals(volatile int *signal_received)
3402{
3403 if (signal_received)
3404 {
3405 get_signal(signal_received);
3406 if (*signal_received)
3407 {
3408 return true;
3409 }
3410 }
3411 return false;
3412}
3413
3414/*
3415 * Wait for socket I/O when outside primary event loop
3416 */
3417static int
3418man_block(struct management *man, volatile int *signal_received, const time_t expire)
3419{
3420 struct timeval tv;
3421 struct event_set_return esr;
3422 int status = -1;
3423
3424 if (man_standalone_ok(man))
3425 {
3426 /* expire time can be already overdue, for this case init zero
3427 * timeout to avoid waiting first time and exit loop early with
3428 * either obtained event or timeout.
3429 */
3430 tv.tv_usec = 0;
3431 tv.tv_sec = 0;
3432
3433 while (true)
3434 {
3436 management_socket_set(man, man->connection.es, NULL, NULL);
3437 if (man_check_for_signals(signal_received))
3438 {
3439 status = -1;
3440 break;
3441 }
3442 status = event_wait(man->connection.es, &tv, &esr, 1);
3443 update_time();
3444 if (man_check_for_signals(signal_received))
3445 {
3446 status = -1;
3447 break;
3448 }
3449
3450 if (status > 0)
3451 {
3452 break;
3453 }
3454 else if (expire && now >= expire)
3455 {
3456 /* set SIGINT signal if expiration time exceeded */
3457 status = 0;
3458 if (signal_received)
3459 {
3460 *signal_received = SIGINT;
3461 }
3462 break;
3463 }
3464
3465 /* wait one second more */
3466 tv.tv_sec = 1;
3467 tv.tv_usec = 0;
3468 }
3469 }
3470 return status;
3471}
3472
3473/*
3474 * Perform management socket output outside primary event loop
3475 */
3476static void
3477man_output_standalone(struct management *man, volatile int *signal_received)
3478{
3479 if (man_standalone_ok(man))
3480 {
3481 while (man->connection.state == MS_CC_WAIT_WRITE)
3482 {
3483 management_io(man);
3484 if (man->connection.state == MS_CC_WAIT_WRITE)
3485 {
3486 man_block(man, signal_received, 0);
3487 }
3488 if (signal_received && *signal_received)
3489 {
3490 break;
3491 }
3492 }
3493 }
3494}
3495
3496/*
3497 * Process management event loop outside primary event loop
3498 */
3499static int
3500man_standalone_event_loop(struct management *man, volatile int *signal_received,
3501 const time_t expire)
3502{
3503 int status = -1;
3504 if (man_standalone_ok(man))
3505 {
3506 status = man_block(man, signal_received, expire);
3507 if (status > 0)
3508 {
3509 management_io(man);
3510 }
3511 }
3512 return status;
3513}
3514
3515#define MWCC_PASSWORD_WAIT (1 << 0)
3516#define MWCC_HOLD_WAIT (1 << 1)
3517#define MWCC_OTHER_WAIT (1 << 2)
3518
3519/*
3520 * Block until client connects
3521 */
3522static void
3523man_wait_for_client_connection(struct management *man, volatile int *signal_received,
3524 const time_t expire, unsigned int flags)
3525{
3527 if (man->connection.state == MS_LISTEN)
3528 {
3529 if (flags & MWCC_PASSWORD_WAIT)
3530 {
3531 msg(D_MANAGEMENT, "Need password(s) from management interface, waiting...");
3532 }
3533 if (flags & MWCC_HOLD_WAIT)
3534 {
3535 msg(D_MANAGEMENT, "Need hold release from management interface, waiting...");
3536 }
3537 if (flags & MWCC_OTHER_WAIT)
3538 {
3539 msg(D_MANAGEMENT, "Need information from management interface, waiting...");
3540 }
3541 do
3542 {
3543 man_standalone_event_loop(man, signal_received, expire);
3544 if (signal_received && *signal_received)
3545 {
3546 break;
3547 }
3548 } while (man->connection.state == MS_LISTEN || man_password_needed(man));
3549 }
3550}
3551
3552/*
3553 * Process the management event loop for sec seconds
3554 */
3555void
3557{
3558 if (man_standalone_ok(man))
3559 {
3560 volatile int signal_received = 0;
3561 const bool standalone_disabled_save = man->persist.standalone_disabled;
3562 time_t expire = 0;
3563
3564 /* This is so M_CLIENT messages will be correctly passed through msg() */
3565 man->persist.standalone_disabled = false;
3566
3567 /* set expire time */
3568 update_time();
3569 if (sec >= 0)
3570 {
3571 expire = now + sec;
3572 }
3573
3574 /* if no client connection, wait for one */
3575 man_wait_for_client_connection(man, &signal_received, expire, 0);
3576 if (signal_received)
3577 {
3578 return;
3579 }
3580
3581 /* run command processing event loop */
3582 do
3583 {
3584 man_standalone_event_loop(man, &signal_received, expire);
3585 if (!signal_received)
3586 {
3587 man_check_for_signals(&signal_received);
3588 }
3589 if (signal_received)
3590 {
3591 return;
3592 }
3593 update_time();
3594 } while (expire && expire > now);
3595
3596 /* revert state */
3597 man->persist.standalone_disabled = standalone_disabled_save;
3598 }
3599 else if (sec > 0)
3600 {
3601 sleep(sec);
3602 }
3603}
3604
3605/*
3606 * Get a username/password from management channel in standalone mode.
3607 */
3608bool
3609management_query_user_pass(struct management *man, struct user_pass *up, const char *type,
3610 const unsigned int flags, const char *static_challenge)
3611{
3612 struct gc_arena gc = gc_new();
3613 bool ret = false;
3614
3615 if (man_standalone_ok(man))
3616 {
3617 volatile int signal_received = 0;
3618 const bool standalone_disabled_save = man->persist.standalone_disabled;
3619 struct buffer alert_msg = alloc_buf_gc(128, &gc);
3620 const char *alert_type = NULL;
3621 const char *prefix = NULL;
3622 unsigned int up_query_mode = 0;
3623 const char *sc = NULL;
3624 ret = true;
3625 /* This is so M_CLIENT messages will be correctly passed through msg() */
3626 man->persist.standalone_disabled = false;
3627 man->persist.special_state_msg = NULL;
3628
3629 CLEAR(man->connection.up_query);
3630
3631 if (flags & GET_USER_PASS_NEED_OK)
3632 {
3633 up_query_mode = UP_QUERY_NEED_OK;
3634 prefix = "NEED-OK";
3635 alert_type = "confirmation";
3636 }
3637 else if (flags & GET_USER_PASS_NEED_STR)
3638 {
3639 up_query_mode = UP_QUERY_NEED_STR;
3640 prefix = "NEED-STR";
3641 alert_type = "string";
3642 }
3643 else if (flags & GET_USER_PASS_PASSWORD_ONLY)
3644 {
3645 up_query_mode = UP_QUERY_PASS;
3646 prefix = "PASSWORD";
3647 alert_type = "password";
3648 }
3649 else if ((man->connection.client_version >= MCV_USERNAME_ONLY) && (flags & GET_USER_PASS_USERNAME_ONLY))
3650 {
3651 up_query_mode = UP_QUERY_USERNAME;
3652 prefix = "PASSWORD";
3653 alert_type = "username";
3654 }
3655 else
3656 {
3657 up_query_mode = UP_QUERY_USER_PASS;
3658 prefix = "PASSWORD";
3659 alert_type = "username/password";
3660 if (static_challenge)
3661 {
3663 }
3664 }
3665 buf_printf(&alert_msg, ">%s:Need '%s' %s", prefix, type, alert_type);
3666
3668 {
3669 buf_printf(&alert_msg, " MSG:%s", up->username);
3670 }
3671
3672 if (sc)
3673 {
3674 buf_printf(&alert_msg, " SC:%d,%s",
3677 sc);
3678 }
3679
3681 if (signal_received)
3682 {
3683 ret = false;
3684 }
3685
3686 if (ret)
3687 {
3688 man->persist.special_state_msg = BSTR(&alert_msg);
3689 msg(M_CLIENT, "%s", man->persist.special_state_msg);
3690
3691 /* tell command line parser which info we need */
3692 man->connection.up_query_mode = up_query_mode;
3693 man->connection.up_query_type = type;
3694
3695 /* run command processing event loop until we get our username/password/response */
3696 do
3697 {
3698 man_standalone_event_loop(man, &signal_received, 0);
3699 if (!signal_received)
3700 {
3701 man_check_for_signals(&signal_received);
3702 }
3703 if (signal_received)
3704 {
3705 ret = false;
3706 break;
3707 }
3708 } while (!man->connection.up_query.defined);
3709 }
3710
3711 /* revert state */
3712 man->connection.up_query_mode = UP_QUERY_DISABLED;
3713 man->connection.up_query_type = NULL;
3714 man->persist.standalone_disabled = standalone_disabled_save;
3715 man->persist.special_state_msg = NULL;
3716
3717 /* pass through blank passwords */
3718 if (!strcmp(man->connection.up_query.password, blank_up))
3719 {
3720 CLEAR(man->connection.up_query.password);
3721 }
3722
3723 /*
3724 * Transfer u/p to return object, zero any record
3725 * we hold in the management object.
3726 */
3727 if (ret)
3728 {
3729 /* preserve caller's settings */
3730 man->connection.up_query.nocache = up->nocache;
3731 *up = man->connection.up_query;
3732 }
3733 secure_memzero(&man->connection.up_query, sizeof(man->connection.up_query));
3734 }
3735
3736 gc_free(&gc);
3737 return ret;
3738}
3739
3740static int
3741management_query_multiline(struct management *man, const char *b64_data, const char *prompt,
3742 const char *cmd, int *state, struct buffer_list **input)
3743{
3744 struct gc_arena gc = gc_new();
3745 int ret = 0;
3746 volatile int signal_received = 0;
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 struct buffer alert_msg;
3759 if (b64_data)
3760 {
3761 alert_msg = alloc_buf_gc(strlen(b64_data) + strlen(prompt) + 3, &gc);
3762 buf_printf(&alert_msg, ">%s:%s", prompt, b64_data);
3763 }
3764 else
3765 {
3766 alert_msg = alloc_buf_gc(strlen(prompt) + 3, &gc);
3767 buf_printf(&alert_msg, ">%s", prompt);
3768 }
3769
3770 man_wait_for_client_connection(man, &signal_received, 0, MWCC_OTHER_WAIT);
3771
3772 if (signal_received)
3773 {
3774 goto done;
3775 }
3776
3777 man->persist.special_state_msg = BSTR(&alert_msg);
3778 msg(M_CLIENT, "%s", man->persist.special_state_msg);
3779
3780 /* run command processing event loop until we get our signature */
3781 do
3782 {
3783 man_standalone_event_loop(man, &signal_received, 0);
3784 if (!signal_received)
3785 {
3786 man_check_for_signals(&signal_received);
3787 }
3788 if (signal_received)
3789 {
3790 goto done;
3791 }
3792 } while (*state != EKS_READY);
3793
3794 ret = 1;
3795 }
3796
3797done:
3798 if (*state == EKS_READY && ret)
3799 {
3800 msg(M_CLIENT, "SUCCESS: %s command succeeded", cmd);
3801 }
3802 else if (*state == EKS_INPUT || *state == EKS_READY)
3803 {
3804 msg(M_CLIENT, "ERROR: %s command failed", cmd);
3805 }
3806
3807 /* revert state */
3808 man->persist.standalone_disabled = standalone_disabled_save;
3809 man->persist.special_state_msg = NULL;
3811 *state = EKS_UNDEF;
3812
3813 gc_free(&gc);
3814 return ret;
3815}
3816
3817static char *
3818/* returns allocated base64 signature */
3820 const char *prompt, const char *cmd, int *state,
3821 struct buffer_list **input)
3822{
3823 int ok;
3824 char *result = NULL;
3825
3826 ok = management_query_multiline(man, b64_data, prompt, cmd, state, input);
3827 if (ok && buffer_list_defined(*input))
3828 {
3830 const struct buffer *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 char *result = NULL;
3852
3853 const int ok = management_query_multiline(man, b64_data, prompt, cmd, state, input);
3854 if (ok && buffer_list_defined(*input))
3855 {
3857 const struct buffer *buf = buffer_list_peek(*input);
3858 if (buf && BLEN(buf) > 0)
3859 {
3860 result = (char *)malloc(BLENZ(buf) + 1);
3862 memcpy(result, buf->data, BLENZ(buf));
3863 result[BLEN(buf)] = '\0';
3864 }
3865 }
3866
3868 *input = NULL;
3869
3870 return result;
3871}
3872
3873char *
3874/* returns allocated base64 signature */
3875management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
3876{
3877 const char *prompt = "PK_SIGN";
3878 const char *desc = "pk-sign";
3880
3881 if (man->connection.client_version <= MCV_DEFAULT)
3882 {
3883 prompt = "RSA_SIGN";
3884 desc = "rsa-sign";
3885 }
3886
3888 if (man->connection.client_version >= MCV_PKSIGN_ALG)
3889 {
3890 buf_write(&buf_data, ",", (int)strlen(","));
3892 }
3893 char *ret = management_query_multiline_flatten(man, (char *)buf_bptr(&buf_data), prompt, desc,
3894 &man->connection.ext_key_state,
3895 &man->connection.ext_key_input);
3897 return ret;
3898}
3899
3900char *
3902{
3903 const char prompt_1[] = "NEED-CERTIFICATE:";
3904 struct buffer buf_prompt = alloc_buf(strlen(cert_name) + 20);
3906 buf_write(&buf_prompt, cert_name, strlen(cert_name) + 1); /* +1 for \0 */
3907
3908 char *result;
3910 management, NULL, (char *)buf_bptr(&buf_prompt), "certificate",
3911 &man->connection.ext_cert_state, &man->connection.ext_cert_input);
3913 return result;
3914}
3915
3916/*
3917 * Return true if management_hold() would block
3918 */
3919bool
3921{
3922 return (man->settings.flags & MF_HOLD) && !man->persist.hold_release && man_standalone_ok(man);
3923}
3924
3925/*
3926 * If the hold flag is enabled, hibernate until a management client releases the hold.
3927 * Return true if the caller should not sleep for an additional time interval.
3928 */
3929bool
3931{
3933 {
3934 volatile int signal_received = 0;
3935 const bool standalone_disabled_save = man->persist.standalone_disabled;
3936 struct gc_arena gc = gc_new();
3937
3939 false; /* This is so M_CLIENT messages will be correctly passed through msg() */
3940 man->persist.special_state_msg = NULL;
3942
3943 man_wait_for_client_connection(man, &signal_received, 0, MWCC_HOLD_WAIT);
3944
3945 if (!signal_received)
3946 {
3947 struct buffer out = alloc_buf_gc(128, &gc);
3948 buf_printf(&out, ">HOLD:Waiting for hold release:%d", holdtime);
3949 man->persist.special_state_msg = BSTR(&out);
3950 msg(M_CLIENT, "%s", man->persist.special_state_msg);
3951
3952 /* run command processing event loop until we get our username/password */
3953 do
3954 {
3955 man_standalone_event_loop(man, &signal_received, 0);
3956 if (!signal_received)
3957 {
3958 man_check_for_signals(&signal_received);
3959 }
3960 if (signal_received)
3961 {
3962 break;
3963 }
3964 } while (!man->persist.hold_release);
3965 }
3966
3967 /* revert state */
3968 man->persist.standalone_disabled = standalone_disabled_save;
3969 man->persist.special_state_msg = NULL;
3970 man->settings.mansig &= ~MANSIG_IGNORE_USR1_HUP;
3971
3972 gc_free(&gc);
3973 return true;
3974 }
3975 return false;
3976}
3977
3978/*
3979 * struct command_line
3980 */
3981
3982struct command_line *
3983command_line_new(const size_t len)
3984{
3985 struct command_line *cl;
3986 ALLOC_OBJ_CLEAR(cl, struct command_line);
3987 cl->buf = alloc_buf(len);
3988 cl->residual = alloc_buf(len);
3989 return cl;
3990}
3991
3992void
3994{
3995 buf_clear(&cl->buf);
3996 buf_clear(&cl->residual);
3997}
3998
3999void
4001{
4002 if (!cl)
4003 {
4004 return;
4005 }
4007 free_buf(&cl->buf);
4008 free_buf(&cl->residual);
4009 free(cl);
4010}
4011
4012void
4013command_line_add(struct command_line *cl, const unsigned char *buf, const size_t len)
4014{
4015 for (size_t i = 0; i < len; ++i)
4016 {
4017 if (buf[i] && char_class(buf[i], (CC_PRINT | CC_NEWLINE)))
4018 {
4019 if (!buf_write_u8(&cl->buf, buf[i]))
4020 {
4021 buf_clear(&cl->buf);
4022 }
4023 }
4024 }
4025}
4026
4027const char *
4029{
4030 const char *ret = NULL;
4031
4032 int i = buf_substring_len(&cl->buf, '\n');
4033 if (i >= 0)
4034 {
4035 buf_copy_excess(&cl->residual, &cl->buf, i);
4036 buf_chomp(&cl->buf);
4037 ret = BSTR(&cl->buf);
4038 }
4039 return ret;
4040}
4041
4042void
4044{
4045 buf_clear(&cl->buf);
4046 buf_copy(&cl->buf, &cl->residual);
4047 buf_clear(&cl->residual);
4048}
4049
4050/*
4051 * struct log_entry
4052 */
4053
4054const char *
4055log_entry_print(const struct log_entry *e, unsigned int flags, struct gc_arena *gc)
4056{
4057 struct buffer out = alloc_buf_gc(ERR_BUF_SIZE, gc);
4058 if (flags & LOG_FATAL_NOTIFY)
4059 {
4060 buf_printf(&out, ">FATAL:");
4061 }
4062 if (flags & LOG_PRINT_LOG_PREFIX)
4063 {
4064 buf_printf(&out, ">LOG:");
4065 }
4066 if (flags & LOG_PRINT_ECHO_PREFIX)
4067 {
4068 buf_printf(&out, ">ECHO:");
4069 }
4070 if (flags & LOG_PRINT_STATE_PREFIX)
4071 {
4072 buf_printf(&out, ">STATE:");
4073 }
4074 if (flags & LOG_PRINT_INT_DATE)
4075 {
4076 buf_printf(&out, "%u,", (unsigned int)e->timestamp);
4077 }
4078 if (flags & LOG_PRINT_MSG_FLAGS)
4079 {
4080 buf_printf(&out, "%s,", msg_flags_string(e->u.msg_flags, gc));
4081 }
4082 if (flags & LOG_PRINT_STATE)
4083 {
4084 buf_printf(&out, "%s,", man_state_name(e->u.state));
4085 }
4086 if (flags & LOG_PRINT_INTVAL)
4087 {
4088 buf_printf(&out, "%d,", e->u.intval);
4089 }
4090 if (e->string)
4091 {
4092 buf_printf(&out, "%s", e->string);
4093 }
4094 if (flags & LOG_PRINT_LOCAL_IP)
4095 {
4096 buf_printf(&out, ",%s", print_in_addr_t(e->local_ip, IA_EMPTY_IF_UNDEF, gc));
4097 }
4098 if (flags & LOG_PRINT_REMOTE_IP)
4099 {
4100 buf_printf(&out, ",%s",
4101 (!addr_defined(&e->remote_sock)
4102 ? ","
4103 : print_sockaddr_ex(&e->remote_sock.addr.sa, ",",
4105 buf_printf(&out, ",%s",
4106 (!addr_defined(&e->local_sock)
4107 ? ","
4108 : print_sockaddr_ex(&e->local_sock.addr.sa, ",",
4110 }
4111 if (flags & LOG_PRINT_LOCAL_IP && !IN6_IS_ADDR_UNSPECIFIED(&e->local_ip6))
4112 {
4113 buf_printf(&out, ",%s", print_in6_addr(e->local_ip6, IA_EMPTY_IF_UNDEF, gc));
4114 }
4115 if (flags & LOG_ECHO_TO_LOG)
4116 {
4117 msg(D_MANAGEMENT, "MANAGEMENT: %s", BSTR(&out));
4118 }
4119 if (flags & LOG_PRINT_CRLF)
4120 {
4121 buf_printf(&out, "\r\n");
4122 }
4123 return BSTR(&out);
4124}
4125
4126static void
4128{
4129 /* Cast away constness of const char* */
4130 free((char *)e->string);
4131 CLEAR(*e);
4132}
4133
4134/*
4135 * struct log_history
4136 */
4137
4138static inline int
4139log_index(const struct log_history *h, int i)
4140{
4141 return modulo_add(h->base, i, h->capacity);
4142}
4143
4144static void
4146{
4147 CLEAR(*h);
4148 h->capacity = capacity;
4150}
4151
4152struct log_history *
4154{
4155 struct log_history *h;
4156 ASSERT(capacity > 0);
4157 ALLOC_OBJ(h, struct log_history);
4159 return h;
4160}
4161
4162static void
4164{
4165 int i;
4166 for (i = 0; i < h->size; ++i)
4167 {
4169 }
4170 free(h->array);
4171}
4172
4173void
4175{
4177 free(h);
4178}
4179
4180void
4181log_history_add(struct log_history *h, const struct log_entry *le)
4182{
4183 struct log_entry *e;
4184 ASSERT(h->size >= 0 && h->size <= h->capacity);
4185 if (h->size == h->capacity)
4186 {
4187 e = &h->array[h->base];
4189 h->base = log_index(h, 1);
4190 }
4191 else
4192 {
4193 e = &h->array[log_index(h, h->size)];
4194 ++h->size;
4195 }
4196
4197 *e = *le;
4198 e->string = string_alloc(le->string, NULL);
4199}
4200
4201void
4202log_history_resize(struct log_history *h, const int capacity)
4203{
4204 if (capacity != h->capacity)
4205 {
4206 struct log_history newlog;
4207 int i;
4208
4209 ASSERT(capacity > 0);
4211
4212 for (i = 0; i < h->size; ++i)
4213 {
4214 log_history_add(&newlog, &h->array[log_index(h, i)]);
4215 }
4216
4218 *h = newlog;
4219 }
4220}
4221
4222const struct log_entry *
4223log_history_ref(const struct log_history *h, const int index)
4224{
4225 if (index >= 0 && index < h->size)
4226 {
4227 return &h->array[log_index(h, (h->size - 1) - index)];
4228 }
4229 else
4230 {
4231 return NULL;
4232 }
4233}
4234
4235void
4237{
4238 if (n < 0)
4239 {
4240 return;
4241 }
4242 else if (management)
4243 {
4245 }
4246 else
4247 {
4248#ifdef _WIN32
4249 win32_sleep(n);
4250#else
4251 if (n > 0)
4252 {
4253 sleep(n);
4254 }
4255#endif
4256 }
4257}
4258
4259void
4260management_check_bytecount_client(struct context *c, struct management *man, struct timeval *timeval)
4261{
4262 if (man->persist.callback.flags & MCF_SERVER)
4263 {
4264 return;
4265 }
4266
4268 {
4269 if (dco_enabled(&c->options))
4270 {
4271 if (dco_get_peer_stats(c, true) < 0)
4272 {
4273 return;
4274 }
4275 }
4276
4279 }
4280}
4281
4282void
4283management_check_bytecount_server(struct multi_context *multi, struct timeval *timeval)
4284{
4286 {
4287 return;
4288 }
4289
4291 {
4292 /* fetch counters from dco */
4293 if (dco_enabled(&multi->top.options))
4294 {
4295 if (dco_get_peer_stats_multi(&multi->top.c1.tuntap->dco, true) < 0)
4296 {
4297 return;
4298 }
4299 }
4300
4301 /* iterate over peers and report counters for each connected peer */
4302 struct hash_iterator hi;
4303 struct hash_element *he;
4304 hash_iterator_init(multi->hash, &hi);
4305 while ((he = hash_iterator_next(&hi)))
4306 {
4307 struct multi_instance *mi = (struct multi_instance *)he->value;
4308 struct context_2 *c2 = &mi->context.c2;
4309
4311 {
4313 }
4314 }
4315 hash_iterator_free(&hi);
4316 }
4317}
4318
4319/* context_2 stats are reset on reconnect. Since client expects stats
4320 * to be preserved across reconnects, we need to save context_2
4321 * stats before tearing the tunnel down.
4322 */
4323void
4325{
4328
4329 /* no need to raise SIGUSR1 on error since we are already closing the instance */
4330 if (dco_enabled(&c->options) && (dco_get_peer_stats(c, false) == 0))
4331 {
4332 man->persist.bytes_in += c->c2.dco_read_bytes;
4334 }
4335}
4336
4337#else /* ifdef ENABLE_MANAGEMENT */
4338
4339#include "win32.h"
4340void
4341management_sleep(const int n)
4342{
4343#ifdef _WIN32
4344 win32_sleep(n);
4345#else
4346 if (n > 0)
4347 {
4348 sleep(n);
4349 }
4350#endif /* ifdef _WIN32 */
4351}
4352
4353#endif /* ENABLE_MANAGEMENT */
#define OPENVPN_BASE64_DECODED_LENGTH(base64_length)
Compute the maximal number of bytes encoded in a base64 string.
Definition base64.h:41
#define OPENVPN_BASE64_LENGTH(binary_length)
Compute resulting base64 length.
Definition base64.h:38
bool buffer_list_defined(const struct buffer_list *ol)
Checks if the list is valid and non-empty.
Definition buffer.c:1175
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:169
void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, const char *sep)
Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len .
Definition buffer.c:1251
void buf_clear(struct buffer *buf)
Zeroise and reset a buffer.
Definition buffer.c:148
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
Definition buffer.c:1181
void buffer_list_aggregate(struct buffer_list *bl, const size_t max)
Aggregates as many buffers as possible from bl in a new buffer of maximum length max_len .
Definition buffer.c:1296
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:226
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
Definition buffer.c:1156
void chomp(char *str)
Remove trailing newline and carriage-return characters from a string.
Definition buffer.c:584
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition buffer.c:1238
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition buffer.c:1165
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
Definition buffer.c:60
void buffer_list_advance(struct buffer_list *ol, ssize_t n)
Advance past n bytes in the head buffer, popping it if it becomes empty.
Definition buffer.c:1319
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
Extract the next token from a buffer, delimited by a given character.
Definition buffer.c:775
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:616
void buffer_list_push(struct buffer_list *ol, const char *str)
Allocates and appends a new buffer containing str as data to ol.
Definition buffer.c:1196
void buf_chomp(struct buffer *buf)
Remove trailing newline and carriage-return characters from a buffer.
Definition buffer.c:522
bool char_class(const unsigned char c, const unsigned int flags)
Test whether a character belongs to one or more character classes.
Definition buffer.c:839
int buf_substring_len(const struct buffer *buf, int delim)
Return the number of bytes in a buffer up to and including a delimiter.
Definition buffer.c:753
#define ALLOC_OBJ(dptr, type)
Allocate memory for a single object of the given type.
Definition buffer.h:2027
static uint8_t * buf_bptr(struct buffer *buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:449
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Copy the content of one buffer to the end of another.
Definition buffer.h:1365
#define ALLOC_ARRAY_CLEAR(dptr, type, n)
Allocate and zero-initialise an array of n elements of the given type.
Definition buffer.h:2079
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:1440
#define CC_NEWLINE
newline
Definition buffer.h:1714
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:687
#define CBPTR(buf)
Return a const pointer to the start of the buffer content.
Definition buffer.h:141
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:769
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1262
static bool buf_write_u8(struct buffer *dest, uint8_t data)
Append a uint8_t to a buffer.
Definition buffer.h:1306
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:153
static void strncpynt(char *dest, const char *src, size_t maxlen)
Like strncpy() but always null-terminates the destination.
Definition buffer.h:710
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2146
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1976
#define CBSTR(buf)
Return the buffer content pointer cast to const char *.
Definition buffer.h:159
#define CC_PRINT
printable (>= 32, != 127)
Definition buffer.h:1708
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2038
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1960
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:289
struct env_set * env_set_create(struct gc_arena *gc)
Definition env_set.c:155
#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:3202
void management_auth_failure(struct management *man, const char *type, const char *reason)
Definition manage.c:3211
void log_history_close(struct log_history *h)
Definition manage.c:4174
struct management * management
Definition manage.c:75
static void man_status(struct management *man, const int version, struct status_output *so)
Definition manage.c:509
void management_check_bytecount_server(struct multi_context *multi, struct timeval *timeval)
Definition manage.c:4283
static void man_up_finalize(struct management *man)
Definition manage.c:742
static bool man_io_error(struct management *man, const char *prefix)
Definition manage.c:2261
struct command_line * command_line_new(const size_t len)
Definition manage.c:3983
static void man_log(struct management *man, const char *parm)
Definition manage.c:721
void management_auth_token(const char *token)
Definition manage.c:3224
static void man_reset_client_socket(struct management *man, const bool exiting)
Definition manage.c:2163
static void man_bytecount(struct management *man, const int update_seconds)
Definition manage.c:529
static void man_connect(struct management *man)
Definition manage.c:2097
#define MWCC_OTHER_WAIT
Definition manage.c:3517
void management_notify_client_needing_auth(struct management *man, const unsigned int mda_key_id, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3067
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:3401
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:579
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:3246
void command_line_free(struct command_line *cl)
Definition manage.c:4000
static int man_block(struct management *man, volatile int *signal_received, const time_t expire)
Definition manage.c:3418
static void man_certificate(struct management *man)
Definition manage.c:1314
void command_line_next(struct command_line *cl)
Definition manage.c:4043
static void man_net(struct management *man)
Definition manage.c:855
void management_clear_callback(struct management *man)
Definition manage.c:2872
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:2570
char * management_query_cert(struct management *man, const char *cert_name)
Definition manage.c:3901
static bool man_standalone_ok(const struct management *man)
Definition manage.c:3395
void management_connection_established(struct management *man, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3109
bool management_hold(struct management *man, int holdtime)
Definition manage.c:3930
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:2628
static int log_index(const struct log_history *h, int i)
Definition manage.c:4139
static void man_connection_clear(struct man_connection *mc)
Definition manage.c:2557
#define AF_DID_RESET
static void man_start_ne32(struct management *man)
Definition manage.c:1850
static void man_connection_settings_reset(struct management *man)
Definition manage.c:1878
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:522
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:3230
static void man_connection_init(struct management *man)
Definition manage.c:2728
static bool env_filter_match(const char *env_str, const int env_filter_level)
Definition manage.c:2935
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:2773
static void man_query_password(struct management *man, const char *type, const char *string)
Definition manage.c:815
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:4153
static void man_listen(struct management *man)
Definition manage.c:2021
static void man_stop_ne32(struct management *man)
Definition manage.c:1870
static void man_forget_passwords(void)
Definition manage.c:847
static void man_query_username(struct management *man, const char *type, const char *string)
Definition manage.c:804
static void man_push_update(struct management *man, const char **p, const push_update_type type)
Definition manage.c:1437
#define MWCC_PASSWORD_WAIT
Definition manage.c:3515
static void man_dispatch_command(struct management *man, struct status_output *so, const char **p, const int nparms)
Definition manage.c:1479
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:475
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:735
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:728
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:1369
struct management * management_init(void)
Definition manage.c:2802
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:3993
static void man_query_need_str(struct management *man, const char *type, const char *action)
Definition manage.c:838
bool management_would_hold(struct management *man)
Definition manage.c:3920
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:679
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:3609
static void man_bytecount_output_client(counter_type bytes_in_total, counter_type bytes_out_total)
Definition manage.c:567
void log_history_add(struct log_history *h, const struct log_entry *le)
Definition manage.c:4181
void management_notify_client_close(const struct management *man, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3119
static void log_entry_free_contents(struct log_entry *e)
Definition manage.c:4127
static int man_mod_signal(const struct management *man, const int signum)
Definition manage.c:449
void management_notify_generic(const char *str)
Definition manage.c:3032
#define MN_AT_LEAST
Definition manage.c:1342
void management_event_loop_n_seconds(struct management *man, int sec)
Definition manage.c:3556
static void man_persist_close(struct man_persist *mp)
Definition manage.c:2606
static void man_kill(struct management *man, const char *victim)
Definition manage.c:591
void management_close(struct management *man)
Definition manage.c:2855
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:2881
static void man_output_list_push_finalize(struct management *man)
Definition manage.c:270
static bool man_need(const char **p, const int n, unsigned int flags)
Checks if the correct number of arguments to a management command are present and otherwise prints an...
Definition manage.c:1353
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:1419
const char * log_entry_print(const struct log_entry *e, unsigned int flags, struct gc_arena *gc)
Definition manage.c:4055
static void man_accept(struct management *man)
Definition manage.c:1981
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:868
static void man_delete_unix_socket(struct management *man)
Definition manage.c:333
void management_io(struct management *man)
Definition manage.c:3284
static void man_wait_for_client_connection(struct management *man, volatile int *signal_received, const time_t expire, unsigned int flags)
Definition manage.c:3523
#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:3038
#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:2816
static void man_welcome(struct management *man)
Definition manage.c:206
static void man_query_need_ok(struct management *man, const char *type, const char *action)
Definition manage.c:829
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:3819
static void man_output_extra_env(struct management *man, const char *prefix)
Definition manage.c:3002
void man_persist_client_stats(struct management *man, struct context *c)
Definition manage.c:4324
static void man_new_connection_post(struct management *man, const char *description)
Definition manage.c:1891
static void log_history_obj_init(struct log_history *h, int capacity)
Definition manage.c:4145
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:2865
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:3477
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:3145
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:4223
static void man_remote_entry_count(struct management *man)
Definition manage.c:918
static void man_settings_close(struct man_settings *ms)
Definition manage.c:2717
void management_check_bytecount_client(struct context *c, struct management *man, struct timeval *timeval)
Definition manage.c:4260
void management_notify(const char *severity, const char *type, const char *text)
Definition manage.c:3026
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:3016
char * management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
Definition manage.c:3875
static void man_remote(struct management *man, const char **p)
Definition manage.c:1383
void management_learn_addr(struct man_def_auth_context *mdac, const struct mroute_addr *addr, const bool primary)
Definition manage.c:3132
void command_line_add(struct command_line *cl, const unsigned char *buf, const size_t len)
Definition manage.c:4013
static int man_standalone_event_loop(struct management *man, volatile int *signal_received, const time_t expire)
Definition manage.c:3500
static void man_process_command(struct management *man, const char *line)
Definition manage.c:2211
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:3741
static void man_output_env(const struct env_set *es, const bool tail, const int env_filter_level, const char *prefix)
Definition manage.c:2981
const char * command_line_get(struct command_line *cl)
Definition manage.c:4028
static void log_history_free_contents(struct log_history *h)
Definition manage.c:4163
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
Definition manage.c:4236
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:4202
static void in_extra_dispatch(struct management *man)
Definition manage.c:1062
static void man_command_unsupported(const char *command_name)
Definition manage.c:502
#define MWCC_HOLD_WAIT
Definition manage.c:3516
void management_post_tunnel_open(struct management *man, const in_addr_t tun_local_ip)
Definition manage.c:3179
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:779
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:724
#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:354
#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:370
#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:410
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
const char * auth_retry_print(void)
Definition options.c:3436
const char title_string[]
Definition options.c:73
bool auth_retry_set(const msglvl_t msglevel, const char *option)
Definition options.c:3413
#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:971
int parse_line(const char *line, char *p[], const int n, const char *file, const int line_num, msglvl_t msglevel, struct gc_arena *gc)
#define MAX_PARMS
Definition options.h:51
time_t now
Definition otime.c:33
static void update_time(void)
Definition otime.h:84
unsigned int platform_getpid(void)
Definition platform.c:333
bool platform_user_get(const char *username, struct platform_state_user *state)
Definition platform.c:80
bool platform_group_get(const char *groupname, struct platform_state_group *state)
Definition platform.c:124
push_update_type
Definition push.h:47
@ UPT_BY_CID
Definition push.h:49
@ UPT_BROADCAST
Definition push.h:48
void throw_signal_soft(const int signum, const char *signal_text)
Throw a soft global signal.
Definition sig.c:204
const char * signal_name(const int sig, const bool upper)
Definition sig.c:91
void throw_signal(const int signum)
Throw a hard signal.
Definition sig.c:175
int parse_signal(const char *signame)
Definition sig.c:64
static void get_signal(volatile int *sig)
Copy the global signal_received (if non-zero) to the passed-in argument sig.
Definition sig.h:109
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
Definition socket.c:785
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:967
void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int ai_family, const char *prefix, bool ipv6only)
Definition socket.c:919
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:2170
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:516
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:515
char * string
Definition env_set.h:38
struct env_item * next
Definition env_set.h:39
struct env_item * list
Definition env_set.h:45
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
void * value
Definition list.h:41
Definition manage.h:107
struct in6_addr local_ip6
Definition manage.h:111
struct openvpn_sockaddr local_sock
Definition manage.h:112
struct openvpn_sockaddr remote_sock
Definition manage.h:113
time_t timestamp
Definition manage.h:108
const char * string
Definition manage.h:109
in_addr_t local_ip
Definition manage.h:110
union log_entry_union u
Definition manage.h:114
int capacity
Definition manage.h:141
struct log_entry * array
Definition manage.h:142
struct event_set * es
Definition manage.h:313
unsigned long in_extra_cid
Definition manage.h:303
const char * up_query_type
Definition manage.h:322
bool password_verified
Definition manage.h:288
int env_filter_level
Definition manage.h:314
struct user_pass up_query
Definition manage.h:324
socket_descriptor_t sd_top
Definition manage.h:279
struct buffer_list * ext_cert_input
Definition manage.h:312
struct command_line * in
Definition manage.h:291
unsigned int in_extra_kid
Definition manage.h:304
struct openvpn_sockaddr remote
Definition manage.h:281
socket_descriptor_t sd_cli
Definition manage.h:280
struct buffer_list * ext_key_input
Definition manage.h:310
int up_query_mode
Definition manage.h:323
int bytecount_update_seconds
Definition manage.h:319
int in_extra_cmd
Definition manage.h:301
struct buffer_list * in_extra
Definition manage.h:302
bool echo_realtime
Definition manage.h:318
bool state_realtime
Definition manage.h:316
struct event_timeout bytecount_update_interval
Definition manage.h:320
int ext_cert_state
Definition manage.h:311
struct buffer_list * out
Definition manage.h:292
int password_tries
Definition manage.h:289
int client_version
Definition manage.h:330
bool log_realtime
Definition manage.h:317
struct net_event_win32 ne32
Definition manage.h:284
int ext_key_state
Definition manage.h:309
unsigned long cid
Definition manage.h:64
unsigned int flags
Definition manage.h:69
counter_type bytes_in
Definition manage.h:234
struct log_history * state
Definition manage.h:228
struct management_callback callback
Definition manage.h:225
struct log_history * log
Definition manage.h:221
struct virtual_output vout
Definition manage.h:222
bool standalone_disabled
Definition manage.h:224
bool defined
Definition manage.h:219
bool hold_release
Definition manage.h:230
struct log_history * echo
Definition manage.h:227
const char * special_state_msg
Definition manage.h:232
counter_type bytes_out
Definition manage.h:235
bool management_over_tunnel
Definition manage.h:248
struct addrinfo * local
Definition manage.h:242
unsigned int flags
Definition manage.h:241
int echo_buffer_size
Definition manage.h:251
unsigned int mansig
Definition manage.h:258
int state_buffer_size
Definition manage.h:252
bool defined
Definition manage.h:240
int log_history_cache
Definition manage.h:250
struct user_pass up
Definition manage.h:249
char *(* get_peer_info)(void *arg, const unsigned long cid)
Definition manage.h:191
bool(* client_auth)(void *arg, const unsigned long cid, const unsigned int mda_key_id, const bool auth, const char *reason, const char *client_reason, struct buffer_list *cc_config)
Definition manage.h:186
int(* kill_by_addr)(void *arg, const in_addr_t addr, const uint16_t port, const uint8_t proto)
Definition manage.h:181
void(* delete_event)(void *arg, event_t event)
Definition manage.h:182
bool(* push_update_broadcast)(void *arg, const char *options)
Definition manage.h:199
bool(* push_update_by_cid)(void *arg, unsigned long cid, const char *options)
Definition manage.h:200
int(* n_clients)(void *arg)
Definition manage.h:183
void(* status)(void *arg, const int version, struct status_output *so)
Definition manage.h:178
bool(* remote_entry_get)(void *arg, unsigned int index, char **remote)
Definition manage.h:198
unsigned int(* remote_entry_count)(void *arg)
Definition manage.h:197
bool(* send_cc_message)(void *arg, const char *message, const char *parameter)
Definition manage.h:184
bool(* proxy_cmd)(void *arg, const char **p)
Definition manage.h:192
void(* show_net)(void *arg, const msglvl_t msglevel)
Definition manage.h:179
unsigned int flags
Definition manage.h:176
bool(* remote_cmd)(void *arg, const char **p)
Definition manage.h:193
int(* kill_by_cn)(void *arg, const char *common_name)
Definition manage.h:180
bool(* kill_by_cid)(void *arg, const unsigned long cid, const char *kill_msg)
Definition manage.h:185
struct man_persist persist
Definition manage.h:335
struct man_connection connection
Definition manage.h:337
struct man_settings settings
Definition manage.h:336
Main OpenVPN server state structure.
Definition multi.h:162
struct hash * hash
VPN tunnel instances indexed by real address of the remote peer.
Definition multi.h:169
struct context top
Storage structure for process-wide configuration.
Definition multi.h:201
Server-mode state structure for one single VPN tunnel.
Definition multi.h:102
struct context context
The context structure storing state for this VPN tunnel.
Definition multi.h:142
dco_context_t dco
Definition tun.h:247
bool defined
Definition misc.h:56
char password[USER_PASS_LEN]
Definition misc.h:71
bool nocache
Definition misc.h:60
char username[USER_PASS_LEN]
Definition misc.h:70
unsigned int flags_default
Definition status.h:34
void * arg
Definition status.h:33
void(* func)(void *arg, const unsigned int flags, const char *str)
Definition status.h:35
#define SIGHUP
Definition syshead.h:55
#define SOCKET_UNDEFINED
Definition syshead.h:443
#define SIGINT
Definition syshead.h:56
#define SIGTERM
Definition syshead.h:59
SOCKET socket_descriptor_t
Definition syshead.h:445
#define sleep(x)
Definition syshead.h:42
#define SIGUSR1
Definition syshead.h:57
uint32_t in_addr_t
Definition syshead.h:52
static int socket_defined(const socket_descriptor_t sd)
Definition syshead.h:453
struct gc_arena gc
Definition test_ssl.c:122
unsigned int msg_flags
Definition manage.h:101
int state
Definition manage.h:102
int intval
Definition manage.h:103
void net_event_win32_init(struct net_event_win32 *ne)
Definition win32.c:420
void net_event_win32_close(struct net_event_win32 *ne)
Definition win32.c:471
void net_event_win32_reset_write(struct net_event_win32 *ne)
Definition win32.c:436
void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd)
Definition win32.c:427
void win32_sleep(const int n)
Definition win32.c:1531
void net_event_win32_reset(struct net_event_win32 *ne)
Definition win32.c:454
void net_event_win32_stop(struct net_event_win32 *ne)
Definition win32.c:460
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