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