]>
Commit | Line | Data |
---|---|---|
c2e86add | 1 | #include "builtin.h" |
8e440259 | 2 | #include "tag.h" |
ec4465ad LT |
3 | |
4 | /* | |
446c6fae RJ |
5 | * A signature file has a very simple fixed format: four lines |
6 | * of "object <sha1>" + "type <typename>" + "tag <tagname>" + | |
7 | * "tagger <committer>", followed by a blank line, a free-form tag | |
8 | * message and a signature block that git itself doesn't care about, | |
9 | * but that can be verified with gpg or similar. | |
ec4465ad | 10 | * |
e0aaf781 | 11 | * The first four lines are guaranteed to be at least 83 bytes: |
ec4465ad | 12 | * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the |
e0aaf781 BC |
13 | * shortest possible type-line, "tag .\n" at 6 bytes is the shortest |
14 | * single-character-tag line, and "tagger . <> 0 +0000\n" at 20 bytes is | |
15 | * the shortest possible tagger-line. | |
ec4465ad LT |
16 | */ |
17 | ||
ec4465ad LT |
18 | /* |
19 | * We refuse to tag something we can't verify. Just because. | |
20 | */ | |
cc400f50 | 21 | static int verify_object(const unsigned char *sha1, const char *expected_type) |
ec4465ad LT |
22 | { |
23 | int ret = -1; | |
21666f1a | 24 | enum object_type type; |
91d7b8af | 25 | unsigned long size; |
4bbf5a26 JH |
26 | void *buffer = read_sha1_file(sha1, &type, &size); |
27 | const unsigned char *repl = lookup_replace_object(sha1); | |
ec4465ad | 28 | |
91d7b8af | 29 | if (buffer) { |
21666f1a | 30 | if (type == type_from_string(expected_type)) |
cc400f50 | 31 | ret = check_sha1_signature(repl, buffer, size, expected_type); |
91d7b8af | 32 | free(buffer); |
ec4465ad LT |
33 | } |
34 | return ret; | |
35 | } | |
36 | ||
37 | static int verify_tag(char *buffer, unsigned long size) | |
38 | { | |
39 | int typelen; | |
40 | char type[20]; | |
41 | unsigned char sha1[20]; | |
e0aaf781 | 42 | const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb; |
ba26ab99 | 43 | size_t len; |
ec4465ad | 44 | |
e0aaf781 | 45 | if (size < 84) |
446c6fae | 46 | return error("wanna fool me ? you obviously got the size wrong !"); |
cfba0459 | 47 | |
ec4465ad LT |
48 | buffer[size] = 0; |
49 | ||
50 | /* Verify object line */ | |
51 | object = buffer; | |
52 | if (memcmp(object, "object ", 7)) | |
446c6fae | 53 | return error("char%d: does not start with \"object \"", 0); |
cfba0459 | 54 | |
ec4465ad | 55 | if (get_sha1_hex(object + 7, sha1)) |
446c6fae | 56 | return error("char%d: could not get SHA1 hash", 7); |
ec4465ad LT |
57 | |
58 | /* Verify type line */ | |
59 | type_line = object + 48; | |
60 | if (memcmp(type_line - 1, "\ntype ", 6)) | |
446c6fae | 61 | return error("char%d: could not find \"\\ntype \"", 47); |
ec4465ad LT |
62 | |
63 | /* Verify tag-line */ | |
64 | tag_line = strchr(type_line, '\n'); | |
65 | if (!tag_line) | |
31d713d0 JN |
66 | return error("char%"PRIuMAX": could not find next \"\\n\"", |
67 | (uintmax_t) (type_line - buffer)); | |
ec4465ad LT |
68 | tag_line++; |
69 | if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n') | |
31d713d0 JN |
70 | return error("char%"PRIuMAX": no \"tag \" found", |
71 | (uintmax_t) (tag_line - buffer)); | |
ec4465ad LT |
72 | |
73 | /* Get the actual type */ | |
74 | typelen = tag_line - type_line - strlen("type \n"); | |
75 | if (typelen >= sizeof(type)) | |
31d713d0 JN |
76 | return error("char%"PRIuMAX": type too long", |
77 | (uintmax_t) (type_line+5 - buffer)); | |
cfba0459 | 78 | |
ec4465ad LT |
79 | memcpy(type, type_line+5, typelen); |
80 | type[typelen] = 0; | |
81 | ||
82 | /* Verify that the object matches */ | |
ec4465ad | 83 | if (verify_object(sha1, type)) |
446c6fae | 84 | return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1)); |
ec4465ad LT |
85 | |
86 | /* Verify the tag-name: we don't allow control characters or spaces in it */ | |
87 | tag_line += 4; | |
88 | for (;;) { | |
89 | unsigned char c = *tag_line++; | |
90 | if (c == '\n') | |
91 | break; | |
92 | if (c > ' ') | |
93 | continue; | |
31d713d0 JN |
94 | return error("char%"PRIuMAX": could not verify tag name", |
95 | (uintmax_t) (tag_line - buffer)); | |
ec4465ad LT |
96 | } |
97 | ||
c818566d EB |
98 | /* Verify the tagger line */ |
99 | tagger_line = tag_line; | |
100 | ||
ba26ab99 | 101 | if (memcmp(tagger_line, "tagger ", 7)) |
31d713d0 JN |
102 | return error("char%"PRIuMAX": could not find \"tagger \"", |
103 | (uintmax_t) (tagger_line - buffer)); | |
e0aaf781 BC |
104 | |
105 | /* | |
106 | * Check for correct form for name and email | |
107 | * i.e. " <" followed by "> " on _this_ line | |
ba26ab99 BC |
108 | * No angle brackets within the name or email address fields. |
109 | * No spaces within the email address field. | |
e0aaf781 BC |
110 | */ |
111 | tagger_line += 7; | |
112 | if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) || | |
ba26ab99 BC |
113 | strpbrk(tagger_line, "<>\n") != lb+1 || |
114 | strpbrk(lb+2, "><\n ") != rb) | |
31d713d0 JN |
115 | return error("char%"PRIuMAX": malformed tagger field", |
116 | (uintmax_t) (tagger_line - buffer)); | |
e0aaf781 BC |
117 | |
118 | /* Check for author name, at least one character, space is acceptable */ | |
119 | if (lb == tagger_line) | |
31d713d0 JN |
120 | return error("char%"PRIuMAX": missing tagger name", |
121 | (uintmax_t) (tagger_line - buffer)); | |
e0aaf781 | 122 | |
ba26ab99 | 123 | /* timestamp, 1 or more digits followed by space */ |
e0aaf781 | 124 | tagger_line = rb + 2; |
ba26ab99 | 125 | if (!(len = strspn(tagger_line, "0123456789"))) |
31d713d0 JN |
126 | return error("char%"PRIuMAX": missing tag timestamp", |
127 | (uintmax_t) (tagger_line - buffer)); | |
ba26ab99 BC |
128 | tagger_line += len; |
129 | if (*tagger_line != ' ') | |
31d713d0 JN |
130 | return error("char%"PRIuMAX": malformed tag timestamp", |
131 | (uintmax_t) (tagger_line - buffer)); | |
ba26ab99 | 132 | tagger_line++; |
446c6fae | 133 | |
e0aaf781 BC |
134 | /* timezone, 5 digits [+-]hhmm, max. 1400 */ |
135 | if (!((tagger_line[0] == '+' || tagger_line[0] == '-') && | |
ba26ab99 | 136 | strspn(tagger_line+1, "0123456789") == 4 && |
e0aaf781 | 137 | tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400)) |
31d713d0 JN |
138 | return error("char%"PRIuMAX": malformed tag timezone", |
139 | (uintmax_t) (tagger_line - buffer)); | |
e0aaf781 BC |
140 | tagger_line += 6; |
141 | ||
142 | /* Verify the blank line separating the header from the body */ | |
143 | if (*tagger_line != '\n') | |
31d713d0 JN |
144 | return error("char%"PRIuMAX": trailing garbage in tag header", |
145 | (uintmax_t) (tagger_line - buffer)); | |
c818566d | 146 | |
ec4465ad LT |
147 | /* The actual stuff afterwards we don't care about.. */ |
148 | return 0; | |
149 | } | |
150 | ||
112dd514 | 151 | int cmd_mktag(int argc, const char **argv, const char *prefix) |
ec4465ad | 152 | { |
f285a2d7 | 153 | struct strbuf buf = STRBUF_INIT; |
ec4465ad LT |
154 | unsigned char result_sha1[20]; |
155 | ||
156 | if (argc != 1) | |
34263de0 | 157 | usage("git mktag < signaturefile"); |
ec4465ad | 158 | |
fd17f5b5 | 159 | if (strbuf_read(&buf, 0, 4096) < 0) { |
0721c314 | 160 | die_errno("could not read from stdin"); |
b97e3dfa | 161 | } |
ec4465ad | 162 | |
a9486b02 PR |
163 | /* Verify it for some basic sanity: it needs to start with |
164 | "object <sha1>\ntype\ntagger " */ | |
fd17f5b5 | 165 | if (verify_tag(buf.buf, buf.len) < 0) |
ec4465ad LT |
166 | die("invalid tag signature file"); |
167 | ||
fd17f5b5 | 168 | if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0) |
ec4465ad | 169 | die("unable to write tag file"); |
e7332f96 | 170 | |
fd17f5b5 | 171 | strbuf_release(&buf); |
ec4465ad LT |
172 | printf("%s\n", sha1_to_hex(result_sha1)); |
173 | return 0; | |
174 | } |