Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Builtin "git verify-tag" |
| 3 | * |
| 4 | * Copyright (c) 2007 Carlos Rica <jasampler@gmail.com> |
| 5 | * |
| 6 | * Based on git-verify-tag.sh |
| 7 | */ |
| 8 | #include "cache.h" |
| 9 | #include "builtin.h" |
| 10 | #include "tag.h" |
| 11 | #include "run-command.h" |
| 12 | #include <signal.h> |
Stephen Boyd | 4855b2a | 2009-07-07 22:15:39 -0700 | [diff] [blame] | 13 | #include "parse-options.h" |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 14 | |
Stephen Boyd | 4855b2a | 2009-07-07 22:15:39 -0700 | [diff] [blame] | 15 | static const char * const verify_tag_usage[] = { |
| 16 | "git verify-tag [-v|--verbose] <tag>...", |
| 17 | NULL |
| 18 | }; |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 19 | |
| 20 | #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----" |
| 21 | |
| 22 | static int run_gpg_verify(const char *buf, unsigned long size, int verbose) |
| 23 | { |
| 24 | struct child_process gpg; |
| 25 | const char *args_gpg[] = {"gpg", "--verify", "FILE", "-", NULL}; |
| 26 | char path[PATH_MAX], *eol; |
| 27 | size_t len; |
| 28 | int fd, ret; |
| 29 | |
| 30 | fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX"); |
| 31 | if (fd < 0) |
| 32 | return error("could not create temporary file '%s': %s", |
| 33 | path, strerror(errno)); |
| 34 | if (write_in_full(fd, buf, size) < 0) |
| 35 | return error("failed writing temporary file '%s': %s", |
| 36 | path, strerror(errno)); |
| 37 | close(fd); |
| 38 | |
| 39 | /* find the length without signature */ |
| 40 | len = 0; |
Johannes Schindelin | fec60a2 | 2007-09-03 17:51:43 +0100 | [diff] [blame] | 41 | while (len < size && prefixcmp(buf + len, PGP_SIGNATURE)) { |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 42 | eol = memchr(buf + len, '\n', size - len); |
| 43 | len += eol ? eol - (buf + len) + 1 : size - len; |
| 44 | } |
| 45 | if (verbose) |
| 46 | write_in_full(1, buf, len); |
| 47 | |
| 48 | memset(&gpg, 0, sizeof(gpg)); |
| 49 | gpg.argv = args_gpg; |
| 50 | gpg.in = -1; |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 51 | args_gpg[2] = path; |
Johannes Sixt | 69fe5ef | 2008-04-02 08:49:59 +0200 | [diff] [blame] | 52 | if (start_command(&gpg)) { |
| 53 | unlink(path); |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 54 | return error("could not run gpg."); |
Johannes Sixt | 69fe5ef | 2008-04-02 08:49:59 +0200 | [diff] [blame] | 55 | } |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 56 | |
| 57 | write_in_full(gpg.in, buf, len); |
| 58 | close(gpg.in); |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 59 | ret = finish_command(&gpg); |
| 60 | |
Alex Riesen | 691f1a2 | 2009-04-29 23:22:56 +0200 | [diff] [blame] | 61 | unlink_or_warn(path); |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | static int verify_tag(const char *name, int verbose) |
| 67 | { |
| 68 | enum object_type type; |
| 69 | unsigned char sha1[20]; |
| 70 | char *buf; |
| 71 | unsigned long size; |
| 72 | int ret; |
| 73 | |
| 74 | if (get_sha1(name, sha1)) |
| 75 | return error("tag '%s' not found.", name); |
| 76 | |
| 77 | type = sha1_object_info(sha1, NULL); |
| 78 | if (type != OBJ_TAG) |
| 79 | return error("%s: cannot verify a non-tag object of type %s.", |
| 80 | name, typename(type)); |
| 81 | |
| 82 | buf = read_sha1_file(sha1, &type, &size); |
| 83 | if (!buf) |
| 84 | return error("%s: unable to read file.", name); |
| 85 | |
| 86 | ret = run_gpg_verify(buf, size, verbose); |
| 87 | |
| 88 | free(buf); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | int cmd_verify_tag(int argc, const char **argv, const char *prefix) |
| 93 | { |
| 94 | int i = 1, verbose = 0, had_error = 0; |
Stephen Boyd | 4855b2a | 2009-07-07 22:15:39 -0700 | [diff] [blame] | 95 | const struct option verify_tag_options[] = { |
René Scharfe | fd03881 | 2010-11-08 18:56:39 +0100 | [diff] [blame^] | 96 | OPT__VERBOSE(&verbose, "be verbose"), |
Stephen Boyd | 4855b2a | 2009-07-07 22:15:39 -0700 | [diff] [blame] | 97 | OPT_END() |
| 98 | }; |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 99 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 100 | git_config(git_default_config, NULL); |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 101 | |
Stephen Boyd | 4855b2a | 2009-07-07 22:15:39 -0700 | [diff] [blame] | 102 | argc = parse_options(argc, argv, prefix, verify_tag_options, |
| 103 | verify_tag_usage, PARSE_OPT_KEEP_ARGV0); |
Olivier Marin | d276189 | 2008-07-28 12:48:44 +0200 | [diff] [blame] | 104 | if (argc <= i) |
Stephen Boyd | 4855b2a | 2009-07-07 22:15:39 -0700 | [diff] [blame] | 105 | usage_with_options(verify_tag_usage, verify_tag_options); |
Olivier Marin | d276189 | 2008-07-28 12:48:44 +0200 | [diff] [blame] | 106 | |
Carlos Rica | 2ae68fc | 2007-07-27 06:07:34 +0200 | [diff] [blame] | 107 | /* sometimes the program was terminated because this signal |
| 108 | * was received in the process of writing the gpg input: */ |
| 109 | signal(SIGPIPE, SIG_IGN); |
| 110 | while (i < argc) |
| 111 | if (verify_tag(argv[i++], verbose)) |
| 112 | had_error = 1; |
| 113 | return had_error; |
| 114 | } |