blob: b05260c83fd8ef766eb2e16fa355501bf1f62fb5 [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"
Linus Torvaldsec4465a2005-04-25 12:07:44 -07003
4/*
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +01005 * 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.
Linus Torvaldsec4465a2005-04-25 12:07:44 -070010 *
11 * The first three lines are guaranteed to be at least 63 bytes:
12 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
13 * shortest possible type-line, and "tag .\n" at 6 bytes is the
Junio C Hamanoa6080a02007-06-07 00:04:01 -070014 * shortest single-character-tag line.
Linus Torvaldsec4465a2005-04-25 12:07:44 -070015 */
16
Linus Torvaldsec4465a2005-04-25 12:07:44 -070017/*
18 * We refuse to tag something we can't verify. Just because.
19 */
20static int verify_object(unsigned char *sha1, const char *expected_type)
21{
22 int ret = -1;
Nicolas Pitre21666f12007-02-26 14:55:59 -050023 enum object_type type;
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040024 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -050025 void *buffer = read_sha1_file(sha1, &type, &size);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070026
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040027 if (buffer) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050028 if (type == type_from_string(expected_type))
29 ret = check_sha1_signature(sha1, buffer, size, expected_type);
Nicolas Pitre91d7b8a2005-05-20 16:57:28 -040030 free(buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070031 }
32 return ret;
33}
34
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010035#ifdef NO_C99_FORMAT
36#define PD_FMT "%d"
37#else
38#define PD_FMT "%td"
39#endif
40
Linus Torvaldsec4465a2005-04-25 12:07:44 -070041static int verify_tag(char *buffer, unsigned long size)
42{
43 int typelen;
44 char type[20];
45 unsigned char sha1[20];
Eric W. Biedermanc8185662005-07-14 19:02:10 -060046 const char *object, *type_line, *tag_line, *tagger_line;
Linus Torvaldsec4465a2005-04-25 12:07:44 -070047
Björn Engelmanne7332f92006-05-23 20:19:04 +020048 if (size < 64)
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010049 return error("wanna fool me ? you obviously got the size wrong !");
Björn Engelmanncfba0452006-05-23 20:20:09 +020050
Linus Torvaldsec4465a2005-04-25 12:07:44 -070051 buffer[size] = 0;
52
53 /* Verify object line */
54 object = buffer;
55 if (memcmp(object, "object ", 7))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010056 return error("char%d: does not start with \"object \"", 0);
Björn Engelmanncfba0452006-05-23 20:20:09 +020057
Linus Torvaldsec4465a2005-04-25 12:07:44 -070058 if (get_sha1_hex(object + 7, sha1))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010059 return error("char%d: could not get SHA1 hash", 7);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070060
61 /* Verify type line */
62 type_line = object + 48;
63 if (memcmp(type_line - 1, "\ntype ", 6))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010064 return error("char%d: could not find \"\\ntype \"", 47);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070065
66 /* Verify tag-line */
67 tag_line = strchr(type_line, '\n');
68 if (!tag_line)
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010069 return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070070 tag_line++;
71 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010072 return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070073
74 /* Get the actual type */
75 typelen = tag_line - type_line - strlen("type \n");
76 if (typelen >= sizeof(type))
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010077 return error("char" PD_FMT ": type too long", type_line+5 - buffer);
Björn Engelmanncfba0452006-05-23 20:20:09 +020078
Linus Torvaldsec4465a2005-04-25 12:07:44 -070079 memcpy(type, type_line+5, typelen);
80 type[typelen] = 0;
81
82 /* Verify that the object matches */
Linus Torvaldsec4465a2005-04-25 12:07:44 -070083 if (verify_object(sha1, type))
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +010084 return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
Linus Torvaldsec4465a2005-04-25 12:07:44 -070085
86 /* Verify the tag-name: we don't allow control characters or spaces in it */
87 tag_line += 4;
88 for (;;) {
89 unsigned char c = *tag_line++;
90 if (c == '\n')
91 break;
92 if (c > ' ')
93 continue;
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +010094 return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070095 }
96
Eric W. Biedermanc8185662005-07-14 19:02:10 -060097 /* Verify the tagger line */
98 tagger_line = tag_line;
99
100 if (memcmp(tagger_line, "tagger", 6) || (tagger_line[6] == '\n'))
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +0100101 return error("char" PD_FMT ": could not find \"tagger\"", tagger_line - buffer);
Ramsay Allan Jones446c6fa2006-07-29 18:15:47 +0100102
103 /* TODO: check for committer info + blank line? */
104 /* Also, the minimum length is probably + "tagger .", or 63+8=71 */
Eric W. Biedermanc8185662005-07-14 19:02:10 -0600105
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700106 /* The actual stuff afterwards we don't care about.. */
107 return 0;
108}
109
Ramsay Allan Jones579d1fb2006-07-30 16:38:28 +0100110#undef PD_FMT
111
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700112int main(int argc, char **argv)
113{
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200114 struct strbuf buf;
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700115 unsigned char result_sha1[20];
116
117 if (argc != 1)
Ramsay Allan Jones15e593e2006-08-03 16:38:39 +0100118 usage("git-mktag < signaturefile");
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700119
Junio C Hamano53228a52005-11-26 00:50:02 -0800120 setup_git_directory();
121
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200122 strbuf_init(&buf, 0);
123 if (strbuf_read(&buf, 0, 4096) < 0) {
Björn Engelmanne7332f92006-05-23 20:19:04 +0200124 die("could not read from stdin");
Linus Torvaldsb97e3df2005-05-29 12:06:32 -0700125 }
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700126
Pavel Roskina9486b02006-07-10 02:57:51 -0400127 /* Verify it for some basic sanity: it needs to start with
128 "object <sha1>\ntype\ntagger " */
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200129 if (verify_tag(buf.buf, buf.len) < 0)
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700130 die("invalid tag signature file");
131
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200132 if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0)
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700133 die("unable to write tag file");
Björn Engelmanne7332f92006-05-23 20:19:04 +0200134
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200135 strbuf_release(&buf);
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700136 printf("%s\n", sha1_to_hex(result_sha1));
137 return 0;
138}