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--get-key keyid [options] OR\n" 57 "\t--version\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" 63 "\t[--verbose]\n"; 64 65 enum optdefs { 66 /* commands */ 67 LIST_KEYS = 1, 68 FIND_KEY, 69 EXPORT_KEY, 70 IMPORT_KEY, 71 GENERATE_KEY, 72 VERSION_CMD, 73 HELP_CMD, 74 GET_KEY, 75 76 /* options */ 77 SSHKEYS, 78 KEYRING, 79 USERID, 80 HOMEDIR, 81 NUMBITS, 82 VERBOSE, 83 COREDUMPS, 84 PASSWDFD, 85 RESULTS, 86 SSHKEYFILE, 87 88 /* debug */ 89 OPS_DEBUG 90 91 }; 92 93 #define EXIT_ERROR 2 94 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}, 107 /* options */ 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}, 119 { NULL, 0, NULL, 0}, 120 }; 121 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 */ 128 } prog_t; 129 130 131 /* print a usage message */ 132 static void 133 print_usage(const char *usagemsg, char *progname) 134 { 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); 141 } 142 143 /* do a command once for a specified file 'f' */ 144 static int 145 netpgp_cmd(netpgp_t *netpgp, prog_t *p, char *f) 146 { 147 char *key; 148 149 switch (p->cmd) { 150 case LIST_KEYS: 151 return (f == NULL) ? netpgp_list_keys(netpgp) : netpgp_match_keys(netpgp, f, "human", stdout); 152 case FIND_KEY: 153 return netpgp_find_key(netpgp, netpgp_getvar(netpgp, "userid")); 154 case EXPORT_KEY: 155 return netpgp_export_key(netpgp, 156 netpgp_getvar(netpgp, "userid")); 157 case IMPORT_KEY: 158 return netpgp_import_key(netpgp, f); 159 case GENERATE_KEY: 160 return netpgp_generate_key(netpgp, 161 netpgp_getvar(netpgp, "userid"), p->numbits); 162 case GET_KEY: 163 key = netpgp_get_key(netpgp, f); 164 if (key) { 165 printf("%s", key); 166 return 1; 167 } 168 (void) fprintf(stderr, "key '%s' not found\n", f); 169 return 0; 170 case HELP_CMD: 171 default: 172 print_usage(usage, p->progname); 173 exit(EXIT_SUCCESS); 174 } 175 } 176 177 int 178 main(int argc, char **argv) 179 { 180 struct stat st; 181 netpgp_t netpgp; 182 prog_t p; 183 int optindex; 184 int ret; 185 int ch; 186 int i; 187 188 (void) memset(&p, 0x0, sizeof(p)); 189 (void) memset(&netpgp, 0x0, sizeof(netpgp)); 190 p.progname = argv[0]; 191 p.numbits = DEFAULT_NUMBITS; 192 if (argc < 2) { 193 print_usage(usage, p.progname); 194 exit(EXIT_ERROR); 195 } 196 /* set some defaults */ 197 netpgp_set_homedir(&netpgp, getenv("HOME"), "/.gnupg", 1); 198 netpgp_setvar(&netpgp, "sshkeydir", "/etc/ssh"); 199 optindex = 0; 200 while ((ch = getopt_long(argc, argv, "", options, &optindex)) != -1) { 201 switch (options[optindex].val) { 202 case LIST_KEYS: 203 p.cmd = options[optindex].val; 204 break; 205 case COREDUMPS: 206 netpgp_setvar(&netpgp, "coredumps", "allowed"); 207 p.cmd = options[optindex].val; 208 break; 209 case GENERATE_KEY: 210 netpgp_setvar(&netpgp, "userid checks", "skip"); 211 p.cmd = options[optindex].val; 212 break; 213 case FIND_KEY: 214 case EXPORT_KEY: 215 case IMPORT_KEY: 216 case GET_KEY: 217 case HELP_CMD: 218 p.cmd = options[optindex].val; 219 break; 220 case VERSION_CMD: 221 printf( 222 "%s\nAll bug reports, praise and chocolate, please, to:\n%s\n", 223 netpgp_get_info("version"), 224 netpgp_get_info("maintainer")); 225 exit(EXIT_SUCCESS); 226 /* options */ 227 case SSHKEYS: 228 netpgp_setvar(&netpgp, "ssh keys", "1"); 229 break; 230 case KEYRING: 231 if (optarg == NULL) { 232 (void) fprintf(stderr, 233 "%s: No keyring argument provided\n", 234 *argv); 235 exit(EXIT_ERROR); 236 } 237 snprintf(p.keyring, sizeof(p.keyring), "%s", optarg); 238 break; 239 case USERID: 240 if (optarg == NULL) { 241 (void) fprintf(stderr, 242 "%s: no userid argument provided\n", 243 *argv); 244 exit(EXIT_ERROR); 245 } 246 netpgp_setvar(&netpgp, "userid", optarg); 247 break; 248 case VERBOSE: 249 netpgp_incvar(&netpgp, "verbose", 1); 250 break; 251 case HOMEDIR: 252 if (optarg == NULL) { 253 (void) fprintf(stderr, 254 "%s: no home directory argument provided\n", 255 *argv); 256 exit(EXIT_ERROR); 257 } 258 netpgp_set_homedir(&netpgp, optarg, NULL, 0); 259 break; 260 case NUMBITS: 261 if (optarg == NULL) { 262 (void) fprintf(stderr, 263 "%s: no number of bits argument provided\n", 264 *argv); 265 exit(EXIT_ERROR); 266 } 267 p.numbits = atoi(optarg); 268 break; 269 case PASSWDFD: 270 if (optarg == NULL) { 271 (void) fprintf(stderr, 272 "%s: no pass-fd argument provided\n", *argv); 273 exit(EXIT_ERROR); 274 } 275 netpgp_setvar(&netpgp, "pass-fd", optarg); 276 break; 277 case RESULTS: 278 if (optarg == NULL) { 279 (void) fprintf(stderr, 280 "No output filename argument provided\n"); 281 exit(EXIT_ERROR); 282 } 283 netpgp_setvar(&netpgp, "results", optarg); 284 break; 285 case SSHKEYFILE: 286 netpgp_setvar(&netpgp, "sshkeyfile", optarg); 287 break; 288 case OPS_DEBUG: 289 netpgp_set_debug(optarg); 290 break; 291 default: 292 p.cmd = HELP_CMD; 293 break; 294 } 295 } 296 /* initialise, and read keys from file */ 297 if (!netpgp_init(&netpgp)) { 298 if (stat(netpgp_getvar(&netpgp, "homedir"), &st) < 0 && 299 mkdir("homedir", 0700) < 0) { 300 (void) fprintf(stderr, "can't create home directory '%s'\n", 301 netpgp_getvar(&netpgp, "homedir")); 302 exit(EXIT_ERROR); 303 } 304 } 305 /* now do the required action for each of the command line args */ 306 ret = EXIT_SUCCESS; 307 if (optind == argc) { 308 if (!netpgp_cmd(&netpgp, &p, NULL)) { 309 ret = EXIT_FAILURE; 310 } 311 } else { 312 for (i = optind; i < argc; i++) { 313 if (!netpgp_cmd(&netpgp, &p, argv[i])) { 314 ret = EXIT_FAILURE; 315 } 316 } 317 } 318 netpgp_end(&netpgp); 319 exit(ret); 320 } 321