connect.c 35 KB

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