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