1 #include "git-compat-util.h"
8 #include "run-command.h"
12 #include "string-list.h"
13 #include "oid-array.h"
14 #include "transport.h"
19 #include "bundle-uri.h"
21 static char *server_capabilities_v1
;
22 static struct strvec server_capabilities_v2
= STRVEC_INIT
;
23 static const char *next_server_feature_value(const char *feature
, int *len
, int *offset
);
25 static int check_ref(const char *name
, unsigned int flags
)
30 if (!skip_prefix(name
, "refs/", &name
))
33 /* REF_NORMAL means that we don't want the magic fake tag refs */
34 if ((flags
& REF_NORMAL
) && check_refname_format(name
, 0))
37 /* REF_HEADS means that we want regular branch heads */
38 if ((flags
& REF_HEADS
) && starts_with(name
, "heads/"))
41 /* REF_TAGS means that we want tags */
42 if ((flags
& REF_TAGS
) && starts_with(name
, "tags/"))
45 /* All type bits clear means that we are ok with anything */
46 return !(flags
& ~REF_NORMAL
);
49 int check_ref_type(const struct ref
*ref
, int flags
)
51 return check_ref(ref
->name
, flags
);
54 static NORETURN
void die_initial_contact(int unexpected
)
57 * A hang-up after seeing some response from the other end
58 * means that it is unexpected, as we know the other end is
59 * willing to talk to us. A hang-up before seeing any
60 * response does not necessarily mean an ACL problem, though.
63 die(_("the remote end hung up upon initial contact"));
65 die(_("Could not read from remote repository.\n\n"
66 "Please make sure you have the correct access rights\n"
67 "and the repository exists."));
70 /* Checks if the server supports the capability 'c' */
71 int server_supports_v2(const char *c
)
75 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
77 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
78 (!*out
|| *out
== '='))
84 void ensure_server_supports_v2(const char *c
)
86 if (!server_supports_v2(c
))
87 die(_("server doesn't support '%s'"), c
);
90 int server_feature_v2(const char *c
, const char **v
)
94 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
96 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
105 int server_supports_feature(const char *c
, const char *feature
,
110 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
112 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
113 (!*out
|| *(out
++) == '=')) {
114 if (parse_feature_request(out
, feature
))
122 die(_("server doesn't support feature '%s'"), feature
);
127 static void process_capabilities_v2(struct packet_reader
*reader
)
129 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
)
130 strvec_push(&server_capabilities_v2
, reader
->line
);
132 if (reader
->status
!= PACKET_READ_FLUSH
)
133 die(_("expected flush after capabilities"));
136 enum protocol_version
discover_version(struct packet_reader
*reader
)
138 enum protocol_version version
= protocol_unknown_version
;
141 * Peek the first line of the server's response to
142 * determine the protocol version the server is speaking.
144 switch (packet_reader_peek(reader
)) {
145 case PACKET_READ_EOF
:
146 die_initial_contact(0);
147 case PACKET_READ_FLUSH
:
148 case PACKET_READ_DELIM
:
149 case PACKET_READ_RESPONSE_END
:
150 version
= protocol_v0
;
152 case PACKET_READ_NORMAL
:
153 version
= determine_protocol_version_client(reader
->line
);
159 process_capabilities_v2(reader
);
162 /* Read the peeked version line */
163 packet_reader_read(reader
);
167 case protocol_unknown_version
:
168 BUG("unknown protocol version");
171 trace2_data_intmax("transfer", NULL
, "negotiated-version", version
);
176 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
179 struct string_list_item
*item
;
182 return; /* just "symref" */
183 /* e.g. "symref=HEAD:refs/heads/master" */
184 sym
= xmemdupz(val
, len
);
185 target
= strchr(sym
, ':');
187 /* just "symref=something" */
190 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
191 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
192 /* "symref=bogus:pair */
194 item
= string_list_append_nodup(symref
, sym
);
202 static void annotate_refs_with_symref_info(struct ref
*ref
)
204 struct string_list symref
= STRING_LIST_INIT_DUP
;
211 val
= next_server_feature_value("symref", &len
, &offset
);
214 parse_one_symref_info(&symref
, val
, len
);
216 string_list_sort(&symref
);
218 for (; ref
; ref
= ref
->next
) {
219 struct string_list_item
*item
;
220 item
= string_list_lookup(&symref
, ref
->name
);
223 ref
->symref
= xstrdup((char *)item
->util
);
225 string_list_clear(&symref
, 0);
228 static void process_capabilities(struct packet_reader
*reader
, int *linelen
)
230 const char *feat_val
;
232 const char *line
= reader
->line
;
233 int nul_location
= strlen(line
);
234 if (nul_location
== *linelen
)
236 server_capabilities_v1
= xstrdup(line
+ nul_location
+ 1);
237 *linelen
= nul_location
;
239 feat_val
= server_feature_value("object-format", &feat_len
);
241 char *hash_name
= xstrndup(feat_val
, feat_len
);
242 int hash_algo
= hash_algo_by_name(hash_name
);
243 if (hash_algo
!= GIT_HASH_UNKNOWN
)
244 reader
->hash_algo
= &hash_algos
[hash_algo
];
247 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
251 static int process_dummy_ref(const struct packet_reader
*reader
)
253 const char *line
= reader
->line
;
254 struct object_id oid
;
257 if (parse_oid_hex_algop(line
, &oid
, &name
, reader
->hash_algo
))
263 return oideq(null_oid(), &oid
) && !strcmp(name
, "capabilities^{}");
266 static void check_no_capabilities(const char *line
, int len
)
268 if (strlen(line
) != len
)
269 warning(_("ignoring capabilities after first line '%s'"),
270 line
+ strlen(line
));
273 static int process_ref(const struct packet_reader
*reader
, int len
,
274 struct ref
***list
, unsigned int flags
,
275 struct oid_array
*extra_have
)
277 const char *line
= reader
->line
;
278 struct object_id old_oid
;
281 if (parse_oid_hex_algop(line
, &old_oid
, &name
, reader
->hash_algo
))
287 if (extra_have
&& !strcmp(name
, ".have")) {
288 oid_array_append(extra_have
, &old_oid
);
289 } else if (!strcmp(name
, "capabilities^{}")) {
290 die(_("protocol error: unexpected capabilities^{}"));
291 } else if (check_ref(name
, flags
)) {
292 struct ref
*ref
= alloc_ref(name
);
293 oidcpy(&ref
->old_oid
, &old_oid
);
297 check_no_capabilities(line
, len
);
301 static int process_shallow(const struct packet_reader
*reader
, int len
,
302 struct oid_array
*shallow_points
)
304 const char *line
= reader
->line
;
306 struct object_id old_oid
;
308 if (!skip_prefix(line
, "shallow ", &arg
))
311 if (get_oid_hex_algop(arg
, &old_oid
, reader
->hash_algo
))
312 die(_("protocol error: expected shallow sha-1, got '%s'"), arg
);
314 die(_("repository on the other end cannot be shallow"));
315 oid_array_append(shallow_points
, &old_oid
);
316 check_no_capabilities(line
, len
);
320 enum get_remote_heads_state
{
321 EXPECTING_FIRST_REF
= 0,
328 * Read all the refs from the other end
330 struct ref
**get_remote_heads(struct packet_reader
*reader
,
331 struct ref
**list
, unsigned int flags
,
332 struct oid_array
*extra_have
,
333 struct oid_array
*shallow_points
)
335 struct ref
**orig_list
= list
;
337 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
341 while (state
!= EXPECTING_DONE
) {
342 switch (packet_reader_read(reader
)) {
343 case PACKET_READ_EOF
:
344 die_initial_contact(1);
345 case PACKET_READ_NORMAL
:
346 len
= reader
->pktlen
;
348 case PACKET_READ_FLUSH
:
349 state
= EXPECTING_DONE
;
351 case PACKET_READ_DELIM
:
352 case PACKET_READ_RESPONSE_END
:
353 die(_("invalid packet"));
357 case EXPECTING_FIRST_REF
:
358 process_capabilities(reader
, &len
);
359 if (process_dummy_ref(reader
)) {
360 state
= EXPECTING_SHALLOW
;
363 state
= EXPECTING_REF
;
366 if (process_ref(reader
, len
, &list
, flags
, extra_have
))
368 state
= EXPECTING_SHALLOW
;
370 case EXPECTING_SHALLOW
:
371 if (process_shallow(reader
, len
, shallow_points
))
373 die(_("protocol error: unexpected '%s'"), reader
->line
);
379 annotate_refs_with_symref_info(*orig_list
);
384 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
385 static int process_ref_v2(struct packet_reader
*reader
, struct ref
***list
,
386 const char **unborn_head_target
)
390 struct object_id old_oid
;
392 struct string_list line_sections
= STRING_LIST_INIT_DUP
;
394 const char *line
= reader
->line
;
397 * Ref lines have a number of fields which are space deliminated. The
398 * first field is the OID of the ref. The second field is the ref
399 * name. Subsequent fields (symref-target and peeled) are optional and
400 * don't have a particular order.
402 if (string_list_split(&line_sections
, line
, ' ', -1) < 2) {
407 if (!strcmp("unborn", line_sections
.items
[i
].string
)) {
409 if (unborn_head_target
&&
410 !strcmp("HEAD", line_sections
.items
[i
++].string
)) {
412 * Look for the symref target (if any). If found,
413 * return it to the caller.
415 for (; i
< line_sections
.nr
; i
++) {
416 const char *arg
= line_sections
.items
[i
].string
;
418 if (skip_prefix(arg
, "symref-target:", &arg
)) {
419 *unborn_head_target
= xstrdup(arg
);
426 if (parse_oid_hex_algop(line_sections
.items
[i
++].string
, &old_oid
, &end
, reader
->hash_algo
) ||
432 ref
= alloc_ref(line_sections
.items
[i
++].string
);
434 memcpy(ref
->old_oid
.hash
, old_oid
.hash
, reader
->hash_algo
->rawsz
);
438 for (; i
< line_sections
.nr
; i
++) {
439 const char *arg
= line_sections
.items
[i
].string
;
440 if (skip_prefix(arg
, "symref-target:", &arg
))
441 ref
->symref
= xstrdup(arg
);
443 if (skip_prefix(arg
, "peeled:", &arg
)) {
444 struct object_id peeled_oid
;
447 if (parse_oid_hex_algop(arg
, &peeled_oid
, &end
,
448 reader
->hash_algo
) || *end
) {
453 peeled_name
= xstrfmt("%s^{}", ref
->name
);
454 peeled
= alloc_ref(peeled_name
);
456 memcpy(peeled
->old_oid
.hash
, peeled_oid
.hash
,
457 reader
->hash_algo
->rawsz
);
459 *list
= &peeled
->next
;
466 string_list_clear(&line_sections
, 0);
470 void check_stateless_delimiter(int stateless_rpc
,
471 struct packet_reader
*reader
,
475 return; /* not in stateless mode, no delimiter expected */
476 if (packet_reader_read(reader
) != PACKET_READ_RESPONSE_END
)
480 static void send_capabilities(int fd_out
, struct packet_reader
*reader
)
482 const char *hash_name
;
484 if (server_supports_v2("agent"))
485 packet_write_fmt(fd_out
, "agent=%s", git_user_agent_sanitized());
487 if (server_feature_v2("object-format", &hash_name
)) {
488 int hash_algo
= hash_algo_by_name(hash_name
);
489 if (hash_algo
== GIT_HASH_UNKNOWN
)
490 die(_("unknown object format '%s' specified by server"), hash_name
);
491 reader
->hash_algo
= &hash_algos
[hash_algo
];
492 packet_write_fmt(fd_out
, "object-format=%s", reader
->hash_algo
->name
);
494 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
498 int get_remote_bundle_uri(int fd_out
, struct packet_reader
*reader
,
499 struct bundle_list
*bundles
, int stateless_rpc
)
503 /* Assert bundle-uri support */
504 ensure_server_supports_v2("bundle-uri");
506 /* (Re-)send capabilities */
507 send_capabilities(fd_out
, reader
);
510 packet_write_fmt(fd_out
, "command=bundle-uri\n");
511 packet_delim(fd_out
);
513 packet_flush(fd_out
);
515 /* Process response from server */
516 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
517 const char *line
= reader
->line
;
520 if (!bundle_uri_parse_line(bundles
, line
))
523 return error(_("error on bundle-uri response line %d: %s"),
527 if (reader
->status
!= PACKET_READ_FLUSH
)
528 return error(_("expected flush after bundle-uri listing"));
531 * Might die(), but obscure enough that that's OK, e.g. in
532 * serve.c we'll call BUG() on its equivalent (the
533 * PACKET_READ_RESPONSE_END check).
535 check_stateless_delimiter(stateless_rpc
, reader
,
536 _("expected response end packet after ref listing"));
541 struct ref
**get_remote_refs(int fd_out
, struct packet_reader
*reader
,
542 struct ref
**list
, int for_push
,
543 struct transport_ls_refs_options
*transport_options
,
544 const struct string_list
*server_options
,
548 struct strvec
*ref_prefixes
= transport_options
?
549 &transport_options
->ref_prefixes
: NULL
;
550 const char **unborn_head_target
= transport_options
?
551 &transport_options
->unborn_head_target
: NULL
;
554 ensure_server_supports_v2("ls-refs");
555 packet_write_fmt(fd_out
, "command=ls-refs\n");
557 /* Send capabilities */
558 send_capabilities(fd_out
, reader
);
560 if (server_options
&& server_options
->nr
) {
561 ensure_server_supports_v2("server-option");
562 for (i
= 0; i
< server_options
->nr
; i
++)
563 packet_write_fmt(fd_out
, "server-option=%s",
564 server_options
->items
[i
].string
);
567 packet_delim(fd_out
);
568 /* When pushing we don't want to request the peeled tags */
570 packet_write_fmt(fd_out
, "peel\n");
571 packet_write_fmt(fd_out
, "symrefs\n");
572 if (server_supports_feature("ls-refs", "unborn", 0))
573 packet_write_fmt(fd_out
, "unborn\n");
574 for (i
= 0; ref_prefixes
&& i
< ref_prefixes
->nr
; i
++) {
575 packet_write_fmt(fd_out
, "ref-prefix %s\n",
578 packet_flush(fd_out
);
580 /* Process response from server */
581 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
582 if (!process_ref_v2(reader
, &list
, unborn_head_target
))
583 die(_("invalid ls-refs response: %s"), reader
->line
);
586 if (reader
->status
!= PACKET_READ_FLUSH
)
587 die(_("expected flush after ref listing"));
589 check_stateless_delimiter(stateless_rpc
, reader
,
590 _("expected response end packet after ref listing"));
595 const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
, int *offset
)
602 len
= strlen(feature
);
604 feature_list
+= *offset
;
605 while (*feature_list
) {
606 const char *found
= strstr(feature_list
, feature
);
609 if (feature_list
== found
|| isspace(found
[-1])) {
610 const char *value
= found
+ len
;
611 /* feature with no value (e.g., "thin-pack") */
612 if (!*value
|| isspace(*value
)) {
616 *offset
= found
+ len
- feature_list
;
619 /* feature with a value (e.g., "agent=git/1.2.3") */
620 else if (*value
== '=') {
624 end
= strcspn(value
, " \t\n");
628 *offset
= value
+ end
- feature_list
;
632 * otherwise we matched a substring of another feature;
636 feature_list
= found
+ 1;
641 int server_supports_hash(const char *desired
, int *feature_supported
)
647 hash
= next_server_feature_value("object-format", &len
, &offset
);
648 if (feature_supported
)
649 *feature_supported
= !!hash
;
651 hash
= hash_algos
[GIT_HASH_SHA1
].name
;
655 if (!xstrncmpz(desired
, hash
, len
))
658 hash
= next_server_feature_value("object-format", &len
, &offset
);
663 int parse_feature_request(const char *feature_list
, const char *feature
)
665 return !!parse_feature_value(feature_list
, feature
, NULL
, NULL
);
668 static const char *next_server_feature_value(const char *feature
, int *len
, int *offset
)
670 return parse_feature_value(server_capabilities_v1
, feature
, len
, offset
);
673 const char *server_feature_value(const char *feature
, int *len
)
675 return parse_feature_value(server_capabilities_v1
, feature
, len
, NULL
);
678 int server_supports(const char *feature
)
680 return !!server_feature_value(feature
, NULL
);
690 int url_is_local_not_ssh(const char *url
)
692 const char *colon
= strchr(url
, ':');
693 const char *slash
= strchr(url
, '/');
694 return !colon
|| (slash
&& slash
< colon
) ||
695 (has_dos_drive_prefix(url
) && is_valid_path(url
));
698 static const char *prot_name(enum protocol protocol
)
709 return "unknown protocol";
713 static enum protocol
get_protocol(const char *name
)
715 if (!strcmp(name
, "ssh"))
717 if (!strcmp(name
, "git"))
719 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
721 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
723 if (!strcmp(name
, "file"))
725 die(_("protocol '%s' is not supported"), name
);
728 static char *host_end(char **hoststart
, int removebrackets
)
730 char *host
= *hoststart
;
732 char *start
= strstr(host
, "@[");
734 start
++; /* Jump over '@' */
737 if (start
[0] == '[') {
738 end
= strchr(start
+ 1, ']');
740 if (removebrackets
) {
742 memmove(start
, start
+ 1, end
- start
);
753 #define STR(s) STR_(s)
755 static void get_host_and_port(char **host
, const char **port
)
758 end
= host_end(host
, 1);
759 colon
= strchr(end
, ':');
761 long portnr
= strtol(colon
+ 1, &end
, 10);
762 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
765 } else if (!colon
[1]) {
771 static void enable_keepalive(int sockfd
)
775 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
776 error_errno(_("unable to set SO_KEEPALIVE on socket"));
781 static const char *ai_name(const struct addrinfo
*ai
)
783 static char addr
[NI_MAXHOST
];
784 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
785 NI_NUMERICHOST
) != 0)
786 xsnprintf(addr
, sizeof(addr
), "(unknown)");
792 * Returns a connected socket() fd, or else die()s.
794 static int git_tcp_connect_sock(char *host
, int flags
)
796 struct strbuf error_message
= STRBUF_INIT
;
798 const char *port
= STR(DEFAULT_GIT_PORT
);
799 struct addrinfo hints
, *ai0
, *ai
;
803 get_host_and_port(&host
, &port
);
807 memset(&hints
, 0, sizeof(hints
));
808 if (flags
& CONNECT_IPV4
)
809 hints
.ai_family
= AF_INET
;
810 else if (flags
& CONNECT_IPV6
)
811 hints
.ai_family
= AF_INET6
;
812 hints
.ai_socktype
= SOCK_STREAM
;
813 hints
.ai_protocol
= IPPROTO_TCP
;
815 if (flags
& CONNECT_VERBOSE
)
816 fprintf(stderr
, _("Looking up %s ... "), host
);
818 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
820 die(_("unable to look up %s (port %s) (%s)"), host
, port
, gai_strerror(gai
));
822 if (flags
& CONNECT_VERBOSE
)
823 /* TRANSLATORS: this is the end of "Looking up %s ... " */
824 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
826 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
827 sockfd
= socket(ai
->ai_family
,
828 ai
->ai_socktype
, ai
->ai_protocol
);
830 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
831 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
832 host
, cnt
, ai_name(ai
), strerror(errno
));
838 if (flags
& CONNECT_VERBOSE
)
839 fprintf(stderr
, "%s ", ai_name(ai
));
846 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
848 enable_keepalive(sockfd
);
850 if (flags
& CONNECT_VERBOSE
)
851 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
852 fprintf_ln(stderr
, _("done."));
854 strbuf_release(&error_message
);
862 * Returns a connected socket() fd, or else die()s.
864 static int git_tcp_connect_sock(char *host
, int flags
)
866 struct strbuf error_message
= STRBUF_INIT
;
868 const char *port
= STR(DEFAULT_GIT_PORT
);
871 struct sockaddr_in sa
;
876 get_host_and_port(&host
, &port
);
878 if (flags
& CONNECT_VERBOSE
)
879 fprintf(stderr
, _("Looking up %s ... "), host
);
881 he
= gethostbyname(host
);
883 die(_("unable to look up %s (%s)"), host
, hstrerror(h_errno
));
884 nport
= strtoul(port
, &ep
, 10);
885 if ( ep
== port
|| *ep
) {
887 struct servent
*se
= getservbyname(port
,"tcp");
889 die(_("unknown port %s"), port
);
893 if (flags
& CONNECT_VERBOSE
)
894 /* TRANSLATORS: this is the end of "Looking up %s ... " */
895 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
897 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
898 memset(&sa
, 0, sizeof sa
);
899 sa
.sin_family
= he
->h_addrtype
;
900 sa
.sin_port
= htons(nport
);
901 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
903 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
905 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
906 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
909 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
916 if (flags
& CONNECT_VERBOSE
)
917 fprintf(stderr
, "%s ",
918 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
923 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
925 enable_keepalive(sockfd
);
927 if (flags
& CONNECT_VERBOSE
)
928 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
929 fprintf_ln(stderr
, _("done."));
938 * Dummy child_process returned by git_connect() if the transport protocol
939 * does not need fork(2).
941 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
943 int git_connection_is_socket(struct child_process
*conn
)
945 return conn
== &no_fork
;
948 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
950 int sockfd
= git_tcp_connect_sock(host
, flags
);
959 static char *git_proxy_command
;
961 static int git_proxy_command_options(const char *var
, const char *value
,
964 if (!strcmp(var
, "core.gitproxy")) {
968 const char *rhost_name
= cb
;
969 int rhost_len
= strlen(rhost_name
);
971 if (git_proxy_command
)
974 return config_error_nonbool(var
);
976 * ;# matches www.kernel.org as well
977 * gitproxy = netcatter-1 for kernel.org
978 * gitproxy = netcatter-2 for sample.xz
979 * gitproxy = netcatter-default
981 for_pos
= strstr(value
, " for ");
983 /* matches everybody */
984 matchlen
= strlen(value
);
986 hostlen
= strlen(for_pos
+ 5);
987 if (rhost_len
< hostlen
)
989 else if (!strncmp(for_pos
+ 5,
990 rhost_name
+ rhost_len
- hostlen
,
992 ((rhost_len
== hostlen
) ||
993 rhost_name
[rhost_len
- hostlen
-1] == '.'))
994 matchlen
= for_pos
- value
;
999 /* core.gitproxy = none for kernel.org */
1000 if (matchlen
== 4 &&
1001 !memcmp(value
, "none", 4))
1003 git_proxy_command
= xmemdupz(value
, matchlen
);
1008 return git_default_config(var
, value
, cb
);
1011 static int git_use_proxy(const char *host
)
1013 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
1014 git_config(git_proxy_command_options
, (void*)host
);
1015 return (git_proxy_command
&& *git_proxy_command
);
1018 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
1020 const char *port
= STR(DEFAULT_GIT_PORT
);
1021 struct child_process
*proxy
;
1023 get_host_and_port(&host
, &port
);
1025 if (looks_like_command_line_option(host
))
1026 die(_("strange hostname '%s' blocked"), host
);
1027 if (looks_like_command_line_option(port
))
1028 die(_("strange port '%s' blocked"), port
);
1030 proxy
= xmalloc(sizeof(*proxy
));
1031 child_process_init(proxy
);
1032 strvec_push(&proxy
->args
, git_proxy_command
);
1033 strvec_push(&proxy
->args
, host
);
1034 strvec_push(&proxy
->args
, port
);
1037 if (start_command(proxy
))
1038 die(_("cannot start proxy %s"), git_proxy_command
);
1039 fd
[0] = proxy
->out
; /* read from proxy stdout */
1040 fd
[1] = proxy
->in
; /* write to proxy stdin */
1044 static char *get_port(char *host
)
1047 char *p
= strchr(host
, ':');
1050 long port
= strtol(p
+ 1, &end
, 10);
1051 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
1061 * Extract protocol and relevant parts from the specified connection URL.
1062 * The caller must free() the returned strings.
1064 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
1070 int separator
= '/';
1071 enum protocol protocol
= PROTO_LOCAL
;
1073 if (is_url(url_orig
))
1074 url
= url_decode(url_orig
);
1076 url
= xstrdup(url_orig
);
1078 host
= strstr(url
, "://");
1081 protocol
= get_protocol(url
);
1085 if (!url_is_local_not_ssh(url
)) {
1086 protocol
= PROTO_SSH
;
1092 * Don't do destructive transforms as protocol code does
1093 * '[]' unwrapping in get_host_and_port()
1095 end
= host_end(&host
, 0);
1097 if (protocol
== PROTO_LOCAL
)
1099 else if (protocol
== PROTO_FILE
&& *host
!= '/' &&
1100 !has_dos_drive_prefix(host
) &&
1101 offset_1st_component(host
- 2) > 1)
1102 path
= host
- 2; /* include the leading "//" */
1103 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
1104 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1106 path
= strchr(end
, separator
);
1108 if (!path
|| !*path
)
1109 die(_("no path specified; see 'git help pull' for valid url syntax"));
1112 * null-terminate hostname and point path to ~ for URL's like this:
1113 * ssh://host.xz/~user/repo
1116 end
= path
; /* Need to \0 terminate host here */
1117 if (separator
== ':')
1118 path
++; /* path starts after ':' */
1119 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
1124 path
= xstrdup(path
);
1127 *ret_host
= xstrdup(host
);
1133 static const char *get_ssh_command(void)
1137 if ((ssh
= getenv("GIT_SSH_COMMAND")))
1140 if (!git_config_get_string_tmp("core.sshcommand", &ssh
))
1152 VARIANT_TORTOISEPLINK
,
1155 static void override_ssh_variant(enum ssh_variant
*ssh_variant
)
1157 const char *variant
= getenv("GIT_SSH_VARIANT");
1159 if (!variant
&& git_config_get_string_tmp("ssh.variant", &variant
))
1162 if (!strcmp(variant
, "auto"))
1163 *ssh_variant
= VARIANT_AUTO
;
1164 else if (!strcmp(variant
, "plink"))
1165 *ssh_variant
= VARIANT_PLINK
;
1166 else if (!strcmp(variant
, "putty"))
1167 *ssh_variant
= VARIANT_PUTTY
;
1168 else if (!strcmp(variant
, "tortoiseplink"))
1169 *ssh_variant
= VARIANT_TORTOISEPLINK
;
1170 else if (!strcmp(variant
, "simple"))
1171 *ssh_variant
= VARIANT_SIMPLE
;
1173 *ssh_variant
= VARIANT_SSH
;
1176 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
1179 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
1180 const char *variant
;
1183 override_ssh_variant(&ssh_variant
);
1185 if (ssh_variant
!= VARIANT_AUTO
)
1189 p
= xstrdup(ssh_command
);
1190 variant
= basename(p
);
1192 const char **ssh_argv
;
1194 p
= xstrdup(ssh_command
);
1195 if (split_cmdline(p
, &ssh_argv
) > 0) {
1196 variant
= basename((char *)ssh_argv
[0]);
1198 * At this point, variant points into the buffer
1199 * referenced by p, hence we do not need ssh_argv
1209 if (!strcasecmp(variant
, "ssh") ||
1210 !strcasecmp(variant
, "ssh.exe"))
1211 ssh_variant
= VARIANT_SSH
;
1212 else if (!strcasecmp(variant
, "plink") ||
1213 !strcasecmp(variant
, "plink.exe"))
1214 ssh_variant
= VARIANT_PLINK
;
1215 else if (!strcasecmp(variant
, "tortoiseplink") ||
1216 !strcasecmp(variant
, "tortoiseplink.exe"))
1217 ssh_variant
= VARIANT_TORTOISEPLINK
;
1224 * Open a connection using Git's native protocol.
1226 * The caller is responsible for freeing hostandport, but this function may
1227 * modify it (for example, to truncate it to remove the port part).
1229 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
1230 const char *path
, const char *prog
,
1231 enum protocol_version version
,
1234 struct child_process
*conn
;
1235 struct strbuf request
= STRBUF_INIT
;
1237 * Set up virtual host information based on where we will
1238 * connect, unless the user has overridden us in
1241 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1243 target_host
= xstrdup(target_host
);
1245 target_host
= xstrdup(hostandport
);
1247 transport_check_allowed("git");
1248 if (strchr(target_host
, '\n') || strchr(path
, '\n'))
1249 die(_("newline is forbidden in git:// hosts and repo paths"));
1252 * These underlying connection commands die() if they
1255 if (git_use_proxy(hostandport
))
1256 conn
= git_proxy_connect(fd
, hostandport
);
1258 conn
= git_tcp_connect(fd
, hostandport
, flags
);
1260 * Separate original protocol components prog and path
1261 * from extended host header with a NUL byte.
1263 * Note: Do not add any other headers here! Doing so
1264 * will cause older git-daemon servers to crash.
1266 strbuf_addf(&request
,
1271 /* If using a new version put that stuff here after a second null byte */
1273 strbuf_addch(&request
, '\0');
1274 strbuf_addf(&request
, "version=%d%c",
1278 packet_write(fd
[1], request
.buf
, request
.len
);
1281 strbuf_release(&request
);
1286 * Append the appropriate environment variables to `env` and options to
1287 * `args` for running ssh in Git's SSH-tunneled transport.
1289 static void push_ssh_options(struct strvec
*args
, struct strvec
*env
,
1290 enum ssh_variant variant
, const char *port
,
1291 enum protocol_version version
, int flags
)
1293 if (variant
== VARIANT_SSH
&&
1295 strvec_push(args
, "-o");
1296 strvec_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
1297 strvec_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1301 if (flags
& CONNECT_IPV4
) {
1304 BUG("VARIANT_AUTO passed to push_ssh_options");
1305 case VARIANT_SIMPLE
:
1306 die(_("ssh variant 'simple' does not support -4"));
1310 case VARIANT_TORTOISEPLINK
:
1311 strvec_push(args
, "-4");
1313 } else if (flags
& CONNECT_IPV6
) {
1316 BUG("VARIANT_AUTO passed to push_ssh_options");
1317 case VARIANT_SIMPLE
:
1318 die(_("ssh variant 'simple' does not support -6"));
1322 case VARIANT_TORTOISEPLINK
:
1323 strvec_push(args
, "-6");
1327 if (variant
== VARIANT_TORTOISEPLINK
)
1328 strvec_push(args
, "-batch");
1333 BUG("VARIANT_AUTO passed to push_ssh_options");
1334 case VARIANT_SIMPLE
:
1335 die(_("ssh variant 'simple' does not support setting port"));
1337 strvec_push(args
, "-p");
1341 case VARIANT_TORTOISEPLINK
:
1342 strvec_push(args
, "-P");
1345 strvec_push(args
, port
);
1349 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1350 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
1351 const char *port
, enum protocol_version version
,
1355 enum ssh_variant variant
;
1357 if (looks_like_command_line_option(ssh_host
))
1358 die(_("strange hostname '%s' blocked"), ssh_host
);
1360 ssh
= get_ssh_command();
1362 variant
= determine_ssh_variant(ssh
, 1);
1365 * GIT_SSH is the no-shell version of
1366 * GIT_SSH_COMMAND (and must remain so for
1367 * historical compatibility).
1369 conn
->use_shell
= 0;
1371 ssh
= getenv("GIT_SSH");
1374 variant
= determine_ssh_variant(ssh
, 0);
1377 if (variant
== VARIANT_AUTO
) {
1378 struct child_process detect
= CHILD_PROCESS_INIT
;
1380 detect
.use_shell
= conn
->use_shell
;
1381 detect
.no_stdin
= detect
.no_stdout
= detect
.no_stderr
= 1;
1383 strvec_push(&detect
.args
, ssh
);
1384 strvec_push(&detect
.args
, "-G");
1385 push_ssh_options(&detect
.args
, &detect
.env
,
1386 VARIANT_SSH
, port
, version
, flags
);
1387 strvec_push(&detect
.args
, ssh_host
);
1389 variant
= run_command(&detect
) ? VARIANT_SIMPLE
: VARIANT_SSH
;
1392 strvec_push(&conn
->args
, ssh
);
1393 push_ssh_options(&conn
->args
, &conn
->env
, variant
, port
, version
,
1395 strvec_push(&conn
->args
, ssh_host
);
1399 * This returns the dummy child_process `no_fork` if the transport protocol
1400 * does not need fork(2), or a struct child_process object if it does. Once
1401 * done, finish the connection with finish_connect() with the value returned
1402 * from this function (it is safe to call finish_connect() with NULL to
1403 * support the former case).
1405 * If it returns, the connect is successful; it just dies on errors (this
1406 * will hopefully be changed in a libification effort, to return NULL when
1407 * the connection failed).
1409 struct child_process
*git_connect(int fd
[2], const char *url
,
1410 const char *prog
, int flags
)
1412 char *hostandport
, *path
;
1413 struct child_process
*conn
;
1414 enum protocol protocol
;
1415 enum protocol_version version
= get_protocol_version_config();
1418 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1419 * to perform a push, then fallback to v0 since the client doesn't know
1420 * how to push yet using v2.
1422 if (version
== protocol_v2
&& !strcmp("git-receive-pack", prog
))
1423 version
= protocol_v0
;
1425 /* Without this we cannot rely on waitpid() to tell
1426 * what happened to our children.
1428 signal(SIGCHLD
, SIG_DFL
);
1430 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1431 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1432 printf("Diag: url=%s\n", url
? url
: "NULL");
1433 printf("Diag: protocol=%s\n", prot_name(protocol
));
1434 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1435 printf("Diag: path=%s\n", path
? path
: "NULL");
1437 } else if (protocol
== PROTO_GIT
) {
1438 conn
= git_connect_git(fd
, hostandport
, path
, prog
, version
, flags
);
1439 conn
->trace2_child_class
= "transport/git";
1441 struct strbuf cmd
= STRBUF_INIT
;
1442 const char *const *var
;
1444 conn
= xmalloc(sizeof(*conn
));
1445 child_process_init(conn
);
1447 if (looks_like_command_line_option(path
))
1448 die(_("strange pathname '%s' blocked"), path
);
1450 strbuf_addstr(&cmd
, prog
);
1451 strbuf_addch(&cmd
, ' ');
1452 sq_quote_buf(&cmd
, path
);
1454 /* remove repo-local variables from the environment */
1455 for (var
= local_repo_env
; *var
; var
++)
1456 strvec_push(&conn
->env
, *var
);
1458 conn
->use_shell
= 1;
1459 conn
->in
= conn
->out
= -1;
1460 if (protocol
== PROTO_SSH
) {
1461 char *ssh_host
= hostandport
;
1462 const char *port
= NULL
;
1463 transport_check_allowed("ssh");
1464 get_host_and_port(&ssh_host
, &port
);
1467 port
= get_port(ssh_host
);
1469 if (flags
& CONNECT_DIAG_URL
) {
1470 printf("Diag: url=%s\n", url
? url
: "NULL");
1471 printf("Diag: protocol=%s\n", prot_name(protocol
));
1472 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1473 printf("Diag: port=%s\n", port
? port
: "NONE");
1474 printf("Diag: path=%s\n", path
? path
: "NULL");
1479 strbuf_release(&cmd
);
1482 conn
->trace2_child_class
= "transport/ssh";
1483 fill_ssh_args(conn
, ssh_host
, port
, version
, flags
);
1485 transport_check_allowed("file");
1486 conn
->trace2_child_class
= "transport/file";
1488 strvec_pushf(&conn
->env
,
1489 GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1493 strvec_push(&conn
->args
, cmd
.buf
);
1495 if (start_command(conn
))
1496 die(_("unable to fork"));
1498 fd
[0] = conn
->out
; /* read from child's stdout */
1499 fd
[1] = conn
->in
; /* write to child's stdin */
1500 strbuf_release(&cmd
);
1507 int finish_connect(struct child_process
*conn
)
1510 if (!conn
|| git_connection_is_socket(conn
))
1513 code
= finish_command(conn
);