]>
Commit | Line | Data |
---|---|---|
731043fd | 1 | #include "git-compat-util.h" |
f7192598 | 2 | #include "cache.h" |
b2141fc1 | 3 | #include "config.h" |
41cb7488 | 4 | #include "pkt-line.h" |
b10d0ec7 | 5 | #include "quote.h" |
6abf5c0c | 6 | #include "refs.h" |
15a1c012 | 7 | #include "run-command.h" |
6b62816c | 8 | #include "remote.h" |
47a59185 | 9 | #include "connect.h" |
9d2e9420 | 10 | #include "url.h" |
a45b5f05 | 11 | #include "string-list.h" |
13eb4626 | 12 | #include "sha1-array.h" |
a5adaced | 13 | #include "transport.h" |
0cd83283 | 14 | #include "strbuf.h" |
2609043d | 15 | #include "protocol.h" |
f7192598 | 16 | |
96f1e58f | 17 | static char *server_capabilities; |
5d54cffc | 18 | static const char *parse_feature_value(const char *, const char *, int *); |
211b5f9e | 19 | |
be0b3f82 | 20 | static int check_ref(const char *name, unsigned int flags) |
2718ff09 LT |
21 | { |
22 | if (!flags) | |
23 | return 1; | |
24 | ||
be0b3f82 | 25 | if (!skip_prefix(name, "refs/", &name)) |
2718ff09 LT |
26 | return 0; |
27 | ||
2718ff09 | 28 | /* REF_NORMAL means that we don't want the magic fake tag refs */ |
8d9c5010 | 29 | if ((flags & REF_NORMAL) && check_refname_format(name, 0)) |
2718ff09 LT |
30 | return 0; |
31 | ||
32 | /* REF_HEADS means that we want regular branch heads */ | |
be0b3f82 | 33 | if ((flags & REF_HEADS) && starts_with(name, "heads/")) |
2718ff09 LT |
34 | return 1; |
35 | ||
36 | /* REF_TAGS means that we want tags */ | |
be0b3f82 | 37 | if ((flags & REF_TAGS) && starts_with(name, "tags/")) |
2718ff09 LT |
38 | return 1; |
39 | ||
40 | /* All type bits clear means that we are ok with anything */ | |
41 | return !(flags & ~REF_NORMAL); | |
42 | } | |
43 | ||
4577370e DB |
44 | int check_ref_type(const struct ref *ref, int flags) |
45 | { | |
be0b3f82 | 46 | return check_ref(ref->name, flags); |
4577370e DB |
47 | } |
48 | ||
55e4f936 | 49 | static void die_initial_contact(int unexpected) |
46284dd1 | 50 | { |
55e4f936 | 51 | if (unexpected) |
f2b93b38 | 52 | die(_("The remote end hung up upon initial contact")); |
46284dd1 | 53 | else |
f2b93b38 VA |
54 | die(_("Could not read from remote repository.\n\n" |
55 | "Please make sure you have the correct access rights\n" | |
56 | "and the repository exists.")); | |
46284dd1 HV |
57 | } |
58 | ||
a45b5f05 JH |
59 | static void parse_one_symref_info(struct string_list *symref, const char *val, int len) |
60 | { | |
61 | char *sym, *target; | |
62 | struct string_list_item *item; | |
63 | ||
64 | if (!len) | |
65 | return; /* just "symref" */ | |
66 | /* e.g. "symref=HEAD:refs/heads/master" */ | |
5c0b13f8 | 67 | sym = xmemdupz(val, len); |
a45b5f05 JH |
68 | target = strchr(sym, ':'); |
69 | if (!target) | |
70 | /* just "symref=something" */ | |
71 | goto reject; | |
72 | *(target++) = '\0'; | |
73 | if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) || | |
74 | check_refname_format(target, REFNAME_ALLOW_ONELEVEL)) | |
75 | /* "symref=bogus:pair */ | |
76 | goto reject; | |
ef4fe561 | 77 | item = string_list_append_nodup(symref, sym); |
a45b5f05 JH |
78 | item->util = target; |
79 | return; | |
80 | reject: | |
81 | free(sym); | |
82 | return; | |
83 | } | |
84 | ||
85 | static void annotate_refs_with_symref_info(struct ref *ref) | |
86 | { | |
87 | struct string_list symref = STRING_LIST_INIT_DUP; | |
88 | const char *feature_list = server_capabilities; | |
89 | ||
90 | while (feature_list) { | |
91 | int len; | |
92 | const char *val; | |
93 | ||
94 | val = parse_feature_value(feature_list, "symref", &len); | |
95 | if (!val) | |
96 | break; | |
97 | parse_one_symref_info(&symref, val, len); | |
98 | feature_list = val + 1; | |
99 | } | |
3383e199 | 100 | string_list_sort(&symref); |
a45b5f05 JH |
101 | |
102 | for (; ref; ref = ref->next) { | |
103 | struct string_list_item *item; | |
104 | item = string_list_lookup(&symref, ref->name); | |
105 | if (!item) | |
106 | continue; | |
107 | ref->symref = xstrdup((char *)item->util); | |
108 | } | |
109 | string_list_clear(&symref, 0); | |
110 | } | |
111 | ||
0cd83283 JT |
112 | /* |
113 | * Read one line of a server's ref advertisement into packet_buffer. | |
114 | */ | |
115 | static int read_remote_ref(int in, char **src_buf, size_t *src_len, | |
116 | int *responded) | |
117 | { | |
118 | int len = packet_read(in, src_buf, src_len, | |
119 | packet_buffer, sizeof(packet_buffer), | |
120 | PACKET_READ_GENTLE_ON_EOF | | |
121 | PACKET_READ_CHOMP_NEWLINE); | |
122 | const char *arg; | |
123 | if (len < 0) | |
124 | die_initial_contact(*responded); | |
125 | if (len > 4 && skip_prefix(packet_buffer, "ERR ", &arg)) | |
126 | die("remote error: %s", arg); | |
127 | ||
128 | *responded = 1; | |
129 | ||
130 | return len; | |
131 | } | |
132 | ||
2609043d BW |
133 | #define EXPECTING_PROTOCOL_VERSION 0 |
134 | #define EXPECTING_FIRST_REF 1 | |
135 | #define EXPECTING_REF 2 | |
136 | #define EXPECTING_SHALLOW 3 | |
137 | ||
138 | /* Returns 1 if packet_buffer is a protocol version pkt-line, 0 otherwise. */ | |
139 | static int process_protocol_version(void) | |
140 | { | |
141 | switch (determine_protocol_version_client(packet_buffer)) { | |
142 | case protocol_v1: | |
143 | return 1; | |
144 | case protocol_v0: | |
145 | return 0; | |
146 | default: | |
147 | die("server is speaking an unknown protocol"); | |
148 | } | |
149 | } | |
0cd83283 JT |
150 | |
151 | static void process_capabilities(int *len) | |
152 | { | |
153 | int nul_location = strlen(packet_buffer); | |
154 | if (nul_location == *len) | |
155 | return; | |
156 | server_capabilities = xstrdup(packet_buffer + nul_location + 1); | |
157 | *len = nul_location; | |
158 | } | |
159 | ||
160 | static int process_dummy_ref(void) | |
161 | { | |
162 | struct object_id oid; | |
163 | const char *name; | |
164 | ||
165 | if (parse_oid_hex(packet_buffer, &oid, &name)) | |
166 | return 0; | |
167 | if (*name != ' ') | |
168 | return 0; | |
169 | name++; | |
170 | ||
171 | return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}"); | |
172 | } | |
173 | ||
174 | static void check_no_capabilities(int len) | |
175 | { | |
176 | if (strlen(packet_buffer) != len) | |
177 | warning("Ignoring capabilities after first line '%s'", | |
178 | packet_buffer + strlen(packet_buffer)); | |
179 | } | |
180 | ||
181 | static int process_ref(int len, struct ref ***list, unsigned int flags, | |
182 | struct oid_array *extra_have) | |
183 | { | |
184 | struct object_id old_oid; | |
185 | const char *name; | |
186 | ||
187 | if (parse_oid_hex(packet_buffer, &old_oid, &name)) | |
188 | return 0; | |
189 | if (*name != ' ') | |
190 | return 0; | |
191 | name++; | |
192 | ||
193 | if (extra_have && !strcmp(name, ".have")) { | |
194 | oid_array_append(extra_have, &old_oid); | |
195 | } else if (!strcmp(name, "capabilities^{}")) { | |
196 | die("protocol error: unexpected capabilities^{}"); | |
197 | } else if (check_ref(name, flags)) { | |
198 | struct ref *ref = alloc_ref(name); | |
199 | oidcpy(&ref->old_oid, &old_oid); | |
200 | **list = ref; | |
201 | *list = &ref->next; | |
202 | } | |
203 | check_no_capabilities(len); | |
204 | return 1; | |
205 | } | |
206 | ||
207 | static int process_shallow(int len, struct oid_array *shallow_points) | |
208 | { | |
209 | const char *arg; | |
210 | struct object_id old_oid; | |
211 | ||
212 | if (!skip_prefix(packet_buffer, "shallow ", &arg)) | |
213 | return 0; | |
214 | ||
215 | if (get_oid_hex(arg, &old_oid)) | |
216 | die("protocol error: expected shallow sha-1, got '%s'", arg); | |
217 | if (!shallow_points) | |
218 | die("repository on the other end cannot be shallow"); | |
219 | oid_array_append(shallow_points, &old_oid); | |
220 | check_no_capabilities(len); | |
221 | return 1; | |
222 | } | |
223 | ||
d1c133f5 LT |
224 | /* |
225 | * Read all the refs from the other end | |
226 | */ | |
85edf4f5 JK |
227 | struct ref **get_remote_heads(int in, char *src_buf, size_t src_len, |
228 | struct ref **list, unsigned int flags, | |
910650d2 | 229 | struct oid_array *extra_have, |
230 | struct oid_array *shallow_points) | |
d1c133f5 | 231 | { |
a45b5f05 | 232 | struct ref **orig_list = list; |
55e4f936 JN |
233 | |
234 | /* | |
235 | * A hang-up after seeing some response from the other end | |
236 | * means that it is unexpected, as we know the other end is | |
237 | * willing to talk to us. A hang-up before seeing any | |
238 | * response does not necessarily mean an ACL problem, though. | |
239 | */ | |
0cd83283 JT |
240 | int responded = 0; |
241 | int len; | |
2609043d | 242 | int state = EXPECTING_PROTOCOL_VERSION; |
46284dd1 | 243 | |
d1c133f5 | 244 | *list = NULL; |
1a7141ff | 245 | |
0cd83283 JT |
246 | while ((len = read_remote_ref(in, &src_buf, &src_len, &responded))) { |
247 | switch (state) { | |
2609043d BW |
248 | case EXPECTING_PROTOCOL_VERSION: |
249 | if (process_protocol_version()) { | |
250 | state = EXPECTING_FIRST_REF; | |
251 | break; | |
252 | } | |
253 | state = EXPECTING_FIRST_REF; | |
254 | /* fallthrough */ | |
0cd83283 JT |
255 | case EXPECTING_FIRST_REF: |
256 | process_capabilities(&len); | |
257 | if (process_dummy_ref()) { | |
258 | state = EXPECTING_SHALLOW; | |
259 | break; | |
260 | } | |
261 | state = EXPECTING_REF; | |
262 | /* fallthrough */ | |
263 | case EXPECTING_REF: | |
264 | if (process_ref(len, &list, flags, extra_have)) | |
265 | break; | |
266 | state = EXPECTING_SHALLOW; | |
267 | /* fallthrough */ | |
268 | case EXPECTING_SHALLOW: | |
269 | if (process_shallow(len, shallow_points)) | |
270 | break; | |
271 | die("protocol error: unexpected '%s'", packet_buffer); | |
272 | default: | |
273 | die("unexpected state %d", state); | |
211b5f9e | 274 | } |
d1c133f5 | 275 | } |
a45b5f05 JH |
276 | |
277 | annotate_refs_with_symref_info(*orig_list); | |
278 | ||
d1c133f5 LT |
279 | return list; |
280 | } | |
281 | ||
5d54cffc | 282 | static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp) |
f47182c8 JH |
283 | { |
284 | int len; | |
285 | ||
286 | if (!feature_list) | |
287 | return NULL; | |
288 | ||
289 | len = strlen(feature); | |
290 | while (*feature_list) { | |
291 | const char *found = strstr(feature_list, feature); | |
292 | if (!found) | |
293 | return NULL; | |
94427108 JK |
294 | if (feature_list == found || isspace(found[-1])) { |
295 | const char *value = found + len; | |
296 | /* feature with no value (e.g., "thin-pack") */ | |
297 | if (!*value || isspace(*value)) { | |
298 | if (lenp) | |
299 | *lenp = 0; | |
300 | return value; | |
301 | } | |
302 | /* feature with a value (e.g., "agent=git/1.2.3") */ | |
303 | else if (*value == '=') { | |
304 | value++; | |
305 | if (lenp) | |
306 | *lenp = strcspn(value, " \t\n"); | |
307 | return value; | |
308 | } | |
309 | /* | |
310 | * otherwise we matched a substring of another feature; | |
311 | * keep looking | |
312 | */ | |
313 | } | |
f47182c8 JH |
314 | feature_list = found + 1; |
315 | } | |
316 | return NULL; | |
211b5f9e JS |
317 | } |
318 | ||
94427108 JK |
319 | int parse_feature_request(const char *feature_list, const char *feature) |
320 | { | |
321 | return !!parse_feature_value(feature_list, feature, NULL); | |
322 | } | |
323 | ||
324 | const char *server_feature_value(const char *feature, int *len) | |
325 | { | |
326 | return parse_feature_value(server_capabilities, feature, len); | |
327 | } | |
328 | ||
329 | int server_supports(const char *feature) | |
330 | { | |
331 | return !!server_feature_value(feature, NULL); | |
332 | } | |
333 | ||
2386d658 LT |
334 | enum protocol { |
335 | PROTO_LOCAL = 1, | |
c59ab2e5 | 336 | PROTO_FILE, |
2386d658 | 337 | PROTO_SSH, |
4b05548f | 338 | PROTO_GIT |
2386d658 LT |
339 | }; |
340 | ||
c59ab2e5 TB |
341 | int url_is_local_not_ssh(const char *url) |
342 | { | |
343 | const char *colon = strchr(url, ':'); | |
344 | const char *slash = strchr(url, '/'); | |
345 | return !colon || (slash && slash < colon) || | |
346 | has_dos_drive_prefix(url); | |
347 | } | |
348 | ||
5610b7c0 TB |
349 | static const char *prot_name(enum protocol protocol) |
350 | { | |
351 | switch (protocol) { | |
352 | case PROTO_LOCAL: | |
c59ab2e5 | 353 | case PROTO_FILE: |
5610b7c0 TB |
354 | return "file"; |
355 | case PROTO_SSH: | |
356 | return "ssh"; | |
357 | case PROTO_GIT: | |
358 | return "git"; | |
359 | default: | |
83e6bda3 | 360 | return "unknown protocol"; |
5610b7c0 TB |
361 | } |
362 | } | |
363 | ||
2386d658 LT |
364 | static enum protocol get_protocol(const char *name) |
365 | { | |
366 | if (!strcmp(name, "ssh")) | |
367 | return PROTO_SSH; | |
368 | if (!strcmp(name, "git")) | |
369 | return PROTO_GIT; | |
07c7782c | 370 | if (!strcmp(name, "git+ssh")) /* deprecated - do not use */ |
c05186cc | 371 | return PROTO_SSH; |
07c7782c | 372 | if (!strcmp(name, "ssh+git")) /* deprecated - do not use */ |
c05186cc | 373 | return PROTO_SSH; |
72a4f4b6 | 374 | if (!strcmp(name, "file")) |
c59ab2e5 | 375 | return PROTO_FILE; |
2386d658 LT |
376 | die("I don't handle protocol '%s'", name); |
377 | } | |
378 | ||
86ceb337 TB |
379 | static char *host_end(char **hoststart, int removebrackets) |
380 | { | |
381 | char *host = *hoststart; | |
382 | char *end; | |
383 | char *start = strstr(host, "@["); | |
384 | if (start) | |
385 | start++; /* Jump over '@' */ | |
386 | else | |
387 | start = host; | |
388 | if (start[0] == '[') { | |
389 | end = strchr(start + 1, ']'); | |
390 | if (end) { | |
391 | if (removebrackets) { | |
392 | *end = 0; | |
393 | memmove(start, start + 1, end - start); | |
394 | end++; | |
395 | } | |
396 | } else | |
397 | end = host; | |
398 | } else | |
399 | end = host; | |
400 | return end; | |
401 | } | |
402 | ||
5ba88448 YH |
403 | #define STR_(s) # s |
404 | #define STR(s) STR_(s) | |
2386d658 | 405 | |
72a534da ML |
406 | static void get_host_and_port(char **host, const char **port) |
407 | { | |
408 | char *colon, *end; | |
86ceb337 | 409 | end = host_end(host, 1); |
72a534da | 410 | colon = strchr(end, ':'); |
72a534da | 411 | if (colon) { |
86ceb337 TB |
412 | long portnr = strtol(colon + 1, &end, 10); |
413 | if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) { | |
414 | *colon = 0; | |
415 | *port = colon + 1; | |
6b6c5f7a TB |
416 | } else if (!colon[1]) { |
417 | *colon = 0; | |
86ceb337 | 418 | } |
72a534da ML |
419 | } |
420 | } | |
421 | ||
e47a8583 EW |
422 | static void enable_keepalive(int sockfd) |
423 | { | |
424 | int ka = 1; | |
425 | ||
426 | if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0) | |
427 | fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n", | |
428 | strerror(errno)); | |
429 | } | |
430 | ||
49744d63 | 431 | #ifndef NO_IPV6 |
4c505f71 | 432 | |
ba505322 AR |
433 | static const char *ai_name(const struct addrinfo *ai) |
434 | { | |
785a9857 BK |
435 | static char addr[NI_MAXHOST]; |
436 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0, | |
437 | NI_NUMERICHOST) != 0) | |
5096d490 | 438 | xsnprintf(addr, sizeof(addr), "(unknown)"); |
785a9857 | 439 | |
ba505322 AR |
440 | return addr; |
441 | } | |
442 | ||
5ad312be JL |
443 | /* |
444 | * Returns a connected socket() fd, or else die()s. | |
445 | */ | |
7841ce79 | 446 | static int git_tcp_connect_sock(char *host, int flags) |
2386d658 | 447 | { |
63a995b6 DZ |
448 | struct strbuf error_message = STRBUF_INIT; |
449 | int sockfd = -1; | |
554fe20d | 450 | const char *port = STR(DEFAULT_GIT_PORT); |
5ba88448 YH |
451 | struct addrinfo hints, *ai0, *ai; |
452 | int gai; | |
ba505322 | 453 | int cnt = 0; |
5ba88448 | 454 | |
72a534da ML |
455 | get_host_and_port(&host, &port); |
456 | if (!*port) | |
457 | port = "<none>"; | |
5ba88448 YH |
458 | |
459 | memset(&hints, 0, sizeof(hints)); | |
c915f11e EW |
460 | if (flags & CONNECT_IPV4) |
461 | hints.ai_family = AF_INET; | |
462 | else if (flags & CONNECT_IPV6) | |
463 | hints.ai_family = AF_INET6; | |
5ba88448 YH |
464 | hints.ai_socktype = SOCK_STREAM; |
465 | hints.ai_protocol = IPPROTO_TCP; | |
466 | ||
7841ce79 MT |
467 | if (flags & CONNECT_VERBOSE) |
468 | fprintf(stderr, "Looking up %s ... ", host); | |
469 | ||
5ba88448 YH |
470 | gai = getaddrinfo(host, port, &hints, &ai); |
471 | if (gai) | |
608d48b2 | 472 | die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai)); |
5ba88448 | 473 | |
7841ce79 MT |
474 | if (flags & CONNECT_VERBOSE) |
475 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
476 | ||
e08afecd | 477 | for (ai0 = ai; ai; ai = ai->ai_next, cnt++) { |
5ad312be JL |
478 | sockfd = socket(ai->ai_family, |
479 | ai->ai_socktype, ai->ai_protocol); | |
63a995b6 DZ |
480 | if ((sockfd < 0) || |
481 | (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) { | |
482 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", | |
483 | host, cnt, ai_name(ai), strerror(errno)); | |
484 | if (0 <= sockfd) | |
485 | close(sockfd); | |
5ba88448 YH |
486 | sockfd = -1; |
487 | continue; | |
2386d658 | 488 | } |
ba505322 AR |
489 | if (flags & CONNECT_VERBOSE) |
490 | fprintf(stderr, "%s ", ai_name(ai)); | |
5ba88448 | 491 | break; |
2386d658 LT |
492 | } |
493 | ||
5ba88448 | 494 | freeaddrinfo(ai0); |
2386d658 | 495 | |
2386d658 | 496 | if (sockfd < 0) |
63a995b6 | 497 | die("unable to connect to %s:\n%s", host, error_message.buf); |
5ba88448 | 498 | |
e47a8583 EW |
499 | enable_keepalive(sockfd); |
500 | ||
7841ce79 MT |
501 | if (flags & CONNECT_VERBOSE) |
502 | fprintf(stderr, "done.\n"); | |
503 | ||
63a995b6 DZ |
504 | strbuf_release(&error_message); |
505 | ||
5ad312be | 506 | return sockfd; |
2386d658 LT |
507 | } |
508 | ||
49744d63 | 509 | #else /* NO_IPV6 */ |
4c505f71 | 510 | |
5ad312be JL |
511 | /* |
512 | * Returns a connected socket() fd, or else die()s. | |
513 | */ | |
7841ce79 | 514 | static int git_tcp_connect_sock(char *host, int flags) |
4c505f71 | 515 | { |
7203a2d1 EFL |
516 | struct strbuf error_message = STRBUF_INIT; |
517 | int sockfd = -1; | |
72a534da ML |
518 | const char *port = STR(DEFAULT_GIT_PORT); |
519 | char *ep; | |
4c505f71 PA |
520 | struct hostent *he; |
521 | struct sockaddr_in sa; | |
522 | char **ap; | |
523 | unsigned int nport; | |
ba505322 | 524 | int cnt; |
4c505f71 | 525 | |
72a534da | 526 | get_host_and_port(&host, &port); |
4c505f71 | 527 | |
7841ce79 MT |
528 | if (flags & CONNECT_VERBOSE) |
529 | fprintf(stderr, "Looking up %s ... ", host); | |
530 | ||
4c505f71 PA |
531 | he = gethostbyname(host); |
532 | if (!he) | |
533 | die("Unable to look up %s (%s)", host, hstrerror(h_errno)); | |
534 | nport = strtoul(port, &ep, 10); | |
535 | if ( ep == port || *ep ) { | |
536 | /* Not numeric */ | |
537 | struct servent *se = getservbyname(port,"tcp"); | |
538 | if ( !se ) | |
d7530708 | 539 | die("Unknown port %s", port); |
4c505f71 PA |
540 | nport = se->s_port; |
541 | } | |
542 | ||
7841ce79 MT |
543 | if (flags & CONNECT_VERBOSE) |
544 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
545 | ||
ba505322 | 546 | for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { |
4c505f71 PA |
547 | memset(&sa, 0, sizeof sa); |
548 | sa.sin_family = he->h_addrtype; | |
6573faff | 549 | sa.sin_port = htons(nport); |
c6164218 | 550 | memcpy(&sa.sin_addr, *ap, he->h_length); |
4c505f71 | 551 | |
7203a2d1 EFL |
552 | sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); |
553 | if ((sockfd < 0) || | |
554 | connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { | |
555 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", | |
ba505322 AR |
556 | host, |
557 | cnt, | |
558 | inet_ntoa(*(struct in_addr *)&sa.sin_addr), | |
7203a2d1 EFL |
559 | strerror(errno)); |
560 | if (0 <= sockfd) | |
561 | close(sockfd); | |
4c505f71 PA |
562 | sockfd = -1; |
563 | continue; | |
564 | } | |
ba505322 AR |
565 | if (flags & CONNECT_VERBOSE) |
566 | fprintf(stderr, "%s ", | |
567 | inet_ntoa(*(struct in_addr *)&sa.sin_addr)); | |
4c505f71 PA |
568 | break; |
569 | } | |
570 | ||
571 | if (sockfd < 0) | |
7203a2d1 | 572 | die("unable to connect to %s:\n%s", host, error_message.buf); |
4c505f71 | 573 | |
e47a8583 EW |
574 | enable_keepalive(sockfd); |
575 | ||
7841ce79 MT |
576 | if (flags & CONNECT_VERBOSE) |
577 | fprintf(stderr, "done.\n"); | |
578 | ||
5ad312be JL |
579 | return sockfd; |
580 | } | |
581 | ||
582 | #endif /* NO_IPV6 */ | |
583 | ||
584 | ||
8e349780 JN |
585 | /* |
586 | * Dummy child_process returned by git_connect() if the transport protocol | |
587 | * does not need fork(2). | |
588 | */ | |
589 | static struct child_process no_fork = CHILD_PROCESS_INIT; | |
590 | ||
591 | int git_connection_is_socket(struct child_process *conn) | |
592 | { | |
593 | return conn == &no_fork; | |
594 | } | |
595 | ||
596 | static struct child_process *git_tcp_connect(int fd[2], char *host, int flags) | |
5ad312be | 597 | { |
7841ce79 | 598 | int sockfd = git_tcp_connect_sock(host, flags); |
5ad312be | 599 | |
4c505f71 | 600 | fd[0] = sockfd; |
ec587fde | 601 | fd[1] = dup(sockfd); |
8e349780 JN |
602 | |
603 | return &no_fork; | |
4c505f71 PA |
604 | } |
605 | ||
4c505f71 | 606 | |
96f1e58f | 607 | static char *git_proxy_command; |
f8014776 | 608 | |
ef90d6d4 JS |
609 | static int git_proxy_command_options(const char *var, const char *value, |
610 | void *cb) | |
f8014776 | 611 | { |
e814bc4d | 612 | if (!strcmp(var, "core.gitproxy")) { |
c3df8568 YH |
613 | const char *for_pos; |
614 | int matchlen = -1; | |
615 | int hostlen; | |
15112c95 EFL |
616 | const char *rhost_name = cb; |
617 | int rhost_len = strlen(rhost_name); | |
c3df8568 | 618 | |
e814bc4d | 619 | if (git_proxy_command) |
f8014776 | 620 | return 0; |
c64b9ad0 JH |
621 | if (!value) |
622 | return config_error_nonbool(var); | |
e814bc4d JH |
623 | /* [core] |
624 | * ;# matches www.kernel.org as well | |
625 | * gitproxy = netcatter-1 for kernel.org | |
626 | * gitproxy = netcatter-2 for sample.xz | |
627 | * gitproxy = netcatter-default | |
628 | */ | |
c3df8568 | 629 | for_pos = strstr(value, " for "); |
e814bc4d JH |
630 | if (!for_pos) |
631 | /* matches everybody */ | |
632 | matchlen = strlen(value); | |
633 | else { | |
634 | hostlen = strlen(for_pos + 5); | |
635 | if (rhost_len < hostlen) | |
636 | matchlen = -1; | |
637 | else if (!strncmp(for_pos + 5, | |
638 | rhost_name + rhost_len - hostlen, | |
639 | hostlen) && | |
640 | ((rhost_len == hostlen) || | |
641 | rhost_name[rhost_len - hostlen -1] == '.')) | |
642 | matchlen = for_pos - value; | |
643 | else | |
644 | matchlen = -1; | |
645 | } | |
646 | if (0 <= matchlen) { | |
647 | /* core.gitproxy = none for kernel.org */ | |
a6080a0a | 648 | if (matchlen == 4 && |
e814bc4d JH |
649 | !memcmp(value, "none", 4)) |
650 | matchlen = 0; | |
182af834 | 651 | git_proxy_command = xmemdupz(value, matchlen); |
f8014776 | 652 | } |
e814bc4d | 653 | return 0; |
f8014776 PC |
654 | } |
655 | ||
ef90d6d4 | 656 | return git_default_config(var, value, cb); |
f8014776 PC |
657 | } |
658 | ||
e814bc4d | 659 | static int git_use_proxy(const char *host) |
f8014776 PC |
660 | { |
661 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); | |
15112c95 | 662 | git_config(git_proxy_command_options, (void*)host); |
e814bc4d | 663 | return (git_proxy_command && *git_proxy_command); |
f8014776 PC |
664 | } |
665 | ||
5cbf8246 | 666 | static struct child_process *git_proxy_connect(int fd[2], char *host) |
f8014776 | 667 | { |
554fe20d | 668 | const char *port = STR(DEFAULT_GIT_PORT); |
5cbf8246 | 669 | struct child_process *proxy; |
f8014776 | 670 | |
72a534da | 671 | get_host_and_port(&host, &port); |
f8014776 | 672 | |
3be4cf09 JK |
673 | if (looks_like_command_line_option(host)) |
674 | die("strange hostname '%s' blocked", host); | |
675 | if (looks_like_command_line_option(port)) | |
676 | die("strange port '%s' blocked", port); | |
677 | ||
483bbd4e RS |
678 | proxy = xmalloc(sizeof(*proxy)); |
679 | child_process_init(proxy); | |
1823bea1 JK |
680 | argv_array_push(&proxy->args, git_proxy_command); |
681 | argv_array_push(&proxy->args, host); | |
682 | argv_array_push(&proxy->args, port); | |
5cbf8246 JK |
683 | proxy->in = -1; |
684 | proxy->out = -1; | |
685 | if (start_command(proxy)) | |
1823bea1 | 686 | die("cannot start proxy %s", git_proxy_command); |
5cbf8246 JK |
687 | fd[0] = proxy->out; /* read from proxy stdout */ |
688 | fd[1] = proxy->in; /* write to proxy stdin */ | |
689 | return proxy; | |
f8014776 PC |
690 | } |
691 | ||
86ceb337 | 692 | static char *get_port(char *host) |
2e776665 LT |
693 | { |
694 | char *end; | |
86ceb337 TB |
695 | char *p = strchr(host, ':'); |
696 | ||
2e776665 | 697 | if (p) { |
8f148253 RS |
698 | long port = strtol(p + 1, &end, 10); |
699 | if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) { | |
86ceb337 TB |
700 | *p = '\0'; |
701 | return p+1; | |
2e776665 LT |
702 | } |
703 | } | |
704 | ||
705 | return NULL; | |
706 | } | |
707 | ||
f7192598 | 708 | /* |
cabc3c12 JS |
709 | * Extract protocol and relevant parts from the specified connection URL. |
710 | * The caller must free() the returned strings. | |
f7192598 | 711 | */ |
cabc3c12 | 712 | static enum protocol parse_connect_url(const char *url_orig, char **ret_host, |
83b05875 | 713 | char **ret_path) |
f7192598 | 714 | { |
9d2e9420 | 715 | char *url; |
8e76bf3f | 716 | char *host, *path; |
356bece0 | 717 | char *end; |
c59ab2e5 | 718 | int separator = '/'; |
faea9ccb | 719 | enum protocol protocol = PROTO_LOCAL; |
f0b7367c | 720 | |
9d2e9420 JK |
721 | if (is_url(url_orig)) |
722 | url = url_decode(url_orig); | |
723 | else | |
724 | url = xstrdup(url_orig); | |
725 | ||
faea9ccb | 726 | host = strstr(url, "://"); |
eeefa7c9 | 727 | if (host) { |
faea9ccb AE |
728 | *host = '\0'; |
729 | protocol = get_protocol(url); | |
730 | host += 3; | |
356bece0 | 731 | } else { |
f7192598 | 732 | host = url; |
c59ab2e5 TB |
733 | if (!url_is_local_not_ssh(url)) { |
734 | protocol = PROTO_SSH; | |
735 | separator = ':'; | |
736 | } | |
356bece0 YH |
737 | } |
738 | ||
9aa5053d | 739 | /* |
83b05875 TB |
740 | * Don't do destructive transforms as protocol code does |
741 | * '[]' unwrapping in get_host_and_port() | |
9aa5053d | 742 | */ |
86ceb337 | 743 | end = host_end(&host, 0); |
356bece0 | 744 | |
c59ab2e5 | 745 | if (protocol == PROTO_LOCAL) |
72a4f4b6 | 746 | path = end; |
c59ab2e5 TB |
747 | else if (protocol == PROTO_FILE && has_dos_drive_prefix(end)) |
748 | path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */ | |
749 | else | |
750 | path = strchr(end, separator); | |
2386d658 | 751 | |
faea9ccb AE |
752 | if (!path || !*path) |
753 | die("No path specified. See 'man git-pull' for valid url syntax"); | |
754 | ||
755 | /* | |
756 | * null-terminate hostname and point path to ~ for URL's like this: | |
757 | * ssh://host.xz/~user/repo | |
758 | */ | |
c59ab2e5 TB |
759 | |
760 | end = path; /* Need to \0 terminate host here */ | |
761 | if (separator == ':') | |
762 | path++; /* path starts after ':' */ | |
763 | if (protocol == PROTO_GIT || protocol == PROTO_SSH) { | |
faea9ccb AE |
764 | if (path[1] == '~') |
765 | path++; | |
faea9ccb AE |
766 | } |
767 | ||
c59ab2e5 TB |
768 | path = xstrdup(path); |
769 | *end = '\0'; | |
770 | ||
cabc3c12 | 771 | *ret_host = xstrdup(host); |
c59ab2e5 | 772 | *ret_path = path; |
cabc3c12 JS |
773 | free(url); |
774 | return protocol; | |
775 | } | |
776 | ||
3c8ede3f NTND |
777 | static const char *get_ssh_command(void) |
778 | { | |
779 | const char *ssh; | |
780 | ||
781 | if ((ssh = getenv("GIT_SSH_COMMAND"))) | |
782 | return ssh; | |
783 | ||
784 | if (!git_config_get_string_const("core.sshcommand", &ssh)) | |
785 | return ssh; | |
786 | ||
787 | return NULL; | |
788 | } | |
789 | ||
94b8ae5a | 790 | enum ssh_variant { |
0da0e49b | 791 | VARIANT_AUTO, |
94b8ae5a BW |
792 | VARIANT_SIMPLE, |
793 | VARIANT_SSH, | |
794 | VARIANT_PLINK, | |
795 | VARIANT_PUTTY, | |
796 | VARIANT_TORTOISEPLINK, | |
797 | }; | |
798 | ||
0da0e49b | 799 | static void override_ssh_variant(enum ssh_variant *ssh_variant) |
e2824e47 | 800 | { |
94b8ae5a | 801 | const char *variant = getenv("GIT_SSH_VARIANT"); |
486c8e8c | 802 | |
94b8ae5a | 803 | if (!variant && git_config_get_string_const("ssh.variant", &variant)) |
0da0e49b | 804 | return; |
486c8e8c | 805 | |
0da0e49b JN |
806 | if (!strcmp(variant, "auto")) |
807 | *ssh_variant = VARIANT_AUTO; | |
808 | else if (!strcmp(variant, "plink")) | |
94b8ae5a BW |
809 | *ssh_variant = VARIANT_PLINK; |
810 | else if (!strcmp(variant, "putty")) | |
811 | *ssh_variant = VARIANT_PUTTY; | |
812 | else if (!strcmp(variant, "tortoiseplink")) | |
813 | *ssh_variant = VARIANT_TORTOISEPLINK; | |
814 | else if (!strcmp(variant, "simple")) | |
815 | *ssh_variant = VARIANT_SIMPLE; | |
816 | else | |
817 | *ssh_variant = VARIANT_SSH; | |
486c8e8c JH |
818 | } |
819 | ||
94b8ae5a BW |
820 | static enum ssh_variant determine_ssh_variant(const char *ssh_command, |
821 | int is_cmdline) | |
486c8e8c | 822 | { |
0da0e49b | 823 | enum ssh_variant ssh_variant = VARIANT_AUTO; |
486c8e8c | 824 | const char *variant; |
e2824e47 JS |
825 | char *p = NULL; |
826 | ||
0da0e49b JN |
827 | override_ssh_variant(&ssh_variant); |
828 | ||
829 | if (ssh_variant != VARIANT_AUTO) | |
94b8ae5a | 830 | return ssh_variant; |
486c8e8c JH |
831 | |
832 | if (!is_cmdline) { | |
e2824e47 JS |
833 | p = xstrdup(ssh_command); |
834 | variant = basename(p); | |
835 | } else { | |
836 | const char **ssh_argv; | |
837 | ||
838 | p = xstrdup(ssh_command); | |
22e5ae5c | 839 | if (split_cmdline(p, &ssh_argv) > 0) { |
e2824e47 JS |
840 | variant = basename((char *)ssh_argv[0]); |
841 | /* | |
842 | * At this point, variant points into the buffer | |
843 | * referenced by p, hence we do not need ssh_argv | |
844 | * any longer. | |
845 | */ | |
846 | free(ssh_argv); | |
5d2993b6 JK |
847 | } else { |
848 | free(p); | |
94b8ae5a | 849 | return ssh_variant; |
5d2993b6 | 850 | } |
e2824e47 JS |
851 | } |
852 | ||
94b8ae5a BW |
853 | if (!strcasecmp(variant, "ssh") || |
854 | !strcasecmp(variant, "ssh.exe")) | |
855 | ssh_variant = VARIANT_SSH; | |
856 | else if (!strcasecmp(variant, "plink") || | |
857 | !strcasecmp(variant, "plink.exe")) | |
858 | ssh_variant = VARIANT_PLINK; | |
e2824e47 | 859 | else if (!strcasecmp(variant, "tortoiseplink") || |
94b8ae5a BW |
860 | !strcasecmp(variant, "tortoiseplink.exe")) |
861 | ssh_variant = VARIANT_TORTOISEPLINK; | |
862 | ||
e2824e47 | 863 | free(p); |
94b8ae5a | 864 | return ssh_variant; |
e2824e47 JS |
865 | } |
866 | ||
2ac67cb6 JN |
867 | /* |
868 | * Open a connection using Git's native protocol. | |
869 | * | |
870 | * The caller is responsible for freeing hostandport, but this function may | |
871 | * modify it (for example, to truncate it to remove the port part). | |
872 | */ | |
873 | static struct child_process *git_connect_git(int fd[2], char *hostandport, | |
874 | const char *path, const char *prog, | |
875 | int flags) | |
876 | { | |
877 | struct child_process *conn; | |
878 | struct strbuf request = STRBUF_INIT; | |
879 | /* | |
880 | * Set up virtual host information based on where we will | |
881 | * connect, unless the user has overridden us in | |
882 | * the environment. | |
883 | */ | |
884 | char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST"); | |
885 | if (target_host) | |
886 | target_host = xstrdup(target_host); | |
887 | else | |
888 | target_host = xstrdup(hostandport); | |
889 | ||
890 | transport_check_allowed("git"); | |
891 | ||
233cd282 JN |
892 | /* |
893 | * These underlying connection commands die() if they | |
2ac67cb6 JN |
894 | * cannot connect. |
895 | */ | |
896 | if (git_use_proxy(hostandport)) | |
897 | conn = git_proxy_connect(fd, hostandport); | |
898 | else | |
899 | conn = git_tcp_connect(fd, hostandport, flags); | |
900 | /* | |
901 | * Separate original protocol components prog and path | |
902 | * from extended host header with a NUL byte. | |
903 | * | |
904 | * Note: Do not add any other headers here! Doing so | |
905 | * will cause older git-daemon servers to crash. | |
906 | */ | |
907 | strbuf_addf(&request, | |
908 | "%s %s%chost=%s%c", | |
909 | prog, path, 0, | |
910 | target_host, 0); | |
911 | ||
912 | /* If using a new version put that stuff here after a second null byte */ | |
913 | if (get_protocol_version_config() > 0) { | |
914 | strbuf_addch(&request, '\0'); | |
915 | strbuf_addf(&request, "version=%d%c", | |
916 | get_protocol_version_config(), '\0'); | |
917 | } | |
918 | ||
919 | packet_write(fd[1], request.buf, request.len); | |
920 | ||
921 | free(target_host); | |
922 | strbuf_release(&request); | |
923 | return conn; | |
924 | } | |
925 | ||
957e2ad2 JN |
926 | /* |
927 | * Append the appropriate environment variables to `env` and options to | |
928 | * `args` for running ssh in Git's SSH-tunneled transport. | |
929 | */ | |
930 | static void push_ssh_options(struct argv_array *args, struct argv_array *env, | |
931 | enum ssh_variant variant, const char *port, | |
932 | int flags) | |
933 | { | |
934 | if (variant == VARIANT_SSH && | |
935 | get_protocol_version_config() > 0) { | |
936 | argv_array_push(args, "-o"); | |
937 | argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT); | |
938 | argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d", | |
939 | get_protocol_version_config()); | |
940 | } | |
941 | ||
a3f5b66f JN |
942 | if (flags & CONNECT_IPV4) { |
943 | switch (variant) { | |
944 | case VARIANT_AUTO: | |
945 | BUG("VARIANT_AUTO passed to push_ssh_options"); | |
946 | case VARIANT_SIMPLE: | |
947 | die("ssh variant 'simple' does not support -4"); | |
948 | case VARIANT_SSH: | |
949 | case VARIANT_PLINK: | |
950 | case VARIANT_PUTTY: | |
951 | case VARIANT_TORTOISEPLINK: | |
957e2ad2 | 952 | argv_array_push(args, "-4"); |
a3f5b66f JN |
953 | } |
954 | } else if (flags & CONNECT_IPV6) { | |
955 | switch (variant) { | |
956 | case VARIANT_AUTO: | |
957 | BUG("VARIANT_AUTO passed to push_ssh_options"); | |
958 | case VARIANT_SIMPLE: | |
959 | die("ssh variant 'simple' does not support -6"); | |
960 | case VARIANT_SSH: | |
961 | case VARIANT_PLINK: | |
962 | case VARIANT_PUTTY: | |
963 | case VARIANT_TORTOISEPLINK: | |
957e2ad2 | 964 | argv_array_push(args, "-6"); |
a3f5b66f | 965 | } |
957e2ad2 JN |
966 | } |
967 | ||
968 | if (variant == VARIANT_TORTOISEPLINK) | |
969 | argv_array_push(args, "-batch"); | |
970 | ||
3fa5e0d0 JN |
971 | if (port) { |
972 | switch (variant) { | |
973 | case VARIANT_AUTO: | |
974 | BUG("VARIANT_AUTO passed to push_ssh_options"); | |
975 | case VARIANT_SIMPLE: | |
976 | die("ssh variant 'simple' does not support setting port"); | |
977 | case VARIANT_SSH: | |
957e2ad2 | 978 | argv_array_push(args, "-p"); |
3fa5e0d0 JN |
979 | break; |
980 | case VARIANT_PLINK: | |
981 | case VARIANT_PUTTY: | |
982 | case VARIANT_TORTOISEPLINK: | |
957e2ad2 | 983 | argv_array_push(args, "-P"); |
3fa5e0d0 | 984 | } |
957e2ad2 JN |
985 | |
986 | argv_array_push(args, port); | |
987 | } | |
988 | } | |
989 | ||
fce54ce4 JN |
990 | /* Prepare a child_process for use by Git's SSH-tunneled transport. */ |
991 | static void fill_ssh_args(struct child_process *conn, const char *ssh_host, | |
992 | const char *port, int flags) | |
993 | { | |
994 | const char *ssh; | |
995 | enum ssh_variant variant; | |
996 | ||
997 | if (looks_like_command_line_option(ssh_host)) | |
998 | die("strange hostname '%s' blocked", ssh_host); | |
999 | ||
1000 | ssh = get_ssh_command(); | |
1001 | if (ssh) { | |
1002 | variant = determine_ssh_variant(ssh, 1); | |
1003 | } else { | |
1004 | /* | |
1005 | * GIT_SSH is the no-shell version of | |
1006 | * GIT_SSH_COMMAND (and must remain so for | |
1007 | * historical compatibility). | |
1008 | */ | |
1009 | conn->use_shell = 0; | |
1010 | ||
1011 | ssh = getenv("GIT_SSH"); | |
1012 | if (!ssh) | |
1013 | ssh = "ssh"; | |
1014 | variant = determine_ssh_variant(ssh, 0); | |
1015 | } | |
1016 | ||
0da0e49b JN |
1017 | if (variant == VARIANT_AUTO) { |
1018 | struct child_process detect = CHILD_PROCESS_INIT; | |
1019 | ||
1020 | detect.use_shell = conn->use_shell; | |
1021 | detect.no_stdin = detect.no_stdout = detect.no_stderr = 1; | |
1022 | ||
1023 | argv_array_push(&detect.args, ssh); | |
1024 | argv_array_push(&detect.args, "-G"); | |
1025 | push_ssh_options(&detect.args, &detect.env_array, | |
1026 | VARIANT_SSH, port, flags); | |
1027 | argv_array_push(&detect.args, ssh_host); | |
1028 | ||
1029 | variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH; | |
1030 | } | |
1031 | ||
fce54ce4 | 1032 | argv_array_push(&conn->args, ssh); |
957e2ad2 | 1033 | push_ssh_options(&conn->args, &conn->env_array, variant, port, flags); |
fce54ce4 JN |
1034 | argv_array_push(&conn->args, ssh_host); |
1035 | } | |
1036 | ||
cabc3c12 | 1037 | /* |
8e349780 JN |
1038 | * This returns the dummy child_process `no_fork` if the transport protocol |
1039 | * does not need fork(2), or a struct child_process object if it does. Once | |
1040 | * done, finish the connection with finish_connect() with the value returned | |
1041 | * from this function (it is safe to call finish_connect() with NULL to | |
1042 | * support the former case). | |
cabc3c12 JS |
1043 | * |
1044 | * If it returns, the connect is successful; it just dies on errors (this | |
1045 | * will hopefully be changed in a libification effort, to return NULL when | |
1046 | * the connection failed). | |
1047 | */ | |
1048 | struct child_process *git_connect(int fd[2], const char *url, | |
1049 | const char *prog, int flags) | |
1050 | { | |
a2036d7e | 1051 | char *hostandport, *path; |
8e349780 | 1052 | struct child_process *conn; |
cabc3c12 | 1053 | enum protocol protocol; |
cabc3c12 JS |
1054 | |
1055 | /* Without this we cannot rely on waitpid() to tell | |
1056 | * what happened to our children. | |
2e776665 | 1057 | */ |
cabc3c12 | 1058 | signal(SIGCHLD, SIG_DFL); |
2e776665 | 1059 | |
a2036d7e | 1060 | protocol = parse_connect_url(url, &hostandport, &path); |
3f55ccab | 1061 | if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) { |
5610b7c0 TB |
1062 | printf("Diag: url=%s\n", url ? url : "NULL"); |
1063 | printf("Diag: protocol=%s\n", prot_name(protocol)); | |
a2036d7e | 1064 | printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL"); |
5610b7c0 | 1065 | printf("Diag: path=%s\n", path ? path : "NULL"); |
a2036d7e TB |
1066 | conn = NULL; |
1067 | } else if (protocol == PROTO_GIT) { | |
2ac67cb6 | 1068 | conn = git_connect_git(fd, hostandport, path, prog, flags); |
a2036d7e | 1069 | } else { |
f1399291 | 1070 | struct strbuf cmd = STRBUF_INIT; |
0c2f0d27 | 1071 | const char *const *var; |
f1399291 | 1072 | |
483bbd4e RS |
1073 | conn = xmalloc(sizeof(*conn)); |
1074 | child_process_init(conn); | |
a2036d7e | 1075 | |
aeeb2d49 JK |
1076 | if (looks_like_command_line_option(path)) |
1077 | die("strange pathname '%s' blocked", path); | |
1078 | ||
a2036d7e TB |
1079 | strbuf_addstr(&cmd, prog); |
1080 | strbuf_addch(&cmd, ' '); | |
1081 | sq_quote_buf(&cmd, path); | |
1082 | ||
aab40438 | 1083 | /* remove repo-local variables from the environment */ |
0c2f0d27 BW |
1084 | for (var = local_repo_env; *var; var++) |
1085 | argv_array_push(&conn->env_array, *var); | |
1086 | ||
a48b409f | 1087 | conn->use_shell = 1; |
a2036d7e | 1088 | conn->in = conn->out = -1; |
a2036d7e | 1089 | if (protocol == PROTO_SSH) { |
a2036d7e TB |
1090 | char *ssh_host = hostandport; |
1091 | const char *port = NULL; | |
a5adaced | 1092 | transport_check_allowed("ssh"); |
a2036d7e | 1093 | get_host_and_port(&ssh_host, &port); |
a2036d7e | 1094 | |
86ceb337 TB |
1095 | if (!port) |
1096 | port = get_port(ssh_host); | |
42da4840 | 1097 | |
3f55ccab TB |
1098 | if (flags & CONNECT_DIAG_URL) { |
1099 | printf("Diag: url=%s\n", url ? url : "NULL"); | |
1100 | printf("Diag: protocol=%s\n", prot_name(protocol)); | |
1101 | printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL"); | |
1102 | printf("Diag: port=%s\n", port ? port : "NONE"); | |
1103 | printf("Diag: path=%s\n", path ? path : "NULL"); | |
a2036d7e | 1104 | |
3f55ccab TB |
1105 | free(hostandport); |
1106 | free(path); | |
04f20c04 | 1107 | free(conn); |
f1399291 | 1108 | strbuf_release(&cmd); |
3f55ccab | 1109 | return NULL; |
37ee646e | 1110 | } |
fce54ce4 | 1111 | fill_ssh_args(conn, ssh_host, port, flags); |
c049b61d | 1112 | } else { |
a5adaced | 1113 | transport_check_allowed("file"); |
0c2f0d27 BW |
1114 | if (get_protocol_version_config() > 0) { |
1115 | argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d", | |
1116 | get_protocol_version_config()); | |
1117 | } | |
4852f723 | 1118 | } |
1823bea1 | 1119 | argv_array_push(&conn->args, cmd.buf); |
f364cb88 | 1120 | |
a2036d7e TB |
1121 | if (start_command(conn)) |
1122 | die("unable to fork"); | |
f364cb88 | 1123 | |
a2036d7e TB |
1124 | fd[0] = conn->out; /* read from child's stdout */ |
1125 | fd[1] = conn->in; /* write to child's stdin */ | |
1126 | strbuf_release(&cmd); | |
1127 | } | |
1128 | free(hostandport); | |
cabc3c12 | 1129 | free(path); |
98158e9c | 1130 | return conn; |
f7192598 LT |
1131 | } |
1132 | ||
98158e9c | 1133 | int finish_connect(struct child_process *conn) |
f7192598 | 1134 | { |
f364cb88 | 1135 | int code; |
7ffe853b | 1136 | if (!conn || git_connection_is_socket(conn)) |
f42a5c4e FBH |
1137 | return 0; |
1138 | ||
f364cb88 | 1139 | code = finish_command(conn); |
98158e9c | 1140 | free(conn); |
f364cb88 | 1141 | return code; |
f7192598 | 1142 | } |