]>
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, | |
c59ab2e5 | 235 | PROTO_FILE, |
2386d658 | 236 | PROTO_SSH, |
4b05548f | 237 | PROTO_GIT |
2386d658 LT |
238 | }; |
239 | ||
c59ab2e5 TB |
240 | int url_is_local_not_ssh(const char *url) |
241 | { | |
242 | const char *colon = strchr(url, ':'); | |
243 | const char *slash = strchr(url, '/'); | |
244 | return !colon || (slash && slash < colon) || | |
245 | has_dos_drive_prefix(url); | |
246 | } | |
247 | ||
5610b7c0 TB |
248 | static const char *prot_name(enum protocol protocol) |
249 | { | |
250 | switch (protocol) { | |
251 | case PROTO_LOCAL: | |
c59ab2e5 | 252 | case PROTO_FILE: |
5610b7c0 TB |
253 | return "file"; |
254 | case PROTO_SSH: | |
255 | return "ssh"; | |
256 | case PROTO_GIT: | |
257 | return "git"; | |
258 | default: | |
259 | return "unkown protocol"; | |
260 | } | |
261 | } | |
262 | ||
2386d658 LT |
263 | static enum protocol get_protocol(const char *name) |
264 | { | |
265 | if (!strcmp(name, "ssh")) | |
266 | return PROTO_SSH; | |
267 | if (!strcmp(name, "git")) | |
268 | return PROTO_GIT; | |
c05186cc LT |
269 | if (!strcmp(name, "git+ssh")) |
270 | return PROTO_SSH; | |
271 | if (!strcmp(name, "ssh+git")) | |
272 | return PROTO_SSH; | |
72a4f4b6 | 273 | if (!strcmp(name, "file")) |
c59ab2e5 | 274 | return PROTO_FILE; |
2386d658 LT |
275 | die("I don't handle protocol '%s'", name); |
276 | } | |
277 | ||
5ba88448 YH |
278 | #define STR_(s) # s |
279 | #define STR(s) STR_(s) | |
2386d658 | 280 | |
72a534da ML |
281 | static void get_host_and_port(char **host, const char **port) |
282 | { | |
283 | char *colon, *end; | |
284 | ||
285 | if (*host[0] == '[') { | |
286 | end = strchr(*host + 1, ']'); | |
287 | if (end) { | |
288 | *end = 0; | |
289 | end++; | |
290 | (*host)++; | |
291 | } else | |
292 | end = *host; | |
293 | } else | |
294 | end = *host; | |
295 | colon = strchr(end, ':'); | |
296 | ||
297 | if (colon) { | |
298 | *colon = 0; | |
299 | *port = colon + 1; | |
300 | } | |
301 | } | |
302 | ||
e47a8583 EW |
303 | static void enable_keepalive(int sockfd) |
304 | { | |
305 | int ka = 1; | |
306 | ||
307 | if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0) | |
308 | fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n", | |
309 | strerror(errno)); | |
310 | } | |
311 | ||
49744d63 | 312 | #ifndef NO_IPV6 |
4c505f71 | 313 | |
ba505322 AR |
314 | static const char *ai_name(const struct addrinfo *ai) |
315 | { | |
785a9857 BK |
316 | static char addr[NI_MAXHOST]; |
317 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0, | |
318 | NI_NUMERICHOST) != 0) | |
ba505322 | 319 | strcpy(addr, "(unknown)"); |
785a9857 | 320 | |
ba505322 AR |
321 | return addr; |
322 | } | |
323 | ||
5ad312be JL |
324 | /* |
325 | * Returns a connected socket() fd, or else die()s. | |
326 | */ | |
7841ce79 | 327 | static int git_tcp_connect_sock(char *host, int flags) |
2386d658 | 328 | { |
63a995b6 DZ |
329 | struct strbuf error_message = STRBUF_INIT; |
330 | int sockfd = -1; | |
554fe20d | 331 | const char *port = STR(DEFAULT_GIT_PORT); |
5ba88448 YH |
332 | struct addrinfo hints, *ai0, *ai; |
333 | int gai; | |
ba505322 | 334 | int cnt = 0; |
5ba88448 | 335 | |
72a534da ML |
336 | get_host_and_port(&host, &port); |
337 | if (!*port) | |
338 | port = "<none>"; | |
5ba88448 YH |
339 | |
340 | memset(&hints, 0, sizeof(hints)); | |
341 | hints.ai_socktype = SOCK_STREAM; | |
342 | hints.ai_protocol = IPPROTO_TCP; | |
343 | ||
7841ce79 MT |
344 | if (flags & CONNECT_VERBOSE) |
345 | fprintf(stderr, "Looking up %s ... ", host); | |
346 | ||
5ba88448 YH |
347 | gai = getaddrinfo(host, port, &hints, &ai); |
348 | if (gai) | |
608d48b2 | 349 | die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai)); |
5ba88448 | 350 | |
7841ce79 MT |
351 | if (flags & CONNECT_VERBOSE) |
352 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
353 | ||
e08afecd | 354 | for (ai0 = ai; ai; ai = ai->ai_next, cnt++) { |
5ad312be JL |
355 | sockfd = socket(ai->ai_family, |
356 | ai->ai_socktype, ai->ai_protocol); | |
63a995b6 DZ |
357 | if ((sockfd < 0) || |
358 | (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) { | |
359 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", | |
360 | host, cnt, ai_name(ai), strerror(errno)); | |
361 | if (0 <= sockfd) | |
362 | close(sockfd); | |
5ba88448 YH |
363 | sockfd = -1; |
364 | continue; | |
2386d658 | 365 | } |
ba505322 AR |
366 | if (flags & CONNECT_VERBOSE) |
367 | fprintf(stderr, "%s ", ai_name(ai)); | |
5ba88448 | 368 | break; |
2386d658 LT |
369 | } |
370 | ||
5ba88448 | 371 | freeaddrinfo(ai0); |
2386d658 | 372 | |
2386d658 | 373 | if (sockfd < 0) |
63a995b6 | 374 | die("unable to connect to %s:\n%s", host, error_message.buf); |
5ba88448 | 375 | |
e47a8583 EW |
376 | enable_keepalive(sockfd); |
377 | ||
7841ce79 MT |
378 | if (flags & CONNECT_VERBOSE) |
379 | fprintf(stderr, "done.\n"); | |
380 | ||
63a995b6 DZ |
381 | strbuf_release(&error_message); |
382 | ||
5ad312be | 383 | return sockfd; |
2386d658 LT |
384 | } |
385 | ||
49744d63 | 386 | #else /* NO_IPV6 */ |
4c505f71 | 387 | |
5ad312be JL |
388 | /* |
389 | * Returns a connected socket() fd, or else die()s. | |
390 | */ | |
7841ce79 | 391 | static int git_tcp_connect_sock(char *host, int flags) |
4c505f71 | 392 | { |
7203a2d1 EFL |
393 | struct strbuf error_message = STRBUF_INIT; |
394 | int sockfd = -1; | |
72a534da ML |
395 | const char *port = STR(DEFAULT_GIT_PORT); |
396 | char *ep; | |
4c505f71 PA |
397 | struct hostent *he; |
398 | struct sockaddr_in sa; | |
399 | char **ap; | |
400 | unsigned int nport; | |
ba505322 | 401 | int cnt; |
4c505f71 | 402 | |
72a534da | 403 | get_host_and_port(&host, &port); |
4c505f71 | 404 | |
7841ce79 MT |
405 | if (flags & CONNECT_VERBOSE) |
406 | fprintf(stderr, "Looking up %s ... ", host); | |
407 | ||
4c505f71 PA |
408 | he = gethostbyname(host); |
409 | if (!he) | |
410 | die("Unable to look up %s (%s)", host, hstrerror(h_errno)); | |
411 | nport = strtoul(port, &ep, 10); | |
412 | if ( ep == port || *ep ) { | |
413 | /* Not numeric */ | |
414 | struct servent *se = getservbyname(port,"tcp"); | |
415 | if ( !se ) | |
d7530708 | 416 | die("Unknown port %s", port); |
4c505f71 PA |
417 | nport = se->s_port; |
418 | } | |
419 | ||
7841ce79 MT |
420 | if (flags & CONNECT_VERBOSE) |
421 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
422 | ||
ba505322 | 423 | for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { |
4c505f71 PA |
424 | memset(&sa, 0, sizeof sa); |
425 | sa.sin_family = he->h_addrtype; | |
6573faff | 426 | sa.sin_port = htons(nport); |
c6164218 | 427 | memcpy(&sa.sin_addr, *ap, he->h_length); |
4c505f71 | 428 | |
7203a2d1 EFL |
429 | sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); |
430 | if ((sockfd < 0) || | |
431 | connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { | |
432 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", | |
ba505322 AR |
433 | host, |
434 | cnt, | |
435 | inet_ntoa(*(struct in_addr *)&sa.sin_addr), | |
7203a2d1 EFL |
436 | strerror(errno)); |
437 | if (0 <= sockfd) | |
438 | close(sockfd); | |
4c505f71 PA |
439 | sockfd = -1; |
440 | continue; | |
441 | } | |
ba505322 AR |
442 | if (flags & CONNECT_VERBOSE) |
443 | fprintf(stderr, "%s ", | |
444 | inet_ntoa(*(struct in_addr *)&sa.sin_addr)); | |
4c505f71 PA |
445 | break; |
446 | } | |
447 | ||
448 | if (sockfd < 0) | |
7203a2d1 | 449 | die("unable to connect to %s:\n%s", host, error_message.buf); |
4c505f71 | 450 | |
e47a8583 EW |
451 | enable_keepalive(sockfd); |
452 | ||
7841ce79 MT |
453 | if (flags & CONNECT_VERBOSE) |
454 | fprintf(stderr, "done.\n"); | |
455 | ||
5ad312be JL |
456 | return sockfd; |
457 | } | |
458 | ||
459 | #endif /* NO_IPV6 */ | |
460 | ||
461 | ||
7841ce79 | 462 | static void git_tcp_connect(int fd[2], char *host, int flags) |
5ad312be | 463 | { |
7841ce79 | 464 | int sockfd = git_tcp_connect_sock(host, flags); |
5ad312be | 465 | |
4c505f71 | 466 | fd[0] = sockfd; |
ec587fde | 467 | fd[1] = dup(sockfd); |
4c505f71 PA |
468 | } |
469 | ||
4c505f71 | 470 | |
96f1e58f | 471 | static char *git_proxy_command; |
f8014776 | 472 | |
ef90d6d4 JS |
473 | static int git_proxy_command_options(const char *var, const char *value, |
474 | void *cb) | |
f8014776 | 475 | { |
e814bc4d | 476 | if (!strcmp(var, "core.gitproxy")) { |
c3df8568 YH |
477 | const char *for_pos; |
478 | int matchlen = -1; | |
479 | int hostlen; | |
15112c95 EFL |
480 | const char *rhost_name = cb; |
481 | int rhost_len = strlen(rhost_name); | |
c3df8568 | 482 | |
e814bc4d | 483 | if (git_proxy_command) |
f8014776 | 484 | return 0; |
c64b9ad0 JH |
485 | if (!value) |
486 | return config_error_nonbool(var); | |
e814bc4d JH |
487 | /* [core] |
488 | * ;# matches www.kernel.org as well | |
489 | * gitproxy = netcatter-1 for kernel.org | |
490 | * gitproxy = netcatter-2 for sample.xz | |
491 | * gitproxy = netcatter-default | |
492 | */ | |
c3df8568 | 493 | for_pos = strstr(value, " for "); |
e814bc4d JH |
494 | if (!for_pos) |
495 | /* matches everybody */ | |
496 | matchlen = strlen(value); | |
497 | else { | |
498 | hostlen = strlen(for_pos + 5); | |
499 | if (rhost_len < hostlen) | |
500 | matchlen = -1; | |
501 | else if (!strncmp(for_pos + 5, | |
502 | rhost_name + rhost_len - hostlen, | |
503 | hostlen) && | |
504 | ((rhost_len == hostlen) || | |
505 | rhost_name[rhost_len - hostlen -1] == '.')) | |
506 | matchlen = for_pos - value; | |
507 | else | |
508 | matchlen = -1; | |
509 | } | |
510 | if (0 <= matchlen) { | |
511 | /* core.gitproxy = none for kernel.org */ | |
a6080a0a | 512 | if (matchlen == 4 && |
e814bc4d JH |
513 | !memcmp(value, "none", 4)) |
514 | matchlen = 0; | |
182af834 | 515 | git_proxy_command = xmemdupz(value, matchlen); |
f8014776 | 516 | } |
e814bc4d | 517 | return 0; |
f8014776 PC |
518 | } |
519 | ||
ef90d6d4 | 520 | return git_default_config(var, value, cb); |
f8014776 PC |
521 | } |
522 | ||
e814bc4d | 523 | static int git_use_proxy(const char *host) |
f8014776 PC |
524 | { |
525 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); | |
15112c95 | 526 | git_config(git_proxy_command_options, (void*)host); |
e814bc4d | 527 | return (git_proxy_command && *git_proxy_command); |
f8014776 PC |
528 | } |
529 | ||
5cbf8246 | 530 | static struct child_process *git_proxy_connect(int fd[2], char *host) |
f8014776 | 531 | { |
554fe20d | 532 | const char *port = STR(DEFAULT_GIT_PORT); |
5cbf8246 JK |
533 | const char **argv; |
534 | struct child_process *proxy; | |
f8014776 | 535 | |
72a534da | 536 | get_host_and_port(&host, &port); |
f8014776 | 537 | |
5cbf8246 | 538 | argv = xmalloc(sizeof(*argv) * 4); |
15a1c012 SP |
539 | argv[0] = git_proxy_command; |
540 | argv[1] = host; | |
541 | argv[2] = port; | |
542 | argv[3] = NULL; | |
5cbf8246 JK |
543 | proxy = xcalloc(1, sizeof(*proxy)); |
544 | proxy->argv = argv; | |
545 | proxy->in = -1; | |
546 | proxy->out = -1; | |
547 | if (start_command(proxy)) | |
15a1c012 | 548 | die("cannot start proxy %s", argv[0]); |
5cbf8246 JK |
549 | fd[0] = proxy->out; /* read from proxy stdout */ |
550 | fd[1] = proxy->in; /* write to proxy stdin */ | |
551 | return proxy; | |
f8014776 PC |
552 | } |
553 | ||
83b05875 | 554 | static const char *get_port_numeric(const char *p) |
2e776665 LT |
555 | { |
556 | char *end; | |
2e776665 | 557 | if (p) { |
8f148253 RS |
558 | long port = strtol(p + 1, &end, 10); |
559 | if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) { | |
83b05875 | 560 | return p; |
2e776665 LT |
561 | } |
562 | } | |
563 | ||
564 | return NULL; | |
565 | } | |
566 | ||
f7192598 | 567 | /* |
cabc3c12 JS |
568 | * Extract protocol and relevant parts from the specified connection URL. |
569 | * The caller must free() the returned strings. | |
f7192598 | 570 | */ |
cabc3c12 | 571 | static enum protocol parse_connect_url(const char *url_orig, char **ret_host, |
83b05875 | 572 | char **ret_path) |
f7192598 | 573 | { |
9d2e9420 | 574 | char *url; |
8e76bf3f | 575 | char *host, *path; |
356bece0 | 576 | char *end; |
c59ab2e5 | 577 | int separator = '/'; |
faea9ccb | 578 | enum protocol protocol = PROTO_LOCAL; |
f0b7367c | 579 | |
9d2e9420 JK |
580 | if (is_url(url_orig)) |
581 | url = url_decode(url_orig); | |
582 | else | |
583 | url = xstrdup(url_orig); | |
584 | ||
faea9ccb | 585 | host = strstr(url, "://"); |
eeefa7c9 | 586 | if (host) { |
faea9ccb AE |
587 | *host = '\0'; |
588 | protocol = get_protocol(url); | |
589 | host += 3; | |
356bece0 | 590 | } else { |
f7192598 | 591 | host = url; |
c59ab2e5 TB |
592 | if (!url_is_local_not_ssh(url)) { |
593 | protocol = PROTO_SSH; | |
594 | separator = ':'; | |
595 | } | |
356bece0 YH |
596 | } |
597 | ||
9aa5053d | 598 | /* |
83b05875 TB |
599 | * Don't do destructive transforms as protocol code does |
600 | * '[]' unwrapping in get_host_and_port() | |
9aa5053d | 601 | */ |
356bece0 YH |
602 | if (host[0] == '[') { |
603 | end = strchr(host + 1, ']'); | |
604 | if (end) { | |
356bece0 | 605 | end++; |
356bece0 YH |
606 | } else |
607 | end = host; | |
608 | } else | |
609 | end = host; | |
610 | ||
c59ab2e5 | 611 | if (protocol == PROTO_LOCAL) |
72a4f4b6 | 612 | path = end; |
c59ab2e5 TB |
613 | else if (protocol == PROTO_FILE && has_dos_drive_prefix(end)) |
614 | path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */ | |
615 | else | |
616 | path = strchr(end, separator); | |
2386d658 | 617 | |
faea9ccb AE |
618 | if (!path || !*path) |
619 | die("No path specified. See 'man git-pull' for valid url syntax"); | |
620 | ||
621 | /* | |
622 | * null-terminate hostname and point path to ~ for URL's like this: | |
623 | * ssh://host.xz/~user/repo | |
624 | */ | |
c59ab2e5 TB |
625 | |
626 | end = path; /* Need to \0 terminate host here */ | |
627 | if (separator == ':') | |
628 | path++; /* path starts after ':' */ | |
629 | if (protocol == PROTO_GIT || protocol == PROTO_SSH) { | |
faea9ccb AE |
630 | if (path[1] == '~') |
631 | path++; | |
faea9ccb AE |
632 | } |
633 | ||
c59ab2e5 TB |
634 | path = xstrdup(path); |
635 | *end = '\0'; | |
636 | ||
cabc3c12 | 637 | *ret_host = xstrdup(host); |
c59ab2e5 | 638 | *ret_path = path; |
cabc3c12 JS |
639 | free(url); |
640 | return protocol; | |
641 | } | |
642 | ||
643 | static struct child_process no_fork; | |
644 | ||
645 | /* | |
646 | * This returns a dummy child_process if the transport protocol does not | |
647 | * need fork(2), or a struct child_process object if it does. Once done, | |
648 | * finish the connection with finish_connect() with the value returned from | |
649 | * this function (it is safe to call finish_connect() with NULL to support | |
650 | * the former case). | |
651 | * | |
652 | * If it returns, the connect is successful; it just dies on errors (this | |
653 | * will hopefully be changed in a libification effort, to return NULL when | |
654 | * the connection failed). | |
655 | */ | |
656 | struct child_process *git_connect(int fd[2], const char *url, | |
657 | const char *prog, int flags) | |
658 | { | |
a2036d7e | 659 | char *hostandport, *path; |
cabc3c12 JS |
660 | struct child_process *conn = &no_fork; |
661 | enum protocol protocol; | |
cabc3c12 JS |
662 | const char **arg; |
663 | struct strbuf cmd = STRBUF_INIT; | |
664 | ||
665 | /* Without this we cannot rely on waitpid() to tell | |
666 | * what happened to our children. | |
667 | */ | |
668 | signal(SIGCHLD, SIG_DFL); | |
669 | ||
a2036d7e | 670 | protocol = parse_connect_url(url, &hostandport, &path); |
5610b7c0 TB |
671 | if (flags & CONNECT_DIAG_URL) { |
672 | printf("Diag: url=%s\n", url ? url : "NULL"); | |
673 | printf("Diag: protocol=%s\n", prot_name(protocol)); | |
a2036d7e | 674 | printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL"); |
5610b7c0 | 675 | printf("Diag: path=%s\n", path ? path : "NULL"); |
a2036d7e TB |
676 | conn = NULL; |
677 | } else if (protocol == PROTO_GIT) { | |
5ad312be JL |
678 | /* These underlying connection commands die() if they |
679 | * cannot connect. | |
680 | */ | |
a2036d7e TB |
681 | char *target_host = xstrdup(hostandport); |
682 | if (git_use_proxy(hostandport)) | |
683 | conn = git_proxy_connect(fd, hostandport); | |
da2a95b2 | 684 | else |
a2036d7e | 685 | git_tcp_connect(fd, hostandport, flags); |
5ad312be JL |
686 | /* |
687 | * Separate original protocol components prog and path | |
73bb33a9 SP |
688 | * from extended host header with a NUL byte. |
689 | * | |
690 | * Note: Do not add any other headers here! Doing so | |
691 | * will cause older git-daemon servers to crash. | |
5ad312be JL |
692 | */ |
693 | packet_write(fd[1], | |
694 | "%s %s%chost=%s%c", | |
695 | prog, path, 0, | |
696 | target_host, 0); | |
697 | free(target_host); | |
a2036d7e TB |
698 | } else { |
699 | conn = xcalloc(1, sizeof(*conn)); | |
700 | ||
701 | strbuf_addstr(&cmd, prog); | |
702 | strbuf_addch(&cmd, ' '); | |
703 | sq_quote_buf(&cmd, path); | |
704 | ||
705 | conn->in = conn->out = -1; | |
706 | conn->argv = arg = xcalloc(7, sizeof(*arg)); | |
707 | if (protocol == PROTO_SSH) { | |
708 | const char *ssh = getenv("GIT_SSH"); | |
709 | int putty = ssh && strcasestr(ssh, "plink"); | |
710 | char *ssh_host = hostandport; | |
711 | const char *port = NULL; | |
712 | get_host_and_port(&ssh_host, &port); | |
713 | port = get_port_numeric(port); | |
714 | ||
715 | if (!ssh) ssh = "ssh"; | |
716 | ||
717 | *arg++ = ssh; | |
718 | if (putty && !strcasestr(ssh, "tortoiseplink")) | |
719 | *arg++ = "-batch"; | |
720 | if (port) { | |
721 | /* P is for PuTTY, p is for OpenSSH */ | |
722 | *arg++ = putty ? "-P" : "-p"; | |
723 | *arg++ = port; | |
724 | } | |
725 | *arg++ = ssh_host; | |
726 | } else { | |
727 | /* remove repo-local variables from the environment */ | |
728 | conn->env = local_repo_env; | |
729 | conn->use_shell = 1; | |
4852f723 | 730 | } |
a2036d7e TB |
731 | *arg++ = cmd.buf; |
732 | *arg = NULL; | |
f364cb88 | 733 | |
a2036d7e TB |
734 | if (start_command(conn)) |
735 | die("unable to fork"); | |
f364cb88 | 736 | |
a2036d7e TB |
737 | fd[0] = conn->out; /* read from child's stdout */ |
738 | fd[1] = conn->in; /* write to child's stdin */ | |
739 | strbuf_release(&cmd); | |
740 | } | |
741 | free(hostandport); | |
cabc3c12 | 742 | free(path); |
98158e9c | 743 | return conn; |
f7192598 LT |
744 | } |
745 | ||
7ffe853b JK |
746 | int git_connection_is_socket(struct child_process *conn) |
747 | { | |
748 | return conn == &no_fork; | |
749 | } | |
750 | ||
98158e9c | 751 | int finish_connect(struct child_process *conn) |
f7192598 | 752 | { |
f364cb88 | 753 | int code; |
7ffe853b | 754 | if (!conn || git_connection_is_socket(conn)) |
f42a5c4e FBH |
755 | return 0; |
756 | ||
f364cb88 JS |
757 | code = finish_command(conn); |
758 | free(conn->argv); | |
98158e9c | 759 | free(conn); |
f364cb88 | 760 | return code; |
f7192598 | 761 | } |