fish.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. PREFIX
  955. g_snprintf(buf, sizeof(buf),
  956. "#ISEXISTS /%s\n"
  957. "ls -l /%s >/dev/null 2>/dev/null\n"
  958. "echo '### '$?\n",
  959. rpath, rpath);
  960. g_free (rpath);
  961. if ( fish_send_command(me, super, buf, OPT_FLUSH) == 0 )
  962. return 1;
  963. return 0;
  964. }
  965. static int fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
  966. {
  967. int ret_code;
  968. PREFIX
  969. (void) mode;
  970. g_snprintf(buf, sizeof(buf),
  971. "#MKD /%s\n"
  972. "if mkdir /%s 2>/dev/null; then\n"
  973. "echo '### 000'\n"
  974. "else\n"
  975. "echo '### 500'\n"
  976. "fi\n",
  977. rpath, rpath);
  978. g_free (rpath);
  979. ret_code = fish_send_command(me, super, buf, OPT_FLUSH);
  980. if ( ret_code != 0 )
  981. return ret_code;
  982. if ( ! fish_exists (me, path) ){
  983. ERRNOR (EACCES, -1);
  984. }
  985. return 0;
  986. }
  987. static int fish_rmdir (struct vfs_class *me, const char *path)
  988. {
  989. PREFIX
  990. g_snprintf(buf, sizeof(buf),
  991. "#RMD /%s\n"
  992. "if rmdir /%s 2>/dev/null; then\n"
  993. "echo '### 000'\n"
  994. "else\n"
  995. "echo '### 500'\n"
  996. "fi\n",
  997. rpath, rpath);
  998. POSTFIX(OPT_FLUSH);
  999. }
  1000. static int
  1001. fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags,
  1002. int mode)
  1003. {
  1004. (void) mode;
  1005. fh->u.fish.append = 0;
  1006. /* File will be written only, so no need to retrieve it */
  1007. if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR))) {
  1008. fh->u.fish.append = flags & O_APPEND;
  1009. if (!fh->ino->localname) {
  1010. int tmp_handle =
  1011. vfs_mkstemps (&fh->ino->localname, me->name,
  1012. fh->ino->ent->name);
  1013. if (tmp_handle == -1)
  1014. return -1;
  1015. close (tmp_handle);
  1016. }
  1017. return 0;
  1018. }
  1019. if (!fh->ino->localname)
  1020. if (vfs_s_retrieve_file (me, fh->ino) == -1)
  1021. return -1;
  1022. if (!fh->ino->localname)
  1023. vfs_die ("retrieve_file failed to fill in localname");
  1024. return 0;
  1025. }
  1026. static void
  1027. fish_fill_names (struct vfs_class *me, fill_names_f func)
  1028. {
  1029. struct vfs_s_super *super = MEDATA->supers;
  1030. char *name;
  1031. char gbuf[10];
  1032. while (super)
  1033. {
  1034. const char *flags = "";
  1035. switch (SUP.flags)
  1036. {
  1037. case FISH_FLAG_RSH:
  1038. flags = ":r";
  1039. break;
  1040. case FISH_FLAG_COMPRESSED:
  1041. flags = ":C";
  1042. break;
  1043. default:
  1044. if (SUP.flags > FISH_FLAG_RSH)
  1045. {
  1046. break;
  1047. g_snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
  1048. flags = gbuf;
  1049. }
  1050. break;
  1051. }
  1052. name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags,
  1053. "/", SUP.cwdir, (char *) NULL);
  1054. (*func)(name);
  1055. g_free (name);
  1056. super = super->next;
  1057. }
  1058. }
  1059. static void *
  1060. fish_open (struct vfs_class *me, const char *file, int flags, int mode)
  1061. {
  1062. /*
  1063. sorry, i've places hack here
  1064. cause fish don't able to open files with O_EXCL flag
  1065. */
  1066. flags &= ~O_EXCL;
  1067. return vfs_s_open (me, file, flags, mode);
  1068. }
  1069. void
  1070. init_fish (void)
  1071. {
  1072. static struct vfs_s_subclass fish_subclass;
  1073. fish_subclass.flags = VFS_S_REMOTE;
  1074. fish_subclass.archive_same = fish_archive_same;
  1075. fish_subclass.open_archive = fish_open_archive;
  1076. fish_subclass.free_archive = fish_free_archive;
  1077. fish_subclass.fh_open = fish_fh_open;
  1078. fish_subclass.dir_load = fish_dir_load;
  1079. fish_subclass.file_store = fish_file_store;
  1080. fish_subclass.linear_start = fish_linear_start;
  1081. fish_subclass.linear_read = fish_linear_read;
  1082. fish_subclass.linear_close = fish_linear_close;
  1083. vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
  1084. vfs_fish_ops.name = "fish";
  1085. vfs_fish_ops.prefix = "sh:";
  1086. vfs_fish_ops.fill_names = fish_fill_names;
  1087. vfs_fish_ops.chmod = fish_chmod;
  1088. vfs_fish_ops.chown = fish_chown;
  1089. vfs_fish_ops.open = fish_open;
  1090. vfs_fish_ops.symlink = fish_symlink;
  1091. vfs_fish_ops.link = fish_link;
  1092. vfs_fish_ops.unlink = fish_unlink;
  1093. vfs_fish_ops.rename = fish_rename;
  1094. vfs_fish_ops.mkdir = fish_mkdir;
  1095. vfs_fish_ops.rmdir = fish_rmdir;
  1096. vfs_fish_ops.ctl = fish_ctl;
  1097. vfs_register_class (&vfs_fish_ops);
  1098. }