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