Jason Riedy | 731043f | 2006-01-25 12:38:36 -0800 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 2 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 4 | #include "hex.h" |
Linus Torvalds | 41cb748 | 2005-07-05 15:44:09 -0700 | [diff] [blame] | 5 | #include "pkt-line.h" |
Junio C Hamano | b10d0ec | 2005-07-08 00:02:52 -0700 | [diff] [blame] | 6 | #include "quote.h" |
Junio C Hamano | 6abf5c0 | 2005-10-16 00:25:26 -0700 | [diff] [blame] | 7 | #include "refs.h" |
Shawn O. Pearce | 15a1c012 | 2007-03-12 19:00:19 -0400 | [diff] [blame] | 8 | #include "run-command.h" |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 9 | #include "remote.h" |
Junio C Hamano | 47a5918 | 2013-07-08 13:56:53 -0700 | [diff] [blame] | 10 | #include "connect.h" |
Jeff King | 9d2e942 | 2010-05-23 05:19:44 -0400 | [diff] [blame] | 11 | #include "url.h" |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 12 | #include "string-list.h" |
Jeff King | fe299ec | 2020-03-30 10:03:46 -0400 | [diff] [blame] | 13 | #include "oid-array.h" |
Jeff King | a5adace | 2015-09-16 13:12:52 -0400 | [diff] [blame] | 14 | #include "transport.h" |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 15 | #include "strbuf.h" |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 16 | #include "version.h" |
Brandon Williams | 2609043 | 2017-10-16 10:55:27 -0700 | [diff] [blame] | 17 | #include "protocol.h" |
Nguyễn Thái Ngọc Duy | 65b5f94 | 2018-05-20 20:40:06 +0200 | [diff] [blame] | 18 | #include "alias.h" |
Ævar Arnfjörð Bjarmason | 0cfde74 | 2022-12-22 15:14:09 +0000 | [diff] [blame] | 19 | #include "bundle-uri.h" |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 20 | |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 21 | static char *server_capabilities_v1; |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 22 | static struct strvec server_capabilities_v2 = STRVEC_INIT; |
brian m. carlson | 2c6a403 | 2020-05-25 19:58:52 +0000 | [diff] [blame] | 23 | static const char *next_server_feature_value(const char *feature, int *len, int *offset); |
Johannes Schindelin | 211b5f9 | 2005-10-28 04:48:54 +0200 | [diff] [blame] | 24 | |
René Scharfe | be0b3f8 | 2014-08-30 11:46:54 +0200 | [diff] [blame] | 25 | static int check_ref(const char *name, unsigned int flags) |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 26 | { |
| 27 | if (!flags) |
| 28 | return 1; |
| 29 | |
René Scharfe | be0b3f8 | 2014-08-30 11:46:54 +0200 | [diff] [blame] | 30 | if (!skip_prefix(name, "refs/", &name)) |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 31 | return 0; |
| 32 | |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 33 | /* REF_NORMAL means that we don't want the magic fake tag refs */ |
ZheNing Hu | 7c3c550 | 2023-03-01 10:20:29 +0000 | [diff] [blame] | 34 | if ((flags & REF_NORMAL) && check_refname_format(name, |
| 35 | REFNAME_ALLOW_ONELEVEL)) |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 36 | return 0; |
| 37 | |
| 38 | /* REF_HEADS means that we want regular branch heads */ |
René Scharfe | be0b3f8 | 2014-08-30 11:46:54 +0200 | [diff] [blame] | 39 | if ((flags & REF_HEADS) && starts_with(name, "heads/")) |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 40 | return 1; |
| 41 | |
| 42 | /* REF_TAGS means that we want tags */ |
René Scharfe | be0b3f8 | 2014-08-30 11:46:54 +0200 | [diff] [blame] | 43 | if ((flags & REF_TAGS) && starts_with(name, "tags/")) |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 44 | return 1; |
| 45 | |
| 46 | /* All type bits clear means that we are ok with anything */ |
| 47 | return !(flags & ~REF_NORMAL); |
| 48 | } |
| 49 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 50 | int check_ref_type(const struct ref *ref, int flags) |
| 51 | { |
René Scharfe | be0b3f8 | 2014-08-30 11:46:54 +0200 | [diff] [blame] | 52 | return check_ref(ref->name, flags); |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Nguyễn Thái Ngọc Duy | d2bff22 | 2018-04-14 19:19:43 +0000 | [diff] [blame] | 55 | static NORETURN void die_initial_contact(int unexpected) |
Heiko Voigt | 46284dd | 2012-06-19 20:24:50 +0200 | [diff] [blame] | 56 | { |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 57 | /* |
| 58 | * A hang-up after seeing some response from the other end |
| 59 | * means that it is unexpected, as we know the other end is |
| 60 | * willing to talk to us. A hang-up before seeing any |
| 61 | * response does not necessarily mean an ACL problem, though. |
| 62 | */ |
Jonathan Nieder | 55e4f93 | 2016-09-09 10:36:29 -0700 | [diff] [blame] | 63 | if (unexpected) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 64 | die(_("the remote end hung up upon initial contact")); |
Heiko Voigt | 46284dd | 2012-06-19 20:24:50 +0200 | [diff] [blame] | 65 | else |
Vasco Almeida | f2b93b3 | 2016-09-19 13:08:17 +0000 | [diff] [blame] | 66 | die(_("Could not read from remote repository.\n\n" |
| 67 | "Please make sure you have the correct access rights\n" |
| 68 | "and the repository exists.")); |
Heiko Voigt | 46284dd | 2012-06-19 20:24:50 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 71 | /* Checks if the server supports the capability 'c' */ |
Jeff King | a31cfe3 | 2022-12-13 05:52:58 -0500 | [diff] [blame] | 72 | int server_supports_v2(const char *c) |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 73 | { |
| 74 | int i; |
| 75 | |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 76 | for (i = 0; i < server_capabilities_v2.nr; i++) { |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 77 | const char *out; |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 78 | if (skip_prefix(server_capabilities_v2.v[i], c, &out) && |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 79 | (!*out || *out == '=')) |
| 80 | return 1; |
| 81 | } |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
Jeff King | a31cfe3 | 2022-12-13 05:52:58 -0500 | [diff] [blame] | 85 | void ensure_server_supports_v2(const char *c) |
| 86 | { |
| 87 | if (!server_supports_v2(c)) |
| 88 | die(_("server doesn't support '%s'"), c); |
| 89 | } |
| 90 | |
brian m. carlson | 1349ffe | 2020-05-25 19:58:53 +0000 | [diff] [blame] | 91 | int server_feature_v2(const char *c, const char **v) |
| 92 | { |
| 93 | int i; |
| 94 | |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 95 | for (i = 0; i < server_capabilities_v2.nr; i++) { |
brian m. carlson | 1349ffe | 2020-05-25 19:58:53 +0000 | [diff] [blame] | 96 | const char *out; |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 97 | if (skip_prefix(server_capabilities_v2.v[i], c, &out) && |
brian m. carlson | 1349ffe | 2020-05-25 19:58:53 +0000 | [diff] [blame] | 98 | (*out == '=')) { |
| 99 | *v = out + 1; |
| 100 | return 1; |
| 101 | } |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 106 | int server_supports_feature(const char *c, const char *feature, |
| 107 | int die_on_error) |
| 108 | { |
| 109 | int i; |
| 110 | |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 111 | for (i = 0; i < server_capabilities_v2.nr; i++) { |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 112 | const char *out; |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 113 | if (skip_prefix(server_capabilities_v2.v[i], c, &out) && |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 114 | (!*out || *(out++) == '=')) { |
| 115 | if (parse_feature_request(out, feature)) |
| 116 | return 1; |
| 117 | else |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (die_on_error) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 123 | die(_("server doesn't support feature '%s'"), feature); |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 128 | static void process_capabilities_v2(struct packet_reader *reader) |
| 129 | { |
| 130 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 131 | strvec_push(&server_capabilities_v2, reader->line); |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 132 | |
| 133 | if (reader->status != PACKET_READ_FLUSH) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 134 | die(_("expected flush after capabilities")); |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 137 | enum protocol_version discover_version(struct packet_reader *reader) |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 138 | { |
| 139 | enum protocol_version version = protocol_unknown_version; |
| 140 | |
| 141 | /* |
| 142 | * Peek the first line of the server's response to |
| 143 | * determine the protocol version the server is speaking. |
| 144 | */ |
| 145 | switch (packet_reader_peek(reader)) { |
| 146 | case PACKET_READ_EOF: |
| 147 | die_initial_contact(0); |
| 148 | case PACKET_READ_FLUSH: |
| 149 | case PACKET_READ_DELIM: |
Denton Liu | 0181b60 | 2020-05-19 06:53:59 -0400 | [diff] [blame] | 150 | case PACKET_READ_RESPONSE_END: |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 151 | version = protocol_v0; |
| 152 | break; |
| 153 | case PACKET_READ_NORMAL: |
| 154 | version = determine_protocol_version_client(reader->line); |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | switch (version) { |
Brandon Williams | 8f6982b | 2018-03-14 11:31:47 -0700 | [diff] [blame] | 159 | case protocol_v2: |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 160 | process_capabilities_v2(reader); |
Brandon Williams | 8f6982b | 2018-03-14 11:31:47 -0700 | [diff] [blame] | 161 | break; |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 162 | case protocol_v1: |
| 163 | /* Read the peeked version line */ |
| 164 | packet_reader_read(reader); |
| 165 | break; |
| 166 | case protocol_v0: |
| 167 | break; |
| 168 | case protocol_unknown_version: |
| 169 | BUG("unknown protocol version"); |
| 170 | } |
| 171 | |
Josh Steadmon | 626beeb | 2021-08-10 10:20:39 -0700 | [diff] [blame] | 172 | trace2_data_intmax("transfer", NULL, "negotiated-version", version); |
| 173 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 174 | return version; |
| 175 | } |
| 176 | |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 177 | static void parse_one_symref_info(struct string_list *symref, const char *val, int len) |
| 178 | { |
| 179 | char *sym, *target; |
| 180 | struct string_list_item *item; |
| 181 | |
| 182 | if (!len) |
| 183 | return; /* just "symref" */ |
| 184 | /* e.g. "symref=HEAD:refs/heads/master" */ |
René Scharfe | 5c0b13f | 2014-07-19 17:35:34 +0200 | [diff] [blame] | 185 | sym = xmemdupz(val, len); |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 186 | target = strchr(sym, ':'); |
| 187 | if (!target) |
| 188 | /* just "symref=something" */ |
| 189 | goto reject; |
| 190 | *(target++) = '\0'; |
| 191 | if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) || |
| 192 | check_refname_format(target, REFNAME_ALLOW_ONELEVEL)) |
| 193 | /* "symref=bogus:pair */ |
| 194 | goto reject; |
Jeff King | ef4fe56 | 2017-05-25 15:33:05 -0400 | [diff] [blame] | 195 | item = string_list_append_nodup(symref, sym); |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 196 | item->util = target; |
| 197 | return; |
| 198 | reject: |
| 199 | free(sym); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | static void annotate_refs_with_symref_info(struct ref *ref) |
| 204 | { |
| 205 | struct string_list symref = STRING_LIST_INIT_DUP; |
brian m. carlson | 2c6a403 | 2020-05-25 19:58:52 +0000 | [diff] [blame] | 206 | int offset = 0; |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 207 | |
brian m. carlson | 2c6a403 | 2020-05-25 19:58:52 +0000 | [diff] [blame] | 208 | while (1) { |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 209 | int len; |
| 210 | const char *val; |
| 211 | |
brian m. carlson | 2c6a403 | 2020-05-25 19:58:52 +0000 | [diff] [blame] | 212 | val = next_server_feature_value("symref", &len, &offset); |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 213 | if (!val) |
| 214 | break; |
| 215 | parse_one_symref_info(&symref, val, len); |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 216 | } |
Michael Haggerty | 3383e19 | 2014-11-25 09:02:35 +0100 | [diff] [blame] | 217 | string_list_sort(&symref); |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 218 | |
| 219 | for (; ref; ref = ref->next) { |
| 220 | struct string_list_item *item; |
| 221 | item = string_list_lookup(&symref, ref->name); |
| 222 | if (!item) |
| 223 | continue; |
| 224 | ref->symref = xstrdup((char *)item->util); |
| 225 | } |
| 226 | string_list_clear(&symref, 0); |
| 227 | } |
| 228 | |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 229 | static void process_capabilities(struct packet_reader *reader, int *linelen) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 230 | { |
brian m. carlson | 7c601dc | 2020-05-25 19:59:00 +0000 | [diff] [blame] | 231 | const char *feat_val; |
| 232 | int feat_len; |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 233 | const char *line = reader->line; |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 234 | int nul_location = strlen(line); |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 235 | if (nul_location == *linelen) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 236 | return; |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 237 | server_capabilities_v1 = xstrdup(line + nul_location + 1); |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 238 | *linelen = nul_location; |
brian m. carlson | 7c601dc | 2020-05-25 19:59:00 +0000 | [diff] [blame] | 239 | |
| 240 | feat_val = server_feature_value("object-format", &feat_len); |
| 241 | if (feat_val) { |
| 242 | char *hash_name = xstrndup(feat_val, feat_len); |
| 243 | int hash_algo = hash_algo_by_name(hash_name); |
| 244 | if (hash_algo != GIT_HASH_UNKNOWN) |
| 245 | reader->hash_algo = &hash_algos[hash_algo]; |
| 246 | free(hash_name); |
| 247 | } else { |
| 248 | reader->hash_algo = &hash_algos[GIT_HASH_SHA1]; |
| 249 | } |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 250 | } |
| 251 | |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 252 | static int process_dummy_ref(const struct packet_reader *reader) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 253 | { |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 254 | const char *line = reader->line; |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 255 | struct object_id oid; |
| 256 | const char *name; |
| 257 | |
brian m. carlson | 7c601dc | 2020-05-25 19:59:00 +0000 | [diff] [blame] | 258 | if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo)) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | if (*name != ' ') |
| 261 | return 0; |
| 262 | name++; |
| 263 | |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 264 | return oideq(null_oid(), &oid) && !strcmp(name, "capabilities^{}"); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 267 | static void check_no_capabilities(const char *line, int len) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 268 | { |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 269 | if (strlen(line) != len) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 270 | warning(_("ignoring capabilities after first line '%s'"), |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 271 | line + strlen(line)); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 272 | } |
| 273 | |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 274 | static int process_ref(const struct packet_reader *reader, int len, |
| 275 | struct ref ***list, unsigned int flags, |
| 276 | struct oid_array *extra_have) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 277 | { |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 278 | const char *line = reader->line; |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 279 | struct object_id old_oid; |
| 280 | const char *name; |
| 281 | |
brian m. carlson | 7c601dc | 2020-05-25 19:59:00 +0000 | [diff] [blame] | 282 | if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo)) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 283 | return 0; |
| 284 | if (*name != ' ') |
| 285 | return 0; |
| 286 | name++; |
| 287 | |
| 288 | if (extra_have && !strcmp(name, ".have")) { |
| 289 | oid_array_append(extra_have, &old_oid); |
| 290 | } else if (!strcmp(name, "capabilities^{}")) { |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 291 | die(_("protocol error: unexpected capabilities^{}")); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 292 | } else if (check_ref(name, flags)) { |
| 293 | struct ref *ref = alloc_ref(name); |
| 294 | oidcpy(&ref->old_oid, &old_oid); |
| 295 | **list = ref; |
| 296 | *list = &ref->next; |
| 297 | } |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 298 | check_no_capabilities(line, len); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 299 | return 1; |
| 300 | } |
| 301 | |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 302 | static int process_shallow(const struct packet_reader *reader, int len, |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 303 | struct oid_array *shallow_points) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 304 | { |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 305 | const char *line = reader->line; |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 306 | const char *arg; |
| 307 | struct object_id old_oid; |
| 308 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 309 | if (!skip_prefix(line, "shallow ", &arg)) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 310 | return 0; |
| 311 | |
brian m. carlson | 7c601dc | 2020-05-25 19:59:00 +0000 | [diff] [blame] | 312 | if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo)) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 313 | die(_("protocol error: expected shallow sha-1, got '%s'"), arg); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 314 | if (!shallow_points) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 315 | die(_("repository on the other end cannot be shallow")); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 316 | oid_array_append(shallow_points, &old_oid); |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 317 | check_no_capabilities(line, len); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 318 | return 1; |
| 319 | } |
| 320 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 321 | enum get_remote_heads_state { |
| 322 | EXPECTING_FIRST_REF = 0, |
| 323 | EXPECTING_REF, |
| 324 | EXPECTING_SHALLOW, |
| 325 | EXPECTING_DONE, |
| 326 | }; |
| 327 | |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 328 | /* |
Linus Torvalds | d1c133f | 2005-07-16 13:55:50 -0700 | [diff] [blame] | 329 | * Read all the refs from the other end |
| 330 | */ |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 331 | struct ref **get_remote_heads(struct packet_reader *reader, |
Jeff King | 85edf4f | 2013-02-20 15:06:45 -0500 | [diff] [blame] | 332 | struct ref **list, unsigned int flags, |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 333 | struct oid_array *extra_have, |
| 334 | struct oid_array *shallow_points) |
Linus Torvalds | d1c133f | 2005-07-16 13:55:50 -0700 | [diff] [blame] | 335 | { |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 336 | struct ref **orig_list = list; |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 337 | int len = 0; |
| 338 | enum get_remote_heads_state state = EXPECTING_FIRST_REF; |
Jonathan Nieder | 55e4f93 | 2016-09-09 10:36:29 -0700 | [diff] [blame] | 339 | |
Linus Torvalds | d1c133f | 2005-07-16 13:55:50 -0700 | [diff] [blame] | 340 | *list = NULL; |
Linus Torvalds | d1c133f | 2005-07-16 13:55:50 -0700 | [diff] [blame] | 341 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 342 | while (state != EXPECTING_DONE) { |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 343 | switch (packet_reader_read(reader)) { |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 344 | case PACKET_READ_EOF: |
| 345 | die_initial_contact(1); |
| 346 | case PACKET_READ_NORMAL: |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 347 | len = reader->pktlen; |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 348 | break; |
| 349 | case PACKET_READ_FLUSH: |
| 350 | state = EXPECTING_DONE; |
| 351 | break; |
| 352 | case PACKET_READ_DELIM: |
Denton Liu | 0181b60 | 2020-05-19 06:53:59 -0400 | [diff] [blame] | 353 | case PACKET_READ_RESPONSE_END: |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 354 | die(_("invalid packet")); |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 357 | switch (state) { |
| 358 | case EXPECTING_FIRST_REF: |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 359 | process_capabilities(reader, &len); |
| 360 | if (process_dummy_ref(reader)) { |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 361 | state = EXPECTING_SHALLOW; |
| 362 | break; |
| 363 | } |
| 364 | state = EXPECTING_REF; |
| 365 | /* fallthrough */ |
| 366 | case EXPECTING_REF: |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 367 | if (process_ref(reader, len, &list, flags, extra_have)) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 368 | break; |
| 369 | state = EXPECTING_SHALLOW; |
| 370 | /* fallthrough */ |
| 371 | case EXPECTING_SHALLOW: |
brian m. carlson | 92315e5 | 2020-05-25 19:58:49 +0000 | [diff] [blame] | 372 | if (process_shallow(reader, len, shallow_points)) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 373 | break; |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 374 | die(_("protocol error: unexpected '%s'"), reader->line); |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 375 | case EXPECTING_DONE: |
| 376 | break; |
Nguyễn Thái Ngọc Duy | b06dcd7 | 2013-12-05 20:02:33 +0700 | [diff] [blame] | 377 | } |
Linus Torvalds | d1c133f | 2005-07-16 13:55:50 -0700 | [diff] [blame] | 378 | } |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 379 | |
| 380 | annotate_refs_with_symref_info(*orig_list); |
| 381 | |
Linus Torvalds | d1c133f | 2005-07-16 13:55:50 -0700 | [diff] [blame] | 382 | return list; |
| 383 | } |
| 384 | |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 385 | /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */ |
Jonathan Tan | 4f37d45 | 2021-02-05 12:48:49 -0800 | [diff] [blame] | 386 | static int process_ref_v2(struct packet_reader *reader, struct ref ***list, |
Ævar Arnfjörð Bjarmason | f36d4f8 | 2022-02-05 01:08:14 +0100 | [diff] [blame] | 387 | const char **unborn_head_target) |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 388 | { |
| 389 | int ret = 1; |
| 390 | int i = 0; |
| 391 | struct object_id old_oid; |
| 392 | struct ref *ref; |
| 393 | struct string_list line_sections = STRING_LIST_INIT_DUP; |
| 394 | const char *end; |
brian m. carlson | 67e9a70 | 2020-05-25 19:59:15 +0000 | [diff] [blame] | 395 | const char *line = reader->line; |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | * Ref lines have a number of fields which are space deliminated. The |
| 399 | * first field is the OID of the ref. The second field is the ref |
| 400 | * name. Subsequent fields (symref-target and peeled) are optional and |
| 401 | * don't have a particular order. |
| 402 | */ |
| 403 | if (string_list_split(&line_sections, line, ' ', -1) < 2) { |
| 404 | ret = 0; |
| 405 | goto out; |
| 406 | } |
| 407 | |
Jonathan Tan | 4f37d45 | 2021-02-05 12:48:49 -0800 | [diff] [blame] | 408 | if (!strcmp("unborn", line_sections.items[i].string)) { |
| 409 | i++; |
| 410 | if (unborn_head_target && |
| 411 | !strcmp("HEAD", line_sections.items[i++].string)) { |
| 412 | /* |
| 413 | * Look for the symref target (if any). If found, |
| 414 | * return it to the caller. |
| 415 | */ |
| 416 | for (; i < line_sections.nr; i++) { |
| 417 | const char *arg = line_sections.items[i].string; |
| 418 | |
| 419 | if (skip_prefix(arg, "symref-target:", &arg)) { |
| 420 | *unborn_head_target = xstrdup(arg); |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | goto out; |
| 426 | } |
brian m. carlson | ab67235 | 2020-05-25 19:59:16 +0000 | [diff] [blame] | 427 | if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) || |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 428 | *end) { |
| 429 | ret = 0; |
| 430 | goto out; |
| 431 | } |
| 432 | |
| 433 | ref = alloc_ref(line_sections.items[i++].string); |
| 434 | |
brian m. carlson | ab67235 | 2020-05-25 19:59:16 +0000 | [diff] [blame] | 435 | memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz); |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 436 | **list = ref; |
| 437 | *list = &ref->next; |
| 438 | |
| 439 | for (; i < line_sections.nr; i++) { |
| 440 | const char *arg = line_sections.items[i].string; |
| 441 | if (skip_prefix(arg, "symref-target:", &arg)) |
| 442 | ref->symref = xstrdup(arg); |
| 443 | |
| 444 | if (skip_prefix(arg, "peeled:", &arg)) { |
| 445 | struct object_id peeled_oid; |
| 446 | char *peeled_name; |
| 447 | struct ref *peeled; |
brian m. carlson | ab67235 | 2020-05-25 19:59:16 +0000 | [diff] [blame] | 448 | if (parse_oid_hex_algop(arg, &peeled_oid, &end, |
| 449 | reader->hash_algo) || *end) { |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 450 | ret = 0; |
| 451 | goto out; |
| 452 | } |
| 453 | |
| 454 | peeled_name = xstrfmt("%s^{}", ref->name); |
| 455 | peeled = alloc_ref(peeled_name); |
| 456 | |
brian m. carlson | ab67235 | 2020-05-25 19:59:16 +0000 | [diff] [blame] | 457 | memcpy(peeled->old_oid.hash, peeled_oid.hash, |
| 458 | reader->hash_algo->rawsz); |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 459 | **list = peeled; |
| 460 | *list = &peeled->next; |
| 461 | |
| 462 | free(peeled_name); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | out: |
| 467 | string_list_clear(&line_sections, 0); |
| 468 | return ret; |
| 469 | } |
| 470 | |
Denton Liu | b0df0c1 | 2020-05-19 06:54:00 -0400 | [diff] [blame] | 471 | void check_stateless_delimiter(int stateless_rpc, |
| 472 | struct packet_reader *reader, |
| 473 | const char *error) |
| 474 | { |
| 475 | if (!stateless_rpc) |
| 476 | return; /* not in stateless mode, no delimiter expected */ |
| 477 | if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END) |
| 478 | die("%s", error); |
| 479 | } |
| 480 | |
Ævar Arnfjörð Bjarmason | 86f4e31 | 2022-05-16 20:10:58 +0000 | [diff] [blame] | 481 | static void send_capabilities(int fd_out, struct packet_reader *reader) |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 482 | { |
brian m. carlson | ab67235 | 2020-05-25 19:59:16 +0000 | [diff] [blame] | 483 | const char *hash_name; |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 484 | |
Jeff King | a31cfe3 | 2022-12-13 05:52:58 -0500 | [diff] [blame] | 485 | if (server_supports_v2("agent")) |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 486 | packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized()); |
| 487 | |
brian m. carlson | ab67235 | 2020-05-25 19:59:16 +0000 | [diff] [blame] | 488 | if (server_feature_v2("object-format", &hash_name)) { |
| 489 | int hash_algo = hash_algo_by_name(hash_name); |
| 490 | if (hash_algo == GIT_HASH_UNKNOWN) |
| 491 | die(_("unknown object format '%s' specified by server"), hash_name); |
| 492 | reader->hash_algo = &hash_algos[hash_algo]; |
| 493 | packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name); |
brian m. carlson | 9de0dd3 | 2020-05-25 19:59:17 +0000 | [diff] [blame] | 494 | } else { |
| 495 | reader->hash_algo = &hash_algos[GIT_HASH_SHA1]; |
brian m. carlson | ab67235 | 2020-05-25 19:59:16 +0000 | [diff] [blame] | 496 | } |
Ævar Arnfjörð Bjarmason | 86f4e31 | 2022-05-16 20:10:58 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Ævar Arnfjörð Bjarmason | 0cfde74 | 2022-12-22 15:14:09 +0000 | [diff] [blame] | 499 | int get_remote_bundle_uri(int fd_out, struct packet_reader *reader, |
| 500 | struct bundle_list *bundles, int stateless_rpc) |
| 501 | { |
| 502 | int line_nr = 1; |
| 503 | |
| 504 | /* Assert bundle-uri support */ |
Junio C Hamano | 0903d8b | 2023-01-02 21:37:18 +0900 | [diff] [blame] | 505 | ensure_server_supports_v2("bundle-uri"); |
Ævar Arnfjörð Bjarmason | 0cfde74 | 2022-12-22 15:14:09 +0000 | [diff] [blame] | 506 | |
| 507 | /* (Re-)send capabilities */ |
| 508 | send_capabilities(fd_out, reader); |
| 509 | |
| 510 | /* Send command */ |
| 511 | packet_write_fmt(fd_out, "command=bundle-uri\n"); |
| 512 | packet_delim(fd_out); |
| 513 | |
| 514 | packet_flush(fd_out); |
| 515 | |
| 516 | /* Process response from server */ |
| 517 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 518 | const char *line = reader->line; |
| 519 | line_nr++; |
| 520 | |
| 521 | if (!bundle_uri_parse_line(bundles, line)) |
| 522 | continue; |
| 523 | |
| 524 | return error(_("error on bundle-uri response line %d: %s"), |
| 525 | line_nr, line); |
| 526 | } |
| 527 | |
| 528 | if (reader->status != PACKET_READ_FLUSH) |
| 529 | return error(_("expected flush after bundle-uri listing")); |
| 530 | |
| 531 | /* |
| 532 | * Might die(), but obscure enough that that's OK, e.g. in |
| 533 | * serve.c we'll call BUG() on its equivalent (the |
| 534 | * PACKET_READ_RESPONSE_END check). |
| 535 | */ |
| 536 | check_stateless_delimiter(stateless_rpc, reader, |
| 537 | _("expected response end packet after ref listing")); |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
Ævar Arnfjörð Bjarmason | 86f4e31 | 2022-05-16 20:10:58 +0000 | [diff] [blame] | 542 | struct ref **get_remote_refs(int fd_out, struct packet_reader *reader, |
| 543 | struct ref **list, int for_push, |
| 544 | struct transport_ls_refs_options *transport_options, |
| 545 | const struct string_list *server_options, |
| 546 | int stateless_rpc) |
| 547 | { |
| 548 | int i; |
| 549 | struct strvec *ref_prefixes = transport_options ? |
| 550 | &transport_options->ref_prefixes : NULL; |
| 551 | const char **unborn_head_target = transport_options ? |
| 552 | &transport_options->unborn_head_target : NULL; |
| 553 | *list = NULL; |
| 554 | |
Jeff King | a31cfe3 | 2022-12-13 05:52:58 -0500 | [diff] [blame] | 555 | ensure_server_supports_v2("ls-refs"); |
| 556 | packet_write_fmt(fd_out, "command=ls-refs\n"); |
Ævar Arnfjörð Bjarmason | 86f4e31 | 2022-05-16 20:10:58 +0000 | [diff] [blame] | 557 | |
| 558 | /* Send capabilities */ |
| 559 | send_capabilities(fd_out, reader); |
brian m. carlson | ab67235 | 2020-05-25 19:59:16 +0000 | [diff] [blame] | 560 | |
Jeff King | a31cfe3 | 2022-12-13 05:52:58 -0500 | [diff] [blame] | 561 | if (server_options && server_options->nr) { |
| 562 | ensure_server_supports_v2("server-option"); |
Brandon Williams | ff47322 | 2018-04-23 15:46:23 -0700 | [diff] [blame] | 563 | for (i = 0; i < server_options->nr; i++) |
| 564 | packet_write_fmt(fd_out, "server-option=%s", |
| 565 | server_options->items[i].string); |
Jeff King | a31cfe3 | 2022-12-13 05:52:58 -0500 | [diff] [blame] | 566 | } |
Brandon Williams | ff47322 | 2018-04-23 15:46:23 -0700 | [diff] [blame] | 567 | |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 568 | packet_delim(fd_out); |
| 569 | /* When pushing we don't want to request the peeled tags */ |
| 570 | if (!for_push) |
| 571 | packet_write_fmt(fd_out, "peel\n"); |
| 572 | packet_write_fmt(fd_out, "symrefs\n"); |
Jonathan Tan | 4f37d45 | 2021-02-05 12:48:49 -0800 | [diff] [blame] | 573 | if (server_supports_feature("ls-refs", "unborn", 0)) |
| 574 | packet_write_fmt(fd_out, "unborn\n"); |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 575 | for (i = 0; ref_prefixes && i < ref_prefixes->nr; i++) { |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 576 | packet_write_fmt(fd_out, "ref-prefix %s\n", |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 577 | ref_prefixes->v[i]); |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 578 | } |
| 579 | packet_flush(fd_out); |
| 580 | |
| 581 | /* Process response from server */ |
| 582 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
Jonathan Tan | 4f37d45 | 2021-02-05 12:48:49 -0800 | [diff] [blame] | 583 | if (!process_ref_v2(reader, &list, unborn_head_target)) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 584 | die(_("invalid ls-refs response: %s"), reader->line); |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | if (reader->status != PACKET_READ_FLUSH) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 588 | die(_("expected flush after ref listing")); |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 589 | |
Denton Liu | b0df0c1 | 2020-05-19 06:54:00 -0400 | [diff] [blame] | 590 | check_stateless_delimiter(stateless_rpc, reader, |
| 591 | _("expected response end packet after ref listing")); |
| 592 | |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 593 | return list; |
| 594 | } |
| 595 | |
brian m. carlson | 84eca27 | 2020-05-25 19:58:58 +0000 | [diff] [blame] | 596 | const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp, int *offset) |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 597 | { |
| 598 | int len; |
| 599 | |
| 600 | if (!feature_list) |
| 601 | return NULL; |
| 602 | |
| 603 | len = strlen(feature); |
brian m. carlson | 2c6a403 | 2020-05-25 19:58:52 +0000 | [diff] [blame] | 604 | if (offset) |
| 605 | feature_list += *offset; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 606 | while (*feature_list) { |
| 607 | const char *found = strstr(feature_list, feature); |
| 608 | if (!found) |
| 609 | return NULL; |
Jeff King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 610 | if (feature_list == found || isspace(found[-1])) { |
| 611 | const char *value = found + len; |
| 612 | /* feature with no value (e.g., "thin-pack") */ |
| 613 | if (!*value || isspace(*value)) { |
| 614 | if (lenp) |
| 615 | *lenp = 0; |
Andrzej Hunt | 44d2aec | 2021-09-26 15:58:33 +0000 | [diff] [blame] | 616 | if (offset) |
| 617 | *offset = found + len - feature_list; |
Jeff King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 618 | return value; |
| 619 | } |
| 620 | /* feature with a value (e.g., "agent=git/1.2.3") */ |
| 621 | else if (*value == '=') { |
brian m. carlson | 2c6a403 | 2020-05-25 19:58:52 +0000 | [diff] [blame] | 622 | int end; |
| 623 | |
Jeff King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 624 | value++; |
brian m. carlson | 2c6a403 | 2020-05-25 19:58:52 +0000 | [diff] [blame] | 625 | end = strcspn(value, " \t\n"); |
Jeff King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 626 | if (lenp) |
brian m. carlson | 2c6a403 | 2020-05-25 19:58:52 +0000 | [diff] [blame] | 627 | *lenp = end; |
| 628 | if (offset) |
| 629 | *offset = value + end - feature_list; |
Jeff King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 630 | return value; |
| 631 | } |
| 632 | /* |
| 633 | * otherwise we matched a substring of another feature; |
| 634 | * keep looking |
| 635 | */ |
| 636 | } |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 637 | feature_list = found + 1; |
| 638 | } |
| 639 | return NULL; |
Johannes Schindelin | 211b5f9 | 2005-10-28 04:48:54 +0200 | [diff] [blame] | 640 | } |
| 641 | |
brian m. carlson | 122037c | 2020-05-25 19:58:56 +0000 | [diff] [blame] | 642 | int server_supports_hash(const char *desired, int *feature_supported) |
| 643 | { |
| 644 | int offset = 0; |
| 645 | int len; |
| 646 | const char *hash; |
| 647 | |
| 648 | hash = next_server_feature_value("object-format", &len, &offset); |
| 649 | if (feature_supported) |
| 650 | *feature_supported = !!hash; |
| 651 | if (!hash) { |
| 652 | hash = hash_algos[GIT_HASH_SHA1].name; |
| 653 | len = strlen(hash); |
| 654 | } |
| 655 | while (hash) { |
| 656 | if (!xstrncmpz(desired, hash, len)) |
| 657 | return 1; |
| 658 | |
| 659 | hash = next_server_feature_value("object-format", &len, &offset); |
| 660 | } |
| 661 | return 0; |
| 662 | } |
| 663 | |
Jeff King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 664 | int parse_feature_request(const char *feature_list, const char *feature) |
| 665 | { |
brian m. carlson | 2c6a403 | 2020-05-25 19:58:52 +0000 | [diff] [blame] | 666 | return !!parse_feature_value(feature_list, feature, NULL, NULL); |
| 667 | } |
| 668 | |
| 669 | static const char *next_server_feature_value(const char *feature, int *len, int *offset) |
| 670 | { |
| 671 | return parse_feature_value(server_capabilities_v1, feature, len, offset); |
Jeff King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | const char *server_feature_value(const char *feature, int *len) |
| 675 | { |
brian m. carlson | 2c6a403 | 2020-05-25 19:58:52 +0000 | [diff] [blame] | 676 | return parse_feature_value(server_capabilities_v1, feature, len, NULL); |
Jeff King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | int server_supports(const char *feature) |
| 680 | { |
| 681 | return !!server_feature_value(feature, NULL); |
| 682 | } |
| 683 | |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 684 | enum protocol { |
| 685 | PROTO_LOCAL = 1, |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 686 | PROTO_FILE, |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 687 | PROTO_SSH, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 688 | PROTO_GIT |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 689 | }; |
| 690 | |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 691 | int url_is_local_not_ssh(const char *url) |
| 692 | { |
| 693 | const char *colon = strchr(url, ':'); |
| 694 | const char *slash = strchr(url, '/'); |
| 695 | return !colon || (slash && slash < colon) || |
Johannes Schindelin | f82a97e | 2019-09-06 00:09:10 +0200 | [diff] [blame] | 696 | (has_dos_drive_prefix(url) && is_valid_path(url)); |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 699 | static const char *prot_name(enum protocol protocol) |
| 700 | { |
| 701 | switch (protocol) { |
| 702 | case PROTO_LOCAL: |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 703 | case PROTO_FILE: |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 704 | return "file"; |
| 705 | case PROTO_SSH: |
| 706 | return "ssh"; |
| 707 | case PROTO_GIT: |
| 708 | return "git"; |
| 709 | default: |
Tobias Klauser | 83e6bda | 2015-09-24 14:44:49 +0200 | [diff] [blame] | 710 | return "unknown protocol"; |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 714 | static enum protocol get_protocol(const char *name) |
| 715 | { |
| 716 | if (!strcmp(name, "ssh")) |
| 717 | return PROTO_SSH; |
| 718 | if (!strcmp(name, "git")) |
| 719 | return PROTO_GIT; |
Carlos Martín Nieto | 07c7782 | 2016-02-15 15:29:06 +0100 | [diff] [blame] | 720 | if (!strcmp(name, "git+ssh")) /* deprecated - do not use */ |
Linus Torvalds | c05186c | 2005-10-14 17:14:56 -0700 | [diff] [blame] | 721 | return PROTO_SSH; |
Carlos Martín Nieto | 07c7782 | 2016-02-15 15:29:06 +0100 | [diff] [blame] | 722 | if (!strcmp(name, "ssh+git")) /* deprecated - do not use */ |
Linus Torvalds | c05186c | 2005-10-14 17:14:56 -0700 | [diff] [blame] | 723 | return PROTO_SSH; |
Linus Torvalds | 72a4f4b | 2007-08-01 10:03:37 -0700 | [diff] [blame] | 724 | if (!strcmp(name, "file")) |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 725 | return PROTO_FILE; |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 726 | die(_("protocol '%s' is not supported"), name); |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 729 | static char *host_end(char **hoststart, int removebrackets) |
| 730 | { |
| 731 | char *host = *hoststart; |
| 732 | char *end; |
| 733 | char *start = strstr(host, "@["); |
| 734 | if (start) |
| 735 | start++; /* Jump over '@' */ |
| 736 | else |
| 737 | start = host; |
| 738 | if (start[0] == '[') { |
| 739 | end = strchr(start + 1, ']'); |
| 740 | if (end) { |
| 741 | if (removebrackets) { |
| 742 | *end = 0; |
| 743 | memmove(start, start + 1, end - start); |
| 744 | end++; |
| 745 | } |
| 746 | } else |
| 747 | end = host; |
| 748 | } else |
| 749 | end = host; |
| 750 | return end; |
| 751 | } |
| 752 | |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 753 | #define STR_(s) # s |
| 754 | #define STR(s) STR_(s) |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 755 | |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 756 | static void get_host_and_port(char **host, const char **port) |
| 757 | { |
| 758 | char *colon, *end; |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 759 | end = host_end(host, 1); |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 760 | colon = strchr(end, ':'); |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 761 | if (colon) { |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 762 | long portnr = strtol(colon + 1, &end, 10); |
| 763 | if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) { |
| 764 | *colon = 0; |
| 765 | *port = colon + 1; |
Torsten Bögershausen | 6b6c5f7 | 2015-04-07 22:03:25 +0200 | [diff] [blame] | 766 | } else if (!colon[1]) { |
| 767 | *colon = 0; |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 768 | } |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Eric Wong | e47a858 | 2011-12-06 04:39:36 +0000 | [diff] [blame] | 772 | static void enable_keepalive(int sockfd) |
| 773 | { |
| 774 | int ka = 1; |
| 775 | |
| 776 | if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 777 | error_errno(_("unable to set SO_KEEPALIVE on socket")); |
Eric Wong | e47a858 | 2011-12-06 04:39:36 +0000 | [diff] [blame] | 778 | } |
| 779 | |
hpa | 49744d6 | 2005-09-28 16:52:21 -0700 | [diff] [blame] | 780 | #ifndef NO_IPV6 |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 781 | |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 782 | static const char *ai_name(const struct addrinfo *ai) |
| 783 | { |
Benjamin Kramer | 785a985 | 2009-04-24 14:16:41 +0200 | [diff] [blame] | 784 | static char addr[NI_MAXHOST]; |
| 785 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0, |
| 786 | NI_NUMERICHOST) != 0) |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 787 | xsnprintf(addr, sizeof(addr), "(unknown)"); |
Benjamin Kramer | 785a985 | 2009-04-24 14:16:41 +0200 | [diff] [blame] | 788 | |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 789 | return addr; |
| 790 | } |
| 791 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 792 | /* |
| 793 | * Returns a connected socket() fd, or else die()s. |
| 794 | */ |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 795 | static int git_tcp_connect_sock(char *host, int flags) |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 796 | { |
Dave Zarzycki | 63a995b | 2011-07-12 09:28:34 -0700 | [diff] [blame] | 797 | struct strbuf error_message = STRBUF_INIT; |
| 798 | int sockfd = -1; |
Timo Hirvonen | 554fe20 | 2006-06-28 12:04:39 +0300 | [diff] [blame] | 799 | const char *port = STR(DEFAULT_GIT_PORT); |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 800 | struct addrinfo hints, *ai0, *ai; |
| 801 | int gai; |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 802 | int cnt = 0; |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 803 | |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 804 | get_host_and_port(&host, &port); |
| 805 | if (!*port) |
| 806 | port = "<none>"; |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 807 | |
| 808 | memset(&hints, 0, sizeof(hints)); |
Eric Wong | c915f11 | 2016-02-03 04:09:14 +0000 | [diff] [blame] | 809 | if (flags & CONNECT_IPV4) |
| 810 | hints.ai_family = AF_INET; |
| 811 | else if (flags & CONNECT_IPV6) |
| 812 | hints.ai_family = AF_INET6; |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 813 | hints.ai_socktype = SOCK_STREAM; |
| 814 | hints.ai_protocol = IPPROTO_TCP; |
| 815 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 816 | if (flags & CONNECT_VERBOSE) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 817 | fprintf(stderr, _("Looking up %s ... "), host); |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 818 | |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 819 | gai = getaddrinfo(host, port, &hints, &ai); |
| 820 | if (gai) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 821 | die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai)); |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 822 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 823 | if (flags & CONNECT_VERBOSE) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 824 | /* TRANSLATORS: this is the end of "Looking up %s ... " */ |
| 825 | fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port); |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 826 | |
Erik Faye-Lund | e08afec | 2011-08-01 13:16:09 +0200 | [diff] [blame] | 827 | for (ai0 = ai; ai; ai = ai->ai_next, cnt++) { |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 828 | sockfd = socket(ai->ai_family, |
| 829 | ai->ai_socktype, ai->ai_protocol); |
Dave Zarzycki | 63a995b | 2011-07-12 09:28:34 -0700 | [diff] [blame] | 830 | if ((sockfd < 0) || |
| 831 | (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) { |
| 832 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", |
| 833 | host, cnt, ai_name(ai), strerror(errno)); |
| 834 | if (0 <= sockfd) |
| 835 | close(sockfd); |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 836 | sockfd = -1; |
| 837 | continue; |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 838 | } |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 839 | if (flags & CONNECT_VERBOSE) |
| 840 | fprintf(stderr, "%s ", ai_name(ai)); |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 841 | break; |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 842 | } |
| 843 | |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 844 | freeaddrinfo(ai0); |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 845 | |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 846 | if (sockfd < 0) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 847 | die(_("unable to connect to %s:\n%s"), host, error_message.buf); |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 848 | |
Eric Wong | e47a858 | 2011-12-06 04:39:36 +0000 | [diff] [blame] | 849 | enable_keepalive(sockfd); |
| 850 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 851 | if (flags & CONNECT_VERBOSE) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 852 | /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */ |
| 853 | fprintf_ln(stderr, _("done.")); |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 854 | |
Dave Zarzycki | 63a995b | 2011-07-12 09:28:34 -0700 | [diff] [blame] | 855 | strbuf_release(&error_message); |
| 856 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 857 | return sockfd; |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 858 | } |
| 859 | |
hpa | 49744d6 | 2005-09-28 16:52:21 -0700 | [diff] [blame] | 860 | #else /* NO_IPV6 */ |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 861 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 862 | /* |
| 863 | * Returns a connected socket() fd, or else die()s. |
| 864 | */ |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 865 | static int git_tcp_connect_sock(char *host, int flags) |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 866 | { |
Erik Faye-Lund | 7203a2d | 2011-08-01 13:16:10 +0200 | [diff] [blame] | 867 | struct strbuf error_message = STRBUF_INIT; |
| 868 | int sockfd = -1; |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 869 | const char *port = STR(DEFAULT_GIT_PORT); |
| 870 | char *ep; |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 871 | struct hostent *he; |
| 872 | struct sockaddr_in sa; |
| 873 | char **ap; |
| 874 | unsigned int nport; |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 875 | int cnt; |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 876 | |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 877 | get_host_and_port(&host, &port); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 878 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 879 | if (flags & CONNECT_VERBOSE) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 880 | fprintf(stderr, _("Looking up %s ... "), host); |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 881 | |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 882 | he = gethostbyname(host); |
| 883 | if (!he) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 884 | die(_("unable to look up %s (%s)"), host, hstrerror(h_errno)); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 885 | nport = strtoul(port, &ep, 10); |
| 886 | if ( ep == port || *ep ) { |
| 887 | /* Not numeric */ |
| 888 | struct servent *se = getservbyname(port,"tcp"); |
| 889 | if ( !se ) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 890 | die(_("unknown port %s"), port); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 891 | nport = se->s_port; |
| 892 | } |
| 893 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 894 | if (flags & CONNECT_VERBOSE) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 895 | /* TRANSLATORS: this is the end of "Looking up %s ... " */ |
| 896 | fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port); |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 897 | |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 898 | for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 899 | memset(&sa, 0, sizeof sa); |
| 900 | sa.sin_family = he->h_addrtype; |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 901 | sa.sin_port = htons(nport); |
Paul Serice | c616421 | 2005-11-22 07:54:23 -0600 | [diff] [blame] | 902 | memcpy(&sa.sin_addr, *ap, he->h_length); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 903 | |
Erik Faye-Lund | 7203a2d | 2011-08-01 13:16:10 +0200 | [diff] [blame] | 904 | sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); |
| 905 | if ((sockfd < 0) || |
| 906 | connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { |
| 907 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 908 | host, |
| 909 | cnt, |
| 910 | inet_ntoa(*(struct in_addr *)&sa.sin_addr), |
Erik Faye-Lund | 7203a2d | 2011-08-01 13:16:10 +0200 | [diff] [blame] | 911 | strerror(errno)); |
| 912 | if (0 <= sockfd) |
| 913 | close(sockfd); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 914 | sockfd = -1; |
| 915 | continue; |
| 916 | } |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 917 | if (flags & CONNECT_VERBOSE) |
| 918 | fprintf(stderr, "%s ", |
| 919 | inet_ntoa(*(struct in_addr *)&sa.sin_addr)); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 920 | break; |
| 921 | } |
| 922 | |
| 923 | if (sockfd < 0) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 924 | die(_("unable to connect to %s:\n%s"), host, error_message.buf); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 925 | |
Eric Wong | e47a858 | 2011-12-06 04:39:36 +0000 | [diff] [blame] | 926 | enable_keepalive(sockfd); |
| 927 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 928 | if (flags & CONNECT_VERBOSE) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 929 | /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */ |
| 930 | fprintf_ln(stderr, _("done.")); |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 931 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 932 | return sockfd; |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 933 | } |
| 934 | |
hpa | 49744d6 | 2005-09-28 16:52:21 -0700 | [diff] [blame] | 935 | #endif /* NO_IPV6 */ |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 936 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 937 | |
Jonathan Nieder | 8e34978 | 2017-11-20 13:22:54 -0800 | [diff] [blame] | 938 | /* |
| 939 | * Dummy child_process returned by git_connect() if the transport protocol |
| 940 | * does not need fork(2). |
| 941 | */ |
| 942 | static struct child_process no_fork = CHILD_PROCESS_INIT; |
| 943 | |
| 944 | int git_connection_is_socket(struct child_process *conn) |
| 945 | { |
| 946 | return conn == &no_fork; |
| 947 | } |
| 948 | |
| 949 | static struct child_process *git_tcp_connect(int fd[2], char *host, int flags) |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 950 | { |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 951 | int sockfd = git_tcp_connect_sock(host, flags); |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 952 | |
| 953 | fd[0] = sockfd; |
Junio C Hamano | ec587fd | 2007-01-21 17:10:51 -0800 | [diff] [blame] | 954 | fd[1] = dup(sockfd); |
Jonathan Nieder | 8e34978 | 2017-11-20 13:22:54 -0800 | [diff] [blame] | 955 | |
| 956 | return &no_fork; |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 960 | static char *git_proxy_command; |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 961 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 962 | static int git_proxy_command_options(const char *var, const char *value, |
| 963 | void *cb) |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 964 | { |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 965 | if (!strcmp(var, "core.gitproxy")) { |
YOSHIFUJI Hideaki / 吉藤英明 | c3df856 | 2005-11-22 12:18:23 +0900 | [diff] [blame] | 966 | const char *for_pos; |
| 967 | int matchlen = -1; |
| 968 | int hostlen; |
Erik Faye-Lund | 15112c9 | 2009-03-11 02:38:12 +0000 | [diff] [blame] | 969 | const char *rhost_name = cb; |
| 970 | int rhost_len = strlen(rhost_name); |
YOSHIFUJI Hideaki / 吉藤英明 | c3df856 | 2005-11-22 12:18:23 +0900 | [diff] [blame] | 971 | |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 972 | if (git_proxy_command) |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 973 | return 0; |
Junio C Hamano | c64b9ad | 2008-02-11 10:52:15 -0800 | [diff] [blame] | 974 | if (!value) |
| 975 | return config_error_nonbool(var); |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 976 | /* [core] |
| 977 | * ;# matches www.kernel.org as well |
| 978 | * gitproxy = netcatter-1 for kernel.org |
| 979 | * gitproxy = netcatter-2 for sample.xz |
| 980 | * gitproxy = netcatter-default |
| 981 | */ |
YOSHIFUJI Hideaki / 吉藤英明 | c3df856 | 2005-11-22 12:18:23 +0900 | [diff] [blame] | 982 | for_pos = strstr(value, " for "); |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 983 | if (!for_pos) |
| 984 | /* matches everybody */ |
| 985 | matchlen = strlen(value); |
| 986 | else { |
| 987 | hostlen = strlen(for_pos + 5); |
| 988 | if (rhost_len < hostlen) |
| 989 | matchlen = -1; |
| 990 | else if (!strncmp(for_pos + 5, |
| 991 | rhost_name + rhost_len - hostlen, |
| 992 | hostlen) && |
| 993 | ((rhost_len == hostlen) || |
| 994 | rhost_name[rhost_len - hostlen -1] == '.')) |
| 995 | matchlen = for_pos - value; |
| 996 | else |
| 997 | matchlen = -1; |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 998 | } |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 999 | if (0 <= matchlen) { |
| 1000 | /* core.gitproxy = none for kernel.org */ |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 1001 | if (matchlen == 4 && |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 1002 | !memcmp(value, "none", 4)) |
| 1003 | matchlen = 0; |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 1004 | git_proxy_command = xmemdupz(value, matchlen); |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 1005 | } |
| 1006 | return 0; |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 1009 | return git_default_config(var, value, cb); |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 1012 | static int git_use_proxy(const char *host) |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 1013 | { |
| 1014 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); |
Erik Faye-Lund | 15112c9 | 2009-03-11 02:38:12 +0000 | [diff] [blame] | 1015 | git_config(git_proxy_command_options, (void*)host); |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 1016 | return (git_proxy_command && *git_proxy_command); |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Jeff King | 5cbf824 | 2011-05-16 02:46:07 -0400 | [diff] [blame] | 1019 | static struct child_process *git_proxy_connect(int fd[2], char *host) |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 1020 | { |
Timo Hirvonen | 554fe20 | 2006-06-28 12:04:39 +0300 | [diff] [blame] | 1021 | const char *port = STR(DEFAULT_GIT_PORT); |
Jeff King | 5cbf824 | 2011-05-16 02:46:07 -0400 | [diff] [blame] | 1022 | struct child_process *proxy; |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 1023 | |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 1024 | get_host_and_port(&host, &port); |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 1025 | |
Jeff King | 3be4cf0 | 2017-07-28 15:26:50 -0400 | [diff] [blame] | 1026 | if (looks_like_command_line_option(host)) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 1027 | die(_("strange hostname '%s' blocked"), host); |
Jeff King | 3be4cf0 | 2017-07-28 15:26:50 -0400 | [diff] [blame] | 1028 | if (looks_like_command_line_option(port)) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 1029 | die(_("strange port '%s' blocked"), port); |
Jeff King | 3be4cf0 | 2017-07-28 15:26:50 -0400 | [diff] [blame] | 1030 | |
René Scharfe | 483bbd4 | 2014-08-19 21:10:48 +0200 | [diff] [blame] | 1031 | proxy = xmalloc(sizeof(*proxy)); |
| 1032 | child_process_init(proxy); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1033 | strvec_push(&proxy->args, git_proxy_command); |
| 1034 | strvec_push(&proxy->args, host); |
| 1035 | strvec_push(&proxy->args, port); |
Jeff King | 5cbf824 | 2011-05-16 02:46:07 -0400 | [diff] [blame] | 1036 | proxy->in = -1; |
| 1037 | proxy->out = -1; |
| 1038 | if (start_command(proxy)) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 1039 | die(_("cannot start proxy %s"), git_proxy_command); |
Jeff King | 5cbf824 | 2011-05-16 02:46:07 -0400 | [diff] [blame] | 1040 | fd[0] = proxy->out; /* read from proxy stdout */ |
| 1041 | fd[1] = proxy->in; /* write to proxy stdin */ |
| 1042 | return proxy; |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 1045 | static char *get_port(char *host) |
Luben Tuikov | 2e77666 | 2007-09-01 02:36:31 -0700 | [diff] [blame] | 1046 | { |
| 1047 | char *end; |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 1048 | char *p = strchr(host, ':'); |
| 1049 | |
Luben Tuikov | 2e77666 | 2007-09-01 02:36:31 -0700 | [diff] [blame] | 1050 | if (p) { |
René Scharfe | 8f14825 | 2008-12-21 02:12:11 +0100 | [diff] [blame] | 1051 | long port = strtol(p + 1, &end, 10); |
| 1052 | if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) { |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 1053 | *p = '\0'; |
| 1054 | return p+1; |
Luben Tuikov | 2e77666 | 2007-09-01 02:36:31 -0700 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | return NULL; |
| 1059 | } |
| 1060 | |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1061 | /* |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 1062 | * Extract protocol and relevant parts from the specified connection URL. |
| 1063 | * The caller must free() the returned strings. |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1064 | */ |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 1065 | static enum protocol parse_connect_url(const char *url_orig, char **ret_host, |
Torsten Bögershausen | 83b0587 | 2013-11-28 20:49:54 +0100 | [diff] [blame] | 1066 | char **ret_path) |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1067 | { |
Jeff King | 9d2e942 | 2010-05-23 05:19:44 -0400 | [diff] [blame] | 1068 | char *url; |
Benjamin Kramer | 8e76bf3 | 2009-03-13 13:51:33 +0100 | [diff] [blame] | 1069 | char *host, *path; |
YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 19:23:42 +0900 | [diff] [blame] | 1070 | char *end; |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 1071 | int separator = '/'; |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 1072 | enum protocol protocol = PROTO_LOCAL; |
Junio C Hamano | f0b7367 | 2006-06-19 18:25:21 -0700 | [diff] [blame] | 1073 | |
Jeff King | 9d2e942 | 2010-05-23 05:19:44 -0400 | [diff] [blame] | 1074 | if (is_url(url_orig)) |
| 1075 | url = url_decode(url_orig); |
| 1076 | else |
| 1077 | url = xstrdup(url_orig); |
| 1078 | |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 1079 | host = strstr(url, "://"); |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 1080 | if (host) { |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 1081 | *host = '\0'; |
| 1082 | protocol = get_protocol(url); |
| 1083 | host += 3; |
YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 19:23:42 +0900 | [diff] [blame] | 1084 | } else { |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1085 | host = url; |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 1086 | if (!url_is_local_not_ssh(url)) { |
| 1087 | protocol = PROTO_SSH; |
| 1088 | separator = ':'; |
| 1089 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 19:23:42 +0900 | [diff] [blame] | 1090 | } |
| 1091 | |
Ilari Liusvaara | 9aa5053 | 2010-01-26 20:24:42 +0200 | [diff] [blame] | 1092 | /* |
Torsten Bögershausen | 83b0587 | 2013-11-28 20:49:54 +0100 | [diff] [blame] | 1093 | * Don't do destructive transforms as protocol code does |
| 1094 | * '[]' unwrapping in get_host_and_port() |
Ilari Liusvaara | 9aa5053 | 2010-01-26 20:24:42 +0200 | [diff] [blame] | 1095 | */ |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 1096 | end = host_end(&host, 0); |
YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 19:23:42 +0900 | [diff] [blame] | 1097 | |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 1098 | if (protocol == PROTO_LOCAL) |
Linus Torvalds | 72a4f4b | 2007-08-01 10:03:37 -0700 | [diff] [blame] | 1099 | path = end; |
Torsten Bögershausen | ebb8d2c | 2019-08-24 15:07:59 -0700 | [diff] [blame] | 1100 | else if (protocol == PROTO_FILE && *host != '/' && |
| 1101 | !has_dos_drive_prefix(host) && |
| 1102 | offset_1st_component(host - 2) > 1) |
| 1103 | path = host - 2; /* include the leading "//" */ |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 1104 | else if (protocol == PROTO_FILE && has_dos_drive_prefix(end)) |
| 1105 | path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */ |
| 1106 | else |
| 1107 | path = strchr(end, separator); |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 1108 | |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 1109 | if (!path || !*path) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 1110 | die(_("no path specified; see 'git help pull' for valid url syntax")); |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 1111 | |
| 1112 | /* |
| 1113 | * null-terminate hostname and point path to ~ for URL's like this: |
| 1114 | * ssh://host.xz/~user/repo |
| 1115 | */ |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 1116 | |
| 1117 | end = path; /* Need to \0 terminate host here */ |
| 1118 | if (separator == ':') |
| 1119 | path++; /* path starts after ':' */ |
| 1120 | if (protocol == PROTO_GIT || protocol == PROTO_SSH) { |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 1121 | if (path[1] == '~') |
| 1122 | path++; |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 1125 | path = xstrdup(path); |
| 1126 | *end = '\0'; |
| 1127 | |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 1128 | *ret_host = xstrdup(host); |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 1129 | *ret_path = path; |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 1130 | free(url); |
| 1131 | return protocol; |
| 1132 | } |
| 1133 | |
Nguyễn Thái Ngọc Duy | 3c8ede3 | 2016-06-26 13:16:35 +0200 | [diff] [blame] | 1134 | static const char *get_ssh_command(void) |
| 1135 | { |
| 1136 | const char *ssh; |
| 1137 | |
| 1138 | if ((ssh = getenv("GIT_SSH_COMMAND"))) |
| 1139 | return ssh; |
| 1140 | |
Jeff King | f1de981 | 2020-08-14 12:17:36 -0400 | [diff] [blame] | 1141 | if (!git_config_get_string_tmp("core.sshcommand", &ssh)) |
Nguyễn Thái Ngọc Duy | 3c8ede3 | 2016-06-26 13:16:35 +0200 | [diff] [blame] | 1142 | return ssh; |
| 1143 | |
| 1144 | return NULL; |
| 1145 | } |
| 1146 | |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1147 | enum ssh_variant { |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 1148 | VARIANT_AUTO, |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1149 | VARIANT_SIMPLE, |
| 1150 | VARIANT_SSH, |
| 1151 | VARIANT_PLINK, |
| 1152 | VARIANT_PUTTY, |
| 1153 | VARIANT_TORTOISEPLINK, |
| 1154 | }; |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 1155 | |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 1156 | static void override_ssh_variant(enum ssh_variant *ssh_variant) |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1157 | { |
| 1158 | const char *variant = getenv("GIT_SSH_VARIANT"); |
| 1159 | |
Jeff King | f1de981 | 2020-08-14 12:17:36 -0400 | [diff] [blame] | 1160 | if (!variant && git_config_get_string_tmp("ssh.variant", &variant)) |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 1161 | return; |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 1162 | |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 1163 | if (!strcmp(variant, "auto")) |
| 1164 | *ssh_variant = VARIANT_AUTO; |
| 1165 | else if (!strcmp(variant, "plink")) |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1166 | *ssh_variant = VARIANT_PLINK; |
| 1167 | else if (!strcmp(variant, "putty")) |
| 1168 | *ssh_variant = VARIANT_PUTTY; |
| 1169 | else if (!strcmp(variant, "tortoiseplink")) |
| 1170 | *ssh_variant = VARIANT_TORTOISEPLINK; |
| 1171 | else if (!strcmp(variant, "simple")) |
| 1172 | *ssh_variant = VARIANT_SIMPLE; |
| 1173 | else |
| 1174 | *ssh_variant = VARIANT_SSH; |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1177 | static enum ssh_variant determine_ssh_variant(const char *ssh_command, |
| 1178 | int is_cmdline) |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 1179 | { |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 1180 | enum ssh_variant ssh_variant = VARIANT_AUTO; |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 1181 | const char *variant; |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1182 | char *p = NULL; |
| 1183 | |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 1184 | override_ssh_variant(&ssh_variant); |
| 1185 | |
| 1186 | if (ssh_variant != VARIANT_AUTO) |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1187 | return ssh_variant; |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 1188 | |
| 1189 | if (!is_cmdline) { |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1190 | p = xstrdup(ssh_command); |
| 1191 | variant = basename(p); |
| 1192 | } else { |
| 1193 | const char **ssh_argv; |
| 1194 | |
| 1195 | p = xstrdup(ssh_command); |
Jeff King | 22e5ae5 | 2017-04-10 20:30:23 -0400 | [diff] [blame] | 1196 | if (split_cmdline(p, &ssh_argv) > 0) { |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1197 | variant = basename((char *)ssh_argv[0]); |
| 1198 | /* |
| 1199 | * At this point, variant points into the buffer |
| 1200 | * referenced by p, hence we do not need ssh_argv |
| 1201 | * any longer. |
| 1202 | */ |
| 1203 | free(ssh_argv); |
Jeff King | 5d2993b | 2017-04-20 16:21:58 -0400 | [diff] [blame] | 1204 | } else { |
| 1205 | free(p); |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1206 | return ssh_variant; |
Jeff King | 5d2993b | 2017-04-20 16:21:58 -0400 | [diff] [blame] | 1207 | } |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1210 | if (!strcasecmp(variant, "ssh") || |
| 1211 | !strcasecmp(variant, "ssh.exe")) |
| 1212 | ssh_variant = VARIANT_SSH; |
| 1213 | else if (!strcasecmp(variant, "plink") || |
| 1214 | !strcasecmp(variant, "plink.exe")) |
| 1215 | ssh_variant = VARIANT_PLINK; |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1216 | else if (!strcasecmp(variant, "tortoiseplink") || |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1217 | !strcasecmp(variant, "tortoiseplink.exe")) |
| 1218 | ssh_variant = VARIANT_TORTOISEPLINK; |
| 1219 | |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1220 | free(p); |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1221 | return ssh_variant; |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1222 | } |
| 1223 | |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1224 | /* |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1225 | * Open a connection using Git's native protocol. |
| 1226 | * |
| 1227 | * The caller is responsible for freeing hostandport, but this function may |
| 1228 | * modify it (for example, to truncate it to remove the port part). |
| 1229 | */ |
| 1230 | static struct child_process *git_connect_git(int fd[2], char *hostandport, |
| 1231 | const char *path, const char *prog, |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1232 | enum protocol_version version, |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1233 | int flags) |
| 1234 | { |
| 1235 | struct child_process *conn; |
| 1236 | struct strbuf request = STRBUF_INIT; |
| 1237 | /* |
| 1238 | * Set up virtual host information based on where we will |
| 1239 | * connect, unless the user has overridden us in |
| 1240 | * the environment. |
| 1241 | */ |
| 1242 | char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST"); |
| 1243 | if (target_host) |
| 1244 | target_host = xstrdup(target_host); |
| 1245 | else |
| 1246 | target_host = xstrdup(hostandport); |
| 1247 | |
| 1248 | transport_check_allowed("git"); |
Jeff King | a02ea57 | 2021-01-07 04:43:58 -0500 | [diff] [blame] | 1249 | if (strchr(target_host, '\n') || strchr(path, '\n')) |
| 1250 | die(_("newline is forbidden in git:// hosts and repo paths")); |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1251 | |
Jonathan Nieder | 233cd28 | 2017-11-20 14:04:58 -0800 | [diff] [blame] | 1252 | /* |
| 1253 | * These underlying connection commands die() if they |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1254 | * cannot connect. |
| 1255 | */ |
| 1256 | if (git_use_proxy(hostandport)) |
| 1257 | conn = git_proxy_connect(fd, hostandport); |
| 1258 | else |
| 1259 | conn = git_tcp_connect(fd, hostandport, flags); |
| 1260 | /* |
| 1261 | * Separate original protocol components prog and path |
| 1262 | * from extended host header with a NUL byte. |
| 1263 | * |
| 1264 | * Note: Do not add any other headers here! Doing so |
| 1265 | * will cause older git-daemon servers to crash. |
| 1266 | */ |
| 1267 | strbuf_addf(&request, |
| 1268 | "%s %s%chost=%s%c", |
| 1269 | prog, path, 0, |
| 1270 | target_host, 0); |
| 1271 | |
| 1272 | /* If using a new version put that stuff here after a second null byte */ |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1273 | if (version > 0) { |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1274 | strbuf_addch(&request, '\0'); |
| 1275 | strbuf_addf(&request, "version=%d%c", |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1276 | version, '\0'); |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | packet_write(fd[1], request.buf, request.len); |
| 1280 | |
| 1281 | free(target_host); |
| 1282 | strbuf_release(&request); |
| 1283 | return conn; |
| 1284 | } |
| 1285 | |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1286 | /* |
| 1287 | * Append the appropriate environment variables to `env` and options to |
| 1288 | * `args` for running ssh in Git's SSH-tunneled transport. |
| 1289 | */ |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1290 | static void push_ssh_options(struct strvec *args, struct strvec *env, |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1291 | enum ssh_variant variant, const char *port, |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1292 | enum protocol_version version, int flags) |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1293 | { |
| 1294 | if (variant == VARIANT_SSH && |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1295 | version > 0) { |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1296 | strvec_push(args, "-o"); |
| 1297 | strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT); |
| 1298 | strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d", |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 1299 | version); |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Jonathan Nieder | a3f5b66 | 2017-11-20 13:30:30 -0800 | [diff] [blame] | 1302 | if (flags & CONNECT_IPV4) { |
| 1303 | switch (variant) { |
| 1304 | case VARIANT_AUTO: |
| 1305 | BUG("VARIANT_AUTO passed to push_ssh_options"); |
| 1306 | case VARIANT_SIMPLE: |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 1307 | die(_("ssh variant 'simple' does not support -4")); |
Jonathan Nieder | a3f5b66 | 2017-11-20 13:30:30 -0800 | [diff] [blame] | 1308 | case VARIANT_SSH: |
| 1309 | case VARIANT_PLINK: |
| 1310 | case VARIANT_PUTTY: |
| 1311 | case VARIANT_TORTOISEPLINK: |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1312 | strvec_push(args, "-4"); |
Jonathan Nieder | a3f5b66 | 2017-11-20 13:30:30 -0800 | [diff] [blame] | 1313 | } |
| 1314 | } else if (flags & CONNECT_IPV6) { |
| 1315 | switch (variant) { |
| 1316 | case VARIANT_AUTO: |
| 1317 | BUG("VARIANT_AUTO passed to push_ssh_options"); |
| 1318 | case VARIANT_SIMPLE: |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 1319 | die(_("ssh variant 'simple' does not support -6")); |
Jonathan Nieder | a3f5b66 | 2017-11-20 13:30:30 -0800 | [diff] [blame] | 1320 | case VARIANT_SSH: |
| 1321 | case VARIANT_PLINK: |
| 1322 | case VARIANT_PUTTY: |
| 1323 | case VARIANT_TORTOISEPLINK: |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1324 | strvec_push(args, "-6"); |
Jonathan Nieder | a3f5b66 | 2017-11-20 13:30:30 -0800 | [diff] [blame] | 1325 | } |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | if (variant == VARIANT_TORTOISEPLINK) |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1329 | strvec_push(args, "-batch"); |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1330 | |
Jonathan Nieder | 3fa5e0d | 2017-11-20 13:31:01 -0800 | [diff] [blame] | 1331 | if (port) { |
| 1332 | switch (variant) { |
| 1333 | case VARIANT_AUTO: |
| 1334 | BUG("VARIANT_AUTO passed to push_ssh_options"); |
| 1335 | case VARIANT_SIMPLE: |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 1336 | die(_("ssh variant 'simple' does not support setting port")); |
Jonathan Nieder | 3fa5e0d | 2017-11-20 13:31:01 -0800 | [diff] [blame] | 1337 | case VARIANT_SSH: |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1338 | strvec_push(args, "-p"); |
Jonathan Nieder | 3fa5e0d | 2017-11-20 13:31:01 -0800 | [diff] [blame] | 1339 | break; |
| 1340 | case VARIANT_PLINK: |
| 1341 | case VARIANT_PUTTY: |
| 1342 | case VARIANT_TORTOISEPLINK: |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1343 | strvec_push(args, "-P"); |
Jonathan Nieder | 3fa5e0d | 2017-11-20 13:31:01 -0800 | [diff] [blame] | 1344 | } |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1345 | |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1346 | strvec_push(args, port); |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | |
Jonathan Nieder | fce54ce | 2017-11-20 14:19:43 -0800 | [diff] [blame] | 1350 | /* Prepare a child_process for use by Git's SSH-tunneled transport. */ |
| 1351 | static void fill_ssh_args(struct child_process *conn, const char *ssh_host, |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1352 | const char *port, enum protocol_version version, |
| 1353 | int flags) |
Jonathan Nieder | fce54ce | 2017-11-20 14:19:43 -0800 | [diff] [blame] | 1354 | { |
| 1355 | const char *ssh; |
| 1356 | enum ssh_variant variant; |
| 1357 | |
| 1358 | if (looks_like_command_line_option(ssh_host)) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 1359 | die(_("strange hostname '%s' blocked"), ssh_host); |
Jonathan Nieder | fce54ce | 2017-11-20 14:19:43 -0800 | [diff] [blame] | 1360 | |
| 1361 | ssh = get_ssh_command(); |
| 1362 | if (ssh) { |
| 1363 | variant = determine_ssh_variant(ssh, 1); |
| 1364 | } else { |
| 1365 | /* |
| 1366 | * GIT_SSH is the no-shell version of |
| 1367 | * GIT_SSH_COMMAND (and must remain so for |
| 1368 | * historical compatibility). |
| 1369 | */ |
| 1370 | conn->use_shell = 0; |
| 1371 | |
| 1372 | ssh = getenv("GIT_SSH"); |
| 1373 | if (!ssh) |
| 1374 | ssh = "ssh"; |
| 1375 | variant = determine_ssh_variant(ssh, 0); |
| 1376 | } |
| 1377 | |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 1378 | if (variant == VARIANT_AUTO) { |
| 1379 | struct child_process detect = CHILD_PROCESS_INIT; |
| 1380 | |
| 1381 | detect.use_shell = conn->use_shell; |
| 1382 | detect.no_stdin = detect.no_stdout = detect.no_stderr = 1; |
| 1383 | |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1384 | strvec_push(&detect.args, ssh); |
| 1385 | strvec_push(&detect.args, "-G"); |
Ævar Arnfjörð Bjarmason | 29fda24 | 2022-06-02 11:09:50 +0200 | [diff] [blame] | 1386 | push_ssh_options(&detect.args, &detect.env, |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1387 | VARIANT_SSH, port, version, flags); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1388 | strvec_push(&detect.args, ssh_host); |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 1389 | |
| 1390 | variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH; |
| 1391 | } |
| 1392 | |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1393 | strvec_push(&conn->args, ssh); |
Ævar Arnfjörð Bjarmason | 29fda24 | 2022-06-02 11:09:50 +0200 | [diff] [blame] | 1394 | push_ssh_options(&conn->args, &conn->env, variant, port, version, |
| 1395 | flags); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1396 | strvec_push(&conn->args, ssh_host); |
Jonathan Nieder | fce54ce | 2017-11-20 14:19:43 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1399 | /* |
Jonathan Nieder | 8e34978 | 2017-11-20 13:22:54 -0800 | [diff] [blame] | 1400 | * This returns the dummy child_process `no_fork` if the transport protocol |
| 1401 | * does not need fork(2), or a struct child_process object if it does. Once |
| 1402 | * done, finish the connection with finish_connect() with the value returned |
| 1403 | * from this function (it is safe to call finish_connect() with NULL to |
| 1404 | * support the former case). |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1405 | * |
| 1406 | * If it returns, the connect is successful; it just dies on errors (this |
| 1407 | * will hopefully be changed in a libification effort, to return NULL when |
| 1408 | * the connection failed). |
| 1409 | */ |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 1410 | struct child_process *git_connect(int fd[2], const char *url, |
Jeff King | eaa0fd6 | 2023-03-17 15:08:51 -0400 | [diff] [blame] | 1411 | const char *name, |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1412 | const char *prog, int flags) |
| 1413 | { |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1414 | char *hostandport, *path; |
Jonathan Nieder | 8e34978 | 2017-11-20 13:22:54 -0800 | [diff] [blame] | 1415 | struct child_process *conn; |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 1416 | enum protocol protocol; |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1417 | enum protocol_version version = get_protocol_version_config(); |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1418 | |
Brandon Williams | 1aa8dded | 2018-03-15 10:31:31 -0700 | [diff] [blame] | 1419 | /* |
| 1420 | * NEEDSWORK: If we are trying to use protocol v2 and we are planning |
Jeff King | eaa0fd6 | 2023-03-17 15:08:51 -0400 | [diff] [blame] | 1421 | * to perform any operation that doesn't involve upload-pack (i.e., a |
| 1422 | * fetch, ls-remote, etc), then fallback to v0 since we don't know how |
| 1423 | * to do anything else (like push or remote archive) via v2. |
Brandon Williams | 1aa8dded | 2018-03-15 10:31:31 -0700 | [diff] [blame] | 1424 | */ |
Jeff King | eaa0fd6 | 2023-03-17 15:08:51 -0400 | [diff] [blame] | 1425 | if (version == protocol_v2 && strcmp("git-upload-pack", name)) |
Brandon Williams | 1aa8dded | 2018-03-15 10:31:31 -0700 | [diff] [blame] | 1426 | version = protocol_v0; |
| 1427 | |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1428 | /* Without this we cannot rely on waitpid() to tell |
| 1429 | * what happened to our children. |
| 1430 | */ |
| 1431 | signal(SIGCHLD, SIG_DFL); |
| 1432 | |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1433 | protocol = parse_connect_url(url, &hostandport, &path); |
Torsten Bögershausen | 3f55cca | 2015-02-21 16:52:55 +0100 | [diff] [blame] | 1434 | if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) { |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 1435 | printf("Diag: url=%s\n", url ? url : "NULL"); |
| 1436 | printf("Diag: protocol=%s\n", prot_name(protocol)); |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1437 | printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL"); |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 1438 | printf("Diag: path=%s\n", path ? path : "NULL"); |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1439 | conn = NULL; |
| 1440 | } else if (protocol == PROTO_GIT) { |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1441 | conn = git_connect_git(fd, hostandport, path, prog, version, flags); |
Jeff Hostetler | abd81a3 | 2019-02-22 14:25:05 -0800 | [diff] [blame] | 1442 | conn->trace2_child_class = "transport/git"; |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1443 | } else { |
Rene Scharfe | f139929 | 2017-08-30 19:49:39 +0200 | [diff] [blame] | 1444 | struct strbuf cmd = STRBUF_INIT; |
Brandon Williams | 0c2f0d2 | 2017-10-16 10:55:28 -0700 | [diff] [blame] | 1445 | const char *const *var; |
Rene Scharfe | f139929 | 2017-08-30 19:49:39 +0200 | [diff] [blame] | 1446 | |
René Scharfe | 483bbd4 | 2014-08-19 21:10:48 +0200 | [diff] [blame] | 1447 | conn = xmalloc(sizeof(*conn)); |
| 1448 | child_process_init(conn); |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1449 | |
Jeff King | aeeb2d4 | 2017-07-28 15:28:55 -0400 | [diff] [blame] | 1450 | if (looks_like_command_line_option(path)) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 1451 | die(_("strange pathname '%s' blocked"), path); |
Jeff King | aeeb2d4 | 2017-07-28 15:28:55 -0400 | [diff] [blame] | 1452 | |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1453 | strbuf_addstr(&cmd, prog); |
| 1454 | strbuf_addch(&cmd, ' '); |
| 1455 | sq_quote_buf(&cmd, path); |
Christian Couder | 0f503d7 | 2006-09-11 07:04:50 +0200 | [diff] [blame] | 1456 | |
Jeff King | aab4043 | 2015-09-04 18:40:08 -0400 | [diff] [blame] | 1457 | /* remove repo-local variables from the environment */ |
Brandon Williams | 0c2f0d2 | 2017-10-16 10:55:28 -0700 | [diff] [blame] | 1458 | for (var = local_repo_env; *var; var++) |
Ævar Arnfjörð Bjarmason | 29fda24 | 2022-06-02 11:09:50 +0200 | [diff] [blame] | 1459 | strvec_push(&conn->env, *var); |
Brandon Williams | 0c2f0d2 | 2017-10-16 10:55:28 -0700 | [diff] [blame] | 1460 | |
Jeff King | a48b409 | 2015-09-08 04:33:14 -0400 | [diff] [blame] | 1461 | conn->use_shell = 1; |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1462 | conn->in = conn->out = -1; |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1463 | if (protocol == PROTO_SSH) { |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1464 | char *ssh_host = hostandport; |
| 1465 | const char *port = NULL; |
Jeff King | a5adace | 2015-09-16 13:12:52 -0400 | [diff] [blame] | 1466 | transport_check_allowed("ssh"); |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1467 | get_host_and_port(&ssh_host, &port); |
Christian Couder | 0f503d7 | 2006-09-11 07:04:50 +0200 | [diff] [blame] | 1468 | |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 1469 | if (!port) |
| 1470 | port = get_port(ssh_host); |
Junio C Hamano | 42da484 | 2015-03-05 12:45:44 -0800 | [diff] [blame] | 1471 | |
Torsten Bögershausen | 3f55cca | 2015-02-21 16:52:55 +0100 | [diff] [blame] | 1472 | if (flags & CONNECT_DIAG_URL) { |
| 1473 | printf("Diag: url=%s\n", url ? url : "NULL"); |
| 1474 | printf("Diag: protocol=%s\n", prot_name(protocol)); |
| 1475 | printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL"); |
| 1476 | printf("Diag: port=%s\n", port ? port : "NONE"); |
| 1477 | printf("Diag: path=%s\n", path ? path : "NULL"); |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1478 | |
Torsten Bögershausen | 3f55cca | 2015-02-21 16:52:55 +0100 | [diff] [blame] | 1479 | free(hostandport); |
| 1480 | free(path); |
Stefan Beller | 04f20c0 | 2015-03-09 09:58:22 -0700 | [diff] [blame] | 1481 | free(conn); |
Rene Scharfe | f139929 | 2017-08-30 19:49:39 +0200 | [diff] [blame] | 1482 | strbuf_release(&cmd); |
Torsten Bögershausen | 3f55cca | 2015-02-21 16:52:55 +0100 | [diff] [blame] | 1483 | return NULL; |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1484 | } |
Jeff Hostetler | abd81a3 | 2019-02-22 14:25:05 -0800 | [diff] [blame] | 1485 | conn->trace2_child_class = "transport/ssh"; |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1486 | fill_ssh_args(conn, ssh_host, port, version, flags); |
Nguyễn Thái Ngọc Duy | c049b61 | 2014-03-13 18:45:31 +0700 | [diff] [blame] | 1487 | } else { |
Jeff King | a5adace | 2015-09-16 13:12:52 -0400 | [diff] [blame] | 1488 | transport_check_allowed("file"); |
Jeff Hostetler | abd81a3 | 2019-02-22 14:25:05 -0800 | [diff] [blame] | 1489 | conn->trace2_child_class = "transport/file"; |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1490 | if (version > 0) { |
Ævar Arnfjörð Bjarmason | 29fda24 | 2022-06-02 11:09:50 +0200 | [diff] [blame] | 1491 | strvec_pushf(&conn->env, |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 1492 | GIT_PROTOCOL_ENVIRONMENT "=version=%d", |
| 1493 | version); |
Brandon Williams | 0c2f0d2 | 2017-10-16 10:55:28 -0700 | [diff] [blame] | 1494 | } |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1495 | } |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1496 | strvec_push(&conn->args, cmd.buf); |
Johannes Sixt | f364cb8 | 2007-10-19 21:47:54 +0200 | [diff] [blame] | 1497 | |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1498 | if (start_command(conn)) |
Nguyễn Thái Ngọc Duy | aad6fdd | 2018-07-21 09:49:28 +0200 | [diff] [blame] | 1499 | die(_("unable to fork")); |
Johannes Sixt | f364cb8 | 2007-10-19 21:47:54 +0200 | [diff] [blame] | 1500 | |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1501 | fd[0] = conn->out; /* read from child's stdout */ |
| 1502 | fd[1] = conn->in; /* write to child's stdin */ |
| 1503 | strbuf_release(&cmd); |
| 1504 | } |
| 1505 | free(hostandport); |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 1506 | free(path); |
Johannes Sixt | 98158e9 | 2007-10-19 21:47:53 +0200 | [diff] [blame] | 1507 | return conn; |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
Johannes Sixt | 98158e9 | 2007-10-19 21:47:53 +0200 | [diff] [blame] | 1510 | int finish_connect(struct child_process *conn) |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1511 | { |
Johannes Sixt | f364cb8 | 2007-10-19 21:47:54 +0200 | [diff] [blame] | 1512 | int code; |
Jeff King | 7ffe853 | 2011-05-16 02:52:11 -0400 | [diff] [blame] | 1513 | if (!conn || git_connection_is_socket(conn)) |
Franck Bui-Huu | f42a5c4 | 2006-09-12 11:00:13 +0200 | [diff] [blame] | 1514 | return 0; |
| 1515 | |
Johannes Sixt | f364cb8 | 2007-10-19 21:47:54 +0200 | [diff] [blame] | 1516 | code = finish_command(conn); |
Johannes Sixt | 98158e9 | 2007-10-19 21:47:53 +0200 | [diff] [blame] | 1517 | free(conn); |
Johannes Sixt | f364cb8 | 2007-10-19 21:47:54 +0200 | [diff] [blame] | 1518 | return code; |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1519 | } |