mktag.c 5.18 KB
Newer Older
1
#include "cache.h"
2
#include "tag.h"
3
#include "exec_cmd.h"
4 5

/*
6 7 8 9 10
 * A signature file has a very simple fixed format: four lines
 * of "object <sha1>" + "type <typename>" + "tag <tagname>" +
 * "tagger <committer>", followed by a blank line, a free-form tag
 * message and a signature block that git itself doesn't care about,
 * but that can be verified with gpg or similar.
11
 *
12
 * The first four lines are guaranteed to be at least 83 bytes:
13
 * "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
14 15 16
 * shortest possible type-line, "tag .\n" at 6 bytes is the shortest
 * single-character-tag line, and "tagger . <> 0 +0000\n" at 20 bytes is
 * the shortest possible tagger-line.
17 18 19 20 21
 */

/*
 * We refuse to tag something we can't verify. Just because.
 */
22
static int verify_object(const unsigned char *sha1, const char *expected_type)
23 24
{
	int ret = -1;
25
	enum object_type type;
Nicolas Pitre's avatar
Nicolas Pitre committed
26
	unsigned long size;
27 28
	const unsigned char *repl;
	void *buffer = read_sha1_file_repl(sha1, &type, &size, &repl);
29

Nicolas Pitre's avatar
Nicolas Pitre committed
30
	if (buffer) {
31
		if (type == type_from_string(expected_type))
32
			ret = check_sha1_signature(repl, buffer, size, expected_type);
Nicolas Pitre's avatar
Nicolas Pitre committed
33
		free(buffer);
34 35 36 37
	}
	return ret;
}

38 39 40 41 42 43
#ifdef NO_C99_FORMAT
#define PD_FMT "%d"
#else
#define PD_FMT "%td"
#endif

44 45 46 47 48
static int verify_tag(char *buffer, unsigned long size)
{
	int typelen;
	char type[20];
	unsigned char sha1[20];
49
	const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb;
50
	size_t len;
51

52
	if (size < 84)
53
		return error("wanna fool me ? you obviously got the size wrong !");
54

55 56 57 58 59
	buffer[size] = 0;

	/* Verify object line */
	object = buffer;
	if (memcmp(object, "object ", 7))
60
		return error("char%d: does not start with \"object \"", 0);
61

62
	if (get_sha1_hex(object + 7, sha1))
63
		return error("char%d: could not get SHA1 hash", 7);
64 65 66 67

	/* Verify type line */
	type_line = object + 48;
	if (memcmp(type_line - 1, "\ntype ", 6))
68
		return error("char%d: could not find \"\\ntype \"", 47);
69 70 71 72

	/* Verify tag-line */
	tag_line = strchr(type_line, '\n');
	if (!tag_line)
73
		return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
74 75
	tag_line++;
	if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
76
		return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
77 78 79 80

	/* Get the actual type */
	typelen = tag_line - type_line - strlen("type \n");
	if (typelen >= sizeof(type))
81
		return error("char" PD_FMT ": type too long", type_line+5 - buffer);
82

83 84 85 86 87
	memcpy(type, type_line+5, typelen);
	type[typelen] = 0;

	/* Verify that the object matches */
	if (verify_object(sha1, type))
88
		return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
89 90 91 92 93 94 95 96 97

	/* Verify the tag-name: we don't allow control characters or spaces in it */
	tag_line += 4;
	for (;;) {
		unsigned char c = *tag_line++;
		if (c == '\n')
			break;
		if (c > ' ')
			continue;
98
		return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
99 100
	}

101 102 103
	/* Verify the tagger line */
	tagger_line = tag_line;

104
	if (memcmp(tagger_line, "tagger ", 7))
105 106 107 108 109 110
		return error("char" PD_FMT ": could not find \"tagger \"",
			tagger_line - buffer);

	/*
	 * Check for correct form for name and email
	 * i.e. " <" followed by "> " on _this_ line
111 112
	 * No angle brackets within the name or email address fields.
	 * No spaces within the email address field.
113 114 115
	 */
	tagger_line += 7;
	if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
116 117 118
		strpbrk(tagger_line, "<>\n") != lb+1 ||
		strpbrk(lb+2, "><\n ") != rb)
		return error("char" PD_FMT ": malformed tagger field",
119 120 121 122 123 124 125
			tagger_line - buffer);

	/* Check for author name, at least one character, space is acceptable */
	if (lb == tagger_line)
		return error("char" PD_FMT ": missing tagger name",
			tagger_line - buffer);

126
	/* timestamp, 1 or more digits followed by space */
127
	tagger_line = rb + 2;
128 129
	if (!(len = strspn(tagger_line, "0123456789")))
		return error("char" PD_FMT ": missing tag timestamp",
130
			tagger_line - buffer);
131 132
	tagger_line += len;
	if (*tagger_line != ' ')
133 134
		return error("char" PD_FMT ": malformed tag timestamp",
			tagger_line - buffer);
135
	tagger_line++;
136

137 138
	/* timezone, 5 digits [+-]hhmm, max. 1400 */
	if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
139
	      strspn(tagger_line+1, "0123456789") == 4 &&
140 141 142 143 144 145 146 147 148
	      tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
		return error("char" PD_FMT ": malformed tag timezone",
			tagger_line - buffer);
	tagger_line += 6;

	/* Verify the blank line separating the header from the body */
	if (*tagger_line != '\n')
		return error("char" PD_FMT ": trailing garbage in tag header",
			tagger_line - buffer);
149

150 151 152 153
	/* The actual stuff afterwards we don't care about.. */
	return 0;
}

154 155
#undef PD_FMT

156 157
int main(int argc, char **argv)
{
158
	struct strbuf buf = STRBUF_INIT;
159 160 161
	unsigned char result_sha1[20];

	if (argc != 1)
162
		usage("git mktag < signaturefile");
163

164 165
	git_extract_argv0_path(argv[0]);

166 167
	setup_git_directory();

168
	if (strbuf_read(&buf, 0, 4096) < 0) {
169
		die("could not read from stdin");
170
	}
171

172 173
	/* Verify it for some basic sanity: it needs to start with
	   "object <sha1>\ntype\ntagger " */
174
	if (verify_tag(buf.buf, buf.len) < 0)
175 176
		die("invalid tag signature file");

177
	if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0)
178
		die("unable to write tag file");
179

180
	strbuf_release(&buf);
181 182 183
	printf("%s\n", sha1_to_hex(result_sha1));
	return 0;
}