xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/netpgpkeys/netpgpkeys.c (revision bbde328be4e75ea9ad02e9715ea13ca54b797ada)
1 /*-
2  * Copyright (c) 2009 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Alistair Crooks (agc@NetBSD.org)
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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.
28  */
29 
30 /* Command line program to perform netpgp operations */
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 
35 #include <getopt.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <netpgp.h>
42 
43 /*
44  * 2048 is the absolute minimum, really - we should really look at
45  * bumping this to 4096 or even higher - agc, 20090522
46  */
47 #define DEFAULT_NUMBITS 2048
48 
49 static const char *usage =
50 	" --help OR\n"
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--list-sigs [options] OR\n"
57 	"\t--get-key keyid [options] OR\n"
58 	"\t--version\n"
59 	"where options are:\n"
60 	"\t[--coredumps] AND/OR\n"
61 	"\t[--homedir=<homedir>] AND/OR\n"
62 	"\t[--keyring=<keyring>] AND/OR\n"
63 	"\t[--userid=<userid>] AND/OR\n"
64 	"\t[--verbose]\n";
65 
66 enum optdefs {
67 	/* commands */
68 	LIST_KEYS = 1,
69 	LIST_SIGS,
70 	FIND_KEY,
71 	EXPORT_KEY,
72 	IMPORT_KEY,
73 	GENERATE_KEY,
74 	VERSION_CMD,
75 	HELP_CMD,
76 	GET_KEY,
77 
78 	/* options */
79 	SSHKEYS,
80 	KEYRING,
81 	USERID,
82 	HOMEDIR,
83 	NUMBITS,
84 	VERBOSE,
85 	COREDUMPS,
86 	PASSWDFD,
87 	RESULTS,
88 	SSHKEYFILE,
89 
90 	/* debug */
91 	OPS_DEBUG
92 
93 };
94 
95 #define EXIT_ERROR	2
96 
97 static struct option options[] = {
98 	/* key-management commands */
99 	{"list-keys",	no_argument,		NULL,	LIST_KEYS},
100 	{"list-sigs",	no_argument,		NULL,	LIST_SIGS},
101 	{"find-key",	no_argument,		NULL,	FIND_KEY},
102 	{"export-key",	no_argument,		NULL,	EXPORT_KEY},
103 	{"import-key",	no_argument,		NULL,	IMPORT_KEY},
104 	{"generate-key", optional_argument,	NULL,	GENERATE_KEY},
105 	{"get-key", 	no_argument,		NULL,	GET_KEY},
106 	/* debugging commands */
107 	{"help",	no_argument,		NULL,	HELP_CMD},
108 	{"version",	no_argument,		NULL,	VERSION_CMD},
109 	{"debug",	required_argument, 	NULL,	OPS_DEBUG},
110 	/* options */
111 	{"coredumps",	no_argument, 		NULL,	COREDUMPS},
112 	{"keyring",	required_argument, 	NULL,	KEYRING},
113 	{"userid",	required_argument, 	NULL,	USERID},
114 	{"home",	required_argument, 	NULL,	HOMEDIR},
115 	{"homedir",	required_argument, 	NULL,	HOMEDIR},
116 	{"numbits",	required_argument, 	NULL,	NUMBITS},
117 	{"ssh-keys",	no_argument, 		NULL,	SSHKEYS},
118 	{"sshkeyfile",	required_argument, 	NULL,	SSHKEYFILE},
119 	{"verbose",	no_argument, 		NULL,	VERBOSE},
120 	{"pass-fd",	required_argument, 	NULL,	PASSWDFD},
121 	{"results",	required_argument, 	NULL,	RESULTS},
122 	{ NULL,		0,			NULL,	0},
123 };
124 
125 /* gather up program variables into one struct */
126 typedef struct prog_t {
127 	char	 keyring[MAXPATHLEN + 1];	/* name of keyring */
128 	char	*progname;			/* program name */
129 	int	 numbits;			/* # of bits */
130 	int	 cmd;				/* netpgpkeys command */
131 } prog_t;
132 
133 
134 /* print a usage message */
135 static void
136 print_usage(const char *usagemsg, char *progname)
137 {
138 	(void) fprintf(stderr,
139 	"%s\nAll bug reports, praise and chocolate, please, to:\n%s\n",
140 				netpgp_get_info("version"),
141 				netpgp_get_info("maintainer"));
142 	(void) fprintf(stderr, "Usage: %s COMMAND OPTIONS:\n%s %s",
143 		progname, progname, usagemsg);
144 }
145 
146 /* do a command once for a specified file 'f' */
147 static int
148 netpgp_cmd(netpgp_t *netpgp, prog_t *p, char *f)
149 {
150 	char	*key;
151 
152 	switch (p->cmd) {
153 	case LIST_KEYS:
154 		return (f == NULL) ? netpgp_list_keys(netpgp, 0) : netpgp_match_keys(netpgp, f, "human", stdout, 0);
155 	case LIST_SIGS:
156 		return (f == NULL) ? netpgp_list_keys(netpgp, 1) : netpgp_match_keys(netpgp, f, "human", stdout, 1);
157 	case FIND_KEY:
158 		return netpgp_find_key(netpgp, netpgp_getvar(netpgp, "userid"));
159 	case EXPORT_KEY:
160 		key = netpgp_export_key(netpgp, netpgp_getvar(netpgp, "userid"));
161 		if (key) {
162 			printf("%s", key);
163 			return 1;
164 		}
165 		(void) fprintf(stderr, "key '%s' not found\n", f);
166 		return 0;
167 	case IMPORT_KEY:
168 		return netpgp_import_key(netpgp, f);
169 	case GENERATE_KEY:
170 		return netpgp_generate_key(netpgp, f, p->numbits);
171 	case GET_KEY:
172 		key = netpgp_get_key(netpgp, f, "human");
173 		if (key) {
174 			printf("%s", key);
175 			return 1;
176 		}
177 		(void) fprintf(stderr, "key '%s' not found\n", f);
178 		return 0;
179 	case HELP_CMD:
180 	default:
181 		print_usage(usage, p->progname);
182 		exit(EXIT_SUCCESS);
183 	}
184 }
185 
186 int
187 main(int argc, char **argv)
188 {
189 	struct stat	st;
190 	netpgp_t	netpgp;
191 	prog_t          p;
192 	int             optindex;
193 	int             ret;
194 	int             ch;
195 	int             i;
196 
197 	(void) memset(&p, 0x0, sizeof(p));
198 	(void) memset(&netpgp, 0x0, sizeof(netpgp));
199 	p.progname = argv[0];
200 	p.numbits = DEFAULT_NUMBITS;
201 	if (argc < 2) {
202 		print_usage(usage, p.progname);
203 		exit(EXIT_ERROR);
204 	}
205 	/* set some defaults */
206 	netpgp_set_homedir(&netpgp, getenv("HOME"), "/.gnupg", 1);
207 	netpgp_setvar(&netpgp, "sshkeydir", "/etc/ssh");
208 	optindex = 0;
209 	while ((ch = getopt_long(argc, argv, "", options, &optindex)) != -1) {
210 		switch (options[optindex].val) {
211 		case COREDUMPS:
212 			netpgp_setvar(&netpgp, "coredumps", "allowed");
213 			p.cmd = options[optindex].val;
214 			break;
215 		case GENERATE_KEY:
216 			netpgp_setvar(&netpgp, "userid checks", "skip");
217 			p.cmd = options[optindex].val;
218 			break;
219 		case LIST_KEYS:
220 		case LIST_SIGS:
221 		case FIND_KEY:
222 		case EXPORT_KEY:
223 		case IMPORT_KEY:
224 		case GET_KEY:
225 		case HELP_CMD:
226 			p.cmd = options[optindex].val;
227 			break;
228 		case VERSION_CMD:
229 			printf(
230 "%s\nAll bug reports, praise and chocolate, please, to:\n%s\n",
231 				netpgp_get_info("version"),
232 				netpgp_get_info("maintainer"));
233 			exit(EXIT_SUCCESS);
234 			/* options */
235 		case SSHKEYS:
236 			netpgp_setvar(&netpgp, "ssh keys", "1");
237 			break;
238 		case KEYRING:
239 			if (optarg == NULL) {
240 				(void) fprintf(stderr,
241 					"%s: No keyring argument provided\n",
242 					*argv);
243 				exit(EXIT_ERROR);
244 			}
245 			snprintf(p.keyring, sizeof(p.keyring), "%s", optarg);
246 			break;
247 		case USERID:
248 			if (optarg == NULL) {
249 				(void) fprintf(stderr,
250 					"%s: no userid argument provided\n",
251 					*argv);
252 				exit(EXIT_ERROR);
253 			}
254 			netpgp_setvar(&netpgp, "userid", optarg);
255 			break;
256 		case VERBOSE:
257 			netpgp_incvar(&netpgp, "verbose", 1);
258 			break;
259 		case HOMEDIR:
260 			if (optarg == NULL) {
261 				(void) fprintf(stderr,
262 				"%s: no home directory argument provided\n",
263 				*argv);
264 				exit(EXIT_ERROR);
265 			}
266 			netpgp_set_homedir(&netpgp, optarg, NULL, 0);
267 			break;
268 		case NUMBITS:
269 			if (optarg == NULL) {
270 				(void) fprintf(stderr,
271 				"%s: no number of bits argument provided\n",
272 				*argv);
273 				exit(EXIT_ERROR);
274 			}
275 			p.numbits = atoi(optarg);
276 			break;
277 		case PASSWDFD:
278 			if (optarg == NULL) {
279 				(void) fprintf(stderr,
280 				"%s: no pass-fd argument provided\n", *argv);
281 				exit(EXIT_ERROR);
282 			}
283 			netpgp_setvar(&netpgp, "pass-fd", optarg);
284 			break;
285 		case RESULTS:
286 			if (optarg == NULL) {
287 				(void) fprintf(stderr,
288 				"No output filename argument provided\n");
289 				exit(EXIT_ERROR);
290 			}
291 			netpgp_setvar(&netpgp, "results", optarg);
292 			break;
293 		case SSHKEYFILE:
294 			netpgp_setvar(&netpgp, "sshkeyfile", optarg);
295 			break;
296 		case OPS_DEBUG:
297 			netpgp_set_debug(optarg);
298 			break;
299 		default:
300 			p.cmd = HELP_CMD;
301 			break;
302 		}
303 	}
304 	/* initialise, and read keys from file */
305 	if (!netpgp_init(&netpgp)) {
306 		if (stat(netpgp_getvar(&netpgp, "homedir"), &st) < 0 &&
307 		    mkdir("homedir", 0700) < 0) {
308 			(void) fprintf(stderr, "can't create home directory '%s'\n",
309 				netpgp_getvar(&netpgp, "homedir"));
310 			exit(EXIT_ERROR);
311 		}
312 	}
313 	/* now do the required action for each of the command line args */
314 	ret = EXIT_SUCCESS;
315 	if (optind == argc) {
316 		if (!netpgp_cmd(&netpgp, &p, NULL)) {
317 			ret = EXIT_FAILURE;
318 		}
319 	} else {
320 		for (i = optind; i < argc; i++) {
321 			if (!netpgp_cmd(&netpgp, &p, argv[i])) {
322 				ret = EXIT_FAILURE;
323 			}
324 		}
325 	}
326 	netpgp_end(&netpgp);
327 	exit(ret);
328 }
329