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