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