1 #include "git-compat-util.h"
7 #include "run-command.h"
11 #include "string-list.h"
12 #include "oid-array.h"
13 #include "transport.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
)
29 if (!skip_prefix(name
, "refs/", &name
))
32 /* REF_NORMAL means that we don't want the magic fake tag refs */
33 if ((flags
& REF_NORMAL
) && check_refname_format(name
, 0))
36 /* REF_HEADS means that we want regular branch heads */
37 if ((flags
& REF_HEADS
) && starts_with(name
, "heads/"))
40 /* REF_TAGS means that we want tags */
41 if ((flags
& REF_TAGS
) && starts_with(name
, "tags/"))
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.
62 die(_("the remote end hung up upon initial contact"));
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
)
74 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
76 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
77 (!*out
|| *out
== '='))
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
)
93 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
95 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
104 int server_supports_feature(const char *c
, const char *feature
,
109 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
111 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
112 (!*out
|| *(out
++) == '=')) {
113 if (parse_feature_request(out
, feature
))
121 die(_("server doesn't support feature '%s'"), feature
);
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
;
151 case PACKET_READ_NORMAL
:
152 version
= determine_protocol_version_client(reader
->line
);
158 process_capabilities_v2(reader
);
161 /* Read the peeked version line */
162 packet_reader_read(reader
);
166 case protocol_unknown_version
:
167 BUG("unknown protocol version");
170 trace2_data_intmax("transfer", NULL
, "negotiated-version", version
);
175 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
178 struct string_list_item
*item
;
181 return; /* just "symref" */
182 /* e.g. "symref=HEAD:refs/heads/master" */
183 sym
= xmemdupz(val
, len
);
184 target
= strchr(sym
, ':');
186 /* just "symref=something" */
189 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
190 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
191 /* "symref=bogus:pair */
193 item
= string_list_append_nodup(symref
, sym
);
201 static void annotate_refs_with_symref_info(struct ref
*ref
)
203 struct string_list symref
= STRING_LIST_INIT_DUP
;
210 val
= next_server_feature_value("symref", &len
, &offset
);
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
);
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
;
231 const char *line
= reader
->line
;
232 int nul_location
= strlen(line
);
233 if (nul_location
== *linelen
)
235 server_capabilities_v1
= xstrdup(line
+ nul_location
+ 1);
236 *linelen
= nul_location
;
238 feat_val
= server_feature_value("object-format", &feat_len
);
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
];
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
;
256 if (parse_oid_hex_algop(line
, &oid
, &name
, reader
->hash_algo
))
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
;
280 if (parse_oid_hex_algop(line
, &old_oid
, &name
, reader
->hash_algo
))
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
);
296 check_no_capabilities(line
, len
);
300 static int process_shallow(const struct packet_reader
*reader
, int len
,
301 struct oid_array
*shallow_points
)
303 const char *line
= reader
->line
;
305 struct object_id old_oid
;
307 if (!skip_prefix(line
, "shallow ", &arg
))
310 if (get_oid_hex_algop(arg
, &old_oid
, reader
->hash_algo
))
311 die(_("protocol error: expected shallow sha-1, got '%s'"), arg
);
313 die(_("repository on the other end cannot be shallow"));
314 oid_array_append(shallow_points
, &old_oid
);
315 check_no_capabilities(line
, len
);
319 enum get_remote_heads_state
{
320 EXPECTING_FIRST_REF
= 0,
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
;
336 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
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
;
347 case PACKET_READ_FLUSH
:
348 state
= EXPECTING_DONE
;
350 case PACKET_READ_DELIM
:
351 case PACKET_READ_RESPONSE_END
:
352 die(_("invalid packet"));
356 case EXPECTING_FIRST_REF
:
357 process_capabilities(reader
, &len
);
358 if (process_dummy_ref(reader
)) {
359 state
= EXPECTING_SHALLOW
;
362 state
= EXPECTING_REF
;
365 if (process_ref(reader
, len
, &list
, flags
, extra_have
))
367 state
= EXPECTING_SHALLOW
;
369 case EXPECTING_SHALLOW
:
370 if (process_shallow(reader
, len
, shallow_points
))
372 die(_("protocol error: unexpected '%s'"), reader
->line
);
378 annotate_refs_with_symref_info(*orig_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
)
389 struct object_id old_oid
;
391 struct string_list line_sections
= STRING_LIST_INIT_DUP
;
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) {
406 if (!strcmp("unborn", line_sections
.items
[i
].string
)) {
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
);
425 if (parse_oid_hex_algop(line_sections
.items
[i
++].string
, &old_oid
, &end
, reader
->hash_algo
) ||
431 ref
= alloc_ref(line_sections
.items
[i
++].string
);
433 memcpy(ref
->old_oid
.hash
, old_oid
.hash
, reader
->hash_algo
->rawsz
);
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
;
446 if (parse_oid_hex_algop(arg
, &peeled_oid
, &end
,
447 reader
->hash_algo
) || *end
) {
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
);
458 *list
= &peeled
->next
;
465 string_list_clear(&line_sections
, 0);
469 void check_stateless_delimiter(int stateless_rpc
,
470 struct packet_reader
*reader
,
474 return; /* not in stateless mode, no delimiter expected */
475 if (packet_reader_read(reader
) != PACKET_READ_RESPONSE_END
)
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
);
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
)
502 /* Assert bundle-uri support */
503 ensure_server_supports_v2("bundle-uri");
505 /* (Re-)send capabilities */
506 send_capabilities(fd_out
, reader
);
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
;
519 if (!bundle_uri_parse_line(bundles
, line
))
522 return error(_("error on bundle-uri response line %d: %s"),
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"));
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
,
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
;
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 */
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",
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"));
594 const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
, int *offset
)
601 len
= strlen(feature
);
603 feature_list
+= *offset
;
604 while (*feature_list
) {
605 const char *found
= strstr(feature_list
, feature
);
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
)) {
615 *offset
= found
+ len
- feature_list
;
618 /* feature with a value (e.g., "agent=git/1.2.3") */
619 else if (*value
== '=') {
623 end
= strcspn(value
, " \t\n");
627 *offset
= value
+ end
- feature_list
;
631 * otherwise we matched a substring of another feature;
635 feature_list
= found
+ 1;
640 int server_supports_hash(const char *desired
, int *feature_supported
)
646 hash
= next_server_feature_value("object-format", &len
, &offset
);
647 if (feature_supported
)
648 *feature_supported
= !!hash
;
650 hash
= hash_algos
[GIT_HASH_SHA1
].name
;
654 if (!xstrncmpz(desired
, hash
, len
))
657 hash
= next_server_feature_value("object-format", &len
, &offset
);
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
);
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
)
708 return "unknown protocol";
712 static enum protocol
get_protocol(const char *name
)
714 if (!strcmp(name
, "ssh"))
716 if (!strcmp(name
, "git"))
718 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
720 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
722 if (!strcmp(name
, "file"))
724 die(_("protocol '%s' is not supported"), name
);
727 static char *host_end(char **hoststart
, int removebrackets
)
729 char *host
= *hoststart
;
731 char *start
= strstr(host
, "@[");
733 start
++; /* Jump over '@' */
736 if (start
[0] == '[') {
737 end
= strchr(start
+ 1, ']');
739 if (removebrackets
) {
741 memmove(start
, start
+ 1, end
- start
);
752 #define STR(s) STR_(s)
754 static void get_host_and_port(char **host
, const char **port
)
757 end
= host_end(host
, 1);
758 colon
= strchr(end
, ':');
760 long portnr
= strtol(colon
+ 1, &end
, 10);
761 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
764 } else if (!colon
[1]) {
770 static void enable_keepalive(int sockfd
)
774 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
775 error_errno(_("unable to set SO_KEEPALIVE on socket"));
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)");
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
;
797 const char *port
= STR(DEFAULT_GIT_PORT
);
798 struct addrinfo hints
, *ai0
, *ai
;
802 get_host_and_port(&host
, &port
);
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
);
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
);
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
));
837 if (flags
& CONNECT_VERBOSE
)
838 fprintf(stderr
, "%s ", ai_name(ai
));
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
);
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
;
867 const char *port
= STR(DEFAULT_GIT_PORT
);
870 struct sockaddr_in sa
;
875 get_host_and_port(&host
, &port
);
877 if (flags
& CONNECT_VERBOSE
)
878 fprintf(stderr
, _("Looking up %s ... "), host
);
880 he
= gethostbyname(host
);
882 die(_("unable to look up %s (%s)"), host
, hstrerror(h_errno
));
883 nport
= strtoul(port
, &ep
, 10);
884 if ( ep
== port
|| *ep
) {
886 struct servent
*se
= getservbyname(port
,"tcp");
888 die(_("unknown port %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);
904 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
905 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
908 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
915 if (flags
& CONNECT_VERBOSE
)
916 fprintf(stderr
, "%s ",
917 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
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."));
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
);
958 static char *git_proxy_command
;
960 static int git_proxy_command_options(const char *var
, const char *value
,
963 if (!strcmp(var
, "core.gitproxy")) {
967 const char *rhost_name
= cb
;
968 int rhost_len
= strlen(rhost_name
);
970 if (git_proxy_command
)
973 return config_error_nonbool(var
);
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 ");
982 /* matches everybody */
983 matchlen
= strlen(value
);
985 hostlen
= strlen(for_pos
+ 5);
986 if (rhost_len
< hostlen
)
988 else if (!strncmp(for_pos
+ 5,
989 rhost_name
+ rhost_len
- hostlen
,
991 ((rhost_len
== hostlen
) ||
992 rhost_name
[rhost_len
- hostlen
-1] == '.'))
993 matchlen
= for_pos
- value
;
998 /* core.gitproxy = none for kernel.org */
1000 !memcmp(value
, "none", 4))
1002 git_proxy_command
= xmemdupz(value
, matchlen
);
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
);
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 */
1043 static char *get_port(char *host
)
1046 char *p
= strchr(host
, ':');
1049 long port
= strtol(p
+ 1, &end
, 10);
1050 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
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
,
1069 int separator
= '/';
1070 enum protocol protocol
= PROTO_LOCAL
;
1072 if (is_url(url_orig
))
1073 url
= url_decode(url_orig
);
1075 url
= xstrdup(url_orig
);
1077 host
= strstr(url
, "://");
1080 protocol
= get_protocol(url
);
1084 if (!url_is_local_not_ssh(url
)) {
1085 protocol
= PROTO_SSH
;
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
)
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" */
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
) {
1123 path
= xstrdup(path
);
1126 *ret_host
= xstrdup(host
);
1132 static const char *get_ssh_command(void)
1136 if ((ssh
= getenv("GIT_SSH_COMMAND")))
1139 if (!git_config_get_string_tmp("core.sshcommand", &ssh
))
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
))
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
;
1172 *ssh_variant
= VARIANT_SSH
;
1175 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
1178 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
1179 const char *variant
;
1182 override_ssh_variant(&ssh_variant
);
1184 if (ssh_variant
!= VARIANT_AUTO
)
1188 p
= xstrdup(ssh_command
);
1189 variant
= basename(p
);
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
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
;
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
,
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
1240 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1242 target_host
= xstrdup(target_host
);
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
1254 if (git_use_proxy(hostandport
))
1255 conn
= git_proxy_connect(fd
, hostandport
);
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
,
1270 /* If using a new version put that stuff here after a second null byte */
1272 strbuf_addch(&request
, '\0');
1273 strbuf_addf(&request
, "version=%d%c",
1277 packet_write(fd
[1], request
.buf
, request
.len
);
1280 strbuf_release(&request
);
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
&&
1294 strvec_push(args
, "-o");
1295 strvec_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
1296 strvec_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1300 if (flags
& CONNECT_IPV4
) {
1303 BUG("VARIANT_AUTO passed to push_ssh_options");
1304 case VARIANT_SIMPLE
:
1305 die(_("ssh variant 'simple' does not support -4"));
1309 case VARIANT_TORTOISEPLINK
:
1310 strvec_push(args
, "-4");
1312 } else if (flags
& CONNECT_IPV6
) {
1315 BUG("VARIANT_AUTO passed to push_ssh_options");
1316 case VARIANT_SIMPLE
:
1317 die(_("ssh variant 'simple' does not support -6"));
1321 case VARIANT_TORTOISEPLINK
:
1322 strvec_push(args
, "-6");
1326 if (variant
== VARIANT_TORTOISEPLINK
)
1327 strvec_push(args
, "-batch");
1332 BUG("VARIANT_AUTO passed to push_ssh_options");
1333 case VARIANT_SIMPLE
:
1334 die(_("ssh variant 'simple' does not support setting port"));
1336 strvec_push(args
, "-p");
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
,
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();
1361 variant
= determine_ssh_variant(ssh
, 1);
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");
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
,
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");
1436 } else if (protocol
== PROTO_GIT
) {
1437 conn
= git_connect_git(fd
, hostandport
, path
, prog
, version
, flags
);
1438 conn
->trace2_child_class
= "transport/git";
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
);
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");
1478 strbuf_release(&cmd
);
1481 conn
->trace2_child_class
= "transport/ssh";
1482 fill_ssh_args(conn
, ssh_host
, port
, version
, flags
);
1484 transport_check_allowed("file");
1485 conn
->trace2_child_class
= "transport/file";
1487 strvec_pushf(&conn
->env
,
1488 GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
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
);
1506 int finish_connect(struct child_process
*conn
)
1509 if (!conn
|| git_connection_is_socket(conn
))
1512 code
= finish_command(conn
);