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