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