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