2 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Alistair Crooks (agc@NetBSD.org)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 /* Command line program to perform netpgp operations */
31 #include <sys/types.h>
32 #include <sys/param.h>
44 * 2048 is the absolute minimum, really - we should really look at
45 * bumping this to 4096 or even higher - agc, 20090522
47 #define DEFAULT_NUMBITS 2048
49 static const char *usage
=
51 "\t--export-keys [options] OR\n"
52 "\t--find-key [options] OR\n"
53 "\t--generate-key [options] OR\n"
54 "\t--import-key [options] OR\n"
55 "\t--list-keys [options] OR\n"
56 "\t--get-key keyid [options] OR\n"
58 "where options are:\n"
59 "\t[--coredumps] AND/OR\n"
60 "\t[--homedir=<homedir>] AND/OR\n"
61 "\t[--keyring=<keyring>] AND/OR\n"
62 "\t[--userid=<userid>] AND/OR\n"
95 static struct option options
[] = {
96 /* key-management commands */
97 {"list-keys", no_argument
, NULL
, LIST_KEYS
},
98 {"find-key", no_argument
, NULL
, FIND_KEY
},
99 {"export-key", no_argument
, NULL
, EXPORT_KEY
},
100 {"import-key", no_argument
, NULL
, IMPORT_KEY
},
101 {"generate-key", no_argument
, NULL
, GENERATE_KEY
},
102 {"get-key", no_argument
, NULL
, GET_KEY
},
103 /* debugging commands */
104 {"help", no_argument
, NULL
, HELP_CMD
},
105 {"version", no_argument
, NULL
, VERSION_CMD
},
106 {"debug", required_argument
, NULL
, OPS_DEBUG
},
108 {"coredumps", no_argument
, NULL
, COREDUMPS
},
109 {"keyring", required_argument
, NULL
, KEYRING
},
110 {"userid", required_argument
, NULL
, USERID
},
111 {"home", required_argument
, NULL
, HOMEDIR
},
112 {"homedir", required_argument
, NULL
, HOMEDIR
},
113 {"numbits", required_argument
, NULL
, NUMBITS
},
114 {"ssh-keys", no_argument
, NULL
, SSHKEYS
},
115 {"sshkeyfile", required_argument
, NULL
, SSHKEYFILE
},
116 {"verbose", no_argument
, NULL
, VERBOSE
},
117 {"pass-fd", required_argument
, NULL
, PASSWDFD
},
118 {"results", required_argument
, NULL
, RESULTS
},
122 /* gather up program variables into one struct */
123 typedef struct prog_t
{
124 char keyring
[MAXPATHLEN
+ 1]; /* name of keyring */
125 char *progname
; /* program name */
126 int numbits
; /* # of bits */
127 int cmd
; /* netpgpkeys command */
131 /* print a usage message */
133 print_usage(const char *usagemsg
, char *progname
)
135 (void) fprintf(stderr
,
136 "%s\nAll bug reports, praise and chocolate, please, to:\n%s\n",
137 netpgp_get_info("version"),
138 netpgp_get_info("maintainer"));
139 (void) fprintf(stderr
, "Usage: %s COMMAND OPTIONS:\n%s %s",
140 progname
, progname
, usagemsg
);
143 /* do a command once for a specified file 'f' */
145 netpgp_cmd(netpgp_t
*netpgp
, prog_t
*p
, char *f
)
151 return (f
== NULL
) ? netpgp_list_keys(netpgp
) : netpgp_match_list_keys(netpgp
, f
);
153 return netpgp_find_key(netpgp
, netpgp_getvar(netpgp
, "userid"));
155 return netpgp_export_key(netpgp
,
156 netpgp_getvar(netpgp
, "userid"));
158 return netpgp_import_key(netpgp
, f
);
160 return netpgp_generate_key(netpgp
,
161 netpgp_getvar(netpgp
, "userid"), p
->numbits
);
163 key
= netpgp_get_key(netpgp
, f
);
168 (void) fprintf(stderr
, "key '%s' not found\n", f
);
172 print_usage(usage
, p
->progname
);
178 main(int argc
, char **argv
)
187 (void) memset(&p
, 0x0, sizeof(p
));
188 (void) memset(&netpgp
, 0x0, sizeof(netpgp
));
189 p
.progname
= argv
[0];
190 p
.numbits
= DEFAULT_NUMBITS
;
192 print_usage(usage
, p
.progname
);
195 /* set some defaults */
196 netpgp_set_homedir(&netpgp
, getenv("HOME"), "/.gnupg", 1);
197 netpgp_setvar(&netpgp
, "sshkeydir", "/etc/ssh");
199 while ((ch
= getopt_long(argc
, argv
, "", options
, &optindex
)) != -1) {
200 switch (options
[optindex
].val
) {
202 p
.cmd
= options
[optindex
].val
;
205 netpgp_setvar(&netpgp
, "coredumps", "allowed");
206 p
.cmd
= options
[optindex
].val
;
209 netpgp_setvar(&netpgp
, "userid checks", "skip");
210 p
.cmd
= options
[optindex
].val
;
217 p
.cmd
= options
[optindex
].val
;
221 "%s\nAll bug reports, praise and chocolate, please, to:\n%s\n",
222 netpgp_get_info("version"),
223 netpgp_get_info("maintainer"));
227 netpgp_setvar(&netpgp
, "ssh keys", "1");
230 if (optarg
== NULL
) {
231 (void) fprintf(stderr
,
232 "%s: No keyring argument provided\n",
236 snprintf(p
.keyring
, sizeof(p
.keyring
), "%s", optarg
);
239 if (optarg
== NULL
) {
240 (void) fprintf(stderr
,
241 "%s: no userid argument provided\n",
245 netpgp_setvar(&netpgp
, "userid", optarg
);
248 netpgp_incvar(&netpgp
, "verbose", 1);
251 if (optarg
== NULL
) {
252 (void) fprintf(stderr
,
253 "%s: no home directory argument provided\n",
257 netpgp_set_homedir(&netpgp
, optarg
, NULL
, 0);
260 if (optarg
== NULL
) {
261 (void) fprintf(stderr
,
262 "%s: no number of bits argument provided\n",
266 p
.numbits
= atoi(optarg
);
269 if (optarg
== NULL
) {
270 (void) fprintf(stderr
,
271 "%s: no pass-fd argument provided\n", *argv
);
274 netpgp_setvar(&netpgp
, "pass-fd", optarg
);
277 if (optarg
== NULL
) {
278 (void) fprintf(stderr
,
279 "No output filename argument provided\n");
282 netpgp_setvar(&netpgp
, "results", optarg
);
285 netpgp_setvar(&netpgp
, "sshkeyfile", optarg
);
288 netpgp_set_debug(optarg
);
295 /* initialise, and read keys from file */
296 if (!netpgp_init(&netpgp
)) {
297 printf("can't initialise\n");
300 /* now do the required action for each of the command line args */
302 if (optind
== argc
) {
303 if (!netpgp_cmd(&netpgp
, &p
, NULL
)) {
307 for (i
= optind
; i
< argc
; i
++) {
308 if (!netpgp_cmd(&netpgp
, &p
, argv
[i
])) {