sed: save the last regex for use later with s///
[git/pclouds.git] / connect.c
blob08b7a5ea8c1d5ef0bfa33bf3baf55c1367778d2e
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 "spawn-pipe.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_ref_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);
41 * Read all the refs from the other end
43 struct ref **get_remote_heads(int in, struct ref **list,
44 int nr_match, char **match,
45 unsigned int flags)
47 *list = NULL;
48 for (;;) {
49 struct ref *ref;
50 unsigned char old_sha1[20];
51 static char buffer[1000];
52 char *name;
53 int len, name_len;
55 len = packet_read_line(in, buffer, sizeof(buffer));
56 if (!len)
57 break;
58 if (buffer[len-1] == '\n')
59 buffer[--len] = 0;
61 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
62 die("protocol error: expected sha/ref, got '%s'", buffer);
63 name = buffer + 41;
65 name_len = strlen(name);
66 if (len != name_len + 41) {
67 if (server_capabilities)
68 free(server_capabilities);
69 server_capabilities = xstrdup(name + name_len + 1);
72 if (!check_ref(name, name_len, flags))
73 continue;
74 if (nr_match && !path_match(name, nr_match, match))
75 continue;
76 ref = alloc_ref(len - 40);
77 hashcpy(ref->old_sha1, old_sha1);
78 memcpy(ref->name, buffer + 41, len - 40);
79 *list = ref;
80 list = &ref->next;
82 return list;
85 int server_supports(const char *feature)
87 return server_capabilities &&
88 strstr(server_capabilities, feature) != NULL;
91 int get_ack(int fd, unsigned char *result_sha1)
93 static char line[1000];
94 int len = packet_read_line(fd, line, sizeof(line));
96 if (!len)
97 die("git-fetch-pack: expected ACK/NAK, got EOF");
98 if (line[len-1] == '\n')
99 line[--len] = 0;
100 if (!strcmp(line, "NAK"))
101 return 0;
102 if (!prefixcmp(line, "ACK ")) {
103 if (!get_sha1_hex(line+4, result_sha1)) {
104 if (strstr(line+45, "continue"))
105 return 2;
106 return 1;
109 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
112 int path_match(const char *path, int nr, char **match)
114 int i;
115 int pathlen = strlen(path);
117 for (i = 0; i < nr; i++) {
118 char *s = match[i];
119 int len = strlen(s);
121 if (!len || len > pathlen)
122 continue;
123 if (memcmp(path + pathlen - len, s, len))
124 continue;
125 if (pathlen > len && path[pathlen - len - 1] != '/')
126 continue;
127 *s = 0;
128 return (i + 1);
130 return 0;
133 enum protocol {
134 PROTO_LOCAL = 1,
135 PROTO_SSH,
136 PROTO_GIT,
139 static enum protocol get_protocol(const char *name)
141 if (!strcmp(name, "ssh"))
142 return PROTO_SSH;
143 if (!strcmp(name, "git"))
144 return PROTO_GIT;
145 if (!strcmp(name, "git+ssh"))
146 return PROTO_SSH;
147 if (!strcmp(name, "ssh+git"))
148 return PROTO_SSH;
149 die("I don't handle protocol '%s'", name);
152 #define STR_(s) # s
153 #define STR(s) STR_(s)
155 #ifndef NO_IPV6
157 static const char *ai_name(const struct addrinfo *ai)
159 static char addr[INET_ADDRSTRLEN];
160 if ( AF_INET == ai->ai_family ) {
161 struct sockaddr_in *in;
162 in = (struct sockaddr_in *)ai->ai_addr;
163 inet_ntop(ai->ai_family, &in->sin_addr, addr, sizeof(addr));
164 } else if ( AF_INET6 == ai->ai_family ) {
165 struct sockaddr_in6 *in;
166 in = (struct sockaddr_in6 *)ai->ai_addr;
167 inet_ntop(ai->ai_family, &in->sin6_addr, addr, sizeof(addr));
168 } else {
169 strcpy(addr, "(unknown)");
171 return addr;
175 * Returns a connected socket() fd, or else die()s.
177 static int git_tcp_connect_sock(char *host, int flags)
179 int sockfd = -1, saved_errno = 0;
180 char *colon, *end;
181 const char *port = STR(DEFAULT_GIT_PORT);
182 struct addrinfo hints, *ai0, *ai;
183 int gai;
184 int cnt = 0;
186 if (host[0] == '[') {
187 end = strchr(host + 1, ']');
188 if (end) {
189 *end = 0;
190 end++;
191 host++;
192 } else
193 end = host;
194 } else
195 end = host;
196 colon = strchr(end, ':');
198 if (colon) {
199 *colon = 0;
200 port = colon + 1;
201 if (!*port)
202 port = "<none>";
205 memset(&hints, 0, sizeof(hints));
206 hints.ai_socktype = SOCK_STREAM;
207 hints.ai_protocol = IPPROTO_TCP;
209 if (flags & CONNECT_VERBOSE)
210 fprintf(stderr, "Looking up %s ... ", host);
212 gai = getaddrinfo(host, port, &hints, &ai);
213 if (gai)
214 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
216 if (flags & CONNECT_VERBOSE)
217 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
219 for (ai0 = ai; ai; ai = ai->ai_next) {
220 sockfd = socket(ai->ai_family,
221 ai->ai_socktype, ai->ai_protocol);
222 if (sockfd < 0) {
223 saved_errno = errno;
224 continue;
226 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
227 saved_errno = errno;
228 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
229 host,
230 cnt,
231 ai_name(ai),
232 strerror(saved_errno));
233 close(sockfd);
234 sockfd = -1;
235 continue;
237 if (flags & CONNECT_VERBOSE)
238 fprintf(stderr, "%s ", ai_name(ai));
239 break;
242 freeaddrinfo(ai0);
244 if (sockfd < 0)
245 die("unable to connect a socket (%s)", strerror(saved_errno));
247 if (flags & CONNECT_VERBOSE)
248 fprintf(stderr, "done.\n");
250 return sockfd;
253 #else /* NO_IPV6 */
256 * Returns a connected socket() fd, or else die()s.
258 static int git_tcp_connect_sock(char *host, int flags)
260 int sockfd = -1, saved_errno = 0;
261 char *colon, *end;
262 char *port = STR(DEFAULT_GIT_PORT), *ep;
263 struct hostent *he;
264 struct sockaddr_in sa;
265 char **ap;
266 unsigned int nport;
267 int cnt;
269 if (host[0] == '[') {
270 end = strchr(host + 1, ']');
271 if (end) {
272 *end = 0;
273 end++;
274 host++;
275 } else
276 end = host;
277 } else
278 end = host;
279 colon = strchr(end, ':');
281 if (colon) {
282 *colon = 0;
283 port = colon + 1;
286 if (flags & CONNECT_VERBOSE)
287 fprintf(stderr, "Looking up %s ... ", host);
289 he = gethostbyname(host);
290 if (!he)
291 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
292 nport = strtoul(port, &ep, 10);
293 if ( ep == port || *ep ) {
294 /* Not numeric */
295 struct servent *se = getservbyname(port,"tcp");
296 if ( !se )
297 die("Unknown port %s\n", port);
298 nport = se->s_port;
301 if (flags & CONNECT_VERBOSE)
302 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
304 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
305 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
306 if (sockfd < 0) {
307 saved_errno = errno;
308 continue;
311 memset(&sa, 0, sizeof sa);
312 sa.sin_family = he->h_addrtype;
313 sa.sin_port = htons(nport);
314 memcpy(&sa.sin_addr, *ap, he->h_length);
316 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
317 saved_errno = errno;
318 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
319 host,
320 cnt,
321 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
322 strerror(saved_errno));
323 close(sockfd);
324 sockfd = -1;
325 continue;
327 if (flags & CONNECT_VERBOSE)
328 fprintf(stderr, "%s ",
329 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
330 break;
333 if (sockfd < 0)
334 die("unable to connect a socket (%s)", strerror(saved_errno));
336 if (flags & CONNECT_VERBOSE)
337 fprintf(stderr, "done.\n");
339 return sockfd;
342 #endif /* NO_IPV6 */
345 static void git_tcp_connect(int fd[2], char *host, int flags)
347 #ifndef __MINGW32__
348 int sockfd = git_tcp_connect_sock(host, flags);
349 #else
350 int sockfd;
351 WSADATA wsa;
353 if (WSAStartup(MAKEWORD(2,2), &wsa))
354 die("unable to initialize winsock subsystem, error %d",
355 WSAGetLastError());
356 atexit((void(*)(void)) WSACleanup);
358 sockfd = git_tcp_connect_sock(host, flags);
359 /* convert into a file descriptor */
360 if ((sockfd = _open_osfhandle(sockfd, O_RDWR|O_BINARY)) < 0)
361 die("unable to make a socket file descriptor: %s",
362 strerror(errno));
363 #endif
365 fd[0] = sockfd;
366 fd[1] = dup(sockfd);
370 static char *git_proxy_command;
371 static const char *rhost_name;
372 static int rhost_len;
374 static int git_proxy_command_options(const char *var, const char *value)
376 if (!strcmp(var, "core.gitproxy")) {
377 const char *for_pos;
378 int matchlen = -1;
379 int hostlen;
381 if (git_proxy_command)
382 return 0;
383 /* [core]
384 * ;# matches www.kernel.org as well
385 * gitproxy = netcatter-1 for kernel.org
386 * gitproxy = netcatter-2 for sample.xz
387 * gitproxy = netcatter-default
389 for_pos = strstr(value, " for ");
390 if (!for_pos)
391 /* matches everybody */
392 matchlen = strlen(value);
393 else {
394 hostlen = strlen(for_pos + 5);
395 if (rhost_len < hostlen)
396 matchlen = -1;
397 else if (!strncmp(for_pos + 5,
398 rhost_name + rhost_len - hostlen,
399 hostlen) &&
400 ((rhost_len == hostlen) ||
401 rhost_name[rhost_len - hostlen -1] == '.'))
402 matchlen = for_pos - value;
403 else
404 matchlen = -1;
406 if (0 <= matchlen) {
407 /* core.gitproxy = none for kernel.org */
408 if (matchlen == 4 &&
409 !memcmp(value, "none", 4))
410 matchlen = 0;
411 git_proxy_command = xmalloc(matchlen + 1);
412 memcpy(git_proxy_command, value, matchlen);
413 git_proxy_command[matchlen] = 0;
415 return 0;
418 return git_default_config(var, value);
421 static int git_use_proxy(const char *host)
423 rhost_name = host;
424 rhost_len = strlen(host);
425 git_proxy_command = getenv("GIT_PROXY_COMMAND");
426 git_config(git_proxy_command_options);
427 rhost_name = NULL;
428 return (git_proxy_command && *git_proxy_command);
431 static void git_proxy_connect(int fd[2], char *host)
433 const char *port = STR(DEFAULT_GIT_PORT);
434 char *colon, *end;
435 const char *argv[4];
436 struct child_process proxy;
438 if (host[0] == '[') {
439 end = strchr(host + 1, ']');
440 if (end) {
441 *end = 0;
442 end++;
443 host++;
444 } else
445 end = host;
446 } else
447 end = host;
448 colon = strchr(end, ':');
450 if (colon) {
451 *colon = 0;
452 port = colon + 1;
455 argv[0] = git_proxy_command;
456 argv[1] = host;
457 argv[2] = port;
458 argv[3] = NULL;
459 memset(&proxy, 0, sizeof(proxy));
460 proxy.argv = argv;
461 proxy.in = -1;
462 proxy.out = -1;
463 if (start_command(&proxy))
464 die("cannot start proxy %s", argv[0]);
465 fd[0] = proxy.out; /* read from proxy stdout */
466 fd[1] = proxy.in; /* write to proxy stdin */
469 #define MAX_CMD_LEN 1024
472 * This returns 0 if the transport protocol does not need fork(2),
473 * or a process id if it does. Once done, finish the connection
474 * with finish_connect() with the value returned from this function
475 * (it is safe to call finish_connect() with 0 to support the former
476 * case).
478 * Does not return a negative value on error; it just dies.
480 pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
482 char *host, *path = url;
483 char *end;
484 int c;
485 int pipefd[2][2];
486 pid_t pid;
487 enum protocol protocol = PROTO_LOCAL;
488 int free_path = 0;
490 /* Without this we cannot rely on waitpid() to tell
491 * what happened to our children.
493 #ifdef SIGCHLD
494 signal(SIGCHLD, SIG_DFL);
495 #endif
497 host = strstr(url, "://");
498 if(host) {
499 *host = '\0';
500 protocol = get_protocol(url);
501 host += 3;
502 c = '/';
503 } else {
504 host = url;
505 c = ':';
508 if (host[0] == '[') {
509 end = strchr(host + 1, ']');
510 if (end) {
511 *end = 0;
512 end++;
513 host++;
514 } else
515 end = host;
516 } else
517 end = host;
519 path = strchr(end, c);
520 if (c == ':') {
521 /* host must have at least 2 chars to catch DOS C:/path */
522 if (path && path - end > 1) {
523 protocol = PROTO_SSH;
524 *path++ = '\0';
525 } else
526 path = host;
529 if (!path || !*path)
530 die("No path specified. See 'man git-pull' for valid url syntax");
533 * null-terminate hostname and point path to ~ for URL's like this:
534 * ssh://host.xz/~user/repo
536 if (protocol != PROTO_LOCAL && host != url) {
537 char *ptr = path;
538 if (path[1] == '~')
539 path++;
540 else {
541 path = xstrdup(ptr);
542 free_path = 1;
545 *ptr = '\0';
548 if (protocol == PROTO_GIT) {
549 /* These underlying connection commands die() if they
550 * cannot connect.
552 char *target_host = xstrdup(host);
553 if (git_use_proxy(host))
554 git_proxy_connect(fd, host);
555 else
556 git_tcp_connect(fd, host, flags);
558 * Separate original protocol components prog and path
559 * from extended components with a NUL byte.
561 packet_write(fd[1],
562 "%s %s%chost=%s%c",
563 prog, path, 0,
564 target_host, 0);
565 free(target_host);
566 if (free_path)
567 free(path);
568 return 0;
571 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
572 die("unable to create pipe pair for communication");
574 char command[MAX_CMD_LEN];
575 char *posn = command;
576 int size = MAX_CMD_LEN;
577 int of = 0;
579 of |= add_to_string(&posn, &size, prog, 0);
580 of |= add_to_string(&posn, &size, " ", 0);
581 of |= add_to_string(&posn, &size, path, 1);
583 if (of)
584 die("command line too long");
586 if (protocol == PROTO_SSH) {
587 const char *argv[] = { NULL, host, command, NULL };
588 const char *ssh = getenv("GIT_SSH");
589 if (!ssh) ssh = "ssh";
590 pid = spawnvpe_pipe(ssh, argv, environ, pipefd[1], pipefd[0]);
592 else {
593 #ifdef __MINGW32__
594 const char *argv[] = { "box", "sh", "-c", command, NULL };
595 #else
596 const char *argv[] = { NULL, "-c", command, NULL };
597 #endif
598 const char **env = copy_environ();
599 env_unsetenv(env, ALTERNATE_DB_ENVIRONMENT);
600 env_unsetenv(env, DB_ENVIRONMENT);
601 env_unsetenv(env, GIT_DIR_ENVIRONMENT);
602 env_unsetenv(env, GIT_WORK_TREE_ENVIRONMENT);
603 env_unsetenv(env, GRAFT_ENVIRONMENT);
604 env_unsetenv(env, INDEX_ENVIRONMENT);
605 #ifdef __MINGW32__
606 pid = spawnve_git_cmd(argv, pipefd[1], pipefd[0], env);
607 #else
608 pid = spawnvpe_pipe("sh", argv, env, pipefd[1], pipefd[0]);
609 #endif
611 if (pid < 0)
612 die("unable to fork");
613 fd[0] = pipefd[0][0];
614 fd[1] = pipefd[1][1];
615 if (free_path)
616 free(path);
617 return pid;
620 int finish_connect(pid_t pid)
622 if (pid == 0)
623 return 0;
625 while (waitpid(pid, NULL, 0) < 0) {
626 if (errno != EINTR)
627 return -1;
629 return 0;