xref: /netbsd-src/external/bsd/libfido2/dist/examples/setpin.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  * Configure a PIN 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 "../openbsd-compat/openbsd-compat.h"
18 
19 static void
20 setpin(const char *path, const char *pin, const char *oldpin)
21 {
22 	fido_dev_t *dev;
23 	int r;
24 
25 	fido_init(0);
26 
27 	if ((dev = fido_dev_new()) == NULL)
28 		errx(1, "fido_dev_new");
29 
30 	if ((r = fido_dev_open(dev, path)) != FIDO_OK)
31 		errx(1, "fido_dev_open: %s (0x%x)", fido_strerr(r), r);
32 
33 	if ((r = fido_dev_set_pin(dev, pin, oldpin)) != FIDO_OK)
34 		errx(1, "fido_setpin: %s (0x%x)", fido_strerr(r), r);
35 
36 	if ((r = fido_dev_close(dev)) != FIDO_OK)
37 		errx(1, "fido_dev_close: %s (0x%x)", fido_strerr(r), r);
38 
39 	fido_dev_free(&dev);
40 }
41 
42 int
43 main(int argc, char **argv)
44 {
45 	if (argc < 3 || argc > 4) {
46 		fprintf(stderr, "usage: setpin <pin> [oldpin] <device>\n");
47 		exit(EXIT_FAILURE);
48 	}
49 
50 	if (argc == 3)
51 		setpin(argv[2], argv[1], NULL);
52 	else
53 		setpin(argv[3], argv[1], argv[2]);
54 
55 	exit(0);
56 }
57