fish.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /* Virtual File System: FISH implementation for transfering files over
  2. shell connections.
  3. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  4. 2007 Free Software Foundation, Inc.
  5. Written by: 1998 Pavel Machek
  6. Spaces fix: 2000 Michal Svec
  7. Derived from ftpfs.c.
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public License
  10. as published by the Free Software Foundation; either version 2 of
  11. the License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU Library General Public License for more details.
  16. You should have received a copy of the GNU Library General Public
  17. License along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  19. /*
  20. * Read README.fish for protocol specification.
  21. *
  22. * Syntax of path is: /#sh:user@host[:Cr]/path
  23. * where C means you want compressed connection,
  24. * and r means you want to use rsh
  25. *
  26. * Namespace: fish_vfs_ops exported.
  27. */
  28. /* Define this if your ssh can take -I option */
  29. #include <config.h>
  30. #include <errno.h>
  31. #include "../src/global.h"
  32. #include "../src/tty.h" /* enable/disable interrupt key */
  33. #include "../src/wtools.h" /* message() */
  34. #include "../src/main.h" /* print_vfs_message */
  35. #include "../src/util.h"
  36. #include "utilvfs.h"
  37. #include "xdirentry.h"
  38. #include "vfs.h"
  39. #include "vfs-impl.h"
  40. #include "gc.h" /* vfs_stamp_create */
  41. #include "tcputil.h"
  42. #include "../src/unixcompat.h"
  43. #include "fish.h"
  44. int fish_directory_timeout = 900;
  45. #define DO_RESOLVE_SYMLINK 1
  46. #define DO_OPEN 2
  47. #define DO_FREE_RESOURCE 4
  48. #define FISH_FLAG_COMPRESSED 1
  49. #define FISH_FLAG_RSH 2
  50. #define OPT_FLUSH 1
  51. #define OPT_IGNORE_ERROR 2
  52. /*
  53. * Reply codes.
  54. */
  55. #define PRELIM 1 /* positive preliminary */
  56. #define COMPLETE 2 /* positive completion */
  57. #define CONTINUE 3 /* positive intermediate */
  58. #define TRANSIENT 4 /* transient negative completion */
  59. #define ERROR 5 /* permanent negative completion */
  60. /* command wait_flag: */
  61. #define NONE 0x00
  62. #define WAIT_REPLY 0x01
  63. #define WANT_STRING 0x02
  64. static char reply_str [80];
  65. static struct vfs_class vfs_fish_ops;
  66. static int
  67. fish_command (struct vfs_class *me, struct vfs_s_super *super,
  68. int wait_reply, const char *fmt, ...)
  69. __attribute__ ((format (__printf__, 4, 5)));
  70. static int fish_decode_reply (char *s, int was_garbage)
  71. {
  72. int code;
  73. if (!sscanf(s, "%d", &code)) {
  74. code = 500;
  75. return 5;
  76. }
  77. if (code<100) return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
  78. return code / 100;
  79. }
  80. /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
  81. static int fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
  82. {
  83. char answer[1024];
  84. int was_garbage = 0;
  85. for (;;) {
  86. if (!vfs_s_get_line(me, sock, answer, sizeof(answer), '\n')) {
  87. if (string_buf)
  88. *string_buf = 0;
  89. return 4;
  90. }
  91. if (strncmp(answer, "### ", 4)) {
  92. was_garbage = 1;
  93. if (string_buf)
  94. g_strlcpy(string_buf, answer, string_len);
  95. } else return fish_decode_reply(answer+4, was_garbage);
  96. }
  97. }
  98. #define SUP super->u.fish
  99. static int
  100. fish_command (struct vfs_class *me, struct vfs_s_super *super,
  101. int wait_reply, const char *fmt, ...)
  102. {
  103. va_list ap;
  104. char *str;
  105. int status;
  106. FILE *logfile = MEDATA->logfile;
  107. va_start (ap, fmt);
  108. str = g_strdup_vprintf (fmt, ap);
  109. va_end (ap);
  110. if (logfile) {
  111. fwrite (str, strlen (str), 1, logfile);
  112. fflush (logfile);
  113. }
  114. enable_interrupt_key ();
  115. status = write (SUP.sockw, str, strlen (str));
  116. g_free (str);
  117. disable_interrupt_key ();
  118. if (status < 0)
  119. return TRANSIENT;
  120. if (wait_reply)
  121. return fish_get_reply (me, SUP.sockr,
  122. (wait_reply & WANT_STRING) ? reply_str :
  123. NULL, sizeof (reply_str) - 1);
  124. return COMPLETE;
  125. }
  126. static void
  127. fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
  128. {
  129. if ((SUP.sockw != -1) || (SUP.sockr != -1)) {
  130. print_vfs_message (_("fish: Disconnecting from %s"),
  131. super->name ? super->name : "???");
  132. fish_command (me, super, NONE, "#BYE\nexit\n");
  133. close (SUP.sockw);
  134. close (SUP.sockr);
  135. SUP.sockw = SUP.sockr = -1;
  136. }
  137. g_free (SUP.host);
  138. g_free (SUP.user);
  139. g_free (SUP.cwdir);
  140. g_free (SUP.password);
  141. }
  142. static void
  143. fish_pipeopen(struct vfs_s_super *super, const char *path, const char *argv[])
  144. {
  145. int fileset1[2], fileset2[2];
  146. int res;
  147. if ((pipe(fileset1)<0) || (pipe(fileset2)<0))
  148. vfs_die("Cannot pipe(): %m.");
  149. if ((res = fork())) {
  150. if (res<0) vfs_die("Cannot fork(): %m.");
  151. /* We are the parent */
  152. close(fileset1[0]);
  153. SUP.sockw = fileset1[1];
  154. close(fileset2[1]);
  155. SUP.sockr = fileset2[0];
  156. } else {
  157. close(0);
  158. dup(fileset1[0]);
  159. close(fileset1[0]); close(fileset1[1]);
  160. close(1); close(2);
  161. dup(fileset2[1]);
  162. /* stderr to /dev/null */
  163. open ("/dev/null", O_WRONLY);
  164. close(fileset2[0]); close(fileset2[1]);
  165. execvp(path, const_cast(char **, argv));
  166. _exit(3);
  167. }
  168. }
  169. /* The returned directory should always contain a trailing slash */
  170. static char *fish_getcwd(struct vfs_class *me, struct vfs_s_super *super)
  171. {
  172. if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
  173. return g_strconcat (reply_str, "/", (char *) NULL);
  174. ERRNOR (EIO, NULL);
  175. }
  176. static int
  177. fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
  178. {
  179. {
  180. char gbuf[10];
  181. const char *argv[10]; /* All of 10 is used now */
  182. const char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
  183. int i = 0;
  184. argv[i++] = xsh;
  185. if (SUP.flags == FISH_FLAG_COMPRESSED)
  186. argv[i++] = "-C";
  187. if (SUP.flags > FISH_FLAG_RSH)
  188. {
  189. argv[i++] = "-p";
  190. g_snprintf (gbuf, sizeof (gbuf), "%d", SUP.flags);
  191. argv[i++] = gbuf;
  192. }
  193. argv[i++] = "-l";
  194. argv[i++] = SUP.user;
  195. argv[i++] = SUP.host;
  196. argv[i++] = "echo FISH:; /bin/sh";
  197. argv[i++] = NULL;
  198. fish_pipeopen (super, xsh, argv);
  199. }
  200. {
  201. char answer[2048];
  202. print_vfs_message (_("fish: Waiting for initial line..."));
  203. if (!vfs_s_get_line (me, SUP.sockr, answer, sizeof (answer), ':'))
  204. ERRNOR (E_PROTO, -1);
  205. print_vfs_message ("%s", answer);
  206. if (strstr (answer, "assword")) {
  207. /* Currently, this does not work. ssh reads passwords from
  208. /dev/tty, not from stdin :-(. */
  209. message (D_ERROR, MSG_ERROR,
  210. _
  211. ("Sorry, we cannot do password authenticated connections for now."));
  212. ERRNOR (EPERM, -1);
  213. if (!SUP.password) {
  214. char *p, *op;
  215. p = g_strconcat (_(" fish: Password required for "),
  216. SUP.user, " ", (char *) NULL);
  217. op = vfs_get_password (p);
  218. g_free (p);
  219. if (op == NULL)
  220. ERRNOR (EPERM, -1);
  221. SUP.password = op;
  222. }
  223. print_vfs_message (_("fish: Sending password..."));
  224. write (SUP.sockw, SUP.password, strlen (SUP.password));
  225. write (SUP.sockw, "\n", 1);
  226. }
  227. }
  228. print_vfs_message (_("fish: Sending initial line..."));
  229. /*
  230. * Run `start_fish_server'. If it doesn't exist - no problem,
  231. * we'll talk directly to the shell.
  232. */
  233. if (fish_command
  234. (me, super, WAIT_REPLY,
  235. "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") !=
  236. COMPLETE)
  237. ERRNOR (E_PROTO, -1);
  238. print_vfs_message (_("fish: Handshaking version..."));
  239. if (fish_command
  240. (me, super, WAIT_REPLY,
  241. "#VER 0.0.0\necho '### 000'\n") != COMPLETE)
  242. ERRNOR (E_PROTO, -1);
  243. /* Set up remote locale to C, otherwise dates cannot be recognized */
  244. if (fish_command
  245. (me, super, WAIT_REPLY,
  246. "LANG=C; LC_ALL=C; LC_TIME=C\n"
  247. "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n")
  248. != COMPLETE)
  249. ERRNOR (E_PROTO, -1);
  250. print_vfs_message (_("fish: Setting up current directory..."));
  251. SUP.cwdir = fish_getcwd (me, super);
  252. print_vfs_message (_("fish: Connected, home %s."), SUP.cwdir);
  253. #if 0
  254. super->name =
  255. g_strconcat ("/#sh:", SUP.user, "@", SUP.host, "/", (char *) NULL);
  256. #endif
  257. super->name = g_strdup (PATH_SEP_STR);
  258. super->root =
  259. vfs_s_new_inode (me, super,
  260. vfs_s_default_stat (me, S_IFDIR | 0755));
  261. return 0;
  262. }
  263. static int
  264. fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
  265. const char *archive_name, char *op)
  266. {
  267. char *host, *user, *password, *p;
  268. int flags;
  269. (void) archive_name;
  270. p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
  271. &password, 0, URL_NOSLASH);
  272. g_free (p);
  273. SUP.host = host;
  274. SUP.user = user;
  275. SUP.flags = flags;
  276. if (!strncmp (op, "rsh:", 4))
  277. SUP.flags = FISH_FLAG_RSH;
  278. SUP.cwdir = NULL;
  279. if (password)
  280. SUP.password = password;
  281. return fish_open_archive_int (me, super);
  282. }
  283. static int
  284. fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
  285. const char *archive_name, char *op, void *cookie)
  286. {
  287. char *host, *user;
  288. int flags;
  289. (void) me;
  290. (void) archive_name;
  291. (void) cookie;
  292. op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
  293. URL_NOSLASH);
  294. g_free (op);
  295. flags = ((strcmp (host, SUP.host) == 0)
  296. && (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
  297. g_free (host);
  298. g_free (user);
  299. return flags;
  300. }
  301. static int
  302. fish_dir_load(struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
  303. {
  304. struct vfs_s_super *super = dir->super;
  305. char buffer[8192];
  306. struct vfs_s_entry *ent = NULL;
  307. FILE *logfile;
  308. char *quoted_path;
  309. int reply_code;
  310. gchar *shell_commands;
  311. #if 0
  312. /*
  313. * Simple FISH debug interface :]
  314. */
  315. if (!(MEDATA->logfile))
  316. {
  317. MEDATA->logfile = fopen("/tmp/mc-FISH.sh", "w");
  318. }
  319. #endif /* 0 */
  320. logfile = MEDATA->logfile;
  321. print_vfs_message(_("fish: Reading directory %s..."), remote_path);
  322. gettimeofday(&dir->timestamp, NULL);
  323. dir->timestamp.tv_sec += fish_directory_timeout;
  324. quoted_path = shell_escape (remote_path);
  325. shell_commands = g_strconcat(
  326. "#LIST /%s\n"
  327. "if `perl -v > /dev/null 2>&1` ; then\n"
  328. "perl -e '\n"
  329. "use strict;\n"
  330. "use POSIX;\n"
  331. "use Fcntl;\n"
  332. "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
  333. "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
  334. "my $dirname = $ARGV[0];\n"
  335. "if (opendir ( DIR, $dirname )) {\n"
  336. "while( (my $filename = readdir(DIR))){\n"
  337. "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
  338. "my $mloctime= scalar localtime $mtime;\n"
  339. ,
  340. "my $shell_escape_regex= s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
  341. "my $e_filename = $filename;\n"
  342. "$e_filename =~ $shell_escape_regex;\n"
  343. "if (S_ISLNK($mode) ) {\n"
  344. "my $linkname = readlink (\"$dirname/$filename\");\n"
  345. "$linkname =~ $shell_escape_regex;\n"
  346. "\n"
  347. "printf(\"R%%o %%o $uid.$gid\\n"
  348. "S$size\\n"
  349. "d$mloctime\\n"
  350. ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
  351. "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
  352. "} else {\n"
  353. "printf(\"R%%o %%o $uid.$gid\\n"
  354. "S$size\\n"
  355. "d$mloctime\\n"
  356. ":\\\"$e_filename\\\"\\n"
  357. "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
  358. "}}\n"
  359. ,
  360. "printf(\"### 200\\n\");\n"
  361. "closedir(DIR);\n"
  362. "} else {\n"
  363. "printf(\"### 500\\n\");\n"
  364. "}\n"
  365. "exit 0\n"
  366. "' /%s ||\n" /* ARGV[0] - path to browse */
  367. " echo '### 500'\n" /* do not hang if perl is to eval it */
  368. "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
  369. "if `ls -Q /%s >/dev/null 2>&1`; then\n"
  370. "LSOPT=\"-Qlan\";\n"
  371. "ADD=0;\n"
  372. "else\n"
  373. "LSOPT=\"-lan\";\n"
  374. "ADD=1;\n"
  375. "fi\n"
  376. "ls $LSOPT \"/%s\" 2>/dev/null | grep '^[^cbt]' | (\n"
  377. "while read p l u g s m d y n; do\n"
  378. "if [ $ADD = 0 ]; then\n"
  379. "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
  380. "elif `sed --version >/dev/null 2>&1` ; then\n"
  381. "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
  382. ,
  383. "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
  384. "else\n"
  385. "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
  386. "fi\n"
  387. "done )\n"
  388. "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
  389. "while read p l u g a i m d y n; do\n"
  390. "if [ $ADD = 0 ]; then\n"
  391. "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
  392. "elif `sed --version >/dev/null 2>&1` ; then\n"
  393. "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
  394. "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
  395. "else\n"
  396. "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
  397. "fi\n"
  398. "done)\n"
  399. "echo '### 200'\n"
  400. "else\n"
  401. "echo '### 500'\n"
  402. "fi\n"
  403. ,
  404. NULL
  405. );
  406. fish_command (me, super, NONE, shell_commands,
  407. quoted_path, quoted_path, quoted_path, quoted_path, quoted_path, quoted_path);
  408. g_free(shell_commands);
  409. g_free (quoted_path);
  410. ent = vfs_s_generate_entry(me, NULL, dir, 0);
  411. while (1) {
  412. int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
  413. if ((!res) || (res == EINTR)) {
  414. vfs_s_free_entry(me, ent);
  415. me->verrno = ECONNRESET;
  416. goto error;
  417. }
  418. if (logfile) {
  419. fputs (buffer, logfile);
  420. fputs ("\n", logfile);
  421. fflush (logfile);
  422. }
  423. if (!strncmp(buffer, "### ", 4))
  424. break;
  425. if ((!buffer[0])) {
  426. if (ent->name) {
  427. vfs_s_insert_entry(me, dir, ent);
  428. ent = vfs_s_generate_entry(me, NULL, dir, 0);
  429. }
  430. continue;
  431. }
  432. #define ST ent->ino->st
  433. switch(buffer[0]) {
  434. case ':': {
  435. char *data_start = buffer+1;
  436. char *filename = data_start;
  437. char *linkname = data_start;
  438. char *filename_bound = filename + strlen(filename);
  439. char *linkname_bound = filename_bound;
  440. if (!strcmp(data_start, "\".\"") || !strcmp(data_start, "\"..\""))
  441. break; /* We'll do "." and ".." ourselves */
  442. if (S_ISLNK(ST.st_mode)) {
  443. /* we expect: "escaped-name" -> "escaped-name"
  444. // -> cannot occur in filenames,
  445. // because it will be escaped to -\> */
  446. if (*filename == '"')
  447. ++filename;
  448. linkname = strstr(filename, "\" -> \"");
  449. if (!linkname)
  450. {
  451. /* broken client, or smth goes wrong */
  452. linkname = filename_bound;
  453. if (filename_bound > filename
  454. && *(filename_bound - 1) == '"')
  455. --filename_bound; /* skip trailing " */
  456. }
  457. else
  458. {
  459. filename_bound = linkname;
  460. linkname += 6; /* strlen ("\" -> \"") */
  461. if (*(linkname_bound - 1) == '"')
  462. --linkname_bound; /* skip trailing " */
  463. }
  464. ent->name = str_dup_range(filename, filename_bound);
  465. shell_unescape(ent->name);
  466. ent->ino->linkname = str_dup_range(linkname, linkname_bound);
  467. shell_unescape(ent->ino->linkname);
  468. } else {
  469. /* we expect: "escaped-name" */
  470. if (filename_bound - filename > 2)
  471. {
  472. /*
  473. there is at least 2 "
  474. and we skip them
  475. */
  476. if (*filename == '"')
  477. ++filename;
  478. if (*(filename_bound - 1) == '"')
  479. --filename_bound;
  480. }
  481. ent->name = str_dup_range(filename, filename_bound);
  482. shell_unescape(ent->name);
  483. }
  484. break;
  485. }
  486. case 'S':
  487. #ifdef HAVE_ATOLL
  488. ST.st_size = (off_t) atoll (buffer+1);
  489. #else
  490. ST.st_size = (off_t) atof (buffer+1);
  491. #endif
  492. break;
  493. case 'P': {
  494. size_t skipped;
  495. vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
  496. break;
  497. }
  498. case 'R': {
  499. /*
  500. raw filemode:
  501. we expect: Roctal-filemode octal-filetype uid.gid
  502. */
  503. size_t skipped;
  504. vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
  505. break;
  506. }
  507. case 'd': {
  508. vfs_split_text(buffer+1);
  509. if (!vfs_parse_filedate(0, &ST.st_ctime))
  510. break;
  511. ST.st_atime = ST.st_mtime = ST.st_ctime;
  512. }
  513. break;
  514. case 'D': {
  515. struct tm tim;
  516. if (sscanf(buffer+1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
  517. &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
  518. break;
  519. ST.st_atime = ST.st_mtime = ST.st_ctime = mktime(&tim);
  520. }
  521. break;
  522. case 'E': {
  523. int maj, min;
  524. if (sscanf(buffer+1, "%d,%d", &maj, &min) != 2)
  525. break;
  526. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  527. ST.st_rdev = makedev (maj, min);
  528. #endif
  529. }
  530. }
  531. }
  532. vfs_s_free_entry (me, ent);
  533. reply_code = fish_decode_reply(buffer + 4, 0);
  534. if (reply_code == COMPLETE) {
  535. g_free (SUP.cwdir);
  536. SUP.cwdir = g_strdup (remote_path);
  537. print_vfs_message (_("%s: done."), me->name);
  538. return 0;
  539. } else if (reply_code == ERROR) {
  540. me->verrno = EACCES;
  541. } else {
  542. me->verrno = E_REMOTE;
  543. }
  544. error:
  545. print_vfs_message (_("%s: failure"), me->name);
  546. return -1;
  547. }
  548. static int
  549. fish_file_store(struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
  550. {
  551. struct vfs_s_super *super = FH_SUPER;
  552. int n, total;
  553. char buffer[8192];
  554. struct stat s;
  555. int was_error = 0;
  556. int h;
  557. char *quoted_name;
  558. h = open (localname, O_RDONLY);
  559. if (h == -1)
  560. ERRNOR (EIO, -1);
  561. if (fstat(h, &s)<0) {
  562. close (h);
  563. ERRNOR (EIO, -1);
  564. }
  565. /* First, try this as stor:
  566. *
  567. * ( head -c number ) | ( cat > file; cat >/dev/null )
  568. *
  569. * If `head' is not present on the remote system, `dd' will be used.
  570. * Unfortunately, we cannot trust most non-GNU `head' implementations
  571. * even if `-c' options is supported. Therefore, we separate GNU head
  572. * (and other modern heads?) using `-q' and `-' . This causes another
  573. * implementations to fail (because of "incorrect options").
  574. *
  575. * Fallback is:
  576. *
  577. * rest=<number>
  578. * while [ $rest -gt 0 ]
  579. * do
  580. * cnt=`expr \( $rest + 255 \) / 256`
  581. * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
  582. * rest=`expr $rest - $n`
  583. * done
  584. *
  585. * `dd' was not designed for full filling of input buffers,
  586. * and does not report exact number of bytes (not blocks).
  587. * Therefore a more complex shell script is needed.
  588. *
  589. * On some systems non-GNU head writes "Usage:" error report to stdout
  590. * instead of stderr. It makes impossible the use of "head || dd"
  591. * algorithm for file appending case, therefore just "dd" is used for it.
  592. */
  593. quoted_name = shell_escape(name);
  594. print_vfs_message(_("fish: store %s: sending command..."), quoted_name );
  595. /* FIXME: File size is limited to ULONG_MAX */
  596. if (!fh->u.fish.append)
  597. n = fish_command (me, super, WAIT_REPLY,
  598. "#STOR %lu /%s\n"
  599. "echo '### 001'\n"
  600. "file=/%s\n"
  601. "res=`exec 3>&1\n"
  602. "(\n"
  603. "head -c %lu -q - || echo DD >&3\n"
  604. ") 2>/dev/null | (\n"
  605. "cat > $file\n"
  606. "cat > /dev/null\n"
  607. ")`; [ \"$res\" = DD ] && {\n"
  608. "> \"$file\"\n"
  609. "rest=%lu\n"
  610. "while [ $rest -gt 0 ]\n"
  611. "do\n"
  612. " cnt=`expr \\( $rest + 255 \\) / 256`\n"
  613. " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
  614. " rest=`expr $rest - $n`\n"
  615. "done\n"
  616. "}; echo '### 200'\n",
  617. (unsigned long) s.st_size, quoted_name,
  618. quoted_name, (unsigned long) s.st_size,
  619. (unsigned long) s.st_size);
  620. else
  621. n = fish_command (me, super, WAIT_REPLY,
  622. "#STOR %lu /%s\n"
  623. "echo '### 001'\n"
  624. "{\n"
  625. "file=/%s\n"
  626. "rest=%lu\n"
  627. "while [ $rest -gt 0 ]\n"
  628. "do\n"
  629. " cnt=`expr \\( $rest + 255 \\) / 256`\n"
  630. " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
  631. " rest=`expr $rest - $n`\n"
  632. "done\n"
  633. "}; echo '### 200'\n",
  634. (unsigned long) s.st_size, quoted_name,
  635. quoted_name, (unsigned long) s.st_size);
  636. if (n != PRELIM) {
  637. close (h);
  638. ERRNOR(E_REMOTE, -1);
  639. }
  640. total = 0;
  641. while (1) {
  642. int t;
  643. while ((n = read(h, buffer, sizeof(buffer))) < 0) {
  644. if ((errno == EINTR) && got_interrupt())
  645. continue;
  646. print_vfs_message(_("fish: Local read failed, sending zeros") );
  647. close(h);
  648. h = open( "/dev/zero", O_RDONLY );
  649. }
  650. if (n == 0)
  651. break;
  652. if ((t = write (SUP.sockw, buffer, n)) != n) {
  653. if (t == -1) {
  654. me->verrno = errno;
  655. } else {
  656. me->verrno = EIO;
  657. }
  658. goto error_return;
  659. }
  660. disable_interrupt_key();
  661. total += n;
  662. print_vfs_message(_("fish: storing %s %d (%lu)"),
  663. was_error ? _("zeros") : _("file"), total,
  664. (unsigned long) s.st_size);
  665. }
  666. close(h);
  667. g_free(quoted_name);
  668. if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
  669. ERRNOR (E_REMOTE, -1);
  670. return 0;
  671. error_return:
  672. close(h);
  673. fish_get_reply(me, SUP.sockr, NULL, 0);
  674. g_free(quoted_name);
  675. return -1;
  676. }
  677. static int
  678. fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
  679. {
  680. char *name;
  681. char *quoted_name;
  682. if (offset)
  683. ERRNOR (E_NOTSUPP, 0);
  684. name = vfs_s_fullpath (me, fh->ino);
  685. if (!name)
  686. return 0;
  687. quoted_name = shell_escape(name);
  688. fh->u.fish.append = 0;
  689. /*
  690. * Check whether the remote file is readable by using `dd' to copy
  691. * a single byte from the remote file to /dev/null. If `dd' completes
  692. * with exit status of 0 use `cat' to send the file contents to the
  693. * standard output (i.e. over the network).
  694. */
  695. offset = fish_command (me, FH_SUPER, WANT_STRING,
  696. "#RETR /%s\n"
  697. "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
  698. "then\n"
  699. "ls -ln /%s 2>/dev/null | (\n"
  700. "read p l u g s r\n"
  701. "echo $s\n"
  702. ")\n"
  703. "echo '### 100'\n"
  704. "cat /%s\n"
  705. "echo '### 200'\n"
  706. "else\n"
  707. "echo '### 500'\n"
  708. "fi\n",
  709. quoted_name, quoted_name, quoted_name, quoted_name );
  710. g_free (quoted_name);
  711. if (offset != PRELIM) ERRNOR (E_REMOTE, 0);
  712. fh->linear = LS_LINEAR_OPEN;
  713. fh->u.fish.got = 0;
  714. errno = 0;
  715. #if SIZEOF_OFF_T == SIZEOF_LONG
  716. fh->u.fish.total = strtol (reply_str, NULL, 10);
  717. #else
  718. fh->u.fish.total = strtoll (reply_str, NULL, 10);
  719. #endif
  720. if (errno != 0)
  721. ERRNOR (E_REMOTE, 0);
  722. return 1;
  723. }
  724. static void
  725. fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
  726. {
  727. struct vfs_s_super *super = FH_SUPER;
  728. char buffer[8192];
  729. int n;
  730. print_vfs_message( _("Aborting transfer...") );
  731. do {
  732. n = MIN(8192, fh->u.fish.total - fh->u.fish.got);
  733. if (n) {
  734. if ((n = read(SUP.sockr, buffer, n)) < 0)
  735. return;
  736. fh->u.fish.got += n;
  737. }
  738. } while (n);
  739. if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
  740. print_vfs_message( _("Error reported after abort.") );
  741. else
  742. print_vfs_message( _("Aborted transfer would be successful.") );
  743. }
  744. static int
  745. fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, int len)
  746. {
  747. struct vfs_s_super *super = FH_SUPER;
  748. int n = 0;
  749. len = MIN( fh->u.fish.total - fh->u.fish.got, len );
  750. disable_interrupt_key();
  751. while (len && ((n = read (SUP.sockr, buf, len))<0)) {
  752. if ((errno == EINTR) && !got_interrupt())
  753. continue;
  754. break;
  755. }
  756. enable_interrupt_key();
  757. if (n>0) fh->u.fish.got += n;
  758. if (n<0) fish_linear_abort(me, fh);
  759. if ((!n) && ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)))
  760. ERRNOR (E_REMOTE, -1);
  761. ERRNOR (errno, n);
  762. }
  763. static void
  764. fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
  765. {
  766. if (fh->u.fish.total != fh->u.fish.got)
  767. fish_linear_abort(me, fh);
  768. }
  769. static int
  770. fish_ctl (void *fh, int ctlop, void *arg)
  771. {
  772. (void) arg;
  773. (void) fh;
  774. (void) ctlop;
  775. return 0;
  776. #if 0
  777. switch (ctlop) {
  778. case VFS_CTL_IS_NOTREADY:
  779. {
  780. int v;
  781. if (!FH->linear)
  782. vfs_die ("You may not do this");
  783. if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
  784. return 0;
  785. v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
  786. if (((v < 0) && (errno == EINTR)) || v == 0)
  787. return 1;
  788. return 0;
  789. }
  790. default:
  791. return 0;
  792. }
  793. #endif
  794. }
  795. static int
  796. fish_send_command(struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
  797. {
  798. int r;
  799. r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
  800. vfs_stamp_create (&vfs_fish_ops, super);
  801. if (r != COMPLETE) ERRNOR (E_REMOTE, -1);
  802. if (flags & OPT_FLUSH)
  803. vfs_s_invalidate(me, super);
  804. return 0;
  805. }
  806. #define PREFIX \
  807. char buf[BUF_LARGE]; \
  808. const char *crpath; \
  809. char *rpath, *mpath = g_strdup (path); \
  810. struct vfs_s_super *super; \
  811. if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) { \
  812. g_free (mpath); \
  813. return -1; \
  814. } \
  815. rpath = shell_escape(crpath); \
  816. g_free (mpath);
  817. #define POSTFIX(flags) \
  818. g_free (rpath); \
  819. return fish_send_command(me, super, buf, flags);
  820. static int
  821. fish_chmod (struct vfs_class *me, const char *path, int mode)
  822. {
  823. PREFIX
  824. g_snprintf(buf, sizeof(buf), "#CHMOD %4.4o /%s\n"
  825. "chmod %4.4o /%s 2>/dev/null\n"
  826. "echo '### 000'\n",
  827. mode & 07777, rpath,
  828. mode & 07777, rpath);
  829. POSTFIX(OPT_FLUSH);
  830. }
  831. #define FISH_OP(name, string) \
  832. static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
  833. { \
  834. char buf[BUF_LARGE]; \
  835. const char *crpath1, *crpath2; \
  836. char *rpath1, *rpath2, *mpath1, *mpath2; \
  837. struct vfs_s_super *super1, *super2; \
  838. if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0))) { \
  839. g_free (mpath1); \
  840. return -1; \
  841. } \
  842. if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0))) { \
  843. g_free (mpath1); \
  844. g_free (mpath2); \
  845. return -1; \
  846. } \
  847. rpath1 = shell_escape (crpath1); \
  848. g_free (mpath1); \
  849. rpath2 = shell_escape (crpath2); \
  850. g_free (mpath2); \
  851. g_snprintf(buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
  852. g_free (rpath1); \
  853. g_free (rpath2); \
  854. return fish_send_command(me, super2, buf, OPT_FLUSH); \
  855. }
  856. FISH_OP(rename, "#RENAME /%s /%s\n"
  857. "mv /%s /%s 2>/dev/null\n"
  858. "echo '### 000'" )
  859. FISH_OP(link, "#LINK /%s /%s\n"
  860. "ln /%s /%s 2>/dev/null\n"
  861. "echo '### 000'" )
  862. static int fish_symlink (struct vfs_class *me, const char *setto, const char *path)
  863. {
  864. char *qsetto;
  865. PREFIX
  866. qsetto = shell_escape (setto);
  867. g_snprintf(buf, sizeof(buf),
  868. "#SYMLINK %s /%s\n"
  869. "ln -s %s /%s 2>/dev/null\n"
  870. "echo '### 000'\n",
  871. qsetto, rpath, qsetto, rpath);
  872. g_free (qsetto);
  873. POSTFIX(OPT_FLUSH);
  874. }
  875. static int
  876. fish_chown (struct vfs_class *me, const char *path, int owner, int group)
  877. {
  878. char *sowner, *sgroup;
  879. struct passwd *pw;
  880. struct group *gr;
  881. if ((pw = getpwuid (owner)) == NULL)
  882. return 0;
  883. if ((gr = getgrgid (group)) == NULL)
  884. return 0;
  885. sowner = pw->pw_name;
  886. sgroup = gr->gr_name;
  887. {
  888. PREFIX
  889. g_snprintf (buf, sizeof(buf),
  890. "#CHOWN %s /%s\n"
  891. "chown %s /%s 2>/dev/null\n"
  892. "echo '### 000'\n",
  893. sowner, rpath,
  894. sowner, rpath);
  895. fish_send_command (me, super, buf, OPT_FLUSH);
  896. /* FIXME: what should we report if chgrp succeeds but chown fails? */
  897. g_snprintf (buf, sizeof(buf),
  898. "#CHGRP /%s \"/%s\"\n"
  899. "chgrp %s \"/%s\" 2>/dev/null\n"
  900. "echo '### 000'\n",
  901. sgroup, rpath,
  902. sgroup, rpath);
  903. /* fish_send_command(me, super, buf, OPT_FLUSH); */
  904. POSTFIX (OPT_FLUSH)
  905. }
  906. }
  907. static int fish_unlink (struct vfs_class *me, const char *path)
  908. {
  909. PREFIX
  910. g_snprintf(buf, sizeof(buf),
  911. "#DELE /%s\n"
  912. "rm -f /%s 2>/dev/null\n"
  913. "echo '### 000'\n",
  914. rpath, rpath);
  915. POSTFIX(OPT_FLUSH);
  916. }
  917. static int fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
  918. {
  919. PREFIX
  920. (void) mode;
  921. g_snprintf(buf, sizeof(buf),
  922. "#MKD /%s\n"
  923. "mkdir /%s 2>/dev/null\n"
  924. "echo '### 000'\n",
  925. rpath, rpath);
  926. POSTFIX(OPT_FLUSH);
  927. }
  928. static int fish_rmdir (struct vfs_class *me, const char *path)
  929. {
  930. PREFIX
  931. g_snprintf(buf, sizeof(buf),
  932. "#RMD /%s\n"
  933. "rmdir /%s 2>/dev/null\n"
  934. "echo '### 000'\n",
  935. rpath, rpath);
  936. POSTFIX(OPT_FLUSH);
  937. }
  938. static int
  939. fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags,
  940. int mode)
  941. {
  942. (void) mode;
  943. fh->u.fish.append = 0;
  944. /* File will be written only, so no need to retrieve it */
  945. if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR))) {
  946. fh->u.fish.append = flags & O_APPEND;
  947. if (!fh->ino->localname) {
  948. int tmp_handle =
  949. vfs_mkstemps (&fh->ino->localname, me->name,
  950. fh->ino->ent->name);
  951. if (tmp_handle == -1)
  952. return -1;
  953. close (tmp_handle);
  954. }
  955. return 0;
  956. }
  957. if (!fh->ino->localname)
  958. if (vfs_s_retrieve_file (me, fh->ino) == -1)
  959. return -1;
  960. if (!fh->ino->localname)
  961. vfs_die ("retrieve_file failed to fill in localname");
  962. return 0;
  963. }
  964. static void
  965. fish_fill_names (struct vfs_class *me, fill_names_f func)
  966. {
  967. struct vfs_s_super *super = MEDATA->supers;
  968. char *name;
  969. char gbuf[10];
  970. while (super)
  971. {
  972. const char *flags = "";
  973. switch (SUP.flags)
  974. {
  975. case FISH_FLAG_RSH:
  976. flags = ":r";
  977. break;
  978. case FISH_FLAG_COMPRESSED:
  979. flags = ":C";
  980. break;
  981. default:
  982. if (SUP.flags > FISH_FLAG_RSH)
  983. {
  984. break;
  985. g_snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
  986. flags = gbuf;
  987. }
  988. break;
  989. }
  990. name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags,
  991. "/", SUP.cwdir, (char *) NULL);
  992. (*func)(name);
  993. g_free (name);
  994. super = super->next;
  995. }
  996. }
  997. void
  998. init_fish (void)
  999. {
  1000. static struct vfs_s_subclass fish_subclass;
  1001. fish_subclass.flags = VFS_S_REMOTE;
  1002. fish_subclass.archive_same = fish_archive_same;
  1003. fish_subclass.open_archive = fish_open_archive;
  1004. fish_subclass.free_archive = fish_free_archive;
  1005. fish_subclass.fh_open = fish_fh_open;
  1006. fish_subclass.dir_load = fish_dir_load;
  1007. fish_subclass.file_store = fish_file_store;
  1008. fish_subclass.linear_start = fish_linear_start;
  1009. fish_subclass.linear_read = fish_linear_read;
  1010. fish_subclass.linear_close = fish_linear_close;
  1011. vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
  1012. vfs_fish_ops.name = "fish";
  1013. vfs_fish_ops.prefix = "sh:";
  1014. vfs_fish_ops.fill_names = fish_fill_names;
  1015. vfs_fish_ops.chmod = fish_chmod;
  1016. vfs_fish_ops.chown = fish_chown;
  1017. vfs_fish_ops.symlink = fish_symlink;
  1018. vfs_fish_ops.link = fish_link;
  1019. vfs_fish_ops.unlink = fish_unlink;
  1020. vfs_fish_ops.rename = fish_rename;
  1021. vfs_fish_ops.mkdir = fish_mkdir;
  1022. vfs_fish_ops.rmdir = fish_rmdir;
  1023. vfs_fish_ops.ctl = fish_ctl;
  1024. vfs_register_class (&vfs_fish_ops);
  1025. }