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