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"
16 #include "transport.h"
22 #include "bundle-uri.h"
24 static char *server_capabilities_v1
;
25 static struct strvec server_capabilities_v2
= STRVEC_INIT
;
26 static const char *next_server_feature_value(const char *feature
, size_t *len
, size_t *offset
);
28 static int check_ref(const char *name
, unsigned int flags
)
33 if (!skip_prefix(name
, "refs/", &name
))
36 /* REF_NORMAL means that we don't want the magic fake tag refs */
37 if ((flags
& REF_NORMAL
) && check_refname_format(name
,
38 REFNAME_ALLOW_ONELEVEL
))
41 /* REF_HEADS means that we want regular branch heads */
42 if ((flags
& REF_HEADS
) && starts_with(name
, "heads/"))
45 /* REF_TAGS means that we want tags */
46 if ((flags
& REF_TAGS
) && starts_with(name
, "tags/"))
49 /* All type bits clear means that we are ok with anything */
50 return !(flags
& ~REF_NORMAL
);
53 int check_ref_type(const struct ref
*ref
, int flags
)
55 return check_ref(ref
->name
, flags
);
58 static NORETURN
void die_initial_contact(int unexpected
)
61 * A hang-up after seeing some response from the other end
62 * means that it is unexpected, as we know the other end is
63 * willing to talk to us. A hang-up before seeing any
64 * response does not necessarily mean an ACL problem, though.
67 die(_("the remote end hung up upon initial contact"));
69 die(_("Could not read from remote repository.\n\n"
70 "Please make sure you have the correct access rights\n"
71 "and the repository exists."));
74 /* Checks if the server supports the capability 'c' */
75 int server_supports_v2(const char *c
)
79 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
81 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
82 (!*out
|| *out
== '='))
88 void ensure_server_supports_v2(const char *c
)
90 if (!server_supports_v2(c
))
91 die(_("server doesn't support '%s'"), c
);
94 int server_feature_v2(const char *c
, const char **v
)
98 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
100 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
109 int server_supports_feature(const char *c
, const char *feature
,
114 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
116 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
117 (!*out
|| *(out
++) == '=')) {
118 if (parse_feature_request(out
, feature
))
126 die(_("server doesn't support feature '%s'"), feature
);
131 static void process_capabilities_v2(struct packet_reader
*reader
)
133 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
)
134 strvec_push(&server_capabilities_v2
, reader
->line
);
136 if (reader
->status
!= PACKET_READ_FLUSH
)
137 die(_("expected flush after capabilities"));
140 enum protocol_version
discover_version(struct packet_reader
*reader
)
142 enum protocol_version version
= protocol_unknown_version
;
145 * Peek the first line of the server's response to
146 * determine the protocol version the server is speaking.
148 switch (packet_reader_peek(reader
)) {
149 case PACKET_READ_EOF
:
150 die_initial_contact(0);
151 case PACKET_READ_FLUSH
:
152 case PACKET_READ_DELIM
:
153 case PACKET_READ_RESPONSE_END
:
154 version
= protocol_v0
;
156 case PACKET_READ_NORMAL
:
157 version
= determine_protocol_version_client(reader
->line
);
163 process_capabilities_v2(reader
);
166 /* Read the peeked version line */
167 packet_reader_read(reader
);
171 case protocol_unknown_version
:
172 BUG("unknown protocol version");
175 trace2_data_intmax("transfer", NULL
, "negotiated-version", version
);
180 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
183 struct string_list_item
*item
;
186 return; /* just "symref" */
187 /* e.g. "symref=HEAD:refs/heads/master" */
188 sym
= xmemdupz(val
, len
);
189 target
= strchr(sym
, ':');
191 /* just "symref=something" */
194 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
195 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
196 /* "symref=bogus:pair */
198 item
= string_list_append_nodup(symref
, sym
);
206 static void annotate_refs_with_symref_info(struct ref
*ref
)
208 struct string_list symref
= STRING_LIST_INIT_DUP
;
215 val
= next_server_feature_value("symref", &len
, &offset
);
218 parse_one_symref_info(&symref
, val
, len
);
220 string_list_sort(&symref
);
222 for (; ref
; ref
= ref
->next
) {
223 struct string_list_item
*item
;
224 item
= string_list_lookup(&symref
, ref
->name
);
227 ref
->symref
= xstrdup((char *)item
->util
);
229 string_list_clear(&symref
, 0);
232 static void process_capabilities(struct packet_reader
*reader
, int *linelen
)
234 const char *feat_val
;
236 const char *line
= reader
->line
;
237 int nul_location
= strlen(line
);
238 if (nul_location
== *linelen
)
240 server_capabilities_v1
= xstrdup(line
+ nul_location
+ 1);
241 *linelen
= nul_location
;
243 feat_val
= server_feature_value("object-format", &feat_len
);
245 char *hash_name
= xstrndup(feat_val
, feat_len
);
246 int hash_algo
= hash_algo_by_name(hash_name
);
247 if (hash_algo
!= GIT_HASH_UNKNOWN
)
248 reader
->hash_algo
= &hash_algos
[hash_algo
];
251 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
255 static int process_dummy_ref(const struct packet_reader
*reader
)
257 const char *line
= reader
->line
;
258 struct object_id oid
;
261 if (parse_oid_hex_algop(line
, &oid
, &name
, reader
->hash_algo
))
267 return oideq(reader
->hash_algo
->null_oid
, &oid
) &&
268 !strcmp(name
, "capabilities^{}");
271 static void check_no_capabilities(const char *line
, int len
)
273 if (strlen(line
) != len
)
274 warning(_("ignoring capabilities after first line '%s'"),
275 line
+ strlen(line
));
278 static int process_ref(const struct packet_reader
*reader
, int len
,
279 struct ref
***list
, unsigned int flags
,
280 struct oid_array
*extra_have
)
282 const char *line
= reader
->line
;
283 struct object_id old_oid
;
286 if (parse_oid_hex_algop(line
, &old_oid
, &name
, reader
->hash_algo
))
292 if (extra_have
&& !strcmp(name
, ".have")) {
293 oid_array_append(extra_have
, &old_oid
);
294 } else if (!strcmp(name
, "capabilities^{}")) {
295 die(_("protocol error: unexpected capabilities^{}"));
296 } else if (check_ref(name
, flags
)) {
297 struct ref
*ref
= alloc_ref(name
);
298 oidcpy(&ref
->old_oid
, &old_oid
);
302 check_no_capabilities(line
, len
);
306 static int process_shallow(const struct packet_reader
*reader
, int len
,
307 struct oid_array
*shallow_points
)
309 const char *line
= reader
->line
;
311 struct object_id old_oid
;
313 if (!skip_prefix(line
, "shallow ", &arg
))
316 if (get_oid_hex_algop(arg
, &old_oid
, reader
->hash_algo
))
317 die(_("protocol error: expected shallow sha-1, got '%s'"), arg
);
319 die(_("repository on the other end cannot be shallow"));
320 oid_array_append(shallow_points
, &old_oid
);
321 check_no_capabilities(line
, len
);
325 enum get_remote_heads_state
{
326 EXPECTING_FIRST_REF
= 0,
333 * Read all the refs from the other end
335 struct ref
**get_remote_heads(struct packet_reader
*reader
,
336 struct ref
**list
, unsigned int flags
,
337 struct oid_array
*extra_have
,
338 struct oid_array
*shallow_points
)
340 struct ref
**orig_list
= list
;
342 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
346 while (state
!= EXPECTING_DONE
) {
347 switch (packet_reader_read(reader
)) {
348 case PACKET_READ_EOF
:
349 die_initial_contact(1);
350 case PACKET_READ_NORMAL
:
351 len
= reader
->pktlen
;
353 case PACKET_READ_FLUSH
:
354 state
= EXPECTING_DONE
;
356 case PACKET_READ_DELIM
:
357 case PACKET_READ_RESPONSE_END
:
358 die(_("invalid packet"));
362 case EXPECTING_FIRST_REF
:
363 process_capabilities(reader
, &len
);
364 if (process_dummy_ref(reader
)) {
365 state
= EXPECTING_SHALLOW
;
368 state
= EXPECTING_REF
;
371 if (process_ref(reader
, len
, &list
, flags
, extra_have
))
373 state
= EXPECTING_SHALLOW
;
375 case EXPECTING_SHALLOW
:
376 if (process_shallow(reader
, len
, shallow_points
))
378 die(_("protocol error: unexpected '%s'"), reader
->line
);
384 annotate_refs_with_symref_info(*orig_list
);
389 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
390 static int process_ref_v2(struct packet_reader
*reader
, struct ref
***list
,
391 const char **unborn_head_target
)
395 struct object_id old_oid
;
397 struct string_list line_sections
= STRING_LIST_INIT_DUP
;
399 const char *line
= reader
->line
;
402 * Ref lines have a number of fields which are space deliminated. The
403 * first field is the OID of the ref. The second field is the ref
404 * name. Subsequent fields (symref-target and peeled) are optional and
405 * don't have a particular order.
407 if (string_list_split(&line_sections
, line
, ' ', -1) < 2) {
412 if (!strcmp("unborn", line_sections
.items
[i
].string
)) {
414 if (unborn_head_target
&&
415 !strcmp("HEAD", line_sections
.items
[i
++].string
)) {
417 * Look for the symref target (if any). If found,
418 * return it to the caller.
420 for (; i
< line_sections
.nr
; i
++) {
421 const char *arg
= line_sections
.items
[i
].string
;
423 if (skip_prefix(arg
, "symref-target:", &arg
)) {
424 *unborn_head_target
= xstrdup(arg
);
431 if (parse_oid_hex_algop(line_sections
.items
[i
++].string
, &old_oid
, &end
, reader
->hash_algo
) ||
437 ref
= alloc_ref(line_sections
.items
[i
++].string
);
439 memcpy(ref
->old_oid
.hash
, old_oid
.hash
, reader
->hash_algo
->rawsz
);
443 for (; i
< line_sections
.nr
; i
++) {
444 const char *arg
= line_sections
.items
[i
].string
;
445 if (skip_prefix(arg
, "symref-target:", &arg
))
446 ref
->symref
= xstrdup(arg
);
448 if (skip_prefix(arg
, "peeled:", &arg
)) {
449 struct object_id peeled_oid
;
452 if (parse_oid_hex_algop(arg
, &peeled_oid
, &end
,
453 reader
->hash_algo
) || *end
) {
458 peeled_name
= xstrfmt("%s^{}", ref
->name
);
459 peeled
= alloc_ref(peeled_name
);
461 memcpy(peeled
->old_oid
.hash
, peeled_oid
.hash
,
462 reader
->hash_algo
->rawsz
);
464 *list
= &peeled
->next
;
471 string_list_clear(&line_sections
, 0);
475 void check_stateless_delimiter(int stateless_rpc
,
476 struct packet_reader
*reader
,
480 return; /* not in stateless mode, no delimiter expected */
481 if (packet_reader_read(reader
) != PACKET_READ_RESPONSE_END
)
485 static void send_capabilities(int fd_out
, struct packet_reader
*reader
)
487 const char *hash_name
;
489 if (server_supports_v2("agent"))
490 packet_write_fmt(fd_out
, "agent=%s", git_user_agent_sanitized());
492 if (server_feature_v2("object-format", &hash_name
)) {
493 int hash_algo
= hash_algo_by_name(hash_name
);
494 if (hash_algo
== GIT_HASH_UNKNOWN
)
495 die(_("unknown object format '%s' specified by server"), hash_name
);
496 reader
->hash_algo
= &hash_algos
[hash_algo
];
497 packet_write_fmt(fd_out
, "object-format=%s", reader
->hash_algo
->name
);
499 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
503 int get_remote_bundle_uri(int fd_out
, struct packet_reader
*reader
,
504 struct bundle_list
*bundles
, int stateless_rpc
)
508 /* Assert bundle-uri support */
509 ensure_server_supports_v2("bundle-uri");
511 /* (Re-)send capabilities */
512 send_capabilities(fd_out
, reader
);
515 packet_write_fmt(fd_out
, "command=bundle-uri\n");
516 packet_delim(fd_out
);
518 packet_flush(fd_out
);
520 /* Process response from server */
521 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
522 const char *line
= reader
->line
;
525 if (!bundle_uri_parse_line(bundles
, line
))
528 return error(_("error on bundle-uri response line %d: %s"),
532 if (reader
->status
!= PACKET_READ_FLUSH
)
533 return error(_("expected flush after bundle-uri listing"));
536 * Might die(), but obscure enough that that's OK, e.g. in
537 * serve.c we'll call BUG() on its equivalent (the
538 * PACKET_READ_RESPONSE_END check).
540 check_stateless_delimiter(stateless_rpc
, reader
,
541 _("expected response end packet after ref listing"));
546 struct ref
**get_remote_refs(int fd_out
, struct packet_reader
*reader
,
547 struct ref
**list
, int for_push
,
548 struct transport_ls_refs_options
*transport_options
,
549 const struct string_list
*server_options
,
553 struct strvec
*ref_prefixes
= transport_options
?
554 &transport_options
->ref_prefixes
: NULL
;
555 const char **unborn_head_target
= transport_options
?
556 &transport_options
->unborn_head_target
: NULL
;
559 ensure_server_supports_v2("ls-refs");
560 packet_write_fmt(fd_out
, "command=ls-refs\n");
562 /* Send capabilities */
563 send_capabilities(fd_out
, reader
);
565 if (server_options
&& server_options
->nr
) {
566 ensure_server_supports_v2("server-option");
567 for (i
= 0; i
< server_options
->nr
; i
++)
568 packet_write_fmt(fd_out
, "server-option=%s",
569 server_options
->items
[i
].string
);
572 packet_delim(fd_out
);
573 /* When pushing we don't want to request the peeled tags */
575 packet_write_fmt(fd_out
, "peel\n");
576 packet_write_fmt(fd_out
, "symrefs\n");
577 if (server_supports_feature("ls-refs", "unborn", 0))
578 packet_write_fmt(fd_out
, "unborn\n");
579 for (i
= 0; ref_prefixes
&& i
< ref_prefixes
->nr
; i
++) {
580 packet_write_fmt(fd_out
, "ref-prefix %s\n",
583 packet_flush(fd_out
);
585 /* Process response from server */
586 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
587 if (!process_ref_v2(reader
, &list
, unborn_head_target
))
588 die(_("invalid ls-refs response: %s"), reader
->line
);
591 if (reader
->status
!= PACKET_READ_FLUSH
)
592 die(_("expected flush after ref listing"));
594 check_stateless_delimiter(stateless_rpc
, reader
,
595 _("expected response end packet after ref listing"));
600 const char *parse_feature_value(const char *feature_list
, const char *feature
, size_t *lenp
, size_t *offset
)
602 const char *orig_start
= feature_list
;
608 len
= strlen(feature
);
610 feature_list
+= *offset
;
611 while (*feature_list
) {
612 const char *found
= strstr(feature_list
, feature
);
615 if (feature_list
== found
|| isspace(found
[-1])) {
616 const char *value
= found
+ len
;
617 /* feature with no value (e.g., "thin-pack") */
618 if (!*value
|| isspace(*value
)) {
622 *offset
= found
+ len
- orig_start
;
625 /* feature with a value (e.g., "agent=git/1.2.3") */
626 else if (*value
== '=') {
630 end
= strcspn(value
, " \t\n");
634 *offset
= value
+ end
- orig_start
;
638 * otherwise we matched a substring of another feature;
642 feature_list
= found
+ 1;
647 int server_supports_hash(const char *desired
, int *feature_supported
)
653 hash
= next_server_feature_value("object-format", &len
, &offset
);
654 if (feature_supported
)
655 *feature_supported
= !!hash
;
657 hash
= hash_algos
[GIT_HASH_SHA1
].name
;
661 if (!xstrncmpz(desired
, hash
, len
))
664 hash
= next_server_feature_value("object-format", &len
, &offset
);
669 int parse_feature_request(const char *feature_list
, const char *feature
)
671 return !!parse_feature_value(feature_list
, feature
, NULL
, NULL
);
674 static const char *next_server_feature_value(const char *feature
, size_t *len
, size_t *offset
)
676 return parse_feature_value(server_capabilities_v1
, feature
, len
, offset
);
679 const char *server_feature_value(const char *feature
, size_t *len
)
681 return parse_feature_value(server_capabilities_v1
, feature
, len
, NULL
);
684 int server_supports(const char *feature
)
686 return !!server_feature_value(feature
, NULL
);
696 int url_is_local_not_ssh(const char *url
)
698 const char *colon
= strchr(url
, ':');
699 const char *slash
= strchr(url
, '/');
700 return !colon
|| (slash
&& slash
< colon
) ||
701 (has_dos_drive_prefix(url
) && is_valid_path(url
));
704 static const char *prot_name(enum protocol protocol
)
715 return "unknown protocol";
719 static enum protocol
get_protocol(const char *name
)
721 if (!strcmp(name
, "ssh"))
723 if (!strcmp(name
, "git"))
725 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
727 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
729 if (!strcmp(name
, "file"))
731 die(_("protocol '%s' is not supported"), name
);
734 static char *host_end(char **hoststart
, int removebrackets
)
736 char *host
= *hoststart
;
738 char *start
= strstr(host
, "@[");
740 start
++; /* Jump over '@' */
743 if (start
[0] == '[') {
744 end
= strchr(start
+ 1, ']');
746 if (removebrackets
) {
748 memmove(start
, start
+ 1, end
- start
);
759 #define STR(s) STR_(s)
761 static void get_host_and_port(char **host
, const char **port
)
764 end
= host_end(host
, 1);
765 colon
= strchr(end
, ':');
767 long portnr
= strtol(colon
+ 1, &end
, 10);
768 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
771 } else if (!colon
[1]) {
777 static void enable_keepalive(int sockfd
)
781 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
782 error_errno(_("unable to set SO_KEEPALIVE on socket"));
787 static const char *ai_name(const struct addrinfo
*ai
)
789 static char addr
[NI_MAXHOST
];
790 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
791 NI_NUMERICHOST
) != 0)
792 xsnprintf(addr
, sizeof(addr
), "(unknown)");
798 * Returns a connected socket() fd, or else die()s.
800 static int git_tcp_connect_sock(char *host
, int flags
)
802 struct strbuf error_message
= STRBUF_INIT
;
804 const char *port
= STR(DEFAULT_GIT_PORT
);
805 struct addrinfo hints
, *ai0
, *ai
;
809 get_host_and_port(&host
, &port
);
813 memset(&hints
, 0, sizeof(hints
));
814 if (flags
& CONNECT_IPV4
)
815 hints
.ai_family
= AF_INET
;
816 else if (flags
& CONNECT_IPV6
)
817 hints
.ai_family
= AF_INET6
;
818 hints
.ai_socktype
= SOCK_STREAM
;
819 hints
.ai_protocol
= IPPROTO_TCP
;
821 if (flags
& CONNECT_VERBOSE
)
822 fprintf(stderr
, _("Looking up %s ... "), host
);
824 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
826 die(_("unable to look up %s (port %s) (%s)"), host
, port
, gai_strerror(gai
));
828 if (flags
& CONNECT_VERBOSE
)
829 /* TRANSLATORS: this is the end of "Looking up %s ... " */
830 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
832 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
833 sockfd
= socket(ai
->ai_family
,
834 ai
->ai_socktype
, ai
->ai_protocol
);
836 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
837 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
838 host
, cnt
, ai_name(ai
), strerror(errno
));
844 if (flags
& CONNECT_VERBOSE
)
845 fprintf(stderr
, "%s ", ai_name(ai
));
852 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
854 enable_keepalive(sockfd
);
856 if (flags
& CONNECT_VERBOSE
)
857 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
858 fprintf_ln(stderr
, _("done."));
860 strbuf_release(&error_message
);
868 * Returns a connected socket() fd, or else die()s.
870 static int git_tcp_connect_sock(char *host
, int flags
)
872 struct strbuf error_message
= STRBUF_INIT
;
874 const char *port
= STR(DEFAULT_GIT_PORT
);
877 struct sockaddr_in sa
;
882 get_host_and_port(&host
, &port
);
884 if (flags
& CONNECT_VERBOSE
)
885 fprintf(stderr
, _("Looking up %s ... "), host
);
887 he
= gethostbyname(host
);
889 die(_("unable to look up %s (%s)"), host
, hstrerror(h_errno
));
890 nport
= strtoul(port
, &ep
, 10);
891 if ( ep
== port
|| *ep
) {
893 struct servent
*se
= getservbyname(port
,"tcp");
895 die(_("unknown port %s"), port
);
899 if (flags
& CONNECT_VERBOSE
)
900 /* TRANSLATORS: this is the end of "Looking up %s ... " */
901 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
903 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
904 memset(&sa
, 0, sizeof sa
);
905 sa
.sin_family
= he
->h_addrtype
;
906 sa
.sin_port
= htons(nport
);
907 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
909 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
911 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
912 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
915 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
922 if (flags
& CONNECT_VERBOSE
)
923 fprintf(stderr
, "%s ",
924 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
929 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
931 enable_keepalive(sockfd
);
933 if (flags
& CONNECT_VERBOSE
)
934 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
935 fprintf_ln(stderr
, _("done."));
944 * Dummy child_process returned by git_connect() if the transport protocol
945 * does not need fork(2).
947 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
949 int git_connection_is_socket(struct child_process
*conn
)
951 return conn
== &no_fork
;
954 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
956 int sockfd
= git_tcp_connect_sock(host
, flags
);
965 static char *git_proxy_command
;
967 static int git_proxy_command_options(const char *var
, const char *value
,
968 const struct config_context
*ctx
, void *cb
)
970 if (!strcmp(var
, "core.gitproxy")) {
974 const char *rhost_name
= cb
;
975 int rhost_len
= strlen(rhost_name
);
977 if (git_proxy_command
)
980 return config_error_nonbool(var
);
982 * ;# matches www.kernel.org as well
983 * gitproxy = netcatter-1 for kernel.org
984 * gitproxy = netcatter-2 for sample.xz
985 * gitproxy = netcatter-default
987 for_pos
= strstr(value
, " for ");
989 /* matches everybody */
990 matchlen
= strlen(value
);
992 hostlen
= strlen(for_pos
+ 5);
993 if (rhost_len
< hostlen
)
995 else if (!strncmp(for_pos
+ 5,
996 rhost_name
+ rhost_len
- hostlen
,
998 ((rhost_len
== hostlen
) ||
999 rhost_name
[rhost_len
- hostlen
-1] == '.'))
1000 matchlen
= for_pos
- value
;
1004 if (0 <= matchlen
) {
1005 /* core.gitproxy = none for kernel.org */
1006 if (matchlen
== 4 &&
1007 !memcmp(value
, "none", 4))
1009 git_proxy_command
= xmemdupz(value
, matchlen
);
1014 return git_default_config(var
, value
, ctx
, cb
);
1017 static int git_use_proxy(const char *host
)
1019 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
1020 git_config(git_proxy_command_options
, (void*)host
);
1021 return (git_proxy_command
&& *git_proxy_command
);
1024 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
1026 const char *port
= STR(DEFAULT_GIT_PORT
);
1027 struct child_process
*proxy
;
1029 get_host_and_port(&host
, &port
);
1031 if (looks_like_command_line_option(host
))
1032 die(_("strange hostname '%s' blocked"), host
);
1033 if (looks_like_command_line_option(port
))
1034 die(_("strange port '%s' blocked"), port
);
1036 proxy
= xmalloc(sizeof(*proxy
));
1037 child_process_init(proxy
);
1038 strvec_push(&proxy
->args
, git_proxy_command
);
1039 strvec_push(&proxy
->args
, host
);
1040 strvec_push(&proxy
->args
, port
);
1043 if (start_command(proxy
))
1044 die(_("cannot start proxy %s"), git_proxy_command
);
1045 fd
[0] = proxy
->out
; /* read from proxy stdout */
1046 fd
[1] = proxy
->in
; /* write to proxy stdin */
1050 static char *get_port(char *host
)
1053 char *p
= strchr(host
, ':');
1056 long port
= strtol(p
+ 1, &end
, 10);
1057 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
1067 * Extract protocol and relevant parts from the specified connection URL.
1068 * The caller must free() the returned strings.
1070 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
1076 int separator
= '/';
1077 enum protocol protocol
= PROTO_LOCAL
;
1079 if (is_url(url_orig
))
1080 url
= url_decode(url_orig
);
1082 url
= xstrdup(url_orig
);
1084 host
= strstr(url
, "://");
1087 protocol
= get_protocol(url
);
1091 if (!url_is_local_not_ssh(url
)) {
1092 protocol
= PROTO_SSH
;
1098 * Don't do destructive transforms as protocol code does
1099 * '[]' unwrapping in get_host_and_port()
1101 end
= host_end(&host
, 0);
1103 if (protocol
== PROTO_LOCAL
)
1105 else if (protocol
== PROTO_FILE
&& *host
!= '/' &&
1106 !has_dos_drive_prefix(host
) &&
1107 offset_1st_component(host
- 2) > 1)
1108 path
= host
- 2; /* include the leading "//" */
1109 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
1110 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1112 path
= strchr(end
, separator
);
1114 if (!path
|| !*path
)
1115 die(_("no path specified; see 'git help pull' for valid url syntax"));
1118 * null-terminate hostname and point path to ~ for URL's like this:
1119 * ssh://host.xz/~user/repo
1122 end
= path
; /* Need to \0 terminate host here */
1123 if (separator
== ':')
1124 path
++; /* path starts after ':' */
1125 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
1130 path
= xstrdup(path
);
1133 *ret_host
= xstrdup(host
);
1139 static const char *get_ssh_command(void)
1143 if ((ssh
= getenv("GIT_SSH_COMMAND")))
1146 if (!git_config_get_string_tmp("core.sshcommand", &ssh
))
1158 VARIANT_TORTOISEPLINK
,
1161 static void override_ssh_variant(enum ssh_variant
*ssh_variant
)
1163 const char *variant
= getenv("GIT_SSH_VARIANT");
1165 if (!variant
&& git_config_get_string_tmp("ssh.variant", &variant
))
1168 if (!strcmp(variant
, "auto"))
1169 *ssh_variant
= VARIANT_AUTO
;
1170 else if (!strcmp(variant
, "plink"))
1171 *ssh_variant
= VARIANT_PLINK
;
1172 else if (!strcmp(variant
, "putty"))
1173 *ssh_variant
= VARIANT_PUTTY
;
1174 else if (!strcmp(variant
, "tortoiseplink"))
1175 *ssh_variant
= VARIANT_TORTOISEPLINK
;
1176 else if (!strcmp(variant
, "simple"))
1177 *ssh_variant
= VARIANT_SIMPLE
;
1179 *ssh_variant
= VARIANT_SSH
;
1182 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
1185 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
1186 const char *variant
;
1189 override_ssh_variant(&ssh_variant
);
1191 if (ssh_variant
!= VARIANT_AUTO
)
1195 p
= xstrdup(ssh_command
);
1196 variant
= basename(p
);
1198 const char **ssh_argv
;
1200 p
= xstrdup(ssh_command
);
1201 if (split_cmdline(p
, &ssh_argv
) > 0) {
1202 variant
= basename((char *)ssh_argv
[0]);
1204 * At this point, variant points into the buffer
1205 * referenced by p, hence we do not need ssh_argv
1215 if (!strcasecmp(variant
, "ssh") ||
1216 !strcasecmp(variant
, "ssh.exe"))
1217 ssh_variant
= VARIANT_SSH
;
1218 else if (!strcasecmp(variant
, "plink") ||
1219 !strcasecmp(variant
, "plink.exe"))
1220 ssh_variant
= VARIANT_PLINK
;
1221 else if (!strcasecmp(variant
, "tortoiseplink") ||
1222 !strcasecmp(variant
, "tortoiseplink.exe"))
1223 ssh_variant
= VARIANT_TORTOISEPLINK
;
1230 * Open a connection using Git's native protocol.
1232 * The caller is responsible for freeing hostandport, but this function may
1233 * modify it (for example, to truncate it to remove the port part).
1235 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
1236 const char *path
, const char *prog
,
1237 enum protocol_version version
,
1240 struct child_process
*conn
;
1241 struct strbuf request
= STRBUF_INIT
;
1243 * Set up virtual host information based on where we will
1244 * connect, unless the user has overridden us in
1247 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1249 target_host
= xstrdup(target_host
);
1251 target_host
= xstrdup(hostandport
);
1253 transport_check_allowed("git");
1254 if (strchr(target_host
, '\n') || strchr(path
, '\n'))
1255 die(_("newline is forbidden in git:// hosts and repo paths"));
1258 * These underlying connection commands die() if they
1261 if (git_use_proxy(hostandport
))
1262 conn
= git_proxy_connect(fd
, hostandport
);
1264 conn
= git_tcp_connect(fd
, hostandport
, flags
);
1266 * Separate original protocol components prog and path
1267 * from extended host header with a NUL byte.
1269 * Note: Do not add any other headers here! Doing so
1270 * will cause older git-daemon servers to crash.
1272 strbuf_addf(&request
,
1277 /* If using a new version put that stuff here after a second null byte */
1279 strbuf_addch(&request
, '\0');
1280 strbuf_addf(&request
, "version=%d%c",
1284 packet_write(fd
[1], request
.buf
, request
.len
);
1287 strbuf_release(&request
);
1292 * Append the appropriate environment variables to `env` and options to
1293 * `args` for running ssh in Git's SSH-tunneled transport.
1295 static void push_ssh_options(struct strvec
*args
, struct strvec
*env
,
1296 enum ssh_variant variant
, const char *port
,
1297 enum protocol_version version
, int flags
)
1299 if (variant
== VARIANT_SSH
&&
1301 strvec_push(args
, "-o");
1302 strvec_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
1303 strvec_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1307 if (flags
& CONNECT_IPV4
) {
1310 BUG("VARIANT_AUTO passed to push_ssh_options");
1311 case VARIANT_SIMPLE
:
1312 die(_("ssh variant 'simple' does not support -4"));
1316 case VARIANT_TORTOISEPLINK
:
1317 strvec_push(args
, "-4");
1319 } else if (flags
& CONNECT_IPV6
) {
1322 BUG("VARIANT_AUTO passed to push_ssh_options");
1323 case VARIANT_SIMPLE
:
1324 die(_("ssh variant 'simple' does not support -6"));
1328 case VARIANT_TORTOISEPLINK
:
1329 strvec_push(args
, "-6");
1333 if (variant
== VARIANT_TORTOISEPLINK
)
1334 strvec_push(args
, "-batch");
1339 BUG("VARIANT_AUTO passed to push_ssh_options");
1340 case VARIANT_SIMPLE
:
1341 die(_("ssh variant 'simple' does not support setting port"));
1343 strvec_push(args
, "-p");
1347 case VARIANT_TORTOISEPLINK
:
1348 strvec_push(args
, "-P");
1351 strvec_push(args
, port
);
1355 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1356 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
1357 const char *port
, enum protocol_version version
,
1361 enum ssh_variant variant
;
1363 if (looks_like_command_line_option(ssh_host
))
1364 die(_("strange hostname '%s' blocked"), ssh_host
);
1366 ssh
= get_ssh_command();
1368 variant
= determine_ssh_variant(ssh
, 1);
1371 * GIT_SSH is the no-shell version of
1372 * GIT_SSH_COMMAND (and must remain so for
1373 * historical compatibility).
1375 conn
->use_shell
= 0;
1377 ssh
= getenv("GIT_SSH");
1380 variant
= determine_ssh_variant(ssh
, 0);
1383 if (variant
== VARIANT_AUTO
) {
1384 struct child_process detect
= CHILD_PROCESS_INIT
;
1386 detect
.use_shell
= conn
->use_shell
;
1387 detect
.no_stdin
= detect
.no_stdout
= detect
.no_stderr
= 1;
1389 strvec_push(&detect
.args
, ssh
);
1390 strvec_push(&detect
.args
, "-G");
1391 push_ssh_options(&detect
.args
, &detect
.env
,
1392 VARIANT_SSH
, port
, version
, flags
);
1393 strvec_push(&detect
.args
, ssh_host
);
1395 variant
= run_command(&detect
) ? VARIANT_SIMPLE
: VARIANT_SSH
;
1398 strvec_push(&conn
->args
, ssh
);
1399 push_ssh_options(&conn
->args
, &conn
->env
, variant
, port
, version
,
1401 strvec_push(&conn
->args
, ssh_host
);
1405 * This returns the dummy child_process `no_fork` if the transport protocol
1406 * does not need fork(2), or a struct child_process object if it does. Once
1407 * done, finish the connection with finish_connect() with the value returned
1408 * from this function (it is safe to call finish_connect() with NULL to
1409 * support the former case).
1411 * If it returns, the connect is successful; it just dies on errors (this
1412 * will hopefully be changed in a libification effort, to return NULL when
1413 * the connection failed).
1415 struct child_process
*git_connect(int fd
[2], const char *url
,
1417 const char *prog
, int flags
)
1419 char *hostandport
, *path
;
1420 struct child_process
*conn
;
1421 enum protocol protocol
;
1422 enum protocol_version version
= get_protocol_version_config();
1425 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1426 * to perform any operation that doesn't involve upload-pack (i.e., a
1427 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
1428 * to do anything else (like push or remote archive) via v2.
1430 if (version
== protocol_v2
&& strcmp("git-upload-pack", name
))
1431 version
= protocol_v0
;
1433 /* Without this we cannot rely on waitpid() to tell
1434 * what happened to our children.
1436 signal(SIGCHLD
, SIG_DFL
);
1438 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1439 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1440 printf("Diag: url=%s\n", url
? url
: "NULL");
1441 printf("Diag: protocol=%s\n", prot_name(protocol
));
1442 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1443 printf("Diag: path=%s\n", path
? path
: "NULL");
1445 } else if (protocol
== PROTO_GIT
) {
1446 conn
= git_connect_git(fd
, hostandport
, path
, prog
, version
, flags
);
1447 conn
->trace2_child_class
= "transport/git";
1449 struct strbuf cmd
= STRBUF_INIT
;
1450 const char *const *var
;
1452 conn
= xmalloc(sizeof(*conn
));
1453 child_process_init(conn
);
1455 if (looks_like_command_line_option(path
))
1456 die(_("strange pathname '%s' blocked"), path
);
1458 strbuf_addstr(&cmd
, prog
);
1459 strbuf_addch(&cmd
, ' ');
1460 sq_quote_buf(&cmd
, path
);
1462 /* remove repo-local variables from the environment */
1463 for (var
= local_repo_env
; *var
; var
++)
1464 strvec_push(&conn
->env
, *var
);
1466 conn
->use_shell
= 1;
1467 conn
->in
= conn
->out
= -1;
1468 if (protocol
== PROTO_SSH
) {
1469 char *ssh_host
= hostandport
;
1470 const char *port
= NULL
;
1471 transport_check_allowed("ssh");
1472 get_host_and_port(&ssh_host
, &port
);
1475 port
= get_port(ssh_host
);
1477 if (flags
& CONNECT_DIAG_URL
) {
1478 printf("Diag: url=%s\n", url
? url
: "NULL");
1479 printf("Diag: protocol=%s\n", prot_name(protocol
));
1480 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1481 printf("Diag: port=%s\n", port
? port
: "NONE");
1482 printf("Diag: path=%s\n", path
? path
: "NULL");
1487 strbuf_release(&cmd
);
1490 conn
->trace2_child_class
= "transport/ssh";
1491 fill_ssh_args(conn
, ssh_host
, port
, version
, flags
);
1493 transport_check_allowed("file");
1494 conn
->trace2_child_class
= "transport/file";
1496 strvec_pushf(&conn
->env
,
1497 GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1501 strvec_push(&conn
->args
, cmd
.buf
);
1503 if (start_command(conn
))
1504 die(_("unable to fork"));
1506 fd
[0] = conn
->out
; /* read from child's stdout */
1507 fd
[1] = conn
->in
; /* write to child's stdin */
1508 strbuf_release(&cmd
);
1515 int finish_connect(struct child_process
*conn
)
1518 if (!conn
|| git_connection_is_socket(conn
))
1521 code
= finish_command(conn
);