]>
Commit | Line | Data |
---|---|---|
731043fd | 1 | #include "git-compat-util.h" |
f7192598 | 2 | #include "cache.h" |
41cb7488 | 3 | #include "pkt-line.h" |
b10d0ec7 | 4 | #include "quote.h" |
6abf5c0c | 5 | #include "refs.h" |
15a1c012 | 6 | #include "run-command.h" |
6b62816c | 7 | #include "remote.h" |
47a59185 | 8 | #include "connect.h" |
9d2e9420 | 9 | #include "url.h" |
a45b5f05 | 10 | #include "string-list.h" |
f7192598 | 11 | |
96f1e58f | 12 | static char *server_capabilities; |
5d54cffc | 13 | static const char *parse_feature_value(const char *, const char *, int *); |
211b5f9e | 14 | |
2718ff09 LT |
15 | static int check_ref(const char *name, int len, unsigned int flags) |
16 | { | |
17 | if (!flags) | |
18 | return 1; | |
19 | ||
c41e20b3 | 20 | if (len < 5 || memcmp(name, "refs/", 5)) |
2718ff09 LT |
21 | return 0; |
22 | ||
23 | /* Skip the "refs/" part */ | |
24 | name += 5; | |
25 | len -= 5; | |
26 | ||
27 | /* REF_NORMAL means that we don't want the magic fake tag refs */ | |
8d9c5010 | 28 | if ((flags & REF_NORMAL) && check_refname_format(name, 0)) |
2718ff09 LT |
29 | return 0; |
30 | ||
31 | /* REF_HEADS means that we want regular branch heads */ | |
32 | if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6)) | |
33 | return 1; | |
34 | ||
35 | /* REF_TAGS means that we want tags */ | |
36 | if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5)) | |
37 | return 1; | |
38 | ||
39 | /* All type bits clear means that we are ok with anything */ | |
40 | return !(flags & ~REF_NORMAL); | |
41 | } | |
42 | ||
4577370e DB |
43 | int check_ref_type(const struct ref *ref, int flags) |
44 | { | |
45 | return check_ref(ref->name, strlen(ref->name), flags); | |
46 | } | |
47 | ||
40c155ff JH |
48 | static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1) |
49 | { | |
50 | ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc); | |
51 | hashcpy(&(extra->array[extra->nr][0]), sha1); | |
52 | extra->nr++; | |
53 | } | |
54 | ||
46284dd1 HV |
55 | static void die_initial_contact(int got_at_least_one_head) |
56 | { | |
57 | if (got_at_least_one_head) | |
58 | die("The remote end hung up upon initial contact"); | |
59 | else | |
60 | die("Could not read from remote repository.\n\n" | |
61 | "Please make sure you have the correct access rights\n" | |
62 | "and the repository exists."); | |
63 | } | |
64 | ||
a45b5f05 JH |
65 | static void parse_one_symref_info(struct string_list *symref, const char *val, int len) |
66 | { | |
67 | char *sym, *target; | |
68 | struct string_list_item *item; | |
69 | ||
70 | if (!len) | |
71 | return; /* just "symref" */ | |
72 | /* e.g. "symref=HEAD:refs/heads/master" */ | |
73 | sym = xmalloc(len + 1); | |
74 | memcpy(sym, val, len); | |
75 | sym[len] = '\0'; | |
76 | target = strchr(sym, ':'); | |
77 | if (!target) | |
78 | /* just "symref=something" */ | |
79 | goto reject; | |
80 | *(target++) = '\0'; | |
81 | if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) || | |
82 | check_refname_format(target, REFNAME_ALLOW_ONELEVEL)) | |
83 | /* "symref=bogus:pair */ | |
84 | goto reject; | |
85 | item = string_list_append(symref, sym); | |
86 | item->util = target; | |
87 | return; | |
88 | reject: | |
89 | free(sym); | |
90 | return; | |
91 | } | |
92 | ||
93 | static void annotate_refs_with_symref_info(struct ref *ref) | |
94 | { | |
95 | struct string_list symref = STRING_LIST_INIT_DUP; | |
96 | const char *feature_list = server_capabilities; | |
97 | ||
98 | while (feature_list) { | |
99 | int len; | |
100 | const char *val; | |
101 | ||
102 | val = parse_feature_value(feature_list, "symref", &len); | |
103 | if (!val) | |
104 | break; | |
105 | parse_one_symref_info(&symref, val, len); | |
106 | feature_list = val + 1; | |
107 | } | |
108 | sort_string_list(&symref); | |
109 | ||
110 | for (; ref; ref = ref->next) { | |
111 | struct string_list_item *item; | |
112 | item = string_list_lookup(&symref, ref->name); | |
113 | if (!item) | |
114 | continue; | |
115 | ref->symref = xstrdup((char *)item->util); | |
116 | } | |
117 | string_list_clear(&symref, 0); | |
118 | } | |
119 | ||
d1c133f5 LT |
120 | /* |
121 | * Read all the refs from the other end | |
122 | */ | |
85edf4f5 JK |
123 | struct ref **get_remote_heads(int in, char *src_buf, size_t src_len, |
124 | struct ref **list, unsigned int flags, | |
40c155ff | 125 | struct extra_have_objects *extra_have) |
d1c133f5 | 126 | { |
a45b5f05 | 127 | struct ref **orig_list = list; |
46284dd1 HV |
128 | int got_at_least_one_head = 0; |
129 | ||
d1c133f5 LT |
130 | *list = NULL; |
131 | for (;;) { | |
132 | struct ref *ref; | |
133 | unsigned char old_sha1[20]; | |
d1c133f5 | 134 | char *name; |
211b5f9e | 135 | int len, name_len; |
74543a04 | 136 | char *buffer = packet_buffer; |
d1c133f5 | 137 | |
85edf4f5 | 138 | len = packet_read(in, &src_buf, &src_len, |
4981fe75 | 139 | packet_buffer, sizeof(packet_buffer), |
819b929d JK |
140 | PACKET_READ_GENTLE_ON_EOF | |
141 | PACKET_READ_CHOMP_NEWLINE); | |
46284dd1 HV |
142 | if (len < 0) |
143 | die_initial_contact(got_at_least_one_head); | |
144 | ||
d1c133f5 LT |
145 | if (!len) |
146 | break; | |
d1c133f5 | 147 | |
a8073289 TPW |
148 | if (len > 4 && !prefixcmp(buffer, "ERR ")) |
149 | die("remote error: %s", buffer + 4); | |
150 | ||
d1c133f5 LT |
151 | if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ') |
152 | die("protocol error: expected sha/ref, got '%s'", buffer); | |
153 | name = buffer + 41; | |
1a7141ff | 154 | |
211b5f9e JS |
155 | name_len = strlen(name); |
156 | if (len != name_len + 41) { | |
8e0f7003 | 157 | free(server_capabilities); |
9befac47 | 158 | server_capabilities = xstrdup(name + name_len + 1); |
211b5f9e JS |
159 | } |
160 | ||
40c155ff JH |
161 | if (extra_have && |
162 | name_len == 5 && !memcmp(".have", name, 5)) { | |
163 | add_extra_have(extra_have, old_sha1); | |
164 | continue; | |
165 | } | |
166 | ||
2718ff09 | 167 | if (!check_ref(name, name_len, flags)) |
cfee10a7 | 168 | continue; |
59c69c0c | 169 | ref = alloc_ref(buffer + 41); |
e702496e | 170 | hashcpy(ref->old_sha1, old_sha1); |
d1c133f5 LT |
171 | *list = ref; |
172 | list = &ref->next; | |
46284dd1 | 173 | got_at_least_one_head = 1; |
d1c133f5 | 174 | } |
a45b5f05 JH |
175 | |
176 | annotate_refs_with_symref_info(*orig_list); | |
177 | ||
d1c133f5 LT |
178 | return list; |
179 | } | |
180 | ||
5d54cffc | 181 | static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp) |
f47182c8 JH |
182 | { |
183 | int len; | |
184 | ||
185 | if (!feature_list) | |
186 | return NULL; | |
187 | ||
188 | len = strlen(feature); | |
189 | while (*feature_list) { | |
190 | const char *found = strstr(feature_list, feature); | |
191 | if (!found) | |
192 | return NULL; | |
94427108 JK |
193 | if (feature_list == found || isspace(found[-1])) { |
194 | const char *value = found + len; | |
195 | /* feature with no value (e.g., "thin-pack") */ | |
196 | if (!*value || isspace(*value)) { | |
197 | if (lenp) | |
198 | *lenp = 0; | |
199 | return value; | |
200 | } | |
201 | /* feature with a value (e.g., "agent=git/1.2.3") */ | |
202 | else if (*value == '=') { | |
203 | value++; | |
204 | if (lenp) | |
205 | *lenp = strcspn(value, " \t\n"); | |
206 | return value; | |
207 | } | |
208 | /* | |
209 | * otherwise we matched a substring of another feature; | |
210 | * keep looking | |
211 | */ | |
212 | } | |
f47182c8 JH |
213 | feature_list = found + 1; |
214 | } | |
215 | return NULL; | |
211b5f9e JS |
216 | } |
217 | ||
94427108 JK |
218 | int parse_feature_request(const char *feature_list, const char *feature) |
219 | { | |
220 | return !!parse_feature_value(feature_list, feature, NULL); | |
221 | } | |
222 | ||
223 | const char *server_feature_value(const char *feature, int *len) | |
224 | { | |
225 | return parse_feature_value(server_capabilities, feature, len); | |
226 | } | |
227 | ||
228 | int server_supports(const char *feature) | |
229 | { | |
230 | return !!server_feature_value(feature, NULL); | |
231 | } | |
232 | ||
2386d658 LT |
233 | enum protocol { |
234 | PROTO_LOCAL = 1, | |
235 | PROTO_SSH, | |
4b05548f | 236 | PROTO_GIT |
2386d658 LT |
237 | }; |
238 | ||
5610b7c0 TB |
239 | static const char *prot_name(enum protocol protocol) |
240 | { | |
241 | switch (protocol) { | |
242 | case PROTO_LOCAL: | |
243 | return "file"; | |
244 | case PROTO_SSH: | |
245 | return "ssh"; | |
246 | case PROTO_GIT: | |
247 | return "git"; | |
248 | default: | |
249 | return "unkown protocol"; | |
250 | } | |
251 | } | |
252 | ||
2386d658 LT |
253 | static enum protocol get_protocol(const char *name) |
254 | { | |
255 | if (!strcmp(name, "ssh")) | |
256 | return PROTO_SSH; | |
257 | if (!strcmp(name, "git")) | |
258 | return PROTO_GIT; | |
c05186cc LT |
259 | if (!strcmp(name, "git+ssh")) |
260 | return PROTO_SSH; | |
261 | if (!strcmp(name, "ssh+git")) | |
262 | return PROTO_SSH; | |
72a4f4b6 LT |
263 | if (!strcmp(name, "file")) |
264 | return PROTO_LOCAL; | |
2386d658 LT |
265 | die("I don't handle protocol '%s'", name); |
266 | } | |
267 | ||
5ba88448 YH |
268 | #define STR_(s) # s |
269 | #define STR(s) STR_(s) | |
2386d658 | 270 | |
72a534da ML |
271 | static void get_host_and_port(char **host, const char **port) |
272 | { | |
273 | char *colon, *end; | |
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 | ||
e47a8583 EW |
293 | static void enable_keepalive(int sockfd) |
294 | { | |
295 | int ka = 1; | |
296 | ||
297 | if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0) | |
298 | fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n", | |
299 | strerror(errno)); | |
300 | } | |
301 | ||
49744d63 | 302 | #ifndef NO_IPV6 |
4c505f71 | 303 | |
ba505322 AR |
304 | static const char *ai_name(const struct addrinfo *ai) |
305 | { | |
785a9857 BK |
306 | static char addr[NI_MAXHOST]; |
307 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0, | |
308 | NI_NUMERICHOST) != 0) | |
ba505322 | 309 | strcpy(addr, "(unknown)"); |
785a9857 | 310 | |
ba505322 AR |
311 | return addr; |
312 | } | |
313 | ||
5ad312be JL |
314 | /* |
315 | * Returns a connected socket() fd, or else die()s. | |
316 | */ | |
7841ce79 | 317 | static int git_tcp_connect_sock(char *host, int flags) |
2386d658 | 318 | { |
63a995b6 DZ |
319 | struct strbuf error_message = STRBUF_INIT; |
320 | int sockfd = -1; | |
554fe20d | 321 | const char *port = STR(DEFAULT_GIT_PORT); |
5ba88448 YH |
322 | struct addrinfo hints, *ai0, *ai; |
323 | int gai; | |
ba505322 | 324 | int cnt = 0; |
5ba88448 | 325 | |
72a534da ML |
326 | get_host_and_port(&host, &port); |
327 | if (!*port) | |
328 | port = "<none>"; | |
5ba88448 YH |
329 | |
330 | memset(&hints, 0, sizeof(hints)); | |
331 | hints.ai_socktype = SOCK_STREAM; | |
332 | hints.ai_protocol = IPPROTO_TCP; | |
333 | ||
7841ce79 MT |
334 | if (flags & CONNECT_VERBOSE) |
335 | fprintf(stderr, "Looking up %s ... ", host); | |
336 | ||
5ba88448 YH |
337 | gai = getaddrinfo(host, port, &hints, &ai); |
338 | if (gai) | |
608d48b2 | 339 | die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai)); |
5ba88448 | 340 | |
7841ce79 MT |
341 | if (flags & CONNECT_VERBOSE) |
342 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
343 | ||
e08afecd | 344 | for (ai0 = ai; ai; ai = ai->ai_next, cnt++) { |
5ad312be JL |
345 | sockfd = socket(ai->ai_family, |
346 | ai->ai_socktype, ai->ai_protocol); | |
63a995b6 DZ |
347 | if ((sockfd < 0) || |
348 | (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) { | |
349 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", | |
350 | host, cnt, ai_name(ai), strerror(errno)); | |
351 | if (0 <= sockfd) | |
352 | close(sockfd); | |
5ba88448 YH |
353 | sockfd = -1; |
354 | continue; | |
2386d658 | 355 | } |
ba505322 AR |
356 | if (flags & CONNECT_VERBOSE) |
357 | fprintf(stderr, "%s ", ai_name(ai)); | |
5ba88448 | 358 | break; |
2386d658 LT |
359 | } |
360 | ||
5ba88448 | 361 | freeaddrinfo(ai0); |
2386d658 | 362 | |
2386d658 | 363 | if (sockfd < 0) |
63a995b6 | 364 | die("unable to connect to %s:\n%s", host, error_message.buf); |
5ba88448 | 365 | |
e47a8583 EW |
366 | enable_keepalive(sockfd); |
367 | ||
7841ce79 MT |
368 | if (flags & CONNECT_VERBOSE) |
369 | fprintf(stderr, "done.\n"); | |
370 | ||
63a995b6 DZ |
371 | strbuf_release(&error_message); |
372 | ||
5ad312be | 373 | return sockfd; |
2386d658 LT |
374 | } |
375 | ||
49744d63 | 376 | #else /* NO_IPV6 */ |
4c505f71 | 377 | |
5ad312be JL |
378 | /* |
379 | * Returns a connected socket() fd, or else die()s. | |
380 | */ | |
7841ce79 | 381 | static int git_tcp_connect_sock(char *host, int flags) |
4c505f71 | 382 | { |
7203a2d1 EFL |
383 | struct strbuf error_message = STRBUF_INIT; |
384 | int sockfd = -1; | |
72a534da ML |
385 | const char *port = STR(DEFAULT_GIT_PORT); |
386 | char *ep; | |
4c505f71 PA |
387 | struct hostent *he; |
388 | struct sockaddr_in sa; | |
389 | char **ap; | |
390 | unsigned int nport; | |
ba505322 | 391 | int cnt; |
4c505f71 | 392 | |
72a534da | 393 | get_host_and_port(&host, &port); |
4c505f71 | 394 | |
7841ce79 MT |
395 | if (flags & CONNECT_VERBOSE) |
396 | fprintf(stderr, "Looking up %s ... ", host); | |
397 | ||
4c505f71 PA |
398 | he = gethostbyname(host); |
399 | if (!he) | |
400 | die("Unable to look up %s (%s)", host, hstrerror(h_errno)); | |
401 | nport = strtoul(port, &ep, 10); | |
402 | if ( ep == port || *ep ) { | |
403 | /* Not numeric */ | |
404 | struct servent *se = getservbyname(port,"tcp"); | |
405 | if ( !se ) | |
d7530708 | 406 | die("Unknown port %s", port); |
4c505f71 PA |
407 | nport = se->s_port; |
408 | } | |
409 | ||
7841ce79 MT |
410 | if (flags & CONNECT_VERBOSE) |
411 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
412 | ||
ba505322 | 413 | for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { |
4c505f71 PA |
414 | memset(&sa, 0, sizeof sa); |
415 | sa.sin_family = he->h_addrtype; | |
6573faff | 416 | sa.sin_port = htons(nport); |
c6164218 | 417 | memcpy(&sa.sin_addr, *ap, he->h_length); |
4c505f71 | 418 | |
7203a2d1 EFL |
419 | sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); |
420 | if ((sockfd < 0) || | |
421 | connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { | |
422 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", | |
ba505322 AR |
423 | host, |
424 | cnt, | |
425 | inet_ntoa(*(struct in_addr *)&sa.sin_addr), | |
7203a2d1 EFL |
426 | strerror(errno)); |
427 | if (0 <= sockfd) | |
428 | close(sockfd); | |
4c505f71 PA |
429 | sockfd = -1; |
430 | continue; | |
431 | } | |
ba505322 AR |
432 | if (flags & CONNECT_VERBOSE) |
433 | fprintf(stderr, "%s ", | |
434 | inet_ntoa(*(struct in_addr *)&sa.sin_addr)); | |
4c505f71 PA |
435 | break; |
436 | } | |
437 | ||
438 | if (sockfd < 0) | |
7203a2d1 | 439 | die("unable to connect to %s:\n%s", host, error_message.buf); |
4c505f71 | 440 | |
e47a8583 EW |
441 | enable_keepalive(sockfd); |
442 | ||
7841ce79 MT |
443 | if (flags & CONNECT_VERBOSE) |
444 | fprintf(stderr, "done.\n"); | |
445 | ||
5ad312be JL |
446 | return sockfd; |
447 | } | |
448 | ||
449 | #endif /* NO_IPV6 */ | |
450 | ||
451 | ||
7841ce79 | 452 | static void git_tcp_connect(int fd[2], char *host, int flags) |
5ad312be | 453 | { |
7841ce79 | 454 | int sockfd = git_tcp_connect_sock(host, flags); |
5ad312be | 455 | |
4c505f71 | 456 | fd[0] = sockfd; |
ec587fde | 457 | fd[1] = dup(sockfd); |
4c505f71 PA |
458 | } |
459 | ||
4c505f71 | 460 | |
96f1e58f | 461 | static char *git_proxy_command; |
f8014776 | 462 | |
ef90d6d4 JS |
463 | static int git_proxy_command_options(const char *var, const char *value, |
464 | void *cb) | |
f8014776 | 465 | { |
e814bc4d | 466 | if (!strcmp(var, "core.gitproxy")) { |
c3df8568 YH |
467 | const char *for_pos; |
468 | int matchlen = -1; | |
469 | int hostlen; | |
15112c95 EFL |
470 | const char *rhost_name = cb; |
471 | int rhost_len = strlen(rhost_name); | |
c3df8568 | 472 | |
e814bc4d | 473 | if (git_proxy_command) |
f8014776 | 474 | return 0; |
c64b9ad0 JH |
475 | if (!value) |
476 | return config_error_nonbool(var); | |
e814bc4d JH |
477 | /* [core] |
478 | * ;# matches www.kernel.org as well | |
479 | * gitproxy = netcatter-1 for kernel.org | |
480 | * gitproxy = netcatter-2 for sample.xz | |
481 | * gitproxy = netcatter-default | |
482 | */ | |
c3df8568 | 483 | for_pos = strstr(value, " for "); |
e814bc4d JH |
484 | if (!for_pos) |
485 | /* matches everybody */ | |
486 | matchlen = strlen(value); | |
487 | else { | |
488 | hostlen = strlen(for_pos + 5); | |
489 | if (rhost_len < hostlen) | |
490 | matchlen = -1; | |
491 | else if (!strncmp(for_pos + 5, | |
492 | rhost_name + rhost_len - hostlen, | |
493 | hostlen) && | |
494 | ((rhost_len == hostlen) || | |
495 | rhost_name[rhost_len - hostlen -1] == '.')) | |
496 | matchlen = for_pos - value; | |
497 | else | |
498 | matchlen = -1; | |
499 | } | |
500 | if (0 <= matchlen) { | |
501 | /* core.gitproxy = none for kernel.org */ | |
a6080a0a | 502 | if (matchlen == 4 && |
e814bc4d JH |
503 | !memcmp(value, "none", 4)) |
504 | matchlen = 0; | |
182af834 | 505 | git_proxy_command = xmemdupz(value, matchlen); |
f8014776 | 506 | } |
e814bc4d | 507 | return 0; |
f8014776 PC |
508 | } |
509 | ||
ef90d6d4 | 510 | return git_default_config(var, value, cb); |
f8014776 PC |
511 | } |
512 | ||
e814bc4d | 513 | static int git_use_proxy(const char *host) |
f8014776 PC |
514 | { |
515 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); | |
15112c95 | 516 | git_config(git_proxy_command_options, (void*)host); |
e814bc4d | 517 | return (git_proxy_command && *git_proxy_command); |
f8014776 PC |
518 | } |
519 | ||
5cbf8246 | 520 | static struct child_process *git_proxy_connect(int fd[2], char *host) |
f8014776 | 521 | { |
554fe20d | 522 | const char *port = STR(DEFAULT_GIT_PORT); |
5cbf8246 JK |
523 | const char **argv; |
524 | struct child_process *proxy; | |
f8014776 | 525 | |
72a534da | 526 | get_host_and_port(&host, &port); |
f8014776 | 527 | |
5cbf8246 | 528 | argv = xmalloc(sizeof(*argv) * 4); |
15a1c012 SP |
529 | argv[0] = git_proxy_command; |
530 | argv[1] = host; | |
531 | argv[2] = port; | |
532 | argv[3] = NULL; | |
5cbf8246 JK |
533 | proxy = xcalloc(1, sizeof(*proxy)); |
534 | proxy->argv = argv; | |
535 | proxy->in = -1; | |
536 | proxy->out = -1; | |
537 | if (start_command(proxy)) | |
15a1c012 | 538 | die("cannot start proxy %s", argv[0]); |
5cbf8246 JK |
539 | fd[0] = proxy->out; /* read from proxy stdout */ |
540 | fd[1] = proxy->in; /* write to proxy stdin */ | |
541 | return proxy; | |
f8014776 PC |
542 | } |
543 | ||
2af202be | 544 | static char *get_port(char *host) |
2e776665 LT |
545 | { |
546 | char *end; | |
547 | char *p = strchr(host, ':'); | |
548 | ||
549 | if (p) { | |
8f148253 RS |
550 | long port = strtol(p + 1, &end, 10); |
551 | if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) { | |
2e776665 LT |
552 | *p = '\0'; |
553 | return p+1; | |
554 | } | |
555 | } | |
556 | ||
557 | return NULL; | |
558 | } | |
559 | ||
f7192598 | 560 | /* |
cabc3c12 JS |
561 | * Extract protocol and relevant parts from the specified connection URL. |
562 | * The caller must free() the returned strings. | |
f7192598 | 563 | */ |
cabc3c12 JS |
564 | static enum protocol parse_connect_url(const char *url_orig, char **ret_host, |
565 | char **ret_port, char **ret_path) | |
f7192598 | 566 | { |
9d2e9420 | 567 | char *url; |
8e76bf3f | 568 | char *host, *path; |
356bece0 | 569 | char *end; |
6a599748 | 570 | int separator; |
faea9ccb | 571 | enum protocol protocol = PROTO_LOCAL; |
da2a95b2 | 572 | int free_path = 0; |
2e776665 | 573 | char *port = NULL; |
f0b7367c | 574 | |
9d2e9420 JK |
575 | if (is_url(url_orig)) |
576 | url = url_decode(url_orig); | |
577 | else | |
578 | url = xstrdup(url_orig); | |
579 | ||
faea9ccb | 580 | host = strstr(url, "://"); |
eeefa7c9 | 581 | if (host) { |
faea9ccb AE |
582 | *host = '\0'; |
583 | protocol = get_protocol(url); | |
584 | host += 3; | |
6a599748 | 585 | separator = '/'; |
356bece0 | 586 | } else { |
f7192598 | 587 | host = url; |
6a599748 | 588 | separator = ':'; |
356bece0 YH |
589 | } |
590 | ||
9aa5053d IL |
591 | /* |
592 | * Don't do destructive transforms with git:// as that | |
9517e6b8 | 593 | * protocol code does '[]' unwrapping of its own. |
9aa5053d | 594 | */ |
356bece0 YH |
595 | if (host[0] == '[') { |
596 | end = strchr(host + 1, ']'); | |
597 | if (end) { | |
9aa5053d IL |
598 | if (protocol != PROTO_GIT) { |
599 | *end = 0; | |
600 | host++; | |
601 | } | |
356bece0 | 602 | end++; |
356bece0 YH |
603 | } else |
604 | end = host; | |
605 | } else | |
606 | end = host; | |
607 | ||
6a599748 | 608 | path = strchr(end, separator); |
be501813 | 609 | if (path && !has_dos_drive_prefix(end)) { |
6a599748 | 610 | if (separator == ':') { |
8d3d28f5 | 611 | if (host != url || path < strchrnul(host, '/')) { |
60003340 NTND |
612 | protocol = PROTO_SSH; |
613 | *path++ = '\0'; | |
614 | } else /* '/' in the host part, assume local path */ | |
615 | path = end; | |
72a4f4b6 LT |
616 | } |
617 | } else | |
618 | path = end; | |
2386d658 | 619 | |
faea9ccb AE |
620 | if (!path || !*path) |
621 | die("No path specified. See 'man git-pull' for valid url syntax"); | |
622 | ||
623 | /* | |
624 | * null-terminate hostname and point path to ~ for URL's like this: | |
625 | * ssh://host.xz/~user/repo | |
626 | */ | |
6a599748 | 627 | if (protocol != PROTO_LOCAL) { |
faea9ccb AE |
628 | char *ptr = path; |
629 | if (path[1] == '~') | |
630 | path++; | |
da2a95b2 | 631 | else { |
9befac47 | 632 | path = xstrdup(ptr); |
da2a95b2 SH |
633 | free_path = 1; |
634 | } | |
faea9ccb AE |
635 | |
636 | *ptr = '\0'; | |
637 | } | |
638 | ||
2e776665 LT |
639 | /* |
640 | * Add support for ssh port: ssh://host.xy:<port>/... | |
641 | */ | |
6a599748 | 642 | if (protocol == PROTO_SSH && separator == '/') |
7acf4382 | 643 | port = get_port(end); |
2e776665 | 644 | |
cabc3c12 JS |
645 | *ret_host = xstrdup(host); |
646 | if (port) | |
647 | *ret_port = xstrdup(port); | |
648 | else | |
649 | *ret_port = NULL; | |
650 | if (free_path) | |
651 | *ret_path = path; | |
652 | else | |
653 | *ret_path = xstrdup(path); | |
654 | free(url); | |
655 | return protocol; | |
656 | } | |
657 | ||
658 | static struct child_process no_fork; | |
659 | ||
660 | /* | |
661 | * This returns a dummy child_process if the transport protocol does not | |
662 | * need fork(2), or a struct child_process object if it does. Once done, | |
663 | * finish the connection with finish_connect() with the value returned from | |
664 | * this function (it is safe to call finish_connect() with NULL to support | |
665 | * the former case). | |
666 | * | |
667 | * If it returns, the connect is successful; it just dies on errors (this | |
668 | * will hopefully be changed in a libification effort, to return NULL when | |
669 | * the connection failed). | |
670 | */ | |
671 | struct child_process *git_connect(int fd[2], const char *url, | |
672 | const char *prog, int flags) | |
673 | { | |
674 | char *host, *path; | |
675 | struct child_process *conn = &no_fork; | |
676 | enum protocol protocol; | |
677 | char *port; | |
678 | const char **arg; | |
679 | struct strbuf cmd = STRBUF_INIT; | |
680 | ||
681 | /* Without this we cannot rely on waitpid() to tell | |
682 | * what happened to our children. | |
683 | */ | |
684 | signal(SIGCHLD, SIG_DFL); | |
685 | ||
686 | protocol = parse_connect_url(url, &host, &port, &path); | |
5610b7c0 TB |
687 | if (flags & CONNECT_DIAG_URL) { |
688 | printf("Diag: url=%s\n", url ? url : "NULL"); | |
689 | printf("Diag: protocol=%s\n", prot_name(protocol)); | |
690 | printf("Diag: hostandport=%s", host ? host : "NULL"); | |
691 | if (port) | |
692 | printf(":%s\n", port); | |
693 | else | |
694 | printf("\n"); | |
695 | printf("Diag: path=%s\n", path ? path : "NULL"); | |
696 | free(host); | |
697 | free(port); | |
698 | free(path); | |
699 | return NULL; | |
700 | } | |
cabc3c12 | 701 | |
f8014776 | 702 | if (protocol == PROTO_GIT) { |
5ad312be JL |
703 | /* These underlying connection commands die() if they |
704 | * cannot connect. | |
705 | */ | |
9befac47 | 706 | char *target_host = xstrdup(host); |
e814bc4d | 707 | if (git_use_proxy(host)) |
5cbf8246 | 708 | conn = git_proxy_connect(fd, host); |
da2a95b2 | 709 | else |
7841ce79 | 710 | git_tcp_connect(fd, host, flags); |
5ad312be JL |
711 | /* |
712 | * Separate original protocol components prog and path | |
73bb33a9 SP |
713 | * from extended host header with a NUL byte. |
714 | * | |
715 | * Note: Do not add any other headers here! Doing so | |
716 | * will cause older git-daemon servers to crash. | |
5ad312be JL |
717 | */ |
718 | packet_write(fd[1], | |
719 | "%s %s%chost=%s%c", | |
720 | prog, path, 0, | |
721 | target_host, 0); | |
722 | free(target_host); | |
cabc3c12 JS |
723 | free(host); |
724 | free(port); | |
725 | free(path); | |
5cbf8246 | 726 | return conn; |
f8014776 | 727 | } |
2386d658 | 728 | |
98158e9c | 729 | conn = xcalloc(1, sizeof(*conn)); |
2e776665 | 730 | |
f364cb88 JS |
731 | strbuf_addstr(&cmd, prog); |
732 | strbuf_addch(&cmd, ' '); | |
733 | sq_quote_buf(&cmd, path); | |
f364cb88 JS |
734 | |
735 | conn->in = conn->out = -1; | |
36ad53ff | 736 | conn->argv = arg = xcalloc(7, sizeof(*arg)); |
f364cb88 JS |
737 | if (protocol == PROTO_SSH) { |
738 | const char *ssh = getenv("GIT_SSH"); | |
36ad53ff | 739 | int putty = ssh && strcasestr(ssh, "plink"); |
f364cb88 JS |
740 | if (!ssh) ssh = "ssh"; |
741 | ||
742 | *arg++ = ssh; | |
36ad53ff EY |
743 | if (putty && !strcasestr(ssh, "tortoiseplink")) |
744 | *arg++ = "-batch"; | |
f364cb88 | 745 | if (port) { |
36ad53ff EY |
746 | /* P is for PuTTY, p is for OpenSSH */ |
747 | *arg++ = putty ? "-P" : "-p"; | |
f364cb88 | 748 | *arg++ = port; |
4852f723 | 749 | } |
f364cb88 JS |
750 | *arg++ = host; |
751 | } | |
752 | else { | |
48a7c1c4 GB |
753 | /* remove repo-local variables from the environment */ |
754 | conn->env = local_repo_env; | |
4cfb2a44 | 755 | conn->use_shell = 1; |
016fb48b | 756 | } |
f364cb88 JS |
757 | *arg++ = cmd.buf; |
758 | *arg = NULL; | |
759 | ||
760 | if (start_command(conn)) | |
761 | die("unable to fork"); | |
762 | ||
763 | fd[0] = conn->out; /* read from child's stdout */ | |
764 | fd[1] = conn->in; /* write to child's stdin */ | |
765 | strbuf_release(&cmd); | |
cabc3c12 JS |
766 | free(host); |
767 | free(port); | |
768 | free(path); | |
98158e9c | 769 | return conn; |
f7192598 LT |
770 | } |
771 | ||
7ffe853b JK |
772 | int git_connection_is_socket(struct child_process *conn) |
773 | { | |
774 | return conn == &no_fork; | |
775 | } | |
776 | ||
98158e9c | 777 | int finish_connect(struct child_process *conn) |
f7192598 | 778 | { |
f364cb88 | 779 | int code; |
7ffe853b | 780 | if (!conn || git_connection_is_socket(conn)) |
f42a5c4e FBH |
781 | return 0; |
782 | ||
f364cb88 JS |
783 | code = finish_command(conn); |
784 | free(conn->argv); | |
98158e9c | 785 | free(conn); |
f364cb88 | 786 | return code; |
f7192598 | 787 | } |