describe: fix --no-exact-match
[git/debian.git] / connect.c
blob3a0186280c439a12fa6564ff9965cf284fb214d3
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 "transport.h"
16 #include "trace2.h"
17 #include "strbuf.h"
18 #include "version.h"
19 #include "protocol.h"
20 #include "alias.h"
21 #include "bundle-uri.h"
23 static char *server_capabilities_v1;
24 static struct strvec server_capabilities_v2 = STRVEC_INIT;
25 static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset);
27 static int check_ref(const char *name, unsigned int flags)
29 if (!flags)
30 return 1;
32 if (!skip_prefix(name, "refs/", &name))
33 return 0;
35 /* REF_NORMAL means that we don't want the magic fake tag refs */
36 if ((flags & REF_NORMAL) && check_refname_format(name,
37 REFNAME_ALLOW_ONELEVEL))
38 return 0;
40 /* REF_HEADS means that we want regular branch heads */
41 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
42 return 1;
44 /* REF_TAGS means that we want tags */
45 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
46 return 1;
48 /* All type bits clear means that we are ok with anything */
49 return !(flags & ~REF_NORMAL);
52 int check_ref_type(const struct ref *ref, int flags)
54 return check_ref(ref->name, flags);
57 static NORETURN void die_initial_contact(int unexpected)
60 * A hang-up after seeing some response from the other end
61 * means that it is unexpected, as we know the other end is
62 * willing to talk to us. A hang-up before seeing any
63 * response does not necessarily mean an ACL problem, though.
65 if (unexpected)
66 die(_("the remote end hung up upon initial contact"));
67 else
68 die(_("Could not read from remote repository.\n\n"
69 "Please make sure you have the correct access rights\n"
70 "and the repository exists."));
73 /* Checks if the server supports the capability 'c' */
74 int server_supports_v2(const char *c)
76 int i;
78 for (i = 0; i < server_capabilities_v2.nr; i++) {
79 const char *out;
80 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
81 (!*out || *out == '='))
82 return 1;
84 return 0;
87 void ensure_server_supports_v2(const char *c)
89 if (!server_supports_v2(c))
90 die(_("server doesn't support '%s'"), c);
93 int server_feature_v2(const char *c, const char **v)
95 int i;
97 for (i = 0; i < server_capabilities_v2.nr; i++) {
98 const char *out;
99 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
100 (*out == '=')) {
101 *v = out + 1;
102 return 1;
105 return 0;
108 int server_supports_feature(const char *c, const char *feature,
109 int die_on_error)
111 int i;
113 for (i = 0; i < server_capabilities_v2.nr; i++) {
114 const char *out;
115 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
116 (!*out || *(out++) == '=')) {
117 if (parse_feature_request(out, feature))
118 return 1;
119 else
120 break;
124 if (die_on_error)
125 die(_("server doesn't support feature '%s'"), feature);
127 return 0;
130 static void process_capabilities_v2(struct packet_reader *reader)
132 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
133 strvec_push(&server_capabilities_v2, reader->line);
135 if (reader->status != PACKET_READ_FLUSH)
136 die(_("expected flush after capabilities"));
139 enum protocol_version discover_version(struct packet_reader *reader)
141 enum protocol_version version = protocol_unknown_version;
144 * Peek the first line of the server's response to
145 * determine the protocol version the server is speaking.
147 switch (packet_reader_peek(reader)) {
148 case PACKET_READ_EOF:
149 die_initial_contact(0);
150 case PACKET_READ_FLUSH:
151 case PACKET_READ_DELIM:
152 case PACKET_READ_RESPONSE_END:
153 version = protocol_v0;
154 break;
155 case PACKET_READ_NORMAL:
156 version = determine_protocol_version_client(reader->line);
157 break;
160 switch (version) {
161 case protocol_v2:
162 process_capabilities_v2(reader);
163 break;
164 case protocol_v1:
165 /* Read the peeked version line */
166 packet_reader_read(reader);
167 break;
168 case protocol_v0:
169 break;
170 case protocol_unknown_version:
171 BUG("unknown protocol version");
174 trace2_data_intmax("transfer", NULL, "negotiated-version", version);
176 return version;
179 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
181 char *sym, *target;
182 struct string_list_item *item;
184 if (!len)
185 return; /* just "symref" */
186 /* e.g. "symref=HEAD:refs/heads/master" */
187 sym = xmemdupz(val, len);
188 target = strchr(sym, ':');
189 if (!target)
190 /* just "symref=something" */
191 goto reject;
192 *(target++) = '\0';
193 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
194 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
195 /* "symref=bogus:pair */
196 goto reject;
197 item = string_list_append_nodup(symref, sym);
198 item->util = target;
199 return;
200 reject:
201 free(sym);
202 return;
205 static void annotate_refs_with_symref_info(struct ref *ref)
207 struct string_list symref = STRING_LIST_INIT_DUP;
208 size_t offset = 0;
210 while (1) {
211 size_t len;
212 const char *val;
214 val = next_server_feature_value("symref", &len, &offset);
215 if (!val)
216 break;
217 parse_one_symref_info(&symref, val, len);
219 string_list_sort(&symref);
221 for (; ref; ref = ref->next) {
222 struct string_list_item *item;
223 item = string_list_lookup(&symref, ref->name);
224 if (!item)
225 continue;
226 ref->symref = xstrdup((char *)item->util);
228 string_list_clear(&symref, 0);
231 static void process_capabilities(struct packet_reader *reader, int *linelen)
233 const char *feat_val;
234 size_t feat_len;
235 const char *line = reader->line;
236 int nul_location = strlen(line);
237 if (nul_location == *linelen)
238 return;
239 server_capabilities_v1 = xstrdup(line + nul_location + 1);
240 *linelen = nul_location;
242 feat_val = server_feature_value("object-format", &feat_len);
243 if (feat_val) {
244 char *hash_name = xstrndup(feat_val, feat_len);
245 int hash_algo = hash_algo_by_name(hash_name);
246 if (hash_algo != GIT_HASH_UNKNOWN)
247 reader->hash_algo = &hash_algos[hash_algo];
248 free(hash_name);
249 } else {
250 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
254 static int process_dummy_ref(const struct packet_reader *reader)
256 const char *line = reader->line;
257 struct object_id oid;
258 const char *name;
260 if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
261 return 0;
262 if (*name != ' ')
263 return 0;
264 name++;
266 return oideq(reader->hash_algo->null_oid, &oid) &&
267 !strcmp(name, "capabilities^{}");
270 static void check_no_capabilities(const char *line, int len)
272 if (strlen(line) != len)
273 warning(_("ignoring capabilities after first line '%s'"),
274 line + strlen(line));
277 static int process_ref(const struct packet_reader *reader, int len,
278 struct ref ***list, unsigned int flags,
279 struct oid_array *extra_have)
281 const char *line = reader->line;
282 struct object_id old_oid;
283 const char *name;
285 if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
286 return 0;
287 if (*name != ' ')
288 return 0;
289 name++;
291 if (extra_have && !strcmp(name, ".have")) {
292 oid_array_append(extra_have, &old_oid);
293 } else if (!strcmp(name, "capabilities^{}")) {
294 die(_("protocol error: unexpected capabilities^{}"));
295 } else if (check_ref(name, flags)) {
296 struct ref *ref = alloc_ref(name);
297 oidcpy(&ref->old_oid, &old_oid);
298 **list = ref;
299 *list = &ref->next;
301 check_no_capabilities(line, len);
302 return 1;
305 static int process_shallow(const struct packet_reader *reader, int len,
306 struct oid_array *shallow_points)
308 const char *line = reader->line;
309 const char *arg;
310 struct object_id old_oid;
312 if (!skip_prefix(line, "shallow ", &arg))
313 return 0;
315 if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo))
316 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
317 if (!shallow_points)
318 die(_("repository on the other end cannot be shallow"));
319 oid_array_append(shallow_points, &old_oid);
320 check_no_capabilities(line, len);
321 return 1;
324 enum get_remote_heads_state {
325 EXPECTING_FIRST_REF = 0,
326 EXPECTING_REF,
327 EXPECTING_SHALLOW,
328 EXPECTING_DONE,
332 * Read all the refs from the other end
334 struct ref **get_remote_heads(struct packet_reader *reader,
335 struct ref **list, unsigned int flags,
336 struct oid_array *extra_have,
337 struct oid_array *shallow_points)
339 struct ref **orig_list = list;
340 int len = 0;
341 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
343 *list = NULL;
345 while (state != EXPECTING_DONE) {
346 switch (packet_reader_read(reader)) {
347 case PACKET_READ_EOF:
348 die_initial_contact(1);
349 case PACKET_READ_NORMAL:
350 len = reader->pktlen;
351 break;
352 case PACKET_READ_FLUSH:
353 state = EXPECTING_DONE;
354 break;
355 case PACKET_READ_DELIM:
356 case PACKET_READ_RESPONSE_END:
357 die(_("invalid packet"));
360 switch (state) {
361 case EXPECTING_FIRST_REF:
362 process_capabilities(reader, &len);
363 if (process_dummy_ref(reader)) {
364 state = EXPECTING_SHALLOW;
365 break;
367 state = EXPECTING_REF;
368 /* fallthrough */
369 case EXPECTING_REF:
370 if (process_ref(reader, len, &list, flags, extra_have))
371 break;
372 state = EXPECTING_SHALLOW;
373 /* fallthrough */
374 case EXPECTING_SHALLOW:
375 if (process_shallow(reader, len, shallow_points))
376 break;
377 die(_("protocol error: unexpected '%s'"), reader->line);
378 case EXPECTING_DONE:
379 break;
383 annotate_refs_with_symref_info(*orig_list);
385 return list;
388 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
389 static int process_ref_v2(struct packet_reader *reader, struct ref ***list,
390 const char **unborn_head_target)
392 int ret = 1;
393 int i = 0;
394 struct object_id old_oid;
395 struct ref *ref;
396 struct string_list line_sections = STRING_LIST_INIT_DUP;
397 const char *end;
398 const char *line = reader->line;
401 * Ref lines have a number of fields which are space deliminated. The
402 * first field is the OID of the ref. The second field is the ref
403 * name. Subsequent fields (symref-target and peeled) are optional and
404 * don't have a particular order.
406 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
407 ret = 0;
408 goto out;
411 if (!strcmp("unborn", line_sections.items[i].string)) {
412 i++;
413 if (unborn_head_target &&
414 !strcmp("HEAD", line_sections.items[i++].string)) {
416 * Look for the symref target (if any). If found,
417 * return it to the caller.
419 for (; i < line_sections.nr; i++) {
420 const char *arg = line_sections.items[i].string;
422 if (skip_prefix(arg, "symref-target:", &arg)) {
423 *unborn_head_target = xstrdup(arg);
424 break;
428 goto out;
430 if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) ||
431 *end) {
432 ret = 0;
433 goto out;
436 ref = alloc_ref(line_sections.items[i++].string);
438 memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
439 **list = ref;
440 *list = &ref->next;
442 for (; i < line_sections.nr; i++) {
443 const char *arg = line_sections.items[i].string;
444 if (skip_prefix(arg, "symref-target:", &arg))
445 ref->symref = xstrdup(arg);
447 if (skip_prefix(arg, "peeled:", &arg)) {
448 struct object_id peeled_oid;
449 char *peeled_name;
450 struct ref *peeled;
451 if (parse_oid_hex_algop(arg, &peeled_oid, &end,
452 reader->hash_algo) || *end) {
453 ret = 0;
454 goto out;
457 peeled_name = xstrfmt("%s^{}", ref->name);
458 peeled = alloc_ref(peeled_name);
460 memcpy(peeled->old_oid.hash, peeled_oid.hash,
461 reader->hash_algo->rawsz);
462 **list = peeled;
463 *list = &peeled->next;
465 free(peeled_name);
469 out:
470 string_list_clear(&line_sections, 0);
471 return ret;
474 void check_stateless_delimiter(int stateless_rpc,
475 struct packet_reader *reader,
476 const char *error)
478 if (!stateless_rpc)
479 return; /* not in stateless mode, no delimiter expected */
480 if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END)
481 die("%s", error);
484 static void send_capabilities(int fd_out, struct packet_reader *reader)
486 const char *hash_name;
488 if (server_supports_v2("agent"))
489 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
491 if (server_feature_v2("object-format", &hash_name)) {
492 int hash_algo = hash_algo_by_name(hash_name);
493 if (hash_algo == GIT_HASH_UNKNOWN)
494 die(_("unknown object format '%s' specified by server"), hash_name);
495 reader->hash_algo = &hash_algos[hash_algo];
496 packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
497 } else {
498 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
502 int get_remote_bundle_uri(int fd_out, struct packet_reader *reader,
503 struct bundle_list *bundles, int stateless_rpc)
505 int line_nr = 1;
507 /* Assert bundle-uri support */
508 ensure_server_supports_v2("bundle-uri");
510 /* (Re-)send capabilities */
511 send_capabilities(fd_out, reader);
513 /* Send command */
514 packet_write_fmt(fd_out, "command=bundle-uri\n");
515 packet_delim(fd_out);
517 packet_flush(fd_out);
519 /* Process response from server */
520 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
521 const char *line = reader->line;
522 line_nr++;
524 if (!bundle_uri_parse_line(bundles, line))
525 continue;
527 return error(_("error on bundle-uri response line %d: %s"),
528 line_nr, line);
531 if (reader->status != PACKET_READ_FLUSH)
532 return error(_("expected flush after bundle-uri listing"));
535 * Might die(), but obscure enough that that's OK, e.g. in
536 * serve.c we'll call BUG() on its equivalent (the
537 * PACKET_READ_RESPONSE_END check).
539 check_stateless_delimiter(stateless_rpc, reader,
540 _("expected response end packet after ref listing"));
542 return 0;
545 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
546 struct ref **list, int for_push,
547 struct transport_ls_refs_options *transport_options,
548 const struct string_list *server_options,
549 int stateless_rpc)
551 int i;
552 struct strvec *ref_prefixes = transport_options ?
553 &transport_options->ref_prefixes : NULL;
554 const char **unborn_head_target = transport_options ?
555 &transport_options->unborn_head_target : NULL;
556 *list = NULL;
558 ensure_server_supports_v2("ls-refs");
559 packet_write_fmt(fd_out, "command=ls-refs\n");
561 /* Send capabilities */
562 send_capabilities(fd_out, reader);
564 if (server_options && server_options->nr) {
565 ensure_server_supports_v2("server-option");
566 for (i = 0; i < server_options->nr; i++)
567 packet_write_fmt(fd_out, "server-option=%s",
568 server_options->items[i].string);
571 packet_delim(fd_out);
572 /* When pushing we don't want to request the peeled tags */
573 if (!for_push)
574 packet_write_fmt(fd_out, "peel\n");
575 packet_write_fmt(fd_out, "symrefs\n");
576 if (server_supports_feature("ls-refs", "unborn", 0))
577 packet_write_fmt(fd_out, "unborn\n");
578 for (i = 0; ref_prefixes && i < ref_prefixes->nr; i++) {
579 packet_write_fmt(fd_out, "ref-prefix %s\n",
580 ref_prefixes->v[i]);
582 packet_flush(fd_out);
584 /* Process response from server */
585 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
586 if (!process_ref_v2(reader, &list, unborn_head_target))
587 die(_("invalid ls-refs response: %s"), reader->line);
590 if (reader->status != PACKET_READ_FLUSH)
591 die(_("expected flush after ref listing"));
593 check_stateless_delimiter(stateless_rpc, reader,
594 _("expected response end packet after ref listing"));
596 return list;
599 const char *parse_feature_value(const char *feature_list, const char *feature, size_t *lenp, size_t *offset)
601 const char *orig_start = feature_list;
602 size_t len;
604 if (!feature_list)
605 return NULL;
607 len = strlen(feature);
608 if (offset)
609 feature_list += *offset;
610 while (*feature_list) {
611 const char *found = strstr(feature_list, feature);
612 if (!found)
613 return NULL;
614 if (feature_list == found || isspace(found[-1])) {
615 const char *value = found + len;
616 /* feature with no value (e.g., "thin-pack") */
617 if (!*value || isspace(*value)) {
618 if (lenp)
619 *lenp = 0;
620 if (offset)
621 *offset = found + len - orig_start;
622 return value;
624 /* feature with a value (e.g., "agent=git/1.2.3") */
625 else if (*value == '=') {
626 size_t end;
628 value++;
629 end = strcspn(value, " \t\n");
630 if (lenp)
631 *lenp = end;
632 if (offset)
633 *offset = value + end - orig_start;
634 return value;
637 * otherwise we matched a substring of another feature;
638 * keep looking
641 feature_list = found + 1;
643 return NULL;
646 int server_supports_hash(const char *desired, int *feature_supported)
648 size_t offset = 0;
649 size_t len;
650 const char *hash;
652 hash = next_server_feature_value("object-format", &len, &offset);
653 if (feature_supported)
654 *feature_supported = !!hash;
655 if (!hash) {
656 hash = hash_algos[GIT_HASH_SHA1].name;
657 len = strlen(hash);
659 while (hash) {
660 if (!xstrncmpz(desired, hash, len))
661 return 1;
663 hash = next_server_feature_value("object-format", &len, &offset);
665 return 0;
668 int parse_feature_request(const char *feature_list, const char *feature)
670 return !!parse_feature_value(feature_list, feature, NULL, NULL);
673 static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset)
675 return parse_feature_value(server_capabilities_v1, feature, len, offset);
678 const char *server_feature_value(const char *feature, size_t *len)
680 return parse_feature_value(server_capabilities_v1, feature, len, NULL);
683 int server_supports(const char *feature)
685 return !!server_feature_value(feature, NULL);
688 enum protocol {
689 PROTO_LOCAL = 1,
690 PROTO_FILE,
691 PROTO_SSH,
692 PROTO_GIT
695 int url_is_local_not_ssh(const char *url)
697 const char *colon = strchr(url, ':');
698 const char *slash = strchr(url, '/');
699 return !colon || (slash && slash < colon) ||
700 (has_dos_drive_prefix(url) && is_valid_path(url));
703 static const char *prot_name(enum protocol protocol)
705 switch (protocol) {
706 case PROTO_LOCAL:
707 case PROTO_FILE:
708 return "file";
709 case PROTO_SSH:
710 return "ssh";
711 case PROTO_GIT:
712 return "git";
713 default:
714 return "unknown protocol";
718 static enum protocol get_protocol(const char *name)
720 if (!strcmp(name, "ssh"))
721 return PROTO_SSH;
722 if (!strcmp(name, "git"))
723 return PROTO_GIT;
724 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
725 return PROTO_SSH;
726 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
727 return PROTO_SSH;
728 if (!strcmp(name, "file"))
729 return PROTO_FILE;
730 die(_("protocol '%s' is not supported"), name);
733 static char *host_end(char **hoststart, int removebrackets)
735 char *host = *hoststart;
736 char *end;
737 char *start = strstr(host, "@[");
738 if (start)
739 start++; /* Jump over '@' */
740 else
741 start = host;
742 if (start[0] == '[') {
743 end = strchr(start + 1, ']');
744 if (end) {
745 if (removebrackets) {
746 *end = 0;
747 memmove(start, start + 1, end - start);
748 end++;
750 } else
751 end = host;
752 } else
753 end = host;
754 return end;
757 #define STR_(s) # s
758 #define STR(s) STR_(s)
760 static void get_host_and_port(char **host, const char **port)
762 char *colon, *end;
763 end = host_end(host, 1);
764 colon = strchr(end, ':');
765 if (colon) {
766 long portnr = strtol(colon + 1, &end, 10);
767 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
768 *colon = 0;
769 *port = colon + 1;
770 } else if (!colon[1]) {
771 *colon = 0;
776 static void enable_keepalive(int sockfd)
778 int ka = 1;
780 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
781 error_errno(_("unable to set SO_KEEPALIVE on socket"));
784 #ifndef NO_IPV6
786 static const char *ai_name(const struct addrinfo *ai)
788 static char addr[NI_MAXHOST];
789 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
790 NI_NUMERICHOST) != 0)
791 xsnprintf(addr, sizeof(addr), "(unknown)");
793 return addr;
797 * Returns a connected socket() fd, or else die()s.
799 static int git_tcp_connect_sock(char *host, int flags)
801 struct strbuf error_message = STRBUF_INIT;
802 int sockfd = -1;
803 const char *port = STR(DEFAULT_GIT_PORT);
804 struct addrinfo hints, *ai0, *ai;
805 int gai;
806 int cnt = 0;
808 get_host_and_port(&host, &port);
809 if (!*port)
810 port = "<none>";
812 memset(&hints, 0, sizeof(hints));
813 if (flags & CONNECT_IPV4)
814 hints.ai_family = AF_INET;
815 else if (flags & CONNECT_IPV6)
816 hints.ai_family = AF_INET6;
817 hints.ai_socktype = SOCK_STREAM;
818 hints.ai_protocol = IPPROTO_TCP;
820 if (flags & CONNECT_VERBOSE)
821 fprintf(stderr, _("Looking up %s ... "), host);
823 gai = getaddrinfo(host, port, &hints, &ai);
824 if (gai)
825 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
827 if (flags & CONNECT_VERBOSE)
828 /* TRANSLATORS: this is the end of "Looking up %s ... " */
829 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
831 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
832 sockfd = socket(ai->ai_family,
833 ai->ai_socktype, ai->ai_protocol);
834 if ((sockfd < 0) ||
835 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
836 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
837 host, cnt, ai_name(ai), strerror(errno));
838 if (0 <= sockfd)
839 close(sockfd);
840 sockfd = -1;
841 continue;
843 if (flags & CONNECT_VERBOSE)
844 fprintf(stderr, "%s ", ai_name(ai));
845 break;
848 freeaddrinfo(ai0);
850 if (sockfd < 0)
851 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
853 enable_keepalive(sockfd);
855 if (flags & CONNECT_VERBOSE)
856 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
857 fprintf_ln(stderr, _("done."));
859 strbuf_release(&error_message);
861 return sockfd;
864 #else /* NO_IPV6 */
867 * Returns a connected socket() fd, or else die()s.
869 static int git_tcp_connect_sock(char *host, int flags)
871 struct strbuf error_message = STRBUF_INIT;
872 int sockfd = -1;
873 const char *port = STR(DEFAULT_GIT_PORT);
874 char *ep;
875 struct hostent *he;
876 struct sockaddr_in sa;
877 char **ap;
878 unsigned int nport;
879 int cnt;
881 get_host_and_port(&host, &port);
883 if (flags & CONNECT_VERBOSE)
884 fprintf(stderr, _("Looking up %s ... "), host);
886 he = gethostbyname(host);
887 if (!he)
888 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
889 nport = strtoul(port, &ep, 10);
890 if ( ep == port || *ep ) {
891 /* Not numeric */
892 struct servent *se = getservbyname(port,"tcp");
893 if ( !se )
894 die(_("unknown port %s"), port);
895 nport = se->s_port;
898 if (flags & CONNECT_VERBOSE)
899 /* TRANSLATORS: this is the end of "Looking up %s ... " */
900 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
902 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
903 memset(&sa, 0, sizeof sa);
904 sa.sin_family = he->h_addrtype;
905 sa.sin_port = htons(nport);
906 memcpy(&sa.sin_addr, *ap, he->h_length);
908 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
909 if ((sockfd < 0) ||
910 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
911 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
912 host,
913 cnt,
914 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
915 strerror(errno));
916 if (0 <= sockfd)
917 close(sockfd);
918 sockfd = -1;
919 continue;
921 if (flags & CONNECT_VERBOSE)
922 fprintf(stderr, "%s ",
923 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
924 break;
927 if (sockfd < 0)
928 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
930 enable_keepalive(sockfd);
932 if (flags & CONNECT_VERBOSE)
933 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
934 fprintf_ln(stderr, _("done."));
936 return sockfd;
939 #endif /* NO_IPV6 */
943 * Dummy child_process returned by git_connect() if the transport protocol
944 * does not need fork(2).
946 static struct child_process no_fork = CHILD_PROCESS_INIT;
948 int git_connection_is_socket(struct child_process *conn)
950 return conn == &no_fork;
953 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
955 int sockfd = git_tcp_connect_sock(host, flags);
957 fd[0] = sockfd;
958 fd[1] = dup(sockfd);
960 return &no_fork;
964 static char *git_proxy_command;
966 static int git_proxy_command_options(const char *var, const char *value,
967 void *cb)
969 if (!strcmp(var, "core.gitproxy")) {
970 const char *for_pos;
971 int matchlen = -1;
972 int hostlen;
973 const char *rhost_name = cb;
974 int rhost_len = strlen(rhost_name);
976 if (git_proxy_command)
977 return 0;
978 if (!value)
979 return config_error_nonbool(var);
980 /* [core]
981 * ;# matches www.kernel.org as well
982 * gitproxy = netcatter-1 for kernel.org
983 * gitproxy = netcatter-2 for sample.xz
984 * gitproxy = netcatter-default
986 for_pos = strstr(value, " for ");
987 if (!for_pos)
988 /* matches everybody */
989 matchlen = strlen(value);
990 else {
991 hostlen = strlen(for_pos + 5);
992 if (rhost_len < hostlen)
993 matchlen = -1;
994 else if (!strncmp(for_pos + 5,
995 rhost_name + rhost_len - hostlen,
996 hostlen) &&
997 ((rhost_len == hostlen) ||
998 rhost_name[rhost_len - hostlen -1] == '.'))
999 matchlen = for_pos - value;
1000 else
1001 matchlen = -1;
1003 if (0 <= matchlen) {
1004 /* core.gitproxy = none for kernel.org */
1005 if (matchlen == 4 &&
1006 !memcmp(value, "none", 4))
1007 matchlen = 0;
1008 git_proxy_command = xmemdupz(value, matchlen);
1010 return 0;
1013 return git_default_config(var, value, cb);
1016 static int git_use_proxy(const char *host)
1018 git_proxy_command = getenv("GIT_PROXY_COMMAND");
1019 git_config(git_proxy_command_options, (void*)host);
1020 return (git_proxy_command && *git_proxy_command);
1023 static struct child_process *git_proxy_connect(int fd[2], char *host)
1025 const char *port = STR(DEFAULT_GIT_PORT);
1026 struct child_process *proxy;
1028 get_host_and_port(&host, &port);
1030 if (looks_like_command_line_option(host))
1031 die(_("strange hostname '%s' blocked"), host);
1032 if (looks_like_command_line_option(port))
1033 die(_("strange port '%s' blocked"), port);
1035 proxy = xmalloc(sizeof(*proxy));
1036 child_process_init(proxy);
1037 strvec_push(&proxy->args, git_proxy_command);
1038 strvec_push(&proxy->args, host);
1039 strvec_push(&proxy->args, port);
1040 proxy->in = -1;
1041 proxy->out = -1;
1042 if (start_command(proxy))
1043 die(_("cannot start proxy %s"), git_proxy_command);
1044 fd[0] = proxy->out; /* read from proxy stdout */
1045 fd[1] = proxy->in; /* write to proxy stdin */
1046 return proxy;
1049 static char *get_port(char *host)
1051 char *end;
1052 char *p = strchr(host, ':');
1054 if (p) {
1055 long port = strtol(p + 1, &end, 10);
1056 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
1057 *p = '\0';
1058 return p+1;
1062 return NULL;
1066 * Extract protocol and relevant parts from the specified connection URL.
1067 * The caller must free() the returned strings.
1069 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
1070 char **ret_path)
1072 char *url;
1073 char *host, *path;
1074 char *end;
1075 int separator = '/';
1076 enum protocol protocol = PROTO_LOCAL;
1078 if (is_url(url_orig))
1079 url = url_decode(url_orig);
1080 else
1081 url = xstrdup(url_orig);
1083 host = strstr(url, "://");
1084 if (host) {
1085 *host = '\0';
1086 protocol = get_protocol(url);
1087 host += 3;
1088 } else {
1089 host = url;
1090 if (!url_is_local_not_ssh(url)) {
1091 protocol = PROTO_SSH;
1092 separator = ':';
1097 * Don't do destructive transforms as protocol code does
1098 * '[]' unwrapping in get_host_and_port()
1100 end = host_end(&host, 0);
1102 if (protocol == PROTO_LOCAL)
1103 path = end;
1104 else if (protocol == PROTO_FILE && *host != '/' &&
1105 !has_dos_drive_prefix(host) &&
1106 offset_1st_component(host - 2) > 1)
1107 path = host - 2; /* include the leading "//" */
1108 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
1109 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1110 else
1111 path = strchr(end, separator);
1113 if (!path || !*path)
1114 die(_("no path specified; see 'git help pull' for valid url syntax"));
1117 * null-terminate hostname and point path to ~ for URL's like this:
1118 * ssh://host.xz/~user/repo
1121 end = path; /* Need to \0 terminate host here */
1122 if (separator == ':')
1123 path++; /* path starts after ':' */
1124 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
1125 if (path[1] == '~')
1126 path++;
1129 path = xstrdup(path);
1130 *end = '\0';
1132 *ret_host = xstrdup(host);
1133 *ret_path = path;
1134 free(url);
1135 return protocol;
1138 static const char *get_ssh_command(void)
1140 const char *ssh;
1142 if ((ssh = getenv("GIT_SSH_COMMAND")))
1143 return ssh;
1145 if (!git_config_get_string_tmp("core.sshcommand", &ssh))
1146 return ssh;
1148 return NULL;
1151 enum ssh_variant {
1152 VARIANT_AUTO,
1153 VARIANT_SIMPLE,
1154 VARIANT_SSH,
1155 VARIANT_PLINK,
1156 VARIANT_PUTTY,
1157 VARIANT_TORTOISEPLINK,
1160 static void override_ssh_variant(enum ssh_variant *ssh_variant)
1162 const char *variant = getenv("GIT_SSH_VARIANT");
1164 if (!variant && git_config_get_string_tmp("ssh.variant", &variant))
1165 return;
1167 if (!strcmp(variant, "auto"))
1168 *ssh_variant = VARIANT_AUTO;
1169 else if (!strcmp(variant, "plink"))
1170 *ssh_variant = VARIANT_PLINK;
1171 else if (!strcmp(variant, "putty"))
1172 *ssh_variant = VARIANT_PUTTY;
1173 else if (!strcmp(variant, "tortoiseplink"))
1174 *ssh_variant = VARIANT_TORTOISEPLINK;
1175 else if (!strcmp(variant, "simple"))
1176 *ssh_variant = VARIANT_SIMPLE;
1177 else
1178 *ssh_variant = VARIANT_SSH;
1181 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1182 int is_cmdline)
1184 enum ssh_variant ssh_variant = VARIANT_AUTO;
1185 const char *variant;
1186 char *p = NULL;
1188 override_ssh_variant(&ssh_variant);
1190 if (ssh_variant != VARIANT_AUTO)
1191 return ssh_variant;
1193 if (!is_cmdline) {
1194 p = xstrdup(ssh_command);
1195 variant = basename(p);
1196 } else {
1197 const char **ssh_argv;
1199 p = xstrdup(ssh_command);
1200 if (split_cmdline(p, &ssh_argv) > 0) {
1201 variant = basename((char *)ssh_argv[0]);
1203 * At this point, variant points into the buffer
1204 * referenced by p, hence we do not need ssh_argv
1205 * any longer.
1207 free(ssh_argv);
1208 } else {
1209 free(p);
1210 return ssh_variant;
1214 if (!strcasecmp(variant, "ssh") ||
1215 !strcasecmp(variant, "ssh.exe"))
1216 ssh_variant = VARIANT_SSH;
1217 else if (!strcasecmp(variant, "plink") ||
1218 !strcasecmp(variant, "plink.exe"))
1219 ssh_variant = VARIANT_PLINK;
1220 else if (!strcasecmp(variant, "tortoiseplink") ||
1221 !strcasecmp(variant, "tortoiseplink.exe"))
1222 ssh_variant = VARIANT_TORTOISEPLINK;
1224 free(p);
1225 return ssh_variant;
1229 * Open a connection using Git's native protocol.
1231 * The caller is responsible for freeing hostandport, but this function may
1232 * modify it (for example, to truncate it to remove the port part).
1234 static struct child_process *git_connect_git(int fd[2], char *hostandport,
1235 const char *path, const char *prog,
1236 enum protocol_version version,
1237 int flags)
1239 struct child_process *conn;
1240 struct strbuf request = STRBUF_INIT;
1242 * Set up virtual host information based on where we will
1243 * connect, unless the user has overridden us in
1244 * the environment.
1246 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1247 if (target_host)
1248 target_host = xstrdup(target_host);
1249 else
1250 target_host = xstrdup(hostandport);
1252 transport_check_allowed("git");
1253 if (strchr(target_host, '\n') || strchr(path, '\n'))
1254 die(_("newline is forbidden in git:// hosts and repo paths"));
1257 * These underlying connection commands die() if they
1258 * cannot connect.
1260 if (git_use_proxy(hostandport))
1261 conn = git_proxy_connect(fd, hostandport);
1262 else
1263 conn = git_tcp_connect(fd, hostandport, flags);
1265 * Separate original protocol components prog and path
1266 * from extended host header with a NUL byte.
1268 * Note: Do not add any other headers here! Doing so
1269 * will cause older git-daemon servers to crash.
1271 strbuf_addf(&request,
1272 "%s %s%chost=%s%c",
1273 prog, path, 0,
1274 target_host, 0);
1276 /* If using a new version put that stuff here after a second null byte */
1277 if (version > 0) {
1278 strbuf_addch(&request, '\0');
1279 strbuf_addf(&request, "version=%d%c",
1280 version, '\0');
1283 packet_write(fd[1], request.buf, request.len);
1285 free(target_host);
1286 strbuf_release(&request);
1287 return conn;
1291 * Append the appropriate environment variables to `env` and options to
1292 * `args` for running ssh in Git's SSH-tunneled transport.
1294 static void push_ssh_options(struct strvec *args, struct strvec *env,
1295 enum ssh_variant variant, const char *port,
1296 enum protocol_version version, int flags)
1298 if (variant == VARIANT_SSH &&
1299 version > 0) {
1300 strvec_push(args, "-o");
1301 strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1302 strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1303 version);
1306 if (flags & CONNECT_IPV4) {
1307 switch (variant) {
1308 case VARIANT_AUTO:
1309 BUG("VARIANT_AUTO passed to push_ssh_options");
1310 case VARIANT_SIMPLE:
1311 die(_("ssh variant 'simple' does not support -4"));
1312 case VARIANT_SSH:
1313 case VARIANT_PLINK:
1314 case VARIANT_PUTTY:
1315 case VARIANT_TORTOISEPLINK:
1316 strvec_push(args, "-4");
1318 } else if (flags & CONNECT_IPV6) {
1319 switch (variant) {
1320 case VARIANT_AUTO:
1321 BUG("VARIANT_AUTO passed to push_ssh_options");
1322 case VARIANT_SIMPLE:
1323 die(_("ssh variant 'simple' does not support -6"));
1324 case VARIANT_SSH:
1325 case VARIANT_PLINK:
1326 case VARIANT_PUTTY:
1327 case VARIANT_TORTOISEPLINK:
1328 strvec_push(args, "-6");
1332 if (variant == VARIANT_TORTOISEPLINK)
1333 strvec_push(args, "-batch");
1335 if (port) {
1336 switch (variant) {
1337 case VARIANT_AUTO:
1338 BUG("VARIANT_AUTO passed to push_ssh_options");
1339 case VARIANT_SIMPLE:
1340 die(_("ssh variant 'simple' does not support setting port"));
1341 case VARIANT_SSH:
1342 strvec_push(args, "-p");
1343 break;
1344 case VARIANT_PLINK:
1345 case VARIANT_PUTTY:
1346 case VARIANT_TORTOISEPLINK:
1347 strvec_push(args, "-P");
1350 strvec_push(args, port);
1354 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1355 static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1356 const char *port, enum protocol_version version,
1357 int flags)
1359 const char *ssh;
1360 enum ssh_variant variant;
1362 if (looks_like_command_line_option(ssh_host))
1363 die(_("strange hostname '%s' blocked"), ssh_host);
1365 ssh = get_ssh_command();
1366 if (ssh) {
1367 variant = determine_ssh_variant(ssh, 1);
1368 } else {
1370 * GIT_SSH is the no-shell version of
1371 * GIT_SSH_COMMAND (and must remain so for
1372 * historical compatibility).
1374 conn->use_shell = 0;
1376 ssh = getenv("GIT_SSH");
1377 if (!ssh)
1378 ssh = "ssh";
1379 variant = determine_ssh_variant(ssh, 0);
1382 if (variant == VARIANT_AUTO) {
1383 struct child_process detect = CHILD_PROCESS_INIT;
1385 detect.use_shell = conn->use_shell;
1386 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1388 strvec_push(&detect.args, ssh);
1389 strvec_push(&detect.args, "-G");
1390 push_ssh_options(&detect.args, &detect.env,
1391 VARIANT_SSH, port, version, flags);
1392 strvec_push(&detect.args, ssh_host);
1394 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1397 strvec_push(&conn->args, ssh);
1398 push_ssh_options(&conn->args, &conn->env, variant, port, version,
1399 flags);
1400 strvec_push(&conn->args, ssh_host);
1404 * This returns the dummy child_process `no_fork` if the transport protocol
1405 * does not need fork(2), or a struct child_process object if it does. Once
1406 * done, finish the connection with finish_connect() with the value returned
1407 * from this function (it is safe to call finish_connect() with NULL to
1408 * support the former case).
1410 * If it returns, the connect is successful; it just dies on errors (this
1411 * will hopefully be changed in a libification effort, to return NULL when
1412 * the connection failed).
1414 struct child_process *git_connect(int fd[2], const char *url,
1415 const char *name,
1416 const char *prog, int flags)
1418 char *hostandport, *path;
1419 struct child_process *conn;
1420 enum protocol protocol;
1421 enum protocol_version version = get_protocol_version_config();
1424 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1425 * to perform any operation that doesn't involve upload-pack (i.e., a
1426 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
1427 * to do anything else (like push or remote archive) via v2.
1429 if (version == protocol_v2 && strcmp("git-upload-pack", name))
1430 version = protocol_v0;
1432 /* Without this we cannot rely on waitpid() to tell
1433 * what happened to our children.
1435 signal(SIGCHLD, SIG_DFL);
1437 protocol = parse_connect_url(url, &hostandport, &path);
1438 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
1439 printf("Diag: url=%s\n", url ? url : "NULL");
1440 printf("Diag: protocol=%s\n", prot_name(protocol));
1441 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1442 printf("Diag: path=%s\n", path ? path : "NULL");
1443 conn = NULL;
1444 } else if (protocol == PROTO_GIT) {
1445 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1446 conn->trace2_child_class = "transport/git";
1447 } else {
1448 struct strbuf cmd = STRBUF_INIT;
1449 const char *const *var;
1451 conn = xmalloc(sizeof(*conn));
1452 child_process_init(conn);
1454 if (looks_like_command_line_option(path))
1455 die(_("strange pathname '%s' blocked"), path);
1457 strbuf_addstr(&cmd, prog);
1458 strbuf_addch(&cmd, ' ');
1459 sq_quote_buf(&cmd, path);
1461 /* remove repo-local variables from the environment */
1462 for (var = local_repo_env; *var; var++)
1463 strvec_push(&conn->env, *var);
1465 conn->use_shell = 1;
1466 conn->in = conn->out = -1;
1467 if (protocol == PROTO_SSH) {
1468 char *ssh_host = hostandport;
1469 const char *port = NULL;
1470 transport_check_allowed("ssh");
1471 get_host_and_port(&ssh_host, &port);
1473 if (!port)
1474 port = get_port(ssh_host);
1476 if (flags & CONNECT_DIAG_URL) {
1477 printf("Diag: url=%s\n", url ? url : "NULL");
1478 printf("Diag: protocol=%s\n", prot_name(protocol));
1479 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1480 printf("Diag: port=%s\n", port ? port : "NONE");
1481 printf("Diag: path=%s\n", path ? path : "NULL");
1483 free(hostandport);
1484 free(path);
1485 free(conn);
1486 strbuf_release(&cmd);
1487 return NULL;
1489 conn->trace2_child_class = "transport/ssh";
1490 fill_ssh_args(conn, ssh_host, port, version, flags);
1491 } else {
1492 transport_check_allowed("file");
1493 conn->trace2_child_class = "transport/file";
1494 if (version > 0) {
1495 strvec_pushf(&conn->env,
1496 GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1497 version);
1500 strvec_push(&conn->args, cmd.buf);
1502 if (start_command(conn))
1503 die(_("unable to fork"));
1505 fd[0] = conn->out; /* read from child's stdout */
1506 fd[1] = conn->in; /* write to child's stdin */
1507 strbuf_release(&cmd);
1509 free(hostandport);
1510 free(path);
1511 return conn;
1514 int finish_connect(struct child_process *conn)
1516 int code;
1517 if (!conn || git_connection_is_socket(conn))
1518 return 0;
1520 code = finish_command(conn);
1521 free(conn);
1522 return code;