1 #include "git-compat-util.h"
3 #include "environment.h"
9 #include "run-command.h"
13 #include "string-list.h"
14 #include "oid-array.h"
15 #include "transport.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
)
32 if (!skip_prefix(name
, "refs/", &name
))
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
))
40 /* REF_HEADS means that we want regular branch heads */
41 if ((flags
& REF_HEADS
) && starts_with(name
, "heads/"))
44 /* REF_TAGS means that we want tags */
45 if ((flags
& REF_TAGS
) && starts_with(name
, "tags/"))
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.
66 die(_("the remote end hung up upon initial contact"));
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
)
78 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
80 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
81 (!*out
|| *out
== '='))
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
)
97 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
99 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
108 int server_supports_feature(const char *c
, const char *feature
,
113 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
115 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
116 (!*out
|| *(out
++) == '=')) {
117 if (parse_feature_request(out
, feature
))
125 die(_("server doesn't support feature '%s'"), feature
);
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
;
155 case PACKET_READ_NORMAL
:
156 version
= determine_protocol_version_client(reader
->line
);
162 process_capabilities_v2(reader
);
165 /* Read the peeked version line */
166 packet_reader_read(reader
);
170 case protocol_unknown_version
:
171 BUG("unknown protocol version");
174 trace2_data_intmax("transfer", NULL
, "negotiated-version", version
);
179 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
182 struct string_list_item
*item
;
185 return; /* just "symref" */
186 /* e.g. "symref=HEAD:refs/heads/master" */
187 sym
= xmemdupz(val
, len
);
188 target
= strchr(sym
, ':');
190 /* just "symref=something" */
193 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
194 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
195 /* "symref=bogus:pair */
197 item
= string_list_append_nodup(symref
, sym
);
205 static void annotate_refs_with_symref_info(struct ref
*ref
)
207 struct string_list symref
= STRING_LIST_INIT_DUP
;
214 val
= next_server_feature_value("symref", &len
, &offset
);
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
);
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
;
235 const char *line
= reader
->line
;
236 int nul_location
= strlen(line
);
237 if (nul_location
== *linelen
)
239 server_capabilities_v1
= xstrdup(line
+ nul_location
+ 1);
240 *linelen
= nul_location
;
242 feat_val
= server_feature_value("object-format", &feat_len
);
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
];
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
;
260 if (parse_oid_hex_algop(line
, &oid
, &name
, reader
->hash_algo
))
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
;
285 if (parse_oid_hex_algop(line
, &old_oid
, &name
, reader
->hash_algo
))
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
);
301 check_no_capabilities(line
, len
);
305 static int process_shallow(const struct packet_reader
*reader
, int len
,
306 struct oid_array
*shallow_points
)
308 const char *line
= reader
->line
;
310 struct object_id old_oid
;
312 if (!skip_prefix(line
, "shallow ", &arg
))
315 if (get_oid_hex_algop(arg
, &old_oid
, reader
->hash_algo
))
316 die(_("protocol error: expected shallow sha-1, got '%s'"), arg
);
318 die(_("repository on the other end cannot be shallow"));
319 oid_array_append(shallow_points
, &old_oid
);
320 check_no_capabilities(line
, len
);
324 enum get_remote_heads_state
{
325 EXPECTING_FIRST_REF
= 0,
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
;
341 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
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
;
352 case PACKET_READ_FLUSH
:
353 state
= EXPECTING_DONE
;
355 case PACKET_READ_DELIM
:
356 case PACKET_READ_RESPONSE_END
:
357 die(_("invalid packet"));
361 case EXPECTING_FIRST_REF
:
362 process_capabilities(reader
, &len
);
363 if (process_dummy_ref(reader
)) {
364 state
= EXPECTING_SHALLOW
;
367 state
= EXPECTING_REF
;
370 if (process_ref(reader
, len
, &list
, flags
, extra_have
))
372 state
= EXPECTING_SHALLOW
;
374 case EXPECTING_SHALLOW
:
375 if (process_shallow(reader
, len
, shallow_points
))
377 die(_("protocol error: unexpected '%s'"), reader
->line
);
383 annotate_refs_with_symref_info(*orig_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
)
394 struct object_id old_oid
;
396 struct string_list line_sections
= STRING_LIST_INIT_DUP
;
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) {
411 if (!strcmp("unborn", line_sections
.items
[i
].string
)) {
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
);
430 if (parse_oid_hex_algop(line_sections
.items
[i
++].string
, &old_oid
, &end
, reader
->hash_algo
) ||
436 ref
= alloc_ref(line_sections
.items
[i
++].string
);
438 memcpy(ref
->old_oid
.hash
, old_oid
.hash
, reader
->hash_algo
->rawsz
);
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
;
451 if (parse_oid_hex_algop(arg
, &peeled_oid
, &end
,
452 reader
->hash_algo
) || *end
) {
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
);
463 *list
= &peeled
->next
;
470 string_list_clear(&line_sections
, 0);
474 void check_stateless_delimiter(int stateless_rpc
,
475 struct packet_reader
*reader
,
479 return; /* not in stateless mode, no delimiter expected */
480 if (packet_reader_read(reader
) != PACKET_READ_RESPONSE_END
)
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
);
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
)
507 /* Assert bundle-uri support */
508 ensure_server_supports_v2("bundle-uri");
510 /* (Re-)send capabilities */
511 send_capabilities(fd_out
, reader
);
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
;
524 if (!bundle_uri_parse_line(bundles
, line
))
527 return error(_("error on bundle-uri response line %d: %s"),
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"));
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
,
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
;
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 */
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",
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"));
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
;
607 len
= strlen(feature
);
609 feature_list
+= *offset
;
610 while (*feature_list
) {
611 const char *found
= strstr(feature_list
, feature
);
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
)) {
621 *offset
= found
+ len
- orig_start
;
624 /* feature with a value (e.g., "agent=git/1.2.3") */
625 else if (*value
== '=') {
629 end
= strcspn(value
, " \t\n");
633 *offset
= value
+ end
- orig_start
;
637 * otherwise we matched a substring of another feature;
641 feature_list
= found
+ 1;
646 int server_supports_hash(const char *desired
, int *feature_supported
)
652 hash
= next_server_feature_value("object-format", &len
, &offset
);
653 if (feature_supported
)
654 *feature_supported
= !!hash
;
656 hash
= hash_algos
[GIT_HASH_SHA1
].name
;
660 if (!xstrncmpz(desired
, hash
, len
))
663 hash
= next_server_feature_value("object-format", &len
, &offset
);
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
);
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
)
714 return "unknown protocol";
718 static enum protocol
get_protocol(const char *name
)
720 if (!strcmp(name
, "ssh"))
722 if (!strcmp(name
, "git"))
724 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
726 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
728 if (!strcmp(name
, "file"))
730 die(_("protocol '%s' is not supported"), name
);
733 static char *host_end(char **hoststart
, int removebrackets
)
735 char *host
= *hoststart
;
737 char *start
= strstr(host
, "@[");
739 start
++; /* Jump over '@' */
742 if (start
[0] == '[') {
743 end
= strchr(start
+ 1, ']');
745 if (removebrackets
) {
747 memmove(start
, start
+ 1, end
- start
);
758 #define STR(s) STR_(s)
760 static void get_host_and_port(char **host
, const char **port
)
763 end
= host_end(host
, 1);
764 colon
= strchr(end
, ':');
766 long portnr
= strtol(colon
+ 1, &end
, 10);
767 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
770 } else if (!colon
[1]) {
776 static void enable_keepalive(int sockfd
)
780 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
781 error_errno(_("unable to set SO_KEEPALIVE on socket"));
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)");
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
;
803 const char *port
= STR(DEFAULT_GIT_PORT
);
804 struct addrinfo hints
, *ai0
, *ai
;
808 get_host_and_port(&host
, &port
);
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
);
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
);
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
));
843 if (flags
& CONNECT_VERBOSE
)
844 fprintf(stderr
, "%s ", ai_name(ai
));
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
);
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
;
873 const char *port
= STR(DEFAULT_GIT_PORT
);
876 struct sockaddr_in sa
;
881 get_host_and_port(&host
, &port
);
883 if (flags
& CONNECT_VERBOSE
)
884 fprintf(stderr
, _("Looking up %s ... "), host
);
886 he
= gethostbyname(host
);
888 die(_("unable to look up %s (%s)"), host
, hstrerror(h_errno
));
889 nport
= strtoul(port
, &ep
, 10);
890 if ( ep
== port
|| *ep
) {
892 struct servent
*se
= getservbyname(port
,"tcp");
894 die(_("unknown port %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);
910 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
911 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
914 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
921 if (flags
& CONNECT_VERBOSE
)
922 fprintf(stderr
, "%s ",
923 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
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."));
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
);
964 static char *git_proxy_command
;
966 static int git_proxy_command_options(const char *var
, const char *value
,
969 if (!strcmp(var
, "core.gitproxy")) {
973 const char *rhost_name
= cb
;
974 int rhost_len
= strlen(rhost_name
);
976 if (git_proxy_command
)
979 return config_error_nonbool(var
);
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 ");
988 /* matches everybody */
989 matchlen
= strlen(value
);
991 hostlen
= strlen(for_pos
+ 5);
992 if (rhost_len
< hostlen
)
994 else if (!strncmp(for_pos
+ 5,
995 rhost_name
+ rhost_len
- hostlen
,
997 ((rhost_len
== hostlen
) ||
998 rhost_name
[rhost_len
- hostlen
-1] == '.'))
999 matchlen
= for_pos
- value
;
1003 if (0 <= matchlen
) {
1004 /* core.gitproxy = none for kernel.org */
1005 if (matchlen
== 4 &&
1006 !memcmp(value
, "none", 4))
1008 git_proxy_command
= xmemdupz(value
, matchlen
);
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
);
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 */
1049 static char *get_port(char *host
)
1052 char *p
= strchr(host
, ':');
1055 long port
= strtol(p
+ 1, &end
, 10);
1056 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
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
,
1075 int separator
= '/';
1076 enum protocol protocol
= PROTO_LOCAL
;
1078 if (is_url(url_orig
))
1079 url
= url_decode(url_orig
);
1081 url
= xstrdup(url_orig
);
1083 host
= strstr(url
, "://");
1086 protocol
= get_protocol(url
);
1090 if (!url_is_local_not_ssh(url
)) {
1091 protocol
= PROTO_SSH
;
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
)
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" */
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
) {
1129 path
= xstrdup(path
);
1132 *ret_host
= xstrdup(host
);
1138 static const char *get_ssh_command(void)
1142 if ((ssh
= getenv("GIT_SSH_COMMAND")))
1145 if (!git_config_get_string_tmp("core.sshcommand", &ssh
))
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
))
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
;
1178 *ssh_variant
= VARIANT_SSH
;
1181 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
1184 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
1185 const char *variant
;
1188 override_ssh_variant(&ssh_variant
);
1190 if (ssh_variant
!= VARIANT_AUTO
)
1194 p
= xstrdup(ssh_command
);
1195 variant
= basename(p
);
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
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
;
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
,
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
1246 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1248 target_host
= xstrdup(target_host
);
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
1260 if (git_use_proxy(hostandport
))
1261 conn
= git_proxy_connect(fd
, hostandport
);
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
,
1276 /* If using a new version put that stuff here after a second null byte */
1278 strbuf_addch(&request
, '\0');
1279 strbuf_addf(&request
, "version=%d%c",
1283 packet_write(fd
[1], request
.buf
, request
.len
);
1286 strbuf_release(&request
);
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
&&
1300 strvec_push(args
, "-o");
1301 strvec_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
1302 strvec_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1306 if (flags
& CONNECT_IPV4
) {
1309 BUG("VARIANT_AUTO passed to push_ssh_options");
1310 case VARIANT_SIMPLE
:
1311 die(_("ssh variant 'simple' does not support -4"));
1315 case VARIANT_TORTOISEPLINK
:
1316 strvec_push(args
, "-4");
1318 } else if (flags
& CONNECT_IPV6
) {
1321 BUG("VARIANT_AUTO passed to push_ssh_options");
1322 case VARIANT_SIMPLE
:
1323 die(_("ssh variant 'simple' does not support -6"));
1327 case VARIANT_TORTOISEPLINK
:
1328 strvec_push(args
, "-6");
1332 if (variant
== VARIANT_TORTOISEPLINK
)
1333 strvec_push(args
, "-batch");
1338 BUG("VARIANT_AUTO passed to push_ssh_options");
1339 case VARIANT_SIMPLE
:
1340 die(_("ssh variant 'simple' does not support setting port"));
1342 strvec_push(args
, "-p");
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
,
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();
1367 variant
= determine_ssh_variant(ssh
, 1);
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");
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
,
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
,
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");
1444 } else if (protocol
== PROTO_GIT
) {
1445 conn
= git_connect_git(fd
, hostandport
, path
, prog
, version
, flags
);
1446 conn
->trace2_child_class
= "transport/git";
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
);
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");
1486 strbuf_release(&cmd
);
1489 conn
->trace2_child_class
= "transport/ssh";
1490 fill_ssh_args(conn
, ssh_host
, port
, version
, flags
);
1492 transport_check_allowed("file");
1493 conn
->trace2_child_class
= "transport/file";
1495 strvec_pushf(&conn
->env
,
1496 GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
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
);
1514 int finish_connect(struct child_process
*conn
)
1517 if (!conn
|| git_connection_is_socket(conn
))
1520 code
= finish_command(conn
);