1/*-
2 * Copyright (c) 2010 Alistair Crooks <agc@NetBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include <sys/types.h>
26#include <sys/param.h>
27
28#include <inttypes.h>
29#include <netpgp.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35#include "b64.h"
36#include "hkpc.h"
37
38#define DEFAULT_NUMBITS 2048
39
40#define DEFAULT_HASH_ALG "SHA256"
41
42int
43main(int argc, char **argv)
44{
45	netpgp_t	 netpgp;
46	char		*res;
47	char		 key[8192];
48	char		 asc[8192];
49	char	 	 server[BUFSIZ];
50	char		*cp;
51	int	 	 family;
52	int	 	 port;
53	int	 	 keyc;
54	int	 	 ascc;
55	int	 	 ok;
56	int	 	 i;
57
58	(void) memset(&netpgp, 0x0, sizeof(netpgp));
59	port = 11371;
60	family = 4;
61	(void) snprintf(server, sizeof(server), "localhost");
62	while ((i = getopt(argc, argv, "f:h:p:")) != -1) {
63		switch(i) {
64		case 'f':
65			family = atoi(optarg);
66			break;
67		case 'h':
68			(void) snprintf(server, sizeof(server), optarg);
69			break;
70		case 'p':
71			port = atoi(optarg);
72			break;
73		default:
74			break;
75		}
76	}
77	netpgp_setvar(&netpgp, "ssh keys", "1");
78	netpgp_setvar(&netpgp, "hash", DEFAULT_HASH_ALG);
79	netpgp_set_homedir(&netpgp, getenv("HOME"), "/.ssh", 1);
80	for (ok = 1, i = optind ; i < argc ; i++) {
81		if (!hkpc_get(&res, server, port, family, "get", argv[i])) {
82			(void) fprintf(stderr, "No such key '%s'\n", argv[i]);
83			ok = 0;
84		}
85		if ((keyc = netpgp_write_sshkey(&netpgp, res, argv[i], key, sizeof(key))) <= 0) {
86			(void) fprintf(stderr, "can't netpgp_write_sshkey '%s'\n", argv[i]);
87			ok = 0;
88		}
89		for (cp = &key[keyc - 1] ; cp > key && *cp != ' ' ; --cp) {
90		}
91		if (cp == key) {
92			cp = argv[i];
93		} else {
94			cp += 1;
95		}
96		/* btoa */
97		ascc = b64encode(key, keyc, asc, sizeof(asc), 0xffffffff);
98		/* write to .ssh/id_c0596823.pub */
99		printf("ssh-rsa %.*s %s\n", ascc, asc, cp);
100	}
101	exit((ok) ? EXIT_SUCCESS : EXIT_FAILURE);
102}
103