pack-objects: fix error when packing same pack twice
[alt-git.git] / connect.c
blob63e59641c0d4d5c60fc4bee2761e6acd471535a2
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 #include "bundle-uri.h"
20 static char *server_capabilities_v1;
21 static struct strvec server_capabilities_v2 = STRVEC_INIT;
22 static const char *next_server_feature_value(const char *feature, int *len, int *offset);
24 static int check_ref(const char *name, unsigned int flags)
26 if (!flags)
27 return 1;
29 if (!skip_prefix(name, "refs/", &name))
30 return 0;
32 /* REF_NORMAL means that we don't want the magic fake tag refs */
33 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
34 return 0;
36 /* REF_HEADS means that we want regular branch heads */
37 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
38 return 1;
40 /* REF_TAGS means that we want tags */
41 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
42 return 1;
44 /* All type bits clear means that we are ok with anything */
45 return !(flags & ~REF_NORMAL);
48 int check_ref_type(const struct ref *ref, int flags)
50 return check_ref(ref->name, flags);
53 static NORETURN void die_initial_contact(int unexpected)
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.
61 if (unexpected)
62 die(_("the remote end hung up upon initial contact"));
63 else
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."));
69 /* Checks if the server supports the capability 'c' */
70 int server_supports_v2(const char *c)
72 int i;
74 for (i = 0; i < server_capabilities_v2.nr; i++) {
75 const char *out;
76 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
77 (!*out || *out == '='))
78 return 1;
80 return 0;
83 void ensure_server_supports_v2(const char *c)
85 if (!server_supports_v2(c))
86 die(_("server doesn't support '%s'"), c);
89 int server_feature_v2(const char *c, const char **v)
91 int i;
93 for (i = 0; i < server_capabilities_v2.nr; i++) {
94 const char *out;
95 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
96 (*out == '=')) {
97 *v = out + 1;
98 return 1;
101 return 0;
104 int server_supports_feature(const char *c, const char *feature,
105 int die_on_error)
107 int i;
109 for (i = 0; i < server_capabilities_v2.nr; i++) {
110 const char *out;
111 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
112 (!*out || *(out++) == '=')) {
113 if (parse_feature_request(out, feature))
114 return 1;
115 else
116 break;
120 if (die_on_error)
121 die(_("server doesn't support feature '%s'"), feature);
123 return 0;
126 static void process_capabilities_v2(struct packet_reader *reader)
128 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
129 strvec_push(&server_capabilities_v2, reader->line);
131 if (reader->status != PACKET_READ_FLUSH)
132 die(_("expected flush after capabilities"));
135 enum protocol_version discover_version(struct packet_reader *reader)
137 enum protocol_version version = protocol_unknown_version;
140 * Peek the first line of the server's response to
141 * determine the protocol version the server is speaking.
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:
148 case PACKET_READ_RESPONSE_END:
149 version = protocol_v0;
150 break;
151 case PACKET_READ_NORMAL:
152 version = determine_protocol_version_client(reader->line);
153 break;
156 switch (version) {
157 case protocol_v2:
158 process_capabilities_v2(reader);
159 break;
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");
170 trace2_data_intmax("transfer", NULL, "negotiated-version", version);
172 return version;
175 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
177 char *sym, *target;
178 struct string_list_item *item;
180 if (!len)
181 return; /* just "symref" */
182 /* e.g. "symref=HEAD:refs/heads/master" */
183 sym = xmemdupz(val, len);
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;
193 item = string_list_append_nodup(symref, sym);
194 item->util = target;
195 return;
196 reject:
197 free(sym);
198 return;
201 static void annotate_refs_with_symref_info(struct ref *ref)
203 struct string_list symref = STRING_LIST_INIT_DUP;
204 int offset = 0;
206 while (1) {
207 int len;
208 const char *val;
210 val = next_server_feature_value("symref", &len, &offset);
211 if (!val)
212 break;
213 parse_one_symref_info(&symref, val, len);
215 string_list_sort(&symref);
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);
224 string_list_clear(&symref, 0);
227 static void process_capabilities(struct packet_reader *reader, int *linelen)
229 const char *feat_val;
230 int feat_len;
231 const char *line = reader->line;
232 int nul_location = strlen(line);
233 if (nul_location == *linelen)
234 return;
235 server_capabilities_v1 = xstrdup(line + nul_location + 1);
236 *linelen = nul_location;
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];
250 static int process_dummy_ref(const struct packet_reader *reader)
252 const char *line = reader->line;
253 struct object_id oid;
254 const char *name;
256 if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
257 return 0;
258 if (*name != ' ')
259 return 0;
260 name++;
262 return oideq(null_oid(), &oid) && !strcmp(name, "capabilities^{}");
265 static void check_no_capabilities(const char *line, int len)
267 if (strlen(line) != len)
268 warning(_("ignoring capabilities after first line '%s'"),
269 line + strlen(line));
272 static int process_ref(const struct packet_reader *reader, int len,
273 struct ref ***list, unsigned int flags,
274 struct oid_array *extra_have)
276 const char *line = reader->line;
277 struct object_id old_oid;
278 const char *name;
280 if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
281 return 0;
282 if (*name != ' ')
283 return 0;
284 name++;
286 if (extra_have && !strcmp(name, ".have")) {
287 oid_array_append(extra_have, &old_oid);
288 } else if (!strcmp(name, "capabilities^{}")) {
289 die(_("protocol error: unexpected capabilities^{}"));
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;
296 check_no_capabilities(line, len);
297 return 1;
300 static int process_shallow(const struct packet_reader *reader, int len,
301 struct oid_array *shallow_points)
303 const char *line = reader->line;
304 const char *arg;
305 struct object_id old_oid;
307 if (!skip_prefix(line, "shallow ", &arg))
308 return 0;
310 if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo))
311 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
312 if (!shallow_points)
313 die(_("repository on the other end cannot be shallow"));
314 oid_array_append(shallow_points, &old_oid);
315 check_no_capabilities(line, len);
316 return 1;
319 enum get_remote_heads_state {
320 EXPECTING_FIRST_REF = 0,
321 EXPECTING_REF,
322 EXPECTING_SHALLOW,
323 EXPECTING_DONE,
327 * Read all the refs from the other end
329 struct ref **get_remote_heads(struct packet_reader *reader,
330 struct ref **list, unsigned int flags,
331 struct oid_array *extra_have,
332 struct oid_array *shallow_points)
334 struct ref **orig_list = list;
335 int len = 0;
336 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
338 *list = NULL;
340 while (state != EXPECTING_DONE) {
341 switch (packet_reader_read(reader)) {
342 case PACKET_READ_EOF:
343 die_initial_contact(1);
344 case PACKET_READ_NORMAL:
345 len = reader->pktlen;
346 break;
347 case PACKET_READ_FLUSH:
348 state = EXPECTING_DONE;
349 break;
350 case PACKET_READ_DELIM:
351 case PACKET_READ_RESPONSE_END:
352 die(_("invalid packet"));
355 switch (state) {
356 case EXPECTING_FIRST_REF:
357 process_capabilities(reader, &len);
358 if (process_dummy_ref(reader)) {
359 state = EXPECTING_SHALLOW;
360 break;
362 state = EXPECTING_REF;
363 /* fallthrough */
364 case EXPECTING_REF:
365 if (process_ref(reader, len, &list, flags, extra_have))
366 break;
367 state = EXPECTING_SHALLOW;
368 /* fallthrough */
369 case EXPECTING_SHALLOW:
370 if (process_shallow(reader, len, shallow_points))
371 break;
372 die(_("protocol error: unexpected '%s'"), reader->line);
373 case EXPECTING_DONE:
374 break;
378 annotate_refs_with_symref_info(*orig_list);
380 return list;
383 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
384 static int process_ref_v2(struct packet_reader *reader, struct ref ***list,
385 const char **unborn_head_target)
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;
393 const char *line = reader->line;
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.
401 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
402 ret = 0;
403 goto out;
406 if (!strcmp("unborn", line_sections.items[i].string)) {
407 i++;
408 if (unborn_head_target &&
409 !strcmp("HEAD", line_sections.items[i++].string)) {
411 * Look for the symref target (if any). If found,
412 * return it to the caller.
414 for (; i < line_sections.nr; i++) {
415 const char *arg = line_sections.items[i].string;
417 if (skip_prefix(arg, "symref-target:", &arg)) {
418 *unborn_head_target = xstrdup(arg);
419 break;
423 goto out;
425 if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) ||
426 *end) {
427 ret = 0;
428 goto out;
431 ref = alloc_ref(line_sections.items[i++].string);
433 memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
434 **list = ref;
435 *list = &ref->next;
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);
442 if (skip_prefix(arg, "peeled:", &arg)) {
443 struct object_id peeled_oid;
444 char *peeled_name;
445 struct ref *peeled;
446 if (parse_oid_hex_algop(arg, &peeled_oid, &end,
447 reader->hash_algo) || *end) {
448 ret = 0;
449 goto out;
452 peeled_name = xstrfmt("%s^{}", ref->name);
453 peeled = alloc_ref(peeled_name);
455 memcpy(peeled->old_oid.hash, peeled_oid.hash,
456 reader->hash_algo->rawsz);
457 **list = peeled;
458 *list = &peeled->next;
460 free(peeled_name);
464 out:
465 string_list_clear(&line_sections, 0);
466 return ret;
469 void check_stateless_delimiter(int stateless_rpc,
470 struct packet_reader *reader,
471 const char *error)
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);
479 static void send_capabilities(int fd_out, struct packet_reader *reader)
481 const char *hash_name;
483 if (server_supports_v2("agent"))
484 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
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];
497 int get_remote_bundle_uri(int fd_out, struct packet_reader *reader,
498 struct bundle_list *bundles, int stateless_rpc)
500 int line_nr = 1;
502 /* Assert bundle-uri support */
503 ensure_server_supports_v2("bundle-uri");
505 /* (Re-)send capabilities */
506 send_capabilities(fd_out, reader);
508 /* Send command */
509 packet_write_fmt(fd_out, "command=bundle-uri\n");
510 packet_delim(fd_out);
512 packet_flush(fd_out);
514 /* Process response from server */
515 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
516 const char *line = reader->line;
517 line_nr++;
519 if (!bundle_uri_parse_line(bundles, line))
520 continue;
522 return error(_("error on bundle-uri response line %d: %s"),
523 line_nr, line);
526 if (reader->status != PACKET_READ_FLUSH)
527 return error(_("expected flush after bundle-uri listing"));
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).
534 check_stateless_delimiter(stateless_rpc, reader,
535 _("expected response end packet after ref listing"));
537 return 0;
540 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
541 struct ref **list, int for_push,
542 struct transport_ls_refs_options *transport_options,
543 const struct string_list *server_options,
544 int stateless_rpc)
546 int i;
547 struct strvec *ref_prefixes = transport_options ?
548 &transport_options->ref_prefixes : NULL;
549 const char **unborn_head_target = transport_options ?
550 &transport_options->unborn_head_target : NULL;
551 *list = NULL;
553 ensure_server_supports_v2("ls-refs");
554 packet_write_fmt(fd_out, "command=ls-refs\n");
556 /* Send capabilities */
557 send_capabilities(fd_out, reader);
559 if (server_options && server_options->nr) {
560 ensure_server_supports_v2("server-option");
561 for (i = 0; i < server_options->nr; i++)
562 packet_write_fmt(fd_out, "server-option=%s",
563 server_options->items[i].string);
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");
571 if (server_supports_feature("ls-refs", "unborn", 0))
572 packet_write_fmt(fd_out, "unborn\n");
573 for (i = 0; ref_prefixes && i < ref_prefixes->nr; i++) {
574 packet_write_fmt(fd_out, "ref-prefix %s\n",
575 ref_prefixes->v[i]);
577 packet_flush(fd_out);
579 /* Process response from server */
580 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
581 if (!process_ref_v2(reader, &list, unborn_head_target))
582 die(_("invalid ls-refs response: %s"), reader->line);
585 if (reader->status != PACKET_READ_FLUSH)
586 die(_("expected flush after ref listing"));
588 check_stateless_delimiter(stateless_rpc, reader,
589 _("expected response end packet after ref listing"));
591 return list;
594 const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp, int *offset)
596 int len;
598 if (!feature_list)
599 return NULL;
601 len = strlen(feature);
602 if (offset)
603 feature_list += *offset;
604 while (*feature_list) {
605 const char *found = strstr(feature_list, feature);
606 if (!found)
607 return NULL;
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;
614 if (offset)
615 *offset = found + len - feature_list;
616 return value;
618 /* feature with a value (e.g., "agent=git/1.2.3") */
619 else if (*value == '=') {
620 int end;
622 value++;
623 end = strcspn(value, " \t\n");
624 if (lenp)
625 *lenp = end;
626 if (offset)
627 *offset = value + end - feature_list;
628 return value;
631 * otherwise we matched a substring of another feature;
632 * keep looking
635 feature_list = found + 1;
637 return NULL;
640 int server_supports_hash(const char *desired, int *feature_supported)
642 int offset = 0;
643 int len;
644 const char *hash;
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);
653 while (hash) {
654 if (!xstrncmpz(desired, hash, len))
655 return 1;
657 hash = next_server_feature_value("object-format", &len, &offset);
659 return 0;
662 int parse_feature_request(const char *feature_list, const char *feature)
664 return !!parse_feature_value(feature_list, feature, NULL, NULL);
667 static const char *next_server_feature_value(const char *feature, int *len, int *offset)
669 return parse_feature_value(server_capabilities_v1, feature, len, offset);
672 const char *server_feature_value(const char *feature, int *len)
674 return parse_feature_value(server_capabilities_v1, feature, len, NULL);
677 int server_supports(const char *feature)
679 return !!server_feature_value(feature, NULL);
682 enum protocol {
683 PROTO_LOCAL = 1,
684 PROTO_FILE,
685 PROTO_SSH,
686 PROTO_GIT
689 int url_is_local_not_ssh(const char *url)
691 const char *colon = strchr(url, ':');
692 const char *slash = strchr(url, '/');
693 return !colon || (slash && slash < colon) ||
694 (has_dos_drive_prefix(url) && is_valid_path(url));
697 static const char *prot_name(enum protocol protocol)
699 switch (protocol) {
700 case PROTO_LOCAL:
701 case PROTO_FILE:
702 return "file";
703 case PROTO_SSH:
704 return "ssh";
705 case PROTO_GIT:
706 return "git";
707 default:
708 return "unknown protocol";
712 static enum protocol get_protocol(const char *name)
714 if (!strcmp(name, "ssh"))
715 return PROTO_SSH;
716 if (!strcmp(name, "git"))
717 return PROTO_GIT;
718 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
719 return PROTO_SSH;
720 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
721 return PROTO_SSH;
722 if (!strcmp(name, "file"))
723 return PROTO_FILE;
724 die(_("protocol '%s' is not supported"), name);
727 static char *host_end(char **hoststart, int removebrackets)
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++;
744 } else
745 end = host;
746 } else
747 end = host;
748 return end;
751 #define STR_(s) # s
752 #define STR(s) STR_(s)
754 static void get_host_and_port(char **host, const char **port)
756 char *colon, *end;
757 end = host_end(host, 1);
758 colon = strchr(end, ':');
759 if (colon) {
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;
764 } else if (!colon[1]) {
765 *colon = 0;
770 static void enable_keepalive(int sockfd)
772 int ka = 1;
774 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
775 error_errno(_("unable to set SO_KEEPALIVE on socket"));
778 #ifndef NO_IPV6
780 static const char *ai_name(const struct addrinfo *ai)
782 static char addr[NI_MAXHOST];
783 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
784 NI_NUMERICHOST) != 0)
785 xsnprintf(addr, sizeof(addr), "(unknown)");
787 return addr;
791 * Returns a connected socket() fd, or else die()s.
793 static int git_tcp_connect_sock(char *host, int flags)
795 struct strbuf error_message = STRBUF_INIT;
796 int sockfd = -1;
797 const char *port = STR(DEFAULT_GIT_PORT);
798 struct addrinfo hints, *ai0, *ai;
799 int gai;
800 int cnt = 0;
802 get_host_and_port(&host, &port);
803 if (!*port)
804 port = "<none>";
806 memset(&hints, 0, sizeof(hints));
807 if (flags & CONNECT_IPV4)
808 hints.ai_family = AF_INET;
809 else if (flags & CONNECT_IPV6)
810 hints.ai_family = AF_INET6;
811 hints.ai_socktype = SOCK_STREAM;
812 hints.ai_protocol = IPPROTO_TCP;
814 if (flags & CONNECT_VERBOSE)
815 fprintf(stderr, _("Looking up %s ... "), host);
817 gai = getaddrinfo(host, port, &hints, &ai);
818 if (gai)
819 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
821 if (flags & CONNECT_VERBOSE)
822 /* TRANSLATORS: this is the end of "Looking up %s ... " */
823 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
825 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
826 sockfd = socket(ai->ai_family,
827 ai->ai_socktype, ai->ai_protocol);
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);
834 sockfd = -1;
835 continue;
837 if (flags & CONNECT_VERBOSE)
838 fprintf(stderr, "%s ", ai_name(ai));
839 break;
842 freeaddrinfo(ai0);
844 if (sockfd < 0)
845 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
847 enable_keepalive(sockfd);
849 if (flags & CONNECT_VERBOSE)
850 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
851 fprintf_ln(stderr, _("done."));
853 strbuf_release(&error_message);
855 return sockfd;
858 #else /* NO_IPV6 */
861 * Returns a connected socket() fd, or else die()s.
863 static int git_tcp_connect_sock(char *host, int flags)
865 struct strbuf error_message = STRBUF_INIT;
866 int sockfd = -1;
867 const char *port = STR(DEFAULT_GIT_PORT);
868 char *ep;
869 struct hostent *he;
870 struct sockaddr_in sa;
871 char **ap;
872 unsigned int nport;
873 int cnt;
875 get_host_and_port(&host, &port);
877 if (flags & CONNECT_VERBOSE)
878 fprintf(stderr, _("Looking up %s ... "), host);
880 he = gethostbyname(host);
881 if (!he)
882 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
883 nport = strtoul(port, &ep, 10);
884 if ( ep == port || *ep ) {
885 /* Not numeric */
886 struct servent *se = getservbyname(port,"tcp");
887 if ( !se )
888 die(_("unknown port %s"), port);
889 nport = se->s_port;
892 if (flags & CONNECT_VERBOSE)
893 /* TRANSLATORS: this is the end of "Looking up %s ... " */
894 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
896 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
897 memset(&sa, 0, sizeof sa);
898 sa.sin_family = he->h_addrtype;
899 sa.sin_port = htons(nport);
900 memcpy(&sa.sin_addr, *ap, he->h_length);
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",
906 host,
907 cnt,
908 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
909 strerror(errno));
910 if (0 <= sockfd)
911 close(sockfd);
912 sockfd = -1;
913 continue;
915 if (flags & CONNECT_VERBOSE)
916 fprintf(stderr, "%s ",
917 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
918 break;
921 if (sockfd < 0)
922 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
924 enable_keepalive(sockfd);
926 if (flags & CONNECT_VERBOSE)
927 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
928 fprintf_ln(stderr, _("done."));
930 return sockfd;
933 #endif /* NO_IPV6 */
937 * Dummy child_process returned by git_connect() if the transport protocol
938 * does not need fork(2).
940 static struct child_process no_fork = CHILD_PROCESS_INIT;
942 int git_connection_is_socket(struct child_process *conn)
944 return conn == &no_fork;
947 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
949 int sockfd = git_tcp_connect_sock(host, flags);
951 fd[0] = sockfd;
952 fd[1] = dup(sockfd);
954 return &no_fork;
958 static char *git_proxy_command;
960 static int git_proxy_command_options(const char *var, const char *value,
961 void *cb)
963 if (!strcmp(var, "core.gitproxy")) {
964 const char *for_pos;
965 int matchlen = -1;
966 int hostlen;
967 const char *rhost_name = cb;
968 int rhost_len = strlen(rhost_name);
970 if (git_proxy_command)
971 return 0;
972 if (!value)
973 return config_error_nonbool(var);
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
980 for_pos = strstr(value, " for ");
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;
997 if (0 <= matchlen) {
998 /* core.gitproxy = none for kernel.org */
999 if (matchlen == 4 &&
1000 !memcmp(value, "none", 4))
1001 matchlen = 0;
1002 git_proxy_command = xmemdupz(value, matchlen);
1004 return 0;
1007 return git_default_config(var, value, cb);
1010 static int git_use_proxy(const char *host)
1012 git_proxy_command = getenv("GIT_PROXY_COMMAND");
1013 git_config(git_proxy_command_options, (void*)host);
1014 return (git_proxy_command && *git_proxy_command);
1017 static struct child_process *git_proxy_connect(int fd[2], char *host)
1019 const char *port = STR(DEFAULT_GIT_PORT);
1020 struct child_process *proxy;
1022 get_host_and_port(&host, &port);
1024 if (looks_like_command_line_option(host))
1025 die(_("strange hostname '%s' blocked"), host);
1026 if (looks_like_command_line_option(port))
1027 die(_("strange port '%s' blocked"), port);
1029 proxy = xmalloc(sizeof(*proxy));
1030 child_process_init(proxy);
1031 strvec_push(&proxy->args, git_proxy_command);
1032 strvec_push(&proxy->args, host);
1033 strvec_push(&proxy->args, port);
1034 proxy->in = -1;
1035 proxy->out = -1;
1036 if (start_command(proxy))
1037 die(_("cannot start proxy %s"), git_proxy_command);
1038 fd[0] = proxy->out; /* read from proxy stdout */
1039 fd[1] = proxy->in; /* write to proxy stdin */
1040 return proxy;
1043 static char *get_port(char *host)
1045 char *end;
1046 char *p = strchr(host, ':');
1048 if (p) {
1049 long port = strtol(p + 1, &end, 10);
1050 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
1051 *p = '\0';
1052 return p+1;
1056 return NULL;
1060 * Extract protocol and relevant parts from the specified connection URL.
1061 * The caller must free() the returned strings.
1063 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
1064 char **ret_path)
1066 char *url;
1067 char *host, *path;
1068 char *end;
1069 int separator = '/';
1070 enum protocol protocol = PROTO_LOCAL;
1072 if (is_url(url_orig))
1073 url = url_decode(url_orig);
1074 else
1075 url = xstrdup(url_orig);
1077 host = strstr(url, "://");
1078 if (host) {
1079 *host = '\0';
1080 protocol = get_protocol(url);
1081 host += 3;
1082 } else {
1083 host = url;
1084 if (!url_is_local_not_ssh(url)) {
1085 protocol = PROTO_SSH;
1086 separator = ':';
1091 * Don't do destructive transforms as protocol code does
1092 * '[]' unwrapping in get_host_and_port()
1094 end = host_end(&host, 0);
1096 if (protocol == PROTO_LOCAL)
1097 path = end;
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 "//" */
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);
1107 if (!path || !*path)
1108 die(_("no path specified; see 'git help pull' for valid url syntax"));
1111 * null-terminate hostname and point path to ~ for URL's like this:
1112 * ssh://host.xz/~user/repo
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) {
1119 if (path[1] == '~')
1120 path++;
1123 path = xstrdup(path);
1124 *end = '\0';
1126 *ret_host = xstrdup(host);
1127 *ret_path = path;
1128 free(url);
1129 return protocol;
1132 static const char *get_ssh_command(void)
1134 const char *ssh;
1136 if ((ssh = getenv("GIT_SSH_COMMAND")))
1137 return ssh;
1139 if (!git_config_get_string_tmp("core.sshcommand", &ssh))
1140 return ssh;
1142 return NULL;
1145 enum ssh_variant {
1146 VARIANT_AUTO,
1147 VARIANT_SIMPLE,
1148 VARIANT_SSH,
1149 VARIANT_PLINK,
1150 VARIANT_PUTTY,
1151 VARIANT_TORTOISEPLINK,
1154 static void override_ssh_variant(enum ssh_variant *ssh_variant)
1156 const char *variant = getenv("GIT_SSH_VARIANT");
1158 if (!variant && git_config_get_string_tmp("ssh.variant", &variant))
1159 return;
1161 if (!strcmp(variant, "auto"))
1162 *ssh_variant = VARIANT_AUTO;
1163 else if (!strcmp(variant, "plink"))
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;
1175 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1176 int is_cmdline)
1178 enum ssh_variant ssh_variant = VARIANT_AUTO;
1179 const char *variant;
1180 char *p = NULL;
1182 override_ssh_variant(&ssh_variant);
1184 if (ssh_variant != VARIANT_AUTO)
1185 return ssh_variant;
1187 if (!is_cmdline) {
1188 p = xstrdup(ssh_command);
1189 variant = basename(p);
1190 } else {
1191 const char **ssh_argv;
1193 p = xstrdup(ssh_command);
1194 if (split_cmdline(p, &ssh_argv) > 0) {
1195 variant = basename((char *)ssh_argv[0]);
1197 * At this point, variant points into the buffer
1198 * referenced by p, hence we do not need ssh_argv
1199 * any longer.
1201 free(ssh_argv);
1202 } else {
1203 free(p);
1204 return ssh_variant;
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;
1214 else if (!strcasecmp(variant, "tortoiseplink") ||
1215 !strcasecmp(variant, "tortoiseplink.exe"))
1216 ssh_variant = VARIANT_TORTOISEPLINK;
1218 free(p);
1219 return ssh_variant;
1223 * Open a connection using Git's native protocol.
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).
1228 static struct child_process *git_connect_git(int fd[2], char *hostandport,
1229 const char *path, const char *prog,
1230 enum protocol_version version,
1231 int flags)
1233 struct child_process *conn;
1234 struct strbuf request = STRBUF_INIT;
1236 * Set up virtual host information based on where we will
1237 * connect, unless the user has overridden us in
1238 * the environment.
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);
1246 transport_check_allowed("git");
1247 if (strchr(target_host, '\n') || strchr(path, '\n'))
1248 die(_("newline is forbidden in git:// hosts and repo paths"));
1251 * These underlying connection commands die() if they
1252 * cannot connect.
1254 if (git_use_proxy(hostandport))
1255 conn = git_proxy_connect(fd, hostandport);
1256 else
1257 conn = git_tcp_connect(fd, hostandport, flags);
1259 * Separate original protocol components prog and path
1260 * from extended host header with a NUL byte.
1262 * Note: Do not add any other headers here! Doing so
1263 * will cause older git-daemon servers to crash.
1265 strbuf_addf(&request,
1266 "%s %s%chost=%s%c",
1267 prog, path, 0,
1268 target_host, 0);
1270 /* If using a new version put that stuff here after a second null byte */
1271 if (version > 0) {
1272 strbuf_addch(&request, '\0');
1273 strbuf_addf(&request, "version=%d%c",
1274 version, '\0');
1277 packet_write(fd[1], request.buf, request.len);
1279 free(target_host);
1280 strbuf_release(&request);
1281 return conn;
1285 * Append the appropriate environment variables to `env` and options to
1286 * `args` for running ssh in Git's SSH-tunneled transport.
1288 static void push_ssh_options(struct strvec *args, struct strvec *env,
1289 enum ssh_variant variant, const char *port,
1290 enum protocol_version version, int flags)
1292 if (variant == VARIANT_SSH &&
1293 version > 0) {
1294 strvec_push(args, "-o");
1295 strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1296 strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1297 version);
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:
1305 die(_("ssh variant 'simple' does not support -4"));
1306 case VARIANT_SSH:
1307 case VARIANT_PLINK:
1308 case VARIANT_PUTTY:
1309 case VARIANT_TORTOISEPLINK:
1310 strvec_push(args, "-4");
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:
1317 die(_("ssh variant 'simple' does not support -6"));
1318 case VARIANT_SSH:
1319 case VARIANT_PLINK:
1320 case VARIANT_PUTTY:
1321 case VARIANT_TORTOISEPLINK:
1322 strvec_push(args, "-6");
1326 if (variant == VARIANT_TORTOISEPLINK)
1327 strvec_push(args, "-batch");
1329 if (port) {
1330 switch (variant) {
1331 case VARIANT_AUTO:
1332 BUG("VARIANT_AUTO passed to push_ssh_options");
1333 case VARIANT_SIMPLE:
1334 die(_("ssh variant 'simple' does not support setting port"));
1335 case VARIANT_SSH:
1336 strvec_push(args, "-p");
1337 break;
1338 case VARIANT_PLINK:
1339 case VARIANT_PUTTY:
1340 case VARIANT_TORTOISEPLINK:
1341 strvec_push(args, "-P");
1344 strvec_push(args, port);
1348 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1349 static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1350 const char *port, enum protocol_version version,
1351 int flags)
1353 const char *ssh;
1354 enum ssh_variant variant;
1356 if (looks_like_command_line_option(ssh_host))
1357 die(_("strange hostname '%s' blocked"), ssh_host);
1359 ssh = get_ssh_command();
1360 if (ssh) {
1361 variant = determine_ssh_variant(ssh, 1);
1362 } else {
1364 * GIT_SSH is the no-shell version of
1365 * GIT_SSH_COMMAND (and must remain so for
1366 * historical compatibility).
1368 conn->use_shell = 0;
1370 ssh = getenv("GIT_SSH");
1371 if (!ssh)
1372 ssh = "ssh";
1373 variant = determine_ssh_variant(ssh, 0);
1376 if (variant == VARIANT_AUTO) {
1377 struct child_process detect = CHILD_PROCESS_INIT;
1379 detect.use_shell = conn->use_shell;
1380 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1382 strvec_push(&detect.args, ssh);
1383 strvec_push(&detect.args, "-G");
1384 push_ssh_options(&detect.args, &detect.env,
1385 VARIANT_SSH, port, version, flags);
1386 strvec_push(&detect.args, ssh_host);
1388 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1391 strvec_push(&conn->args, ssh);
1392 push_ssh_options(&conn->args, &conn->env, variant, port, version,
1393 flags);
1394 strvec_push(&conn->args, ssh_host);
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).
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).
1408 struct child_process *git_connect(int fd[2], const char *url,
1409 const char *prog, int flags)
1411 char *hostandport, *path;
1412 struct child_process *conn;
1413 enum protocol protocol;
1414 enum protocol_version version = get_protocol_version_config();
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.
1421 if (version == protocol_v2 && !strcmp("git-receive-pack", prog))
1422 version = protocol_v0;
1424 /* Without this we cannot rely on waitpid() to tell
1425 * what happened to our children.
1427 signal(SIGCHLD, SIG_DFL);
1429 protocol = parse_connect_url(url, &hostandport, &path);
1430 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
1431 printf("Diag: url=%s\n", url ? url : "NULL");
1432 printf("Diag: protocol=%s\n", prot_name(protocol));
1433 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1434 printf("Diag: path=%s\n", path ? path : "NULL");
1435 conn = NULL;
1436 } else if (protocol == PROTO_GIT) {
1437 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1438 conn->trace2_child_class = "transport/git";
1439 } else {
1440 struct strbuf cmd = STRBUF_INIT;
1441 const char *const *var;
1443 conn = xmalloc(sizeof(*conn));
1444 child_process_init(conn);
1446 if (looks_like_command_line_option(path))
1447 die(_("strange pathname '%s' blocked"), path);
1449 strbuf_addstr(&cmd, prog);
1450 strbuf_addch(&cmd, ' ');
1451 sq_quote_buf(&cmd, path);
1453 /* remove repo-local variables from the environment */
1454 for (var = local_repo_env; *var; var++)
1455 strvec_push(&conn->env, *var);
1457 conn->use_shell = 1;
1458 conn->in = conn->out = -1;
1459 if (protocol == PROTO_SSH) {
1460 char *ssh_host = hostandport;
1461 const char *port = NULL;
1462 transport_check_allowed("ssh");
1463 get_host_and_port(&ssh_host, &port);
1465 if (!port)
1466 port = get_port(ssh_host);
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");
1475 free(hostandport);
1476 free(path);
1477 free(conn);
1478 strbuf_release(&cmd);
1479 return NULL;
1481 conn->trace2_child_class = "transport/ssh";
1482 fill_ssh_args(conn, ssh_host, port, version, flags);
1483 } else {
1484 transport_check_allowed("file");
1485 conn->trace2_child_class = "transport/file";
1486 if (version > 0) {
1487 strvec_pushf(&conn->env,
1488 GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1489 version);
1492 strvec_push(&conn->args, cmd.buf);
1494 if (start_command(conn))
1495 die(_("unable to fork"));
1497 fd[0] = conn->out; /* read from child's stdout */
1498 fd[1] = conn->in; /* write to child's stdin */
1499 strbuf_release(&cmd);
1501 free(hostandport);
1502 free(path);
1503 return conn;
1506 int finish_connect(struct child_process *conn)
1508 int code;
1509 if (!conn || git_connection_is_socket(conn))
1510 return 0;
1512 code = finish_command(conn);
1513 free(conn);
1514 return code;