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