netops.c 6.79 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 <ctype.h>
9 10 11 12
#include "git2/errors.h"

#include "common.h"
#include "netops.h"
13
#include "posix.h"
14
#include "buffer.h"
15
#include "http_parser.h"
16
#include "global.h"
17

18 19 20
int gitno_recv(gitno_buffer *buf)
{
	return buf->recv(buf);
21 22
}

23 24 25 26 27
void gitno_buffer_setup_callback(
	gitno_buffer *buf,
	char *data,
	size_t len,
	int (*recv)(gitno_buffer *buf), void *cb_data)
28 29 30 31 32 33 34 35 36
{
	memset(data, 0x0, len);
	buf->data = data;
	buf->len = len;
	buf->offset = 0;
	buf->recv = recv;
	buf->cb_data = cb_data;
}

37
static int recv_stream(gitno_buffer *buf)
38
{
39 40
	git_stream *io = (git_stream *) buf->cb_data;
	int ret;
41

42 43 44 45 46 47
	ret = git_stream_read(io, buf->data + buf->offset, buf->len - buf->offset);
	if (ret < 0)
		return -1;

	buf->offset += ret;
	return ret;
48 49
}

50 51 52 53 54 55
void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data, size_t len)
{
	memset(data, 0x0, len);
	buf->data = data;
	buf->len = len;
	buf->offset = 0;
56 57
	buf->recv = recv_stream;
	buf->cb_data = st;
58 59
}

60
/* Consume up to ptr and move the rest of the buffer to the beginning */
61
void gitno_consume(gitno_buffer *buf, const char *ptr)
62
{
63
	size_t consumed;
64

65
	assert(ptr - buf->data >= 0);
66 67
	assert(ptr - buf->data <= (int) buf->len);

68
	consumed = ptr - buf->data;
69

70 71 72
	memmove(buf->data, ptr, buf->offset - consumed);
	memset(buf->data + buf->offset, 0x0, buf->len - buf->offset);
	buf->offset -= consumed;
73 74 75
}

/* Consume const bytes and move the rest of the buffer to the beginning */
76
void gitno_consume_n(gitno_buffer *buf, size_t cons)
77 78 79 80 81 82
{
	memmove(buf->data, buf->data + cons, buf->len - buf->offset);
	memset(buf->data + cons, 0x0, buf->len - buf->offset);
	buf->offset -= cons;
}

83
/* Match host names according to RFC 2818 rules */
84
int gitno__match_host(const char *pattern, const char *host)
85 86
{
	for (;;) {
87
		char c = git__tolower(*pattern++);
88 89 90 91 92 93 94 95 96 97

		if (c == '\0')
			return *host ? -1 : 0;

		if (c == '*') {
			c = *pattern;
			/* '*' at the end matches everything left */
			if (c == '\0')
				return 0;

98 99 100 101 102 103 104
	/*
	 * We've found a pattern, so move towards the next matching
	 * char. The '.' is handled specially because wildcards aren't
	 * allowed to cross subdomains.
	 */

			while(*host) {
105
				char h = git__tolower(*host);
106
				if (c == h)
107
					return gitno__match_host(pattern, host++);
108
				if (h == '.')
109
					return gitno__match_host(pattern, host);
110
				host++;
111
			}
112
			return -1;
113 114
		}

115
		if (c != git__tolower(*host++))
116 117 118 119 120 121
			return -1;
	}

	return -1;
}

122 123 124 125 126 127
static const char *prefix_http = "http://";
static const char *prefix_https = "https://";

int gitno_connection_data_from_url(
		gitno_connection_data *data,
		const char *url,
128
		const char *service_suffix)
129
{
130
	int error = -1;
131
	const char *default_port = NULL, *path_search_start = NULL;
132
	char *original_host = NULL;
133 134 135 136

	/* service_suffix is optional */
	assert(data && url);

137
	/* Save these for comparison later */
138 139
	original_host = data->host;
	data->host = NULL;
140 141
	gitno_connection_data_free_ptrs(data);

142
	if (!git__prefixcmp(url, prefix_http)) {
143
		path_search_start = url + strlen(prefix_http);
144 145 146
		default_port = "80";

		if (data->use_ssl) {
147
			giterr_set(GITERR_NET, "redirect from HTTPS to HTTP is not allowed");
148
			goto cleanup;
149
		}
150 151
	} else if (!git__prefixcmp(url, prefix_https)) {
		path_search_start = url + strlen(prefix_https);
152 153
		default_port = "443";
		data->use_ssl = true;
154
	} else if (url[0] == '/')
155 156
		default_port = data->use_ssl ? "443" : "80";

157
	if (!default_port) {
158
		giterr_set(GITERR_NET, "unrecognized URL prefix");
159
		goto cleanup;
160 161 162
	}

	error = gitno_extract_url_parts(
163
		&data->host, &data->port, &data->path, &data->user, &data->pass,
164 165
		url, default_port);

166 167
	if (url[0] == '/') {
		/* Relative redirect; reuse original host name and port */
168
		path_search_start = url;
169 170 171 172 173
		git__free(data->host);
		data->host = original_host;
		original_host = NULL;
	}

174
	if (!error) {
175
		const char *path = strchr(path_search_start, '/');
176 177 178 179
		size_t pathlen = strlen(path);
		size_t suffixlen = service_suffix ? strlen(service_suffix) : 0;

		if (suffixlen &&
Carlos Martín Nieto committed
180 181
		    !memcmp(path + pathlen - suffixlen, service_suffix, suffixlen)) {
			git__free(data->path);
182
			data->path = git__strndup(path, pathlen - suffixlen);
Carlos Martín Nieto committed
183 184
		} else {
			git__free(data->path);
185
			data->path = git__strdup(path);
Carlos Martín Nieto committed
186
		}
187 188 189

		/* Check for errors in the resulting data */
		if (original_host && url[0] != '/' && strcmp(original_host, data->host)) {
190
			giterr_set(GITERR_NET, "cross host redirect not allowed");
191 192 193 194
			error = -1;
		}
	}

195 196
cleanup:
	if (original_host) git__free(original_host);
197 198 199 200 201 202 203 204 205 206 207 208
	return error;
}

void gitno_connection_data_free_ptrs(gitno_connection_data *d)
{
	git__free(d->host); d->host = NULL;
	git__free(d->port); d->port = NULL;
	git__free(d->path); d->path = NULL;
	git__free(d->user); d->user = NULL;
	git__free(d->pass); d->pass = NULL;
}

209
#define hex2c(c) ((c | 32) % 39 - 9)
210 211 212
static char* unescape(char *str)
{
	int x, y;
Russell Belfer committed
213
	int len = (int)strlen(str);
214

215
	for (x=y=0; str[y]; ++x, ++y) {
216
		if ((str[x] = str[y]) == '%') {
217 218 219 220
			if (y < len-2 && isxdigit(str[y+1]) && isxdigit(str[y+2])) {
				str[x] = (hex2c(str[y+1]) << 4) + hex2c(str[y+2]);
				y += 2;
			}
221 222 223 224 225 226
		}
	}
	str[x] = '\0';
	return str;
}

227 228 229
int gitno_extract_url_parts(
		char **host,
		char **port,
230
		char **path,
231 232 233 234
		char **username,
		char **password,
		const char *url,
		const char *default_port)
235
{
236 237
	struct http_parser_url u = {0};
	const char *_host, *_port, *_path, *_userinfo;
238

239
	if (http_parser_parse_url(url, strlen(url), false, &u)) {
240
		giterr_set(GITERR_NET, "malformed URL '%s'", url);
241
		return GIT_EINVALIDSPEC;
242
	}
243

244 245 246 247 248
	_host = url+u.field_data[UF_HOST].off;
	_port = url+u.field_data[UF_PORT].off;
	_path = url+u.field_data[UF_PATH].off;
	_userinfo = url+u.field_data[UF_USERINFO].off;

Ben Straub committed
249
	if (u.field_set & (1 << UF_HOST)) {
250 251
		*host = git__substrdup(_host, u.field_data[UF_HOST].len);
		GITERR_CHECK_ALLOC(*host);
252
	}
253

Ben Straub committed
254
	if (u.field_set & (1 << UF_PORT))
255 256
		*port = git__substrdup(_port, u.field_data[UF_PORT].len);
	else
257 258
		*port = git__strdup(default_port);
	GITERR_CHECK_ALLOC(*port);
259

260 261 262 263 264 265 266 267 268 269 270 271
	if (path) {
		if (u.field_set & (1 << UF_PATH)) {
			*path = git__substrdup(_path, u.field_data[UF_PATH].len);
			GITERR_CHECK_ALLOC(*path);
		} else {
			git__free(*port);
			*port = NULL;
			git__free(*host);
			*host = NULL;
			giterr_set(GITERR_NET, "invalid url, missing path");
			return GIT_EINVALIDSPEC;
		}
272 273
	}

Ben Straub committed
274 275
	if (u.field_set & (1 << UF_USERINFO)) {
		const char *colon = memchr(_userinfo, ':', u.field_data[UF_USERINFO].len);
Ben Straub committed
276
		if (colon) {
277 278
			*username = unescape(git__substrdup(_userinfo, colon - _userinfo));
			*password = unescape(git__substrdup(colon+1, u.field_data[UF_USERINFO].len - (colon+1-_userinfo)));
279 280 281 282 283
			GITERR_CHECK_ALLOC(*password);
		} else {
			*username = git__substrdup(_userinfo, u.field_data[UF_USERINFO].len);
		}
		GITERR_CHECK_ALLOC(*username);
284

285 286
	}

287
	return 0;
288
}