]> git.ipfire.org Git - thirdparty/git.git/blame - connect.c
Merge branch 'bc/sha-256-part-2'
[thirdparty/git.git] / connect.c
CommitLineData
731043fd 1#include "git-compat-util.h"
f7192598 2#include "cache.h"
b2141fc1 3#include "config.h"
41cb7488 4#include "pkt-line.h"
b10d0ec7 5#include "quote.h"
6abf5c0c 6#include "refs.h"
15a1c012 7#include "run-command.h"
6b62816c 8#include "remote.h"
47a59185 9#include "connect.h"
9d2e9420 10#include "url.h"
a45b5f05 11#include "string-list.h"
fe299ec5 12#include "oid-array.h"
a5adaced 13#include "transport.h"
0cd83283 14#include "strbuf.h"
e52449b6 15#include "version.h"
2609043d 16#include "protocol.h"
65b5f948 17#include "alias.h"
f7192598 18
e52449b6
BW
19static char *server_capabilities_v1;
20static struct argv_array server_capabilities_v2 = ARGV_ARRAY_INIT;
2c6a403d 21static const char *next_server_feature_value(const char *feature, int *len, int *offset);
211b5f9e 22
be0b3f82 23static int check_ref(const char *name, unsigned int flags)
2718ff09
LT
24{
25 if (!flags)
26 return 1;
27
be0b3f82 28 if (!skip_prefix(name, "refs/", &name))
2718ff09
LT
29 return 0;
30
2718ff09 31 /* REF_NORMAL means that we don't want the magic fake tag refs */
8d9c5010 32 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
2718ff09
LT
33 return 0;
34
35 /* REF_HEADS means that we want regular branch heads */
be0b3f82 36 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
2718ff09
LT
37 return 1;
38
39 /* REF_TAGS means that we want tags */
be0b3f82 40 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
2718ff09
LT
41 return 1;
42
43 /* All type bits clear means that we are ok with anything */
44 return !(flags & ~REF_NORMAL);
45}
46
4577370e
DB
47int check_ref_type(const struct ref *ref, int flags)
48{
be0b3f82 49 return check_ref(ref->name, flags);
4577370e
DB
50}
51
d2bff22c 52static NORETURN void die_initial_contact(int unexpected)
46284dd1 53{
7e3e479b
BW
54 /*
55 * A hang-up after seeing some response from the other end
56 * means that it is unexpected, as we know the other end is
57 * willing to talk to us. A hang-up before seeing any
58 * response does not necessarily mean an ACL problem, though.
59 */
55e4f936 60 if (unexpected)
1a07e59c 61 die(_("the remote end hung up upon initial contact"));
46284dd1 62 else
f2b93b38
VA
63 die(_("Could not read from remote repository.\n\n"
64 "Please make sure you have the correct access rights\n"
65 "and the repository exists."));
46284dd1
HV
66}
67
e52449b6
BW
68/* Checks if the server supports the capability 'c' */
69int server_supports_v2(const char *c, int die_on_error)
70{
71 int i;
72
73 for (i = 0; i < server_capabilities_v2.argc; i++) {
74 const char *out;
75 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
76 (!*out || *out == '='))
77 return 1;
78 }
79
80 if (die_on_error)
aad6fddb 81 die(_("server doesn't support '%s'"), c);
e52449b6
BW
82
83 return 0;
84}
85
1349ffed 86int server_feature_v2(const char *c, const char **v)
87{
88 int i;
89
90 for (i = 0; i < server_capabilities_v2.argc; i++) {
91 const char *out;
92 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
93 (*out == '=')) {
94 *v = out + 1;
95 return 1;
96 }
97 }
98 return 0;
99}
100
f7e20501
BW
101int server_supports_feature(const char *c, const char *feature,
102 int die_on_error)
103{
104 int i;
105
106 for (i = 0; i < server_capabilities_v2.argc; i++) {
107 const char *out;
108 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
109 (!*out || *(out++) == '=')) {
110 if (parse_feature_request(out, feature))
111 return 1;
112 else
113 break;
114 }
115 }
116
117 if (die_on_error)
aad6fddb 118 die(_("server doesn't support feature '%s'"), feature);
f7e20501
BW
119
120 return 0;
121}
122
e52449b6
BW
123static void process_capabilities_v2(struct packet_reader *reader)
124{
125 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
126 argv_array_push(&server_capabilities_v2, reader->line);
127
128 if (reader->status != PACKET_READ_FLUSH)
aad6fddb 129 die(_("expected flush after capabilities"));
e52449b6
BW
130}
131
ad6ac124 132enum protocol_version discover_version(struct packet_reader *reader)
7e3e479b
BW
133{
134 enum protocol_version version = protocol_unknown_version;
135
136 /*
137 * Peek the first line of the server's response to
138 * determine the protocol version the server is speaking.
139 */
140 switch (packet_reader_peek(reader)) {
141 case PACKET_READ_EOF:
142 die_initial_contact(0);
143 case PACKET_READ_FLUSH:
144 case PACKET_READ_DELIM:
0181b600 145 case PACKET_READ_RESPONSE_END:
7e3e479b
BW
146 version = protocol_v0;
147 break;
148 case PACKET_READ_NORMAL:
149 version = determine_protocol_version_client(reader->line);
150 break;
151 }
152
153 switch (version) {
8f6982b4 154 case protocol_v2:
e52449b6 155 process_capabilities_v2(reader);
8f6982b4 156 break;
7e3e479b
BW
157 case protocol_v1:
158 /* Read the peeked version line */
159 packet_reader_read(reader);
160 break;
161 case protocol_v0:
162 break;
163 case protocol_unknown_version:
164 BUG("unknown protocol version");
165 }
166
167 return version;
168}
169
a45b5f05
JH
170static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
171{
172 char *sym, *target;
173 struct string_list_item *item;
174
175 if (!len)
176 return; /* just "symref" */
177 /* e.g. "symref=HEAD:refs/heads/master" */
5c0b13f8 178 sym = xmemdupz(val, len);
a45b5f05
JH
179 target = strchr(sym, ':');
180 if (!target)
181 /* just "symref=something" */
182 goto reject;
183 *(target++) = '\0';
184 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
185 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
186 /* "symref=bogus:pair */
187 goto reject;
ef4fe561 188 item = string_list_append_nodup(symref, sym);
a45b5f05
JH
189 item->util = target;
190 return;
191reject:
192 free(sym);
193 return;
194}
195
196static void annotate_refs_with_symref_info(struct ref *ref)
197{
198 struct string_list symref = STRING_LIST_INIT_DUP;
2c6a403d 199 int offset = 0;
a45b5f05 200
2c6a403d 201 while (1) {
a45b5f05
JH
202 int len;
203 const char *val;
204
2c6a403d 205 val = next_server_feature_value("symref", &len, &offset);
a45b5f05
JH
206 if (!val)
207 break;
208 parse_one_symref_info(&symref, val, len);
a45b5f05 209 }
3383e199 210 string_list_sort(&symref);
a45b5f05
JH
211
212 for (; ref; ref = ref->next) {
213 struct string_list_item *item;
214 item = string_list_lookup(&symref, ref->name);
215 if (!item)
216 continue;
217 ref->symref = xstrdup((char *)item->util);
218 }
219 string_list_clear(&symref, 0);
220}
221
92315e50 222static void process_capabilities(struct packet_reader *reader, int *linelen)
2609043d 223{
7c601dc3 224 const char *feat_val;
225 int feat_len;
92315e50 226 const char *line = reader->line;
7e3e479b 227 int nul_location = strlen(line);
92315e50 228 if (nul_location == *linelen)
0cd83283 229 return;
e52449b6 230 server_capabilities_v1 = xstrdup(line + nul_location + 1);
92315e50 231 *linelen = nul_location;
7c601dc3 232
233 feat_val = server_feature_value("object-format", &feat_len);
234 if (feat_val) {
235 char *hash_name = xstrndup(feat_val, feat_len);
236 int hash_algo = hash_algo_by_name(hash_name);
237 if (hash_algo != GIT_HASH_UNKNOWN)
238 reader->hash_algo = &hash_algos[hash_algo];
239 free(hash_name);
240 } else {
241 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
242 }
0cd83283
JT
243}
244
92315e50 245static int process_dummy_ref(const struct packet_reader *reader)
0cd83283 246{
92315e50 247 const char *line = reader->line;
0cd83283
JT
248 struct object_id oid;
249 const char *name;
250
7c601dc3 251 if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
0cd83283
JT
252 return 0;
253 if (*name != ' ')
254 return 0;
255 name++;
256
4a7e27e9 257 return oideq(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
0cd83283
JT
258}
259
7e3e479b 260static void check_no_capabilities(const char *line, int len)
0cd83283 261{
7e3e479b 262 if (strlen(line) != len)
aad6fddb 263 warning(_("ignoring capabilities after first line '%s'"),
7e3e479b 264 line + strlen(line));
0cd83283
JT
265}
266
92315e50 267static int process_ref(const struct packet_reader *reader, int len,
268 struct ref ***list, unsigned int flags,
269 struct oid_array *extra_have)
0cd83283 270{
92315e50 271 const char *line = reader->line;
0cd83283
JT
272 struct object_id old_oid;
273 const char *name;
274
7c601dc3 275 if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
0cd83283
JT
276 return 0;
277 if (*name != ' ')
278 return 0;
279 name++;
280
281 if (extra_have && !strcmp(name, ".have")) {
282 oid_array_append(extra_have, &old_oid);
283 } else if (!strcmp(name, "capabilities^{}")) {
aad6fddb 284 die(_("protocol error: unexpected capabilities^{}"));
0cd83283
JT
285 } else if (check_ref(name, flags)) {
286 struct ref *ref = alloc_ref(name);
287 oidcpy(&ref->old_oid, &old_oid);
288 **list = ref;
289 *list = &ref->next;
290 }
7e3e479b 291 check_no_capabilities(line, len);
0cd83283
JT
292 return 1;
293}
294
92315e50 295static int process_shallow(const struct packet_reader *reader, int len,
7e3e479b 296 struct oid_array *shallow_points)
0cd83283 297{
92315e50 298 const char *line = reader->line;
0cd83283
JT
299 const char *arg;
300 struct object_id old_oid;
301
7e3e479b 302 if (!skip_prefix(line, "shallow ", &arg))
0cd83283
JT
303 return 0;
304
7c601dc3 305 if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo))
aad6fddb 306 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
0cd83283 307 if (!shallow_points)
aad6fddb 308 die(_("repository on the other end cannot be shallow"));
0cd83283 309 oid_array_append(shallow_points, &old_oid);
7e3e479b 310 check_no_capabilities(line, len);
0cd83283
JT
311 return 1;
312}
313
7e3e479b
BW
314enum get_remote_heads_state {
315 EXPECTING_FIRST_REF = 0,
316 EXPECTING_REF,
317 EXPECTING_SHALLOW,
318 EXPECTING_DONE,
319};
320
d1c133f5
LT
321/*
322 * Read all the refs from the other end
323 */
ad6ac124 324struct ref **get_remote_heads(struct packet_reader *reader,
85edf4f5 325 struct ref **list, unsigned int flags,
910650d2 326 struct oid_array *extra_have,
327 struct oid_array *shallow_points)
d1c133f5 328{
a45b5f05 329 struct ref **orig_list = list;
7e3e479b
BW
330 int len = 0;
331 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
55e4f936 332
d1c133f5 333 *list = NULL;
1a7141ff 334
7e3e479b 335 while (state != EXPECTING_DONE) {
ad6ac124 336 switch (packet_reader_read(reader)) {
7e3e479b
BW
337 case PACKET_READ_EOF:
338 die_initial_contact(1);
339 case PACKET_READ_NORMAL:
ad6ac124 340 len = reader->pktlen;
7e3e479b
BW
341 break;
342 case PACKET_READ_FLUSH:
343 state = EXPECTING_DONE;
344 break;
345 case PACKET_READ_DELIM:
0181b600 346 case PACKET_READ_RESPONSE_END:
aad6fddb 347 die(_("invalid packet"));
7e3e479b
BW
348 }
349
0cd83283
JT
350 switch (state) {
351 case EXPECTING_FIRST_REF:
92315e50 352 process_capabilities(reader, &len);
353 if (process_dummy_ref(reader)) {
0cd83283
JT
354 state = EXPECTING_SHALLOW;
355 break;
356 }
357 state = EXPECTING_REF;
358 /* fallthrough */
359 case EXPECTING_REF:
92315e50 360 if (process_ref(reader, len, &list, flags, extra_have))
0cd83283
JT
361 break;
362 state = EXPECTING_SHALLOW;
363 /* fallthrough */
364 case EXPECTING_SHALLOW:
92315e50 365 if (process_shallow(reader, len, shallow_points))
0cd83283 366 break;
aad6fddb 367 die(_("protocol error: unexpected '%s'"), reader->line);
7e3e479b
BW
368 case EXPECTING_DONE:
369 break;
211b5f9e 370 }
d1c133f5 371 }
a45b5f05
JH
372
373 annotate_refs_with_symref_info(*orig_list);
374
d1c133f5
LT
375 return list;
376}
377
e52449b6 378/* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
67e9a707 379static int process_ref_v2(struct packet_reader *reader, struct ref ***list)
e52449b6
BW
380{
381 int ret = 1;
382 int i = 0;
383 struct object_id old_oid;
384 struct ref *ref;
385 struct string_list line_sections = STRING_LIST_INIT_DUP;
386 const char *end;
67e9a707 387 const char *line = reader->line;
e52449b6
BW
388
389 /*
390 * Ref lines have a number of fields which are space deliminated. The
391 * first field is the OID of the ref. The second field is the ref
392 * name. Subsequent fields (symref-target and peeled) are optional and
393 * don't have a particular order.
394 */
395 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
396 ret = 0;
397 goto out;
398 }
399
ab67235b 400 if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) ||
e52449b6
BW
401 *end) {
402 ret = 0;
403 goto out;
404 }
405
406 ref = alloc_ref(line_sections.items[i++].string);
407
ab67235b 408 memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
e52449b6
BW
409 **list = ref;
410 *list = &ref->next;
411
412 for (; i < line_sections.nr; i++) {
413 const char *arg = line_sections.items[i].string;
414 if (skip_prefix(arg, "symref-target:", &arg))
415 ref->symref = xstrdup(arg);
416
417 if (skip_prefix(arg, "peeled:", &arg)) {
418 struct object_id peeled_oid;
419 char *peeled_name;
420 struct ref *peeled;
ab67235b 421 if (parse_oid_hex_algop(arg, &peeled_oid, &end,
422 reader->hash_algo) || *end) {
e52449b6
BW
423 ret = 0;
424 goto out;
425 }
426
427 peeled_name = xstrfmt("%s^{}", ref->name);
428 peeled = alloc_ref(peeled_name);
429
ab67235b 430 memcpy(peeled->old_oid.hash, peeled_oid.hash,
431 reader->hash_algo->rawsz);
e52449b6
BW
432 **list = peeled;
433 *list = &peeled->next;
434
435 free(peeled_name);
436 }
437 }
438
439out:
440 string_list_clear(&line_sections, 0);
441 return ret;
442}
443
b0df0c16
DL
444void check_stateless_delimiter(int stateless_rpc,
445 struct packet_reader *reader,
446 const char *error)
447{
448 if (!stateless_rpc)
449 return; /* not in stateless mode, no delimiter expected */
450 if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END)
451 die("%s", error);
452}
453
e52449b6
BW
454struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
455 struct ref **list, int for_push,
ff473221 456 const struct argv_array *ref_prefixes,
b0df0c16
DL
457 const struct string_list *server_options,
458 int stateless_rpc)
e52449b6
BW
459{
460 int i;
ab67235b 461 const char *hash_name;
e52449b6
BW
462 *list = NULL;
463
464 if (server_supports_v2("ls-refs", 1))
465 packet_write_fmt(fd_out, "command=ls-refs\n");
466
467 if (server_supports_v2("agent", 0))
468 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
469
ab67235b 470 if (server_feature_v2("object-format", &hash_name)) {
471 int hash_algo = hash_algo_by_name(hash_name);
472 if (hash_algo == GIT_HASH_UNKNOWN)
473 die(_("unknown object format '%s' specified by server"), hash_name);
474 reader->hash_algo = &hash_algos[hash_algo];
475 packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
9de0dd36 476 } else {
477 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
ab67235b 478 }
479
ff473221
BW
480 if (server_options && server_options->nr &&
481 server_supports_v2("server-option", 1))
482 for (i = 0; i < server_options->nr; i++)
483 packet_write_fmt(fd_out, "server-option=%s",
484 server_options->items[i].string);
485
e52449b6
BW
486 packet_delim(fd_out);
487 /* When pushing we don't want to request the peeled tags */
488 if (!for_push)
489 packet_write_fmt(fd_out, "peel\n");
490 packet_write_fmt(fd_out, "symrefs\n");
491 for (i = 0; ref_prefixes && i < ref_prefixes->argc; i++) {
492 packet_write_fmt(fd_out, "ref-prefix %s\n",
493 ref_prefixes->argv[i]);
494 }
495 packet_flush(fd_out);
496
497 /* Process response from server */
498 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
67e9a707 499 if (!process_ref_v2(reader, &list))
aad6fddb 500 die(_("invalid ls-refs response: %s"), reader->line);
e52449b6
BW
501 }
502
503 if (reader->status != PACKET_READ_FLUSH)
aad6fddb 504 die(_("expected flush after ref listing"));
e52449b6 505
b0df0c16
DL
506 check_stateless_delimiter(stateless_rpc, reader,
507 _("expected response end packet after ref listing"));
508
e52449b6
BW
509 return list;
510}
511
84eca27a 512const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp, int *offset)
f47182c8
JH
513{
514 int len;
515
516 if (!feature_list)
517 return NULL;
518
519 len = strlen(feature);
2c6a403d 520 if (offset)
521 feature_list += *offset;
f47182c8
JH
522 while (*feature_list) {
523 const char *found = strstr(feature_list, feature);
524 if (!found)
525 return NULL;
94427108
JK
526 if (feature_list == found || isspace(found[-1])) {
527 const char *value = found + len;
528 /* feature with no value (e.g., "thin-pack") */
529 if (!*value || isspace(*value)) {
530 if (lenp)
531 *lenp = 0;
532 return value;
533 }
534 /* feature with a value (e.g., "agent=git/1.2.3") */
535 else if (*value == '=') {
2c6a403d 536 int end;
537
94427108 538 value++;
2c6a403d 539 end = strcspn(value, " \t\n");
94427108 540 if (lenp)
2c6a403d 541 *lenp = end;
542 if (offset)
543 *offset = value + end - feature_list;
94427108
JK
544 return value;
545 }
546 /*
547 * otherwise we matched a substring of another feature;
548 * keep looking
549 */
550 }
f47182c8
JH
551 feature_list = found + 1;
552 }
553 return NULL;
211b5f9e
JS
554}
555
122037c2 556int server_supports_hash(const char *desired, int *feature_supported)
557{
558 int offset = 0;
559 int len;
560 const char *hash;
561
562 hash = next_server_feature_value("object-format", &len, &offset);
563 if (feature_supported)
564 *feature_supported = !!hash;
565 if (!hash) {
566 hash = hash_algos[GIT_HASH_SHA1].name;
567 len = strlen(hash);
568 }
569 while (hash) {
570 if (!xstrncmpz(desired, hash, len))
571 return 1;
572
573 hash = next_server_feature_value("object-format", &len, &offset);
574 }
575 return 0;
576}
577
94427108
JK
578int parse_feature_request(const char *feature_list, const char *feature)
579{
2c6a403d 580 return !!parse_feature_value(feature_list, feature, NULL, NULL);
581}
582
583static const char *next_server_feature_value(const char *feature, int *len, int *offset)
584{
585 return parse_feature_value(server_capabilities_v1, feature, len, offset);
94427108
JK
586}
587
588const char *server_feature_value(const char *feature, int *len)
589{
2c6a403d 590 return parse_feature_value(server_capabilities_v1, feature, len, NULL);
94427108
JK
591}
592
593int server_supports(const char *feature)
594{
595 return !!server_feature_value(feature, NULL);
596}
597
2386d658
LT
598enum protocol {
599 PROTO_LOCAL = 1,
c59ab2e5 600 PROTO_FILE,
2386d658 601 PROTO_SSH,
4b05548f 602 PROTO_GIT
2386d658
LT
603};
604
c59ab2e5
TB
605int url_is_local_not_ssh(const char *url)
606{
607 const char *colon = strchr(url, ':');
608 const char *slash = strchr(url, '/');
609 return !colon || (slash && slash < colon) ||
f82a97eb 610 (has_dos_drive_prefix(url) && is_valid_path(url));
c59ab2e5
TB
611}
612
5610b7c0
TB
613static const char *prot_name(enum protocol protocol)
614{
615 switch (protocol) {
616 case PROTO_LOCAL:
c59ab2e5 617 case PROTO_FILE:
5610b7c0
TB
618 return "file";
619 case PROTO_SSH:
620 return "ssh";
621 case PROTO_GIT:
622 return "git";
623 default:
83e6bda3 624 return "unknown protocol";
5610b7c0
TB
625 }
626}
627
2386d658
LT
628static enum protocol get_protocol(const char *name)
629{
630 if (!strcmp(name, "ssh"))
631 return PROTO_SSH;
632 if (!strcmp(name, "git"))
633 return PROTO_GIT;
07c7782c 634 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
c05186cc 635 return PROTO_SSH;
07c7782c 636 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
c05186cc 637 return PROTO_SSH;
72a4f4b6 638 if (!strcmp(name, "file"))
c59ab2e5 639 return PROTO_FILE;
aad6fddb 640 die(_("protocol '%s' is not supported"), name);
2386d658
LT
641}
642
86ceb337
TB
643static char *host_end(char **hoststart, int removebrackets)
644{
645 char *host = *hoststart;
646 char *end;
647 char *start = strstr(host, "@[");
648 if (start)
649 start++; /* Jump over '@' */
650 else
651 start = host;
652 if (start[0] == '[') {
653 end = strchr(start + 1, ']');
654 if (end) {
655 if (removebrackets) {
656 *end = 0;
657 memmove(start, start + 1, end - start);
658 end++;
659 }
660 } else
661 end = host;
662 } else
663 end = host;
664 return end;
665}
666
5ba88448
YH
667#define STR_(s) # s
668#define STR(s) STR_(s)
2386d658 669
72a534da
ML
670static void get_host_and_port(char **host, const char **port)
671{
672 char *colon, *end;
86ceb337 673 end = host_end(host, 1);
72a534da 674 colon = strchr(end, ':');
72a534da 675 if (colon) {
86ceb337
TB
676 long portnr = strtol(colon + 1, &end, 10);
677 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
678 *colon = 0;
679 *port = colon + 1;
6b6c5f7a
TB
680 } else if (!colon[1]) {
681 *colon = 0;
86ceb337 682 }
72a534da
ML
683 }
684}
685
e47a8583
EW
686static void enable_keepalive(int sockfd)
687{
688 int ka = 1;
689
690 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
aad6fddb 691 error_errno(_("unable to set SO_KEEPALIVE on socket"));
e47a8583
EW
692}
693
49744d63 694#ifndef NO_IPV6
4c505f71 695
ba505322
AR
696static const char *ai_name(const struct addrinfo *ai)
697{
785a9857
BK
698 static char addr[NI_MAXHOST];
699 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
700 NI_NUMERICHOST) != 0)
5096d490 701 xsnprintf(addr, sizeof(addr), "(unknown)");
785a9857 702
ba505322
AR
703 return addr;
704}
705
5ad312be
JL
706/*
707 * Returns a connected socket() fd, or else die()s.
708 */
7841ce79 709static int git_tcp_connect_sock(char *host, int flags)
2386d658 710{
63a995b6
DZ
711 struct strbuf error_message = STRBUF_INIT;
712 int sockfd = -1;
554fe20d 713 const char *port = STR(DEFAULT_GIT_PORT);
5ba88448
YH
714 struct addrinfo hints, *ai0, *ai;
715 int gai;
ba505322 716 int cnt = 0;
5ba88448 717
72a534da
ML
718 get_host_and_port(&host, &port);
719 if (!*port)
720 port = "<none>";
5ba88448
YH
721
722 memset(&hints, 0, sizeof(hints));
c915f11e
EW
723 if (flags & CONNECT_IPV4)
724 hints.ai_family = AF_INET;
725 else if (flags & CONNECT_IPV6)
726 hints.ai_family = AF_INET6;
5ba88448
YH
727 hints.ai_socktype = SOCK_STREAM;
728 hints.ai_protocol = IPPROTO_TCP;
729
7841ce79 730 if (flags & CONNECT_VERBOSE)
aad6fddb 731 fprintf(stderr, _("Looking up %s ... "), host);
7841ce79 732
5ba88448
YH
733 gai = getaddrinfo(host, port, &hints, &ai);
734 if (gai)
aad6fddb 735 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
5ba88448 736
7841ce79 737 if (flags & CONNECT_VERBOSE)
aad6fddb
NTND
738 /* TRANSLATORS: this is the end of "Looking up %s ... " */
739 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
7841ce79 740
e08afecd 741 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
5ad312be
JL
742 sockfd = socket(ai->ai_family,
743 ai->ai_socktype, ai->ai_protocol);
63a995b6
DZ
744 if ((sockfd < 0) ||
745 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
746 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
747 host, cnt, ai_name(ai), strerror(errno));
748 if (0 <= sockfd)
749 close(sockfd);
5ba88448
YH
750 sockfd = -1;
751 continue;
2386d658 752 }
ba505322
AR
753 if (flags & CONNECT_VERBOSE)
754 fprintf(stderr, "%s ", ai_name(ai));
5ba88448 755 break;
2386d658
LT
756 }
757
5ba88448 758 freeaddrinfo(ai0);
2386d658 759
2386d658 760 if (sockfd < 0)
aad6fddb 761 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
5ba88448 762
e47a8583
EW
763 enable_keepalive(sockfd);
764
7841ce79 765 if (flags & CONNECT_VERBOSE)
aad6fddb
NTND
766 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
767 fprintf_ln(stderr, _("done."));
7841ce79 768
63a995b6
DZ
769 strbuf_release(&error_message);
770
5ad312be 771 return sockfd;
2386d658
LT
772}
773
49744d63 774#else /* NO_IPV6 */
4c505f71 775
5ad312be
JL
776/*
777 * Returns a connected socket() fd, or else die()s.
778 */
7841ce79 779static int git_tcp_connect_sock(char *host, int flags)
4c505f71 780{
7203a2d1
EFL
781 struct strbuf error_message = STRBUF_INIT;
782 int sockfd = -1;
72a534da
ML
783 const char *port = STR(DEFAULT_GIT_PORT);
784 char *ep;
4c505f71
PA
785 struct hostent *he;
786 struct sockaddr_in sa;
787 char **ap;
788 unsigned int nport;
ba505322 789 int cnt;
4c505f71 790
72a534da 791 get_host_and_port(&host, &port);
4c505f71 792
7841ce79 793 if (flags & CONNECT_VERBOSE)
aad6fddb 794 fprintf(stderr, _("Looking up %s ... "), host);
7841ce79 795
4c505f71
PA
796 he = gethostbyname(host);
797 if (!he)
aad6fddb 798 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
4c505f71
PA
799 nport = strtoul(port, &ep, 10);
800 if ( ep == port || *ep ) {
801 /* Not numeric */
802 struct servent *se = getservbyname(port,"tcp");
803 if ( !se )
aad6fddb 804 die(_("unknown port %s"), port);
4c505f71
PA
805 nport = se->s_port;
806 }
807
7841ce79 808 if (flags & CONNECT_VERBOSE)
aad6fddb
NTND
809 /* TRANSLATORS: this is the end of "Looking up %s ... " */
810 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
7841ce79 811
ba505322 812 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
4c505f71
PA
813 memset(&sa, 0, sizeof sa);
814 sa.sin_family = he->h_addrtype;
6573faff 815 sa.sin_port = htons(nport);
c6164218 816 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71 817
7203a2d1
EFL
818 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
819 if ((sockfd < 0) ||
820 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
821 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
ba505322
AR
822 host,
823 cnt,
824 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
7203a2d1
EFL
825 strerror(errno));
826 if (0 <= sockfd)
827 close(sockfd);
4c505f71
PA
828 sockfd = -1;
829 continue;
830 }
ba505322
AR
831 if (flags & CONNECT_VERBOSE)
832 fprintf(stderr, "%s ",
833 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
4c505f71
PA
834 break;
835 }
836
837 if (sockfd < 0)
aad6fddb 838 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
4c505f71 839
e47a8583
EW
840 enable_keepalive(sockfd);
841
7841ce79 842 if (flags & CONNECT_VERBOSE)
aad6fddb
NTND
843 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
844 fprintf_ln(stderr, _("done."));
7841ce79 845
5ad312be
JL
846 return sockfd;
847}
848
849#endif /* NO_IPV6 */
850
851
8e349780
JN
852/*
853 * Dummy child_process returned by git_connect() if the transport protocol
854 * does not need fork(2).
855 */
856static struct child_process no_fork = CHILD_PROCESS_INIT;
857
858int git_connection_is_socket(struct child_process *conn)
859{
860 return conn == &no_fork;
861}
862
863static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
5ad312be 864{
7841ce79 865 int sockfd = git_tcp_connect_sock(host, flags);
5ad312be 866
4c505f71 867 fd[0] = sockfd;
ec587fde 868 fd[1] = dup(sockfd);
8e349780
JN
869
870 return &no_fork;
4c505f71
PA
871}
872
4c505f71 873
96f1e58f 874static char *git_proxy_command;
f8014776 875
ef90d6d4
JS
876static int git_proxy_command_options(const char *var, const char *value,
877 void *cb)
f8014776 878{
e814bc4d 879 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
880 const char *for_pos;
881 int matchlen = -1;
882 int hostlen;
15112c95
EFL
883 const char *rhost_name = cb;
884 int rhost_len = strlen(rhost_name);
c3df8568 885
e814bc4d 886 if (git_proxy_command)
f8014776 887 return 0;
c64b9ad0
JH
888 if (!value)
889 return config_error_nonbool(var);
e814bc4d
JH
890 /* [core]
891 * ;# matches www.kernel.org as well
892 * gitproxy = netcatter-1 for kernel.org
893 * gitproxy = netcatter-2 for sample.xz
894 * gitproxy = netcatter-default
895 */
c3df8568 896 for_pos = strstr(value, " for ");
e814bc4d
JH
897 if (!for_pos)
898 /* matches everybody */
899 matchlen = strlen(value);
900 else {
901 hostlen = strlen(for_pos + 5);
902 if (rhost_len < hostlen)
903 matchlen = -1;
904 else if (!strncmp(for_pos + 5,
905 rhost_name + rhost_len - hostlen,
906 hostlen) &&
907 ((rhost_len == hostlen) ||
908 rhost_name[rhost_len - hostlen -1] == '.'))
909 matchlen = for_pos - value;
910 else
911 matchlen = -1;
912 }
913 if (0 <= matchlen) {
914 /* core.gitproxy = none for kernel.org */
a6080a0a 915 if (matchlen == 4 &&
e814bc4d
JH
916 !memcmp(value, "none", 4))
917 matchlen = 0;
182af834 918 git_proxy_command = xmemdupz(value, matchlen);
f8014776 919 }
e814bc4d 920 return 0;
f8014776
PC
921 }
922
ef90d6d4 923 return git_default_config(var, value, cb);
f8014776
PC
924}
925
e814bc4d 926static int git_use_proxy(const char *host)
f8014776
PC
927{
928 git_proxy_command = getenv("GIT_PROXY_COMMAND");
15112c95 929 git_config(git_proxy_command_options, (void*)host);
e814bc4d 930 return (git_proxy_command && *git_proxy_command);
f8014776
PC
931}
932
5cbf8246 933static struct child_process *git_proxy_connect(int fd[2], char *host)
f8014776 934{
554fe20d 935 const char *port = STR(DEFAULT_GIT_PORT);
5cbf8246 936 struct child_process *proxy;
f8014776 937
72a534da 938 get_host_and_port(&host, &port);
f8014776 939
3be4cf09 940 if (looks_like_command_line_option(host))
aad6fddb 941 die(_("strange hostname '%s' blocked"), host);
3be4cf09 942 if (looks_like_command_line_option(port))
aad6fddb 943 die(_("strange port '%s' blocked"), port);
3be4cf09 944
483bbd4e
RS
945 proxy = xmalloc(sizeof(*proxy));
946 child_process_init(proxy);
1823bea1
JK
947 argv_array_push(&proxy->args, git_proxy_command);
948 argv_array_push(&proxy->args, host);
949 argv_array_push(&proxy->args, port);
5cbf8246
JK
950 proxy->in = -1;
951 proxy->out = -1;
952 if (start_command(proxy))
aad6fddb 953 die(_("cannot start proxy %s"), git_proxy_command);
5cbf8246
JK
954 fd[0] = proxy->out; /* read from proxy stdout */
955 fd[1] = proxy->in; /* write to proxy stdin */
956 return proxy;
f8014776
PC
957}
958
86ceb337 959static char *get_port(char *host)
2e776665
LT
960{
961 char *end;
86ceb337
TB
962 char *p = strchr(host, ':');
963
2e776665 964 if (p) {
8f148253
RS
965 long port = strtol(p + 1, &end, 10);
966 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
86ceb337
TB
967 *p = '\0';
968 return p+1;
2e776665
LT
969 }
970 }
971
972 return NULL;
973}
974
f7192598 975/*
cabc3c12
JS
976 * Extract protocol and relevant parts from the specified connection URL.
977 * The caller must free() the returned strings.
f7192598 978 */
cabc3c12 979static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
83b05875 980 char **ret_path)
f7192598 981{
9d2e9420 982 char *url;
8e76bf3f 983 char *host, *path;
356bece0 984 char *end;
c59ab2e5 985 int separator = '/';
faea9ccb 986 enum protocol protocol = PROTO_LOCAL;
f0b7367c 987
9d2e9420
JK
988 if (is_url(url_orig))
989 url = url_decode(url_orig);
990 else
991 url = xstrdup(url_orig);
992
faea9ccb 993 host = strstr(url, "://");
eeefa7c9 994 if (host) {
faea9ccb
AE
995 *host = '\0';
996 protocol = get_protocol(url);
997 host += 3;
356bece0 998 } else {
f7192598 999 host = url;
c59ab2e5
TB
1000 if (!url_is_local_not_ssh(url)) {
1001 protocol = PROTO_SSH;
1002 separator = ':';
1003 }
356bece0
YH
1004 }
1005
9aa5053d 1006 /*
83b05875
TB
1007 * Don't do destructive transforms as protocol code does
1008 * '[]' unwrapping in get_host_and_port()
9aa5053d 1009 */
86ceb337 1010 end = host_end(&host, 0);
356bece0 1011
c59ab2e5 1012 if (protocol == PROTO_LOCAL)
72a4f4b6 1013 path = end;
ebb8d2c9
TB
1014 else if (protocol == PROTO_FILE && *host != '/' &&
1015 !has_dos_drive_prefix(host) &&
1016 offset_1st_component(host - 2) > 1)
1017 path = host - 2; /* include the leading "//" */
c59ab2e5
TB
1018 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
1019 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1020 else
1021 path = strchr(end, separator);
2386d658 1022
faea9ccb 1023 if (!path || !*path)
aad6fddb 1024 die(_("no path specified; see 'git help pull' for valid url syntax"));
faea9ccb
AE
1025
1026 /*
1027 * null-terminate hostname and point path to ~ for URL's like this:
1028 * ssh://host.xz/~user/repo
1029 */
c59ab2e5
TB
1030
1031 end = path; /* Need to \0 terminate host here */
1032 if (separator == ':')
1033 path++; /* path starts after ':' */
1034 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
faea9ccb
AE
1035 if (path[1] == '~')
1036 path++;
faea9ccb
AE
1037 }
1038
c59ab2e5
TB
1039 path = xstrdup(path);
1040 *end = '\0';
1041
cabc3c12 1042 *ret_host = xstrdup(host);
c59ab2e5 1043 *ret_path = path;
cabc3c12
JS
1044 free(url);
1045 return protocol;
1046}
1047
3c8ede3f
NTND
1048static const char *get_ssh_command(void)
1049{
1050 const char *ssh;
1051
1052 if ((ssh = getenv("GIT_SSH_COMMAND")))
1053 return ssh;
1054
1055 if (!git_config_get_string_const("core.sshcommand", &ssh))
1056 return ssh;
1057
1058 return NULL;
1059}
1060
94b8ae5a 1061enum ssh_variant {
0da0e49b 1062 VARIANT_AUTO,
94b8ae5a
BW
1063 VARIANT_SIMPLE,
1064 VARIANT_SSH,
1065 VARIANT_PLINK,
1066 VARIANT_PUTTY,
1067 VARIANT_TORTOISEPLINK,
1068};
1069
0da0e49b 1070static void override_ssh_variant(enum ssh_variant *ssh_variant)
e2824e47 1071{
94b8ae5a 1072 const char *variant = getenv("GIT_SSH_VARIANT");
486c8e8c 1073
94b8ae5a 1074 if (!variant && git_config_get_string_const("ssh.variant", &variant))
0da0e49b 1075 return;
486c8e8c 1076
0da0e49b
JN
1077 if (!strcmp(variant, "auto"))
1078 *ssh_variant = VARIANT_AUTO;
1079 else if (!strcmp(variant, "plink"))
94b8ae5a
BW
1080 *ssh_variant = VARIANT_PLINK;
1081 else if (!strcmp(variant, "putty"))
1082 *ssh_variant = VARIANT_PUTTY;
1083 else if (!strcmp(variant, "tortoiseplink"))
1084 *ssh_variant = VARIANT_TORTOISEPLINK;
1085 else if (!strcmp(variant, "simple"))
1086 *ssh_variant = VARIANT_SIMPLE;
1087 else
1088 *ssh_variant = VARIANT_SSH;
486c8e8c
JH
1089}
1090
94b8ae5a
BW
1091static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1092 int is_cmdline)
486c8e8c 1093{
0da0e49b 1094 enum ssh_variant ssh_variant = VARIANT_AUTO;
486c8e8c 1095 const char *variant;
e2824e47
JS
1096 char *p = NULL;
1097
0da0e49b
JN
1098 override_ssh_variant(&ssh_variant);
1099
1100 if (ssh_variant != VARIANT_AUTO)
94b8ae5a 1101 return ssh_variant;
486c8e8c
JH
1102
1103 if (!is_cmdline) {
e2824e47
JS
1104 p = xstrdup(ssh_command);
1105 variant = basename(p);
1106 } else {
1107 const char **ssh_argv;
1108
1109 p = xstrdup(ssh_command);
22e5ae5c 1110 if (split_cmdline(p, &ssh_argv) > 0) {
e2824e47
JS
1111 variant = basename((char *)ssh_argv[0]);
1112 /*
1113 * At this point, variant points into the buffer
1114 * referenced by p, hence we do not need ssh_argv
1115 * any longer.
1116 */
1117 free(ssh_argv);
5d2993b6
JK
1118 } else {
1119 free(p);
94b8ae5a 1120 return ssh_variant;
5d2993b6 1121 }
e2824e47
JS
1122 }
1123
94b8ae5a
BW
1124 if (!strcasecmp(variant, "ssh") ||
1125 !strcasecmp(variant, "ssh.exe"))
1126 ssh_variant = VARIANT_SSH;
1127 else if (!strcasecmp(variant, "plink") ||
1128 !strcasecmp(variant, "plink.exe"))
1129 ssh_variant = VARIANT_PLINK;
e2824e47 1130 else if (!strcasecmp(variant, "tortoiseplink") ||
94b8ae5a
BW
1131 !strcasecmp(variant, "tortoiseplink.exe"))
1132 ssh_variant = VARIANT_TORTOISEPLINK;
1133
e2824e47 1134 free(p);
94b8ae5a 1135 return ssh_variant;
e2824e47
JS
1136}
1137
2ac67cb6
JN
1138/*
1139 * Open a connection using Git's native protocol.
1140 *
1141 * The caller is responsible for freeing hostandport, but this function may
1142 * modify it (for example, to truncate it to remove the port part).
1143 */
1144static struct child_process *git_connect_git(int fd[2], char *hostandport,
1145 const char *path, const char *prog,
40fc51e3 1146 enum protocol_version version,
2ac67cb6
JN
1147 int flags)
1148{
1149 struct child_process *conn;
1150 struct strbuf request = STRBUF_INIT;
1151 /*
1152 * Set up virtual host information based on where we will
1153 * connect, unless the user has overridden us in
1154 * the environment.
1155 */
1156 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1157 if (target_host)
1158 target_host = xstrdup(target_host);
1159 else
1160 target_host = xstrdup(hostandport);
1161
1162 transport_check_allowed("git");
1163
233cd282
JN
1164 /*
1165 * These underlying connection commands die() if they
2ac67cb6
JN
1166 * cannot connect.
1167 */
1168 if (git_use_proxy(hostandport))
1169 conn = git_proxy_connect(fd, hostandport);
1170 else
1171 conn = git_tcp_connect(fd, hostandport, flags);
1172 /*
1173 * Separate original protocol components prog and path
1174 * from extended host header with a NUL byte.
1175 *
1176 * Note: Do not add any other headers here! Doing so
1177 * will cause older git-daemon servers to crash.
1178 */
1179 strbuf_addf(&request,
1180 "%s %s%chost=%s%c",
1181 prog, path, 0,
1182 target_host, 0);
1183
1184 /* If using a new version put that stuff here after a second null byte */
40fc51e3 1185 if (version > 0) {
2ac67cb6
JN
1186 strbuf_addch(&request, '\0');
1187 strbuf_addf(&request, "version=%d%c",
40fc51e3 1188 version, '\0');
2ac67cb6
JN
1189 }
1190
1191 packet_write(fd[1], request.buf, request.len);
1192
1193 free(target_host);
1194 strbuf_release(&request);
1195 return conn;
1196}
1197
957e2ad2
JN
1198/*
1199 * Append the appropriate environment variables to `env` and options to
1200 * `args` for running ssh in Git's SSH-tunneled transport.
1201 */
1202static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1203 enum ssh_variant variant, const char *port,
40fc51e3 1204 enum protocol_version version, int flags)
957e2ad2
JN
1205{
1206 if (variant == VARIANT_SSH &&
40fc51e3 1207 version > 0) {
957e2ad2
JN
1208 argv_array_push(args, "-o");
1209 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1210 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
40fc51e3 1211 version);
957e2ad2
JN
1212 }
1213
a3f5b66f
JN
1214 if (flags & CONNECT_IPV4) {
1215 switch (variant) {
1216 case VARIANT_AUTO:
1217 BUG("VARIANT_AUTO passed to push_ssh_options");
1218 case VARIANT_SIMPLE:
aad6fddb 1219 die(_("ssh variant 'simple' does not support -4"));
a3f5b66f
JN
1220 case VARIANT_SSH:
1221 case VARIANT_PLINK:
1222 case VARIANT_PUTTY:
1223 case VARIANT_TORTOISEPLINK:
957e2ad2 1224 argv_array_push(args, "-4");
a3f5b66f
JN
1225 }
1226 } else if (flags & CONNECT_IPV6) {
1227 switch (variant) {
1228 case VARIANT_AUTO:
1229 BUG("VARIANT_AUTO passed to push_ssh_options");
1230 case VARIANT_SIMPLE:
aad6fddb 1231 die(_("ssh variant 'simple' does not support -6"));
a3f5b66f
JN
1232 case VARIANT_SSH:
1233 case VARIANT_PLINK:
1234 case VARIANT_PUTTY:
1235 case VARIANT_TORTOISEPLINK:
957e2ad2 1236 argv_array_push(args, "-6");
a3f5b66f 1237 }
957e2ad2
JN
1238 }
1239
1240 if (variant == VARIANT_TORTOISEPLINK)
1241 argv_array_push(args, "-batch");
1242
3fa5e0d0
JN
1243 if (port) {
1244 switch (variant) {
1245 case VARIANT_AUTO:
1246 BUG("VARIANT_AUTO passed to push_ssh_options");
1247 case VARIANT_SIMPLE:
aad6fddb 1248 die(_("ssh variant 'simple' does not support setting port"));
3fa5e0d0 1249 case VARIANT_SSH:
957e2ad2 1250 argv_array_push(args, "-p");
3fa5e0d0
JN
1251 break;
1252 case VARIANT_PLINK:
1253 case VARIANT_PUTTY:
1254 case VARIANT_TORTOISEPLINK:
957e2ad2 1255 argv_array_push(args, "-P");
3fa5e0d0 1256 }
957e2ad2
JN
1257
1258 argv_array_push(args, port);
1259 }
1260}
1261
fce54ce4
JN
1262/* Prepare a child_process for use by Git's SSH-tunneled transport. */
1263static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
40fc51e3
BW
1264 const char *port, enum protocol_version version,
1265 int flags)
fce54ce4
JN
1266{
1267 const char *ssh;
1268 enum ssh_variant variant;
1269
1270 if (looks_like_command_line_option(ssh_host))
aad6fddb 1271 die(_("strange hostname '%s' blocked"), ssh_host);
fce54ce4
JN
1272
1273 ssh = get_ssh_command();
1274 if (ssh) {
1275 variant = determine_ssh_variant(ssh, 1);
1276 } else {
1277 /*
1278 * GIT_SSH is the no-shell version of
1279 * GIT_SSH_COMMAND (and must remain so for
1280 * historical compatibility).
1281 */
1282 conn->use_shell = 0;
1283
1284 ssh = getenv("GIT_SSH");
1285 if (!ssh)
1286 ssh = "ssh";
1287 variant = determine_ssh_variant(ssh, 0);
1288 }
1289
0da0e49b
JN
1290 if (variant == VARIANT_AUTO) {
1291 struct child_process detect = CHILD_PROCESS_INIT;
1292
1293 detect.use_shell = conn->use_shell;
1294 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1295
1296 argv_array_push(&detect.args, ssh);
1297 argv_array_push(&detect.args, "-G");
1298 push_ssh_options(&detect.args, &detect.env_array,
40fc51e3 1299 VARIANT_SSH, port, version, flags);
0da0e49b
JN
1300 argv_array_push(&detect.args, ssh_host);
1301
1302 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1303 }
1304
fce54ce4 1305 argv_array_push(&conn->args, ssh);
40fc51e3 1306 push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
fce54ce4
JN
1307 argv_array_push(&conn->args, ssh_host);
1308}
1309
cabc3c12 1310/*
8e349780
JN
1311 * This returns the dummy child_process `no_fork` if the transport protocol
1312 * does not need fork(2), or a struct child_process object if it does. Once
1313 * done, finish the connection with finish_connect() with the value returned
1314 * from this function (it is safe to call finish_connect() with NULL to
1315 * support the former case).
cabc3c12
JS
1316 *
1317 * If it returns, the connect is successful; it just dies on errors (this
1318 * will hopefully be changed in a libification effort, to return NULL when
1319 * the connection failed).
1320 */
1321struct child_process *git_connect(int fd[2], const char *url,
1322 const char *prog, int flags)
1323{
a2036d7e 1324 char *hostandport, *path;
8e349780 1325 struct child_process *conn;
cabc3c12 1326 enum protocol protocol;
40fc51e3 1327 enum protocol_version version = get_protocol_version_config();
cabc3c12 1328
1aa8dded
BW
1329 /*
1330 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1331 * to perform a push, then fallback to v0 since the client doesn't know
1332 * how to push yet using v2.
1333 */
1334 if (version == protocol_v2 && !strcmp("git-receive-pack", prog))
1335 version = protocol_v0;
1336
cabc3c12
JS
1337 /* Without this we cannot rely on waitpid() to tell
1338 * what happened to our children.
2e776665 1339 */
cabc3c12 1340 signal(SIGCHLD, SIG_DFL);
2e776665 1341
a2036d7e 1342 protocol = parse_connect_url(url, &hostandport, &path);
3f55ccab 1343 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
5610b7c0
TB
1344 printf("Diag: url=%s\n", url ? url : "NULL");
1345 printf("Diag: protocol=%s\n", prot_name(protocol));
a2036d7e 1346 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
5610b7c0 1347 printf("Diag: path=%s\n", path ? path : "NULL");
a2036d7e
TB
1348 conn = NULL;
1349 } else if (protocol == PROTO_GIT) {
40fc51e3 1350 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
abd81a3d 1351 conn->trace2_child_class = "transport/git";
a2036d7e 1352 } else {
f1399291 1353 struct strbuf cmd = STRBUF_INIT;
0c2f0d27 1354 const char *const *var;
f1399291 1355
483bbd4e
RS
1356 conn = xmalloc(sizeof(*conn));
1357 child_process_init(conn);
a2036d7e 1358
aeeb2d49 1359 if (looks_like_command_line_option(path))
aad6fddb 1360 die(_("strange pathname '%s' blocked"), path);
aeeb2d49 1361
a2036d7e
TB
1362 strbuf_addstr(&cmd, prog);
1363 strbuf_addch(&cmd, ' ');
1364 sq_quote_buf(&cmd, path);
1365
aab40438 1366 /* remove repo-local variables from the environment */
0c2f0d27
BW
1367 for (var = local_repo_env; *var; var++)
1368 argv_array_push(&conn->env_array, *var);
1369
a48b409f 1370 conn->use_shell = 1;
a2036d7e 1371 conn->in = conn->out = -1;
a2036d7e 1372 if (protocol == PROTO_SSH) {
a2036d7e
TB
1373 char *ssh_host = hostandport;
1374 const char *port = NULL;
a5adaced 1375 transport_check_allowed("ssh");
a2036d7e 1376 get_host_and_port(&ssh_host, &port);
a2036d7e 1377
86ceb337
TB
1378 if (!port)
1379 port = get_port(ssh_host);
42da4840 1380
3f55ccab
TB
1381 if (flags & CONNECT_DIAG_URL) {
1382 printf("Diag: url=%s\n", url ? url : "NULL");
1383 printf("Diag: protocol=%s\n", prot_name(protocol));
1384 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1385 printf("Diag: port=%s\n", port ? port : "NONE");
1386 printf("Diag: path=%s\n", path ? path : "NULL");
a2036d7e 1387
3f55ccab
TB
1388 free(hostandport);
1389 free(path);
04f20c04 1390 free(conn);
f1399291 1391 strbuf_release(&cmd);
3f55ccab 1392 return NULL;
37ee646e 1393 }
abd81a3d 1394 conn->trace2_child_class = "transport/ssh";
40fc51e3 1395 fill_ssh_args(conn, ssh_host, port, version, flags);
c049b61d 1396 } else {
a5adaced 1397 transport_check_allowed("file");
abd81a3d 1398 conn->trace2_child_class = "transport/file";
40fc51e3 1399 if (version > 0) {
0c2f0d27 1400 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
40fc51e3 1401 version);
0c2f0d27 1402 }
4852f723 1403 }
1823bea1 1404 argv_array_push(&conn->args, cmd.buf);
f364cb88 1405
a2036d7e 1406 if (start_command(conn))
aad6fddb 1407 die(_("unable to fork"));
f364cb88 1408
a2036d7e
TB
1409 fd[0] = conn->out; /* read from child's stdout */
1410 fd[1] = conn->in; /* write to child's stdin */
1411 strbuf_release(&cmd);
1412 }
1413 free(hostandport);
cabc3c12 1414 free(path);
98158e9c 1415 return conn;
f7192598
LT
1416}
1417
98158e9c 1418int finish_connect(struct child_process *conn)
f7192598 1419{
f364cb88 1420 int code;
7ffe853b 1421 if (!conn || git_connection_is_socket(conn))
f42a5c4e
FBH
1422 return 0;
1423
f364cb88 1424 code = finish_command(conn);
98158e9c 1425 free(conn);
f364cb88 1426 return code;
f7192598 1427}