connect.c: Support PuTTY plink and TortoisePlink as SSH on Windows
[git.git] / connect.c
Commit [+]AuthorDateLineData
731043fd Jason Riedy2006-01-25 12:38:36 -08001#include "git-compat-util.h"
f7192598 Linus Torvalds2005-07-04 11:57:58 -07002#include "cache.h"
41cb7488 Linus Torvalds2005-07-05 15:44:09 -07003#include "pkt-line.h"
b10d0ec7 Junio C Hamano2005-07-08 00:02:52 -07004#include "quote.h"
6abf5c0c Junio C Hamano2005-10-16 00:25:26 -07005#include "refs.h"
15a1c012 Shawn O. Pearce2007-03-12 19:00:19 -04006#include "run-command.h"
6b62816c Daniel Barkalow2007-05-12 11:45:59 -04007#include "remote.h"
f7192598 Linus Torvalds2005-07-04 11:57:58 -07008
96f1e58f David Rientjes2006-08-15 10:23:48 -07009static char *server_capabilities;
211b5f9e Johannes Schindelin2005-10-28 04:48:54 +020010
2718ff09
LT
Linus Torvalds2006-07-04 12:29:10 -070011static int check_ref(const char *name, int len, unsigned int flags)
12{
13 if (!flags)
14 return 1;
15
c41e20b3 Andy Whitcroft2006-09-05 20:00:17 +010016 if (len < 5 || memcmp(name, "refs/", 5))
2718ff09
LT
Linus Torvalds2006-07-04 12:29:10 -070017 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
4577370e
DB
Daniel Barkalow2007-10-29 21:05:40 -040039int check_ref_type(const struct ref *ref, int flags)
40{
41 return check_ref(ref->name, strlen(ref->name), flags);
42}
43
40c155ff
JH
Junio C Hamano2008-09-09 01:27:09 -070044static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1)
45{
46 ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc);
47 hashcpy(&(extra->array[extra->nr][0]), sha1);
48 extra->nr++;
49}
50
d1c133f5
LT
Linus Torvalds2005-07-16 13:55:50 -070051/*
52 * Read all the refs from the other end
53 */
1a7141ff Junio C Hamano2005-10-13 18:57:40 -070054struct ref **get_remote_heads(int in, struct ref **list,
2718ff09 Linus Torvalds2006-07-04 12:29:10 -070055 int nr_match, char **match,
40c155ff
JH
Junio C Hamano2008-09-09 01:27:09 -070056 unsigned int flags,
57 struct extra_have_objects *extra_have)
d1c133f5
LT
Linus Torvalds2005-07-16 13:55:50 -070058{
59 *list = NULL;
60 for (;;) {
61 struct ref *ref;
62 unsigned char old_sha1[20];
63 static char buffer[1000];
64 char *name;
211b5f9e Johannes Schindelin2005-10-28 04:48:54 +020065 int len, name_len;
d1c133f5
LT
Linus Torvalds2005-07-16 13:55:50 -070066
67 len = packet_read_line(in, buffer, sizeof(buffer));
68 if (!len)
69 break;
70 if (buffer[len-1] == '\n')
71 buffer[--len] = 0;
72
a8073289
TPW
Tom Preston-Werner2008-11-01 11:44:45 -070073 if (len > 4 && !prefixcmp(buffer, "ERR "))
74 die("remote error: %s", buffer + 4);
75
d1c133f5
LT
Linus Torvalds2005-07-16 13:55:50 -070076 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;
1a7141ff Junio C Hamano2005-10-13 18:57:40 -070079
211b5f9e
JS
Johannes Schindelin2005-10-28 04:48:54 +020080 name_len = strlen(name);
81 if (len != name_len + 41) {
8e0f7003 Jim Meyering2008-01-31 18:26:32 +010082 free(server_capabilities);
9befac47 Shawn O. Pearce2006-09-02 00:16:31 -040083 server_capabilities = xstrdup(name + name_len + 1);
211b5f9e
JS
Johannes Schindelin2005-10-28 04:48:54 +020084 }
85
40c155ff
JH
Junio C Hamano2008-09-09 01:27:09 -070086 if (extra_have &&
87 name_len == 5 && !memcmp(".have", name, 5)) {
88 add_extra_have(extra_have, old_sha1);
89 continue;
90 }
91
2718ff09 Linus Torvalds2006-07-04 12:29:10 -070092 if (!check_ref(name, name_len, flags))
cfee10a7 Junio C Hamano2005-12-25 23:18:37 -080093 continue;
d1c133f5
LT
Linus Torvalds2005-07-16 13:55:50 -070094 if (nr_match && !path_match(name, nr_match, match))
95 continue;
59c69c0c René Scharfe2008-10-18 10:44:18 +020096 ref = alloc_ref(buffer + 41);
e702496e Shawn O. Pearce2006-08-23 02:49:00 -040097 hashcpy(ref->old_sha1, old_sha1);
d1c133f5
LT
Linus Torvalds2005-07-16 13:55:50 -070098 *list = ref;
99 list = &ref->next;
100 }
101 return list;
102}
103
211b5f9e
JS
Johannes Schindelin2005-10-28 04:48:54 +0200104int server_supports(const char *feature)
105{
1f5881bb
JS
Johannes Schindelin2005-10-28 05:56:41 +0200106 return server_capabilities &&
107 strstr(server_capabilities, feature) != NULL;
211b5f9e
JS
Johannes Schindelin2005-10-28 04:48:54 +0200108}
109
41cb7488
LT
Linus Torvalds2005-07-05 15:44:09 -0700110int get_ack(int fd, unsigned char *result_sha1)
111{
112 static char line[1000];
113 int len = packet_read_line(fd, line, sizeof(line));
114
115 if (!len)
7e44c935 Junio C Hamano2008-08-31 09:39:19 -0700116 die("git fetch-pack: expected ACK/NAK, got EOF");
41cb7488
LT
Linus Torvalds2005-07-05 15:44:09 -0700117 if (line[len-1] == '\n')
118 line[--len] = 0;
119 if (!strcmp(line, "NAK"))
120 return 0;
cc44c765 Junio C Hamano2007-02-20 01:53:29 -0800121 if (!prefixcmp(line, "ACK ")) {
c4c86f07
JS
Johannes Schindelin2005-10-28 04:50:26 +0200122 if (!get_sha1_hex(line+4, result_sha1)) {
123 if (strstr(line+45, "continue"))
124 return 2;
41cb7488 Linus Torvalds2005-07-05 15:44:09 -0700125 return 1;
c4c86f07 Johannes Schindelin2005-10-28 04:50:26 +0200126 }
41cb7488 Linus Torvalds2005-07-05 15:44:09 -0700127 }
7e44c935 Junio C Hamano2008-08-31 09:39:19 -0700128 die("git fetch_pack: expected ACK/NAK, got '%s'", line);
41cb7488
LT
Linus Torvalds2005-07-05 15:44:09 -0700129}
130
013e7c7f
LT
Linus Torvalds2005-07-04 13:24:30 -0700131int path_match(const char *path, int nr, char **match)
132{
133 int i;
134 int pathlen = strlen(path);
135
136 for (i = 0; i < nr; i++) {
137 char *s = match[i];
138 int len = strlen(s);
139
140 if (!len || len > pathlen)
141 continue;
142 if (memcmp(path + pathlen - len, s, len))
143 continue;
144 if (pathlen > len && path[pathlen - len - 1] != '/')
145 continue;
146 *s = 0;
9546010b Junio C Hamano2006-05-11 15:28:44 -0700147 return (i + 1);
013e7c7f
LT
Linus Torvalds2005-07-04 13:24:30 -0700148 }
149 return 0;
150}
151
2386d658
LT
Linus Torvalds2005-07-13 18:46:20 -0700152enum protocol {
153 PROTO_LOCAL = 1,
154 PROTO_SSH,
155 PROTO_GIT,
156};
157
158static enum protocol get_protocol(const char *name)
159{
160 if (!strcmp(name, "ssh"))
161 return PROTO_SSH;
162 if (!strcmp(name, "git"))
163 return PROTO_GIT;
c05186cc
LT
Linus Torvalds2005-10-14 17:14:56 -0700164 if (!strcmp(name, "git+ssh"))
165 return PROTO_SSH;
166 if (!strcmp(name, "ssh+git"))
167 return PROTO_SSH;
72a4f4b6
LT
Linus Torvalds2007-08-01 10:03:37 -0700168 if (!strcmp(name, "file"))
169 return PROTO_LOCAL;
2386d658
LT
Linus Torvalds2005-07-13 18:46:20 -0700170 die("I don't handle protocol '%s'", name);
171}
172
5ba88448
YH
YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400173#define STR_(s) # s
174#define STR(s) STR_(s)
2386d658 Linus Torvalds2005-07-13 18:46:20 -0700175
49744d63 H. Peter Anvin2005-09-28 16:52:21 -0700176#ifndef NO_IPV6
4c505f71 H. Peter Anvin2005-09-28 16:37:58 -0700177
ba505322
AR
Alex Riesen2007-05-23 23:34:27 +0200178static const char *ai_name(const struct addrinfo *ai)
179{
785a9857
BK
Benjamin Kramer2009-04-24 14:16:41 +0200180 static char addr[NI_MAXHOST];
181 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
182 NI_NUMERICHOST) != 0)
ba505322 Alex Riesen2007-05-23 23:34:27 +0200183 strcpy(addr, "(unknown)");
785a9857 Benjamin Kramer2009-04-24 14:16:41 +0200184
ba505322
AR
Alex Riesen2007-05-23 23:34:27 +0200185 return addr;
186}
187
5ad312be
JL
Jon Loeliger2006-06-06 22:58:41 -0500188/*
189 * Returns a connected socket() fd, or else die()s.
190 */
7841ce79 Michael S. Tsirkin2007-05-16 20:09:41 +0300191static int git_tcp_connect_sock(char *host, int flags)
2386d658 Linus Torvalds2005-07-13 18:46:20 -0700192{
ac3bc6c1 Petr Baudis2006-07-01 23:56:26 +0200193 int sockfd = -1, saved_errno = 0;
5ba88448 YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400194 char *colon, *end;
554fe20d Timo Hirvonen2006-06-28 12:04:39 +0300195 const char *port = STR(DEFAULT_GIT_PORT);
5ba88448
YH
YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400196 struct addrinfo hints, *ai0, *ai;
197 int gai;
ba505322 Alex Riesen2007-05-23 23:34:27 +0200198 int cnt = 0;
5ba88448
YH
YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400199
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, ':');
211
ce6f8e7e
LT
Linus Torvalds2005-07-23 11:10:21 -0700212 if (colon) {
213 *colon = 0;
5ba88448 YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400214 port = colon + 1;
608d48b2
LT
Linus Torvalds2007-03-27 09:50:20 -0700215 if (!*port)
216 port = "<none>";
ce6f8e7e Linus Torvalds2005-07-23 11:10:21 -0700217 }
5ba88448
YH
YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400218
219 memset(&hints, 0, sizeof(hints));
220 hints.ai_socktype = SOCK_STREAM;
221 hints.ai_protocol = IPPROTO_TCP;
222
7841ce79
MT
Michael S. Tsirkin2007-05-16 20:09:41 +0300223 if (flags & CONNECT_VERBOSE)
224 fprintf(stderr, "Looking up %s ... ", host);
225
5ba88448
YH
YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400226 gai = getaddrinfo(host, port, &hints, &ai);
227 if (gai)
608d48b2 Linus Torvalds2007-03-27 09:50:20 -0700228 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
5ba88448 YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400229
7841ce79
MT
Michael S. Tsirkin2007-05-16 20:09:41 +0300230 if (flags & CONNECT_VERBOSE)
231 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
232
5ba88448 YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400233 for (ai0 = ai; ai; ai = ai->ai_next) {
5ad312be
JL
Jon Loeliger2006-06-06 22:58:41 -0500234 sockfd = socket(ai->ai_family,
235 ai->ai_socktype, ai->ai_protocol);
ac3bc6c1
PB
Petr Baudis2006-07-01 23:56:26 +0200236 if (sockfd < 0) {
237 saved_errno = errno;
5ba88448 YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400238 continue;
ac3bc6c1 Petr Baudis2006-07-01 23:56:26 +0200239 }
5ba88448 YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400240 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
ac3bc6c1 Petr Baudis2006-07-01 23:56:26 +0200241 saved_errno = errno;
7cbf2f24 Alex Riesen2007-06-12 22:52:10 +0200242 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
ba505322
AR
Alex Riesen2007-05-23 23:34:27 +0200243 host,
244 cnt,
245 ai_name(ai),
ba505322 Alex Riesen2007-05-23 23:34:27 +0200246 strerror(saved_errno));
5ba88448
YH
YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400247 close(sockfd);
248 sockfd = -1;
249 continue;
2386d658 Linus Torvalds2005-07-13 18:46:20 -0700250 }
ba505322
AR
Alex Riesen2007-05-23 23:34:27 +0200251 if (flags & CONNECT_VERBOSE)
252 fprintf(stderr, "%s ", ai_name(ai));
5ba88448 YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400253 break;
2386d658
LT
Linus Torvalds2005-07-13 18:46:20 -0700254 }
255
5ba88448 YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400256 freeaddrinfo(ai0);
2386d658 Linus Torvalds2005-07-13 18:46:20 -0700257
2386d658 Linus Torvalds2005-07-13 18:46:20 -0700258 if (sockfd < 0)
ac3bc6c1 Petr Baudis2006-07-01 23:56:26 +0200259 die("unable to connect a socket (%s)", strerror(saved_errno));
5ba88448 YOSHIFUJI Hideaki2005-07-21 09:10:36 -0400260
7841ce79
MT
Michael S. Tsirkin2007-05-16 20:09:41 +0300261 if (flags & CONNECT_VERBOSE)
262 fprintf(stderr, "done.\n");
263
5ad312be Jon Loeliger2006-06-06 22:58:41 -0500264 return sockfd;
2386d658
LT
Linus Torvalds2005-07-13 18:46:20 -0700265}
266
49744d63 H. Peter Anvin2005-09-28 16:52:21 -0700267#else /* NO_IPV6 */
4c505f71 H. Peter Anvin2005-09-28 16:37:58 -0700268
5ad312be
JL
Jon Loeliger2006-06-06 22:58:41 -0500269/*
270 * Returns a connected socket() fd, or else die()s.
271 */
7841ce79 Michael S. Tsirkin2007-05-16 20:09:41 +0300272static int git_tcp_connect_sock(char *host, int flags)
4c505f71 H. Peter Anvin2005-09-28 16:37:58 -0700273{
ac3bc6c1 Petr Baudis2006-07-01 23:56:26 +0200274 int sockfd = -1, saved_errno = 0;
4c505f71
PA
H. Peter Anvin2005-09-28 16:37:58 -0700275 char *colon, *end;
276 char *port = STR(DEFAULT_GIT_PORT), *ep;
277 struct hostent *he;
278 struct sockaddr_in sa;
279 char **ap;
280 unsigned int nport;
ba505322 Alex Riesen2007-05-23 23:34:27 +0200281 int cnt;
4c505f71
PA
H. Peter Anvin2005-09-28 16:37:58 -0700282
283 if (host[0] == '[') {
284 end = strchr(host + 1, ']');
285 if (end) {
286 *end = 0;
287 end++;
288 host++;
289 } else
290 end = host;
291 } else
292 end = host;
293 colon = strchr(end, ':');
294
295 if (colon) {
296 *colon = 0;
297 port = colon + 1;
298 }
299
7841ce79
MT
Michael S. Tsirkin2007-05-16 20:09:41 +0300300 if (flags & CONNECT_VERBOSE)
301 fprintf(stderr, "Looking up %s ... ", host);
302
4c505f71
PA
H. Peter Anvin2005-09-28 16:37:58 -0700303 he = gethostbyname(host);
304 if (!he)
305 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
306 nport = strtoul(port, &ep, 10);
307 if ( ep == port || *ep ) {
308 /* Not numeric */
309 struct servent *se = getservbyname(port,"tcp");
310 if ( !se )
d7530708 Alexander Potashev2009-01-04 21:38:41 +0300311 die("Unknown port %s", port);
4c505f71
PA
H. Peter Anvin2005-09-28 16:37:58 -0700312 nport = se->s_port;
313 }
314
7841ce79
MT
Michael S. Tsirkin2007-05-16 20:09:41 +0300315 if (flags & CONNECT_VERBOSE)
316 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
317
ba505322 Alex Riesen2007-05-23 23:34:27 +0200318 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
4c505f71 H. Peter Anvin2005-09-28 16:37:58 -0700319 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
ac3bc6c1
PB
Petr Baudis2006-07-01 23:56:26 +0200320 if (sockfd < 0) {
321 saved_errno = errno;
4c505f71 H. Peter Anvin2005-09-28 16:37:58 -0700322 continue;
ac3bc6c1 Petr Baudis2006-07-01 23:56:26 +0200323 }
4c505f71
PA
H. Peter Anvin2005-09-28 16:37:58 -0700324
325 memset(&sa, 0, sizeof sa);
326 sa.sin_family = he->h_addrtype;
6573faff H. Peter Anvin2005-09-28 17:26:44 -0700327 sa.sin_port = htons(nport);
c6164218 Paul Serice2005-11-22 07:54:23 -0600328 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71
PA
H. Peter Anvin2005-09-28 16:37:58 -0700329
330 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
ac3bc6c1 Petr Baudis2006-07-01 23:56:26 +0200331 saved_errno = errno;
7cbf2f24 Alex Riesen2007-06-12 22:52:10 +0200332 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
ba505322
AR
Alex Riesen2007-05-23 23:34:27 +0200333 host,
334 cnt,
335 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
ba505322 Alex Riesen2007-05-23 23:34:27 +0200336 strerror(saved_errno));
4c505f71
PA
H. Peter Anvin2005-09-28 16:37:58 -0700337 close(sockfd);
338 sockfd = -1;
339 continue;
340 }
ba505322
AR
Alex Riesen2007-05-23 23:34:27 +0200341 if (flags & CONNECT_VERBOSE)
342 fprintf(stderr, "%s ",
343 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
4c505f71
PA
H. Peter Anvin2005-09-28 16:37:58 -0700344 break;
345 }
346
347 if (sockfd < 0)
ac3bc6c1 Petr Baudis2006-07-01 23:56:26 +0200348 die("unable to connect a socket (%s)", strerror(saved_errno));
4c505f71 H. Peter Anvin2005-09-28 16:37:58 -0700349
7841ce79
MT
Michael S. Tsirkin2007-05-16 20:09:41 +0300350 if (flags & CONNECT_VERBOSE)
351 fprintf(stderr, "done.\n");
352
5ad312be
JL
Jon Loeliger2006-06-06 22:58:41 -0500353 return sockfd;
354}
355
356#endif /* NO_IPV6 */
357
358
7841ce79 Michael S. Tsirkin2007-05-16 20:09:41 +0300359static void git_tcp_connect(int fd[2], char *host, int flags)
5ad312be Jon Loeliger2006-06-06 22:58:41 -0500360{
7841ce79 Michael S. Tsirkin2007-05-16 20:09:41 +0300361 int sockfd = git_tcp_connect_sock(host, flags);
5ad312be Jon Loeliger2006-06-06 22:58:41 -0500362
4c505f71 H. Peter Anvin2005-09-28 16:37:58 -0700363 fd[0] = sockfd;
ec587fde Junio C Hamano2007-01-21 17:10:51 -0800364 fd[1] = dup(sockfd);
4c505f71
PA
H. Peter Anvin2005-09-28 16:37:58 -0700365}
366
4c505f71 H. Peter Anvin2005-09-28 16:37:58 -0700367
96f1e58f David Rientjes2006-08-15 10:23:48 -0700368static char *git_proxy_command;
f8014776 Paul Collins2005-11-04 14:57:16 +0000369
ef90d6d4
JS
Johannes Schindelin2008-05-14 18:46:53 +0100370static int git_proxy_command_options(const char *var, const char *value,
371 void *cb)
f8014776 Paul Collins2005-11-04 14:57:16 +0000372{
e814bc4d Junio C Hamano2005-11-19 03:48:56 -0800373 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
YOSHIFUJI Hideaki2005-11-22 12:18:23 +0900374 const char *for_pos;
375 int matchlen = -1;
376 int hostlen;
15112c95
EFL
Erik Faye-Lund2009-03-11 02:38:12 +0000377 const char *rhost_name = cb;
378 int rhost_len = strlen(rhost_name);
c3df8568 YOSHIFUJI Hideaki2005-11-22 12:18:23 +0900379
e814bc4d Junio C Hamano2005-11-19 03:48:56 -0800380 if (git_proxy_command)
f8014776 Paul Collins2005-11-04 14:57:16 +0000381 return 0;
c64b9ad0
JH
Junio C Hamano2008-02-11 10:52:15 -0800382 if (!value)
383 return config_error_nonbool(var);
e814bc4d
JH
Junio C Hamano2005-11-19 03:48:56 -0800384 /* [core]
385 * ;# matches www.kernel.org as well
386 * gitproxy = netcatter-1 for kernel.org
387 * gitproxy = netcatter-2 for sample.xz
388 * gitproxy = netcatter-default
389 */
c3df8568 YOSHIFUJI Hideaki2005-11-22 12:18:23 +0900390 for_pos = strstr(value, " for ");
e814bc4d
JH
Junio C Hamano2005-11-19 03:48:56 -0800391 if (!for_pos)
392 /* matches everybody */
393 matchlen = strlen(value);
394 else {
395 hostlen = strlen(for_pos + 5);
396 if (rhost_len < hostlen)
397 matchlen = -1;
398 else if (!strncmp(for_pos + 5,
399 rhost_name + rhost_len - hostlen,
400 hostlen) &&
401 ((rhost_len == hostlen) ||
402 rhost_name[rhost_len - hostlen -1] == '.'))
403 matchlen = for_pos - value;
404 else
405 matchlen = -1;
406 }
407 if (0 <= matchlen) {
408 /* core.gitproxy = none for kernel.org */
a6080a0a Junio C Hamano2007-06-07 00:04:01 -0700409 if (matchlen == 4 &&
e814bc4d
JH
Junio C Hamano2005-11-19 03:48:56 -0800410 !memcmp(value, "none", 4))
411 matchlen = 0;
182af834 Pierre Habouzit2007-09-16 00:32:36 +0200412 git_proxy_command = xmemdupz(value, matchlen);
f8014776 Paul Collins2005-11-04 14:57:16 +0000413 }
e814bc4d Junio C Hamano2005-11-19 03:48:56 -0800414 return 0;
f8014776
PC
Paul Collins2005-11-04 14:57:16 +0000415 }
416
ef90d6d4 Johannes Schindelin2008-05-14 18:46:53 +0100417 return git_default_config(var, value, cb);
f8014776
PC
Paul Collins2005-11-04 14:57:16 +0000418}
419
e814bc4d Junio C Hamano2005-11-19 03:48:56 -0800420static int git_use_proxy(const char *host)
f8014776
PC
Paul Collins2005-11-04 14:57:16 +0000421{
422 git_proxy_command = getenv("GIT_PROXY_COMMAND");
15112c95 Erik Faye-Lund2009-03-11 02:38:12 +0000423 git_config(git_proxy_command_options, (void*)host);
e814bc4d Junio C Hamano2005-11-19 03:48:56 -0800424 return (git_proxy_command && *git_proxy_command);
f8014776
PC
Paul Collins2005-11-04 14:57:16 +0000425}
426
c78963d2 Junio C Hamano2006-06-28 03:50:33 -0700427static void git_proxy_connect(int fd[2], char *host)
f8014776 Paul Collins2005-11-04 14:57:16 +0000428{
554fe20d Timo Hirvonen2006-06-28 12:04:39 +0300429 const char *port = STR(DEFAULT_GIT_PORT);
f8014776 Paul Collins2005-11-04 14:57:16 +0000430 char *colon, *end;
15a1c012
SP
Shawn O. Pearce2007-03-12 19:00:19 -0400431 const char *argv[4];
432 struct child_process proxy;
f8014776
PC
Paul Collins2005-11-04 14:57:16 +0000433
434 if (host[0] == '[') {
435 end = strchr(host + 1, ']');
436 if (end) {
437 *end = 0;
438 end++;
439 host++;
440 } else
441 end = host;
442 } else
443 end = host;
444 colon = strchr(end, ':');
445
446 if (colon) {
447 *colon = 0;
448 port = colon + 1;
449 }
450
15a1c012
SP
Shawn O. Pearce2007-03-12 19:00:19 -0400451 argv[0] = git_proxy_command;
452 argv[1] = host;
453 argv[2] = port;
454 argv[3] = NULL;
455 memset(&proxy, 0, sizeof(proxy));
456 proxy.argv = argv;
457 proxy.in = -1;
458 proxy.out = -1;
459 if (start_command(&proxy))
460 die("cannot start proxy %s", argv[0]);
461 fd[0] = proxy.out; /* read from proxy stdout */
462 fd[1] = proxy.in; /* write to proxy stdin */
f8014776
PC
Paul Collins2005-11-04 14:57:16 +0000463}
464
0f503d77
CC
Christian Couder2006-09-11 07:04:50 +0200465#define MAX_CMD_LEN 1024
466
2e776665
LT
Luben Tuikov2007-09-01 02:36:31 -0700467char *get_port(char *host)
468{
469 char *end;
470 char *p = strchr(host, ':');
471
472 if (p) {
8f148253
RS
René Scharfe2008-12-21 02:12:11 +0100473 long port = strtol(p + 1, &end, 10);
474 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
2e776665
LT
Luben Tuikov2007-09-01 02:36:31 -0700475 *p = '\0';
476 return p+1;
477 }
478 }
479
480 return NULL;
481}
482
fb32c917
JS
Johannes Schindelin2008-02-10 03:06:57 +0000483static struct child_process no_fork;
484
f7192598 Linus Torvalds2005-07-04 11:57:58 -0700485/*
fb32c917
JS
Johannes Schindelin2008-02-10 03:06:57 +0000486 * This returns a dummy child_process if the transport protocol does not
487 * need fork(2), or a struct child_process object if it does. Once done,
488 * finish the connection with finish_connect() with the value returned from
489 * this function (it is safe to call finish_connect() with NULL to support
490 * the former case).
f42a5c4e Franck Bui-Huu2006-09-12 11:00:13 +0200491 *
fb32c917
JS
Johannes Schindelin2008-02-10 03:06:57 +0000492 * If it returns, the connect is successful; it just dies on errors (this
493 * will hopefully be changed in a libification effort, to return NULL when
494 * the connection failed).
f7192598 Linus Torvalds2005-07-04 11:57:58 -0700495 */
4577370e Daniel Barkalow2007-10-29 21:05:40 -0400496struct child_process *git_connect(int fd[2], const char *url_orig,
98158e9c Johannes Sixt2007-10-19 21:47:53 +0200497 const char *prog, int flags)
f7192598 Linus Torvalds2005-07-04 11:57:58 -0700498{
4577370e Daniel Barkalow2007-10-29 21:05:40 -0400499 char *url = xstrdup(url_orig);
8e76bf3f Benjamin Kramer2009-03-13 13:51:33 +0100500 char *host, *path;
356bece0
YH
YOSHIFUJI Hideaki2005-12-21 19:23:42 +0900501 char *end;
502 int c;
98158e9c Johannes Sixt2007-10-19 21:47:53 +0200503 struct child_process *conn;
faea9ccb Andreas Ericsson2005-11-17 20:37:14 +0100504 enum protocol protocol = PROTO_LOCAL;
da2a95b2 Serge E. Hallyn2006-04-17 10:14:47 -0500505 int free_path = 0;
2e776665 Luben Tuikov2007-09-01 02:36:31 -0700506 char *port = NULL;
f364cb88
JS
Johannes Sixt2007-10-19 21:47:54 +0200507 const char **arg;
508 struct strbuf cmd;
faea9ccb Andreas Ericsson2005-11-17 20:37:14 +0100509
f0b7367c
JH
Junio C Hamano2006-06-19 18:25:21 -0700510 /* Without this we cannot rely on waitpid() to tell
511 * what happened to our children.
512 */
513 signal(SIGCHLD, SIG_DFL);
514
faea9ccb
AE
Andreas Ericsson2005-11-17 20:37:14 +0100515 host = strstr(url, "://");
516 if(host) {
517 *host = '\0';
518 protocol = get_protocol(url);
519 host += 3;
356bece0
YH
YOSHIFUJI Hideaki2005-12-21 19:23:42 +0900520 c = '/';
521 } else {
f7192598 Linus Torvalds2005-07-04 11:57:58 -0700522 host = url;
356bece0
YH
YOSHIFUJI Hideaki2005-12-21 19:23:42 +0900523 c = ':';
524 }
525
526 if (host[0] == '[') {
527 end = strchr(host + 1, ']');
528 if (end) {
529 *end = 0;
530 end++;
531 host++;
532 } else
533 end = host;
534 } else
535 end = host;
536
537 path = strchr(end, c);
be501813 Johannes Sixt2007-11-30 22:51:10 +0100538 if (path && !has_dos_drive_prefix(end)) {
72a4f4b6 Linus Torvalds2007-08-01 10:03:37 -0700539 if (c == ':') {
faea9ccb Andreas Ericsson2005-11-17 20:37:14 +0100540 protocol = PROTO_SSH;
356bece0 YOSHIFUJI Hideaki2005-12-21 19:23:42 +0900541 *path++ = '\0';
72a4f4b6
LT
Linus Torvalds2007-08-01 10:03:37 -0700542 }
543 } else
544 path = end;
2386d658 Linus Torvalds2005-07-13 18:46:20 -0700545
faea9ccb
AE
Andreas Ericsson2005-11-17 20:37:14 +0100546 if (!path || !*path)
547 die("No path specified. See 'man git-pull' for valid url syntax");
548
549 /*
550 * null-terminate hostname and point path to ~ for URL's like this:
551 * ssh://host.xz/~user/repo
552 */
553 if (protocol != PROTO_LOCAL && host != url) {
554 char *ptr = path;
555 if (path[1] == '~')
556 path++;
da2a95b2 Serge E. Hallyn2006-04-17 10:14:47 -0500557 else {
9befac47 Shawn O. Pearce2006-09-02 00:16:31 -0400558 path = xstrdup(ptr);
da2a95b2
SH
Serge E. Hallyn2006-04-17 10:14:47 -0500559 free_path = 1;
560 }
faea9ccb
AE
Andreas Ericsson2005-11-17 20:37:14 +0100561
562 *ptr = '\0';
563 }
564
2e776665
LT
Luben Tuikov2007-09-01 02:36:31 -0700565 /*
566 * Add support for ssh port: ssh://host.xy:<port>/...
567 */
568 if (protocol == PROTO_SSH && host != url)
569 port = get_port(host);
570
f8014776 Paul Collins2005-11-04 14:57:16 +0000571 if (protocol == PROTO_GIT) {
5ad312be
JL
Jon Loeliger2006-06-06 22:58:41 -0500572 /* These underlying connection commands die() if they
573 * cannot connect.
574 */
9befac47 Shawn O. Pearce2006-09-02 00:16:31 -0400575 char *target_host = xstrdup(host);
e814bc4d Junio C Hamano2005-11-19 03:48:56 -0800576 if (git_use_proxy(host))
c78963d2 Junio C Hamano2006-06-28 03:50:33 -0700577 git_proxy_connect(fd, host);
da2a95b2 Serge E. Hallyn2006-04-17 10:14:47 -0500578 else
7841ce79 Michael S. Tsirkin2007-05-16 20:09:41 +0300579 git_tcp_connect(fd, host, flags);
5ad312be
JL
Jon Loeliger2006-06-06 22:58:41 -0500580 /*
581 * Separate original protocol components prog and path
582 * from extended components with a NUL byte.
583 */
584 packet_write(fd[1],
585 "%s %s%chost=%s%c",
586 prog, path, 0,
587 target_host, 0);
588 free(target_host);
4577370e Daniel Barkalow2007-10-29 21:05:40 -0400589 free(url);
da2a95b2
SH
Serge E. Hallyn2006-04-17 10:14:47 -0500590 if (free_path)
591 free(path);
fb32c917 Johannes Schindelin2008-02-10 03:06:57 +0000592 return &no_fork;
f8014776 Paul Collins2005-11-04 14:57:16 +0000593 }
2386d658 Linus Torvalds2005-07-13 18:46:20 -0700594
98158e9c Johannes Sixt2007-10-19 21:47:53 +0200595 conn = xcalloc(1, sizeof(*conn));
2e776665 Luben Tuikov2007-09-01 02:36:31 -0700596
f364cb88
JS
Johannes Sixt2007-10-19 21:47:54 +0200597 strbuf_init(&cmd, MAX_CMD_LEN);
598 strbuf_addstr(&cmd, prog);
599 strbuf_addch(&cmd, ' ');
600 sq_quote_buf(&cmd, path);
601 if (cmd.len >= MAX_CMD_LEN)
602 die("command line too long");
603
604 conn->in = conn->out = -1;
36ad53ff Edward Z. Yang2009-05-31 18:15:21 +0200605 conn->argv = arg = xcalloc(7, sizeof(*arg));
f364cb88
JS
Johannes Sixt2007-10-19 21:47:54 +0200606 if (protocol == PROTO_SSH) {
607 const char *ssh = getenv("GIT_SSH");
36ad53ff Edward Z. Yang2009-05-31 18:15:21 +0200608 int putty = ssh && strcasestr(ssh, "plink");
f364cb88
JS
Johannes Sixt2007-10-19 21:47:54 +0200609 if (!ssh) ssh = "ssh";
610
611 *arg++ = ssh;
36ad53ff
EY
Edward Z. Yang2009-05-31 18:15:21 +0200612 if (putty && !strcasestr(ssh, "tortoiseplink"))
613 *arg++ = "-batch";
f364cb88 Johannes Sixt2007-10-19 21:47:54 +0200614 if (port) {
36ad53ff
EY
Edward Z. Yang2009-05-31 18:15:21 +0200615 /* P is for PuTTY, p is for OpenSSH */
616 *arg++ = putty ? "-P" : "-p";
f364cb88 Johannes Sixt2007-10-19 21:47:54 +0200617 *arg++ = port;
4852f723 Martin Sivak2005-08-03 17:15:42 +0200618 }
f364cb88
JS
Johannes Sixt2007-10-19 21:47:54 +0200619 *arg++ = host;
620 }
621 else {
622 /* remove these from the environment */
623 const char *env[] = {
624 ALTERNATE_DB_ENVIRONMENT,
625 DB_ENVIRONMENT,
626 GIT_DIR_ENVIRONMENT,
627 GIT_WORK_TREE_ENVIRONMENT,
628 GRAFT_ENVIRONMENT,
629 INDEX_ENVIRONMENT,
630 NULL
631 };
632 conn->env = env;
633 *arg++ = "sh";
634 *arg++ = "-c";
016fb48b Matt Draisey2006-01-19 15:58:03 -0500635 }
f364cb88
JS
Johannes Sixt2007-10-19 21:47:54 +0200636 *arg++ = cmd.buf;
637 *arg = NULL;
638
639 if (start_command(conn))
640 die("unable to fork");
641
642 fd[0] = conn->out; /* read from child's stdout */
643 fd[1] = conn->in; /* write to child's stdin */
644 strbuf_release(&cmd);
4577370e Daniel Barkalow2007-10-29 21:05:40 -0400645 free(url);
da2a95b2
SH
Serge E. Hallyn2006-04-17 10:14:47 -0500646 if (free_path)
647 free(path);
98158e9c Johannes Sixt2007-10-19 21:47:53 +0200648 return conn;
f7192598
LT
Linus Torvalds2005-07-04 11:57:58 -0700649}
650
98158e9c Johannes Sixt2007-10-19 21:47:53 +0200651int finish_connect(struct child_process *conn)
f7192598 Linus Torvalds2005-07-04 11:57:58 -0700652{
f364cb88 Johannes Sixt2007-10-19 21:47:54 +0200653 int code;
fb32c917 Johannes Schindelin2008-02-10 03:06:57 +0000654 if (!conn || conn == &no_fork)
f42a5c4e
FBH
Franck Bui-Huu2006-09-12 11:00:13 +0200655 return 0;
656
f364cb88
JS
Johannes Sixt2007-10-19 21:47:54 +0200657 code = finish_command(conn);
658 free(conn->argv);
98158e9c Johannes Sixt2007-10-19 21:47:53 +0200659 free(conn);
f364cb88 Johannes Sixt2007-10-19 21:47:54 +0200660 return code;
f7192598 Linus Torvalds2005-07-04 11:57:58 -0700661}