2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008-2012 Intel Corporation.
5 * Copyright © 2008 Nick Andrew <nick@nick-andrew.net>
7 * Author: David Woodhouse <dwmw2@infradead.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * version 2.1, as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to:
21 * Free Software Foundation, Inc.
22 * 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301 USA
27 /* Various BSD systems require this for getline() to be visible */
33 #include <android/log.h>
48 #include <sys/utsname.h>
49 #include <sys/types.h>
50 #include <openssl/ui.h>
57 #include "openconnect-internal.h"
59 static int write_new_config(void *_vpninfo,
60 char *buf, int buflen);
61 static void write_progress(void *_vpninfo,
62 int level, const char *fmt, ...);
63 static void syslog_progress(void *_vpninfo,
64 int level, const char *fmt, ...);
65 static int validate_peer_cert(void *_vpninfo,
66 OPENCONNECT_X509 *peer_cert,
68 static int process_auth_form(void *_vpninfo,
69 struct oc_auth_form *form);
71 /* A sanity check that the openconnect executable is running against a
72 library of the same version */
73 #define openconnect_version_str openconnect_binary_version
75 #undef openconnect_version_str
77 int verbose = PRG_INFO;
79 int do_passphrase_from_fsid;
84 OPT_AUTHGROUP = 0x100,
95 OPT_KEY_PASSWORD_FROM_FSID,
99 OPT_NO_HTTP_KEEPALIVE,
103 OPT_PASSWORD_ON_STDIN,
105 OPT_RECONNECT_TIMEOUT,
113 * The 'name' field in Solaris 'struct option' lacks the 'const', and causes
114 * lots of warnings unless we cast it... https://www.illumos.org/issues/1881
116 #define OPTION(name, arg, abbrev) {(char *)name, arg, NULL, abbrev}
118 #define OPTION(name, arg, abbrev) {name, arg, NULL, abbrev}
121 static struct option long_options[] = {
122 OPTION("background", 0, 'b'),
123 OPTION("pid-file", 1, OPT_PIDFILE),
124 OPTION("certificate", 1, 'c'),
125 OPTION("sslkey", 1, 'k'),
126 OPTION("cookie", 1, 'C'),
127 OPTION("deflate", 0, 'd'),
128 OPTION("no-deflate", 0, 'D'),
129 OPTION("cert-expire-warning", 1, 'e'),
130 OPTION("usergroup", 1, 'g'),
131 OPTION("help", 0, 'h'),
132 OPTION("interface", 1, 'i'),
133 OPTION("mtu", 1, 'm'),
134 OPTION("base-mtu", 1, OPT_BASEMTU),
135 OPTION("setuid", 1, 'U'),
136 OPTION("script", 1, 's'),
137 OPTION("script-tun", 0, 'S'),
138 OPTION("syslog", 0, 'l'),
139 OPTION("key-type", 1, 'K'),
140 OPTION("key-password", 1, 'p'),
141 OPTION("proxy", 1, 'P'),
142 OPTION("user", 1, 'u'),
143 OPTION("verbose", 0, 'v'),
144 OPTION("version", 0, 'V'),
145 OPTION("cafile", 1, OPT_CAFILE),
146 OPTION("config", 1, OPT_CONFIGFILE),
147 OPTION("no-dtls", 0, OPT_NO_DTLS),
148 OPTION("cookieonly", 0, OPT_COOKIEONLY),
149 OPTION("printcookie", 0, OPT_PRINTCOOKIE),
150 OPTION("quiet", 0, 'q'),
151 OPTION("queue-len", 1, 'Q'),
152 OPTION("xmlconfig", 1, 'x'),
153 OPTION("cookie-on-stdin", 0, OPT_COOKIE_ON_STDIN),
154 OPTION("passwd-on-stdin", 0, OPT_PASSWORD_ON_STDIN),
155 OPTION("no-passwd", 0, OPT_NO_PASSWD),
156 OPTION("reconnect-timeout", 1, OPT_RECONNECT_TIMEOUT),
157 OPTION("dtls-ciphers", 1, OPT_DTLS_CIPHERS),
158 OPTION("authgroup", 1, OPT_AUTHGROUP),
159 OPTION("servercert", 1, OPT_SERVERCERT),
160 OPTION("key-password-from-fsid", 0, OPT_KEY_PASSWORD_FROM_FSID),
161 OPTION("useragent", 1, OPT_USERAGENT),
162 OPTION("csd-user", 1, OPT_CSD_USER),
163 OPTION("csd-wrapper", 1, OPT_CSD_WRAPPER),
164 OPTION("disable-ipv6", 0, OPT_DISABLE_IPV6),
165 OPTION("no-proxy", 0, OPT_NO_PROXY),
166 OPTION("libproxy", 0, OPT_LIBPROXY),
167 OPTION("no-http-keepalive", 0, OPT_NO_HTTP_KEEPALIVE),
168 OPTION("no-cert-check", 0, OPT_NO_CERT_CHECK),
169 OPTION("force-dpd", 1, OPT_FORCE_DPD),
170 OPTION("non-inter", 0, OPT_NON_INTER),
174 static void helpmessage(void)
176 printf(_("For assistance with OpenConnect, please see the web page at\n"
177 " http://www.infradead.org/openconnect/mail.html\n"));
180 static void usage(void)
182 printf(_("Usage: openconnect [options] <server>\n"));
183 printf(_("Open client for Cisco AnyConnect VPN, version %s\n\n"), openconnect_version_str);
184 printf(" --config=CONFIGFILE %s\n", _("Read options from config file"));
185 printf(" -b, --background %s\n", _("Continue in background after startup"));
186 printf(" --pid-file=PIDFILE %s\n", _("Write the daemons pid to this file"));
187 printf(" -c, --certificate=CERT %s\n", _("Use SSL client certificate CERT"));
188 printf(" -e, --cert-expire-warning=DAYS %s\n", _("Warn when certificate lifetime < DAYS"));
189 printf(" -k, --sslkey=KEY %s\n", _("Use SSL private key file KEY"));
190 printf(" -K, --key-type=TYPE %s\n", _("Private key type (PKCS#12 / TPM / PEM)"));
191 printf(" -C, --cookie=COOKIE %s\n", _("Use WebVPN cookie COOKIE"));
192 printf(" --cookie-on-stdin %s\n", _("Read cookie from standard input"));
193 printf(" -d, --deflate %s\n", _("Enable compression (default)"));
194 printf(" -D, --no-deflate %s\n", _("Disable compression"));
195 printf(" --force-dpd=INTERVAL %s\n", _("Set minimum Dead Peer Detection interval"));
196 printf(" -g, --usergroup=GROUP %s\n", _("Set login usergroup"));
197 printf(" -h, --help %s\n", _("Display help text"));
198 printf(" -i, --interface=IFNAME %s\n", _("Use IFNAME for tunnel interface"));
199 printf(" -l, --syslog %s\n", _("Use syslog for progress messages"));
200 printf(" -U, --setuid=USER %s\n", _("Drop privileges after connecting"));
201 printf(" --csd-user=USER %s\n", _("Drop privileges during CSD execution"));
202 printf(" --csd-wrapper=SCRIPT %s\n", _("Run SCRIPT instead of CSD binary"));
203 printf(" -m, --mtu=MTU %s\n", _("Request MTU from server"));
204 printf(" --base-mtu=MTU %s\n", _("Indicate path MTU to/from server"));
205 printf(" -p, --key-password=PASS %s\n", _("Set key passphrase or TPM SRK PIN"));
206 printf(" --key-password-from-fsid %s\n", _("Key passphrase is fsid of file system"));
207 printf(" -P, --proxy=URL %s\n", _("Set proxy server"));
208 printf(" --no-proxy %s\n", _("Disable proxy"));
209 printf(" --libproxy %s\n", _("Use libproxy to automatically configure proxy"));
211 printf(" %s\n", _("(NOTE: libproxy disabled in this build)"));
213 printf(" -q, --quiet %s\n", _("Less output"));
214 printf(" -Q, --queue-len=LEN %s\n", _("Set packet queue limit to LEN pkts"));
215 printf(" -s, --script=SCRIPT %s\n", _("Shell command line for using a vpnc-compatible config script"));
216 printf(" %s: \"%s\"\n", _("default"), DEFAULT_VPNCSCRIPT);
217 printf(" -S, --script-tun %s\n", _("Pass traffic to 'script' program, not tun"));
218 printf(" -u, --user=NAME %s\n", _("Set login username"));
219 printf(" -V, --version %s\n", _("Report version number"));
220 printf(" -v, --verbose %s\n", _("More output"));
221 printf(" -x, --xmlconfig=CONFIG %s\n", _("XML config file"));
222 printf(" --authgroup=GROUP %s\n", _("Choose authentication login selection"));
223 printf(" --cookieonly %s\n", _("Fetch webvpn cookie only; don't connect"));
224 printf(" --printcookie %s\n", _("Print webvpn cookie before connecting"));
225 printf(" --cafile=FILE %s\n", _("Cert file for server verification"));
226 printf(" --disable-ipv6 %s\n", _("Do not ask for IPv6 connectivity"));
227 printf(" --dtls-ciphers=LIST %s\n", _("OpenSSL ciphers to support for DTLS"));
228 printf(" --no-dtls %s\n", _("Disable DTLS"));
229 printf(" --no-http-keepalive %s\n", _("Disable HTTP connection re-use"));
230 printf(" --no-passwd %s\n", _("Disable password/SecurID authentication"));
231 printf(" --no-cert-check %s\n", _("Do not require server SSL cert to be valid"));
232 printf(" --non-inter %s\n", _("Do not expect user input; exit if it is required"));
233 printf(" --passwd-on-stdin %s\n", _("Read password from standard input"));
234 printf(" --reconnect-timeout %s\n", _("Connection retry timeout in seconds"));
235 printf(" --servercert=FINGERPRINT %s\n", _("Server's certificate SHA1 fingerprint"));
236 printf(" --useragent=STRING %s\n", _("HTTP header User-Agent: field"));
243 static void read_stdin(char **string)
245 char *c = malloc(100);
247 fprintf(stderr, _("Allocation failure for string from stdin\n"));
250 if (!fgets(c, 100, stdin)) {
251 perror(_("fgets (stdin)"));
257 c = strchr(*string, '\n');
261 static void handle_sigusr(int sig)
265 else if (sig == SIGUSR2)
269 static FILE *config_file = NULL;
270 static int config_line_num = 0;
272 /* There are three ways to handle config_arg:
274 * 1. We only care about it transiently and it can be lost entirely
275 * (e.g. vpninfo->reconnect_timeout = atoi(config_arg);
276 * 2. We need to kep it, but it's a static string and will never be freed
277 * so when it's part of argv[] we can use it in place, but when it comes
278 * from a file we have to strdup() because otherwise it'll be overwritten.
279 * For this we use the keep_config_arg() macro below.
280 * 3. It may be freed during normal operation, so we have to use strdup()
281 * even when it's an option from argv[]. (e.g. vpninfo->cert_password).
283 #define keep_config_arg() (config_file?strdup(config_arg):config_arg)
285 static int next_option(int argc, char **argv, char **config_arg)
287 /* These get re-used */
288 static char *line_buf = NULL;
289 static size_t line_size = 0;
299 opt = getopt_long(argc, argv,
300 "bC:c:e:Ddg:hi:k:K:lpP:Q:qSs:U:u:Vvx:",
303 *config_arg = optarg;
307 llen = getline(&line_buf, &line_size, config_file);
309 if (feof(config_file)) {
314 fprintf(stderr, _("Failed to get line from config file: %s\n"),
320 /* Strip the trailing newline (coping with DOS newlines) */
321 if (llen && line[llen-1] == '\n')
323 if (llen && line[llen-1] == '\r')
326 /* Skip and leading whitespace */
327 while (line[0] == ' ' || line[0] == '\t' || line[0] == '\r')
330 /* Ignore comments and empty lines */
331 if (!line[0] || line[0] == '#') {
336 /* Try to match on a known option... naïvely. This could be improved. */
337 for (this = long_options; this->name; this++) {
338 optlen = strlen(this->name);
339 /* If the option isn't followed by whitespace or NUL, or
340 perhaps an equals sign if the option takes an argument,
341 then it's not a match */
342 if (!strncmp(this->name, line, optlen) &&
343 (!line[optlen] || line[optlen] == ' ' || line[optlen] == '\t' ||
344 line[optlen] == '='))
350 for (l = line; *l && *l != ' ' && *l != '\t'; l++)
354 fprintf(stderr, _("Unrecognised option at line %d: '%s'\n"),
355 config_line_num, line);
359 while (*line == ' ' || *line == '\t' ||
360 (*line == '=' && this->has_arg && !ate_equals && ++ate_equals))
363 if (!this->has_arg && *line) {
364 fprintf(stderr, _("Option '%s' does not take an argument at line %d\n"),
365 this->name, config_line_num);
367 } else if (this->has_arg && !*line) {
368 fprintf(stderr, _("Option '%s' requires an argument at line %d\n"),
369 this->name, config_line_num);
379 int main(int argc, char **argv)
381 struct openconnect_info *vpninfo;
382 struct utsname utsbuf;
386 char *urlpath = NULL;
387 char *proxy = getenv("https_proxy");
389 uid_t uid = getuid();
391 char *pidfile = NULL;
396 bindtextdomain("openconnect", LOCALEDIR);
397 setlocale(LC_ALL, "");
400 if (strcmp(openconnect_version_str, openconnect_binary_version)) {
401 fprintf(stderr, _("WARNING: This version of openconnect is %s but\n"
402 " the libopenconnect library is %s\n"),
403 openconnect_binary_version, openconnect_version_str);
406 openconnect_init_ssl();
408 vpninfo = malloc(sizeof(*vpninfo));
410 fprintf(stderr, _("Failed to allocate vpninfo structure\n"));
413 memset(vpninfo, 0, sizeof(*vpninfo));
415 /* Set up some defaults */
416 vpninfo->tun_fd = vpninfo->ssl_fd = vpninfo->dtls_fd = vpninfo->new_dtls_fd = -1;
417 vpninfo->useragent = openconnect_create_useragent("Open AnyConnect VPN Agent");
419 vpninfo->deflate = 1;
420 vpninfo->dtls_attempt_period = 60;
421 vpninfo->max_qlen = 10;
422 vpninfo->reconnect_interval = RECONNECT_INTERVAL_MIN;
423 vpninfo->reconnect_timeout = 300;
424 vpninfo->uid_csd = 0;
425 vpninfo->uid_csd_given = 0;
426 vpninfo->validate_peer_cert = validate_peer_cert;
427 vpninfo->process_auth_form = process_auth_form;
428 vpninfo->cbdata = vpninfo;
429 vpninfo->cert_expire_warning = 60 * 86400;
430 vpninfo->vpnc_script = DEFAULT_VPNCSCRIPT;
431 vpninfo->cancel_fd = -1;
434 vpninfo->localname = utsbuf.nodename;
436 vpninfo->localname = "localhost";
438 while ((opt = next_option(argc, argv, &config_arg))) {
446 fprintf(stderr, _("Cannot use 'config' option inside config file\n"));
449 config_file = fopen(config_arg, "r");
451 fprintf(stderr, _("Cannot open config file '%s': %s\n"),
452 config_arg, strerror(errno));
456 /* The next option will come from the file... */
459 vpninfo->cafile = keep_config_arg();
462 pidfile = keep_config_arg();
465 vpninfo->servercert = keep_config_arg();
468 vpninfo->dtls_attempt_period = 0;
473 case OPT_PRINTCOOKIE:
476 case OPT_COOKIE_ON_STDIN:
477 read_stdin(&vpninfo->cookie);
478 /* If the cookie is empty, ignore it */
479 if (! *vpninfo->cookie) {
480 vpninfo->cookie = NULL;
483 case OPT_PASSWORD_ON_STDIN:
484 read_stdin(&vpninfo->password);
487 vpninfo->nopasswd = 1;
492 case OPT_RECONNECT_TIMEOUT:
493 vpninfo->reconnect_timeout = atoi(config_arg);
495 case OPT_DTLS_CIPHERS:
496 vpninfo->dtls_ciphers = keep_config_arg();
499 vpninfo->authgroup = keep_config_arg();
505 vpninfo->cookie = keep_config_arg();
508 vpninfo->cert = keep_config_arg();
511 vpninfo->cert_expire_warning = 86400 * atoi(config_arg);
514 vpninfo->sslkey = keep_config_arg();
517 if (!strcasecmp(config_arg, "PKCS#12") ||
518 !strcasecmp(config_arg, "PKCS12")) {
519 vpninfo->cert_type = CERT_TYPE_PKCS12;
520 } else if (!strcasecmp(config_arg, "TPM")) {
521 vpninfo->cert_type = CERT_TYPE_TPM;
522 } else if (!strcasecmp(config_arg, "PEM")) {
523 vpninfo->cert_type = CERT_TYPE_PEM;
525 fprintf(stderr, _("Unknown certificate type '%s'\n"),
530 vpninfo->deflate = 1;
533 vpninfo->deflate = 0;
537 urlpath = strdup(config_arg);
542 vpninfo->ifname = keep_config_arg();
548 vpninfo->mtu = atol(config_arg);
549 if (vpninfo->mtu < 576) {
550 fprintf(stderr, _("MTU %d too small\n"), vpninfo->mtu);
555 vpninfo->basemtu = atol(config_arg);
556 if (vpninfo->basemtu < 576) {
557 fprintf(stderr, _("MTU %d too small\n"), vpninfo->basemtu);
558 vpninfo->basemtu = 576;
562 vpninfo->cert_password = strdup(config_arg);
565 proxy = keep_config_arg();
575 case OPT_NO_HTTP_KEEPALIVE:
577 _("Disabling all HTTP connection re-use due to --no-http-keepalive option.\n"
578 "If this helps, please report to <openconnect-devel@lists.infradead.org>.\n"));
579 vpninfo->no_http_keepalive = 1;
581 case OPT_NO_CERT_CHECK:
585 vpninfo->vpnc_script = keep_config_arg();
588 vpninfo->script_tun = 1;
591 vpninfo->username = keep_config_arg();
595 uid = strtol(config_arg, &strend, 0);
597 struct passwd *pw = getpwnam(config_arg);
599 fprintf(stderr, _("Invalid user \"%s\"\n"),
609 vpninfo->uid_csd = strtol(config_arg, &strend, 0);
611 struct passwd *pw = getpwnam(config_arg);
613 fprintf(stderr, _("Invalid user \"%s\"\n"),
617 vpninfo->uid_csd = pw->pw_uid;
619 vpninfo->uid_csd_given = 1;
622 case OPT_CSD_WRAPPER:
623 vpninfo->csd_wrapper = keep_config_arg();
625 case OPT_DISABLE_IPV6:
626 vpninfo->disable_ipv6 = 1;
629 vpninfo->max_qlen = atol(config_arg);
630 if (!vpninfo->max_qlen) {
631 fprintf(stderr, _("Queue length zero not permitted; using 1\n"));
632 vpninfo->max_qlen = 1;
642 printf(_("OpenConnect version %s\n"), openconnect_version_str);
645 vpninfo->xmlconfig = keep_config_arg();
646 vpninfo->write_new_config = write_new_config;
648 case OPT_KEY_PASSWORD_FROM_FSID:
649 do_passphrase_from_fsid = 1;
652 free(vpninfo->useragent);
653 vpninfo->useragent = strdup(config_arg);
656 vpninfo->dtls_times.dpd = vpninfo->ssl_times.dpd = atoi(config_arg);
663 if (optind < argc - 1) {
664 fprintf(stderr, _("Too many arguments on command line\n"));
666 } else if (optind > argc - 1) {
667 fprintf(stderr, _("No server specified\n"));
671 if (!vpninfo->sslkey)
672 vpninfo->sslkey = vpninfo->cert;
674 vpninfo->progress = write_progress;
678 vpninfo->proxy_factory = px_proxy_factory_new();
680 fprintf(stderr, _("This version of openconnect was built without libproxy support\n"));
685 if (proxy && openconnect_set_http_proxy(vpninfo, strdup(proxy)))
690 openlog("openconnect", LOG_PID, LOG_DAEMON);
692 vpninfo->progress = syslog_progress;
695 memset(&sa, 0, sizeof(sa));
696 sa.sa_handler = handle_sigusr;
698 sigaction(SIGUSR1, &sa, NULL);
699 sigaction(SIGUSR2, &sa, NULL);
701 if (vpninfo->sslkey && do_passphrase_from_fsid)
702 openconnect_passphrase_from_fsid(vpninfo);
704 if (config_lookup_host(vpninfo, argv[optind]))
707 if (!vpninfo->hostname) {
708 char *url = strdup(argv[optind]);
710 if (openconnect_parse_url(vpninfo, url))
716 /* Historically, the path in the URL superseded the one in the
717 * --usergroup argument, just because of the order in which they
718 * were processed. Preserve that behaviour. */
719 if (urlpath && !vpninfo->urlpath) {
720 vpninfo->urlpath = urlpath;
729 if (!vpninfo->cookie && openconnect_obtain_cookie(vpninfo)) {
730 fprintf(stderr, _("Failed to obtain WebVPN cookie\n"));
735 printf("%s\n", vpninfo->cookie);
737 /* We use cookieonly=2 for 'print it and continue' */
740 if (make_cstp_connection(vpninfo)) {
741 fprintf(stderr, _("Creating SSL connection failed\n"));
745 if (setup_tun(vpninfo)) {
746 fprintf(stderr, _("Set up tun device failed\n"));
750 if (uid != getuid()) {
752 fprintf(stderr, _("Failed to set uid %ld\n"),
758 if (vpninfo->dtls_attempt_period && setup_dtls(vpninfo))
759 fprintf(stderr, _("Set up DTLS failed; using SSL instead\n"));
761 vpn_progress(vpninfo, PRG_INFO,
762 _("Connected %s as %s%s%s, using %s\n"), vpninfo->ifname,
763 vpninfo->vpn_addr?:"",
764 (vpninfo->vpn_addr6 && vpninfo->vpn_addr)?" + ":"",
765 vpninfo->vpn_addr6?:"",
766 (vpninfo->dtls_fd == -1) ?
767 (vpninfo->deflate ? "SSL + deflate" : "SSL")
770 if (!vpninfo->vpnc_script) {
771 vpn_progress(vpninfo, PRG_INFO,
772 _("No --script argument provided; DNS and routing are not configured\n"));
773 vpn_progress(vpninfo, PRG_INFO,
774 _("See http://www.infradead.org/openconnect/vpnc-script.html\n"));
780 /* Open the pidfile before forking, so we can report errors
781 more sanely. It's *possible* that we'll fail to write to
782 it, but very unlikely. */
783 if (pidfile != NULL) {
784 fp = fopen(pidfile, "w");
786 fprintf(stderr, _("Failed to open '%s' for write: %s\n"),
787 pidfile, strerror(errno));
791 if ((pid = fork())) {
793 fprintf(fp, "%d\n", pid);
796 vpn_progress(vpninfo, PRG_INFO,
797 _("Continuing in background; pid %d\n"),
804 vpn_mainloop(vpninfo);
810 static int write_new_config(void *_vpninfo, char *buf, int buflen)
812 struct openconnect_info *vpninfo = _vpninfo;
816 config_fd = open(vpninfo->xmlconfig, O_WRONLY|O_TRUNC|O_CREAT, 0644);
819 fprintf(stderr, _("Failed to open %s for write: %s\n"),
820 vpninfo->xmlconfig, strerror(err));
824 /* FIXME: We should actually write to a new tempfile, then rename */
825 if(write(config_fd, buf, buflen) != buflen) {
827 fprintf(stderr, _("Failed to write config to %s: %s\n"),
828 vpninfo->xmlconfig, strerror(err));
836 void write_progress(void *_vpninfo, int level, const char *fmt, ...)
838 FILE *outf = level ? stdout : stderr;
841 if (verbose >= level) {
843 vfprintf(outf, fmt, args);
850 void syslog_progress(void *_vpninfo, int level, const char *fmt, ...)
853 ANDROID_LOG_ERROR, /* PRG_ERR */
854 ANDROID_LOG_INFO, /* PRG_INFO */
855 ANDROID_LOG_DEBUG, /* PRG_DEBUG */
856 ANDROID_LOG_DEBUG /* PRG_TRACE */
860 if (verbose >= level) {
862 va_copy(args2, args);
863 __android_log_vprint(l[level], "openconnect", fmt, args);
864 /* Android wants it to stderr too, so the GUI can scrape
865 it and display it as well as going to syslog */
866 vfprintf(stderr, fmt, args2);
872 void syslog_progress(void *_vpninfo, int level, const char *fmt, ...)
874 int priority = level ? LOG_INFO : LOG_NOTICE;
877 if (verbose >= level) {
879 vsyslog(priority, fmt, args);
885 struct accepted_cert {
886 struct accepted_cert *next;
887 char fingerprint[SHA1_SIZE * 2 + 1];
891 static int validate_peer_cert(void *_vpninfo, OPENCONNECT_X509 *peer_cert,
894 struct openconnect_info *vpninfo = _vpninfo;
895 char fingerprint[SHA1_SIZE * 2 + 1];
896 struct accepted_cert *this;
902 ret = openconnect_get_cert_sha1(vpninfo, peer_cert, fingerprint);
906 for (this = accepted_certs; this; this = this->next) {
907 if (!strcasecmp(this->host, vpninfo->hostname) &&
908 !strcasecmp(this->fingerprint, fingerprint))
917 printf(_("\nCertificate from VPN server \"%s\" failed verification.\n"
918 "Reason: %s\n"), vpninfo->hostname, reason);
923 printf(_("Enter '%s' to accept, '%s' to abort; anything else to view: "),
925 if (!fgets(buf, sizeof(buf), stdin))
927 p = strchr(buf, '\n');
931 if (!strcasecmp(buf, _("yes"))) {
932 struct accepted_cert *newcert = malloc(sizeof(*newcert) +
933 strlen(vpninfo->hostname) + 1);
935 newcert->next = accepted_certs;
936 accepted_certs = newcert;
937 strcpy(newcert->fingerprint, fingerprint);
938 strcpy(newcert->host, vpninfo->hostname);
942 if (!strcasecmp(buf, _("no")))
945 details = openconnect_get_cert_details(vpninfo, peer_cert);
946 fputs(details, stderr);
948 fprintf(stderr, _("SHA1 fingerprint: %s\n"), fingerprint);
956 * = 0, when form was parsed and POST required
957 * = 1, when response was cancelled by user
959 static int process_auth_form(void *_vpninfo,
960 struct oc_auth_form *form)
962 struct openconnect_info *vpninfo = _vpninfo;
963 struct oc_form_opt *opt;
976 /* scan for select options first so they are displayed first */
977 for (opt = form->opts; opt; opt = opt->next) {
978 if (opt->type == OC_FORM_OPT_SELECT) {
979 struct oc_form_opt_select *select_opt = (void *)opt;
980 struct oc_choice *choice = NULL;
983 if (!select_opt->nr_choices)
986 if (vpninfo->authgroup &&
987 !strcmp(opt->name, "group_list")) {
988 for (i = 0; i < select_opt->nr_choices; i++) {
989 choice = &select_opt->choices[i];
991 if (!strcmp(vpninfo->authgroup,
993 opt->value = choice->name;
998 vpn_progress(vpninfo, PRG_ERR,
999 _("Auth choice \"%s\" not available\n"),
1000 vpninfo->authgroup);
1002 if (!opt->value && select_opt->nr_choices == 1) {
1003 choice = &select_opt->choices[0];
1004 opt->value = choice->name;
1010 printf("%s [", opt->label);
1011 for (i = 0; i < select_opt->nr_choices; i++) {
1012 choice = &select_opt->choices[i];
1016 printf("%s", choice->label);
1020 if (!fgets(response, sizeof(response), stdin) || !strlen(response))
1023 p = strchr(response, '\n');
1027 for (i = 0; i < select_opt->nr_choices; i++) {
1028 choice = &select_opt->choices[i];
1030 if (!strcmp(response, choice->label)) {
1031 select_opt->form.value = choice->name;
1035 if (!select_opt->form.value) {
1036 vpn_progress(vpninfo, PRG_ERR,
1037 _("Auth choice \"%s\" not valid\n"),
1044 for (opt = form->opts; opt; opt = opt->next) {
1046 if (opt->type == OC_FORM_OPT_TEXT) {
1047 if (vpninfo->username &&
1048 !strcmp(opt->name, "username")) {
1049 opt->value = strdup(vpninfo->username);
1053 opt->value=malloc(80);
1057 printf("%s", opt->label);
1059 if (!fgets(opt->value, 80, stdin) || !strlen(opt->value))
1062 p = strchr(opt->value, '\n');
1067 } else if (opt->type == OC_FORM_OPT_PASSWORD) {
1068 if (vpninfo->password &&
1069 !strcmp(opt->name, "password")) {
1070 opt->value = strdup(vpninfo->password);
1071 vpninfo->password = NULL;
1076 opt->value=malloc(80);
1080 printf("%s", opt->label);
1084 tcsetattr(0, TCSANOW, &t);
1086 p = fgets(opt->value, 80, stdin);
1089 tcsetattr(0, TCSANOW, &t);
1092 if (!p || !strlen(opt->value))
1095 p = strchr(opt->value, '\n');
1103 if (vpninfo->password) {
1104 free(vpninfo->password);
1105 vpninfo->password = NULL;