summaryrefslogtreecommitdiff
path: root/transport.c
diff options
context:
space:
mode:
Diffstat (limited to 'transport.c')
-rw-r--r--transport.c105
1 files changed, 76 insertions, 29 deletions
diff --git a/transport.c b/transport.c
index 2d51348f3f..988047b12e 100644
--- a/transport.c
+++ b/transport.c
@@ -15,6 +15,7 @@
#include "submodule.h"
#include "string-list.h"
#include "sha1-array.h"
+#include "sigchain.h"
/* rsync support */
@@ -79,7 +80,7 @@ static int read_loose_refs(struct strbuf *path, int name_offset,
continue;
next = alloc_ref(path->buf + name_offset);
if (read_in_full(fd, buffer, 40) != 40 ||
- get_sha1_hex(buffer, next->old_sha1)) {
+ get_oid_hex(buffer, &next->old_oid)) {
close(fd);
free(next);
continue;
@@ -131,7 +132,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
if (!(*list)->next || cmp < 0) {
struct ref *next = alloc_ref(buffer + 41);
buffer[40] = '\0';
- if (get_sha1_hex(buffer, next->old_sha1)) {
+ if (get_oid_hex(buffer, &next->old_oid)) {
warning ("invalid SHA-1: %s", buffer);
free(next);
continue;
@@ -162,7 +163,7 @@ static void set_upstreams(struct transport *transport, struct ref *refs,
continue;
if (!ref->peer_ref)
continue;
- if (is_null_sha1(ref->new_sha1))
+ if (is_null_oid(&ref->new_oid))
continue;
/* Follow symbolic refs (mainly for HEAD). */
@@ -412,7 +413,7 @@ static struct ref *get_refs_from_bundle(struct transport *transport, int for_pus
for (i = 0; i < data->header.references.nr; i++) {
struct ref_list_entry *e = data->header.references.list + i;
struct ref *ref = alloc_ref(e->name);
- hashcpy(ref->old_sha1, e->sha1);
+ hashcpy(ref->old_oid.hash, e->sha1);
ref->next = result;
result = ref;
}
@@ -608,7 +609,7 @@ void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int v
delete_ref(rs.dst, NULL, 0);
} else
update_ref("update by push", rs.dst,
- ref->new_sha1, NULL, 0, 0);
+ ref->new_oid.hash, NULL, 0, 0);
free(rs.dst);
}
}
@@ -648,29 +649,30 @@ static void print_ok_ref_status(struct ref *ref, int porcelain)
{
if (ref->deletion)
print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
- else if (is_null_sha1(ref->old_sha1))
+ else if (is_null_oid(&ref->old_oid))
print_ref_status('*',
(starts_with(ref->name, "refs/tags/") ? "[new tag]" :
"[new branch]"),
ref, ref->peer_ref, NULL, porcelain);
else {
- char quickref[84];
+ struct strbuf quickref = STRBUF_INIT;
char type;
const char *msg;
- strcpy(quickref, status_abbrev(ref->old_sha1));
+ strbuf_addstr(&quickref, status_abbrev(ref->old_oid.hash));
if (ref->forced_update) {
- strcat(quickref, "...");
+ strbuf_addstr(&quickref, "...");
type = '+';
msg = "forced update";
} else {
- strcat(quickref, "..");
+ strbuf_addstr(&quickref, "..");
type = ' ';
msg = NULL;
}
- strcat(quickref, status_abbrev(ref->new_sha1));
+ strbuf_addstr(&quickref, status_abbrev(ref->new_oid.hash));
- print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
+ print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
+ strbuf_release(&quickref);
}
}
@@ -915,6 +917,42 @@ static int external_specification_len(const char *url)
return strchr(url, ':') - url;
}
+static const struct string_list *protocol_whitelist(void)
+{
+ static int enabled = -1;
+ static struct string_list allowed = STRING_LIST_INIT_DUP;
+
+ if (enabled < 0) {
+ const char *v = getenv("GIT_ALLOW_PROTOCOL");
+ if (v) {
+ string_list_split(&allowed, v, ':', -1);
+ string_list_sort(&allowed);
+ enabled = 1;
+ } else {
+ enabled = 0;
+ }
+ }
+
+ return enabled ? &allowed : NULL;
+}
+
+int is_transport_allowed(const char *type)
+{
+ const struct string_list *allowed = protocol_whitelist();
+ return !allowed || string_list_has_string(allowed, type);
+}
+
+void transport_check_allowed(const char *type)
+{
+ if (!is_transport_allowed(type))
+ die("transport '%s' not allowed", type);
+}
+
+int transport_restrict_protocols(void)
+{
+ return !!protocol_whitelist();
+}
+
struct transport *transport_get(struct remote *remote, const char *url)
{
const char *helper;
@@ -946,12 +984,14 @@ struct transport *transport_get(struct remote *remote, const char *url)
if (helper) {
transport_helper_init(ret, helper);
} else if (starts_with(url, "rsync:")) {
+ transport_check_allowed("rsync");
ret->get_refs_list = get_refs_via_rsync;
ret->fetch = fetch_objs_via_rsync;
ret->push = rsync_transport_push;
ret->smart_options = NULL;
} else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
+ transport_check_allowed("file");
ret->data = data;
ret->get_refs_list = get_refs_from_bundle;
ret->fetch = fetch_refs_from_bundle;
@@ -963,7 +1003,10 @@ struct transport *transport_get(struct remote *remote, const char *url)
|| starts_with(url, "ssh://")
|| starts_with(url, "git+ssh://")
|| starts_with(url, "ssh+git://")) {
- /* These are builtin smart transports. */
+ /*
+ * These are builtin smart transports; "allowed" transports
+ * will be checked individually in git_connect.
+ */
struct git_transport_data *data = xcalloc(1, sizeof(*data));
ret->data = data;
ret->set_option = NULL;
@@ -1085,6 +1128,8 @@ static int run_pre_push_hook(struct transport *transport,
return -1;
}
+ sigchain_push(SIGPIPE, SIG_IGN);
+
strbuf_init(&buf, 256);
for (r = remote_refs; r; r = r->next) {
@@ -1095,11 +1140,13 @@ static int run_pre_push_hook(struct transport *transport,
strbuf_reset(&buf);
strbuf_addf( &buf, "%s %s %s %s\n",
- r->peer_ref->name, sha1_to_hex(r->new_sha1),
- r->name, sha1_to_hex(r->old_sha1));
+ r->peer_ref->name, oid_to_hex(&r->new_oid),
+ r->name, oid_to_hex(&r->old_oid));
- if (write_in_full(proc.in, buf.buf, buf.len) != buf.len) {
- ret = -1;
+ if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
+ /* We do not mind if a hook does not read all refs. */
+ if (errno != EPIPE)
+ ret = -1;
break;
}
}
@@ -1110,6 +1157,8 @@ static int run_pre_push_hook(struct transport *transport,
if (!ret)
ret = x;
+ sigchain_pop(SIGPIPE);
+
x = finish_command(&proc);
if (!ret)
ret = x;
@@ -1176,8 +1225,8 @@ int transport_push(struct transport *transport,
if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
struct ref *ref = remote_refs;
for (; ref; ref = ref->next)
- if (!is_null_sha1(ref->new_sha1) &&
- !push_unpushed_submodules(ref->new_sha1,
+ if (!is_null_oid(&ref->new_oid) &&
+ !push_unpushed_submodules(ref->new_oid.hash,
transport->remote->name))
die ("Failed to push all needed submodules!");
}
@@ -1188,8 +1237,8 @@ int transport_push(struct transport *transport,
struct string_list needs_pushing = STRING_LIST_INIT_DUP;
for (; ref; ref = ref->next)
- if (!is_null_sha1(ref->new_sha1) &&
- find_unpushed_submodules(ref->new_sha1,
+ if (!is_null_oid(&ref->new_oid) &&
+ find_unpushed_submodules(ref->new_oid.hash,
transport->remote->name, &needs_pushing))
die_with_unpushed_submodules(&needs_pushing);
}
@@ -1242,8 +1291,8 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)
for (rm = refs; rm; rm = rm->next) {
nr_refs++;
if (rm->peer_ref &&
- !is_null_sha1(rm->old_sha1) &&
- !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
+ !is_null_oid(&rm->old_oid) &&
+ !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
continue;
ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
heads[nr_heads++] = rm;
@@ -1257,7 +1306,7 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)
* This condition shouldn't be met in a non-deepening fetch
* (see builtin/fetch.c:quickfetch()).
*/
- heads = xmalloc(nr_refs * sizeof(*heads));
+ ALLOC_ARRAY(heads, nr_refs);
for (rm = refs; rm; rm = rm->next)
heads[nr_heads++] = rm;
}
@@ -1301,7 +1350,7 @@ int transport_disconnect(struct transport *transport)
*/
char *transport_anonymize_url(const char *url)
{
- char *anon_url, *scheme_prefix, *anon_part;
+ char *scheme_prefix, *anon_part;
size_t anon_len, prefix_len = 0;
anon_part = strchr(url, '@');
@@ -1335,10 +1384,8 @@ char *transport_anonymize_url(const char *url)
goto literal_copy;
prefix_len = scheme_prefix - url + 3;
}
- anon_url = xcalloc(1, 1 + prefix_len + anon_len);
- memcpy(anon_url, url, prefix_len);
- memcpy(anon_url + prefix_len, anon_part, anon_len);
- return anon_url;
+ return xstrfmt("%.*s%.*s", (int)prefix_len, url,
+ (int)anon_len, anon_part);
literal_copy:
return xstrdup(url);
}