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"
16 static char *server_capabilities
;
17 static const char *parse_feature_value(const char *, const char *, int *);
19 static int check_ref(const char *name
, unsigned int flags
)
24 if (!skip_prefix(name
, "refs/", &name
))
27 /* REF_NORMAL means that we don't want the magic fake tag refs */
28 if ((flags
& REF_NORMAL
) && check_refname_format(name
, 0))
31 /* REF_HEADS means that we want regular branch heads */
32 if ((flags
& REF_HEADS
) && starts_with(name
, "heads/"))
35 /* REF_TAGS means that we want tags */
36 if ((flags
& REF_TAGS
) && starts_with(name
, "tags/"))
39 /* All type bits clear means that we are ok with anything */
40 return !(flags
& ~REF_NORMAL
);
43 int check_ref_type(const struct ref
*ref
, int flags
)
45 return check_ref(ref
->name
, flags
);
48 static void die_initial_contact(int unexpected
)
51 die(_("The remote end hung up upon initial contact"));
53 die(_("Could not read from remote repository.\n\n"
54 "Please make sure you have the correct access rights\n"
55 "and the repository exists."));
58 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
61 struct string_list_item
*item
;
64 return; /* just "symref" */
65 /* e.g. "symref=HEAD:refs/heads/master" */
66 sym
= xmemdupz(val
, len
);
67 target
= strchr(sym
, ':');
69 /* just "symref=something" */
72 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
73 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
74 /* "symref=bogus:pair */
76 item
= string_list_append_nodup(symref
, sym
);
84 static void annotate_refs_with_symref_info(struct ref
*ref
)
86 struct string_list symref
= STRING_LIST_INIT_DUP
;
87 const char *feature_list
= server_capabilities
;
89 while (feature_list
) {
93 val
= parse_feature_value(feature_list
, "symref", &len
);
96 parse_one_symref_info(&symref
, val
, len
);
97 feature_list
= val
+ 1;
99 string_list_sort(&symref
);
101 for (; ref
; ref
= ref
->next
) {
102 struct string_list_item
*item
;
103 item
= string_list_lookup(&symref
, ref
->name
);
106 ref
->symref
= xstrdup((char *)item
->util
);
108 string_list_clear(&symref
, 0);
112 * Read one line of a server's ref advertisement into packet_buffer.
114 static int read_remote_ref(int in
, char **src_buf
, size_t *src_len
,
117 int len
= packet_read(in
, src_buf
, src_len
,
118 packet_buffer
, sizeof(packet_buffer
),
119 PACKET_READ_GENTLE_ON_EOF
|
120 PACKET_READ_CHOMP_NEWLINE
);
123 die_initial_contact(*responded
);
124 if (len
> 4 && skip_prefix(packet_buffer
, "ERR ", &arg
))
125 die("remote error: %s", arg
);
132 #define EXPECTING_FIRST_REF 0
133 #define EXPECTING_REF 1
134 #define EXPECTING_SHALLOW 2
136 static void process_capabilities(int *len
)
138 int nul_location
= strlen(packet_buffer
);
139 if (nul_location
== *len
)
141 server_capabilities
= xstrdup(packet_buffer
+ nul_location
+ 1);
145 static int process_dummy_ref(void)
147 struct object_id oid
;
150 if (parse_oid_hex(packet_buffer
, &oid
, &name
))
156 return !oidcmp(&null_oid
, &oid
) && !strcmp(name
, "capabilities^{}");
159 static void check_no_capabilities(int len
)
161 if (strlen(packet_buffer
) != len
)
162 warning("Ignoring capabilities after first line '%s'",
163 packet_buffer
+ strlen(packet_buffer
));
166 static int process_ref(int len
, struct ref
***list
, unsigned int flags
,
167 struct oid_array
*extra_have
)
169 struct object_id old_oid
;
172 if (parse_oid_hex(packet_buffer
, &old_oid
, &name
))
178 if (extra_have
&& !strcmp(name
, ".have")) {
179 oid_array_append(extra_have
, &old_oid
);
180 } else if (!strcmp(name
, "capabilities^{}")) {
181 die("protocol error: unexpected capabilities^{}");
182 } else if (check_ref(name
, flags
)) {
183 struct ref
*ref
= alloc_ref(name
);
184 oidcpy(&ref
->old_oid
, &old_oid
);
188 check_no_capabilities(len
);
192 static int process_shallow(int len
, struct oid_array
*shallow_points
)
195 struct object_id old_oid
;
197 if (!skip_prefix(packet_buffer
, "shallow ", &arg
))
200 if (get_oid_hex(arg
, &old_oid
))
201 die("protocol error: expected shallow sha-1, got '%s'", arg
);
203 die("repository on the other end cannot be shallow");
204 oid_array_append(shallow_points
, &old_oid
);
205 check_no_capabilities(len
);
210 * Read all the refs from the other end
212 struct ref
**get_remote_heads(int in
, char *src_buf
, size_t src_len
,
213 struct ref
**list
, unsigned int flags
,
214 struct oid_array
*extra_have
,
215 struct oid_array
*shallow_points
)
217 struct ref
**orig_list
= list
;
220 * A hang-up after seeing some response from the other end
221 * means that it is unexpected, as we know the other end is
222 * willing to talk to us. A hang-up before seeing any
223 * response does not necessarily mean an ACL problem, though.
227 int state
= EXPECTING_FIRST_REF
;
231 while ((len
= read_remote_ref(in
, &src_buf
, &src_len
, &responded
))) {
233 case EXPECTING_FIRST_REF
:
234 process_capabilities(&len
);
235 if (process_dummy_ref()) {
236 state
= EXPECTING_SHALLOW
;
239 state
= EXPECTING_REF
;
242 if (process_ref(len
, &list
, flags
, extra_have
))
244 state
= EXPECTING_SHALLOW
;
246 case EXPECTING_SHALLOW
:
247 if (process_shallow(len
, shallow_points
))
249 die("protocol error: unexpected '%s'", packet_buffer
);
251 die("unexpected state %d", state
);
255 annotate_refs_with_symref_info(*orig_list
);
260 static const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
)
267 len
= strlen(feature
);
268 while (*feature_list
) {
269 const char *found
= strstr(feature_list
, feature
);
272 if (feature_list
== found
|| isspace(found
[-1])) {
273 const char *value
= found
+ len
;
274 /* feature with no value (e.g., "thin-pack") */
275 if (!*value
|| isspace(*value
)) {
280 /* feature with a value (e.g., "agent=git/1.2.3") */
281 else if (*value
== '=') {
284 *lenp
= strcspn(value
, " \t\n");
288 * otherwise we matched a substring of another feature;
292 feature_list
= found
+ 1;
297 int parse_feature_request(const char *feature_list
, const char *feature
)
299 return !!parse_feature_value(feature_list
, feature
, NULL
);
302 const char *server_feature_value(const char *feature
, int *len
)
304 return parse_feature_value(server_capabilities
, feature
, len
);
307 int server_supports(const char *feature
)
309 return !!server_feature_value(feature
, NULL
);
319 int url_is_local_not_ssh(const char *url
)
321 const char *colon
= strchr(url
, ':');
322 const char *slash
= strchr(url
, '/');
323 return !colon
|| (slash
&& slash
< colon
) ||
324 has_dos_drive_prefix(url
);
327 static const char *prot_name(enum protocol protocol
)
338 return "unknown protocol";
342 static enum protocol
get_protocol(const char *name
)
344 if (!strcmp(name
, "ssh"))
346 if (!strcmp(name
, "git"))
348 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
350 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
352 if (!strcmp(name
, "file"))
354 die("I don't handle protocol '%s'", name
);
357 static char *host_end(char **hoststart
, int removebrackets
)
359 char *host
= *hoststart
;
361 char *start
= strstr(host
, "@[");
363 start
++; /* Jump over '@' */
366 if (start
[0] == '[') {
367 end
= strchr(start
+ 1, ']');
369 if (removebrackets
) {
371 memmove(start
, start
+ 1, end
- start
);
382 #define STR(s) STR_(s)
384 static void get_host_and_port(char **host
, const char **port
)
387 end
= host_end(host
, 1);
388 colon
= strchr(end
, ':');
390 long portnr
= strtol(colon
+ 1, &end
, 10);
391 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
394 } else if (!colon
[1]) {
400 static void enable_keepalive(int sockfd
)
404 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
405 fprintf(stderr
, "unable to set SO_KEEPALIVE on socket: %s\n",
411 static const char *ai_name(const struct addrinfo
*ai
)
413 static char addr
[NI_MAXHOST
];
414 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
415 NI_NUMERICHOST
) != 0)
416 xsnprintf(addr
, sizeof(addr
), "(unknown)");
422 * Returns a connected socket() fd, or else die()s.
424 static int git_tcp_connect_sock(char *host
, int flags
)
426 struct strbuf error_message
= STRBUF_INIT
;
428 const char *port
= STR(DEFAULT_GIT_PORT
);
429 struct addrinfo hints
, *ai0
, *ai
;
433 get_host_and_port(&host
, &port
);
437 memset(&hints
, 0, sizeof(hints
));
438 if (flags
& CONNECT_IPV4
)
439 hints
.ai_family
= AF_INET
;
440 else if (flags
& CONNECT_IPV6
)
441 hints
.ai_family
= AF_INET6
;
442 hints
.ai_socktype
= SOCK_STREAM
;
443 hints
.ai_protocol
= IPPROTO_TCP
;
445 if (flags
& CONNECT_VERBOSE
)
446 fprintf(stderr
, "Looking up %s ... ", host
);
448 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
450 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
452 if (flags
& CONNECT_VERBOSE
)
453 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
455 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
456 sockfd
= socket(ai
->ai_family
,
457 ai
->ai_socktype
, ai
->ai_protocol
);
459 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
460 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
461 host
, cnt
, ai_name(ai
), strerror(errno
));
467 if (flags
& CONNECT_VERBOSE
)
468 fprintf(stderr
, "%s ", ai_name(ai
));
475 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
477 enable_keepalive(sockfd
);
479 if (flags
& CONNECT_VERBOSE
)
480 fprintf(stderr
, "done.\n");
482 strbuf_release(&error_message
);
490 * Returns a connected socket() fd, or else die()s.
492 static int git_tcp_connect_sock(char *host
, int flags
)
494 struct strbuf error_message
= STRBUF_INIT
;
496 const char *port
= STR(DEFAULT_GIT_PORT
);
499 struct sockaddr_in sa
;
504 get_host_and_port(&host
, &port
);
506 if (flags
& CONNECT_VERBOSE
)
507 fprintf(stderr
, "Looking up %s ... ", host
);
509 he
= gethostbyname(host
);
511 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
512 nport
= strtoul(port
, &ep
, 10);
513 if ( ep
== port
|| *ep
) {
515 struct servent
*se
= getservbyname(port
,"tcp");
517 die("Unknown port %s", port
);
521 if (flags
& CONNECT_VERBOSE
)
522 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
524 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
525 memset(&sa
, 0, sizeof sa
);
526 sa
.sin_family
= he
->h_addrtype
;
527 sa
.sin_port
= htons(nport
);
528 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
530 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
532 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
533 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
536 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
543 if (flags
& CONNECT_VERBOSE
)
544 fprintf(stderr
, "%s ",
545 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
550 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
552 enable_keepalive(sockfd
);
554 if (flags
& CONNECT_VERBOSE
)
555 fprintf(stderr
, "done.\n");
563 static void git_tcp_connect(int fd
[2], char *host
, int flags
)
565 int sockfd
= git_tcp_connect_sock(host
, flags
);
572 static char *git_proxy_command
;
574 static int git_proxy_command_options(const char *var
, const char *value
,
577 if (!strcmp(var
, "core.gitproxy")) {
581 const char *rhost_name
= cb
;
582 int rhost_len
= strlen(rhost_name
);
584 if (git_proxy_command
)
587 return config_error_nonbool(var
);
589 * ;# matches www.kernel.org as well
590 * gitproxy = netcatter-1 for kernel.org
591 * gitproxy = netcatter-2 for sample.xz
592 * gitproxy = netcatter-default
594 for_pos
= strstr(value
, " for ");
596 /* matches everybody */
597 matchlen
= strlen(value
);
599 hostlen
= strlen(for_pos
+ 5);
600 if (rhost_len
< hostlen
)
602 else if (!strncmp(for_pos
+ 5,
603 rhost_name
+ rhost_len
- hostlen
,
605 ((rhost_len
== hostlen
) ||
606 rhost_name
[rhost_len
- hostlen
-1] == '.'))
607 matchlen
= for_pos
- value
;
612 /* core.gitproxy = none for kernel.org */
614 !memcmp(value
, "none", 4))
616 git_proxy_command
= xmemdupz(value
, matchlen
);
621 return git_default_config(var
, value
, cb
);
624 static int git_use_proxy(const char *host
)
626 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
627 git_config(git_proxy_command_options
, (void*)host
);
628 return (git_proxy_command
&& *git_proxy_command
);
631 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
633 const char *port
= STR(DEFAULT_GIT_PORT
);
634 struct child_process
*proxy
;
636 get_host_and_port(&host
, &port
);
638 if (looks_like_command_line_option(host
))
639 die("strange hostname '%s' blocked", host
);
640 if (looks_like_command_line_option(port
))
641 die("strange port '%s' blocked", port
);
643 proxy
= xmalloc(sizeof(*proxy
));
644 child_process_init(proxy
);
645 argv_array_push(&proxy
->args
, git_proxy_command
);
646 argv_array_push(&proxy
->args
, host
);
647 argv_array_push(&proxy
->args
, port
);
650 if (start_command(proxy
))
651 die("cannot start proxy %s", git_proxy_command
);
652 fd
[0] = proxy
->out
; /* read from proxy stdout */
653 fd
[1] = proxy
->in
; /* write to proxy stdin */
657 static char *get_port(char *host
)
660 char *p
= strchr(host
, ':');
663 long port
= strtol(p
+ 1, &end
, 10);
664 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
674 * Extract protocol and relevant parts from the specified connection URL.
675 * The caller must free() the returned strings.
677 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
684 enum protocol protocol
= PROTO_LOCAL
;
686 if (is_url(url_orig
))
687 url
= url_decode(url_orig
);
689 url
= xstrdup(url_orig
);
691 host
= strstr(url
, "://");
694 protocol
= get_protocol(url
);
698 if (!url_is_local_not_ssh(url
)) {
699 protocol
= PROTO_SSH
;
705 * Don't do destructive transforms as protocol code does
706 * '[]' unwrapping in get_host_and_port()
708 end
= host_end(&host
, 0);
710 if (protocol
== PROTO_LOCAL
)
712 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
713 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
715 path
= strchr(end
, separator
);
718 die("No path specified. See 'man git-pull' for valid url syntax");
721 * null-terminate hostname and point path to ~ for URL's like this:
722 * ssh://host.xz/~user/repo
725 end
= path
; /* Need to \0 terminate host here */
726 if (separator
== ':')
727 path
++; /* path starts after ':' */
728 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
733 path
= xstrdup(path
);
736 *ret_host
= xstrdup(host
);
742 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
744 static const char *get_ssh_command(void)
748 if ((ssh
= getenv("GIT_SSH_COMMAND")))
751 if (!git_config_get_string_const("core.sshcommand", &ssh
))
757 static int override_ssh_variant(int *port_option
, int *needs_batch
)
761 variant
= xstrdup_or_null(getenv("GIT_SSH_VARIANT"));
763 git_config_get_string("ssh.variant", &variant
))
766 if (!strcmp(variant
, "plink") || !strcmp(variant
, "putty")) {
769 } else if (!strcmp(variant
, "tortoiseplink")) {
780 static void handle_ssh_variant(const char *ssh_command
, int is_cmdline
,
781 int *port_option
, int *needs_batch
)
786 if (override_ssh_variant(port_option
, needs_batch
))
790 p
= xstrdup(ssh_command
);
791 variant
= basename(p
);
793 const char **ssh_argv
;
795 p
= xstrdup(ssh_command
);
796 if (split_cmdline(p
, &ssh_argv
) > 0) {
797 variant
= basename((char *)ssh_argv
[0]);
799 * At this point, variant points into the buffer
800 * referenced by p, hence we do not need ssh_argv
810 if (!strcasecmp(variant
, "plink") ||
811 !strcasecmp(variant
, "plink.exe"))
813 else if (!strcasecmp(variant
, "tortoiseplink") ||
814 !strcasecmp(variant
, "tortoiseplink.exe")) {
822 * This returns a dummy child_process if the transport protocol does not
823 * need fork(2), or a struct child_process object if it does. Once done,
824 * finish the connection with finish_connect() with the value returned from
825 * this function (it is safe to call finish_connect() with NULL to support
828 * If it returns, the connect is successful; it just dies on errors (this
829 * will hopefully be changed in a libification effort, to return NULL when
830 * the connection failed).
832 struct child_process
*git_connect(int fd
[2], const char *url
,
833 const char *prog
, int flags
)
835 char *hostandport
, *path
;
836 struct child_process
*conn
= &no_fork
;
837 enum protocol protocol
;
839 /* Without this we cannot rely on waitpid() to tell
840 * what happened to our children.
842 signal(SIGCHLD
, SIG_DFL
);
844 protocol
= parse_connect_url(url
, &hostandport
, &path
);
845 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
846 printf("Diag: url=%s\n", url
? url
: "NULL");
847 printf("Diag: protocol=%s\n", prot_name(protocol
));
848 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
849 printf("Diag: path=%s\n", path
? path
: "NULL");
851 } else if (protocol
== PROTO_GIT
) {
853 * Set up virtual host information based on where we will
854 * connect, unless the user has overridden us in
857 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
859 target_host
= xstrdup(target_host
);
861 target_host
= xstrdup(hostandport
);
863 transport_check_allowed("git");
865 /* These underlying connection commands die() if they
868 if (git_use_proxy(hostandport
))
869 conn
= git_proxy_connect(fd
, hostandport
);
871 git_tcp_connect(fd
, hostandport
, flags
);
873 * Separate original protocol components prog and path
874 * from extended host header with a NUL byte.
876 * Note: Do not add any other headers here! Doing so
877 * will cause older git-daemon servers to crash.
879 packet_write_fmt(fd
[1],
885 struct strbuf cmd
= STRBUF_INIT
;
887 conn
= xmalloc(sizeof(*conn
));
888 child_process_init(conn
);
890 if (looks_like_command_line_option(path
))
891 die("strange pathname '%s' blocked", path
);
893 strbuf_addstr(&cmd
, prog
);
894 strbuf_addch(&cmd
, ' ');
895 sq_quote_buf(&cmd
, path
);
897 /* remove repo-local variables from the environment */
898 conn
->env
= local_repo_env
;
900 conn
->in
= conn
->out
= -1;
901 if (protocol
== PROTO_SSH
) {
904 int port_option
= 'p';
905 char *ssh_host
= hostandport
;
906 const char *port
= NULL
;
907 transport_check_allowed("ssh");
908 get_host_and_port(&ssh_host
, &port
);
911 port
= get_port(ssh_host
);
913 if (flags
& CONNECT_DIAG_URL
) {
914 printf("Diag: url=%s\n", url
? url
: "NULL");
915 printf("Diag: protocol=%s\n", prot_name(protocol
));
916 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
917 printf("Diag: port=%s\n", port
? port
: "NONE");
918 printf("Diag: path=%s\n", path
? path
: "NULL");
923 strbuf_release(&cmd
);
927 if (looks_like_command_line_option(ssh_host
))
928 die("strange hostname '%s' blocked", ssh_host
);
930 ssh
= get_ssh_command();
932 handle_ssh_variant(ssh
, 1, &port_option
,
936 * GIT_SSH is the no-shell version of
937 * GIT_SSH_COMMAND (and must remain so for
938 * historical compatibility).
942 ssh
= getenv("GIT_SSH");
946 handle_ssh_variant(ssh
, 0,
951 argv_array_push(&conn
->args
, ssh
);
952 if (flags
& CONNECT_IPV4
)
953 argv_array_push(&conn
->args
, "-4");
954 else if (flags
& CONNECT_IPV6
)
955 argv_array_push(&conn
->args
, "-6");
957 argv_array_push(&conn
->args
, "-batch");
959 argv_array_pushf(&conn
->args
,
961 argv_array_push(&conn
->args
, port
);
963 argv_array_push(&conn
->args
, ssh_host
);
965 transport_check_allowed("file");
967 argv_array_push(&conn
->args
, cmd
.buf
);
969 if (start_command(conn
))
970 die("unable to fork");
972 fd
[0] = conn
->out
; /* read from child's stdout */
973 fd
[1] = conn
->in
; /* write to child's stdin */
974 strbuf_release(&cmd
);
981 int git_connection_is_socket(struct child_process
*conn
)
983 return conn
== &no_fork
;
986 int finish_connect(struct child_process
*conn
)
989 if (!conn
|| git_connection_is_socket(conn
))
992 code
= finish_command(conn
);