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