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