blob: 2778481264d0e1fdd8864743fc860ef63718524e [file] [log] [blame]
Jason Riedy731043f2006-01-25 12:38:36 -08001#include "git-compat-util.h"
Linus Torvaldsf7192592005-07-04 11:57:58 -07002#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Linus Torvalds41cb7482005-07-05 15:44:09 -07004#include "pkt-line.h"
Junio C Hamanob10d0ec2005-07-08 00:02:52 -07005#include "quote.h"
Junio C Hamano6abf5c02005-10-16 00:25:26 -07006#include "refs.h"
Shawn O. Pearce15a1c0122007-03-12 19:00:19 -04007#include "run-command.h"
Daniel Barkalow6b628162007-05-12 11:45:59 -04008#include "remote.h"
Junio C Hamano47a59182013-07-08 13:56:53 -07009#include "connect.h"
Jeff King9d2e9422010-05-23 05:19:44 -040010#include "url.h"
Junio C Hamanoa45b5f02013-09-17 19:10:31 -070011#include "string-list.h"
Nguyễn Thái Ngọc Duy13eb4622013-12-05 20:02:29 +070012#include "sha1-array.h"
Jeff Kinga5adace2015-09-16 13:12:52 -040013#include "transport.h"
Jonathan Tan0cd83282017-09-26 16:56:19 -070014#include "strbuf.h"
Brandon Williamse52449b2018-03-15 10:31:21 -070015#include "version.h"
Brandon Williams26090432017-10-16 10:55:27 -070016#include "protocol.h"
Nguyễn Thái Ngọc Duy65b5f942018-05-20 20:40:06 +020017#include "alias.h"
Linus Torvaldsf7192592005-07-04 11:57:58 -070018
Brandon Williamse52449b2018-03-15 10:31:21 -070019static char *server_capabilities_v1;
20static struct argv_array server_capabilities_v2 = ARGV_ARRAY_INIT;
Junio C Hamano5d54cff2013-09-17 16:29:28 -070021static const char *parse_feature_value(const char *, const char *, int *);
Johannes Schindelin211b5f92005-10-28 04:48:54 +020022
René Scharfebe0b3f82014-08-30 11:46:54 +020023static int check_ref(const char *name, unsigned int flags)
Linus Torvalds2718ff02006-07-04 12:29:10 -070024{
25 if (!flags)
26 return 1;
27
René Scharfebe0b3f82014-08-30 11:46:54 +020028 if (!skip_prefix(name, "refs/", &name))
Linus Torvalds2718ff02006-07-04 12:29:10 -070029 return 0;
30
Linus Torvalds2718ff02006-07-04 12:29:10 -070031 /* REF_NORMAL means that we don't want the magic fake tag refs */
Michael Haggerty8d9c5012011-09-15 23:10:25 +020032 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
Linus Torvalds2718ff02006-07-04 12:29:10 -070033 return 0;
34
35 /* REF_HEADS means that we want regular branch heads */
René Scharfebe0b3f82014-08-30 11:46:54 +020036 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
Linus Torvalds2718ff02006-07-04 12:29:10 -070037 return 1;
38
39 /* REF_TAGS means that we want tags */
René Scharfebe0b3f82014-08-30 11:46:54 +020040 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
Linus Torvalds2718ff02006-07-04 12:29:10 -070041 return 1;
42
43 /* All type bits clear means that we are ok with anything */
44 return !(flags & ~REF_NORMAL);
45}
46
Daniel Barkalow45773702007-10-29 21:05:40 -040047int check_ref_type(const struct ref *ref, int flags)
48{
René Scharfebe0b3f82014-08-30 11:46:54 +020049 return check_ref(ref->name, flags);
Daniel Barkalow45773702007-10-29 21:05:40 -040050}
51
Nguyễn Thái Ngọc Duyd2bff222018-04-14 19:19:43 +000052static NORETURN void die_initial_contact(int unexpected)
Heiko Voigt46284dd2012-06-19 20:24:50 +020053{
Brandon Williams7e3e4792018-03-14 11:31:44 -070054 /*
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 Nieder55e4f932016-09-09 10:36:29 -070060 if (unexpected)
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +020061 die(_("the remote end hung up upon initial contact"));
Heiko Voigt46284dd2012-06-19 20:24:50 +020062 else
Vasco Almeidaf2b93b32016-09-19 13:08:17 +000063 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 Voigt46284dd2012-06-19 20:24:50 +020066}
67
Brandon Williamse52449b2018-03-15 10:31:21 -070068/* Checks if the server supports the capability 'c' */
69int 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 Duyaad6fdd2018-07-21 09:49:28 +020081 die(_("server doesn't support '%s'"), c);
Brandon Williamse52449b2018-03-15 10:31:21 -070082
83 return 0;
84}
85
Brandon Williamsf7e20502018-03-15 10:31:29 -070086int 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 Duyaad6fdd2018-07-21 09:49:28 +0200103 die(_("server doesn't support feature '%s'"), feature);
Brandon Williamsf7e20502018-03-15 10:31:29 -0700104
105 return 0;
106}
107
Brandon Williamse52449b2018-03-15 10:31:21 -0700108static 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 Duyaad6fdd2018-07-21 09:49:28 +0200114 die(_("expected flush after capabilities"));
Brandon Williamse52449b2018-03-15 10:31:21 -0700115}
116
Brandon Williamsad6ac122018-03-14 11:31:45 -0700117enum protocol_version discover_version(struct packet_reader *reader)
Brandon Williams7e3e4792018-03-14 11:31:44 -0700118{
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 Williams8f6982b2018-03-14 11:31:47 -0700138 case protocol_v2:
Brandon Williamse52449b2018-03-15 10:31:21 -0700139 process_capabilities_v2(reader);
Brandon Williams8f6982b2018-03-14 11:31:47 -0700140 break;
Brandon Williams7e3e4792018-03-14 11:31:44 -0700141 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 Hamanoa45b5f02013-09-17 19:10:31 -0700154static 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é Scharfe5c0b13f2014-07-19 17:35:34 +0200162 sym = xmemdupz(val, len);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700163 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 Kingef4fe562017-05-25 15:33:05 -0400172 item = string_list_append_nodup(symref, sym);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700173 item->util = target;
174 return;
175reject:
176 free(sym);
177 return;
178}
179
180static void annotate_refs_with_symref_info(struct ref *ref)
181{
182 struct string_list symref = STRING_LIST_INIT_DUP;
Brandon Williamse52449b2018-03-15 10:31:21 -0700183 const char *feature_list = server_capabilities_v1;
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700184
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 Haggerty3383e192014-11-25 09:02:35 +0100195 string_list_sort(&symref);
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700196
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 Williams7e3e4792018-03-14 11:31:44 -0700207static void process_capabilities(const char *line, int *len)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700208{
Brandon Williams7e3e4792018-03-14 11:31:44 -0700209 int nul_location = strlen(line);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700210 if (nul_location == *len)
211 return;
Brandon Williamse52449b2018-03-15 10:31:21 -0700212 server_capabilities_v1 = xstrdup(line + nul_location + 1);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700213 *len = nul_location;
214}
215
Brandon Williams7e3e4792018-03-14 11:31:44 -0700216static int process_dummy_ref(const char *line)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700217{
218 struct object_id oid;
219 const char *name;
220
Brandon Williams7e3e4792018-03-14 11:31:44 -0700221 if (parse_oid_hex(line, &oid, &name))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700222 return 0;
223 if (*name != ' ')
224 return 0;
225 name++;
226
Jeff King4a7e27e2018-08-28 17:22:40 -0400227 return oideq(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
Jonathan Tan0cd83282017-09-26 16:56:19 -0700228}
229
Brandon Williams7e3e4792018-03-14 11:31:44 -0700230static void check_no_capabilities(const char *line, int len)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700231{
Brandon Williams7e3e4792018-03-14 11:31:44 -0700232 if (strlen(line) != len)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200233 warning(_("ignoring capabilities after first line '%s'"),
Brandon Williams7e3e4792018-03-14 11:31:44 -0700234 line + strlen(line));
Jonathan Tan0cd83282017-09-26 16:56:19 -0700235}
236
Brandon Williams7e3e4792018-03-14 11:31:44 -0700237static int process_ref(const char *line, int len, struct ref ***list,
238 unsigned int flags, struct oid_array *extra_have)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700239{
240 struct object_id old_oid;
241 const char *name;
242
Brandon Williams7e3e4792018-03-14 11:31:44 -0700243 if (parse_oid_hex(line, &old_oid, &name))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700244 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 Duyaad6fdd2018-07-21 09:49:28 +0200252 die(_("protocol error: unexpected capabilities^{}"));
Jonathan Tan0cd83282017-09-26 16:56:19 -0700253 } 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 Williams7e3e4792018-03-14 11:31:44 -0700259 check_no_capabilities(line, len);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700260 return 1;
261}
262
Brandon Williams7e3e4792018-03-14 11:31:44 -0700263static int process_shallow(const char *line, int len,
264 struct oid_array *shallow_points)
Jonathan Tan0cd83282017-09-26 16:56:19 -0700265{
266 const char *arg;
267 struct object_id old_oid;
268
Brandon Williams7e3e4792018-03-14 11:31:44 -0700269 if (!skip_prefix(line, "shallow ", &arg))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700270 return 0;
271
272 if (get_oid_hex(arg, &old_oid))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200273 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700274 if (!shallow_points)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200275 die(_("repository on the other end cannot be shallow"));
Jonathan Tan0cd83282017-09-26 16:56:19 -0700276 oid_array_append(shallow_points, &old_oid);
Brandon Williams7e3e4792018-03-14 11:31:44 -0700277 check_no_capabilities(line, len);
Jonathan Tan0cd83282017-09-26 16:56:19 -0700278 return 1;
279}
280
Brandon Williams7e3e4792018-03-14 11:31:44 -0700281enum get_remote_heads_state {
282 EXPECTING_FIRST_REF = 0,
283 EXPECTING_REF,
284 EXPECTING_SHALLOW,
285 EXPECTING_DONE,
286};
287
Jonathan Tan0cd83282017-09-26 16:56:19 -0700288/*
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700289 * Read all the refs from the other end
290 */
Brandon Williamsad6ac122018-03-14 11:31:45 -0700291struct ref **get_remote_heads(struct packet_reader *reader,
Jeff King85edf4f2013-02-20 15:06:45 -0500292 struct ref **list, unsigned int flags,
brian m. carlson910650d2017-03-31 01:40:00 +0000293 struct oid_array *extra_have,
294 struct oid_array *shallow_points)
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700295{
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700296 struct ref **orig_list = list;
Brandon Williams7e3e4792018-03-14 11:31:44 -0700297 int len = 0;
298 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
Jonathan Nieder55e4f932016-09-09 10:36:29 -0700299
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700300 *list = NULL;
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700301
Brandon Williams7e3e4792018-03-14 11:31:44 -0700302 while (state != EXPECTING_DONE) {
Brandon Williamsad6ac122018-03-14 11:31:45 -0700303 switch (packet_reader_read(reader)) {
Brandon Williams7e3e4792018-03-14 11:31:44 -0700304 case PACKET_READ_EOF:
305 die_initial_contact(1);
306 case PACKET_READ_NORMAL:
Brandon Williamsad6ac122018-03-14 11:31:45 -0700307 len = reader->pktlen;
Brandon Williams7e3e4792018-03-14 11:31:44 -0700308 break;
309 case PACKET_READ_FLUSH:
310 state = EXPECTING_DONE;
311 break;
312 case PACKET_READ_DELIM:
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200313 die(_("invalid packet"));
Brandon Williams7e3e4792018-03-14 11:31:44 -0700314 }
315
Jonathan Tan0cd83282017-09-26 16:56:19 -0700316 switch (state) {
317 case EXPECTING_FIRST_REF:
Brandon Williamsad6ac122018-03-14 11:31:45 -0700318 process_capabilities(reader->line, &len);
319 if (process_dummy_ref(reader->line)) {
Jonathan Tan0cd83282017-09-26 16:56:19 -0700320 state = EXPECTING_SHALLOW;
321 break;
322 }
323 state = EXPECTING_REF;
324 /* fallthrough */
325 case EXPECTING_REF:
Brandon Williamsad6ac122018-03-14 11:31:45 -0700326 if (process_ref(reader->line, len, &list, flags, extra_have))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700327 break;
328 state = EXPECTING_SHALLOW;
329 /* fallthrough */
330 case EXPECTING_SHALLOW:
Brandon Williamsad6ac122018-03-14 11:31:45 -0700331 if (process_shallow(reader->line, len, shallow_points))
Jonathan Tan0cd83282017-09-26 16:56:19 -0700332 break;
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200333 die(_("protocol error: unexpected '%s'"), reader->line);
Brandon Williams7e3e4792018-03-14 11:31:44 -0700334 case EXPECTING_DONE:
335 break;
Nguyễn Thái Ngọc Duyb06dcd72013-12-05 20:02:33 +0700336 }
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700337 }
Junio C Hamanoa45b5f02013-09-17 19:10:31 -0700338
339 annotate_refs_with_symref_info(*orig_list);
340
Linus Torvaldsd1c133f2005-07-16 13:55:50 -0700341 return list;
342}
343
Brandon Williamse52449b2018-03-15 10:31:21 -0700344/* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
345static 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
402out:
403 string_list_clear(&line_sections, 0);
404 return ret;
405}
406
407struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
408 struct ref **list, int for_push,
Brandon Williamsff473222018-04-23 15:46:23 -0700409 const struct argv_array *ref_prefixes,
410 const struct string_list *server_options)
Brandon Williamse52449b2018-03-15 10:31:21 -0700411{
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 Williamsff473222018-04-23 15:46:23 -0700421 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 Williamse52449b2018-03-15 10:31:21 -0700427 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 Duyaad6fdd2018-07-21 09:49:28 +0200441 die(_("invalid ls-refs response: %s"), reader->line);
Brandon Williamse52449b2018-03-15 10:31:21 -0700442 }
443
444 if (reader->status != PACKET_READ_FLUSH)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200445 die(_("expected flush after ref listing"));
Brandon Williamse52449b2018-03-15 10:31:21 -0700446
447 return list;
448}
449
Junio C Hamano5d54cff2013-09-17 16:29:28 -0700450static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
Junio C Hamanof47182c2012-01-08 22:06:19 +0100451{
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 King94427102012-08-13 21:59:27 -0400462 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 Hamanof47182c2012-01-08 22:06:19 +0100482 feature_list = found + 1;
483 }
484 return NULL;
Johannes Schindelin211b5f92005-10-28 04:48:54 +0200485}
486
Jeff King94427102012-08-13 21:59:27 -0400487int parse_feature_request(const char *feature_list, const char *feature)
488{
489 return !!parse_feature_value(feature_list, feature, NULL);
490}
491
492const char *server_feature_value(const char *feature, int *len)
493{
Brandon Williamse52449b2018-03-15 10:31:21 -0700494 return parse_feature_value(server_capabilities_v1, feature, len);
Jeff King94427102012-08-13 21:59:27 -0400495}
496
497int server_supports(const char *feature)
498{
499 return !!server_feature_value(feature, NULL);
500}
501
Linus Torvalds2386d652005-07-13 18:46:20 -0700502enum protocol {
503 PROTO_LOCAL = 1,
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100504 PROTO_FILE,
Linus Torvalds2386d652005-07-13 18:46:20 -0700505 PROTO_SSH,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000506 PROTO_GIT
Linus Torvalds2386d652005-07-13 18:46:20 -0700507};
508
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100509int 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ögershausen5610b7c2013-11-28 20:49:17 +0100517static const char *prot_name(enum protocol protocol)
518{
519 switch (protocol) {
520 case PROTO_LOCAL:
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100521 case PROTO_FILE:
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +0100522 return "file";
523 case PROTO_SSH:
524 return "ssh";
525 case PROTO_GIT:
526 return "git";
527 default:
Tobias Klauser83e6bda2015-09-24 14:44:49 +0200528 return "unknown protocol";
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +0100529 }
530}
531
Linus Torvalds2386d652005-07-13 18:46:20 -0700532static 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 Nieto07c77822016-02-15 15:29:06 +0100538 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
Linus Torvaldsc05186c2005-10-14 17:14:56 -0700539 return PROTO_SSH;
Carlos Martín Nieto07c77822016-02-15 15:29:06 +0100540 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
Linus Torvaldsc05186c2005-10-14 17:14:56 -0700541 return PROTO_SSH;
Linus Torvalds72a4f4b2007-08-01 10:03:37 -0700542 if (!strcmp(name, "file"))
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100543 return PROTO_FILE;
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200544 die(_("protocol '%s' is not supported"), name);
Linus Torvalds2386d652005-07-13 18:46:20 -0700545}
546
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100547static 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 Hideaki5ba88442005-07-21 09:10:36 -0400571#define STR_(s) # s
572#define STR(s) STR_(s)
Linus Torvalds2386d652005-07-13 18:46:20 -0700573
Michael Lukashov72a534d2010-02-17 20:56:02 +0000574static void get_host_and_port(char **host, const char **port)
575{
576 char *colon, *end;
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100577 end = host_end(host, 1);
Michael Lukashov72a534d2010-02-17 20:56:02 +0000578 colon = strchr(end, ':');
Michael Lukashov72a534d2010-02-17 20:56:02 +0000579 if (colon) {
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100580 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ögershausen6b6c5f72015-04-07 22:03:25 +0200584 } else if (!colon[1]) {
585 *colon = 0;
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100586 }
Michael Lukashov72a534d2010-02-17 20:56:02 +0000587 }
588}
589
Eric Wonge47a8582011-12-06 04:39:36 +0000590static 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 Duyaad6fdd2018-07-21 09:49:28 +0200595 error_errno(_("unable to set SO_KEEPALIVE on socket"));
Eric Wonge47a8582011-12-06 04:39:36 +0000596}
597
hpa49744d62005-09-28 16:52:21 -0700598#ifndef NO_IPV6
hpa4c505f72005-09-28 16:37:58 -0700599
Alex Riesenba505322007-05-23 23:34:27 +0200600static const char *ai_name(const struct addrinfo *ai)
601{
Benjamin Kramer785a9852009-04-24 14:16:41 +0200602 static char addr[NI_MAXHOST];
603 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
604 NI_NUMERICHOST) != 0)
Jeff King5096d492015-09-24 17:06:08 -0400605 xsnprintf(addr, sizeof(addr), "(unknown)");
Benjamin Kramer785a9852009-04-24 14:16:41 +0200606
Alex Riesenba505322007-05-23 23:34:27 +0200607 return addr;
608}
609
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500610/*
611 * Returns a connected socket() fd, or else die()s.
612 */
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300613static int git_tcp_connect_sock(char *host, int flags)
Linus Torvalds2386d652005-07-13 18:46:20 -0700614{
Dave Zarzycki63a995b2011-07-12 09:28:34 -0700615 struct strbuf error_message = STRBUF_INIT;
616 int sockfd = -1;
Timo Hirvonen554fe202006-06-28 12:04:39 +0300617 const char *port = STR(DEFAULT_GIT_PORT);
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400618 struct addrinfo hints, *ai0, *ai;
619 int gai;
Alex Riesenba505322007-05-23 23:34:27 +0200620 int cnt = 0;
Linus Torvalds2386d652005-07-13 18:46:20 -0700621
Michael Lukashov72a534d2010-02-17 20:56:02 +0000622 get_host_and_port(&host, &port);
623 if (!*port)
624 port = "<none>";
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400625
626 memset(&hints, 0, sizeof(hints));
Eric Wongc915f112016-02-03 04:09:14 +0000627 if (flags & CONNECT_IPV4)
628 hints.ai_family = AF_INET;
629 else if (flags & CONNECT_IPV6)
630 hints.ai_family = AF_INET6;
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400631 hints.ai_socktype = SOCK_STREAM;
632 hints.ai_protocol = IPPROTO_TCP;
633
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300634 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200635 fprintf(stderr, _("Looking up %s ... "), host);
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300636
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400637 gai = getaddrinfo(host, port, &hints, &ai);
638 if (gai)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200639 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400640
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300641 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200642 /* TRANSLATORS: this is the end of "Looking up %s ... " */
643 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300644
Erik Faye-Lunde08afec2011-08-01 13:16:09 +0200645 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500646 sockfd = socket(ai->ai_family,
647 ai->ai_socktype, ai->ai_protocol);
Dave Zarzycki63a995b2011-07-12 09:28:34 -0700648 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 Hideaki5ba88442005-07-21 09:10:36 -0400654 sockfd = -1;
655 continue;
Linus Torvalds2386d652005-07-13 18:46:20 -0700656 }
Alex Riesenba505322007-05-23 23:34:27 +0200657 if (flags & CONNECT_VERBOSE)
658 fprintf(stderr, "%s ", ai_name(ai));
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400659 break;
Linus Torvalds2386d652005-07-13 18:46:20 -0700660 }
661
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400662 freeaddrinfo(ai0);
Linus Torvalds2386d652005-07-13 18:46:20 -0700663
Linus Torvalds2386d652005-07-13 18:46:20 -0700664 if (sockfd < 0)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200665 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400666
Eric Wonge47a8582011-12-06 04:39:36 +0000667 enable_keepalive(sockfd);
668
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300669 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200670 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
671 fprintf_ln(stderr, _("done."));
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300672
Dave Zarzycki63a995b2011-07-12 09:28:34 -0700673 strbuf_release(&error_message);
674
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500675 return sockfd;
Linus Torvalds2386d652005-07-13 18:46:20 -0700676}
677
hpa49744d62005-09-28 16:52:21 -0700678#else /* NO_IPV6 */
hpa4c505f72005-09-28 16:37:58 -0700679
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500680/*
681 * Returns a connected socket() fd, or else die()s.
682 */
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300683static int git_tcp_connect_sock(char *host, int flags)
hpa4c505f72005-09-28 16:37:58 -0700684{
Erik Faye-Lund7203a2d2011-08-01 13:16:10 +0200685 struct strbuf error_message = STRBUF_INIT;
686 int sockfd = -1;
Michael Lukashov72a534d2010-02-17 20:56:02 +0000687 const char *port = STR(DEFAULT_GIT_PORT);
688 char *ep;
hpa4c505f72005-09-28 16:37:58 -0700689 struct hostent *he;
690 struct sockaddr_in sa;
691 char **ap;
692 unsigned int nport;
Alex Riesenba505322007-05-23 23:34:27 +0200693 int cnt;
hpa4c505f72005-09-28 16:37:58 -0700694
Michael Lukashov72a534d2010-02-17 20:56:02 +0000695 get_host_and_port(&host, &port);
hpa4c505f72005-09-28 16:37:58 -0700696
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300697 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200698 fprintf(stderr, _("Looking up %s ... "), host);
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300699
hpa4c505f72005-09-28 16:37:58 -0700700 he = gethostbyname(host);
701 if (!he)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200702 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
hpa4c505f72005-09-28 16:37:58 -0700703 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 Duyaad6fdd2018-07-21 09:49:28 +0200708 die(_("unknown port %s"), port);
hpa4c505f72005-09-28 16:37:58 -0700709 nport = se->s_port;
710 }
711
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300712 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200713 /* TRANSLATORS: this is the end of "Looking up %s ... " */
714 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300715
Alex Riesenba505322007-05-23 23:34:27 +0200716 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
hpa4c505f72005-09-28 16:37:58 -0700717 memset(&sa, 0, sizeof sa);
718 sa.sin_family = he->h_addrtype;
Peter Anvin6573faf2005-09-28 17:26:44 -0700719 sa.sin_port = htons(nport);
Paul Sericec6164212005-11-22 07:54:23 -0600720 memcpy(&sa.sin_addr, *ap, he->h_length);
hpa4c505f72005-09-28 16:37:58 -0700721
Erik Faye-Lund7203a2d2011-08-01 13:16:10 +0200722 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 Riesenba505322007-05-23 23:34:27 +0200726 host,
727 cnt,
728 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
Erik Faye-Lund7203a2d2011-08-01 13:16:10 +0200729 strerror(errno));
730 if (0 <= sockfd)
731 close(sockfd);
hpa4c505f72005-09-28 16:37:58 -0700732 sockfd = -1;
733 continue;
734 }
Alex Riesenba505322007-05-23 23:34:27 +0200735 if (flags & CONNECT_VERBOSE)
736 fprintf(stderr, "%s ",
737 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
hpa4c505f72005-09-28 16:37:58 -0700738 break;
739 }
740
741 if (sockfd < 0)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200742 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
hpa4c505f72005-09-28 16:37:58 -0700743
Eric Wonge47a8582011-12-06 04:39:36 +0000744 enable_keepalive(sockfd);
745
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300746 if (flags & CONNECT_VERBOSE)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200747 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
748 fprintf_ln(stderr, _("done."));
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300749
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500750 return sockfd;
hpa4c505f72005-09-28 16:37:58 -0700751}
752
hpa49744d62005-09-28 16:52:21 -0700753#endif /* NO_IPV6 */
hpa4c505f72005-09-28 16:37:58 -0700754
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500755
Jonathan Nieder8e349782017-11-20 13:22:54 -0800756/*
757 * Dummy child_process returned by git_connect() if the transport protocol
758 * does not need fork(2).
759 */
760static struct child_process no_fork = CHILD_PROCESS_INIT;
761
762int git_connection_is_socket(struct child_process *conn)
763{
764 return conn == &no_fork;
765}
766
767static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500768{
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300769 int sockfd = git_tcp_connect_sock(host, flags);
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500770
771 fd[0] = sockfd;
Junio C Hamanoec587fd2007-01-21 17:10:51 -0800772 fd[1] = dup(sockfd);
Jonathan Nieder8e349782017-11-20 13:22:54 -0800773
774 return &no_fork;
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500775}
776
777
David Rientjes96f1e582006-08-15 10:23:48 -0700778static char *git_proxy_command;
Paul Collinsf8014772005-11-04 14:57:16 +0000779
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100780static int git_proxy_command_options(const char *var, const char *value,
781 void *cb)
Paul Collinsf8014772005-11-04 14:57:16 +0000782{
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800783 if (!strcmp(var, "core.gitproxy")) {
YOSHIFUJI Hideaki / 吉藤英明c3df8562005-11-22 12:18:23 +0900784 const char *for_pos;
785 int matchlen = -1;
786 int hostlen;
Erik Faye-Lund15112c92009-03-11 02:38:12 +0000787 const char *rhost_name = cb;
788 int rhost_len = strlen(rhost_name);
YOSHIFUJI Hideaki / 吉藤英明c3df8562005-11-22 12:18:23 +0900789
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800790 if (git_proxy_command)
Paul Collinsf8014772005-11-04 14:57:16 +0000791 return 0;
Junio C Hamanoc64b9ad2008-02-11 10:52:15 -0800792 if (!value)
793 return config_error_nonbool(var);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800794 /* [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 / 吉藤英明c3df8562005-11-22 12:18:23 +0900800 for_pos = strstr(value, " for ");
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800801 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 Collinsf8014772005-11-04 14:57:16 +0000816 }
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800817 if (0 <= matchlen) {
818 /* core.gitproxy = none for kernel.org */
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700819 if (matchlen == 4 &&
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800820 !memcmp(value, "none", 4))
821 matchlen = 0;
Pierre Habouzit182af832007-09-16 00:32:36 +0200822 git_proxy_command = xmemdupz(value, matchlen);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800823 }
824 return 0;
Paul Collinsf8014772005-11-04 14:57:16 +0000825 }
826
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100827 return git_default_config(var, value, cb);
Paul Collinsf8014772005-11-04 14:57:16 +0000828}
829
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800830static int git_use_proxy(const char *host)
Paul Collinsf8014772005-11-04 14:57:16 +0000831{
832 git_proxy_command = getenv("GIT_PROXY_COMMAND");
Erik Faye-Lund15112c92009-03-11 02:38:12 +0000833 git_config(git_proxy_command_options, (void*)host);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800834 return (git_proxy_command && *git_proxy_command);
Paul Collinsf8014772005-11-04 14:57:16 +0000835}
836
Jeff King5cbf8242011-05-16 02:46:07 -0400837static struct child_process *git_proxy_connect(int fd[2], char *host)
Paul Collinsf8014772005-11-04 14:57:16 +0000838{
Timo Hirvonen554fe202006-06-28 12:04:39 +0300839 const char *port = STR(DEFAULT_GIT_PORT);
Jeff King5cbf8242011-05-16 02:46:07 -0400840 struct child_process *proxy;
Paul Collinsf8014772005-11-04 14:57:16 +0000841
Michael Lukashov72a534d2010-02-17 20:56:02 +0000842 get_host_and_port(&host, &port);
Paul Collinsf8014772005-11-04 14:57:16 +0000843
Jeff King3be4cf02017-07-28 15:26:50 -0400844 if (looks_like_command_line_option(host))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200845 die(_("strange hostname '%s' blocked"), host);
Jeff King3be4cf02017-07-28 15:26:50 -0400846 if (looks_like_command_line_option(port))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200847 die(_("strange port '%s' blocked"), port);
Jeff King3be4cf02017-07-28 15:26:50 -0400848
René Scharfe483bbd42014-08-19 21:10:48 +0200849 proxy = xmalloc(sizeof(*proxy));
850 child_process_init(proxy);
Jeff King1823bea2014-05-15 04:34:09 -0400851 argv_array_push(&proxy->args, git_proxy_command);
852 argv_array_push(&proxy->args, host);
853 argv_array_push(&proxy->args, port);
Jeff King5cbf8242011-05-16 02:46:07 -0400854 proxy->in = -1;
855 proxy->out = -1;
856 if (start_command(proxy))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200857 die(_("cannot start proxy %s"), git_proxy_command);
Jeff King5cbf8242011-05-16 02:46:07 -0400858 fd[0] = proxy->out; /* read from proxy stdout */
859 fd[1] = proxy->in; /* write to proxy stdin */
860 return proxy;
Paul Collinsf8014772005-11-04 14:57:16 +0000861}
862
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100863static char *get_port(char *host)
Luben Tuikov2e776662007-09-01 02:36:31 -0700864{
865 char *end;
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100866 char *p = strchr(host, ':');
867
Luben Tuikov2e776662007-09-01 02:36:31 -0700868 if (p) {
René Scharfe8f148252008-12-21 02:12:11 +0100869 long port = strtol(p + 1, &end, 10);
870 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100871 *p = '\0';
872 return p+1;
Luben Tuikov2e776662007-09-01 02:36:31 -0700873 }
874 }
875
876 return NULL;
877}
878
Linus Torvaldsf7192592005-07-04 11:57:58 -0700879/*
Johannes Sixtcabc3c12013-11-28 20:49:01 +0100880 * Extract protocol and relevant parts from the specified connection URL.
881 * The caller must free() the returned strings.
Linus Torvaldsf7192592005-07-04 11:57:58 -0700882 */
Johannes Sixtcabc3c12013-11-28 20:49:01 +0100883static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
Torsten Bögershausen83b05872013-11-28 20:49:54 +0100884 char **ret_path)
Linus Torvaldsf7192592005-07-04 11:57:58 -0700885{
Jeff King9d2e9422010-05-23 05:19:44 -0400886 char *url;
Benjamin Kramer8e76bf32009-03-13 13:51:33 +0100887 char *host, *path;
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900888 char *end;
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100889 int separator = '/';
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100890 enum protocol protocol = PROTO_LOCAL;
Junio C Hamanof0b73672006-06-19 18:25:21 -0700891
Jeff King9d2e9422010-05-23 05:19:44 -0400892 if (is_url(url_orig))
893 url = url_decode(url_orig);
894 else
895 url = xstrdup(url_orig);
896
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100897 host = strstr(url, "://");
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400898 if (host) {
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100899 *host = '\0';
900 protocol = get_protocol(url);
901 host += 3;
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900902 } else {
Linus Torvaldsf7192592005-07-04 11:57:58 -0700903 host = url;
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100904 if (!url_is_local_not_ssh(url)) {
905 protocol = PROTO_SSH;
906 separator = ':';
907 }
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900908 }
909
Ilari Liusvaara9aa50532010-01-26 20:24:42 +0200910 /*
Torsten Bögershausen83b05872013-11-28 20:49:54 +0100911 * Don't do destructive transforms as protocol code does
912 * '[]' unwrapping in get_host_and_port()
Ilari Liusvaara9aa50532010-01-26 20:24:42 +0200913 */
Torsten Bögershausen86ceb332015-02-21 16:52:48 +0100914 end = host_end(&host, 0);
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900915
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100916 if (protocol == PROTO_LOCAL)
Linus Torvalds72a4f4b2007-08-01 10:03:37 -0700917 path = end;
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100918 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 Torvalds2386d652005-07-13 18:46:20 -0700922
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100923 if (!path || !*path)
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +0200924 die(_("no path specified; see 'git help pull' for valid url syntax"));
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100925
926 /*
927 * null-terminate hostname and point path to ~ for URL's like this:
928 * ssh://host.xz/~user/repo
929 */
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100930
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 Ericssonfaea9cc2005-11-17 20:37:14 +0100935 if (path[1] == '~')
936 path++;
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100937 }
938
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100939 path = xstrdup(path);
940 *end = '\0';
941
Johannes Sixtcabc3c12013-11-28 20:49:01 +0100942 *ret_host = xstrdup(host);
Torsten Bögershausenc59ab2e2013-11-28 20:50:03 +0100943 *ret_path = path;
Johannes Sixtcabc3c12013-11-28 20:49:01 +0100944 free(url);
945 return protocol;
946}
947
Nguyễn Thái Ngọc Duy3c8ede32016-06-26 13:16:35 +0200948static 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 Williams94b8ae52017-10-16 10:55:31 -0700961enum ssh_variant {
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800962 VARIANT_AUTO,
Brandon Williams94b8ae52017-10-16 10:55:31 -0700963 VARIANT_SIMPLE,
964 VARIANT_SSH,
965 VARIANT_PLINK,
966 VARIANT_PUTTY,
967 VARIANT_TORTOISEPLINK,
968};
Junio C Hamano486c8e82017-02-09 09:20:25 -0800969
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800970static void override_ssh_variant(enum ssh_variant *ssh_variant)
Brandon Williams94b8ae52017-10-16 10:55:31 -0700971{
972 const char *variant = getenv("GIT_SSH_VARIANT");
973
974 if (!variant && git_config_get_string_const("ssh.variant", &variant))
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800975 return;
Junio C Hamano486c8e82017-02-09 09:20:25 -0800976
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800977 if (!strcmp(variant, "auto"))
978 *ssh_variant = VARIANT_AUTO;
979 else if (!strcmp(variant, "plink"))
Brandon Williams94b8ae52017-10-16 10:55:31 -0700980 *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 Hamano486c8e82017-02-09 09:20:25 -0800989}
990
Brandon Williams94b8ae52017-10-16 10:55:31 -0700991static enum ssh_variant determine_ssh_variant(const char *ssh_command,
992 int is_cmdline)
Junio C Hamano486c8e82017-02-09 09:20:25 -0800993{
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800994 enum ssh_variant ssh_variant = VARIANT_AUTO;
Junio C Hamano486c8e82017-02-09 09:20:25 -0800995 const char *variant;
Johannes Schindeline2824e42017-02-01 13:01:10 +0100996 char *p = NULL;
997
Jonathan Nieder0da0e492017-11-20 13:30:04 -0800998 override_ssh_variant(&ssh_variant);
999
1000 if (ssh_variant != VARIANT_AUTO)
Brandon Williams94b8ae52017-10-16 10:55:31 -07001001 return ssh_variant;
Junio C Hamano486c8e82017-02-09 09:20:25 -08001002
1003 if (!is_cmdline) {
Johannes Schindeline2824e42017-02-01 13:01:10 +01001004 p = xstrdup(ssh_command);
1005 variant = basename(p);
1006 } else {
1007 const char **ssh_argv;
1008
1009 p = xstrdup(ssh_command);
Jeff King22e5ae52017-04-10 20:30:23 -04001010 if (split_cmdline(p, &ssh_argv) > 0) {
Johannes Schindeline2824e42017-02-01 13:01:10 +01001011 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 King5d2993b2017-04-20 16:21:58 -04001018 } else {
1019 free(p);
Brandon Williams94b8ae52017-10-16 10:55:31 -07001020 return ssh_variant;
Jeff King5d2993b2017-04-20 16:21:58 -04001021 }
Johannes Schindeline2824e42017-02-01 13:01:10 +01001022 }
1023
Brandon Williams94b8ae52017-10-16 10:55:31 -07001024 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 Schindeline2824e42017-02-01 13:01:10 +01001030 else if (!strcasecmp(variant, "tortoiseplink") ||
Brandon Williams94b8ae52017-10-16 10:55:31 -07001031 !strcasecmp(variant, "tortoiseplink.exe"))
1032 ssh_variant = VARIANT_TORTOISEPLINK;
1033
Johannes Schindeline2824e42017-02-01 13:01:10 +01001034 free(p);
Brandon Williams94b8ae52017-10-16 10:55:31 -07001035 return ssh_variant;
Johannes Schindeline2824e42017-02-01 13:01:10 +01001036}
1037
Linus Torvaldsf7192592005-07-04 11:57:58 -07001038/*
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001039 * 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 */
1044static struct child_process *git_connect_git(int fd[2], char *hostandport,
1045 const char *path, const char *prog,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001046 enum protocol_version version,
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001047 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 Nieder233cd282017-11-20 14:04:58 -08001064 /*
1065 * These underlying connection commands die() if they
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001066 * 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 Williams40fc51e2018-03-15 10:31:30 -07001085 if (version > 0) {
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001086 strbuf_addch(&request, '\0');
1087 strbuf_addf(&request, "version=%d%c",
Brandon Williams40fc51e2018-03-15 10:31:30 -07001088 version, '\0');
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001089 }
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 Nieder957e2ad2017-11-20 13:26:19 -08001098/*
1099 * Append the appropriate environment variables to `env` and options to
1100 * `args` for running ssh in Git's SSH-tunneled transport.
1101 */
1102static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1103 enum ssh_variant variant, const char *port,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001104 enum protocol_version version, int flags)
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001105{
1106 if (variant == VARIANT_SSH &&
Brandon Williams40fc51e2018-03-15 10:31:30 -07001107 version > 0) {
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001108 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 Williams40fc51e2018-03-15 10:31:30 -07001111 version);
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001112 }
1113
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001114 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 Duyaad6fdd2018-07-21 09:49:28 +02001119 die(_("ssh variant 'simple' does not support -4"));
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001120 case VARIANT_SSH:
1121 case VARIANT_PLINK:
1122 case VARIANT_PUTTY:
1123 case VARIANT_TORTOISEPLINK:
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001124 argv_array_push(args, "-4");
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001125 }
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 Duyaad6fdd2018-07-21 09:49:28 +02001131 die(_("ssh variant 'simple' does not support -6"));
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001132 case VARIANT_SSH:
1133 case VARIANT_PLINK:
1134 case VARIANT_PUTTY:
1135 case VARIANT_TORTOISEPLINK:
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001136 argv_array_push(args, "-6");
Jonathan Niedera3f5b662017-11-20 13:30:30 -08001137 }
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001138 }
1139
1140 if (variant == VARIANT_TORTOISEPLINK)
1141 argv_array_push(args, "-batch");
1142
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001143 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 Duyaad6fdd2018-07-21 09:49:28 +02001148 die(_("ssh variant 'simple' does not support setting port"));
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001149 case VARIANT_SSH:
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001150 argv_array_push(args, "-p");
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001151 break;
1152 case VARIANT_PLINK:
1153 case VARIANT_PUTTY:
1154 case VARIANT_TORTOISEPLINK:
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001155 argv_array_push(args, "-P");
Jonathan Nieder3fa5e0d2017-11-20 13:31:01 -08001156 }
Jonathan Nieder957e2ad2017-11-20 13:26:19 -08001157
1158 argv_array_push(args, port);
1159 }
1160}
1161
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001162/* Prepare a child_process for use by Git's SSH-tunneled transport. */
1163static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
Brandon Williams40fc51e2018-03-15 10:31:30 -07001164 const char *port, enum protocol_version version,
1165 int flags)
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001166{
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 Duyaad6fdd2018-07-21 09:49:28 +02001171 die(_("strange hostname '%s' blocked"), ssh_host);
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001172
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 Nieder0da0e492017-11-20 13:30:04 -08001190 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 Williams40fc51e2018-03-15 10:31:30 -07001199 VARIANT_SSH, port, version, flags);
Jonathan Nieder0da0e492017-11-20 13:30:04 -08001200 argv_array_push(&detect.args, ssh_host);
1201
1202 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1203 }
1204
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001205 argv_array_push(&conn->args, ssh);
Brandon Williams40fc51e2018-03-15 10:31:30 -07001206 push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
Jonathan Niederfce54ce2017-11-20 14:19:43 -08001207 argv_array_push(&conn->args, ssh_host);
1208}
1209
Jonathan Nieder2ac67cb2017-11-20 13:23:27 -08001210/*
Jonathan Nieder8e349782017-11-20 13:22:54 -08001211 * 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 Torvaldsf7192592005-07-04 11:57:58 -07001216 *
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 Sixtcabc3c12013-11-28 20:49:01 +01001221struct child_process *git_connect(int fd[2], const char *url,
Linus Torvaldsf7192592005-07-04 11:57:58 -07001222 const char *prog, int flags)
1223{
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001224 char *hostandport, *path;
Jonathan Nieder8e349782017-11-20 13:22:54 -08001225 struct child_process *conn;
Johannes Sixtcabc3c12013-11-28 20:49:01 +01001226 enum protocol protocol;
Brandon Williams40fc51e2018-03-15 10:31:30 -07001227 enum protocol_version version = get_protocol_version_config();
Linus Torvaldsf7192592005-07-04 11:57:58 -07001228
Brandon Williams1aa8dded2018-03-15 10:31:31 -07001229 /*
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 Torvaldsf7192592005-07-04 11:57:58 -07001237 /* Without this we cannot rely on waitpid() to tell
1238 * what happened to our children.
1239 */
1240 signal(SIGCHLD, SIG_DFL);
1241
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001242 protocol = parse_connect_url(url, &hostandport, &path);
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001243 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +01001244 printf("Diag: url=%s\n", url ? url : "NULL");
1245 printf("Diag: protocol=%s\n", prot_name(protocol));
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001246 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +01001247 printf("Diag: path=%s\n", path ? path : "NULL");
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001248 conn = NULL;
1249 } else if (protocol == PROTO_GIT) {
Brandon Williams40fc51e2018-03-15 10:31:30 -07001250 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
Jeff Hostetlerabd81a32019-02-22 14:25:05 -08001251 conn->trace2_child_class = "transport/git";
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001252 } else {
Rene Scharfef1399292017-08-30 19:49:39 +02001253 struct strbuf cmd = STRBUF_INIT;
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001254 const char *const *var;
Rene Scharfef1399292017-08-30 19:49:39 +02001255
René Scharfe483bbd42014-08-19 21:10:48 +02001256 conn = xmalloc(sizeof(*conn));
1257 child_process_init(conn);
Linus Torvaldsf7192592005-07-04 11:57:58 -07001258
Jeff Kingaeeb2d42017-07-28 15:28:55 -04001259 if (looks_like_command_line_option(path))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001260 die(_("strange pathname '%s' blocked"), path);
Jeff Kingaeeb2d42017-07-28 15:28:55 -04001261
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001262 strbuf_addstr(&cmd, prog);
1263 strbuf_addch(&cmd, ' ');
1264 sq_quote_buf(&cmd, path);
Christian Couder0f503d72006-09-11 07:04:50 +02001265
Jeff Kingaab40432015-09-04 18:40:08 -04001266 /* remove repo-local variables from the environment */
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001267 for (var = local_repo_env; *var; var++)
1268 argv_array_push(&conn->env_array, *var);
1269
Jeff Kinga48b4092015-09-08 04:33:14 -04001270 conn->use_shell = 1;
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001271 conn->in = conn->out = -1;
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001272 if (protocol == PROTO_SSH) {
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001273 char *ssh_host = hostandport;
1274 const char *port = NULL;
Jeff Kinga5adace2015-09-16 13:12:52 -04001275 transport_check_allowed("ssh");
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001276 get_host_and_port(&ssh_host, &port);
Christian Couder0f503d72006-09-11 07:04:50 +02001277
Torsten Bögershausen86ceb332015-02-21 16:52:48 +01001278 if (!port)
1279 port = get_port(ssh_host);
Junio C Hamano42da4842015-03-05 12:45:44 -08001280
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001281 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 Torvaldsf7192592005-07-04 11:57:58 -07001287
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001288 free(hostandport);
1289 free(path);
Stefan Beller04f20c02015-03-09 09:58:22 -07001290 free(conn);
Rene Scharfef1399292017-08-30 19:49:39 +02001291 strbuf_release(&cmd);
Torsten Bögershausen3f55cca2015-02-21 16:52:55 +01001292 return NULL;
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001293 }
Jeff Hostetlerabd81a32019-02-22 14:25:05 -08001294 conn->trace2_child_class = "transport/ssh";
Brandon Williams40fc51e2018-03-15 10:31:30 -07001295 fill_ssh_args(conn, ssh_host, port, version, flags);
Nguyễn Thái Ngọc Duyc049b612014-03-13 18:45:31 +07001296 } else {
Jeff Kinga5adace2015-09-16 13:12:52 -04001297 transport_check_allowed("file");
Jeff Hostetlerabd81a32019-02-22 14:25:05 -08001298 conn->trace2_child_class = "transport/file";
Brandon Williams40fc51e2018-03-15 10:31:30 -07001299 if (version > 0) {
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001300 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
Brandon Williams40fc51e2018-03-15 10:31:30 -07001301 version);
Brandon Williams0c2f0d22017-10-16 10:55:28 -07001302 }
Linus Torvaldsf7192592005-07-04 11:57:58 -07001303 }
Jeff King1823bea2014-05-15 04:34:09 -04001304 argv_array_push(&conn->args, cmd.buf);
Johannes Sixtf364cb82007-10-19 21:47:54 +02001305
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001306 if (start_command(conn))
Nguyễn Thái Ngọc Duyaad6fdd2018-07-21 09:49:28 +02001307 die(_("unable to fork"));
Johannes Sixtf364cb82007-10-19 21:47:54 +02001308
Torsten Bögershausena2036d72013-11-28 20:50:15 +01001309 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 Sixtcabc3c12013-11-28 20:49:01 +01001314 free(path);
Johannes Sixt98158e92007-10-19 21:47:53 +02001315 return conn;
Linus Torvaldsf7192592005-07-04 11:57:58 -07001316}
1317
Johannes Sixt98158e92007-10-19 21:47:53 +02001318int finish_connect(struct child_process *conn)
Linus Torvaldsf7192592005-07-04 11:57:58 -07001319{
Johannes Sixtf364cb82007-10-19 21:47:54 +02001320 int code;
Jeff King7ffe8532011-05-16 02:52:11 -04001321 if (!conn || git_connection_is_socket(conn))
Franck Bui-Huuf42a5c42006-09-12 11:00:13 +02001322 return 0;
1323
Johannes Sixtf364cb82007-10-19 21:47:54 +02001324 code = finish_command(conn);
Johannes Sixt98158e92007-10-19 21:47:53 +02001325 free(conn);
Johannes Sixtf364cb82007-10-19 21:47:54 +02001326 return code;
Linus Torvaldsf7192592005-07-04 11:57:58 -07001327}