THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1332 lines
32KB

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