blob: 0d77737a5363c39fd71376c0f483bc8808538754 [file] [log] [blame]
Jason Riedy731043f2006-01-25 12:38:36 -08001#include "git-compat-util.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00003#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00004#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00005#include "hex.h"
Linus Torvalds41cb7482005-07-05 15:44:09 -07006#include "pkt-line.h"
Junio C Hamanob10d0ec2005-07-08 00:02:52 -07007#include "quote.h"
Junio C Hamano6abf5c02005-10-16 00:25:26 -07008#include "refs.h"
Shawn O. Pearce15a1c0122007-03-12 19:00:19 -04009#include "run-command.h"
Daniel Barkalow6b628162007-05-12 11:45:59 -040010#include "remote.h"
Junio C Hamano47a59182013-07-08 13:56:53 -070011#include "connect.h"
Jeff King9d2e9422010-05-23 05:19:44 -040012#include "url.h"
Junio C Hamanoa45b5f02013-09-17 19:10:31 -070013#include "string-list.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040014#include "oid-array.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000015#include "path.h"
Jeff Kinga5adace2015-09-16 13:12:52 -040016#include "transport.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000017#include "trace2.h"
Jonathan Tan0cd83282017-09-26 16:56:19 -070018#include "strbuf.h"
Brandon Williamse52449b2018-03-15 10:31:21 -070019#include "version.h"
Brandon Williams26090432017-10-16 10:55:27 -070020#include "protocol.h"
Nguyễn Thái Ngọc Duy65b5f942018-05-20 20:40:06 +020021#include "alias.h"
Ævar Arnfjörð Bjarmason0cfde742022-12-22 15:14:09 +000022#include "bundle-uri.h"
Linus Torvaldsf7192592005-07-04 11:57:58 -070023
Brandon Williamse52449b2018-03-15 10:31:21 -070024static char *server_capabilities_v1;
Jeff Kingef8d7ac2020-07-28 16:24:53 -040025static struct strvec server_capabilities_v2 = STRVEC_INIT;
Jeff King7ce4c8f2023-04-14 17:25:20 -040026static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset);
Johannes Schindelin211b5f92005-10-28 04:48:54 +020027
René Scharfebe0b3f82014-08-30 11:46:54 +020028static int check_ref(const char *name, unsigned int flags)
Linus Torvalds2718ff02006-07-04 12:29:10 -070029{
30 if (!flags)
31 return 1;
32
René Scharfebe0b3f82014-08-30 11:46:54 +020033 if (!skip_prefix(name, "refs/", &name))
Linus Torvalds2718ff02006-07-04 12:29:10 -070034 return 0;
35
Linus Torvalds2718ff02006-07-04 12:29:10 -070036 /* REF_NORMAL means that we don't want the magic fake tag refs */
ZheNing Hu7c3c5502023-03-01 10:20:29 +000037 if ((flags & REF_NORMAL) && check_refname_format(name,
38 REFNAME_ALLOW_ONELEVEL))
Linus Torvalds2718ff02006-07-04 12:29:10 -070039 return 0;
40
41 /* REF_HEADS means that we want regular branch heads */
René Scharfebe0b3f82014-08-30 11:46:54 +020042 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
Linus Torvalds2718ff02006-07-04 12:29:10 -070043 return 1;
44
45 /* REF_TAGS means that we want tags */
René Scharfebe0b3f82014-08-30 11:46:54 +020046 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
Linus Torvalds2718ff02006-07-04 12:29:10 -070047 return 1;
48
49 /* All type bits clear means that we are ok with anything */
50 return !(flags & ~REF_NORMAL);
51}
52
Daniel Barkalow45773702007-10-29 21:05:40 -040053int check_ref_type(const struct ref *ref, int flags)
54{
René Scharfebe0b3f82014-08-30 11:46:54 +020055 return check_ref(ref->name, flags);
Daniel Barkalow45773702007-10-29 21:05:40 -040056}
57
Nguyễn Thái Ngọc Duyd2bff222018-04-14 19:19:43 +000058static NORETURN void die_initial_contact(int unexpected)
Heiko Voigt46284dd2012-06-19 20:24:50 +020059{
Brandon Williams7e3e4792018-03-14 11:31:44 -070060 /*
61 * A hang-up after seeing some response from the other end
62 * means that it is unexpected, as we know the other end is
63 * willing to talk to us. A hang-up before seeing any
64 * response does not necessarily mean an ACL problem, though.
65 */
Jonathan Nieder55e4f932016-09-09 10:36:29 -070066 if (unexpected)
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +020067 die(_("the remote end hung up upon initial contact"));
Heiko Voigt46284dd2012-06-19 20:24:50 +020068 else
Vasco Almeidaf2b93b32016-09-19 13:08:17 +000069 die(_("Could not read from remote repository.\n\n"
70 "Please make sure you have the correct access rights\n"
71 "and the repository exists."));
Heiko Voigt46284dd2012-06-19 20:24:50 +020072}
73
Brandon Williamse52449b2018-03-15 10:31:21 -070074/* Checks if the server supports the capability 'c' */
Jeff Kinga31cfe32022-12-13 05:52:58 -050075int server_supports_v2(const char *c)
Brandon Williamse52449b2018-03-15 10:31:21 -070076{
77 int i;
78
Jeff Kingd70a9eb2020-07-28 20:37:20 -040079 for (i = 0; i < server_capabilities_v2.nr; i++) {
Brandon Williamse52449b2018-03-15 10:31:21 -070080 const char *out;
Jeff Kingd70a9eb2020-07-28 20:37:20 -040081 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
Brandon Williamse52449b2018-03-15 10:31:21 -070082 (!*out || *out == '='))
83 return 1;
84 }
Brandon Williamse52449b2018-03-15 10:31:21 -070085 return 0;
86}
87
Jeff Kinga31cfe32022-12-13 05:52:58 -050088void ensure_server_supports_v2(const char *c)
89{
90 if (!server_supports_v2(c))
91 die(_("server doesn't support '%s'"), c);
92}
93
brian m. carlson1349ffe2020-05-25 19:58:53 +000094int server_feature_v2(const char *c, const char **v)
95{
96 int i;
97
Jeff Kingd70a9eb2020-07-28 20:37:20 -040098 for (i = 0; i < server_capabilities_v2.nr; i++) {
brian m. carlson1349ffe2020-05-25 19:58:53 +000099 const char *out;
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400100 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
brian m. carlson1349ffe2020-05-25 19:58:53 +0000101 (*out == '=')) {
102 *v = out + 1;
103 return 1;
104 }
105 }
106 return 0;
107}
108
Brandon Williamsf7e20502018-03-15 10:31:29 -0700109int server_supports_feature(const char *c, const char *feature,
110 int die_on_error)
111{
112 int i;
113
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400114 for (i = 0; i < server_capabilities_v2.nr; i++) {
Brandon Williamsf7e20502018-03-15 10:31:29 -0700115 const char *out;
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400116 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
Brandon Williamsf7e20502018-03-15 10:31:29 -0700117 (!*out || *(out++) == '=')) {
118 if (parse_feature_request(out, feature))
119 return 1;
120 else
121 break;
122 }
123 }
124
125 if (die_on_error)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200126 die(_("server doesn't support feature '%s'"), feature);
Brandon Williamsf7e20502018-03-15 10:31:29 -0700127
128 return 0;
129}
130
Brandon Williamse52449b2018-03-15 10:31:21 -0700131static void process_capabilities_v2(struct packet_reader *reader)
132{
133 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400134 strvec_push(&server_capabilities_v2, reader->line);
Brandon Williamse52449b2018-03-15 10:31:21 -0700135
136 if (reader->status != PACKET_READ_FLUSH)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200137 die(_("expected flush after capabilities"));
Brandon Williamse52449b2018-03-15 10:31:21 -0700138}
139
Brandon Williamsad6ac122018-03-14 11:31:45 -0700140enum protocol_version discover_version(struct packet_reader *reader)
Brandon Williams7e3e4792018-03-14 11:31:44 -0700141{
142 enum protocol_version version = protocol_unknown_version;
143
144 /*
145 * Peek the first line of the server's response to
146 * determine the protocol version the server is speaking.
147 */
148 switch (packet_reader_peek(reader)) {
149 case PACKET_READ_EOF:
150 die_initial_contact(0);
151 case PACKET_READ_FLUSH:
152 case PACKET_READ_DELIM:
Denton Liu0181b602020-05-19 06:53:59 -0400153 case PACKET_READ_RESPONSE_END:
Brandon Williams7e3e4792018-03-14 11:31:44 -0700154 version = protocol_v0;
155 break;
156 case PACKET_READ_NORMAL:
157 version = determine_protocol_version_client(reader->line);
158 break;
159 }
160
161 switch (version) {
Brandon Williams8f6982b2018-03-14 11:31:47 -0700162 case protocol_v2:
Brandon Williamse52449b2018-03-15 10:31:21 -0700163 process_capabilities_v2(reader);
Brandon Williams8f6982b2018-03-14 11:31:47 -0700164 break;
Brandon Williams7e3e4792018-03-14 11:31:44 -0700165 case protocol_v1:
166 /* Read the peeked version line */
167 packet_reader_read(reader);
168 break;
169 case protocol_v0:
170 break;
171 case protocol_unknown_version:
172 BUG("unknown protocol version");
173 }
174
Josh Steadmon626beeb2021-08-10 10:20:39 -0700175 trace2_data_intmax("transfer", NULL, "negotiated-version", version);
176
Brandon Williams7e3e4792018-03-14 11:31:44 -0700177 return version;
178}
179
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700180static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
181{
182 char *sym, *target;
183 struct string_list_item *item;
184
185 if (!len)
186 return; /* just "symref" */
187 /* e.g. "symref=HEAD:refs/heads/master" */
René Scharfe5c0b13f2014-07-19 17:35:34 +0200188 sym = xmemdupz(val, len);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700189 target = strchr(sym, ':');
190 if (!target)
191 /* just "symref=something" */
192 goto reject;
193 *(target++) = '\0';
194 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
195 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
196 /* "symref=bogus:pair */
197 goto reject;
Jeff Kingef4fe562017-05-25 15:33:05 -0400198 item = string_list_append_nodup(symref, sym);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700199 item->util = target;
200 return;
201reject:
202 free(sym);
203 return;
204}
205
206static void annotate_refs_with_symref_info(struct ref *ref)
207{
208 struct string_list symref = STRING_LIST_INIT_DUP;
Jeff King7ce4c8f2023-04-14 17:25:20 -0400209 size_t offset = 0;
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700210
brian m. carlson2c6a4032020-05-25 19:58:52 +0000211 while (1) {
Jeff King7ce4c8f2023-04-14 17:25:20 -0400212 size_t len;
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700213 const char *val;
214
brian m. carlson2c6a4032020-05-25 19:58:52 +0000215 val = next_server_feature_value("symref", &len, &offset);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700216 if (!val)
217 break;
218 parse_one_symref_info(&symref, val, len);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700219 }
Michael Haggerty3383e192014-11-25 09:02:35 +0100220 string_list_sort(&symref);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700221
222 for (; ref; ref = ref->next) {
223 struct string_list_item *item;
224 item = string_list_lookup(&symref, ref->name);
225 if (!item)
226 continue;
227 ref->symref = xstrdup((char *)item->util);
228 }
229 string_list_clear(&symref, 0);
230}
231
brian m. carlson92315e52020-05-25 19:58:49 +0000232static void process_capabilities(struct packet_reader *reader, int *linelen)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700233{
brian m. carlson7c601dc2020-05-25 19:59:00 +0000234 const char *feat_val;
Jeff King7ce4c8f2023-04-14 17:25:20 -0400235 size_t feat_len;
brian m. carlson92315e52020-05-25 19:58:49 +0000236 const char *line = reader->line;
Brandon Williams7e3e4792018-03-14 11:31:44 -0700237 int nul_location = strlen(line);
brian m. carlson92315e52020-05-25 19:58:49 +0000238 if (nul_location == *linelen)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700239 return;
Brandon Williamse52449b2018-03-15 10:31:21 -0700240 server_capabilities_v1 = xstrdup(line + nul_location + 1);
brian m. carlson92315e52020-05-25 19:58:49 +0000241 *linelen = nul_location;
brian m. carlson7c601dc2020-05-25 19:59:00 +0000242
243 feat_val = server_feature_value("object-format", &feat_len);
244 if (feat_val) {
245 char *hash_name = xstrndup(feat_val, feat_len);
246 int hash_algo = hash_algo_by_name(hash_name);
247 if (hash_algo != GIT_HASH_UNKNOWN)
248 reader->hash_algo = &hash_algos[hash_algo];
249 free(hash_name);
250 } else {
251 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
252 }
Jonathan Tan0cd83282017-09-26 16:56:19 -0700253}
254
brian m. carlson92315e52020-05-25 19:58:49 +0000255static int process_dummy_ref(const struct packet_reader *reader)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700256{
brian m. carlson92315e52020-05-25 19:58:49 +0000257 const char *line = reader->line;
Jonathan Tan0cd83282017-09-26 16:56:19 -0700258 struct object_id oid;
259 const char *name;
260
brian m. carlson7c601dc2020-05-25 19:59:00 +0000261 if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700262 return 0;
263 if (*name != ' ')
264 return 0;
265 name++;
266
Jeff King13e67aa2023-04-14 17:25:11 -0400267 return oideq(reader->hash_algo->null_oid, &oid) &&
268 !strcmp(name, "capabilities^{}");
Jonathan Tan0cd83282017-09-26 16:56:19 -0700269}
270
Brandon Williams7e3e4792018-03-14 11:31:44 -0700271static void check_no_capabilities(const char *line, int len)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700272{
Brandon Williams7e3e4792018-03-14 11:31:44 -0700273 if (strlen(line) != len)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200274 warning(_("ignoring capabilities after first line '%s'"),
Brandon Williams7e3e4792018-03-14 11:31:44 -0700275 line + strlen(line));
Jonathan Tan0cd83282017-09-26 16:56:19 -0700276}
277
brian m. carlson92315e52020-05-25 19:58:49 +0000278static int process_ref(const struct packet_reader *reader, int len,
279 struct ref ***list, unsigned int flags,
280 struct oid_array *extra_have)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700281{
brian m. carlson92315e52020-05-25 19:58:49 +0000282 const char *line = reader->line;
Jonathan Tan0cd83282017-09-26 16:56:19 -0700283 struct object_id old_oid;
284 const char *name;
285
brian m. carlson7c601dc2020-05-25 19:59:00 +0000286 if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700287 return 0;
288 if (*name != ' ')
289 return 0;
290 name++;
291
292 if (extra_have && !strcmp(name, ".have")) {
293 oid_array_append(extra_have, &old_oid);
294 } else if (!strcmp(name, "capabilities^{}")) {
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200295 die(_("protocol error: unexpected capabilities^{}"));
Jonathan Tan0cd83282017-09-26 16:56:19 -0700296 } else if (check_ref(name, flags)) {
297 struct ref *ref = alloc_ref(name);
298 oidcpy(&ref->old_oid, &old_oid);
299 **list = ref;
300 *list = &ref->next;
301 }
Brandon Williams7e3e4792018-03-14 11:31:44 -0700302 check_no_capabilities(line, len);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700303 return 1;
304}
305
brian m. carlson92315e52020-05-25 19:58:49 +0000306static int process_shallow(const struct packet_reader *reader, int len,
Brandon Williams7e3e4792018-03-14 11:31:44 -0700307 struct oid_array *shallow_points)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700308{
brian m. carlson92315e52020-05-25 19:58:49 +0000309 const char *line = reader->line;
Jonathan Tan0cd83282017-09-26 16:56:19 -0700310 const char *arg;
311 struct object_id old_oid;
312
Brandon Williams7e3e4792018-03-14 11:31:44 -0700313 if (!skip_prefix(line, "shallow ", &arg))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700314 return 0;
315
brian m. carlson7c601dc2020-05-25 19:59:00 +0000316 if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200317 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700318 if (!shallow_points)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200319 die(_("repository on the other end cannot be shallow"));
Jonathan Tan0cd83282017-09-26 16:56:19 -0700320 oid_array_append(shallow_points, &old_oid);
Brandon Williams7e3e4792018-03-14 11:31:44 -0700321 check_no_capabilities(line, len);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700322 return 1;
323}
324
Brandon Williams7e3e4792018-03-14 11:31:44 -0700325enum get_remote_heads_state {
326 EXPECTING_FIRST_REF = 0,
327 EXPECTING_REF,
328 EXPECTING_SHALLOW,
329 EXPECTING_DONE,
330};
331
Jonathan Tan0cd83282017-09-26 16:56:19 -0700332/*
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700333 * Read all the refs from the other end
334 */
Brandon Williamsad6ac122018-03-14 11:31:45 -0700335struct ref **get_remote_heads(struct packet_reader *reader,
Jeff King85edf4f2013-02-20 15:06:45 -0500336 struct ref **list, unsigned int flags,
brian m. carlson910650d2017-03-31 01:40:00 +0000337 struct oid_array *extra_have,
338 struct oid_array *shallow_points)
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700339{
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700340 struct ref **orig_list = list;
Brandon Williams7e3e4792018-03-14 11:31:44 -0700341 int len = 0;
342 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
Jonathan Nieder55e4f932016-09-09 10:36:29 -0700343
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700344 *list = NULL;
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700345
Brandon Williams7e3e4792018-03-14 11:31:44 -0700346 while (state != EXPECTING_DONE) {
Brandon Williamsad6ac122018-03-14 11:31:45 -0700347 switch (packet_reader_read(reader)) {
Brandon Williams7e3e4792018-03-14 11:31:44 -0700348 case PACKET_READ_EOF:
349 die_initial_contact(1);
350 case PACKET_READ_NORMAL:
Brandon Williamsad6ac122018-03-14 11:31:45 -0700351 len = reader->pktlen;
Brandon Williams7e3e4792018-03-14 11:31:44 -0700352 break;
353 case PACKET_READ_FLUSH:
354 state = EXPECTING_DONE;
355 break;
356 case PACKET_READ_DELIM:
Denton Liu0181b602020-05-19 06:53:59 -0400357 case PACKET_READ_RESPONSE_END:
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200358 die(_("invalid packet"));
Brandon Williams7e3e4792018-03-14 11:31:44 -0700359 }
360
Jonathan Tan0cd83282017-09-26 16:56:19 -0700361 switch (state) {
362 case EXPECTING_FIRST_REF:
brian m. carlson92315e52020-05-25 19:58:49 +0000363 process_capabilities(reader, &len);
364 if (process_dummy_ref(reader)) {
Jonathan Tan0cd83282017-09-26 16:56:19 -0700365 state = EXPECTING_SHALLOW;
366 break;
367 }
368 state = EXPECTING_REF;
369 /* fallthrough */
370 case EXPECTING_REF:
brian m. carlson92315e52020-05-25 19:58:49 +0000371 if (process_ref(reader, len, &list, flags, extra_have))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700372 break;
373 state = EXPECTING_SHALLOW;
374 /* fallthrough */
375 case EXPECTING_SHALLOW:
brian m. carlson92315e52020-05-25 19:58:49 +0000376 if (process_shallow(reader, len, shallow_points))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700377 break;
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200378 die(_("protocol error: unexpected '%s'"), reader->line);
Brandon Williams7e3e4792018-03-14 11:31:44 -0700379 case EXPECTING_DONE:
380 break;
Nguyễn Thái Ngọc Duyb06dcd72013-12-05 20:02:33 +0700381 }
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700382 }
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700383
384 annotate_refs_with_symref_info(*orig_list);
385
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700386 return list;
387}
388
Brandon Williamse52449b2018-03-15 10:31:21 -0700389/* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
Jonathan Tan4f37d452021-02-05 12:48:49 -0800390static int process_ref_v2(struct packet_reader *reader, struct ref ***list,
Ævar Arnfjörð Bjarmasonf36d4f82022-02-05 01:08:14 +0100391 const char **unborn_head_target)
Brandon Williamse52449b2018-03-15 10:31:21 -0700392{
393 int ret = 1;
394 int i = 0;
395 struct object_id old_oid;
396 struct ref *ref;
397 struct string_list line_sections = STRING_LIST_INIT_DUP;
398 const char *end;
brian m. carlson67e9a702020-05-25 19:59:15 +0000399 const char *line = reader->line;
Brandon Williamse52449b2018-03-15 10:31:21 -0700400
401 /*
402 * Ref lines have a number of fields which are space deliminated. The
403 * first field is the OID of the ref. The second field is the ref
404 * name. Subsequent fields (symref-target and peeled) are optional and
405 * don't have a particular order.
406 */
407 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
408 ret = 0;
409 goto out;
410 }
411
Jonathan Tan4f37d452021-02-05 12:48:49 -0800412 if (!strcmp("unborn", line_sections.items[i].string)) {
413 i++;
414 if (unborn_head_target &&
415 !strcmp("HEAD", line_sections.items[i++].string)) {
416 /*
417 * Look for the symref target (if any). If found,
418 * return it to the caller.
419 */
420 for (; i < line_sections.nr; i++) {
421 const char *arg = line_sections.items[i].string;
422
423 if (skip_prefix(arg, "symref-target:", &arg)) {
424 *unborn_head_target = xstrdup(arg);
425 break;
426 }
427 }
428 }
429 goto out;
430 }
brian m. carlsonab672352020-05-25 19:59:16 +0000431 if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) ||
Brandon Williamse52449b2018-03-15 10:31:21 -0700432 *end) {
433 ret = 0;
434 goto out;
435 }
436
437 ref = alloc_ref(line_sections.items[i++].string);
438
brian m. carlsonab672352020-05-25 19:59:16 +0000439 memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
Brandon Williamse52449b2018-03-15 10:31:21 -0700440 **list = ref;
441 *list = &ref->next;
442
443 for (; i < line_sections.nr; i++) {
444 const char *arg = line_sections.items[i].string;
445 if (skip_prefix(arg, "symref-target:", &arg))
446 ref->symref = xstrdup(arg);
447
448 if (skip_prefix(arg, "peeled:", &arg)) {
449 struct object_id peeled_oid;
450 char *peeled_name;
451 struct ref *peeled;
brian m. carlsonab672352020-05-25 19:59:16 +0000452 if (parse_oid_hex_algop(arg, &peeled_oid, &end,
453 reader->hash_algo) || *end) {
Brandon Williamse52449b2018-03-15 10:31:21 -0700454 ret = 0;
455 goto out;
456 }
457
458 peeled_name = xstrfmt("%s^{}", ref->name);
459 peeled = alloc_ref(peeled_name);
460
brian m. carlsonab672352020-05-25 19:59:16 +0000461 memcpy(peeled->old_oid.hash, peeled_oid.hash,
462 reader->hash_algo->rawsz);
Brandon Williamse52449b2018-03-15 10:31:21 -0700463 **list = peeled;
464 *list = &peeled->next;
465
466 free(peeled_name);
467 }
468 }
469
470out:
471 string_list_clear(&line_sections, 0);
472 return ret;
473}
474
Denton Liub0df0c12020-05-19 06:54:00 -0400475void check_stateless_delimiter(int stateless_rpc,
476 struct packet_reader *reader,
477 const char *error)
478{
479 if (!stateless_rpc)
480 return; /* not in stateless mode, no delimiter expected */
481 if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END)
482 die("%s", error);
483}
484
Ævar Arnfjörð Bjarmason86f4e312022-05-16 20:10:58 +0000485static void send_capabilities(int fd_out, struct packet_reader *reader)
Brandon Williamse52449b2018-03-15 10:31:21 -0700486{
brian m. carlsonab672352020-05-25 19:59:16 +0000487 const char *hash_name;
Brandon Williamse52449b2018-03-15 10:31:21 -0700488
Jeff Kinga31cfe32022-12-13 05:52:58 -0500489 if (server_supports_v2("agent"))
Brandon Williamse52449b2018-03-15 10:31:21 -0700490 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
491
brian m. carlsonab672352020-05-25 19:59:16 +0000492 if (server_feature_v2("object-format", &hash_name)) {
493 int hash_algo = hash_algo_by_name(hash_name);
494 if (hash_algo == GIT_HASH_UNKNOWN)
495 die(_("unknown object format '%s' specified by server"), hash_name);
496 reader->hash_algo = &hash_algos[hash_algo];
497 packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
brian m. carlson9de0dd32020-05-25 19:59:17 +0000498 } else {
499 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
brian m. carlsonab672352020-05-25 19:59:16 +0000500 }
Ævar Arnfjörð Bjarmason86f4e312022-05-16 20:10:58 +0000501}
502
Ævar Arnfjörð Bjarmason0cfde742022-12-22 15:14:09 +0000503int get_remote_bundle_uri(int fd_out, struct packet_reader *reader,
504 struct bundle_list *bundles, int stateless_rpc)
505{
506 int line_nr = 1;
507
508 /* Assert bundle-uri support */
Junio C Hamano0903d8b2023-01-02 21:37:18 +0900509 ensure_server_supports_v2("bundle-uri");
Ævar Arnfjörð Bjarmason0cfde742022-12-22 15:14:09 +0000510
511 /* (Re-)send capabilities */
512 send_capabilities(fd_out, reader);
513
514 /* Send command */
515 packet_write_fmt(fd_out, "command=bundle-uri\n");
516 packet_delim(fd_out);
517
518 packet_flush(fd_out);
519
520 /* Process response from server */
521 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
522 const char *line = reader->line;
523 line_nr++;
524
525 if (!bundle_uri_parse_line(bundles, line))
526 continue;
527
528 return error(_("error on bundle-uri response line %d: %s"),
529 line_nr, line);
530 }
531
532 if (reader->status != PACKET_READ_FLUSH)
533 return error(_("expected flush after bundle-uri listing"));
534
535 /*
536 * Might die(), but obscure enough that that's OK, e.g. in
537 * serve.c we'll call BUG() on its equivalent (the
538 * PACKET_READ_RESPONSE_END check).
539 */
540 check_stateless_delimiter(stateless_rpc, reader,
541 _("expected response end packet after ref listing"));
542
543 return 0;
544}
545
Ævar Arnfjörð Bjarmason86f4e312022-05-16 20:10:58 +0000546struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
547 struct ref **list, int for_push,
548 struct transport_ls_refs_options *transport_options,
549 const struct string_list *server_options,
550 int stateless_rpc)
551{
552 int i;
553 struct strvec *ref_prefixes = transport_options ?
554 &transport_options->ref_prefixes : NULL;
555 const char **unborn_head_target = transport_options ?
556 &transport_options->unborn_head_target : NULL;
557 *list = NULL;
558
Jeff Kinga31cfe32022-12-13 05:52:58 -0500559 ensure_server_supports_v2("ls-refs");
560 packet_write_fmt(fd_out, "command=ls-refs\n");
Ævar Arnfjörð Bjarmason86f4e312022-05-16 20:10:58 +0000561
562 /* Send capabilities */
563 send_capabilities(fd_out, reader);
brian m. carlsonab672352020-05-25 19:59:16 +0000564
Jeff Kinga31cfe32022-12-13 05:52:58 -0500565 if (server_options && server_options->nr) {
566 ensure_server_supports_v2("server-option");
Brandon Williamsff473222018-04-23 15:46:23 -0700567 for (i = 0; i < server_options->nr; i++)
568 packet_write_fmt(fd_out, "server-option=%s",
569 server_options->items[i].string);
Jeff Kinga31cfe32022-12-13 05:52:58 -0500570 }
Brandon Williamsff473222018-04-23 15:46:23 -0700571
Brandon Williamse52449b2018-03-15 10:31:21 -0700572 packet_delim(fd_out);
573 /* When pushing we don't want to request the peeled tags */
574 if (!for_push)
575 packet_write_fmt(fd_out, "peel\n");
576 packet_write_fmt(fd_out, "symrefs\n");
Jonathan Tan4f37d452021-02-05 12:48:49 -0800577 if (server_supports_feature("ls-refs", "unborn", 0))
578 packet_write_fmt(fd_out, "unborn\n");
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400579 for (i = 0; ref_prefixes && i < ref_prefixes->nr; i++) {
Brandon Williamse52449b2018-03-15 10:31:21 -0700580 packet_write_fmt(fd_out, "ref-prefix %s\n",
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400581 ref_prefixes->v[i]);
Brandon Williamse52449b2018-03-15 10:31:21 -0700582 }
583 packet_flush(fd_out);
584
585 /* Process response from server */
586 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
Jonathan Tan4f37d452021-02-05 12:48:49 -0800587 if (!process_ref_v2(reader, &list, unborn_head_target))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200588 die(_("invalid ls-refs response: %s"), reader->line);
Brandon Williamse52449b2018-03-15 10:31:21 -0700589 }
590
591 if (reader->status != PACKET_READ_FLUSH)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200592 die(_("expected flush after ref listing"));
Brandon Williamse52449b2018-03-15 10:31:21 -0700593
Denton Liub0df0c12020-05-19 06:54:00 -0400594 check_stateless_delimiter(stateless_rpc, reader,
595 _("expected response end packet after ref listing"));
596
Brandon Williamse52449b2018-03-15 10:31:21 -0700597 return list;
598}
599
Jeff King7ce4c8f2023-04-14 17:25:20 -0400600const char *parse_feature_value(const char *feature_list, const char *feature, size_t *lenp, size_t *offset)
Junio C Hamanof47182c2012-01-08 22:06:19 +0100601{
Jeff Kingaa962fe2023-04-14 17:24:16 -0400602 const char *orig_start = feature_list;
Jeff King7ce4c8f2023-04-14 17:25:20 -0400603 size_t len;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100604
605 if (!feature_list)
606 return NULL;
607
608 len = strlen(feature);
brian m. carlson2c6a4032020-05-25 19:58:52 +0000609 if (offset)
610 feature_list += *offset;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100611 while (*feature_list) {
612 const char *found = strstr(feature_list, feature);
613 if (!found)
614 return NULL;
Jeff King94427102012-08-13 21:59:27 -0400615 if (feature_list == found || isspace(found[-1])) {
616 const char *value = found + len;
617 /* feature with no value (e.g., "thin-pack") */
618 if (!*value || isspace(*value)) {
619 if (lenp)
620 *lenp = 0;
Andrzej Hunt44d2aec2021-09-26 15:58:33 +0000621 if (offset)
Jeff Kingaa962fe2023-04-14 17:24:16 -0400622 *offset = found + len - orig_start;
Jeff King94427102012-08-13 21:59:27 -0400623 return value;
624 }
625 /* feature with a value (e.g., "agent=git/1.2.3") */
626 else if (*value == '=') {
Jeff King7ce4c8f2023-04-14 17:25:20 -0400627 size_t end;
brian m. carlson2c6a4032020-05-25 19:58:52 +0000628
Jeff King94427102012-08-13 21:59:27 -0400629 value++;
brian m. carlson2c6a4032020-05-25 19:58:52 +0000630 end = strcspn(value, " \t\n");
Jeff King94427102012-08-13 21:59:27 -0400631 if (lenp)
brian m. carlson2c6a4032020-05-25 19:58:52 +0000632 *lenp = end;
633 if (offset)
Jeff Kingaa962fe2023-04-14 17:24:16 -0400634 *offset = value + end - orig_start;
Jeff King94427102012-08-13 21:59:27 -0400635 return value;
636 }
637 /*
638 * otherwise we matched a substring of another feature;
639 * keep looking
640 */
641 }
Junio C Hamanof47182c2012-01-08 22:06:19 +0100642 feature_list = found + 1;
643 }
644 return NULL;
Johannes Schindelin211b5f92005-10-28 04:48:54 +0200645}
646
brian m. carlson122037c2020-05-25 19:58:56 +0000647int server_supports_hash(const char *desired, int *feature_supported)
648{
Jeff King7ce4c8f2023-04-14 17:25:20 -0400649 size_t offset = 0;
650 size_t len;
brian m. carlson122037c2020-05-25 19:58:56 +0000651 const char *hash;
652
653 hash = next_server_feature_value("object-format", &len, &offset);
654 if (feature_supported)
655 *feature_supported = !!hash;
656 if (!hash) {
657 hash = hash_algos[GIT_HASH_SHA1].name;
658 len = strlen(hash);
659 }
660 while (hash) {
661 if (!xstrncmpz(desired, hash, len))
662 return 1;
663
664 hash = next_server_feature_value("object-format", &len, &offset);
665 }
666 return 0;
667}
668
Jeff King94427102012-08-13 21:59:27 -0400669int parse_feature_request(const char *feature_list, const char *feature)
670{
brian m. carlson2c6a4032020-05-25 19:58:52 +0000671 return !!parse_feature_value(feature_list, feature, NULL, NULL);
672}
673
Jeff King7ce4c8f2023-04-14 17:25:20 -0400674static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset)
brian m. carlson2c6a4032020-05-25 19:58:52 +0000675{
676 return parse_feature_value(server_capabilities_v1, feature, len, offset);
Jeff King94427102012-08-13 21:59:27 -0400677}
678
Jeff King7ce4c8f2023-04-14 17:25:20 -0400679const char *server_feature_value(const char *feature, size_t *len)
Jeff King94427102012-08-13 21:59:27 -0400680{
brian m. carlson2c6a4032020-05-25 19:58:52 +0000681 return parse_feature_value(server_capabilities_v1, feature, len, NULL);
Jeff King94427102012-08-13 21:59:27 -0400682}
683
684int server_supports(const char *feature)
685{
686 return !!server_feature_value(feature, NULL);
687}
688
Linus Torvalds2386d652005-07-13 18:46:20 -0700689enum protocol {
690 PROTO_LOCAL = 1,
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100691 PROTO_FILE,
Linus Torvalds2386d652005-07-13 18:46:20 -0700692 PROTO_SSH,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000693 PROTO_GIT
Linus Torvalds2386d652005-07-13 18:46:20 -0700694};
695
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100696int url_is_local_not_ssh(const char *url)
697{
698 const char *colon = strchr(url, ':');
699 const char *slash = strchr(url, '/');
700 return !colon || (slash && slash < colon) ||
Johannes Schindelinf82a97e2019-09-06 00:09:10 +0200701 (has_dos_drive_prefix(url) && is_valid_path(url));
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100702}
703
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +0100704static const char *prot_name(enum protocol protocol)
705{
706 switch (protocol) {
707 case PROTO_LOCAL:
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100708 case PROTO_FILE:
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +0100709 return "file";
710 case PROTO_SSH:
711 return "ssh";
712 case PROTO_GIT:
713 return "git";
714 default:
Tobias Klauser83e6bda2015-09-24 14:44:49 +0200715 return "unknown protocol";
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +0100716 }
717}
718
Linus Torvalds2386d652005-07-13 18:46:20 -0700719static enum protocol get_protocol(const char *name)
720{
721 if (!strcmp(name, "ssh"))
722 return PROTO_SSH;
723 if (!strcmp(name, "git"))
724 return PROTO_GIT;
Carlos Martín Nieto07c77822016-02-15 15:29:06 +0100725 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
Linus Torvaldsc05186c2005-10-14 17:14:56 -0700726 return PROTO_SSH;
Carlos Martín Nieto07c77822016-02-15 15:29:06 +0100727 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
Linus Torvaldsc05186c2005-10-14 17:14:56 -0700728 return PROTO_SSH;
Linus Torvalds72a4f4b2007-08-01 10:03:37 -0700729 if (!strcmp(name, "file"))
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100730 return PROTO_FILE;
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200731 die(_("protocol '%s' is not supported"), name);
Linus Torvalds2386d652005-07-13 18:46:20 -0700732}
733
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100734static char *host_end(char **hoststart, int removebrackets)
735{
736 char *host = *hoststart;
737 char *end;
738 char *start = strstr(host, "@[");
739 if (start)
740 start++; /* Jump over '@' */
741 else
742 start = host;
743 if (start[0] == '[') {
744 end = strchr(start + 1, ']');
745 if (end) {
746 if (removebrackets) {
747 *end = 0;
748 memmove(start, start + 1, end - start);
749 end++;
750 }
751 } else
752 end = host;
753 } else
754 end = host;
755 return end;
756}
757
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400758#define STR_(s) # s
759#define STR(s) STR_(s)
Linus Torvalds2386d652005-07-13 18:46:20 -0700760
Michael Lukashov72a534d2010-02-17 20:56:02 +0000761static void get_host_and_port(char **host, const char **port)
762{
763 char *colon, *end;
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100764 end = host_end(host, 1);
Michael Lukashov72a534d2010-02-17 20:56:02 +0000765 colon = strchr(end, ':');
Michael Lukashov72a534d2010-02-17 20:56:02 +0000766 if (colon) {
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100767 long portnr = strtol(colon + 1, &end, 10);
768 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
769 *colon = 0;
770 *port = colon + 1;
Torsten Bögershausen6b6c5f72015-04-07 22:03:25 +0200771 } else if (!colon[1]) {
772 *colon = 0;
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100773 }
Michael Lukashov72a534d2010-02-17 20:56:02 +0000774 }
775}
776
Eric Wonge47a8582011-12-06 04:39:36 +0000777static void enable_keepalive(int sockfd)
778{
779 int ka = 1;
780
781 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200782 error_errno(_("unable to set SO_KEEPALIVE on socket"));
Eric Wonge47a8582011-12-06 04:39:36 +0000783}
784
hpa49744d62005-09-28 16:52:21 -0700785#ifndef NO_IPV6
hpa4c505f72005-09-28 16:37:58 -0700786
Alex Riesenba505322007-05-23 23:34:27 +0200787static const char *ai_name(const struct addrinfo *ai)
788{
Benjamin Kramer785a9852009-04-24 14:16:41 +0200789 static char addr[NI_MAXHOST];
790 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
791 NI_NUMERICHOST) != 0)
Jeff King5096d492015-09-24 17:06:08 -0400792 xsnprintf(addr, sizeof(addr), "(unknown)");
Benjamin Kramer785a9852009-04-24 14:16:41 +0200793
Alex Riesenba505322007-05-23 23:34:27 +0200794 return addr;
795}
796
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500797/*
798 * Returns a connected socket() fd, or else die()s.
799 */
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300800static int git_tcp_connect_sock(char *host, int flags)
Linus Torvalds2386d652005-07-13 18:46:20 -0700801{
Dave Zarzycki63a995b2011-07-12 09:28:34 -0700802 struct strbuf error_message = STRBUF_INIT;
803 int sockfd = -1;
Timo Hirvonen554fe202006-06-28 12:04:39 +0300804 const char *port = STR(DEFAULT_GIT_PORT);
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400805 struct addrinfo hints, *ai0, *ai;
806 int gai;
Alex Riesenba505322007-05-23 23:34:27 +0200807 int cnt = 0;
Linus Torvalds2386d652005-07-13 18:46:20 -0700808
Michael Lukashov72a534d2010-02-17 20:56:02 +0000809 get_host_and_port(&host, &port);
810 if (!*port)
811 port = "<none>";
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400812
813 memset(&hints, 0, sizeof(hints));
Eric Wongc915f112016-02-03 04:09:14 +0000814 if (flags & CONNECT_IPV4)
815 hints.ai_family = AF_INET;
816 else if (flags & CONNECT_IPV6)
817 hints.ai_family = AF_INET6;
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400818 hints.ai_socktype = SOCK_STREAM;
819 hints.ai_protocol = IPPROTO_TCP;
820
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300821 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200822 fprintf(stderr, _("Looking up %s ... "), host);
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300823
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400824 gai = getaddrinfo(host, port, &hints, &ai);
825 if (gai)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200826 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400827
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300828 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200829 /* TRANSLATORS: this is the end of "Looking up %s ... " */
830 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300831
Erik Faye-Lunde08afec2011-08-01 13:16:09 +0200832 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500833 sockfd = socket(ai->ai_family,
834 ai->ai_socktype, ai->ai_protocol);
Dave Zarzycki63a995b2011-07-12 09:28:34 -0700835 if ((sockfd < 0) ||
836 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
837 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
838 host, cnt, ai_name(ai), strerror(errno));
839 if (0 <= sockfd)
840 close(sockfd);
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400841 sockfd = -1;
842 continue;
Linus Torvalds2386d652005-07-13 18:46:20 -0700843 }
Alex Riesenba505322007-05-23 23:34:27 +0200844 if (flags & CONNECT_VERBOSE)
845 fprintf(stderr, "%s ", ai_name(ai));
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400846 break;
Linus Torvalds2386d652005-07-13 18:46:20 -0700847 }
848
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400849 freeaddrinfo(ai0);
Linus Torvalds2386d652005-07-13 18:46:20 -0700850
Linus Torvalds2386d652005-07-13 18:46:20 -0700851 if (sockfd < 0)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200852 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400853
Eric Wonge47a8582011-12-06 04:39:36 +0000854 enable_keepalive(sockfd);
855
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300856 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200857 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
858 fprintf_ln(stderr, _("done."));
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300859
Dave Zarzycki63a995b2011-07-12 09:28:34 -0700860 strbuf_release(&error_message);
861
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500862 return sockfd;
Linus Torvalds2386d652005-07-13 18:46:20 -0700863}
864
hpa49744d62005-09-28 16:52:21 -0700865#else /* NO_IPV6 */
hpa4c505f72005-09-28 16:37:58 -0700866
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500867/*
868 * Returns a connected socket() fd, or else die()s.
869 */
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300870static int git_tcp_connect_sock(char *host, int flags)
hpa4c505f72005-09-28 16:37:58 -0700871{
Erik Faye-Lund7203a2d2011-08-01 13:16:10 +0200872 struct strbuf error_message = STRBUF_INIT;
873 int sockfd = -1;
Michael Lukashov72a534d2010-02-17 20:56:02 +0000874 const char *port = STR(DEFAULT_GIT_PORT);
875 char *ep;
hpa4c505f72005-09-28 16:37:58 -0700876 struct hostent *he;
877 struct sockaddr_in sa;
878 char **ap;
879 unsigned int nport;
Alex Riesenba505322007-05-23 23:34:27 +0200880 int cnt;
hpa4c505f72005-09-28 16:37:58 -0700881
Michael Lukashov72a534d2010-02-17 20:56:02 +0000882 get_host_and_port(&host, &port);
hpa4c505f72005-09-28 16:37:58 -0700883
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300884 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200885 fprintf(stderr, _("Looking up %s ... "), host);
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300886
hpa4c505f72005-09-28 16:37:58 -0700887 he = gethostbyname(host);
888 if (!he)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200889 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
hpa4c505f72005-09-28 16:37:58 -0700890 nport = strtoul(port, &ep, 10);
891 if ( ep == port || *ep ) {
892 /* Not numeric */
893 struct servent *se = getservbyname(port,"tcp");
894 if ( !se )
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200895 die(_("unknown port %s"), port);
hpa4c505f72005-09-28 16:37:58 -0700896 nport = se->s_port;
897 }
898
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300899 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200900 /* TRANSLATORS: this is the end of "Looking up %s ... " */
901 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300902
Alex Riesenba505322007-05-23 23:34:27 +0200903 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
hpa4c505f72005-09-28 16:37:58 -0700904 memset(&sa, 0, sizeof sa);
905 sa.sin_family = he->h_addrtype;
Peter Anvin6573faf2005-09-28 17:26:44 -0700906 sa.sin_port = htons(nport);
Paul Sericec6164212005-11-22 07:54:23 -0600907 memcpy(&sa.sin_addr, *ap, he->h_length);
hpa4c505f72005-09-28 16:37:58 -0700908
Erik Faye-Lund7203a2d2011-08-01 13:16:10 +0200909 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
910 if ((sockfd < 0) ||
911 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
912 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
Alex Riesenba505322007-05-23 23:34:27 +0200913 host,
914 cnt,
915 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
Erik Faye-Lund7203a2d2011-08-01 13:16:10 +0200916 strerror(errno));
917 if (0 <= sockfd)
918 close(sockfd);
hpa4c505f72005-09-28 16:37:58 -0700919 sockfd = -1;
920 continue;
921 }
Alex Riesenba505322007-05-23 23:34:27 +0200922 if (flags & CONNECT_VERBOSE)
923 fprintf(stderr, "%s ",
924 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
hpa4c505f72005-09-28 16:37:58 -0700925 break;
926 }
927
928 if (sockfd < 0)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200929 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
hpa4c505f72005-09-28 16:37:58 -0700930
Eric Wonge47a8582011-12-06 04:39:36 +0000931 enable_keepalive(sockfd);
932
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300933 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200934 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
935 fprintf_ln(stderr, _("done."));
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300936
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500937 return sockfd;
hpa4c505f72005-09-28 16:37:58 -0700938}
939
hpa49744d62005-09-28 16:52:21 -0700940#endif /* NO_IPV6 */
hpa4c505f72005-09-28 16:37:58 -0700941
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500942
Jonathan Nieder8e349782017-11-20 13:22:54 -0800943/*
944 * Dummy child_process returned by git_connect() if the transport protocol
945 * does not need fork(2).
946 */
947static struct child_process no_fork = CHILD_PROCESS_INIT;
948
949int git_connection_is_socket(struct child_process *conn)
950{
951 return conn == &no_fork;
952}
953
954static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500955{
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300956 int sockfd = git_tcp_connect_sock(host, flags);
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500957
958 fd[0] = sockfd;
Junio C Hamanoec587fd2007-01-21 17:10:51 -0800959 fd[1] = dup(sockfd);
Jonathan Nieder8e349782017-11-20 13:22:54 -0800960
961 return &no_fork;
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500962}
963
964
David Rientjes96f1e582006-08-15 10:23:48 -0700965static char *git_proxy_command;
Paul Collinsf8014772005-11-04 14:57:16 +0000966
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100967static int git_proxy_command_options(const char *var, const char *value,
Glen Chooa4e7e312023-06-28 19:26:22 +0000968 const struct config_context *ctx, void *cb)
Paul Collinsf8014772005-11-04 14:57:16 +0000969{
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800970 if (!strcmp(var, "core.gitproxy")) {
YOSHIFUJI Hideaki / 吉藤英明c3df8562005-11-22 12:18:23 +0900971 const char *for_pos;
972 int matchlen = -1;
973 int hostlen;
Erik Faye-Lund15112c92009-03-11 02:38:12 +0000974 const char *rhost_name = cb;
975 int rhost_len = strlen(rhost_name);
YOSHIFUJI Hideaki / 吉藤英明c3df8562005-11-22 12:18:23 +0900976
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800977 if (git_proxy_command)
Paul Collinsf8014772005-11-04 14:57:16 +0000978 return 0;
Junio C Hamanoc64b9ad2008-02-11 10:52:15 -0800979 if (!value)
980 return config_error_nonbool(var);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800981 /* [core]
982 * ;# matches www.kernel.org as well
983 * gitproxy = netcatter-1 for kernel.org
984 * gitproxy = netcatter-2 for sample.xz
985 * gitproxy = netcatter-default
986 */
YOSHIFUJI Hideaki / 吉藤英明c3df8562005-11-22 12:18:23 +0900987 for_pos = strstr(value, " for ");
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800988 if (!for_pos)
989 /* matches everybody */
990 matchlen = strlen(value);
991 else {
992 hostlen = strlen(for_pos + 5);
993 if (rhost_len < hostlen)
994 matchlen = -1;
995 else if (!strncmp(for_pos + 5,
996 rhost_name + rhost_len - hostlen,
997 hostlen) &&
998 ((rhost_len == hostlen) ||
999 rhost_name[rhost_len - hostlen -1] == '.'))
1000 matchlen = for_pos - value;
1001 else
1002 matchlen = -1;
Paul Collinsf8014772005-11-04 14:57:16 +00001003 }
Junio C Hamanoe814bc42005-11-19 03:48:56 -08001004 if (0 <= matchlen) {
1005 /* core.gitproxy = none for kernel.org */
Junio C Hamanoa6080a02007-06-07 00:04:01 -07001006 if (matchlen == 4 &&
Junio C Hamanoe814bc42005-11-19 03:48:56 -08001007 !memcmp(value, "none", 4))
1008 matchlen = 0;
Pierre Habouzit182af832007-09-16 00:32:36 +02001009 git_proxy_command = xmemdupz(value, matchlen);
Junio C Hamanoe814bc42005-11-19 03:48:56 -08001010 }
1011 return 0;
Paul Collinsf8014772005-11-04 14:57:16 +00001012 }
1013
Glen Chooa4e7e312023-06-28 19:26:22 +00001014 return git_default_config(var, value, ctx, cb);
Paul Collinsf8014772005-11-04 14:57:16 +00001015}
1016
Junio C Hamanoe814bc42005-11-19 03:48:56 -08001017static int git_use_proxy(const char *host)
Paul Collinsf8014772005-11-04 14:57:16 +00001018{
1019 git_proxy_command = getenv("GIT_PROXY_COMMAND");
Erik Faye-Lund15112c92009-03-11 02:38:12 +00001020 git_config(git_proxy_command_options, (void*)host);
Junio C Hamanoe814bc42005-11-19 03:48:56 -08001021 return (git_proxy_command && *git_proxy_command);
Paul Collinsf8014772005-11-04 14:57:16 +00001022}
1023
Jeff King5cbf8242011-05-16 02:46:07 -04001024static struct child_process *git_proxy_connect(int fd[2], char *host)
Paul Collinsf8014772005-11-04 14:57:16 +00001025{
Timo Hirvonen554fe202006-06-28 12:04:39 +03001026 const char *port = STR(DEFAULT_GIT_PORT);
Jeff King5cbf8242011-05-16 02:46:07 -04001027 struct child_process *proxy;
Paul Collinsf8014772005-11-04 14:57:16 +00001028
Michael Lukashov72a534d2010-02-17 20:56:02 +00001029 get_host_and_port(&host, &port);
Paul Collinsf8014772005-11-04 14:57:16 +00001030
Jeff King3be4cf02017-07-28 15:26:50 -04001031 if (looks_like_command_line_option(host))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001032 die(_("strange hostname '%s' blocked"), host);
Jeff King3be4cf02017-07-28 15:26:50 -04001033 if (looks_like_command_line_option(port))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001034 die(_("strange port '%s' blocked"), port);
Jeff King3be4cf02017-07-28 15:26:50 -04001035
René Scharfe483bbd42014-08-19 21:10:48 +02001036 proxy = xmalloc(sizeof(*proxy));
1037 child_process_init(proxy);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001038 strvec_push(&proxy->args, git_proxy_command);
1039 strvec_push(&proxy->args, host);
1040 strvec_push(&proxy->args, port);
Jeff King5cbf8242011-05-16 02:46:07 -04001041 proxy->in = -1;
1042 proxy->out = -1;
1043 if (start_command(proxy))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001044 die(_("cannot start proxy %s"), git_proxy_command);
Jeff King5cbf8242011-05-16 02:46:07 -04001045 fd[0] = proxy->out; /* read from proxy stdout */
1046 fd[1] = proxy->in; /* write to proxy stdin */
1047 return proxy;
Paul Collinsf8014772005-11-04 14:57:16 +00001048}
1049
Torsten Bögershausen86ceb332015-02-21 16:52:48 +01001050static char *get_port(char *host)
Luben Tuikov2e776662007-09-01 02:36:31 -07001051{
1052 char *end;
Torsten Bögershausen86ceb332015-02-21 16:52:48 +01001053 char *p = strchr(host, ':');
1054
Luben Tuikov2e776662007-09-01 02:36:31 -07001055 if (p) {
René Scharfe8f148252008-12-21 02:12:11 +01001056 long port = strtol(p + 1, &end, 10);
1057 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
Torsten Bögershausen86ceb332015-02-21 16:52:48 +01001058 *p = '\0';
1059 return p+1;
Luben Tuikov2e776662007-09-01 02:36:31 -07001060 }
1061 }
1062
1063 return NULL;
1064}
1065
Linus Torvaldsf7192592005-07-04 11:57:58 -07001066/*
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001067 * Extract protocol and relevant parts from the specified connection URL.
1068 * The caller must free() the returned strings.
Linus Torvaldsf7192592005-07-04 11:57:58 -07001069 */
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001070static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
Torsten Bögershausen83b05872013-11-28 20:49:54 +01001071 char **ret_path)
Linus Torvaldsf7192592005-07-04 11:57:58 -07001072{
Jeff King9d2e9422010-05-23 05:19:44 -04001073 char *url;
Benjamin Kramer8e76bf32009-03-13 13:51:33 +01001074 char *host, *path;
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +09001075 char *end;
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +01001076 int separator = '/';
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +01001077 enum protocol protocol = PROTO_LOCAL;
Junio C Hamanof0b73672006-06-19 18:25:21 -07001078
Jeff King9d2e9422010-05-23 05:19:44 -04001079 if (is_url(url_orig))
1080 url = url_decode(url_orig);
1081 else
1082 url = xstrdup(url_orig);
1083
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +01001084 host = strstr(url, "://");
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04001085 if (host) {
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +01001086 *host = '\0';
1087 protocol = get_protocol(url);
1088 host += 3;
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +09001089 } else {
Linus Torvaldsf7192592005-07-04 11:57:58 -07001090 host = url;
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +01001091 if (!url_is_local_not_ssh(url)) {
1092 protocol = PROTO_SSH;
1093 separator = ':';
1094 }
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +09001095 }
1096
Ilari Liusvaara9aa50532010-01-26 20:24:42 +02001097 /*
Torsten Bögershausen83b05872013-11-28 20:49:54 +01001098 * Don't do destructive transforms as protocol code does
1099 * '[]' unwrapping in get_host_and_port()
Ilari Liusvaara9aa50532010-01-26 20:24:42 +02001100 */
Torsten Bögershausen86ceb332015-02-21 16:52:48 +01001101 end = host_end(&host, 0);
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +09001102
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +01001103 if (protocol == PROTO_LOCAL)
Linus Torvalds72a4f4b2007-08-01 10:03:37 -07001104 path = end;
Torsten Bögershausenebb8d2c2019-08-24 15:07:59 -07001105 else if (protocol == PROTO_FILE && *host != '/' &&
1106 !has_dos_drive_prefix(host) &&
1107 offset_1st_component(host - 2) > 1)
1108 path = host - 2; /* include the leading "//" */
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +01001109 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
1110 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1111 else
1112 path = strchr(end, separator);
Linus Torvalds2386d652005-07-13 18:46:20 -07001113
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +01001114 if (!path || !*path)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001115 die(_("no path specified; see 'git help pull' for valid url syntax"));
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +01001116
1117 /*
1118 * null-terminate hostname and point path to ~ for URL's like this:
1119 * ssh://host.xz/~user/repo
1120 */
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +01001121
1122 end = path; /* Need to \0 terminate host here */
1123 if (separator == ':')
1124 path++; /* path starts after ':' */
1125 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +01001126 if (path[1] == '~')
1127 path++;
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +01001128 }
1129
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +01001130 path = xstrdup(path);
1131 *end = '\0';
1132
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001133 *ret_host = xstrdup(host);
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +01001134 *ret_path = path;
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001135 free(url);
1136 return protocol;
1137}
1138
Nguyễn Thái Ngọc Duy3c8ede32016-06-26 13:16:35 +02001139static const char *get_ssh_command(void)
1140{
1141 const char *ssh;
1142
1143 if ((ssh = getenv("GIT_SSH_COMMAND")))
1144 return ssh;
1145
Jeff Kingf1de9812020-08-14 12:17:36 -04001146 if (!git_config_get_string_tmp("core.sshcommand", &ssh))
Nguyễn Thái Ngọc Duy3c8ede32016-06-26 13:16:35 +02001147 return ssh;
1148
1149 return NULL;
1150}
1151
Brandon Williams94b8ae52017-10-16 10:55:31 -07001152enum ssh_variant {
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001153 VARIANT_AUTO,
Brandon Williams94b8ae52017-10-16 10:55:31 -07001154 VARIANT_SIMPLE,
1155 VARIANT_SSH,
1156 VARIANT_PLINK,
1157 VARIANT_PUTTY,
1158 VARIANT_TORTOISEPLINK,
1159};
Junio C Hamano486c8e82017-02-09 09:20:25 -08001160
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001161static void override_ssh_variant(enum ssh_variant *ssh_variant)
Brandon Williams94b8ae52017-10-16 10:55:31 -07001162{
1163 const char *variant = getenv("GIT_SSH_VARIANT");
1164
Jeff Kingf1de9812020-08-14 12:17:36 -04001165 if (!variant && git_config_get_string_tmp("ssh.variant", &variant))
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001166 return;
Junio C Hamano486c8e82017-02-09 09:20:25 -08001167
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001168 if (!strcmp(variant, "auto"))
1169 *ssh_variant = VARIANT_AUTO;
1170 else if (!strcmp(variant, "plink"))
Brandon Williams94b8ae52017-10-16 10:55:31 -07001171 *ssh_variant = VARIANT_PLINK;
1172 else if (!strcmp(variant, "putty"))
1173 *ssh_variant = VARIANT_PUTTY;
1174 else if (!strcmp(variant, "tortoiseplink"))
1175 *ssh_variant = VARIANT_TORTOISEPLINK;
1176 else if (!strcmp(variant, "simple"))
1177 *ssh_variant = VARIANT_SIMPLE;
1178 else
1179 *ssh_variant = VARIANT_SSH;
Junio C Hamano486c8e82017-02-09 09:20:25 -08001180}
1181
Brandon Williams94b8ae52017-10-16 10:55:31 -07001182static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1183 int is_cmdline)
Junio C Hamano486c8e82017-02-09 09:20:25 -08001184{
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001185 enum ssh_variant ssh_variant = VARIANT_AUTO;
Junio C Hamano486c8e82017-02-09 09:20:25 -08001186 const char *variant;
Johannes Schindeline2824e42017-02-01 13:01:10 +01001187 char *p = NULL;
1188
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001189 override_ssh_variant(&ssh_variant);
1190
1191 if (ssh_variant != VARIANT_AUTO)
Brandon Williams94b8ae52017-10-16 10:55:31 -07001192 return ssh_variant;
Junio C Hamano486c8e82017-02-09 09:20:25 -08001193
1194 if (!is_cmdline) {
Johannes Schindeline2824e42017-02-01 13:01:10 +01001195 p = xstrdup(ssh_command);
1196 variant = basename(p);
1197 } else {
1198 const char **ssh_argv;
1199
1200 p = xstrdup(ssh_command);
Jeff King22e5ae52017-04-10 20:30:23 -04001201 if (split_cmdline(p, &ssh_argv) > 0) {
Johannes Schindeline2824e42017-02-01 13:01:10 +01001202 variant = basename((char *)ssh_argv[0]);
1203 /*
1204 * At this point, variant points into the buffer
1205 * referenced by p, hence we do not need ssh_argv
1206 * any longer.
1207 */
1208 free(ssh_argv);
Jeff King5d2993b2017-04-20 16:21:58 -04001209 } else {
1210 free(p);
Brandon Williams94b8ae52017-10-16 10:55:31 -07001211 return ssh_variant;
Jeff King5d2993b2017-04-20 16:21:58 -04001212 }
Johannes Schindeline2824e42017-02-01 13:01:10 +01001213 }
1214
Brandon Williams94b8ae52017-10-16 10:55:31 -07001215 if (!strcasecmp(variant, "ssh") ||
1216 !strcasecmp(variant, "ssh.exe"))
1217 ssh_variant = VARIANT_SSH;
1218 else if (!strcasecmp(variant, "plink") ||
1219 !strcasecmp(variant, "plink.exe"))
1220 ssh_variant = VARIANT_PLINK;
Johannes Schindeline2824e42017-02-01 13:01:10 +01001221 else if (!strcasecmp(variant, "tortoiseplink") ||
Brandon Williams94b8ae52017-10-16 10:55:31 -07001222 !strcasecmp(variant, "tortoiseplink.exe"))
1223 ssh_variant = VARIANT_TORTOISEPLINK;
1224
Johannes Schindeline2824e42017-02-01 13:01:10 +01001225 free(p);
Brandon Williams94b8ae52017-10-16 10:55:31 -07001226 return ssh_variant;
Johannes Schindeline2824e42017-02-01 13:01:10 +01001227}
1228
Linus Torvaldsf7192592005-07-04 11:57:58 -07001229/*
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001230 * Open a connection using Git's native protocol.
1231 *
1232 * The caller is responsible for freeing hostandport, but this function may
1233 * modify it (for example, to truncate it to remove the port part).
1234 */
1235static struct child_process *git_connect_git(int fd[2], char *hostandport,
1236 const char *path, const char *prog,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001237 enum protocol_version version,
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001238 int flags)
1239{
1240 struct child_process *conn;
1241 struct strbuf request = STRBUF_INIT;
1242 /*
1243 * Set up virtual host information based on where we will
1244 * connect, unless the user has overridden us in
1245 * the environment.
1246 */
1247 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1248 if (target_host)
1249 target_host = xstrdup(target_host);
1250 else
1251 target_host = xstrdup(hostandport);
1252
1253 transport_check_allowed("git");
Jeff Kinga02ea572021-01-07 04:43:58 -05001254 if (strchr(target_host, '\n') || strchr(path, '\n'))
1255 die(_("newline is forbidden in git:// hosts and repo paths"));
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001256
Jonathan Nieder233cd282017-11-20 14:04:58 -08001257 /*
1258 * These underlying connection commands die() if they
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001259 * cannot connect.
1260 */
1261 if (git_use_proxy(hostandport))
1262 conn = git_proxy_connect(fd, hostandport);
1263 else
1264 conn = git_tcp_connect(fd, hostandport, flags);
1265 /*
1266 * Separate original protocol components prog and path
1267 * from extended host header with a NUL byte.
1268 *
1269 * Note: Do not add any other headers here! Doing so
1270 * will cause older git-daemon servers to crash.
1271 */
1272 strbuf_addf(&request,
1273 "%s %s%chost=%s%c",
1274 prog, path, 0,
1275 target_host, 0);
1276
1277 /* If using a new version put that stuff here after a second null byte */
Brandon Williams40fc51e2018-03-15 10:31:30 -07001278 if (version > 0) {
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001279 strbuf_addch(&request, '\0');
1280 strbuf_addf(&request, "version=%d%c",
Brandon Williams40fc51e2018-03-15 10:31:30 -07001281 version, '\0');
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001282 }
1283
1284 packet_write(fd[1], request.buf, request.len);
1285
1286 free(target_host);
1287 strbuf_release(&request);
1288 return conn;
1289}
1290
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001291/*
1292 * Append the appropriate environment variables to `env` and options to
1293 * `args` for running ssh in Git's SSH-tunneled transport.
1294 */
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001295static void push_ssh_options(struct strvec *args, struct strvec *env,
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001296 enum ssh_variant variant, const char *port,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001297 enum protocol_version version, int flags)
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001298{
1299 if (variant == VARIANT_SSH &&
Brandon Williams40fc51e2018-03-15 10:31:30 -07001300 version > 0) {
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001301 strvec_push(args, "-o");
1302 strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1303 strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
Jeff Kingf6d89422020-07-28 16:26:31 -04001304 version);
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001305 }
1306
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001307 if (flags & CONNECT_IPV4) {
1308 switch (variant) {
1309 case VARIANT_AUTO:
1310 BUG("VARIANT_AUTO passed to push_ssh_options");
1311 case VARIANT_SIMPLE:
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001312 die(_("ssh variant 'simple' does not support -4"));
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001313 case VARIANT_SSH:
1314 case VARIANT_PLINK:
1315 case VARIANT_PUTTY:
1316 case VARIANT_TORTOISEPLINK:
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001317 strvec_push(args, "-4");
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001318 }
1319 } else if (flags & CONNECT_IPV6) {
1320 switch (variant) {
1321 case VARIANT_AUTO:
1322 BUG("VARIANT_AUTO passed to push_ssh_options");
1323 case VARIANT_SIMPLE:
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001324 die(_("ssh variant 'simple' does not support -6"));
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001325 case VARIANT_SSH:
1326 case VARIANT_PLINK:
1327 case VARIANT_PUTTY:
1328 case VARIANT_TORTOISEPLINK:
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001329 strvec_push(args, "-6");
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001330 }
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001331 }
1332
1333 if (variant == VARIANT_TORTOISEPLINK)
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001334 strvec_push(args, "-batch");
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001335
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001336 if (port) {
1337 switch (variant) {
1338 case VARIANT_AUTO:
1339 BUG("VARIANT_AUTO passed to push_ssh_options");
1340 case VARIANT_SIMPLE:
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001341 die(_("ssh variant 'simple' does not support setting port"));
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001342 case VARIANT_SSH:
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001343 strvec_push(args, "-p");
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001344 break;
1345 case VARIANT_PLINK:
1346 case VARIANT_PUTTY:
1347 case VARIANT_TORTOISEPLINK:
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001348 strvec_push(args, "-P");
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001349 }
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001350
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001351 strvec_push(args, port);
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001352 }
1353}
1354
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001355/* Prepare a child_process for use by Git's SSH-tunneled transport. */
1356static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001357 const char *port, enum protocol_version version,
1358 int flags)
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001359{
1360 const char *ssh;
1361 enum ssh_variant variant;
1362
1363 if (looks_like_command_line_option(ssh_host))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001364 die(_("strange hostname '%s' blocked"), ssh_host);
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001365
1366 ssh = get_ssh_command();
1367 if (ssh) {
1368 variant = determine_ssh_variant(ssh, 1);
1369 } else {
1370 /*
1371 * GIT_SSH is the no-shell version of
1372 * GIT_SSH_COMMAND (and must remain so for
1373 * historical compatibility).
1374 */
1375 conn->use_shell = 0;
1376
1377 ssh = getenv("GIT_SSH");
1378 if (!ssh)
1379 ssh = "ssh";
1380 variant = determine_ssh_variant(ssh, 0);
1381 }
1382
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001383 if (variant == VARIANT_AUTO) {
1384 struct child_process detect = CHILD_PROCESS_INIT;
1385
1386 detect.use_shell = conn->use_shell;
1387 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1388
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001389 strvec_push(&detect.args, ssh);
1390 strvec_push(&detect.args, "-G");
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001391 push_ssh_options(&detect.args, &detect.env,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001392 VARIANT_SSH, port, version, flags);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001393 strvec_push(&detect.args, ssh_host);
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001394
1395 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1396 }
1397
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001398 strvec_push(&conn->args, ssh);
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001399 push_ssh_options(&conn->args, &conn->env, variant, port, version,
1400 flags);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001401 strvec_push(&conn->args, ssh_host);
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001402}
1403
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001404/*
Jonathan Nieder8e349782017-11-20 13:22:54 -08001405 * This returns the dummy child_process `no_fork` if the transport protocol
1406 * does not need fork(2), or a struct child_process object if it does. Once
1407 * done, finish the connection with finish_connect() with the value returned
1408 * from this function (it is safe to call finish_connect() with NULL to
1409 * support the former case).
Linus Torvaldsf7192592005-07-04 11:57:58 -07001410 *
1411 * If it returns, the connect is successful; it just dies on errors (this
1412 * will hopefully be changed in a libification effort, to return NULL when
1413 * the connection failed).
1414 */
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001415struct child_process *git_connect(int fd[2], const char *url,
Jeff Kingeaa0fd62023-03-17 15:08:51 -04001416 const char *name,
Linus Torvaldsf7192592005-07-04 11:57:58 -07001417 const char *prog, int flags)
1418{
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001419 char *hostandport, *path;
Jonathan Nieder8e349782017-11-20 13:22:54 -08001420 struct child_process *conn;
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001421 enum protocol protocol;
Brandon Williams40fc51e2018-03-15 10:31:30 -07001422 enum protocol_version version = get_protocol_version_config();
Linus Torvaldsf7192592005-07-04 11:57:58 -07001423
Brandon Williams1aa8dded2018-03-15 10:31:31 -07001424 /*
1425 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
Jeff Kingeaa0fd62023-03-17 15:08:51 -04001426 * to perform any operation that doesn't involve upload-pack (i.e., a
1427 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
1428 * to do anything else (like push or remote archive) via v2.
Brandon Williams1aa8dded2018-03-15 10:31:31 -07001429 */
Jeff Kingeaa0fd62023-03-17 15:08:51 -04001430 if (version == protocol_v2 && strcmp("git-upload-pack", name))
Brandon Williams1aa8dded2018-03-15 10:31:31 -07001431 version = protocol_v0;
1432
Linus Torvaldsf7192592005-07-04 11:57:58 -07001433 /* Without this we cannot rely on waitpid() to tell
1434 * what happened to our children.
1435 */
1436 signal(SIGCHLD, SIG_DFL);
1437
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001438 protocol = parse_connect_url(url, &hostandport, &path);
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001439 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +01001440 printf("Diag: url=%s\n", url ? url : "NULL");
1441 printf("Diag: protocol=%s\n", prot_name(protocol));
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001442 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +01001443 printf("Diag: path=%s\n", path ? path : "NULL");
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001444 conn = NULL;
1445 } else if (protocol == PROTO_GIT) {
Brandon Williams40fc51e2018-03-15 10:31:30 -07001446 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
Jeff Hostetlerabd81a32019-02-22 14:25:05 -08001447 conn->trace2_child_class = "transport/git";
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001448 } else {
Rene Scharfef1399292017-08-30 19:49:39 +02001449 struct strbuf cmd = STRBUF_INIT;
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001450 const char *const *var;
Rene Scharfef1399292017-08-30 19:49:39 +02001451
René Scharfe483bbd42014-08-19 21:10:48 +02001452 conn = xmalloc(sizeof(*conn));
1453 child_process_init(conn);
Linus Torvaldsf7192592005-07-04 11:57:58 -07001454
Jeff Kingaeeb2d42017-07-28 15:28:55 -04001455 if (looks_like_command_line_option(path))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001456 die(_("strange pathname '%s' blocked"), path);
Jeff Kingaeeb2d42017-07-28 15:28:55 -04001457
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001458 strbuf_addstr(&cmd, prog);
1459 strbuf_addch(&cmd, ' ');
1460 sq_quote_buf(&cmd, path);
Christian Couder0f503d72006-09-11 07:04:50 +02001461
Jeff Kingaab40432015-09-04 18:40:08 -04001462 /* remove repo-local variables from the environment */
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001463 for (var = local_repo_env; *var; var++)
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001464 strvec_push(&conn->env, *var);
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001465
Jeff Kinga48b4092015-09-08 04:33:14 -04001466 conn->use_shell = 1;
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001467 conn->in = conn->out = -1;
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001468 if (protocol == PROTO_SSH) {
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001469 char *ssh_host = hostandport;
1470 const char *port = NULL;
Jeff Kinga5adace2015-09-16 13:12:52 -04001471 transport_check_allowed("ssh");
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001472 get_host_and_port(&ssh_host, &port);
Christian Couder0f503d72006-09-11 07:04:50 +02001473
Torsten Bögershausen86ceb332015-02-21 16:52:48 +01001474 if (!port)
1475 port = get_port(ssh_host);
Junio C Hamano42da4842015-03-05 12:45:44 -08001476
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001477 if (flags & CONNECT_DIAG_URL) {
1478 printf("Diag: url=%s\n", url ? url : "NULL");
1479 printf("Diag: protocol=%s\n", prot_name(protocol));
1480 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1481 printf("Diag: port=%s\n", port ? port : "NONE");
1482 printf("Diag: path=%s\n", path ? path : "NULL");
Linus Torvaldsf7192592005-07-04 11:57:58 -07001483
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001484 free(hostandport);
1485 free(path);
Stefan Beller04f20c02015-03-09 09:58:22 -07001486 free(conn);
Rene Scharfef1399292017-08-30 19:49:39 +02001487 strbuf_release(&cmd);
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001488 return NULL;
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001489 }
Jeff Hostetlerabd81a32019-02-22 14:25:05 -08001490 conn->trace2_child_class = "transport/ssh";
Brandon Williams40fc51e2018-03-15 10:31:30 -07001491 fill_ssh_args(conn, ssh_host, port, version, flags);
Nguyễn Thái Ngọc Duyc049b612014-03-13 18:45:31 +07001492 } else {
Jeff Kinga5adace2015-09-16 13:12:52 -04001493 transport_check_allowed("file");
Jeff Hostetlerabd81a32019-02-22 14:25:05 -08001494 conn->trace2_child_class = "transport/file";
Brandon Williams40fc51e2018-03-15 10:31:30 -07001495 if (version > 0) {
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001496 strvec_pushf(&conn->env,
Jeff Kingf6d89422020-07-28 16:26:31 -04001497 GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1498 version);
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001499 }
Linus Torvaldsf7192592005-07-04 11:57:58 -07001500 }
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001501 strvec_push(&conn->args, cmd.buf);
Johannes Sixtf364cb82007-10-19 21:47:54 +02001502
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001503 if (start_command(conn))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001504 die(_("unable to fork"));
Johannes Sixtf364cb82007-10-19 21:47:54 +02001505
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001506 fd[0] = conn->out; /* read from child's stdout */
1507 fd[1] = conn->in; /* write to child's stdin */
1508 strbuf_release(&cmd);
1509 }
1510 free(hostandport);
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001511 free(path);
Johannes Sixt98158e92007-10-19 21:47:53 +02001512 return conn;
Linus Torvaldsf7192592005-07-04 11:57:58 -07001513}
1514
Johannes Sixt98158e92007-10-19 21:47:53 +02001515int finish_connect(struct child_process *conn)
Linus Torvaldsf7192592005-07-04 11:57:58 -07001516{
Johannes Sixtf364cb82007-10-19 21:47:54 +02001517 int code;
Jeff King7ffe8532011-05-16 02:52:11 -04001518 if (!conn || git_connection_is_socket(conn))
Franck Bui-Huuf42a5c42006-09-12 11:00:13 +02001519 return 0;
1520
Johannes Sixtf364cb82007-10-19 21:47:54 +02001521 code = finish_command(conn);
Johannes Sixt98158e92007-10-19 21:47:53 +02001522 free(conn);
Johannes Sixtf364cb82007-10-19 21:47:54 +02001523 return code;
Linus Torvaldsf7192592005-07-04 11:57:58 -07001524}