signature.c 7.77 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7
 */

8
#include "signature.h"
9

10
#include "repository.h"
11
#include "git2/common.h"
12
#include "posix.h"
13

14
void git_signature_free(git_signature *sig)
15
{
16
	if (sig == NULL)
17 18
		return;

19
	git__free(sig->name);
20
	sig->name = NULL;
21
	git__free(sig->email);
22
	sig->email = NULL;
23
	git__free(sig);
24 25
}

26 27
static int signature_error(const char *msg)
{
28
	git_error_set(GIT_ERROR_INVALID, "failed to parse signature - %s", msg);
29 30 31
	return -1;
}

32
static bool contains_angle_brackets(const char *input)
33
{
34
	return strchr(input, '<') != NULL || strchr(input, '>') != NULL;
35 36
}

37 38 39 40 41 42 43 44 45 46 47 48 49 50
static bool is_crud(unsigned char c)
{
	return  c <= 32  ||
		c == '.' ||
		c == ',' ||
		c == ':' ||
		c == ';' ||
		c == '<' ||
		c == '>' ||
		c == '"' ||
		c == '\\' ||
		c == '\'';
}

51
static char *extract_trimmed(const char *ptr, size_t len)
52
{
53
	while (len && is_crud((unsigned char)ptr[0])) {
54 55
		ptr++; len--;
	}
56

57
	while (len && is_crud((unsigned char)ptr[len - 1])) {
58 59 60 61
		len--;
	}

	return git__substrdup(ptr, len);
62 63
}

64
int git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset)
65
{
66
	git_signature *p = NULL;
67

68 69
	GIT_ASSERT_ARG(name);
	GIT_ASSERT_ARG(email);
70

71 72
	*sig_out = NULL;

73 74 75 76 77 78
	if (contains_angle_brackets(name) ||
		contains_angle_brackets(email)) {
		return signature_error(
			"Neither `name` nor `email` should contain angle brackets chars.");
	}

79
	p = git__calloc(1, sizeof(git_signature));
80
	GIT_ERROR_CHECK_ALLOC(p);
81

82
	p->name = extract_trimmed(name, strlen(name));
83
	GIT_ERROR_CHECK_ALLOC(p->name);
84
	p->email = extract_trimmed(email, strlen(email));
85
	GIT_ERROR_CHECK_ALLOC(p->email);
86

87
	if (p->name[0] == '\0' || p->email[0] == '\0') {
88
		git_signature_free(p);
89
		return signature_error("Signature cannot have an empty name or email");
90
	}
91

92 93
	p->when.time = time;
	p->when.offset = offset;
94
	p->when.sign = (offset < 0) ? '-' : '+';
95

96
	*sig_out = p;
97
	return 0;
98 99
}

100
int git_signature_dup(git_signature **dest, const git_signature *source)
101
{
102
	git_signature *signature;
103

104 105 106 107
	if (source == NULL)
		return 0;

	signature = git__calloc(1, sizeof(git_signature));
108
	GIT_ERROR_CHECK_ALLOC(signature);
109 110

	signature->name = git__strdup(source->name);
111
	GIT_ERROR_CHECK_ALLOC(signature->name);
112

113
	signature->email = git__strdup(source->email);
114
	GIT_ERROR_CHECK_ALLOC(signature->email);
115

116 117
	signature->when.time = source->when.time;
	signature->when.offset = source->when.offset;
118
	signature->when.sign = source->when.sign;
119

120 121 122
	*dest = signature;

	return 0;
123 124
}

125 126 127 128 129 130 131 132
int git_signature__pdup(git_signature **dest, const git_signature *source, git_pool *pool)
{
	git_signature *signature;

	if (source == NULL)
		return 0;

	signature = git_pool_mallocz(pool, sizeof(git_signature));
133
	GIT_ERROR_CHECK_ALLOC(signature);
134 135

	signature->name = git_pool_strdup(pool, source->name);
136
	GIT_ERROR_CHECK_ALLOC(signature->name);
137 138

	signature->email = git_pool_strdup(pool, source->email);
139
	GIT_ERROR_CHECK_ALLOC(signature->email);
140 141 142

	signature->when.time = source->when.time;
	signature->when.offset = source->when.offset;
143
	signature->when.sign = source->when.sign;
144 145 146 147 148 149

	*dest = signature;

	return 0;
}

150
int git_signature_now(git_signature **sig_out, const char *name, const char *email)
151 152
{
	time_t now;
153
	time_t offset;
154
	struct tm *utc_tm;
155
	git_signature *sig;
156
	struct tm _utc;
157

158 159
	*sig_out = NULL;

160 161 162 163 164 165 166 167
	/*
	 * Get the current time as seconds since the epoch and
	 * transform that into a tm struct containing the time at
	 * UTC. Give that to mktime which considers it a local time
	 * (tm_isdst = -1 asks it to take DST into account) and gives
	 * us that time as seconds since the epoch. The difference
	 * between its return value and 'now' is our offset to UTC.
	 */
168
	time(&now);
169
	utc_tm = p_gmtime_r(&now, &_utc);
170
	utc_tm->tm_isdst = -1;
171
	offset = (time_t)difftime(now, mktime(utc_tm));
172
	offset /= 60;
173

174 175
	if (git_signature_new(&sig, name, email, now, (int)offset) < 0)
		return -1;
176 177 178

	*sig_out = sig;

179 180 181
	return 0;
}

182 183 184
int git_signature_default(git_signature **out, git_repository *repo)
{
	int error;
185
	git_config *cfg;
186 187
	const char *user_name, *user_email;

188
	if ((error = git_repository_config_snapshot(&cfg, repo)) < 0)
189 190 191 192 193 194 195 196 197 198
		return error;

	if (!(error = git_config_get_string(&user_name, cfg, "user.name")) &&
		!(error = git_config_get_string(&user_email, cfg, "user.email")))
		error = git_signature_now(out, user_name, user_email);

	git_config_free(cfg);
	return error;
}

199
int git_signature__parse(git_signature *sig, const char **buffer_out,
200
		const char *buffer_end, const char *header, char ender)
201
{
202
	const char *buffer = *buffer_out;
203
	const char *email_start, *email_end;
204

205
	memset(sig, 0, sizeof(git_signature));
206

207 208
	if (ender &&
		(buffer_end = memchr(buffer, ender, buffer_end - buffer)) == NULL)
209
		return signature_error("no newline given");
210

211 212
	if (header) {
		const size_t header_len = strlen(header);
213

214
		if (buffer + header_len >= buffer_end || memcmp(buffer, header, header_len) != 0)
215
			return signature_error("expected prefix doesn't match actual");
216

217 218
		buffer += header_len;
	}
219

220 221
	email_start = git__memrchr(buffer, '<', buffer_end - buffer);
	email_end = git__memrchr(buffer, '>', buffer_end - buffer);
222

223
	if (!email_start || !email_end || email_end <= email_start)
224
		return signature_error("malformed e-mail");
225

226
	email_start += 1;
Vicent Marti committed
227
	sig->name = extract_trimmed(buffer, email_start - buffer - 1);
228
	sig->email = extract_trimmed(email_start, email_end - email_start);
229

230 231 232 233
	/* Do we even have a time at the end of the signature? */
	if (email_end + 2 < buffer_end) {
		const char *time_start = email_end + 2;
		const char *time_end;
234

235 236
		if (git__strntol64(&sig->when.time, time_start,
				   buffer_end - time_start, &time_end, 10) < 0) {
237 238
			git__free(sig->name);
			git__free(sig->email);
239
			sig->name = sig->email = NULL;
240
			return signature_error("invalid Unix timestamp");
241
		}
242

Vicent Marti committed
243
		/* do we have a timezone? */
244 245 246 247 248 249
		if (time_end + 1 < buffer_end) {
			int offset, hours, mins;
			const char *tz_start, *tz_end;

			tz_start = time_end + 1;

nulltoken committed
250
			if ((tz_start[0] != '-' && tz_start[0] != '+') ||
251
			    git__strntol32(&offset, tz_start + 1,
252
					   buffer_end - tz_start - 1, &tz_end, 10) < 0) {
253
				/* malformed timezone, just assume it's zero */
254 255
				offset = 0;
			}
256 257 258

			hours = offset / 100;
			mins = offset % 100;
259

260 261 262 263
			/*
			 * only store timezone if it's not overflowing;
			 * see http://www.worldtimezone.com/faq.html
			 */
264
			if (hours <= 14 && mins <= 59) {
265
				sig->when.offset = (hours * 60) + mins;
266
				sig->when.sign = tz_start[0];
267 268 269 270
				if (tz_start[0] == '-')
					sig->when.offset = -sig->when.offset;
			}
		}
271
	}
272

273
	*buffer_out = buffer_end + 1;
274
	return 0;
275 276
}

277 278 279 280 281 282
int git_signature_from_buffer(git_signature **out, const char *buf)
{
	git_signature *sig;
	const char *buf_end;
	int error;

283 284
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(buf);
285 286 287 288

	*out = NULL;

	sig = git__calloc(1, sizeof(git_signature));
289
	GIT_ERROR_CHECK_ALLOC(sig);
290 291 292 293 294 295 296 297 298 299 300 301

	buf_end = buf + strlen(buf);
	error = git_signature__parse(sig, &buf, buf_end, NULL, '\0');

	if (error)
		git__free(sig);
	else
		*out = sig;

	return error;
}

302 303 304 305 306 307
void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig)
{
	int offset, hours, mins;
	char sign;

	offset = sig->when.offset;
308
	sign = (sig->when.offset < 0 || sig->when.sign == '-') ? '-' : '+';
309 310 311 312 313 314 315 316 317 318 319

	if (offset < 0)
		offset = -offset;

	hours = offset / 60;
	mins = offset % 60;

	git_buf_printf(buf, "%s%s <%s> %u %c%02d%02d\n",
			header ? header : "", sig->name, sig->email,
			(unsigned)sig->when.time, sign, hours, mins);
}
320

321 322
bool git_signature__equal(const git_signature *one, const git_signature *two)
{
323 324
	GIT_ASSERT_ARG(one);
	GIT_ASSERT_ARG(two);
325 326 327 328 329

	return
		git__strcmp(one->name, two->name) == 0 &&
		git__strcmp(one->email, two->email) == 0 &&
		one->when.time == two->when.time &&
330 331
		one->when.offset == two->when.offset &&
		one->when.sign == two->when.sign;
332 333
}