10afa8e06SEd Maste /*
20afa8e06SEd Maste * Copyright (c) 2018 Yubico AB. All rights reserved.
30afa8e06SEd Maste * Use of this source code is governed by a BSD-style
40afa8e06SEd Maste * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste * SPDX-License-Identifier: BSD-2-Clause
60afa8e06SEd Maste */
70afa8e06SEd Maste
80afa8e06SEd Maste /*
90afa8e06SEd Maste * Configure a PIN on a given authenticator.
100afa8e06SEd Maste */
110afa8e06SEd Maste
120afa8e06SEd Maste #include <fido.h>
130afa8e06SEd Maste #include <stdio.h>
140afa8e06SEd Maste #include <stdlib.h>
150afa8e06SEd Maste
160afa8e06SEd Maste #include "../openbsd-compat/openbsd-compat.h"
170afa8e06SEd Maste
180afa8e06SEd Maste static void
setpin(const char * path,const char * pin,const char * oldpin)190afa8e06SEd Maste setpin(const char *path, const char *pin, const char *oldpin)
200afa8e06SEd Maste {
210afa8e06SEd Maste fido_dev_t *dev;
220afa8e06SEd Maste int r;
230afa8e06SEd Maste
240afa8e06SEd Maste fido_init(0);
250afa8e06SEd Maste
260afa8e06SEd Maste if ((dev = fido_dev_new()) == NULL)
270afa8e06SEd Maste errx(1, "fido_dev_new");
280afa8e06SEd Maste
290afa8e06SEd Maste if ((r = fido_dev_open(dev, path)) != FIDO_OK)
300afa8e06SEd Maste errx(1, "fido_dev_open: %s (0x%x)", fido_strerr(r), r);
310afa8e06SEd Maste
320afa8e06SEd Maste if ((r = fido_dev_set_pin(dev, pin, oldpin)) != FIDO_OK)
33*2ccfa855SEd Maste errx(1, "fido_dev_set_pin: %s (0x%x)", fido_strerr(r), r);
340afa8e06SEd Maste
350afa8e06SEd Maste if ((r = fido_dev_close(dev)) != FIDO_OK)
360afa8e06SEd Maste errx(1, "fido_dev_close: %s (0x%x)", fido_strerr(r), r);
370afa8e06SEd Maste
380afa8e06SEd Maste fido_dev_free(&dev);
390afa8e06SEd Maste }
400afa8e06SEd Maste
410afa8e06SEd Maste int
main(int argc,char ** argv)420afa8e06SEd Maste main(int argc, char **argv)
430afa8e06SEd Maste {
440afa8e06SEd Maste if (argc < 3 || argc > 4) {
450afa8e06SEd Maste fprintf(stderr, "usage: setpin <pin> [oldpin] <device>\n");
460afa8e06SEd Maste exit(EXIT_FAILURE);
470afa8e06SEd Maste }
480afa8e06SEd Maste
490afa8e06SEd Maste if (argc == 3)
500afa8e06SEd Maste setpin(argv[2], argv[1], NULL);
510afa8e06SEd Maste else
520afa8e06SEd Maste setpin(argv[3], argv[1], argv[2]);
530afa8e06SEd Maste
540afa8e06SEd Maste exit(0);
550afa8e06SEd Maste }
56