builtin/remote.c: reorder function definitions
[git/jrn.git] / connect.c
blob40868610ab1c02d10cc7cdc927fdf97a5e010414
1 #include "git-compat-util.h"
2 #include "cache.h"
3 #include "pkt-line.h"
4 #include "quote.h"
5 #include "refs.h"
6 #include "run-command.h"
7 #include "remote.h"
8 #include "connect.h"
9 #include "url.h"
11 static char *server_capabilities;
13 static int check_ref(const char *name, int len, unsigned int flags)
15 if (!flags)
16 return 1;
18 if (len < 5 || memcmp(name, "refs/", 5))
19 return 0;
21 /* Skip the "refs/" part */
22 name += 5;
23 len -= 5;
25 /* REF_NORMAL means that we don't want the magic fake tag refs */
26 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
27 return 0;
29 /* REF_HEADS means that we want regular branch heads */
30 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
31 return 1;
33 /* REF_TAGS means that we want tags */
34 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
35 return 1;
37 /* All type bits clear means that we are ok with anything */
38 return !(flags & ~REF_NORMAL);
41 int check_ref_type(const struct ref *ref, int flags)
43 return check_ref(ref->name, strlen(ref->name), flags);
46 static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1)
48 ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc);
49 hashcpy(&(extra->array[extra->nr][0]), sha1);
50 extra->nr++;
53 static void die_initial_contact(int got_at_least_one_head)
55 if (got_at_least_one_head)
56 die("The remote end hung up upon initial contact");
57 else
58 die("Could not read from remote repository.\n\n"
59 "Please make sure you have the correct access rights\n"
60 "and the repository exists.");
64 * Read all the refs from the other end
66 struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
67 struct ref **list, unsigned int flags,
68 struct extra_have_objects *extra_have)
70 int got_at_least_one_head = 0;
72 *list = NULL;
73 for (;;) {
74 struct ref *ref;
75 unsigned char old_sha1[20];
76 char *name;
77 int len, name_len;
78 char *buffer = packet_buffer;
80 len = packet_read(in, &src_buf, &src_len,
81 packet_buffer, sizeof(packet_buffer),
82 PACKET_READ_GENTLE_ON_EOF |
83 PACKET_READ_CHOMP_NEWLINE);
84 if (len < 0)
85 die_initial_contact(got_at_least_one_head);
87 if (!len)
88 break;
90 if (len > 4 && !prefixcmp(buffer, "ERR "))
91 die("remote error: %s", buffer + 4);
93 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
94 die("protocol error: expected sha/ref, got '%s'", buffer);
95 name = buffer + 41;
97 name_len = strlen(name);
98 if (len != name_len + 41) {
99 free(server_capabilities);
100 server_capabilities = xstrdup(name + name_len + 1);
103 if (extra_have &&
104 name_len == 5 && !memcmp(".have", name, 5)) {
105 add_extra_have(extra_have, old_sha1);
106 continue;
109 if (!check_ref(name, name_len, flags))
110 continue;
111 ref = alloc_ref(buffer + 41);
112 hashcpy(ref->old_sha1, old_sha1);
113 *list = ref;
114 list = &ref->next;
115 got_at_least_one_head = 1;
117 return list;
120 const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
122 int len;
124 if (!feature_list)
125 return NULL;
127 len = strlen(feature);
128 while (*feature_list) {
129 const char *found = strstr(feature_list, feature);
130 if (!found)
131 return NULL;
132 if (feature_list == found || isspace(found[-1])) {
133 const char *value = found + len;
134 /* feature with no value (e.g., "thin-pack") */
135 if (!*value || isspace(*value)) {
136 if (lenp)
137 *lenp = 0;
138 return value;
140 /* feature with a value (e.g., "agent=git/1.2.3") */
141 else if (*value == '=') {
142 value++;
143 if (lenp)
144 *lenp = strcspn(value, " \t\n");
145 return value;
148 * otherwise we matched a substring of another feature;
149 * keep looking
152 feature_list = found + 1;
154 return NULL;
157 int parse_feature_request(const char *feature_list, const char *feature)
159 return !!parse_feature_value(feature_list, feature, NULL);
162 const char *server_feature_value(const char *feature, int *len)
164 return parse_feature_value(server_capabilities, feature, len);
167 int server_supports(const char *feature)
169 return !!server_feature_value(feature, NULL);
172 enum protocol {
173 PROTO_LOCAL = 1,
174 PROTO_SSH,
175 PROTO_GIT
178 static enum protocol get_protocol(const char *name)
180 if (!strcmp(name, "ssh"))
181 return PROTO_SSH;
182 if (!strcmp(name, "git"))
183 return PROTO_GIT;
184 if (!strcmp(name, "git+ssh"))
185 return PROTO_SSH;
186 if (!strcmp(name, "ssh+git"))
187 return PROTO_SSH;
188 if (!strcmp(name, "file"))
189 return PROTO_LOCAL;
190 die("I don't handle protocol '%s'", name);
193 #define STR_(s) # s
194 #define STR(s) STR_(s)
196 static void get_host_and_port(char **host, const char **port)
198 char *colon, *end;
200 if (*host[0] == '[') {
201 end = strchr(*host + 1, ']');
202 if (end) {
203 *end = 0;
204 end++;
205 (*host)++;
206 } else
207 end = *host;
208 } else
209 end = *host;
210 colon = strchr(end, ':');
212 if (colon) {
213 *colon = 0;
214 *port = colon + 1;
218 static void enable_keepalive(int sockfd)
220 int ka = 1;
222 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
223 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
224 strerror(errno));
227 #ifndef NO_IPV6
229 static const char *ai_name(const struct addrinfo *ai)
231 static char addr[NI_MAXHOST];
232 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
233 NI_NUMERICHOST) != 0)
234 strcpy(addr, "(unknown)");
236 return addr;
240 * Returns a connected socket() fd, or else die()s.
242 static int git_tcp_connect_sock(char *host, int flags)
244 struct strbuf error_message = STRBUF_INIT;
245 int sockfd = -1;
246 const char *port = STR(DEFAULT_GIT_PORT);
247 struct addrinfo hints, *ai0, *ai;
248 int gai;
249 int cnt = 0;
251 get_host_and_port(&host, &port);
252 if (!*port)
253 port = "<none>";
255 memset(&hints, 0, sizeof(hints));
256 hints.ai_socktype = SOCK_STREAM;
257 hints.ai_protocol = IPPROTO_TCP;
259 if (flags & CONNECT_VERBOSE)
260 fprintf(stderr, "Looking up %s ... ", host);
262 gai = getaddrinfo(host, port, &hints, &ai);
263 if (gai)
264 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
266 if (flags & CONNECT_VERBOSE)
267 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
269 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
270 sockfd = socket(ai->ai_family,
271 ai->ai_socktype, ai->ai_protocol);
272 if ((sockfd < 0) ||
273 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
274 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
275 host, cnt, ai_name(ai), strerror(errno));
276 if (0 <= sockfd)
277 close(sockfd);
278 sockfd = -1;
279 continue;
281 if (flags & CONNECT_VERBOSE)
282 fprintf(stderr, "%s ", ai_name(ai));
283 break;
286 freeaddrinfo(ai0);
288 if (sockfd < 0)
289 die("unable to connect to %s:\n%s", host, error_message.buf);
291 enable_keepalive(sockfd);
293 if (flags & CONNECT_VERBOSE)
294 fprintf(stderr, "done.\n");
296 strbuf_release(&error_message);
298 return sockfd;
301 #else /* NO_IPV6 */
304 * Returns a connected socket() fd, or else die()s.
306 static int git_tcp_connect_sock(char *host, int flags)
308 struct strbuf error_message = STRBUF_INIT;
309 int sockfd = -1;
310 const char *port = STR(DEFAULT_GIT_PORT);
311 char *ep;
312 struct hostent *he;
313 struct sockaddr_in sa;
314 char **ap;
315 unsigned int nport;
316 int cnt;
318 get_host_and_port(&host, &port);
320 if (flags & CONNECT_VERBOSE)
321 fprintf(stderr, "Looking up %s ... ", host);
323 he = gethostbyname(host);
324 if (!he)
325 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
326 nport = strtoul(port, &ep, 10);
327 if ( ep == port || *ep ) {
328 /* Not numeric */
329 struct servent *se = getservbyname(port,"tcp");
330 if ( !se )
331 die("Unknown port %s", port);
332 nport = se->s_port;
335 if (flags & CONNECT_VERBOSE)
336 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
338 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
339 memset(&sa, 0, sizeof sa);
340 sa.sin_family = he->h_addrtype;
341 sa.sin_port = htons(nport);
342 memcpy(&sa.sin_addr, *ap, he->h_length);
344 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
345 if ((sockfd < 0) ||
346 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
347 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
348 host,
349 cnt,
350 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
351 strerror(errno));
352 if (0 <= sockfd)
353 close(sockfd);
354 sockfd = -1;
355 continue;
357 if (flags & CONNECT_VERBOSE)
358 fprintf(stderr, "%s ",
359 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
360 break;
363 if (sockfd < 0)
364 die("unable to connect to %s:\n%s", host, error_message.buf);
366 enable_keepalive(sockfd);
368 if (flags & CONNECT_VERBOSE)
369 fprintf(stderr, "done.\n");
371 return sockfd;
374 #endif /* NO_IPV6 */
377 static void git_tcp_connect(int fd[2], char *host, int flags)
379 int sockfd = git_tcp_connect_sock(host, flags);
381 fd[0] = sockfd;
382 fd[1] = dup(sockfd);
386 static char *git_proxy_command;
388 static int git_proxy_command_options(const char *var, const char *value,
389 void *cb)
391 if (!strcmp(var, "core.gitproxy")) {
392 const char *for_pos;
393 int matchlen = -1;
394 int hostlen;
395 const char *rhost_name = cb;
396 int rhost_len = strlen(rhost_name);
398 if (git_proxy_command)
399 return 0;
400 if (!value)
401 return config_error_nonbool(var);
402 /* [core]
403 * ;# matches www.kernel.org as well
404 * gitproxy = netcatter-1 for kernel.org
405 * gitproxy = netcatter-2 for sample.xz
406 * gitproxy = netcatter-default
408 for_pos = strstr(value, " for ");
409 if (!for_pos)
410 /* matches everybody */
411 matchlen = strlen(value);
412 else {
413 hostlen = strlen(for_pos + 5);
414 if (rhost_len < hostlen)
415 matchlen = -1;
416 else if (!strncmp(for_pos + 5,
417 rhost_name + rhost_len - hostlen,
418 hostlen) &&
419 ((rhost_len == hostlen) ||
420 rhost_name[rhost_len - hostlen -1] == '.'))
421 matchlen = for_pos - value;
422 else
423 matchlen = -1;
425 if (0 <= matchlen) {
426 /* core.gitproxy = none for kernel.org */
427 if (matchlen == 4 &&
428 !memcmp(value, "none", 4))
429 matchlen = 0;
430 git_proxy_command = xmemdupz(value, matchlen);
432 return 0;
435 return git_default_config(var, value, cb);
438 static int git_use_proxy(const char *host)
440 git_proxy_command = getenv("GIT_PROXY_COMMAND");
441 git_config(git_proxy_command_options, (void*)host);
442 return (git_proxy_command && *git_proxy_command);
445 static struct child_process *git_proxy_connect(int fd[2], char *host)
447 const char *port = STR(DEFAULT_GIT_PORT);
448 const char **argv;
449 struct child_process *proxy;
451 get_host_and_port(&host, &port);
453 argv = xmalloc(sizeof(*argv) * 4);
454 argv[0] = git_proxy_command;
455 argv[1] = host;
456 argv[2] = port;
457 argv[3] = NULL;
458 proxy = xcalloc(1, sizeof(*proxy));
459 proxy->argv = argv;
460 proxy->in = -1;
461 proxy->out = -1;
462 if (start_command(proxy))
463 die("cannot start proxy %s", argv[0]);
464 fd[0] = proxy->out; /* read from proxy stdout */
465 fd[1] = proxy->in; /* write to proxy stdin */
466 return proxy;
469 #define MAX_CMD_LEN 1024
471 static char *get_port(char *host)
473 char *end;
474 char *p = strchr(host, ':');
476 if (p) {
477 long port = strtol(p + 1, &end, 10);
478 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
479 *p = '\0';
480 return p+1;
484 return NULL;
487 static struct child_process no_fork;
490 * This returns a dummy child_process if the transport protocol does not
491 * need fork(2), or a struct child_process object if it does. Once done,
492 * finish the connection with finish_connect() with the value returned from
493 * this function (it is safe to call finish_connect() with NULL to support
494 * the former case).
496 * If it returns, the connect is successful; it just dies on errors (this
497 * will hopefully be changed in a libification effort, to return NULL when
498 * the connection failed).
500 struct child_process *git_connect(int fd[2], const char *url_orig,
501 const char *prog, int flags)
503 char *url;
504 char *host, *path;
505 char *end;
506 int c;
507 struct child_process *conn = &no_fork;
508 enum protocol protocol = PROTO_LOCAL;
509 int free_path = 0;
510 char *port = NULL;
511 const char **arg;
512 struct strbuf cmd;
514 /* Without this we cannot rely on waitpid() to tell
515 * what happened to our children.
517 signal(SIGCHLD, SIG_DFL);
519 if (is_url(url_orig))
520 url = url_decode(url_orig);
521 else
522 url = xstrdup(url_orig);
524 host = strstr(url, "://");
525 if (host) {
526 *host = '\0';
527 protocol = get_protocol(url);
528 host += 3;
529 c = '/';
530 } else {
531 host = url;
532 c = ':';
536 * Don't do destructive transforms with git:// as that
537 * protocol code does '[]' unwrapping of its own.
539 if (host[0] == '[') {
540 end = strchr(host + 1, ']');
541 if (end) {
542 if (protocol != PROTO_GIT) {
543 *end = 0;
544 host++;
546 end++;
547 } else
548 end = host;
549 } else
550 end = host;
552 path = strchr(end, c);
553 if (path && !has_dos_drive_prefix(end)) {
554 if (c == ':') {
555 if (host != url || path < strchrnul(host, '/')) {
556 protocol = PROTO_SSH;
557 *path++ = '\0';
558 } else /* '/' in the host part, assume local path */
559 path = end;
561 } else
562 path = end;
564 if (!path || !*path)
565 die("No path specified. See 'man git-pull' for valid url syntax");
568 * null-terminate hostname and point path to ~ for URL's like this:
569 * ssh://host.xz/~user/repo
571 if (protocol != PROTO_LOCAL && host != url) {
572 char *ptr = path;
573 if (path[1] == '~')
574 path++;
575 else {
576 path = xstrdup(ptr);
577 free_path = 1;
580 *ptr = '\0';
584 * Add support for ssh port: ssh://host.xy:<port>/...
586 if (protocol == PROTO_SSH && host != url)
587 port = get_port(end);
589 if (protocol == PROTO_GIT) {
590 /* These underlying connection commands die() if they
591 * cannot connect.
593 char *target_host = xstrdup(host);
594 if (git_use_proxy(host))
595 conn = git_proxy_connect(fd, host);
596 else
597 git_tcp_connect(fd, host, flags);
599 * Separate original protocol components prog and path
600 * from extended host header with a NUL byte.
602 * Note: Do not add any other headers here! Doing so
603 * will cause older git-daemon servers to crash.
605 packet_write(fd[1],
606 "%s %s%chost=%s%c",
607 prog, path, 0,
608 target_host, 0);
609 free(target_host);
610 free(url);
611 if (free_path)
612 free(path);
613 return conn;
616 conn = xcalloc(1, sizeof(*conn));
618 strbuf_init(&cmd, MAX_CMD_LEN);
619 strbuf_addstr(&cmd, prog);
620 strbuf_addch(&cmd, ' ');
621 sq_quote_buf(&cmd, path);
622 if (cmd.len >= MAX_CMD_LEN)
623 die("command line too long");
625 conn->in = conn->out = -1;
626 conn->argv = arg = xcalloc(7, sizeof(*arg));
627 if (protocol == PROTO_SSH) {
628 const char *ssh = getenv("GIT_SSH");
629 int putty = ssh && strcasestr(ssh, "plink");
630 if (!ssh) ssh = "ssh";
632 *arg++ = ssh;
633 if (putty && !strcasestr(ssh, "tortoiseplink"))
634 *arg++ = "-batch";
635 if (port) {
636 /* P is for PuTTY, p is for OpenSSH */
637 *arg++ = putty ? "-P" : "-p";
638 *arg++ = port;
640 *arg++ = host;
642 else {
643 /* remove repo-local variables from the environment */
644 conn->env = local_repo_env;
645 conn->use_shell = 1;
647 *arg++ = cmd.buf;
648 *arg = NULL;
650 if (start_command(conn))
651 die("unable to fork");
653 fd[0] = conn->out; /* read from child's stdout */
654 fd[1] = conn->in; /* write to child's stdin */
655 strbuf_release(&cmd);
656 free(url);
657 if (free_path)
658 free(path);
659 return conn;
662 int git_connection_is_socket(struct child_process *conn)
664 return conn == &no_fork;
667 int finish_connect(struct child_process *conn)
669 int code;
670 if (!conn || git_connection_is_socket(conn))
671 return 0;
673 code = finish_command(conn);
674 free(conn->argv);
675 free(conn);
676 return code;