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_list_keys(netpgp, f); 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 netpgp_t netpgp; 181 prog_t p; 182 int optindex; 183 int ret; 184 int ch; 185 int i; 186 187 (void) memset(&p, 0x0, sizeof(p)); 188 (void) memset(&netpgp, 0x0, sizeof(netpgp)); 189 p.progname = argv[0]; 190 p.numbits = DEFAULT_NUMBITS; 191 if (argc < 2) { 192 print_usage(usage, p.progname); 193 exit(EXIT_ERROR); 194 } 195 /* set some defaults */ 196 netpgp_set_homedir(&netpgp, getenv("HOME"), "/.gnupg", 1); 197 netpgp_setvar(&netpgp, "sshkeydir", "/etc/ssh"); 198 optindex = 0; 199 while ((ch = getopt_long(argc, argv, "", options, &optindex)) != -1) { 200 switch (options[optindex].val) { 201 case LIST_KEYS: 202 p.cmd = options[optindex].val; 203 break; 204 case COREDUMPS: 205 netpgp_setvar(&netpgp, "coredumps", "allowed"); 206 p.cmd = options[optindex].val; 207 break; 208 case GENERATE_KEY: 209 netpgp_setvar(&netpgp, "userid checks", "skip"); 210 p.cmd = options[optindex].val; 211 break; 212 case FIND_KEY: 213 case EXPORT_KEY: 214 case IMPORT_KEY: 215 case GET_KEY: 216 case HELP_CMD: 217 p.cmd = options[optindex].val; 218 break; 219 case VERSION_CMD: 220 printf( 221 "%s\nAll bug reports, praise and chocolate, please, to:\n%s\n", 222 netpgp_get_info("version"), 223 netpgp_get_info("maintainer")); 224 exit(EXIT_SUCCESS); 225 /* options */ 226 case SSHKEYS: 227 netpgp_setvar(&netpgp, "ssh keys", "1"); 228 break; 229 case KEYRING: 230 if (optarg == NULL) { 231 (void) fprintf(stderr, 232 "%s: No keyring argument provided\n", 233 *argv); 234 exit(EXIT_ERROR); 235 } 236 snprintf(p.keyring, sizeof(p.keyring), "%s", optarg); 237 break; 238 case USERID: 239 if (optarg == NULL) { 240 (void) fprintf(stderr, 241 "%s: no userid argument provided\n", 242 *argv); 243 exit(EXIT_ERROR); 244 } 245 netpgp_setvar(&netpgp, "userid", optarg); 246 break; 247 case VERBOSE: 248 netpgp_incvar(&netpgp, "verbose", 1); 249 break; 250 case HOMEDIR: 251 if (optarg == NULL) { 252 (void) fprintf(stderr, 253 "%s: no home directory argument provided\n", 254 *argv); 255 exit(EXIT_ERROR); 256 } 257 netpgp_set_homedir(&netpgp, optarg, NULL, 0); 258 break; 259 case NUMBITS: 260 if (optarg == NULL) { 261 (void) fprintf(stderr, 262 "%s: no number of bits argument provided\n", 263 *argv); 264 exit(EXIT_ERROR); 265 } 266 p.numbits = atoi(optarg); 267 break; 268 case PASSWDFD: 269 if (optarg == NULL) { 270 (void) fprintf(stderr, 271 "%s: no pass-fd argument provided\n", *argv); 272 exit(EXIT_ERROR); 273 } 274 netpgp_setvar(&netpgp, "pass-fd", optarg); 275 break; 276 case RESULTS: 277 if (optarg == NULL) { 278 (void) fprintf(stderr, 279 "No output filename argument provided\n"); 280 exit(EXIT_ERROR); 281 } 282 netpgp_setvar(&netpgp, "results", optarg); 283 break; 284 case SSHKEYFILE: 285 netpgp_setvar(&netpgp, "sshkeyfile", optarg); 286 break; 287 case OPS_DEBUG: 288 netpgp_set_debug(optarg); 289 break; 290 default: 291 p.cmd = HELP_CMD; 292 break; 293 } 294 } 295 /* initialise, and read keys from file */ 296 if (!netpgp_init(&netpgp)) { 297 printf("can't initialise\n"); 298 exit(EXIT_ERROR); 299 } 300 /* now do the required action for each of the command line args */ 301 ret = EXIT_SUCCESS; 302 if (optind == argc) { 303 if (!netpgp_cmd(&netpgp, &p, NULL)) { 304 ret = EXIT_FAILURE; 305 } 306 } else { 307 for (i = optind; i < argc; i++) { 308 if (!netpgp_cmd(&netpgp, &p, argv[i])) { 309 ret = EXIT_FAILURE; 310 } 311 } 312 } 313 netpgp_end(&netpgp); 314 exit(ret); 315 } 316