Merge branch 'tb/object-access-overflow-protection'
[git/debian.git] / connect.c
blob0d77737a5363c39fd71376c0f483bc8808538754
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "pkt-line.h"
7 #include "quote.h"
8 #include "refs.h"
9 #include "run-command.h"
10 #include "remote.h"
11 #include "connect.h"
12 #include "url.h"
13 #include "string-list.h"
14 #include "oid-array.h"
15 #include "path.h"
16 #include "transport.h"
17 #include "trace2.h"
18 #include "strbuf.h"
19 #include "version.h"
20 #include "protocol.h"
21 #include "alias.h"
22 #include "bundle-uri.h"
24 static char *server_capabilities_v1;
25 static struct strvec server_capabilities_v2 = STRVEC_INIT;
26 static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset);
28 static int check_ref(const char *name, unsigned int flags)
30 if (!flags)
31 return 1;
33 if (!skip_prefix(name, "refs/", &name))
34 return 0;
36 /* REF_NORMAL means that we don't want the magic fake tag refs */
37 if ((flags & REF_NORMAL) && check_refname_format(name,
38 REFNAME_ALLOW_ONELEVEL))
39 return 0;
41 /* REF_HEADS means that we want regular branch heads */
42 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
43 return 1;
45 /* REF_TAGS means that we want tags */
46 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
47 return 1;
49 /* All type bits clear means that we are ok with anything */
50 return !(flags & ~REF_NORMAL);
53 int check_ref_type(const struct ref *ref, int flags)
55 return check_ref(ref->name, flags);
58 static NORETURN void die_initial_contact(int unexpected)
61 * A hang-up after seeing some response from the other end
62 * means that it is unexpected, as we know the other end is
63 * willing to talk to us. A hang-up before seeing any
64 * response does not necessarily mean an ACL problem, though.
66 if (unexpected)
67 die(_("the remote end hung up upon initial contact"));
68 else
69 die(_("Could not read from remote repository.\n\n"
70 "Please make sure you have the correct access rights\n"
71 "and the repository exists."));
74 /* Checks if the server supports the capability 'c' */
75 int server_supports_v2(const char *c)
77 int i;
79 for (i = 0; i < server_capabilities_v2.nr; i++) {
80 const char *out;
81 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
82 (!*out || *out == '='))
83 return 1;
85 return 0;
88 void ensure_server_supports_v2(const char *c)
90 if (!server_supports_v2(c))
91 die(_("server doesn't support '%s'"), c);
94 int server_feature_v2(const char *c, const char **v)
96 int i;
98 for (i = 0; i < server_capabilities_v2.nr; i++) {
99 const char *out;
100 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
101 (*out == '=')) {
102 *v = out + 1;
103 return 1;
106 return 0;
109 int server_supports_feature(const char *c, const char *feature,
110 int die_on_error)
112 int i;
114 for (i = 0; i < server_capabilities_v2.nr; i++) {
115 const char *out;
116 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
117 (!*out || *(out++) == '=')) {
118 if (parse_feature_request(out, feature))
119 return 1;
120 else
121 break;
125 if (die_on_error)
126 die(_("server doesn't support feature '%s'"), feature);
128 return 0;
131 static void process_capabilities_v2(struct packet_reader *reader)
133 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
134 strvec_push(&server_capabilities_v2, reader->line);
136 if (reader->status != PACKET_READ_FLUSH)
137 die(_("expected flush after capabilities"));
140 enum protocol_version discover_version(struct packet_reader *reader)
142 enum protocol_version version = protocol_unknown_version;
145 * Peek the first line of the server's response to
146 * determine the protocol version the server is speaking.
148 switch (packet_reader_peek(reader)) {
149 case PACKET_READ_EOF:
150 die_initial_contact(0);
151 case PACKET_READ_FLUSH:
152 case PACKET_READ_DELIM:
153 case PACKET_READ_RESPONSE_END:
154 version = protocol_v0;
155 break;
156 case PACKET_READ_NORMAL:
157 version = determine_protocol_version_client(reader->line);
158 break;
161 switch (version) {
162 case protocol_v2:
163 process_capabilities_v2(reader);
164 break;
165 case protocol_v1:
166 /* Read the peeked version line */
167 packet_reader_read(reader);
168 break;
169 case protocol_v0:
170 break;
171 case protocol_unknown_version:
172 BUG("unknown protocol version");
175 trace2_data_intmax("transfer", NULL, "negotiated-version", version);
177 return version;
180 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
182 char *sym, *target;
183 struct string_list_item *item;
185 if (!len)
186 return; /* just "symref" */
187 /* e.g. "symref=HEAD:refs/heads/master" */
188 sym = xmemdupz(val, len);
189 target = strchr(sym, ':');
190 if (!target)
191 /* just "symref=something" */
192 goto reject;
193 *(target++) = '\0';
194 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
195 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
196 /* "symref=bogus:pair */
197 goto reject;
198 item = string_list_append_nodup(symref, sym);
199 item->util = target;
200 return;
201 reject:
202 free(sym);
203 return;
206 static void annotate_refs_with_symref_info(struct ref *ref)
208 struct string_list symref = STRING_LIST_INIT_DUP;
209 size_t offset = 0;
211 while (1) {
212 size_t len;
213 const char *val;
215 val = next_server_feature_value("symref", &len, &offset);
216 if (!val)
217 break;
218 parse_one_symref_info(&symref, val, len);
220 string_list_sort(&symref);
222 for (; ref; ref = ref->next) {
223 struct string_list_item *item;
224 item = string_list_lookup(&symref, ref->name);
225 if (!item)
226 continue;
227 ref->symref = xstrdup((char *)item->util);
229 string_list_clear(&symref, 0);
232 static void process_capabilities(struct packet_reader *reader, int *linelen)
234 const char *feat_val;
235 size_t feat_len;
236 const char *line = reader->line;
237 int nul_location = strlen(line);
238 if (nul_location == *linelen)
239 return;
240 server_capabilities_v1 = xstrdup(line + nul_location + 1);
241 *linelen = nul_location;
243 feat_val = server_feature_value("object-format", &feat_len);
244 if (feat_val) {
245 char *hash_name = xstrndup(feat_val, feat_len);
246 int hash_algo = hash_algo_by_name(hash_name);
247 if (hash_algo != GIT_HASH_UNKNOWN)
248 reader->hash_algo = &hash_algos[hash_algo];
249 free(hash_name);
250 } else {
251 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
255 static int process_dummy_ref(const struct packet_reader *reader)
257 const char *line = reader->line;
258 struct object_id oid;
259 const char *name;
261 if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
262 return 0;
263 if (*name != ' ')
264 return 0;
265 name++;
267 return oideq(reader->hash_algo->null_oid, &oid) &&
268 !strcmp(name, "capabilities^{}");
271 static void check_no_capabilities(const char *line, int len)
273 if (strlen(line) != len)
274 warning(_("ignoring capabilities after first line '%s'"),
275 line + strlen(line));
278 static int process_ref(const struct packet_reader *reader, int len,
279 struct ref ***list, unsigned int flags,
280 struct oid_array *extra_have)
282 const char *line = reader->line;
283 struct object_id old_oid;
284 const char *name;
286 if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
287 return 0;
288 if (*name != ' ')
289 return 0;
290 name++;
292 if (extra_have && !strcmp(name, ".have")) {
293 oid_array_append(extra_have, &old_oid);
294 } else if (!strcmp(name, "capabilities^{}")) {
295 die(_("protocol error: unexpected capabilities^{}"));
296 } else if (check_ref(name, flags)) {
297 struct ref *ref = alloc_ref(name);
298 oidcpy(&ref->old_oid, &old_oid);
299 **list = ref;
300 *list = &ref->next;
302 check_no_capabilities(line, len);
303 return 1;
306 static int process_shallow(const struct packet_reader *reader, int len,
307 struct oid_array *shallow_points)
309 const char *line = reader->line;
310 const char *arg;
311 struct object_id old_oid;
313 if (!skip_prefix(line, "shallow ", &arg))
314 return 0;
316 if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo))
317 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
318 if (!shallow_points)
319 die(_("repository on the other end cannot be shallow"));
320 oid_array_append(shallow_points, &old_oid);
321 check_no_capabilities(line, len);
322 return 1;
325 enum get_remote_heads_state {
326 EXPECTING_FIRST_REF = 0,
327 EXPECTING_REF,
328 EXPECTING_SHALLOW,
329 EXPECTING_DONE,
333 * Read all the refs from the other end
335 struct ref **get_remote_heads(struct packet_reader *reader,
336 struct ref **list, unsigned int flags,
337 struct oid_array *extra_have,
338 struct oid_array *shallow_points)
340 struct ref **orig_list = list;
341 int len = 0;
342 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
344 *list = NULL;
346 while (state != EXPECTING_DONE) {
347 switch (packet_reader_read(reader)) {
348 case PACKET_READ_EOF:
349 die_initial_contact(1);
350 case PACKET_READ_NORMAL:
351 len = reader->pktlen;
352 break;
353 case PACKET_READ_FLUSH:
354 state = EXPECTING_DONE;
355 break;
356 case PACKET_READ_DELIM:
357 case PACKET_READ_RESPONSE_END:
358 die(_("invalid packet"));
361 switch (state) {
362 case EXPECTING_FIRST_REF:
363 process_capabilities(reader, &len);
364 if (process_dummy_ref(reader)) {
365 state = EXPECTING_SHALLOW;
366 break;
368 state = EXPECTING_REF;
369 /* fallthrough */
370 case EXPECTING_REF:
371 if (process_ref(reader, len, &list, flags, extra_have))
372 break;
373 state = EXPECTING_SHALLOW;
374 /* fallthrough */
375 case EXPECTING_SHALLOW:
376 if (process_shallow(reader, len, shallow_points))
377 break;
378 die(_("protocol error: unexpected '%s'"), reader->line);
379 case EXPECTING_DONE:
380 break;
384 annotate_refs_with_symref_info(*orig_list);
386 return list;
389 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
390 static int process_ref_v2(struct packet_reader *reader, struct ref ***list,
391 const char **unborn_head_target)
393 int ret = 1;
394 int i = 0;
395 struct object_id old_oid;
396 struct ref *ref;
397 struct string_list line_sections = STRING_LIST_INIT_DUP;
398 const char *end;
399 const char *line = reader->line;
402 * Ref lines have a number of fields which are space deliminated. The
403 * first field is the OID of the ref. The second field is the ref
404 * name. Subsequent fields (symref-target and peeled) are optional and
405 * don't have a particular order.
407 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
408 ret = 0;
409 goto out;
412 if (!strcmp("unborn", line_sections.items[i].string)) {
413 i++;
414 if (unborn_head_target &&
415 !strcmp("HEAD", line_sections.items[i++].string)) {
417 * Look for the symref target (if any). If found,
418 * return it to the caller.
420 for (; i < line_sections.nr; i++) {
421 const char *arg = line_sections.items[i].string;
423 if (skip_prefix(arg, "symref-target:", &arg)) {
424 *unborn_head_target = xstrdup(arg);
425 break;
429 goto out;
431 if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) ||
432 *end) {
433 ret = 0;
434 goto out;
437 ref = alloc_ref(line_sections.items[i++].string);
439 memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
440 **list = ref;
441 *list = &ref->next;
443 for (; i < line_sections.nr; i++) {
444 const char *arg = line_sections.items[i].string;
445 if (skip_prefix(arg, "symref-target:", &arg))
446 ref->symref = xstrdup(arg);
448 if (skip_prefix(arg, "peeled:", &arg)) {
449 struct object_id peeled_oid;
450 char *peeled_name;
451 struct ref *peeled;
452 if (parse_oid_hex_algop(arg, &peeled_oid, &end,
453 reader->hash_algo) || *end) {
454 ret = 0;
455 goto out;
458 peeled_name = xstrfmt("%s^{}", ref->name);
459 peeled = alloc_ref(peeled_name);
461 memcpy(peeled->old_oid.hash, peeled_oid.hash,
462 reader->hash_algo->rawsz);
463 **list = peeled;
464 *list = &peeled->next;
466 free(peeled_name);
470 out:
471 string_list_clear(&line_sections, 0);
472 return ret;
475 void check_stateless_delimiter(int stateless_rpc,
476 struct packet_reader *reader,
477 const char *error)
479 if (!stateless_rpc)
480 return; /* not in stateless mode, no delimiter expected */
481 if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END)
482 die("%s", error);
485 static void send_capabilities(int fd_out, struct packet_reader *reader)
487 const char *hash_name;
489 if (server_supports_v2("agent"))
490 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
492 if (server_feature_v2("object-format", &hash_name)) {
493 int hash_algo = hash_algo_by_name(hash_name);
494 if (hash_algo == GIT_HASH_UNKNOWN)
495 die(_("unknown object format '%s' specified by server"), hash_name);
496 reader->hash_algo = &hash_algos[hash_algo];
497 packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
498 } else {
499 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
503 int get_remote_bundle_uri(int fd_out, struct packet_reader *reader,
504 struct bundle_list *bundles, int stateless_rpc)
506 int line_nr = 1;
508 /* Assert bundle-uri support */
509 ensure_server_supports_v2("bundle-uri");
511 /* (Re-)send capabilities */
512 send_capabilities(fd_out, reader);
514 /* Send command */
515 packet_write_fmt(fd_out, "command=bundle-uri\n");
516 packet_delim(fd_out);
518 packet_flush(fd_out);
520 /* Process response from server */
521 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
522 const char *line = reader->line;
523 line_nr++;
525 if (!bundle_uri_parse_line(bundles, line))
526 continue;
528 return error(_("error on bundle-uri response line %d: %s"),
529 line_nr, line);
532 if (reader->status != PACKET_READ_FLUSH)
533 return error(_("expected flush after bundle-uri listing"));
536 * Might die(), but obscure enough that that's OK, e.g. in
537 * serve.c we'll call BUG() on its equivalent (the
538 * PACKET_READ_RESPONSE_END check).
540 check_stateless_delimiter(stateless_rpc, reader,
541 _("expected response end packet after ref listing"));
543 return 0;
546 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
547 struct ref **list, int for_push,
548 struct transport_ls_refs_options *transport_options,
549 const struct string_list *server_options,
550 int stateless_rpc)
552 int i;
553 struct strvec *ref_prefixes = transport_options ?
554 &transport_options->ref_prefixes : NULL;
555 const char **unborn_head_target = transport_options ?
556 &transport_options->unborn_head_target : NULL;
557 *list = NULL;
559 ensure_server_supports_v2("ls-refs");
560 packet_write_fmt(fd_out, "command=ls-refs\n");
562 /* Send capabilities */
563 send_capabilities(fd_out, reader);
565 if (server_options && server_options->nr) {
566 ensure_server_supports_v2("server-option");
567 for (i = 0; i < server_options->nr; i++)
568 packet_write_fmt(fd_out, "server-option=%s",
569 server_options->items[i].string);
572 packet_delim(fd_out);
573 /* When pushing we don't want to request the peeled tags */
574 if (!for_push)
575 packet_write_fmt(fd_out, "peel\n");
576 packet_write_fmt(fd_out, "symrefs\n");
577 if (server_supports_feature("ls-refs", "unborn", 0))
578 packet_write_fmt(fd_out, "unborn\n");
579 for (i = 0; ref_prefixes && i < ref_prefixes->nr; i++) {
580 packet_write_fmt(fd_out, "ref-prefix %s\n",
581 ref_prefixes->v[i]);
583 packet_flush(fd_out);
585 /* Process response from server */
586 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
587 if (!process_ref_v2(reader, &list, unborn_head_target))
588 die(_("invalid ls-refs response: %s"), reader->line);
591 if (reader->status != PACKET_READ_FLUSH)
592 die(_("expected flush after ref listing"));
594 check_stateless_delimiter(stateless_rpc, reader,
595 _("expected response end packet after ref listing"));
597 return list;
600 const char *parse_feature_value(const char *feature_list, const char *feature, size_t *lenp, size_t *offset)
602 const char *orig_start = feature_list;
603 size_t len;
605 if (!feature_list)
606 return NULL;
608 len = strlen(feature);
609 if (offset)
610 feature_list += *offset;
611 while (*feature_list) {
612 const char *found = strstr(feature_list, feature);
613 if (!found)
614 return NULL;
615 if (feature_list == found || isspace(found[-1])) {
616 const char *value = found + len;
617 /* feature with no value (e.g., "thin-pack") */
618 if (!*value || isspace(*value)) {
619 if (lenp)
620 *lenp = 0;
621 if (offset)
622 *offset = found + len - orig_start;
623 return value;
625 /* feature with a value (e.g., "agent=git/1.2.3") */
626 else if (*value == '=') {
627 size_t end;
629 value++;
630 end = strcspn(value, " \t\n");
631 if (lenp)
632 *lenp = end;
633 if (offset)
634 *offset = value + end - orig_start;
635 return value;
638 * otherwise we matched a substring of another feature;
639 * keep looking
642 feature_list = found + 1;
644 return NULL;
647 int server_supports_hash(const char *desired, int *feature_supported)
649 size_t offset = 0;
650 size_t len;
651 const char *hash;
653 hash = next_server_feature_value("object-format", &len, &offset);
654 if (feature_supported)
655 *feature_supported = !!hash;
656 if (!hash) {
657 hash = hash_algos[GIT_HASH_SHA1].name;
658 len = strlen(hash);
660 while (hash) {
661 if (!xstrncmpz(desired, hash, len))
662 return 1;
664 hash = next_server_feature_value("object-format", &len, &offset);
666 return 0;
669 int parse_feature_request(const char *feature_list, const char *feature)
671 return !!parse_feature_value(feature_list, feature, NULL, NULL);
674 static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset)
676 return parse_feature_value(server_capabilities_v1, feature, len, offset);
679 const char *server_feature_value(const char *feature, size_t *len)
681 return parse_feature_value(server_capabilities_v1, feature, len, NULL);
684 int server_supports(const char *feature)
686 return !!server_feature_value(feature, NULL);
689 enum protocol {
690 PROTO_LOCAL = 1,
691 PROTO_FILE,
692 PROTO_SSH,
693 PROTO_GIT
696 int url_is_local_not_ssh(const char *url)
698 const char *colon = strchr(url, ':');
699 const char *slash = strchr(url, '/');
700 return !colon || (slash && slash < colon) ||
701 (has_dos_drive_prefix(url) && is_valid_path(url));
704 static const char *prot_name(enum protocol protocol)
706 switch (protocol) {
707 case PROTO_LOCAL:
708 case PROTO_FILE:
709 return "file";
710 case PROTO_SSH:
711 return "ssh";
712 case PROTO_GIT:
713 return "git";
714 default:
715 return "unknown protocol";
719 static enum protocol get_protocol(const char *name)
721 if (!strcmp(name, "ssh"))
722 return PROTO_SSH;
723 if (!strcmp(name, "git"))
724 return PROTO_GIT;
725 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
726 return PROTO_SSH;
727 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
728 return PROTO_SSH;
729 if (!strcmp(name, "file"))
730 return PROTO_FILE;
731 die(_("protocol '%s' is not supported"), name);
734 static char *host_end(char **hoststart, int removebrackets)
736 char *host = *hoststart;
737 char *end;
738 char *start = strstr(host, "@[");
739 if (start)
740 start++; /* Jump over '@' */
741 else
742 start = host;
743 if (start[0] == '[') {
744 end = strchr(start + 1, ']');
745 if (end) {
746 if (removebrackets) {
747 *end = 0;
748 memmove(start, start + 1, end - start);
749 end++;
751 } else
752 end = host;
753 } else
754 end = host;
755 return end;
758 #define STR_(s) # s
759 #define STR(s) STR_(s)
761 static void get_host_and_port(char **host, const char **port)
763 char *colon, *end;
764 end = host_end(host, 1);
765 colon = strchr(end, ':');
766 if (colon) {
767 long portnr = strtol(colon + 1, &end, 10);
768 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
769 *colon = 0;
770 *port = colon + 1;
771 } else if (!colon[1]) {
772 *colon = 0;
777 static void enable_keepalive(int sockfd)
779 int ka = 1;
781 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
782 error_errno(_("unable to set SO_KEEPALIVE on socket"));
785 #ifndef NO_IPV6
787 static const char *ai_name(const struct addrinfo *ai)
789 static char addr[NI_MAXHOST];
790 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
791 NI_NUMERICHOST) != 0)
792 xsnprintf(addr, sizeof(addr), "(unknown)");
794 return addr;
798 * Returns a connected socket() fd, or else die()s.
800 static int git_tcp_connect_sock(char *host, int flags)
802 struct strbuf error_message = STRBUF_INIT;
803 int sockfd = -1;
804 const char *port = STR(DEFAULT_GIT_PORT);
805 struct addrinfo hints, *ai0, *ai;
806 int gai;
807 int cnt = 0;
809 get_host_and_port(&host, &port);
810 if (!*port)
811 port = "<none>";
813 memset(&hints, 0, sizeof(hints));
814 if (flags & CONNECT_IPV4)
815 hints.ai_family = AF_INET;
816 else if (flags & CONNECT_IPV6)
817 hints.ai_family = AF_INET6;
818 hints.ai_socktype = SOCK_STREAM;
819 hints.ai_protocol = IPPROTO_TCP;
821 if (flags & CONNECT_VERBOSE)
822 fprintf(stderr, _("Looking up %s ... "), host);
824 gai = getaddrinfo(host, port, &hints, &ai);
825 if (gai)
826 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
828 if (flags & CONNECT_VERBOSE)
829 /* TRANSLATORS: this is the end of "Looking up %s ... " */
830 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
832 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
833 sockfd = socket(ai->ai_family,
834 ai->ai_socktype, ai->ai_protocol);
835 if ((sockfd < 0) ||
836 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
837 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
838 host, cnt, ai_name(ai), strerror(errno));
839 if (0 <= sockfd)
840 close(sockfd);
841 sockfd = -1;
842 continue;
844 if (flags & CONNECT_VERBOSE)
845 fprintf(stderr, "%s ", ai_name(ai));
846 break;
849 freeaddrinfo(ai0);
851 if (sockfd < 0)
852 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
854 enable_keepalive(sockfd);
856 if (flags & CONNECT_VERBOSE)
857 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
858 fprintf_ln(stderr, _("done."));
860 strbuf_release(&error_message);
862 return sockfd;
865 #else /* NO_IPV6 */
868 * Returns a connected socket() fd, or else die()s.
870 static int git_tcp_connect_sock(char *host, int flags)
872 struct strbuf error_message = STRBUF_INIT;
873 int sockfd = -1;
874 const char *port = STR(DEFAULT_GIT_PORT);
875 char *ep;
876 struct hostent *he;
877 struct sockaddr_in sa;
878 char **ap;
879 unsigned int nport;
880 int cnt;
882 get_host_and_port(&host, &port);
884 if (flags & CONNECT_VERBOSE)
885 fprintf(stderr, _("Looking up %s ... "), host);
887 he = gethostbyname(host);
888 if (!he)
889 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
890 nport = strtoul(port, &ep, 10);
891 if ( ep == port || *ep ) {
892 /* Not numeric */
893 struct servent *se = getservbyname(port,"tcp");
894 if ( !se )
895 die(_("unknown port %s"), port);
896 nport = se->s_port;
899 if (flags & CONNECT_VERBOSE)
900 /* TRANSLATORS: this is the end of "Looking up %s ... " */
901 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
903 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
904 memset(&sa, 0, sizeof sa);
905 sa.sin_family = he->h_addrtype;
906 sa.sin_port = htons(nport);
907 memcpy(&sa.sin_addr, *ap, he->h_length);
909 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
910 if ((sockfd < 0) ||
911 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
912 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
913 host,
914 cnt,
915 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
916 strerror(errno));
917 if (0 <= sockfd)
918 close(sockfd);
919 sockfd = -1;
920 continue;
922 if (flags & CONNECT_VERBOSE)
923 fprintf(stderr, "%s ",
924 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
925 break;
928 if (sockfd < 0)
929 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
931 enable_keepalive(sockfd);
933 if (flags & CONNECT_VERBOSE)
934 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
935 fprintf_ln(stderr, _("done."));
937 return sockfd;
940 #endif /* NO_IPV6 */
944 * Dummy child_process returned by git_connect() if the transport protocol
945 * does not need fork(2).
947 static struct child_process no_fork = CHILD_PROCESS_INIT;
949 int git_connection_is_socket(struct child_process *conn)
951 return conn == &no_fork;
954 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
956 int sockfd = git_tcp_connect_sock(host, flags);
958 fd[0] = sockfd;
959 fd[1] = dup(sockfd);
961 return &no_fork;
965 static char *git_proxy_command;
967 static int git_proxy_command_options(const char *var, const char *value,
968 const struct config_context *ctx, void *cb)
970 if (!strcmp(var, "core.gitproxy")) {
971 const char *for_pos;
972 int matchlen = -1;
973 int hostlen;
974 const char *rhost_name = cb;
975 int rhost_len = strlen(rhost_name);
977 if (git_proxy_command)
978 return 0;
979 if (!value)
980 return config_error_nonbool(var);
981 /* [core]
982 * ;# matches www.kernel.org as well
983 * gitproxy = netcatter-1 for kernel.org
984 * gitproxy = netcatter-2 for sample.xz
985 * gitproxy = netcatter-default
987 for_pos = strstr(value, " for ");
988 if (!for_pos)
989 /* matches everybody */
990 matchlen = strlen(value);
991 else {
992 hostlen = strlen(for_pos + 5);
993 if (rhost_len < hostlen)
994 matchlen = -1;
995 else if (!strncmp(for_pos + 5,
996 rhost_name + rhost_len - hostlen,
997 hostlen) &&
998 ((rhost_len == hostlen) ||
999 rhost_name[rhost_len - hostlen -1] == '.'))
1000 matchlen = for_pos - value;
1001 else
1002 matchlen = -1;
1004 if (0 <= matchlen) {
1005 /* core.gitproxy = none for kernel.org */
1006 if (matchlen == 4 &&
1007 !memcmp(value, "none", 4))
1008 matchlen = 0;
1009 git_proxy_command = xmemdupz(value, matchlen);
1011 return 0;
1014 return git_default_config(var, value, ctx, cb);
1017 static int git_use_proxy(const char *host)
1019 git_proxy_command = getenv("GIT_PROXY_COMMAND");
1020 git_config(git_proxy_command_options, (void*)host);
1021 return (git_proxy_command && *git_proxy_command);
1024 static struct child_process *git_proxy_connect(int fd[2], char *host)
1026 const char *port = STR(DEFAULT_GIT_PORT);
1027 struct child_process *proxy;
1029 get_host_and_port(&host, &port);
1031 if (looks_like_command_line_option(host))
1032 die(_("strange hostname '%s' blocked"), host);
1033 if (looks_like_command_line_option(port))
1034 die(_("strange port '%s' blocked"), port);
1036 proxy = xmalloc(sizeof(*proxy));
1037 child_process_init(proxy);
1038 strvec_push(&proxy->args, git_proxy_command);
1039 strvec_push(&proxy->args, host);
1040 strvec_push(&proxy->args, port);
1041 proxy->in = -1;
1042 proxy->out = -1;
1043 if (start_command(proxy))
1044 die(_("cannot start proxy %s"), git_proxy_command);
1045 fd[0] = proxy->out; /* read from proxy stdout */
1046 fd[1] = proxy->in; /* write to proxy stdin */
1047 return proxy;
1050 static char *get_port(char *host)
1052 char *end;
1053 char *p = strchr(host, ':');
1055 if (p) {
1056 long port = strtol(p + 1, &end, 10);
1057 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
1058 *p = '\0';
1059 return p+1;
1063 return NULL;
1067 * Extract protocol and relevant parts from the specified connection URL.
1068 * The caller must free() the returned strings.
1070 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
1071 char **ret_path)
1073 char *url;
1074 char *host, *path;
1075 char *end;
1076 int separator = '/';
1077 enum protocol protocol = PROTO_LOCAL;
1079 if (is_url(url_orig))
1080 url = url_decode(url_orig);
1081 else
1082 url = xstrdup(url_orig);
1084 host = strstr(url, "://");
1085 if (host) {
1086 *host = '\0';
1087 protocol = get_protocol(url);
1088 host += 3;
1089 } else {
1090 host = url;
1091 if (!url_is_local_not_ssh(url)) {
1092 protocol = PROTO_SSH;
1093 separator = ':';
1098 * Don't do destructive transforms as protocol code does
1099 * '[]' unwrapping in get_host_and_port()
1101 end = host_end(&host, 0);
1103 if (protocol == PROTO_LOCAL)
1104 path = end;
1105 else if (protocol == PROTO_FILE && *host != '/' &&
1106 !has_dos_drive_prefix(host) &&
1107 offset_1st_component(host - 2) > 1)
1108 path = host - 2; /* include the leading "//" */
1109 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
1110 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1111 else
1112 path = strchr(end, separator);
1114 if (!path || !*path)
1115 die(_("no path specified; see 'git help pull' for valid url syntax"));
1118 * null-terminate hostname and point path to ~ for URL's like this:
1119 * ssh://host.xz/~user/repo
1122 end = path; /* Need to \0 terminate host here */
1123 if (separator == ':')
1124 path++; /* path starts after ':' */
1125 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
1126 if (path[1] == '~')
1127 path++;
1130 path = xstrdup(path);
1131 *end = '\0';
1133 *ret_host = xstrdup(host);
1134 *ret_path = path;
1135 free(url);
1136 return protocol;
1139 static const char *get_ssh_command(void)
1141 const char *ssh;
1143 if ((ssh = getenv("GIT_SSH_COMMAND")))
1144 return ssh;
1146 if (!git_config_get_string_tmp("core.sshcommand", &ssh))
1147 return ssh;
1149 return NULL;
1152 enum ssh_variant {
1153 VARIANT_AUTO,
1154 VARIANT_SIMPLE,
1155 VARIANT_SSH,
1156 VARIANT_PLINK,
1157 VARIANT_PUTTY,
1158 VARIANT_TORTOISEPLINK,
1161 static void override_ssh_variant(enum ssh_variant *ssh_variant)
1163 const char *variant = getenv("GIT_SSH_VARIANT");
1165 if (!variant && git_config_get_string_tmp("ssh.variant", &variant))
1166 return;
1168 if (!strcmp(variant, "auto"))
1169 *ssh_variant = VARIANT_AUTO;
1170 else if (!strcmp(variant, "plink"))
1171 *ssh_variant = VARIANT_PLINK;
1172 else if (!strcmp(variant, "putty"))
1173 *ssh_variant = VARIANT_PUTTY;
1174 else if (!strcmp(variant, "tortoiseplink"))
1175 *ssh_variant = VARIANT_TORTOISEPLINK;
1176 else if (!strcmp(variant, "simple"))
1177 *ssh_variant = VARIANT_SIMPLE;
1178 else
1179 *ssh_variant = VARIANT_SSH;
1182 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1183 int is_cmdline)
1185 enum ssh_variant ssh_variant = VARIANT_AUTO;
1186 const char *variant;
1187 char *p = NULL;
1189 override_ssh_variant(&ssh_variant);
1191 if (ssh_variant != VARIANT_AUTO)
1192 return ssh_variant;
1194 if (!is_cmdline) {
1195 p = xstrdup(ssh_command);
1196 variant = basename(p);
1197 } else {
1198 const char **ssh_argv;
1200 p = xstrdup(ssh_command);
1201 if (split_cmdline(p, &ssh_argv) > 0) {
1202 variant = basename((char *)ssh_argv[0]);
1204 * At this point, variant points into the buffer
1205 * referenced by p, hence we do not need ssh_argv
1206 * any longer.
1208 free(ssh_argv);
1209 } else {
1210 free(p);
1211 return ssh_variant;
1215 if (!strcasecmp(variant, "ssh") ||
1216 !strcasecmp(variant, "ssh.exe"))
1217 ssh_variant = VARIANT_SSH;
1218 else if (!strcasecmp(variant, "plink") ||
1219 !strcasecmp(variant, "plink.exe"))
1220 ssh_variant = VARIANT_PLINK;
1221 else if (!strcasecmp(variant, "tortoiseplink") ||
1222 !strcasecmp(variant, "tortoiseplink.exe"))
1223 ssh_variant = VARIANT_TORTOISEPLINK;
1225 free(p);
1226 return ssh_variant;
1230 * Open a connection using Git's native protocol.
1232 * The caller is responsible for freeing hostandport, but this function may
1233 * modify it (for example, to truncate it to remove the port part).
1235 static struct child_process *git_connect_git(int fd[2], char *hostandport,
1236 const char *path, const char *prog,
1237 enum protocol_version version,
1238 int flags)
1240 struct child_process *conn;
1241 struct strbuf request = STRBUF_INIT;
1243 * Set up virtual host information based on where we will
1244 * connect, unless the user has overridden us in
1245 * the environment.
1247 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1248 if (target_host)
1249 target_host = xstrdup(target_host);
1250 else
1251 target_host = xstrdup(hostandport);
1253 transport_check_allowed("git");
1254 if (strchr(target_host, '\n') || strchr(path, '\n'))
1255 die(_("newline is forbidden in git:// hosts and repo paths"));
1258 * These underlying connection commands die() if they
1259 * cannot connect.
1261 if (git_use_proxy(hostandport))
1262 conn = git_proxy_connect(fd, hostandport);
1263 else
1264 conn = git_tcp_connect(fd, hostandport, flags);
1266 * Separate original protocol components prog and path
1267 * from extended host header with a NUL byte.
1269 * Note: Do not add any other headers here! Doing so
1270 * will cause older git-daemon servers to crash.
1272 strbuf_addf(&request,
1273 "%s %s%chost=%s%c",
1274 prog, path, 0,
1275 target_host, 0);
1277 /* If using a new version put that stuff here after a second null byte */
1278 if (version > 0) {
1279 strbuf_addch(&request, '\0');
1280 strbuf_addf(&request, "version=%d%c",
1281 version, '\0');
1284 packet_write(fd[1], request.buf, request.len);
1286 free(target_host);
1287 strbuf_release(&request);
1288 return conn;
1292 * Append the appropriate environment variables to `env` and options to
1293 * `args` for running ssh in Git's SSH-tunneled transport.
1295 static void push_ssh_options(struct strvec *args, struct strvec *env,
1296 enum ssh_variant variant, const char *port,
1297 enum protocol_version version, int flags)
1299 if (variant == VARIANT_SSH &&
1300 version > 0) {
1301 strvec_push(args, "-o");
1302 strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1303 strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1304 version);
1307 if (flags & CONNECT_IPV4) {
1308 switch (variant) {
1309 case VARIANT_AUTO:
1310 BUG("VARIANT_AUTO passed to push_ssh_options");
1311 case VARIANT_SIMPLE:
1312 die(_("ssh variant 'simple' does not support -4"));
1313 case VARIANT_SSH:
1314 case VARIANT_PLINK:
1315 case VARIANT_PUTTY:
1316 case VARIANT_TORTOISEPLINK:
1317 strvec_push(args, "-4");
1319 } else if (flags & CONNECT_IPV6) {
1320 switch (variant) {
1321 case VARIANT_AUTO:
1322 BUG("VARIANT_AUTO passed to push_ssh_options");
1323 case VARIANT_SIMPLE:
1324 die(_("ssh variant 'simple' does not support -6"));
1325 case VARIANT_SSH:
1326 case VARIANT_PLINK:
1327 case VARIANT_PUTTY:
1328 case VARIANT_TORTOISEPLINK:
1329 strvec_push(args, "-6");
1333 if (variant == VARIANT_TORTOISEPLINK)
1334 strvec_push(args, "-batch");
1336 if (port) {
1337 switch (variant) {
1338 case VARIANT_AUTO:
1339 BUG("VARIANT_AUTO passed to push_ssh_options");
1340 case VARIANT_SIMPLE:
1341 die(_("ssh variant 'simple' does not support setting port"));
1342 case VARIANT_SSH:
1343 strvec_push(args, "-p");
1344 break;
1345 case VARIANT_PLINK:
1346 case VARIANT_PUTTY:
1347 case VARIANT_TORTOISEPLINK:
1348 strvec_push(args, "-P");
1351 strvec_push(args, port);
1355 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1356 static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1357 const char *port, enum protocol_version version,
1358 int flags)
1360 const char *ssh;
1361 enum ssh_variant variant;
1363 if (looks_like_command_line_option(ssh_host))
1364 die(_("strange hostname '%s' blocked"), ssh_host);
1366 ssh = get_ssh_command();
1367 if (ssh) {
1368 variant = determine_ssh_variant(ssh, 1);
1369 } else {
1371 * GIT_SSH is the no-shell version of
1372 * GIT_SSH_COMMAND (and must remain so for
1373 * historical compatibility).
1375 conn->use_shell = 0;
1377 ssh = getenv("GIT_SSH");
1378 if (!ssh)
1379 ssh = "ssh";
1380 variant = determine_ssh_variant(ssh, 0);
1383 if (variant == VARIANT_AUTO) {
1384 struct child_process detect = CHILD_PROCESS_INIT;
1386 detect.use_shell = conn->use_shell;
1387 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1389 strvec_push(&detect.args, ssh);
1390 strvec_push(&detect.args, "-G");
1391 push_ssh_options(&detect.args, &detect.env,
1392 VARIANT_SSH, port, version, flags);
1393 strvec_push(&detect.args, ssh_host);
1395 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1398 strvec_push(&conn->args, ssh);
1399 push_ssh_options(&conn->args, &conn->env, variant, port, version,
1400 flags);
1401 strvec_push(&conn->args, ssh_host);
1405 * This returns the dummy child_process `no_fork` if the transport protocol
1406 * does not need fork(2), or a struct child_process object if it does. Once
1407 * done, finish the connection with finish_connect() with the value returned
1408 * from this function (it is safe to call finish_connect() with NULL to
1409 * support the former case).
1411 * If it returns, the connect is successful; it just dies on errors (this
1412 * will hopefully be changed in a libification effort, to return NULL when
1413 * the connection failed).
1415 struct child_process *git_connect(int fd[2], const char *url,
1416 const char *name,
1417 const char *prog, int flags)
1419 char *hostandport, *path;
1420 struct child_process *conn;
1421 enum protocol protocol;
1422 enum protocol_version version = get_protocol_version_config();
1425 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1426 * to perform any operation that doesn't involve upload-pack (i.e., a
1427 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
1428 * to do anything else (like push or remote archive) via v2.
1430 if (version == protocol_v2 && strcmp("git-upload-pack", name))
1431 version = protocol_v0;
1433 /* Without this we cannot rely on waitpid() to tell
1434 * what happened to our children.
1436 signal(SIGCHLD, SIG_DFL);
1438 protocol = parse_connect_url(url, &hostandport, &path);
1439 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
1440 printf("Diag: url=%s\n", url ? url : "NULL");
1441 printf("Diag: protocol=%s\n", prot_name(protocol));
1442 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1443 printf("Diag: path=%s\n", path ? path : "NULL");
1444 conn = NULL;
1445 } else if (protocol == PROTO_GIT) {
1446 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1447 conn->trace2_child_class = "transport/git";
1448 } else {
1449 struct strbuf cmd = STRBUF_INIT;
1450 const char *const *var;
1452 conn = xmalloc(sizeof(*conn));
1453 child_process_init(conn);
1455 if (looks_like_command_line_option(path))
1456 die(_("strange pathname '%s' blocked"), path);
1458 strbuf_addstr(&cmd, prog);
1459 strbuf_addch(&cmd, ' ');
1460 sq_quote_buf(&cmd, path);
1462 /* remove repo-local variables from the environment */
1463 for (var = local_repo_env; *var; var++)
1464 strvec_push(&conn->env, *var);
1466 conn->use_shell = 1;
1467 conn->in = conn->out = -1;
1468 if (protocol == PROTO_SSH) {
1469 char *ssh_host = hostandport;
1470 const char *port = NULL;
1471 transport_check_allowed("ssh");
1472 get_host_and_port(&ssh_host, &port);
1474 if (!port)
1475 port = get_port(ssh_host);
1477 if (flags & CONNECT_DIAG_URL) {
1478 printf("Diag: url=%s\n", url ? url : "NULL");
1479 printf("Diag: protocol=%s\n", prot_name(protocol));
1480 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1481 printf("Diag: port=%s\n", port ? port : "NONE");
1482 printf("Diag: path=%s\n", path ? path : "NULL");
1484 free(hostandport);
1485 free(path);
1486 free(conn);
1487 strbuf_release(&cmd);
1488 return NULL;
1490 conn->trace2_child_class = "transport/ssh";
1491 fill_ssh_args(conn, ssh_host, port, version, flags);
1492 } else {
1493 transport_check_allowed("file");
1494 conn->trace2_child_class = "transport/file";
1495 if (version > 0) {
1496 strvec_pushf(&conn->env,
1497 GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1498 version);
1501 strvec_push(&conn->args, cmd.buf);
1503 if (start_command(conn))
1504 die(_("unable to fork"));
1506 fd[0] = conn->out; /* read from child's stdout */
1507 fd[1] = conn->in; /* write to child's stdin */
1508 strbuf_release(&cmd);
1510 free(hostandport);
1511 free(path);
1512 return conn;
1515 int finish_connect(struct child_process *conn)
1517 int code;
1518 if (!conn || git_connection_is_socket(conn))
1519 return 0;
1521 code = finish_command(conn);
1522 free(conn);
1523 return code;