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>
56 #include "openconnect-internal.h"
58 static int write_new_config(void *_vpninfo,
59 char *buf, int buflen);
60 static void write_progress(void *_vpninfo,
61 int level, const char *fmt, ...);
62 static void syslog_progress(void *_vpninfo,
63 int level, const char *fmt, ...);
64 static int validate_peer_cert(void *_vpninfo,
65 OPENCONNECT_X509 *peer_cert,
67 static int process_auth_form(void *_vpninfo,
68 struct oc_auth_form *form);
69 static void init_stoken(struct openconnect_info *vpninfo,
70 const char *token_str);
72 /* A sanity check that the openconnect executable is running against a
73 library of the same version */
74 #define openconnect_version_str openconnect_binary_version
76 #undef openconnect_version_str
78 int verbose = PRG_INFO;
80 int do_passphrase_from_fsid;
86 OPT_AUTHENTICATE = 0x100,
98 OPT_KEY_PASSWORD_FROM_FSID,
102 OPT_NO_HTTP_KEEPALIVE,
106 OPT_PASSWORD_ON_STDIN,
108 OPT_RECONNECT_TIMEOUT,
118 * The 'name' field in Solaris 'struct option' lacks the 'const', and causes
119 * lots of warnings unless we cast it... https://www.illumos.org/issues/1881
121 #define OPTION(name, arg, abbrev) {(char *)name, arg, NULL, abbrev}
123 #define OPTION(name, arg, abbrev) {name, arg, NULL, abbrev}
126 static struct option long_options[] = {
127 OPTION("background", 0, 'b'),
128 OPTION("pid-file", 1, OPT_PIDFILE),
129 OPTION("certificate", 1, 'c'),
130 OPTION("sslkey", 1, 'k'),
131 OPTION("cookie", 1, 'C'),
132 OPTION("deflate", 0, 'd'),
133 OPTION("no-deflate", 0, 'D'),
134 OPTION("cert-expire-warning", 1, 'e'),
135 OPTION("usergroup", 1, 'g'),
136 OPTION("help", 0, 'h'),
137 OPTION("interface", 1, 'i'),
138 OPTION("mtu", 1, 'm'),
139 OPTION("base-mtu", 1, OPT_BASEMTU),
140 OPTION("setuid", 1, 'U'),
141 OPTION("script", 1, 's'),
142 OPTION("script-tun", 0, 'S'),
143 OPTION("syslog", 0, 'l'),
144 OPTION("key-password", 1, 'p'),
145 OPTION("proxy", 1, 'P'),
146 OPTION("user", 1, 'u'),
147 OPTION("verbose", 0, 'v'),
148 OPTION("version", 0, 'V'),
149 OPTION("cafile", 1, OPT_CAFILE),
150 OPTION("config", 1, OPT_CONFIGFILE),
151 OPTION("no-dtls", 0, OPT_NO_DTLS),
152 OPTION("authenticate", 0, OPT_AUTHENTICATE),
153 OPTION("cookieonly", 0, OPT_COOKIEONLY),
154 OPTION("printcookie", 0, OPT_PRINTCOOKIE),
155 OPTION("quiet", 0, 'q'),
156 OPTION("queue-len", 1, 'Q'),
157 OPTION("xmlconfig", 1, 'x'),
158 OPTION("cookie-on-stdin", 0, OPT_COOKIE_ON_STDIN),
159 OPTION("passwd-on-stdin", 0, OPT_PASSWORD_ON_STDIN),
160 OPTION("no-passwd", 0, OPT_NO_PASSWD),
161 OPTION("reconnect-timeout", 1, OPT_RECONNECT_TIMEOUT),
162 OPTION("dtls-ciphers", 1, OPT_DTLS_CIPHERS),
163 OPTION("authgroup", 1, OPT_AUTHGROUP),
164 OPTION("servercert", 1, OPT_SERVERCERT),
165 OPTION("key-password-from-fsid", 0, OPT_KEY_PASSWORD_FROM_FSID),
166 OPTION("useragent", 1, OPT_USERAGENT),
167 OPTION("csd-user", 1, OPT_CSD_USER),
168 OPTION("csd-wrapper", 1, OPT_CSD_WRAPPER),
169 OPTION("disable-ipv6", 0, OPT_DISABLE_IPV6),
170 OPTION("no-proxy", 0, OPT_NO_PROXY),
171 OPTION("libproxy", 0, OPT_LIBPROXY),
172 OPTION("no-http-keepalive", 0, OPT_NO_HTTP_KEEPALIVE),
173 OPTION("no-cert-check", 0, OPT_NO_CERT_CHECK),
174 OPTION("force-dpd", 1, OPT_FORCE_DPD),
175 OPTION("non-inter", 0, OPT_NON_INTER),
176 OPTION("dtls-local-port", 1, OPT_DTLS_LOCAL_PORT),
177 OPTION("stoken", 2, OPT_STOKEN),
181 static void helpmessage(void)
183 printf(_("For assistance with OpenConnect, please see the web page at\n"
184 " http://www.infradead.org/openconnect/mail.html\n"));
187 static void print_build_opts(void)
189 const char *comma = ", ", *sep = comma + 1;
191 #if defined (OPENCONNECT_OPENSSL)
192 printf(_("Using OpenSSL. Features present:"));
193 #elif defined (OPENCONNECT_GNUTLS)
194 printf(_("Using GnuTLS. Features present:"));
197 if (openconnect_has_tss_blob_support()) {
198 printf("%sTPM", sep);
201 #if defined (OPENCONNECT_OPENSSL) && defined (HAVE_ENGINE)
203 printf("%sTPM (%s)", sep, _("OpenSSL ENGINE not present"));
207 if (openconnect_has_pkcs11_support()) {
208 printf("%sPKCS#11", sep);
211 if (openconnect_has_stoken_support()) {
212 printf("%sSoftware token", sep);
217 printf("%sDTLS", sep);
218 #if defined (OPENCONNECT_GNUTLS) && defined (DTLS_OPENSSL)
219 printf(" (%s)", _("using OpenSSL"));
223 printf(_("\nWARNING: No DTLS support in this binary. Performance will be impaired.\n"));
227 static void usage(void)
229 printf(_("Usage: openconnect [options] <server>\n"));
230 printf(_("Open client for Cisco AnyConnect VPN, version %s\n\n"), openconnect_version_str);
232 printf(" --config=CONFIGFILE %s\n", _("Read options from config file"));
233 printf(" -b, --background %s\n", _("Continue in background after startup"));
234 printf(" --pid-file=PIDFILE %s\n", _("Write the daemon's PID to this file"));
235 printf(" -c, --certificate=CERT %s\n", _("Use SSL client certificate CERT"));
236 printf(" -e, --cert-expire-warning=DAYS %s\n", _("Warn when certificate lifetime < DAYS"));
237 printf(" -k, --sslkey=KEY %s\n", _("Use SSL private key file KEY"));
238 printf(" -C, --cookie=COOKIE %s\n", _("Use WebVPN cookie COOKIE"));
239 printf(" --cookie-on-stdin %s\n", _("Read cookie from standard input"));
240 printf(" -d, --deflate %s\n", _("Enable compression (default)"));
241 printf(" -D, --no-deflate %s\n", _("Disable compression"));
242 printf(" --force-dpd=INTERVAL %s\n", _("Set minimum Dead Peer Detection interval"));
243 printf(" -g, --usergroup=GROUP %s\n", _("Set login usergroup"));
244 printf(" -h, --help %s\n", _("Display help text"));
245 printf(" -i, --interface=IFNAME %s\n", _("Use IFNAME for tunnel interface"));
246 printf(" -l, --syslog %s\n", _("Use syslog for progress messages"));
247 printf(" -U, --setuid=USER %s\n", _("Drop privileges after connecting"));
248 printf(" --csd-user=USER %s\n", _("Drop privileges during CSD execution"));
249 printf(" --csd-wrapper=SCRIPT %s\n", _("Run SCRIPT instead of CSD binary"));
250 printf(" -m, --mtu=MTU %s\n", _("Request MTU from server"));
251 printf(" --base-mtu=MTU %s\n", _("Indicate path MTU to/from server"));
252 printf(" -p, --key-password=PASS %s\n", _("Set key passphrase or TPM SRK PIN"));
253 printf(" --key-password-from-fsid %s\n", _("Key passphrase is fsid of file system"));
254 printf(" -P, --proxy=URL %s\n", _("Set proxy server"));
255 printf(" --no-proxy %s\n", _("Disable proxy"));
256 printf(" --libproxy %s\n", _("Use libproxy to automatically configure proxy"));
258 printf(" %s\n", _("(NOTE: libproxy disabled in this build)"));
260 printf(" -q, --quiet %s\n", _("Less output"));
261 printf(" -Q, --queue-len=LEN %s\n", _("Set packet queue limit to LEN pkts"));
262 printf(" -s, --script=SCRIPT %s\n", _("Shell command line for using a vpnc-compatible config script"));
263 printf(" %s: \"%s\"\n", _("default"), DEFAULT_VPNCSCRIPT);
264 printf(" -S, --script-tun %s\n", _("Pass traffic to 'script' program, not tun"));
265 printf(" -u, --user=NAME %s\n", _("Set login username"));
266 printf(" -V, --version %s\n", _("Report version number"));
267 printf(" -v, --verbose %s\n", _("More output"));
268 printf(" -x, --xmlconfig=CONFIG %s\n", _("XML config file"));
269 printf(" --authgroup=GROUP %s\n", _("Choose authentication login selection"));
270 printf(" --authenticate %s\n", _("Authenticate only and print login info"));
271 printf(" --cookieonly %s\n", _("Fetch webvpn cookie only; don't connect"));
272 printf(" --printcookie %s\n", _("Print webvpn cookie before connecting"));
273 printf(" --cafile=FILE %s\n", _("Cert file for server verification"));
274 printf(" --disable-ipv6 %s\n", _("Do not ask for IPv6 connectivity"));
275 printf(" --dtls-ciphers=LIST %s\n", _("OpenSSL ciphers to support for DTLS"));
276 printf(" --no-dtls %s\n", _("Disable DTLS"));
277 printf(" --no-http-keepalive %s\n", _("Disable HTTP connection re-use"));
278 printf(" --no-passwd %s\n", _("Disable password/SecurID authentication"));
279 printf(" --no-cert-check %s\n", _("Do not require server SSL cert to be valid"));
280 printf(" --non-inter %s\n", _("Do not expect user input; exit if it is required"));
281 printf(" --passwd-on-stdin %s\n", _("Read password from standard input"));
282 printf(" --stoken[=TOKENSTRING] %s\n", _("Use software token to generate password"));
283 #ifndef LIBSTOKEN_HDR
284 printf(" %s\n", _("(NOTE: libstoken disabled in this build)"));
286 printf(" --reconnect-timeout %s\n", _("Connection retry timeout in seconds"));
287 printf(" --servercert=FINGERPRINT %s\n", _("Server's certificate SHA1 fingerprint"));
288 printf(" --useragent=STRING %s\n", _("HTTP header User-Agent: field"));
289 printf(" --dtls-local-port=PORT %s\n", _("Set local port for DTLS datagrams"));
296 static void read_stdin(char **string)
298 char *c = malloc(100);
300 fprintf(stderr, _("Allocation failure for string from stdin\n"));
303 if (!fgets(c, 100, stdin)) {
304 perror(_("fgets (stdin)"));
310 c = strchr(*string, '\n');
314 static void handle_sigusr(int sig)
318 else if (sig == SIGUSR2)
322 static FILE *config_file = NULL;
323 static int config_line_num = 0;
325 /* There are three ways to handle config_arg:
327 * 1. We only care about it transiently and it can be lost entirely
328 * (e.g. vpninfo->reconnect_timeout = atoi(config_arg);
329 * 2. We need to keep it, but it's a static string and will never be freed
330 * so when it's part of argv[] we can use it in place, but when it comes
331 * from a file we have to strdup() because otherwise it'll be overwritten.
332 * For this we use the keep_config_arg() macro below.
333 * 3. It may be freed during normal operation, so we have to use strdup()
334 * even when it's an option from argv[]. (e.g. vpninfo->cert_password).
336 #define keep_config_arg() (config_file && config_arg ? strdup(config_arg) : config_arg)
338 static int next_option(int argc, char **argv, char **config_arg)
340 /* These get re-used */
341 static char *line_buf = NULL;
342 static size_t line_size = 0;
352 opt = getopt_long(argc, argv,
353 "bC:c:e:Ddg:hi:k:lp:P:Q:qSs:U:u:Vvx:",
356 *config_arg = optarg;
360 llen = getline(&line_buf, &line_size, config_file);
362 if (feof(config_file)) {
367 fprintf(stderr, _("Failed to get line from config file: %s\n"),
373 /* Strip the trailing newline (coping with DOS newlines) */
374 if (llen && line[llen-1] == '\n')
376 if (llen && line[llen-1] == '\r')
379 /* Skip and leading whitespace */
380 while (line[0] == ' ' || line[0] == '\t' || line[0] == '\r')
383 /* Ignore comments and empty lines */
384 if (!line[0] || line[0] == '#') {
389 /* Try to match on a known option... naïvely. This could be improved. */
390 for (this = long_options; this->name; this++) {
391 optlen = strlen(this->name);
392 /* If the option isn't followed by whitespace or NUL, or
393 perhaps an equals sign if the option takes an argument,
394 then it's not a match */
395 if (!strncmp(this->name, line, optlen) &&
396 (!line[optlen] || line[optlen] == ' ' || line[optlen] == '\t' ||
397 line[optlen] == '='))
403 for (l = line; *l && *l != ' ' && *l != '\t'; l++)
407 fprintf(stderr, _("Unrecognised option at line %d: '%s'\n"),
408 config_line_num, line);
412 while (*line == ' ' || *line == '\t' ||
413 (*line == '=' && this->has_arg && !ate_equals && ++ate_equals))
416 if (!this->has_arg && *line) {
417 fprintf(stderr, _("Option '%s' does not take an argument at line %d\n"),
418 this->name, config_line_num);
420 } else if (this->has_arg == 1 && !*line) {
421 fprintf(stderr, _("Option '%s' requires an argument at line %d\n"),
422 this->name, config_line_num);
424 } else if (this->has_arg == 2 && !*line) {
434 int main(int argc, char **argv)
436 struct openconnect_info *vpninfo;
437 struct utsname utsbuf;
440 char *urlpath = NULL;
441 char *proxy = getenv("https_proxy");
443 uid_t uid = getuid();
445 char *pidfile = NULL;
449 char *token_str = NULL;
452 bindtextdomain("openconnect", LOCALEDIR);
453 setlocale(LC_ALL, "");
456 if (strcmp(openconnect_version_str, openconnect_binary_version)) {
457 fprintf(stderr, _("WARNING: This version of openconnect is %s but\n"
458 " the libopenconnect library is %s\n"),
459 openconnect_binary_version, openconnect_version_str);
462 openconnect_init_ssl();
464 vpninfo = malloc(sizeof(*vpninfo));
466 fprintf(stderr, _("Failed to allocate vpninfo structure\n"));
469 memset(vpninfo, 0, sizeof(*vpninfo));
471 /* Set up some defaults */
472 vpninfo->tun_fd = vpninfo->ssl_fd = vpninfo->dtls_fd = vpninfo->new_dtls_fd = -1;
473 vpninfo->useragent = openconnect_create_useragent("Open AnyConnect VPN Agent");
475 vpninfo->deflate = 1;
476 vpninfo->dtls_attempt_period = 60;
477 vpninfo->max_qlen = 10;
478 vpninfo->reconnect_interval = RECONNECT_INTERVAL_MIN;
479 vpninfo->reconnect_timeout = 300;
480 vpninfo->uid_csd = 0;
481 /* We could let them override this on the command line some day, perhaps */
483 vpninfo->csd_xmltag = "csdMac";
485 vpninfo->csd_xmltag = "csdLinux";
487 vpninfo->uid_csd = 0;
488 vpninfo->uid_csd_given = 0;
489 vpninfo->validate_peer_cert = validate_peer_cert;
490 vpninfo->process_auth_form = process_auth_form;
491 vpninfo->cbdata = vpninfo;
492 vpninfo->cert_expire_warning = 60 * 86400;
493 vpninfo->vpnc_script = DEFAULT_VPNCSCRIPT;
494 vpninfo->cancel_fd = -1;
497 vpninfo->localname = utsbuf.nodename;
499 vpninfo->localname = "localhost";
501 while ((opt = next_option(argc, argv, &config_arg))) {
509 fprintf(stderr, _("Cannot use 'config' option inside config file\n"));
512 config_file = fopen(config_arg, "r");
514 fprintf(stderr, _("Cannot open config file '%s': %s\n"),
515 config_arg, strerror(errno));
519 /* The next option will come from the file... */
522 vpninfo->cafile = keep_config_arg();
525 pidfile = keep_config_arg();
528 vpninfo->servercert = keep_config_arg();
531 vpninfo->dtls_attempt_period = 0;
536 case OPT_PRINTCOOKIE:
539 case OPT_AUTHENTICATE:
542 case OPT_COOKIE_ON_STDIN:
543 read_stdin(&vpninfo->cookie);
544 /* If the cookie is empty, ignore it */
545 if (! *vpninfo->cookie) {
546 vpninfo->cookie = NULL;
549 case OPT_PASSWORD_ON_STDIN:
550 read_stdin(&vpninfo->password);
553 vpninfo->nopasswd = 1;
558 case OPT_RECONNECT_TIMEOUT:
559 vpninfo->reconnect_timeout = atoi(config_arg);
561 case OPT_DTLS_CIPHERS:
562 vpninfo->dtls_ciphers = keep_config_arg();
565 vpninfo->authgroup = keep_config_arg();
571 vpninfo->cookie = keep_config_arg();
574 vpninfo->cert = strdup(config_arg);
577 vpninfo->cert_expire_warning = 86400 * atoi(config_arg);
580 vpninfo->sslkey = strdup(config_arg);
583 vpninfo->deflate = 1;
586 vpninfo->deflate = 0;
590 urlpath = strdup(config_arg);
595 vpninfo->ifname = keep_config_arg();
601 vpninfo->reqmtu = atol(config_arg);
602 if (vpninfo->reqmtu < 576) {
603 fprintf(stderr, _("MTU %d too small\n"), vpninfo->reqmtu);
604 vpninfo->reqmtu = 576;
608 vpninfo->basemtu = atol(config_arg);
609 if (vpninfo->basemtu < 576) {
610 fprintf(stderr, _("MTU %d too small\n"), vpninfo->basemtu);
611 vpninfo->basemtu = 576;
615 vpninfo->cert_password = strdup(config_arg);
618 proxy = keep_config_arg();
629 case OPT_NO_HTTP_KEEPALIVE:
631 _("Disabling all HTTP connection re-use due to --no-http-keepalive option.\n"
632 "If this helps, please report to <openconnect-devel@lists.infradead.org>.\n"));
633 vpninfo->no_http_keepalive = 1;
635 case OPT_NO_CERT_CHECK:
639 vpninfo->vpnc_script = keep_config_arg();
642 vpninfo->script_tun = 1;
645 vpninfo->username = keep_config_arg();
649 uid = strtol(config_arg, &strend, 0);
651 struct passwd *pw = getpwnam(config_arg);
653 fprintf(stderr, _("Invalid user \"%s\"\n"),
663 vpninfo->uid_csd = strtol(config_arg, &strend, 0);
665 struct passwd *pw = getpwnam(config_arg);
667 fprintf(stderr, _("Invalid user \"%s\"\n"),
671 vpninfo->uid_csd = pw->pw_uid;
673 vpninfo->uid_csd_given = 1;
676 case OPT_CSD_WRAPPER:
677 vpninfo->csd_wrapper = keep_config_arg();
679 case OPT_DISABLE_IPV6:
680 vpninfo->disable_ipv6 = 1;
683 vpninfo->max_qlen = atol(config_arg);
684 if (!vpninfo->max_qlen) {
685 fprintf(stderr, _("Queue length zero not permitted; using 1\n"));
686 vpninfo->max_qlen = 1;
696 printf(_("OpenConnect version %s\n"), openconnect_version_str);
700 vpninfo->xmlconfig = keep_config_arg();
701 vpninfo->write_new_config = write_new_config;
703 case OPT_KEY_PASSWORD_FROM_FSID:
704 do_passphrase_from_fsid = 1;
707 free(vpninfo->useragent);
708 vpninfo->useragent = strdup(config_arg);
711 vpninfo->dtls_times.dpd = vpninfo->ssl_times.dpd = atoi(config_arg);
713 case OPT_DTLS_LOCAL_PORT:
714 vpninfo->dtls_local_port = atoi(config_arg);
718 token_str = keep_config_arg();
725 if (optind < argc - 1) {
726 fprintf(stderr, _("Too many arguments on command line\n"));
728 } else if (optind > argc - 1) {
729 fprintf(stderr, _("No server specified\n"));
733 if (!vpninfo->sslkey)
734 vpninfo->sslkey = vpninfo->cert;
736 vpninfo->progress = write_progress;
740 vpninfo->proxy_factory = px_proxy_factory_new();
742 fprintf(stderr, _("This version of openconnect was built without libproxy support\n"));
748 init_stoken(vpninfo, token_str);
750 if (proxy && openconnect_set_http_proxy(vpninfo, strdup(proxy)))
755 openlog("openconnect", LOG_PID, LOG_DAEMON);
757 vpninfo->progress = syslog_progress;
760 memset(&sa, 0, sizeof(sa));
761 sa.sa_handler = handle_sigusr;
763 sigaction(SIGUSR1, &sa, NULL);
764 sigaction(SIGUSR2, &sa, NULL);
766 if (vpninfo->sslkey && do_passphrase_from_fsid)
767 openconnect_passphrase_from_fsid(vpninfo);
769 if (config_lookup_host(vpninfo, argv[optind]))
772 if (!vpninfo->hostname) {
773 char *url = strdup(argv[optind]);
775 if (openconnect_parse_url(vpninfo, url))
781 /* Historically, the path in the URL superseded the one in the
782 * --usergroup argument, just because of the order in which they
783 * were processed. Preserve that behaviour. */
784 if (urlpath && !vpninfo->urlpath) {
785 vpninfo->urlpath = urlpath;
794 if (!vpninfo->cookie && openconnect_obtain_cookie(vpninfo)) {
795 if (vpninfo->csd_scriptname) {
796 unlink(vpninfo->csd_scriptname);
797 vpninfo->csd_scriptname = NULL;
799 fprintf(stderr, _("Failed to obtain WebVPN cookie\n"));
803 if (cookieonly == 3) {
805 printf("COOKIE='%s'\n", vpninfo->cookie);
806 printf("HOST='%s'\n", vpninfo->hostname);
807 if (vpninfo->peer_cert) {
808 char buf[41] = {0, };
809 openconnect_get_cert_sha1(vpninfo, vpninfo->peer_cert, buf);
810 printf("FINGERPRINT='%s'\n", buf);
812 openconnect_vpninfo_free(vpninfo);
814 } else if (cookieonly) {
815 printf("%s\n", vpninfo->cookie);
816 if (cookieonly == 1) {
817 /* We use cookieonly=2 for 'print it and continue' */
818 openconnect_vpninfo_free(vpninfo);
822 if (make_cstp_connection(vpninfo)) {
823 fprintf(stderr, _("Creating SSL connection failed\n"));
827 if (setup_tun(vpninfo)) {
828 fprintf(stderr, _("Set up tun device failed\n"));
832 if (uid != getuid()) {
834 fprintf(stderr, _("Failed to set uid %ld\n"),
840 if (vpninfo->dtls_attempt_period && setup_dtls(vpninfo))
841 fprintf(stderr, _("Set up DTLS failed; using SSL instead\n"));
843 vpn_progress(vpninfo, PRG_INFO,
844 _("Connected %s as %s%s%s, using %s\n"), vpninfo->ifname,
845 vpninfo->vpn_addr?:"",
846 (vpninfo->vpn_addr6 && vpninfo->vpn_addr)?" + ":"",
847 vpninfo->vpn_addr6?:"",
848 (vpninfo->dtls_fd == -1) ?
849 (vpninfo->deflate ? "SSL + deflate" : "SSL")
852 if (!vpninfo->vpnc_script) {
853 vpn_progress(vpninfo, PRG_INFO,
854 _("No --script argument provided; DNS and routing are not configured\n"));
855 vpn_progress(vpninfo, PRG_INFO,
856 _("See http://www.infradead.org/openconnect/vpnc-script.html\n"));
862 /* Open the pidfile before forking, so we can report errors
863 more sanely. It's *possible* that we'll fail to write to
864 it, but very unlikely. */
865 if (pidfile != NULL) {
866 fp = fopen(pidfile, "w");
868 fprintf(stderr, _("Failed to open '%s' for write: %s\n"),
869 pidfile, strerror(errno));
873 if ((pid = fork())) {
875 fprintf(fp, "%d\n", pid);
878 vpn_progress(vpninfo, PRG_INFO,
879 _("Continuing in background; pid %d\n"),
886 vpn_mainloop(vpninfo);
892 static int write_new_config(void *_vpninfo, char *buf, int buflen)
894 struct openconnect_info *vpninfo = _vpninfo;
898 config_fd = open(vpninfo->xmlconfig, O_WRONLY|O_TRUNC|O_CREAT, 0644);
901 fprintf(stderr, _("Failed to open %s for write: %s\n"),
902 vpninfo->xmlconfig, strerror(err));
906 /* FIXME: We should actually write to a new tempfile, then rename */
907 if(write(config_fd, buf, buflen) != buflen) {
909 fprintf(stderr, _("Failed to write config to %s: %s\n"),
910 vpninfo->xmlconfig, strerror(err));
919 void write_progress(void *_vpninfo, int level, const char *fmt, ...)
921 FILE *outf = level ? stdout : stderr;
927 if (verbose >= level) {
929 vfprintf(outf, fmt, args);
936 void syslog_progress(void *_vpninfo, int level, const char *fmt, ...)
939 ANDROID_LOG_ERROR, /* PRG_ERR */
940 ANDROID_LOG_INFO, /* PRG_INFO */
941 ANDROID_LOG_DEBUG, /* PRG_DEBUG */
942 ANDROID_LOG_DEBUG /* PRG_TRACE */
946 if (verbose >= level) {
948 va_copy(args2, args);
949 __android_log_vprint(l[level], "openconnect", fmt, args);
950 /* Android wants it to stderr too, so the GUI can scrape
951 it and display it as well as going to syslog */
952 vfprintf(stderr, fmt, args2);
958 void syslog_progress(void *_vpninfo, int level, const char *fmt, ...)
960 int priority = level ? LOG_INFO : LOG_NOTICE;
963 if (verbose >= level) {
965 vsyslog(priority, fmt, args);
971 struct accepted_cert {
972 struct accepted_cert *next;
973 char fingerprint[SHA1_SIZE * 2 + 1];
977 static int validate_peer_cert(void *_vpninfo, OPENCONNECT_X509 *peer_cert,
980 struct openconnect_info *vpninfo = _vpninfo;
981 char fingerprint[SHA1_SIZE * 2 + 1];
982 struct accepted_cert *this;
988 ret = openconnect_get_cert_sha1(vpninfo, peer_cert, fingerprint);
992 for (this = accepted_certs; this; this = this->next) {
993 if (!strcasecmp(this->host, vpninfo->hostname) &&
994 !strcasecmp(this->fingerprint, fingerprint))
1003 fprintf(stderr, _("\nCertificate from VPN server \"%s\" failed verification.\n"
1004 "Reason: %s\n"), vpninfo->hostname, reason);
1009 fprintf(stderr, _("Enter '%s' to accept, '%s' to abort; anything else to view: "),
1011 if (!fgets(buf, sizeof(buf), stdin))
1013 p = strchr(buf, '\n');
1017 if (!strcasecmp(buf, _("yes"))) {
1018 struct accepted_cert *newcert = malloc(sizeof(*newcert) +
1019 strlen(vpninfo->hostname) + 1);
1021 newcert->next = accepted_certs;
1022 accepted_certs = newcert;
1023 strcpy(newcert->fingerprint, fingerprint);
1024 strcpy(newcert->host, vpninfo->hostname);
1028 if (!strcasecmp(buf, _("no")))
1031 details = openconnect_get_cert_details(vpninfo, peer_cert);
1032 fputs(details, stderr);
1034 fprintf(stderr, _("SHA1 fingerprint: %s\n"), fingerprint);
1041 * = 0, when form was parsed and POST required
1042 * = 1, when response was cancelled by user
1044 static int process_auth_form(void *_vpninfo,
1045 struct oc_auth_form *form)
1047 struct openconnect_info *vpninfo = _vpninfo;
1048 struct oc_form_opt *opt;
1049 char response[1024];
1053 fprintf(stderr, "%s\n", form->banner);
1056 fprintf(stderr, "%s\n", form->error);
1059 fprintf(stderr, "%s\n", form->message);
1061 /* scan for select options first so they are displayed first */
1062 for (opt = form->opts; opt; opt = opt->next) {
1063 if (opt->type == OC_FORM_OPT_SELECT) {
1064 struct oc_form_opt_select *select_opt = (void *)opt;
1065 struct oc_choice *choice = NULL;
1068 if (!select_opt->nr_choices)
1071 if (vpninfo->authgroup &&
1072 !strcmp(opt->name, "group_list")) {
1073 for (i = 0; i < select_opt->nr_choices; i++) {
1074 choice = &select_opt->choices[i];
1076 if (!strcmp(vpninfo->authgroup,
1078 opt->value = choice->name;
1083 vpn_progress(vpninfo, PRG_ERR,
1084 _("Auth choice \"%s\" not available\n"),
1085 vpninfo->authgroup);
1087 if (!opt->value && select_opt->nr_choices == 1) {
1088 choice = &select_opt->choices[0];
1089 opt->value = choice->name;
1096 vpn_progress(vpninfo, PRG_ERR,
1097 _("User input required in non-interactive mode\n"));
1100 fprintf(stderr, "%s [", opt->label);
1101 for (i = 0; i < select_opt->nr_choices; i++) {
1102 choice = &select_opt->choices[i];
1104 fprintf(stderr, "|");
1106 fprintf(stderr, "%s", choice->label);
1108 fprintf(stderr, "]:");
1111 if (!fgets(response, sizeof(response), stdin) || !strlen(response))
1114 p = strchr(response, '\n');
1118 for (i = 0; i < select_opt->nr_choices; i++) {
1119 choice = &select_opt->choices[i];
1121 if (!strcmp(response, choice->label)) {
1122 select_opt->form.value = choice->name;
1126 if (!select_opt->form.value) {
1127 vpn_progress(vpninfo, PRG_ERR,
1128 _("Auth choice \"%s\" not valid\n"),
1135 for (opt = form->opts; opt; opt = opt->next) {
1137 if (opt->type == OC_FORM_OPT_TEXT) {
1138 if (vpninfo->username &&
1139 !strcmp(opt->name, "username")) {
1140 opt->value = strdup(vpninfo->username);
1143 } else if (non_inter) {
1144 vpn_progress(vpninfo, PRG_ERR,
1145 _("User input required in non-interactive mode\n"));
1148 opt->value=malloc(80);
1152 fprintf(stderr, "%s", opt->label);
1155 if (!fgets(opt->value, 80, stdin) || !strlen(opt->value))
1158 p = strchr(opt->value, '\n');
1163 } else if (opt->type == OC_FORM_OPT_PASSWORD) {
1164 if (vpninfo->password &&
1165 !strcmp(opt->name, "password")) {
1166 opt->value = vpninfo->password;
1167 vpninfo->password = NULL;
1170 } else if (non_inter) {
1171 vpn_progress(vpninfo, PRG_ERR,
1172 _("User input required in non-interactive mode\n"));
1176 opt->value=malloc(80);
1180 fprintf(stderr, "%s", opt->label);
1185 tcsetattr(0, TCSANOW, &t);
1187 p = fgets(opt->value, 80, stdin);
1190 tcsetattr(0, TCSANOW, &t);
1191 fprintf(stderr, "\n");
1193 if (!p || !strlen(opt->value))
1196 p = strchr(opt->value, '\n');
1204 if (vpninfo->password) {
1205 free(vpninfo->password);
1206 vpninfo->password = NULL;
1212 for (opt = form->opts; opt; opt = opt->next) {
1213 if (opt->value && (opt->type == OC_FORM_OPT_TEXT ||
1214 opt->type == OC_FORM_OPT_PASSWORD)) {
1222 static void init_stoken(struct openconnect_info *vpninfo,
1223 const char *token_str)
1225 int ret = openconnect_set_stoken_mode(vpninfo, 1, token_str);
1231 fprintf(stderr, _("Soft token string is invalid\n"));
1234 fprintf(stderr, _("Can't open ~/.stokenrc file\n"));
1237 fprintf(stderr, _("OpenConnect was not built with soft token support\n"));
1240 fprintf(stderr, _("General failure in libstoken\n"));