-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2775 lines (2457 loc) · 73.7 KB
/
main.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
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2008-2015 Intel Corporation.
* Copyright © 2008 Nick Andrew <[email protected]>
* Copyright © 2013 John Morrissey <[email protected]>
*
* Author: David Woodhouse <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1, 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
* Lesser General Public License for more details.
*/
#include <config.h>
#ifdef HAVE_GETLINE
/* Various BSD systems require this for getline() to be visible */
#define _WITH_GETLINE
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <getopt.h>
#include <time.h>
#include <locale.h>
#ifdef LIBPROXY_HDR
#include LIBPROXY_HDR
#endif
#include "openconnect-internal.h"
#ifdef _WIN32
#include <shlwapi.h>
#include <wtypes.h>
#include <wincon.h>
#else
#include <sys/utsname.h>
#include <pwd.h>
#include <termios.h>
#endif
#ifdef HAVE_NL_LANGINFO
#include <langinfo.h>
static const char *legacy_charset;
#endif
static int write_new_config(void *_vpninfo,
const char *buf, int buflen);
static void __attribute__ ((format(printf, 3, 4)))
write_progress(void *_vpninfo, int level, const char *fmt, ...);
static int validate_peer_cert(void *_vpninfo, const char *reason);
static int process_auth_form_cb(void *_vpninfo,
struct oc_auth_form *form);
static void init_token(struct openconnect_info *vpninfo,
oc_token_mode_t token_mode, const char *token_str);
/* A sanity check that the openconnect executable is running against a
library of the same version */
#define openconnect_version_str openconnect_binary_version
#include <version.c>
#undef openconnect_version_str
static int verbose = PRG_INFO;
static int timestamp;
#ifndef _WIN32
static int background;
static int use_syslog = 0;
int wrote_pid = 0;
static char *pidfile = NULL;
#endif
static int do_passphrase_from_fsid;
static int non_inter;
static int cookieonly;
static int allow_stdin_read;
static char *token_filename;
static int allowed_fingerprints;
struct accepted_cert {
struct accepted_cert *next;
char *fingerprint;
char *host;
int port;
} *accepted_certs;
static char *username;
static char *password;
static char *authgroup;
static int authgroup_set;
static int last_form_empty;
static int sig_cmd_fd;
static void add_form_field(char *field);
#ifdef __ANDROID__
#include <android/log.h>
static void __attribute__ ((format(printf, 3, 4)))
syslog_progress(void *_vpninfo, int level, const char *fmt, ...)
{
static int l[4] = {
ANDROID_LOG_ERROR, /* PRG_ERR */
ANDROID_LOG_INFO, /* PRG_INFO */
ANDROID_LOG_DEBUG, /* PRG_DEBUG */
ANDROID_LOG_DEBUG /* PRG_TRACE */
};
va_list args, args2;
if (verbose >= level) {
va_start(args, fmt);
va_copy(args2, args);
__android_log_vprint(l[level], "openconnect", fmt, args);
/* Android wants it to stderr too, so the GUI can scrape
it and display it as well as going to syslog */
vfprintf(stderr, fmt, args2);
va_end(args);
va_end(args2);
}
}
#define openlog(...) /* */
#elif defined(_WIN32) || defined(__native_client__)
/*
* FIXME: Perhaps we could implement syslog_progress() using these APIs:
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa364148%28v=vs.85%29.aspx
*/
#else /* !__ANDROID__ && !_WIN32 && !__native_client__ */
#include <syslog.h>
static void __attribute__ ((format(printf, 3, 4)))
syslog_progress(void *_vpninfo, int level, const char *fmt, ...)
{
int priority = level ? LOG_INFO : LOG_NOTICE;
va_list args;
if (verbose >= level) {
va_start(args, fmt);
vsyslog(priority, fmt, args);
va_end(args);
}
}
#endif
enum {
OPT_AUTHENTICATE = 0x100,
OPT_AUTHGROUP,
OPT_BASEMTU,
OPT_CAFILE,
OPT_COMPRESSION,
OPT_CONFIGFILE,
OPT_COOKIEONLY,
OPT_COOKIE_ON_STDIN,
OPT_CSD_USER,
OPT_CSD_WRAPPER,
OPT_CIPHERSUITES,
OPT_DISABLE_IPV6,
OPT_DTLS_CIPHERS,
OPT_DTLS12_CIPHERS,
OPT_DUMP_HTTP,
OPT_FORCE_DPD,
OPT_FORCE_TROJAN,
OPT_GNUTLS_DEBUG,
OPT_JUNIPER,
OPT_KEY_PASSWORD_FROM_FSID,
OPT_LIBPROXY,
OPT_NO_CERT_CHECK,
OPT_NO_DTLS,
OPT_NO_HTTP_KEEPALIVE,
OPT_NO_SYSTEM_TRUST,
OPT_NO_PASSWD,
OPT_NO_PROXY,
OPT_NO_XMLPOST,
OPT_PIDFILE,
OPT_PASSWORD_ON_STDIN,
OPT_PRINTCOOKIE,
OPT_RECONNECT_TIMEOUT,
OPT_SERVERCERT,
OPT_RESOLVE,
OPT_USERAGENT,
OPT_NON_INTER,
OPT_DTLS_LOCAL_PORT,
OPT_TOKEN_MODE,
OPT_TOKEN_SECRET,
OPT_OS,
OPT_TIMESTAMP,
OPT_PFS,
OPT_ALLOW_INSECURE_CRYPTO,
OPT_PROXY_AUTH,
OPT_HTTP_AUTH,
OPT_LOCAL_HOSTNAME,
OPT_PROTOCOL,
OPT_PASSTOS,
OPT_VERSION,
};
#ifdef __sun__
/*
* The 'name' field in Solaris 'struct option' lacks the 'const', and causes
* lots of warnings unless we cast it... https://www.illumos.org/issues/1881
*/
#define OPTION(name, arg, abbrev) {(char *)name, arg, NULL, abbrev}
#else
#define OPTION(name, arg, abbrev) {name, arg, NULL, abbrev}
#endif
static const struct option long_options[] = {
#ifndef _WIN32
OPTION("background", 0, 'b'),
OPTION("pid-file", 1, OPT_PIDFILE),
OPTION("setuid", 1, 'U'),
OPTION("script-tun", 0, 'S'),
OPTION("syslog", 0, 'l'),
OPTION("csd-user", 1, OPT_CSD_USER),
OPTION("csd-wrapper", 1, OPT_CSD_WRAPPER),
#endif
OPTION("pfs", 0, OPT_PFS),
OPTION("allow-insecure-crypto", 0, OPT_ALLOW_INSECURE_CRYPTO),
OPTION("certificate", 1, 'c'),
OPTION("sslkey", 1, 'k'),
OPTION("cookie", 1, 'C'),
OPTION("compression", 1, OPT_COMPRESSION),
OPTION("deflate", 0, 'd'),
OPTION("juniper", 0, OPT_JUNIPER),
OPTION("no-deflate", 0, 'D'),
OPTION("cert-expire-warning", 1, 'e'),
OPTION("usergroup", 1, 'g'),
OPTION("help", 0, 'h'),
OPTION("http-auth", 1, OPT_HTTP_AUTH),
OPTION("interface", 1, 'i'),
OPTION("mtu", 1, 'm'),
OPTION("base-mtu", 1, OPT_BASEMTU),
OPTION("script", 1, 's'),
OPTION("timestamp", 0, OPT_TIMESTAMP),
OPTION("passtos", 0, OPT_PASSTOS),
OPTION("key-password", 1, 'p'),
OPTION("proxy", 1, 'P'),
OPTION("proxy-auth", 1, OPT_PROXY_AUTH),
OPTION("user", 1, 'u'),
OPTION("verbose", 0, 'v'),
OPTION("version", 0, 'V'),
OPTION("cafile", 1, OPT_CAFILE),
OPTION("config", 1, OPT_CONFIGFILE),
OPTION("no-dtls", 0, OPT_NO_DTLS),
OPTION("authenticate", 0, OPT_AUTHENTICATE),
OPTION("cookieonly", 0, OPT_COOKIEONLY),
OPTION("printcookie", 0, OPT_PRINTCOOKIE),
OPTION("quiet", 0, 'q'),
OPTION("queue-len", 1, 'Q'),
OPTION("xmlconfig", 1, 'x'),
OPTION("cookie-on-stdin", 0, OPT_COOKIE_ON_STDIN),
OPTION("passwd-on-stdin", 0, OPT_PASSWORD_ON_STDIN),
OPTION("no-passwd", 0, OPT_NO_PASSWD),
OPTION("reconnect-timeout", 1, OPT_RECONNECT_TIMEOUT),
OPTION("dtls-ciphers", 1, OPT_DTLS_CIPHERS),
OPTION("dtls12-ciphers", 1, OPT_DTLS12_CIPHERS),
OPTION("authgroup", 1, OPT_AUTHGROUP),
OPTION("servercert", 1, OPT_SERVERCERT),
OPTION("resolve", 1, OPT_RESOLVE),
OPTION("key-password-from-fsid", 0, OPT_KEY_PASSWORD_FROM_FSID),
OPTION("useragent", 1, OPT_USERAGENT),
OPTION("version-string", 1, OPT_VERSION),
OPTION("local-hostname", 1, OPT_LOCAL_HOSTNAME),
OPTION("disable-ipv6", 0, OPT_DISABLE_IPV6),
OPTION("no-proxy", 0, OPT_NO_PROXY),
OPTION("libproxy", 0, OPT_LIBPROXY),
OPTION("no-http-keepalive", 0, OPT_NO_HTTP_KEEPALIVE),
OPTION("no-cert-check", 0, OPT_NO_CERT_CHECK),
OPTION("force-dpd", 1, OPT_FORCE_DPD),
OPTION("force-trojan", 1, OPT_FORCE_TROJAN),
OPTION("non-inter", 0, OPT_NON_INTER),
OPTION("dtls-local-port", 1, OPT_DTLS_LOCAL_PORT),
OPTION("token-mode", 1, OPT_TOKEN_MODE),
OPTION("token-secret", 1, OPT_TOKEN_SECRET),
OPTION("os", 1, OPT_OS),
OPTION("no-xmlpost", 0, OPT_NO_XMLPOST),
OPTION("dump-http-traffic", 0, OPT_DUMP_HTTP),
OPTION("no-system-trust", 0, OPT_NO_SYSTEM_TRUST),
OPTION("protocol", 1, OPT_PROTOCOL),
OPTION("form-entry", 1, 'F'),
#ifdef OPENCONNECT_GNUTLS
OPTION("gnutls-debug", 1, OPT_GNUTLS_DEBUG),
OPTION("gnutls-priority", 1, OPT_CIPHERSUITES),
#elif defined(OPENCONNECT_OPENSSL)
OPTION("openssl-ciphers", 1, OPT_CIPHERSUITES),
#endif
OPTION(NULL, 0, 0)
};
#ifdef OPENCONNECT_GNUTLS
static void oc_gnutls_log_func(int level, const char *str)
{
fputs(str, stderr);
}
#endif
#ifdef _WIN32
static int __attribute__ ((format(printf, 2, 0)))
vfprintf_utf8(FILE *f, const char *fmt, va_list args)
{
HANDLE h = GetStdHandle(f == stdout ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
wchar_t wbuf[1024];
char buf[1024];
int bytes, wchars;
/* No need to NUL-terminate strings here */
bytes = _vsnprintf(buf, sizeof(buf), fmt, args);
if (bytes < 0)
return bytes;
if (bytes > sizeof(buf))
bytes = sizeof(buf);
wchars = MultiByteToWideChar(CP_UTF8, 0, buf, bytes, wbuf, sizeof(wbuf)/2);
if (!wchars)
return -1;
/*
* If writing to console fails, that's probably due to redirection.
* Convert to console CP and write to the FH, following the example of
* https://github.com/wine-mirror/wine/blob/e909986e6e/programs/whoami/main.c#L33-L49
*/
if (!WriteConsoleW(h, wbuf, wchars, NULL, NULL)) {
bytes = WideCharToMultiByte(GetConsoleOutputCP(), 0, wbuf, wchars,
buf, sizeof(buf), NULL, NULL);
if (!bytes)
return -1;
return fwrite(buf, 1, bytes, f);
}
return bytes;
}
static int __attribute__ ((format(printf, 2, 3)))
fprintf_utf8(FILE *f, const char *fmt, ...)
{
va_list args;
int ret;
va_start(args, fmt);
ret = vfprintf_utf8(f, fmt, args);
va_end(args);
return ret;
}
static wchar_t **argv_w;
/* This isn't so much "convert" the arg to UTF-8, as go grubbing
* around in the real UTF-16 command line and find the corresponding
* argument *there*, and convert *that* to UTF-8. Ick. But the
* alternative is to implement wgetopt(), and that's even more horrid. */
static char *convert_arg_to_utf8(char **argv, char *arg)
{
char *utf8;
int chars;
int offset;
if (!argv_w) {
int argc_w;
argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
if (!argv_w) {
char *errstr = openconnect__win32_strerror(GetLastError());
fprintf(stderr, _("CommandLineToArgvW() failed: %s\n"),
errstr);
free(errstr);
exit(1);
}
}
offset = arg - argv[optind - 1];
/* Sanity check */
if (offset < 0 || offset >= strlen(argv[optind - 1]) ||
(offset && (argv[optind - 1][offset-1] != '=' ||
argv_w[optind - 1][offset - 1] != '='))) {
fprintf(stderr, _("Fatal error in command line handling\n"));
exit(1);
}
chars = WideCharToMultiByte(CP_UTF8, 0, argv_w[optind-1] + offset, -1,
NULL, 0, NULL, NULL);
utf8 = malloc(chars);
if (!utf8)
return arg;
WideCharToMultiByte(CP_UTF8, 0, argv_w[optind-1] + offset, -1, utf8,
chars, NULL, NULL);
return utf8;
}
#undef fprintf
#undef vfprintf
#define fprintf fprintf_utf8
#define vfprintf vfprintf_utf8
#define is_arg_utf8(str) (0)
static void read_stdin(char **string, int hidden, int allow_fail)
{
CONSOLE_READCONSOLE_CONTROL rcc = { sizeof(rcc), 0, 13, 0 };
HANDLE stdinh = GetStdHandle(STD_INPUT_HANDLE);
DWORD cmode, nr_read;
wchar_t wbuf[1024];
char *buf;
if (GetConsoleMode(stdinh, &cmode)) {
if (hidden)
SetConsoleMode(stdinh, cmode & (~ENABLE_ECHO_INPUT));
if (!ReadConsoleW(stdinh, wbuf, sizeof(wbuf)/2, &nr_read, &rcc)) {
char *errstr = openconnect__win32_strerror(GetLastError());
fprintf(stderr, _("ReadConsole() failed: %s\n"), errstr);
free(errstr);
*string = NULL;
if (hidden)
SetConsoleMode(stdinh, cmode);
return;
}
if (hidden)
SetConsoleMode(stdinh, cmode);
} else {
/* Not a console; maybe reading from a piped stdin? */
if (!fgetws(wbuf, sizeof(wbuf)/2, stdin)) {
char *errstr = openconnect__win32_strerror(GetLastError());
fprintf(stderr, _("fgetws() failed: %s\n"), errstr);
free(errstr);
*string = NULL;
return;
}
nr_read = wcslen(wbuf);
}
if (nr_read >= 2 && wbuf[nr_read - 1] == 10 && wbuf[nr_read - 2] == 13) {
/* remove trailing "\r\n" */
wbuf[nr_read - 2] = 0;
nr_read -= 2;
} else if (nr_read >= 1 && wbuf[nr_read - 1] == 10) {
/* remove trailing "\n" */
wbuf[nr_read - 1] = 0;
nr_read -= 1;
}
nr_read = WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, NULL, 0, NULL, NULL);
if (!nr_read) {
char *errstr = openconnect__win32_strerror(GetLastError());
fprintf(stderr, _("Error converting console input: %s\n"),
errstr);
free(errstr);
return;
}
buf = malloc(nr_read);
if (!buf) {
fprintf(stderr, _("Allocation failure for string from stdin\n"));
exit(1);
}
if (!WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, nr_read, NULL, NULL)) {
char *errstr = openconnect__win32_strerror(GetLastError());
fprintf(stderr, _("Error converting console input: %s\n"),
errstr);
free(errstr);
free(buf);
return;
}
*string = buf;
}
#elif defined(HAVE_ICONV)
#include <iconv.h>
static int is_ascii(char *str)
{
while (str && *str) {
if ((unsigned char)*str > 0x7f)
return 0;
str++;
}
return 1;
}
static int __attribute__ ((format(printf, 2, 0)))
vfprintf_utf8(FILE *f, const char *fmt, va_list args)
{
char *utf8_str;
iconv_t ic;
int ret;
char outbuf[80];
ICONV_CONST char *ic_in;
char *ic_out;
size_t insize, outsize;
if (!legacy_charset)
return vfprintf(f, fmt, args);
ret = vasprintf(&utf8_str, fmt, args);
if (ret < 0)
return -1;
if (is_ascii(utf8_str))
return fwrite(utf8_str, 1, strlen(utf8_str), f);
ic = iconv_open(legacy_charset, "UTF-8");
if (ic == (iconv_t) -1) {
/* Better than nothing... */
ret = fprintf(f, "%s", utf8_str);
free(utf8_str);
return ret;
}
ic_in = utf8_str;
insize = strlen(utf8_str);
ret = 0;
while (insize) {
ic_out = outbuf;
outsize = sizeof(outbuf) - 1;
if (iconv(ic, &ic_in, &insize, &ic_out, &outsize) == (size_t)-1) {
if (errno == EILSEQ) {
do {
ic_in++;
insize--;
} while (insize && (ic_in[0] & 0xc0) == 0x80);
ic_out[0] = '?';
outsize--;
} else if (errno != E2BIG)
break;
}
ret += fwrite(outbuf, 1, sizeof(outbuf) - 1 - outsize, f);
}
iconv_close(ic);
return ret;
}
static int __attribute__ ((format(printf, 2, 3)))
fprintf_utf8(FILE *f, const char *fmt, ...)
{
va_list args;
int ret;
va_start(args, fmt);
ret = vfprintf_utf8(f, fmt, args);
va_end(args);
return ret;
}
static char *convert_to_utf8(char *legacy, int free_it)
{
char *utf8_str;
iconv_t ic;
ICONV_CONST char *ic_in;
char *ic_out;
size_t insize, outsize;
if (!legacy_charset || is_ascii(legacy))
return legacy;
ic = iconv_open("UTF-8", legacy_charset);
if (ic == (iconv_t) -1)
return legacy;
insize = strlen(legacy) + 1;
ic_in = legacy;
outsize = insize;
ic_out = utf8_str = malloc(outsize);
if (!utf8_str) {
enomem:
iconv_close(ic);
return legacy;
}
while (insize) {
if (iconv(ic, &ic_in, &insize, &ic_out, &outsize) == (size_t)-1) {
if (errno == E2BIG) {
int outlen = ic_out - utf8_str;
realloc_inplace(utf8_str, outlen + 10);
if (!utf8_str)
goto enomem;
ic_out = utf8_str + outlen;
outsize = 10;
} else {
/* Should never happen */
perror("iconv");
free(utf8_str);
goto enomem;
}
}
}
iconv_close(ic);
if (free_it)
free(legacy);
return utf8_str;
}
#define fprintf fprintf_utf8
#define vfprintf vfprintf_utf8
#define convert_arg_to_utf8(av, l) convert_to_utf8((l), 0)
#define is_arg_utf8(a) (!legacy_charset || is_ascii(a))
#else
#define convert_to_utf8(l,f) (l)
#define convert_arg_to_utf8(av, l) (l)
#define is_arg_utf8(a) (1)
#endif
static void helpmessage(void)
{
printf(_("For assistance with OpenConnect, please see the web page at\n"
" http://www.infradead.org/openconnect/mail.html\n"));
}
static void print_build_opts(void)
{
const char *comma = ", ", *sep = comma + 1;
printf(_("Using %s. Features present:"), openconnect_get_tls_library_version());
if (openconnect_has_tss_blob_support()) {
printf("%sTPM", sep);
sep = comma;
}
if (openconnect_has_tss2_blob_support()) {
printf("%sTPMv2", sep);
sep = comma;
}
#if defined(OPENCONNECT_OPENSSL) && defined(HAVE_ENGINE)
else {
printf("%sTPM (%s)", sep, _("OpenSSL ENGINE not present"));
sep = comma;
}
#endif
if (openconnect_has_pkcs11_support()) {
printf("%sPKCS#11", sep);
sep = comma;
}
if (openconnect_has_stoken_support()) {
printf("%sRSA software token", sep);
sep = comma;
}
switch(openconnect_has_oath_support()) {
case 2:
printf("%sHOTP software token", sep);
sep = comma;
/* fall through */
case 1:
printf("%sTOTP software token", sep);
sep = comma;
}
if (openconnect_has_yubioath_support()) {
printf("%sYubikey OATH", sep);
sep = comma;
}
if (openconnect_has_system_key_support()) {
printf("%sSystem keys", sep);
sep = comma;
}
#ifdef HAVE_DTLS
printf("%sDTLS", sep);
#endif
#ifdef HAVE_ESP
printf("%sESP", sep);
#endif
printf("\n");
#if !defined(HAVE_DTLS) || !defined(HAVE_ESP)
printf(_("WARNING: This binary lacks DTLS and/or ESP support. Performance will be impaired.\n"));
#endif
}
static void print_supported_protocols(void)
{
const char *comma = ", ", *sep = comma + 1;
struct oc_vpn_proto *protos, *p;
int n;
n = openconnect_get_supported_protocols(&protos);
if (n>=0) {
printf(_("Supported protocols:"));
for (p=protos; n; p++, n--) {
printf("%s%s%s", sep, p->name, p==protos ? _(" (default)") : "");
sep = comma;
}
printf("\n");
free(protos);
}
}
static void print_supported_protocols_usage(void)
{
struct oc_vpn_proto *protos, *p;
int n;
n = openconnect_get_supported_protocols(&protos);
if (n>=0) {
printf("\n%s:\n", _("Set VPN protocol"));
for (p=protos; n; p++, n--)
printf(" --protocol=%-16s %s%s\n",
p->name, p->description, p==protos ? _(" (default)") : "");
openconnect_free_supported_protocols(protos);
}
}
#ifndef _WIN32
static const char default_vpncscript[] = DEFAULT_VPNCSCRIPT;
static void read_stdin(char **string, int hidden, int allow_fail)
{
char *c, *got, *buf = malloc(1025);
int fd = fileno(stdin);
struct termios t;
if (!buf) {
fprintf(stderr, _("Allocation failure for string from stdin\n"));
exit(1);
}
if (hidden) {
tcgetattr(fd, &t);
t.c_lflag &= ~ECHO;
tcsetattr(fd, TCSANOW, &t);
}
got = fgets(buf, 1025, stdin);
if (hidden) {
t.c_lflag |= ECHO;
tcsetattr(fd, TCSANOW, &t);
fprintf(stderr, "\n");
}
if (!got) {
if (allow_fail) {
*string = NULL;
free(buf);
return;
} else {
perror(_("fgets (stdin)"));
exit(1);
}
}
c = strchr(buf, '\n');
if (c)
*c = 0;
*string = convert_to_utf8(buf, 1);
}
static void handle_signal(int sig)
{
char cmd;
switch (sig) {
case SIGTERM:
cmd = OC_CMD_CANCEL;
break;
case SIGHUP:
cmd = OC_CMD_DETACH;
break;
case SIGINT:
#ifdef INSECURE_DEBUGGING
cmd = OC_CMD_DETACH;
#else
cmd = OC_CMD_CANCEL;
#endif
break;
case SIGUSR1:
cmd = OC_CMD_STATS;
break;
case SIGUSR2:
default:
cmd = OC_CMD_PAUSE;
break;
}
if (write(sig_cmd_fd, &cmd, 1) < 0) {
/* suppress warn_unused_result */
}
}
#else /* _WIN32 */
static const char *default_vpncscript;
static void set_default_vpncscript(void)
{
if (PathIsRelative(DEFAULT_VPNCSCRIPT)) {
char *c = strrchr(_pgmptr, '\\');
if (!c) {
fprintf(stderr, _("Cannot process this executable path \"%s\""),
_pgmptr);
exit(1);
}
if (asprintf((char **)&default_vpncscript, "%.*s%s",
(c - _pgmptr + 1), _pgmptr, DEFAULT_VPNCSCRIPT) < 0) {
fprintf(stderr, _("Allocation for vpnc-script path failed\n"));
exit(1);
}
} else {
default_vpncscript = "cscript " DEFAULT_VPNCSCRIPT;
}
}
#endif
static struct oc_vpn_option *gai_overrides;
static int gai_override_cb(void *cbdata, const char *node,
const char *service, const struct addrinfo *hints,
struct addrinfo **res)
{
struct openconnect_info *vpninfo = cbdata;
struct oc_vpn_option *p = gai_overrides;
while (p) {
if (!strcmp(node, p->option)) {
vpn_progress(vpninfo, PRG_TRACE, _("Override hostname '%s' to '%s'\n"),
node, p->value);
node = p->value;
break;
}
p = p->next;
}
return getaddrinfo(node, service, hints, res);
}
static void usage(void)
{
printf(_("Usage: openconnect [options] <server>\n"));
printf(_("Open client for multiple VPN protocols, version %s\n\n"), openconnect_version_str);
print_build_opts();
printf(" --config=CONFIGFILE %s\n", _("Read options from config file"));
printf(" -V, --version %s\n", _("Report version number"));
printf(" -h, --help %s\n", _("Display help text"));
print_supported_protocols_usage();
printf("\n%s:\n", _("Authentication"));
printf(" -u, --user=NAME %s\n", _("Set login username"));
printf(" --no-passwd %s\n", _("Disable password/SecurID authentication"));
printf(" --non-inter %s\n", _("Do not expect user input; exit if it is required"));
printf(" --passwd-on-stdin %s\n", _("Read password from standard input"));
printf(" --authgroup=GROUP %s\n", _("Choose authentication login selection"));
printf(" -F, --form-entry=FORM:OPT=VALUE %s\n", _("Provide authentication form responses"));
printf(" -c, --certificate=CERT %s\n", _("Use SSL client certificate CERT"));
printf(" -k, --sslkey=KEY %s\n", _("Use SSL private key file KEY"));
printf(" -e, --cert-expire-warning=DAYS %s\n", _("Warn when certificate lifetime < DAYS"));
printf(" -g, --usergroup=GROUP %s\n", _("Set login usergroup"));
printf(" -p, --key-password=PASS %s\n", _("Set key passphrase or TPM SRK PIN"));
printf(" --key-password-from-fsid %s\n", _("Key passphrase is fsid of file system"));
printf(" --token-mode=MODE %s\n", _("Software token type: rsa, totp, hotp or oidc"));
printf(" --token-secret=STRING %s\n", _("Software token secret or oidc token"));
#ifndef HAVE_LIBSTOKEN
printf(" %s\n", _("(NOTE: libstoken (RSA SecurID) disabled in this build)"));
#endif
#ifndef HAVE_LIBPCSCLITE
printf(" %s\n", _("(NOTE: Yubikey OATH disabled in this build)"));
#endif
printf("\n%s:\n", _("Server validation"));
printf(" --servercert=FINGERPRINT %s\n", _("Accept only server certificate with this fingerprint"));
printf(" --no-system-trust %s\n", _("Disable default system certificate authorities"));
printf(" --cafile=FILE %s\n", _("Cert file for server verification"));
printf("\n%s:\n", _("Internet connectivity"));
printf(" -P, --proxy=URL %s\n", _("Set proxy server"));
printf(" --proxy-auth=METHODS %s\n", _("Set proxy authentication methods"));
printf(" --no-proxy %s\n", _("Disable proxy"));
printf(" --libproxy %s\n", _("Use libproxy to automatically configure proxy"));
#ifndef LIBPROXY_HDR
printf(" %s\n", _("(NOTE: libproxy disabled in this build)"));
#endif
printf(" --reconnect-timeout %s\n", _("Connection retry timeout in seconds"));
printf(" --resolve=HOST:IP %s\n", _("Use IP when connecting to HOST"));
printf(" --passtos %s\n", _("Copy TOS / TCLASS field into DTLS and ESP packets"));
printf(" --dtls-local-port=PORT %s\n", _("Set local port for DTLS and ESP datagrams"));
printf("\n%s:\n", _("Authentication (two-phase)"));
printf(" -C, --cookie=COOKIE %s\n", _("Use authentication cookie COOKIE"));
printf(" --cookie-on-stdin %s\n", _("Read cookie from standard input"));
printf(" --authenticate %s\n", _("Authenticate only and print login info"));
printf(" --cookieonly %s\n", _("Fetch and print cookie only; don't connect"));
printf(" --printcookie %s\n", _("Print cookie before connecting"));
#ifndef _WIN32
printf("\n%s:\n", _("Process control"));
printf(" -b, --background %s\n", _("Continue in background after startup"));
printf(" --pid-file=PIDFILE %s\n", _("Write the daemon's PID to this file"));
printf(" -U, --setuid=USER %s\n", _("Drop privileges after connecting"));
#endif
printf("\n%s:\n", _("Logging (two-phase)"));
#ifndef _WIN32
printf(" -l, --syslog %s\n", _("Use syslog for progress messages"));
#endif
printf(" -v, --verbose %s\n", _("More output"));
printf(" -q, --quiet %s\n", _("Less output"));
printf(" --dump-http-traffic %s\n", _("Dump HTTP authentication traffic (implies --verbose)"));
printf(" --timestamp %s\n", _("Prepend timestamp to progress messages"));
printf("\n%s:\n", _("VPN configuration script"));
printf(" -i, --interface=IFNAME %s\n", _("Use IFNAME for tunnel interface"));
printf(" -s, --script=SCRIPT %s\n", _("Shell command line for using a vpnc-compatible config script"));
printf(" %s: \"%s\"\n", _("default"), default_vpncscript);
#ifndef _WIN32
printf(" -S, --script-tun %s\n", _("Pass traffic to 'script' program, not tun"));
#endif
printf("\n%s:\n", _("Tunnel control"));
printf(" --disable-ipv6 %s\n", _("Do not ask for IPv6 connectivity"));
printf(" -x, --xmlconfig=CONFIG %s\n", _("XML config file"));
printf(" -m, --mtu=MTU %s\n", _("Request MTU from server (legacy servers only)"));
printf(" --base-mtu=MTU %s\n", _("Indicate path MTU to/from server"));
printf(" -d, --deflate %s\n", _("Enable stateful compression (default is stateless only)"));
printf(" -D, --no-deflate %s\n", _("Disable all compression"));
printf(" --force-dpd=INTERVAL %s\n", _("Set minimum Dead Peer Detection interval (in seconds)"));
printf(" --pfs %s\n", _("Require perfect forward secrecy"));
printf(" --no-dtls %s\n", _("Disable DTLS and ESP"));
printf(" --dtls-ciphers=LIST %s\n", _("OpenSSL ciphers to support for DTLS"));
printf(" -Q, --queue-len=LEN %s\n", _("Set packet queue limit to LEN pkts"));
printf("\n%s:\n", _("Local system information"));
printf(" --useragent=STRING %s\n", _("HTTP header User-Agent: field"));
printf(" --local-hostname=STRING %s\n", _("Local hostname to advertise to server"));
printf(" --os=STRING %s\n", _("OS type (linux,linux-64,win,...) to report"));
printf(" --version-string=STRING %s\n", _("reported version string during authentication"));
printf(" (%s %s)\n", _("default:"), openconnect_version_str);
#ifndef _WIN32
printf("\n%s:\n", _("Trojan binary (CSD) execution"));
printf(" --csd-user=USER %s\n", _("Drop privileges during trojan execution"));
printf(" --csd-wrapper=SCRIPT %s\n", _("Run SCRIPT instead of trojan binary"));
printf(" --force-trojan=INTERVAL %s\n", _("Set minimum interval for rerunning trojan (in seconds)"));
#endif
printf("\n%s:\n", _("Server bugs"));
printf(" --no-http-keepalive %s\n", _("Disable HTTP connection re-use"));
printf(" --no-xmlpost %s\n", _("Do not attempt XML POST authentication"));
printf(" --allow-insecure-crypto %s\n", _("Allow use of the ancient, insecure 3DES and RC4 ciphers"));
printf(" %s\n", _("(and attempt to override OS crypto policies)"));
printf("\n");
helpmessage();
exit(1);
}
static FILE *config_file = NULL;
static int config_line_num = 0;
static char *xstrdup(const char *arg)
{
char *ret;
if (!arg)
return NULL;
ret = strdup(arg);
if (!ret) {
fprintf(stderr, _("Failed to allocate string\n"));
exit(1);
}
return ret;
}
/* There are three ways to handle config_arg:
*
* 1. We only care about it transiently and it can be lost entirely
* (e.g. vpninfo->reconnect_timeout = atoi(config_arg);
* 2. We need to keep it, but it's a static string and will never be freed
* so when it's part of argv[] we can use it in place (unless it needs
* converting to UTF-8), but when it comes from a file we have to strdup()
* because otherwise it'll be overwritten.
* For this we use the keep_config_arg() macro below.
* 3. It may be freed during normal operation, so we have to use strdup()
* or convert_arg_to_utf8() even when it's an option from argv[].
* (e.g. vpninfo->certinfo[0].password).
* For this we use the dup_config_arg() macro below.
*/