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, size_t 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-response");
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 msg(D_MANAGEMENT, "MANAGEMENT: TCP %s error: %s", prefix, strerror(err));
2329 return true;
2330 }
2331 else
2332 {
2333 return false;
2334 }
2335}
2336
2337#ifdef TARGET_ANDROID
2338static ssize_t
2339man_send_with_fd(int fd, const void *ptr, size_t nbytes, int flags, int sendfd)
2340{
2341 struct msghdr msg = { 0 };
2342 struct iovec iov[1];
2343
2344 union
2345 {
2346 struct cmsghdr cm;
2347 char control[CMSG_SPACE(sizeof(int))];
2348 } control_un;
2349 struct cmsghdr *cmptr;
2350
2351 msg.msg_control = control_un.control;
2352 msg.msg_controllen = sizeof(control_un.control);
2353
2354 cmptr = CMSG_FIRSTHDR(&msg);
2355 cmptr->cmsg_len = CMSG_LEN(sizeof(int));
2356 cmptr->cmsg_level = SOL_SOCKET;
2357 cmptr->cmsg_type = SCM_RIGHTS;
2358 *((int *)CMSG_DATA(cmptr)) = sendfd;
2359
2360 msg.msg_name = NULL;
2361 msg.msg_namelen = 0;
2362
2363 /* sendmsg takes a const msghdr, but we can't construct that here
2364 directly, so cast */
2365 iov[0].iov_base = (void *)ptr;
2366 iov[0].iov_len = nbytes;
2367 msg.msg_iov = iov;
2368 msg.msg_iovlen = 1;
2369
2370 return sendmsg(fd, &msg, flags);
2371}
2372
2373static ssize_t
2374man_recv_with_fd(int fd, void *ptr, size_t nbytes, int flags, int *recvfd)
2375{
2376 struct msghdr msghdr = { 0 };
2377 struct iovec iov[1];
2378 ssize_t n;
2379
2380 union
2381 {
2382 struct cmsghdr cm;
2383 char control[CMSG_SPACE(sizeof(int))];
2384 } control_un;
2385 struct cmsghdr *cmptr;
2386
2387 msghdr.msg_control = control_un.control;
2388 msghdr.msg_controllen = sizeof(control_un.control);
2389
2390 msghdr.msg_name = NULL;
2391 msghdr.msg_namelen = 0;
2392
2393 iov[0].iov_base = ptr;
2394 iov[0].iov_len = nbytes;
2395 msghdr.msg_iov = iov;
2396 msghdr.msg_iovlen = 1;
2397
2398 if ((n = recvmsg(fd, &msghdr, flags)) <= 0)
2399 {
2400 return (n);
2401 }
2402
2403 if ((cmptr = CMSG_FIRSTHDR(&msghdr)) != NULL && cmptr->cmsg_len == CMSG_LEN(sizeof(int)))
2404 {
2405 if (cmptr->cmsg_level != SOL_SOCKET)
2406 {
2407 msg(M_ERR, "control level != SOL_SOCKET");
2408 }
2409 if (cmptr->cmsg_type != SCM_RIGHTS)
2410 {
2411 msg(M_ERR, "control type != SCM_RIGHTS");
2412 }
2413 *recvfd = *((int *)CMSG_DATA(cmptr));
2414 }
2415 else
2416 {
2417 *recvfd = -1; /* descriptor was not passed */
2418 }
2419 return (n);
2420}
2421
2422/*
2423 * The android control method will instruct the GUI part of openvpn to do
2424 * the route/ifconfig/open tun command. See doc/android.txt for details.
2425 */
2426bool
2427management_android_control(const struct management *man, const char *command, const char *msg)
2428{
2429 if (!man)
2430 {
2431 msg(M_FATAL, "Required management interface not available.");
2432 }
2433 struct user_pass up;
2434 CLEAR(up);
2435 strlcpy(up.username, msg, sizeof(up.username));
2436
2438 return strcmp("ok", up.password) == 0;
2439}
2440
2441/*
2442 * In Android 4.4 it is not possible to open a new tun device and then close the
2443 * old tun device without breaking the whole VPNService stack until the device
2444 * is rebooted. This management method ask the UI what method should be taken to
2445 * ensure the optimal solution for the situation
2446 */
2447int
2448managment_android_persisttun_action(struct management *man)
2449{
2450 struct user_pass up;
2451 CLEAR(up);
2452 strcpy(up.username, "tunmethod");
2454 (void *)0);
2455 if (!strcmp("NOACTION", up.password))
2456 {
2457 return ANDROID_KEEP_OLD_TUN;
2458 }
2459 else if (!strcmp("OPEN_BEFORE_CLOSE", up.password))
2460 {
2461 return ANDROID_OPEN_BEFORE_CLOSE;
2462 }
2463 else
2464 {
2465 msg(M_ERR, "Got unrecognised '%s' from management for PERSIST_TUN_ACTION query",
2466 up.password);
2467 }
2468
2469 ASSERT(0);
2470 return ANDROID_OPEN_BEFORE_CLOSE;
2471}
2472
2473
2474#endif /* ifdef TARGET_ANDROID */
2475
2476#if defined(__GNUC__) || defined(__clang__)
2477#pragma GCC diagnostic push
2478#pragma GCC diagnostic ignored "-Wsign-conversion"
2479#endif
2480
2481static ssize_t
2483{
2484 /*
2485 * read command line from socket
2486 */
2487 unsigned char buf[256];
2488 ssize_t len = 0;
2489
2490#ifdef TARGET_ANDROID
2491 int fd;
2492 len = man_recv_with_fd(man->connection.sd_cli, buf, sizeof(buf), MSG_NOSIGNAL, &fd);
2493 if (fd >= 0)
2494 {
2495 man->connection.lastfdreceived = fd;
2496 }
2497#else /* ifdef TARGET_ANDROID */
2498 len = recv(man->connection.sd_cli, (void *)buf, sizeof(buf), MSG_NOSIGNAL);
2499#endif
2500
2501 if (len == 0)
2502 {
2503 man_reset_client_socket(man, false);
2504 }
2505 else if (len > 0)
2506 {
2507 bool processed_command = false;
2508
2509 ASSERT(len <= (ssize_t)sizeof(buf));
2510 command_line_add(man->connection.in, buf, (size_t)len);
2511
2512 /*
2513 * Reset output object
2514 */
2516
2517 /*
2518 * process command line if complete
2519 */
2520 {
2521 const char *line;
2522 while ((line = command_line_get(man->connection.in)))
2523 {
2524 if (man->connection.in_extra)
2525 {
2526 if (!strcmp(line, "END"))
2527 {
2528 in_extra_dispatch(man);
2529 }
2530 else
2531 {
2533 }
2534 }
2535 else
2536 {
2537 man_process_command(man, (char *)line);
2538 }
2539 if (man->connection.halt)
2540 {
2541 break;
2542 }
2544 processed_command = true;
2545 }
2546 }
2547
2548 /*
2549 * Reset output state to MS_CC_WAIT_(READ|WRITE)
2550 */
2551 if (man->connection.halt)
2552 {
2553 man_reset_client_socket(man, false);
2554 len = 0;
2555 }
2556 else
2557 {
2558 if (processed_command)
2559 {
2560 man_prompt(man);
2561 }
2563 }
2564 }
2565 else /* len < 0 */
2566 {
2567 if (man_io_error(man, "recv"))
2568 {
2569 man_reset_client_socket(man, false);
2570 }
2571 }
2572 return len;
2573}
2574
2575static ssize_t
2577{
2578 const int size_hint = 1024;
2579 ssize_t sent = 0;
2580
2581 buffer_list_aggregate(man->connection.out, size_hint);
2582 const struct buffer *buf = buffer_list_peek(man->connection.out);
2583 if (buf && BLEN(buf))
2584 {
2585 const int len = min_int(size_hint, BLEN(buf));
2586#ifdef TARGET_ANDROID
2587 if (man->connection.fdtosend > 0)
2588 {
2589 sent = man_send_with_fd(man->connection.sd_cli, CBPTR(buf), len, MSG_NOSIGNAL,
2590 man->connection.fdtosend);
2591 man->connection.fdtosend = -1;
2592 }
2593 else
2594#endif
2595 {
2596 sent = send(man->connection.sd_cli, CBSTR(buf), len, MSG_NOSIGNAL);
2597 }
2598 if (sent >= 0)
2599 {
2601 }
2602 else
2603 {
2604 if (man_io_error(man, "send"))
2605 {
2606 man_reset_client_socket(man, false);
2607 }
2608 }
2609 }
2610
2611 /*
2612 * Reset output state to MS_CC_WAIT_(READ|WRITE)
2613 */
2615
2616 return sent;
2617}
2618
2619#if defined(__GNUC__) || defined(__clang__)
2620#pragma GCC diagnostic pop
2621#endif
2622
2623static void
2625{
2626 CLEAR(*mc);
2627
2628 /* set initial state */
2629 mc->state = MS_INITIAL;
2630
2631 /* clear socket descriptors */
2634}
2635
2636static void
2637man_persist_init(struct management *man, const int log_history_cache, const int echo_buffer_size,
2638 const int state_buffer_size)
2639{
2640 struct man_persist *mp = &man->persist;
2641 if (!mp->defined)
2642 {
2643 CLEAR(*mp);
2644
2645 /* initialize log history store */
2646 mp->log = log_history_init(log_history_cache);
2647
2648 /*
2649 * Initialize virtual output object, so that functions
2650 * which write to a virtual_output object can be redirected
2651 * here to the management object.
2652 */
2654 mp->vout.arg = man;
2657
2658 /*
2659 * Initialize --echo list
2660 */
2661 man->persist.echo = log_history_init(echo_buffer_size);
2662
2663 /*
2664 * Initialize --state list
2665 */
2666 man->persist.state = log_history_init(state_buffer_size);
2667
2668 mp->defined = true;
2669 }
2670}
2671
2672static void
2674{
2675 if (mp->log)
2676 {
2679 }
2680
2681 if (mp->echo)
2682 {
2684 }
2685
2686 if (mp->state)
2687 {
2689 }
2690
2691 CLEAR(*mp);
2692}
2693
2694static void
2695man_settings_init(struct man_settings *ms, const char *addr, const char *port,
2696 const char *pass_file, const char *client_user, const char *client_group,
2697 const int log_history_cache, const int echo_buffer_size,
2698 const int state_buffer_size, const int remap_sigusr1, const unsigned int flags)
2699{
2700 if (!ms->defined)
2701 {
2702 CLEAR(*ms);
2703
2704 ms->flags = flags;
2705
2706 /*
2707 * Get username/password
2708 */
2709 if (pass_file)
2710 {
2711 get_user_pass(&ms->up, pass_file, "Management", GET_USER_PASS_PASSWORD_ONLY);
2712 }
2713
2714#if UNIX_SOCK_SUPPORT
2715 /*
2716 * lookup client UID/GID if specified
2717 */
2718 if (client_user)
2719 {
2720 ASSERT(platform_user_get(client_user, &ms->user));
2721 msg(D_MANAGEMENT, "MANAGEMENT: client_uid=%d", ms->user.uid);
2722 }
2723 if (client_group)
2724 {
2725 ASSERT(platform_group_get(client_group, &ms->group));
2726 msg(D_MANAGEMENT, "MANAGEMENT: client_gid=%d", ms->group.gid);
2727 }
2728
2729 if (ms->flags & MF_UNIX_SOCK)
2730 {
2731 sockaddr_unix_init(&ms->local_unix, addr);
2732 }
2733 else
2734#endif
2735 {
2736 /*
2737 * Run management over tunnel, or
2738 * separate channel?
2739 */
2740 if (streq(addr, "tunnel") && !(flags & MF_CONNECT_AS_CLIENT))
2741 {
2742 ms->management_over_tunnel = true;
2743 }
2744 else
2745 {
2746 int status;
2747 unsigned int resolve_flags = GETADDR_RESOLVE | GETADDR_WARN_ON_SIGNAL | GETADDR_FATAL;
2748
2749 if (!(flags & MF_CONNECT_AS_CLIENT))
2750 {
2751 resolve_flags |= GETADDR_PASSIVE;
2752 }
2753
2754 status =
2755 openvpn_getaddrinfo(resolve_flags, addr, port, 0, NULL, AF_UNSPEC, &ms->local);
2756 ASSERT(status == 0);
2757 }
2758 }
2759
2760 /*
2761 * Log history and echo buffer may need to be resized
2762 */
2763 ms->log_history_cache = log_history_cache;
2764 ms->echo_buffer_size = echo_buffer_size;
2765 ms->state_buffer_size = state_buffer_size;
2766
2767 /*
2768 * Set remap sigusr1 flags
2769 */
2770 if (remap_sigusr1 == SIGHUP)
2771 {
2773 }
2774 else if (remap_sigusr1 == SIGTERM)
2775 {
2777 }
2778
2779 ms->defined = true;
2780 }
2781}
2782
2783static void
2785{
2786 if (ms->local)
2787 {
2788 freeaddrinfo(ms->local);
2789 }
2790 CLEAR(*ms);
2791}
2792
2793
2794static void
2796{
2797 if (man->connection.state == MS_INITIAL)
2798 {
2799#ifdef _WIN32
2800 /*
2801 * This object is a sort of TCP/IP helper
2802 * for Windows.
2803 */
2805#endif
2806
2807 /*
2808 * Allocate helper objects for command line input and
2809 * command output from/to the socket.
2810 */
2811 man->connection.in = command_line_new(1024);
2813
2814 /*
2815 * Initialize event set for standalone usage, when we are
2816 * running outside of the primary event loop.
2817 */
2818 {
2819 int maxevents = 1;
2820 man->connection.es = event_set_init(&maxevents, EVENT_METHOD_FAST);
2821 }
2822
2823 man->connection.client_version = MCV_DEFAULT; /* default version */
2824
2825 /*
2826 * Listen/connect socket
2827 */
2829 {
2830 man_connect(man);
2831 }
2832 else
2833 {
2834 man_listen(man);
2835 }
2836 }
2837}
2838
2839static void
2841{
2842 struct man_connection *mc = &man->connection;
2843
2844 event_free(mc->es);
2845#ifdef _WIN32
2847#endif
2848 if (socket_defined(mc->sd_top))
2849 {
2850 man_close_socket(man, mc->sd_top);
2852 }
2853 if (socket_defined(mc->sd_cli))
2854 {
2855 man_close_socket(man, mc->sd_cli);
2856 }
2857
2858 command_line_free(mc->in);
2859 buffer_list_free(mc->out);
2860
2862
2866}
2867
2868struct management *
2870{
2871 struct management *man;
2872 ALLOC_OBJ_CLEAR(man, struct management);
2873
2876
2878
2879 return man;
2880}
2881
2882bool
2883management_open(struct management *man, const char *addr, const char *port, const char *pass_file,
2884 const char *client_user, const char *client_group, const int log_history_cache,
2885 const int echo_buffer_size, const int state_buffer_size, const int remap_sigusr1,
2886 const unsigned int flags)
2887{
2888 bool ret = false;
2889
2890 /*
2891 * Save the settings only if they have not
2892 * been saved before.
2893 */
2894 man_settings_init(&man->settings, addr, port, pass_file, client_user, client_group,
2895 log_history_cache, echo_buffer_size, state_buffer_size, remap_sigusr1, flags);
2896
2897 /*
2898 * The log is initially sized to MANAGEMENT_LOG_HISTORY_INITIAL_SIZE,
2899 * but may be changed here. Ditto for echo and state buffers.
2900 */
2904
2905 /*
2906 * If connection object is uninitialized and we are not doing
2907 * over-the-tunnel management, then open (listening) connection.
2908 */
2909 if (man->connection.state == MS_INITIAL)
2910 {
2912 {
2914 ret = true;
2915 }
2916 }
2917
2918 return ret;
2919}
2920
2921void
2923{
2924 man_output_list_push_finalize(man); /* flush output queue */
2928 free(man);
2929}
2930
2931void
2933{
2934 man->persist.standalone_disabled = true;
2935 man->persist.callback = *cb;
2936}
2937
2938void
2940{
2941 man->persist.standalone_disabled = false;
2942 man->persist.hold_release = false;
2943 CLEAR(man->persist.callback);
2944 man_output_list_push_finalize(man); /* flush output queue */
2945}
2946
2947void
2948management_set_state(struct management *man, const int state, const char *detail,
2949 const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6,
2950 const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
2951{
2952 if (man->persist.state
2953 && (!(man->settings.flags & MF_SERVER) || state < OPENVPN_STATE_CLIENT_BASE))
2954 {
2955 struct gc_arena gc = gc_new();
2956 struct log_entry e;
2957 const char *out = NULL;
2958
2959 update_time();
2960 CLEAR(e);
2961 e.timestamp = now;
2962 e.u.state = state;
2963 e.string = detail;
2964 if (tun_local_ip)
2965 {
2966 e.local_ip = *tun_local_ip;
2967 }
2968 if (tun_local_ip6)
2969 {
2970 e.local_ip6 = *tun_local_ip6;
2971 }
2972 if (local)
2973 {
2974 e.local_sock = *local;
2975 }
2976 if (remote)
2977 {
2978 e.remote_sock = *remote;
2979 }
2980
2981 log_history_add(man->persist.state, &e);
2982
2983 if (man->connection.state_realtime)
2984 {
2985 out = log_entry_print(&e,
2989 &gc);
2990 }
2991
2992 if (out)
2993 {
2994 man_output_list_push(man, out);
2995 }
2996
2997 gc_free(&gc);
2998 }
2999}
3000
3001static bool
3002env_filter_match(const char *env_str, const int env_filter_level)
3003{
3004 static const char *env_names[] = { "username=",
3005 "password=",
3006 "X509_0_CN=",
3007 "tls_serial_",
3008 "untrusted_ip=",
3009 "ifconfig_local=",
3010 "ifconfig_netmask=",
3011 "daemon_start_time=",
3012 "daemon_pid=",
3013 "dev=",
3014 "ifconfig_pool_remote_ip=",
3015 "ifconfig_pool_netmask=",
3016 "time_duration=",
3017 "bytes_sent=",
3018 "bytes_received=",
3019 "session_id=",
3020 "session_state=",
3021 "acc_protocols=",
3022 "acc_message_len=" };
3023
3024 if (env_filter_level == 0)
3025 {
3026 return true;
3027 }
3028 else if (env_filter_level <= 1 && !strncmp(env_str, "X509_", 5))
3029 {
3030 return true;
3031 }
3032 else if (env_filter_level <= 2)
3033 {
3034 size_t i;
3035 for (i = 0; i < SIZE(env_names); ++i)
3036 {
3037 const char *en = env_names[i];
3038 const size_t len = strlen(en);
3039 if (!strncmp(env_str, en, len))
3040 {
3041 return true;
3042 }
3043 }
3044 return false;
3045 }
3046 return false;
3047}
3048
3049static void
3050man_output_env(const struct env_set *es, const bool tail, const int env_filter_level,
3051 const char *prefix)
3052{
3053 if (es)
3054 {
3055 struct env_item *e;
3056 for (e = es->list; e != NULL; e = e->next)
3057 {
3058 if (e->string && (!env_filter_level || env_filter_match(e->string, env_filter_level)))
3059 {
3060 msg(M_CLIENT, ">%s:ENV,%s", prefix, e->string);
3061 }
3062 }
3063 }
3064 if (tail)
3065 {
3066 msg(M_CLIENT, ">%s:ENV,END", prefix);
3067 }
3068}
3069
3070static void
3071man_output_extra_env(struct management *man, const char *prefix)
3072{
3073 struct gc_arena gc = gc_new();
3074 struct env_set *es = env_set_create(&gc);
3075 if (man->persist.callback.n_clients)
3076 {
3077 const int nclients = (*man->persist.callback.n_clients)(man->persist.callback.arg);
3078 setenv_int(es, "n_clients", nclients);
3079 }
3080 man_output_env(es, false, man->connection.env_filter_level, prefix);
3081 gc_free(&gc);
3082}
3083
3084void
3085management_up_down(const struct management *man, const char *updown, const struct env_set *es)
3086{
3087 if (man->settings.flags & MF_UP_DOWN)
3088 {
3089 msg(M_CLIENT, ">UPDOWN:%s", updown);
3090 man_output_env(es, true, 0, "UPDOWN");
3091 }
3092}
3093
3094void
3095management_notify(const char *severity, const char *type, const char *text)
3096{
3097 msg(M_CLIENT, ">NOTIFY:%s,%s,%s", severity, type, text);
3098}
3099
3100void
3102{
3103 msg(M_CLIENT, "%s", str);
3104}
3105
3106static void
3108{
3110 {
3111 const char *peer_info =
3112 (*man->persist.callback.get_peer_info)(man->persist.callback.arg, mdac->cid);
3113 if (peer_info)
3114 {
3115 char line[256];
3116 struct buffer buf;
3117
3118 buf_set_read(&buf, (const uint8_t *)peer_info, strlen(peer_info));
3119 while (buf_parse(&buf, '\n', line, sizeof(line)))
3120 {
3121 chomp(line);
3122 if (validate_peer_info_line(line))
3123 {
3124 msg(M_CLIENT, ">CLIENT:ENV,%s", line);
3125 }
3126 else
3127 {
3128 msg(D_MANAGEMENT, "validation failed on peer_info line received from client");
3129 }
3130 }
3131 }
3132 }
3133}
3134
3135void
3136management_notify_client_needing_auth(struct management *man, const unsigned int mda_key_id,
3137 struct man_def_auth_context *mdac, const struct env_set *es)
3138{
3139 if (!(mdac->flags & DAF_CONNECTION_CLOSED))
3140 {
3141 const char *mode = "CONNECT";
3143 {
3144 mode = "REAUTH";
3145 }
3146 msg(M_CLIENT, ">CLIENT:%s,%lu,%u", mode, mdac->cid, mda_key_id);
3147 man_output_extra_env(man, "CLIENT");
3148 if (man->connection.env_filter_level > 0)
3149 {
3150 man_output_peer_info_env(man, mdac);
3151 }
3152 man_output_env(es, true, man->connection.env_filter_level, "CLIENT");
3153 mdac->flags |= DAF_INITIAL_AUTH;
3154 }
3155}
3156
3157void
3158management_notify_client_cr_response(unsigned mda_key_id, const struct man_def_auth_context *mdac,
3159 const struct env_set *es, const char *response)
3160{
3161 if (management)
3162 {
3163 msg(M_CLIENT, ">CLIENT:CR_RESPONSE,%lu,%u,%s", mdac->cid, mda_key_id, response);
3166 {
3168 }
3170 }
3171}
3172
3173
3174void
3176 const struct env_set *es)
3177{
3179 msg(M_CLIENT, ">CLIENT:ESTABLISHED,%lu", mdac->cid);
3180 man_output_extra_env(man, "CLIENT");
3181 man_output_env(es, true, man->connection.env_filter_level, "CLIENT");
3182}
3183
3184void
3186 struct man_def_auth_context *mdac,
3187 const struct env_set *es)
3188{
3189 if ((mdac->flags & DAF_INITIAL_AUTH) && !(mdac->flags & DAF_CONNECTION_CLOSED))
3190 {
3191 msg(M_CLIENT, ">CLIENT:DISCONNECT,%lu", mdac->cid);
3192 man_output_env(es, true, man->connection.env_filter_level, "CLIENT");
3194 }
3195}
3196
3197void
3199 const struct mroute_addr *addr, const bool primary)
3200{
3201 struct gc_arena gc = gc_new();
3202 if ((mdac->flags & DAF_INITIAL_AUTH) && !(mdac->flags & DAF_CONNECTION_CLOSED))
3203 {
3204 msg(M_CLIENT, ">CLIENT:ADDRESS,%lu,%s,%d", mdac->cid,
3205 mroute_addr_print_ex(addr, MAPF_SUBNET, &gc), BOOL_CAST(primary));
3206 }
3207 gc_free(&gc);
3208}
3209
3210void
3211management_echo(struct management *man, const char *string, const bool pull)
3212{
3213 if (man->persist.echo)
3214 {
3215 struct gc_arena gc = gc_new();
3216 struct log_entry e;
3217 const char *out = NULL;
3218
3219 update_time();
3220 CLEAR(e);
3221 e.timestamp = now;
3222 e.string = string;
3223 e.u.intval = BOOL_CAST(pull);
3224
3225 log_history_add(man->persist.echo, &e);
3226
3227 if (man->connection.echo_realtime)
3228 {
3229 out = log_entry_print(&e,
3232 &gc);
3233 }
3234
3235 if (out)
3236 {
3237 man_output_list_push(man, out);
3238 }
3239
3240 gc_free(&gc);
3241 }
3242}
3243
3244void
3245management_post_tunnel_open(struct management *man, const in_addr_t tun_local_ip)
3246{
3247 /*
3248 * If we are running management over the tunnel,
3249 * this is the place to initialize the connection.
3250 */
3252 {
3253 /* listen on our local TUN/TAP IP address */
3254 struct in_addr ia;
3255 int ret;
3256 char buf[INET_ADDRSTRLEN];
3257
3258 ia.s_addr = htonl(tun_local_ip);
3259 inet_ntop(AF_INET, &ia, buf, sizeof(buf));
3260 ret =
3261 openvpn_getaddrinfo(GETADDR_PASSIVE, buf, NULL, 0, NULL, AF_INET, &man->settings.local);
3262 ASSERT(ret == 0);
3264 }
3265}
3266
3267void
3269{
3271 {
3273 }
3274}
3275
3276void
3277management_auth_failure(const struct management *man, const char *type, const char *reason)
3278{
3279 if (reason)
3280 {
3281 msg(M_CLIENT, ">PASSWORD:Verification Failed: '%s' ['%s']", type, reason);
3282 }
3283 else
3284 {
3285 msg(M_CLIENT, ">PASSWORD:Verification Failed: '%s'", type);
3286 }
3287}
3288
3289void
3290management_auth_token(const char *token)
3291{
3292 msg(M_CLIENT, ">PASSWORD:Auth-Token:%s", token);
3293}
3294
3295static inline bool
3296man_persist_state(unsigned int *persistent, const int n)
3297{
3298 if (persistent)
3299 {
3300 if (*persistent == (unsigned int)n)
3301 {
3302 return false;
3303 }
3304 *persistent = (unsigned int)n;
3305 }
3306 return true;
3307}
3308
3309#ifdef _WIN32
3310
3311void
3312management_socket_set(struct management *man, struct event_set *es, void *arg,
3313 unsigned int *persistent)
3314{
3315 if (man->connection.state != MS_INITIAL)
3316 {
3319
3320 switch (man->connection.state)
3321 {
3322 case MS_LISTEN:
3323 if (man_persist_state(persistent, 1))
3324 {
3325 event_ctl(es, ev, EVENT_READ, arg);
3326 }
3327 break;
3328
3329 case MS_CC_WAIT_READ:
3330 if (man_persist_state(persistent, 2))
3331 {
3332 event_ctl(es, ev, EVENT_READ, arg);
3333 }
3334 break;
3335
3336 case MS_CC_WAIT_WRITE:
3337 if (man_persist_state(persistent, 3))
3338 {
3339 event_ctl(es, ev, EVENT_READ | EVENT_WRITE, arg);
3340 }
3341 break;
3342
3343 default:
3344 ASSERT(0);
3345 }
3346 }
3347}
3348
3349void
3351{
3352 if (man->connection.state != MS_INITIAL)
3353 {
3354 long net_events;
3357
3358 if (net_events & FD_CLOSE)
3359 {
3360 man_reset_client_socket(man, false);
3361 }
3362 else
3363 {
3364 if (man->connection.state == MS_LISTEN)
3365 {
3366 if (net_events & FD_ACCEPT)
3367 {
3368 man_accept(man);
3370 }
3371 }
3372 else if (man->connection.state == MS_CC_WAIT_READ
3374 {
3375 if (net_events & FD_READ)
3376 {
3377 while (man_read(man) > 0)
3378 {
3379 }
3381 }
3382
3383 if (net_events & FD_WRITE)
3384 {
3385 ssize_t status = man_write(man);
3386 if (status < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
3387 {
3389 }
3390 }
3391 }
3392 }
3393 }
3394}
3395
3396#else /* ifdef _WIN32 */
3397
3398void
3399management_socket_set(struct management *man, struct event_set *es, void *arg,
3400 unsigned int *persistent)
3401{
3402 switch (man->connection.state)
3403 {
3404 case MS_LISTEN:
3405 if (man_persist_state(persistent, 1))
3406 {
3407 event_ctl(es, man->connection.sd_top, EVENT_READ, arg);
3408 }
3409 break;
3410
3411 case MS_CC_WAIT_READ:
3412 if (man_persist_state(persistent, 2))
3413 {
3414 event_ctl(es, man->connection.sd_cli, EVENT_READ, arg);
3415 }
3416 break;
3417
3418 case MS_CC_WAIT_WRITE:
3419 if (man_persist_state(persistent, 3))
3420 {
3421 event_ctl(es, man->connection.sd_cli, EVENT_WRITE, arg);
3422 }
3423 break;
3424
3425 case MS_INITIAL:
3426 break;
3427
3428 default:
3429 ASSERT(0);
3430 }
3431}
3432
3433void
3434management_io(struct management *man)
3435{
3436 switch (man->connection.state)
3437 {
3438 case MS_LISTEN:
3439 man_accept(man);
3440 break;
3441
3442 case MS_CC_WAIT_READ:
3443 man_read(man);
3444 break;
3445
3446 case MS_CC_WAIT_WRITE:
3447 man_write(man);
3448 break;
3449
3450 case MS_INITIAL:
3451 break;
3452
3453 default:
3454 ASSERT(0);
3455 }
3456}
3457
3458#endif /* ifdef _WIN32 */
3459
3460static inline bool
3462{
3464}
3465
3466static bool
3467man_check_for_signals(volatile int *signal_received)
3468{
3469 if (signal_received)
3470 {
3471 get_signal(signal_received);
3472 if (*signal_received)
3473 {
3474 return true;
3475 }
3476 }
3477 return false;
3478}
3479
3480/*
3481 * Wait for socket I/O when outside primary event loop
3482 */
3483static int
3484man_block(struct management *man, volatile int *signal_received, const time_t expire)
3485{
3486 struct timeval tv;
3487 struct event_set_return esr;
3488 int status = -1;
3489
3490 if (man_standalone_ok(man))
3491 {
3492 /* expire time can be already overdue, for this case init zero
3493 * timeout to avoid waiting first time and exit loop early with
3494 * either obtained event or timeout.
3495 */
3496 tv.tv_usec = 0;
3497 tv.tv_sec = 0;
3498
3499 while (true)
3500 {
3502 management_socket_set(man, man->connection.es, NULL, NULL);
3503 if (man_check_for_signals(signal_received))
3504 {
3505 status = -1;
3506 break;
3507 }
3508 status = event_wait(man->connection.es, &tv, &esr, 1);
3509 update_time();
3510 if (man_check_for_signals(signal_received))
3511 {
3512 status = -1;
3513 break;
3514 }
3515
3516 if (status > 0)
3517 {
3518 break;
3519 }
3520 else if (expire && now >= expire)
3521 {
3522 /* set SIGINT signal if expiration time exceeded */
3523 status = 0;
3524 if (signal_received)
3525 {
3526 *signal_received = SIGINT;
3527 }
3528 break;
3529 }
3530
3531 /* wait one second more */
3532 tv.tv_sec = 1;
3533 tv.tv_usec = 0;
3534 }
3535 }
3536 return status;
3537}
3538
3539/*
3540 * Perform management socket output outside primary event loop
3541 */
3542static void
3543man_output_standalone(struct management *man, volatile int *signal_received)
3544{
3545 if (man_standalone_ok(man))
3546 {
3547 while (man->connection.state == MS_CC_WAIT_WRITE)
3548 {
3549 management_io(man);
3550 if (man->connection.state == MS_CC_WAIT_WRITE)
3551 {
3552 man_block(man, signal_received, 0);
3553 }
3554 if (signal_received && *signal_received)
3555 {
3556 break;
3557 }
3558 }
3559 }
3560}
3561
3562/*
3563 * Process management event loop outside primary event loop
3564 */
3565static int
3566man_standalone_event_loop(struct management *man, volatile int *signal_received,
3567 const time_t expire)
3568{
3569 int status = -1;
3570 if (man_standalone_ok(man))
3571 {
3572 status = man_block(man, signal_received, expire);
3573 if (status > 0)
3574 {
3575 management_io(man);
3576 }
3577 }
3578 return status;
3579}
3580
3581#define MWCC_PASSWORD_WAIT (1 << 0)
3582#define MWCC_HOLD_WAIT (1 << 1)
3583#define MWCC_OTHER_WAIT (1 << 2)
3584
3585/*
3586 * Block until client connects
3587 */
3588static void
3589man_wait_for_client_connection(struct management *man, volatile int *signal_received,
3590 const time_t expire, unsigned int flags)
3591{
3593 if (man->connection.state == MS_LISTEN)
3594 {
3595 if (flags & MWCC_PASSWORD_WAIT)
3596 {
3597 msg(D_MANAGEMENT, "Need password(s) from management interface, waiting...");
3598 }
3599 if (flags & MWCC_HOLD_WAIT)
3600 {
3601 msg(D_MANAGEMENT, "Need hold release from management interface, waiting...");
3602 }
3603 if (flags & MWCC_OTHER_WAIT)
3604 {
3605 msg(D_MANAGEMENT, "Need information from management interface, waiting...");
3606 }
3607 do
3608 {
3609 man_standalone_event_loop(man, signal_received, expire);
3610 if (signal_received && *signal_received)
3611 {
3612 break;
3613 }
3614 } while (man->connection.state == MS_LISTEN || man_password_needed(man));
3615 }
3616}
3617
3618#if defined(__GNUC__) || defined(__clang__)
3619#pragma GCC diagnostic push
3620#pragma GCC diagnostic ignored "-Wsign-conversion"
3621#endif
3622
3623/*
3624 * Process the management event loop for sec seconds
3625 */
3626void
3628{
3629 if (man_standalone_ok(man))
3630 {
3631 volatile int signal_received = 0;
3632 const bool standalone_disabled_save = man->persist.standalone_disabled;
3633 time_t expire = 0;
3634
3635 /* This is so M_CLIENT messages will be correctly passed through msg() */
3636 man->persist.standalone_disabled = false;
3637
3638 /* set expire time */
3639 update_time();
3640 if (sec >= 0)
3641 {
3642 expire = now + sec;
3643 }
3644
3645 /* if no client connection, wait for one */
3646 man_wait_for_client_connection(man, &signal_received, expire, 0);
3647 if (signal_received)
3648 {
3649 return;
3650 }
3651
3652 /* run command processing event loop */
3653 do
3654 {
3655 man_standalone_event_loop(man, &signal_received, expire);
3656 if (!signal_received)
3657 {
3658 man_check_for_signals(&signal_received);
3659 }
3660 if (signal_received)
3661 {
3662 return;
3663 }
3664 update_time();
3665 } while (expire && expire > now);
3666
3667 /* revert state */
3668 man->persist.standalone_disabled = standalone_disabled_save;
3669 }
3670 else if (sec > 0)
3671 {
3672 sleep(sec);
3673 }
3674}
3675
3676/*
3677 * Get a username/password from management channel in standalone mode.
3678 */
3679bool
3680management_query_user_pass(struct management *man, struct user_pass *up, const char *type,
3681 const unsigned int flags, const char *static_challenge)
3682{
3683 struct gc_arena gc = gc_new();
3684 bool ret = false;
3685
3686 if (man_standalone_ok(man))
3687 {
3688 volatile int signal_received = 0;
3689 const bool standalone_disabled_save = man->persist.standalone_disabled;
3690 struct buffer alert_msg = alloc_buf_gc(256, &gc);
3691 const char *alert_type = NULL;
3692 const char *prefix = NULL;
3693 int up_query_mode = 0;
3694 const char *sc = NULL;
3695 ret = true;
3696 /* This is so M_CLIENT messages will be correctly passed through msg() */
3697 man->persist.standalone_disabled = false;
3698 man->persist.special_state_msg = NULL;
3699
3701
3702 if (flags & GET_USER_PASS_NEED_OK)
3703 {
3704 up_query_mode = UP_QUERY_NEED_OK;
3705 prefix = "NEED-OK";
3706 alert_type = "confirmation";
3707 }
3708 else if (flags & GET_USER_PASS_NEED_STR)
3709 {
3710 up_query_mode = UP_QUERY_NEED_STR;
3711 prefix = "NEED-STR";
3712 alert_type = "string";
3713 }
3714 else if (flags & GET_USER_PASS_PASSWORD_ONLY)
3715 {
3716 up_query_mode = UP_QUERY_PASS;
3717 prefix = "PASSWORD";
3718 alert_type = "password";
3719 }
3721 {
3722 up_query_mode = UP_QUERY_USERNAME;
3723 prefix = "PASSWORD";
3724 alert_type = "username";
3725 }
3726 else
3727 {
3728 up_query_mode = UP_QUERY_USER_PASS;
3729 prefix = "PASSWORD";
3730 alert_type = "username/password";
3731 if (static_challenge)
3732 {
3733 sc = static_challenge;
3734 }
3735 }
3736 buf_printf(&alert_msg, ">%s:Need '%s' %s", prefix, type, alert_type);
3737
3739 {
3740 buf_printf(&alert_msg, " MSG:%s", up->username);
3741 }
3742
3743 if (up_query_mode == UP_QUERY_USER_PASS
3745 && up->username[0])
3746 {
3747 /*
3748 * The buffer up is prefilled with username from auth-file or inline.
3749 * Indicate it in the prompt as base64 encoded.
3750 */
3751 char *user64 = NULL;
3752 const int user64_len = openvpn_base64_encode(up->username, (int)strlen(up->username), &user64);
3753 if (user64_len == -1)
3754 {
3755 msg(M_NONFATAL, "ERROR: could not base64-encode username");
3756 }
3757 else if (user64_len + 4 > 128)
3758 {
3759 msg(M_WARN | M_INFO, "Preferred username too long for user-pass prompt sent to management; omitting UN:<username> hint.");
3760 }
3761 else
3762 {
3763 buf_printf(&alert_msg, " UN:%s", user64);
3764 }
3765 free(user64);
3766 }
3767
3768 if (sc)
3769 {
3770 if (!buf_printf(&alert_msg, " SC:%d,%s",
3773 sc))
3774 {
3775 msg(M_WARN | M_INFO, "SC:<challenge-text> in user-pass prompt sent to management got truncated.");
3776 }
3777 }
3778
3779 man_wait_for_client_connection(man, &signal_received, 0, MWCC_PASSWORD_WAIT);
3780 if (signal_received)
3781 {
3782 ret = false;
3783 }
3784
3785 if (ret)
3786 {
3787 man->persist.special_state_msg = BSTR(&alert_msg);
3789
3790 /* tell command line parser which info we need */
3791 man->connection.up_query_mode = up_query_mode;
3792 man->connection.up_query_type = type;
3793
3794 /* run command processing event loop until we get our username/password/response */
3795 do
3796 {
3797 man_standalone_event_loop(man, &signal_received, 0);
3798 if (!signal_received)
3799 {
3800 man_check_for_signals(&signal_received);
3801 }
3802 if (signal_received)
3803 {
3804 ret = false;
3805 break;
3806 }
3807 } while (!man->connection.up_query.defined);
3808 }
3809
3810 /* revert state */
3812 man->connection.up_query_type = NULL;
3813 man->persist.standalone_disabled = standalone_disabled_save;
3814 man->persist.special_state_msg = NULL;
3815
3816 /* pass through blank passwords */
3817 if (!strcmp(man->connection.up_query.password, blank_up))
3818 {
3820 }
3821
3822 /*
3823 * Transfer u/p to return object, zero any record
3824 * we hold in the management object.
3825 */
3826 if (ret)
3827 {
3828 /* preserve caller's settings */
3830 *up = man->connection.up_query;
3831 }
3833 }
3834
3835 gc_free(&gc);
3836 return ret;
3837}
3838
3839static int
3840management_query_multiline(struct management *man, const char *b64_data, const char *prompt,
3841 const char *cmd, int *state)
3842{
3843 struct gc_arena gc = gc_new();
3844 int ret = 0;
3845 volatile int signal_received = 0;
3846 const bool standalone_disabled_save = man->persist.standalone_disabled;
3847 struct man_connection *mc = &man->connection;
3848
3849 if (man_standalone_ok(man))
3850 {
3851 /* This is so M_CLIENT messages will be correctly passed through msg() */
3852 man->persist.standalone_disabled = false;
3853 man->persist.special_state_msg = NULL;
3854
3855 *state = EKS_SOLICIT;
3856
3857 struct buffer alert_msg;
3858 if (b64_data)
3859 {
3860 alert_msg = alloc_buf_gc(strlen(b64_data) + strlen(prompt) + 3, &gc);
3861 buf_printf(&alert_msg, ">%s:%s", prompt, b64_data);
3862 }
3863 else
3864 {
3865 alert_msg = alloc_buf_gc(strlen(prompt) + 3, &gc);
3866 buf_printf(&alert_msg, ">%s", prompt);
3867 }
3868
3869 man_wait_for_client_connection(man, &signal_received, 0, MWCC_OTHER_WAIT);
3870
3871 if (signal_received)
3872 {
3873 goto done;
3874 }
3875
3876 man->persist.special_state_msg = BSTR(&alert_msg);
3878
3879 /* run command processing event loop until we get our signature */
3880 do
3881 {
3882 man_standalone_event_loop(man, &signal_received, 0);
3883 if (!signal_received)
3884 {
3885 man_check_for_signals(&signal_received);
3886 }
3887 if (signal_received)
3888 {
3889 goto done;
3890 }
3891 } while (*state != EKS_READY);
3892
3893 ret = 1;
3894 }
3895
3896done:
3897 if (*state == EKS_READY && ret)
3898 {
3899 msg(M_CLIENT, "SUCCESS: %s command succeeded", cmd);
3900 }
3901 else if (*state == EKS_INPUT || *state == EKS_READY)
3902 {
3903 msg(M_CLIENT, "ERROR: %s command failed", cmd);
3904 }
3905
3906 /* revert state */
3907 man->persist.standalone_disabled = standalone_disabled_save;
3908 man->persist.special_state_msg = NULL;
3910 *state = EKS_UNDEF;
3911
3912 gc_free(&gc);
3913 return ret;
3914}
3915
3916static char *
3917/* returns allocated base64 signature */
3919 const char *prompt, const char *cmd, int *state,
3920 struct buffer_list **input)
3921{
3922 int ok;
3923 char *result = NULL;
3924
3925 ok = management_query_multiline(man, b64_data, prompt, cmd, state);
3926 if (ok && buffer_list_defined(*input))
3927 {
3928 buffer_list_aggregate_separator(*input, 10000, "\n");
3929 const struct buffer *buf = buffer_list_peek(*input);
3930 if (buf && BLEN(buf) > 0)
3931 {
3932 result = (char *)malloc(BLENZ(buf) + 1);
3933 check_malloc_return(result);
3934 memcpy(result, buf->data, BLENZ(buf));
3935 result[BLEN(buf)] = '\0';
3936 }
3937 }
3938
3939 buffer_list_free(*input);
3940 *input = NULL;
3941
3942 return result;
3943}
3944
3945static char *
3946/* returns allocated base64 signature */
3947management_query_multiline_flatten(struct management *man, const char *b64_data, const char *prompt,
3948 const char *cmd, int *state, struct buffer_list **input)
3949{
3950 char *result = NULL;
3951
3952 const int ok = management_query_multiline(man, b64_data, prompt, cmd, state);
3953 if (ok && buffer_list_defined(*input))
3954 {
3955 buffer_list_aggregate(*input, 2048);
3956 const struct buffer *buf = buffer_list_peek(*input);
3957 if (buf && BLEN(buf) > 0)
3958 {
3959 result = (char *)malloc(BLENZ(buf) + 1);
3960 check_malloc_return(result);
3961 memcpy(result, buf->data, BLENZ(buf));
3962 result[BLEN(buf)] = '\0';
3963 }
3964 }
3965
3966 buffer_list_free(*input);
3967 *input = NULL;
3968
3969 return result;
3970}
3971
3972char *
3973/* returns allocated base64 signature */
3974management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
3975{
3976 const char *prompt = "PK_SIGN";
3977 const char *desc = "pk-sign";
3978 struct buffer buf_data = alloc_buf(strlen(b64_data) + strlen(algorithm) + 20);
3979
3981 {
3982 prompt = "RSA_SIGN";
3983 desc = "rsa-sign";
3984 }
3985
3986 buf_write(&buf_data, b64_data, strlen(b64_data));
3988 {
3989 buf_write(&buf_data, ",", strlen(","));
3990 buf_write(&buf_data, algorithm, strlen(algorithm));
3991 }
3992 char *ret = management_query_multiline_flatten(man, (char *)buf_bptr(&buf_data), prompt, desc,
3995 free_buf(&buf_data);
3996 return ret;
3997}
3998
3999char *
4000management_query_cert(struct management *man, const char *cert_name)
4001{
4002 const char prompt_1[] = "NEED-CERTIFICATE:";
4003 struct buffer buf_prompt = alloc_buf(strlen(cert_name) + 20);
4004 buf_write(&buf_prompt, prompt_1, strlen(prompt_1));
4005 buf_write(&buf_prompt, cert_name, strlen(cert_name) + 1); /* +1 for \0 */
4006
4007 char *result;
4009 man, NULL, (char *)buf_bptr(&buf_prompt), "certificate", &man->connection.ext_cert_state,
4011 free_buf(&buf_prompt);
4012 return result;
4013}
4014
4015/*
4016 * Return true if management_hold() would block
4017 */
4018bool
4020{
4021 return (man->settings.flags & MF_HOLD) && !man->persist.hold_release && man_standalone_ok(man);
4022}
4023
4024/*
4025 * If the hold flag is enabled, hibernate until a management client releases the hold.
4026 * Return true if the caller should not sleep for an additional time interval.
4027 */
4028bool
4029management_hold(struct management *man, int holdtime)
4030{
4031 if (management_would_hold(man))
4032 {
4033 volatile int signal_received = 0;
4034 const bool standalone_disabled_save = man->persist.standalone_disabled;
4035 struct gc_arena gc = gc_new();
4036
4037 /* This is so M_CLIENT messages will be correctly passed through msg() */
4038 man->persist.standalone_disabled = false;
4039 man->persist.special_state_msg = NULL;
4041
4042 man_wait_for_client_connection(man, &signal_received, 0, MWCC_HOLD_WAIT);
4043
4044 if (!signal_received)
4045 {
4046 struct buffer out = alloc_buf_gc(128, &gc);
4047 buf_printf(&out, ">HOLD:Waiting for hold release:%d", holdtime);
4048 man->persist.special_state_msg = BSTR(&out);
4050
4051 /* run command processing event loop until we get our username/password */
4052 do
4053 {
4054 man_standalone_event_loop(man, &signal_received, 0);
4055 if (!signal_received)
4056 {
4057 man_check_for_signals(&signal_received);
4058 }
4059 if (signal_received)
4060 {
4061 break;
4062 }
4063 } while (!man->persist.hold_release);
4064 }
4065
4066 /* revert state */
4067 man->persist.standalone_disabled = standalone_disabled_save;
4068 man->persist.special_state_msg = NULL;
4070
4071 gc_free(&gc);
4072 return true;
4073 }
4074 return false;
4075}
4076
4077/*
4078 * struct command_line
4079 */
4080
4081struct command_line *
4082command_line_new(const size_t len)
4083{
4084 struct command_line *cl;
4085 ALLOC_OBJ_CLEAR(cl, struct command_line);
4086 cl->buf = alloc_buf(len);
4087 cl->residual = alloc_buf(len);
4088 return cl;
4089}
4090
4091void
4093{
4094 buf_clear(&cl->buf);
4095 buf_clear(&cl->residual);
4096}
4097
4098void
4100{
4101 if (!cl)
4102 {
4103 return;
4104 }
4106 free_buf(&cl->buf);
4107 free_buf(&cl->residual);
4108 free(cl);
4109}
4110
4111void
4112command_line_add(struct command_line *cl, const unsigned char *buf, const size_t len)
4113{
4114 for (size_t i = 0; i < len; ++i)
4115 {
4116 if (buf[i] && char_class(buf[i], (CC_PRINT | CC_NEWLINE)))
4117 {
4118 if (!buf_write_u8(&cl->buf, buf[i]))
4119 {
4120 buf_clear(&cl->buf);
4121 }
4122 }
4123 }
4124}
4125
4126const char *
4128{
4129 const char *ret = NULL;
4130
4131 int i = buf_substring_len(&cl->buf, '\n');
4132 if (i >= 0)
4133 {
4134 buf_copy_excess(&cl->residual, &cl->buf, i);
4135 buf_chomp(&cl->buf);
4136 ret = BSTR(&cl->buf);
4137 }
4138 return ret;
4139}
4140
4141void
4143{
4144 buf_clear(&cl->buf);
4145 buf_copy(&cl->buf, &cl->residual);
4146 buf_clear(&cl->residual);
4147}
4148
4149/*
4150 * struct log_entry
4151 */
4152
4153const char *
4154log_entry_print(const struct log_entry *e, unsigned int flags, struct gc_arena *gc)
4155{
4156 struct buffer out = alloc_buf_gc(ERR_BUF_SIZE, gc);
4157 if (flags & LOG_FATAL_NOTIFY)
4158 {
4159 buf_printf(&out, ">FATAL:");
4160 }
4161 if (flags & LOG_PRINT_LOG_PREFIX)
4162 {
4163 buf_printf(&out, ">LOG:");
4164 }
4165 if (flags & LOG_PRINT_ECHO_PREFIX)
4166 {
4167 buf_printf(&out, ">ECHO:");
4168 }
4169 if (flags & LOG_PRINT_STATE_PREFIX)
4170 {
4171 buf_printf(&out, ">STATE:");
4172 }
4173 if (flags & LOG_PRINT_INT_DATE)
4174 {
4175 buf_printf(&out, "%u,", (unsigned int)e->timestamp);
4176 }
4177 if (flags & LOG_PRINT_MSG_FLAGS)
4178 {
4179 buf_printf(&out, "%s,", msg_flags_string(e->u.msg_flags, gc));
4180 }
4181 if (flags & LOG_PRINT_STATE)
4182 {
4183 buf_printf(&out, "%s,", man_state_name(e->u.state));
4184 }
4185 if (flags & LOG_PRINT_INTVAL)
4186 {
4187 buf_printf(&out, "%d,", e->u.intval);
4188 }
4189 if (e->string)
4190 {
4191 buf_printf(&out, "%s", e->string);
4192 }
4193 if (flags & LOG_PRINT_LOCAL_IP)
4194 {
4196 }
4197 if (flags & LOG_PRINT_REMOTE_IP)
4198 {
4199 buf_printf(&out, ",%s",
4201 ? ","
4204 buf_printf(&out, ",%s",
4206 ? ","
4209 }
4210 if (flags & LOG_PRINT_LOCAL_IP && !IN6_IS_ADDR_UNSPECIFIED(&e->local_ip6))
4211 {
4213 }
4214 if (flags & LOG_ECHO_TO_LOG)
4215 {
4216 msg(D_MANAGEMENT, "MANAGEMENT: %s", BSTR(&out));
4217 }
4218 if (flags & LOG_PRINT_CRLF)
4219 {
4220 buf_printf(&out, "\r\n");
4221 }
4222 return BSTR(&out);
4223}
4224
4225static void
4227{
4228 /* Cast away constness of const char* */
4229 free((char *)e->string);
4230 CLEAR(*e);
4231}
4232
4233/*
4234 * struct log_history
4235 */
4236
4237static inline int
4238log_index(const struct log_history *h, int i)
4239{
4240 return modulo_add(h->base, i, h->capacity);
4241}
4242
4243static void
4245{
4246 CLEAR(*h);
4247 h->capacity = capacity;
4249}
4250
4251struct log_history *
4253{
4254 struct log_history *h;
4255 ASSERT(capacity > 0);
4256 ALLOC_OBJ(h, struct log_history);
4258 return h;
4259}
4260
4261static void
4263{
4264 int i;
4265 for (i = 0; i < h->size; ++i)
4266 {
4268 }
4269 free(h->array);
4270}
4271
4272void
4274{
4276 free(h);
4277}
4278
4279void
4280log_history_add(struct log_history *h, const struct log_entry *le)
4281{
4282 struct log_entry *e;
4283 ASSERT(h->size >= 0 && h->size <= h->capacity);
4284 if (h->size == h->capacity)
4285 {
4286 e = &h->array[h->base];
4288 h->base = log_index(h, 1);
4289 }
4290 else
4291 {
4292 e = &h->array[log_index(h, h->size)];
4293 ++h->size;
4294 }
4295
4296 *e = *le;
4297 e->string = string_alloc(le->string, NULL);
4298}
4299
4300void
4301log_history_resize(struct log_history *h, const int capacity)
4302{
4303 if (capacity != h->capacity)
4304 {
4305 struct log_history newlog;
4306 int i;
4307
4308 ASSERT(capacity > 0);
4310
4311 for (i = 0; i < h->size; ++i)
4312 {
4313 log_history_add(&newlog, &h->array[log_index(h, i)]);
4314 }
4315
4317 *h = newlog;
4318 }
4319}
4320
4321const struct log_entry *
4322log_history_ref(const struct log_history *h, const int index)
4323{
4324 if (index >= 0 && index < h->size)
4325 {
4326 return &h->array[log_index(h, (h->size - 1) - index)];
4327 }
4328 else
4329 {
4330 return NULL;
4331 }
4332}
4333
4334void
4336{
4337 if (n < 0)
4338 {
4339 return;
4340 }
4341 else if (management)
4342 {
4344 }
4345 else
4346 {
4347#ifdef _WIN32
4348 win32_sleep(n);
4349#else
4350 if (n > 0)
4351 {
4352 sleep(n);
4353 }
4354#endif
4355 }
4356}
4357
4358#if defined(__GNUC__) || defined(__clang__)
4359#pragma GCC diagnostic pop
4360#endif
4361
4362void
4363management_check_bytecount_client(struct context *c, struct management *man, struct timeval *timeval)
4364{
4365 if (man->persist.callback.flags & MCF_SERVER)
4366 {
4367 return;
4368 }
4369
4371 {
4372 if (dco_enabled(&c->options))
4373 {
4374 if (dco_get_peer_stats(c, true) < 0)
4375 {
4376 return;
4377 }
4378 }
4379
4382 }
4383}
4384
4385void
4386management_check_bytecount_server(struct multi_context *multi, struct timeval *timeval)
4387{
4389 {
4390 return;
4391 }
4392
4394 {
4395 /* fetch counters from dco */
4396 if (dco_enabled(&multi->top.options))
4397 {
4398 if (dco_get_peer_stats_multi(&multi->top.c1.tuntap->dco, true) < 0)
4399 {
4400 return;
4401 }
4402 }
4403
4404 /* iterate over peers and report counters for each connected peer */
4405 for (uint32_t i = 0; i <= multi->max_peerid; i++)
4406 {
4407 struct multi_instance *mi = multi->instances[i];
4408
4409 if (!mi)
4410 {
4411 continue;
4412 }
4413 struct context_2 *c2 = &mi->context.c2;
4414
4416 {
4418 }
4419 }
4420 }
4421}
4422
4423/* context_2 stats are reset on reconnect. Since client expects stats
4424 * to be preserved across reconnects, we need to save context_2
4425 * stats before tearing the tunnel down.
4426 */
4427void
4429{
4432
4433 /* no need to raise SIGUSR1 on error since we are already closing the instance */
4434 if (dco_enabled(&c->options) && (dco_get_peer_stats(c, false) == 0))
4435 {
4436 man->persist.bytes_in += c->c2.dco_read_bytes;
4438 }
4439}
4440
4441bool
4443 struct buffer_list *input)
4444{
4445 /* also ensures that are enough inputs in the list to safely call
4446 * buffer_list_peek without extra NULL checks */
4447 if (!input || input->size < 3)
4448 {
4449 msg(M_CLIENT, "not enough input lines for sending acc message, "
4450 "missing protocol, flag and/or messages lines");
4451 return false;
4452 }
4453
4454 struct gc_arena gc = gc_new();
4455
4456 const struct buffer *proto_buf = buffer_list_peek(input);
4457 const char *protocol = string_alloc(CBSTR(proto_buf), &gc);
4458 buffer_list_pop(input);
4459
4460 const struct buffer *encoding_buf = buffer_list_peek(input);
4461
4462 const char *acc_err_reason = NULL;
4463 int acc_flags = parse_acc_message_flags(buf_cstr(encoding_buf), &acc_err_reason, &gc);
4464 if (acc_flags < 0)
4465 {
4466 msg(M_CLIENT, "ERROR: parsing acc message flags failed: %s", acc_err_reason);
4467 goto error;
4468 }
4469
4470 buffer_list_pop(input);
4471
4472 buffer_list_aggregate_separator(input, 10000, "");
4473 const struct buffer *acc_msg_buf = buffer_list_peek(input);
4474
4475 if (!multi->acc_negotiated_protocols
4477 {
4478 msg(M_CLIENT, "ERROR: protocol not in the list of negotiated protocols: %s",
4480 goto error;
4481 }
4482
4483 bool ret = send_acc_message(c, multi, session, protocol,
4484 acc_flags & ACC_MESSAGE_FLAGS_FRAGMENT,
4485 buf_cstr(acc_msg_buf),
4486 acc_flags & ACC_MESSAGE_FLAGS_BASE64);
4487 gc_free(&gc);
4488 return ret;
4489error:
4490 gc_free(&gc);
4491 return false;
4492}
4493#else /* ifdef ENABLE_MANAGEMENT */
4494
4495#include "win32.h"
4496
4497#if !defined(_WIN32) && (defined(__GNUC__) || defined(__clang__))
4498#pragma GCC diagnostic push
4499#pragma GCC diagnostic ignored "-Wsign-conversion"
4500#endif
4501
4502void
4503management_sleep(const int n)
4504{
4505#ifdef _WIN32
4506 win32_sleep(n);
4507#else
4508 if (n > 0)
4509 {
4510 sleep(n);
4511 }
4512#endif /* ifdef _WIN32 */
4513}
4514
4515#if !defined(_WIN32) && (defined(__GNUC__) || defined(__clang__))
4516#pragma GCC diagnostic pop
4517#endif
4518
4519#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:239
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:102
@ ACC_MESSAGE_FLAGS_BASE64
Definition acc.h:101
#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:1204
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:174
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:1285
void buf_clear(struct buffer *buf)
Zeroise and reset a buffer.
Definition buffer.c:153
void buffer_list_reset(struct buffer_list *ol)
Empty the list ol and frees all the contained buffers.
Definition buffer.c:1210
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:1334
void buffer_list_pop(struct buffer_list *ol)
Remove and free the head buffer of the list.
Definition buffer.c:1340
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
struct buffer_list * buffer_list_new(void)
Allocate an empty buffer list of capacity max_size.
Definition buffer.c:1185
void chomp(char *str)
Remove trailing newline and carriage-return characters from a string.
Definition buffer.c:596
struct buffer * buffer_list_peek(struct buffer_list *ol)
Retrieve the head buffer.
Definition buffer.c:1267
void buffer_list_free(struct buffer_list *ol)
Frees a buffer list and all the buffers in it.
Definition buffer.c:1194
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:1357
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:783
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:618
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:1225
void buf_chomp(struct buffer *buf)
Remove trailing newline and carriage-return characters from a buffer.
Definition buffer.c:530
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:847
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:761
#define ALLOC_OBJ(dptr, type)
Allocate memory for a single object of the given type.
Definition buffer.h:2025
static uint8_t * buf_bptr(struct buffer *buf)
Return a pointer to the start of the buffer content.
Definition buffer.h: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:1347
#define ALLOC_ARRAY_CLEAR(dptr, type, n)
Allocate and zero-initialise an array of n elements of the given type.
Definition buffer.h:2077
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
Truncate src to len bytes and copy any excess to dest.
Definition buffer.h:1422
#define streq(x, y)
helper to work around the semantics of the strcmp() return value
Definition buffer.h:1822
#define CC_NEWLINE
newline
Definition buffer.h:1700
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:1288
#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:2144
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
#define CBSTR(buf)
Return the buffer content pointer cast to const char *.
Definition buffer.h:159
#define CC_PRINT
printable (>= 32, != 127)
Definition buffer.h:1694
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2036
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
uint64_t counter_type
Definition common.h:31
#define counter_format
Definition common.h:32
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:135
#define M_INFO
Definition errlevel.h:54
#define D_MANAGEMENT
Definition errlevel.h:86
#define D_LINK_ERRORS
Definition errlevel.h:56
struct event_set * event_set_init(int *maxevents, unsigned int flags)
Definition event.c:1205
#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:77
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:1261
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 management_pre_tunnel_close(struct management *man)
Definition manage.c:3268
void log_history_close(struct log_history *h)
Definition manage.c:4273
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:4386
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:4082
static void man_log(struct management *man, const char *parm)
Definition manage.c:725
void management_auth_token(const char *token)
Definition manage.c:3290
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:3583
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:3136
static ssize_t man_read(struct management *man)
Definition manage.c:2482
static bool man_check_for_signals(volatile int *signal_received)
Definition manage.c:3467
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:3312
void command_line_free(struct command_line *cl)
Definition manage.c:4099
static int man_block(struct management *man, volatile int *signal_received, const time_t expire)
Definition manage.c:3484
static void man_certificate(struct management *man)
Definition manage.c:1370
void command_line_next(struct command_line *cl)
Definition manage.c:4142
void management_clear_callback(struct management *man)
Definition manage.c:2939
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:2637
char * management_query_cert(struct management *man, const char *cert_name)
Definition manage.c:4000
static bool man_standalone_ok(const struct management *man)
Definition manage.c:3461
bool management_send_acc_message(struct context *c, struct tls_multi *multi, struct tls_session *session, struct buffer_list *input)
Definition manage.c:4442
void management_connection_established(struct management *man, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3175
bool management_hold(struct management *man, int holdtime)
Definition manage.c:4029
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:2695
static int log_index(const struct log_history *h, int i)
Definition manage.c:4238
static void man_connection_clear(struct man_connection *mc)
Definition manage.c:2624
#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:3296
static void man_connection_init(struct management *man)
Definition manage.c:2795
static bool env_filter_match(const char *env_str, const int env_filter_level)
Definition manage.c:3002
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:2840
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:4252
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:3581
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_query_user_pass(struct management *man, const char *type, const char *string, const bool needed, const char *prompt, char *dest, size_t len)
Definition manage.c:783
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:3947
static void man_proxy(struct management *man, const char **p)
Definition manage.c:1425
struct management * management_init(void)
Definition manage.c:2869
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:4092
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:4019
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:3680
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:4280
void management_notify_client_close(const struct management *man, struct man_def_auth_context *mdac, const struct env_set *es)
Definition manage.c:3185
static void log_entry_free_contents(struct log_entry *e)
Definition manage.c:4226
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:3101
#define MN_AT_LEAST
Definition manage.c:1398
void management_event_loop_n_seconds(struct management *man, int sec)
Definition manage.c:3627
static void man_persist_close(struct man_persist *mp)
Definition manage.c:2673
static void man_kill(struct management *man, const char *victim)
Definition manage.c:595
void management_close(struct management *man)
Definition manage.c:2922
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:2948
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:4154
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:3350
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:3589
#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:3107
#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:2883
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:3918
static void man_output_extra_env(struct management *man, const char *prefix)
Definition manage.c:3071
void man_persist_client_stats(struct management *man, struct context *c)
Definition manage.c:4428
void management_auth_failure(const struct management *man, const char *type, const char *reason)
Definition manage.c:3277
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:4244
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
static int management_query_multiline(struct management *man, const char *b64_data, const char *prompt, const char *cmd, int *state)
Definition manage.c:3840
#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:2932
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:3543
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:3158
void management_echo(struct management *man, const char *string, const bool pull)
Definition manage.c:3211
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:4322
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:2784
void management_check_bytecount_client(struct context *c, struct management *man, struct timeval *timeval)
Definition manage.c:4363
void management_notify(const char *severity, const char *type, const char *text)
Definition manage.c:3095
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:3974
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:3198
void command_line_add(struct command_line *cl, const unsigned char *buf, const size_t len)
Definition manage.c:4112
static int man_standalone_event_loop(struct management *man, volatile int *signal_received, const time_t expire)
Definition manage.c:3566
static void man_process_command(struct management *man, const char *line)
Definition manage.c:2271
static void man_output_env(const struct env_set *es, const bool tail, const int env_filter_level, const char *prefix)
Definition manage.c:3050
const char * command_line_get(struct command_line *cl)
Definition manage.c:4127
static void log_history_free_contents(struct log_history *h)
Definition manage.c:4262
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
Definition manage.c:4335
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:4301
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:3582
void management_post_tunnel_open(struct management *man, const in_addr_t tun_local_ip)
Definition manage.c:3245
static ssize_t man_write(struct management *man)
Definition manage.c:2576
void management_up_down(const struct management *man, const char *updown, const struct env_set *es)
Definition manage.c:3085
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:735
#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:757
#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:415
#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:3349
const char title_string[]
Definition options.c:75
bool auth_retry_set(const msglvl_t msglevel, const char *option)
Definition options.c:3326
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Definition options.h:981
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:338
bool platform_user_get(const char *username, struct platform_state_user *state)
Definition platform.c:85
bool platform_group_get(const char *groupname, struct platform_state_group *state)
Definition platform.c:129
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:562
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
Definition socket.c:431
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:610
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:410
bool ssl_clean_auth_token(void)
Definition ssl.c:399
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:64
bool status_close(struct status_output *so)
Definition status.c:187
A singly-linked list of buffers, with head/tail pointers for O(1) push.
Definition buffer.h:2168
size_t size
Current number of entries.
Definition buffer.h:2171
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:155
Level 2 context containing state that is reset on both SIGHUP and SIGUSR1 restarts.
Definition openvpn.h:207
counter_type link_read_bytes
Definition openvpn.h:249
counter_type link_write_bytes
Definition openvpn.h:252
counter_type dco_read_bytes
Definition openvpn.h:250
struct man_def_auth_context mda_context
Definition openvpn.h:424
counter_type dco_write_bytes
Definition openvpn.h:253
Contains all state information for one tunnel.
Definition openvpn.h:445
struct context_2 c2
Level 2 context.
Definition openvpn.h:490
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:446
struct context_1 c1
Level 1 context.
Definition openvpn.h:489
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
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
uint32_t max_peerid
highest currently allocated peer-id and maximum allocated/valid index in instances
Definition multi.h:174
struct context top
Storage structure for process-wide configuration.
Definition multi.h:217
struct multi_instance ** instances
Array of multi_instances with the size of max_clients.
Definition multi.h:171
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
unsigned int flags
Definition status.h:52
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:439
#define SIGINT
Definition syshead.h:56
#define SIGTERM
Definition syshead.h:59
SOCKET socket_descriptor_t
Definition syshead.h:441
#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:449
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:1536
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