~ubuntu-branches/ubuntu/quantal/ykclient/quantal

« back to all changes in this revision

Viewing changes to tool.c

  • Committer: Bazaar Package Importer
  • Author(s): Tollef Fog Heen
  • Date: 2011-06-23 22:40:08 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110623224008-sn5pm6dy9pcr91zm
Tags: 2.6-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* tool.c --- Command line interface to libykclient.
2
2
 *
3
 
 * Written by Simon Josefsson <simon@josefsson.org>.
4
3
 * Copyright (c) 2006, 2007, 2008, 2009, 2011 Yubico AB
5
4
 * All rights reserved.
6
5
 *
46
45
  "\n"
47
46
  "  Options :\n"
48
47
  "    --url URL                Validation service URL (eg: \"http://api.yubico.com/wsapi/verify?id=%%d&otp=%%s\")\n"
49
 
  ;
 
48
  "    --apikey Key             API key for HMAC validation of request/response\n";
50
49
 
51
50
static struct option long_options[] = {
52
51
  {"url", 1, 0, 'u'},
 
52
  {"apikey", 1, 0, 'a'},
53
53
  {0, 0, 0, 0}
54
54
};
55
55
 
56
56
/* Parse command line parameters. */
57
57
void
58
 
parse_args(int argc, char *argv[],
59
 
           int *client_id,
60
 
           char **token,
61
 
           char **url,
62
 
           char **api_key
63
 
           )
 
58
parse_args (int argc, char *argv[],
 
59
            int *client_id, char **token, char **url, char **api_key)
64
60
{
65
61
  while (1)
66
62
    {
67
63
      int option_index = 0;
68
64
 
69
 
      int c = getopt_long(argc, argv, "",
70
 
                          long_options, &option_index);
 
65
      int c = getopt_long (argc, argv, "",
 
66
                           long_options, &option_index);
71
67
      if (c == -1)
72
68
        break;
73
69
 
74
70
      switch (c)
75
71
        {
 
72
        case 'a':
 
73
          if (strlen (optarg) < 16)
 
74
            {
 
75
              fprintf (stderr,
 
76
                       "error: API key must be at least 16 characters");
 
77
              exit (EXIT_FAILURE);
 
78
            }
 
79
          *api_key = optarg;
 
80
          break;
76
81
        case 'u':
77
 
          if (strncmp ("http://", optarg, 7) != 0 && strncmp ("https://", optarg, 8) != 0)
78
 
            errx (EXIT_FAILURE, "error: validation url must be http or https.");
 
82
          if (strncmp ("http://", optarg, 7) != 0
 
83
              && strncmp ("https://", optarg, 8) != 0)
 
84
            {
 
85
              fprintf (stderr, "error: validation url must be http or https");
 
86
              exit (EXIT_FAILURE);
 
87
            }
79
88
          *url = optarg;
80
89
          break;
81
90
        }
83
92
 
84
93
  if (argc - optind != 2)
85
94
    {
86
 
      errx (EXIT_FAILURE, usage);
 
95
      fprintf (stderr, "%s", usage);
 
96
      exit (EXIT_FAILURE);
87
97
    }
88
98
 
89
99
  /* Now get mandatory numeric client_id */
90
100
  *client_id = atoi (argv[optind++]);
91
101
 
92
102
  if (*client_id <= 0)
93
 
    errx (EXIT_FAILURE, "error: client identity must be a non-zero integer.");
 
103
    {
 
104
      fprintf (stderr, "error: client identity must be a non-zero integer.");
 
105
      exit (EXIT_FAILURE);
 
106
    }
94
107
 
95
108
  /* Likewise mandatory OTP token */
96
109
  *token = argv[optind++];
97
110
  if (strlen (*token) < 32)
98
111
    {
99
 
      errx (EXIT_FAILURE, "error: ModHex encoded token must be at least 32 characters.");
 
112
      fprintf (stderr,
 
113
               "error: modhex encoded token must be at least 32 characters");
 
114
      exit (EXIT_FAILURE);
100
115
    }
101
116
}
102
117
 
107
122
  char *token, *url = NULL, *api_key = NULL;
108
123
  int ret, optind;
109
124
 
110
 
  parse_args (argc, argv,
111
 
              &client_id,
112
 
              &token,
113
 
              &url,
114
 
              &api_key
115
 
              );
 
125
  parse_args (argc, argv, &client_id, &token, &url, &api_key);
116
126
 
117
127
  /* Debug. */
118
128
  fprintf (stderr, "Input:\n");
120
130
    fprintf (stderr, "  validation URL: %s\n", url);
121
131
  fprintf (stderr, "  client id: %d\n", client_id);
122
132
  fprintf (stderr, "  token: %s\n", token);
 
133
  if (api_key != NULL)
 
134
    fprintf (stderr, "  api key: %s\n", api_key);
123
135
 
124
 
  ret = ykclient_verify_otp_v2 (NULL, token, client_id, NULL, 1, (const char **) &url, api_key);
 
136
  ret =
 
137
    ykclient_verify_otp_v2 (NULL, token, client_id, NULL, 1,
 
138
                            (const char **) &url, api_key);
125
139
 
126
140
  printf ("Verification output (%d): %s\n", ret, ykclient_strerror (ret));
127
141