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