fish.c 32 KB

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