summaryrefslogtreecommitdiff
path: root/src/shared/libfido2-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-10-08 21:55:04 +0200
committerLennart Poettering <lennart@poettering.net>2021-10-11 11:12:29 +0200
commit4f0cfa77414c2377fbf747b6acfe2f98a3f34d2d (patch)
tree84de8a6e5fe15e5fe6a00915a1994632c05ad755 /src/shared/libfido2-util.c
parent64c590fb06ff9089eef4ebee36047e0762ea4f50 (diff)
downloadsystemd-4f0cfa77414c2377fbf747b6acfe2f98a3f34d2d.tar.gz
libfido2-util: add helper that checks whether a FIDO2 device is plugged in
Diffstat (limited to 'src/shared/libfido2-util.c')
-rw-r--r--src/shared/libfido2-util.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/shared/libfido2-util.c b/src/shared/libfido2-util.c
index a3356c139a..87b88f04d6 100644
--- a/src/shared/libfido2-util.c
+++ b/src/shared/libfido2-util.c
@@ -12,6 +12,7 @@
#include "memory-util.h"
#include "random-util.h"
#include "strv.h"
+#include "unistd.h"
static void *libfido2_dl = NULL;
@@ -1077,3 +1078,52 @@ finish:
"FIDO2 tokens not supported on this build.");
#endif
}
+
+int fido2_have_device(const char *device) {
+#if HAVE_LIBFIDO2
+ size_t allocated = 64, found = 0;
+ fido_dev_info_t *di = NULL;
+ int r;
+
+ /* Return == 0 if not devices are found, > 0 if at least one is found */
+
+ r = dlopen_libfido2();
+ if (r < 0)
+ return log_error_errno(r, "FIDO2 support is not installed.");
+
+ if (device) {
+ if (access(device, F_OK) < 0) {
+ if (errno == ENOENT)
+ return 0;
+
+ return log_error_errno(errno, "Failed to determine whether device '%s' exists: %m", device);
+ }
+
+ return 1;
+ }
+
+ di = sym_fido_dev_info_new(allocated);
+ if (!di)
+ return log_oom();
+
+ r = sym_fido_dev_info_manifest(di, allocated, &found);
+ if (r == FIDO_ERR_INTERNAL) {
+ /* The library returns FIDO_ERR_INTERNAL when no devices are found. I wish it wouldn't. */
+ r = 0;
+ goto finish;
+ }
+ if (r != FIDO_OK) {
+ r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to enumerate FIDO2 devices: %s", sym_fido_strerr(r));
+ goto finish;
+ }
+
+ r = found;
+
+finish:
+ sym_fido_dev_info_free(&di, allocated);
+ return r;
+#else
+ return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
+ "FIDO2 tokens not supported on this build.");
+#endif
+}