blob: b34dc80451fb375b112c1338a0aa3326a0a168ad [file] [log] [blame]
Jason Riedy731043f2006-01-25 12:38:36 -08001#include "git-compat-util.h"
Linus Torvaldsf7192592005-07-04 11:57:58 -07002#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Linus Torvalds41cb7482005-07-05 15:44:09 -07004#include "pkt-line.h"
Junio C Hamanob10d0ec2005-07-08 00:02:52 -07005#include "quote.h"
Junio C Hamano6abf5c02005-10-16 00:25:26 -07006#include "refs.h"
Shawn O. Pearce15a1c0122007-03-12 19:00:19 -04007#include "run-command.h"
Daniel Barkalow6b628162007-05-12 11:45:59 -04008#include "remote.h"
Junio C Hamano47a59182013-07-08 13:56:53 -07009#include "connect.h"
Jeff King9d2e9422010-05-23 05:19:44 -040010#include "url.h"
Junio C Hamanoa45b5f02013-09-17 19:10:31 -070011#include "string-list.h"
Nguyễn Thái Ngọc Duy13eb4622013-12-05 20:02:29 +070012#include "sha1-array.h"
Jeff Kinga5adace2015-09-16 13:12:52 -040013#include "transport.h"
Jonathan Tan0cd83282017-09-26 16:56:19 -070014#include "strbuf.h"
Brandon Williamse52449b2018-03-15 10:31:21 -070015#include "version.h"
Brandon Williams26090432017-10-16 10:55:27 -070016#include "protocol.h"
Linus Torvaldsf7192592005-07-04 11:57:58 -070017
Brandon Williamse52449b2018-03-15 10:31:21 -070018static char *server_capabilities_v1;
19static struct argv_array server_capabilities_v2 = ARGV_ARRAY_INIT;
Junio C Hamano5d54cff2013-09-17 16:29:28 -070020static const char *parse_feature_value(const char *, const char *, int *);
Johannes Schindelin211b5f92005-10-28 04:48:54 +020021
René Scharfebe0b3f82014-08-30 11:46:54 +020022static int check_ref(const char *name, unsigned int flags)
Linus Torvalds2718ff02006-07-04 12:29:10 -070023{
24 if (!flags)
25 return 1;
26
René Scharfebe0b3f82014-08-30 11:46:54 +020027 if (!skip_prefix(name, "refs/", &name))
Linus Torvalds2718ff02006-07-04 12:29:10 -070028 return 0;
29
Linus Torvalds2718ff02006-07-04 12:29:10 -070030 /* REF_NORMAL means that we don't want the magic fake tag refs */
Michael Haggerty8d9c5012011-09-15 23:10:25 +020031 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
Linus Torvalds2718ff02006-07-04 12:29:10 -070032 return 0;
33
34 /* REF_HEADS means that we want regular branch heads */
René Scharfebe0b3f82014-08-30 11:46:54 +020035 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
Linus Torvalds2718ff02006-07-04 12:29:10 -070036 return 1;
37
38 /* REF_TAGS means that we want tags */
René Scharfebe0b3f82014-08-30 11:46:54 +020039 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
Linus Torvalds2718ff02006-07-04 12:29:10 -070040 return 1;
41
42 /* All type bits clear means that we are ok with anything */
43 return !(flags & ~REF_NORMAL);
44}
45
Daniel Barkalow45773702007-10-29 21:05:40 -040046int check_ref_type(const struct ref *ref, int flags)
47{
René Scharfebe0b3f82014-08-30 11:46:54 +020048 return check_ref(ref->name, flags);
Daniel Barkalow45773702007-10-29 21:05:40 -040049}
50
Nguyễn Thái Ngọc Duyd2bff222018-04-14 19:19:43 +000051static NORETURN void die_initial_contact(int unexpected)
Heiko Voigt46284dd2012-06-19 20:24:50 +020052{
Brandon Williams7e3e4792018-03-14 11:31:44 -070053 /*
54 * A hang-up after seeing some response from the other end
55 * means that it is unexpected, as we know the other end is
56 * willing to talk to us. A hang-up before seeing any
57 * response does not necessarily mean an ACL problem, though.
58 */
Jonathan Nieder55e4f932016-09-09 10:36:29 -070059 if (unexpected)
Vasco Almeidaf2b93b32016-09-19 13:08:17 +000060 die(_("The remote end hung up upon initial contact"));
Heiko Voigt46284dd2012-06-19 20:24:50 +020061 else
Vasco Almeidaf2b93b32016-09-19 13:08:17 +000062 die(_("Could not read from remote repository.\n\n"
63 "Please make sure you have the correct access rights\n"
64 "and the repository exists."));
Heiko Voigt46284dd2012-06-19 20:24:50 +020065}
66
Brandon Williamse52449b2018-03-15 10:31:21 -070067/* Checks if the server supports the capability 'c' */
68int server_supports_v2(const char *c, int die_on_error)
69{
70 int i;
71
72 for (i = 0; i < server_capabilities_v2.argc; i++) {
73 const char *out;
74 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
75 (!*out || *out == '='))
76 return 1;
77 }
78
79 if (die_on_error)
80 die("server doesn't support '%s'", c);
81
82 return 0;
83}
84
Brandon Williamsf7e20502018-03-15 10:31:29 -070085int server_supports_feature(const char *c, const char *feature,
86 int die_on_error)
87{
88 int i;
89
90 for (i = 0; i < server_capabilities_v2.argc; i++) {
91 const char *out;
92 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
93 (!*out || *(out++) == '=')) {
94 if (parse_feature_request(out, feature))
95 return 1;
96 else
97 break;
98 }
99 }
100
101 if (die_on_error)
102 die("server doesn't support feature '%s'", feature);
103
104 return 0;
105}
106
Brandon Williamse52449b2018-03-15 10:31:21 -0700107static void process_capabilities_v2(struct packet_reader *reader)
108{
109 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
110 argv_array_push(&server_capabilities_v2, reader->line);
111
112 if (reader->status != PACKET_READ_FLUSH)
113 die("expected flush after capabilities");
114}
115
Brandon Williamsad6ac122018-03-14 11:31:45 -0700116enum protocol_version discover_version(struct packet_reader *reader)
Brandon Williams7e3e4792018-03-14 11:31:44 -0700117{
118 enum protocol_version version = protocol_unknown_version;
119
120 /*
121 * Peek the first line of the server's response to
122 * determine the protocol version the server is speaking.
123 */
124 switch (packet_reader_peek(reader)) {
125 case PACKET_READ_EOF:
126 die_initial_contact(0);
127 case PACKET_READ_FLUSH:
128 case PACKET_READ_DELIM:
129 version = protocol_v0;
130 break;
131 case PACKET_READ_NORMAL:
132 version = determine_protocol_version_client(reader->line);
133 break;
134 }
135
136 switch (version) {
Brandon Williams8f6982b2018-03-14 11:31:47 -0700137 case protocol_v2:
Brandon Williamse52449b2018-03-15 10:31:21 -0700138 process_capabilities_v2(reader);
Brandon Williams8f6982b2018-03-14 11:31:47 -0700139 break;
Brandon Williams7e3e4792018-03-14 11:31:44 -0700140 case protocol_v1:
141 /* Read the peeked version line */
142 packet_reader_read(reader);
143 break;
144 case protocol_v0:
145 break;
146 case protocol_unknown_version:
147 BUG("unknown protocol version");
148 }
149
150 return version;
151}
152
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700153static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
154{
155 char *sym, *target;
156 struct string_list_item *item;
157
158 if (!len)
159 return; /* just "symref" */
160 /* e.g. "symref=HEAD:refs/heads/master" */
René Scharfe5c0b13f2014-07-19 17:35:34 +0200161 sym = xmemdupz(val, len);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700162 target = strchr(sym, ':');
163 if (!target)
164 /* just "symref=something" */
165 goto reject;
166 *(target++) = '\0';
167 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
168 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
169 /* "symref=bogus:pair */
170 goto reject;
Jeff Kingef4fe562017-05-25 15:33:05 -0400171 item = string_list_append_nodup(symref, sym);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700172 item->util = target;
173 return;
174reject:
175 free(sym);
176 return;
177}
178
179static void annotate_refs_with_symref_info(struct ref *ref)
180{
181 struct string_list symref = STRING_LIST_INIT_DUP;
Brandon Williamse52449b2018-03-15 10:31:21 -0700182 const char *feature_list = server_capabilities_v1;
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700183
184 while (feature_list) {
185 int len;
186 const char *val;
187
188 val = parse_feature_value(feature_list, "symref", &len);
189 if (!val)
190 break;
191 parse_one_symref_info(&symref, val, len);
192 feature_list = val + 1;
193 }
Michael Haggerty3383e192014-11-25 09:02:35 +0100194 string_list_sort(&symref);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700195
196 for (; ref; ref = ref->next) {
197 struct string_list_item *item;
198 item = string_list_lookup(&symref, ref->name);
199 if (!item)
200 continue;
201 ref->symref = xstrdup((char *)item->util);
202 }
203 string_list_clear(&symref, 0);
204}
205
Brandon Williams7e3e4792018-03-14 11:31:44 -0700206static void process_capabilities(const char *line, int *len)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700207{
Brandon Williams7e3e4792018-03-14 11:31:44 -0700208 int nul_location = strlen(line);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700209 if (nul_location == *len)
210 return;
Brandon Williamse52449b2018-03-15 10:31:21 -0700211 server_capabilities_v1 = xstrdup(line + nul_location + 1);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700212 *len = nul_location;
213}
214
Brandon Williams7e3e4792018-03-14 11:31:44 -0700215static int process_dummy_ref(const char *line)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700216{
217 struct object_id oid;
218 const char *name;
219
Brandon Williams7e3e4792018-03-14 11:31:44 -0700220 if (parse_oid_hex(line, &oid, &name))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700221 return 0;
222 if (*name != ' ')
223 return 0;
224 name++;
225
226 return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
227}
228
Brandon Williams7e3e4792018-03-14 11:31:44 -0700229static void check_no_capabilities(const char *line, int len)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700230{
Brandon Williams7e3e4792018-03-14 11:31:44 -0700231 if (strlen(line) != len)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700232 warning("Ignoring capabilities after first line '%s'",
Brandon Williams7e3e4792018-03-14 11:31:44 -0700233 line + strlen(line));
Jonathan Tan0cd83282017-09-26 16:56:19 -0700234}
235
Brandon Williams7e3e4792018-03-14 11:31:44 -0700236static int process_ref(const char *line, int len, struct ref ***list,
237 unsigned int flags, struct oid_array *extra_have)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700238{
239 struct object_id old_oid;
240 const char *name;
241
Brandon Williams7e3e4792018-03-14 11:31:44 -0700242 if (parse_oid_hex(line, &old_oid, &name))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700243 return 0;
244 if (*name != ' ')
245 return 0;
246 name++;
247
248 if (extra_have && !strcmp(name, ".have")) {
249 oid_array_append(extra_have, &old_oid);
250 } else if (!strcmp(name, "capabilities^{}")) {
251 die("protocol error: unexpected capabilities^{}");
252 } else if (check_ref(name, flags)) {
253 struct ref *ref = alloc_ref(name);
254 oidcpy(&ref->old_oid, &old_oid);
255 **list = ref;
256 *list = &ref->next;
257 }
Brandon Williams7e3e4792018-03-14 11:31:44 -0700258 check_no_capabilities(line, len);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700259 return 1;
260}
261
Brandon Williams7e3e4792018-03-14 11:31:44 -0700262static int process_shallow(const char *line, int len,
263 struct oid_array *shallow_points)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700264{
265 const char *arg;
266 struct object_id old_oid;
267
Brandon Williams7e3e4792018-03-14 11:31:44 -0700268 if (!skip_prefix(line, "shallow ", &arg))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700269 return 0;
270
271 if (get_oid_hex(arg, &old_oid))
272 die("protocol error: expected shallow sha-1, got '%s'", arg);
273 if (!shallow_points)
274 die("repository on the other end cannot be shallow");
275 oid_array_append(shallow_points, &old_oid);
Brandon Williams7e3e4792018-03-14 11:31:44 -0700276 check_no_capabilities(line, len);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700277 return 1;
278}
279
Brandon Williams7e3e4792018-03-14 11:31:44 -0700280enum get_remote_heads_state {
281 EXPECTING_FIRST_REF = 0,
282 EXPECTING_REF,
283 EXPECTING_SHALLOW,
284 EXPECTING_DONE,
285};
286
Jonathan Tan0cd83282017-09-26 16:56:19 -0700287/*
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700288 * Read all the refs from the other end
289 */
Brandon Williamsad6ac122018-03-14 11:31:45 -0700290struct ref **get_remote_heads(struct packet_reader *reader,
Jeff King85edf4f2013-02-20 15:06:45 -0500291 struct ref **list, unsigned int flags,
brian m. carlson910650d2017-03-31 01:40:00 +0000292 struct oid_array *extra_have,
293 struct oid_array *shallow_points)
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700294{
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700295 struct ref **orig_list = list;
Brandon Williams7e3e4792018-03-14 11:31:44 -0700296 int len = 0;
297 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
Brandon Williams7e3e4792018-03-14 11:31:44 -0700298 const char *arg;
Jonathan Nieder55e4f932016-09-09 10:36:29 -0700299
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700300 *list = NULL;
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700301
Brandon Williams7e3e4792018-03-14 11:31:44 -0700302 while (state != EXPECTING_DONE) {
Brandon Williamsad6ac122018-03-14 11:31:45 -0700303 switch (packet_reader_read(reader)) {
Brandon Williams7e3e4792018-03-14 11:31:44 -0700304 case PACKET_READ_EOF:
305 die_initial_contact(1);
306 case PACKET_READ_NORMAL:
Brandon Williamsad6ac122018-03-14 11:31:45 -0700307 len = reader->pktlen;
308 if (len > 4 && skip_prefix(reader->line, "ERR ", &arg))
Brandon Williams7e3e4792018-03-14 11:31:44 -0700309 die("remote error: %s", arg);
310 break;
311 case PACKET_READ_FLUSH:
312 state = EXPECTING_DONE;
313 break;
314 case PACKET_READ_DELIM:
315 die("invalid packet");
316 }
317
Jonathan Tan0cd83282017-09-26 16:56:19 -0700318 switch (state) {
319 case EXPECTING_FIRST_REF:
Brandon Williamsad6ac122018-03-14 11:31:45 -0700320 process_capabilities(reader->line, &len);
321 if (process_dummy_ref(reader->line)) {
Jonathan Tan0cd83282017-09-26 16:56:19 -0700322 state = EXPECTING_SHALLOW;
323 break;
324 }
325 state = EXPECTING_REF;
326 /* fallthrough */
327 case EXPECTING_REF:
Brandon Williamsad6ac122018-03-14 11:31:45 -0700328 if (process_ref(reader->line, len, &list, flags, extra_have))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700329 break;
330 state = EXPECTING_SHALLOW;
331 /* fallthrough */
332 case EXPECTING_SHALLOW:
Brandon Williamsad6ac122018-03-14 11:31:45 -0700333 if (process_shallow(reader->line, len, shallow_points))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700334 break;
Brandon Williamsad6ac122018-03-14 11:31:45 -0700335 die("protocol error: unexpected '%s'", reader->line);
Brandon Williams7e3e4792018-03-14 11:31:44 -0700336 case EXPECTING_DONE:
337 break;
Nguyễn Thái Ngọc Duyb06dcd72013-12-05 20:02:33 +0700338 }
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700339 }
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700340
341 annotate_refs_with_symref_info(*orig_list);
342
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700343 return list;
344}
345
Brandon Williamse52449b2018-03-15 10:31:21 -0700346/* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
347static int process_ref_v2(const char *line, struct ref ***list)
348{
349 int ret = 1;
350 int i = 0;
351 struct object_id old_oid;
352 struct ref *ref;
353 struct string_list line_sections = STRING_LIST_INIT_DUP;
354 const char *end;
355
356 /*
357 * Ref lines have a number of fields which are space deliminated. The
358 * first field is the OID of the ref. The second field is the ref
359 * name. Subsequent fields (symref-target and peeled) are optional and
360 * don't have a particular order.
361 */
362 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
363 ret = 0;
364 goto out;
365 }
366
367 if (parse_oid_hex(line_sections.items[i++].string, &old_oid, &end) ||
368 *end) {
369 ret = 0;
370 goto out;
371 }
372
373 ref = alloc_ref(line_sections.items[i++].string);
374
375 oidcpy(&ref->old_oid, &old_oid);
376 **list = ref;
377 *list = &ref->next;
378
379 for (; i < line_sections.nr; i++) {
380 const char *arg = line_sections.items[i].string;
381 if (skip_prefix(arg, "symref-target:", &arg))
382 ref->symref = xstrdup(arg);
383
384 if (skip_prefix(arg, "peeled:", &arg)) {
385 struct object_id peeled_oid;
386 char *peeled_name;
387 struct ref *peeled;
388 if (parse_oid_hex(arg, &peeled_oid, &end) || *end) {
389 ret = 0;
390 goto out;
391 }
392
393 peeled_name = xstrfmt("%s^{}", ref->name);
394 peeled = alloc_ref(peeled_name);
395
396 oidcpy(&peeled->old_oid, &peeled_oid);
397 **list = peeled;
398 *list = &peeled->next;
399
400 free(peeled_name);
401 }
402 }
403
404out:
405 string_list_clear(&line_sections, 0);
406 return ret;
407}
408
409struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
410 struct ref **list, int for_push,
411 const struct argv_array *ref_prefixes)
412{
413 int i;
414 *list = NULL;
415
416 if (server_supports_v2("ls-refs", 1))
417 packet_write_fmt(fd_out, "command=ls-refs\n");
418
419 if (server_supports_v2("agent", 0))
420 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
421
422 packet_delim(fd_out);
423 /* When pushing we don't want to request the peeled tags */
424 if (!for_push)
425 packet_write_fmt(fd_out, "peel\n");
426 packet_write_fmt(fd_out, "symrefs\n");
427 for (i = 0; ref_prefixes && i < ref_prefixes->argc; i++) {
428 packet_write_fmt(fd_out, "ref-prefix %s\n",
429 ref_prefixes->argv[i]);
430 }
431 packet_flush(fd_out);
432
433 /* Process response from server */
434 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
435 if (!process_ref_v2(reader->line, &list))
436 die("invalid ls-refs response: %s", reader->line);
437 }
438
439 if (reader->status != PACKET_READ_FLUSH)
440 die("expected flush after ref listing");
441
442 return list;
443}
444
Junio C Hamano5d54cff2013-09-17 16:29:28 -0700445static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
Junio C Hamanof47182c2012-01-08 22:06:19 +0100446{
447 int len;
448
449 if (!feature_list)
450 return NULL;
451
452 len = strlen(feature);
453 while (*feature_list) {
454 const char *found = strstr(feature_list, feature);
455 if (!found)
456 return NULL;
Jeff King94427102012-08-13 21:59:27 -0400457 if (feature_list == found || isspace(found[-1])) {
458 const char *value = found + len;
459 /* feature with no value (e.g., "thin-pack") */
460 if (!*value || isspace(*value)) {
461 if (lenp)
462 *lenp = 0;
463 return value;
464 }
465 /* feature with a value (e.g., "agent=git/1.2.3") */
466 else if (*value == '=') {
467 value++;
468 if (lenp)
469 *lenp = strcspn(value, " \t\n");
470 return value;
471 }
472 /*
473 * otherwise we matched a substring of another feature;
474 * keep looking
475 */
476 }
Junio C Hamanof47182c2012-01-08 22:06:19 +0100477 feature_list = found + 1;
478 }
479 return NULL;
Johannes Schindelin211b5f92005-10-28 04:48:54 +0200480}
481
Jeff King94427102012-08-13 21:59:27 -0400482int parse_feature_request(const char *feature_list, const char *feature)
483{
484 return !!parse_feature_value(feature_list, feature, NULL);
485}
486
487const char *server_feature_value(const char *feature, int *len)
488{
Brandon Williamse52449b2018-03-15 10:31:21 -0700489 return parse_feature_value(server_capabilities_v1, feature, len);
Jeff King94427102012-08-13 21:59:27 -0400490}
491
492int server_supports(const char *feature)
493{
494 return !!server_feature_value(feature, NULL);
495}
496
Linus Torvalds2386d652005-07-13 18:46:20 -0700497enum protocol {
498 PROTO_LOCAL = 1,
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100499 PROTO_FILE,
Linus Torvalds2386d652005-07-13 18:46:20 -0700500 PROTO_SSH,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000501 PROTO_GIT
Linus Torvalds2386d652005-07-13 18:46:20 -0700502};
503
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100504int url_is_local_not_ssh(const char *url)
505{
506 const char *colon = strchr(url, ':');
507 const char *slash = strchr(url, '/');
508 return !colon || (slash && slash < colon) ||
509 has_dos_drive_prefix(url);
510}
511
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +0100512static const char *prot_name(enum protocol protocol)
513{
514 switch (protocol) {
515 case PROTO_LOCAL:
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100516 case PROTO_FILE:
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +0100517 return "file";
518 case PROTO_SSH:
519 return "ssh";
520 case PROTO_GIT:
521 return "git";
522 default:
Tobias Klauser83e6bda2015-09-24 14:44:49 +0200523 return "unknown protocol";
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +0100524 }
525}
526
Linus Torvalds2386d652005-07-13 18:46:20 -0700527static enum protocol get_protocol(const char *name)
528{
529 if (!strcmp(name, "ssh"))
530 return PROTO_SSH;
531 if (!strcmp(name, "git"))
532 return PROTO_GIT;
Carlos Martín Nieto07c77822016-02-15 15:29:06 +0100533 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
Linus Torvaldsc05186c2005-10-14 17:14:56 -0700534 return PROTO_SSH;
Carlos Martín Nieto07c77822016-02-15 15:29:06 +0100535 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
Linus Torvaldsc05186c2005-10-14 17:14:56 -0700536 return PROTO_SSH;
Linus Torvalds72a4f4b2007-08-01 10:03:37 -0700537 if (!strcmp(name, "file"))
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100538 return PROTO_FILE;
Linus Torvalds2386d652005-07-13 18:46:20 -0700539 die("I don't handle protocol '%s'", name);
540}
541
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100542static char *host_end(char **hoststart, int removebrackets)
543{
544 char *host = *hoststart;
545 char *end;
546 char *start = strstr(host, "@[");
547 if (start)
548 start++; /* Jump over '@' */
549 else
550 start = host;
551 if (start[0] == '[') {
552 end = strchr(start + 1, ']');
553 if (end) {
554 if (removebrackets) {
555 *end = 0;
556 memmove(start, start + 1, end - start);
557 end++;
558 }
559 } else
560 end = host;
561 } else
562 end = host;
563 return end;
564}
565
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400566#define STR_(s) # s
567#define STR(s) STR_(s)
Linus Torvalds2386d652005-07-13 18:46:20 -0700568
Michael Lukashov72a534d2010-02-17 20:56:02 +0000569static void get_host_and_port(char **host, const char **port)
570{
571 char *colon, *end;
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100572 end = host_end(host, 1);
Michael Lukashov72a534d2010-02-17 20:56:02 +0000573 colon = strchr(end, ':');
Michael Lukashov72a534d2010-02-17 20:56:02 +0000574 if (colon) {
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100575 long portnr = strtol(colon + 1, &end, 10);
576 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
577 *colon = 0;
578 *port = colon + 1;
Torsten Bögershausen6b6c5f72015-04-07 22:03:25 +0200579 } else if (!colon[1]) {
580 *colon = 0;
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100581 }
Michael Lukashov72a534d2010-02-17 20:56:02 +0000582 }
583}
584
Eric Wonge47a8582011-12-06 04:39:36 +0000585static void enable_keepalive(int sockfd)
586{
587 int ka = 1;
588
589 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
590 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
591 strerror(errno));
592}
593
hpa49744d62005-09-28 16:52:21 -0700594#ifndef NO_IPV6
hpa4c505f72005-09-28 16:37:58 -0700595
Alex Riesenba505322007-05-23 23:34:27 +0200596static const char *ai_name(const struct addrinfo *ai)
597{
Benjamin Kramer785a9852009-04-24 14:16:41 +0200598 static char addr[NI_MAXHOST];
599 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
600 NI_NUMERICHOST) != 0)
Jeff King5096d492015-09-24 17:06:08 -0400601 xsnprintf(addr, sizeof(addr), "(unknown)");
Benjamin Kramer785a9852009-04-24 14:16:41 +0200602
Alex Riesenba505322007-05-23 23:34:27 +0200603 return addr;
604}
605
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500606/*
607 * Returns a connected socket() fd, or else die()s.
608 */
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300609static int git_tcp_connect_sock(char *host, int flags)
Linus Torvalds2386d652005-07-13 18:46:20 -0700610{
Dave Zarzycki63a995b2011-07-12 09:28:34 -0700611 struct strbuf error_message = STRBUF_INIT;
612 int sockfd = -1;
Timo Hirvonen554fe202006-06-28 12:04:39 +0300613 const char *port = STR(DEFAULT_GIT_PORT);
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400614 struct addrinfo hints, *ai0, *ai;
615 int gai;
Alex Riesenba505322007-05-23 23:34:27 +0200616 int cnt = 0;
Linus Torvalds2386d652005-07-13 18:46:20 -0700617
Michael Lukashov72a534d2010-02-17 20:56:02 +0000618 get_host_and_port(&host, &port);
619 if (!*port)
620 port = "<none>";
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400621
622 memset(&hints, 0, sizeof(hints));
Eric Wongc915f112016-02-03 04:09:14 +0000623 if (flags & CONNECT_IPV4)
624 hints.ai_family = AF_INET;
625 else if (flags & CONNECT_IPV6)
626 hints.ai_family = AF_INET6;
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400627 hints.ai_socktype = SOCK_STREAM;
628 hints.ai_protocol = IPPROTO_TCP;
629
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300630 if (flags & CONNECT_VERBOSE)
631 fprintf(stderr, "Looking up %s ... ", host);
632
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400633 gai = getaddrinfo(host, port, &hints, &ai);
634 if (gai)
Linus Torvalds608d48b2007-03-27 09:50:20 -0700635 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400636
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300637 if (flags & CONNECT_VERBOSE)
638 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
639
Erik Faye-Lunde08afec2011-08-01 13:16:09 +0200640 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500641 sockfd = socket(ai->ai_family,
642 ai->ai_socktype, ai->ai_protocol);
Dave Zarzycki63a995b2011-07-12 09:28:34 -0700643 if ((sockfd < 0) ||
644 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
645 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
646 host, cnt, ai_name(ai), strerror(errno));
647 if (0 <= sockfd)
648 close(sockfd);
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400649 sockfd = -1;
650 continue;
Linus Torvalds2386d652005-07-13 18:46:20 -0700651 }
Alex Riesenba505322007-05-23 23:34:27 +0200652 if (flags & CONNECT_VERBOSE)
653 fprintf(stderr, "%s ", ai_name(ai));
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400654 break;
Linus Torvalds2386d652005-07-13 18:46:20 -0700655 }
656
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400657 freeaddrinfo(ai0);
Linus Torvalds2386d652005-07-13 18:46:20 -0700658
Linus Torvalds2386d652005-07-13 18:46:20 -0700659 if (sockfd < 0)
Dave Zarzycki63a995b2011-07-12 09:28:34 -0700660 die("unable to connect to %s:\n%s", host, error_message.buf);
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400661
Eric Wonge47a8582011-12-06 04:39:36 +0000662 enable_keepalive(sockfd);
663
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300664 if (flags & CONNECT_VERBOSE)
665 fprintf(stderr, "done.\n");
666
Dave Zarzycki63a995b2011-07-12 09:28:34 -0700667 strbuf_release(&error_message);
668
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500669 return sockfd;
Linus Torvalds2386d652005-07-13 18:46:20 -0700670}
671
hpa49744d62005-09-28 16:52:21 -0700672#else /* NO_IPV6 */
hpa4c505f72005-09-28 16:37:58 -0700673
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500674/*
675 * Returns a connected socket() fd, or else die()s.
676 */
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300677static int git_tcp_connect_sock(char *host, int flags)
hpa4c505f72005-09-28 16:37:58 -0700678{
Erik Faye-Lund7203a2d2011-08-01 13:16:10 +0200679 struct strbuf error_message = STRBUF_INIT;
680 int sockfd = -1;
Michael Lukashov72a534d2010-02-17 20:56:02 +0000681 const char *port = STR(DEFAULT_GIT_PORT);
682 char *ep;
hpa4c505f72005-09-28 16:37:58 -0700683 struct hostent *he;
684 struct sockaddr_in sa;
685 char **ap;
686 unsigned int nport;
Alex Riesenba505322007-05-23 23:34:27 +0200687 int cnt;
hpa4c505f72005-09-28 16:37:58 -0700688
Michael Lukashov72a534d2010-02-17 20:56:02 +0000689 get_host_and_port(&host, &port);
hpa4c505f72005-09-28 16:37:58 -0700690
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300691 if (flags & CONNECT_VERBOSE)
692 fprintf(stderr, "Looking up %s ... ", host);
693
hpa4c505f72005-09-28 16:37:58 -0700694 he = gethostbyname(host);
695 if (!he)
696 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
697 nport = strtoul(port, &ep, 10);
698 if ( ep == port || *ep ) {
699 /* Not numeric */
700 struct servent *se = getservbyname(port,"tcp");
701 if ( !se )
Alexander Potashevd7530702009-01-04 21:38:41 +0300702 die("Unknown port %s", port);
hpa4c505f72005-09-28 16:37:58 -0700703 nport = se->s_port;
704 }
705
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300706 if (flags & CONNECT_VERBOSE)
707 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
708
Alex Riesenba505322007-05-23 23:34:27 +0200709 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
hpa4c505f72005-09-28 16:37:58 -0700710 memset(&sa, 0, sizeof sa);
711 sa.sin_family = he->h_addrtype;
Peter Anvin6573faf2005-09-28 17:26:44 -0700712 sa.sin_port = htons(nport);
Paul Sericec6164212005-11-22 07:54:23 -0600713 memcpy(&sa.sin_addr, *ap, he->h_length);
hpa4c505f72005-09-28 16:37:58 -0700714
Erik Faye-Lund7203a2d2011-08-01 13:16:10 +0200715 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
716 if ((sockfd < 0) ||
717 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
718 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
Alex Riesenba505322007-05-23 23:34:27 +0200719 host,
720 cnt,
721 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
Erik Faye-Lund7203a2d2011-08-01 13:16:10 +0200722 strerror(errno));
723 if (0 <= sockfd)
724 close(sockfd);
hpa4c505f72005-09-28 16:37:58 -0700725 sockfd = -1;
726 continue;
727 }
Alex Riesenba505322007-05-23 23:34:27 +0200728 if (flags & CONNECT_VERBOSE)
729 fprintf(stderr, "%s ",
730 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
hpa4c505f72005-09-28 16:37:58 -0700731 break;
732 }
733
734 if (sockfd < 0)
Erik Faye-Lund7203a2d2011-08-01 13:16:10 +0200735 die("unable to connect to %s:\n%s", host, error_message.buf);
hpa4c505f72005-09-28 16:37:58 -0700736
Eric Wonge47a8582011-12-06 04:39:36 +0000737 enable_keepalive(sockfd);
738
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300739 if (flags & CONNECT_VERBOSE)
740 fprintf(stderr, "done.\n");
741
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500742 return sockfd;
hpa4c505f72005-09-28 16:37:58 -0700743}
744
hpa49744d62005-09-28 16:52:21 -0700745#endif /* NO_IPV6 */
hpa4c505f72005-09-28 16:37:58 -0700746
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500747
Jonathan Nieder8e349782017-11-20 13:22:54 -0800748/*
749 * Dummy child_process returned by git_connect() if the transport protocol
750 * does not need fork(2).
751 */
752static struct child_process no_fork = CHILD_PROCESS_INIT;
753
754int git_connection_is_socket(struct child_process *conn)
755{
756 return conn == &no_fork;
757}
758
759static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500760{
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300761 int sockfd = git_tcp_connect_sock(host, flags);
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500762
763 fd[0] = sockfd;
Junio C Hamanoec587fd2007-01-21 17:10:51 -0800764 fd[1] = dup(sockfd);
Jonathan Nieder8e349782017-11-20 13:22:54 -0800765
766 return &no_fork;
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500767}
768
769
David Rientjes96f1e582006-08-15 10:23:48 -0700770static char *git_proxy_command;
Paul Collinsf8014772005-11-04 14:57:16 +0000771
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100772static int git_proxy_command_options(const char *var, const char *value,
773 void *cb)
Paul Collinsf8014772005-11-04 14:57:16 +0000774{
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800775 if (!strcmp(var, "core.gitproxy")) {
YOSHIFUJI Hideaki / 吉藤英明c3df8562005-11-22 12:18:23 +0900776 const char *for_pos;
777 int matchlen = -1;
778 int hostlen;
Erik Faye-Lund15112c92009-03-11 02:38:12 +0000779 const char *rhost_name = cb;
780 int rhost_len = strlen(rhost_name);
YOSHIFUJI Hideaki / 吉藤英明c3df8562005-11-22 12:18:23 +0900781
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800782 if (git_proxy_command)
Paul Collinsf8014772005-11-04 14:57:16 +0000783 return 0;
Junio C Hamanoc64b9ad2008-02-11 10:52:15 -0800784 if (!value)
785 return config_error_nonbool(var);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800786 /* [core]
787 * ;# matches www.kernel.org as well
788 * gitproxy = netcatter-1 for kernel.org
789 * gitproxy = netcatter-2 for sample.xz
790 * gitproxy = netcatter-default
791 */
YOSHIFUJI Hideaki / 吉藤英明c3df8562005-11-22 12:18:23 +0900792 for_pos = strstr(value, " for ");
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800793 if (!for_pos)
794 /* matches everybody */
795 matchlen = strlen(value);
796 else {
797 hostlen = strlen(for_pos + 5);
798 if (rhost_len < hostlen)
799 matchlen = -1;
800 else if (!strncmp(for_pos + 5,
801 rhost_name + rhost_len - hostlen,
802 hostlen) &&
803 ((rhost_len == hostlen) ||
804 rhost_name[rhost_len - hostlen -1] == '.'))
805 matchlen = for_pos - value;
806 else
807 matchlen = -1;
Paul Collinsf8014772005-11-04 14:57:16 +0000808 }
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800809 if (0 <= matchlen) {
810 /* core.gitproxy = none for kernel.org */
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700811 if (matchlen == 4 &&
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800812 !memcmp(value, "none", 4))
813 matchlen = 0;
Pierre Habouzit182af832007-09-16 00:32:36 +0200814 git_proxy_command = xmemdupz(value, matchlen);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800815 }
816 return 0;
Paul Collinsf8014772005-11-04 14:57:16 +0000817 }
818
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100819 return git_default_config(var, value, cb);
Paul Collinsf8014772005-11-04 14:57:16 +0000820}
821
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800822static int git_use_proxy(const char *host)
Paul Collinsf8014772005-11-04 14:57:16 +0000823{
824 git_proxy_command = getenv("GIT_PROXY_COMMAND");
Erik Faye-Lund15112c92009-03-11 02:38:12 +0000825 git_config(git_proxy_command_options, (void*)host);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800826 return (git_proxy_command && *git_proxy_command);
Paul Collinsf8014772005-11-04 14:57:16 +0000827}
828
Jeff King5cbf8242011-05-16 02:46:07 -0400829static struct child_process *git_proxy_connect(int fd[2], char *host)
Paul Collinsf8014772005-11-04 14:57:16 +0000830{
Timo Hirvonen554fe202006-06-28 12:04:39 +0300831 const char *port = STR(DEFAULT_GIT_PORT);
Jeff King5cbf8242011-05-16 02:46:07 -0400832 struct child_process *proxy;
Paul Collinsf8014772005-11-04 14:57:16 +0000833
Michael Lukashov72a534d2010-02-17 20:56:02 +0000834 get_host_and_port(&host, &port);
Paul Collinsf8014772005-11-04 14:57:16 +0000835
Jeff King3be4cf02017-07-28 15:26:50 -0400836 if (looks_like_command_line_option(host))
837 die("strange hostname '%s' blocked", host);
838 if (looks_like_command_line_option(port))
839 die("strange port '%s' blocked", port);
840
René Scharfe483bbd42014-08-19 21:10:48 +0200841 proxy = xmalloc(sizeof(*proxy));
842 child_process_init(proxy);
Jeff King1823bea2014-05-15 04:34:09 -0400843 argv_array_push(&proxy->args, git_proxy_command);
844 argv_array_push(&proxy->args, host);
845 argv_array_push(&proxy->args, port);
Jeff King5cbf8242011-05-16 02:46:07 -0400846 proxy->in = -1;
847 proxy->out = -1;
848 if (start_command(proxy))
Jeff King1823bea2014-05-15 04:34:09 -0400849 die("cannot start proxy %s", git_proxy_command);
Jeff King5cbf8242011-05-16 02:46:07 -0400850 fd[0] = proxy->out; /* read from proxy stdout */
851 fd[1] = proxy->in; /* write to proxy stdin */
852 return proxy;
Paul Collinsf8014772005-11-04 14:57:16 +0000853}
854
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100855static char *get_port(char *host)
Luben Tuikov2e776662007-09-01 02:36:31 -0700856{
857 char *end;
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100858 char *p = strchr(host, ':');
859
Luben Tuikov2e776662007-09-01 02:36:31 -0700860 if (p) {
René Scharfe8f148252008-12-21 02:12:11 +0100861 long port = strtol(p + 1, &end, 10);
862 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100863 *p = '\0';
864 return p+1;
Luben Tuikov2e776662007-09-01 02:36:31 -0700865 }
866 }
867
868 return NULL;
869}
870
Linus Torvaldsf7192592005-07-04 11:57:58 -0700871/*
Johannes Sixtcabc3c12013-11-28 20:49:01 +0100872 * Extract protocol and relevant parts from the specified connection URL.
873 * The caller must free() the returned strings.
Linus Torvaldsf7192592005-07-04 11:57:58 -0700874 */
Johannes Sixtcabc3c12013-11-28 20:49:01 +0100875static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
Torsten Bögershausen83b05872013-11-28 20:49:54 +0100876 char **ret_path)
Linus Torvaldsf7192592005-07-04 11:57:58 -0700877{
Jeff King9d2e9422010-05-23 05:19:44 -0400878 char *url;
Benjamin Kramer8e76bf32009-03-13 13:51:33 +0100879 char *host, *path;
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900880 char *end;
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100881 int separator = '/';
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100882 enum protocol protocol = PROTO_LOCAL;
Junio C Hamanof0b73672006-06-19 18:25:21 -0700883
Jeff King9d2e9422010-05-23 05:19:44 -0400884 if (is_url(url_orig))
885 url = url_decode(url_orig);
886 else
887 url = xstrdup(url_orig);
888
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100889 host = strstr(url, "://");
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400890 if (host) {
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100891 *host = '\0';
892 protocol = get_protocol(url);
893 host += 3;
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900894 } else {
Linus Torvaldsf7192592005-07-04 11:57:58 -0700895 host = url;
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100896 if (!url_is_local_not_ssh(url)) {
897 protocol = PROTO_SSH;
898 separator = ':';
899 }
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900900 }
901
Ilari Liusvaara9aa50532010-01-26 20:24:42 +0200902 /*
Torsten Bögershausen83b05872013-11-28 20:49:54 +0100903 * Don't do destructive transforms as protocol code does
904 * '[]' unwrapping in get_host_and_port()
Ilari Liusvaara9aa50532010-01-26 20:24:42 +0200905 */
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100906 end = host_end(&host, 0);
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900907
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100908 if (protocol == PROTO_LOCAL)
Linus Torvalds72a4f4b2007-08-01 10:03:37 -0700909 path = end;
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100910 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
911 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
912 else
913 path = strchr(end, separator);
Linus Torvalds2386d652005-07-13 18:46:20 -0700914
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100915 if (!path || !*path)
916 die("No path specified. See 'man git-pull' for valid url syntax");
917
918 /*
919 * null-terminate hostname and point path to ~ for URL's like this:
920 * ssh://host.xz/~user/repo
921 */
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100922
923 end = path; /* Need to \0 terminate host here */
924 if (separator == ':')
925 path++; /* path starts after ':' */
926 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100927 if (path[1] == '~')
928 path++;
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100929 }
930
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100931 path = xstrdup(path);
932 *end = '\0';
933
Johannes Sixtcabc3c12013-11-28 20:49:01 +0100934 *ret_host = xstrdup(host);
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100935 *ret_path = path;
Johannes Sixtcabc3c12013-11-28 20:49:01 +0100936 free(url);
937 return protocol;
938}
939
Nguyễn Thái Ngọc Duy3c8ede32016-06-26 13:16:35 +0200940static const char *get_ssh_command(void)
941{
942 const char *ssh;
943
944 if ((ssh = getenv("GIT_SSH_COMMAND")))
945 return ssh;
946
947 if (!git_config_get_string_const("core.sshcommand", &ssh))
948 return ssh;
949
950 return NULL;
951}
952
Brandon Williams94b8ae52017-10-16 10:55:31 -0700953enum ssh_variant {
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800954 VARIANT_AUTO,
Brandon Williams94b8ae52017-10-16 10:55:31 -0700955 VARIANT_SIMPLE,
956 VARIANT_SSH,
957 VARIANT_PLINK,
958 VARIANT_PUTTY,
959 VARIANT_TORTOISEPLINK,
960};
Junio C Hamano486c8e82017-02-09 09:20:25 -0800961
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800962static void override_ssh_variant(enum ssh_variant *ssh_variant)
Brandon Williams94b8ae52017-10-16 10:55:31 -0700963{
964 const char *variant = getenv("GIT_SSH_VARIANT");
965
966 if (!variant && git_config_get_string_const("ssh.variant", &variant))
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800967 return;
Junio C Hamano486c8e82017-02-09 09:20:25 -0800968
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800969 if (!strcmp(variant, "auto"))
970 *ssh_variant = VARIANT_AUTO;
971 else if (!strcmp(variant, "plink"))
Brandon Williams94b8ae52017-10-16 10:55:31 -0700972 *ssh_variant = VARIANT_PLINK;
973 else if (!strcmp(variant, "putty"))
974 *ssh_variant = VARIANT_PUTTY;
975 else if (!strcmp(variant, "tortoiseplink"))
976 *ssh_variant = VARIANT_TORTOISEPLINK;
977 else if (!strcmp(variant, "simple"))
978 *ssh_variant = VARIANT_SIMPLE;
979 else
980 *ssh_variant = VARIANT_SSH;
Junio C Hamano486c8e82017-02-09 09:20:25 -0800981}
982
Brandon Williams94b8ae52017-10-16 10:55:31 -0700983static enum ssh_variant determine_ssh_variant(const char *ssh_command,
984 int is_cmdline)
Junio C Hamano486c8e82017-02-09 09:20:25 -0800985{
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800986 enum ssh_variant ssh_variant = VARIANT_AUTO;
Junio C Hamano486c8e82017-02-09 09:20:25 -0800987 const char *variant;
Johannes Schindeline2824e42017-02-01 13:01:10 +0100988 char *p = NULL;
989
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800990 override_ssh_variant(&ssh_variant);
991
992 if (ssh_variant != VARIANT_AUTO)
Brandon Williams94b8ae52017-10-16 10:55:31 -0700993 return ssh_variant;
Junio C Hamano486c8e82017-02-09 09:20:25 -0800994
995 if (!is_cmdline) {
Johannes Schindeline2824e42017-02-01 13:01:10 +0100996 p = xstrdup(ssh_command);
997 variant = basename(p);
998 } else {
999 const char **ssh_argv;
1000
1001 p = xstrdup(ssh_command);
Jeff King22e5ae52017-04-10 20:30:23 -04001002 if (split_cmdline(p, &ssh_argv) > 0) {
Johannes Schindeline2824e42017-02-01 13:01:10 +01001003 variant = basename((char *)ssh_argv[0]);
1004 /*
1005 * At this point, variant points into the buffer
1006 * referenced by p, hence we do not need ssh_argv
1007 * any longer.
1008 */
1009 free(ssh_argv);
Jeff King5d2993b2017-04-20 16:21:58 -04001010 } else {
1011 free(p);
Brandon Williams94b8ae52017-10-16 10:55:31 -07001012 return ssh_variant;
Jeff King5d2993b2017-04-20 16:21:58 -04001013 }
Johannes Schindeline2824e42017-02-01 13:01:10 +01001014 }
1015
Brandon Williams94b8ae52017-10-16 10:55:31 -07001016 if (!strcasecmp(variant, "ssh") ||
1017 !strcasecmp(variant, "ssh.exe"))
1018 ssh_variant = VARIANT_SSH;
1019 else if (!strcasecmp(variant, "plink") ||
1020 !strcasecmp(variant, "plink.exe"))
1021 ssh_variant = VARIANT_PLINK;
Johannes Schindeline2824e42017-02-01 13:01:10 +01001022 else if (!strcasecmp(variant, "tortoiseplink") ||
Brandon Williams94b8ae52017-10-16 10:55:31 -07001023 !strcasecmp(variant, "tortoiseplink.exe"))
1024 ssh_variant = VARIANT_TORTOISEPLINK;
1025
Johannes Schindeline2824e42017-02-01 13:01:10 +01001026 free(p);
Brandon Williams94b8ae52017-10-16 10:55:31 -07001027 return ssh_variant;
Johannes Schindeline2824e42017-02-01 13:01:10 +01001028}
1029
Linus Torvaldsf7192592005-07-04 11:57:58 -07001030/*
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001031 * Open a connection using Git's native protocol.
1032 *
1033 * The caller is responsible for freeing hostandport, but this function may
1034 * modify it (for example, to truncate it to remove the port part).
1035 */
1036static struct child_process *git_connect_git(int fd[2], char *hostandport,
1037 const char *path, const char *prog,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001038 enum protocol_version version,
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001039 int flags)
1040{
1041 struct child_process *conn;
1042 struct strbuf request = STRBUF_INIT;
1043 /*
1044 * Set up virtual host information based on where we will
1045 * connect, unless the user has overridden us in
1046 * the environment.
1047 */
1048 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1049 if (target_host)
1050 target_host = xstrdup(target_host);
1051 else
1052 target_host = xstrdup(hostandport);
1053
1054 transport_check_allowed("git");
1055
Jonathan Nieder233cd282017-11-20 14:04:58 -08001056 /*
1057 * These underlying connection commands die() if they
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001058 * cannot connect.
1059 */
1060 if (git_use_proxy(hostandport))
1061 conn = git_proxy_connect(fd, hostandport);
1062 else
1063 conn = git_tcp_connect(fd, hostandport, flags);
1064 /*
1065 * Separate original protocol components prog and path
1066 * from extended host header with a NUL byte.
1067 *
1068 * Note: Do not add any other headers here! Doing so
1069 * will cause older git-daemon servers to crash.
1070 */
1071 strbuf_addf(&request,
1072 "%s %s%chost=%s%c",
1073 prog, path, 0,
1074 target_host, 0);
1075
1076 /* If using a new version put that stuff here after a second null byte */
Brandon Williams40fc51e2018-03-15 10:31:30 -07001077 if (version > 0) {
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001078 strbuf_addch(&request, '\0');
1079 strbuf_addf(&request, "version=%d%c",
Brandon Williams40fc51e2018-03-15 10:31:30 -07001080 version, '\0');
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001081 }
1082
1083 packet_write(fd[1], request.buf, request.len);
1084
1085 free(target_host);
1086 strbuf_release(&request);
1087 return conn;
1088}
1089
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001090/*
1091 * Append the appropriate environment variables to `env` and options to
1092 * `args` for running ssh in Git's SSH-tunneled transport.
1093 */
1094static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1095 enum ssh_variant variant, const char *port,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001096 enum protocol_version version, int flags)
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001097{
1098 if (variant == VARIANT_SSH &&
Brandon Williams40fc51e2018-03-15 10:31:30 -07001099 version > 0) {
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001100 argv_array_push(args, "-o");
1101 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1102 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
Brandon Williams40fc51e2018-03-15 10:31:30 -07001103 version);
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001104 }
1105
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001106 if (flags & CONNECT_IPV4) {
1107 switch (variant) {
1108 case VARIANT_AUTO:
1109 BUG("VARIANT_AUTO passed to push_ssh_options");
1110 case VARIANT_SIMPLE:
1111 die("ssh variant 'simple' does not support -4");
1112 case VARIANT_SSH:
1113 case VARIANT_PLINK:
1114 case VARIANT_PUTTY:
1115 case VARIANT_TORTOISEPLINK:
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001116 argv_array_push(args, "-4");
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001117 }
1118 } else if (flags & CONNECT_IPV6) {
1119 switch (variant) {
1120 case VARIANT_AUTO:
1121 BUG("VARIANT_AUTO passed to push_ssh_options");
1122 case VARIANT_SIMPLE:
1123 die("ssh variant 'simple' does not support -6");
1124 case VARIANT_SSH:
1125 case VARIANT_PLINK:
1126 case VARIANT_PUTTY:
1127 case VARIANT_TORTOISEPLINK:
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001128 argv_array_push(args, "-6");
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001129 }
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001130 }
1131
1132 if (variant == VARIANT_TORTOISEPLINK)
1133 argv_array_push(args, "-batch");
1134
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001135 if (port) {
1136 switch (variant) {
1137 case VARIANT_AUTO:
1138 BUG("VARIANT_AUTO passed to push_ssh_options");
1139 case VARIANT_SIMPLE:
1140 die("ssh variant 'simple' does not support setting port");
1141 case VARIANT_SSH:
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001142 argv_array_push(args, "-p");
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001143 break;
1144 case VARIANT_PLINK:
1145 case VARIANT_PUTTY:
1146 case VARIANT_TORTOISEPLINK:
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001147 argv_array_push(args, "-P");
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001148 }
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001149
1150 argv_array_push(args, port);
1151 }
1152}
1153
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001154/* Prepare a child_process for use by Git's SSH-tunneled transport. */
1155static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001156 const char *port, enum protocol_version version,
1157 int flags)
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001158{
1159 const char *ssh;
1160 enum ssh_variant variant;
1161
1162 if (looks_like_command_line_option(ssh_host))
1163 die("strange hostname '%s' blocked", ssh_host);
1164
1165 ssh = get_ssh_command();
1166 if (ssh) {
1167 variant = determine_ssh_variant(ssh, 1);
1168 } else {
1169 /*
1170 * GIT_SSH is the no-shell version of
1171 * GIT_SSH_COMMAND (and must remain so for
1172 * historical compatibility).
1173 */
1174 conn->use_shell = 0;
1175
1176 ssh = getenv("GIT_SSH");
1177 if (!ssh)
1178 ssh = "ssh";
1179 variant = determine_ssh_variant(ssh, 0);
1180 }
1181
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001182 if (variant == VARIANT_AUTO) {
1183 struct child_process detect = CHILD_PROCESS_INIT;
1184
1185 detect.use_shell = conn->use_shell;
1186 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1187
1188 argv_array_push(&detect.args, ssh);
1189 argv_array_push(&detect.args, "-G");
1190 push_ssh_options(&detect.args, &detect.env_array,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001191 VARIANT_SSH, port, version, flags);
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001192 argv_array_push(&detect.args, ssh_host);
1193
1194 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1195 }
1196
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001197 argv_array_push(&conn->args, ssh);
Brandon Williams40fc51e2018-03-15 10:31:30 -07001198 push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001199 argv_array_push(&conn->args, ssh_host);
1200}
1201
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001202/*
Jonathan Nieder8e349782017-11-20 13:22:54 -08001203 * This returns the dummy child_process `no_fork` if the transport protocol
1204 * does not need fork(2), or a struct child_process object if it does. Once
1205 * done, finish the connection with finish_connect() with the value returned
1206 * from this function (it is safe to call finish_connect() with NULL to
1207 * support the former case).
Linus Torvaldsf7192592005-07-04 11:57:58 -07001208 *
1209 * If it returns, the connect is successful; it just dies on errors (this
1210 * will hopefully be changed in a libification effort, to return NULL when
1211 * the connection failed).
1212 */
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001213struct child_process *git_connect(int fd[2], const char *url,
Linus Torvaldsf7192592005-07-04 11:57:58 -07001214 const char *prog, int flags)
1215{
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001216 char *hostandport, *path;
Jonathan Nieder8e349782017-11-20 13:22:54 -08001217 struct child_process *conn;
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001218 enum protocol protocol;
Brandon Williams40fc51e2018-03-15 10:31:30 -07001219 enum protocol_version version = get_protocol_version_config();
Linus Torvaldsf7192592005-07-04 11:57:58 -07001220
Brandon Williams1aa8dded2018-03-15 10:31:31 -07001221 /*
1222 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1223 * to perform a push, then fallback to v0 since the client doesn't know
1224 * how to push yet using v2.
1225 */
1226 if (version == protocol_v2 && !strcmp("git-receive-pack", prog))
1227 version = protocol_v0;
1228
Linus Torvaldsf7192592005-07-04 11:57:58 -07001229 /* Without this we cannot rely on waitpid() to tell
1230 * what happened to our children.
1231 */
1232 signal(SIGCHLD, SIG_DFL);
1233
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001234 protocol = parse_connect_url(url, &hostandport, &path);
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001235 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +01001236 printf("Diag: url=%s\n", url ? url : "NULL");
1237 printf("Diag: protocol=%s\n", prot_name(protocol));
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001238 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +01001239 printf("Diag: path=%s\n", path ? path : "NULL");
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001240 conn = NULL;
1241 } else if (protocol == PROTO_GIT) {
Brandon Williams40fc51e2018-03-15 10:31:30 -07001242 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001243 } else {
Rene Scharfef1399292017-08-30 19:49:39 +02001244 struct strbuf cmd = STRBUF_INIT;
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001245 const char *const *var;
Rene Scharfef1399292017-08-30 19:49:39 +02001246
René Scharfe483bbd42014-08-19 21:10:48 +02001247 conn = xmalloc(sizeof(*conn));
1248 child_process_init(conn);
Linus Torvaldsf7192592005-07-04 11:57:58 -07001249
Jeff Kingaeeb2d42017-07-28 15:28:55 -04001250 if (looks_like_command_line_option(path))
1251 die("strange pathname '%s' blocked", path);
1252
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001253 strbuf_addstr(&cmd, prog);
1254 strbuf_addch(&cmd, ' ');
1255 sq_quote_buf(&cmd, path);
Christian Couder0f503d72006-09-11 07:04:50 +02001256
Jeff Kingaab40432015-09-04 18:40:08 -04001257 /* remove repo-local variables from the environment */
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001258 for (var = local_repo_env; *var; var++)
1259 argv_array_push(&conn->env_array, *var);
1260
Jeff Kinga48b4092015-09-08 04:33:14 -04001261 conn->use_shell = 1;
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001262 conn->in = conn->out = -1;
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001263 if (protocol == PROTO_SSH) {
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001264 char *ssh_host = hostandport;
1265 const char *port = NULL;
Jeff Kinga5adace2015-09-16 13:12:52 -04001266 transport_check_allowed("ssh");
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001267 get_host_and_port(&ssh_host, &port);
Christian Couder0f503d72006-09-11 07:04:50 +02001268
Torsten Bögershausen86ceb332015-02-21 16:52:48 +01001269 if (!port)
1270 port = get_port(ssh_host);
Junio C Hamano42da4842015-03-05 12:45:44 -08001271
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001272 if (flags & CONNECT_DIAG_URL) {
1273 printf("Diag: url=%s\n", url ? url : "NULL");
1274 printf("Diag: protocol=%s\n", prot_name(protocol));
1275 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1276 printf("Diag: port=%s\n", port ? port : "NONE");
1277 printf("Diag: path=%s\n", path ? path : "NULL");
Linus Torvaldsf7192592005-07-04 11:57:58 -07001278
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001279 free(hostandport);
1280 free(path);
Stefan Beller04f20c02015-03-09 09:58:22 -07001281 free(conn);
Rene Scharfef1399292017-08-30 19:49:39 +02001282 strbuf_release(&cmd);
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001283 return NULL;
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001284 }
Brandon Williams40fc51e2018-03-15 10:31:30 -07001285 fill_ssh_args(conn, ssh_host, port, version, flags);
Nguyễn Thái Ngọc Duyc049b612014-03-13 18:45:31 +07001286 } else {
Jeff Kinga5adace2015-09-16 13:12:52 -04001287 transport_check_allowed("file");
Brandon Williams40fc51e2018-03-15 10:31:30 -07001288 if (version > 0) {
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001289 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
Brandon Williams40fc51e2018-03-15 10:31:30 -07001290 version);
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001291 }
Linus Torvaldsf7192592005-07-04 11:57:58 -07001292 }
Jeff King1823bea2014-05-15 04:34:09 -04001293 argv_array_push(&conn->args, cmd.buf);
Johannes Sixtf364cb82007-10-19 21:47:54 +02001294
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001295 if (start_command(conn))
1296 die("unable to fork");
Johannes Sixtf364cb82007-10-19 21:47:54 +02001297
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001298 fd[0] = conn->out; /* read from child's stdout */
1299 fd[1] = conn->in; /* write to child's stdin */
1300 strbuf_release(&cmd);
1301 }
1302 free(hostandport);
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001303 free(path);
Johannes Sixt98158e92007-10-19 21:47:53 +02001304 return conn;
Linus Torvaldsf7192592005-07-04 11:57:58 -07001305}
1306
Johannes Sixt98158e92007-10-19 21:47:53 +02001307int finish_connect(struct child_process *conn)
Linus Torvaldsf7192592005-07-04 11:57:58 -07001308{
Johannes Sixtf364cb82007-10-19 21:47:54 +02001309 int code;
Jeff King7ffe8532011-05-16 02:52:11 -04001310 if (!conn || git_connection_is_socket(conn))
Franck Bui-Huuf42a5c42006-09-12 11:00:13 +02001311 return 0;
1312
Johannes Sixtf364cb82007-10-19 21:47:54 +02001313 code = finish_command(conn);
Johannes Sixt98158e92007-10-19 21:47:53 +02001314 free(conn);
Johannes Sixtf364cb82007-10-19 21:47:54 +02001315 return code;
Linus Torvaldsf7192592005-07-04 11:57:58 -07001316}