| @@ -117,48 +117,61 @@ static struct option long_options[]= | | | @@ -117,48 +117,61 @@ static struct option long_options[]= |
117 | | | 117 | |
118 | /* debug */ | | 118 | /* debug */ |
119 | { "debug", required_argument, NULL, OPS_DEBUG }, | | 119 | { "debug", required_argument, NULL, OPS_DEBUG }, |
120 | | | 120 | |
121 | { 0,0,0,0}, | | 121 | { 0,0,0,0}, |
122 | }; | | 122 | }; |
123 | | | 123 | |
124 | static void print_usage(const char* usagemsg, char* progname) | | 124 | static void print_usage(const char* usagemsg, char* progname) |
125 | { | | 125 | { |
126 | fprintf(stderr, "\nUsage: "); | | 126 | fprintf(stderr, "\nUsage: "); |
127 | fprintf(stderr, usagemsg, basename(progname)); | | 127 | fprintf(stderr, usagemsg, basename(progname)); |
128 | } | | 128 | } |
129 | | | 129 | |
| | | 130 | /* wrapper to get a pass phrase from the user */ |
| | | 131 | static void |
| | | 132 | get_pass_phrase(char *phrase, size_t size) |
| | | 133 | { |
| | | 134 | char *p; |
| | | 135 | |
| | | 136 | if ((p = getpass("openpgp pass phrase: ")) == NULL) { |
| | | 137 | exit(EXIT_ERROR); |
| | | 138 | } |
| | | 139 | (void) snprintf(phrase, size, "%s", p); |
| | | 140 | } |
| | | 141 | |
130 | int main(int argc, char **argv) | | 142 | int main(int argc, char **argv) |
131 | { | | 143 | { |
132 | int optindex=0; | | 144 | int optindex=0; |
133 | int ch=0; | | 145 | int ch=0; |
134 | int cmd=0; | | 146 | int cmd=0; |
135 | int armour=0; | | 147 | int armour=0; |
136 | int fd=0; | | 148 | int fd=0; |
137 | | | 149 | |
138 | pname=argv[0]; | | 150 | pname=argv[0]; |
139 | char opt_keyring[MAXBUF+1]=""; | | 151 | char opt_keyring[MAXBUF+1]=""; |
140 | char opt_userid[MAXBUF+1]=""; | | 152 | char opt_userid[MAXBUF+1]=""; |
141 | char opt_passphrase[MAXBUF+1]=""; | | 153 | char opt_passphrase[MAXBUF+1]=""; |
142 | char opt_filename[MAXBUF+1]=""; | | 154 | char opt_filename[MAXBUF+1]=""; |
143 | char opt_homedir[MAXBUF+1]=""; | | 155 | char opt_homedir[MAXBUF+1]=""; |
144 | | | 156 | |
145 | int got_homedir=0; | | 157 | int got_homedir=0; |
146 | int got_keyring=0; | | 158 | int got_keyring=0; |
147 | int got_userid=0; | | 159 | int got_userid=0; |
148 | int got_passphrase=0; | | 160 | int got_passphrase=0; |
149 | int got_filename=0; | | 161 | int got_filename=0; |
150 | int got_numbits=0; | | 162 | int got_numbits=0; |
151 | int numbits=DEFAULT_NUMBITS; | | 163 | int numbits=DEFAULT_NUMBITS; |
| | | 164 | int ex; |
152 | char outputfilename[MAXBUF+1]=""; | | 165 | char outputfilename[MAXBUF+1]=""; |
153 | ops_keyring_t* myring=NULL; | | 166 | ops_keyring_t* myring=NULL; |
154 | char myring_name[MAXBUF+1]=""; | | 167 | char myring_name[MAXBUF+1]=""; |
155 | ops_keyring_t* pubring=NULL; | | 168 | ops_keyring_t* pubring=NULL; |
156 | char pubring_name[MAXBUF+1]=""; | | 169 | char pubring_name[MAXBUF+1]=""; |
157 | ops_keyring_t* secring=NULL; | | 170 | ops_keyring_t* secring=NULL; |
158 | char secring_name[MAXBUF+1]=""; | | 171 | char secring_name[MAXBUF+1]=""; |
159 | const ops_keydata_t* keydata=NULL; | | 172 | const ops_keydata_t* keydata=NULL; |
160 | char *suffix=NULL; | | 173 | char *suffix=NULL; |
161 | char *dir=NULL; | | 174 | char *dir=NULL; |
162 | char default_homedir[MAXBUF+1]=""; | | 175 | char default_homedir[MAXBUF+1]=""; |
163 | ops_boolean_t overwrite=ops_true; | | 176 | ops_boolean_t overwrite=ops_true; |
164 | ops_keydata_t* mykeydata=NULL; | | 177 | ops_keydata_t* mykeydata=NULL; |
| @@ -231,26 +244,29 @@ int main(int argc, char **argv) | | | @@ -231,26 +244,29 @@ int main(int argc, char **argv) |
231 | cmd=LIST_PACKETS; | | 244 | cmd=LIST_PACKETS; |
232 | break; | | 245 | break; |
233 | | | 246 | |
234 | // option | | 247 | // option |
235 | | | 248 | |
236 | case KEYRING: | | 249 | case KEYRING: |
237 | assert(optarg); | | 250 | assert(optarg); |
238 | snprintf(opt_keyring,MAXBUF,"%s",optarg); | | 251 | snprintf(opt_keyring,MAXBUF,"%s",optarg); |
239 | got_keyring=1; | | 252 | got_keyring=1; |
240 | break; | | 253 | break; |
241 | | | 254 | |
242 | case USERID: | | 255 | case USERID: |
243 | assert(optarg); | | 256 | assert(optarg); |
| | | 257 | if (ops_get_debug_level(__FILE__)) { |
| | | 258 | (void) fprintf(stderr, "user_id is '%s'\n", optarg); |
| | | 259 | } |
244 | snprintf(opt_userid,MAXBUF,"%s",optarg); | | 260 | snprintf(opt_userid,MAXBUF,"%s",optarg); |
245 | got_userid=1; | | 261 | got_userid=1; |
246 | break; | | 262 | break; |
247 | | | 263 | |
248 | case PASSPHRASE: | | 264 | case PASSPHRASE: |
249 | assert(optarg); | | 265 | assert(optarg); |
250 | snprintf(opt_passphrase,MAXBUF,"%s",optarg); | | 266 | snprintf(opt_passphrase,MAXBUF,"%s",optarg); |
251 | got_passphrase=1; | | 267 | got_passphrase=1; |
252 | break; | | 268 | break; |
253 | | | 269 | |
254 | case FILENAME: | | 270 | case FILENAME: |
255 | assert(optarg); | | 271 | assert(optarg); |
256 | snprintf(opt_filename,MAXBUF,"%s",optarg); | | 272 | snprintf(opt_filename,MAXBUF,"%s",optarg); |
| @@ -335,41 +351,33 @@ int main(int argc, char **argv) | | | @@ -335,41 +351,33 @@ int main(int argc, char **argv) |
335 | case LIST_KEYS: | | 351 | case LIST_KEYS: |
336 | ops_keyring_list((got_keyring) ? myring : pubring); | | 352 | ops_keyring_list((got_keyring) ? myring : pubring); |
337 | break; | | 353 | break; |
338 | | | 354 | |
339 | //case LIST_PACKETS: | | 355 | //case LIST_PACKETS: |
340 | | | 356 | |
341 | case FIND_KEY: | | 357 | case FIND_KEY: |
342 | if (!got_userid) | | 358 | if (!got_userid) |
343 | { | | 359 | { |
344 | print_usage(usage_find_key,pname); | | 360 | print_usage(usage_find_key,pname); |
345 | exit(EXIT_ERROR); | | 361 | exit(EXIT_ERROR); |
346 | } | | 362 | } |
347 | | | 363 | |
348 | // fprintf(stderr,"userid: %s\n", opt_userid); | | 364 | if (ops_get_debug_level(__FILE__)) { |
349 | //keydata=ops_keydata_new(); | | 365 | (void) fprintf(stderr,"userid: %s\n", opt_userid); |
350 | if (!got_keyring) | | 366 | } |
351 | keydata=ops_keyring_find_key_by_userid(pubring, opt_userid); | | 367 | keydata = ops_keyring_find_key_by_userid((got_keyring) ? |
352 | else | | 368 | myring : pubring, opt_userid); |
353 | keydata=ops_keyring_find_key_by_userid(myring, opt_userid); | | 369 | exit((keydata) ? EXIT_FAILURE : EXIT_SUCCESS); |
354 | // ops_keyring_free(&keyring); | | 370 | // ops_keyring_free(&keyring); |
355 | if (keydata) | | | |
356 | { | | | |
357 | exit(EXIT_FAILURE); | | | |
358 | } | | | |
359 | else | | | |
360 | { | | | |
361 | exit(EXIT_SUCCESS); | | | |
362 | } | | | |
363 | break; | | 371 | break; |
364 | | | 372 | |
365 | case EXPORT_KEY: | | 373 | case EXPORT_KEY: |
366 | if (!got_keyring || !got_userid) | | 374 | if (!got_keyring || !got_userid) |
367 | { | | 375 | { |
368 | print_usage(usage_export_key,pname); | | 376 | print_usage(usage_export_key,pname); |
369 | exit(EXIT_ERROR); | | 377 | exit(EXIT_ERROR); |
370 | } | | 378 | } |
371 | | | 379 | |
372 | if (got_keyring) | | 380 | if (got_keyring) |
373 | keydata=ops_keyring_find_key_by_userid(myring, opt_userid); | | 381 | keydata=ops_keyring_find_key_by_userid(myring, opt_userid); |
374 | else | | 382 | else |
375 | keydata=ops_keyring_find_key_by_userid(pubring, opt_userid); | | 383 | keydata=ops_keyring_find_key_by_userid(pubring, opt_userid); |
| @@ -496,26 +504,32 @@ int main(int argc, char **argv) | | | @@ -496,26 +504,32 @@ int main(int argc, char **argv) |
496 | { | | 504 | { |
497 | print_usage(usage_sign, pname); | | 505 | print_usage(usage_sign, pname); |
498 | exit(EXIT_ERROR); | | 506 | exit(EXIT_ERROR); |
499 | } | | 507 | } |
500 | | | 508 | |
501 | // get key with which to sign | | 509 | // get key with which to sign |
502 | keydata=ops_keyring_find_key_by_userid(secring,opt_userid); | | 510 | keydata=ops_keyring_find_key_by_userid(secring,opt_userid); |
503 | if (!keydata) | | 511 | if (!keydata) |
504 | { | | 512 | { |
505 | fprintf(stderr,"Userid '%s' not found in keyring\n", | | 513 | fprintf(stderr,"Userid '%s' not found in keyring\n", |
506 | opt_userid); | | 514 | opt_userid); |
507 | exit(EXIT_ERROR); | | 515 | exit(EXIT_ERROR); |
508 | } | | 516 | } |
| | | 517 | |
| | | 518 | /* get the passphrase */ |
| | | 519 | if (opt_passphrase[0] == 0x0) { |
| | | 520 | get_pass_phrase(opt_passphrase, sizeof(opt_passphrase)); |
| | | 521 | } |
| | | 522 | |
509 | // now decrypt key | | 523 | // now decrypt key |
510 | skey=ops_decrypt_secret_key_from_data(keydata,opt_passphrase); | | 524 | skey=ops_decrypt_secret_key_from_data(keydata,opt_passphrase); |
511 | assert(skey); | | 525 | assert(skey); |
512 | | | 526 | |
513 | // sign file | | 527 | // sign file |
514 | overwrite=ops_true; | | 528 | overwrite=ops_true; |
515 | ops_sign_file(opt_filename, NULL, skey, armour, overwrite); | | 529 | ops_sign_file(opt_filename, NULL, skey, armour, overwrite); |
516 | break; | | 530 | break; |
517 | | | 531 | |
518 | case CLEARSIGN: | | 532 | case CLEARSIGN: |
519 | if (!got_filename || !got_userid) | | 533 | if (!got_filename || !got_userid) |
520 | { | | 534 | { |
521 | print_usage(usage_clearsign, pname); | | 535 | print_usage(usage_clearsign, pname); |
| @@ -539,33 +553,36 @@ int main(int argc, char **argv) | | | @@ -539,33 +553,36 @@ int main(int argc, char **argv) |
539 | break; | | 553 | break; |
540 | | | 554 | |
541 | case VERIFY: | | 555 | case VERIFY: |
542 | if (!got_filename) | | 556 | if (!got_filename) |
543 | { | | 557 | { |
544 | print_usage(usage_verify, pname); | | 558 | print_usage(usage_verify, pname); |
545 | exit(EXIT_ERROR); | | 559 | exit(EXIT_ERROR); |
546 | } | | 560 | } |
547 | | | 561 | |
548 | validate_result=ops_mallocz(sizeof (ops_validate_result_t)); | | 562 | validate_result=ops_mallocz(sizeof (ops_validate_result_t)); |
549 | | | 563 | |
550 | if (ops_validate_file(validate_result, opt_filename, armour, pubring)==ops_true) | | 564 | if (ops_validate_file(validate_result, opt_filename, armour, pubring)==ops_true) |
551 | { | | 565 | { |
552 | fprintf(stdout, "Verify OK\n"); | | 566 | printf("Good signature for \"%s\"\n", opt_filename); |
| | | 567 | ex = EXIT_SUCCESS; |
553 | } | | 568 | } |
554 | else | | 569 | else |
555 | { | | 570 | { |
556 | fprintf(stdout, "Verify FAIL: %d invalid signatures, %d unknown signatures\n", validate_result->invalid_count, validate_result->unknown_signer_count); | | 571 | printf("\"%s\": verification failure: %d invalid signatures, %d unknown signatures\n", opt_filename, validate_result->invalid_count, validate_result->unknown_signer_count); |
| | | 572 | ex = EXIT_FAILURE; |
557 | } | | 573 | } |
558 | ops_validate_result_free(validate_result); | | 574 | ops_validate_result_free(validate_result); |
| | | 575 | exit(ex); |
559 | break; | | 576 | break; |
560 | | | 577 | |
561 | case LIST_PACKETS: | | 578 | case LIST_PACKETS: |
562 | if (!got_filename) | | 579 | if (!got_filename) |
563 | { | | 580 | { |
564 | print_usage(usage_list_packets, pname); | | 581 | print_usage(usage_list_packets, pname); |
565 | exit(EXIT_ERROR); | | 582 | exit(EXIT_ERROR); |
566 | } | | 583 | } |
567 | ops_list_packets(opt_filename, armour, pubring, callback_cmd_get_passphrase_from_cmdline); | | 584 | ops_list_packets(opt_filename, armour, pubring, callback_cmd_get_passphrase_from_cmdline); |
568 | break; | | 585 | break; |
569 | | | 586 | |
570 | default: | | 587 | default: |
571 | print_usage(usage,pname); | | 588 | print_usage(usage,pname); |