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