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