forked from fries/android-external-openvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.c
2968 lines (2690 loc) · 71.8 KB
/
manage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* OpenVPN -- An application to securely tunnel IP networks
* over a single TCP/UDP port, with support for SSL/TLS-based
* session authentication and key exchange,
* packet encryption, packet authentication, and
* packet compression.
*
* Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "syshead.h"
#ifdef ENABLE_MANAGEMENT
#include "error.h"
#include "fdmisc.h"
#include "options.h"
#include "sig.h"
#include "event.h"
#include "otime.h"
#include "integer.h"
#include "misc.h"
#include "ssl.h"
#include "common.h"
#include "manage.h"
#include "memdbg.h"
#ifdef ENABLE_PKCS11
#include "pkcs11.h"
#endif
#define MANAGEMENT_ECHO_PULL_INFO 0
#if MANAGEMENT_ECHO_PULL_INFO
#define MANAGEMENT_ECHO_FLAGS LOG_PRINT_INTVAL
#else
#define MANAGEMENT_ECHO_FLAGS 0
#endif
/* tag for blank username/password */
static const char blank_up[] = "[[BLANK]]";
struct management *management; /* GLOBAL */
/* static forward declarations */
static void man_output_standalone (struct management *man, volatile int *signal_received);
static void man_reset_client_socket (struct management *man, const bool exiting);
static void
man_help ()
{
msg (M_CLIENT, "Management Interface for %s", title_string);
msg (M_CLIENT, "Commands:");
msg (M_CLIENT, "auth-retry t : Auth failure retry mode (none,interact,nointeract).");
msg (M_CLIENT, "bytecount n : Show bytes in/out, update every n secs (0=off).");
msg (M_CLIENT, "echo [on|off] [N|all] : Like log, but only show messages in echo buffer.");
msg (M_CLIENT, "exit|quit : Close management session.");
msg (M_CLIENT, "forget-passwords : Forget passwords entered so far.");
msg (M_CLIENT, "help : Print this message.");
msg (M_CLIENT, "hold [on|off|release] : Set/show hold flag to on/off state, or");
msg (M_CLIENT, " release current hold and start tunnel.");
msg (M_CLIENT, "kill cn : Kill the client instance(s) having common name cn.");
msg (M_CLIENT, "kill IP:port : Kill the client instance connecting from IP:port.");
msg (M_CLIENT, "load-stats : Show global server load stats.");
msg (M_CLIENT, "log [on|off] [N|all] : Turn on/off realtime log display");
msg (M_CLIENT, " + show last N lines or 'all' for entire history.");
msg (M_CLIENT, "mute [n] : Set log mute level to n, or show level if n is absent.");
msg (M_CLIENT, "needok type action : Enter confirmation for NEED-OK request of 'type',");
msg (M_CLIENT, " where action = 'ok' or 'cancel'.");
msg (M_CLIENT, "needstr type action : Enter confirmation for NEED-STR request of 'type',");
msg (M_CLIENT, " where action is reply string.");
msg (M_CLIENT, "net : (Windows only) Show network info and routing table.");
msg (M_CLIENT, "password type p : Enter password p for a queried OpenVPN password.");
msg (M_CLIENT, "pid : Show process ID of the current OpenVPN process.");
#ifdef ENABLE_PKCS11
msg (M_CLIENT, "pkcs11-id-count : Get number of available PKCS#11 identities.");
msg (M_CLIENT, "pkcs11-id-get index : Get PKCS#11 identity at index.");
#endif
#ifdef MANAGEMENT_DEF_AUTH
msg (M_CLIENT, "client-auth CID KID : Authenticate client-id/key-id CID/KID (MULTILINE)");
msg (M_CLIENT, "client-auth-nt CID KID : Authenticate client-id/key-id CID/KID");
msg (M_CLIENT, "client-deny CID KID R [CR] : Deny auth client-id/key-id CID/KID with log reason");
msg (M_CLIENT, " text R and optional client reason text CR");
msg (M_CLIENT, "client-kill CID : Kill client instance CID");
#ifdef MANAGEMENT_PF
msg (M_CLIENT, "client-pf CID : Define packet filter for client CID (MULTILINE)");
#endif
#endif
msg (M_CLIENT, "signal s : Send signal s to daemon,");
msg (M_CLIENT, " s = SIGHUP|SIGTERM|SIGUSR1|SIGUSR2.");
msg (M_CLIENT, "state [on|off] [N|all] : Like log, but show state history.");
msg (M_CLIENT, "status [n] : Show current daemon status info using format #n.");
msg (M_CLIENT, "test n : Produce n lines of output for testing/debugging.");
msg (M_CLIENT, "username type u : Enter username u for a queried OpenVPN username.");
msg (M_CLIENT, "verb [n] : Set log verbosity level to n, or show if n is absent.");
msg (M_CLIENT, "version : Show current version number.");
msg (M_CLIENT, "END");
}
static const char *
man_state_name (const int state)
{
switch (state)
{
case OPENVPN_STATE_INITIAL:
return "INITIAL";
case OPENVPN_STATE_CONNECTING:
return "CONNECTING";
case OPENVPN_STATE_WAIT:
return "WAIT";
case OPENVPN_STATE_AUTH:
return "AUTH";
case OPENVPN_STATE_GET_CONFIG:
return "GET_CONFIG";
case OPENVPN_STATE_ASSIGN_IP:
return "ASSIGN_IP";
case OPENVPN_STATE_ADD_ROUTES:
return "ADD_ROUTES";
case OPENVPN_STATE_CONNECTED:
return "CONNECTED";
case OPENVPN_STATE_RECONNECTING:
return "RECONNECTING";
case OPENVPN_STATE_EXITING:
return "EXITING";
case OPENVPN_STATE_RESOLVE:
return "RESOLVE";
case OPENVPN_STATE_TCP_CONNECT:
return "TCP_CONNECT";
default:
return "?";
}
}
static void
man_welcome (struct management *man)
{
msg (M_CLIENT, ">INFO:OpenVPN Management Interface Version %d -- type 'help' for more info",
MANAGEMENT_VERSION);
if (man->persist.special_state_msg)
msg (M_CLIENT, "%s", man->persist.special_state_msg);
}
static inline bool
man_password_needed (struct management *man)
{
return man->settings.up.defined && !man->connection.password_verified;
}
static void
man_check_password (struct management *man, const char *line)
{
if (man_password_needed (man))
{
if (streq (line, man->settings.up.password))
{
man->connection.password_verified = true;
msg (M_CLIENT, "SUCCESS: password is correct");
man_welcome (man);
}
else
{
man->connection.password_verified = false;
msg (M_CLIENT, "ERROR: bad password");
if (++man->connection.password_tries >= MANAGEMENT_N_PASSWORD_RETRIES)
{
msg (M_WARN, "MAN: client connection rejected after %d failed password attempts",
MANAGEMENT_N_PASSWORD_RETRIES);
man->connection.halt = true;
}
}
}
}
static void
man_update_io_state (struct management *man)
{
if (socket_defined (man->connection.sd_cli))
{
if (buffer_list_defined (man->connection.out))
{
man->connection.state = MS_CC_WAIT_WRITE;
}
else
{
man->connection.state = MS_CC_WAIT_READ;
}
}
}
static void
man_output_list_push (struct management *man, const char *str)
{
if (management_connected (man))
{
if (str)
buffer_list_push (man->connection.out, (const unsigned char *) str);
man_update_io_state (man);
if (!man->persist.standalone_disabled)
{
volatile int signal_received = 0;
man_output_standalone (man, &signal_received);
}
}
}
static void
man_prompt (struct management *man)
{
if (man_password_needed (man))
man_output_list_push (man, "ENTER PASSWORD:");
#if 0 /* should we use prompt? */
else
man_output_list_push (man, PACKAGE_NAME ">");
#endif
}
static void
man_delete_unix_socket (struct management *man)
{
#if UNIX_SOCK_SUPPORT
if ((man->settings.flags & (MF_UNIX_SOCK|MF_CONNECT_AS_CLIENT)) == MF_UNIX_SOCK)
socket_delete_unix (&man->settings.local_unix);
#endif
}
static void
man_close_socket (struct management *man, const socket_descriptor_t sd)
{
#ifndef WIN32
/*
* Windows doesn't need this because the ne32 event is permanently
* enabled at struct management scope.
*/
if (man->persist.callback.delete_event)
(*man->persist.callback.delete_event) (man->persist.callback.arg, sd);
#endif
openvpn_close_socket (sd);
}
static void
virtual_output_callback_func (void *arg, const unsigned int flags, const char *str)
{
static int recursive_level = 0; /* GLOBAL */
if (!recursive_level) /* don't allow recursion */
{
struct gc_arena gc = gc_new ();
struct management *man = (struct management *) arg;
struct log_entry e;
const char *out = NULL;
++recursive_level;
CLEAR (e);
update_time ();
e.timestamp = now;
e.u.msg_flags = flags;
e.string = str;
if (flags & M_FATAL)
man->persist.standalone_disabled = false;
if (flags != M_CLIENT)
log_history_add (man->persist.log, &e);
if (!man_password_needed (man))
{
if (flags == M_CLIENT)
out = log_entry_print (&e, LOG_PRINT_CRLF, &gc);
else if (man->connection.log_realtime)
out = log_entry_print (&e, LOG_PRINT_INT_DATE
| LOG_PRINT_MSG_FLAGS
| LOG_PRINT_LOG_PREFIX
| LOG_PRINT_CRLF, &gc);
if (out)
man_output_list_push (man, out);
if (flags & M_FATAL)
{
out = log_entry_print (&e, LOG_FATAL_NOTIFY|LOG_PRINT_CRLF, &gc);
if (out)
{
man_output_list_push (man, out);
man_reset_client_socket (man, true);
}
}
}
--recursive_level;
gc_free (&gc);
}
}
/*
* Given a signal, return the signal with possible remapping applied,
* or -1 if the signal should be ignored.
*/
static int
man_mod_signal (const struct management *man, const int signum)
{
const unsigned int flags = man->settings.mansig;
int s = signum;
if (s == SIGUSR1)
{
if (flags & MANSIG_MAP_USR1_TO_HUP)
s = SIGHUP;
if (flags & MANSIG_MAP_USR1_TO_TERM)
s = SIGTERM;
}
if (flags & MANSIG_IGNORE_USR1_HUP)
{
if (s == SIGHUP || s == SIGUSR1)
s = -1;
}
return s;
}
static void
man_signal (struct management *man, const char *name)
{
const int sig = parse_signal (name);
if (sig >= 0)
{
const int sig_mod = man_mod_signal (man, sig);
if (sig_mod >= 0)
{
throw_signal (sig_mod);
msg (M_CLIENT, "SUCCESS: signal %s thrown", signal_name (sig_mod, true));
}
else
{
if (man->persist.special_state_msg)
msg (M_CLIENT, "%s", man->persist.special_state_msg);
else
msg (M_CLIENT, "ERROR: signal '%s' is currently ignored", name);
}
}
else
{
msg (M_CLIENT, "ERROR: signal '%s' is not a known signal type", name);
}
}
static void
man_status (struct management *man, const int version, struct status_output *so)
{
if (man->persist.callback.status)
{
(*man->persist.callback.status) (man->persist.callback.arg, version, so);
}
else
{
msg (M_CLIENT, "ERROR: The 'status' command is not supported by the current daemon mode");
}
}
static void
man_bytecount (struct management *man, const int update_seconds)
{
if (update_seconds >= 0)
man->connection.bytecount_update_seconds = update_seconds;
else
man->connection.bytecount_update_seconds = 0;
msg (M_CLIENT, "SUCCESS: bytecount interval changed");
}
void
man_bytecount_output_client (struct management *man)
{
char in[32];
char out[32];
/* do in a roundabout way to work around possible mingw or mingw-glibc bug */
openvpn_snprintf (in, sizeof (in), counter_format, man->persist.bytes_in);
openvpn_snprintf (out, sizeof (out), counter_format, man->persist.bytes_out);
msg (M_CLIENT, ">BYTECOUNT:%s,%s", in, out);
man->connection.bytecount_last_update = now;
}
#ifdef MANAGEMENT_DEF_AUTH
void
man_bytecount_output_server (struct management *man,
const counter_type *bytes_in_total,
const counter_type *bytes_out_total,
struct man_def_auth_context *mdac)
{
char in[32];
char out[32];
/* do in a roundabout way to work around possible mingw or mingw-glibc bug */
openvpn_snprintf (in, sizeof (in), counter_format, *bytes_in_total);
openvpn_snprintf (out, sizeof (out), counter_format, *bytes_out_total);
msg (M_CLIENT, ">BYTECOUNT_CLI:%lu,%s,%s", mdac->cid, in, out);
mdac->bytecount_last_update = now;
}
#endif
static void
man_kill (struct management *man, const char *victim)
{
struct gc_arena gc = gc_new ();
if (man->persist.callback.kill_by_cn && man->persist.callback.kill_by_addr)
{
struct buffer buf;
char p1[128];
char p2[128];
int n_killed;
buf_set_read (&buf, (uint8_t*) victim, strlen (victim) + 1);
buf_parse (&buf, ':', p1, sizeof (p1));
buf_parse (&buf, ':', p2, sizeof (p2));
if (strlen (p1) && strlen (p2))
{
/* IP:port specified */
bool status;
const in_addr_t addr = getaddr (GETADDR_HOST_ORDER|GETADDR_MSG_VIRT_OUT, p1, 0, &status, NULL);
if (status)
{
const int port = atoi (p2);
if (port > 0 && port < 65536)
{
n_killed = (*man->persist.callback.kill_by_addr) (man->persist.callback.arg, addr, port);
if (n_killed > 0)
{
msg (M_CLIENT, "SUCCESS: %d client(s) at address %s:%d killed",
n_killed,
print_in_addr_t (addr, 0, &gc),
port);
}
else
{
msg (M_CLIENT, "ERROR: client at address %s:%d not found",
print_in_addr_t (addr, 0, &gc),
port);
}
}
else
{
msg (M_CLIENT, "ERROR: port number is out of range: %s", p2);
}
}
else
{
msg (M_CLIENT, "ERROR: error parsing IP address: %s", p1);
}
}
else if (strlen (p1))
{
/* common name specified */
n_killed = (*man->persist.callback.kill_by_cn) (man->persist.callback.arg, p1);
if (n_killed > 0)
{
msg (M_CLIENT, "SUCCESS: common name '%s' found, %d client(s) killed", p1, n_killed);
}
else
{
msg (M_CLIENT, "ERROR: common name '%s' not found", p1);
}
}
else
{
msg (M_CLIENT, "ERROR: kill parse");
}
}
else
{
msg (M_CLIENT, "ERROR: The 'kill' command is not supported by the current daemon mode");
}
gc_free (&gc);
}
/*
* General-purpose history command handler
* for the log and echo commands.
*/
static void
man_history (struct management *man,
const char *parm,
const char *type,
struct log_history *log,
bool *realtime,
const unsigned int lep_flags)
{
struct gc_arena gc = gc_new ();
int n = 0;
if (streq (parm, "on"))
{
*realtime = true;
msg (M_CLIENT, "SUCCESS: real-time %s notification set to ON", type);
}
else if (streq (parm, "off"))
{
*realtime = false;
msg (M_CLIENT, "SUCCESS: real-time %s notification set to OFF", type);
}
else if (streq (parm, "all") || (n = atoi (parm)) > 0)
{
const int size = log_history_size (log);
const int start = (n ? n : size) - 1;
int i;
for (i = start; i >= 0; --i)
{
const struct log_entry *e = log_history_ref (log, i);
if (e)
{
const char *out = log_entry_print (e, lep_flags, &gc);
virtual_output_callback_func (man, M_CLIENT, out);
}
}
msg (M_CLIENT, "END");
}
else
{
msg (M_CLIENT, "ERROR: %s parameter must be 'on' or 'off' or some number n or 'all'", type);
}
gc_free (&gc);
}
static void
man_log (struct management *man, const char *parm)
{
man_history (man,
parm,
"log",
man->persist.log,
&man->connection.log_realtime,
LOG_PRINT_INT_DATE|LOG_PRINT_MSG_FLAGS);
}
static void
man_echo (struct management *man, const char *parm)
{
man_history (man,
parm,
"echo",
man->persist.echo,
&man->connection.echo_realtime,
LOG_PRINT_INT_DATE|MANAGEMENT_ECHO_FLAGS);
}
static void
man_state (struct management *man, const char *parm)
{
man_history (man,
parm,
"state",
man->persist.state,
&man->connection.state_realtime,
LOG_PRINT_INT_DATE|LOG_PRINT_STATE|
LOG_PRINT_LOCAL_IP|LOG_PRINT_REMOTE_IP);
}
static void
man_up_finalize (struct management *man)
{
switch (man->connection.up_query_mode)
{
case UP_QUERY_DISABLED:
man->connection.up_query.defined = false;
break;
case UP_QUERY_USER_PASS:
if (strlen (man->connection.up_query.username) && strlen (man->connection.up_query.password))
man->connection.up_query.defined = true;
break;
case UP_QUERY_PASS:
if (strlen (man->connection.up_query.password))
man->connection.up_query.defined = true;
break;
case UP_QUERY_NEED_OK:
if (strlen (man->connection.up_query.password))
man->connection.up_query.defined = true;
break;
case UP_QUERY_NEED_STR:
if (strlen (man->connection.up_query.password))
man->connection.up_query.defined = true;
break;
default:
ASSERT (0);
}
}
static void
man_query_user_pass (struct management *man,
const char *type,
const char *string,
const bool needed,
const char *prompt,
char *dest,
int len)
{
if (needed)
{
ASSERT (man->connection.up_query_type);
if (streq (man->connection.up_query_type, type))
{
strncpynt (dest, string, len);
man_up_finalize (man);
msg (M_CLIENT, "SUCCESS: '%s' %s entered, but not yet verified",
type,
prompt);
}
else
msg (M_CLIENT, "ERROR: %s of type '%s' entered, but we need one of type '%s'",
prompt,
type,
man->connection.up_query_type);
}
else
{
msg (M_CLIENT, "ERROR: no %s is currently needed at this time", prompt);
}
}
static void
man_query_username (struct management *man, const char *type, const char *string)
{
const bool needed = (man->connection.up_query_mode == UP_QUERY_USER_PASS && man->connection.up_query_type);
man_query_user_pass (man, type, string, needed, "username", man->connection.up_query.username, USER_PASS_LEN);
}
static void
man_query_password (struct management *man, const char *type, const char *string)
{
const bool needed = ((man->connection.up_query_mode == UP_QUERY_USER_PASS
|| man->connection.up_query_mode == UP_QUERY_PASS)
&& man->connection.up_query_type);
if (!string[0]) /* allow blank passwords to be passed through using the blank_up tag */
string = blank_up;
man_query_user_pass (man, type, string, needed, "password", man->connection.up_query.password, USER_PASS_LEN);
}
static void
man_query_need_ok (struct management *man, const char *type, const char *action)
{
const bool needed = ((man->connection.up_query_mode == UP_QUERY_NEED_OK) && man->connection.up_query_type);
man_query_user_pass (man, type, action, needed, "needok-confirmation", man->connection.up_query.password, USER_PASS_LEN);
}
static void
man_query_need_str (struct management *man, const char *type, const char *action)
{
const bool needed = ((man->connection.up_query_mode == UP_QUERY_NEED_STR) && man->connection.up_query_type);
man_query_user_pass (man, type, action, needed, "needstr-string", man->connection.up_query.password, USER_PASS_LEN);
}
static void
man_forget_passwords (struct management *man)
{
#if defined(USE_CRYPTO) && defined(USE_SSL)
ssl_purge_auth ();
msg (M_CLIENT, "SUCCESS: Passwords were forgotten");
#endif
}
static void
man_net (struct management *man)
{
if (man->persist.callback.show_net)
{
(*man->persist.callback.show_net) (man->persist.callback.arg, M_CLIENT);
}
else
{
msg (M_CLIENT, "ERROR: The 'net' command is not supported by the current daemon mode");
}
}
#ifdef ENABLE_PKCS11
static void
man_pkcs11_id_count (struct management *man)
{
msg (M_CLIENT, ">PKCS11ID-COUNT:%d", pkcs11_management_id_count ());
}
static void
man_pkcs11_id_get (struct management *man, const int index)
{
char *id = NULL;
char *base64 = NULL;
if (pkcs11_management_id_get (index, &id, &base64))
msg (M_CLIENT, ">PKCS11ID-ENTRY:'%d', ID:'%s', BLOB:'%s'", index, id, base64);
else
msg (M_CLIENT, ">PKCS11ID-ENTRY:'%d'", index);
if (id != NULL)
free (id);
if (base64 != NULL)
free (base64);
}
#endif
static void
man_hold (struct management *man, const char *cmd)
{
if (cmd)
{
if (streq (cmd, "on"))
{
man->settings.flags |= MF_HOLD;
msg (M_CLIENT, "SUCCESS: hold flag set to ON");
}
else if (streq (cmd, "off"))
{
man->settings.flags &= ~MF_HOLD;
msg (M_CLIENT, "SUCCESS: hold flag set to OFF");
}
else if (streq (cmd, "release"))
{
man->persist.hold_release = true;
msg (M_CLIENT, "SUCCESS: hold release succeeded");
}
else
{
msg (M_CLIENT, "ERROR: bad hold command parameter");
}
}
else
msg (M_CLIENT, "SUCCESS: hold=%d", BOOL_CAST(man->settings.flags & MF_HOLD));
}
#ifdef MANAGEMENT_DEF_AUTH
static bool
parse_cid (const char *str, unsigned long *cid)
{
if (sscanf (str, "%lu", cid) == 1)
return true;
else
{
msg (M_CLIENT, "ERROR: cannot parse CID");
return false;
}
}
static bool
parse_kid (const char *str, unsigned int *kid)
{
if (sscanf (str, "%u", kid) == 1)
return true;
else
{
msg (M_CLIENT, "ERROR: cannot parse KID");
return false;
}
}
static void
in_extra_reset (struct man_connection *mc, const bool new)
{
if (mc)
{
if (!new)
{
mc->in_extra_cmd = IEC_UNDEF;
mc->in_extra_cid = 0;
mc->in_extra_kid = 0;
}
if (mc->in_extra)
{
buffer_list_free (mc->in_extra);
mc->in_extra = NULL;
}
if (new)
mc->in_extra = buffer_list_new (0);
}
}
static void
in_extra_dispatch (struct management *man)
{
switch (man->connection.in_extra_cmd)
{
case IEC_CLIENT_AUTH:
if (man->persist.callback.client_auth)
{
const bool status = (*man->persist.callback.client_auth)
(man->persist.callback.arg,
man->connection.in_extra_cid,
man->connection.in_extra_kid,
true,
NULL,
NULL,
man->connection.in_extra);
man->connection.in_extra = NULL;
if (status)
{
msg (M_CLIENT, "SUCCESS: client-auth command succeeded");
}
else
{
msg (M_CLIENT, "ERROR: client-auth command failed");
}
}
else
{
msg (M_CLIENT, "ERROR: The client-auth command is not supported by the current daemon mode");
}
break;
#ifdef MANAGEMENT_PF
case IEC_CLIENT_PF:
if (man->persist.callback.client_pf)
{
const bool status = (*man->persist.callback.client_pf)
(man->persist.callback.arg,
man->connection.in_extra_cid,
man->connection.in_extra);
man->connection.in_extra = NULL;
if (status)
{
msg (M_CLIENT, "SUCCESS: client-pf command succeeded");
}
else
{
msg (M_CLIENT, "ERROR: client-pf command failed");
}
}
else
{
msg (M_CLIENT, "ERROR: The client-pf command is not supported by the current daemon mode");
}
break;
#endif
}
in_extra_reset (&man->connection, false);
}
static void
man_client_auth (struct management *man, const char *cid_str, const char *kid_str, const bool extra)
{
struct man_connection *mc = &man->connection;
mc->in_extra_cid = 0;
mc->in_extra_kid = 0;
if (parse_cid (cid_str, &mc->in_extra_cid)
&& parse_kid (kid_str, &mc->in_extra_kid))
{
mc->in_extra_cmd = IEC_CLIENT_AUTH;
in_extra_reset (mc, true);
if (!extra)
in_extra_dispatch (man);
}
}
static void
man_client_deny (struct management *man, const char *cid_str, const char *kid_str, const char *reason, const char *client_reason)
{
unsigned long cid = 0;
unsigned int kid = 0;
if (parse_cid (cid_str, &cid) && parse_kid (kid_str, &kid))
{
if (man->persist.callback.client_auth)
{
const bool status = (*man->persist.callback.client_auth)
(man->persist.callback.arg,
cid,
kid,
false,
reason,
client_reason,
NULL);
if (status)
{
msg (M_CLIENT, "SUCCESS: client-deny command succeeded");
}
else
{
msg (M_CLIENT, "ERROR: client-deny command failed");
}
}
else
{
msg (M_CLIENT, "ERROR: The client-deny command is not supported by the current daemon mode");
}
}
}
static void
man_client_kill (struct management *man, const char *cid_str)
{
unsigned long cid = 0;
if (parse_cid (cid_str, &cid))
{
if (man->persist.callback.kill_by_cid)
{
const bool status = (*man->persist.callback.kill_by_cid) (man->persist.callback.arg, cid);
if (status)
{
msg (M_CLIENT, "SUCCESS: client-kill command succeeded");
}
else
{
msg (M_CLIENT, "ERROR: client-kill command failed");
}
}
else
{
msg (M_CLIENT, "ERROR: The client-kill command is not supported by the current daemon mode");
}
}
}
static void
man_client_n_clients (struct management *man)
{
if (man->persist.callback.n_clients)
{
const int nclients = (*man->persist.callback.n_clients) (man->persist.callback.arg);
msg (M_CLIENT, "SUCCESS: nclients=%d", nclients);
}
else
{
msg (M_CLIENT, "ERROR: The nclients command is not supported by the current daemon mode");
}
}
#ifdef MANAGEMENT_PF
static void
man_client_pf (struct management *man, const char *cid_str)
{
struct man_connection *mc = &man->connection;
mc->in_extra_cid = 0;
mc->in_extra_kid = 0;
if (parse_cid (cid_str, &mc->in_extra_cid))
{
mc->in_extra_cmd = IEC_CLIENT_PF;
in_extra_reset (mc, true);
}
}
#endif
#endif
static void
man_load_stats (struct management *man)
{
extern counter_type link_read_bytes_global;
extern counter_type link_write_bytes_global;
int nclients = 0;
if (man->persist.callback.n_clients)
nclients = (*man->persist.callback.n_clients) (man->persist.callback.arg);
msg (M_CLIENT, "SUCCESS: nclients=%d,bytesin=" counter_format ",bytesout=" counter_format,
nclients,
link_read_bytes_global,
link_write_bytes_global);
}
#define MN_AT_LEAST (1<<0)
static bool
man_need (struct management *man, const char **p, const int n, unsigned int flags)
{
int i;
ASSERT (p[0]);
for (i = 1; i <= n; ++i)
{
if (!p[i])
{
msg (M_CLIENT, "ERROR: the '%s' command requires %s%d parameter%s",
p[0],
(flags & MN_AT_LEAST) ? "at least " : "",
n,
n > 1 ? "s" : "");
return false;
}
}
return true;
}
static void
man_dispatch_command (struct management *man, struct status_output *so, const char **p, const int nparms)
{
struct gc_arena gc = gc_new ();
ASSERT (p[0]);
if (streq (p[0], "exit") || streq (p[0], "quit"))
{