]> git.ipfire.org Git - thirdparty/git.git/blame - connect.c
The twentieth batch
[thirdparty/git.git] / connect.c
CommitLineData
731043fd 1#include "git-compat-util.h"
b2141fc1 2#include "config.h"
32a8f510 3#include "environment.h"
f394e093 4#include "gettext.h"
41771fa4 5#include "hex.h"
41cb7488 6#include "pkt-line.h"
b10d0ec7 7#include "quote.h"
6abf5c0c 8#include "refs.h"
15a1c012 9#include "run-command.h"
6b62816c 10#include "remote.h"
47a59185 11#include "connect.h"
9d2e9420 12#include "url.h"
a45b5f05 13#include "string-list.h"
fe299ec5 14#include "oid-array.h"
c339932b 15#include "path.h"
a5adaced 16#include "transport.h"
74ea5c95 17#include "trace2.h"
0cd83283 18#include "strbuf.h"
e52449b6 19#include "version.h"
2609043d 20#include "protocol.h"
65b5f948 21#include "alias.h"
0cfde740 22#include "bundle-uri.h"
f7192598 23
e52449b6 24static char *server_capabilities_v1;
ef8d7ac4 25static struct strvec server_capabilities_v2 = STRVEC_INIT;
7ce4c8f7 26static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset);
211b5f9e 27
be0b3f82 28static int check_ref(const char *name, unsigned int flags)
2718ff09
LT
29{
30 if (!flags)
31 return 1;
32
be0b3f82 33 if (!skip_prefix(name, "refs/", &name))
2718ff09
LT
34 return 0;
35
2718ff09 36 /* REF_NORMAL means that we don't want the magic fake tag refs */
7c3c5502
ZH
37 if ((flags & REF_NORMAL) && check_refname_format(name,
38 REFNAME_ALLOW_ONELEVEL))
2718ff09
LT
39 return 0;
40
41 /* REF_HEADS means that we want regular branch heads */
be0b3f82 42 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
2718ff09
LT
43 return 1;
44
45 /* REF_TAGS means that we want tags */
be0b3f82 46 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
2718ff09
LT
47 return 1;
48
49 /* All type bits clear means that we are ok with anything */
50 return !(flags & ~REF_NORMAL);
51}
52
4577370e
DB
53int check_ref_type(const struct ref *ref, int flags)
54{
be0b3f82 55 return check_ref(ref->name, flags);
4577370e
DB
56}
57
d2bff22c 58static NORETURN void die_initial_contact(int unexpected)
46284dd1 59{
7e3e479b
BW
60 /*
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 */
55e4f936 66 if (unexpected)
1a07e59c 67 die(_("the remote end hung up upon initial contact"));
46284dd1 68 else
f2b93b38
VA
69 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."));
46284dd1
HV
72}
73
e52449b6 74/* Checks if the server supports the capability 'c' */
a31cfe32 75int server_supports_v2(const char *c)
e52449b6
BW
76{
77 int i;
78
d70a9eb6 79 for (i = 0; i < server_capabilities_v2.nr; i++) {
e52449b6 80 const char *out;
d70a9eb6 81 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
e52449b6
BW
82 (!*out || *out == '='))
83 return 1;
84 }
a31cfe32
JK
85 return 0;
86}
e52449b6 87
a31cfe32
JK
88void ensure_server_supports_v2(const char *c)
89{
90 if (!server_supports_v2(c))
aad6fddb 91 die(_("server doesn't support '%s'"), c);
e52449b6
BW
92}
93
1349ffed 94int server_feature_v2(const char *c, const char **v)
95{
96 int i;
97
d70a9eb6 98 for (i = 0; i < server_capabilities_v2.nr; i++) {
1349ffed 99 const char *out;
d70a9eb6 100 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
1349ffed 101 (*out == '=')) {
102 *v = out + 1;
103 return 1;
104 }
105 }
106 return 0;
107}
108
f7e20501
BW
109int server_supports_feature(const char *c, const char *feature,
110 int die_on_error)
111{
112 int i;
113
d70a9eb6 114 for (i = 0; i < server_capabilities_v2.nr; i++) {
f7e20501 115 const char *out;
d70a9eb6 116 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
f7e20501
BW
117 (!*out || *(out++) == '=')) {
118 if (parse_feature_request(out, feature))
119 return 1;
120 else
121 break;
122 }
123 }
124
125 if (die_on_error)
aad6fddb 126 die(_("server doesn't support feature '%s'"), feature);
f7e20501
BW
127
128 return 0;
129}
130
e52449b6
BW
131static void process_capabilities_v2(struct packet_reader *reader)
132{
133 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
ef8d7ac4 134 strvec_push(&server_capabilities_v2, reader->line);
e52449b6
BW
135
136 if (reader->status != PACKET_READ_FLUSH)
aad6fddb 137 die(_("expected flush after capabilities"));
e52449b6
BW
138}
139
ad6ac124 140enum protocol_version discover_version(struct packet_reader *reader)
7e3e479b
BW
141{
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:
0181b600 153 case PACKET_READ_RESPONSE_END:
7e3e479b
BW
154 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) {
8f6982b4 162 case protocol_v2:
e52449b6 163 process_capabilities_v2(reader);
8f6982b4 164 break;
7e3e479b
BW
165 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
626beebd
JS
175 trace2_data_intmax("transfer", NULL, "negotiated-version", version);
176
7e3e479b
BW
177 return version;
178}
179
a45b5f05
JH
180static 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" */
5c0b13f8 188 sym = xmemdupz(val, len);
a45b5f05
JH
189 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;
ef4fe561 198 item = string_list_append_nodup(symref, sym);
a45b5f05
JH
199 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;
7ce4c8f7 209 size_t offset = 0;
a45b5f05 210
2c6a403d 211 while (1) {
7ce4c8f7 212 size_t len;
a45b5f05
JH
213 const char *val;
214
2c6a403d 215 val = next_server_feature_value("symref", &len, &offset);
a45b5f05
JH
216 if (!val)
217 break;
218 parse_one_symref_info(&symref, val, len);
a45b5f05 219 }
3383e199 220 string_list_sort(&symref);
a45b5f05
JH
221
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
92315e50 232static void process_capabilities(struct packet_reader *reader, int *linelen)
2609043d 233{
7c601dc3 234 const char *feat_val;
7ce4c8f7 235 size_t feat_len;
92315e50 236 const char *line = reader->line;
7e3e479b 237 int nul_location = strlen(line);
92315e50 238 if (nul_location == *linelen)
0cd83283 239 return;
e52449b6 240 server_capabilities_v1 = xstrdup(line + nul_location + 1);
92315e50 241 *linelen = nul_location;
7c601dc3 242
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 }
0cd83283
JT
253}
254
92315e50 255static int process_dummy_ref(const struct packet_reader *reader)
0cd83283 256{
92315e50 257 const char *line = reader->line;
0cd83283
JT
258 struct object_id oid;
259 const char *name;
260
7c601dc3 261 if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
0cd83283
JT
262 return 0;
263 if (*name != ' ')
264 return 0;
265 name++;
266
13e67aa3
JK
267 return oideq(reader->hash_algo->null_oid, &oid) &&
268 !strcmp(name, "capabilities^{}");
0cd83283
JT
269}
270
7e3e479b 271static void check_no_capabilities(const char *line, int len)
0cd83283 272{
7e3e479b 273 if (strlen(line) != len)
aad6fddb 274 warning(_("ignoring capabilities after first line '%s'"),
7e3e479b 275 line + strlen(line));
0cd83283
JT
276}
277
92315e50 278static int process_ref(const struct packet_reader *reader, int len,
279 struct ref ***list, unsigned int flags,
280 struct oid_array *extra_have)
0cd83283 281{
92315e50 282 const char *line = reader->line;
0cd83283
JT
283 struct object_id old_oid;
284 const char *name;
285
7c601dc3 286 if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
0cd83283
JT
287 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^{}")) {
aad6fddb 295 die(_("protocol error: unexpected capabilities^{}"));
0cd83283
JT
296 } 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 }
7e3e479b 302 check_no_capabilities(line, len);
0cd83283
JT
303 return 1;
304}
305
92315e50 306static int process_shallow(const struct packet_reader *reader, int len,
7e3e479b 307 struct oid_array *shallow_points)
0cd83283 308{
92315e50 309 const char *line = reader->line;
0cd83283
JT
310 const char *arg;
311 struct object_id old_oid;
312
7e3e479b 313 if (!skip_prefix(line, "shallow ", &arg))
0cd83283
JT
314 return 0;
315
7c601dc3 316 if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo))
aad6fddb 317 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
0cd83283 318 if (!shallow_points)
aad6fddb 319 die(_("repository on the other end cannot be shallow"));
0cd83283 320 oid_array_append(shallow_points, &old_oid);
7e3e479b 321 check_no_capabilities(line, len);
0cd83283
JT
322 return 1;
323}
324
7e3e479b
BW
325enum get_remote_heads_state {
326 EXPECTING_FIRST_REF = 0,
327 EXPECTING_REF,
328 EXPECTING_SHALLOW,
329 EXPECTING_DONE,
330};
331
d1c133f5
LT
332/*
333 * Read all the refs from the other end
334 */
ad6ac124 335struct ref **get_remote_heads(struct packet_reader *reader,
85edf4f5 336 struct ref **list, unsigned int flags,
910650d2 337 struct oid_array *extra_have,
338 struct oid_array *shallow_points)
d1c133f5 339{
a45b5f05 340 struct ref **orig_list = list;
7e3e479b
BW
341 int len = 0;
342 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
55e4f936 343
d1c133f5 344 *list = NULL;
1a7141ff 345
7e3e479b 346 while (state != EXPECTING_DONE) {
ad6ac124 347 switch (packet_reader_read(reader)) {
7e3e479b
BW
348 case PACKET_READ_EOF:
349 die_initial_contact(1);
350 case PACKET_READ_NORMAL:
ad6ac124 351 len = reader->pktlen;
7e3e479b
BW
352 break;
353 case PACKET_READ_FLUSH:
354 state = EXPECTING_DONE;
355 break;
356 case PACKET_READ_DELIM:
0181b600 357 case PACKET_READ_RESPONSE_END:
aad6fddb 358 die(_("invalid packet"));
7e3e479b
BW
359 }
360
0cd83283
JT
361 switch (state) {
362 case EXPECTING_FIRST_REF:
92315e50 363 process_capabilities(reader, &len);
364 if (process_dummy_ref(reader)) {
0cd83283
JT
365 state = EXPECTING_SHALLOW;
366 break;
367 }
368 state = EXPECTING_REF;
369 /* fallthrough */
370 case EXPECTING_REF:
92315e50 371 if (process_ref(reader, len, &list, flags, extra_have))
0cd83283
JT
372 break;
373 state = EXPECTING_SHALLOW;
374 /* fallthrough */
375 case EXPECTING_SHALLOW:
92315e50 376 if (process_shallow(reader, len, shallow_points))
0cd83283 377 break;
aad6fddb 378 die(_("protocol error: unexpected '%s'"), reader->line);
7e3e479b
BW
379 case EXPECTING_DONE:
380 break;
211b5f9e 381 }
d1c133f5 382 }
a45b5f05
JH
383
384 annotate_refs_with_symref_info(*orig_list);
385
d1c133f5
LT
386 return list;
387}
388
e52449b6 389/* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
4f37d457 390static int process_ref_v2(struct packet_reader *reader, struct ref ***list,
f36d4f83 391 const char **unborn_head_target)
e52449b6
BW
392{
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;
67e9a707 399 const char *line = reader->line;
e52449b6
BW
400
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
4f37d457
JT
412 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 }
ab67235b 431 if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) ||
e52449b6
BW
432 *end) {
433 ret = 0;
434 goto out;
435 }
436
437 ref = alloc_ref(line_sections.items[i++].string);
438
ab67235b 439 memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
e52449b6
BW
440 **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;
ab67235b 452 if (parse_oid_hex_algop(arg, &peeled_oid, &end,
453 reader->hash_algo) || *end) {
e52449b6
BW
454 ret = 0;
455 goto out;
456 }
457
458 peeled_name = xstrfmt("%s^{}", ref->name);
459 peeled = alloc_ref(peeled_name);
460
ab67235b 461 memcpy(peeled->old_oid.hash, peeled_oid.hash,
462 reader->hash_algo->rawsz);
e52449b6
BW
463 **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
b0df0c16
DL
475void 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
86f4e312
ÆAB
485static void send_capabilities(int fd_out, struct packet_reader *reader)
486{
487 const char *hash_name;
488
a31cfe32 489 if (server_supports_v2("agent"))
86f4e312
ÆAB
490 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
491
492 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);
498 } else {
499 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
500 }
501}
502
0cfde740
ÆAB
503int 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 */
0903d8bb 509 ensure_server_supports_v2("bundle-uri");
0cfde740
ÆAB
510
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
e52449b6
BW
546struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
547 struct ref **list, int for_push,
39835409 548 struct transport_ls_refs_options *transport_options,
b0df0c16
DL
549 const struct string_list *server_options,
550 int stateless_rpc)
e52449b6
BW
551{
552 int i;
39835409
JT
553 struct strvec *ref_prefixes = transport_options ?
554 &transport_options->ref_prefixes : NULL;
f36d4f83 555 const char **unborn_head_target = transport_options ?
4f37d457 556 &transport_options->unborn_head_target : NULL;
e52449b6
BW
557 *list = NULL;
558
a31cfe32
JK
559 ensure_server_supports_v2("ls-refs");
560 packet_write_fmt(fd_out, "command=ls-refs\n");
e52449b6 561
86f4e312
ÆAB
562 /* Send capabilities */
563 send_capabilities(fd_out, reader);
ab67235b 564
a31cfe32
JK
565 if (server_options && server_options->nr) {
566 ensure_server_supports_v2("server-option");
ff473221
BW
567 for (i = 0; i < server_options->nr; i++)
568 packet_write_fmt(fd_out, "server-option=%s",
569 server_options->items[i].string);
a31cfe32 570 }
ff473221 571
e52449b6
BW
572 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");
4f37d457
JT
577 if (server_supports_feature("ls-refs", "unborn", 0))
578 packet_write_fmt(fd_out, "unborn\n");
d70a9eb6 579 for (i = 0; ref_prefixes && i < ref_prefixes->nr; i++) {
e52449b6 580 packet_write_fmt(fd_out, "ref-prefix %s\n",
d70a9eb6 581 ref_prefixes->v[i]);
e52449b6
BW
582 }
583 packet_flush(fd_out);
584
585 /* Process response from server */
586 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
4f37d457 587 if (!process_ref_v2(reader, &list, unborn_head_target))
aad6fddb 588 die(_("invalid ls-refs response: %s"), reader->line);
e52449b6
BW
589 }
590
591 if (reader->status != PACKET_READ_FLUSH)
aad6fddb 592 die(_("expected flush after ref listing"));
e52449b6 593
b0df0c16
DL
594 check_stateless_delimiter(stateless_rpc, reader,
595 _("expected response end packet after ref listing"));
596
e52449b6
BW
597 return list;
598}
599
7ce4c8f7 600const char *parse_feature_value(const char *feature_list, const char *feature, size_t *lenp, size_t *offset)
f47182c8 601{
aa962fef 602 const char *orig_start = feature_list;
7ce4c8f7 603 size_t len;
f47182c8
JH
604
605 if (!feature_list)
606 return NULL;
607
608 len = strlen(feature);
2c6a403d 609 if (offset)
610 feature_list += *offset;
f47182c8
JH
611 while (*feature_list) {
612 const char *found = strstr(feature_list, feature);
613 if (!found)
614 return NULL;
94427108
JK
615 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;
44d2aec6 621 if (offset)
aa962fef 622 *offset = found + len - orig_start;
94427108
JK
623 return value;
624 }
625 /* feature with a value (e.g., "agent=git/1.2.3") */
626 else if (*value == '=') {
7ce4c8f7 627 size_t end;
2c6a403d 628
94427108 629 value++;
2c6a403d 630 end = strcspn(value, " \t\n");
94427108 631 if (lenp)
2c6a403d 632 *lenp = end;
633 if (offset)
aa962fef 634 *offset = value + end - orig_start;
94427108
JK
635 return value;
636 }
637 /*
638 * otherwise we matched a substring of another feature;
639 * keep looking
640 */
641 }
f47182c8
JH
642 feature_list = found + 1;
643 }
644 return NULL;
211b5f9e
JS
645}
646
122037c2 647int server_supports_hash(const char *desired, int *feature_supported)
648{
7ce4c8f7
JK
649 size_t offset = 0;
650 size_t len;
122037c2 651 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
94427108
JK
669int parse_feature_request(const char *feature_list, const char *feature)
670{
2c6a403d 671 return !!parse_feature_value(feature_list, feature, NULL, NULL);
672}
673
7ce4c8f7 674static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset)
2c6a403d 675{
676 return parse_feature_value(server_capabilities_v1, feature, len, offset);
94427108
JK
677}
678
7ce4c8f7 679const char *server_feature_value(const char *feature, size_t *len)
94427108 680{
2c6a403d 681 return parse_feature_value(server_capabilities_v1, feature, len, NULL);
94427108
JK
682}
683
684int server_supports(const char *feature)
685{
686 return !!server_feature_value(feature, NULL);
687}
688
2386d658
LT
689enum protocol {
690 PROTO_LOCAL = 1,
c59ab2e5 691 PROTO_FILE,
2386d658 692 PROTO_SSH,
4b05548f 693 PROTO_GIT
2386d658
LT
694};
695
c59ab2e5
TB
696int 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) ||
f82a97eb 701 (has_dos_drive_prefix(url) && is_valid_path(url));
c59ab2e5
TB
702}
703
5610b7c0
TB
704static const char *prot_name(enum protocol protocol)
705{
706 switch (protocol) {
707 case PROTO_LOCAL:
c59ab2e5 708 case PROTO_FILE:
5610b7c0
TB
709 return "file";
710 case PROTO_SSH:
711 return "ssh";
712 case PROTO_GIT:
713 return "git";
714 default:
83e6bda3 715 return "unknown protocol";
5610b7c0
TB
716 }
717}
718
2386d658
LT
719static 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;
07c7782c 725 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
c05186cc 726 return PROTO_SSH;
07c7782c 727 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
c05186cc 728 return PROTO_SSH;
72a4f4b6 729 if (!strcmp(name, "file"))
c59ab2e5 730 return PROTO_FILE;
aad6fddb 731 die(_("protocol '%s' is not supported"), name);
2386d658
LT
732}
733
86ceb337
TB
734static 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
5ba88448
YH
758#define STR_(s) # s
759#define STR(s) STR_(s)
2386d658 760
72a534da
ML
761static void get_host_and_port(char **host, const char **port)
762{
763 char *colon, *end;
86ceb337 764 end = host_end(host, 1);
72a534da 765 colon = strchr(end, ':');
72a534da 766 if (colon) {
86ceb337
TB
767 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;
6b6c5f7a
TB
771 } else if (!colon[1]) {
772 *colon = 0;
86ceb337 773 }
72a534da
ML
774 }
775}
776
e47a8583
EW
777static void enable_keepalive(int sockfd)
778{
779 int ka = 1;
780
781 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
aad6fddb 782 error_errno(_("unable to set SO_KEEPALIVE on socket"));
e47a8583
EW
783}
784
49744d63 785#ifndef NO_IPV6
4c505f71 786
ba505322
AR
787static const char *ai_name(const struct addrinfo *ai)
788{
785a9857
BK
789 static char addr[NI_MAXHOST];
790 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
791 NI_NUMERICHOST) != 0)
5096d490 792 xsnprintf(addr, sizeof(addr), "(unknown)");
785a9857 793
ba505322
AR
794 return addr;
795}
796
5ad312be
JL
797/*
798 * Returns a connected socket() fd, or else die()s.
799 */
7841ce79 800static int git_tcp_connect_sock(char *host, int flags)
2386d658 801{
63a995b6
DZ
802 struct strbuf error_message = STRBUF_INIT;
803 int sockfd = -1;
554fe20d 804 const char *port = STR(DEFAULT_GIT_PORT);
5ba88448
YH
805 struct addrinfo hints, *ai0, *ai;
806 int gai;
ba505322 807 int cnt = 0;
5ba88448 808
72a534da
ML
809 get_host_and_port(&host, &port);
810 if (!*port)
811 port = "<none>";
5ba88448
YH
812
813 memset(&hints, 0, sizeof(hints));
c915f11e
EW
814 if (flags & CONNECT_IPV4)
815 hints.ai_family = AF_INET;
816 else if (flags & CONNECT_IPV6)
817 hints.ai_family = AF_INET6;
5ba88448
YH
818 hints.ai_socktype = SOCK_STREAM;
819 hints.ai_protocol = IPPROTO_TCP;
820
7841ce79 821 if (flags & CONNECT_VERBOSE)
aad6fddb 822 fprintf(stderr, _("Looking up %s ... "), host);
7841ce79 823
5ba88448
YH
824 gai = getaddrinfo(host, port, &hints, &ai);
825 if (gai)
aad6fddb 826 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
5ba88448 827
7841ce79 828 if (flags & CONNECT_VERBOSE)
aad6fddb
NTND
829 /* TRANSLATORS: this is the end of "Looking up %s ... " */
830 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
7841ce79 831
e08afecd 832 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
5ad312be
JL
833 sockfd = socket(ai->ai_family,
834 ai->ai_socktype, ai->ai_protocol);
63a995b6
DZ
835 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);
5ba88448
YH
841 sockfd = -1;
842 continue;
2386d658 843 }
ba505322
AR
844 if (flags & CONNECT_VERBOSE)
845 fprintf(stderr, "%s ", ai_name(ai));
5ba88448 846 break;
2386d658
LT
847 }
848
5ba88448 849 freeaddrinfo(ai0);
2386d658 850
2386d658 851 if (sockfd < 0)
aad6fddb 852 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
5ba88448 853
e47a8583
EW
854 enable_keepalive(sockfd);
855
7841ce79 856 if (flags & CONNECT_VERBOSE)
aad6fddb
NTND
857 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
858 fprintf_ln(stderr, _("done."));
7841ce79 859
63a995b6
DZ
860 strbuf_release(&error_message);
861
5ad312be 862 return sockfd;
2386d658
LT
863}
864
49744d63 865#else /* NO_IPV6 */
4c505f71 866
5ad312be
JL
867/*
868 * Returns a connected socket() fd, or else die()s.
869 */
7841ce79 870static int git_tcp_connect_sock(char *host, int flags)
4c505f71 871{
7203a2d1
EFL
872 struct strbuf error_message = STRBUF_INIT;
873 int sockfd = -1;
72a534da
ML
874 const char *port = STR(DEFAULT_GIT_PORT);
875 char *ep;
4c505f71
PA
876 struct hostent *he;
877 struct sockaddr_in sa;
878 char **ap;
879 unsigned int nport;
ba505322 880 int cnt;
4c505f71 881
72a534da 882 get_host_and_port(&host, &port);
4c505f71 883
7841ce79 884 if (flags & CONNECT_VERBOSE)
aad6fddb 885 fprintf(stderr, _("Looking up %s ... "), host);
7841ce79 886
4c505f71
PA
887 he = gethostbyname(host);
888 if (!he)
aad6fddb 889 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
4c505f71
PA
890 nport = strtoul(port, &ep, 10);
891 if ( ep == port || *ep ) {
892 /* Not numeric */
893 struct servent *se = getservbyname(port,"tcp");
894 if ( !se )
aad6fddb 895 die(_("unknown port %s"), port);
4c505f71
PA
896 nport = se->s_port;
897 }
898
7841ce79 899 if (flags & CONNECT_VERBOSE)
aad6fddb
NTND
900 /* TRANSLATORS: this is the end of "Looking up %s ... " */
901 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
7841ce79 902
ba505322 903 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
4c505f71
PA
904 memset(&sa, 0, sizeof sa);
905 sa.sin_family = he->h_addrtype;
6573faff 906 sa.sin_port = htons(nport);
c6164218 907 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71 908
7203a2d1
EFL
909 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",
ba505322
AR
913 host,
914 cnt,
915 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
7203a2d1
EFL
916 strerror(errno));
917 if (0 <= sockfd)
918 close(sockfd);
4c505f71
PA
919 sockfd = -1;
920 continue;
921 }
ba505322
AR
922 if (flags & CONNECT_VERBOSE)
923 fprintf(stderr, "%s ",
924 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
4c505f71
PA
925 break;
926 }
927
928 if (sockfd < 0)
aad6fddb 929 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
4c505f71 930
e47a8583
EW
931 enable_keepalive(sockfd);
932
7841ce79 933 if (flags & CONNECT_VERBOSE)
aad6fddb
NTND
934 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
935 fprintf_ln(stderr, _("done."));
7841ce79 936
5ad312be
JL
937 return sockfd;
938}
939
940#endif /* NO_IPV6 */
941
942
8e349780
JN
943/*
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)
5ad312be 955{
7841ce79 956 int sockfd = git_tcp_connect_sock(host, flags);
5ad312be 957
4c505f71 958 fd[0] = sockfd;
ec587fde 959 fd[1] = dup(sockfd);
8e349780
JN
960
961 return &no_fork;
4c505f71
PA
962}
963
4c505f71 964
96f1e58f 965static char *git_proxy_command;
f8014776 966
ef90d6d4 967static int git_proxy_command_options(const char *var, const char *value,
a4e7e317 968 const struct config_context *ctx, void *cb)
f8014776 969{
e814bc4d 970 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
971 const char *for_pos;
972 int matchlen = -1;
973 int hostlen;
15112c95
EFL
974 const char *rhost_name = cb;
975 int rhost_len = strlen(rhost_name);
c3df8568 976
e814bc4d 977 if (git_proxy_command)
f8014776 978 return 0;
c64b9ad0
JH
979 if (!value)
980 return config_error_nonbool(var);
e814bc4d
JH
981 /* [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 */
c3df8568 987 for_pos = strstr(value, " for ");
e814bc4d
JH
988 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;
1003 }
1004 if (0 <= matchlen) {
1005 /* core.gitproxy = none for kernel.org */
a6080a0a 1006 if (matchlen == 4 &&
e814bc4d
JH
1007 !memcmp(value, "none", 4))
1008 matchlen = 0;
182af834 1009 git_proxy_command = xmemdupz(value, matchlen);
f8014776 1010 }
e814bc4d 1011 return 0;
f8014776
PC
1012 }
1013
a4e7e317 1014 return git_default_config(var, value, ctx, cb);
f8014776
PC
1015}
1016
e814bc4d 1017static int git_use_proxy(const char *host)
f8014776
PC
1018{
1019 git_proxy_command = getenv("GIT_PROXY_COMMAND");
15112c95 1020 git_config(git_proxy_command_options, (void*)host);
e814bc4d 1021 return (git_proxy_command && *git_proxy_command);
f8014776
PC
1022}
1023
5cbf8246 1024static struct child_process *git_proxy_connect(int fd[2], char *host)
f8014776 1025{
554fe20d 1026 const char *port = STR(DEFAULT_GIT_PORT);
5cbf8246 1027 struct child_process *proxy;
f8014776 1028
72a534da 1029 get_host_and_port(&host, &port);
f8014776 1030
3be4cf09 1031 if (looks_like_command_line_option(host))
aad6fddb 1032 die(_("strange hostname '%s' blocked"), host);
3be4cf09 1033 if (looks_like_command_line_option(port))
aad6fddb 1034 die(_("strange port '%s' blocked"), port);
3be4cf09 1035
483bbd4e
RS
1036 proxy = xmalloc(sizeof(*proxy));
1037 child_process_init(proxy);
ef8d7ac4
JK
1038 strvec_push(&proxy->args, git_proxy_command);
1039 strvec_push(&proxy->args, host);
1040 strvec_push(&proxy->args, port);
5cbf8246
JK
1041 proxy->in = -1;
1042 proxy->out = -1;
1043 if (start_command(proxy))
aad6fddb 1044 die(_("cannot start proxy %s"), git_proxy_command);
5cbf8246
JK
1045 fd[0] = proxy->out; /* read from proxy stdout */
1046 fd[1] = proxy->in; /* write to proxy stdin */
1047 return proxy;
f8014776
PC
1048}
1049
86ceb337 1050static char *get_port(char *host)
2e776665
LT
1051{
1052 char *end;
86ceb337
TB
1053 char *p = strchr(host, ':');
1054
2e776665 1055 if (p) {
8f148253
RS
1056 long port = strtol(p + 1, &end, 10);
1057 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
86ceb337
TB
1058 *p = '\0';
1059 return p+1;
2e776665
LT
1060 }
1061 }
1062
1063 return NULL;
1064}
1065
f7192598 1066/*
cabc3c12
JS
1067 * Extract protocol and relevant parts from the specified connection URL.
1068 * The caller must free() the returned strings.
f7192598 1069 */
cabc3c12 1070static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
83b05875 1071 char **ret_path)
f7192598 1072{
9d2e9420 1073 char *url;
8e76bf3f 1074 char *host, *path;
356bece0 1075 char *end;
c59ab2e5 1076 int separator = '/';
faea9ccb 1077 enum protocol protocol = PROTO_LOCAL;
f0b7367c 1078
9d2e9420
JK
1079 if (is_url(url_orig))
1080 url = url_decode(url_orig);
1081 else
1082 url = xstrdup(url_orig);
1083
faea9ccb 1084 host = strstr(url, "://");
eeefa7c9 1085 if (host) {
faea9ccb
AE
1086 *host = '\0';
1087 protocol = get_protocol(url);
1088 host += 3;
356bece0 1089 } else {
f7192598 1090 host = url;
c59ab2e5
TB
1091 if (!url_is_local_not_ssh(url)) {
1092 protocol = PROTO_SSH;
1093 separator = ':';
1094 }
356bece0
YH
1095 }
1096
9aa5053d 1097 /*
83b05875
TB
1098 * Don't do destructive transforms as protocol code does
1099 * '[]' unwrapping in get_host_and_port()
9aa5053d 1100 */
86ceb337 1101 end = host_end(&host, 0);
356bece0 1102
c59ab2e5 1103 if (protocol == PROTO_LOCAL)
72a4f4b6 1104 path = end;
ebb8d2c9
TB
1105 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 "//" */
c59ab2e5
TB
1109 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);
2386d658 1113
faea9ccb 1114 if (!path || !*path)
aad6fddb 1115 die(_("no path specified; see 'git help pull' for valid url syntax"));
faea9ccb
AE
1116
1117 /*
1118 * null-terminate hostname and point path to ~ for URL's like this:
1119 * ssh://host.xz/~user/repo
1120 */
c59ab2e5
TB
1121
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) {
faea9ccb
AE
1126 if (path[1] == '~')
1127 path++;
faea9ccb
AE
1128 }
1129
c59ab2e5
TB
1130 path = xstrdup(path);
1131 *end = '\0';
1132
cabc3c12 1133 *ret_host = xstrdup(host);
c59ab2e5 1134 *ret_path = path;
cabc3c12
JS
1135 free(url);
1136 return protocol;
1137}
1138
3c8ede3f
NTND
1139static const char *get_ssh_command(void)
1140{
1141 const char *ssh;
1142
1143 if ((ssh = getenv("GIT_SSH_COMMAND")))
1144 return ssh;
1145
f1de981e 1146 if (!git_config_get_string_tmp("core.sshcommand", &ssh))
3c8ede3f
NTND
1147 return ssh;
1148
1149 return NULL;
1150}
1151
94b8ae5a 1152enum ssh_variant {
0da0e49b 1153 VARIANT_AUTO,
94b8ae5a
BW
1154 VARIANT_SIMPLE,
1155 VARIANT_SSH,
1156 VARIANT_PLINK,
1157 VARIANT_PUTTY,
1158 VARIANT_TORTOISEPLINK,
1159};
1160
0da0e49b 1161static void override_ssh_variant(enum ssh_variant *ssh_variant)
e2824e47 1162{
94b8ae5a 1163 const char *variant = getenv("GIT_SSH_VARIANT");
486c8e8c 1164
f1de981e 1165 if (!variant && git_config_get_string_tmp("ssh.variant", &variant))
0da0e49b 1166 return;
486c8e8c 1167
0da0e49b
JN
1168 if (!strcmp(variant, "auto"))
1169 *ssh_variant = VARIANT_AUTO;
1170 else if (!strcmp(variant, "plink"))
94b8ae5a
BW
1171 *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;
486c8e8c
JH
1180}
1181
94b8ae5a
BW
1182static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1183 int is_cmdline)
486c8e8c 1184{
0da0e49b 1185 enum ssh_variant ssh_variant = VARIANT_AUTO;
486c8e8c 1186 const char *variant;
e2824e47
JS
1187 char *p = NULL;
1188
0da0e49b
JN
1189 override_ssh_variant(&ssh_variant);
1190
1191 if (ssh_variant != VARIANT_AUTO)
94b8ae5a 1192 return ssh_variant;
486c8e8c
JH
1193
1194 if (!is_cmdline) {
e2824e47
JS
1195 p = xstrdup(ssh_command);
1196 variant = basename(p);
1197 } else {
1198 const char **ssh_argv;
1199
1200 p = xstrdup(ssh_command);
22e5ae5c 1201 if (split_cmdline(p, &ssh_argv) > 0) {
e2824e47
JS
1202 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);
5d2993b6
JK
1209 } else {
1210 free(p);
94b8ae5a 1211 return ssh_variant;
5d2993b6 1212 }
e2824e47
JS
1213 }
1214
94b8ae5a
BW
1215 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;
e2824e47 1221 else if (!strcasecmp(variant, "tortoiseplink") ||
94b8ae5a
BW
1222 !strcasecmp(variant, "tortoiseplink.exe"))
1223 ssh_variant = VARIANT_TORTOISEPLINK;
1224
e2824e47 1225 free(p);
94b8ae5a 1226 return ssh_variant;
e2824e47
JS
1227}
1228
2ac67cb6
JN
1229/*
1230 * 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,
40fc51e3 1237 enum protocol_version version,
2ac67cb6
JN
1238 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");
a02ea577
JK
1254 if (strchr(target_host, '\n') || strchr(path, '\n'))
1255 die(_("newline is forbidden in git:// hosts and repo paths"));
2ac67cb6 1256
233cd282
JN
1257 /*
1258 * These underlying connection commands die() if they
2ac67cb6
JN
1259 * 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 */
40fc51e3 1278 if (version > 0) {
2ac67cb6
JN
1279 strbuf_addch(&request, '\0');
1280 strbuf_addf(&request, "version=%d%c",
40fc51e3 1281 version, '\0');
2ac67cb6
JN
1282 }
1283
1284 packet_write(fd[1], request.buf, request.len);
1285
1286 free(target_host);
1287 strbuf_release(&request);
1288 return conn;
1289}
1290
957e2ad2
JN
1291/*
1292 * Append the appropriate environment variables to `env` and options to
1293 * `args` for running ssh in Git's SSH-tunneled transport.
1294 */
ef8d7ac4 1295static void push_ssh_options(struct strvec *args, struct strvec *env,
957e2ad2 1296 enum ssh_variant variant, const char *port,
40fc51e3 1297 enum protocol_version version, int flags)
957e2ad2
JN
1298{
1299 if (variant == VARIANT_SSH &&
40fc51e3 1300 version > 0) {
ef8d7ac4
JK
1301 strvec_push(args, "-o");
1302 strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1303 strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
f6d8942b 1304 version);
957e2ad2
JN
1305 }
1306
a3f5b66f
JN
1307 if (flags & CONNECT_IPV4) {
1308 switch (variant) {
1309 case VARIANT_AUTO:
1310 BUG("VARIANT_AUTO passed to push_ssh_options");
1311 case VARIANT_SIMPLE:
aad6fddb 1312 die(_("ssh variant 'simple' does not support -4"));
a3f5b66f
JN
1313 case VARIANT_SSH:
1314 case VARIANT_PLINK:
1315 case VARIANT_PUTTY:
1316 case VARIANT_TORTOISEPLINK:
ef8d7ac4 1317 strvec_push(args, "-4");
a3f5b66f
JN
1318 }
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:
aad6fddb 1324 die(_("ssh variant 'simple' does not support -6"));
a3f5b66f
JN
1325 case VARIANT_SSH:
1326 case VARIANT_PLINK:
1327 case VARIANT_PUTTY:
1328 case VARIANT_TORTOISEPLINK:
ef8d7ac4 1329 strvec_push(args, "-6");
a3f5b66f 1330 }
957e2ad2
JN
1331 }
1332
1333 if (variant == VARIANT_TORTOISEPLINK)
ef8d7ac4 1334 strvec_push(args, "-batch");
957e2ad2 1335
3fa5e0d0
JN
1336 if (port) {
1337 switch (variant) {
1338 case VARIANT_AUTO:
1339 BUG("VARIANT_AUTO passed to push_ssh_options");
1340 case VARIANT_SIMPLE:
aad6fddb 1341 die(_("ssh variant 'simple' does not support setting port"));
3fa5e0d0 1342 case VARIANT_SSH:
ef8d7ac4 1343 strvec_push(args, "-p");
3fa5e0d0
JN
1344 break;
1345 case VARIANT_PLINK:
1346 case VARIANT_PUTTY:
1347 case VARIANT_TORTOISEPLINK:
ef8d7ac4 1348 strvec_push(args, "-P");
3fa5e0d0 1349 }
957e2ad2 1350
ef8d7ac4 1351 strvec_push(args, port);
957e2ad2
JN
1352 }
1353}
1354
fce54ce4
JN
1355/* 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,
40fc51e3
BW
1357 const char *port, enum protocol_version version,
1358 int flags)
fce54ce4
JN
1359{
1360 const char *ssh;
1361 enum ssh_variant variant;
1362
1363 if (looks_like_command_line_option(ssh_host))
aad6fddb 1364 die(_("strange hostname '%s' blocked"), ssh_host);
fce54ce4
JN
1365
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
0da0e49b
JN
1383 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
ef8d7ac4
JK
1389 strvec_push(&detect.args, ssh);
1390 strvec_push(&detect.args, "-G");
29fda24d 1391 push_ssh_options(&detect.args, &detect.env,
40fc51e3 1392 VARIANT_SSH, port, version, flags);
ef8d7ac4 1393 strvec_push(&detect.args, ssh_host);
0da0e49b
JN
1394
1395 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1396 }
1397
ef8d7ac4 1398 strvec_push(&conn->args, ssh);
29fda24d
ÆAB
1399 push_ssh_options(&conn->args, &conn->env, variant, port, version,
1400 flags);
ef8d7ac4 1401 strvec_push(&conn->args, ssh_host);
fce54ce4
JN
1402}
1403
cabc3c12 1404/*
8e349780
JN
1405 * 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).
cabc3c12
JS
1410 *
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 */
1415struct child_process *git_connect(int fd[2], const char *url,
eaa0fd65 1416 const char *name,
cabc3c12
JS
1417 const char *prog, int flags)
1418{
a2036d7e 1419 char *hostandport, *path;
8e349780 1420 struct child_process *conn;
cabc3c12 1421 enum protocol protocol;
40fc51e3 1422 enum protocol_version version = get_protocol_version_config();
cabc3c12 1423
1aa8dded
BW
1424 /*
1425 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
eaa0fd65
JK
1426 * 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.
1aa8dded 1429 */
eaa0fd65 1430 if (version == protocol_v2 && strcmp("git-upload-pack", name))
1aa8dded
BW
1431 version = protocol_v0;
1432
cabc3c12
JS
1433 /* Without this we cannot rely on waitpid() to tell
1434 * what happened to our children.
2e776665 1435 */
cabc3c12 1436 signal(SIGCHLD, SIG_DFL);
2e776665 1437
a2036d7e 1438 protocol = parse_connect_url(url, &hostandport, &path);
3f55ccab 1439 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
5610b7c0
TB
1440 printf("Diag: url=%s\n", url ? url : "NULL");
1441 printf("Diag: protocol=%s\n", prot_name(protocol));
a2036d7e 1442 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
5610b7c0 1443 printf("Diag: path=%s\n", path ? path : "NULL");
a2036d7e
TB
1444 conn = NULL;
1445 } else if (protocol == PROTO_GIT) {
40fc51e3 1446 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
abd81a3d 1447 conn->trace2_child_class = "transport/git";
a2036d7e 1448 } else {
f1399291 1449 struct strbuf cmd = STRBUF_INIT;
0c2f0d27 1450 const char *const *var;
f1399291 1451
483bbd4e
RS
1452 conn = xmalloc(sizeof(*conn));
1453 child_process_init(conn);
a2036d7e 1454
aeeb2d49 1455 if (looks_like_command_line_option(path))
aad6fddb 1456 die(_("strange pathname '%s' blocked"), path);
aeeb2d49 1457
a2036d7e
TB
1458 strbuf_addstr(&cmd, prog);
1459 strbuf_addch(&cmd, ' ');
1460 sq_quote_buf(&cmd, path);
1461
aab40438 1462 /* remove repo-local variables from the environment */
0c2f0d27 1463 for (var = local_repo_env; *var; var++)
29fda24d 1464 strvec_push(&conn->env, *var);
0c2f0d27 1465
a48b409f 1466 conn->use_shell = 1;
a2036d7e 1467 conn->in = conn->out = -1;
a2036d7e 1468 if (protocol == PROTO_SSH) {
a2036d7e
TB
1469 char *ssh_host = hostandport;
1470 const char *port = NULL;
a5adaced 1471 transport_check_allowed("ssh");
a2036d7e 1472 get_host_and_port(&ssh_host, &port);
a2036d7e 1473
86ceb337
TB
1474 if (!port)
1475 port = get_port(ssh_host);
42da4840 1476
3f55ccab
TB
1477 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");
a2036d7e 1483
3f55ccab
TB
1484 free(hostandport);
1485 free(path);
04f20c04 1486 free(conn);
f1399291 1487 strbuf_release(&cmd);
3f55ccab 1488 return NULL;
37ee646e 1489 }
abd81a3d 1490 conn->trace2_child_class = "transport/ssh";
40fc51e3 1491 fill_ssh_args(conn, ssh_host, port, version, flags);
c049b61d 1492 } else {
a5adaced 1493 transport_check_allowed("file");
abd81a3d 1494 conn->trace2_child_class = "transport/file";
40fc51e3 1495 if (version > 0) {
29fda24d 1496 strvec_pushf(&conn->env,
f6d8942b
JK
1497 GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1498 version);
0c2f0d27 1499 }
4852f723 1500 }
ef8d7ac4 1501 strvec_push(&conn->args, cmd.buf);
f364cb88 1502
a2036d7e 1503 if (start_command(conn))
aad6fddb 1504 die(_("unable to fork"));
f364cb88 1505
a2036d7e
TB
1506 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);
cabc3c12 1511 free(path);
98158e9c 1512 return conn;
f7192598
LT
1513}
1514
98158e9c 1515int finish_connect(struct child_process *conn)
f7192598 1516{
f364cb88 1517 int code;
7ffe853b 1518 if (!conn || git_connection_is_socket(conn))
f42a5c4e
FBH
1519 return 0;
1520
f364cb88 1521 code = finish_command(conn);
98158e9c 1522 free(conn);
f364cb88 1523 return code;
f7192598 1524}