]> git.ipfire.org Git - thirdparty/git.git/blob - connect.c
ssh: introduce a 'simple' ssh variant
[thirdparty/git.git] / connect.c
1 #include "git-compat-util.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "pkt-line.h"
5 #include "quote.h"
6 #include "refs.h"
7 #include "run-command.h"
8 #include "remote.h"
9 #include "connect.h"
10 #include "url.h"
11 #include "string-list.h"
12 #include "sha1-array.h"
13 #include "transport.h"
14 #include "strbuf.h"
15 #include "protocol.h"
16
17 static char *server_capabilities;
18 static const char *parse_feature_value(const char *, const char *, int *);
19
20 static int check_ref(const char *name, unsigned int flags)
21 {
22 if (!flags)
23 return 1;
24
25 if (!skip_prefix(name, "refs/", &name))
26 return 0;
27
28 /* REF_NORMAL means that we don't want the magic fake tag refs */
29 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
30 return 0;
31
32 /* REF_HEADS means that we want regular branch heads */
33 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
34 return 1;
35
36 /* REF_TAGS means that we want tags */
37 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
38 return 1;
39
40 /* All type bits clear means that we are ok with anything */
41 return !(flags & ~REF_NORMAL);
42 }
43
44 int check_ref_type(const struct ref *ref, int flags)
45 {
46 return check_ref(ref->name, flags);
47 }
48
49 static void die_initial_contact(int unexpected)
50 {
51 if (unexpected)
52 die(_("The remote end hung up upon initial contact"));
53 else
54 die(_("Could not read from remote repository.\n\n"
55 "Please make sure you have the correct access rights\n"
56 "and the repository exists."));
57 }
58
59 static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
60 {
61 char *sym, *target;
62 struct string_list_item *item;
63
64 if (!len)
65 return; /* just "symref" */
66 /* e.g. "symref=HEAD:refs/heads/master" */
67 sym = xmemdupz(val, len);
68 target = strchr(sym, ':');
69 if (!target)
70 /* just "symref=something" */
71 goto reject;
72 *(target++) = '\0';
73 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
74 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
75 /* "symref=bogus:pair */
76 goto reject;
77 item = string_list_append_nodup(symref, sym);
78 item->util = target;
79 return;
80 reject:
81 free(sym);
82 return;
83 }
84
85 static void annotate_refs_with_symref_info(struct ref *ref)
86 {
87 struct string_list symref = STRING_LIST_INIT_DUP;
88 const char *feature_list = server_capabilities;
89
90 while (feature_list) {
91 int len;
92 const char *val;
93
94 val = parse_feature_value(feature_list, "symref", &len);
95 if (!val)
96 break;
97 parse_one_symref_info(&symref, val, len);
98 feature_list = val + 1;
99 }
100 string_list_sort(&symref);
101
102 for (; ref; ref = ref->next) {
103 struct string_list_item *item;
104 item = string_list_lookup(&symref, ref->name);
105 if (!item)
106 continue;
107 ref->symref = xstrdup((char *)item->util);
108 }
109 string_list_clear(&symref, 0);
110 }
111
112 /*
113 * Read one line of a server's ref advertisement into packet_buffer.
114 */
115 static int read_remote_ref(int in, char **src_buf, size_t *src_len,
116 int *responded)
117 {
118 int len = packet_read(in, src_buf, src_len,
119 packet_buffer, sizeof(packet_buffer),
120 PACKET_READ_GENTLE_ON_EOF |
121 PACKET_READ_CHOMP_NEWLINE);
122 const char *arg;
123 if (len < 0)
124 die_initial_contact(*responded);
125 if (len > 4 && skip_prefix(packet_buffer, "ERR ", &arg))
126 die("remote error: %s", arg);
127
128 *responded = 1;
129
130 return len;
131 }
132
133 #define EXPECTING_PROTOCOL_VERSION 0
134 #define EXPECTING_FIRST_REF 1
135 #define EXPECTING_REF 2
136 #define EXPECTING_SHALLOW 3
137
138 /* Returns 1 if packet_buffer is a protocol version pkt-line, 0 otherwise. */
139 static int process_protocol_version(void)
140 {
141 switch (determine_protocol_version_client(packet_buffer)) {
142 case protocol_v1:
143 return 1;
144 case protocol_v0:
145 return 0;
146 default:
147 die("server is speaking an unknown protocol");
148 }
149 }
150
151 static void process_capabilities(int *len)
152 {
153 int nul_location = strlen(packet_buffer);
154 if (nul_location == *len)
155 return;
156 server_capabilities = xstrdup(packet_buffer + nul_location + 1);
157 *len = nul_location;
158 }
159
160 static int process_dummy_ref(void)
161 {
162 struct object_id oid;
163 const char *name;
164
165 if (parse_oid_hex(packet_buffer, &oid, &name))
166 return 0;
167 if (*name != ' ')
168 return 0;
169 name++;
170
171 return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
172 }
173
174 static void check_no_capabilities(int len)
175 {
176 if (strlen(packet_buffer) != len)
177 warning("Ignoring capabilities after first line '%s'",
178 packet_buffer + strlen(packet_buffer));
179 }
180
181 static int process_ref(int len, struct ref ***list, unsigned int flags,
182 struct oid_array *extra_have)
183 {
184 struct object_id old_oid;
185 const char *name;
186
187 if (parse_oid_hex(packet_buffer, &old_oid, &name))
188 return 0;
189 if (*name != ' ')
190 return 0;
191 name++;
192
193 if (extra_have && !strcmp(name, ".have")) {
194 oid_array_append(extra_have, &old_oid);
195 } else if (!strcmp(name, "capabilities^{}")) {
196 die("protocol error: unexpected capabilities^{}");
197 } else if (check_ref(name, flags)) {
198 struct ref *ref = alloc_ref(name);
199 oidcpy(&ref->old_oid, &old_oid);
200 **list = ref;
201 *list = &ref->next;
202 }
203 check_no_capabilities(len);
204 return 1;
205 }
206
207 static int process_shallow(int len, struct oid_array *shallow_points)
208 {
209 const char *arg;
210 struct object_id old_oid;
211
212 if (!skip_prefix(packet_buffer, "shallow ", &arg))
213 return 0;
214
215 if (get_oid_hex(arg, &old_oid))
216 die("protocol error: expected shallow sha-1, got '%s'", arg);
217 if (!shallow_points)
218 die("repository on the other end cannot be shallow");
219 oid_array_append(shallow_points, &old_oid);
220 check_no_capabilities(len);
221 return 1;
222 }
223
224 /*
225 * Read all the refs from the other end
226 */
227 struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
228 struct ref **list, unsigned int flags,
229 struct oid_array *extra_have,
230 struct oid_array *shallow_points)
231 {
232 struct ref **orig_list = list;
233
234 /*
235 * A hang-up after seeing some response from the other end
236 * means that it is unexpected, as we know the other end is
237 * willing to talk to us. A hang-up before seeing any
238 * response does not necessarily mean an ACL problem, though.
239 */
240 int responded = 0;
241 int len;
242 int state = EXPECTING_PROTOCOL_VERSION;
243
244 *list = NULL;
245
246 while ((len = read_remote_ref(in, &src_buf, &src_len, &responded))) {
247 switch (state) {
248 case EXPECTING_PROTOCOL_VERSION:
249 if (process_protocol_version()) {
250 state = EXPECTING_FIRST_REF;
251 break;
252 }
253 state = EXPECTING_FIRST_REF;
254 /* fallthrough */
255 case EXPECTING_FIRST_REF:
256 process_capabilities(&len);
257 if (process_dummy_ref()) {
258 state = EXPECTING_SHALLOW;
259 break;
260 }
261 state = EXPECTING_REF;
262 /* fallthrough */
263 case EXPECTING_REF:
264 if (process_ref(len, &list, flags, extra_have))
265 break;
266 state = EXPECTING_SHALLOW;
267 /* fallthrough */
268 case EXPECTING_SHALLOW:
269 if (process_shallow(len, shallow_points))
270 break;
271 die("protocol error: unexpected '%s'", packet_buffer);
272 default:
273 die("unexpected state %d", state);
274 }
275 }
276
277 annotate_refs_with_symref_info(*orig_list);
278
279 return list;
280 }
281
282 static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
283 {
284 int len;
285
286 if (!feature_list)
287 return NULL;
288
289 len = strlen(feature);
290 while (*feature_list) {
291 const char *found = strstr(feature_list, feature);
292 if (!found)
293 return NULL;
294 if (feature_list == found || isspace(found[-1])) {
295 const char *value = found + len;
296 /* feature with no value (e.g., "thin-pack") */
297 if (!*value || isspace(*value)) {
298 if (lenp)
299 *lenp = 0;
300 return value;
301 }
302 /* feature with a value (e.g., "agent=git/1.2.3") */
303 else if (*value == '=') {
304 value++;
305 if (lenp)
306 *lenp = strcspn(value, " \t\n");
307 return value;
308 }
309 /*
310 * otherwise we matched a substring of another feature;
311 * keep looking
312 */
313 }
314 feature_list = found + 1;
315 }
316 return NULL;
317 }
318
319 int parse_feature_request(const char *feature_list, const char *feature)
320 {
321 return !!parse_feature_value(feature_list, feature, NULL);
322 }
323
324 const char *server_feature_value(const char *feature, int *len)
325 {
326 return parse_feature_value(server_capabilities, feature, len);
327 }
328
329 int server_supports(const char *feature)
330 {
331 return !!server_feature_value(feature, NULL);
332 }
333
334 enum protocol {
335 PROTO_LOCAL = 1,
336 PROTO_FILE,
337 PROTO_SSH,
338 PROTO_GIT
339 };
340
341 int url_is_local_not_ssh(const char *url)
342 {
343 const char *colon = strchr(url, ':');
344 const char *slash = strchr(url, '/');
345 return !colon || (slash && slash < colon) ||
346 has_dos_drive_prefix(url);
347 }
348
349 static const char *prot_name(enum protocol protocol)
350 {
351 switch (protocol) {
352 case PROTO_LOCAL:
353 case PROTO_FILE:
354 return "file";
355 case PROTO_SSH:
356 return "ssh";
357 case PROTO_GIT:
358 return "git";
359 default:
360 return "unknown protocol";
361 }
362 }
363
364 static enum protocol get_protocol(const char *name)
365 {
366 if (!strcmp(name, "ssh"))
367 return PROTO_SSH;
368 if (!strcmp(name, "git"))
369 return PROTO_GIT;
370 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
371 return PROTO_SSH;
372 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
373 return PROTO_SSH;
374 if (!strcmp(name, "file"))
375 return PROTO_FILE;
376 die("I don't handle protocol '%s'", name);
377 }
378
379 static char *host_end(char **hoststart, int removebrackets)
380 {
381 char *host = *hoststart;
382 char *end;
383 char *start = strstr(host, "@[");
384 if (start)
385 start++; /* Jump over '@' */
386 else
387 start = host;
388 if (start[0] == '[') {
389 end = strchr(start + 1, ']');
390 if (end) {
391 if (removebrackets) {
392 *end = 0;
393 memmove(start, start + 1, end - start);
394 end++;
395 }
396 } else
397 end = host;
398 } else
399 end = host;
400 return end;
401 }
402
403 #define STR_(s) # s
404 #define STR(s) STR_(s)
405
406 static void get_host_and_port(char **host, const char **port)
407 {
408 char *colon, *end;
409 end = host_end(host, 1);
410 colon = strchr(end, ':');
411 if (colon) {
412 long portnr = strtol(colon + 1, &end, 10);
413 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
414 *colon = 0;
415 *port = colon + 1;
416 } else if (!colon[1]) {
417 *colon = 0;
418 }
419 }
420 }
421
422 static void enable_keepalive(int sockfd)
423 {
424 int ka = 1;
425
426 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
427 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
428 strerror(errno));
429 }
430
431 #ifndef NO_IPV6
432
433 static const char *ai_name(const struct addrinfo *ai)
434 {
435 static char addr[NI_MAXHOST];
436 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
437 NI_NUMERICHOST) != 0)
438 xsnprintf(addr, sizeof(addr), "(unknown)");
439
440 return addr;
441 }
442
443 /*
444 * Returns a connected socket() fd, or else die()s.
445 */
446 static int git_tcp_connect_sock(char *host, int flags)
447 {
448 struct strbuf error_message = STRBUF_INIT;
449 int sockfd = -1;
450 const char *port = STR(DEFAULT_GIT_PORT);
451 struct addrinfo hints, *ai0, *ai;
452 int gai;
453 int cnt = 0;
454
455 get_host_and_port(&host, &port);
456 if (!*port)
457 port = "<none>";
458
459 memset(&hints, 0, sizeof(hints));
460 if (flags & CONNECT_IPV4)
461 hints.ai_family = AF_INET;
462 else if (flags & CONNECT_IPV6)
463 hints.ai_family = AF_INET6;
464 hints.ai_socktype = SOCK_STREAM;
465 hints.ai_protocol = IPPROTO_TCP;
466
467 if (flags & CONNECT_VERBOSE)
468 fprintf(stderr, "Looking up %s ... ", host);
469
470 gai = getaddrinfo(host, port, &hints, &ai);
471 if (gai)
472 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
473
474 if (flags & CONNECT_VERBOSE)
475 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
476
477 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
478 sockfd = socket(ai->ai_family,
479 ai->ai_socktype, ai->ai_protocol);
480 if ((sockfd < 0) ||
481 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
482 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
483 host, cnt, ai_name(ai), strerror(errno));
484 if (0 <= sockfd)
485 close(sockfd);
486 sockfd = -1;
487 continue;
488 }
489 if (flags & CONNECT_VERBOSE)
490 fprintf(stderr, "%s ", ai_name(ai));
491 break;
492 }
493
494 freeaddrinfo(ai0);
495
496 if (sockfd < 0)
497 die("unable to connect to %s:\n%s", host, error_message.buf);
498
499 enable_keepalive(sockfd);
500
501 if (flags & CONNECT_VERBOSE)
502 fprintf(stderr, "done.\n");
503
504 strbuf_release(&error_message);
505
506 return sockfd;
507 }
508
509 #else /* NO_IPV6 */
510
511 /*
512 * Returns a connected socket() fd, or else die()s.
513 */
514 static int git_tcp_connect_sock(char *host, int flags)
515 {
516 struct strbuf error_message = STRBUF_INIT;
517 int sockfd = -1;
518 const char *port = STR(DEFAULT_GIT_PORT);
519 char *ep;
520 struct hostent *he;
521 struct sockaddr_in sa;
522 char **ap;
523 unsigned int nport;
524 int cnt;
525
526 get_host_and_port(&host, &port);
527
528 if (flags & CONNECT_VERBOSE)
529 fprintf(stderr, "Looking up %s ... ", host);
530
531 he = gethostbyname(host);
532 if (!he)
533 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
534 nport = strtoul(port, &ep, 10);
535 if ( ep == port || *ep ) {
536 /* Not numeric */
537 struct servent *se = getservbyname(port,"tcp");
538 if ( !se )
539 die("Unknown port %s", port);
540 nport = se->s_port;
541 }
542
543 if (flags & CONNECT_VERBOSE)
544 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
545
546 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
547 memset(&sa, 0, sizeof sa);
548 sa.sin_family = he->h_addrtype;
549 sa.sin_port = htons(nport);
550 memcpy(&sa.sin_addr, *ap, he->h_length);
551
552 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
553 if ((sockfd < 0) ||
554 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
555 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
556 host,
557 cnt,
558 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
559 strerror(errno));
560 if (0 <= sockfd)
561 close(sockfd);
562 sockfd = -1;
563 continue;
564 }
565 if (flags & CONNECT_VERBOSE)
566 fprintf(stderr, "%s ",
567 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
568 break;
569 }
570
571 if (sockfd < 0)
572 die("unable to connect to %s:\n%s", host, error_message.buf);
573
574 enable_keepalive(sockfd);
575
576 if (flags & CONNECT_VERBOSE)
577 fprintf(stderr, "done.\n");
578
579 return sockfd;
580 }
581
582 #endif /* NO_IPV6 */
583
584
585 static void git_tcp_connect(int fd[2], char *host, int flags)
586 {
587 int sockfd = git_tcp_connect_sock(host, flags);
588
589 fd[0] = sockfd;
590 fd[1] = dup(sockfd);
591 }
592
593
594 static char *git_proxy_command;
595
596 static int git_proxy_command_options(const char *var, const char *value,
597 void *cb)
598 {
599 if (!strcmp(var, "core.gitproxy")) {
600 const char *for_pos;
601 int matchlen = -1;
602 int hostlen;
603 const char *rhost_name = cb;
604 int rhost_len = strlen(rhost_name);
605
606 if (git_proxy_command)
607 return 0;
608 if (!value)
609 return config_error_nonbool(var);
610 /* [core]
611 * ;# matches www.kernel.org as well
612 * gitproxy = netcatter-1 for kernel.org
613 * gitproxy = netcatter-2 for sample.xz
614 * gitproxy = netcatter-default
615 */
616 for_pos = strstr(value, " for ");
617 if (!for_pos)
618 /* matches everybody */
619 matchlen = strlen(value);
620 else {
621 hostlen = strlen(for_pos + 5);
622 if (rhost_len < hostlen)
623 matchlen = -1;
624 else if (!strncmp(for_pos + 5,
625 rhost_name + rhost_len - hostlen,
626 hostlen) &&
627 ((rhost_len == hostlen) ||
628 rhost_name[rhost_len - hostlen -1] == '.'))
629 matchlen = for_pos - value;
630 else
631 matchlen = -1;
632 }
633 if (0 <= matchlen) {
634 /* core.gitproxy = none for kernel.org */
635 if (matchlen == 4 &&
636 !memcmp(value, "none", 4))
637 matchlen = 0;
638 git_proxy_command = xmemdupz(value, matchlen);
639 }
640 return 0;
641 }
642
643 return git_default_config(var, value, cb);
644 }
645
646 static int git_use_proxy(const char *host)
647 {
648 git_proxy_command = getenv("GIT_PROXY_COMMAND");
649 git_config(git_proxy_command_options, (void*)host);
650 return (git_proxy_command && *git_proxy_command);
651 }
652
653 static struct child_process *git_proxy_connect(int fd[2], char *host)
654 {
655 const char *port = STR(DEFAULT_GIT_PORT);
656 struct child_process *proxy;
657
658 get_host_and_port(&host, &port);
659
660 if (looks_like_command_line_option(host))
661 die("strange hostname '%s' blocked", host);
662 if (looks_like_command_line_option(port))
663 die("strange port '%s' blocked", port);
664
665 proxy = xmalloc(sizeof(*proxy));
666 child_process_init(proxy);
667 argv_array_push(&proxy->args, git_proxy_command);
668 argv_array_push(&proxy->args, host);
669 argv_array_push(&proxy->args, port);
670 proxy->in = -1;
671 proxy->out = -1;
672 if (start_command(proxy))
673 die("cannot start proxy %s", git_proxy_command);
674 fd[0] = proxy->out; /* read from proxy stdout */
675 fd[1] = proxy->in; /* write to proxy stdin */
676 return proxy;
677 }
678
679 static char *get_port(char *host)
680 {
681 char *end;
682 char *p = strchr(host, ':');
683
684 if (p) {
685 long port = strtol(p + 1, &end, 10);
686 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
687 *p = '\0';
688 return p+1;
689 }
690 }
691
692 return NULL;
693 }
694
695 /*
696 * Extract protocol and relevant parts from the specified connection URL.
697 * The caller must free() the returned strings.
698 */
699 static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
700 char **ret_path)
701 {
702 char *url;
703 char *host, *path;
704 char *end;
705 int separator = '/';
706 enum protocol protocol = PROTO_LOCAL;
707
708 if (is_url(url_orig))
709 url = url_decode(url_orig);
710 else
711 url = xstrdup(url_orig);
712
713 host = strstr(url, "://");
714 if (host) {
715 *host = '\0';
716 protocol = get_protocol(url);
717 host += 3;
718 } else {
719 host = url;
720 if (!url_is_local_not_ssh(url)) {
721 protocol = PROTO_SSH;
722 separator = ':';
723 }
724 }
725
726 /*
727 * Don't do destructive transforms as protocol code does
728 * '[]' unwrapping in get_host_and_port()
729 */
730 end = host_end(&host, 0);
731
732 if (protocol == PROTO_LOCAL)
733 path = end;
734 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
735 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
736 else
737 path = strchr(end, separator);
738
739 if (!path || !*path)
740 die("No path specified. See 'man git-pull' for valid url syntax");
741
742 /*
743 * null-terminate hostname and point path to ~ for URL's like this:
744 * ssh://host.xz/~user/repo
745 */
746
747 end = path; /* Need to \0 terminate host here */
748 if (separator == ':')
749 path++; /* path starts after ':' */
750 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
751 if (path[1] == '~')
752 path++;
753 }
754
755 path = xstrdup(path);
756 *end = '\0';
757
758 *ret_host = xstrdup(host);
759 *ret_path = path;
760 free(url);
761 return protocol;
762 }
763
764 static struct child_process no_fork = CHILD_PROCESS_INIT;
765
766 static const char *get_ssh_command(void)
767 {
768 const char *ssh;
769
770 if ((ssh = getenv("GIT_SSH_COMMAND")))
771 return ssh;
772
773 if (!git_config_get_string_const("core.sshcommand", &ssh))
774 return ssh;
775
776 return NULL;
777 }
778
779 enum ssh_variant {
780 VARIANT_SIMPLE,
781 VARIANT_SSH,
782 VARIANT_PLINK,
783 VARIANT_PUTTY,
784 VARIANT_TORTOISEPLINK,
785 };
786
787 static int override_ssh_variant(enum ssh_variant *ssh_variant)
788 {
789 const char *variant = getenv("GIT_SSH_VARIANT");
790
791 if (!variant && git_config_get_string_const("ssh.variant", &variant))
792 return 0;
793
794 if (!strcmp(variant, "plink"))
795 *ssh_variant = VARIANT_PLINK;
796 else if (!strcmp(variant, "putty"))
797 *ssh_variant = VARIANT_PUTTY;
798 else if (!strcmp(variant, "tortoiseplink"))
799 *ssh_variant = VARIANT_TORTOISEPLINK;
800 else if (!strcmp(variant, "simple"))
801 *ssh_variant = VARIANT_SIMPLE;
802 else
803 *ssh_variant = VARIANT_SSH;
804
805 return 1;
806 }
807
808 static enum ssh_variant determine_ssh_variant(const char *ssh_command,
809 int is_cmdline)
810 {
811 enum ssh_variant ssh_variant = VARIANT_SIMPLE;
812 const char *variant;
813 char *p = NULL;
814
815 if (override_ssh_variant(&ssh_variant))
816 return ssh_variant;
817
818 if (!is_cmdline) {
819 p = xstrdup(ssh_command);
820 variant = basename(p);
821 } else {
822 const char **ssh_argv;
823
824 p = xstrdup(ssh_command);
825 if (split_cmdline(p, &ssh_argv) > 0) {
826 variant = basename((char *)ssh_argv[0]);
827 /*
828 * At this point, variant points into the buffer
829 * referenced by p, hence we do not need ssh_argv
830 * any longer.
831 */
832 free(ssh_argv);
833 } else {
834 free(p);
835 return ssh_variant;
836 }
837 }
838
839 if (!strcasecmp(variant, "ssh") ||
840 !strcasecmp(variant, "ssh.exe"))
841 ssh_variant = VARIANT_SSH;
842 else if (!strcasecmp(variant, "plink") ||
843 !strcasecmp(variant, "plink.exe"))
844 ssh_variant = VARIANT_PLINK;
845 else if (!strcasecmp(variant, "tortoiseplink") ||
846 !strcasecmp(variant, "tortoiseplink.exe"))
847 ssh_variant = VARIANT_TORTOISEPLINK;
848
849 free(p);
850 return ssh_variant;
851 }
852
853 /*
854 * This returns a dummy child_process if the transport protocol does not
855 * need fork(2), or a struct child_process object if it does. Once done,
856 * finish the connection with finish_connect() with the value returned from
857 * this function (it is safe to call finish_connect() with NULL to support
858 * the former case).
859 *
860 * If it returns, the connect is successful; it just dies on errors (this
861 * will hopefully be changed in a libification effort, to return NULL when
862 * the connection failed).
863 */
864 struct child_process *git_connect(int fd[2], const char *url,
865 const char *prog, int flags)
866 {
867 char *hostandport, *path;
868 struct child_process *conn = &no_fork;
869 enum protocol protocol;
870
871 /* Without this we cannot rely on waitpid() to tell
872 * what happened to our children.
873 */
874 signal(SIGCHLD, SIG_DFL);
875
876 protocol = parse_connect_url(url, &hostandport, &path);
877 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
878 printf("Diag: url=%s\n", url ? url : "NULL");
879 printf("Diag: protocol=%s\n", prot_name(protocol));
880 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
881 printf("Diag: path=%s\n", path ? path : "NULL");
882 conn = NULL;
883 } else if (protocol == PROTO_GIT) {
884 struct strbuf request = STRBUF_INIT;
885 /*
886 * Set up virtual host information based on where we will
887 * connect, unless the user has overridden us in
888 * the environment.
889 */
890 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
891 if (target_host)
892 target_host = xstrdup(target_host);
893 else
894 target_host = xstrdup(hostandport);
895
896 transport_check_allowed("git");
897
898 /* These underlying connection commands die() if they
899 * cannot connect.
900 */
901 if (git_use_proxy(hostandport))
902 conn = git_proxy_connect(fd, hostandport);
903 else
904 git_tcp_connect(fd, hostandport, flags);
905 /*
906 * Separate original protocol components prog and path
907 * from extended host header with a NUL byte.
908 *
909 * Note: Do not add any other headers here! Doing so
910 * will cause older git-daemon servers to crash.
911 */
912 strbuf_addf(&request,
913 "%s %s%chost=%s%c",
914 prog, path, 0,
915 target_host, 0);
916
917 /* If using a new version put that stuff here after a second null byte */
918 if (get_protocol_version_config() > 0) {
919 strbuf_addch(&request, '\0');
920 strbuf_addf(&request, "version=%d%c",
921 get_protocol_version_config(), '\0');
922 }
923
924 packet_write(fd[1], request.buf, request.len);
925
926 free(target_host);
927 strbuf_release(&request);
928 } else {
929 struct strbuf cmd = STRBUF_INIT;
930 const char *const *var;
931
932 conn = xmalloc(sizeof(*conn));
933 child_process_init(conn);
934
935 if (looks_like_command_line_option(path))
936 die("strange pathname '%s' blocked", path);
937
938 strbuf_addstr(&cmd, prog);
939 strbuf_addch(&cmd, ' ');
940 sq_quote_buf(&cmd, path);
941
942 /* remove repo-local variables from the environment */
943 for (var = local_repo_env; *var; var++)
944 argv_array_push(&conn->env_array, *var);
945
946 conn->use_shell = 1;
947 conn->in = conn->out = -1;
948 if (protocol == PROTO_SSH) {
949 const char *ssh;
950 enum ssh_variant variant;
951 char *ssh_host = hostandport;
952 const char *port = NULL;
953 transport_check_allowed("ssh");
954 get_host_and_port(&ssh_host, &port);
955
956 if (!port)
957 port = get_port(ssh_host);
958
959 if (flags & CONNECT_DIAG_URL) {
960 printf("Diag: url=%s\n", url ? url : "NULL");
961 printf("Diag: protocol=%s\n", prot_name(protocol));
962 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
963 printf("Diag: port=%s\n", port ? port : "NONE");
964 printf("Diag: path=%s\n", path ? path : "NULL");
965
966 free(hostandport);
967 free(path);
968 free(conn);
969 strbuf_release(&cmd);
970 return NULL;
971 }
972
973 if (looks_like_command_line_option(ssh_host))
974 die("strange hostname '%s' blocked", ssh_host);
975
976 ssh = get_ssh_command();
977 if (ssh) {
978 variant = determine_ssh_variant(ssh, 1);
979 } else {
980 /*
981 * GIT_SSH is the no-shell version of
982 * GIT_SSH_COMMAND (and must remain so for
983 * historical compatibility).
984 */
985 conn->use_shell = 0;
986
987 ssh = getenv("GIT_SSH");
988 if (!ssh)
989 ssh = "ssh";
990 variant = determine_ssh_variant(ssh, 0);
991 }
992
993 argv_array_push(&conn->args, ssh);
994
995 if (variant == VARIANT_SSH &&
996 get_protocol_version_config() > 0) {
997 argv_array_push(&conn->args, "-o");
998 argv_array_push(&conn->args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
999 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1000 get_protocol_version_config());
1001 }
1002
1003 if (variant != VARIANT_SIMPLE) {
1004 if (flags & CONNECT_IPV4)
1005 argv_array_push(&conn->args, "-4");
1006 else if (flags & CONNECT_IPV6)
1007 argv_array_push(&conn->args, "-6");
1008 }
1009
1010 if (variant == VARIANT_TORTOISEPLINK)
1011 argv_array_push(&conn->args, "-batch");
1012
1013 if (port && variant != VARIANT_SIMPLE) {
1014 if (variant == VARIANT_SSH)
1015 argv_array_push(&conn->args, "-p");
1016 else
1017 argv_array_push(&conn->args, "-P");
1018
1019 argv_array_push(&conn->args, port);
1020 }
1021
1022 argv_array_push(&conn->args, ssh_host);
1023 } else {
1024 transport_check_allowed("file");
1025 if (get_protocol_version_config() > 0) {
1026 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1027 get_protocol_version_config());
1028 }
1029 }
1030 argv_array_push(&conn->args, cmd.buf);
1031
1032 if (start_command(conn))
1033 die("unable to fork");
1034
1035 fd[0] = conn->out; /* read from child's stdout */
1036 fd[1] = conn->in; /* write to child's stdin */
1037 strbuf_release(&cmd);
1038 }
1039 free(hostandport);
1040 free(path);
1041 return conn;
1042 }
1043
1044 int git_connection_is_socket(struct child_process *conn)
1045 {
1046 return conn == &no_fork;
1047 }
1048
1049 int finish_connect(struct child_process *conn)
1050 {
1051 int code;
1052 if (!conn || git_connection_is_socket(conn))
1053 return 0;
1054
1055 code = finish_command(conn);
1056 free(conn);
1057 return code;
1058 }