blob: 6fb7dc8578d68526c64179d7fc43aaa5a72445f6 [file] [log] [blame]
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001#include "builtin.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02002#include "tag.h"
Stefan Beller47f351e2018-04-11 17:21:06 -07003#include "replace-object.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Linus Torvaldsec4465a2005-04-25 12:07:44 -07005
6/*
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +01007 * 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.
Linus Torvaldsec4465a2005-04-25 12:07:44 -070012 *
Brandon Caseye0aaf782008-03-27 11:16:04 -050013 * The first four lines are guaranteed to be at least 83 bytes:
Linus Torvaldsec4465a2005-04-25 12:07:44 -070014 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
Brandon Caseye0aaf782008-03-27 11:16:04 -050015 * 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.
Linus Torvaldsec4465a2005-04-25 12:07:44 -070018 */
19
Linus Torvaldsec4465a2005-04-25 12:07:44 -070020/*
21 * We refuse to tag something we can't verify. Just because.
22 */
brian m. carlsoneedc9942018-03-12 02:27:34 +000023static int verify_object(const struct object_id *oid, const char *expected_type)
Linus Torvaldsec4465a2005-04-25 12:07:44 -070024{
25 int ret = -1;
Nicolas Pitre21666f12007-02-26 14:55:59 -050026 enum object_type type;
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040027 unsigned long size;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +000028 void *buffer = read_object_file(oid, &type, &size);
Stefan Beller1f2e7ce2018-04-11 17:21:13 -070029 const struct object_id *repl = lookup_replace_object(the_repository, oid);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070030
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040031 if (buffer) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050032 if (type == type_from_string(expected_type))
brian m. carlsonb383a132018-03-12 02:27:54 +000033 ret = check_object_signature(repl, buffer, size, expected_type);
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040034 free(buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070035 }
36 return ret;
37}
38
39static int verify_tag(char *buffer, unsigned long size)
40{
41 int typelen;
42 char type[20];
brian m. carlsoneedc9942018-03-12 02:27:34 +000043 struct object_id oid;
44 const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb, *p;
Brandon Caseyba26ab92008-03-31 18:25:23 -050045 size_t len;
Linus Torvaldsec4465a2005-04-25 12:07:44 -070046
Brandon Caseye0aaf782008-03-27 11:16:04 -050047 if (size < 84)
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010048 return error("wanna fool me ? you obviously got the size wrong !");
Björn Engelmanncfba0452006-05-23 20:20:09 +020049
Linus Torvaldsec4465a2005-04-25 12:07:44 -070050 buffer[size] = 0;
51
52 /* Verify object line */
53 object = buffer;
54 if (memcmp(object, "object ", 7))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010055 return error("char%d: does not start with \"object \"", 0);
Björn Engelmanncfba0452006-05-23 20:20:09 +020056
brian m. carlsoneedc9942018-03-12 02:27:34 +000057 if (parse_oid_hex(object + 7, &oid, &p))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010058 return error("char%d: could not get SHA1 hash", 7);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070059
60 /* Verify type line */
brian m. carlsoneedc9942018-03-12 02:27:34 +000061 type_line = p + 1;
Linus Torvaldsec4465a2005-04-25 12:07:44 -070062 if (memcmp(type_line - 1, "\ntype ", 6))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010063 return error("char%d: could not find \"\\ntype \"", 47);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070064
65 /* Verify tag-line */
66 tag_line = strchr(type_line, '\n');
67 if (!tag_line)
Jonathan Nieder31d713d2011-03-16 00:14:22 -050068 return error("char%"PRIuMAX": could not find next \"\\n\"",
69 (uintmax_t) (type_line - buffer));
Linus Torvaldsec4465a2005-04-25 12:07:44 -070070 tag_line++;
71 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
Jonathan Nieder31d713d2011-03-16 00:14:22 -050072 return error("char%"PRIuMAX": no \"tag \" found",
73 (uintmax_t) (tag_line - buffer));
Linus Torvaldsec4465a2005-04-25 12:07:44 -070074
75 /* Get the actual type */
76 typelen = tag_line - type_line - strlen("type \n");
77 if (typelen >= sizeof(type))
Jonathan Nieder31d713d2011-03-16 00:14:22 -050078 return error("char%"PRIuMAX": type too long",
79 (uintmax_t) (type_line+5 - buffer));
Björn Engelmanncfba0452006-05-23 20:20:09 +020080
Linus Torvaldsec4465a2005-04-25 12:07:44 -070081 memcpy(type, type_line+5, typelen);
82 type[typelen] = 0;
83
84 /* Verify that the object matches */
brian m. carlsoneedc9942018-03-12 02:27:34 +000085 if (verify_object(&oid, type))
86 return error("char%d: could not verify object %s", 7, oid_to_hex(&oid));
Linus Torvaldsec4465a2005-04-25 12:07:44 -070087
88 /* Verify the tag-name: we don't allow control characters or spaces in it */
89 tag_line += 4;
90 for (;;) {
91 unsigned char c = *tag_line++;
92 if (c == '\n')
93 break;
94 if (c > ' ')
95 continue;
Jonathan Nieder31d713d2011-03-16 00:14:22 -050096 return error("char%"PRIuMAX": could not verify tag name",
97 (uintmax_t) (tag_line - buffer));
Linus Torvaldsec4465a2005-04-25 12:07:44 -070098 }
99
Eric W. Biedermanc8185662005-07-14 19:02:10 -0600100 /* Verify the tagger line */
101 tagger_line = tag_line;
102
Brandon Caseyba26ab92008-03-31 18:25:23 -0500103 if (memcmp(tagger_line, "tagger ", 7))
Jonathan Nieder31d713d2011-03-16 00:14:22 -0500104 return error("char%"PRIuMAX": could not find \"tagger \"",
105 (uintmax_t) (tagger_line - buffer));
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +0100106
Brandon Caseye0aaf782008-03-27 11:16:04 -0500107 /*
108 * Check for correct form for name and email
109 * i.e. " <" followed by "> " on _this_ line
Brandon Caseyba26ab92008-03-31 18:25:23 -0500110 * No angle brackets within the name or email address fields.
111 * No spaces within the email address field.
Brandon Caseye0aaf782008-03-27 11:16:04 -0500112 */
113 tagger_line += 7;
114 if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
Brandon Caseyba26ab92008-03-31 18:25:23 -0500115 strpbrk(tagger_line, "<>\n") != lb+1 ||
116 strpbrk(lb+2, "><\n ") != rb)
Jonathan Nieder31d713d2011-03-16 00:14:22 -0500117 return error("char%"PRIuMAX": malformed tagger field",
118 (uintmax_t) (tagger_line - buffer));
Brandon Caseye0aaf782008-03-27 11:16:04 -0500119
120 /* Check for author name, at least one character, space is acceptable */
121 if (lb == tagger_line)
Jonathan Nieder31d713d2011-03-16 00:14:22 -0500122 return error("char%"PRIuMAX": missing tagger name",
123 (uintmax_t) (tagger_line - buffer));
Brandon Caseye0aaf782008-03-27 11:16:04 -0500124
Brandon Caseyba26ab92008-03-31 18:25:23 -0500125 /* timestamp, 1 or more digits followed by space */
Brandon Caseye0aaf782008-03-27 11:16:04 -0500126 tagger_line = rb + 2;
Brandon Caseyba26ab92008-03-31 18:25:23 -0500127 if (!(len = strspn(tagger_line, "0123456789")))
Jonathan Nieder31d713d2011-03-16 00:14:22 -0500128 return error("char%"PRIuMAX": missing tag timestamp",
129 (uintmax_t) (tagger_line - buffer));
Brandon Caseyba26ab92008-03-31 18:25:23 -0500130 tagger_line += len;
131 if (*tagger_line != ' ')
Jonathan Nieder31d713d2011-03-16 00:14:22 -0500132 return error("char%"PRIuMAX": malformed tag timestamp",
133 (uintmax_t) (tagger_line - buffer));
Brandon Caseyba26ab92008-03-31 18:25:23 -0500134 tagger_line++;
Brandon Caseye0aaf782008-03-27 11:16:04 -0500135
136 /* timezone, 5 digits [+-]hhmm, max. 1400 */
137 if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
Brandon Caseyba26ab92008-03-31 18:25:23 -0500138 strspn(tagger_line+1, "0123456789") == 4 &&
Brandon Caseye0aaf782008-03-27 11:16:04 -0500139 tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
Jonathan Nieder31d713d2011-03-16 00:14:22 -0500140 return error("char%"PRIuMAX": malformed tag timezone",
141 (uintmax_t) (tagger_line - buffer));
Brandon Caseye0aaf782008-03-27 11:16:04 -0500142 tagger_line += 6;
143
144 /* Verify the blank line separating the header from the body */
145 if (*tagger_line != '\n')
Jonathan Nieder31d713d2011-03-16 00:14:22 -0500146 return error("char%"PRIuMAX": trailing garbage in tag header",
147 (uintmax_t) (tagger_line - buffer));
Eric W. Biedermanc8185662005-07-14 19:02:10 -0600148
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700149 /* The actual stuff afterwards we don't care about.. */
150 return 0;
151}
152
Linus Torvalds112dd512010-01-22 07:34:44 -0800153int cmd_mktag(int argc, const char **argv, const char *prefix)
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700154{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500155 struct strbuf buf = STRBUF_INIT;
Patryk Obaraa09c9852018-01-28 01:13:19 +0100156 struct object_id result;
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700157
158 if (argc != 1)
Junio C Hamano33e8fc82015-10-16 11:27:42 -0700159 usage("git mktag");
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700160
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200161 if (strbuf_read(&buf, 0, 4096) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +0200162 die_errno("could not read from stdin");
Linus Torvaldsb97e3df2005-05-29 12:06:32 -0700163 }
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700164
Pavel Roskina9486b02006-07-10 02:57:51 -0400165 /* Verify it for some basic sanity: it needs to start with
166 "object <sha1>\ntype\ntagger " */
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200167 if (verify_tag(buf.buf, buf.len) < 0)
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700168 die("invalid tag signature file");
169
Patryk Obaraa09c9852018-01-28 01:13:19 +0100170 if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0)
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700171 die("unable to write tag file");
Björn Engelmanne7332f92006-05-23 20:19:04 +0200172
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200173 strbuf_release(&buf);
Patryk Obaraa09c9852018-01-28 01:13:19 +0100174 printf("%s\n", oid_to_hex(&result));
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700175 return 0;
176}