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" |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 17 | |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 18 | static char *server_capabilities_v1; |
| 19 | static struct argv_array server_capabilities_v2 = ARGV_ARRAY_INIT; |
Junio C Hamano | 5d54cff | 2013-09-17 16:29:28 -0700 | [diff] [blame] | 20 | static const char *parse_feature_value(const char *, const char *, int *); |
Johannes Schindelin | 211b5f9 | 2005-10-28 04:48:54 +0200 | [diff] [blame] | 21 | |
René Scharfe | be0b3f8 | 2014-08-30 11:46:54 +0200 | [diff] [blame] | 22 | static int check_ref(const char *name, unsigned int flags) |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 23 | { |
| 24 | if (!flags) |
| 25 | return 1; |
| 26 | |
René Scharfe | be0b3f8 | 2014-08-30 11:46:54 +0200 | [diff] [blame] | 27 | if (!skip_prefix(name, "refs/", &name)) |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 28 | return 0; |
| 29 | |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 30 | /* 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] | 31 | if ((flags & REF_NORMAL) && check_refname_format(name, 0)) |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 32 | return 0; |
| 33 | |
| 34 | /* REF_HEADS means that we want regular branch heads */ |
René Scharfe | be0b3f8 | 2014-08-30 11:46:54 +0200 | [diff] [blame] | 35 | if ((flags & REF_HEADS) && starts_with(name, "heads/")) |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 36 | return 1; |
| 37 | |
| 38 | /* REF_TAGS means that we want tags */ |
René Scharfe | be0b3f8 | 2014-08-30 11:46:54 +0200 | [diff] [blame] | 39 | if ((flags & REF_TAGS) && starts_with(name, "tags/")) |
Linus Torvalds | 2718ff0 | 2006-07-04 12:29:10 -0700 | [diff] [blame] | 40 | return 1; |
| 41 | |
| 42 | /* All type bits clear means that we are ok with anything */ |
| 43 | return !(flags & ~REF_NORMAL); |
| 44 | } |
| 45 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 46 | int check_ref_type(const struct ref *ref, int flags) |
| 47 | { |
René Scharfe | be0b3f8 | 2014-08-30 11:46:54 +0200 | [diff] [blame] | 48 | return check_ref(ref->name, flags); |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Nguyễn Thái Ngọc Duy | d2bff22 | 2018-04-14 19:19:43 +0000 | [diff] [blame] | 51 | static NORETURN void die_initial_contact(int unexpected) |
Heiko Voigt | 46284dd | 2012-06-19 20:24:50 +0200 | [diff] [blame] | 52 | { |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 53 | /* |
| 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 Nieder | 55e4f93 | 2016-09-09 10:36:29 -0700 | [diff] [blame] | 59 | if (unexpected) |
Vasco Almeida | f2b93b3 | 2016-09-19 13:08:17 +0000 | [diff] [blame] | 60 | die(_("The remote end hung up upon initial contact")); |
Heiko Voigt | 46284dd | 2012-06-19 20:24:50 +0200 | [diff] [blame] | 61 | else |
Vasco Almeida | f2b93b3 | 2016-09-19 13:08:17 +0000 | [diff] [blame] | 62 | 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 Voigt | 46284dd | 2012-06-19 20:24:50 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 67 | /* Checks if the server supports the capability 'c' */ |
| 68 | int 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 Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 85 | int 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 Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 107 | static 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 Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 116 | enum protocol_version discover_version(struct packet_reader *reader) |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 117 | { |
| 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 Williams | 8f6982b | 2018-03-14 11:31:47 -0700 | [diff] [blame] | 137 | case protocol_v2: |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 138 | process_capabilities_v2(reader); |
Brandon Williams | 8f6982b | 2018-03-14 11:31:47 -0700 | [diff] [blame] | 139 | break; |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 140 | 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 Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 153 | static 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é Scharfe | 5c0b13f | 2014-07-19 17:35:34 +0200 | [diff] [blame] | 161 | sym = xmemdupz(val, len); |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 162 | 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 King | ef4fe56 | 2017-05-25 15:33:05 -0400 | [diff] [blame] | 171 | item = string_list_append_nodup(symref, sym); |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 172 | item->util = target; |
| 173 | return; |
| 174 | reject: |
| 175 | free(sym); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | static void annotate_refs_with_symref_info(struct ref *ref) |
| 180 | { |
| 181 | struct string_list symref = STRING_LIST_INIT_DUP; |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 182 | const char *feature_list = server_capabilities_v1; |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 183 | |
| 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 Haggerty | 3383e19 | 2014-11-25 09:02:35 +0100 | [diff] [blame] | 194 | string_list_sort(&symref); |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 195 | |
| 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 Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 206 | static void process_capabilities(const char *line, int *len) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 207 | { |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 208 | int nul_location = strlen(line); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 209 | if (nul_location == *len) |
| 210 | return; |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 211 | server_capabilities_v1 = xstrdup(line + nul_location + 1); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 212 | *len = nul_location; |
| 213 | } |
| 214 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 215 | static int process_dummy_ref(const char *line) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 216 | { |
| 217 | struct object_id oid; |
| 218 | const char *name; |
| 219 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 220 | if (parse_oid_hex(line, &oid, &name)) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | if (*name != ' ') |
| 223 | return 0; |
| 224 | name++; |
| 225 | |
| 226 | return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}"); |
| 227 | } |
| 228 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 229 | static void check_no_capabilities(const char *line, int len) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 230 | { |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 231 | if (strlen(line) != len) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 232 | warning("Ignoring capabilities after first line '%s'", |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 233 | line + strlen(line)); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 236 | static int process_ref(const char *line, int len, struct ref ***list, |
| 237 | unsigned int flags, struct oid_array *extra_have) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 238 | { |
| 239 | struct object_id old_oid; |
| 240 | const char *name; |
| 241 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 242 | if (parse_oid_hex(line, &old_oid, &name)) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 243 | 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 Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 258 | check_no_capabilities(line, len); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 259 | return 1; |
| 260 | } |
| 261 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 262 | static int process_shallow(const char *line, int len, |
| 263 | struct oid_array *shallow_points) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 264 | { |
| 265 | const char *arg; |
| 266 | struct object_id old_oid; |
| 267 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 268 | if (!skip_prefix(line, "shallow ", &arg)) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 269 | 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 Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 276 | check_no_capabilities(line, len); |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 277 | return 1; |
| 278 | } |
| 279 | |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 280 | enum get_remote_heads_state { |
| 281 | EXPECTING_FIRST_REF = 0, |
| 282 | EXPECTING_REF, |
| 283 | EXPECTING_SHALLOW, |
| 284 | EXPECTING_DONE, |
| 285 | }; |
| 286 | |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 287 | /* |
Linus Torvalds | d1c133f | 2005-07-16 13:55:50 -0700 | [diff] [blame] | 288 | * Read all the refs from the other end |
| 289 | */ |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 290 | struct ref **get_remote_heads(struct packet_reader *reader, |
Jeff King | 85edf4f | 2013-02-20 15:06:45 -0500 | [diff] [blame] | 291 | struct ref **list, unsigned int flags, |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 292 | struct oid_array *extra_have, |
| 293 | struct oid_array *shallow_points) |
Linus Torvalds | d1c133f | 2005-07-16 13:55:50 -0700 | [diff] [blame] | 294 | { |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 295 | struct ref **orig_list = list; |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 296 | int len = 0; |
| 297 | enum get_remote_heads_state state = EXPECTING_FIRST_REF; |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 298 | const char *arg; |
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; |
| 308 | if (len > 4 && skip_prefix(reader->line, "ERR ", &arg)) |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 309 | 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 Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 318 | switch (state) { |
| 319 | case EXPECTING_FIRST_REF: |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 320 | process_capabilities(reader->line, &len); |
| 321 | if (process_dummy_ref(reader->line)) { |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 322 | state = EXPECTING_SHALLOW; |
| 323 | break; |
| 324 | } |
| 325 | state = EXPECTING_REF; |
| 326 | /* fallthrough */ |
| 327 | case EXPECTING_REF: |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 328 | if (process_ref(reader->line, len, &list, flags, extra_have)) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 329 | break; |
| 330 | state = EXPECTING_SHALLOW; |
| 331 | /* fallthrough */ |
| 332 | case EXPECTING_SHALLOW: |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 333 | if (process_shallow(reader->line, len, shallow_points)) |
Jonathan Tan | 0cd8328 | 2017-09-26 16:56:19 -0700 | [diff] [blame] | 334 | break; |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 335 | die("protocol error: unexpected '%s'", reader->line); |
Brandon Williams | 7e3e479 | 2018-03-14 11:31:44 -0700 | [diff] [blame] | 336 | case EXPECTING_DONE: |
| 337 | break; |
Nguyễn Thái Ngọc Duy | b06dcd7 | 2013-12-05 20:02:33 +0700 | [diff] [blame] | 338 | } |
Linus Torvalds | d1c133f | 2005-07-16 13:55:50 -0700 | [diff] [blame] | 339 | } |
Junio C Hamano | a45b5f0 | 2013-09-17 19:10:31 -0700 | [diff] [blame] | 340 | |
| 341 | annotate_refs_with_symref_info(*orig_list); |
| 342 | |
Linus Torvalds | d1c133f | 2005-07-16 13:55:50 -0700 | [diff] [blame] | 343 | return list; |
| 344 | } |
| 345 | |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 346 | /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */ |
| 347 | static int process_ref_v2(const char *line, struct ref ***list) |
| 348 | { |
| 349 | int ret = 1; |
| 350 | int i = 0; |
| 351 | struct object_id old_oid; |
| 352 | struct ref *ref; |
| 353 | struct string_list line_sections = STRING_LIST_INIT_DUP; |
| 354 | const char *end; |
| 355 | |
| 356 | /* |
| 357 | * Ref lines have a number of fields which are space deliminated. The |
| 358 | * first field is the OID of the ref. The second field is the ref |
| 359 | * name. Subsequent fields (symref-target and peeled) are optional and |
| 360 | * don't have a particular order. |
| 361 | */ |
| 362 | if (string_list_split(&line_sections, line, ' ', -1) < 2) { |
| 363 | ret = 0; |
| 364 | goto out; |
| 365 | } |
| 366 | |
| 367 | if (parse_oid_hex(line_sections.items[i++].string, &old_oid, &end) || |
| 368 | *end) { |
| 369 | ret = 0; |
| 370 | goto out; |
| 371 | } |
| 372 | |
| 373 | ref = alloc_ref(line_sections.items[i++].string); |
| 374 | |
| 375 | oidcpy(&ref->old_oid, &old_oid); |
| 376 | **list = ref; |
| 377 | *list = &ref->next; |
| 378 | |
| 379 | for (; i < line_sections.nr; i++) { |
| 380 | const char *arg = line_sections.items[i].string; |
| 381 | if (skip_prefix(arg, "symref-target:", &arg)) |
| 382 | ref->symref = xstrdup(arg); |
| 383 | |
| 384 | if (skip_prefix(arg, "peeled:", &arg)) { |
| 385 | struct object_id peeled_oid; |
| 386 | char *peeled_name; |
| 387 | struct ref *peeled; |
| 388 | if (parse_oid_hex(arg, &peeled_oid, &end) || *end) { |
| 389 | ret = 0; |
| 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | peeled_name = xstrfmt("%s^{}", ref->name); |
| 394 | peeled = alloc_ref(peeled_name); |
| 395 | |
| 396 | oidcpy(&peeled->old_oid, &peeled_oid); |
| 397 | **list = peeled; |
| 398 | *list = &peeled->next; |
| 399 | |
| 400 | free(peeled_name); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | out: |
| 405 | string_list_clear(&line_sections, 0); |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | struct 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 Hamano | 5d54cff | 2013-09-17 16:29:28 -0700 | [diff] [blame] | 445 | 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] | 446 | { |
| 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 King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 457 | 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 Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 477 | feature_list = found + 1; |
| 478 | } |
| 479 | return NULL; |
Johannes Schindelin | 211b5f9 | 2005-10-28 04:48:54 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Jeff King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 482 | int parse_feature_request(const char *feature_list, const char *feature) |
| 483 | { |
| 484 | return !!parse_feature_value(feature_list, feature, NULL); |
| 485 | } |
| 486 | |
| 487 | const char *server_feature_value(const char *feature, int *len) |
| 488 | { |
Brandon Williams | e52449b | 2018-03-15 10:31:21 -0700 | [diff] [blame] | 489 | return parse_feature_value(server_capabilities_v1, feature, len); |
Jeff King | 9442710 | 2012-08-13 21:59:27 -0400 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | int server_supports(const char *feature) |
| 493 | { |
| 494 | return !!server_feature_value(feature, NULL); |
| 495 | } |
| 496 | |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 497 | enum protocol { |
| 498 | PROTO_LOCAL = 1, |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 499 | PROTO_FILE, |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 500 | PROTO_SSH, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 501 | PROTO_GIT |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 502 | }; |
| 503 | |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 504 | int 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ögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 512 | static const char *prot_name(enum protocol protocol) |
| 513 | { |
| 514 | switch (protocol) { |
| 515 | case PROTO_LOCAL: |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 516 | case PROTO_FILE: |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 517 | return "file"; |
| 518 | case PROTO_SSH: |
| 519 | return "ssh"; |
| 520 | case PROTO_GIT: |
| 521 | return "git"; |
| 522 | default: |
Tobias Klauser | 83e6bda | 2015-09-24 14:44:49 +0200 | [diff] [blame] | 523 | return "unknown protocol"; |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 527 | static 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 Nieto | 07c7782 | 2016-02-15 15:29:06 +0100 | [diff] [blame] | 533 | if (!strcmp(name, "git+ssh")) /* deprecated - do not use */ |
Linus Torvalds | c05186c | 2005-10-14 17:14:56 -0700 | [diff] [blame] | 534 | return PROTO_SSH; |
Carlos Martín Nieto | 07c7782 | 2016-02-15 15:29:06 +0100 | [diff] [blame] | 535 | if (!strcmp(name, "ssh+git")) /* deprecated - do not use */ |
Linus Torvalds | c05186c | 2005-10-14 17:14:56 -0700 | [diff] [blame] | 536 | return PROTO_SSH; |
Linus Torvalds | 72a4f4b | 2007-08-01 10:03:37 -0700 | [diff] [blame] | 537 | if (!strcmp(name, "file")) |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 538 | return PROTO_FILE; |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 539 | die("I don't handle protocol '%s'", name); |
| 540 | } |
| 541 | |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 542 | static 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 Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 566 | #define STR_(s) # s |
| 567 | #define STR(s) STR_(s) |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 568 | |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 569 | static void get_host_and_port(char **host, const char **port) |
| 570 | { |
| 571 | char *colon, *end; |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 572 | end = host_end(host, 1); |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 573 | colon = strchr(end, ':'); |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 574 | if (colon) { |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 575 | 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ögershausen | 6b6c5f7 | 2015-04-07 22:03:25 +0200 | [diff] [blame] | 579 | } else if (!colon[1]) { |
| 580 | *colon = 0; |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 581 | } |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Eric Wong | e47a858 | 2011-12-06 04:39:36 +0000 | [diff] [blame] | 585 | static 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 | |
hpa | 49744d6 | 2005-09-28 16:52:21 -0700 | [diff] [blame] | 594 | #ifndef NO_IPV6 |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 595 | |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 596 | static const char *ai_name(const struct addrinfo *ai) |
| 597 | { |
Benjamin Kramer | 785a985 | 2009-04-24 14:16:41 +0200 | [diff] [blame] | 598 | static char addr[NI_MAXHOST]; |
| 599 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0, |
| 600 | NI_NUMERICHOST) != 0) |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 601 | xsnprintf(addr, sizeof(addr), "(unknown)"); |
Benjamin Kramer | 785a985 | 2009-04-24 14:16:41 +0200 | [diff] [blame] | 602 | |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 603 | return addr; |
| 604 | } |
| 605 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 606 | /* |
| 607 | * Returns a connected socket() fd, or else die()s. |
| 608 | */ |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 609 | static int git_tcp_connect_sock(char *host, int flags) |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 610 | { |
Dave Zarzycki | 63a995b | 2011-07-12 09:28:34 -0700 | [diff] [blame] | 611 | struct strbuf error_message = STRBUF_INIT; |
| 612 | int sockfd = -1; |
Timo Hirvonen | 554fe20 | 2006-06-28 12:04:39 +0300 | [diff] [blame] | 613 | const char *port = STR(DEFAULT_GIT_PORT); |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 614 | struct addrinfo hints, *ai0, *ai; |
| 615 | int gai; |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 616 | int cnt = 0; |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 617 | |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 618 | get_host_and_port(&host, &port); |
| 619 | if (!*port) |
| 620 | port = "<none>"; |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 621 | |
| 622 | memset(&hints, 0, sizeof(hints)); |
Eric Wong | c915f11 | 2016-02-03 04:09:14 +0000 | [diff] [blame] | 623 | if (flags & CONNECT_IPV4) |
| 624 | hints.ai_family = AF_INET; |
| 625 | else if (flags & CONNECT_IPV6) |
| 626 | hints.ai_family = AF_INET6; |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 627 | hints.ai_socktype = SOCK_STREAM; |
| 628 | hints.ai_protocol = IPPROTO_TCP; |
| 629 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 630 | if (flags & CONNECT_VERBOSE) |
| 631 | fprintf(stderr, "Looking up %s ... ", host); |
| 632 | |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 633 | gai = getaddrinfo(host, port, &hints, &ai); |
| 634 | if (gai) |
Linus Torvalds | 608d48b | 2007-03-27 09:50:20 -0700 | [diff] [blame] | 635 | 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] | 636 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 637 | if (flags & CONNECT_VERBOSE) |
| 638 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); |
| 639 | |
Erik Faye-Lund | e08afec | 2011-08-01 13:16:09 +0200 | [diff] [blame] | 640 | for (ai0 = ai; ai; ai = ai->ai_next, cnt++) { |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 641 | sockfd = socket(ai->ai_family, |
| 642 | ai->ai_socktype, ai->ai_protocol); |
Dave Zarzycki | 63a995b | 2011-07-12 09:28:34 -0700 | [diff] [blame] | 643 | 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 Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 649 | sockfd = -1; |
| 650 | continue; |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 651 | } |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 652 | if (flags & CONNECT_VERBOSE) |
| 653 | fprintf(stderr, "%s ", ai_name(ai)); |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 654 | break; |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 655 | } |
| 656 | |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 657 | freeaddrinfo(ai0); |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 658 | |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 659 | if (sockfd < 0) |
Dave Zarzycki | 63a995b | 2011-07-12 09:28:34 -0700 | [diff] [blame] | 660 | die("unable to connect to %s:\n%s", host, error_message.buf); |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 09:10:36 -0400 | [diff] [blame] | 661 | |
Eric Wong | e47a858 | 2011-12-06 04:39:36 +0000 | [diff] [blame] | 662 | enable_keepalive(sockfd); |
| 663 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 664 | if (flags & CONNECT_VERBOSE) |
| 665 | fprintf(stderr, "done.\n"); |
| 666 | |
Dave Zarzycki | 63a995b | 2011-07-12 09:28:34 -0700 | [diff] [blame] | 667 | strbuf_release(&error_message); |
| 668 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 669 | return sockfd; |
Linus Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 670 | } |
| 671 | |
hpa | 49744d6 | 2005-09-28 16:52:21 -0700 | [diff] [blame] | 672 | #else /* NO_IPV6 */ |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 673 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 674 | /* |
| 675 | * Returns a connected socket() fd, or else die()s. |
| 676 | */ |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 677 | static int git_tcp_connect_sock(char *host, int flags) |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 678 | { |
Erik Faye-Lund | 7203a2d | 2011-08-01 13:16:10 +0200 | [diff] [blame] | 679 | struct strbuf error_message = STRBUF_INIT; |
| 680 | int sockfd = -1; |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 681 | const char *port = STR(DEFAULT_GIT_PORT); |
| 682 | char *ep; |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 683 | struct hostent *he; |
| 684 | struct sockaddr_in sa; |
| 685 | char **ap; |
| 686 | unsigned int nport; |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 687 | int cnt; |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 688 | |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 689 | get_host_and_port(&host, &port); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 690 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 691 | if (flags & CONNECT_VERBOSE) |
| 692 | fprintf(stderr, "Looking up %s ... ", host); |
| 693 | |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 694 | 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 Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 702 | die("Unknown port %s", port); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 703 | nport = se->s_port; |
| 704 | } |
| 705 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 706 | if (flags & CONNECT_VERBOSE) |
| 707 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); |
| 708 | |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 709 | for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 710 | memset(&sa, 0, sizeof sa); |
| 711 | sa.sin_family = he->h_addrtype; |
Peter Anvin | 6573faf | 2005-09-28 17:26:44 -0700 | [diff] [blame] | 712 | sa.sin_port = htons(nport); |
Paul Serice | c616421 | 2005-11-22 07:54:23 -0600 | [diff] [blame] | 713 | memcpy(&sa.sin_addr, *ap, he->h_length); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 714 | |
Erik Faye-Lund | 7203a2d | 2011-08-01 13:16:10 +0200 | [diff] [blame] | 715 | 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 Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 719 | host, |
| 720 | cnt, |
| 721 | inet_ntoa(*(struct in_addr *)&sa.sin_addr), |
Erik Faye-Lund | 7203a2d | 2011-08-01 13:16:10 +0200 | [diff] [blame] | 722 | strerror(errno)); |
| 723 | if (0 <= sockfd) |
| 724 | close(sockfd); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 725 | sockfd = -1; |
| 726 | continue; |
| 727 | } |
Alex Riesen | ba50532 | 2007-05-23 23:34:27 +0200 | [diff] [blame] | 728 | if (flags & CONNECT_VERBOSE) |
| 729 | fprintf(stderr, "%s ", |
| 730 | inet_ntoa(*(struct in_addr *)&sa.sin_addr)); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 731 | break; |
| 732 | } |
| 733 | |
| 734 | if (sockfd < 0) |
Erik Faye-Lund | 7203a2d | 2011-08-01 13:16:10 +0200 | [diff] [blame] | 735 | die("unable to connect to %s:\n%s", host, error_message.buf); |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 736 | |
Eric Wong | e47a858 | 2011-12-06 04:39:36 +0000 | [diff] [blame] | 737 | enable_keepalive(sockfd); |
| 738 | |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 739 | if (flags & CONNECT_VERBOSE) |
| 740 | fprintf(stderr, "done.\n"); |
| 741 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 742 | return sockfd; |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 743 | } |
| 744 | |
hpa | 49744d6 | 2005-09-28 16:52:21 -0700 | [diff] [blame] | 745 | #endif /* NO_IPV6 */ |
hpa | 4c505f7 | 2005-09-28 16:37:58 -0700 | [diff] [blame] | 746 | |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 747 | |
Jonathan Nieder | 8e34978 | 2017-11-20 13:22:54 -0800 | [diff] [blame] | 748 | /* |
| 749 | * Dummy child_process returned by git_connect() if the transport protocol |
| 750 | * does not need fork(2). |
| 751 | */ |
| 752 | static struct child_process no_fork = CHILD_PROCESS_INIT; |
| 753 | |
| 754 | int git_connection_is_socket(struct child_process *conn) |
| 755 | { |
| 756 | return conn == &no_fork; |
| 757 | } |
| 758 | |
| 759 | 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] | 760 | { |
Michael S. Tsirkin | 7841ce7 | 2007-05-16 20:09:41 +0300 | [diff] [blame] | 761 | int sockfd = git_tcp_connect_sock(host, flags); |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 762 | |
| 763 | fd[0] = sockfd; |
Junio C Hamano | ec587fd | 2007-01-21 17:10:51 -0800 | [diff] [blame] | 764 | fd[1] = dup(sockfd); |
Jonathan Nieder | 8e34978 | 2017-11-20 13:22:54 -0800 | [diff] [blame] | 765 | |
| 766 | return &no_fork; |
Jon Loeliger | 5ad312b | 2006-06-06 22:58:41 -0500 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 770 | static char *git_proxy_command; |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 771 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 772 | static int git_proxy_command_options(const char *var, const char *value, |
| 773 | void *cb) |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 774 | { |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 775 | if (!strcmp(var, "core.gitproxy")) { |
YOSHIFUJI Hideaki / 吉藤英明 | c3df856 | 2005-11-22 12:18:23 +0900 | [diff] [blame] | 776 | const char *for_pos; |
| 777 | int matchlen = -1; |
| 778 | int hostlen; |
Erik Faye-Lund | 15112c9 | 2009-03-11 02:38:12 +0000 | [diff] [blame] | 779 | const char *rhost_name = cb; |
| 780 | int rhost_len = strlen(rhost_name); |
YOSHIFUJI Hideaki / 吉藤英明 | c3df856 | 2005-11-22 12:18:23 +0900 | [diff] [blame] | 781 | |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 782 | if (git_proxy_command) |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 783 | return 0; |
Junio C Hamano | c64b9ad | 2008-02-11 10:52:15 -0800 | [diff] [blame] | 784 | if (!value) |
| 785 | return config_error_nonbool(var); |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 786 | /* [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 / 吉藤英明 | c3df856 | 2005-11-22 12:18:23 +0900 | [diff] [blame] | 792 | for_pos = strstr(value, " for "); |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 793 | 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 Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 808 | } |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 809 | if (0 <= matchlen) { |
| 810 | /* core.gitproxy = none for kernel.org */ |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 811 | if (matchlen == 4 && |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 812 | !memcmp(value, "none", 4)) |
| 813 | matchlen = 0; |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 814 | git_proxy_command = xmemdupz(value, matchlen); |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 815 | } |
| 816 | return 0; |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 819 | return git_default_config(var, value, cb); |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 822 | static int git_use_proxy(const char *host) |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 823 | { |
| 824 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); |
Erik Faye-Lund | 15112c9 | 2009-03-11 02:38:12 +0000 | [diff] [blame] | 825 | git_config(git_proxy_command_options, (void*)host); |
Junio C Hamano | e814bc4 | 2005-11-19 03:48:56 -0800 | [diff] [blame] | 826 | return (git_proxy_command && *git_proxy_command); |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Jeff King | 5cbf824 | 2011-05-16 02:46:07 -0400 | [diff] [blame] | 829 | static struct child_process *git_proxy_connect(int fd[2], char *host) |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 830 | { |
Timo Hirvonen | 554fe20 | 2006-06-28 12:04:39 +0300 | [diff] [blame] | 831 | const char *port = STR(DEFAULT_GIT_PORT); |
Jeff King | 5cbf824 | 2011-05-16 02:46:07 -0400 | [diff] [blame] | 832 | struct child_process *proxy; |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 833 | |
Michael Lukashov | 72a534d | 2010-02-17 20:56:02 +0000 | [diff] [blame] | 834 | get_host_and_port(&host, &port); |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 835 | |
Jeff King | 3be4cf0 | 2017-07-28 15:26:50 -0400 | [diff] [blame] | 836 | 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é Scharfe | 483bbd4 | 2014-08-19 21:10:48 +0200 | [diff] [blame] | 841 | proxy = xmalloc(sizeof(*proxy)); |
| 842 | child_process_init(proxy); |
Jeff King | 1823bea | 2014-05-15 04:34:09 -0400 | [diff] [blame] | 843 | argv_array_push(&proxy->args, git_proxy_command); |
| 844 | argv_array_push(&proxy->args, host); |
| 845 | argv_array_push(&proxy->args, port); |
Jeff King | 5cbf824 | 2011-05-16 02:46:07 -0400 | [diff] [blame] | 846 | proxy->in = -1; |
| 847 | proxy->out = -1; |
| 848 | if (start_command(proxy)) |
Jeff King | 1823bea | 2014-05-15 04:34:09 -0400 | [diff] [blame] | 849 | die("cannot start proxy %s", git_proxy_command); |
Jeff King | 5cbf824 | 2011-05-16 02:46:07 -0400 | [diff] [blame] | 850 | fd[0] = proxy->out; /* read from proxy stdout */ |
| 851 | fd[1] = proxy->in; /* write to proxy stdin */ |
| 852 | return proxy; |
Paul Collins | f801477 | 2005-11-04 14:57:16 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 855 | static char *get_port(char *host) |
Luben Tuikov | 2e77666 | 2007-09-01 02:36:31 -0700 | [diff] [blame] | 856 | { |
| 857 | char *end; |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 858 | char *p = strchr(host, ':'); |
| 859 | |
Luben Tuikov | 2e77666 | 2007-09-01 02:36:31 -0700 | [diff] [blame] | 860 | if (p) { |
René Scharfe | 8f14825 | 2008-12-21 02:12:11 +0100 | [diff] [blame] | 861 | long port = strtol(p + 1, &end, 10); |
| 862 | if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) { |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 863 | *p = '\0'; |
| 864 | return p+1; |
Luben Tuikov | 2e77666 | 2007-09-01 02:36:31 -0700 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
| 868 | return NULL; |
| 869 | } |
| 870 | |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 871 | /* |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 872 | * Extract protocol and relevant parts from the specified connection URL. |
| 873 | * The caller must free() the returned strings. |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 874 | */ |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 875 | 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] | 876 | char **ret_path) |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 877 | { |
Jeff King | 9d2e942 | 2010-05-23 05:19:44 -0400 | [diff] [blame] | 878 | char *url; |
Benjamin Kramer | 8e76bf3 | 2009-03-13 13:51:33 +0100 | [diff] [blame] | 879 | char *host, *path; |
YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 19:23:42 +0900 | [diff] [blame] | 880 | char *end; |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 881 | int separator = '/'; |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 882 | enum protocol protocol = PROTO_LOCAL; |
Junio C Hamano | f0b7367 | 2006-06-19 18:25:21 -0700 | [diff] [blame] | 883 | |
Jeff King | 9d2e942 | 2010-05-23 05:19:44 -0400 | [diff] [blame] | 884 | if (is_url(url_orig)) |
| 885 | url = url_decode(url_orig); |
| 886 | else |
| 887 | url = xstrdup(url_orig); |
| 888 | |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 889 | host = strstr(url, "://"); |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 890 | if (host) { |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 891 | *host = '\0'; |
| 892 | protocol = get_protocol(url); |
| 893 | host += 3; |
YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 19:23:42 +0900 | [diff] [blame] | 894 | } else { |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 895 | host = url; |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 896 | if (!url_is_local_not_ssh(url)) { |
| 897 | protocol = PROTO_SSH; |
| 898 | separator = ':'; |
| 899 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 19:23:42 +0900 | [diff] [blame] | 900 | } |
| 901 | |
Ilari Liusvaara | 9aa5053 | 2010-01-26 20:24:42 +0200 | [diff] [blame] | 902 | /* |
Torsten Bögershausen | 83b0587 | 2013-11-28 20:49:54 +0100 | [diff] [blame] | 903 | * Don't do destructive transforms as protocol code does |
| 904 | * '[]' unwrapping in get_host_and_port() |
Ilari Liusvaara | 9aa5053 | 2010-01-26 20:24:42 +0200 | [diff] [blame] | 905 | */ |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 906 | end = host_end(&host, 0); |
YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 19:23:42 +0900 | [diff] [blame] | 907 | |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 908 | if (protocol == PROTO_LOCAL) |
Linus Torvalds | 72a4f4b | 2007-08-01 10:03:37 -0700 | [diff] [blame] | 909 | path = end; |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 910 | 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 Torvalds | 2386d65 | 2005-07-13 18:46:20 -0700 | [diff] [blame] | 914 | |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 915 | 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ögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 922 | |
| 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 Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 927 | if (path[1] == '~') |
| 928 | path++; |
Andreas Ericsson | faea9cc | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 929 | } |
| 930 | |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 931 | path = xstrdup(path); |
| 932 | *end = '\0'; |
| 933 | |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 934 | *ret_host = xstrdup(host); |
Torsten Bögershausen | c59ab2e | 2013-11-28 20:50:03 +0100 | [diff] [blame] | 935 | *ret_path = path; |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 936 | free(url); |
| 937 | return protocol; |
| 938 | } |
| 939 | |
Nguyễn Thái Ngọc Duy | 3c8ede3 | 2016-06-26 13:16:35 +0200 | [diff] [blame] | 940 | static 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 Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 953 | enum ssh_variant { |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 954 | VARIANT_AUTO, |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 955 | VARIANT_SIMPLE, |
| 956 | VARIANT_SSH, |
| 957 | VARIANT_PLINK, |
| 958 | VARIANT_PUTTY, |
| 959 | VARIANT_TORTOISEPLINK, |
| 960 | }; |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 961 | |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 962 | static void override_ssh_variant(enum ssh_variant *ssh_variant) |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 963 | { |
| 964 | const char *variant = getenv("GIT_SSH_VARIANT"); |
| 965 | |
| 966 | if (!variant && git_config_get_string_const("ssh.variant", &variant)) |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 967 | return; |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 968 | |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 969 | if (!strcmp(variant, "auto")) |
| 970 | *ssh_variant = VARIANT_AUTO; |
| 971 | else if (!strcmp(variant, "plink")) |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 972 | *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 Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 981 | } |
| 982 | |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 983 | static enum ssh_variant determine_ssh_variant(const char *ssh_command, |
| 984 | int is_cmdline) |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 985 | { |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 986 | enum ssh_variant ssh_variant = VARIANT_AUTO; |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 987 | const char *variant; |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 988 | char *p = NULL; |
| 989 | |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 990 | override_ssh_variant(&ssh_variant); |
| 991 | |
| 992 | if (ssh_variant != VARIANT_AUTO) |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 993 | return ssh_variant; |
Junio C Hamano | 486c8e8 | 2017-02-09 09:20:25 -0800 | [diff] [blame] | 994 | |
| 995 | if (!is_cmdline) { |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 996 | p = xstrdup(ssh_command); |
| 997 | variant = basename(p); |
| 998 | } else { |
| 999 | const char **ssh_argv; |
| 1000 | |
| 1001 | p = xstrdup(ssh_command); |
Jeff King | 22e5ae5 | 2017-04-10 20:30:23 -0400 | [diff] [blame] | 1002 | if (split_cmdline(p, &ssh_argv) > 0) { |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1003 | 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 King | 5d2993b | 2017-04-20 16:21:58 -0400 | [diff] [blame] | 1010 | } else { |
| 1011 | free(p); |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1012 | return ssh_variant; |
Jeff King | 5d2993b | 2017-04-20 16:21:58 -0400 | [diff] [blame] | 1013 | } |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1014 | } |
| 1015 | |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1016 | 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 Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1022 | else if (!strcasecmp(variant, "tortoiseplink") || |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1023 | !strcasecmp(variant, "tortoiseplink.exe")) |
| 1024 | ssh_variant = VARIANT_TORTOISEPLINK; |
| 1025 | |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1026 | free(p); |
Brandon Williams | 94b8ae5 | 2017-10-16 10:55:31 -0700 | [diff] [blame] | 1027 | return ssh_variant; |
Johannes Schindelin | e2824e4 | 2017-02-01 13:01:10 +0100 | [diff] [blame] | 1028 | } |
| 1029 | |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1030 | /* |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1031 | * 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 | */ |
| 1036 | static struct child_process *git_connect_git(int fd[2], char *hostandport, |
| 1037 | const char *path, const char *prog, |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1038 | enum protocol_version version, |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1039 | 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 Nieder | 233cd28 | 2017-11-20 14:04:58 -0800 | [diff] [blame] | 1056 | /* |
| 1057 | * These underlying connection commands die() if they |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1058 | * 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 Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1077 | if (version > 0) { |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1078 | strbuf_addch(&request, '\0'); |
| 1079 | strbuf_addf(&request, "version=%d%c", |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1080 | version, '\0'); |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1081 | } |
| 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 Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1090 | /* |
| 1091 | * Append the appropriate environment variables to `env` and options to |
| 1092 | * `args` for running ssh in Git's SSH-tunneled transport. |
| 1093 | */ |
| 1094 | static void push_ssh_options(struct argv_array *args, struct argv_array *env, |
| 1095 | enum ssh_variant variant, const char *port, |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1096 | enum protocol_version version, int flags) |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1097 | { |
| 1098 | if (variant == VARIANT_SSH && |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1099 | version > 0) { |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1100 | 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 Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1103 | version); |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
Jonathan Nieder | a3f5b66 | 2017-11-20 13:30:30 -0800 | [diff] [blame] | 1106 | 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 Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1116 | argv_array_push(args, "-4"); |
Jonathan Nieder | a3f5b66 | 2017-11-20 13:30:30 -0800 | [diff] [blame] | 1117 | } |
| 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 Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1128 | argv_array_push(args, "-6"); |
Jonathan Nieder | a3f5b66 | 2017-11-20 13:30:30 -0800 | [diff] [blame] | 1129 | } |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | if (variant == VARIANT_TORTOISEPLINK) |
| 1133 | argv_array_push(args, "-batch"); |
| 1134 | |
Jonathan Nieder | 3fa5e0d | 2017-11-20 13:31:01 -0800 | [diff] [blame] | 1135 | 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 Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1142 | argv_array_push(args, "-p"); |
Jonathan Nieder | 3fa5e0d | 2017-11-20 13:31:01 -0800 | [diff] [blame] | 1143 | break; |
| 1144 | case VARIANT_PLINK: |
| 1145 | case VARIANT_PUTTY: |
| 1146 | case VARIANT_TORTOISEPLINK: |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1147 | argv_array_push(args, "-P"); |
Jonathan Nieder | 3fa5e0d | 2017-11-20 13:31:01 -0800 | [diff] [blame] | 1148 | } |
Jonathan Nieder | 957e2ad | 2017-11-20 13:26:19 -0800 | [diff] [blame] | 1149 | |
| 1150 | argv_array_push(args, port); |
| 1151 | } |
| 1152 | } |
| 1153 | |
Jonathan Nieder | fce54ce | 2017-11-20 14:19:43 -0800 | [diff] [blame] | 1154 | /* Prepare a child_process for use by Git's SSH-tunneled transport. */ |
| 1155 | 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] | 1156 | const char *port, enum protocol_version version, |
| 1157 | int flags) |
Jonathan Nieder | fce54ce | 2017-11-20 14:19:43 -0800 | [diff] [blame] | 1158 | { |
| 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 Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 1182 | 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 Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1191 | VARIANT_SSH, port, version, flags); |
Jonathan Nieder | 0da0e49 | 2017-11-20 13:30:04 -0800 | [diff] [blame] | 1192 | argv_array_push(&detect.args, ssh_host); |
| 1193 | |
| 1194 | variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH; |
| 1195 | } |
| 1196 | |
Jonathan Nieder | fce54ce | 2017-11-20 14:19:43 -0800 | [diff] [blame] | 1197 | argv_array_push(&conn->args, ssh); |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1198 | push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags); |
Jonathan Nieder | fce54ce | 2017-11-20 14:19:43 -0800 | [diff] [blame] | 1199 | argv_array_push(&conn->args, ssh_host); |
| 1200 | } |
| 1201 | |
Jonathan Nieder | 2ac67cb | 2017-11-20 13:23:27 -0800 | [diff] [blame] | 1202 | /* |
Jonathan Nieder | 8e34978 | 2017-11-20 13:22:54 -0800 | [diff] [blame] | 1203 | * 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 Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1208 | * |
| 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 Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 1213 | struct child_process *git_connect(int fd[2], const char *url, |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1214 | const char *prog, int flags) |
| 1215 | { |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1216 | char *hostandport, *path; |
Jonathan Nieder | 8e34978 | 2017-11-20 13:22:54 -0800 | [diff] [blame] | 1217 | struct child_process *conn; |
Johannes Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 1218 | enum protocol protocol; |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1219 | enum protocol_version version = get_protocol_version_config(); |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1220 | |
Brandon Williams | 1aa8dded | 2018-03-15 10:31:31 -0700 | [diff] [blame] | 1221 | /* |
| 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 Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1229 | /* Without this we cannot rely on waitpid() to tell |
| 1230 | * what happened to our children. |
| 1231 | */ |
| 1232 | signal(SIGCHLD, SIG_DFL); |
| 1233 | |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1234 | protocol = parse_connect_url(url, &hostandport, &path); |
Torsten Bögershausen | 3f55cca | 2015-02-21 16:52:55 +0100 | [diff] [blame] | 1235 | if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) { |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 1236 | printf("Diag: url=%s\n", url ? url : "NULL"); |
| 1237 | printf("Diag: protocol=%s\n", prot_name(protocol)); |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1238 | printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL"); |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 1239 | printf("Diag: path=%s\n", path ? path : "NULL"); |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1240 | conn = NULL; |
| 1241 | } else if (protocol == PROTO_GIT) { |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1242 | conn = git_connect_git(fd, hostandport, path, prog, version, flags); |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1243 | } else { |
Rene Scharfe | f139929 | 2017-08-30 19:49:39 +0200 | [diff] [blame] | 1244 | struct strbuf cmd = STRBUF_INIT; |
Brandon Williams | 0c2f0d2 | 2017-10-16 10:55:28 -0700 | [diff] [blame] | 1245 | const char *const *var; |
Rene Scharfe | f139929 | 2017-08-30 19:49:39 +0200 | [diff] [blame] | 1246 | |
René Scharfe | 483bbd4 | 2014-08-19 21:10:48 +0200 | [diff] [blame] | 1247 | conn = xmalloc(sizeof(*conn)); |
| 1248 | child_process_init(conn); |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1249 | |
Jeff King | aeeb2d4 | 2017-07-28 15:28:55 -0400 | [diff] [blame] | 1250 | if (looks_like_command_line_option(path)) |
| 1251 | die("strange pathname '%s' blocked", path); |
| 1252 | |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1253 | strbuf_addstr(&cmd, prog); |
| 1254 | strbuf_addch(&cmd, ' '); |
| 1255 | sq_quote_buf(&cmd, path); |
Christian Couder | 0f503d7 | 2006-09-11 07:04:50 +0200 | [diff] [blame] | 1256 | |
Jeff King | aab4043 | 2015-09-04 18:40:08 -0400 | [diff] [blame] | 1257 | /* remove repo-local variables from the environment */ |
Brandon Williams | 0c2f0d2 | 2017-10-16 10:55:28 -0700 | [diff] [blame] | 1258 | for (var = local_repo_env; *var; var++) |
| 1259 | argv_array_push(&conn->env_array, *var); |
| 1260 | |
Jeff King | a48b409 | 2015-09-08 04:33:14 -0400 | [diff] [blame] | 1261 | conn->use_shell = 1; |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1262 | conn->in = conn->out = -1; |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1263 | if (protocol == PROTO_SSH) { |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1264 | char *ssh_host = hostandport; |
| 1265 | const char *port = NULL; |
Jeff King | a5adace | 2015-09-16 13:12:52 -0400 | [diff] [blame] | 1266 | transport_check_allowed("ssh"); |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1267 | get_host_and_port(&ssh_host, &port); |
Christian Couder | 0f503d7 | 2006-09-11 07:04:50 +0200 | [diff] [blame] | 1268 | |
Torsten Bögershausen | 86ceb33 | 2015-02-21 16:52:48 +0100 | [diff] [blame] | 1269 | if (!port) |
| 1270 | port = get_port(ssh_host); |
Junio C Hamano | 42da484 | 2015-03-05 12:45:44 -0800 | [diff] [blame] | 1271 | |
Torsten Bögershausen | 3f55cca | 2015-02-21 16:52:55 +0100 | [diff] [blame] | 1272 | 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 Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1278 | |
Torsten Bögershausen | 3f55cca | 2015-02-21 16:52:55 +0100 | [diff] [blame] | 1279 | free(hostandport); |
| 1280 | free(path); |
Stefan Beller | 04f20c0 | 2015-03-09 09:58:22 -0700 | [diff] [blame] | 1281 | free(conn); |
Rene Scharfe | f139929 | 2017-08-30 19:49:39 +0200 | [diff] [blame] | 1282 | strbuf_release(&cmd); |
Torsten Bögershausen | 3f55cca | 2015-02-21 16:52:55 +0100 | [diff] [blame] | 1283 | return NULL; |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1284 | } |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1285 | 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] | 1286 | } else { |
Jeff King | a5adace | 2015-09-16 13:12:52 -0400 | [diff] [blame] | 1287 | transport_check_allowed("file"); |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1288 | if (version > 0) { |
Brandon Williams | 0c2f0d2 | 2017-10-16 10:55:28 -0700 | [diff] [blame] | 1289 | argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d", |
Brandon Williams | 40fc51e | 2018-03-15 10:31:30 -0700 | [diff] [blame] | 1290 | version); |
Brandon Williams | 0c2f0d2 | 2017-10-16 10:55:28 -0700 | [diff] [blame] | 1291 | } |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1292 | } |
Jeff King | 1823bea | 2014-05-15 04:34:09 -0400 | [diff] [blame] | 1293 | argv_array_push(&conn->args, cmd.buf); |
Johannes Sixt | f364cb8 | 2007-10-19 21:47:54 +0200 | [diff] [blame] | 1294 | |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1295 | if (start_command(conn)) |
| 1296 | die("unable to fork"); |
Johannes Sixt | f364cb8 | 2007-10-19 21:47:54 +0200 | [diff] [blame] | 1297 | |
Torsten Bögershausen | a2036d7 | 2013-11-28 20:50:15 +0100 | [diff] [blame] | 1298 | 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 Sixt | cabc3c1 | 2013-11-28 20:49:01 +0100 | [diff] [blame] | 1303 | free(path); |
Johannes Sixt | 98158e9 | 2007-10-19 21:47:53 +0200 | [diff] [blame] | 1304 | return conn; |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Johannes Sixt | 98158e9 | 2007-10-19 21:47:53 +0200 | [diff] [blame] | 1307 | int finish_connect(struct child_process *conn) |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1308 | { |
Johannes Sixt | f364cb8 | 2007-10-19 21:47:54 +0200 | [diff] [blame] | 1309 | int code; |
Jeff King | 7ffe853 | 2011-05-16 02:52:11 -0400 | [diff] [blame] | 1310 | if (!conn || git_connection_is_socket(conn)) |
Franck Bui-Huu | f42a5c4 | 2006-09-12 11:00:13 +0200 | [diff] [blame] | 1311 | return 0; |
| 1312 | |
Johannes Sixt | f364cb8 | 2007-10-19 21:47:54 +0200 | [diff] [blame] | 1313 | code = finish_command(conn); |
Johannes Sixt | 98158e9 | 2007-10-19 21:47:53 +0200 | [diff] [blame] | 1314 | free(conn); |
Johannes Sixt | f364cb8 | 2007-10-19 21:47:54 +0200 | [diff] [blame] | 1315 | return code; |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 1316 | } |