Fix installation of templates on ancient systems.
[git/git.git] / mktag.c
CommitLineData
ec4465ad 1#include "cache.h"
8e440259 2#include "tag.h"
ec4465ad
LT
3
4/*
5 * A signature file has a very simple fixed format: three lines
6 * of "object <sha1>" + "type <typename>" + "tag <tagname>",
7 * followed by some free-form signature that git itself doesn't
8 * care about, but that can be verified with gpg or similar.
9 *
10 * The first three lines are guaranteed to be at least 63 bytes:
11 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
12 * shortest possible type-line, and "tag .\n" at 6 bytes is the
13 * shortest single-character-tag line.
14 *
15 * We also artificially limit the size of the full object to 8kB.
16 * Just because I'm a lazy bastard, and if you can't fit a signature
17 * in that size, you're doing something wrong.
18 */
19
a9486b02 20/* Some random size */
ec4465ad
LT
21#define MAXSIZE (8192)
22
23/*
24 * We refuse to tag something we can't verify. Just because.
25 */
26static int verify_object(unsigned char *sha1, const char *expected_type)
27{
28 int ret = -1;
91d7b8af
NP
29 char type[100];
30 unsigned long size;
31 void *buffer = read_sha1_file(sha1, type, &size);
ec4465ad 32
91d7b8af
NP
33 if (buffer) {
34 if (!strcmp(type, expected_type))
35 ret = check_sha1_signature(sha1, buffer, size, type);
36 free(buffer);
ec4465ad
LT
37 }
38 return ret;
39}
40
41static int verify_tag(char *buffer, unsigned long size)
42{
43 int typelen;
44 char type[20];
45 unsigned char sha1[20];
c818566d 46 const char *object, *type_line, *tag_line, *tagger_line;
ec4465ad 47
e7332f96 48 if (size < 64)
cfba0459
BE
49 return error("wanna fool me ? you obviously got the size wrong !\n");
50
ec4465ad
LT
51 buffer[size] = 0;
52
53 /* Verify object line */
54 object = buffer;
55 if (memcmp(object, "object ", 7))
cfba0459
BE
56 return error("char%d: does not start with \"object \"\n", 0);
57
ec4465ad 58 if (get_sha1_hex(object + 7, sha1))
cfba0459 59 return error("char%d: could not get SHA1 hash\n", 7);
ec4465ad
LT
60
61 /* Verify type line */
62 type_line = object + 48;
63 if (memcmp(type_line - 1, "\ntype ", 6))
cfba0459 64 return error("char%d: could not find \"\\ntype \"\n", 47);
ec4465ad
LT
65
66 /* Verify tag-line */
67 tag_line = strchr(type_line, '\n');
68 if (!tag_line)
cfba0459 69 return error("char%td: could not find next \"\\n\"\n", type_line - buffer);
ec4465ad
LT
70 tag_line++;
71 if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
cfba0459 72 return error("char%td: no \"tag \" found\n", tag_line - buffer);
ec4465ad
LT
73
74 /* Get the actual type */
75 typelen = tag_line - type_line - strlen("type \n");
76 if (typelen >= sizeof(type))
cfba0459
BE
77 return error("char%td: type too long\n", type_line+5 - buffer);
78
ec4465ad
LT
79 memcpy(type, type_line+5, typelen);
80 type[typelen] = 0;
81
82 /* Verify that the object matches */
83 if (get_sha1_hex(object + 7, sha1))
cfba0459
BE
84 return error("char%d: could not get SHA1 hash but this is really odd since i got it before !\n", 7);
85
ec4465ad 86 if (verify_object(sha1, type))
cfba0459 87 return error("char%d: could not verify object %s\n", 7, sha1);
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;
cfba0459 97 return error("char%td: could not verify tag name\n", tag_line - buffer);
ec4465ad
LT
98 }
99
c818566d
EB
100 /* Verify the tagger line */
101 tagger_line = tag_line;
102
103 if (memcmp(tagger_line, "tagger", 6) || (tagger_line[6] == '\n'))
cfba0459 104 return error("char%td: could not find \"tagger\"\n", tagger_line - buffer);
c818566d 105
ec4465ad
LT
106 /* The actual stuff afterwards we don't care about.. */
107 return 0;
108}
109
110int main(int argc, char **argv)
111{
e7332f96
BE
112 unsigned long size = 4096;
113 char *buffer = malloc(size);
ec4465ad
LT
114 unsigned char result_sha1[20];
115
116 if (argc != 1)
117 usage("cat <signaturefile> | git-mktag");
118
53228a5f
JH
119 setup_git_directory();
120
e7332f96
BE
121 if (read_pipe(0, &buffer, &size)) {
122 free(buffer);
123 die("could not read from stdin");
b97e3dfa 124 }
ec4465ad 125
a9486b02
PR
126 /* Verify it for some basic sanity: it needs to start with
127 "object <sha1>\ntype\ntagger " */
ec4465ad
LT
128 if (verify_tag(buffer, size) < 0)
129 die("invalid tag signature file");
130
8e440259 131 if (write_sha1_file(buffer, size, tag_type, result_sha1) < 0)
ec4465ad 132 die("unable to write tag file");
e7332f96
BE
133
134 free(buffer);
135
ec4465ad
LT
136 printf("%s\n", sha1_to_hex(result_sha1));
137 return 0;
138}