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