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