drop "match" parameter from get_remote_heads
[git/gitweb.git] / connect.c
blob48df90bfe56068109476ec62fb8209db20c4e8cf
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 "url.h"
10 static char *server_capabilities;
12 static int check_ref(const char *name, int len, unsigned int flags)
14 if (!flags)
15 return 1;
17 if (len < 5 || memcmp(name, "refs/", 5))
18 return 0;
20 /* Skip the "refs/" part */
21 name += 5;
22 len -= 5;
24 /* REF_NORMAL means that we don't want the magic fake tag refs */
25 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
26 return 0;
28 /* REF_HEADS means that we want regular branch heads */
29 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
30 return 1;
32 /* REF_TAGS means that we want tags */
33 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
34 return 1;
36 /* All type bits clear means that we are ok with anything */
37 return !(flags & ~REF_NORMAL);
40 int check_ref_type(const struct ref *ref, int flags)
42 return check_ref(ref->name, strlen(ref->name), flags);
45 static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1)
47 ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc);
48 hashcpy(&(extra->array[extra->nr][0]), sha1);
49 extra->nr++;
53 * Read all the refs from the other end
55 struct ref **get_remote_heads(int in, struct ref **list,
56 unsigned int flags,
57 struct extra_have_objects *extra_have)
59 *list = NULL;
60 for (;;) {
61 struct ref *ref;
62 unsigned char old_sha1[20];
63 static char buffer[1000];
64 char *name;
65 int len, name_len;
67 len = packet_read_line(in, buffer, sizeof(buffer));
68 if (!len)
69 break;
70 if (buffer[len-1] == '\n')
71 buffer[--len] = 0;
73 if (len > 4 && !prefixcmp(buffer, "ERR "))
74 die("remote error: %s", buffer + 4);
76 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
77 die("protocol error: expected sha/ref, got '%s'", buffer);
78 name = buffer + 41;
80 name_len = strlen(name);
81 if (len != name_len + 41) {
82 free(server_capabilities);
83 server_capabilities = xstrdup(name + name_len + 1);
86 if (extra_have &&
87 name_len == 5 && !memcmp(".have", name, 5)) {
88 add_extra_have(extra_have, old_sha1);
89 continue;
92 if (!check_ref(name, name_len, flags))
93 continue;
94 ref = alloc_ref(buffer + 41);
95 hashcpy(ref->old_sha1, old_sha1);
96 *list = ref;
97 list = &ref->next;
99 return list;
102 int server_supports(const char *feature)
104 return server_capabilities &&
105 strstr(server_capabilities, feature) != NULL;
108 int path_match(const char *path, int nr, char **match)
110 int i;
111 int pathlen = strlen(path);
113 for (i = 0; i < nr; i++) {
114 char *s = match[i];
115 int len = strlen(s);
117 if (!len || len > pathlen)
118 continue;
119 if (memcmp(path + pathlen - len, s, len))
120 continue;
121 if (pathlen > len && path[pathlen - len - 1] != '/')
122 continue;
123 *s = 0;
124 return (i + 1);
126 return 0;
129 enum protocol {
130 PROTO_LOCAL = 1,
131 PROTO_SSH,
132 PROTO_GIT
135 static enum protocol get_protocol(const char *name)
137 if (!strcmp(name, "ssh"))
138 return PROTO_SSH;
139 if (!strcmp(name, "git"))
140 return PROTO_GIT;
141 if (!strcmp(name, "git+ssh"))
142 return PROTO_SSH;
143 if (!strcmp(name, "ssh+git"))
144 return PROTO_SSH;
145 if (!strcmp(name, "file"))
146 return PROTO_LOCAL;
147 die("I don't handle protocol '%s'", name);
150 #define STR_(s) # s
151 #define STR(s) STR_(s)
153 static void get_host_and_port(char **host, const char **port)
155 char *colon, *end;
157 if (*host[0] == '[') {
158 end = strchr(*host + 1, ']');
159 if (end) {
160 *end = 0;
161 end++;
162 (*host)++;
163 } else
164 end = *host;
165 } else
166 end = *host;
167 colon = strchr(end, ':');
169 if (colon) {
170 *colon = 0;
171 *port = colon + 1;
175 #ifndef NO_IPV6
177 static const char *ai_name(const struct addrinfo *ai)
179 static char addr[NI_MAXHOST];
180 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
181 NI_NUMERICHOST) != 0)
182 strcpy(addr, "(unknown)");
184 return addr;
188 * Returns a connected socket() fd, or else die()s.
190 static int git_tcp_connect_sock(char *host, int flags)
192 struct strbuf error_message = STRBUF_INIT;
193 int sockfd = -1;
194 const char *port = STR(DEFAULT_GIT_PORT);
195 struct addrinfo hints, *ai0, *ai;
196 int gai;
197 int cnt = 0;
199 get_host_and_port(&host, &port);
200 if (!*port)
201 port = "<none>";
203 memset(&hints, 0, sizeof(hints));
204 hints.ai_socktype = SOCK_STREAM;
205 hints.ai_protocol = IPPROTO_TCP;
207 if (flags & CONNECT_VERBOSE)
208 fprintf(stderr, "Looking up %s ... ", host);
210 gai = getaddrinfo(host, port, &hints, &ai);
211 if (gai)
212 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
214 if (flags & CONNECT_VERBOSE)
215 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
217 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
218 sockfd = socket(ai->ai_family,
219 ai->ai_socktype, ai->ai_protocol);
220 if ((sockfd < 0) ||
221 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
222 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
223 host, cnt, ai_name(ai), strerror(errno));
224 if (0 <= sockfd)
225 close(sockfd);
226 sockfd = -1;
227 continue;
229 if (flags & CONNECT_VERBOSE)
230 fprintf(stderr, "%s ", ai_name(ai));
231 break;
234 freeaddrinfo(ai0);
236 if (sockfd < 0)
237 die("unable to connect to %s:\n%s", host, error_message.buf);
239 if (flags & CONNECT_VERBOSE)
240 fprintf(stderr, "done.\n");
242 strbuf_release(&error_message);
244 return sockfd;
247 #else /* NO_IPV6 */
250 * Returns a connected socket() fd, or else die()s.
252 static int git_tcp_connect_sock(char *host, int flags)
254 struct strbuf error_message = STRBUF_INIT;
255 int sockfd = -1;
256 const char *port = STR(DEFAULT_GIT_PORT);
257 char *ep;
258 struct hostent *he;
259 struct sockaddr_in sa;
260 char **ap;
261 unsigned int nport;
262 int cnt;
264 get_host_and_port(&host, &port);
266 if (flags & CONNECT_VERBOSE)
267 fprintf(stderr, "Looking up %s ... ", host);
269 he = gethostbyname(host);
270 if (!he)
271 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
272 nport = strtoul(port, &ep, 10);
273 if ( ep == port || *ep ) {
274 /* Not numeric */
275 struct servent *se = getservbyname(port,"tcp");
276 if ( !se )
277 die("Unknown port %s", port);
278 nport = se->s_port;
281 if (flags & CONNECT_VERBOSE)
282 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
284 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
285 memset(&sa, 0, sizeof sa);
286 sa.sin_family = he->h_addrtype;
287 sa.sin_port = htons(nport);
288 memcpy(&sa.sin_addr, *ap, he->h_length);
290 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
291 if ((sockfd < 0) ||
292 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
293 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
294 host,
295 cnt,
296 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
297 strerror(errno));
298 if (0 <= sockfd)
299 close(sockfd);
300 sockfd = -1;
301 continue;
303 if (flags & CONNECT_VERBOSE)
304 fprintf(stderr, "%s ",
305 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
306 break;
309 if (sockfd < 0)
310 die("unable to connect to %s:\n%s", host, error_message.buf);
312 if (flags & CONNECT_VERBOSE)
313 fprintf(stderr, "done.\n");
315 return sockfd;
318 #endif /* NO_IPV6 */
321 static void git_tcp_connect(int fd[2], char *host, int flags)
323 int sockfd = git_tcp_connect_sock(host, flags);
325 fd[0] = sockfd;
326 fd[1] = dup(sockfd);
330 static char *git_proxy_command;
332 static int git_proxy_command_options(const char *var, const char *value,
333 void *cb)
335 if (!strcmp(var, "core.gitproxy")) {
336 const char *for_pos;
337 int matchlen = -1;
338 int hostlen;
339 const char *rhost_name = cb;
340 int rhost_len = strlen(rhost_name);
342 if (git_proxy_command)
343 return 0;
344 if (!value)
345 return config_error_nonbool(var);
346 /* [core]
347 * ;# matches www.kernel.org as well
348 * gitproxy = netcatter-1 for kernel.org
349 * gitproxy = netcatter-2 for sample.xz
350 * gitproxy = netcatter-default
352 for_pos = strstr(value, " for ");
353 if (!for_pos)
354 /* matches everybody */
355 matchlen = strlen(value);
356 else {
357 hostlen = strlen(for_pos + 5);
358 if (rhost_len < hostlen)
359 matchlen = -1;
360 else if (!strncmp(for_pos + 5,
361 rhost_name + rhost_len - hostlen,
362 hostlen) &&
363 ((rhost_len == hostlen) ||
364 rhost_name[rhost_len - hostlen -1] == '.'))
365 matchlen = for_pos - value;
366 else
367 matchlen = -1;
369 if (0 <= matchlen) {
370 /* core.gitproxy = none for kernel.org */
371 if (matchlen == 4 &&
372 !memcmp(value, "none", 4))
373 matchlen = 0;
374 git_proxy_command = xmemdupz(value, matchlen);
376 return 0;
379 return git_default_config(var, value, cb);
382 static int git_use_proxy(const char *host)
384 git_proxy_command = getenv("GIT_PROXY_COMMAND");
385 git_config(git_proxy_command_options, (void*)host);
386 return (git_proxy_command && *git_proxy_command);
389 static struct child_process *git_proxy_connect(int fd[2], char *host)
391 const char *port = STR(DEFAULT_GIT_PORT);
392 const char **argv;
393 struct child_process *proxy;
395 get_host_and_port(&host, &port);
397 argv = xmalloc(sizeof(*argv) * 4);
398 argv[0] = git_proxy_command;
399 argv[1] = host;
400 argv[2] = port;
401 argv[3] = NULL;
402 proxy = xcalloc(1, sizeof(*proxy));
403 proxy->argv = argv;
404 proxy->in = -1;
405 proxy->out = -1;
406 if (start_command(proxy))
407 die("cannot start proxy %s", argv[0]);
408 fd[0] = proxy->out; /* read from proxy stdout */
409 fd[1] = proxy->in; /* write to proxy stdin */
410 return proxy;
413 #define MAX_CMD_LEN 1024
415 static char *get_port(char *host)
417 char *end;
418 char *p = strchr(host, ':');
420 if (p) {
421 long port = strtol(p + 1, &end, 10);
422 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
423 *p = '\0';
424 return p+1;
428 return NULL;
431 static struct child_process no_fork;
434 * This returns a dummy child_process if the transport protocol does not
435 * need fork(2), or a struct child_process object if it does. Once done,
436 * finish the connection with finish_connect() with the value returned from
437 * this function (it is safe to call finish_connect() with NULL to support
438 * the former case).
440 * If it returns, the connect is successful; it just dies on errors (this
441 * will hopefully be changed in a libification effort, to return NULL when
442 * the connection failed).
444 struct child_process *git_connect(int fd[2], const char *url_orig,
445 const char *prog, int flags)
447 char *url;
448 char *host, *path;
449 char *end;
450 int c;
451 struct child_process *conn = &no_fork;
452 enum protocol protocol = PROTO_LOCAL;
453 int free_path = 0;
454 char *port = NULL;
455 const char **arg;
456 struct strbuf cmd;
458 /* Without this we cannot rely on waitpid() to tell
459 * what happened to our children.
461 signal(SIGCHLD, SIG_DFL);
463 if (is_url(url_orig))
464 url = url_decode(url_orig);
465 else
466 url = xstrdup(url_orig);
468 host = strstr(url, "://");
469 if (host) {
470 *host = '\0';
471 protocol = get_protocol(url);
472 host += 3;
473 c = '/';
474 } else {
475 host = url;
476 c = ':';
480 * Don't do destructive transforms with git:// as that
481 * protocol code does '[]' unwrapping of its own.
483 if (host[0] == '[') {
484 end = strchr(host + 1, ']');
485 if (end) {
486 if (protocol != PROTO_GIT) {
487 *end = 0;
488 host++;
490 end++;
491 } else
492 end = host;
493 } else
494 end = host;
496 path = strchr(end, c);
497 if (path && !has_dos_drive_prefix(end)) {
498 if (c == ':') {
499 protocol = PROTO_SSH;
500 *path++ = '\0';
502 } else
503 path = end;
505 if (!path || !*path)
506 die("No path specified. See 'man git-pull' for valid url syntax");
509 * null-terminate hostname and point path to ~ for URL's like this:
510 * ssh://host.xz/~user/repo
512 if (protocol != PROTO_LOCAL && host != url) {
513 char *ptr = path;
514 if (path[1] == '~')
515 path++;
516 else {
517 path = xstrdup(ptr);
518 free_path = 1;
521 *ptr = '\0';
525 * Add support for ssh port: ssh://host.xy:<port>/...
527 if (protocol == PROTO_SSH && host != url)
528 port = get_port(host);
530 if (protocol == PROTO_GIT) {
531 /* These underlying connection commands die() if they
532 * cannot connect.
534 char *target_host = xstrdup(host);
535 if (git_use_proxy(host))
536 conn = git_proxy_connect(fd, host);
537 else
538 git_tcp_connect(fd, host, flags);
540 * Separate original protocol components prog and path
541 * from extended host header with a NUL byte.
543 * Note: Do not add any other headers here! Doing so
544 * will cause older git-daemon servers to crash.
546 packet_write(fd[1],
547 "%s %s%chost=%s%c",
548 prog, path, 0,
549 target_host, 0);
550 free(target_host);
551 free(url);
552 if (free_path)
553 free(path);
554 return conn;
557 conn = xcalloc(1, sizeof(*conn));
559 strbuf_init(&cmd, MAX_CMD_LEN);
560 strbuf_addstr(&cmd, prog);
561 strbuf_addch(&cmd, ' ');
562 sq_quote_buf(&cmd, path);
563 if (cmd.len >= MAX_CMD_LEN)
564 die("command line too long");
566 conn->in = conn->out = -1;
567 conn->argv = arg = xcalloc(7, sizeof(*arg));
568 if (protocol == PROTO_SSH) {
569 const char *ssh = getenv("GIT_SSH");
570 int putty = ssh && strcasestr(ssh, "plink");
571 if (!ssh) ssh = "ssh";
573 *arg++ = ssh;
574 if (putty && !strcasestr(ssh, "tortoiseplink"))
575 *arg++ = "-batch";
576 if (port) {
577 /* P is for PuTTY, p is for OpenSSH */
578 *arg++ = putty ? "-P" : "-p";
579 *arg++ = port;
581 *arg++ = host;
583 else {
584 /* remove repo-local variables from the environment */
585 conn->env = local_repo_env;
586 conn->use_shell = 1;
588 *arg++ = cmd.buf;
589 *arg = NULL;
591 if (start_command(conn))
592 die("unable to fork");
594 fd[0] = conn->out; /* read from child's stdout */
595 fd[1] = conn->in; /* write to child's stdin */
596 strbuf_release(&cmd);
597 free(url);
598 if (free_path)
599 free(path);
600 return conn;
603 int git_connection_is_socket(struct child_process *conn)
605 return conn == &no_fork;
608 int finish_connect(struct child_process *conn)
610 int code;
611 if (!conn || git_connection_is_socket(conn))
612 return 0;
614 code = finish_command(conn);
615 free(conn->argv);
616 free(conn);
617 return code;
620 char *git_getpass(const char *prompt)
622 const char *askpass;
623 struct child_process pass;
624 const char *args[3];
625 static struct strbuf buffer = STRBUF_INIT;
627 askpass = getenv("GIT_ASKPASS");
628 if (!askpass)
629 askpass = askpass_program;
630 if (!askpass)
631 askpass = getenv("SSH_ASKPASS");
632 if (!askpass || !(*askpass)) {
633 char *result = getpass(prompt);
634 if (!result)
635 die_errno("Could not read password");
636 return result;
639 args[0] = askpass;
640 args[1] = prompt;
641 args[2] = NULL;
643 memset(&pass, 0, sizeof(pass));
644 pass.argv = args;
645 pass.out = -1;
647 if (start_command(&pass))
648 exit(1);
650 strbuf_reset(&buffer);
651 if (strbuf_read(&buffer, pass.out, 20) < 0)
652 die("failed to read password from %s\n", askpass);
654 close(pass.out);
656 if (finish_command(&pass))
657 exit(1);
659 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
661 return buffer.buf;