]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "tag.h" | |
3 | #include "object-store.h" | |
4 | #include "commit.h" | |
5 | #include "tree.h" | |
6 | #include "blob.h" | |
7 | #include "alloc.h" | |
8 | #include "gpg-interface.h" | |
9 | #include "packfile.h" | |
10 | ||
11 | const char *tag_type = "tag"; | |
12 | ||
13 | static int run_gpg_verify(const char *buf, unsigned long size, unsigned flags) | |
14 | { | |
15 | struct signature_check sigc; | |
16 | struct strbuf payload = STRBUF_INIT; | |
17 | struct strbuf signature = STRBUF_INIT; | |
18 | int ret; | |
19 | ||
20 | memset(&sigc, 0, sizeof(sigc)); | |
21 | ||
22 | if (!parse_signature(buf, size, &payload, &signature)) { | |
23 | if (flags & GPG_VERIFY_VERBOSE) | |
24 | write_in_full(1, buf, size); | |
25 | return error("no signature found"); | |
26 | } | |
27 | ||
28 | ret = check_signature(payload.buf, payload.len, signature.buf, | |
29 | signature.len, &sigc); | |
30 | ||
31 | if (!(flags & GPG_VERIFY_OMIT_STATUS)) | |
32 | print_signature_buffer(&sigc, flags); | |
33 | ||
34 | signature_check_clear(&sigc); | |
35 | strbuf_release(&payload); | |
36 | strbuf_release(&signature); | |
37 | return ret; | |
38 | } | |
39 | ||
40 | int gpg_verify_tag(const struct object_id *oid, const char *name_to_report, | |
41 | unsigned flags) | |
42 | { | |
43 | enum object_type type; | |
44 | char *buf; | |
45 | unsigned long size; | |
46 | int ret; | |
47 | ||
48 | type = oid_object_info(the_repository, oid, NULL); | |
49 | if (type != OBJ_TAG) | |
50 | return error("%s: cannot verify a non-tag object of type %s.", | |
51 | name_to_report ? | |
52 | name_to_report : | |
53 | find_unique_abbrev(oid, DEFAULT_ABBREV), | |
54 | type_name(type)); | |
55 | ||
56 | buf = read_object_file(oid, &type, &size); | |
57 | if (!buf) | |
58 | return error("%s: unable to read file.", | |
59 | name_to_report ? | |
60 | name_to_report : | |
61 | find_unique_abbrev(oid, DEFAULT_ABBREV)); | |
62 | ||
63 | ret = run_gpg_verify(buf, size, flags); | |
64 | ||
65 | free(buf); | |
66 | return ret; | |
67 | } | |
68 | ||
69 | struct object *deref_tag(struct repository *r, struct object *o, const char *warn, int warnlen) | |
70 | { | |
71 | struct object_id *last_oid = NULL; | |
72 | while (o && o->type == OBJ_TAG) | |
73 | if (((struct tag *)o)->tagged) { | |
74 | last_oid = &((struct tag *)o)->tagged->oid; | |
75 | o = parse_object(r, last_oid); | |
76 | } else { | |
77 | last_oid = NULL; | |
78 | o = NULL; | |
79 | } | |
80 | if (!o && warn) { | |
81 | if (last_oid && is_promisor_object(last_oid)) | |
82 | return NULL; | |
83 | if (!warnlen) | |
84 | warnlen = strlen(warn); | |
85 | error("missing object referenced by '%.*s'", warnlen, warn); | |
86 | } | |
87 | return o; | |
88 | } | |
89 | ||
90 | struct object *deref_tag_noverify(struct object *o) | |
91 | { | |
92 | while (o && o->type == OBJ_TAG) { | |
93 | o = parse_object(the_repository, &o->oid); | |
94 | if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged) | |
95 | o = ((struct tag *)o)->tagged; | |
96 | else | |
97 | o = NULL; | |
98 | } | |
99 | return o; | |
100 | } | |
101 | ||
102 | struct tag *lookup_tag(struct repository *r, const struct object_id *oid) | |
103 | { | |
104 | struct object *obj = lookup_object(r, oid); | |
105 | if (!obj) | |
106 | return create_object(r, oid, alloc_tag_node(r)); | |
107 | return object_as_type(obj, OBJ_TAG, 0); | |
108 | } | |
109 | ||
110 | static timestamp_t parse_tag_date(const char *buf, const char *tail) | |
111 | { | |
112 | const char *dateptr; | |
113 | ||
114 | while (buf < tail && *buf++ != '>') | |
115 | /* nada */; | |
116 | if (buf >= tail) | |
117 | return 0; | |
118 | dateptr = buf; | |
119 | while (buf < tail && *buf++ != '\n') | |
120 | /* nada */; | |
121 | if (buf >= tail) | |
122 | return 0; | |
123 | /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */ | |
124 | return parse_timestamp(dateptr, NULL, 10); | |
125 | } | |
126 | ||
127 | void release_tag_memory(struct tag *t) | |
128 | { | |
129 | free(t->tag); | |
130 | t->tagged = NULL; | |
131 | t->object.parsed = 0; | |
132 | t->date = 0; | |
133 | } | |
134 | ||
135 | int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, unsigned long size) | |
136 | { | |
137 | struct object_id oid; | |
138 | char type[20]; | |
139 | const char *bufptr = data; | |
140 | const char *tail = bufptr + size; | |
141 | const char *nl; | |
142 | ||
143 | if (item->object.parsed) | |
144 | return 0; | |
145 | ||
146 | if (item->tag) { | |
147 | /* | |
148 | * Presumably left over from a previous failed parse; | |
149 | * clear it out in preparation for re-parsing (we'll probably | |
150 | * hit the same error, which lets us tell our current caller | |
151 | * about the problem). | |
152 | */ | |
153 | FREE_AND_NULL(item->tag); | |
154 | } | |
155 | ||
156 | if (size < the_hash_algo->hexsz + 24) | |
157 | return -1; | |
158 | if (memcmp("object ", bufptr, 7) || parse_oid_hex(bufptr + 7, &oid, &bufptr) || *bufptr++ != '\n') | |
159 | return -1; | |
160 | ||
161 | if (!starts_with(bufptr, "type ")) | |
162 | return -1; | |
163 | bufptr += 5; | |
164 | nl = memchr(bufptr, '\n', tail - bufptr); | |
165 | if (!nl || sizeof(type) <= (nl - bufptr)) | |
166 | return -1; | |
167 | memcpy(type, bufptr, nl - bufptr); | |
168 | type[nl - bufptr] = '\0'; | |
169 | bufptr = nl + 1; | |
170 | ||
171 | if (!strcmp(type, blob_type)) { | |
172 | item->tagged = (struct object *)lookup_blob(r, &oid); | |
173 | } else if (!strcmp(type, tree_type)) { | |
174 | item->tagged = (struct object *)lookup_tree(r, &oid); | |
175 | } else if (!strcmp(type, commit_type)) { | |
176 | item->tagged = (struct object *)lookup_commit(r, &oid); | |
177 | } else if (!strcmp(type, tag_type)) { | |
178 | item->tagged = (struct object *)lookup_tag(r, &oid); | |
179 | } else { | |
180 | return error("unknown tag type '%s' in %s", | |
181 | type, oid_to_hex(&item->object.oid)); | |
182 | } | |
183 | ||
184 | if (!item->tagged) | |
185 | return error("bad tag pointer to %s in %s", | |
186 | oid_to_hex(&oid), | |
187 | oid_to_hex(&item->object.oid)); | |
188 | ||
189 | if (bufptr + 4 < tail && starts_with(bufptr, "tag ")) | |
190 | ; /* good */ | |
191 | else | |
192 | return -1; | |
193 | bufptr += 4; | |
194 | nl = memchr(bufptr, '\n', tail - bufptr); | |
195 | if (!nl) | |
196 | return -1; | |
197 | item->tag = xmemdupz(bufptr, nl - bufptr); | |
198 | bufptr = nl + 1; | |
199 | ||
200 | if (bufptr + 7 < tail && starts_with(bufptr, "tagger ")) | |
201 | item->date = parse_tag_date(bufptr, tail); | |
202 | else | |
203 | item->date = 0; | |
204 | ||
205 | item->object.parsed = 1; | |
206 | return 0; | |
207 | } | |
208 | ||
209 | int parse_tag(struct tag *item) | |
210 | { | |
211 | enum object_type type; | |
212 | void *data; | |
213 | unsigned long size; | |
214 | int ret; | |
215 | ||
216 | if (item->object.parsed) | |
217 | return 0; | |
218 | data = read_object_file(&item->object.oid, &type, &size); | |
219 | if (!data) | |
220 | return error("Could not read %s", | |
221 | oid_to_hex(&item->object.oid)); | |
222 | if (type != OBJ_TAG) { | |
223 | free(data); | |
224 | return error("Object %s not a tag", | |
225 | oid_to_hex(&item->object.oid)); | |
226 | } | |
227 | ret = parse_tag_buffer(the_repository, item, data, size); | |
228 | free(data); | |
229 | return ret; | |
230 | } | |
231 | ||
232 | struct object_id *get_tagged_oid(struct tag *tag) | |
233 | { | |
234 | if (!tag->tagged) | |
235 | die("bad tag"); | |
236 | return &tag->tagged->oid; | |
237 | } |