1 #include "git-compat-util.h"
7 #include "run-command.h"
11 #include "string-list.h"
12 #include "sha1-array.h"
13 #include "transport.h"
18 static char *server_capabilities_v1
;
19 static struct argv_array server_capabilities_v2
= ARGV_ARRAY_INIT
;
20 static const char *parse_feature_value(const char *, const char *, int *);
22 static int check_ref(const char *name
, unsigned int flags
)
27 if (!skip_prefix(name
, "refs/", &name
))
30 /* REF_NORMAL means that we don't want the magic fake tag refs */
31 if ((flags
& REF_NORMAL
) && check_refname_format(name
, 0))
34 /* REF_HEADS means that we want regular branch heads */
35 if ((flags
& REF_HEADS
) && starts_with(name
, "heads/"))
38 /* REF_TAGS means that we want tags */
39 if ((flags
& REF_TAGS
) && starts_with(name
, "tags/"))
42 /* All type bits clear means that we are ok with anything */
43 return !(flags
& ~REF_NORMAL
);
46 int check_ref_type(const struct ref
*ref
, int flags
)
48 return check_ref(ref
->name
, flags
);
51 static void die_initial_contact(int unexpected
)
54 * A hang-up after seeing some response from the other end
55 * means that it is unexpected, as we know the other end is
56 * willing to talk to us. A hang-up before seeing any
57 * response does not necessarily mean an ACL problem, though.
60 die(_("The remote end hung up upon initial contact"));
62 die(_("Could not read from remote repository.\n\n"
63 "Please make sure you have the correct access rights\n"
64 "and the repository exists."));
67 /* Checks if the server supports the capability 'c' */
68 int server_supports_v2(const char *c
, int die_on_error
)
72 for (i
= 0; i
< server_capabilities_v2
.argc
; i
++) {
74 if (skip_prefix(server_capabilities_v2
.argv
[i
], c
, &out
) &&
75 (!*out
|| *out
== '='))
80 die("server doesn't support '%s'", c
);
85 static void process_capabilities_v2(struct packet_reader
*reader
)
87 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
)
88 argv_array_push(&server_capabilities_v2
, reader
->line
);
90 if (reader
->status
!= PACKET_READ_FLUSH
)
91 die("expected flush after capabilities");
94 enum protocol_version
discover_version(struct packet_reader
*reader
)
96 enum protocol_version version
= protocol_unknown_version
;
99 * Peek the first line of the server's response to
100 * determine the protocol version the server is speaking.
102 switch (packet_reader_peek(reader
)) {
103 case PACKET_READ_EOF
:
104 die_initial_contact(0);
105 case PACKET_READ_FLUSH
:
106 case PACKET_READ_DELIM
:
107 version
= protocol_v0
;
109 case PACKET_READ_NORMAL
:
110 version
= determine_protocol_version_client(reader
->line
);
116 process_capabilities_v2(reader
);
119 /* Read the peeked version line */
120 packet_reader_read(reader
);
124 case protocol_unknown_version
:
125 BUG("unknown protocol version");
131 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
134 struct string_list_item
*item
;
137 return; /* just "symref" */
138 /* e.g. "symref=HEAD:refs/heads/master" */
139 sym
= xmemdupz(val
, len
);
140 target
= strchr(sym
, ':');
142 /* just "symref=something" */
145 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
146 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
147 /* "symref=bogus:pair */
149 item
= string_list_append_nodup(symref
, sym
);
157 static void annotate_refs_with_symref_info(struct ref
*ref
)
159 struct string_list symref
= STRING_LIST_INIT_DUP
;
160 const char *feature_list
= server_capabilities_v1
;
162 while (feature_list
) {
166 val
= parse_feature_value(feature_list
, "symref", &len
);
169 parse_one_symref_info(&symref
, val
, len
);
170 feature_list
= val
+ 1;
172 string_list_sort(&symref
);
174 for (; ref
; ref
= ref
->next
) {
175 struct string_list_item
*item
;
176 item
= string_list_lookup(&symref
, ref
->name
);
179 ref
->symref
= xstrdup((char *)item
->util
);
181 string_list_clear(&symref
, 0);
184 static void process_capabilities(const char *line
, int *len
)
186 int nul_location
= strlen(line
);
187 if (nul_location
== *len
)
189 server_capabilities_v1
= xstrdup(line
+ nul_location
+ 1);
193 static int process_dummy_ref(const char *line
)
195 struct object_id oid
;
198 if (parse_oid_hex(line
, &oid
, &name
))
204 return !oidcmp(&null_oid
, &oid
) && !strcmp(name
, "capabilities^{}");
207 static void check_no_capabilities(const char *line
, int len
)
209 if (strlen(line
) != len
)
210 warning("Ignoring capabilities after first line '%s'",
211 line
+ strlen(line
));
214 static int process_ref(const char *line
, int len
, struct ref
***list
,
215 unsigned int flags
, struct oid_array
*extra_have
)
217 struct object_id old_oid
;
220 if (parse_oid_hex(line
, &old_oid
, &name
))
226 if (extra_have
&& !strcmp(name
, ".have")) {
227 oid_array_append(extra_have
, &old_oid
);
228 } else if (!strcmp(name
, "capabilities^{}")) {
229 die("protocol error: unexpected capabilities^{}");
230 } else if (check_ref(name
, flags
)) {
231 struct ref
*ref
= alloc_ref(name
);
232 oidcpy(&ref
->old_oid
, &old_oid
);
236 check_no_capabilities(line
, len
);
240 static int process_shallow(const char *line
, int len
,
241 struct oid_array
*shallow_points
)
244 struct object_id old_oid
;
246 if (!skip_prefix(line
, "shallow ", &arg
))
249 if (get_oid_hex(arg
, &old_oid
))
250 die("protocol error: expected shallow sha-1, got '%s'", arg
);
252 die("repository on the other end cannot be shallow");
253 oid_array_append(shallow_points
, &old_oid
);
254 check_no_capabilities(line
, len
);
258 enum get_remote_heads_state
{
259 EXPECTING_FIRST_REF
= 0,
266 * Read all the refs from the other end
268 struct ref
**get_remote_heads(struct packet_reader
*reader
,
269 struct ref
**list
, unsigned int flags
,
270 struct oid_array
*extra_have
,
271 struct oid_array
*shallow_points
)
273 struct ref
**orig_list
= list
;
275 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
280 while (state
!= EXPECTING_DONE
) {
281 switch (packet_reader_read(reader
)) {
282 case PACKET_READ_EOF
:
283 die_initial_contact(1);
284 case PACKET_READ_NORMAL
:
285 len
= reader
->pktlen
;
286 if (len
> 4 && skip_prefix(reader
->line
, "ERR ", &arg
))
287 die("remote error: %s", arg
);
289 case PACKET_READ_FLUSH
:
290 state
= EXPECTING_DONE
;
292 case PACKET_READ_DELIM
:
293 die("invalid packet");
297 case EXPECTING_FIRST_REF
:
298 process_capabilities(reader
->line
, &len
);
299 if (process_dummy_ref(reader
->line
)) {
300 state
= EXPECTING_SHALLOW
;
303 state
= EXPECTING_REF
;
306 if (process_ref(reader
->line
, len
, &list
, flags
, extra_have
))
308 state
= EXPECTING_SHALLOW
;
310 case EXPECTING_SHALLOW
:
311 if (process_shallow(reader
->line
, len
, shallow_points
))
313 die("protocol error: unexpected '%s'", reader
->line
);
319 annotate_refs_with_symref_info(*orig_list
);
324 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
325 static int process_ref_v2(const char *line
, struct ref
***list
)
329 struct object_id old_oid
;
331 struct string_list line_sections
= STRING_LIST_INIT_DUP
;
335 * Ref lines have a number of fields which are space deliminated. The
336 * first field is the OID of the ref. The second field is the ref
337 * name. Subsequent fields (symref-target and peeled) are optional and
338 * don't have a particular order.
340 if (string_list_split(&line_sections
, line
, ' ', -1) < 2) {
345 if (parse_oid_hex(line_sections
.items
[i
++].string
, &old_oid
, &end
) ||
351 ref
= alloc_ref(line_sections
.items
[i
++].string
);
353 oidcpy(&ref
->old_oid
, &old_oid
);
357 for (; i
< line_sections
.nr
; i
++) {
358 const char *arg
= line_sections
.items
[i
].string
;
359 if (skip_prefix(arg
, "symref-target:", &arg
))
360 ref
->symref
= xstrdup(arg
);
362 if (skip_prefix(arg
, "peeled:", &arg
)) {
363 struct object_id peeled_oid
;
366 if (parse_oid_hex(arg
, &peeled_oid
, &end
) || *end
) {
371 peeled_name
= xstrfmt("%s^{}", ref
->name
);
372 peeled
= alloc_ref(peeled_name
);
374 oidcpy(&peeled
->old_oid
, &peeled_oid
);
376 *list
= &peeled
->next
;
383 string_list_clear(&line_sections
, 0);
387 struct ref
**get_remote_refs(int fd_out
, struct packet_reader
*reader
,
388 struct ref
**list
, int for_push
,
389 const struct argv_array
*ref_prefixes
)
394 if (server_supports_v2("ls-refs", 1))
395 packet_write_fmt(fd_out
, "command=ls-refs\n");
397 if (server_supports_v2("agent", 0))
398 packet_write_fmt(fd_out
, "agent=%s", git_user_agent_sanitized());
400 packet_delim(fd_out
);
401 /* When pushing we don't want to request the peeled tags */
403 packet_write_fmt(fd_out
, "peel\n");
404 packet_write_fmt(fd_out
, "symrefs\n");
405 for (i
= 0; ref_prefixes
&& i
< ref_prefixes
->argc
; i
++) {
406 packet_write_fmt(fd_out
, "ref-prefix %s\n",
407 ref_prefixes
->argv
[i
]);
409 packet_flush(fd_out
);
411 /* Process response from server */
412 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
413 if (!process_ref_v2(reader
->line
, &list
))
414 die("invalid ls-refs response: %s", reader
->line
);
417 if (reader
->status
!= PACKET_READ_FLUSH
)
418 die("expected flush after ref listing");
423 static const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
)
430 len
= strlen(feature
);
431 while (*feature_list
) {
432 const char *found
= strstr(feature_list
, feature
);
435 if (feature_list
== found
|| isspace(found
[-1])) {
436 const char *value
= found
+ len
;
437 /* feature with no value (e.g., "thin-pack") */
438 if (!*value
|| isspace(*value
)) {
443 /* feature with a value (e.g., "agent=git/1.2.3") */
444 else if (*value
== '=') {
447 *lenp
= strcspn(value
, " \t\n");
451 * otherwise we matched a substring of another feature;
455 feature_list
= found
+ 1;
460 int parse_feature_request(const char *feature_list
, const char *feature
)
462 return !!parse_feature_value(feature_list
, feature
, NULL
);
465 const char *server_feature_value(const char *feature
, int *len
)
467 return parse_feature_value(server_capabilities_v1
, feature
, len
);
470 int server_supports(const char *feature
)
472 return !!server_feature_value(feature
, NULL
);
482 int url_is_local_not_ssh(const char *url
)
484 const char *colon
= strchr(url
, ':');
485 const char *slash
= strchr(url
, '/');
486 return !colon
|| (slash
&& slash
< colon
) ||
487 has_dos_drive_prefix(url
);
490 static const char *prot_name(enum protocol protocol
)
501 return "unknown protocol";
505 static enum protocol
get_protocol(const char *name
)
507 if (!strcmp(name
, "ssh"))
509 if (!strcmp(name
, "git"))
511 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
513 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
515 if (!strcmp(name
, "file"))
517 die("I don't handle protocol '%s'", name
);
520 static char *host_end(char **hoststart
, int removebrackets
)
522 char *host
= *hoststart
;
524 char *start
= strstr(host
, "@[");
526 start
++; /* Jump over '@' */
529 if (start
[0] == '[') {
530 end
= strchr(start
+ 1, ']');
532 if (removebrackets
) {
534 memmove(start
, start
+ 1, end
- start
);
545 #define STR(s) STR_(s)
547 static void get_host_and_port(char **host
, const char **port
)
550 end
= host_end(host
, 1);
551 colon
= strchr(end
, ':');
553 long portnr
= strtol(colon
+ 1, &end
, 10);
554 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
557 } else if (!colon
[1]) {
563 static void enable_keepalive(int sockfd
)
567 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
568 fprintf(stderr
, "unable to set SO_KEEPALIVE on socket: %s\n",
574 static const char *ai_name(const struct addrinfo
*ai
)
576 static char addr
[NI_MAXHOST
];
577 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
578 NI_NUMERICHOST
) != 0)
579 xsnprintf(addr
, sizeof(addr
), "(unknown)");
585 * Returns a connected socket() fd, or else die()s.
587 static int git_tcp_connect_sock(char *host
, int flags
)
589 struct strbuf error_message
= STRBUF_INIT
;
591 const char *port
= STR(DEFAULT_GIT_PORT
);
592 struct addrinfo hints
, *ai0
, *ai
;
596 get_host_and_port(&host
, &port
);
600 memset(&hints
, 0, sizeof(hints
));
601 if (flags
& CONNECT_IPV4
)
602 hints
.ai_family
= AF_INET
;
603 else if (flags
& CONNECT_IPV6
)
604 hints
.ai_family
= AF_INET6
;
605 hints
.ai_socktype
= SOCK_STREAM
;
606 hints
.ai_protocol
= IPPROTO_TCP
;
608 if (flags
& CONNECT_VERBOSE
)
609 fprintf(stderr
, "Looking up %s ... ", host
);
611 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
613 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
615 if (flags
& CONNECT_VERBOSE
)
616 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
618 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
619 sockfd
= socket(ai
->ai_family
,
620 ai
->ai_socktype
, ai
->ai_protocol
);
622 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
623 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
624 host
, cnt
, ai_name(ai
), strerror(errno
));
630 if (flags
& CONNECT_VERBOSE
)
631 fprintf(stderr
, "%s ", ai_name(ai
));
638 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
640 enable_keepalive(sockfd
);
642 if (flags
& CONNECT_VERBOSE
)
643 fprintf(stderr
, "done.\n");
645 strbuf_release(&error_message
);
653 * Returns a connected socket() fd, or else die()s.
655 static int git_tcp_connect_sock(char *host
, int flags
)
657 struct strbuf error_message
= STRBUF_INIT
;
659 const char *port
= STR(DEFAULT_GIT_PORT
);
662 struct sockaddr_in sa
;
667 get_host_and_port(&host
, &port
);
669 if (flags
& CONNECT_VERBOSE
)
670 fprintf(stderr
, "Looking up %s ... ", host
);
672 he
= gethostbyname(host
);
674 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
675 nport
= strtoul(port
, &ep
, 10);
676 if ( ep
== port
|| *ep
) {
678 struct servent
*se
= getservbyname(port
,"tcp");
680 die("Unknown port %s", port
);
684 if (flags
& CONNECT_VERBOSE
)
685 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
687 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
688 memset(&sa
, 0, sizeof sa
);
689 sa
.sin_family
= he
->h_addrtype
;
690 sa
.sin_port
= htons(nport
);
691 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
693 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
695 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
696 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
699 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
706 if (flags
& CONNECT_VERBOSE
)
707 fprintf(stderr
, "%s ",
708 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
713 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
715 enable_keepalive(sockfd
);
717 if (flags
& CONNECT_VERBOSE
)
718 fprintf(stderr
, "done.\n");
727 * Dummy child_process returned by git_connect() if the transport protocol
728 * does not need fork(2).
730 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
732 int git_connection_is_socket(struct child_process
*conn
)
734 return conn
== &no_fork
;
737 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
739 int sockfd
= git_tcp_connect_sock(host
, flags
);
748 static char *git_proxy_command
;
750 static int git_proxy_command_options(const char *var
, const char *value
,
753 if (!strcmp(var
, "core.gitproxy")) {
757 const char *rhost_name
= cb
;
758 int rhost_len
= strlen(rhost_name
);
760 if (git_proxy_command
)
763 return config_error_nonbool(var
);
765 * ;# matches www.kernel.org as well
766 * gitproxy = netcatter-1 for kernel.org
767 * gitproxy = netcatter-2 for sample.xz
768 * gitproxy = netcatter-default
770 for_pos
= strstr(value
, " for ");
772 /* matches everybody */
773 matchlen
= strlen(value
);
775 hostlen
= strlen(for_pos
+ 5);
776 if (rhost_len
< hostlen
)
778 else if (!strncmp(for_pos
+ 5,
779 rhost_name
+ rhost_len
- hostlen
,
781 ((rhost_len
== hostlen
) ||
782 rhost_name
[rhost_len
- hostlen
-1] == '.'))
783 matchlen
= for_pos
- value
;
788 /* core.gitproxy = none for kernel.org */
790 !memcmp(value
, "none", 4))
792 git_proxy_command
= xmemdupz(value
, matchlen
);
797 return git_default_config(var
, value
, cb
);
800 static int git_use_proxy(const char *host
)
802 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
803 git_config(git_proxy_command_options
, (void*)host
);
804 return (git_proxy_command
&& *git_proxy_command
);
807 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
809 const char *port
= STR(DEFAULT_GIT_PORT
);
810 struct child_process
*proxy
;
812 get_host_and_port(&host
, &port
);
814 if (looks_like_command_line_option(host
))
815 die("strange hostname '%s' blocked", host
);
816 if (looks_like_command_line_option(port
))
817 die("strange port '%s' blocked", port
);
819 proxy
= xmalloc(sizeof(*proxy
));
820 child_process_init(proxy
);
821 argv_array_push(&proxy
->args
, git_proxy_command
);
822 argv_array_push(&proxy
->args
, host
);
823 argv_array_push(&proxy
->args
, port
);
826 if (start_command(proxy
))
827 die("cannot start proxy %s", git_proxy_command
);
828 fd
[0] = proxy
->out
; /* read from proxy stdout */
829 fd
[1] = proxy
->in
; /* write to proxy stdin */
833 static char *get_port(char *host
)
836 char *p
= strchr(host
, ':');
839 long port
= strtol(p
+ 1, &end
, 10);
840 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
850 * Extract protocol and relevant parts from the specified connection URL.
851 * The caller must free() the returned strings.
853 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
860 enum protocol protocol
= PROTO_LOCAL
;
862 if (is_url(url_orig
))
863 url
= url_decode(url_orig
);
865 url
= xstrdup(url_orig
);
867 host
= strstr(url
, "://");
870 protocol
= get_protocol(url
);
874 if (!url_is_local_not_ssh(url
)) {
875 protocol
= PROTO_SSH
;
881 * Don't do destructive transforms as protocol code does
882 * '[]' unwrapping in get_host_and_port()
884 end
= host_end(&host
, 0);
886 if (protocol
== PROTO_LOCAL
)
888 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
889 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
891 path
= strchr(end
, separator
);
894 die("No path specified. See 'man git-pull' for valid url syntax");
897 * null-terminate hostname and point path to ~ for URL's like this:
898 * ssh://host.xz/~user/repo
901 end
= path
; /* Need to \0 terminate host here */
902 if (separator
== ':')
903 path
++; /* path starts after ':' */
904 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
909 path
= xstrdup(path
);
912 *ret_host
= xstrdup(host
);
918 static const char *get_ssh_command(void)
922 if ((ssh
= getenv("GIT_SSH_COMMAND")))
925 if (!git_config_get_string_const("core.sshcommand", &ssh
))
937 VARIANT_TORTOISEPLINK
,
940 static void override_ssh_variant(enum ssh_variant
*ssh_variant
)
942 const char *variant
= getenv("GIT_SSH_VARIANT");
944 if (!variant
&& git_config_get_string_const("ssh.variant", &variant
))
947 if (!strcmp(variant
, "auto"))
948 *ssh_variant
= VARIANT_AUTO
;
949 else if (!strcmp(variant
, "plink"))
950 *ssh_variant
= VARIANT_PLINK
;
951 else if (!strcmp(variant
, "putty"))
952 *ssh_variant
= VARIANT_PUTTY
;
953 else if (!strcmp(variant
, "tortoiseplink"))
954 *ssh_variant
= VARIANT_TORTOISEPLINK
;
955 else if (!strcmp(variant
, "simple"))
956 *ssh_variant
= VARIANT_SIMPLE
;
958 *ssh_variant
= VARIANT_SSH
;
961 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
964 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
968 override_ssh_variant(&ssh_variant
);
970 if (ssh_variant
!= VARIANT_AUTO
)
974 p
= xstrdup(ssh_command
);
975 variant
= basename(p
);
977 const char **ssh_argv
;
979 p
= xstrdup(ssh_command
);
980 if (split_cmdline(p
, &ssh_argv
) > 0) {
981 variant
= basename((char *)ssh_argv
[0]);
983 * At this point, variant points into the buffer
984 * referenced by p, hence we do not need ssh_argv
994 if (!strcasecmp(variant
, "ssh") ||
995 !strcasecmp(variant
, "ssh.exe"))
996 ssh_variant
= VARIANT_SSH
;
997 else if (!strcasecmp(variant
, "plink") ||
998 !strcasecmp(variant
, "plink.exe"))
999 ssh_variant
= VARIANT_PLINK
;
1000 else if (!strcasecmp(variant
, "tortoiseplink") ||
1001 !strcasecmp(variant
, "tortoiseplink.exe"))
1002 ssh_variant
= VARIANT_TORTOISEPLINK
;
1009 * Open a connection using Git's native protocol.
1011 * The caller is responsible for freeing hostandport, but this function may
1012 * modify it (for example, to truncate it to remove the port part).
1014 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
1015 const char *path
, const char *prog
,
1018 struct child_process
*conn
;
1019 struct strbuf request
= STRBUF_INIT
;
1021 * Set up virtual host information based on where we will
1022 * connect, unless the user has overridden us in
1025 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1027 target_host
= xstrdup(target_host
);
1029 target_host
= xstrdup(hostandport
);
1031 transport_check_allowed("git");
1034 * These underlying connection commands die() if they
1037 if (git_use_proxy(hostandport
))
1038 conn
= git_proxy_connect(fd
, hostandport
);
1040 conn
= git_tcp_connect(fd
, hostandport
, flags
);
1042 * Separate original protocol components prog and path
1043 * from extended host header with a NUL byte.
1045 * Note: Do not add any other headers here! Doing so
1046 * will cause older git-daemon servers to crash.
1048 strbuf_addf(&request
,
1053 /* If using a new version put that stuff here after a second null byte */
1054 if (get_protocol_version_config() > 0) {
1055 strbuf_addch(&request
, '\0');
1056 strbuf_addf(&request
, "version=%d%c",
1057 get_protocol_version_config(), '\0');
1060 packet_write(fd
[1], request
.buf
, request
.len
);
1063 strbuf_release(&request
);
1068 * Append the appropriate environment variables to `env` and options to
1069 * `args` for running ssh in Git's SSH-tunneled transport.
1071 static void push_ssh_options(struct argv_array
*args
, struct argv_array
*env
,
1072 enum ssh_variant variant
, const char *port
,
1075 if (variant
== VARIANT_SSH
&&
1076 get_protocol_version_config() > 0) {
1077 argv_array_push(args
, "-o");
1078 argv_array_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
1079 argv_array_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1080 get_protocol_version_config());
1083 if (flags
& CONNECT_IPV4
) {
1086 BUG("VARIANT_AUTO passed to push_ssh_options");
1087 case VARIANT_SIMPLE
:
1088 die("ssh variant 'simple' does not support -4");
1092 case VARIANT_TORTOISEPLINK
:
1093 argv_array_push(args
, "-4");
1095 } else if (flags
& CONNECT_IPV6
) {
1098 BUG("VARIANT_AUTO passed to push_ssh_options");
1099 case VARIANT_SIMPLE
:
1100 die("ssh variant 'simple' does not support -6");
1104 case VARIANT_TORTOISEPLINK
:
1105 argv_array_push(args
, "-6");
1109 if (variant
== VARIANT_TORTOISEPLINK
)
1110 argv_array_push(args
, "-batch");
1115 BUG("VARIANT_AUTO passed to push_ssh_options");
1116 case VARIANT_SIMPLE
:
1117 die("ssh variant 'simple' does not support setting port");
1119 argv_array_push(args
, "-p");
1123 case VARIANT_TORTOISEPLINK
:
1124 argv_array_push(args
, "-P");
1127 argv_array_push(args
, port
);
1131 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1132 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
1133 const char *port
, int flags
)
1136 enum ssh_variant variant
;
1138 if (looks_like_command_line_option(ssh_host
))
1139 die("strange hostname '%s' blocked", ssh_host
);
1141 ssh
= get_ssh_command();
1143 variant
= determine_ssh_variant(ssh
, 1);
1146 * GIT_SSH is the no-shell version of
1147 * GIT_SSH_COMMAND (and must remain so for
1148 * historical compatibility).
1150 conn
->use_shell
= 0;
1152 ssh
= getenv("GIT_SSH");
1155 variant
= determine_ssh_variant(ssh
, 0);
1158 if (variant
== VARIANT_AUTO
) {
1159 struct child_process detect
= CHILD_PROCESS_INIT
;
1161 detect
.use_shell
= conn
->use_shell
;
1162 detect
.no_stdin
= detect
.no_stdout
= detect
.no_stderr
= 1;
1164 argv_array_push(&detect
.args
, ssh
);
1165 argv_array_push(&detect
.args
, "-G");
1166 push_ssh_options(&detect
.args
, &detect
.env_array
,
1167 VARIANT_SSH
, port
, flags
);
1168 argv_array_push(&detect
.args
, ssh_host
);
1170 variant
= run_command(&detect
) ? VARIANT_SIMPLE
: VARIANT_SSH
;
1173 argv_array_push(&conn
->args
, ssh
);
1174 push_ssh_options(&conn
->args
, &conn
->env_array
, variant
, port
, flags
);
1175 argv_array_push(&conn
->args
, ssh_host
);
1179 * This returns the dummy child_process `no_fork` if the transport protocol
1180 * does not need fork(2), or a struct child_process object if it does. Once
1181 * done, finish the connection with finish_connect() with the value returned
1182 * from this function (it is safe to call finish_connect() with NULL to
1183 * support the former case).
1185 * If it returns, the connect is successful; it just dies on errors (this
1186 * will hopefully be changed in a libification effort, to return NULL when
1187 * the connection failed).
1189 struct child_process
*git_connect(int fd
[2], const char *url
,
1190 const char *prog
, int flags
)
1192 char *hostandport
, *path
;
1193 struct child_process
*conn
;
1194 enum protocol protocol
;
1196 /* Without this we cannot rely on waitpid() to tell
1197 * what happened to our children.
1199 signal(SIGCHLD
, SIG_DFL
);
1201 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1202 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1203 printf("Diag: url=%s\n", url
? url
: "NULL");
1204 printf("Diag: protocol=%s\n", prot_name(protocol
));
1205 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1206 printf("Diag: path=%s\n", path
? path
: "NULL");
1208 } else if (protocol
== PROTO_GIT
) {
1209 conn
= git_connect_git(fd
, hostandport
, path
, prog
, flags
);
1211 struct strbuf cmd
= STRBUF_INIT
;
1212 const char *const *var
;
1214 conn
= xmalloc(sizeof(*conn
));
1215 child_process_init(conn
);
1217 if (looks_like_command_line_option(path
))
1218 die("strange pathname '%s' blocked", path
);
1220 strbuf_addstr(&cmd
, prog
);
1221 strbuf_addch(&cmd
, ' ');
1222 sq_quote_buf(&cmd
, path
);
1224 /* remove repo-local variables from the environment */
1225 for (var
= local_repo_env
; *var
; var
++)
1226 argv_array_push(&conn
->env_array
, *var
);
1228 conn
->use_shell
= 1;
1229 conn
->in
= conn
->out
= -1;
1230 if (protocol
== PROTO_SSH
) {
1231 char *ssh_host
= hostandport
;
1232 const char *port
= NULL
;
1233 transport_check_allowed("ssh");
1234 get_host_and_port(&ssh_host
, &port
);
1237 port
= get_port(ssh_host
);
1239 if (flags
& CONNECT_DIAG_URL
) {
1240 printf("Diag: url=%s\n", url
? url
: "NULL");
1241 printf("Diag: protocol=%s\n", prot_name(protocol
));
1242 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1243 printf("Diag: port=%s\n", port
? port
: "NONE");
1244 printf("Diag: path=%s\n", path
? path
: "NULL");
1249 strbuf_release(&cmd
);
1252 fill_ssh_args(conn
, ssh_host
, port
, flags
);
1254 transport_check_allowed("file");
1255 if (get_protocol_version_config() > 0) {
1256 argv_array_pushf(&conn
->env_array
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1257 get_protocol_version_config());
1260 argv_array_push(&conn
->args
, cmd
.buf
);
1262 if (start_command(conn
))
1263 die("unable to fork");
1265 fd
[0] = conn
->out
; /* read from child's stdout */
1266 fd
[1] = conn
->in
; /* write to child's stdin */
1267 strbuf_release(&cmd
);
1274 int finish_connect(struct child_process
*conn
)
1277 if (!conn
|| git_connection_is_socket(conn
))
1280 code
= finish_command(conn
);