xref: /freebsd/contrib/libfido2/examples/reset.c (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
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 <fido.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 
15 #include "../openbsd-compat/openbsd-compat.h"
16 #include "extern.h"
17 
18 int
19 main(int argc, char **argv)
20 {
21 	fido_dev_t	*dev;
22 	int		 r;
23 
24 	if (argc != 2) {
25 		fprintf(stderr, "usage: reset <device>\n");
26 		exit(EXIT_FAILURE);
27 	}
28 
29 	fido_init(0);
30 
31 	if ((dev = fido_dev_new()) == NULL)
32 		errx(1, "fido_dev_new");
33 
34 	if ((r = fido_dev_open(dev, argv[1])) != FIDO_OK)
35 		errx(1, "fido_dev_open: %s (0x%x)", fido_strerr(r), r);
36 
37 #ifdef SIGNAL_EXAMPLE
38 	prepare_signal_handler(SIGINT);
39 #endif
40 
41 	if ((r = fido_dev_reset(dev)) != FIDO_OK) {
42 #ifdef SIGNAL_EXAMPLE
43 		if (got_signal)
44 			fido_dev_cancel(dev);
45 #endif
46 		errx(1, "fido_reset: %s (0x%x)", fido_strerr(r), r);
47 	}
48 
49 	if ((r = fido_dev_close(dev)) != FIDO_OK)
50 		errx(1, "fido_dev_close: %s (0x%x)", fido_strerr(r), r);
51 
52 	fido_dev_free(&dev);
53 
54 	exit(0);
55 }
56