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