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