Пример #1
0
int
main (int argc, char *argv[])
{
  int exit_code = EXIT_FAILURE;
  char challenge[BUFSIZ];
  size_t chal_len;
  char *response = NULL;
  u2fh_devs *devs = NULL;
  u2fh_cmdflags flags = 0;
  u2fh_rc rc;

  rc = u2fh_global_init (1);
  if (rc != U2FH_OK)
    {
      fprintf (stderr, "error: u2fh_global_init (%d): %s\n", rc,
	       u2fh_strerror (rc));
      exit (EXIT_FAILURE);
    }

  rc = u2fh_devs_init (&devs);
  if (rc != U2FH_OK)
    {
      fprintf (stderr, "error: u2fh_devs_init (%d): %s\n", rc,
	       u2fh_strerror (rc));
      goto done;
    }

  int num_devices = 0;
  rc = u2fh_devs_discover (devs, &num_devices);
  if (rc != U2FH_OK)
    {
      fprintf (stderr, "error: u2fh_devs_discover (%d): %s\n", rc,
	       u2fh_strerror (rc));
      goto done;
    }

  int i;
  for (i = 0; i <= num_devices; i++) {
    fprintf (stderr, "pinging device %d = %d\n", i,
             ping_device(devs, i));
  }

  exit_code = EXIT_SUCCESS;

done:
  u2fh_devs_done (devs);
  u2fh_global_done ();

  exit (exit_code);
}
Пример #2
0
/**
 * u2fh_devs_discover:
 * @devs: device handle, from u2fh_devs_init().
 * @max_index: will on return be set to the maximum index, may be NULL; if
 *   there is 1 device this will be 0, if there are 2 devices this
 *   will be 1, and so on.
 *
 * Discover and open new devices.  This function can safely be called
 * several times and will free resources associated with unplugged
 * devices and open new.
 *
 * Returns: On success, %U2FH_OK (integer 0) is returned, when no U2F
 *   device could be found %U2FH_NO_U2F_DEVICE is returned, or another
 *   #u2fh_rc error code.
 */
u2fh_rc
u2fh_devs_discover (u2fh_devs * devs, unsigned *max_index)
{
  struct hid_device_info *di, *cur_dev;
  u2fh_rc res = U2FH_NO_U2F_DEVICE;
  unsigned index;

  di = hid_enumerate (0, 0);
  for (cur_dev = di; cur_dev; cur_dev = cur_dev->next)
    {
      int found = 0;
      unsigned short usage_page = 0, usage = 0;

      for (index = 0; index < devs->num_devices; index++)
	{
	  struct u2fdevice *dev = &devs->devs[index];
	  if (!dev->is_alive)
	    {
	      continue;
	    }
	  if (strcmp (dev->device_path, cur_dev->path) == 0)
	    {
	      if (ping_device (devs, index) == U2FH_OK)
		{
		  found = 1;
		  res = U2FH_OK;
		  break;
		}
	      else
		{
		  if (debug)
		    {
		      fprintf (stderr, "Device %s failed ping, dead.\n",
			       dev->device_path);
		    }
		  close_device (dev);
		  break;
		}
	    }
	}
      if (found)
	{
	  continue;
	}

      get_usages (cur_dev, &usage_page, &usage);
      if (usage_page == FIDO_USAGE_PAGE && usage == FIDO_USAGE_U2FHID)
	{
	  devs->devs =
	    realloc (devs->devs,
		     (devs->num_devices + 1) * sizeof (struct u2fdevice));
	  struct u2fdevice *dev = &devs->devs[devs->num_devices];
	  memset (dev, 0, sizeof (struct u2fdevice));
	  dev->devh = hid_open_path (cur_dev->path);
	  if (dev->devh != NULL)
	    {
	      dev->device_path = strdup (cur_dev->path);
	      if (dev->device_path == NULL)
		{
		  close_device (dev);
		  return U2FH_MEMORY_ERROR;
		}
	      index = devs->num_devices++;
	      dev->is_alive = 1;
	      if (init_device (devs, index) == U2FH_OK)
		{
		  if (cur_dev->product_string)
		    {
		      size_t len =
			wcstombs (NULL, cur_dev->product_string, 0);
		      dev->device_string = malloc (len + 1);
		      if (dev->device_string == NULL)
			{
			  close_device (dev);
			  return U2FH_MEMORY_ERROR;
			}
		      memset (dev->device_string, 0, len + 1);
		      wcstombs (dev->device_string, cur_dev->product_string,
				len);
		      if (debug)
			{
			  fprintf (stderr, "device %s discovered as '%s'\n",
				   dev->device_path, dev->device_string);
			}
		    }
		  res = U2FH_OK;
		}
	      else
		{
		  close_device (dev);
		  devs->num_devices--;
		}
	    }
	}
    }

  for (index = 0; index < devs->num_devices; index++)
    {
      struct u2fdevice *dev = &devs->devs[index];
      int found = 0;

      if (!dev->is_alive)
	{
	  continue;
	}
      for (cur_dev = di; cur_dev; cur_dev = cur_dev->next)
	{
	  if (strcmp (cur_dev->path, dev->device_path) == 0)
	    {
	      found = 1;
	    }
	}
      if (!found)
	{
	  if (debug)
	    {
	      fprintf (stderr, "device %s looks dead.\n", dev->device_path);
	    }
	  close_device (dev);
	}
    }

  hid_free_enumeration (di);
  if (res == U2FH_OK && max_index)
    *max_index = devs->num_devices - 1;

  return res;
}