Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1 | #include "builtin.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 2 | #include "tag.h" |
Stefan Beller | 47f351e | 2018-04-11 17:21:06 -0700 | [diff] [blame] | 3 | #include "replace-object.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 4 | #include "object-store.h" |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 5 | |
| 6 | /* |
Ramsay Allan Jones | 446c6fa | 2006-07-29 18:15:47 +0100 | [diff] [blame] | 7 | * A signature file has a very simple fixed format: four lines |
| 8 | * of "object <sha1>" + "type <typename>" + "tag <tagname>" + |
| 9 | * "tagger <committer>", followed by a blank line, a free-form tag |
| 10 | * message and a signature block that git itself doesn't care about, |
| 11 | * but that can be verified with gpg or similar. |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 12 | * |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 13 | * The first four lines are guaranteed to be at least 83 bytes: |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 14 | * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 15 | * shortest possible type-line, "tag .\n" at 6 bytes is the shortest |
| 16 | * single-character-tag line, and "tagger . <> 0 +0000\n" at 20 bytes is |
| 17 | * the shortest possible tagger-line. |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 20 | /* |
| 21 | * We refuse to tag something we can't verify. Just because. |
| 22 | */ |
brian m. carlson | eedc994 | 2018-03-12 02:27:34 +0000 | [diff] [blame] | 23 | static int verify_object(const struct object_id *oid, const char *expected_type) |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 24 | { |
| 25 | int ret = -1; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 26 | enum object_type type; |
Nicolas Pitre | 91d7b8a | 2005-05-20 16:57:28 -0400 | [diff] [blame] | 27 | unsigned long size; |
brian m. carlson | b4f5aca | 2018-03-12 02:27:53 +0000 | [diff] [blame] | 28 | void *buffer = read_object_file(oid, &type, &size); |
Stefan Beller | 1f2e7ce | 2018-04-11 17:21:13 -0700 | [diff] [blame] | 29 | const struct object_id *repl = lookup_replace_object(the_repository, oid); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 30 | |
Nicolas Pitre | 91d7b8a | 2005-05-20 16:57:28 -0400 | [diff] [blame] | 31 | if (buffer) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 32 | if (type == type_from_string(expected_type)) |
brian m. carlson | b383a13 | 2018-03-12 02:27:54 +0000 | [diff] [blame] | 33 | ret = check_object_signature(repl, buffer, size, expected_type); |
Nicolas Pitre | 91d7b8a | 2005-05-20 16:57:28 -0400 | [diff] [blame] | 34 | free(buffer); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 35 | } |
| 36 | return ret; |
| 37 | } |
| 38 | |
| 39 | static int verify_tag(char *buffer, unsigned long size) |
| 40 | { |
| 41 | int typelen; |
| 42 | char type[20]; |
brian m. carlson | eedc994 | 2018-03-12 02:27:34 +0000 | [diff] [blame] | 43 | struct object_id oid; |
| 44 | const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb, *p; |
Brandon Casey | ba26ab9 | 2008-03-31 18:25:23 -0500 | [diff] [blame] | 45 | size_t len; |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 46 | |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 47 | if (size < 84) |
Ramsay Allan Jones | 446c6fa | 2006-07-29 18:15:47 +0100 | [diff] [blame] | 48 | return error("wanna fool me ? you obviously got the size wrong !"); |
Björn Engelmann | cfba045 | 2006-05-23 20:20:09 +0200 | [diff] [blame] | 49 | |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 50 | buffer[size] = 0; |
| 51 | |
| 52 | /* Verify object line */ |
| 53 | object = buffer; |
| 54 | if (memcmp(object, "object ", 7)) |
Ramsay Allan Jones | 446c6fa | 2006-07-29 18:15:47 +0100 | [diff] [blame] | 55 | return error("char%d: does not start with \"object \"", 0); |
Björn Engelmann | cfba045 | 2006-05-23 20:20:09 +0200 | [diff] [blame] | 56 | |
brian m. carlson | eedc994 | 2018-03-12 02:27:34 +0000 | [diff] [blame] | 57 | if (parse_oid_hex(object + 7, &oid, &p)) |
Ramsay Allan Jones | 446c6fa | 2006-07-29 18:15:47 +0100 | [diff] [blame] | 58 | return error("char%d: could not get SHA1 hash", 7); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 59 | |
| 60 | /* Verify type line */ |
brian m. carlson | eedc994 | 2018-03-12 02:27:34 +0000 | [diff] [blame] | 61 | type_line = p + 1; |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 62 | if (memcmp(type_line - 1, "\ntype ", 6)) |
Ramsay Allan Jones | 446c6fa | 2006-07-29 18:15:47 +0100 | [diff] [blame] | 63 | return error("char%d: could not find \"\\ntype \"", 47); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 64 | |
| 65 | /* Verify tag-line */ |
| 66 | tag_line = strchr(type_line, '\n'); |
| 67 | if (!tag_line) |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 68 | return error("char%"PRIuMAX": could not find next \"\\n\"", |
| 69 | (uintmax_t) (type_line - buffer)); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 70 | tag_line++; |
| 71 | if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n') |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 72 | return error("char%"PRIuMAX": no \"tag \" found", |
| 73 | (uintmax_t) (tag_line - buffer)); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 74 | |
| 75 | /* Get the actual type */ |
| 76 | typelen = tag_line - type_line - strlen("type \n"); |
| 77 | if (typelen >= sizeof(type)) |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 78 | return error("char%"PRIuMAX": type too long", |
| 79 | (uintmax_t) (type_line+5 - buffer)); |
Björn Engelmann | cfba045 | 2006-05-23 20:20:09 +0200 | [diff] [blame] | 80 | |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 81 | memcpy(type, type_line+5, typelen); |
| 82 | type[typelen] = 0; |
| 83 | |
| 84 | /* Verify that the object matches */ |
brian m. carlson | eedc994 | 2018-03-12 02:27:34 +0000 | [diff] [blame] | 85 | if (verify_object(&oid, type)) |
| 86 | return error("char%d: could not verify object %s", 7, oid_to_hex(&oid)); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 87 | |
| 88 | /* Verify the tag-name: we don't allow control characters or spaces in it */ |
| 89 | tag_line += 4; |
| 90 | for (;;) { |
| 91 | unsigned char c = *tag_line++; |
| 92 | if (c == '\n') |
| 93 | break; |
| 94 | if (c > ' ') |
| 95 | continue; |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 96 | return error("char%"PRIuMAX": could not verify tag name", |
| 97 | (uintmax_t) (tag_line - buffer)); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Eric W. Biederman | c818566 | 2005-07-14 19:02:10 -0600 | [diff] [blame] | 100 | /* Verify the tagger line */ |
| 101 | tagger_line = tag_line; |
| 102 | |
Brandon Casey | ba26ab9 | 2008-03-31 18:25:23 -0500 | [diff] [blame] | 103 | if (memcmp(tagger_line, "tagger ", 7)) |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 104 | return error("char%"PRIuMAX": could not find \"tagger \"", |
| 105 | (uintmax_t) (tagger_line - buffer)); |
Ramsay Allan Jones | 446c6fa | 2006-07-29 18:15:47 +0100 | [diff] [blame] | 106 | |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 107 | /* |
| 108 | * Check for correct form for name and email |
| 109 | * i.e. " <" followed by "> " on _this_ line |
Brandon Casey | ba26ab9 | 2008-03-31 18:25:23 -0500 | [diff] [blame] | 110 | * No angle brackets within the name or email address fields. |
| 111 | * No spaces within the email address field. |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 112 | */ |
| 113 | tagger_line += 7; |
| 114 | if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) || |
Brandon Casey | ba26ab9 | 2008-03-31 18:25:23 -0500 | [diff] [blame] | 115 | strpbrk(tagger_line, "<>\n") != lb+1 || |
| 116 | strpbrk(lb+2, "><\n ") != rb) |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 117 | return error("char%"PRIuMAX": malformed tagger field", |
| 118 | (uintmax_t) (tagger_line - buffer)); |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 119 | |
| 120 | /* Check for author name, at least one character, space is acceptable */ |
| 121 | if (lb == tagger_line) |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 122 | return error("char%"PRIuMAX": missing tagger name", |
| 123 | (uintmax_t) (tagger_line - buffer)); |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 124 | |
Brandon Casey | ba26ab9 | 2008-03-31 18:25:23 -0500 | [diff] [blame] | 125 | /* timestamp, 1 or more digits followed by space */ |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 126 | tagger_line = rb + 2; |
Brandon Casey | ba26ab9 | 2008-03-31 18:25:23 -0500 | [diff] [blame] | 127 | if (!(len = strspn(tagger_line, "0123456789"))) |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 128 | return error("char%"PRIuMAX": missing tag timestamp", |
| 129 | (uintmax_t) (tagger_line - buffer)); |
Brandon Casey | ba26ab9 | 2008-03-31 18:25:23 -0500 | [diff] [blame] | 130 | tagger_line += len; |
| 131 | if (*tagger_line != ' ') |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 132 | return error("char%"PRIuMAX": malformed tag timestamp", |
| 133 | (uintmax_t) (tagger_line - buffer)); |
Brandon Casey | ba26ab9 | 2008-03-31 18:25:23 -0500 | [diff] [blame] | 134 | tagger_line++; |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 135 | |
| 136 | /* timezone, 5 digits [+-]hhmm, max. 1400 */ |
| 137 | if (!((tagger_line[0] == '+' || tagger_line[0] == '-') && |
Brandon Casey | ba26ab9 | 2008-03-31 18:25:23 -0500 | [diff] [blame] | 138 | strspn(tagger_line+1, "0123456789") == 4 && |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 139 | tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400)) |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 140 | return error("char%"PRIuMAX": malformed tag timezone", |
| 141 | (uintmax_t) (tagger_line - buffer)); |
Brandon Casey | e0aaf78 | 2008-03-27 11:16:04 -0500 | [diff] [blame] | 142 | tagger_line += 6; |
| 143 | |
| 144 | /* Verify the blank line separating the header from the body */ |
| 145 | if (*tagger_line != '\n') |
Jonathan Nieder | 31d713d | 2011-03-16 00:14:22 -0500 | [diff] [blame] | 146 | return error("char%"PRIuMAX": trailing garbage in tag header", |
| 147 | (uintmax_t) (tagger_line - buffer)); |
Eric W. Biederman | c818566 | 2005-07-14 19:02:10 -0600 | [diff] [blame] | 148 | |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 149 | /* The actual stuff afterwards we don't care about.. */ |
| 150 | return 0; |
| 151 | } |
| 152 | |
Linus Torvalds | 112dd51 | 2010-01-22 07:34:44 -0800 | [diff] [blame] | 153 | int cmd_mktag(int argc, const char **argv, const char *prefix) |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 154 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 155 | struct strbuf buf = STRBUF_INIT; |
Patryk Obara | a09c985 | 2018-01-28 01:13:19 +0100 | [diff] [blame] | 156 | struct object_id result; |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 157 | |
| 158 | if (argc != 1) |
Junio C Hamano | 33e8fc8 | 2015-10-16 11:27:42 -0700 | [diff] [blame] | 159 | usage("git mktag"); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 160 | |
Pierre Habouzit | fd17f5b | 2007-09-10 12:35:09 +0200 | [diff] [blame] | 161 | if (strbuf_read(&buf, 0, 4096) < 0) { |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 162 | die_errno("could not read from stdin"); |
Linus Torvalds | b97e3df | 2005-05-29 12:06:32 -0700 | [diff] [blame] | 163 | } |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 164 | |
Pavel Roskin | a9486b0 | 2006-07-10 02:57:51 -0400 | [diff] [blame] | 165 | /* Verify it for some basic sanity: it needs to start with |
| 166 | "object <sha1>\ntype\ntagger " */ |
Pierre Habouzit | fd17f5b | 2007-09-10 12:35:09 +0200 | [diff] [blame] | 167 | if (verify_tag(buf.buf, buf.len) < 0) |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 168 | die("invalid tag signature file"); |
| 169 | |
Patryk Obara | a09c985 | 2018-01-28 01:13:19 +0100 | [diff] [blame] | 170 | if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0) |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 171 | die("unable to write tag file"); |
Björn Engelmann | e7332f9 | 2006-05-23 20:19:04 +0200 | [diff] [blame] | 172 | |
Pierre Habouzit | fd17f5b | 2007-09-10 12:35:09 +0200 | [diff] [blame] | 173 | strbuf_release(&buf); |
Patryk Obara | a09c985 | 2018-01-28 01:13:19 +0100 | [diff] [blame] | 174 | printf("%s\n", oid_to_hex(&result)); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 175 | return 0; |
| 176 | } |