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