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