13 int eprint(const char *format, ...)
18 return vfprintf(stderr, format, ap);
22 static void usage(const char const *cmd)
24 eprint( "usage: %s [options] [username]\n"
25 " -h - show this help and exit\n"
26 " -o backend-option - token option \"backend:key=val\"\n"
27 " -f auth-file - auth state file to read/write\n"
28 " -a secret | -A file-with-secret | -A -\n"
29 " - 40-character hexadecimal secret\n"
30 " -s token-serial - public I.D. of the token\n"
31 " -n nonce - initial nonce\n"
32 " -l payload - keyring unlock password\n"
33 " -p password - login password\n"
37 int main(int argc, char *argv[])
44 unsigned char bsecret[20];
45 unsigned char *secret = NULL;
49 char *id = getlogin();
53 while ((c = getopt(argc, argv, "ho:f:a:A:s:n:l:p:")) != -1)
59 if (pcsc_option(optarg)) {
60 eprint("Option \"%s\" bad\n", optarg);
71 eprint("-a and -A are mutually exclusive\n");
79 eprint("-A and -a are mutually exclusive\n");
99 if (optind == (argc - 1)) {
103 if (optind != argc) {
111 if (!strcmp(secfn, "-")) sfp = stdin;
112 else sfp = fopen(secfn, "r");
114 eprint("cannot open \"%s\": %s\n",
115 secfn, strerror(errno));
118 if (!fgets(secbuf, sizeof(secbuf), sfp)) {
119 eprint("cannot read \"%s\": %s\n",
120 secfn, strerror(errno));
123 for (p = secbuf + strlen(secbuf) - 1;
124 *p == '\n' || *p == '\r'; p--) *p = '\n';
130 eprint("cannot determine userid\n");
134 if (strlen(hsecret) != 40) {
136 "secret wrong, must be exactly 40 chars\n");
139 if (strspn(hsecret, "0123456789abcdefABCDEF") != 40) {
141 "secret wrong, must be hexadecimal string\n");
144 for (i = 0; i < 20; i++)
145 sscanf(hsecret + i * 2, "%2hhx", &bsecret[i]);
148 return update_authfile(fn, tokenid, id, password, nonce,
149 secret, sizeof(bsecret),
150 (unsigned char *)payload, strlen(payload));