1 #include "git-compat-util.h"
9 #include "run-command.h"
13 #include "string-list.h"
14 #include "oid-array.h"
15 #include "transport.h"
20 #include "bundle-uri.h"
22 static char *server_capabilities_v1
;
23 static struct strvec server_capabilities_v2
= STRVEC_INIT
;
24 static const char *next_server_feature_value(const char *feature
, int *len
, int *offset
);
26 static int check_ref(const char *name
, unsigned int flags
)
31 if (!skip_prefix(name
, "refs/", &name
))
34 /* REF_NORMAL means that we don't want the magic fake tag refs */
35 if ((flags
& REF_NORMAL
) && check_refname_format(name
,
36 REFNAME_ALLOW_ONELEVEL
))
39 /* REF_HEADS means that we want regular branch heads */
40 if ((flags
& REF_HEADS
) && starts_with(name
, "heads/"))
43 /* REF_TAGS means that we want tags */
44 if ((flags
& REF_TAGS
) && starts_with(name
, "tags/"))
47 /* All type bits clear means that we are ok with anything */
48 return !(flags
& ~REF_NORMAL
);
51 int check_ref_type(const struct ref
*ref
, int flags
)
53 return check_ref(ref
->name
, flags
);
56 static NORETURN
void die_initial_contact(int unexpected
)
59 * A hang-up after seeing some response from the other end
60 * means that it is unexpected, as we know the other end is
61 * willing to talk to us. A hang-up before seeing any
62 * response does not necessarily mean an ACL problem, though.
65 die(_("the remote end hung up upon initial contact"));
67 die(_("Could not read from remote repository.\n\n"
68 "Please make sure you have the correct access rights\n"
69 "and the repository exists."));
72 /* Checks if the server supports the capability 'c' */
73 int server_supports_v2(const char *c
)
77 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
79 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
80 (!*out
|| *out
== '='))
86 void ensure_server_supports_v2(const char *c
)
88 if (!server_supports_v2(c
))
89 die(_("server doesn't support '%s'"), c
);
92 int server_feature_v2(const char *c
, const char **v
)
96 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
98 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
107 int server_supports_feature(const char *c
, const char *feature
,
112 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
114 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
115 (!*out
|| *(out
++) == '=')) {
116 if (parse_feature_request(out
, feature
))
124 die(_("server doesn't support feature '%s'"), feature
);
129 static void process_capabilities_v2(struct packet_reader
*reader
)
131 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
)
132 strvec_push(&server_capabilities_v2
, reader
->line
);
134 if (reader
->status
!= PACKET_READ_FLUSH
)
135 die(_("expected flush after capabilities"));
138 enum protocol_version
discover_version(struct packet_reader
*reader
)
140 enum protocol_version version
= protocol_unknown_version
;
143 * Peek the first line of the server's response to
144 * determine the protocol version the server is speaking.
146 switch (packet_reader_peek(reader
)) {
147 case PACKET_READ_EOF
:
148 die_initial_contact(0);
149 case PACKET_READ_FLUSH
:
150 case PACKET_READ_DELIM
:
151 case PACKET_READ_RESPONSE_END
:
152 version
= protocol_v0
;
154 case PACKET_READ_NORMAL
:
155 version
= determine_protocol_version_client(reader
->line
);
161 process_capabilities_v2(reader
);
164 /* Read the peeked version line */
165 packet_reader_read(reader
);
169 case protocol_unknown_version
:
170 BUG("unknown protocol version");
173 trace2_data_intmax("transfer", NULL
, "negotiated-version", version
);
178 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
181 struct string_list_item
*item
;
184 return; /* just "symref" */
185 /* e.g. "symref=HEAD:refs/heads/master" */
186 sym
= xmemdupz(val
, len
);
187 target
= strchr(sym
, ':');
189 /* just "symref=something" */
192 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
193 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
194 /* "symref=bogus:pair */
196 item
= string_list_append_nodup(symref
, sym
);
204 static void annotate_refs_with_symref_info(struct ref
*ref
)
206 struct string_list symref
= STRING_LIST_INIT_DUP
;
213 val
= next_server_feature_value("symref", &len
, &offset
);
216 parse_one_symref_info(&symref
, val
, len
);
218 string_list_sort(&symref
);
220 for (; ref
; ref
= ref
->next
) {
221 struct string_list_item
*item
;
222 item
= string_list_lookup(&symref
, ref
->name
);
225 ref
->symref
= xstrdup((char *)item
->util
);
227 string_list_clear(&symref
, 0);
230 static void process_capabilities(struct packet_reader
*reader
, int *linelen
)
232 const char *feat_val
;
234 const char *line
= reader
->line
;
235 int nul_location
= strlen(line
);
236 if (nul_location
== *linelen
)
238 server_capabilities_v1
= xstrdup(line
+ nul_location
+ 1);
239 *linelen
= nul_location
;
241 feat_val
= server_feature_value("object-format", &feat_len
);
243 char *hash_name
= xstrndup(feat_val
, feat_len
);
244 int hash_algo
= hash_algo_by_name(hash_name
);
245 if (hash_algo
!= GIT_HASH_UNKNOWN
)
246 reader
->hash_algo
= &hash_algos
[hash_algo
];
249 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
253 static int process_dummy_ref(const struct packet_reader
*reader
)
255 const char *line
= reader
->line
;
256 struct object_id oid
;
259 if (parse_oid_hex_algop(line
, &oid
, &name
, reader
->hash_algo
))
265 return oideq(null_oid(), &oid
) && !strcmp(name
, "capabilities^{}");
268 static void check_no_capabilities(const char *line
, int len
)
270 if (strlen(line
) != len
)
271 warning(_("ignoring capabilities after first line '%s'"),
272 line
+ strlen(line
));
275 static int process_ref(const struct packet_reader
*reader
, int len
,
276 struct ref
***list
, unsigned int flags
,
277 struct oid_array
*extra_have
)
279 const char *line
= reader
->line
;
280 struct object_id old_oid
;
283 if (parse_oid_hex_algop(line
, &old_oid
, &name
, reader
->hash_algo
))
289 if (extra_have
&& !strcmp(name
, ".have")) {
290 oid_array_append(extra_have
, &old_oid
);
291 } else if (!strcmp(name
, "capabilities^{}")) {
292 die(_("protocol error: unexpected capabilities^{}"));
293 } else if (check_ref(name
, flags
)) {
294 struct ref
*ref
= alloc_ref(name
);
295 oidcpy(&ref
->old_oid
, &old_oid
);
299 check_no_capabilities(line
, len
);
303 static int process_shallow(const struct packet_reader
*reader
, int len
,
304 struct oid_array
*shallow_points
)
306 const char *line
= reader
->line
;
308 struct object_id old_oid
;
310 if (!skip_prefix(line
, "shallow ", &arg
))
313 if (get_oid_hex_algop(arg
, &old_oid
, reader
->hash_algo
))
314 die(_("protocol error: expected shallow sha-1, got '%s'"), arg
);
316 die(_("repository on the other end cannot be shallow"));
317 oid_array_append(shallow_points
, &old_oid
);
318 check_no_capabilities(line
, len
);
322 enum get_remote_heads_state
{
323 EXPECTING_FIRST_REF
= 0,
330 * Read all the refs from the other end
332 struct ref
**get_remote_heads(struct packet_reader
*reader
,
333 struct ref
**list
, unsigned int flags
,
334 struct oid_array
*extra_have
,
335 struct oid_array
*shallow_points
)
337 struct ref
**orig_list
= list
;
339 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
343 while (state
!= EXPECTING_DONE
) {
344 switch (packet_reader_read(reader
)) {
345 case PACKET_READ_EOF
:
346 die_initial_contact(1);
347 case PACKET_READ_NORMAL
:
348 len
= reader
->pktlen
;
350 case PACKET_READ_FLUSH
:
351 state
= EXPECTING_DONE
;
353 case PACKET_READ_DELIM
:
354 case PACKET_READ_RESPONSE_END
:
355 die(_("invalid packet"));
359 case EXPECTING_FIRST_REF
:
360 process_capabilities(reader
, &len
);
361 if (process_dummy_ref(reader
)) {
362 state
= EXPECTING_SHALLOW
;
365 state
= EXPECTING_REF
;
368 if (process_ref(reader
, len
, &list
, flags
, extra_have
))
370 state
= EXPECTING_SHALLOW
;
372 case EXPECTING_SHALLOW
:
373 if (process_shallow(reader
, len
, shallow_points
))
375 die(_("protocol error: unexpected '%s'"), reader
->line
);
381 annotate_refs_with_symref_info(*orig_list
);
386 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
387 static int process_ref_v2(struct packet_reader
*reader
, struct ref
***list
,
388 const char **unborn_head_target
)
392 struct object_id old_oid
;
394 struct string_list line_sections
= STRING_LIST_INIT_DUP
;
396 const char *line
= reader
->line
;
399 * Ref lines have a number of fields which are space deliminated. The
400 * first field is the OID of the ref. The second field is the ref
401 * name. Subsequent fields (symref-target and peeled) are optional and
402 * don't have a particular order.
404 if (string_list_split(&line_sections
, line
, ' ', -1) < 2) {
409 if (!strcmp("unborn", line_sections
.items
[i
].string
)) {
411 if (unborn_head_target
&&
412 !strcmp("HEAD", line_sections
.items
[i
++].string
)) {
414 * Look for the symref target (if any). If found,
415 * return it to the caller.
417 for (; i
< line_sections
.nr
; i
++) {
418 const char *arg
= line_sections
.items
[i
].string
;
420 if (skip_prefix(arg
, "symref-target:", &arg
)) {
421 *unborn_head_target
= xstrdup(arg
);
428 if (parse_oid_hex_algop(line_sections
.items
[i
++].string
, &old_oid
, &end
, reader
->hash_algo
) ||
434 ref
= alloc_ref(line_sections
.items
[i
++].string
);
436 memcpy(ref
->old_oid
.hash
, old_oid
.hash
, reader
->hash_algo
->rawsz
);
440 for (; i
< line_sections
.nr
; i
++) {
441 const char *arg
= line_sections
.items
[i
].string
;
442 if (skip_prefix(arg
, "symref-target:", &arg
))
443 ref
->symref
= xstrdup(arg
);
445 if (skip_prefix(arg
, "peeled:", &arg
)) {
446 struct object_id peeled_oid
;
449 if (parse_oid_hex_algop(arg
, &peeled_oid
, &end
,
450 reader
->hash_algo
) || *end
) {
455 peeled_name
= xstrfmt("%s^{}", ref
->name
);
456 peeled
= alloc_ref(peeled_name
);
458 memcpy(peeled
->old_oid
.hash
, peeled_oid
.hash
,
459 reader
->hash_algo
->rawsz
);
461 *list
= &peeled
->next
;
468 string_list_clear(&line_sections
, 0);
472 void check_stateless_delimiter(int stateless_rpc
,
473 struct packet_reader
*reader
,
477 return; /* not in stateless mode, no delimiter expected */
478 if (packet_reader_read(reader
) != PACKET_READ_RESPONSE_END
)
482 static void send_capabilities(int fd_out
, struct packet_reader
*reader
)
484 const char *hash_name
;
486 if (server_supports_v2("agent"))
487 packet_write_fmt(fd_out
, "agent=%s", git_user_agent_sanitized());
489 if (server_feature_v2("object-format", &hash_name
)) {
490 int hash_algo
= hash_algo_by_name(hash_name
);
491 if (hash_algo
== GIT_HASH_UNKNOWN
)
492 die(_("unknown object format '%s' specified by server"), hash_name
);
493 reader
->hash_algo
= &hash_algos
[hash_algo
];
494 packet_write_fmt(fd_out
, "object-format=%s", reader
->hash_algo
->name
);
496 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
500 int get_remote_bundle_uri(int fd_out
, struct packet_reader
*reader
,
501 struct bundle_list
*bundles
, int stateless_rpc
)
505 /* Assert bundle-uri support */
506 ensure_server_supports_v2("bundle-uri");
508 /* (Re-)send capabilities */
509 send_capabilities(fd_out
, reader
);
512 packet_write_fmt(fd_out
, "command=bundle-uri\n");
513 packet_delim(fd_out
);
515 packet_flush(fd_out
);
517 /* Process response from server */
518 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
519 const char *line
= reader
->line
;
522 if (!bundle_uri_parse_line(bundles
, line
))
525 return error(_("error on bundle-uri response line %d: %s"),
529 if (reader
->status
!= PACKET_READ_FLUSH
)
530 return error(_("expected flush after bundle-uri listing"));
533 * Might die(), but obscure enough that that's OK, e.g. in
534 * serve.c we'll call BUG() on its equivalent (the
535 * PACKET_READ_RESPONSE_END check).
537 check_stateless_delimiter(stateless_rpc
, reader
,
538 _("expected response end packet after ref listing"));
543 struct ref
**get_remote_refs(int fd_out
, struct packet_reader
*reader
,
544 struct ref
**list
, int for_push
,
545 struct transport_ls_refs_options
*transport_options
,
546 const struct string_list
*server_options
,
550 struct strvec
*ref_prefixes
= transport_options
?
551 &transport_options
->ref_prefixes
: NULL
;
552 const char **unborn_head_target
= transport_options
?
553 &transport_options
->unborn_head_target
: NULL
;
556 ensure_server_supports_v2("ls-refs");
557 packet_write_fmt(fd_out
, "command=ls-refs\n");
559 /* Send capabilities */
560 send_capabilities(fd_out
, reader
);
562 if (server_options
&& server_options
->nr
) {
563 ensure_server_supports_v2("server-option");
564 for (i
= 0; i
< server_options
->nr
; i
++)
565 packet_write_fmt(fd_out
, "server-option=%s",
566 server_options
->items
[i
].string
);
569 packet_delim(fd_out
);
570 /* When pushing we don't want to request the peeled tags */
572 packet_write_fmt(fd_out
, "peel\n");
573 packet_write_fmt(fd_out
, "symrefs\n");
574 if (server_supports_feature("ls-refs", "unborn", 0))
575 packet_write_fmt(fd_out
, "unborn\n");
576 for (i
= 0; ref_prefixes
&& i
< ref_prefixes
->nr
; i
++) {
577 packet_write_fmt(fd_out
, "ref-prefix %s\n",
580 packet_flush(fd_out
);
582 /* Process response from server */
583 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
584 if (!process_ref_v2(reader
, &list
, unborn_head_target
))
585 die(_("invalid ls-refs response: %s"), reader
->line
);
588 if (reader
->status
!= PACKET_READ_FLUSH
)
589 die(_("expected flush after ref listing"));
591 check_stateless_delimiter(stateless_rpc
, reader
,
592 _("expected response end packet after ref listing"));
597 const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
, int *offset
)
604 len
= strlen(feature
);
606 feature_list
+= *offset
;
607 while (*feature_list
) {
608 const char *found
= strstr(feature_list
, feature
);
611 if (feature_list
== found
|| isspace(found
[-1])) {
612 const char *value
= found
+ len
;
613 /* feature with no value (e.g., "thin-pack") */
614 if (!*value
|| isspace(*value
)) {
618 *offset
= found
+ len
- feature_list
;
621 /* feature with a value (e.g., "agent=git/1.2.3") */
622 else if (*value
== '=') {
626 end
= strcspn(value
, " \t\n");
630 *offset
= value
+ end
- feature_list
;
634 * otherwise we matched a substring of another feature;
638 feature_list
= found
+ 1;
643 int server_supports_hash(const char *desired
, int *feature_supported
)
649 hash
= next_server_feature_value("object-format", &len
, &offset
);
650 if (feature_supported
)
651 *feature_supported
= !!hash
;
653 hash
= hash_algos
[GIT_HASH_SHA1
].name
;
657 if (!xstrncmpz(desired
, hash
, len
))
660 hash
= next_server_feature_value("object-format", &len
, &offset
);
665 int parse_feature_request(const char *feature_list
, const char *feature
)
667 return !!parse_feature_value(feature_list
, feature
, NULL
, NULL
);
670 static const char *next_server_feature_value(const char *feature
, int *len
, int *offset
)
672 return parse_feature_value(server_capabilities_v1
, feature
, len
, offset
);
675 const char *server_feature_value(const char *feature
, int *len
)
677 return parse_feature_value(server_capabilities_v1
, feature
, len
, NULL
);
680 int server_supports(const char *feature
)
682 return !!server_feature_value(feature
, NULL
);
692 int url_is_local_not_ssh(const char *url
)
694 const char *colon
= strchr(url
, ':');
695 const char *slash
= strchr(url
, '/');
696 return !colon
|| (slash
&& slash
< colon
) ||
697 (has_dos_drive_prefix(url
) && is_valid_path(url
));
700 static const char *prot_name(enum protocol protocol
)
711 return "unknown protocol";
715 static enum protocol
get_protocol(const char *name
)
717 if (!strcmp(name
, "ssh"))
719 if (!strcmp(name
, "git"))
721 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
723 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
725 if (!strcmp(name
, "file"))
727 die(_("protocol '%s' is not supported"), name
);
730 static char *host_end(char **hoststart
, int removebrackets
)
732 char *host
= *hoststart
;
734 char *start
= strstr(host
, "@[");
736 start
++; /* Jump over '@' */
739 if (start
[0] == '[') {
740 end
= strchr(start
+ 1, ']');
742 if (removebrackets
) {
744 memmove(start
, start
+ 1, end
- start
);
755 #define STR(s) STR_(s)
757 static void get_host_and_port(char **host
, const char **port
)
760 end
= host_end(host
, 1);
761 colon
= strchr(end
, ':');
763 long portnr
= strtol(colon
+ 1, &end
, 10);
764 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
767 } else if (!colon
[1]) {
773 static void enable_keepalive(int sockfd
)
777 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
778 error_errno(_("unable to set SO_KEEPALIVE on socket"));
783 static const char *ai_name(const struct addrinfo
*ai
)
785 static char addr
[NI_MAXHOST
];
786 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
787 NI_NUMERICHOST
) != 0)
788 xsnprintf(addr
, sizeof(addr
), "(unknown)");
794 * Returns a connected socket() fd, or else die()s.
796 static int git_tcp_connect_sock(char *host
, int flags
)
798 struct strbuf error_message
= STRBUF_INIT
;
800 const char *port
= STR(DEFAULT_GIT_PORT
);
801 struct addrinfo hints
, *ai0
, *ai
;
805 get_host_and_port(&host
, &port
);
809 memset(&hints
, 0, sizeof(hints
));
810 if (flags
& CONNECT_IPV4
)
811 hints
.ai_family
= AF_INET
;
812 else if (flags
& CONNECT_IPV6
)
813 hints
.ai_family
= AF_INET6
;
814 hints
.ai_socktype
= SOCK_STREAM
;
815 hints
.ai_protocol
= IPPROTO_TCP
;
817 if (flags
& CONNECT_VERBOSE
)
818 fprintf(stderr
, _("Looking up %s ... "), host
);
820 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
822 die(_("unable to look up %s (port %s) (%s)"), host
, port
, gai_strerror(gai
));
824 if (flags
& CONNECT_VERBOSE
)
825 /* TRANSLATORS: this is the end of "Looking up %s ... " */
826 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
828 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
829 sockfd
= socket(ai
->ai_family
,
830 ai
->ai_socktype
, ai
->ai_protocol
);
832 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
833 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
834 host
, cnt
, ai_name(ai
), strerror(errno
));
840 if (flags
& CONNECT_VERBOSE
)
841 fprintf(stderr
, "%s ", ai_name(ai
));
848 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
850 enable_keepalive(sockfd
);
852 if (flags
& CONNECT_VERBOSE
)
853 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
854 fprintf_ln(stderr
, _("done."));
856 strbuf_release(&error_message
);
864 * Returns a connected socket() fd, or else die()s.
866 static int git_tcp_connect_sock(char *host
, int flags
)
868 struct strbuf error_message
= STRBUF_INIT
;
870 const char *port
= STR(DEFAULT_GIT_PORT
);
873 struct sockaddr_in sa
;
878 get_host_and_port(&host
, &port
);
880 if (flags
& CONNECT_VERBOSE
)
881 fprintf(stderr
, _("Looking up %s ... "), host
);
883 he
= gethostbyname(host
);
885 die(_("unable to look up %s (%s)"), host
, hstrerror(h_errno
));
886 nport
= strtoul(port
, &ep
, 10);
887 if ( ep
== port
|| *ep
) {
889 struct servent
*se
= getservbyname(port
,"tcp");
891 die(_("unknown port %s"), port
);
895 if (flags
& CONNECT_VERBOSE
)
896 /* TRANSLATORS: this is the end of "Looking up %s ... " */
897 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
899 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
900 memset(&sa
, 0, sizeof sa
);
901 sa
.sin_family
= he
->h_addrtype
;
902 sa
.sin_port
= htons(nport
);
903 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
905 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
907 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
908 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
911 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
918 if (flags
& CONNECT_VERBOSE
)
919 fprintf(stderr
, "%s ",
920 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
925 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
927 enable_keepalive(sockfd
);
929 if (flags
& CONNECT_VERBOSE
)
930 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
931 fprintf_ln(stderr
, _("done."));
940 * Dummy child_process returned by git_connect() if the transport protocol
941 * does not need fork(2).
943 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
945 int git_connection_is_socket(struct child_process
*conn
)
947 return conn
== &no_fork
;
950 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
952 int sockfd
= git_tcp_connect_sock(host
, flags
);
961 static char *git_proxy_command
;
963 static int git_proxy_command_options(const char *var
, const char *value
,
966 if (!strcmp(var
, "core.gitproxy")) {
970 const char *rhost_name
= cb
;
971 int rhost_len
= strlen(rhost_name
);
973 if (git_proxy_command
)
976 return config_error_nonbool(var
);
978 * ;# matches www.kernel.org as well
979 * gitproxy = netcatter-1 for kernel.org
980 * gitproxy = netcatter-2 for sample.xz
981 * gitproxy = netcatter-default
983 for_pos
= strstr(value
, " for ");
985 /* matches everybody */
986 matchlen
= strlen(value
);
988 hostlen
= strlen(for_pos
+ 5);
989 if (rhost_len
< hostlen
)
991 else if (!strncmp(for_pos
+ 5,
992 rhost_name
+ rhost_len
- hostlen
,
994 ((rhost_len
== hostlen
) ||
995 rhost_name
[rhost_len
- hostlen
-1] == '.'))
996 matchlen
= for_pos
- value
;
1000 if (0 <= matchlen
) {
1001 /* core.gitproxy = none for kernel.org */
1002 if (matchlen
== 4 &&
1003 !memcmp(value
, "none", 4))
1005 git_proxy_command
= xmemdupz(value
, matchlen
);
1010 return git_default_config(var
, value
, cb
);
1013 static int git_use_proxy(const char *host
)
1015 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
1016 git_config(git_proxy_command_options
, (void*)host
);
1017 return (git_proxy_command
&& *git_proxy_command
);
1020 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
1022 const char *port
= STR(DEFAULT_GIT_PORT
);
1023 struct child_process
*proxy
;
1025 get_host_and_port(&host
, &port
);
1027 if (looks_like_command_line_option(host
))
1028 die(_("strange hostname '%s' blocked"), host
);
1029 if (looks_like_command_line_option(port
))
1030 die(_("strange port '%s' blocked"), port
);
1032 proxy
= xmalloc(sizeof(*proxy
));
1033 child_process_init(proxy
);
1034 strvec_push(&proxy
->args
, git_proxy_command
);
1035 strvec_push(&proxy
->args
, host
);
1036 strvec_push(&proxy
->args
, port
);
1039 if (start_command(proxy
))
1040 die(_("cannot start proxy %s"), git_proxy_command
);
1041 fd
[0] = proxy
->out
; /* read from proxy stdout */
1042 fd
[1] = proxy
->in
; /* write to proxy stdin */
1046 static char *get_port(char *host
)
1049 char *p
= strchr(host
, ':');
1052 long port
= strtol(p
+ 1, &end
, 10);
1053 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
1063 * Extract protocol and relevant parts from the specified connection URL.
1064 * The caller must free() the returned strings.
1066 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
1072 int separator
= '/';
1073 enum protocol protocol
= PROTO_LOCAL
;
1075 if (is_url(url_orig
))
1076 url
= url_decode(url_orig
);
1078 url
= xstrdup(url_orig
);
1080 host
= strstr(url
, "://");
1083 protocol
= get_protocol(url
);
1087 if (!url_is_local_not_ssh(url
)) {
1088 protocol
= PROTO_SSH
;
1094 * Don't do destructive transforms as protocol code does
1095 * '[]' unwrapping in get_host_and_port()
1097 end
= host_end(&host
, 0);
1099 if (protocol
== PROTO_LOCAL
)
1101 else if (protocol
== PROTO_FILE
&& *host
!= '/' &&
1102 !has_dos_drive_prefix(host
) &&
1103 offset_1st_component(host
- 2) > 1)
1104 path
= host
- 2; /* include the leading "//" */
1105 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
1106 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1108 path
= strchr(end
, separator
);
1110 if (!path
|| !*path
)
1111 die(_("no path specified; see 'git help pull' for valid url syntax"));
1114 * null-terminate hostname and point path to ~ for URL's like this:
1115 * ssh://host.xz/~user/repo
1118 end
= path
; /* Need to \0 terminate host here */
1119 if (separator
== ':')
1120 path
++; /* path starts after ':' */
1121 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
1126 path
= xstrdup(path
);
1129 *ret_host
= xstrdup(host
);
1135 static const char *get_ssh_command(void)
1139 if ((ssh
= getenv("GIT_SSH_COMMAND")))
1142 if (!git_config_get_string_tmp("core.sshcommand", &ssh
))
1154 VARIANT_TORTOISEPLINK
,
1157 static void override_ssh_variant(enum ssh_variant
*ssh_variant
)
1159 const char *variant
= getenv("GIT_SSH_VARIANT");
1161 if (!variant
&& git_config_get_string_tmp("ssh.variant", &variant
))
1164 if (!strcmp(variant
, "auto"))
1165 *ssh_variant
= VARIANT_AUTO
;
1166 else if (!strcmp(variant
, "plink"))
1167 *ssh_variant
= VARIANT_PLINK
;
1168 else if (!strcmp(variant
, "putty"))
1169 *ssh_variant
= VARIANT_PUTTY
;
1170 else if (!strcmp(variant
, "tortoiseplink"))
1171 *ssh_variant
= VARIANT_TORTOISEPLINK
;
1172 else if (!strcmp(variant
, "simple"))
1173 *ssh_variant
= VARIANT_SIMPLE
;
1175 *ssh_variant
= VARIANT_SSH
;
1178 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
1181 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
1182 const char *variant
;
1185 override_ssh_variant(&ssh_variant
);
1187 if (ssh_variant
!= VARIANT_AUTO
)
1191 p
= xstrdup(ssh_command
);
1192 variant
= basename(p
);
1194 const char **ssh_argv
;
1196 p
= xstrdup(ssh_command
);
1197 if (split_cmdline(p
, &ssh_argv
) > 0) {
1198 variant
= basename((char *)ssh_argv
[0]);
1200 * At this point, variant points into the buffer
1201 * referenced by p, hence we do not need ssh_argv
1211 if (!strcasecmp(variant
, "ssh") ||
1212 !strcasecmp(variant
, "ssh.exe"))
1213 ssh_variant
= VARIANT_SSH
;
1214 else if (!strcasecmp(variant
, "plink") ||
1215 !strcasecmp(variant
, "plink.exe"))
1216 ssh_variant
= VARIANT_PLINK
;
1217 else if (!strcasecmp(variant
, "tortoiseplink") ||
1218 !strcasecmp(variant
, "tortoiseplink.exe"))
1219 ssh_variant
= VARIANT_TORTOISEPLINK
;
1226 * Open a connection using Git's native protocol.
1228 * The caller is responsible for freeing hostandport, but this function may
1229 * modify it (for example, to truncate it to remove the port part).
1231 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
1232 const char *path
, const char *prog
,
1233 enum protocol_version version
,
1236 struct child_process
*conn
;
1237 struct strbuf request
= STRBUF_INIT
;
1239 * Set up virtual host information based on where we will
1240 * connect, unless the user has overridden us in
1243 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1245 target_host
= xstrdup(target_host
);
1247 target_host
= xstrdup(hostandport
);
1249 transport_check_allowed("git");
1250 if (strchr(target_host
, '\n') || strchr(path
, '\n'))
1251 die(_("newline is forbidden in git:// hosts and repo paths"));
1254 * These underlying connection commands die() if they
1257 if (git_use_proxy(hostandport
))
1258 conn
= git_proxy_connect(fd
, hostandport
);
1260 conn
= git_tcp_connect(fd
, hostandport
, flags
);
1262 * Separate original protocol components prog and path
1263 * from extended host header with a NUL byte.
1265 * Note: Do not add any other headers here! Doing so
1266 * will cause older git-daemon servers to crash.
1268 strbuf_addf(&request
,
1273 /* If using a new version put that stuff here after a second null byte */
1275 strbuf_addch(&request
, '\0');
1276 strbuf_addf(&request
, "version=%d%c",
1280 packet_write(fd
[1], request
.buf
, request
.len
);
1283 strbuf_release(&request
);
1288 * Append the appropriate environment variables to `env` and options to
1289 * `args` for running ssh in Git's SSH-tunneled transport.
1291 static void push_ssh_options(struct strvec
*args
, struct strvec
*env
,
1292 enum ssh_variant variant
, const char *port
,
1293 enum protocol_version version
, int flags
)
1295 if (variant
== VARIANT_SSH
&&
1297 strvec_push(args
, "-o");
1298 strvec_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
1299 strvec_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1303 if (flags
& CONNECT_IPV4
) {
1306 BUG("VARIANT_AUTO passed to push_ssh_options");
1307 case VARIANT_SIMPLE
:
1308 die(_("ssh variant 'simple' does not support -4"));
1312 case VARIANT_TORTOISEPLINK
:
1313 strvec_push(args
, "-4");
1315 } else if (flags
& CONNECT_IPV6
) {
1318 BUG("VARIANT_AUTO passed to push_ssh_options");
1319 case VARIANT_SIMPLE
:
1320 die(_("ssh variant 'simple' does not support -6"));
1324 case VARIANT_TORTOISEPLINK
:
1325 strvec_push(args
, "-6");
1329 if (variant
== VARIANT_TORTOISEPLINK
)
1330 strvec_push(args
, "-batch");
1335 BUG("VARIANT_AUTO passed to push_ssh_options");
1336 case VARIANT_SIMPLE
:
1337 die(_("ssh variant 'simple' does not support setting port"));
1339 strvec_push(args
, "-p");
1343 case VARIANT_TORTOISEPLINK
:
1344 strvec_push(args
, "-P");
1347 strvec_push(args
, port
);
1351 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1352 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
1353 const char *port
, enum protocol_version version
,
1357 enum ssh_variant variant
;
1359 if (looks_like_command_line_option(ssh_host
))
1360 die(_("strange hostname '%s' blocked"), ssh_host
);
1362 ssh
= get_ssh_command();
1364 variant
= determine_ssh_variant(ssh
, 1);
1367 * GIT_SSH is the no-shell version of
1368 * GIT_SSH_COMMAND (and must remain so for
1369 * historical compatibility).
1371 conn
->use_shell
= 0;
1373 ssh
= getenv("GIT_SSH");
1376 variant
= determine_ssh_variant(ssh
, 0);
1379 if (variant
== VARIANT_AUTO
) {
1380 struct child_process detect
= CHILD_PROCESS_INIT
;
1382 detect
.use_shell
= conn
->use_shell
;
1383 detect
.no_stdin
= detect
.no_stdout
= detect
.no_stderr
= 1;
1385 strvec_push(&detect
.args
, ssh
);
1386 strvec_push(&detect
.args
, "-G");
1387 push_ssh_options(&detect
.args
, &detect
.env
,
1388 VARIANT_SSH
, port
, version
, flags
);
1389 strvec_push(&detect
.args
, ssh_host
);
1391 variant
= run_command(&detect
) ? VARIANT_SIMPLE
: VARIANT_SSH
;
1394 strvec_push(&conn
->args
, ssh
);
1395 push_ssh_options(&conn
->args
, &conn
->env
, variant
, port
, version
,
1397 strvec_push(&conn
->args
, ssh_host
);
1401 * This returns the dummy child_process `no_fork` if the transport protocol
1402 * does not need fork(2), or a struct child_process object if it does. Once
1403 * done, finish the connection with finish_connect() with the value returned
1404 * from this function (it is safe to call finish_connect() with NULL to
1405 * support the former case).
1407 * If it returns, the connect is successful; it just dies on errors (this
1408 * will hopefully be changed in a libification effort, to return NULL when
1409 * the connection failed).
1411 struct child_process
*git_connect(int fd
[2], const char *url
,
1412 const char *prog
, int flags
)
1414 char *hostandport
, *path
;
1415 struct child_process
*conn
;
1416 enum protocol protocol
;
1417 enum protocol_version version
= get_protocol_version_config();
1420 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1421 * to perform a push, then fallback to v0 since the client doesn't know
1422 * how to push yet using v2.
1424 if (version
== protocol_v2
&& !strcmp("git-receive-pack", prog
))
1425 version
= protocol_v0
;
1427 /* Without this we cannot rely on waitpid() to tell
1428 * what happened to our children.
1430 signal(SIGCHLD
, SIG_DFL
);
1432 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1433 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1434 printf("Diag: url=%s\n", url
? url
: "NULL");
1435 printf("Diag: protocol=%s\n", prot_name(protocol
));
1436 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1437 printf("Diag: path=%s\n", path
? path
: "NULL");
1439 } else if (protocol
== PROTO_GIT
) {
1440 conn
= git_connect_git(fd
, hostandport
, path
, prog
, version
, flags
);
1441 conn
->trace2_child_class
= "transport/git";
1443 struct strbuf cmd
= STRBUF_INIT
;
1444 const char *const *var
;
1446 conn
= xmalloc(sizeof(*conn
));
1447 child_process_init(conn
);
1449 if (looks_like_command_line_option(path
))
1450 die(_("strange pathname '%s' blocked"), path
);
1452 strbuf_addstr(&cmd
, prog
);
1453 strbuf_addch(&cmd
, ' ');
1454 sq_quote_buf(&cmd
, path
);
1456 /* remove repo-local variables from the environment */
1457 for (var
= local_repo_env
; *var
; var
++)
1458 strvec_push(&conn
->env
, *var
);
1460 conn
->use_shell
= 1;
1461 conn
->in
= conn
->out
= -1;
1462 if (protocol
== PROTO_SSH
) {
1463 char *ssh_host
= hostandport
;
1464 const char *port
= NULL
;
1465 transport_check_allowed("ssh");
1466 get_host_and_port(&ssh_host
, &port
);
1469 port
= get_port(ssh_host
);
1471 if (flags
& CONNECT_DIAG_URL
) {
1472 printf("Diag: url=%s\n", url
? url
: "NULL");
1473 printf("Diag: protocol=%s\n", prot_name(protocol
));
1474 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1475 printf("Diag: port=%s\n", port
? port
: "NONE");
1476 printf("Diag: path=%s\n", path
? path
: "NULL");
1481 strbuf_release(&cmd
);
1484 conn
->trace2_child_class
= "transport/ssh";
1485 fill_ssh_args(conn
, ssh_host
, port
, version
, flags
);
1487 transport_check_allowed("file");
1488 conn
->trace2_child_class
= "transport/file";
1490 strvec_pushf(&conn
->env
,
1491 GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1495 strvec_push(&conn
->args
, cmd
.buf
);
1497 if (start_command(conn
))
1498 die(_("unable to fork"));
1500 fd
[0] = conn
->out
; /* read from child's stdout */
1501 fd
[1] = conn
->in
; /* write to child's stdin */
1502 strbuf_release(&cmd
);
1509 int finish_connect(struct child_process
*conn
)
1512 if (!conn
|| git_connection_is_socket(conn
))
1515 code
= finish_command(conn
);