| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/* |
| 3 |
* Identifies FIDO CTAP1 ("U2F")/CTAP2 security tokens based on the usage declared in their report |
| 4 |
* descriptor and outputs suitable environment variables. |
| 5 |
* |
| 6 |
* Inspired by Andrew Lutomirski's 'u2f-hidraw-policy.c' |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <fcntl.h> |
| 10 |
#include <linux/hid.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <unistd.h> |
| 13 |
|
| 14 |
#include "alloc-util.h" |
| 15 |
#include "build.h" |
| 16 |
#include "device-private.h" |
| 17 |
#include "device-util.h" |
| 18 |
#include "fd-util.h" |
| 19 |
#include "fido_id_desc.h" |
| 20 |
#include "format-table.h" |
| 21 |
#include "help-util.h" |
| 22 |
#include "log.h" |
| 23 |
#include "main-func.h" |
| 24 |
#include "options.h" |
| 25 |
#include "path-util.h" |
| 26 |
#include "strv.h" |
| 27 |
#include "udev-util.h" |
| 28 |
|
| 29 |
static const char *arg_device = NULL; |
| 30 |
|
| 31 |
static int help(void) { |
| 32 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 33 |
int r; |
| 34 |
|
| 35 |
r = option_parser_get_help_table(&options); |
| 36 |
if (r < 0) |
| 37 |
return r; |
| 38 |
|
| 39 |
help_cmdline("[OPTIONS...] SYSFS_PATH"); |
| 40 |
help_abstract("Identify FIDO security tokens."); |
| 41 |
help_section("Options"); |
| 42 |
|
| 43 |
return table_print_or_warn(options); |
| 44 |
} |
| 45 |
|
| 46 |
static int parse_argv(int argc, char *argv[]) { |
| 47 |
assert(argc >= 0); |
| 48 |
assert(argv); |
| 49 |
|
| 50 |
OptionParser opts = { argc, argv }; |
| 51 |
|
| 52 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 53 |
switch (c) { |
| 54 |
|
| 55 |
OPTION_COMMON_HELP: |
| 56 |
return help(); |
| 57 |
|
| 58 |
OPTION_COMMON_VERSION: |
| 59 |
return version(); |
| 60 |
} |
| 61 |
|
| 62 |
char **args = option_parser_get_args(&opts); |
| 63 |
if (strv_length(args) > 1) |
| 64 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Error: unexpected argument."); |
| 65 |
|
| 66 |
arg_device = args[0]; |
| 67 |
return 1; |
| 68 |
} |
| 69 |
|
| 70 |
static int run(int argc, char **argv) { |
| 71 |
_cleanup_(sd_device_unrefp) struct sd_device *device = NULL; |
| 72 |
_cleanup_free_ char *desc_path = NULL; |
| 73 |
_cleanup_close_ int fd = -EBADF; |
| 74 |
struct sd_device *hid_device; |
| 75 |
const char *sys_path; |
| 76 |
uint8_t desc[HID_MAX_DESCRIPTOR_SIZE]; |
| 77 |
ssize_t desc_len; |
| 78 |
int r; |
| 79 |
|
| 80 |
(void) udev_parse_config(); |
| 81 |
log_setup(); |
| 82 |
|
| 83 |
r = parse_argv(argc, argv); |
| 84 |
if (r <= 0) |
| 85 |
return r; |
| 86 |
|
| 87 |
if (arg_device) { |
| 88 |
r = sd_device_new_from_syspath(&device, arg_device); |
| 89 |
if (r < 0) |
| 90 |
return log_error_errno(r, "Failed to get device from syspath %s: %m", arg_device); |
| 91 |
} else { |
| 92 |
r = device_new_from_strv(&device, environ); |
| 93 |
if (r < 0) |
| 94 |
return log_error_errno(r, "Failed to get current device from environment: %m"); |
| 95 |
} |
| 96 |
|
| 97 |
r = sd_device_get_parent(device, &hid_device); |
| 98 |
if (r < 0) |
| 99 |
return log_device_error_errno(device, r, "Failed to get parent HID device: %m"); |
| 100 |
|
| 101 |
r = sd_device_get_syspath(hid_device, &sys_path); |
| 102 |
if (r < 0) |
| 103 |
return log_device_error_errno(hid_device, r, "Failed to get syspath for HID device: %m"); |
| 104 |
|
| 105 |
desc_path = path_join(sys_path, "report_descriptor"); |
| 106 |
if (!desc_path) |
| 107 |
return log_oom(); |
| 108 |
|
| 109 |
fd = open(desc_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NOCTTY); |
| 110 |
if (fd < 0) |
| 111 |
return log_device_error_errno(hid_device, errno, |
| 112 |
"Failed to open report descriptor at '%s': %m", desc_path); |
| 113 |
|
| 114 |
desc_len = read(fd, desc, sizeof(desc)); |
| 115 |
if (desc_len < 0) |
| 116 |
return log_device_error_errno(hid_device, errno, |
| 117 |
"Failed to read report descriptor at '%s': %m", desc_path); |
| 118 |
if (desc_len == 0) |
| 119 |
return log_device_debug_errno(hid_device, SYNTHETIC_ERRNO(EINVAL), |
| 120 |
"Empty report descriptor at '%s'.", desc_path); |
| 121 |
|
| 122 |
r = is_fido_security_token_desc(desc, desc_len); |
| 123 |
if (r < 0) |
| 124 |
return log_device_debug_errno(hid_device, r, |
| 125 |
"Failed to parse report descriptor at '%s'.", desc_path); |
| 126 |
if (r > 0) { |
| 127 |
printf("ID_FIDO_TOKEN=1\n"); |
| 128 |
printf("ID_SECURITY_TOKEN=1\n"); |
| 129 |
} |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
DEFINE_MAIN_FUNCTION(run); |