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