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