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