cosmetics
[tomato.git] / release / src / router / openvpn / manage.c
blob0f72f64051961964deaaeac8f1e9632be2251c86
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.
8 * Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
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.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "syshead.h"
27 #ifdef ENABLE_MANAGEMENT
29 #include "error.h"
30 #include "fdmisc.h"
31 #include "options.h"
32 #include "sig.h"
33 #include "event.h"
34 #include "otime.h"
35 #include "integer.h"
36 #include "misc.h"
37 #include "ssl.h"
38 #include "common.h"
39 #include "manage.h"
41 #include "memdbg.h"
43 #ifdef ENABLE_PKCS11
44 #include "pkcs11.h"
45 #endif
47 #define MANAGEMENT_ECHO_PULL_INFO 0
49 #if MANAGEMENT_ECHO_PULL_INFO
50 #define MANAGEMENT_ECHO_FLAGS LOG_PRINT_INTVAL
51 #else
52 #define MANAGEMENT_ECHO_FLAGS 0
53 #endif
55 /* tag for blank username/password */
56 static const char blank_up[] = "[[BLANK]]";
58 struct management *management; /* GLOBAL */
60 /* static forward declarations */
61 static void man_output_standalone (struct management *man, volatile int *signal_received);
62 static void man_reset_client_socket (struct management *man, const bool exiting);
64 static void
65 man_help ()
67 msg (M_CLIENT, "Management Interface for %s", title_string);
68 msg (M_CLIENT, "Commands:");
69 msg (M_CLIENT, "auth-retry t : Auth failure retry mode (none,interact,nointeract).");
70 msg (M_CLIENT, "bytecount n : Show bytes in/out, update every n secs (0=off).");
71 msg (M_CLIENT, "echo [on|off] [N|all] : Like log, but only show messages in echo buffer.");
72 msg (M_CLIENT, "exit|quit : Close management session.");
73 msg (M_CLIENT, "forget-passwords : Forget passwords entered so far.");
74 msg (M_CLIENT, "help : Print this message.");
75 msg (M_CLIENT, "hold [on|off|release] : Set/show hold flag to on/off state, or");
76 msg (M_CLIENT, " release current hold and start tunnel.");
77 msg (M_CLIENT, "kill cn : Kill the client instance(s) having common name cn.");
78 msg (M_CLIENT, "kill IP:port : Kill the client instance connecting from IP:port.");
79 msg (M_CLIENT, "load-stats : Show global server load stats.");
80 msg (M_CLIENT, "log [on|off] [N|all] : Turn on/off realtime log display");
81 msg (M_CLIENT, " + show last N lines or 'all' for entire history.");
82 msg (M_CLIENT, "mute [n] : Set log mute level to n, or show level if n is absent.");
83 msg (M_CLIENT, "needok type action : Enter confirmation for NEED-OK request of 'type',");
84 msg (M_CLIENT, " where action = 'ok' or 'cancel'.");
85 msg (M_CLIENT, "needstr type action : Enter confirmation for NEED-STR request of 'type',");
86 msg (M_CLIENT, " where action is reply string.");
87 msg (M_CLIENT, "net : (Windows only) Show network info and routing table.");
88 msg (M_CLIENT, "password type p : Enter password p for a queried OpenVPN password.");
89 msg (M_CLIENT, "pid : Show process ID of the current OpenVPN process.");
90 #ifdef ENABLE_PKCS11
91 msg (M_CLIENT, "pkcs11-id-count : Get number of available PKCS#11 identities.");
92 msg (M_CLIENT, "pkcs11-id-get index : Get PKCS#11 identity at index.");
93 #endif
94 #ifdef MANAGEMENT_DEF_AUTH
95 msg (M_CLIENT, "client-auth CID KID : Authenticate client-id/key-id CID/KID (MULTILINE)");
96 msg (M_CLIENT, "client-auth-nt CID KID : Authenticate client-id/key-id CID/KID");
97 msg (M_CLIENT, "client-deny CID KID R [CR] : Deny auth client-id/key-id CID/KID with log reason");
98 msg (M_CLIENT, " text R and optional client reason text CR");
99 msg (M_CLIENT, "client-kill CID : Kill client instance CID");
100 #ifdef MANAGEMENT_PF
101 msg (M_CLIENT, "client-pf CID : Define packet filter for client CID (MULTILINE)");
102 #endif
103 #endif
104 msg (M_CLIENT, "signal s : Send signal s to daemon,");
105 msg (M_CLIENT, " s = SIGHUP|SIGTERM|SIGUSR1|SIGUSR2.");
106 msg (M_CLIENT, "state [on|off] [N|all] : Like log, but show state history.");
107 msg (M_CLIENT, "status [n] : Show current daemon status info using format #n.");
108 msg (M_CLIENT, "test n : Produce n lines of output for testing/debugging.");
109 msg (M_CLIENT, "username type u : Enter username u for a queried OpenVPN username.");
110 msg (M_CLIENT, "verb [n] : Set log verbosity level to n, or show if n is absent.");
111 msg (M_CLIENT, "version : Show current version number.");
112 msg (M_CLIENT, "END");
115 static const char *
116 man_state_name (const int state)
118 switch (state)
120 case OPENVPN_STATE_INITIAL:
121 return "INITIAL";
122 case OPENVPN_STATE_CONNECTING:
123 return "CONNECTING";
124 case OPENVPN_STATE_WAIT:
125 return "WAIT";
126 case OPENVPN_STATE_AUTH:
127 return "AUTH";
128 case OPENVPN_STATE_GET_CONFIG:
129 return "GET_CONFIG";
130 case OPENVPN_STATE_ASSIGN_IP:
131 return "ASSIGN_IP";
132 case OPENVPN_STATE_ADD_ROUTES:
133 return "ADD_ROUTES";
134 case OPENVPN_STATE_CONNECTED:
135 return "CONNECTED";
136 case OPENVPN_STATE_RECONNECTING:
137 return "RECONNECTING";
138 case OPENVPN_STATE_EXITING:
139 return "EXITING";
140 case OPENVPN_STATE_RESOLVE:
141 return "RESOLVE";
142 case OPENVPN_STATE_TCP_CONNECT:
143 return "TCP_CONNECT";
144 default:
145 return "?";
149 static void
150 man_welcome (struct management *man)
152 msg (M_CLIENT, ">INFO:OpenVPN Management Interface Version %d -- type 'help' for more info",
153 MANAGEMENT_VERSION);
154 if (man->persist.special_state_msg)
155 msg (M_CLIENT, "%s", man->persist.special_state_msg);
158 static inline bool
159 man_password_needed (struct management *man)
161 return man->settings.up.defined && !man->connection.password_verified;
164 static void
165 man_check_password (struct management *man, const char *line)
167 if (man_password_needed (man))
169 if (streq (line, man->settings.up.password))
171 man->connection.password_verified = true;
172 msg (M_CLIENT, "SUCCESS: password is correct");
173 man_welcome (man);
175 else
177 man->connection.password_verified = false;
178 msg (M_CLIENT, "ERROR: bad password");
179 if (++man->connection.password_tries >= MANAGEMENT_N_PASSWORD_RETRIES)
181 msg (M_WARN, "MAN: client connection rejected after %d failed password attempts",
182 MANAGEMENT_N_PASSWORD_RETRIES);
183 man->connection.halt = true;
189 static void
190 man_update_io_state (struct management *man)
192 if (socket_defined (man->connection.sd_cli))
194 if (buffer_list_defined (man->connection.out))
196 man->connection.state = MS_CC_WAIT_WRITE;
198 else
200 man->connection.state = MS_CC_WAIT_READ;
205 static void
206 man_output_list_push (struct management *man, const char *str)
208 if (management_connected (man))
210 if (str)
211 buffer_list_push (man->connection.out, (const unsigned char *) str);
212 man_update_io_state (man);
213 if (!man->persist.standalone_disabled)
215 volatile int signal_received = 0;
216 man_output_standalone (man, &signal_received);
221 static void
222 man_prompt (struct management *man)
224 if (man_password_needed (man))
225 man_output_list_push (man, "ENTER PASSWORD:");
226 #if 0 /* should we use prompt? */
227 else
228 man_output_list_push (man, PACKAGE_NAME ">");
229 #endif
232 static void
233 man_delete_unix_socket (struct management *man)
235 #if UNIX_SOCK_SUPPORT
236 if ((man->settings.flags & (MF_UNIX_SOCK|MF_CONNECT_AS_CLIENT)) == MF_UNIX_SOCK)
237 socket_delete_unix (&man->settings.local_unix);
238 #endif
241 static void
242 man_close_socket (struct management *man, const socket_descriptor_t sd)
244 #ifndef WIN32
246 * Windows doesn't need this because the ne32 event is permanently
247 * enabled at struct management scope.
249 if (man->persist.callback.delete_event)
250 (*man->persist.callback.delete_event) (man->persist.callback.arg, sd);
251 #endif
252 openvpn_close_socket (sd);
255 static void
256 virtual_output_callback_func (void *arg, const unsigned int flags, const char *str)
258 static int recursive_level = 0; /* GLOBAL */
260 if (!recursive_level) /* don't allow recursion */
262 struct gc_arena gc = gc_new ();
263 struct management *man = (struct management *) arg;
264 struct log_entry e;
265 const char *out = NULL;
267 ++recursive_level;
269 CLEAR (e);
270 update_time ();
271 e.timestamp = now;
272 e.u.msg_flags = flags;
273 e.string = str;
275 if (flags & M_FATAL)
276 man->persist.standalone_disabled = false;
278 if (flags != M_CLIENT)
279 log_history_add (man->persist.log, &e);
281 if (!man_password_needed (man))
283 if (flags == M_CLIENT)
284 out = log_entry_print (&e, LOG_PRINT_CRLF, &gc);
285 else if (man->connection.log_realtime)
286 out = log_entry_print (&e, LOG_PRINT_INT_DATE
287 | LOG_PRINT_MSG_FLAGS
288 | LOG_PRINT_LOG_PREFIX
289 | LOG_PRINT_CRLF, &gc);
290 if (out)
291 man_output_list_push (man, out);
292 if (flags & M_FATAL)
294 out = log_entry_print (&e, LOG_FATAL_NOTIFY|LOG_PRINT_CRLF, &gc);
295 if (out)
297 man_output_list_push (man, out);
298 man_reset_client_socket (man, true);
303 --recursive_level;
304 gc_free (&gc);
309 * Given a signal, return the signal with possible remapping applied,
310 * or -1 if the signal should be ignored.
312 static int
313 man_mod_signal (const struct management *man, const int signum)
315 const unsigned int flags = man->settings.mansig;
316 int s = signum;
317 if (s == SIGUSR1)
319 if (flags & MANSIG_MAP_USR1_TO_HUP)
320 s = SIGHUP;
321 if (flags & MANSIG_MAP_USR1_TO_TERM)
322 s = SIGTERM;
324 if (flags & MANSIG_IGNORE_USR1_HUP)
326 if (s == SIGHUP || s == SIGUSR1)
327 s = -1;
329 return s;
332 static void
333 man_signal (struct management *man, const char *name)
335 const int sig = parse_signal (name);
336 if (sig >= 0)
338 const int sig_mod = man_mod_signal (man, sig);
339 if (sig_mod >= 0)
341 throw_signal (sig_mod);
342 msg (M_CLIENT, "SUCCESS: signal %s thrown", signal_name (sig_mod, true));
344 else
346 if (man->persist.special_state_msg)
347 msg (M_CLIENT, "%s", man->persist.special_state_msg);
348 else
349 msg (M_CLIENT, "ERROR: signal '%s' is currently ignored", name);
352 else
354 msg (M_CLIENT, "ERROR: signal '%s' is not a known signal type", name);
358 static void
359 man_status (struct management *man, const int version, struct status_output *so)
361 if (man->persist.callback.status)
363 (*man->persist.callback.status) (man->persist.callback.arg, version, so);
365 else
367 msg (M_CLIENT, "ERROR: The 'status' command is not supported by the current daemon mode");
371 static void
372 man_bytecount (struct management *man, const int update_seconds)
374 if (update_seconds >= 0)
375 man->connection.bytecount_update_seconds = update_seconds;
376 else
377 man->connection.bytecount_update_seconds = 0;
378 msg (M_CLIENT, "SUCCESS: bytecount interval changed");
381 void
382 man_bytecount_output_client (struct management *man)
384 char in[32];
385 char out[32];
386 /* do in a roundabout way to work around possible mingw or mingw-glibc bug */
387 openvpn_snprintf (in, sizeof (in), counter_format, man->persist.bytes_in);
388 openvpn_snprintf (out, sizeof (out), counter_format, man->persist.bytes_out);
389 msg (M_CLIENT, ">BYTECOUNT:%s,%s", in, out);
390 man->connection.bytecount_last_update = now;
393 #ifdef MANAGEMENT_DEF_AUTH
395 void
396 man_bytecount_output_server (struct management *man,
397 const counter_type *bytes_in_total,
398 const counter_type *bytes_out_total,
399 struct man_def_auth_context *mdac)
401 char in[32];
402 char out[32];
403 /* do in a roundabout way to work around possible mingw or mingw-glibc bug */
404 openvpn_snprintf (in, sizeof (in), counter_format, *bytes_in_total);
405 openvpn_snprintf (out, sizeof (out), counter_format, *bytes_out_total);
406 msg (M_CLIENT, ">BYTECOUNT_CLI:%lu,%s,%s", mdac->cid, in, out);
407 mdac->bytecount_last_update = now;
410 #endif
412 static void
413 man_kill (struct management *man, const char *victim)
415 struct gc_arena gc = gc_new ();
417 if (man->persist.callback.kill_by_cn && man->persist.callback.kill_by_addr)
419 struct buffer buf;
420 char p1[128];
421 char p2[128];
422 int n_killed;
424 buf_set_read (&buf, (uint8_t*) victim, strlen (victim) + 1);
425 buf_parse (&buf, ':', p1, sizeof (p1));
426 buf_parse (&buf, ':', p2, sizeof (p2));
428 if (strlen (p1) && strlen (p2))
430 /* IP:port specified */
431 bool status;
432 const in_addr_t addr = getaddr (GETADDR_HOST_ORDER|GETADDR_MSG_VIRT_OUT, p1, 0, &status, NULL);
433 if (status)
435 const int port = atoi (p2);
436 if (port > 0 && port < 65536)
438 n_killed = (*man->persist.callback.kill_by_addr) (man->persist.callback.arg, addr, port);
439 if (n_killed > 0)
441 msg (M_CLIENT, "SUCCESS: %d client(s) at address %s:%d killed",
442 n_killed,
443 print_in_addr_t (addr, 0, &gc),
444 port);
446 else
448 msg (M_CLIENT, "ERROR: client at address %s:%d not found",
449 print_in_addr_t (addr, 0, &gc),
450 port);
453 else
455 msg (M_CLIENT, "ERROR: port number is out of range: %s", p2);
458 else
460 msg (M_CLIENT, "ERROR: error parsing IP address: %s", p1);
463 else if (strlen (p1))
465 /* common name specified */
466 n_killed = (*man->persist.callback.kill_by_cn) (man->persist.callback.arg, p1);
467 if (n_killed > 0)
469 msg (M_CLIENT, "SUCCESS: common name '%s' found, %d client(s) killed", p1, n_killed);
471 else
473 msg (M_CLIENT, "ERROR: common name '%s' not found", p1);
476 else
478 msg (M_CLIENT, "ERROR: kill parse");
481 else
483 msg (M_CLIENT, "ERROR: The 'kill' command is not supported by the current daemon mode");
486 gc_free (&gc);
490 * General-purpose history command handler
491 * for the log and echo commands.
493 static void
494 man_history (struct management *man,
495 const char *parm,
496 const char *type,
497 struct log_history *log,
498 bool *realtime,
499 const unsigned int lep_flags)
501 struct gc_arena gc = gc_new ();
502 int n = 0;
504 if (streq (parm, "on"))
506 *realtime = true;
507 msg (M_CLIENT, "SUCCESS: real-time %s notification set to ON", type);
509 else if (streq (parm, "off"))
511 *realtime = false;
512 msg (M_CLIENT, "SUCCESS: real-time %s notification set to OFF", type);
514 else if (streq (parm, "all") || (n = atoi (parm)) > 0)
516 const int size = log_history_size (log);
517 const int start = (n ? n : size) - 1;
518 int i;
520 for (i = start; i >= 0; --i)
522 const struct log_entry *e = log_history_ref (log, i);
523 if (e)
525 const char *out = log_entry_print (e, lep_flags, &gc);
526 virtual_output_callback_func (man, M_CLIENT, out);
529 msg (M_CLIENT, "END");
531 else
533 msg (M_CLIENT, "ERROR: %s parameter must be 'on' or 'off' or some number n or 'all'", type);
536 gc_free (&gc);
539 static void
540 man_log (struct management *man, const char *parm)
542 man_history (man,
543 parm,
544 "log",
545 man->persist.log,
546 &man->connection.log_realtime,
547 LOG_PRINT_INT_DATE|LOG_PRINT_MSG_FLAGS);
550 static void
551 man_echo (struct management *man, const char *parm)
553 man_history (man,
554 parm,
555 "echo",
556 man->persist.echo,
557 &man->connection.echo_realtime,
558 LOG_PRINT_INT_DATE|MANAGEMENT_ECHO_FLAGS);
561 static void
562 man_state (struct management *man, const char *parm)
564 man_history (man,
565 parm,
566 "state",
567 man->persist.state,
568 &man->connection.state_realtime,
569 LOG_PRINT_INT_DATE|LOG_PRINT_STATE|
570 LOG_PRINT_LOCAL_IP|LOG_PRINT_REMOTE_IP);
573 static void
574 man_up_finalize (struct management *man)
576 switch (man->connection.up_query_mode)
578 case UP_QUERY_DISABLED:
579 man->connection.up_query.defined = false;
580 break;
581 case UP_QUERY_USER_PASS:
582 if (strlen (man->connection.up_query.username) && strlen (man->connection.up_query.password))
583 man->connection.up_query.defined = true;
584 break;
585 case UP_QUERY_PASS:
586 if (strlen (man->connection.up_query.password))
587 man->connection.up_query.defined = true;
588 break;
589 case UP_QUERY_NEED_OK:
590 if (strlen (man->connection.up_query.password))
591 man->connection.up_query.defined = true;
592 break;
593 case UP_QUERY_NEED_STR:
594 if (strlen (man->connection.up_query.password))
595 man->connection.up_query.defined = true;
596 break;
597 default:
598 ASSERT (0);
602 static void
603 man_query_user_pass (struct management *man,
604 const char *type,
605 const char *string,
606 const bool needed,
607 const char *prompt,
608 char *dest,
609 int len)
611 if (needed)
613 ASSERT (man->connection.up_query_type);
614 if (streq (man->connection.up_query_type, type))
616 strncpynt (dest, string, len);
617 man_up_finalize (man);
618 msg (M_CLIENT, "SUCCESS: '%s' %s entered, but not yet verified",
619 type,
620 prompt);
622 else
623 msg (M_CLIENT, "ERROR: %s of type '%s' entered, but we need one of type '%s'",
624 prompt,
625 type,
626 man->connection.up_query_type);
628 else
630 msg (M_CLIENT, "ERROR: no %s is currently needed at this time", prompt);
634 static void
635 man_query_username (struct management *man, const char *type, const char *string)
637 const bool needed = (man->connection.up_query_mode == UP_QUERY_USER_PASS && man->connection.up_query_type);
638 man_query_user_pass (man, type, string, needed, "username", man->connection.up_query.username, USER_PASS_LEN);
641 static void
642 man_query_password (struct management *man, const char *type, const char *string)
644 const bool needed = ((man->connection.up_query_mode == UP_QUERY_USER_PASS
645 || man->connection.up_query_mode == UP_QUERY_PASS)
646 && man->connection.up_query_type);
647 if (!string[0]) /* allow blank passwords to be passed through using the blank_up tag */
648 string = blank_up;
649 man_query_user_pass (man, type, string, needed, "password", man->connection.up_query.password, USER_PASS_LEN);
652 static void
653 man_query_need_ok (struct management *man, const char *type, const char *action)
655 const bool needed = ((man->connection.up_query_mode == UP_QUERY_NEED_OK) && man->connection.up_query_type);
656 man_query_user_pass (man, type, action, needed, "needok-confirmation", man->connection.up_query.password, USER_PASS_LEN);
659 static void
660 man_query_need_str (struct management *man, const char *type, const char *action)
662 const bool needed = ((man->connection.up_query_mode == UP_QUERY_NEED_STR) && man->connection.up_query_type);
663 man_query_user_pass (man, type, action, needed, "needstr-string", man->connection.up_query.password, USER_PASS_LEN);
666 static void
667 man_forget_passwords (struct management *man)
669 #if defined(USE_CRYPTO) && defined(USE_SSL)
670 ssl_purge_auth ();
671 msg (M_CLIENT, "SUCCESS: Passwords were forgotten");
672 #endif
675 static void
676 man_net (struct management *man)
678 if (man->persist.callback.show_net)
680 (*man->persist.callback.show_net) (man->persist.callback.arg, M_CLIENT);
682 else
684 msg (M_CLIENT, "ERROR: The 'net' command is not supported by the current daemon mode");
688 #ifdef ENABLE_PKCS11
690 static void
691 man_pkcs11_id_count (struct management *man)
693 msg (M_CLIENT, ">PKCS11ID-COUNT:%d", pkcs11_management_id_count ());
696 static void
697 man_pkcs11_id_get (struct management *man, const int index)
699 char *id = NULL;
700 char *base64 = NULL;
702 if (pkcs11_management_id_get (index, &id, &base64))
703 msg (M_CLIENT, ">PKCS11ID-ENTRY:'%d', ID:'%s', BLOB:'%s'", index, id, base64);
704 else
705 msg (M_CLIENT, ">PKCS11ID-ENTRY:'%d'", index);
707 if (id != NULL)
708 free (id);
709 if (base64 != NULL)
710 free (base64);
713 #endif
715 static void
716 man_hold (struct management *man, const char *cmd)
718 if (cmd)
720 if (streq (cmd, "on"))
722 man->settings.flags |= MF_HOLD;
723 msg (M_CLIENT, "SUCCESS: hold flag set to ON");
725 else if (streq (cmd, "off"))
727 man->settings.flags &= ~MF_HOLD;
728 msg (M_CLIENT, "SUCCESS: hold flag set to OFF");
730 else if (streq (cmd, "release"))
732 man->persist.hold_release = true;
733 msg (M_CLIENT, "SUCCESS: hold release succeeded");
735 else
737 msg (M_CLIENT, "ERROR: bad hold command parameter");
740 else
741 msg (M_CLIENT, "SUCCESS: hold=%d", BOOL_CAST(man->settings.flags & MF_HOLD));
744 #ifdef MANAGEMENT_DEF_AUTH
746 static bool
747 parse_cid (const char *str, unsigned long *cid)
749 if (sscanf (str, "%lu", cid) == 1)
750 return true;
751 else
753 msg (M_CLIENT, "ERROR: cannot parse CID");
754 return false;
758 static bool
759 parse_kid (const char *str, unsigned int *kid)
761 if (sscanf (str, "%u", kid) == 1)
762 return true;
763 else
765 msg (M_CLIENT, "ERROR: cannot parse KID");
766 return false;
770 static void
771 in_extra_reset (struct man_connection *mc, const bool new)
773 if (mc)
775 if (!new)
777 mc->in_extra_cmd = IEC_UNDEF;
778 mc->in_extra_cid = 0;
779 mc->in_extra_kid = 0;
781 if (mc->in_extra)
783 buffer_list_free (mc->in_extra);
784 mc->in_extra = NULL;
786 if (new)
787 mc->in_extra = buffer_list_new (0);
791 static void
792 in_extra_dispatch (struct management *man)
794 switch (man->connection.in_extra_cmd)
796 case IEC_CLIENT_AUTH:
797 if (man->persist.callback.client_auth)
799 const bool status = (*man->persist.callback.client_auth)
800 (man->persist.callback.arg,
801 man->connection.in_extra_cid,
802 man->connection.in_extra_kid,
803 true,
804 NULL,
805 NULL,
806 man->connection.in_extra);
807 man->connection.in_extra = NULL;
808 if (status)
810 msg (M_CLIENT, "SUCCESS: client-auth command succeeded");
812 else
814 msg (M_CLIENT, "ERROR: client-auth command failed");
817 else
819 msg (M_CLIENT, "ERROR: The client-auth command is not supported by the current daemon mode");
821 break;
822 #ifdef MANAGEMENT_PF
823 case IEC_CLIENT_PF:
824 if (man->persist.callback.client_pf)
826 const bool status = (*man->persist.callback.client_pf)
827 (man->persist.callback.arg,
828 man->connection.in_extra_cid,
829 man->connection.in_extra);
830 man->connection.in_extra = NULL;
831 if (status)
833 msg (M_CLIENT, "SUCCESS: client-pf command succeeded");
835 else
837 msg (M_CLIENT, "ERROR: client-pf command failed");
840 else
842 msg (M_CLIENT, "ERROR: The client-pf command is not supported by the current daemon mode");
844 break;
845 #endif
847 in_extra_reset (&man->connection, false);
850 static void
851 man_client_auth (struct management *man, const char *cid_str, const char *kid_str, const bool extra)
853 struct man_connection *mc = &man->connection;
854 mc->in_extra_cid = 0;
855 mc->in_extra_kid = 0;
856 if (parse_cid (cid_str, &mc->in_extra_cid)
857 && parse_kid (kid_str, &mc->in_extra_kid))
859 mc->in_extra_cmd = IEC_CLIENT_AUTH;
860 in_extra_reset (mc, true);
861 if (!extra)
862 in_extra_dispatch (man);
866 static void
867 man_client_deny (struct management *man, const char *cid_str, const char *kid_str, const char *reason, const char *client_reason)
869 unsigned long cid = 0;
870 unsigned int kid = 0;
871 if (parse_cid (cid_str, &cid) && parse_kid (kid_str, &kid))
873 if (man->persist.callback.client_auth)
875 const bool status = (*man->persist.callback.client_auth)
876 (man->persist.callback.arg,
877 cid,
878 kid,
879 false,
880 reason,
881 client_reason,
882 NULL);
883 if (status)
885 msg (M_CLIENT, "SUCCESS: client-deny command succeeded");
887 else
889 msg (M_CLIENT, "ERROR: client-deny command failed");
892 else
894 msg (M_CLIENT, "ERROR: The client-deny command is not supported by the current daemon mode");
899 static void
900 man_client_kill (struct management *man, const char *cid_str)
902 unsigned long cid = 0;
903 if (parse_cid (cid_str, &cid))
905 if (man->persist.callback.kill_by_cid)
907 const bool status = (*man->persist.callback.kill_by_cid) (man->persist.callback.arg, cid);
908 if (status)
910 msg (M_CLIENT, "SUCCESS: client-kill command succeeded");
912 else
914 msg (M_CLIENT, "ERROR: client-kill command failed");
917 else
919 msg (M_CLIENT, "ERROR: The client-kill command is not supported by the current daemon mode");
924 static void
925 man_client_n_clients (struct management *man)
927 if (man->persist.callback.n_clients)
929 const int nclients = (*man->persist.callback.n_clients) (man->persist.callback.arg);
930 msg (M_CLIENT, "SUCCESS: nclients=%d", nclients);
932 else
934 msg (M_CLIENT, "ERROR: The nclients command is not supported by the current daemon mode");
938 #ifdef MANAGEMENT_PF
940 static void
941 man_client_pf (struct management *man, const char *cid_str)
943 struct man_connection *mc = &man->connection;
944 mc->in_extra_cid = 0;
945 mc->in_extra_kid = 0;
946 if (parse_cid (cid_str, &mc->in_extra_cid))
948 mc->in_extra_cmd = IEC_CLIENT_PF;
949 in_extra_reset (mc, true);
953 #endif
954 #endif
956 static void
957 man_load_stats (struct management *man)
959 extern counter_type link_read_bytes_global;
960 extern counter_type link_write_bytes_global;
961 int nclients = 0;
963 if (man->persist.callback.n_clients)
964 nclients = (*man->persist.callback.n_clients) (man->persist.callback.arg);
965 msg (M_CLIENT, "SUCCESS: nclients=%d,bytesin=" counter_format ",bytesout=" counter_format,
966 nclients,
967 link_read_bytes_global,
968 link_write_bytes_global);
971 #define MN_AT_LEAST (1<<0)
973 static bool
974 man_need (struct management *man, const char **p, const int n, unsigned int flags)
976 int i;
977 ASSERT (p[0]);
978 for (i = 1; i <= n; ++i)
980 if (!p[i])
982 msg (M_CLIENT, "ERROR: the '%s' command requires %s%d parameter%s",
983 p[0],
984 (flags & MN_AT_LEAST) ? "at least " : "",
986 n > 1 ? "s" : "");
987 return false;
990 return true;
993 static void
994 man_dispatch_command (struct management *man, struct status_output *so, const char **p, const int nparms)
996 struct gc_arena gc = gc_new ();
998 ASSERT (p[0]);
999 if (streq (p[0], "exit") || streq (p[0], "quit"))
1001 man->connection.halt = true;
1002 goto done;
1004 else if (streq (p[0], "help"))
1006 man_help ();
1008 else if (streq (p[0], "version"))
1010 msg (M_CLIENT, "OpenVPN Version: %s", title_string);
1011 msg (M_CLIENT, "Management Version: %d", MANAGEMENT_VERSION);
1012 msg (M_CLIENT, "END");
1014 else if (streq (p[0], "pid"))
1016 msg (M_CLIENT, "SUCCESS: pid=%d", openvpn_getpid ());
1018 #ifdef MANAGEMENT_DEF_AUTH
1019 else if (streq (p[0], "nclients"))
1021 man_client_n_clients (man);
1023 #endif
1024 else if (streq (p[0], "signal"))
1026 if (man_need (man, p, 1, 0))
1027 man_signal (man, p[1]);
1029 else if (streq (p[0], "load-stats"))
1031 man_load_stats (man);
1033 else if (streq (p[0], "status"))
1035 int version = 0;
1036 if (p[1])
1037 version = atoi (p[1]);
1038 man_status (man, version, so);
1040 else if (streq (p[0], "kill"))
1042 if (man_need (man, p, 1, 0))
1043 man_kill (man, p[1]);
1045 else if (streq (p[0], "verb"))
1047 if (p[1])
1049 const int level = atoi(p[1]);
1050 if (set_debug_level (level, 0))
1051 msg (M_CLIENT, "SUCCESS: verb level changed");
1052 else
1053 msg (M_CLIENT, "ERROR: verb level is out of range");
1055 else
1056 msg (M_CLIENT, "SUCCESS: verb=%d", get_debug_level ());
1058 else if (streq (p[0], "mute"))
1060 if (p[1])
1062 const int level = atoi(p[1]);
1063 if (set_mute_cutoff (level))
1064 msg (M_CLIENT, "SUCCESS: mute level changed");
1065 else
1066 msg (M_CLIENT, "ERROR: mute level is out of range");
1068 else
1069 msg (M_CLIENT, "SUCCESS: mute=%d", get_mute_cutoff ());
1071 else if (streq (p[0], "auth-retry"))
1073 #if P2MP
1074 if (p[1])
1076 if (auth_retry_set (M_CLIENT, p[1]))
1077 msg (M_CLIENT, "SUCCESS: auth-retry parameter changed");
1078 else
1079 msg (M_CLIENT, "ERROR: bad auth-retry parameter");
1081 else
1082 msg (M_CLIENT, "SUCCESS: auth-retry=%s", auth_retry_print ());
1083 #else
1084 msg (M_CLIENT, "ERROR: auth-retry feature is unavailable");
1085 #endif
1087 else if (streq (p[0], "state"))
1089 if (!p[1])
1091 man_state (man, "1");
1093 else
1095 if (p[1])
1096 man_state (man, p[1]);
1097 if (p[2])
1098 man_state (man, p[2]);
1101 else if (streq (p[0], "log"))
1103 if (man_need (man, p, 1, MN_AT_LEAST))
1105 if (p[1])
1106 man_log (man, p[1]);
1107 if (p[2])
1108 man_log (man, p[2]);
1111 else if (streq (p[0], "echo"))
1113 if (man_need (man, p, 1, MN_AT_LEAST))
1115 if (p[1])
1116 man_echo (man, p[1]);
1117 if (p[2])
1118 man_echo (man, p[2]);
1121 else if (streq (p[0], "username"))
1123 if (man_need (man, p, 2, 0))
1124 man_query_username (man, p[1], p[2]);
1126 else if (streq (p[0], "password"))
1128 if (man_need (man, p, 2, 0))
1129 man_query_password (man, p[1], p[2]);
1131 else if (streq (p[0], "forget-passwords"))
1133 man_forget_passwords (man);
1135 else if (streq (p[0], "needok"))
1137 if (man_need (man, p, 2, 0))
1138 man_query_need_ok (man, p[1], p[2]);
1140 else if (streq (p[0], "needstr"))
1142 if (man_need (man, p, 2, 0))
1143 man_query_need_str (man, p[1], p[2]);
1145 else if (streq (p[0], "net"))
1147 man_net (man);
1149 else if (streq (p[0], "hold"))
1151 man_hold (man, p[1]);
1153 else if (streq (p[0], "bytecount"))
1155 if (man_need (man, p, 1, 0))
1156 man_bytecount (man, atoi(p[1]));
1158 #ifdef MANAGEMENT_DEF_AUTH
1159 else if (streq (p[0], "client-kill"))
1161 if (man_need (man, p, 1, 0))
1162 man_client_kill (man, p[1]);
1164 else if (streq (p[0], "client-deny"))
1166 if (man_need (man, p, 3, MN_AT_LEAST))
1167 man_client_deny (man, p[1], p[2], p[3], p[4]);
1169 else if (streq (p[0], "client-auth-nt"))
1171 if (man_need (man, p, 2, 0))
1172 man_client_auth (man, p[1], p[2], false);
1174 else if (streq (p[0], "client-auth"))
1176 if (man_need (man, p, 2, 0))
1177 man_client_auth (man, p[1], p[2], true);
1179 #ifdef MANAGEMENT_PF
1180 else if (streq (p[0], "client-pf"))
1182 if (man_need (man, p, 1, 0))
1183 man_client_pf (man, p[1]);
1185 #endif
1186 #endif
1187 #ifdef ENABLE_PKCS11
1188 else if (streq (p[0], "pkcs11-id-count"))
1190 man_pkcs11_id_count (man);
1192 else if (streq (p[0], "pkcs11-id-get"))
1194 if (man_need (man, p, 1, 0))
1195 man_pkcs11_id_get (man, atoi(p[1]));
1197 #endif
1198 #if 1
1199 else if (streq (p[0], "test"))
1201 if (man_need (man, p, 1, 0))
1203 int i;
1204 const int n = atoi (p[1]);
1205 for (i = 0; i < n; ++i)
1207 msg (M_CLIENT, "[%d] The purpose of this command is to generate large amounts of output.", i);
1211 #endif
1212 else
1214 msg (M_CLIENT, "ERROR: unknown command, enter 'help' for more options");
1217 done:
1218 gc_free (&gc);
1221 #ifdef WIN32
1223 static void
1224 man_start_ne32 (struct management *man)
1226 switch (man->connection.state)
1228 case MS_LISTEN:
1229 net_event_win32_start (&man->connection.ne32, FD_ACCEPT, man->connection.sd_top);
1230 break;
1231 case MS_CC_WAIT_READ:
1232 case MS_CC_WAIT_WRITE:
1233 net_event_win32_start (&man->connection.ne32, FD_READ|FD_WRITE|FD_CLOSE, man->connection.sd_cli);
1234 break;
1235 default:
1236 ASSERT (0);
1240 static void
1241 man_stop_ne32 (struct management *man)
1243 net_event_win32_stop (&man->connection.ne32);
1246 #endif
1248 static void
1249 man_record_peer_info (struct management *man)
1251 struct gc_arena gc = gc_new ();
1252 if (man->settings.write_peer_info_file)
1254 bool success = false;
1255 #ifdef HAVE_GETSOCKNAME
1256 if (socket_defined (man->connection.sd_cli))
1258 struct sockaddr_in addr;
1259 socklen_t addrlen = sizeof (addr);
1260 int status;
1262 CLEAR (addr);
1263 status = getsockname (man->connection.sd_cli, (struct sockaddr *)&addr, &addrlen);
1264 if (!status && addrlen == sizeof (addr))
1266 const in_addr_t a = ntohl (addr.sin_addr.s_addr);
1267 const int p = ntohs (addr.sin_port);
1268 FILE *fp = fopen (man->settings.write_peer_info_file, "w");
1269 if (fp)
1271 fprintf (fp, "%s\n%d\n", print_in_addr_t (a, 0, &gc), p);
1272 if (!fclose (fp))
1273 success = true;
1277 #endif
1278 if (!success)
1280 msg (D_MANAGEMENT, "MANAGEMENT: failed to write peer info to file %s",
1281 man->settings.write_peer_info_file);
1282 throw_signal_soft (SIGTERM, "management-connect-failed");
1285 gc_free (&gc);
1288 static void
1289 man_connection_settings_reset (struct management *man)
1291 man->connection.state_realtime = false;
1292 man->connection.log_realtime = false;
1293 man->connection.echo_realtime = false;
1294 man->connection.bytecount_update_seconds = 0;
1295 man->connection.password_verified = false;
1296 man->connection.password_tries = 0;
1297 man->connection.halt = false;
1298 man->connection.state = MS_CC_WAIT_WRITE;
1301 static void
1302 man_new_connection_post (struct management *man, const char *description)
1304 struct gc_arena gc = gc_new ();
1306 set_nonblock (man->connection.sd_cli);
1307 set_cloexec (man->connection.sd_cli);
1309 man_connection_settings_reset (man);
1311 #ifdef WIN32
1312 man_start_ne32 (man);
1313 #endif
1315 #if UNIX_SOCK_SUPPORT
1316 if (man->settings.flags & MF_UNIX_SOCK)
1318 msg (D_MANAGEMENT, "MANAGEMENT: %s %s",
1319 description,
1320 sockaddr_unix_name (&man->settings.local_unix, "NULL"));
1322 else
1323 #endif
1324 msg (D_MANAGEMENT, "MANAGEMENT: %s %s",
1325 description,
1326 print_sockaddr (&man->settings.local, &gc));
1328 buffer_list_reset (man->connection.out);
1330 if (!man_password_needed (man))
1331 man_welcome (man);
1332 man_prompt (man);
1333 man_update_io_state (man);
1335 gc_free (&gc);
1338 #if UNIX_SOCK_SUPPORT
1339 static bool
1340 man_verify_unix_peer_uid_gid (struct management *man, const socket_descriptor_t sd)
1342 if (socket_defined (sd) && (man->settings.client_uid != -1 || man->settings.client_gid != -1))
1344 static const char err_prefix[] = "MANAGEMENT: unix domain socket client connection rejected --";
1345 int uid, gid;
1346 if (unix_socket_get_peer_uid_gid (man->connection.sd_cli, &uid, &gid))
1348 if (man->settings.client_uid != -1 && man->settings.client_uid != uid)
1350 msg (D_MANAGEMENT, "%s UID of socket peer (%d) doesn't match required value (%d) as given by --management-client-user",
1351 err_prefix, uid, man->settings.client_uid);
1352 return false;
1354 if (man->settings.client_gid != -1 && man->settings.client_gid != gid)
1356 msg (D_MANAGEMENT, "%s GID of socket peer (%d) doesn't match required value (%d) as given by --management-client-group",
1357 err_prefix, gid, man->settings.client_gid);
1358 return false;
1361 else
1363 msg (D_MANAGEMENT, "%s cannot get UID/GID of socket peer", err_prefix);
1364 return false;
1367 return true;
1369 #endif
1371 static void
1372 man_accept (struct management *man)
1374 struct link_socket_actual act;
1375 CLEAR (act);
1378 * Accept the TCP or Unix domain socket client.
1380 #if UNIX_SOCK_SUPPORT
1381 if (man->settings.flags & MF_UNIX_SOCK)
1383 struct sockaddr_un remote;
1384 man->connection.sd_cli = socket_accept_unix (man->connection.sd_top, &remote);
1385 if (!man_verify_unix_peer_uid_gid (man, man->connection.sd_cli))
1386 sd_close (&man->connection.sd_cli);
1388 else
1389 #endif
1390 man->connection.sd_cli = socket_do_accept (man->connection.sd_top, &act, false);
1392 if (socket_defined (man->connection.sd_cli))
1394 man->connection.remote = act.dest;
1396 if (socket_defined (man->connection.sd_top))
1398 #ifdef WIN32
1399 man_stop_ne32 (man);
1400 #endif
1403 man_new_connection_post (man, "Client connected from");
1407 static void
1408 man_listen (struct management *man)
1410 struct gc_arena gc = gc_new ();
1413 * Initialize state
1415 man->connection.state = MS_LISTEN;
1416 man->connection.sd_cli = SOCKET_UNDEFINED;
1419 * Initialize listening socket
1421 if (man->connection.sd_top == SOCKET_UNDEFINED)
1423 #if UNIX_SOCK_SUPPORT
1424 if (man->settings.flags & MF_UNIX_SOCK)
1426 man_delete_unix_socket (man);
1427 man->connection.sd_top = create_socket_unix ();
1428 socket_bind_unix (man->connection.sd_top, &man->settings.local_unix, "MANAGEMENT");
1430 else
1431 #endif
1433 man->connection.sd_top = create_socket_tcp ();
1434 socket_bind (man->connection.sd_top, &man->settings.local, "MANAGEMENT");
1438 * Listen for connection
1440 if (listen (man->connection.sd_top, 1))
1441 msg (M_SOCKERR, "MANAGEMENT: listen() failed");
1444 * Set misc socket properties
1446 set_nonblock (man->connection.sd_top);
1447 set_cloexec (man->connection.sd_top);
1449 #if UNIX_SOCK_SUPPORT
1450 if (man->settings.flags & MF_UNIX_SOCK)
1452 msg (D_MANAGEMENT, "MANAGEMENT: unix domain socket listening on %s",
1453 sockaddr_unix_name (&man->settings.local_unix, "NULL"));
1455 else
1456 #endif
1457 msg (D_MANAGEMENT, "MANAGEMENT: TCP Socket listening on %s",
1458 print_sockaddr (&man->settings.local, &gc));
1461 #ifdef WIN32
1462 man_start_ne32 (man);
1463 #endif
1465 gc_free (&gc);
1468 static void
1469 man_connect (struct management *man)
1471 struct gc_arena gc = gc_new ();
1472 int status;
1473 int signal_received = 0;
1476 * Initialize state
1478 man->connection.state = MS_INITIAL;
1479 man->connection.sd_top = SOCKET_UNDEFINED;
1481 #if UNIX_SOCK_SUPPORT
1482 if (man->settings.flags & MF_UNIX_SOCK)
1484 man->connection.sd_cli = create_socket_unix ();
1485 status = socket_connect_unix (man->connection.sd_cli, &man->settings.local_unix);
1486 if (!status && !man_verify_unix_peer_uid_gid (man, man->connection.sd_cli))
1488 #ifdef EPERM
1489 status = EPERM;
1490 #else
1491 status = 1;
1492 #endif
1493 sd_close (&man->connection.sd_cli);
1496 else
1497 #endif
1499 man->connection.sd_cli = create_socket_tcp ();
1500 status = openvpn_connect (man->connection.sd_cli,
1501 &man->settings.local,
1503 &signal_received);
1506 if (signal_received)
1508 throw_signal (signal_received);
1509 goto done;
1512 if (status)
1514 #if UNIX_SOCK_SUPPORT
1515 if (man->settings.flags & MF_UNIX_SOCK)
1517 msg (D_LINK_ERRORS,
1518 "MANAGEMENT: connect to unix socket %s failed: %s",
1519 sockaddr_unix_name (&man->settings.local_unix, "NULL"),
1520 strerror_ts (status, &gc));
1522 else
1523 #endif
1524 msg (D_LINK_ERRORS,
1525 "MANAGEMENT: connect to %s failed: %s",
1526 print_sockaddr (&man->settings.local, &gc),
1527 strerror_ts (status, &gc));
1528 throw_signal_soft (SIGTERM, "management-connect-failed");
1529 goto done;
1532 man_record_peer_info (man);
1533 man_new_connection_post (man, "Connected to management server at");
1535 done:
1536 gc_free (&gc);
1539 static void
1540 man_reset_client_socket (struct management *man, const bool exiting)
1542 if (socket_defined (man->connection.sd_cli))
1544 msg (D_MANAGEMENT, "MANAGEMENT: Client disconnected");
1545 #ifdef WIN32
1546 man_stop_ne32 (man);
1547 #endif
1548 man_close_socket (man, man->connection.sd_cli);
1549 man->connection.sd_cli = SOCKET_UNDEFINED;
1550 command_line_reset (man->connection.in);
1551 buffer_list_reset (man->connection.out);
1552 #ifdef MANAGEMENT_DEF_AUTH
1553 in_extra_reset (&man->connection, false);
1554 #endif
1556 if (!exiting)
1558 #if defined(USE_CRYPTO) && defined(USE_SSL)
1559 if (man->settings.flags & MF_FORGET_DISCONNECT)
1560 ssl_purge_auth ();
1561 #endif
1562 if (man->settings.flags & MF_SIGNAL) {
1563 int mysig = man_mod_signal (man, SIGUSR1);
1564 if (mysig >= 0)
1566 msg (D_MANAGEMENT, "MANAGEMENT: Triggering management signal");
1567 throw_signal_soft (mysig, "management-disconnect");
1571 if (man->settings.flags & MF_CONNECT_AS_CLIENT)
1573 msg (D_MANAGEMENT, "MANAGEMENT: Triggering management exit");
1574 throw_signal_soft (SIGTERM, "management-exit");
1576 else
1577 man_listen (man);
1581 static void
1582 man_process_command (struct management *man, const char *line)
1584 struct gc_arena gc = gc_new ();
1585 struct status_output *so;
1586 int nparms;
1587 char *parms[MAX_PARMS+1];
1589 CLEAR (parms);
1590 so = status_open (NULL, 0, -1, &man->persist.vout, 0);
1591 #ifdef MANAGEMENT_DEF_AUTH
1592 in_extra_reset (&man->connection, false);
1593 #endif
1595 if (man_password_needed (man))
1597 man_check_password (man, line);
1599 else
1601 nparms = parse_line (line, parms, MAX_PARMS, "TCP", 0, M_CLIENT, &gc);
1602 if (parms[0] && streq (parms[0], "password"))
1603 msg (D_MANAGEMENT_DEBUG, "MANAGEMENT: CMD 'password [...]'");
1604 else if (!streq (line, "load-stats"))
1605 msg (D_MANAGEMENT_DEBUG, "MANAGEMENT: CMD '%s'", line);
1607 #if 0
1608 /* DEBUGGING -- print args */
1610 int i;
1611 for (i = 0; i < nparms; ++i)
1612 msg (M_INFO, "[%d] '%s'", i, parms[i]);
1614 #endif
1616 if (nparms > 0)
1617 man_dispatch_command (man, so, (const char **)parms, nparms);
1620 CLEAR (parms);
1621 status_close (so);
1622 gc_free (&gc);
1625 static bool
1626 man_io_error (struct management *man, const char *prefix)
1628 const int err = openvpn_errno_socket ();
1630 if (!ignore_sys_error (err))
1632 struct gc_arena gc = gc_new ();
1633 msg (D_MANAGEMENT, "MANAGEMENT: TCP %s error: %s",
1634 prefix,
1635 strerror_ts (err, &gc));
1636 gc_free (&gc);
1637 return true;
1639 else
1640 return false;
1643 static int
1644 man_read (struct management *man)
1647 * read command line from socket
1649 unsigned char buf[256];
1650 int len = 0;
1652 len = recv (man->connection.sd_cli, buf, sizeof (buf), MSG_NOSIGNAL);
1653 if (len == 0)
1655 man_reset_client_socket (man, false);
1657 else if (len > 0)
1659 bool processed_command = false;
1661 ASSERT (len <= (int) sizeof (buf));
1662 command_line_add (man->connection.in, buf, len);
1665 * Reset output object
1667 buffer_list_reset (man->connection.out);
1670 * process command line if complete
1673 const unsigned char *line;
1674 while ((line = command_line_get (man->connection.in)))
1676 #ifdef MANAGEMENT_DEF_AUTH
1677 if (man->connection.in_extra)
1679 if (!strcmp ((char *)line, "END"))
1681 in_extra_dispatch (man);
1682 in_extra_reset (&man->connection, false);
1684 else
1686 buffer_list_push (man->connection.in_extra, line);
1689 else
1690 #endif
1691 man_process_command (man, (char *) line);
1692 if (man->connection.halt)
1693 break;
1694 command_line_next (man->connection.in);
1695 processed_command = true;
1700 * Reset output state to MS_CC_WAIT_(READ|WRITE)
1702 if (man->connection.halt)
1704 man_reset_client_socket (man, false);
1705 len = 0;
1707 else
1709 if (processed_command)
1710 man_prompt (man);
1711 man_update_io_state (man);
1714 else /* len < 0 */
1716 if (man_io_error (man, "recv"))
1717 man_reset_client_socket (man, false);
1719 return len;
1722 static int
1723 man_write (struct management *man)
1725 const int max_send = 256;
1726 int sent = 0;
1728 const struct buffer *buf = buffer_list_peek (man->connection.out);
1729 if (buf && BLEN (buf))
1731 const int len = min_int (max_send, BLEN (buf));
1732 sent = send (man->connection.sd_cli, BPTR (buf), len, MSG_NOSIGNAL);
1733 if (sent >= 0)
1735 buffer_list_advance (man->connection.out, sent);
1737 else if (sent < 0)
1739 if (man_io_error (man, "send"))
1740 man_reset_client_socket (man, false);
1745 * Reset output state to MS_CC_WAIT_(READ|WRITE)
1747 man_update_io_state (man);
1749 return sent;
1752 static void
1753 man_connection_clear (struct man_connection *mc)
1755 CLEAR (*mc);
1757 /* set initial state */
1758 mc->state = MS_INITIAL;
1760 /* clear socket descriptors */
1761 mc->sd_top = SOCKET_UNDEFINED;
1762 mc->sd_cli = SOCKET_UNDEFINED;
1765 static void
1766 man_persist_init (struct management *man,
1767 const int log_history_cache,
1768 const int echo_buffer_size,
1769 const int state_buffer_size)
1771 struct man_persist *mp = &man->persist;
1772 if (!mp->defined)
1774 CLEAR (*mp);
1776 /* initialize log history store */
1777 mp->log = log_history_init (log_history_cache);
1780 * Initialize virtual output object, so that functions
1781 * which write to a virtual_output object can be redirected
1782 * here to the management object.
1784 mp->vout.func = virtual_output_callback_func;
1785 mp->vout.arg = man;
1786 mp->vout.flags_default = M_CLIENT;
1787 msg_set_virtual_output (&mp->vout);
1790 * Initialize --echo list
1792 man->persist.echo = log_history_init (echo_buffer_size);
1795 * Initialize --state list
1797 man->persist.state = log_history_init (state_buffer_size);
1799 mp->defined = true;
1803 static void
1804 man_persist_close (struct man_persist *mp)
1806 if (mp->log)
1808 msg_set_virtual_output (NULL);
1809 log_history_close (mp->log);
1812 if (mp->echo)
1813 log_history_close (mp->echo);
1815 if (mp->state)
1816 log_history_close (mp->state);
1818 CLEAR (*mp);
1821 static void
1822 man_settings_init (struct man_settings *ms,
1823 const char *addr,
1824 const int port,
1825 const char *pass_file,
1826 const char *client_user,
1827 const char *client_group,
1828 const int log_history_cache,
1829 const int echo_buffer_size,
1830 const int state_buffer_size,
1831 const char *write_peer_info_file,
1832 const int remap_sigusr1,
1833 const unsigned int flags)
1835 if (!ms->defined)
1837 CLEAR (*ms);
1839 ms->flags = flags;
1840 ms->client_uid = -1;
1841 ms->client_gid = -1;
1844 * Get username/password
1846 if (pass_file)
1847 get_user_pass (&ms->up, pass_file, "Management", GET_USER_PASS_PASSWORD_ONLY);
1850 * lookup client UID/GID if specified
1852 if (client_user)
1854 struct user_state s;
1855 get_user (client_user, &s);
1856 ms->client_uid = user_state_uid (&s);
1857 msg (D_MANAGEMENT, "MANAGEMENT: client_uid=%d", ms->client_uid);
1858 ASSERT (ms->client_uid >= 0);
1860 if (client_group)
1862 struct group_state s;
1863 get_group (client_group, &s);
1864 ms->client_gid = group_state_gid (&s);
1865 msg (D_MANAGEMENT, "MANAGEMENT: client_gid=%d", ms->client_gid);
1866 ASSERT (ms->client_gid >= 0);
1869 ms->write_peer_info_file = string_alloc (write_peer_info_file, NULL);
1871 #if UNIX_SOCK_SUPPORT
1872 if (ms->flags & MF_UNIX_SOCK)
1873 sockaddr_unix_init (&ms->local_unix, addr);
1874 else
1875 #endif
1878 * Initialize socket address
1880 ms->local.sa.sin_family = AF_INET;
1881 ms->local.sa.sin_addr.s_addr = 0;
1882 ms->local.sa.sin_port = htons (port);
1885 * Run management over tunnel, or
1886 * separate channel?
1888 if (streq (addr, "tunnel") && !(flags & MF_CONNECT_AS_CLIENT))
1890 ms->management_over_tunnel = true;
1892 else
1894 ms->local.sa.sin_addr.s_addr = getaddr
1895 (GETADDR_RESOLVE|GETADDR_WARN_ON_SIGNAL|GETADDR_FATAL, addr, 0, NULL, NULL);
1900 * Log history and echo buffer may need to be resized
1902 ms->log_history_cache = log_history_cache;
1903 ms->echo_buffer_size = echo_buffer_size;
1904 ms->state_buffer_size = state_buffer_size;
1907 * Set remap sigusr1 flags
1909 if (remap_sigusr1 == SIGHUP)
1910 ms->mansig |= MANSIG_MAP_USR1_TO_HUP;
1911 else if (remap_sigusr1 == SIGTERM)
1912 ms->mansig |= MANSIG_MAP_USR1_TO_TERM;
1914 ms->defined = true;
1918 static void
1919 man_settings_close (struct man_settings *ms)
1921 free (ms->write_peer_info_file);
1922 CLEAR (*ms);
1926 static void
1927 man_connection_init (struct management *man)
1929 if (man->connection.state == MS_INITIAL)
1931 #ifdef WIN32
1933 * This object is a sort of TCP/IP helper
1934 * for Windows.
1936 net_event_win32_init (&man->connection.ne32);
1937 #endif
1940 * Allocate helper objects for command line input and
1941 * command output from/to the socket.
1943 man->connection.in = command_line_new (1024);
1944 man->connection.out = buffer_list_new (0);
1947 * Initialize event set for standalone usage, when we are
1948 * running outside of the primary event loop.
1951 int maxevents = 1;
1952 man->connection.es = event_set_init (&maxevents, EVENT_METHOD_FAST);
1956 * Listen/connect socket
1958 if (man->settings.flags & MF_CONNECT_AS_CLIENT)
1959 man_connect (man);
1960 else
1961 man_listen (man);
1965 static void
1966 man_connection_close (struct management *man)
1968 struct man_connection *mc = &man->connection;
1970 if (mc->es)
1971 event_free (mc->es);
1972 #ifdef WIN32
1973 net_event_win32_close (&mc->ne32);
1974 #endif
1975 if (socket_defined (mc->sd_top))
1977 man_close_socket (man, mc->sd_top);
1978 man_delete_unix_socket (man);
1980 if (socket_defined (mc->sd_cli))
1981 man_close_socket (man, mc->sd_cli);
1982 if (mc->in)
1983 command_line_free (mc->in);
1984 if (mc->out)
1985 buffer_list_free (mc->out);
1986 #ifdef MANAGEMENT_DEF_AUTH
1987 in_extra_reset (&man->connection, false);
1988 #endif
1989 man_connection_clear (mc);
1992 struct management *
1993 management_init (void)
1995 struct management *man;
1996 ALLOC_OBJ_CLEAR (man, struct management);
1998 man_persist_init (man,
1999 MANAGEMENT_LOG_HISTORY_INITIAL_SIZE,
2000 MANAGEMENT_ECHO_BUFFER_SIZE,
2001 MANAGEMENT_STATE_BUFFER_SIZE);
2003 man_connection_clear (&man->connection);
2005 return man;
2008 bool
2009 management_open (struct management *man,
2010 const char *addr,
2011 const int port,
2012 const char *pass_file,
2013 const char *client_user,
2014 const char *client_group,
2015 const int log_history_cache,
2016 const int echo_buffer_size,
2017 const int state_buffer_size,
2018 const char *write_peer_info_file,
2019 const int remap_sigusr1,
2020 const unsigned int flags)
2022 bool ret = false;
2025 * Save the settings only if they have not
2026 * been saved before.
2028 man_settings_init (&man->settings,
2029 addr,
2030 port,
2031 pass_file,
2032 client_user,
2033 client_group,
2034 log_history_cache,
2035 echo_buffer_size,
2036 state_buffer_size,
2037 write_peer_info_file,
2038 remap_sigusr1,
2039 flags);
2042 * The log is initially sized to MANAGEMENT_LOG_HISTORY_INITIAL_SIZE,
2043 * but may be changed here. Ditto for echo and state buffers.
2045 log_history_resize (man->persist.log, man->settings.log_history_cache);
2046 log_history_resize (man->persist.echo, man->settings.echo_buffer_size);
2047 log_history_resize (man->persist.state, man->settings.state_buffer_size);
2050 * If connection object is uninitialized and we are not doing
2051 * over-the-tunnel management, then open (listening) connection.
2053 if (man->connection.state == MS_INITIAL)
2055 if (!man->settings.management_over_tunnel)
2057 man_connection_init (man);
2058 ret = true;
2062 return ret;
2065 void
2066 management_close (struct management *man)
2068 man_connection_close (man);
2069 man_settings_close (&man->settings);
2070 man_persist_close (&man->persist);
2071 free (man);
2074 void
2075 management_set_callback (struct management *man,
2076 const struct management_callback *cb)
2078 man->persist.standalone_disabled = true;
2079 man->persist.callback = *cb;
2082 void
2083 management_clear_callback (struct management *man)
2085 man->persist.standalone_disabled = false;
2086 man->persist.hold_release = false;
2087 CLEAR (man->persist.callback);
2088 man_output_list_push (man, NULL); /* flush output queue */
2091 void
2092 management_set_state (struct management *man,
2093 const int state,
2094 const char *detail,
2095 const in_addr_t tun_local_ip,
2096 const in_addr_t tun_remote_ip)
2098 if (man->persist.state && (!(man->settings.flags & MF_SERVER) || state < OPENVPN_STATE_CLIENT_BASE))
2100 struct gc_arena gc = gc_new ();
2101 struct log_entry e;
2102 const char *out = NULL;
2104 update_time ();
2105 CLEAR (e);
2106 e.timestamp = now;
2107 e.u.state = state;
2108 e.string = detail;
2109 e.local_ip = tun_local_ip;
2110 e.remote_ip = tun_remote_ip;
2112 log_history_add (man->persist.state, &e);
2114 if (man->connection.state_realtime)
2115 out = log_entry_print (&e, LOG_PRINT_STATE_PREFIX
2116 | LOG_PRINT_INT_DATE
2117 | LOG_PRINT_STATE
2118 | LOG_PRINT_LOCAL_IP
2119 | LOG_PRINT_REMOTE_IP
2120 | LOG_PRINT_CRLF
2121 | LOG_ECHO_TO_LOG, &gc);
2123 if (out)
2124 man_output_list_push (man, out);
2126 gc_free (&gc);
2130 #ifdef MANAGEMENT_DEF_AUTH
2132 static void
2133 man_output_env (const struct env_set *es, const bool tail)
2135 if (es)
2137 struct env_item *e;
2138 for (e = es->list; e != NULL; e = e->next)
2140 if (e->string)
2141 msg (M_CLIENT, ">CLIENT:ENV,%s", e->string);
2144 if (tail)
2145 msg (M_CLIENT, ">CLIENT:ENV,END");
2148 static void
2149 man_output_extra_env (struct management *man)
2151 struct gc_arena gc = gc_new ();
2152 struct env_set *es = env_set_create (&gc);
2153 if (man->persist.callback.n_clients)
2155 const int nclients = (*man->persist.callback.n_clients) (man->persist.callback.arg);
2156 setenv_int (es, "n_clients", nclients);
2158 man_output_env (es, false);
2159 gc_free (&gc);
2162 void
2163 management_notify_client_needing_auth (struct management *management,
2164 const unsigned int mda_key_id,
2165 struct man_def_auth_context *mdac,
2166 const struct env_set *es)
2168 if (!(mdac->flags & DAF_CONNECTION_CLOSED))
2170 const char *mode = "CONNECT";
2171 if (mdac->flags & DAF_CONNECTION_ESTABLISHED)
2172 mode = "REAUTH";
2173 msg (M_CLIENT, ">CLIENT:%s,%lu,%u", mode, mdac->cid, mda_key_id);
2174 man_output_extra_env (management);
2175 man_output_env (es, true);
2176 mdac->flags |= DAF_INITIAL_AUTH;
2180 void
2181 management_connection_established (struct management *management,
2182 struct man_def_auth_context *mdac,
2183 const struct env_set *es)
2185 mdac->flags |= DAF_CONNECTION_ESTABLISHED;
2186 msg (M_CLIENT, ">CLIENT:ESTABLISHED,%lu", mdac->cid);
2187 man_output_extra_env (management);
2188 man_output_env (es, true);
2191 void
2192 management_notify_client_close (struct management *management,
2193 struct man_def_auth_context *mdac,
2194 const struct env_set *es)
2196 if ((mdac->flags & DAF_INITIAL_AUTH) && !(mdac->flags & DAF_CONNECTION_CLOSED))
2198 msg (M_CLIENT, ">CLIENT:DISCONNECT,%lu", mdac->cid);
2199 man_output_env (es, true);
2200 mdac->flags |= DAF_CONNECTION_CLOSED;
2204 void
2205 management_learn_addr (struct management *management,
2206 struct man_def_auth_context *mdac,
2207 const struct mroute_addr *addr,
2208 const bool primary)
2210 struct gc_arena gc = gc_new ();
2211 if ((mdac->flags & DAF_INITIAL_AUTH) && !(mdac->flags & DAF_CONNECTION_CLOSED))
2213 msg (M_CLIENT, ">CLIENT:ADDRESS,%lu,%s,%d",
2214 mdac->cid,
2215 mroute_addr_print_ex (addr, MAPF_SUBNET, &gc),
2216 BOOL_CAST (primary));
2218 gc_free (&gc);
2221 #endif
2223 void
2224 management_echo (struct management *man, const char *string, const bool pull)
2226 if (man->persist.echo)
2228 struct gc_arena gc = gc_new ();
2229 struct log_entry e;
2230 const char *out = NULL;
2232 update_time ();
2233 CLEAR (e);
2234 e.timestamp = now;
2235 e.string = string;
2236 e.u.intval = BOOL_CAST (pull);
2238 log_history_add (man->persist.echo, &e);
2240 if (man->connection.echo_realtime)
2241 out = log_entry_print (&e, LOG_PRINT_INT_DATE|LOG_PRINT_ECHO_PREFIX|LOG_PRINT_CRLF|MANAGEMENT_ECHO_FLAGS, &gc);
2243 if (out)
2244 man_output_list_push (man, out);
2246 gc_free (&gc);
2250 void
2251 management_post_tunnel_open (struct management *man, const in_addr_t tun_local_ip)
2254 * If we are running management over the tunnel,
2255 * this is the place to initialize the connection.
2257 if (man->settings.management_over_tunnel
2258 && man->connection.state == MS_INITIAL)
2260 /* listen on our local TUN/TAP IP address */
2261 man->settings.local.sa.sin_addr.s_addr = htonl (tun_local_ip);
2262 man_connection_init (man);
2267 void
2268 management_pre_tunnel_close (struct management *man)
2270 if (man->settings.management_over_tunnel)
2271 man_connection_close (man);
2274 void
2275 management_auth_failure (struct management *man, const char *type)
2277 msg (M_CLIENT, ">PASSWORD:Verification Failed: '%s'", type);
2280 static inline bool
2281 man_persist_state (unsigned int *persistent, const int n)
2283 if (persistent)
2285 if (*persistent == (unsigned int)n)
2286 return false;
2287 *persistent = n;
2289 return true;
2292 #ifdef WIN32
2294 void
2295 management_socket_set (struct management *man,
2296 struct event_set *es,
2297 void *arg,
2298 unsigned int *persistent)
2300 if (man->connection.state != MS_INITIAL)
2302 event_t ev = net_event_win32_get_event (&man->connection.ne32);
2303 net_event_win32_reset_write (&man->connection.ne32);
2305 switch (man->connection.state)
2307 case MS_LISTEN:
2308 if (man_persist_state (persistent, 1))
2309 event_ctl (es, ev, EVENT_READ, arg);
2310 break;
2311 case MS_CC_WAIT_READ:
2312 if (man_persist_state (persistent, 2))
2313 event_ctl (es, ev, EVENT_READ, arg);
2314 break;
2315 case MS_CC_WAIT_WRITE:
2316 if (man_persist_state (persistent, 3))
2317 event_ctl (es, ev, EVENT_READ|EVENT_WRITE, arg);
2318 break;
2319 default:
2320 ASSERT (0);
2325 void
2326 management_io (struct management *man)
2328 if (man->connection.state != MS_INITIAL)
2330 long net_events;
2331 net_event_win32_reset (&man->connection.ne32);
2332 net_events = net_event_win32_get_event_mask (&man->connection.ne32);
2334 if (net_events & FD_CLOSE)
2336 man_reset_client_socket (man, false);
2338 else
2340 if (man->connection.state == MS_LISTEN)
2342 if (net_events & FD_ACCEPT)
2344 man_accept (man);
2345 net_event_win32_clear_selected_events (&man->connection.ne32, FD_ACCEPT);
2348 else if (man->connection.state == MS_CC_WAIT_READ)
2350 if (net_events & FD_READ)
2352 while (man_read (man) > 0)
2354 net_event_win32_clear_selected_events (&man->connection.ne32, FD_READ);
2358 if (man->connection.state == MS_CC_WAIT_WRITE)
2360 if (net_events & FD_WRITE)
2362 int status;
2363 /* dmsg (M_INFO, "FD_WRITE set"); */
2364 status = man_write (man);
2365 if (status < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
2367 /* dmsg (M_INFO, "FD_WRITE cleared"); */
2368 net_event_win32_clear_selected_events (&man->connection.ne32, FD_WRITE);
2376 #else
2378 void
2379 management_socket_set (struct management *man,
2380 struct event_set *es,
2381 void *arg,
2382 unsigned int *persistent)
2384 switch (man->connection.state)
2386 case MS_LISTEN:
2387 if (man_persist_state (persistent, 1))
2388 event_ctl (es, man->connection.sd_top, EVENT_READ, arg);
2389 break;
2390 case MS_CC_WAIT_READ:
2391 if (man_persist_state (persistent, 2))
2392 event_ctl (es, man->connection.sd_cli, EVENT_READ, arg);
2393 break;
2394 case MS_CC_WAIT_WRITE:
2395 if (man_persist_state (persistent, 3))
2396 event_ctl (es, man->connection.sd_cli, EVENT_WRITE, arg);
2397 break;
2398 case MS_INITIAL:
2399 break;
2400 default:
2401 ASSERT (0);
2405 void
2406 management_io (struct management *man)
2408 switch (man->connection.state)
2410 case MS_LISTEN:
2411 man_accept (man);
2412 break;
2413 case MS_CC_WAIT_READ:
2414 man_read (man);
2415 break;
2416 case MS_CC_WAIT_WRITE:
2417 man_write (man);
2418 break;
2419 case MS_INITIAL:
2420 break;
2421 default:
2422 ASSERT (0);
2426 #endif
2428 static inline bool
2429 man_standalone_ok (const struct management *man)
2431 return !man->settings.management_over_tunnel && man->connection.state != MS_INITIAL;
2434 static bool
2435 man_check_for_signals (volatile int *signal_received)
2437 if (signal_received)
2439 get_signal (signal_received);
2440 if (*signal_received)
2441 return true;
2443 return false;
2447 * Wait for socket I/O when outside primary event loop
2449 static int
2450 man_block (struct management *man, volatile int *signal_received, const time_t expire)
2452 struct timeval tv;
2453 struct event_set_return esr;
2454 int status = -1;
2456 if (man_standalone_ok (man))
2460 event_reset (man->connection.es);
2461 management_socket_set (man, man->connection.es, NULL, NULL);
2462 tv.tv_usec = 0;
2463 tv.tv_sec = 1;
2464 if (man_check_for_signals (signal_received))
2466 status = -1;
2467 break;
2469 status = event_wait (man->connection.es, &tv, &esr, 1);
2470 update_time ();
2471 if (man_check_for_signals (signal_received))
2473 status = -1;
2474 break;
2476 /* set SIGINT signal if expiration time exceeded */
2477 if (expire && now >= expire)
2479 status = 0;
2480 if (signal_received)
2481 *signal_received = SIGINT;
2482 break;
2484 } while (status != 1);
2486 return status;
2490 * Perform management socket output outside primary event loop
2492 static void
2493 man_output_standalone (struct management *man, volatile int *signal_received)
2495 if (man_standalone_ok (man))
2497 while (man->connection.state == MS_CC_WAIT_WRITE)
2499 management_io (man);
2500 if (man->connection.state == MS_CC_WAIT_WRITE)
2501 man_block (man, signal_received, 0);
2502 if (signal_received && *signal_received)
2503 break;
2509 * Process management event loop outside primary event loop
2511 static int
2512 man_standalone_event_loop (struct management *man, volatile int *signal_received, const time_t expire)
2514 int status;
2515 ASSERT (man_standalone_ok (man));
2516 status = man_block (man, signal_received, expire);
2517 if (status > 0)
2518 management_io (man);
2519 return status;
2522 #define MWCC_PASSWORD_WAIT (1<<0)
2523 #define MWCC_HOLD_WAIT (1<<1)
2526 * Block until client connects
2528 static void
2529 man_wait_for_client_connection (struct management *man,
2530 volatile int *signal_received,
2531 const time_t expire,
2532 unsigned int flags)
2534 ASSERT (man_standalone_ok (man));
2535 if (man->connection.state == MS_LISTEN)
2537 if (flags & MWCC_PASSWORD_WAIT)
2538 msg (D_MANAGEMENT, "Need password(s) from management interface, waiting...");
2539 if (flags & MWCC_HOLD_WAIT)
2540 msg (D_MANAGEMENT, "Need hold release from management interface, waiting...");
2541 do {
2542 man_standalone_event_loop (man, signal_received, expire);
2543 if (signal_received && *signal_received)
2544 break;
2545 } while (man->connection.state == MS_LISTEN || man_password_needed (man));
2550 * Process the management event loop for sec seconds
2552 void
2553 management_event_loop_n_seconds (struct management *man, int sec)
2555 if (man_standalone_ok (man))
2557 volatile int signal_received = 0;
2558 const bool standalone_disabled_save = man->persist.standalone_disabled;
2559 time_t expire;
2561 man->persist.standalone_disabled = false; /* This is so M_CLIENT messages will be correctly passed through msg() */
2563 /* set expire time */
2564 update_time ();
2565 expire = now + sec;
2567 /* if no client connection, wait for one */
2568 man_wait_for_client_connection (man, &signal_received, expire, 0);
2569 if (signal_received)
2570 return;
2572 /* run command processing event loop until we get our username/password */
2573 while (true)
2575 man_standalone_event_loop (man, &signal_received, expire);
2576 if (signal_received)
2577 return;
2580 /* revert state */
2581 man->persist.standalone_disabled = standalone_disabled_save;
2583 else
2585 sleep (sec);
2590 * Get a username/password from management channel in standalone mode.
2592 bool
2593 management_query_user_pass (struct management *man,
2594 struct user_pass *up,
2595 const char *type,
2596 const unsigned int flags)
2598 struct gc_arena gc = gc_new ();
2599 bool ret = false;
2601 if (man_standalone_ok (man))
2603 volatile int signal_received = 0;
2604 const bool standalone_disabled_save = man->persist.standalone_disabled;
2605 struct buffer alert_msg = alloc_buf_gc (128, &gc);
2606 const char *alert_type = NULL;
2607 const char *prefix = NULL;
2608 unsigned int up_query_mode = 0;
2610 ret = true;
2611 man->persist.standalone_disabled = false; /* This is so M_CLIENT messages will be correctly passed through msg() */
2612 man->persist.special_state_msg = NULL;
2614 CLEAR (man->connection.up_query);
2616 if (flags & GET_USER_PASS_NEED_OK)
2618 up_query_mode = UP_QUERY_NEED_OK;
2619 prefix= "NEED-OK";
2620 alert_type = "confirmation";
2622 else if (flags & GET_USER_PASS_NEED_STR)
2624 up_query_mode = UP_QUERY_NEED_STR;
2625 prefix= "NEED-STR";
2626 alert_type = "string";
2628 else if (flags & GET_USER_PASS_PASSWORD_ONLY)
2630 up_query_mode = UP_QUERY_PASS;
2631 prefix = "PASSWORD";
2632 alert_type = "password";
2634 else
2636 up_query_mode = UP_QUERY_USER_PASS;
2637 prefix = "PASSWORD";
2638 alert_type = "username/password";
2640 buf_printf (&alert_msg, ">%s:Need '%s' %s",
2641 prefix,
2642 type,
2643 alert_type);
2645 if (flags & (GET_USER_PASS_NEED_OK | GET_USER_PASS_NEED_STR))
2646 buf_printf (&alert_msg, " MSG:%s", up->username);
2648 man_wait_for_client_connection (man, &signal_received, 0, MWCC_PASSWORD_WAIT);
2649 if (signal_received)
2650 ret = false;
2652 if (ret)
2654 man->persist.special_state_msg = BSTR (&alert_msg);
2655 msg (M_CLIENT, "%s", man->persist.special_state_msg);
2657 /* tell command line parser which info we need */
2658 man->connection.up_query_mode = up_query_mode;
2659 man->connection.up_query_type = type;
2661 /* run command processing event loop until we get our username/password */
2664 man_standalone_event_loop (man, &signal_received, 0);
2665 if (signal_received)
2667 ret = false;
2668 break;
2670 } while (!man->connection.up_query.defined);
2673 /* revert state */
2674 man->connection.up_query_mode = UP_QUERY_DISABLED;
2675 man->connection.up_query_type = NULL;
2676 man->persist.standalone_disabled = standalone_disabled_save;
2677 man->persist.special_state_msg = NULL;
2679 /* pass through blank passwords */
2680 if (!strcmp (man->connection.up_query.password, blank_up))
2681 CLEAR (man->connection.up_query.password);
2684 * Transfer u/p to return object, zero any record
2685 * we hold in the management object.
2687 if (ret)
2689 man->connection.up_query.nocache = up->nocache; /* preserve caller's nocache setting */
2690 *up = man->connection.up_query;
2692 CLEAR (man->connection.up_query);
2695 gc_free (&gc);
2696 return ret;
2700 * Return true if management_hold() would block
2702 bool
2703 management_would_hold (struct management *man)
2705 return (man->settings.flags & MF_HOLD) && !man->persist.hold_release && man_standalone_ok (man);
2709 * Return true if (from the management interface's perspective) OpenVPN should
2710 * daemonize.
2712 bool
2713 management_should_daemonize (struct management *man)
2715 return management_would_hold (man) || (man->settings.flags & MF_QUERY_PASSWORDS);
2719 * If the hold flag is enabled, hibernate until a management client releases the hold.
2720 * Return true if the caller should not sleep for an additional time interval.
2722 bool
2723 management_hold (struct management *man)
2725 if (management_would_hold (man))
2727 volatile int signal_received = 0;
2728 const bool standalone_disabled_save = man->persist.standalone_disabled;
2730 man->persist.standalone_disabled = false; /* This is so M_CLIENT messages will be correctly passed through msg() */
2731 man->persist.special_state_msg = NULL;
2732 man->settings.mansig |= MANSIG_IGNORE_USR1_HUP;
2734 man_wait_for_client_connection (man, &signal_received, 0, MWCC_HOLD_WAIT);
2736 if (!signal_received)
2738 man->persist.special_state_msg = ">HOLD:Waiting for hold release";
2739 msg (M_CLIENT, "%s", man->persist.special_state_msg);
2741 /* run command processing event loop until we get our username/password */
2744 man_standalone_event_loop (man, &signal_received, 0);
2745 if (signal_received)
2746 break;
2747 } while (!man->persist.hold_release);
2750 /* revert state */
2751 man->persist.standalone_disabled = standalone_disabled_save;
2752 man->persist.special_state_msg = NULL;
2753 man->settings.mansig &= ~MANSIG_IGNORE_USR1_HUP;
2755 return true;
2757 return false;
2761 * struct command_line
2764 struct command_line *
2765 command_line_new (const int buf_len)
2767 struct command_line *cl;
2768 ALLOC_OBJ_CLEAR (cl, struct command_line);
2769 cl->buf = alloc_buf (buf_len);
2770 cl->residual = alloc_buf (buf_len);
2771 return cl;
2774 void
2775 command_line_reset (struct command_line *cl)
2777 buf_clear (&cl->buf);
2778 buf_clear (&cl->residual);
2781 void
2782 command_line_free (struct command_line *cl)
2784 command_line_reset (cl);
2785 free_buf (&cl->buf);
2786 free_buf (&cl->residual);
2787 free (cl);
2790 void
2791 command_line_add (struct command_line *cl, const unsigned char *buf, const int len)
2793 int i;
2794 for (i = 0; i < len; ++i)
2796 if (buf[i] && (isprint(buf[i]) || buf[i] == '\n'))
2798 if (!buf_write_u8 (&cl->buf, buf[i]))
2799 buf_clear (&cl->buf);
2804 const unsigned char *
2805 command_line_get (struct command_line *cl)
2807 int i;
2808 const unsigned char *ret = NULL;
2810 i = buf_substring_len (&cl->buf, '\n');
2811 if (i >= 0)
2813 buf_copy_excess (&cl->residual, &cl->buf, i);
2814 buf_chomp (&cl->buf);
2815 ret = (const unsigned char *) BSTR (&cl->buf);
2817 return ret;
2820 void
2821 command_line_next (struct command_line *cl)
2823 buf_clear (&cl->buf);
2824 buf_copy (&cl->buf, &cl->residual);
2825 buf_clear (&cl->residual);
2829 * struct log_entry
2832 const char *
2833 log_entry_print (const struct log_entry *e, unsigned int flags, struct gc_arena *gc)
2835 struct buffer out = alloc_buf_gc (ERR_BUF_SIZE, gc);
2836 if (flags & LOG_FATAL_NOTIFY)
2837 buf_printf (&out, ">FATAL:");
2838 if (flags & LOG_PRINT_LOG_PREFIX)
2839 buf_printf (&out, ">LOG:");
2840 if (flags & LOG_PRINT_ECHO_PREFIX)
2841 buf_printf (&out, ">ECHO:");
2842 if (flags & LOG_PRINT_STATE_PREFIX)
2843 buf_printf (&out, ">STATE:");
2844 if (flags & LOG_PRINT_INT_DATE)
2845 buf_printf (&out, "%u,", (unsigned int)e->timestamp);
2846 if (flags & LOG_PRINT_MSG_FLAGS)
2847 buf_printf (&out, "%s,", msg_flags_string (e->u.msg_flags, gc));
2848 if (flags & LOG_PRINT_STATE)
2849 buf_printf (&out, "%s,", man_state_name (e->u.state));
2850 if (flags & LOG_PRINT_INTVAL)
2851 buf_printf (&out, "%d,", e->u.intval);
2852 if (e->string)
2853 buf_printf (&out, "%s", e->string);
2854 if (flags & LOG_PRINT_LOCAL_IP)
2855 buf_printf (&out, ",%s", print_in_addr_t (e->local_ip, IA_EMPTY_IF_UNDEF, gc));
2856 if (flags & LOG_PRINT_REMOTE_IP)
2857 buf_printf (&out, ",%s", print_in_addr_t (e->remote_ip, IA_EMPTY_IF_UNDEF, gc));
2858 if (flags & LOG_ECHO_TO_LOG)
2859 msg (D_MANAGEMENT, "MANAGEMENT: %s", BSTR (&out));
2860 if (flags & LOG_PRINT_CRLF)
2861 buf_printf (&out, "\r\n");
2862 return BSTR (&out);
2865 static void
2866 log_entry_free_contents (struct log_entry *e)
2868 if (e->string)
2869 free ((char *)e->string);
2870 CLEAR (*e);
2874 * struct log_history
2877 static inline int
2878 log_index (const struct log_history *h, int i)
2880 return modulo_add (h->base, i, h->capacity);
2883 static void
2884 log_history_obj_init (struct log_history *h, int capacity)
2886 CLEAR (*h);
2887 h->capacity = capacity;
2888 ALLOC_ARRAY_CLEAR (h->array, struct log_entry, capacity);
2891 struct log_history *
2892 log_history_init (const int capacity)
2894 struct log_history *h;
2895 ASSERT (capacity > 0);
2896 ALLOC_OBJ (h, struct log_history);
2897 log_history_obj_init (h, capacity);
2898 return h;
2901 static void
2902 log_history_free_contents (struct log_history *h)
2904 int i;
2905 for (i = 0; i < h->size; ++i)
2906 log_entry_free_contents (&h->array[log_index(h, i)]);
2907 free (h->array);
2910 void
2911 log_history_close (struct log_history *h)
2913 log_history_free_contents (h);
2914 free (h);
2917 void
2918 log_history_add (struct log_history *h, const struct log_entry *le)
2920 struct log_entry *e;
2921 ASSERT (h->size >= 0 && h->size <= h->capacity);
2922 if (h->size == h->capacity)
2924 e = &h->array[h->base];
2925 log_entry_free_contents (e);
2926 h->base = log_index (h, 1);
2928 else
2930 e = &h->array[log_index(h, h->size)];
2931 ++h->size;
2934 *e = *le;
2935 e->string = string_alloc (le->string, NULL);
2938 void
2939 log_history_resize (struct log_history *h, const int capacity)
2941 if (capacity != h->capacity)
2943 struct log_history newlog;
2944 int i;
2946 ASSERT (capacity > 0);
2947 log_history_obj_init (&newlog, capacity);
2949 for (i = 0; i < h->size; ++i)
2950 log_history_add (&newlog, &h->array[log_index(h, i)]);
2952 log_history_free_contents (h);
2953 *h = newlog;
2957 const struct log_entry *
2958 log_history_ref (const struct log_history *h, const int index)
2960 if (index >= 0 && index < h->size)
2961 return &h->array[log_index(h, (h->size - 1) - index)];
2962 else
2963 return NULL;
2966 #else
2967 static void dummy(void) {}
2968 #endif /* ENABLE_MANAGEMENT */