<commit_before>/* tool.c --- Command line interface to libykclient.
 *
 * Written by <NAME> <<EMAIL>>.
 * Copyright (c) 2006, 2007, 2008, 2009, 2011 Yubico AB
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer in the documentation and/or other materials provided
 *      with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "ykclient.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

int
main (int argc, char *argv[])
{
  char *client_id, *token, *url = NULL, *api_key = NULL;
  int ret, optind;

  /* Parse command line parameters. */
  if (argc < 3 || argc > 4)
    {
      printf ("Usage: %s [<validation_url>] <client_id> <yubikey_otp>\n", argv[0]);
      printf (" VALIDATION_URL: URL of the validation server (eg: \"http://api.yubico.com/wsapi/verify?id=%%d&otp=%%s\")\n");
      printf (" CLIENT_ID: your client id integer\n");
      printf (" YUBIKEY_OTP: One-time password generated by yubikey\n");
      return EXIT_FAILURE;
    }

  optind = 1;

  /* Look for optional validation_url */
  if (atoi (argv[optind]) <= 0) {
    /* not numeric - ie. not client_id */
    if (strncmp ("http://", argv[optind], 7) != 0 && strncmp ("https://", argv[optind], 8) != 0)
      {
	/* Invalid URL...*/
	printf ("error: validation url must be http or https.\n");
	return EXIT_FAILURE;
      }

      url = argv[optind++];
  }

  /* Now get mandatory numeric client_id */
  client_id = argv[optind++];

  if (atoi (client_id) <= 0)
    {
      printf ("error: client identity must be a non-zero integer.\n");
      return EXIT_FAILURE;
    }

  /* Likewise mandatory OTP token */
  token = argv[optind++];
  if (strlen (token) < 32)
    {
      printf ("error: ModHex encoded token must be at least 32 characters.\n");
      return EXIT_FAILURE;
    }

  /* Debug. */
  printf ("Input:\n");
  if (url)
    printf ("  validation URL: %s\n", url);
  printf ("  client id: %d\n", atoi (client_id));
  printf ("  token: %s\n", token);

  ret = ykclient_verify_otp_v2 (NULL, token, atoi (client_id), NULL, 1, (const char **) &url, api_key);

  printf ("Verification output (%d): %s\n", ret, ykclient_strerror (ret));

  if (ret != YKCLIENT_OK)
    return EXIT_FAILURE;

  return EXIT_SUCCESS;
}<commit_msg>Implement option parsing with getopt_long.<commit_after>/* tool.c --- Command line interface to libykclient.
 *
 * Written by <NAME> <<EMAIL>>.
 * Copyright (c) 2006, 2007, 2008, 2009, 2011 Yubico AB
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer in the documentation and/or other materials provided
 *      with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "ykclient.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <getopt.h>

const char *usage =
  "Usage: ykclient [options] <client_id> <yubikey_otp>\n"
  "  client_id: your client id integer\n"
  "  yubikey_otp: One-time password generated by yubikey\n"
  "\n"
  "  Options :\n"
  "    --url URL		Validation service URL (eg: \"http://api.yubico.com/wsapi/verify?id=%%d&otp=%%s\")\n"
  ;

static struct option long_options[] = {
  {"url", 1, 0, 'u'},
  {0, 0, 0, 0}
};

/* Parse command line parameters. */
void
parse_args(int argc, char *argv[],
	   int *client_id,
	   char **token,
	   char **url,
	   char **api_key
	   )
{
  while (1)
    {
      int option_index = 0;

      int c = getopt_long(argc, argv, "",
			  long_options, &option_index);
      if (c == -1)
	break;

      switch (c)
	{
	case 'u':
	  if (strncmp ("http://", optarg, 7) != 0 && strncmp ("https://", optarg, 8) != 0)
	    errx (EXIT_FAILURE, "error: validation url must be http or https.");
	  *url = optarg;
	  break;
	}
    }

  if (argc - optind != 2)
    {
      errx (EXIT_FAILURE, usage);
    }

  /* Now get mandatory numeric client_id */
  *client_id = atoi (argv[optind++]);

  if (*client_id <= 0)
    errx (EXIT_FAILURE, "error: client identity must be a non-zero integer.");

  /* Likewise mandatory OTP token */
  *token = argv[optind++];
  if (strlen (*token) < 32)
    {
      errx (EXIT_FAILURE, "error: ModHex encoded token must be at least 32 characters.");
    }
}

int
main (int argc, char *argv[])
{
  int client_id;
  char *token, *url = NULL, *api_key = NULL;
  int ret, optind;

  parse_args (argc, argv,
	      &client_id,
	      &token,
	      &url,
	      &api_key
	      );

  /* Debug. */
  fprintf (stderr, "Input:\n");
  if (url)
    fprintf (stderr, "  validation URL: %s\n", url);
  fprintf (stderr, "  client id: %d\n", client_id);
  fprintf (stderr, "  token: %s\n", token);

  ret = ykclient_verify_otp_v2 (NULL, token, client_id, NULL, 1, (const char **) &url, api_key);

  printf ("Verification output (%d): %s\n", ret, ykclient_strerror (ret));

  if (ret != YKCLIENT_OK)
    return EXIT_FAILURE;

  return EXIT_SUCCESS;
}
{'fraction_non_alphanumeric': 0.12306201550387597, 'fraction_numerical': 0.01065891472868217, 'mean_word_length': 3.2852906287069987, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 26, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5402732', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2272, 'n_words': 855}