xref: /netbsd-src/external/bsd/libfido2/dist/examples/reset.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*
2  * Copyright (c) 2018 Yubico AB. All rights reserved.
3  * Use of this source code is governed by a BSD-style
4  * license that can be found in the LICENSE file.
5  */
6 
7 /*
8  * Perform a factory reset on a given authenticator.
9  */
10 
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 
16 #include "fido.h"
17 #include "extern.h"
18 #include "../openbsd-compat/openbsd-compat.h"
19 
20 int
21 main(int argc, char **argv)
22 {
23 	fido_dev_t	*dev;
24 	int		 r;
25 
26 	if (argc != 2) {
27 		fprintf(stderr, "usage: reset <device>\n");
28 		exit(EXIT_FAILURE);
29 	}
30 
31 	fido_init(0);
32 
33 	if ((dev = fido_dev_new()) == NULL)
34 		errx(1, "fido_dev_new");
35 
36 	if ((r = fido_dev_open(dev, argv[1])) != FIDO_OK)
37 		errx(1, "fido_dev_open: %s (0x%x)", fido_strerr(r), r);
38 
39 #ifdef SIGNAL_EXAMPLE
40 	prepare_signal_handler(SIGINT);
41 #endif
42 
43 	if ((r = fido_dev_reset(dev)) != FIDO_OK) {
44 #ifdef SIGNAL_EXAMPLE
45 		if (got_signal)
46 			fido_dev_cancel(dev);
47 #endif
48 		errx(1, "fido_reset: %s (0x%x)", fido_strerr(r), r);
49 	}
50 
51 	if ((r = fido_dev_close(dev)) != FIDO_OK)
52 		errx(1, "fido_dev_close: %s (0x%x)", fido_strerr(r), r);
53 
54 	fido_dev_free(&dev);
55 
56 	exit(0);
57 }
58