Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "pkt-line.h"
9 #include "quote.h"
10 #include "refs.h"
11 #include "run-command.h"
12 #include "remote.h"
13 #include "connect.h"
14 #include "url.h"
15 #include "string-list.h"
16 #include "oid-array.h"
17 #include "path.h"
18 #include "transport.h"
19 #include "trace2.h"
20 #include "strbuf.h"
21 #include "version.h"
22 #include "protocol.h"
23 #include "alias.h"
24 #include "bundle-uri.h"
25 #include "promisor-remote.h"
26
27 static char *server_capabilities_v1;
28 static struct strvec server_capabilities_v2 = STRVEC_INIT;
29 static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset);
30
31 static int check_ref(const char *name, unsigned int flags)
32 {
33 if (!flags)
34 return 1;
35
36 if (!skip_prefix(name, "refs/", &name))
37 return 0;
38
39 /* REF_NORMAL means that we don't want the magic fake tag refs */
40 if ((flags & REF_NORMAL) && check_refname_format(name,
41 REFNAME_ALLOW_ONELEVEL))
42 return 0;
43
44 /* REF_BRANCHES means that we want regular branch heads */
45 if ((flags & REF_BRANCHES) && starts_with(name, "heads/"))
46 return 1;
47
48 /* REF_TAGS means that we want tags */
49 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
50 return 1;
51
52 /* All type bits clear means that we are ok with anything */
53 return !(flags & ~REF_NORMAL);
54 }
55
56 int check_ref_type(const struct ref *ref, int flags)
57 {
58 return check_ref(ref->name, flags);
59 }
60
61 static NORETURN void die_initial_contact(int unexpected)
62 {
63 /*
64 * A hang-up after seeing some response from the other end
65 * means that it is unexpected, as we know the other end is
66 * willing to talk to us. A hang-up before seeing any
67 * response does not necessarily mean an ACL problem, though.
68 */
69 if (unexpected)
70 die(_("the remote end hung up upon initial contact"));
71 else
72 die(_("Could not read from remote repository.\n\n"
73 "Please make sure you have the correct access rights\n"
74 "and the repository exists."));
75 }
76
77 /* Checks if the server supports the capability 'c' */
78 int server_supports_v2(const char *c)
79 {
80 size_t i;
81
82 for (i = 0; i < server_capabilities_v2.nr; i++) {
83 const char *out;
84 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
85 (!*out || *out == '='))
86 return 1;
87 }
88 return 0;
89 }
90
91 void ensure_server_supports_v2(const char *c)
92 {
93 if (!server_supports_v2(c))
94 die(_("server doesn't support '%s'"), c);
95 }
96
97 int server_feature_v2(const char *c, const char **v)
98 {
99 size_t i;
100
101 for (i = 0; i < server_capabilities_v2.nr; i++) {
102 const char *out;
103 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
104 (*out == '=')) {
105 *v = out + 1;
106 return 1;
107 }
108 }
109 return 0;
110 }
111
112 int server_supports_feature(const char *c, const char *feature,
113 int die_on_error)
114 {
115 size_t i;
116
117 for (i = 0; i < server_capabilities_v2.nr; i++) {
118 const char *out;
119 if (skip_prefix(server_capabilities_v2.v[i], c, &out) &&
120 (!*out || *(out++) == '=')) {
121 if (parse_feature_request(out, feature))
122 return 1;
123 else
124 break;
125 }
126 }
127
128 if (die_on_error)
129 die(_("server doesn't support feature '%s'"), feature);
130
131 return 0;
132 }
133
134 static void process_capabilities_v2(struct packet_reader *reader)
135 {
136 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
137 strvec_push(&server_capabilities_v2, reader->line);
138
139 if (reader->status != PACKET_READ_FLUSH)
140 die(_("expected flush after capabilities"));
141 }
142
143 enum protocol_version discover_version(struct packet_reader *reader)
144 {
145 enum protocol_version version = protocol_unknown_version;
146
147 /*
148 * Peek the first line of the server's response to
149 * determine the protocol version the server is speaking.
150 */
151 switch (packet_reader_peek(reader)) {
152 case PACKET_READ_EOF:
153 die_initial_contact(0);
154 case PACKET_READ_FLUSH:
155 case PACKET_READ_DELIM:
156 case PACKET_READ_RESPONSE_END:
157 version = protocol_v0;
158 break;
159 case PACKET_READ_NORMAL:
160 version = determine_protocol_version_client(reader->line);
161 break;
162 }
163
164 switch (version) {
165 case protocol_v2:
166 process_capabilities_v2(reader);
167 break;
168 case protocol_v1:
169 /* Read the peeked version line */
170 packet_reader_read(reader);
171 break;
172 case protocol_v0:
173 break;
174 case protocol_unknown_version:
175 BUG("unknown protocol version");
176 }
177
178 trace2_data_intmax("transfer", NULL, "negotiated-version", version);
179
180 return version;
181 }
182
183 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
184 {
185 char *sym, *target;
186 struct string_list_item *item;
187
188 if (!len)
189 return; /* just "symref" */
190 /* e.g. "symref=HEAD:refs/heads/master" */
191 sym = xmemdupz(val, len);
192 target = strchr(sym, ':');
193 if (!target)
194 /* just "symref=something" */
195 goto reject;
196 *(target++) = '\0';
197 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
198 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
199 /* "symref=bogus:pair */
200 goto reject;
201 item = string_list_append_nodup(symref, sym);
202 item->util = target;
203 return;
204 reject:
205 free(sym);
206 return;
207 }
208
209 static void annotate_refs_with_symref_info(struct ref *ref)
210 {
211 struct string_list symref = STRING_LIST_INIT_DUP;
212 size_t offset = 0;
213
214 while (1) {
215 size_t len;
216 const char *val;
217
218 val = next_server_feature_value("symref", &len, &offset);
219 if (!val)
220 break;
221 parse_one_symref_info(&symref, val, len);
222 }
223 string_list_sort(&symref);
224
225 for (; ref; ref = ref->next) {
226 struct string_list_item *item;
227 item = string_list_lookup(&symref, ref->name);
228 if (!item)
229 continue;
230 ref->symref = xstrdup((char *)item->util);
231 }
232 string_list_clear(&symref, 0);
233 }
234
235 static void process_capabilities(struct packet_reader *reader, size_t *linelen)
236 {
237 const char *feat_val;
238 size_t feat_len;
239 const char *line = reader->line;
240 size_t nul_location = strlen(line);
241 if (nul_location == *linelen)
242 return;
243
244 free(server_capabilities_v1);
245 server_capabilities_v1 = xstrdup(line + nul_location + 1);
246 *linelen = nul_location;
247
248 feat_val = server_feature_value("object-format", &feat_len);
249 if (feat_val) {
250 char *hash_name = xstrndup(feat_val, feat_len);
251 int hash_algo = hash_algo_by_name(hash_name);
252 if (hash_algo != GIT_HASH_UNKNOWN)
253 reader->hash_algo = &hash_algos[hash_algo];
254 free(hash_name);
255 } else {
256 reader->hash_algo = &hash_algos[GIT_HASH_SHA1_LEGACY];
257 }
258 }
259
260 static int process_dummy_ref(const struct packet_reader *reader)
261 {
262 const char *line = reader->line;
263 struct object_id oid;
264 const char *name;
265
266 if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo))
267 return 0;
268 if (*name != ' ')
269 return 0;
270 name++;
271
272 return oideq(reader->hash_algo->null_oid, &oid) &&
273 !strcmp(name, "capabilities^{}");
274 }
275
276 static void check_no_capabilities(const char *line, size_t len)
277 {
278 if (strlen(line) != len)
279 warning(_("ignoring capabilities after first line '%s'"),
280 line + strlen(line));
281 }
282
283 static int process_ref(const struct packet_reader *reader, size_t len,
284 struct ref ***list, unsigned int flags,
285 struct oid_array *extra_have)
286 {
287 const char *line = reader->line;
288 struct object_id old_oid;
289 const char *name;
290
291 if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo))
292 return 0;
293 if (*name != ' ')
294 return 0;
295 name++;
296
297 if (extra_have && !strcmp(name, ".have")) {
298 oid_array_append(extra_have, &old_oid);
299 } else if (!strcmp(name, "capabilities^{}")) {
300 die(_("protocol error: unexpected capabilities^{}"));
301 } else if (check_ref(name, flags)) {
302 struct ref *ref = alloc_ref(name);
303 oidcpy(&ref->old_oid, &old_oid);
304 **list = ref;
305 *list = &ref->next;
306 }
307 check_no_capabilities(line, len);
308 return 1;
309 }
310
311 static int process_shallow(const struct packet_reader *reader, size_t len,
312 struct oid_array *shallow_points)
313 {
314 const char *line = reader->line;
315 const char *arg;
316 struct object_id old_oid;
317
318 if (!skip_prefix(line, "shallow ", &arg))
319 return 0;
320
321 if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo))
322 die(_("protocol error: expected shallow sha-1, got '%s'"), arg);
323 if (!shallow_points)
324 die(_("repository on the other end cannot be shallow"));
325 oid_array_append(shallow_points, &old_oid);
326 check_no_capabilities(line, len);
327 return 1;
328 }
329
330 enum get_remote_heads_state {
331 EXPECTING_FIRST_REF = 0,
332 EXPECTING_REF,
333 EXPECTING_SHALLOW,
334 EXPECTING_DONE,
335 };
336
337 /*
338 * Read all the refs from the other end
339 */
340 struct ref **get_remote_heads(struct packet_reader *reader,
341 struct ref **list, unsigned int flags,
342 struct oid_array *extra_have,
343 struct oid_array *shallow_points)
344 {
345 struct ref **orig_list = list;
346 size_t len = 0;
347 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
348
349 *list = NULL;
350
351 while (state != EXPECTING_DONE) {
352 switch (packet_reader_read(reader)) {
353 case PACKET_READ_EOF:
354 die_initial_contact(1);
355 case PACKET_READ_NORMAL:
356 len = reader->pktlen;
357 break;
358 case PACKET_READ_FLUSH:
359 state = EXPECTING_DONE;
360 break;
361 case PACKET_READ_DELIM:
362 case PACKET_READ_RESPONSE_END:
363 die(_("invalid packet"));
364 }
365
366 switch (state) {
367 case EXPECTING_FIRST_REF:
368 process_capabilities(reader, &len);
369 if (process_dummy_ref(reader)) {
370 state = EXPECTING_SHALLOW;
371 break;
372 }
373 state = EXPECTING_REF;
374 /* fallthrough */
375 case EXPECTING_REF:
376 if (process_ref(reader, len, &list, flags, extra_have))
377 break;
378 state = EXPECTING_SHALLOW;
379 /* fallthrough */
380 case EXPECTING_SHALLOW:
381 if (process_shallow(reader, len, shallow_points))
382 break;
383 die(_("protocol error: unexpected '%s'"), reader->line);
384 case EXPECTING_DONE:
385 break;
386 }
387 }
388
389 annotate_refs_with_symref_info(*orig_list);
390
391 return list;
392 }
393
394 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
395 static int process_ref_v2(struct packet_reader *reader, struct ref ***list,
396 const char **unborn_head_target)
397 {
398 int ret = 1;
399 size_t i = 0;
400 struct object_id old_oid;
401 struct ref *ref;
402 struct string_list line_sections = STRING_LIST_INIT_DUP;
403 const char *end;
404 const char *line = reader->line;
405
406 /*
407 * Ref lines have a number of fields which are space deliminated. The
408 * first field is the OID of the ref. The second field is the ref
409 * name. Subsequent fields (symref-target and peeled) are optional and
410 * don't have a particular order.
411 */
412 if (string_list_split(&line_sections, line, " ", -1) < 2) {
413 ret = 0;
414 goto out;
415 }
416
417 if (!strcmp("unborn", line_sections.items[i].string)) {
418 i++;
419 if (unborn_head_target &&
420 !strcmp("HEAD", line_sections.items[i++].string)) {
421 /*
422 * Look for the symref target (if any). If found,
423 * return it to the caller.
424 */
425 for (; i < line_sections.nr; i++) {
426 const char *arg = line_sections.items[i].string;
427
428 if (skip_prefix(arg, "symref-target:", &arg)) {
429 *unborn_head_target = xstrdup(arg);
430 break;
431 }
432 }
433 }
434 goto out;
435 }
436 if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) ||
437 *end) {
438 ret = 0;
439 goto out;
440 }
441
442 ref = alloc_ref(line_sections.items[i++].string);
443
444 memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz);
445 **list = ref;
446 *list = &ref->next;
447
448 for (; i < line_sections.nr; i++) {
449 const char *arg = line_sections.items[i].string;
450 if (skip_prefix(arg, "symref-target:", &arg))
451 ref->symref = xstrdup(arg);
452
453 if (skip_prefix(arg, "peeled:", &arg)) {
454 struct object_id peeled_oid;
455 char *peeled_name;
456 struct ref *peeled;
457 if (parse_oid_hex_algop(arg, &peeled_oid, &end,
458 reader->hash_algo) || *end) {
459 ret = 0;
460 goto out;
461 }
462
463 peeled_name = xstrfmt("%s^{}", ref->name);
464 peeled = alloc_ref(peeled_name);
465
466 memcpy(peeled->old_oid.hash, peeled_oid.hash,
467 reader->hash_algo->rawsz);
468 **list = peeled;
469 *list = &peeled->next;
470
471 free(peeled_name);
472 }
473 }
474
475 out:
476 string_list_clear(&line_sections, 0);
477 return ret;
478 }
479
480 void check_stateless_delimiter(int stateless_rpc,
481 struct packet_reader *reader,
482 const char *error)
483 {
484 if (!stateless_rpc)
485 return; /* not in stateless mode, no delimiter expected */
486 if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END)
487 die("%s", error);
488 }
489
490 static void send_capabilities(int fd_out, struct packet_reader *reader)
491 {
492 const char *hash_name;
493 const char *promisor_remote_info;
494
495 if (server_supports_v2("agent"))
496 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
497
498 if (server_feature_v2("object-format", &hash_name)) {
499 int hash_algo = hash_algo_by_name(hash_name);
500 if (hash_algo == GIT_HASH_UNKNOWN)
501 die(_("unknown object format '%s' specified by server"), hash_name);
502 reader->hash_algo = &hash_algos[hash_algo];
503 packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name);
504 } else {
505 reader->hash_algo = &hash_algos[GIT_HASH_SHA1_LEGACY];
506 }
507 if (server_feature_v2("promisor-remote", &promisor_remote_info)) {
508 char *reply;
509 promisor_remote_reply(promisor_remote_info, &reply);
510 if (reply) {
511 packet_write_fmt(fd_out, "promisor-remote=%s", reply);
512 free(reply);
513 }
514 }
515 }
516
517 int get_remote_bundle_uri(int fd_out, struct packet_reader *reader,
518 struct bundle_list *bundles, int stateless_rpc)
519 {
520 int line_nr = 1, err = 0;
521
522 /* Assert bundle-uri support */
523 ensure_server_supports_v2("bundle-uri");
524
525 /* (Re-)send capabilities */
526 send_capabilities(fd_out, reader);
527
528 /* Send command */
529 packet_write_fmt(fd_out, "command=bundle-uri\n");
530 packet_delim(fd_out);
531
532 packet_flush(fd_out);
533
534 /* Process response from server */
535 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
536 const char *line = reader->line;
537 line_nr++;
538
539 /*
540 * Do not parse if an error was encountered, but
541 * continue draining the response so no stale data
542 * is left in the reader for subsequent protocol
543 * exchanges.
544 */
545 if (err)
546 continue;
547
548 if (!bundle_uri_parse_line(bundles, line))
549 continue;
550
551 err = error(_("error on bundle-uri response line %d: %s"),
552 line_nr, line);
553 }
554
555 if (reader->status != PACKET_READ_FLUSH)
556 return error(_("expected flush after bundle-uri listing"));
557
558 /*
559 * Might die(), but obscure enough that that's OK, e.g. in
560 * serve.c we'll call BUG() on its equivalent (the
561 * PACKET_READ_RESPONSE_END check).
562 */
563 check_stateless_delimiter(stateless_rpc, reader,
564 _("expected response end packet after ref listing"));
565
566 return err;
567 }
568
569 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
570 struct ref **list, int for_push,
571 struct transport_ls_refs_options *transport_options,
572 const struct string_list *server_options,
573 int stateless_rpc)
574 {
575 size_t i;
576 struct strvec *ref_prefixes = transport_options ?
577 &transport_options->ref_prefixes : NULL;
578 const char **unborn_head_target = transport_options ?
579 &transport_options->unborn_head_target : NULL;
580 *list = NULL;
581
582 ensure_server_supports_v2("ls-refs");
583 packet_write_fmt(fd_out, "command=ls-refs\n");
584
585 /* Send capabilities */
586 send_capabilities(fd_out, reader);
587
588 if (server_options && server_options->nr) {
589 ensure_server_supports_v2("server-option");
590 for (i = 0; i < server_options->nr; i++)
591 packet_write_fmt(fd_out, "server-option=%s",
592 server_options->items[i].string);
593 }
594
595 packet_delim(fd_out);
596 /* When pushing we don't want to request the peeled tags */
597 if (!for_push)
598 packet_write_fmt(fd_out, "peel\n");
599 packet_write_fmt(fd_out, "symrefs\n");
600 if (server_supports_feature("ls-refs", "unborn", 0))
601 packet_write_fmt(fd_out, "unborn\n");
602 for (i = 0; ref_prefixes && i < ref_prefixes->nr; i++) {
603 packet_write_fmt(fd_out, "ref-prefix %s\n",
604 ref_prefixes->v[i]);
605 }
606 packet_flush(fd_out);
607
608 /* Process response from server */
609 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
610 if (!process_ref_v2(reader, &list, unborn_head_target))
611 die(_("invalid ls-refs response: %s"), reader->line);
612 }
613
614 if (reader->status != PACKET_READ_FLUSH)
615 die(_("expected flush after ref listing"));
616
617 check_stateless_delimiter(stateless_rpc, reader,
618 _("expected response end packet after ref listing"));
619
620 return list;
621 }
622
623 const char *parse_feature_value(const char *feature_list, const char *feature, size_t *lenp, size_t *offset)
624 {
625 const char *orig_start = feature_list;
626 size_t len;
627
628 if (!feature_list)
629 return NULL;
630
631 len = strlen(feature);
632 if (offset)
633 feature_list += *offset;
634 while (*feature_list) {
635 const char *found = strstr(feature_list, feature);
636 if (!found)
637 return NULL;
638 if (feature_list == found || isspace(found[-1])) {
639 const char *value = found + len;
640 /* feature with no value (e.g., "thin-pack") */
641 if (!*value || isspace(*value)) {
642 if (lenp)
643 *lenp = 0;
644 if (offset)
645 *offset = found + len - orig_start;
646 return value;
647 }
648 /* feature with a value (e.g., "agent=git/1.2.3-Linux") */
649 else if (*value == '=') {
650 size_t end;
651
652 value++;
653 end = strcspn(value, " \t\n");
654 if (lenp)
655 *lenp = end;
656 if (offset)
657 *offset = value + end - orig_start;
658 return value;
659 }
660 /*
661 * otherwise we matched a substring of another feature;
662 * keep looking
663 */
664 }
665 feature_list = found + 1;
666 }
667 return NULL;
668 }
669
670 int server_supports_hash(const char *desired, int *feature_supported)
671 {
672 size_t offset = 0;
673 size_t len;
674 const char *hash;
675
676 hash = next_server_feature_value("object-format", &len, &offset);
677 if (feature_supported)
678 *feature_supported = !!hash;
679 if (!hash) {
680 hash = hash_algos[GIT_HASH_SHA1_LEGACY].name;
681 len = strlen(hash);
682 }
683 while (hash) {
684 if (!xstrncmpz(desired, hash, len))
685 return 1;
686
687 hash = next_server_feature_value("object-format", &len, &offset);
688 }
689 return 0;
690 }
691
692 int parse_feature_request(const char *feature_list, const char *feature)
693 {
694 return !!parse_feature_value(feature_list, feature, NULL, NULL);
695 }
696
697 static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset)
698 {
699 return parse_feature_value(server_capabilities_v1, feature, len, offset);
700 }
701
702 const char *server_feature_value(const char *feature, size_t *len)
703 {
704 return parse_feature_value(server_capabilities_v1, feature, len, NULL);
705 }
706
707 int server_supports(const char *feature)
708 {
709 return !!server_feature_value(feature, NULL);
710 }
711
712 void write_command_and_capabilities(struct strbuf *req_buf, const char *command,
713 const struct string_list *server_options)
714 {
715 const char *hash_name;
716 int advertise_sid = 0;
717
718 repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
719
720 ensure_server_supports_v2(command);
721 packet_buf_write(req_buf, "command=%s", command);
722 if (server_supports_v2("agent"))
723 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
724 if (advertise_sid && server_supports_v2("session-id"))
725 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
726 if (server_options && server_options->nr) {
727 ensure_server_supports_v2("server-option");
728 for (size_t i = 0; i < server_options->nr; i++)
729 packet_buf_write(req_buf, "server-option=%s",
730 server_options->items[i].string);
731 }
732
733 if (server_feature_v2("object-format", &hash_name)) {
734 const unsigned int hash_algo = hash_algo_by_name(hash_name);
735 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
736 die(_("mismatched algorithms: client %s; server %s"),
737 the_hash_algo->name, hash_name);
738 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
739 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
740 die(_("the server does not support algorithm '%s'"),
741 the_hash_algo->name);
742 }
743 packet_buf_delim(req_buf);
744 }
745
746 static const char *url_scheme_name(enum url_scheme scheme)
747 {
748 switch (scheme) {
749 case URL_SCHEME_LOCAL:
750 case URL_SCHEME_FILE:
751 return "file";
752 case URL_SCHEME_SSH:
753 return "ssh";
754 case URL_SCHEME_GIT:
755 return "git";
756 default:
757 return "unknown protocol";
758 }
759 }
760
761 static char *host_end(char **hoststart, int removebrackets)
762 {
763 char *host = *hoststart;
764 char *end;
765 char *start = strstr(host, "@[");
766 if (start)
767 start++; /* Jump over '@' */
768 else
769 start = host;
770 if (start[0] == '[') {
771 end = strchr(start + 1, ']');
772 if (end) {
773 if (removebrackets) {
774 *end = 0;
775 memmove(start, start + 1, end - start);
776 end++;
777 }
778 } else
779 end = host;
780 } else
781 end = host;
782 return end;
783 }
784
785 #define STR_(s) # s
786 #define STR(s) STR_(s)
787
788 static void get_host_and_port(char **host, const char **port)
789 {
790 char *colon, *end;
791 end = host_end(host, 1);
792 colon = strchr(end, ':');
793 if (colon) {
794 long portnr = strtol(colon + 1, &end, 10);
795 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
796 *colon = 0;
797 *port = colon + 1;
798 } else if (!colon[1]) {
799 *colon = 0;
800 }
801 }
802 }
803
804 static void enable_keepalive(int sockfd)
805 {
806 int ka = 1;
807
808 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
809 error_errno(_("unable to set SO_KEEPALIVE on socket"));
810 }
811
812 #ifndef NO_IPV6
813
814 static const char *ai_name(const struct addrinfo *ai)
815 {
816 static char addr[NI_MAXHOST];
817 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
818 NI_NUMERICHOST) != 0)
819 xsnprintf(addr, sizeof(addr), "(unknown)");
820
821 return addr;
822 }
823
824 /*
825 * Returns a connected socket() fd, or else die()s.
826 */
827 static int git_tcp_connect_sock(char *host, int flags)
828 {
829 struct strbuf error_message = STRBUF_INIT;
830 int sockfd = -1;
831 const char *port = STR(DEFAULT_GIT_PORT);
832 struct addrinfo hints, *ai0, *ai;
833 int gai;
834 int cnt = 0;
835
836 get_host_and_port(&host, &port);
837 if (!*port)
838 port = "<none>";
839
840 memset(&hints, 0, sizeof(hints));
841 if (flags & CONNECT_IPV4)
842 hints.ai_family = AF_INET;
843 else if (flags & CONNECT_IPV6)
844 hints.ai_family = AF_INET6;
845 hints.ai_socktype = SOCK_STREAM;
846 hints.ai_protocol = IPPROTO_TCP;
847
848 if (flags & CONNECT_VERBOSE)
849 fprintf(stderr, _("Looking up %s ... "), host);
850
851 gai = getaddrinfo(host, port, &hints, &ai);
852 if (gai)
853 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai));
854
855 if (flags & CONNECT_VERBOSE)
856 /* TRANSLATORS: this is the end of "Looking up %s ... " */
857 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
858
859 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
860 sockfd = socket(ai->ai_family,
861 ai->ai_socktype, ai->ai_protocol);
862 if ((sockfd < 0) ||
863 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
864 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
865 host, cnt, ai_name(ai), strerror(errno));
866 if (0 <= sockfd)
867 close(sockfd);
868 sockfd = -1;
869 continue;
870 }
871 if (flags & CONNECT_VERBOSE)
872 fprintf(stderr, "%s ", ai_name(ai));
873 break;
874 }
875
876 freeaddrinfo(ai0);
877
878 if (sockfd < 0)
879 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
880
881 enable_keepalive(sockfd);
882
883 if (flags & CONNECT_VERBOSE)
884 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
885 fprintf_ln(stderr, _("done."));
886
887 strbuf_release(&error_message);
888
889 return sockfd;
890 }
891
892 #else /* NO_IPV6 */
893
894 /*
895 * Returns a connected socket() fd, or else die()s.
896 */
897 static int git_tcp_connect_sock(char *host, int flags)
898 {
899 struct strbuf error_message = STRBUF_INIT;
900 int sockfd = -1;
901 const char *port = STR(DEFAULT_GIT_PORT);
902 char *ep;
903 struct hostent *he;
904 struct sockaddr_in sa;
905 char **ap;
906 unsigned int nport;
907 int cnt;
908
909 get_host_and_port(&host, &port);
910
911 if (flags & CONNECT_VERBOSE)
912 fprintf(stderr, _("Looking up %s ... "), host);
913
914 he = gethostbyname(host);
915 if (!he)
916 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno));
917 nport = strtoul(port, &ep, 10);
918 if ( ep == port || *ep ) {
919 /* Not numeric */
920 struct servent *se = getservbyname(port,"tcp");
921 if ( !se )
922 die(_("unknown port %s"), port);
923 nport = se->s_port;
924 }
925
926 if (flags & CONNECT_VERBOSE)
927 /* TRANSLATORS: this is the end of "Looking up %s ... " */
928 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port);
929
930 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
931 memset(&sa, 0, sizeof sa);
932 sa.sin_family = he->h_addrtype;
933 sa.sin_port = htons(nport);
934 memcpy(&sa.sin_addr, *ap, he->h_length);
935
936 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
937 if ((sockfd < 0) ||
938 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
939 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
940 host,
941 cnt,
942 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
943 strerror(errno));
944 if (0 <= sockfd)
945 close(sockfd);
946 sockfd = -1;
947 continue;
948 }
949 if (flags & CONNECT_VERBOSE)
950 fprintf(stderr, "%s ",
951 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
952 break;
953 }
954
955 if (sockfd < 0)
956 die(_("unable to connect to %s:\n%s"), host, error_message.buf);
957
958 enable_keepalive(sockfd);
959
960 if (flags & CONNECT_VERBOSE)
961 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
962 fprintf_ln(stderr, _("done."));
963
964 return sockfd;
965 }
966
967 #endif /* NO_IPV6 */
968
969
970 /*
971 * Dummy child_process returned by git_connect() if the transport protocol
972 * does not need fork(2).
973 */
974 static struct child_process no_fork = CHILD_PROCESS_INIT;
975
976 int git_connection_is_socket(struct child_process *conn)
977 {
978 return conn == &no_fork;
979 }
980
981 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
982 {
983 int sockfd = git_tcp_connect_sock(host, flags);
984
985 fd[0] = sockfd;
986 fd[1] = dup(sockfd);
987
988 return &no_fork;
989 }
990
991
992 static char *git_proxy_command;
993
994 static int git_proxy_command_options(const char *var, const char *value,
995 const struct config_context *ctx, void *cb)
996 {
997 if (!strcmp(var, "core.gitproxy")) {
998 const char *for_pos;
999 int matchlen = -1;
1000 int hostlen;
1001 const char *rhost_name = cb;
1002 int rhost_len = strlen(rhost_name);
1003
1004 if (git_proxy_command)
1005 return 0;
1006 if (!value)
1007 return config_error_nonbool(var);
1008 /* [core]
1009 * ;# matches www.kernel.org as well
1010 * gitproxy = netcatter-1 for kernel.org
1011 * gitproxy = netcatter-2 for sample.xz
1012 * gitproxy = netcatter-default
1013 */
1014 for_pos = strstr(value, " for ");
1015 if (!for_pos)
1016 /* matches everybody */
1017 matchlen = strlen(value);
1018 else {
1019 hostlen = strlen(for_pos + 5);
1020 if (rhost_len < hostlen)
1021 matchlen = -1;
1022 else if (!strncmp(for_pos + 5,
1023 rhost_name + rhost_len - hostlen,
1024 hostlen) &&
1025 ((rhost_len == hostlen) ||
1026 rhost_name[rhost_len - hostlen -1] == '.'))
1027 matchlen = for_pos - value;
1028 else
1029 matchlen = -1;
1030 }
1031 if (0 <= matchlen) {
1032 /* core.gitproxy = none for kernel.org */
1033 if (matchlen == 4 &&
1034 !memcmp(value, "none", 4))
1035 matchlen = 0;
1036 git_proxy_command = xmemdupz(value, matchlen);
1037 }
1038 return 0;
1039 }
1040
1041 return git_default_config(var, value, ctx, cb);
1042 }
1043
1044 static int git_use_proxy(const char *host)
1045 {
1046 git_proxy_command = getenv("GIT_PROXY_COMMAND");
1047 repo_config(the_repository, git_proxy_command_options, (void*)host);
1048 return (git_proxy_command && *git_proxy_command);
1049 }
1050
1051 static struct child_process *git_proxy_connect(int fd[2], char *host)
1052 {
1053 const char *port = STR(DEFAULT_GIT_PORT);
1054 struct child_process *proxy;
1055
1056 get_host_and_port(&host, &port);
1057
1058 if (looks_like_command_line_option(host))
1059 die(_("strange hostname '%s' blocked"), host);
1060 if (looks_like_command_line_option(port))
1061 die(_("strange port '%s' blocked"), port);
1062
1063 proxy = xmalloc(sizeof(*proxy));
1064 child_process_init(proxy);
1065 strvec_push(&proxy->args, git_proxy_command);
1066 strvec_push(&proxy->args, host);
1067 strvec_push(&proxy->args, port);
1068 proxy->in = -1;
1069 proxy->out = -1;
1070 if (start_command(proxy))
1071 die(_("cannot start proxy %s"), git_proxy_command);
1072 fd[0] = proxy->out; /* read from proxy stdout */
1073 fd[1] = proxy->in; /* write to proxy stdin */
1074 return proxy;
1075 }
1076
1077 static char *get_port(char *host)
1078 {
1079 char *end;
1080 char *p = strchr(host, ':');
1081
1082 if (p) {
1083 long port = strtol(p + 1, &end, 10);
1084 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
1085 *p = '\0';
1086 return p+1;
1087 }
1088 }
1089
1090 return NULL;
1091 }
1092
1093 /*
1094 * Extract protocol and relevant parts from the specified connection URL.
1095 * The caller must free() the returned strings.
1096 */
1097 static enum url_scheme parse_connect_url(const char *url_orig, char **ret_host,
1098 char **ret_path)
1099 {
1100 char *url;
1101 char *host, *path;
1102 char *end;
1103 int separator = '/';
1104 enum url_scheme scheme = URL_SCHEME_LOCAL;
1105
1106 if (is_url(url_orig))
1107 url = url_decode(url_orig);
1108 else
1109 url = xstrdup(url_orig);
1110
1111 host = strstr(url, "://");
1112 if (host) {
1113 *host = '\0';
1114 scheme = url_get_scheme(url);
1115 if (scheme == URL_SCHEME_UNKNOWN)
1116 die(_("protocol '%s' is not supported"), url);
1117 host += 3;
1118 } else {
1119 host = url;
1120 if (!url_is_local_not_ssh(url)) {
1121 scheme = URL_SCHEME_SSH;
1122 separator = ':';
1123 }
1124 }
1125
1126 /*
1127 * Don't do destructive transforms as protocol code does
1128 * '[]' unwrapping in get_host_and_port()
1129 */
1130 end = host_end(&host, 0);
1131
1132 if (scheme == URL_SCHEME_LOCAL)
1133 path = end;
1134 else if (scheme == URL_SCHEME_FILE && *host != '/' &&
1135 !has_dos_drive_prefix(host) &&
1136 offset_1st_component(host - 2) > 1)
1137 path = host - 2; /* include the leading "//" */
1138 else if (scheme == URL_SCHEME_FILE && has_dos_drive_prefix(end))
1139 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1140 else
1141 path = strchr(end, separator);
1142
1143 if (!path || !*path)
1144 die(_("no path specified; see 'git help pull' for valid url syntax"));
1145
1146 /*
1147 * null-terminate hostname and point path to ~ for URL's like this:
1148 * ssh://host.xz/~user/repo
1149 */
1150
1151 end = path; /* Need to \0 terminate host here */
1152 if (separator == ':')
1153 path++; /* path starts after ':' */
1154 if (scheme == URL_SCHEME_GIT || scheme == URL_SCHEME_SSH) {
1155 if (path[1] == '~')
1156 path++;
1157 }
1158
1159 path = xstrdup(path);
1160 *end = '\0';
1161
1162 *ret_host = xstrdup(host);
1163 *ret_path = path;
1164 free(url);
1165 return scheme;
1166 }
1167
1168 static const char *get_ssh_command(void)
1169 {
1170 const char *ssh;
1171
1172 if ((ssh = getenv("GIT_SSH_COMMAND")))
1173 return ssh;
1174
1175 if (!repo_config_get_string_tmp(the_repository, "core.sshcommand", &ssh))
1176 return ssh;
1177
1178 return NULL;
1179 }
1180
1181 enum ssh_variant {
1182 VARIANT_AUTO,
1183 VARIANT_SIMPLE,
1184 VARIANT_SSH,
1185 VARIANT_PLINK,
1186 VARIANT_PUTTY,
1187 VARIANT_TORTOISEPLINK,
1188 };
1189
1190 static void override_ssh_variant(enum ssh_variant *ssh_variant)
1191 {
1192 const char *variant = getenv("GIT_SSH_VARIANT");
1193
1194 if (!variant && repo_config_get_string_tmp(the_repository, "ssh.variant", &variant))
1195 return;
1196
1197 if (!strcmp(variant, "auto"))
1198 *ssh_variant = VARIANT_AUTO;
1199 else if (!strcmp(variant, "plink"))
1200 *ssh_variant = VARIANT_PLINK;
1201 else if (!strcmp(variant, "putty"))
1202 *ssh_variant = VARIANT_PUTTY;
1203 else if (!strcmp(variant, "tortoiseplink"))
1204 *ssh_variant = VARIANT_TORTOISEPLINK;
1205 else if (!strcmp(variant, "simple"))
1206 *ssh_variant = VARIANT_SIMPLE;
1207 else
1208 *ssh_variant = VARIANT_SSH;
1209 }
1210
1211 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
1212 int is_cmdline)
1213 {
1214 enum ssh_variant ssh_variant = VARIANT_AUTO;
1215 const char *variant;
1216 char *p = NULL;
1217
1218 override_ssh_variant(&ssh_variant);
1219
1220 if (ssh_variant != VARIANT_AUTO)
1221 return ssh_variant;
1222
1223 if (!is_cmdline) {
1224 p = xstrdup(ssh_command);
1225 variant = basename(p);
1226 } else {
1227 const char **ssh_argv;
1228
1229 p = xstrdup(ssh_command);
1230 if (split_cmdline(p, &ssh_argv) > 0) {
1231 variant = basename((char *)ssh_argv[0]);
1232 /*
1233 * At this point, variant points into the buffer
1234 * referenced by p, hence we do not need ssh_argv
1235 * any longer.
1236 */
1237 free(ssh_argv);
1238 } else {
1239 free(p);
1240 return ssh_variant;
1241 }
1242 }
1243
1244 if (!strcasecmp(variant, "ssh") ||
1245 !strcasecmp(variant, "ssh.exe"))
1246 ssh_variant = VARIANT_SSH;
1247 else if (!strcasecmp(variant, "plink") ||
1248 !strcasecmp(variant, "plink.exe"))
1249 ssh_variant = VARIANT_PLINK;
1250 else if (!strcasecmp(variant, "tortoiseplink") ||
1251 !strcasecmp(variant, "tortoiseplink.exe"))
1252 ssh_variant = VARIANT_TORTOISEPLINK;
1253
1254 free(p);
1255 return ssh_variant;
1256 }
1257
1258 /*
1259 * Open a connection using Git's native protocol.
1260 *
1261 * The caller is responsible for freeing hostandport, but this function may
1262 * modify it (for example, to truncate it to remove the port part).
1263 */
1264 static struct child_process *git_connect_git(int fd[2], char *hostandport,
1265 const char *path, const char *prog,
1266 enum protocol_version version,
1267 int flags)
1268 {
1269 struct child_process *conn;
1270 struct strbuf request = STRBUF_INIT;
1271 /*
1272 * Set up virtual host information based on where we will
1273 * connect, unless the user has overridden us in
1274 * the environment.
1275 */
1276 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1277 if (target_host)
1278 target_host = xstrdup(target_host);
1279 else
1280 target_host = xstrdup(hostandport);
1281
1282 transport_check_allowed("git");
1283 if (strchr(target_host, '\n') || strchr(path, '\n'))
1284 die(_("newline is forbidden in git:// hosts and repo paths"));
1285
1286 /*
1287 * These underlying connection commands die() if they
1288 * cannot connect.
1289 */
1290 if (git_use_proxy(hostandport))
1291 conn = git_proxy_connect(fd, hostandport);
1292 else
1293 conn = git_tcp_connect(fd, hostandport, flags);
1294 /*
1295 * Separate original protocol components prog and path
1296 * from extended host header with a NUL byte.
1297 *
1298 * Note: Do not add any other headers here! Doing so
1299 * will cause older git-daemon servers to crash.
1300 */
1301 strbuf_addf(&request,
1302 "%s %s%chost=%s%c",
1303 prog, path, 0,
1304 target_host, 0);
1305
1306 /* If using a new version put that stuff here after a second null byte */
1307 if (version > 0) {
1308 strbuf_addch(&request, '\0');
1309 strbuf_addf(&request, "version=%d%c",
1310 version, '\0');
1311 }
1312
1313 packet_write(fd[1], request.buf, request.len);
1314
1315 free(target_host);
1316 strbuf_release(&request);
1317 return conn;
1318 }
1319
1320 /*
1321 * Append the appropriate environment variables to `env` and options to
1322 * `args` for running ssh in Git's SSH-tunneled transport.
1323 */
1324 static void push_ssh_options(struct strvec *args, struct strvec *env,
1325 enum ssh_variant variant, const char *port,
1326 enum protocol_version version, int flags)
1327 {
1328 if (variant == VARIANT_SSH &&
1329 version > 0) {
1330 strvec_push(args, "-o");
1331 strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1332 strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1333 version);
1334 }
1335
1336 if (flags & CONNECT_IPV4) {
1337 switch (variant) {
1338 case VARIANT_AUTO:
1339 BUG("VARIANT_AUTO passed to push_ssh_options");
1340 case VARIANT_SIMPLE:
1341 die(_("ssh variant 'simple' does not support -4"));
1342 case VARIANT_SSH:
1343 case VARIANT_PLINK:
1344 case VARIANT_PUTTY:
1345 case VARIANT_TORTOISEPLINK:
1346 strvec_push(args, "-4");
1347 }
1348 } else if (flags & CONNECT_IPV6) {
1349 switch (variant) {
1350 case VARIANT_AUTO:
1351 BUG("VARIANT_AUTO passed to push_ssh_options");
1352 case VARIANT_SIMPLE:
1353 die(_("ssh variant 'simple' does not support -6"));
1354 case VARIANT_SSH:
1355 case VARIANT_PLINK:
1356 case VARIANT_PUTTY:
1357 case VARIANT_TORTOISEPLINK:
1358 strvec_push(args, "-6");
1359 }
1360 }
1361
1362 if (variant == VARIANT_TORTOISEPLINK)
1363 strvec_push(args, "-batch");
1364
1365 if (port) {
1366 switch (variant) {
1367 case VARIANT_AUTO:
1368 BUG("VARIANT_AUTO passed to push_ssh_options");
1369 case VARIANT_SIMPLE:
1370 die(_("ssh variant 'simple' does not support setting port"));
1371 case VARIANT_SSH:
1372 strvec_push(args, "-p");
1373 break;
1374 case VARIANT_PLINK:
1375 case VARIANT_PUTTY:
1376 case VARIANT_TORTOISEPLINK:
1377 strvec_push(args, "-P");
1378 }
1379
1380 strvec_push(args, port);
1381 }
1382 }
1383
1384 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1385 static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1386 const char *port, enum protocol_version version,
1387 int flags)
1388 {
1389 const char *ssh;
1390 enum ssh_variant variant;
1391
1392 if (looks_like_command_line_option(ssh_host))
1393 die(_("strange hostname '%s' blocked"), ssh_host);
1394
1395 ssh = get_ssh_command();
1396 if (ssh) {
1397 variant = determine_ssh_variant(ssh, 1);
1398 } else {
1399 /*
1400 * GIT_SSH is the no-shell version of
1401 * GIT_SSH_COMMAND (and must remain so for
1402 * historical compatibility).
1403 */
1404 conn->use_shell = 0;
1405
1406 ssh = getenv("GIT_SSH");
1407 if (!ssh)
1408 ssh = "ssh";
1409 variant = determine_ssh_variant(ssh, 0);
1410 }
1411
1412 if (variant == VARIANT_AUTO) {
1413 struct child_process detect = CHILD_PROCESS_INIT;
1414
1415 detect.use_shell = conn->use_shell;
1416 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1417
1418 strvec_push(&detect.args, ssh);
1419 strvec_push(&detect.args, "-G");
1420 push_ssh_options(&detect.args, &detect.env,
1421 VARIANT_SSH, port, version, flags);
1422 strvec_push(&detect.args, ssh_host);
1423
1424 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1425 }
1426
1427 strvec_push(&conn->args, ssh);
1428 push_ssh_options(&conn->args, &conn->env, variant, port, version,
1429 flags);
1430 strvec_push(&conn->args, ssh_host);
1431 }
1432
1433 /*
1434 * This returns the dummy child_process `no_fork` if the transport protocol
1435 * does not need fork(2), or a struct child_process object if it does. Once
1436 * done, finish the connection with finish_connect() with the value returned
1437 * from this function (it is safe to call finish_connect() with NULL to
1438 * support the former case).
1439 *
1440 * If it returns, the connect is successful; it just dies on errors (this
1441 * will hopefully be changed in a libification effort, to return NULL when
1442 * the connection failed).
1443 */
1444 struct child_process *git_connect(int fd[2], const char *url,
1445 enum git_connect_service service,
1446 const char *prog, int flags)
1447 {
1448 char *hostandport, *path;
1449 struct child_process *conn;
1450 enum url_scheme scheme;
1451 enum protocol_version version = get_protocol_version_config();
1452
1453 /*
1454 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1455 * to perform any operation that doesn't involve upload-pack (i.e., a
1456 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
1457 * to do anything else (like push or remote archive) via v2.
1458 */
1459 if (version == protocol_v2 && service != GIT_CONNECT_UPLOAD_PACK)
1460 version = protocol_v0;
1461
1462 /* Without this we cannot rely on waitpid() to tell
1463 * what happened to our children.
1464 */
1465 signal(SIGCHLD, SIG_DFL);
1466
1467 scheme = parse_connect_url(url, &hostandport, &path);
1468 if ((flags & CONNECT_DIAG_URL) && (scheme != URL_SCHEME_SSH)) {
1469 printf("Diag: url=%s\n", url ? url : "NULL");
1470 printf("Diag: protocol=%s\n", url_scheme_name(scheme));
1471 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
1472 printf("Diag: path=%s\n", path ? path : "NULL");
1473 conn = NULL;
1474 } else if (scheme == URL_SCHEME_GIT) {
1475 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
1476 conn->trace2_child_class = "transport/git";
1477 } else {
1478 struct strbuf cmd = STRBUF_INIT;
1479 const char *const *var;
1480
1481 conn = xmalloc(sizeof(*conn));
1482 child_process_init(conn);
1483
1484 if (looks_like_command_line_option(path))
1485 die(_("strange pathname '%s' blocked"), path);
1486
1487 strbuf_addstr(&cmd, prog);
1488 strbuf_addch(&cmd, ' ');
1489 sq_quote_buf(&cmd, path);
1490
1491 /* remove repo-local variables from the environment */
1492 for (var = local_repo_env; *var; var++)
1493 strvec_push(&conn->env, *var);
1494
1495 conn->use_shell = 1;
1496 conn->in = conn->out = -1;
1497 if (scheme == URL_SCHEME_SSH) {
1498 char *ssh_host = hostandport;
1499 const char *port = NULL;
1500 transport_check_allowed("ssh");
1501 get_host_and_port(&ssh_host, &port);
1502
1503 if (!port)
1504 port = get_port(ssh_host);
1505
1506 if (flags & CONNECT_DIAG_URL) {
1507 printf("Diag: url=%s\n", url ? url : "NULL");
1508 printf("Diag: protocol=%s\n", url_scheme_name(scheme));
1509 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1510 printf("Diag: port=%s\n", port ? port : "NONE");
1511 printf("Diag: path=%s\n", path ? path : "NULL");
1512
1513 free(hostandport);
1514 free(path);
1515 child_process_clear(conn);
1516 free(conn);
1517 strbuf_release(&cmd);
1518 return NULL;
1519 }
1520 conn->trace2_child_class = "transport/ssh";
1521 fill_ssh_args(conn, ssh_host, port, version, flags);
1522 } else {
1523 transport_check_allowed("file");
1524 conn->trace2_child_class = "transport/file";
1525 if (version > 0) {
1526 strvec_pushf(&conn->env,
1527 GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1528 version);
1529 }
1530 }
1531 strvec_push(&conn->args, cmd.buf);
1532
1533 if (start_command(conn))
1534 die(_("unable to fork"));
1535
1536 fd[0] = conn->out; /* read from child's stdout */
1537 fd[1] = conn->in; /* write to child's stdin */
1538 strbuf_release(&cmd);
1539 }
1540 free(hostandport);
1541 free(path);
1542 return conn;
1543 }
1544
1545 int finish_connect(struct child_process *conn)
1546 {
1547 int code;
1548 if (!conn || git_connection_is_socket(conn))
1549 return 0;
1550
1551 code = finish_command(conn);
1552 free(conn);
1553 return code;
1554 }