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
9static char *server_capabilities;
10
11static int check_ref(const char *name, int len, unsigned int flags)
12{
13 if (!flags)
14 return 1;
15
16 if (len < 5 || memcmp(name, "refs/", 5))
17 return 0;
18
19 /* Skip the "refs/" part */
20 name += 5;
21 len -= 5;
22
23 /* REF_NORMAL means that we don't want the magic fake tag refs */
24 if ((flags & REF_NORMAL) && check_ref_format(name) < 0)
25 return 0;
26
27 /* REF_HEADS means that we want regular branch heads */
28 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
29 return 1;
30
31 /* REF_TAGS means that we want tags */
32 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
33 return 1;
34
35 /* All type bits clear means that we are ok with anything */
36 return !(flags & ~REF_NORMAL);
37}
38
39int check_ref_type(const struct ref *ref, int flags)
40{
41 return check_ref(ref->name, strlen(ref->name), flags);
42}
43
44/*
45 * Read all the refs from the other end
46 */
47struct ref **get_remote_heads(int in, struct ref **list,
48 int nr_match, char **match,
49 unsigned int flags)
50{
51 *list = NULL;
52 for (;;) {
53 struct ref *ref;
54 unsigned char old_sha1[20];
55 static char buffer[1000];
56 char *name;
57 int len, name_len;
58
59 len = packet_read_line(in, buffer, sizeof(buffer));
60 if (!len)
61 break;
62 if (buffer[len-1] == '\n')
63 buffer[--len] = 0;
64
65 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
66 die("protocol error: expected sha/ref, got '%s'", buffer);
67 name = buffer + 41;
68
69 name_len = strlen(name);
70 if (len != name_len + 41) {
71 if (server_capabilities)
72 free(server_capabilities);
73 server_capabilities = xstrdup(name + name_len + 1);
74 }
75
76 if (!check_ref(name, name_len, flags))
77 continue;
78 if (nr_match && !path_match(name, nr_match, match))
79 continue;
80 ref = alloc_ref(name_len + 1);
81 hashcpy(ref->old_sha1, old_sha1);
82 memcpy(ref->name, buffer + 41, name_len + 1);
83 *list = ref;
84 list = &ref->next;
85 }
86 return list;
87}
88
89int server_supports(const char *feature)
90{
91 return server_capabilities &&
92 strstr(server_capabilities, feature) != NULL;
93}
94
95int get_ack(int fd, unsigned char *result_sha1)
96{
97 static char line[1000];
98 int len = packet_read_line(fd, line, sizeof(line));
99
100 if (!len)
101 die("git-fetch-pack: expected ACK/NAK, got EOF");
102 if (line[len-1] == '\n')
103 line[--len] = 0;
104 if (!strcmp(line, "NAK"))
105 return 0;
106 if (!prefixcmp(line, "ACK ")) {
107 if (!get_sha1_hex(line+4, result_sha1)) {
108 if (strstr(line+45, "continue"))
109 return 2;
110 return 1;
111 }
112 }
113 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
114}
115
116int path_match(const char *path, int nr, char **match)
117{
118 int i;
119 int pathlen = strlen(path);
120
121 for (i = 0; i < nr; i++) {
122 char *s = match[i];
123 int len = strlen(s);
124
125 if (!len || len > pathlen)
126 continue;
127 if (memcmp(path + pathlen - len, s, len))
128 continue;
129 if (pathlen > len && path[pathlen - len - 1] != '/')
130 continue;
131 *s = 0;
132 return (i + 1);
133 }
134 return 0;
135}
136
137enum protocol {
138 PROTO_LOCAL = 1,
139 PROTO_SSH,
140 PROTO_GIT,
141};
142
143static enum protocol get_protocol(const char *name)
144{
145 if (!strcmp(name, "ssh"))
146 return PROTO_SSH;
147 if (!strcmp(name, "git"))
148 return PROTO_GIT;
149 if (!strcmp(name, "git+ssh"))
150 return PROTO_SSH;
151 if (!strcmp(name, "ssh+git"))
152 return PROTO_SSH;
153 if (!strcmp(name, "file"))
154 return PROTO_LOCAL;
155 die("I don't handle protocol '%s'", name);
156}
157
158#define STR_(s) # s
159#define STR(s) STR_(s)
160
161#ifndef NO_IPV6
162
163static const char *ai_name(const struct addrinfo *ai)
164{
165 static char addr[INET_ADDRSTRLEN];
166 if ( AF_INET == ai->ai_family ) {
167 struct sockaddr_in *in;
168 in = (struct sockaddr_in *)ai->ai_addr;
169 inet_ntop(ai->ai_family, &in->sin_addr, addr, sizeof(addr));
170 } else if ( AF_INET6 == ai->ai_family ) {
171 struct sockaddr_in6 *in;
172 in = (struct sockaddr_in6 *)ai->ai_addr;
173 inet_ntop(ai->ai_family, &in->sin6_addr, addr, sizeof(addr));
174 } else {
175 strcpy(addr, "(unknown)");
176 }
177 return addr;
178}
179
180/*
181 * Returns a connected socket() fd, or else die()s.
182 */
183static int git_tcp_connect_sock(char *host, int flags)
184{
185 int sockfd = -1, saved_errno = 0;
186 char *colon, *end;
187 const char *port = STR(DEFAULT_GIT_PORT);
188 struct addrinfo hints, *ai0, *ai;
189 int gai;
190 int cnt = 0;
191
192 if (host[0] == '[') {
193 end = strchr(host + 1, ']');
194 if (end) {
195 *end = 0;
196 end++;
197 host++;
198 } else
199 end = host;
200 } else
201 end = host;
202 colon = strchr(end, ':');
203
204 if (colon) {
205 *colon = 0;
206 port = colon + 1;
207 if (!*port)
208 port = "<none>";
209 }
210
211 memset(&hints, 0, sizeof(hints));
212 hints.ai_socktype = SOCK_STREAM;
213 hints.ai_protocol = IPPROTO_TCP;
214
215 if (flags & CONNECT_VERBOSE)
216 fprintf(stderr, "Looking up %s ... ", host);
217
218 gai = getaddrinfo(host, port, &hints, &ai);
219 if (gai)
220 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
221
222 if (flags & CONNECT_VERBOSE)
223 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
224
225 for (ai0 = ai; ai; ai = ai->ai_next) {
226 sockfd = socket(ai->ai_family,
227 ai->ai_socktype, ai->ai_protocol);
228 if (sockfd < 0) {
229 saved_errno = errno;
230 continue;
231 }
232 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
233 saved_errno = errno;
234 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
235 host,
236 cnt,
237 ai_name(ai),
238 strerror(saved_errno));
239 close(sockfd);
240 sockfd = -1;
241 continue;
242 }
243 if (flags & CONNECT_VERBOSE)
244 fprintf(stderr, "%s ", ai_name(ai));
245 break;
246 }
247
248 freeaddrinfo(ai0);
249
250 if (sockfd < 0)
251 die("unable to connect a socket (%s)", strerror(saved_errno));
252
253 if (flags & CONNECT_VERBOSE)
254 fprintf(stderr, "done.\n");
255
256 return sockfd;
257}
258
259#else /* NO_IPV6 */
260
261/*
262 * Returns a connected socket() fd, or else die()s.
263 */
264static int git_tcp_connect_sock(char *host, int flags)
265{
266 int sockfd = -1, saved_errno = 0;
267 char *colon, *end;
268 char *port = STR(DEFAULT_GIT_PORT), *ep;
269 struct hostent *he;
270 struct sockaddr_in sa;
271 char **ap;
272 unsigned int nport;
273 int cnt;
274
275 if (host[0] == '[') {
276 end = strchr(host + 1, ']');
277 if (end) {
278 *end = 0;
279 end++;
280 host++;
281 } else
282 end = host;
283 } else
284 end = host;
285 colon = strchr(end, ':');
286
287 if (colon) {
288 *colon = 0;
289 port = colon + 1;
290 }
291
292 if (flags & CONNECT_VERBOSE)
293 fprintf(stderr, "Looking up %s ... ", host);
294
295 he = gethostbyname(host);
296 if (!he)
297 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
298 nport = strtoul(port, &ep, 10);
299 if ( ep == port || *ep ) {
300 /* Not numeric */
301 struct servent *se = getservbyname(port,"tcp");
302 if ( !se )
303 die("Unknown port %s\n", port);
304 nport = se->s_port;
305 }
306
307 if (flags & CONNECT_VERBOSE)
308 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
309
310 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
311 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
312 if (sockfd < 0) {
313 saved_errno = errno;
314 continue;
315 }
316
317 memset(&sa, 0, sizeof sa);
318 sa.sin_family = he->h_addrtype;
319 sa.sin_port = htons(nport);
320 memcpy(&sa.sin_addr, *ap, he->h_length);
321
322 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
323 saved_errno = errno;
324 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
325 host,
326 cnt,
327 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
328 strerror(saved_errno));
329 close(sockfd);
330 sockfd = -1;
331 continue;
332 }
333 if (flags & CONNECT_VERBOSE)
334 fprintf(stderr, "%s ",
335 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
336 break;
337 }
338
339 if (sockfd < 0)
340 die("unable to connect a socket (%s)", strerror(saved_errno));
341
342 if (flags & CONNECT_VERBOSE)
343 fprintf(stderr, "done.\n");
344
345 return sockfd;
346}
347
348#endif /* NO_IPV6 */
349
350
351static void git_tcp_connect(int fd[2], char *host, int flags)
352{
353 int sockfd = git_tcp_connect_sock(host, flags);
354
355 fd[0] = sockfd;
356 fd[1] = dup(sockfd);
357}
358
359
360static char *git_proxy_command;
361static const char *rhost_name;
362static int rhost_len;
363
364static int git_proxy_command_options(const char *var, const char *value)
365{
366 if (!strcmp(var, "core.gitproxy")) {
367 const char *for_pos;
368 int matchlen = -1;
369 int hostlen;
370
371 if (git_proxy_command)
372 return 0;
373 /* [core]
374 * ;# matches www.kernel.org as well
375 * gitproxy = netcatter-1 for kernel.org
376 * gitproxy = netcatter-2 for sample.xz
377 * gitproxy = netcatter-default
378 */
379 for_pos = strstr(value, " for ");
380 if (!for_pos)
381 /* matches everybody */
382 matchlen = strlen(value);
383 else {
384 hostlen = strlen(for_pos + 5);
385 if (rhost_len < hostlen)
386 matchlen = -1;
387 else if (!strncmp(for_pos + 5,
388 rhost_name + rhost_len - hostlen,
389 hostlen) &&
390 ((rhost_len == hostlen) ||
391 rhost_name[rhost_len - hostlen -1] == '.'))
392 matchlen = for_pos - value;
393 else
394 matchlen = -1;
395 }
396 if (0 <= matchlen) {
397 /* core.gitproxy = none for kernel.org */
398 if (matchlen == 4 &&
399 !memcmp(value, "none", 4))
400 matchlen = 0;
401 git_proxy_command = xmemdupz(value, matchlen);
402 }
403 return 0;
404 }
405
406 return git_default_config(var, value);
407}
408
409static int git_use_proxy(const char *host)
410{
411 rhost_name = host;
412 rhost_len = strlen(host);
413 git_proxy_command = getenv("GIT_PROXY_COMMAND");
414 git_config(git_proxy_command_options);
415 rhost_name = NULL;
416 return (git_proxy_command && *git_proxy_command);
417}
418
419static void git_proxy_connect(int fd[2], char *host)
420{
421 const char *port = STR(DEFAULT_GIT_PORT);
422 char *colon, *end;
423 const char *argv[4];
424 struct child_process proxy;
425
426 if (host[0] == '[') {
427 end = strchr(host + 1, ']');
428 if (end) {
429 *end = 0;
430 end++;
431 host++;
432 } else
433 end = host;
434 } else
435 end = host;
436 colon = strchr(end, ':');
437
438 if (colon) {
439 *colon = 0;
440 port = colon + 1;
441 }
442
443 argv[0] = git_proxy_command;
444 argv[1] = host;
445 argv[2] = port;
446 argv[3] = NULL;
447 memset(&proxy, 0, sizeof(proxy));
448 proxy.argv = argv;
449 proxy.in = -1;
450 proxy.out = -1;
451 if (start_command(&proxy))
452 die("cannot start proxy %s", argv[0]);
453 fd[0] = proxy.out; /* read from proxy stdout */
454 fd[1] = proxy.in; /* write to proxy stdin */
455}
456
457#define MAX_CMD_LEN 1024
458
459char *get_port(char *host)
460{
461 char *end;
462 char *p = strchr(host, ':');
463
464 if (p) {
465 strtol(p+1, &end, 10);
466 if (*end == '\0') {
467 *p = '\0';
468 return p+1;
469 }
470 }
471
472 return NULL;
473}
474
475static struct child_process no_fork;
476
477/*
478 * This returns a dummy child_process if the transport protocol does not
479 * need fork(2), or a struct child_process object if it does. Once done,
480 * finish the connection with finish_connect() with the value returned from
481 * this function (it is safe to call finish_connect() with NULL to support
482 * the former case).
483 *
484 * If it returns, the connect is successful; it just dies on errors (this
485 * will hopefully be changed in a libification effort, to return NULL when
486 * the connection failed).
487 */
488struct child_process *git_connect(int fd[2], const char *url_orig,
489 const char *prog, int flags)
490{
491 char *url = xstrdup(url_orig);
492 char *host, *path = url;
493 char *end;
494 int c;
495 struct child_process *conn;
496 enum protocol protocol = PROTO_LOCAL;
497 int free_path = 0;
498 char *port = NULL;
499 const char **arg;
500 struct strbuf cmd;
501
502 /* Without this we cannot rely on waitpid() to tell
503 * what happened to our children.
504 */
505 signal(SIGCHLD, SIG_DFL);
506
507 host = strstr(url, "://");
508 if(host) {
509 *host = '\0';
510 protocol = get_protocol(url);
511 host += 3;
512 c = '/';
513 } else {
514 host = url;
515 c = ':';
516 }
517
518 if (host[0] == '[') {
519 end = strchr(host + 1, ']');
520 if (end) {
521 *end = 0;
522 end++;
523 host++;
524 } else
525 end = host;
526 } else
527 end = host;
528
529 path = strchr(end, c);
530 if (path) {
531 if (c == ':') {
532 protocol = PROTO_SSH;
533 *path++ = '\0';
534 }
535 } else
536 path = end;
537
538 if (!path || !*path)
539 die("No path specified. See 'man git-pull' for valid url syntax");
540
541 /*
542 * null-terminate hostname and point path to ~ for URL's like this:
543 * ssh://host.xz/~user/repo
544 */
545 if (protocol != PROTO_LOCAL && host != url) {
546 char *ptr = path;
547 if (path[1] == '~')
548 path++;
549 else {
550 path = xstrdup(ptr);
551 free_path = 1;
552 }
553
554 *ptr = '\0';
555 }
556
557 /*
558 * Add support for ssh port: ssh://host.xy:<port>/...
559 */
560 if (protocol == PROTO_SSH && host != url)
561 port = get_port(host);
562
563 if (protocol == PROTO_GIT) {
564 /* These underlying connection commands die() if they
565 * cannot connect.
566 */
567 char *target_host = xstrdup(host);
568 if (git_use_proxy(host))
569 git_proxy_connect(fd, host);
570 else
571 git_tcp_connect(fd, host, flags);
572 /*
573 * Separate original protocol components prog and path
574 * from extended components with a NUL byte.
575 */
576 packet_write(fd[1],
577 "%s %s%chost=%s%c",
578 prog, path, 0,
579 target_host, 0);
580 free(target_host);
581 free(url);
582 if (free_path)
583 free(path);
584 return &no_fork;
585 }
586
587 conn = xcalloc(1, sizeof(*conn));
588
589 strbuf_init(&cmd, MAX_CMD_LEN);
590 strbuf_addstr(&cmd, prog);
591 strbuf_addch(&cmd, ' ');
592 sq_quote_buf(&cmd, path);
593 if (cmd.len >= MAX_CMD_LEN)
594 die("command line too long");
595
596 conn->in = conn->out = -1;
597 conn->argv = arg = xcalloc(6, sizeof(*arg));
598 if (protocol == PROTO_SSH) {
599 const char *ssh = getenv("GIT_SSH");
600 if (!ssh) ssh = "ssh";
601
602 *arg++ = ssh;
603 if (port) {
604 *arg++ = "-p";
605 *arg++ = port;
606 }
607 *arg++ = host;
608 }
609 else {
610 /* remove these from the environment */
611 const char *env[] = {
612 ALTERNATE_DB_ENVIRONMENT,
613 DB_ENVIRONMENT,
614 GIT_DIR_ENVIRONMENT,
615 GIT_WORK_TREE_ENVIRONMENT,
616 GRAFT_ENVIRONMENT,
617 INDEX_ENVIRONMENT,
618 NULL
619 };
620 conn->env = env;
621 *arg++ = "sh";
622 *arg++ = "-c";
623 }
624 *arg++ = cmd.buf;
625 *arg = NULL;
626
627 if (start_command(conn))
628 die("unable to fork");
629
630 fd[0] = conn->out; /* read from child's stdout */
631 fd[1] = conn->in; /* write to child's stdin */
632 strbuf_release(&cmd);
633 free(url);
634 if (free_path)
635 free(path);
636 return conn;
637}
638
639int finish_connect(struct child_process *conn)
640{
641 int code;
642 if (!conn || conn == &no_fork)
643 return 0;
644
645 code = finish_command(conn);
646 free(conn->argv);
647 free(conn);
648 return code;
649}