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