You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
5.2 KiB
180 lines
5.2 KiB
20 years ago
|
#include "cache.h"
|
||
19 years ago
|
#include "tag.h"
|
||
16 years ago
|
#include "exec_cmd.h"
|
||
20 years ago
|
|
||
|
/*
|
||
19 years ago
|
* 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.
|
||
20 years ago
|
*
|
||
17 years ago
|
* The first four lines are guaranteed to be at least 83 bytes:
|
||
20 years ago
|
* "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
|
||
17 years ago
|
* 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.
|
||
20 years ago
|
*/
|
||
|
|
||
|
/*
|
||
|
* We refuse to tag something we can't verify. Just because.
|
||
|
*/
|
||
16 years ago
|
static int verify_object(const unsigned char *sha1, const char *expected_type)
|
||
20 years ago
|
{
|
||
|
int ret = -1;
|
||
18 years ago
|
enum object_type type;
|
||
20 years ago
|
unsigned long size;
|
||
16 years ago
|
const unsigned char *repl;
|
||
|
void *buffer = read_sha1_file_repl(sha1, &type, &size, &repl);
|
||
20 years ago
|
|
||
20 years ago
|
if (buffer) {
|
||
18 years ago
|
if (type == type_from_string(expected_type))
|
||
16 years ago
|
ret = check_sha1_signature(repl, buffer, size, expected_type);
|
||
20 years ago
|
free(buffer);
|
||
20 years ago
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
19 years ago
|
#ifdef NO_C99_FORMAT
|
||
|
#define PD_FMT "%d"
|
||
|
#else
|
||
|
#define PD_FMT "%td"
|
||
|
#endif
|
||
|
|
||
20 years ago
|
static int verify_tag(char *buffer, unsigned long size)
|
||
|
{
|
||
|
int typelen;
|
||
|
char type[20];
|
||
|
unsigned char sha1[20];
|
||
17 years ago
|
const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb;
|
||
17 years ago
|
size_t len;
|
||
20 years ago
|
|
||
17 years ago
|
if (size < 84)
|
||
19 years ago
|
return error("wanna fool me ? you obviously got the size wrong !");
|
||
19 years ago
|
|
||
20 years ago
|
buffer[size] = 0;
|
||
|
|
||
|
/* Verify object line */
|
||
|
object = buffer;
|
||
|
if (memcmp(object, "object ", 7))
|
||
19 years ago
|
return error("char%d: does not start with \"object \"", 0);
|
||
19 years ago
|
|
||
20 years ago
|
if (get_sha1_hex(object + 7, sha1))
|
||
19 years ago
|
return error("char%d: could not get SHA1 hash", 7);
|
||
20 years ago
|
|
||
|
/* Verify type line */
|
||
|
type_line = object + 48;
|
||
|
if (memcmp(type_line - 1, "\ntype ", 6))
|
||
19 years ago
|
return error("char%d: could not find \"\\ntype \"", 47);
|
||
20 years ago
|
|
||
|
/* Verify tag-line */
|
||
|
tag_line = strchr(type_line, '\n');
|
||
|
if (!tag_line)
|
||
19 years ago
|
return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
|
||
20 years ago
|
tag_line++;
|
||
|
if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
|
||
19 years ago
|
return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
|
||
20 years ago
|
|
||
|
/* Get the actual type */
|
||
|
typelen = tag_line - type_line - strlen("type \n");
|
||
|
if (typelen >= sizeof(type))
|
||
19 years ago
|
return error("char" PD_FMT ": type too long", type_line+5 - buffer);
|
||
19 years ago
|
|
||
20 years ago
|
memcpy(type, type_line+5, typelen);
|
||
|
type[typelen] = 0;
|
||
|
|
||
|
/* Verify that the object matches */
|
||
|
if (verify_object(sha1, type))
|
||
19 years ago
|
return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
|
||
20 years ago
|
|
||
|
/* 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;
|
||
19 years ago
|
return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
|
||
20 years ago
|
}
|
||
|
|
||
20 years ago
|
/* Verify the tagger line */
|
||
|
tagger_line = tag_line;
|
||
|
|
||
17 years ago
|
if (memcmp(tagger_line, "tagger ", 7))
|
||
17 years ago
|
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
|
||
17 years ago
|
* No angle brackets within the name or email address fields.
|
||
|
* No spaces within the email address field.
|
||
17 years ago
|
*/
|
||
|
tagger_line += 7;
|
||
|
if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
|
||
17 years ago
|
strpbrk(tagger_line, "<>\n") != lb+1 ||
|
||
|
strpbrk(lb+2, "><\n ") != rb)
|
||
|
return error("char" PD_FMT ": malformed tagger field",
|
||
17 years ago
|
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);
|
||
|
|
||
17 years ago
|
/* timestamp, 1 or more digits followed by space */
|
||
17 years ago
|
tagger_line = rb + 2;
|
||
17 years ago
|
if (!(len = strspn(tagger_line, "0123456789")))
|
||
|
return error("char" PD_FMT ": missing tag timestamp",
|
||
17 years ago
|
tagger_line - buffer);
|
||
17 years ago
|
tagger_line += len;
|
||
|
if (*tagger_line != ' ')
|
||
17 years ago
|
return error("char" PD_FMT ": malformed tag timestamp",
|
||
|
tagger_line - buffer);
|
||
17 years ago
|
tagger_line++;
|
||
19 years ago
|
|
||
17 years ago
|
/* timezone, 5 digits [+-]hhmm, max. 1400 */
|
||
|
if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
|
||
17 years ago
|
strspn(tagger_line+1, "0123456789") == 4 &&
|
||
17 years ago
|
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);
|
||
20 years ago
|
|
||
20 years ago
|
/* The actual stuff afterwards we don't care about.. */
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
19 years ago
|
#undef PD_FMT
|
||
|
|
||
15 years ago
|
int cmd_mktag(int argc, const char **argv, const char *prefix)
|
||
20 years ago
|
{
|
||
16 years ago
|
struct strbuf buf = STRBUF_INIT;
|
||
20 years ago
|
unsigned char result_sha1[20];
|
||
|
|
||
|
if (argc != 1)
|
||
16 years ago
|
usage("git mktag < signaturefile");
|
||
20 years ago
|
|
||
18 years ago
|
if (strbuf_read(&buf, 0, 4096) < 0) {
|
||
16 years ago
|
die_errno("could not read from stdin");
|
||
20 years ago
|
}
|
||
20 years ago
|
|
||
19 years ago
|
/* Verify it for some basic sanity: it needs to start with
|
||
|
"object <sha1>\ntype\ntagger " */
|
||
18 years ago
|
if (verify_tag(buf.buf, buf.len) < 0)
|
||
20 years ago
|
die("invalid tag signature file");
|
||
|
|
||
18 years ago
|
if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0)
|
||
20 years ago
|
die("unable to write tag file");
|
||
19 years ago
|
|
||
18 years ago
|
strbuf_release(&buf);
|
||
20 years ago
|
printf("%s\n", sha1_to_hex(result_sha1));
|
||
|
return 0;
|
||
|
}
|