3e074d54ec0ce25443d9e0626852b08faa6b5a83
[packages.git] / net / openconnect / patches / 100-passwd_file.patch
1 --- a/main.c
2 +++ b/main.c
3 @@ -77,6 +77,7 @@ enum {
4         OPT_CAFILE,
5         OPT_COOKIEONLY,
6         OPT_COOKIE_ON_STDIN,
7 +       OPT_COOKIE_FILE,
8         OPT_CSD_USER,
9         OPT_CSD_WRAPPER,
10         OPT_DISABLE_IPV6,
11 @@ -91,6 +92,7 @@ enum {
12         OPT_NO_PROXY,
13         OPT_PIDFILE,
14         OPT_PASSWORD_ON_STDIN,
15 +       OPT_PASSWORD_FILE,
16         OPT_PRINTCOOKIE,
17         OPT_RECONNECT_TIMEOUT,
18         OPT_SERVERCERT,
19 @@ -139,7 +141,9 @@ static struct option long_options[] = {
20         OPTION("queue-len", 1, 'Q'),
21         OPTION("xmlconfig", 1, 'x'),
22         OPTION("cookie-on-stdin", 0, OPT_COOKIE_ON_STDIN),
23 +       OPTION("cookie-file", 1, OPT_COOKIE_FILE),
24         OPTION("passwd-on-stdin", 0, OPT_PASSWORD_ON_STDIN),
25 +       OPTION("passwd-file", 1, OPT_PASSWORD_FILE),
26         OPTION("no-passwd", 0, OPT_NO_PASSWD),
27         OPTION("reconnect-timeout", 1, OPT_RECONNECT_TIMEOUT),
28         OPTION("dtls-ciphers", 1, OPT_DTLS_CIPHERS),
29 @@ -177,6 +181,7 @@ static void usage(void)
30         printf("  -K, --key-type=TYPE             %s\n", _("Private key type (PKCS#12 / TPM / PEM)"));
31         printf("  -C, --cookie=COOKIE             %s\n", _("Use WebVPN cookie COOKIE"));
32         printf("      --cookie-on-stdin           %s\n", _("Read cookie from standard input"));
33 +       printf("      --cookie-file=FILE          %s\n", _("Read cookie from a file"));
34         printf("  -d, --deflate                   %s\n", _("Enable compression (default)"));
35         printf("  -D, --no-deflate                %s\n", _("Disable compression"));
36         printf("      --force-dpd=INTERVAL        %s\n", _("Set minimum Dead Peer Detection interval"));
37 @@ -217,6 +222,7 @@ static void usage(void)
38         printf("      --no-cert-check             %s\n", _("Do not require server SSL cert to be valid"));
39         printf("      --non-inter                 %s\n", _("Do not expect user input; exit if it is required"));
40         printf("      --passwd-on-stdin           %s\n", _("Read password from standard input"));
41 +       printf("      --passwd-file=FILE          %s\n", _("Read password from a file"));
42         printf("      --reconnect-timeout         %s\n", _("Connection retry timeout in seconds"));
43         printf("      --servercert=FINGERPRINT    %s\n", _("Server's certificate SHA1 fingerprint"));
44         printf("      --useragent=STRING          %s\n", _("HTTP header User-Agent: field"));
45 @@ -226,15 +232,28 @@ static void usage(void)
46         exit(1);
47  }
48  
49 -static void read_stdin(char **string)
50 +static void read_file(const char *file, char **string)
51  {
52         char *c = malloc(100);
53 +       FILE *f;
54 +
55 +       if (file) {
56 +               f = fopen(file, "r");
57 +               if (!f) {
58 +                       fprintf(stderr, _("Failed to open password file\n"));
59 +                       exit(1);
60 +               }
61 +       } else {
62 +               file = "stdin";
63 +               f = stdin;
64 +       }
65 +
66         if (!c) {
67 -               fprintf(stderr, _("Allocation failure for string from stdin\n"));
68 +               fprintf(stderr, _("Allocation failure for string from %s\n"), file);
69                 exit(1);
70         }
71 -       if (!fgets(c, 100, stdin)) {
72 -               perror(_("fgets (stdin)"));
73 +       if (!fgets(c, 100, f)) {
74 +               perror(_("fgets"));
75                 exit(1);
76         }
77  
78 @@ -332,14 +351,20 @@ int main(int argc, char **argv)
79                         cookieonly = 2;
80                         break;
81                 case OPT_COOKIE_ON_STDIN:
82 -                       read_stdin(&vpninfo->cookie);
83 +                       optarg = NULL;
84 +                       /* fall through */
85 +               case OPT_COOKIE_FILE:
86 +                       read_file(optarg, &vpninfo->cookie);
87                         /* If the cookie is empty, ignore it */
88                         if (! *vpninfo->cookie) {
89                                 vpninfo->cookie = NULL;
90                         }
91                         break;
92                 case OPT_PASSWORD_ON_STDIN:
93 -                       read_stdin(&vpninfo->password);
94 +                       optarg = NULL;
95 +                       /* fall through */
96 +               case OPT_PASSWORD_FILE:
97 +                       read_file(optarg, &vpninfo->password);
98                         break;
99                 case OPT_NO_PASSWD:
100                         vpninfo->nopasswd = 1;