transport: refactor protocol whitelist code
[gitweb.git] / transport.c
index 5485e2ab1e86fd7f02c2078ed6505cdec292ee4f..647d2c2afaadb4f39dd74b182db969eaf0623aa0 100644 (file)
@@ -14,6 +14,7 @@
 #include "url.h"
 #include "submodule.h"
 #include "string-list.h"
+#include "sha1-array.h"
 
 /* rsync support */
 
@@ -116,7 +117,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
                        return;
                }
 
-               if (hexval(buffer[0]) > 0xf)
+               if (!isxdigit(buffer[0]))
                        continue;
                len = strlen(buffer);
                if (len && buffer[len - 1] == '\n')
@@ -167,15 +168,16 @@ static void set_upstreams(struct transport *transport, struct ref *refs,
                /* Follow symbolic refs (mainly for HEAD). */
                localname = ref->peer_ref->name;
                remotename = ref->name;
-               tmp = resolve_ref_unsafe(localname, sha, 1, &flag);
+               tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
+                                        sha, &flag);
                if (tmp && flag & REF_ISSYMREF &&
-                       !prefixcmp(tmp, "refs/heads/"))
+                       starts_with(tmp, "refs/heads/"))
                        localname = tmp;
 
                /* Both source and destination must be local branches. */
-               if (!localname || prefixcmp(localname, "refs/heads/"))
+               if (!localname || !starts_with(localname, "refs/heads/"))
                        continue;
-               if (!remotename || prefixcmp(remotename, "refs/heads/"))
+               if (!remotename || !starts_with(remotename, "refs/heads/"))
                        continue;
 
                if (!pretend)
@@ -191,14 +193,16 @@ static void set_upstreams(struct transport *transport, struct ref *refs,
 
 static const char *rsync_url(const char *url)
 {
-       return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
+       if (!starts_with(url, "rsync://"))
+               skip_prefix(url, "rsync:", &url);
+       return url;
 }
 
 static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
 {
        struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
        struct ref dummy = {NULL}, *tail = &dummy;
-       struct child_process rsync;
+       struct child_process rsync = CHILD_PROCESS_INIT;
        const char *args[5];
        int temp_dir_len;
 
@@ -215,7 +219,6 @@ static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
        strbuf_addstr(&buf, rsync_url(transport->url));
        strbuf_addstr(&buf, "/refs");
 
-       memset(&rsync, 0, sizeof(rsync));
        rsync.argv = args;
        rsync.stdout_to_stderr = 1;
        args[0] = "rsync";
@@ -260,32 +263,19 @@ static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
 static int fetch_objs_via_rsync(struct transport *transport,
                                int nr_objs, struct ref **to_fetch)
 {
-       struct strbuf buf = STRBUF_INIT;
-       struct child_process rsync;
-       const char *args[8];
-       int result;
+       struct child_process rsync = CHILD_PROCESS_INIT;
 
-       strbuf_addstr(&buf, rsync_url(transport->url));
-       strbuf_addstr(&buf, "/objects/");
-
-       memset(&rsync, 0, sizeof(rsync));
-       rsync.argv = args;
        rsync.stdout_to_stderr = 1;
-       args[0] = "rsync";
-       args[1] = (transport->verbose > 1) ? "-rv" : "-r";
-       args[2] = "--ignore-existing";
-       args[3] = "--exclude";
-       args[4] = "info";
-       args[5] = buf.buf;
-       args[6] = get_object_directory();
-       args[7] = NULL;
+       argv_array_push(&rsync.args, "rsync");
+       argv_array_push(&rsync.args, (transport->verbose > 1) ? "-rv" : "-r");
+       argv_array_push(&rsync.args, "--ignore-existing");
+       argv_array_push(&rsync.args, "--exclude");
+       argv_array_push(&rsync.args, "info");
+       argv_array_pushf(&rsync.args, "%s/objects/", rsync_url(transport->url));
+       argv_array_push(&rsync.args, get_object_directory());
 
        /* NEEDSWORK: handle one level of alternates */
-       result = run_command(&rsync);
-
-       strbuf_release(&buf);
-
-       return result;
+       return run_command(&rsync);
 }
 
 static int write_one_ref(const char *name, const unsigned char *sha1,
@@ -296,8 +286,8 @@ static int write_one_ref(const char *name, const unsigned char *sha1,
        FILE *f;
 
        /* when called via for_each_ref(), flags is non-zero */
-       if (flags && prefixcmp(name, "refs/heads/") &&
-                       prefixcmp(name, "refs/tags/"))
+       if (flags && !starts_with(name, "refs/heads/") &&
+                       !starts_with(name, "refs/tags/"))
                return 0;
 
        strbuf_addstr(buf, name);
@@ -336,7 +326,7 @@ static int rsync_transport_push(struct transport *transport,
 {
        struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
        int result = 0, i;
-       struct child_process rsync;
+       struct child_process rsync = CHILD_PROCESS_INIT;
        const char *args[10];
 
        if (flags & TRANSPORT_PUSH_MIRROR)
@@ -347,7 +337,6 @@ static int rsync_transport_push(struct transport *transport,
        strbuf_addstr(&buf, rsync_url(transport->url));
        strbuf_addch(&buf, '/');
 
-       memset(&rsync, 0, sizeof(rsync));
        rsync.argv = args;
        rsync.stdout_to_stderr = 1;
        i = 0;
@@ -454,7 +443,8 @@ struct git_transport_data {
        struct child_process *conn;
        int fd[2];
        unsigned got_remote_heads : 1;
-       struct extra_have_objects extra_have;
+       struct sha1_array extra_have;
+       struct sha1_array shallow;
 };
 
 static int set_git_option(struct git_transport_options *opts,
@@ -475,6 +465,9 @@ static int set_git_option(struct git_transport_options *opts,
        } else if (!strcmp(name, TRANS_OPT_KEEP)) {
                opts->keep = !!value;
                return 0;
+       } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
+               opts->update_shallow = !!value;
+               return 0;
        } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
                if (!value)
                        opts->depth = 0;
@@ -485,6 +478,9 @@ static int set_git_option(struct git_transport_options *opts,
                                die("transport: invalid depth option '%s'", value);
                }
                return 0;
+       } else if (!strcmp(name, TRANS_OPT_PUSH_CERT)) {
+               opts->push_cert = !!value;
+               return 0;
        }
        return 1;
 }
@@ -511,7 +507,9 @@ static struct ref *get_refs_via_connect(struct transport *transport, int for_pus
 
        connect_setup(transport, for_push, 0);
        get_remote_heads(data->fd[0], NULL, 0, &refs,
-                        for_push ? REF_NORMAL : 0, &data->extra_have);
+                        for_push ? REF_NORMAL : 0,
+                        &data->extra_have,
+                        &data->shallow);
        data->got_remote_heads = 1;
 
        return refs;
@@ -521,7 +519,7 @@ static int fetch_refs_via_pack(struct transport *transport,
                               int nr_heads, struct ref **to_fetch)
 {
        struct git_transport_data *data = transport->data;
-       const struct ref *refs;
+       struct ref *refs;
        char *dest = xstrdup(transport->url);
        struct fetch_pack_args args;
        struct ref *refs_tmp = NULL;
@@ -538,28 +536,33 @@ static int fetch_refs_via_pack(struct transport *transport,
        args.depth = data->options.depth;
        args.check_self_contained_and_connected =
                data->options.check_self_contained_and_connected;
+       args.cloning = transport->cloning;
+       args.update_shallow = data->options.update_shallow;
 
        if (!data->got_remote_heads) {
                connect_setup(transport, 0, 0);
-               get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0, NULL);
+               get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
+                                NULL, &data->shallow);
                data->got_remote_heads = 1;
        }
 
        refs = fetch_pack(&args, data->fd, data->conn,
                          refs_tmp ? refs_tmp : transport->remote_refs,
-                         dest, to_fetch, nr_heads,
+                         dest, to_fetch, nr_heads, &data->shallow,
                          &transport->pack_lockfile);
        close(data->fd[0]);
        close(data->fd[1]);
-       if (finish_connect(data->conn))
+       if (finish_connect(data->conn)) {
+               free_refs(refs);
                refs = NULL;
+       }
        data->conn = NULL;
        data->got_remote_heads = 0;
        data->options.self_contained_and_connected =
                args.self_contained_and_connected;
 
        free_refs(refs_tmp);
-
+       free_refs(refs);
        free(dest);
        return (refs ? 0 : -1);
 }
@@ -652,7 +655,7 @@ static void print_ok_ref_status(struct ref *ref, int porcelain)
                print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
        else if (is_null_sha1(ref->old_sha1))
                print_ref_status('*',
-                       (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
+                       (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
                        "[new branch]"),
                        ref, ref->peer_ref, NULL, porcelain);
        else {
@@ -713,6 +716,10 @@ static int print_one_push_status(struct ref *ref, const char *dest, int count, i
                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
                                                 "stale info", porcelain);
                break;
+       case REF_STATUS_REJECT_SHALLOW:
+               print_ref_status('!', "[rejected]", ref, ref->peer_ref,
+                                                "new shallow roots not allowed", porcelain);
+               break;
        case REF_STATUS_REMOTE_REJECT:
                print_ref_status('!', "[remote rejected]", ref,
                                                 ref->deletion ? NULL : ref->peer_ref,
@@ -739,7 +746,7 @@ void transport_print_push_status(const char *dest, struct ref *refs,
        unsigned char head_sha1[20];
        char *head;
 
-       head = resolve_refdup("HEAD", head_sha1, 1, NULL);
+       head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
 
        if (verbose) {
                for (ref = refs; ref; ref = ref->next)
@@ -770,6 +777,7 @@ void transport_print_push_status(const char *dest, struct ref *refs,
                        *reject_reasons |= REJECT_NEEDS_FORCE;
                }
        }
+       free(head);
 }
 
 void transport_verify_remote_names(int nr_heads, const char **heads)
@@ -805,7 +813,8 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re
                struct ref *tmp_refs;
                connect_setup(transport, 1, 0);
 
-               get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL, NULL);
+               get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
+                                NULL, &data->shallow);
                data->got_remote_heads = 1;
        }
 
@@ -818,6 +827,8 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re
        args.progress = transport->progress;
        args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
        args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
+       args.push_cert = !!(flags & TRANSPORT_PUSH_CERT);
+       args.url = transport->url;
 
        ret = send_pack(&args, data->fd, data->conn, remote_refs,
                        &data->extra_have);
@@ -898,6 +909,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;
@@ -922,31 +969,36 @@ struct transport *transport_get(struct remote *remote, const char *url)
 
                while (is_urlschemechar(p == url, *p))
                        p++;
-               if (!prefixcmp(p, "::"))
+               if (starts_with(p, "::"))
                        helper = xstrndup(url, p - url);
        }
 
        if (helper) {
                transport_helper_init(ret, helper);
-       } else if (!prefixcmp(url, "rsync:")) {
+       } 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;
                ret->disconnect = close_bundle;
                ret->smart_options = NULL;
        } else if (!is_url(url)
-               || !prefixcmp(url, "file://")
-               || !prefixcmp(url, "git://")
-               || !prefixcmp(url, "ssh://")
-               || !prefixcmp(url, "git+ssh://")
-               || !prefixcmp(url, "ssh+git://")) {
-               /* These are builtin smart transports. */
+               || starts_with(url, "file://")
+               || starts_with(url, "git://")
+               || starts_with(url, "ssh://")
+               || starts_with(url, "git+ssh://")
+               || starts_with(url, "ssh+git://")) {
+               /*
+                * 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;
@@ -962,9 +1014,7 @@ struct transport *transport_get(struct remote *remote, const char *url)
        } else {
                /* Unknown protocol in URL. Pass to external handler. */
                int len = external_specification_len(url);
-               char *handler = xmalloc(len + 1);
-               handler[len] = 0;
-               strncpy(handler, url, len);
+               char *handler = xmemdupz(url, len);
                transport_helper_init(ret, handler);
        }
 
@@ -1051,7 +1101,7 @@ static int run_pre_push_hook(struct transport *transport,
 {
        int ret = 0, x;
        struct ref *r;
-       struct child_process proc;
+       struct child_process proc = CHILD_PROCESS_INIT;
        struct strbuf buf;
        const char *argv[4];
 
@@ -1062,7 +1112,6 @@ static int run_pre_push_hook(struct transport *transport,
        argv[2] = transport->url;
        argv[3] = NULL;
 
-       memset(&proc, 0, sizeof(proc));
        proc.argv = argv;
        proc.in = -1;
 
@@ -1117,8 +1166,7 @@ int transport_push(struct transport *transport,
 
                return transport->push(transport, refspec_nr, refspec, flags);
        } else if (transport->push_refs) {
-               struct ref *remote_refs =
-                       transport->get_refs_list(transport, 1);
+               struct ref *remote_refs;
                struct ref *local_refs = get_local_heads();
                int match_flags = MATCH_REFS_NONE;
                int verbose = (transport->verbose > 0);
@@ -1127,6 +1175,11 @@ int transport_push(struct transport *transport,
                int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
                int push_ret, ret, err;
 
+               if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
+                       return -1;
+
+               remote_refs = transport->get_refs_list(transport, 1);
+
                if (flags & TRANSPORT_PUSH_ALL)
                        match_flags |= MATCH_REFS_ALL;
                if (flags & TRANSPORT_PUSH_MIRROR)
@@ -1167,10 +1220,8 @@ int transport_push(struct transport *transport,
                if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
                              TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
                        struct ref *ref = remote_refs;
-                       struct string_list needs_pushing;
+                       struct string_list needs_pushing = STRING_LIST_INIT_DUP;
 
-                       memset(&needs_pushing, 0, sizeof(struct string_list));
-                       needs_pushing.strdup_strings = 1;
                        for (; ref; ref = ref->next)
                                if (!is_null_sha1(ref->new_sha1) &&
                                    find_unpushed_submodules(ref->new_sha1,
@@ -1350,11 +1401,11 @@ static int refs_from_alternate_cb(struct alternate_object_database *e,
        while (other[len-1] == '/')
                other[--len] = '\0';
        if (len < 8 || memcmp(other + len - 8, "/objects", 8))
-               return 0;
+               goto out;
        /* Is this a git repository with refs? */
        memcpy(other + len - 8, "/refs", 6);
        if (!is_directory(other))
-               return 0;
+               goto out;
        other[len - 8] = '\0';
        remote = remote_get(other);
        transport = transport_get(remote, other);
@@ -1363,6 +1414,7 @@ static int refs_from_alternate_cb(struct alternate_object_database *e,
             extra = extra->next)
                cb->fn(extra, cb->data);
        transport_disconnect(transport);
+out:
        free(other);
        return 0;
 }