blob: a3b4270c18ea78fa36f7243de5a9e05e2066e030 [file] [log] [blame]
Linus Torvaldsec4465a2005-04-25 12:07:44 -07001#include "cache.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02002#include "tag.h"
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +01003#include "exec_cmd.h"
Linus Torvaldsec4465a2005-04-25 12:07:44 -07004
5/*
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +01006 * A signature file has a very simple fixed format: four lines
7 * of "object <sha1>" + "type <typename>" + "tag <tagname>" +
8 * "tagger <committer>", followed by a blank line, a free-form tag
9 * message and a signature block that git itself doesn't care about,
10 * but that can be verified with gpg or similar.
Linus Torvaldsec4465a2005-04-25 12:07:44 -070011 *
Brandon Caseye0aaf782008-03-27 11:16:04 -050012 * The first four lines are guaranteed to be at least 83 bytes:
Linus Torvaldsec4465a2005-04-25 12:07:44 -070013 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
Brandon Caseye0aaf782008-03-27 11:16:04 -050014 * shortest possible type-line, "tag .\n" at 6 bytes is the shortest
15 * single-character-tag line, and "tagger . <> 0 +0000\n" at 20 bytes is
16 * the shortest possible tagger-line.
Linus Torvaldsec4465a2005-04-25 12:07:44 -070017 */
18
Linus Torvaldsec4465a2005-04-25 12:07:44 -070019/*
20 * We refuse to tag something we can't verify. Just because.
21 */
Christian Coudercc400f52009-01-23 10:07:26 +010022static int verify_object(const unsigned char *sha1, const char *expected_type)
Linus Torvaldsec4465a2005-04-25 12:07:44 -070023{
24 int ret = -1;
Nicolas Pitre21666f12007-02-26 14:55:59 -050025 enum object_type type;
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040026 unsigned long size;
Christian Coudercc400f52009-01-23 10:07:26 +010027 const unsigned char *repl;
28 void *buffer = read_sha1_file_repl(sha1, &type, &size, &repl);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070029
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040030 if (buffer) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050031 if (type == type_from_string(expected_type))
Christian Coudercc400f52009-01-23 10:07:26 +010032 ret = check_sha1_signature(repl, buffer, size, expected_type);
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040033 free(buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070034 }
35 return ret;
36}
37
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010038#ifdef NO_C99_FORMAT
39#define PD_FMT "%d"
40#else
41#define PD_FMT "%td"
42#endif
43
Linus Torvaldsec4465a2005-04-25 12:07:44 -070044static int verify_tag(char *buffer, unsigned long size)
45{
46 int typelen;
47 char type[20];
48 unsigned char sha1[20];
Brandon Caseye0aaf782008-03-27 11:16:04 -050049 const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb;
Brandon Caseyba26ab92008-03-31 18:25:23 -050050 size_t len;
Linus Torvaldsec4465a2005-04-25 12:07:44 -070051
Brandon Caseye0aaf782008-03-27 11:16:04 -050052 if (size < 84)
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010053 return error("wanna fool me ? you obviously got the size wrong !");
Björn Engelmanncfba0452006-05-23 20:20:09 +020054
Linus Torvaldsec4465a2005-04-25 12:07:44 -070055 buffer[size] = 0;
56
57 /* Verify object line */
58 object = buffer;
59 if (memcmp(object, "object ", 7))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010060 return error("char%d: does not start with \"object \"", 0);
Björn Engelmanncfba0452006-05-23 20:20:09 +020061
Linus Torvaldsec4465a2005-04-25 12:07:44 -070062 if (get_sha1_hex(object + 7, sha1))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010063 return error("char%d: could not get SHA1 hash", 7);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070064
65 /* Verify type line */
66 type_line = object + 48;
67 if (memcmp(type_line - 1, "\ntype ", 6))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010068 return error("char%d: could not find \"\\ntype \"", 47);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070069
70 /* Verify tag-line */
71 tag_line = strchr(type_line, '\n');
72 if (!tag_line)
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010073 return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070074 tag_line++;
75 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010076 return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070077
78 /* Get the actual type */
79 typelen = tag_line - type_line - strlen("type \n");
80 if (typelen >= sizeof(type))
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010081 return error("char" PD_FMT ": type too long", type_line+5 - buffer);
Björn Engelmanncfba0452006-05-23 20:20:09 +020082
Linus Torvaldsec4465a2005-04-25 12:07:44 -070083 memcpy(type, type_line+5, typelen);
84 type[typelen] = 0;
85
86 /* Verify that the object matches */
Linus Torvaldsec4465a2005-04-25 12:07:44 -070087 if (verify_object(sha1, type))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010088 return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
Linus Torvaldsec4465a2005-04-25 12:07:44 -070089
90 /* Verify the tag-name: we don't allow control characters or spaces in it */
91 tag_line += 4;
92 for (;;) {
93 unsigned char c = *tag_line++;
94 if (c == '\n')
95 break;
96 if (c > ' ')
97 continue;
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010098 return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070099 }
100
Eric W. Biedermanc8185662005-07-14 19:02:10 -0600101 /* Verify the tagger line */
102 tagger_line = tag_line;
103
Brandon Caseyba26ab92008-03-31 18:25:23 -0500104 if (memcmp(tagger_line, "tagger ", 7))
Brandon Caseye0aaf782008-03-27 11:16:04 -0500105 return error("char" PD_FMT ": could not find \"tagger \"",
106 tagger_line - buffer);
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +0100107
Brandon Caseye0aaf782008-03-27 11:16:04 -0500108 /*
109 * Check for correct form for name and email
110 * i.e. " <" followed by "> " on _this_ line
Brandon Caseyba26ab92008-03-31 18:25:23 -0500111 * No angle brackets within the name or email address fields.
112 * No spaces within the email address field.
Brandon Caseye0aaf782008-03-27 11:16:04 -0500113 */
114 tagger_line += 7;
115 if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
Brandon Caseyba26ab92008-03-31 18:25:23 -0500116 strpbrk(tagger_line, "<>\n") != lb+1 ||
117 strpbrk(lb+2, "><\n ") != rb)
118 return error("char" PD_FMT ": malformed tagger field",
Brandon Caseye0aaf782008-03-27 11:16:04 -0500119 tagger_line - buffer);
120
121 /* Check for author name, at least one character, space is acceptable */
122 if (lb == tagger_line)
123 return error("char" PD_FMT ": missing tagger name",
124 tagger_line - buffer);
125
Brandon Caseyba26ab92008-03-31 18:25:23 -0500126 /* timestamp, 1 or more digits followed by space */
Brandon Caseye0aaf782008-03-27 11:16:04 -0500127 tagger_line = rb + 2;
Brandon Caseyba26ab92008-03-31 18:25:23 -0500128 if (!(len = strspn(tagger_line, "0123456789")))
129 return error("char" PD_FMT ": missing tag timestamp",
130 tagger_line - buffer);
131 tagger_line += len;
132 if (*tagger_line != ' ')
Brandon Caseye0aaf782008-03-27 11:16:04 -0500133 return error("char" PD_FMT ": malformed tag timestamp",
134 tagger_line - buffer);
Brandon Caseyba26ab92008-03-31 18:25:23 -0500135 tagger_line++;
Brandon Caseye0aaf782008-03-27 11:16:04 -0500136
137 /* timezone, 5 digits [+-]hhmm, max. 1400 */
138 if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
Brandon Caseyba26ab92008-03-31 18:25:23 -0500139 strspn(tagger_line+1, "0123456789") == 4 &&
Brandon Caseye0aaf782008-03-27 11:16:04 -0500140 tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
141 return error("char" PD_FMT ": malformed tag timezone",
142 tagger_line - buffer);
143 tagger_line += 6;
144
145 /* Verify the blank line separating the header from the body */
146 if (*tagger_line != '\n')
147 return error("char" PD_FMT ": trailing garbage in tag header",
148 tagger_line - buffer);
Eric W. Biedermanc8185662005-07-14 19:02:10 -0600149
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700150 /* The actual stuff afterwards we don't care about.. */
151 return 0;
152}
153
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +0100154#undef PD_FMT
155
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700156int main(int argc, char **argv)
157{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500158 struct strbuf buf = STRBUF_INIT;
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700159 unsigned char result_sha1[20];
160
161 if (argc != 1)
Alexander Potashev34263de2009-01-04 21:39:27 +0300162 usage("git mktag < signaturefile");
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700163
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +0100164 git_extract_argv0_path(argv[0]);
165
Junio C Hamano53228a52005-11-26 00:50:02 -0800166 setup_git_directory();
167
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200168 if (strbuf_read(&buf, 0, 4096) < 0) {
Thomas Rast0721c312009-06-27 17:58:47 +0200169 die_errno("could not read from stdin");
Linus Torvaldsb97e3df2005-05-29 12:06:32 -0700170 }
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700171
Pavel Roskina9486b02006-07-10 02:57:51 -0400172 /* Verify it for some basic sanity: it needs to start with
173 "object <sha1>\ntype\ntagger " */
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200174 if (verify_tag(buf.buf, buf.len) < 0)
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700175 die("invalid tag signature file");
176
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200177 if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0)
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700178 die("unable to write tag file");
Björn Engelmanne7332f92006-05-23 20:19:04 +0200179
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200180 strbuf_release(&buf);
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700181 printf("%s\n", sha1_to_hex(result_sha1));
182 return 0;
183}