Blame src/netops.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include <ctype.h>
Packit Service 20376f
#include "git2/errors.h"
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "netops.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
#include "http_parser.h"
Packit Service 20376f
#include "global.h"
Packit Service 20376f
Packit Service 20376f
int gitno_recv(gitno_buffer *buf)
Packit Service 20376f
{
Packit Service 20376f
	return buf->recv(buf);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void gitno_buffer_setup_callback(
Packit Service 20376f
	gitno_buffer *buf,
Packit Service 20376f
	char *data,
Packit Service 20376f
	size_t len,
Packit Service 20376f
	int (*recv)(gitno_buffer *buf), void *cb_data)
Packit Service 20376f
{
Packit Service 20376f
	memset(data, 0x0, len);
Packit Service 20376f
	buf->data = data;
Packit Service 20376f
	buf->len = len;
Packit Service 20376f
	buf->offset = 0;
Packit Service 20376f
	buf->recv = recv;
Packit Service 20376f
	buf->cb_data = cb_data;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int recv_stream(gitno_buffer *buf)
Packit Service 20376f
{
Packit Service 20376f
	git_stream *io = (git_stream *) buf->cb_data;
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	ret = git_stream_read(io, buf->data + buf->offset, buf->len - buf->offset);
Packit Service 20376f
	if (ret < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	buf->offset += ret;
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	memset(data, 0x0, len);
Packit Service 20376f
	buf->data = data;
Packit Service 20376f
	buf->len = len;
Packit Service 20376f
	buf->offset = 0;
Packit Service 20376f
	buf->recv = recv_stream;
Packit Service 20376f
	buf->cb_data = st;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Consume up to ptr and move the rest of the buffer to the beginning */
Packit Service 20376f
void gitno_consume(gitno_buffer *buf, const char *ptr)
Packit Service 20376f
{
Packit Service 20376f
	size_t consumed;
Packit Service 20376f
Packit Service 20376f
	assert(ptr - buf->data >= 0);
Packit Service 20376f
	assert(ptr - buf->data <= (int) buf->len);
Packit Service 20376f
Packit Service 20376f
	consumed = ptr - buf->data;
Packit Service 20376f
Packit Service 20376f
	memmove(buf->data, ptr, buf->offset - consumed);
Packit Service 20376f
	memset(buf->data + buf->offset, 0x0, buf->len - buf->offset);
Packit Service 20376f
	buf->offset -= consumed;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Consume const bytes and move the rest of the buffer to the beginning */
Packit Service 20376f
void gitno_consume_n(gitno_buffer *buf, size_t cons)
Packit Service 20376f
{
Packit Service 20376f
	memmove(buf->data, buf->data + cons, buf->len - buf->offset);
Packit Service 20376f
	memset(buf->data + cons, 0x0, buf->len - buf->offset);
Packit Service 20376f
	buf->offset -= cons;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Match host names according to RFC 2818 rules */
Packit Service 20376f
int gitno__match_host(const char *pattern, const char *host)
Packit Service 20376f
{
Packit Service 20376f
	for (;;) {
Packit Service 20376f
		char c = git__tolower(*pattern++);
Packit Service 20376f
Packit Service 20376f
		if (c == '\0')
Packit Service 20376f
			return *host ? -1 : 0;
Packit Service 20376f
Packit Service 20376f
		if (c == '*') {
Packit Service 20376f
			c = *pattern;
Packit Service 20376f
			/* '*' at the end matches everything left */
Packit Service 20376f
			if (c == '\0')
Packit Service 20376f
				return 0;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * We've found a pattern, so move towards the next matching
Packit Service 20376f
	 * char. The '.' is handled specially because wildcards aren't
Packit Service 20376f
	 * allowed to cross subdomains.
Packit Service 20376f
	 */
Packit Service 20376f
Packit Service 20376f
			while(*host) {
Packit Service 20376f
				char h = git__tolower(*host);
Packit Service 20376f
				if (c == h)
Packit Service 20376f
					return gitno__match_host(pattern, host++);
Packit Service 20376f
				if (h == '.')
Packit Service 20376f
					return gitno__match_host(pattern, host);
Packit Service 20376f
				host++;
Packit Service 20376f
			}
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (c != git__tolower(*host++))
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static const char *prefix_http = "http://";
Packit Service 20376f
static const char *prefix_https = "https://";
Packit Service 20376f
Packit Service 20376f
int gitno_connection_data_from_url(
Packit Service 20376f
		gitno_connection_data *data,
Packit Service 20376f
		const char *url,
Packit Service 20376f
		const char *service_suffix)
Packit Service 20376f
{
Packit Service 20376f
	int error = -1;
Packit Service 20376f
	const char *default_port = NULL, *path_search_start = NULL;
Packit Service 20376f
	char *original_host = NULL;
Packit Service 20376f
Packit Service 20376f
	/* service_suffix is optional */
Packit Service 20376f
	assert(data && url);
Packit Service 20376f
Packit Service 20376f
	/* Save these for comparison later */
Packit Service 20376f
	original_host = data->host;
Packit Service 20376f
	data->host = NULL;
Packit Service 20376f
	gitno_connection_data_free_ptrs(data);
Packit Service 20376f
Packit Service 20376f
	if (!git__prefixcmp(url, prefix_http)) {
Packit Service 20376f
		path_search_start = url + strlen(prefix_http);
Packit Service 20376f
		default_port = "80";
Packit Service 20376f
Packit Service 20376f
		if (data->use_ssl) {
Packit Service 20376f
			giterr_set(GITERR_NET, "redirect from HTTPS to HTTP is not allowed");
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
		}
Packit Service 20376f
	} else if (!git__prefixcmp(url, prefix_https)) {
Packit Service 20376f
		path_search_start = url + strlen(prefix_https);
Packit Service 20376f
		default_port = "443";
Packit Service 20376f
		data->use_ssl = true;
Packit Service 20376f
	} else if (url[0] == '/')
Packit Service 20376f
		default_port = data->use_ssl ? "443" : "80";
Packit Service 20376f
Packit Service 20376f
	if (!default_port) {
Packit Service 20376f
		giterr_set(GITERR_NET, "unrecognized URL prefix");
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = gitno_extract_url_parts(
Packit Service 20376f
		&data->host, &data->port, &data->path, &data->user, &data->pass,
Packit Service 20376f
		url, default_port);
Packit Service 20376f
Packit Service 20376f
	if (url[0] == '/') {
Packit Service 20376f
		/* Relative redirect; reuse original host name and port */
Packit Service 20376f
		path_search_start = url;
Packit Service 20376f
		git__free(data->host);
Packit Service 20376f
		data->host = original_host;
Packit Service 20376f
		original_host = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!error) {
Packit Service 20376f
		const char *path = strchr(path_search_start, '/');
Packit Service 20376f
		size_t pathlen = strlen(path);
Packit Service 20376f
		size_t suffixlen = service_suffix ? strlen(service_suffix) : 0;
Packit Service 20376f
Packit Service 20376f
		if (suffixlen &&
Packit Service 20376f
		    !memcmp(path + pathlen - suffixlen, service_suffix, suffixlen)) {
Packit Service 20376f
			git__free(data->path);
Packit Service 20376f
			data->path = git__strndup(path, pathlen - suffixlen);
Packit Service 20376f
		} else {
Packit Service 20376f
			git__free(data->path);
Packit Service 20376f
			data->path = git__strdup(path);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Check for errors in the resulting data */
Packit Service 20376f
		if (original_host && url[0] != '/' && strcmp(original_host, data->host)) {
Packit Service 20376f
			giterr_set(GITERR_NET, "cross host redirect not allowed");
Packit Service 20376f
			error = -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	if (original_host) git__free(original_host);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void gitno_connection_data_free_ptrs(gitno_connection_data *d)
Packit Service 20376f
{
Packit Service 20376f
	git__free(d->host); d->host = NULL;
Packit Service 20376f
	git__free(d->port); d->port = NULL;
Packit Service 20376f
	git__free(d->path); d->path = NULL;
Packit Service 20376f
	git__free(d->user); d->user = NULL;
Packit Service 20376f
	git__free(d->pass); d->pass = NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define hex2c(c) ((c | 32) % 39 - 9)
Packit Service 20376f
static char* unescape(char *str)
Packit Service 20376f
{
Packit Service 20376f
	int x, y;
Packit Service 20376f
	int len = (int)strlen(str);
Packit Service 20376f
Packit Service 20376f
	for (x=y=0; str[y]; ++x, ++y) {
Packit Service 20376f
		if ((str[x] = str[y]) == '%') {
Packit Service 20376f
			if (y < len-2 && isxdigit(str[y+1]) && isxdigit(str[y+2])) {
Packit Service 20376f
				str[x] = (hex2c(str[y+1]) << 4) + hex2c(str[y+2]);
Packit Service 20376f
				y += 2;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
	str[x] = '\0';
Packit Service 20376f
	return str;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int gitno_extract_url_parts(
Packit Service 20376f
		char **host,
Packit Service 20376f
		char **port,
Packit Service 20376f
		char **path,
Packit Service 20376f
		char **username,
Packit Service 20376f
		char **password,
Packit Service 20376f
		const char *url,
Packit Service 20376f
		const char *default_port)
Packit Service 20376f
{
Packit Service 20376f
	struct http_parser_url u = {0};
Packit Service 20376f
	const char *_host, *_port, *_path, *_userinfo;
Packit Service 20376f
Packit Service 20376f
	if (http_parser_parse_url(url, strlen(url), false, &u)) {
Packit Service 20376f
		giterr_set(GITERR_NET, "malformed URL '%s'", url);
Packit Service 20376f
		return GIT_EINVALIDSPEC;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	_host = url+u.field_data[UF_HOST].off;
Packit Service 20376f
	_port = url+u.field_data[UF_PORT].off;
Packit Service 20376f
	_path = url+u.field_data[UF_PATH].off;
Packit Service 20376f
	_userinfo = url+u.field_data[UF_USERINFO].off;
Packit Service 20376f
Packit Service 20376f
	if (u.field_set & (1 << UF_HOST)) {
Packit Service 20376f
		*host = git__substrdup(_host, u.field_data[UF_HOST].len);
Packit Service 20376f
		GITERR_CHECK_ALLOC(*host);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (u.field_set & (1 << UF_PORT))
Packit Service 20376f
		*port = git__substrdup(_port, u.field_data[UF_PORT].len);
Packit Service 20376f
	else
Packit Service 20376f
		*port = git__strdup(default_port);
Packit Service 20376f
	GITERR_CHECK_ALLOC(*port);
Packit Service 20376f
Packit Service 20376f
	if (path) {
Packit Service 20376f
		if (u.field_set & (1 << UF_PATH)) {
Packit Service 20376f
			*path = git__substrdup(_path, u.field_data[UF_PATH].len);
Packit Service 20376f
			GITERR_CHECK_ALLOC(*path);
Packit Service 20376f
		} else {
Packit Service 20376f
			git__free(*port);
Packit Service 20376f
			*port = NULL;
Packit Service 20376f
			git__free(*host);
Packit Service 20376f
			*host = NULL;
Packit Service 20376f
			giterr_set(GITERR_NET, "invalid url, missing path");
Packit Service 20376f
			return GIT_EINVALIDSPEC;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (u.field_set & (1 << UF_USERINFO)) {
Packit Service 20376f
		const char *colon = memchr(_userinfo, ':', u.field_data[UF_USERINFO].len);
Packit Service 20376f
		if (colon) {
Packit Service 20376f
			*username = unescape(git__substrdup(_userinfo, colon - _userinfo));
Packit Service 20376f
			*password = unescape(git__substrdup(colon+1, u.field_data[UF_USERINFO].len - (colon+1-_userinfo)));
Packit Service 20376f
			GITERR_CHECK_ALLOC(*password);
Packit Service 20376f
		} else {
Packit Service 20376f
			*username = git__substrdup(_userinfo, u.field_data[UF_USERINFO].len);
Packit Service 20376f
		}
Packit Service 20376f
		GITERR_CHECK_ALLOC(*username);
Packit Service 20376f
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}