]>
Commit | Line | Data |
---|---|---|
ec4465ad | 1 | #include "cache.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 LT |
10 | * |
11 | * The first three lines are guaranteed to be at least 63 bytes: | |
12 | * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the | |
13 | * shortest possible type-line, and "tag .\n" at 6 bytes is the | |
a6080a0a | 14 | * shortest single-character-tag line. |
ec4465ad LT |
15 | */ |
16 | ||
ec4465ad LT |
17 | /* |
18 | * We refuse to tag something we can't verify. Just because. | |
19 | */ | |
20 | static int verify_object(unsigned char *sha1, const char *expected_type) | |
21 | { | |
22 | int ret = -1; | |
21666f1a | 23 | enum object_type type; |
91d7b8af | 24 | unsigned long size; |
21666f1a | 25 | void *buffer = read_sha1_file(sha1, &type, &size); |
ec4465ad | 26 | |
91d7b8af | 27 | if (buffer) { |
21666f1a NP |
28 | if (type == type_from_string(expected_type)) |
29 | ret = check_sha1_signature(sha1, buffer, size, expected_type); | |
91d7b8af | 30 | free(buffer); |
ec4465ad LT |
31 | } |
32 | return ret; | |
33 | } | |
34 | ||
579d1fbf RJ |
35 | #ifdef NO_C99_FORMAT |
36 | #define PD_FMT "%d" | |
37 | #else | |
38 | #define PD_FMT "%td" | |
39 | #endif | |
40 | ||
ec4465ad LT |
41 | static int verify_tag(char *buffer, unsigned long size) |
42 | { | |
43 | int typelen; | |
44 | char type[20]; | |
45 | unsigned char sha1[20]; | |
c818566d | 46 | const char *object, *type_line, *tag_line, *tagger_line; |
ec4465ad | 47 | |
e7332f96 | 48 | if (size < 64) |
446c6fae | 49 | return error("wanna fool me ? you obviously got the size wrong !"); |
cfba0459 | 50 | |
ec4465ad LT |
51 | buffer[size] = 0; |
52 | ||
53 | /* Verify object line */ | |
54 | object = buffer; | |
55 | if (memcmp(object, "object ", 7)) | |
446c6fae | 56 | return error("char%d: does not start with \"object \"", 0); |
cfba0459 | 57 | |
ec4465ad | 58 | if (get_sha1_hex(object + 7, sha1)) |
446c6fae | 59 | return error("char%d: could not get SHA1 hash", 7); |
ec4465ad LT |
60 | |
61 | /* Verify type line */ | |
62 | type_line = object + 48; | |
63 | if (memcmp(type_line - 1, "\ntype ", 6)) | |
446c6fae | 64 | return error("char%d: could not find \"\\ntype \"", 47); |
ec4465ad LT |
65 | |
66 | /* Verify tag-line */ | |
67 | tag_line = strchr(type_line, '\n'); | |
68 | if (!tag_line) | |
579d1fbf | 69 | return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer); |
ec4465ad LT |
70 | tag_line++; |
71 | if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n') | |
579d1fbf | 72 | return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer); |
ec4465ad LT |
73 | |
74 | /* Get the actual type */ | |
75 | typelen = tag_line - type_line - strlen("type \n"); | |
76 | if (typelen >= sizeof(type)) | |
579d1fbf | 77 | return error("char" PD_FMT ": type too long", 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; | |
579d1fbf | 94 | return error("char" PD_FMT ": could not verify tag name", tag_line - buffer); |
ec4465ad LT |
95 | } |
96 | ||
c818566d EB |
97 | /* Verify the tagger line */ |
98 | tagger_line = tag_line; | |
99 | ||
100 | if (memcmp(tagger_line, "tagger", 6) || (tagger_line[6] == '\n')) | |
579d1fbf | 101 | return error("char" PD_FMT ": could not find \"tagger\"", tagger_line - buffer); |
446c6fae RJ |
102 | |
103 | /* TODO: check for committer info + blank line? */ | |
104 | /* Also, the minimum length is probably + "tagger .", or 63+8=71 */ | |
c818566d | 105 | |
ec4465ad LT |
106 | /* The actual stuff afterwards we don't care about.. */ |
107 | return 0; | |
108 | } | |
109 | ||
579d1fbf RJ |
110 | #undef PD_FMT |
111 | ||
ec4465ad LT |
112 | int main(int argc, char **argv) |
113 | { | |
e7332f96 | 114 | unsigned long size = 4096; |
2d7320d0 | 115 | char *buffer = xmalloc(size); |
ec4465ad LT |
116 | unsigned char result_sha1[20]; |
117 | ||
118 | if (argc != 1) | |
15e593e4 | 119 | usage("git-mktag < signaturefile"); |
ec4465ad | 120 | |
53228a5f JH |
121 | setup_git_directory(); |
122 | ||
c4fba0a3 | 123 | if (read_fd(0, &buffer, &size)) { |
e7332f96 BE |
124 | free(buffer); |
125 | die("could not read from stdin"); | |
b97e3dfa | 126 | } |
ec4465ad | 127 | |
a9486b02 PR |
128 | /* Verify it for some basic sanity: it needs to start with |
129 | "object <sha1>\ntype\ntagger " */ | |
ec4465ad LT |
130 | if (verify_tag(buffer, size) < 0) |
131 | die("invalid tag signature file"); | |
132 | ||
8e440259 | 133 | if (write_sha1_file(buffer, size, tag_type, result_sha1) < 0) |
ec4465ad | 134 | die("unable to write tag file"); |
e7332f96 BE |
135 | |
136 | free(buffer); | |
137 | ||
ec4465ad LT |
138 | printf("%s\n", sha1_to_hex(result_sha1)); |
139 | return 0; | |
140 | } |