Manual pages: mc • mcdiff • mcedit • mcview

root/src/vfs/shell/shell.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. shell_set_blksize
  2. shell_default_stat
  3. shell_load_script_from_file
  4. shell_decode_reply
  5. shell_get_reply
  6. shell_command
  7. G_GNUC_PRINTF
  8. G_GNUC_PRINTF
  9. G_GNUC_PRINTF
  10. shell_new_archive
  11. shell_free_archive
  12. shell_pipeopen
  13. shell_set_env
  14. shell_info
  15. shell_open_archive_pipeopen
  16. shell_open_archive_talk
  17. shell_open_archive_int
  18. shell_open_archive
  19. shell_archive_same
  20. shell_parse_ls
  21. shell_dir_load
  22. shell_file_store
  23. shell_linear_start
  24. shell_linear_abort
  25. shell_linear_read
  26. shell_linear_close
  27. shell_ctl
  28. shell_rename
  29. shell_link
  30. shell_symlink
  31. shell_stat
  32. shell_lstat
  33. shell_fstat
  34. shell_chmod
  35. shell_chown
  36. shell_utime
  37. shell_unlink
  38. shell_exists
  39. shell_mkdir
  40. shell_rmdir
  41. shell_fh_new
  42. shell_fh_open
  43. shell_fill_names
  44. shell_open
  45. vfs_init_shell

   1 /*
   2    Virtual File System: SHELL implementation for transferring files over
   3    shell connections.
   4 
   5    Copyright (C) 1998-2026
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Pavel Machek, 1998
  10    Michal Svec, 2000
  11    Andrew Borodin <aborodin@vmail.ru>, 2010-2022
  12    Slava Zanko <slavazanko@gmail.com>, 2010, 2013
  13    Ilia Maslakov <il.smind@gmail.com>, 2010
  14 
  15    Derived from ftpfs.c.
  16 
  17    This file is part of the Midnight Commander.
  18 
  19    The Midnight Commander is free software: you can redistribute it
  20    and/or modify it under the terms of the GNU General Public License as
  21    published by the Free Software Foundation, either version 3 of the License,
  22    or (at your option) any later version.
  23 
  24    The Midnight Commander is distributed in the hope that it will be useful,
  25    but WITHOUT ANY WARRANTY; without even the implied warranty of
  26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27    GNU General Public License for more details.
  28 
  29    You should have received a copy of the GNU General Public License
  30    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  31  */
  32 
  33 /**
  34  * \file
  35  * \brief Source: Virtual File System: SHELL implementation for transferring files over
  36  * shell connections
  37  * \author Pavel Machek
  38  * \author Michal Svec
  39  * \date 1998, 2000
  40  *
  41  * Derived from ftpfs.c
  42  * Read README.shell for protocol specification.
  43  *
  44  * Syntax of path is: \verbatim sh://user@host[:Cr]/path \endverbatim
  45  *      where C means you want compressed connection,
  46  *      and r means you want to use rsh
  47  *
  48  * Namespace: shell_vfs_ops exported.
  49  */
  50 
  51 /* Define this if your ssh can take -I option */
  52 
  53 #include <config.h>
  54 #include <errno.h>
  55 #include <pwd.h>
  56 #include <grp.h>
  57 #include <stdlib.h>
  58 #include <string.h>
  59 #include <inttypes.h>  // uintmax_t
  60 
  61 #include "lib/global.h"
  62 #include "lib/tty/tty.h"  // enable/disable interrupt key
  63 #include "lib/strutil.h"
  64 #include "lib/unixcompat.h"
  65 #include "lib/fileloc.h"
  66 #include "lib/util.h"  // my_exit()
  67 #include "lib/mcconfig.h"
  68 #include "lib/widget.h"  // message()
  69 
  70 #include "src/execute.h"  // pre_exec, post_exec
  71 
  72 #include "lib/vfs/vfs.h"
  73 #include "lib/vfs/utilvfs.h"
  74 #include "lib/vfs/netutil.h"
  75 #include "lib/vfs/xdirentry.h"
  76 #include "lib/vfs/gc.h"  // vfs_stamp_create
  77 
  78 #include "shell.h"
  79 
  80 /*** global variables ****************************************************************************/
  81 
  82 int shell_directory_timeout = 900;
  83 
  84 /*** file scope macro definitions ****************************************************************/
  85 
  86 #define DO_RESOLVE_SYMLINK    1
  87 #define DO_OPEN               2
  88 #define DO_FREE_RESOURCE      4
  89 
  90 #define SHELL_FLAG_COMPRESSED 1
  91 #define SHELL_FLAG_RSH        2
  92 
  93 #define OPT_FLUSH             1
  94 #define OPT_IGNORE_ERROR      2
  95 
  96 /*
  97  * Reply codes.
  98  */
  99 #define PRELIM    1  // positive preliminary
 100 #define COMPLETE  2  // positive completion
 101 #define CONTINUE  3  // positive intermediate
 102 #define TRANSIENT 4  // transient negative completion
 103 #define ERROR     5  // permanent negative completion
 104 
 105 /* command wait_flag: */
 106 #define NONE        0x00
 107 #define WAIT_REPLY  0x01
 108 #define WANT_STRING 0x02
 109 
 110 /* environment flags */
 111 #define SHELL_HAVE_HEAD        1
 112 #define SHELL_HAVE_SED         2
 113 #define SHELL_HAVE_AWK         4
 114 #define SHELL_HAVE_PERL        8
 115 #define SHELL_HAVE_LSQ         16
 116 #define SHELL_HAVE_DATE_MDYT   32
 117 #define SHELL_HAVE_TAIL        64
 118 
 119 #define SHELL_SUPER(super)     ((shell_super_t *) (super))
 120 #define SHELL_FILE_HANDLER(fh) ((shell_file_handler_t *) fh)
 121 
 122 /*** file scope type declarations ****************************************************************/
 123 
 124 typedef struct
 125 {
 126     struct vfs_s_super base;  // base class
 127 
 128     int sockr;
 129     int sockw;
 130     char *scr_ls;
 131     char *scr_chmod;
 132     char *scr_utime;
 133     char *scr_exists;
 134     char *scr_mkdir;
 135     char *scr_unlink;
 136     char *scr_chown;
 137     char *scr_rmdir;
 138     char *scr_ln;
 139     char *scr_mv;
 140     char *scr_hardlink;
 141     char *scr_get;
 142     char *scr_send;
 143     char *scr_append;
 144     char *scr_info;
 145     int host_flags;
 146     GString *scr_env;
 147 } shell_super_t;
 148 
 149 typedef struct
 150 {
 151     vfs_file_handler_t base;  // base class
 152 
 153     off_t got;
 154     off_t total;
 155     gboolean append;
 156 } shell_file_handler_t;
 157 
 158 /*** forward declarations (file scope functions) *************************************************/
 159 
 160 /*** file scope variables ************************************************************************/
 161 
 162 static char reply_str[80];
 163 
 164 static struct vfs_s_subclass shell_subclass;
 165 static struct vfs_class *vfs_shell_ops = VFS_CLASS (&shell_subclass);
 166 
 167 static const struct
 168 {
 169     size_t offset;
 170     const char *name;
 171 } scripts[] = {
 172     { offsetof (shell_super_t, scr_ls), VFS_SHELL_LS_FILE },
 173     { offsetof (shell_super_t, scr_exists), VFS_SHELL_EXISTS_FILE },
 174     { offsetof (shell_super_t, scr_mkdir), VFS_SHELL_MKDIR_FILE },
 175     { offsetof (shell_super_t, scr_unlink), VFS_SHELL_UNLINK_FILE },
 176     { offsetof (shell_super_t, scr_chown), VFS_SHELL_CHOWN_FILE },
 177     { offsetof (shell_super_t, scr_chmod), VFS_SHELL_CHMOD_FILE },
 178     { offsetof (shell_super_t, scr_utime), VFS_SHELL_UTIME_FILE },
 179     { offsetof (shell_super_t, scr_rmdir), VFS_SHELL_RMDIR_FILE },
 180     { offsetof (shell_super_t, scr_ln), VFS_SHELL_LN_FILE },
 181     { offsetof (shell_super_t, scr_mv), VFS_SHELL_MV_FILE },
 182     { offsetof (shell_super_t, scr_hardlink), VFS_SHELL_HARDLINK_FILE },
 183     { offsetof (shell_super_t, scr_get), VFS_SHELL_GET_FILE },
 184     { offsetof (shell_super_t, scr_send), VFS_SHELL_SEND_FILE },
 185     { offsetof (shell_super_t, scr_append), VFS_SHELL_APPEND_FILE },
 186     { offsetof (shell_super_t, scr_info), VFS_SHELL_INFO_FILE },
 187 };
 188 
 189 /* --------------------------------------------------------------------------------------------- */
 190 /*** file scope functions ************************************************************************/
 191 /* --------------------------------------------------------------------------------------------- */
 192 
 193 static void
 194 shell_set_blksize (struct stat *s)
     /* [previous][next][first][last][top][bottom][index][help]  */
 195 {
 196 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
 197     // redefine block size
 198     s->st_blksize = 64 * 1024;  // FIXME
 199 #endif
 200 }
 201 
 202 /* --------------------------------------------------------------------------------------------- */
 203 
 204 static struct stat *
 205 shell_default_stat (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
 206 {
 207     struct stat *s;
 208 
 209     s = vfs_s_default_stat (me, S_IFDIR | 0755);
 210     shell_set_blksize (s);
 211     vfs_adjust_stat (s);
 212 
 213     return s;
 214 }
 215 
 216 /* --------------------------------------------------------------------------------------------- */
 217 
 218 static char *
 219 shell_load_script_from_file (const char *hostname, const char *script_name, GError **error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 220 {
 221     char *scr_filename = NULL;
 222     char *scr_content;
 223     gsize scr_len = 0;
 224 
 225     // 1st: scan user directory
 226     scr_filename = g_build_path (PATH_SEP_STR, mc_config_get_data_path (), VFS_SHELL_PREFIX,
 227                                  hostname, script_name, (char *) NULL);
 228     // silent about user dir
 229     g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
 230     g_free (scr_filename);
 231     // 2nd: scan system dir
 232     if (scr_content == NULL)
 233     {
 234         scr_filename =
 235             g_build_path (PATH_SEP_STR, LIBEXECDIR, VFS_SHELL_PREFIX, script_name, (char *) NULL);
 236         g_file_get_contents (scr_filename, &scr_content, &scr_len, error);
 237         g_free (scr_filename);
 238     }
 239 
 240     return scr_content;
 241 }
 242 
 243 /* --------------------------------------------------------------------------------------------- */
 244 
 245 static int
 246 shell_decode_reply (char *s, gboolean was_garbage)
     /* [previous][next][first][last][top][bottom][index][help]  */
 247 {
 248     int code;
 249 
 250     if (sscanf (s, "%d", &code) == 0)
 251     {
 252         code = 500;
 253         return 5;
 254     }
 255     if (code < 100)
 256         return was_garbage ? ERROR : (code == 0 ? COMPLETE : PRELIM);
 257     return code / 100;
 258 }
 259 
 260 /* --------------------------------------------------------------------------------------------- */
 261 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
 262 
 263 static int
 264 shell_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 265 {
 266     char answer[BUF_1K];
 267     gboolean was_garbage = FALSE;
 268 
 269     while (TRUE)
 270     {
 271         if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
 272         {
 273             if (string_buf != NULL)
 274                 *string_buf = '\0';
 275             return 4;
 276         }
 277 
 278         if (strncmp (answer, "### ", 4) == 0)
 279             return shell_decode_reply (answer + 4, was_garbage ? 1 : 0);
 280 
 281         was_garbage = TRUE;
 282         if (string_buf != NULL)
 283             g_strlcpy (string_buf, answer, string_len);
 284     }
 285 }
 286 
 287 /* --------------------------------------------------------------------------------------------- */
 288 
 289 static int
 290 shell_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *cmd,
     /* [previous][next][first][last][top][bottom][index][help]  */
 291                size_t cmd_len)
 292 {
 293     ssize_t status;
 294     FILE *logfile = me->logfile;
 295 
 296     if (cmd_len == (size_t) (-1))
 297         cmd_len = strlen (cmd);
 298 
 299     if (logfile != NULL)
 300     {
 301         size_t ret;
 302 
 303         ret = fwrite (cmd, cmd_len, 1, logfile);
 304         ret = fflush (logfile);
 305         (void) ret;
 306     }
 307 
 308     tty_enable_interrupt_key ();
 309     status = write (SHELL_SUPER (super)->sockw, cmd, cmd_len);
 310     tty_disable_interrupt_key ();
 311 
 312     if (status < 0)
 313         return TRANSIENT;
 314 
 315     if (wait_reply)
 316         return shell_get_reply (me, SHELL_SUPER (super)->sockr,
 317                                 (wait_reply & WANT_STRING) != 0 ? reply_str : NULL,
 318                                 sizeof (reply_str) - 1);
 319     return COMPLETE;
 320 }
 321 
 322 /* --------------------------------------------------------------------------------------------- */
 323 
 324 static int G_GNUC_PRINTF (5, 0)
     /* [previous][next][first][last][top][bottom][index][help]  */
 325 shell_command_va (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *scr,
 326                   const char *vars, va_list ap)
 327 {
 328     int r;
 329     GString *command;
 330 
 331     command = mc_g_string_dup (SHELL_SUPER (super)->scr_env);
 332     g_string_append_vprintf (command, vars, ap);
 333     g_string_append (command, scr);
 334     r = shell_command (me, super, wait_reply, command->str, command->len);
 335     g_string_free (command, TRUE);
 336 
 337     return r;
 338 }
 339 
 340 /* --------------------------------------------------------------------------------------------- */
 341 
 342 static int G_GNUC_PRINTF (5, 6)
     /* [previous][next][first][last][top][bottom][index][help]  */
 343 shell_command_v (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *scr,
 344                  const char *vars, ...)
 345 {
 346     int r;
 347     va_list ap;
 348 
 349     va_start (ap, vars);
 350     r = shell_command_va (me, super, wait_reply, scr, vars, ap);
 351     va_end (ap);
 352 
 353     return r;
 354 }
 355 
 356 /* --------------------------------------------------------------------------------------------- */
 357 
 358 static int G_GNUC_PRINTF (5, 6)
     /* [previous][next][first][last][top][bottom][index][help]  */
 359 shell_send_command (struct vfs_class *me, struct vfs_s_super *super, int flags, const char *scr,
 360                     const char *vars, ...)
 361 {
 362     int r;
 363     va_list ap;
 364 
 365     va_start (ap, vars);
 366     r = shell_command_va (me, super, WAIT_REPLY, scr, vars, ap);
 367     va_end (ap);
 368     vfs_stamp_create (vfs_shell_ops, super);
 369 
 370     if (r != COMPLETE)
 371         ERRNOR (E_REMOTE, -1);
 372     if ((flags & OPT_FLUSH) != 0)
 373         vfs_s_invalidate (me, super);
 374 
 375     return 0;
 376 }
 377 
 378 /* --------------------------------------------------------------------------------------------- */
 379 
 380 static struct vfs_s_super *
 381 shell_new_archive (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
 382 {
 383     shell_super_t *arch;
 384 
 385     arch = g_new0 (shell_super_t, 1);
 386     arch->base.me = me;
 387     arch->sockr = -1;
 388     arch->sockw = -1;
 389 
 390     return VFS_SUPER (arch);
 391 }
 392 
 393 /* --------------------------------------------------------------------------------------------- */
 394 
 395 static void
 396 shell_free_archive (struct vfs_class *me, struct vfs_s_super *super)
     /* [previous][next][first][last][top][bottom][index][help]  */
 397 {
 398     shell_super_t *shell_super = SHELL_SUPER (super);
 399     size_t i;
 400 
 401     if ((shell_super->sockw != -1) || (shell_super->sockr != -1))
 402         vfs_print_message (_ ("shell: Disconnecting from %s"), super->name ? super->name : "???");
 403 
 404     if (shell_super->sockw != -1)
 405     {
 406         shell_command (me, super, NONE, "exit\n", -1);
 407         close (shell_super->sockw);
 408         shell_super->sockw = -1;
 409     }
 410 
 411     if (shell_super->sockr != -1)
 412     {
 413         close (shell_super->sockr);
 414         shell_super->sockr = -1;
 415     }
 416 
 417     for (i = 0; i < G_N_ELEMENTS (scripts); i++)
 418         g_free (G_STRUCT_MEMBER (char *, shell_super, scripts[i].offset));
 419 
 420     if (shell_super->scr_env != NULL)
 421         g_string_free (shell_super->scr_env, TRUE);
 422 }
 423 
 424 /* --------------------------------------------------------------------------------------------- */
 425 
 426 static void
 427 shell_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
     /* [previous][next][first][last][top][bottom][index][help]  */
 428 {
 429     int fileset1[2], fileset2[2];
 430     int res;
 431 
 432     if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
 433         vfs_die ("Cannot pipe(): %m.");
 434 
 435     res = my_fork ();
 436 
 437     if (res != 0)
 438     {
 439         if (res < 0)
 440             vfs_die ("Cannot fork(): %m.");
 441         // We are the parent
 442         close (fileset1[0]);
 443         SHELL_SUPER (super)->sockw = fileset1[1];
 444         close (fileset2[1]);
 445         SHELL_SUPER (super)->sockr = fileset2[0];
 446     }
 447     else
 448     {
 449         res = dup2 (fileset1[0], STDIN_FILENO);
 450         close (fileset1[0]);
 451         close (fileset1[1]);
 452         res = dup2 (fileset2[1], STDOUT_FILENO);
 453         close (STDERR_FILENO);
 454         // stderr to /dev/null
 455         res = open ("/dev/null", O_WRONLY);
 456         close (fileset2[0]);
 457         close (fileset2[1]);
 458         my_execvp (path, (char **) argv);
 459         my_exit (3);
 460     }
 461 }
 462 
 463 /* --------------------------------------------------------------------------------------------- */
 464 
 465 static GString *
 466 shell_set_env (int flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 467 {
 468     GString *ret;
 469 
 470     ret = g_string_sized_new (256);
 471 
 472     if ((flags & SHELL_HAVE_HEAD) != 0)
 473         g_string_append (ret, "SHELL_HAVE_HEAD=1 export SHELL_HAVE_HEAD; ");
 474 
 475     if ((flags & SHELL_HAVE_SED) != 0)
 476         g_string_append (ret, "SHELL_HAVE_SED=1 export SHELL_HAVE_SED; ");
 477 
 478     if ((flags & SHELL_HAVE_AWK) != 0)
 479         g_string_append (ret, "SHELL_HAVE_AWK=1 export SHELL_HAVE_AWK; ");
 480 
 481     if ((flags & SHELL_HAVE_PERL) != 0)
 482         g_string_append (ret, "SHELL_HAVE_PERL=1 export SHELL_HAVE_PERL; ");
 483 
 484     if ((flags & SHELL_HAVE_LSQ) != 0)
 485         g_string_append (ret, "SHELL_HAVE_LSQ=1 export SHELL_HAVE_LSQ; ");
 486 
 487     if ((flags & SHELL_HAVE_DATE_MDYT) != 0)
 488         g_string_append (ret, "SHELL_HAVE_DATE_MDYT=1 export SHELL_HAVE_DATE_MDYT; ");
 489 
 490     if ((flags & SHELL_HAVE_TAIL) != 0)
 491         g_string_append (ret, "SHELL_HAVE_TAIL=1 export SHELL_HAVE_TAIL; ");
 492 
 493     return ret;
 494 }
 495 
 496 /* --------------------------------------------------------------------------------------------- */
 497 
 498 static gboolean
 499 shell_info (struct vfs_class *me, struct vfs_s_super *super)
     /* [previous][next][first][last][top][bottom][index][help]  */
 500 {
 501     shell_super_t *shell_super = SHELL_SUPER (super);
 502 
 503     if (shell_command (me, super, NONE, shell_super->scr_info, -1) == COMPLETE)
 504     {
 505         while (TRUE)
 506         {
 507             int res;
 508             char buffer[BUF_8K] = "";
 509 
 510             res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), shell_super->sockr);
 511             if ((res == 0) || (res == EINTR))
 512                 ERRNOR (ECONNRESET, FALSE);
 513             if (strncmp (buffer, "### ", 4) == 0)
 514                 break;
 515             shell_super->host_flags = atol (buffer);
 516         }
 517         return TRUE;
 518     }
 519     ERRNOR (E_PROTO, FALSE);
 520 }
 521 
 522 /* --------------------------------------------------------------------------------------------- */
 523 
 524 static void
 525 shell_open_archive_pipeopen (struct vfs_s_super *super)
     /* [previous][next][first][last][top][bottom][index][help]  */
 526 {
 527     char gbuf[10];
 528     const char *argv[10];  // All of 10 is used now
 529     const char *xsh = (super->path_element->port == SHELL_FLAG_RSH ? "rsh" : "ssh");
 530     int i = 0;
 531 
 532     argv[i++] = xsh;
 533     if (super->path_element->port == SHELL_FLAG_COMPRESSED)
 534         argv[i++] = "-C";
 535 
 536     if (super->path_element->port > SHELL_FLAG_RSH)
 537     {
 538         argv[i++] = "-p";
 539         g_snprintf (gbuf, sizeof (gbuf), "%d", super->path_element->port);
 540         argv[i++] = gbuf;
 541     }
 542 
 543     /*
 544      * Add the user name to the ssh command line only if it was explicitly
 545      * set in vfs URL. rsh/ssh will get current user by default
 546      * plus we can set convenient overrides in  ~/.ssh/config (explicit -l
 547      * option breaks it for some)
 548      */
 549 
 550     if (super->path_element->user != NULL)
 551     {
 552         argv[i++] = "-l";
 553         argv[i++] = super->path_element->user;
 554     }
 555     else
 556     {
 557         // The rest of the code assumes it to be a valid username
 558         super->path_element->user = vfs_get_local_username ();
 559     }
 560 
 561     argv[i++] = super->path_element->host;
 562     argv[i++] = "echo SHELL:; /bin/sh";
 563     argv[i++] = NULL;
 564 
 565     shell_pipeopen (super, xsh, argv);
 566 }
 567 
 568 /* --------------------------------------------------------------------------------------------- */
 569 
 570 static gboolean
 571 shell_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super)
     /* [previous][next][first][last][top][bottom][index][help]  */
 572 {
 573     shell_super_t *shell_super = SHELL_SUPER (super);
 574     char answer[BUF_2K];
 575 
 576     printf ("\n%s\n", _ ("shell: Waiting for initial line..."));
 577 
 578     if (vfs_s_get_line (me, shell_super->sockr, answer, sizeof (answer), ':') == 0)
 579         return FALSE;
 580 
 581     if (strstr (answer, "assword") != NULL)
 582     {
 583         /* Currently, this does not work. ssh reads passwords from
 584            /dev/tty, not from stdin :-(. */
 585 
 586         printf ("\n%s\n", _ ("Sorry, we cannot do password authenticated connections for now."));
 587 
 588         return FALSE;
 589 #if 0
 590         if (super->path_element->password == NULL)
 591         {
 592             char *p, *op;
 593 
 594             p = g_strdup_printf (_("shell: Password is required for %s"),
 595                                  super->path_element->user);
 596             op = vfs_get_password (p);
 597             g_free (p);
 598             if (op == NULL)
 599                 return FALSE;
 600             super->path_element->password = op;
 601         }
 602 
 603         printf ("\n%s\n", _("shell: Sending password..."));
 604 
 605         {
 606             size_t str_len;
 607 
 608             str_len = strlen (super->path_element->password);
 609             if ((write (shell_super.sockw, super->path_element->password, str_len) !=
 610                  (ssize_t) str_len) || (write (shell_super->sockw, "\n", 1) != 1))
 611                 return FALSE;
 612         }
 613 #endif
 614     }
 615     return TRUE;
 616 }
 617 
 618 /* --------------------------------------------------------------------------------------------- */
 619 
 620 static int
 621 shell_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
     /* [previous][next][first][last][top][bottom][index][help]  */
 622 {
 623     gboolean ftalk;
 624 
 625     // hide panels
 626     pre_exec ();
 627 
 628     // open pipe
 629     shell_open_archive_pipeopen (super);
 630 
 631     // Start talk with ssh-server (password prompt, etc )
 632     ftalk = shell_open_archive_talk (me, super);
 633 
 634     // show panels
 635     post_exec ();
 636 
 637     if (!ftalk)
 638         ERRNOR (E_PROTO, -1);
 639 
 640     vfs_print_message ("%s", _ ("shell: Sending initial line..."));
 641 
 642     // Set up remote locale to C, otherwise dates cannot be recognized
 643     if (shell_command (me, super, WAIT_REPLY,
 644                        "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n"
 645                        "echo '### 200'\n",
 646                        -1)
 647         != COMPLETE)
 648         ERRNOR (E_PROTO, -1);
 649 
 650     vfs_print_message ("%s", _ ("shell: Getting host info..."));
 651     if (shell_info (me, super))
 652         SHELL_SUPER (super)->scr_env = shell_set_env (SHELL_SUPER (super)->host_flags);
 653 
 654 #if 0
 655     super->name =
 656         g_strconcat ("sh://", super->path_element->user, "@", super->path_element->host,
 657                      PATH_SEP_STR, (char *) NULL);
 658 #else
 659     super->name = g_strdup (PATH_SEP_STR);
 660 #endif
 661 
 662     super->root = vfs_s_new_inode (me, super, shell_default_stat (me));
 663 
 664     return 0;
 665 }
 666 
 667 /* --------------------------------------------------------------------------------------------- */
 668 
 669 static int
 670 shell_open_archive (struct vfs_s_super *super, const vfs_path_t *vpath,
     /* [previous][next][first][last][top][bottom][index][help]  */
 671                     const vfs_path_element_t *vpath_element)
 672 {
 673     shell_super_t *shell_super = SHELL_SUPER (super);
 674     size_t i;
 675 
 676     (void) vpath;
 677 
 678     super->path_element = vfs_path_element_clone (vpath_element);
 679 
 680     if (strncmp (vpath_element->vfs_prefix, "rsh", 3) == 0)
 681         super->path_element->port = SHELL_FLAG_RSH;
 682 
 683     for (i = 0; i < G_N_ELEMENTS (scripts); i++)
 684     {
 685         GError *error = NULL;
 686 
 687         G_STRUCT_MEMBER (char *, shell_super, scripts[i].offset) =
 688             shell_load_script_from_file (super->path_element->host, scripts[i].name, &error);
 689         if (error != NULL)
 690         {
 691             message (D_ERROR, MSG_ERROR, _ ("shell: Cannot load script\n%s"), error->message);
 692             g_error_free (error);
 693             return -1;
 694         }
 695     }
 696 
 697     return shell_open_archive_int (vpath_element->class, super);
 698 }
 699 
 700 /* --------------------------------------------------------------------------------------------- */
 701 
 702 static int
 703 shell_archive_same (const vfs_path_element_t *vpath_element, struct vfs_s_super *super,
     /* [previous][next][first][last][top][bottom][index][help]  */
 704                     const vfs_path_t *vpath, void *cookie)
 705 {
 706     vfs_path_element_t *path_element;
 707     int result;
 708 
 709     (void) vpath;
 710     (void) cookie;
 711 
 712     path_element = vfs_path_element_clone (vpath_element);
 713 
 714     if (path_element->user == NULL)
 715         path_element->user = vfs_get_local_username ();
 716 
 717     result = ((strcmp (path_element->host, super->path_element->host) == 0)
 718               && (strcmp (path_element->user, super->path_element->user) == 0)
 719               && (path_element->port == super->path_element->port))
 720         ? 1
 721         : 0;
 722 
 723     vfs_path_element_free (path_element);
 724 
 725     return result;
 726 }
 727 
 728 /* --------------------------------------------------------------------------------------------- */
 729 
 730 static void
 731 shell_parse_ls (char *buffer, struct vfs_s_entry *ent)
     /* [previous][next][first][last][top][bottom][index][help]  */
 732 {
 733 #define ST ent->ino->st
 734 
 735     buffer++;
 736 
 737     switch (buffer[-1])
 738     {
 739     case ':':
 740     {
 741         char *filename;
 742         char *filename_bound;
 743         char *temp;
 744 
 745         filename = buffer;
 746 
 747         if (strcmp (filename, "\".\"") == 0 || strcmp (filename, "\"..\"") == 0)
 748             break;  // We'll do "." and ".." ourselves
 749 
 750         filename_bound = filename + strlen (filename);
 751 
 752         if (S_ISLNK (ST.st_mode))
 753         {
 754             char *linkname;
 755             char *linkname_bound;
 756             /* we expect: "escaped-name" -> "escaped-name"
 757                //     -> cannot occur in filenames,
 758                //     because it will be escaped to -\> */
 759 
 760             linkname_bound = filename_bound;
 761 
 762             if (*filename == '"')
 763                 ++filename;
 764 
 765             linkname = strstr (filename, "\" -> \"");
 766             if (linkname == NULL)
 767             {
 768                 // broken client, or smth goes wrong
 769                 linkname = filename_bound;
 770                 if (filename_bound > filename && *(filename_bound - 1) == '"')
 771                     --filename_bound;  // skip trailing "
 772             }
 773             else
 774             {
 775                 filename_bound = linkname;
 776                 linkname += 6;  // strlen ("\" -> \"")
 777                 if (*(linkname_bound - 1) == '"')
 778                     --linkname_bound;  // skip trailing "
 779             }
 780 
 781             ent->name = g_strndup (filename, filename_bound - filename);
 782             temp = ent->name;
 783             ent->name = str_shell_unescape (ent->name);
 784             g_free (temp);
 785 
 786             ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
 787             temp = ent->ino->linkname;
 788             ent->ino->linkname = str_shell_unescape (ent->ino->linkname);
 789             g_free (temp);
 790         }
 791         else
 792         {
 793             // we expect: "escaped-name"
 794             if (filename_bound - filename > 2)
 795             {
 796                 /*
 797                    there is at least 2 "
 798                    and we skip them
 799                  */
 800                 if (*filename == '"')
 801                     ++filename;
 802                 if (*(filename_bound - 1) == '"')
 803                     --filename_bound;
 804             }
 805 
 806             ent->name = g_strndup (filename, filename_bound - filename);
 807             temp = ent->name;
 808             ent->name = str_shell_unescape (ent->name);
 809             g_free (temp);
 810         }
 811         break;
 812     }
 813 
 814     case 'S':
 815         ST.st_size = (off_t) g_ascii_strtoll (buffer, NULL, 10);
 816         break;
 817 
 818     case 'P':
 819     {
 820         size_t skipped;
 821 
 822         vfs_parse_filemode (buffer, &skipped, &ST.st_mode);
 823         break;
 824     }
 825 
 826     case 'R':
 827     {
 828         /*
 829            raw filemode:
 830            we expect: Roctal-filemode octal-filetype uid.gid
 831          */
 832         size_t skipped;
 833 
 834         vfs_parse_raw_filemode (buffer, &skipped, &ST.st_mode);
 835         break;
 836     }
 837 
 838     case 'd':
 839         vfs_split_text (buffer);
 840         vfs_zero_stat_times (&ST);
 841         if (vfs_parse_filedate (0, &ST.st_ctime) == 0)
 842             break;
 843         ST.st_atime = ST.st_mtime = ST.st_ctime;
 844         break;
 845 
 846     case 'D':
 847     {
 848         struct tm tim;
 849 
 850         memset (&tim, 0, sizeof (tim));
 851         if (sscanf (buffer, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon, &tim.tm_mday,
 852                     &tim.tm_hour, &tim.tm_min, &tim.tm_sec)
 853             != 6)
 854             break;
 855         vfs_zero_stat_times (&ST);
 856         ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
 857     }
 858     break;
 859 
 860     case 'E':
 861     {
 862         int maj, min;
 863 
 864         if (sscanf (buffer, "%d,%d", &maj, &min) != 2)
 865             break;
 866 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 867         ST.st_rdev = makedev (maj, min);
 868 #endif
 869     }
 870     break;
 871 
 872     default:
 873         break;
 874     }
 875 
 876 #undef ST
 877 }
 878 
 879 /* --------------------------------------------------------------------------------------------- */
 880 
 881 static int
 882 shell_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, const char *remote_path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 883 {
 884     struct vfs_s_super *super = dir->super;
 885     char buffer[BUF_8K] = "\0";
 886     struct vfs_s_entry *ent = NULL;
 887     char *quoted_path;
 888     int reply_code;
 889 
 890     /*
 891      * Simple SHELL debug interface :]
 892      */
 893 #if 0
 894     if (me->logfile == NULL)
 895         me->logfile = fopen ("/tmp/mc-SHELL.sh", "w");
 896 #endif
 897 
 898     vfs_print_message (_ ("shell: Reading directory %s..."), remote_path);
 899 
 900     dir->timestamp = g_get_monotonic_time () + shell_directory_timeout * G_USEC_PER_SEC;
 901 
 902     quoted_path = str_shell_escape (remote_path);
 903     (void) shell_command_v (me, super, NONE, SHELL_SUPER (super)->scr_ls, "SHELL_FILENAME=%s;\n",
 904                             quoted_path);
 905     g_free (quoted_path);
 906 
 907     ent = vfs_s_generate_entry (me, NULL, dir, 0);
 908 
 909     while (TRUE)
 910     {
 911         int res;
 912 
 913         res =
 914             vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SHELL_SUPER (super)->sockr);
 915 
 916         if ((res == 0) || (res == EINTR))
 917         {
 918             vfs_s_free_entry (me, ent);
 919             me->verrno = ECONNRESET;
 920             goto error;
 921         }
 922         if (me->logfile != NULL)
 923         {
 924             fputs (buffer, me->logfile);
 925             fputs ("\n", me->logfile);
 926             fflush (me->logfile);
 927         }
 928         if (strncmp (buffer, "### ", 4) == 0)
 929             break;
 930 
 931         if (buffer[0] != '\0')
 932             shell_parse_ls (buffer, ent);
 933         else if (ent->name != NULL)
 934         {
 935             vfs_s_insert_entry (me, dir, ent);
 936             ent = vfs_s_generate_entry (me, NULL, dir, 0);
 937         }
 938     }
 939 
 940     vfs_s_free_entry (me, ent);
 941     reply_code = shell_decode_reply (buffer + 4, 0);
 942     if (reply_code == COMPLETE)
 943     {
 944         vfs_print_message (_ ("%s: done."), me->name);
 945         return 0;
 946     }
 947 
 948     me->verrno = reply_code == ERROR ? EACCES : E_REMOTE;
 949 
 950 error:
 951     vfs_print_message (_ ("%s: failure"), me->name);
 952     return -1;
 953 }
 954 
 955 /* --------------------------------------------------------------------------------------------- */
 956 
 957 static int
 958 shell_file_store (struct vfs_class *me, vfs_file_handler_t *fh, char *name, char *localname)
     /* [previous][next][first][last][top][bottom][index][help]  */
 959 {
 960     shell_file_handler_t *shell = SHELL_FILE_HANDLER (fh);
 961     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
 962     shell_super_t *shell_super = SHELL_SUPER (super);
 963     int code;
 964     off_t total = 0;
 965     char buffer[BUF_8K];
 966     struct stat s;
 967     int h;
 968     char *quoted_name;
 969 
 970     h = open (localname, O_RDONLY);
 971     if (h == -1)
 972         ERRNOR (EIO, -1);
 973     if (fstat (h, &s) < 0)
 974     {
 975         close (h);
 976         ERRNOR (EIO, -1);
 977     }
 978 
 979     /* First, try this as stor:
 980      *
 981      *     ( head -c number ) | ( cat > file; cat >/dev/null )
 982      *
 983      *  If 'head' is not present on the remote system, 'dd' will be used.
 984      * Unfortunately, we cannot trust most non-GNU 'head' implementations
 985      * even if '-c' options is supported. Therefore, we separate GNU head
 986      * (and other modern heads?) using '-q' and '-' . This causes another
 987      * implementations to fail (because of "incorrect options").
 988      *
 989      *  Fallback is:
 990      *
 991      *     rest=<number>
 992      *     while [ $rest -gt 0 ]
 993      *     do
 994      *        cnt=`expr \( $rest + 255 \) / 256`
 995      *        n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
 996      *        rest=`expr $rest - $n`
 997      *     done
 998      *
 999      *  'dd' was not designed for full filling of input buffers,
1000      *  and does not report exact number of bytes (not blocks).
1001      *  Therefore a more complex shell script is needed.
1002      *
1003      *   On some systems non-GNU head writes "Usage:" error report to stdout
1004      *  instead of stderr. It makes impossible the use of "head || dd"
1005      *  algorithm for file appending case, therefore just "dd" is used for it.
1006      */
1007 
1008     quoted_name = str_shell_escape (name);
1009     vfs_print_message (_ ("shell: store %s: sending command..."), quoted_name);
1010 
1011     // FIXME: File size is limited to ULONG_MAX
1012     code = shell_command_v (
1013         me, super, WAIT_REPLY, shell->append ? shell_super->scr_append : shell_super->scr_send,
1014         "SHELL_FILENAME=%s SHELL_FILESIZE=%" PRIuMAX ";\n", quoted_name, (uintmax_t) s.st_size);
1015     g_free (quoted_name);
1016 
1017     if (code != PRELIM)
1018     {
1019         close (h);
1020         ERRNOR (E_REMOTE, -1);
1021     }
1022 
1023     while (TRUE)
1024     {
1025         ssize_t n, t;
1026 
1027         while ((n = read (h, buffer, sizeof (buffer))) < 0)
1028         {
1029             if ((errno == EINTR) && tty_got_interrupt ())
1030                 continue;
1031             vfs_print_message ("%s", _ ("shell: Local read failed, sending zeros"));
1032             close (h);
1033             h = open ("/dev/zero", O_RDONLY);
1034         }
1035 
1036         if (n == 0)
1037             break;
1038 
1039         t = write (shell_super->sockw, buffer, n);
1040         if (t != n)
1041         {
1042             if (t == -1)
1043                 me->verrno = errno;
1044             else
1045                 me->verrno = EIO;
1046             goto error_return;
1047         }
1048         tty_disable_interrupt_key ();
1049         total += n;
1050         vfs_print_message ("%s: %" PRIuMAX "/%" PRIuMAX, _ ("shell: storing file"),
1051                            (uintmax_t) total, (uintmax_t) s.st_size);
1052     }
1053     close (h);
1054 
1055     if (shell_get_reply (me, shell_super->sockr, NULL, 0) != COMPLETE)
1056         ERRNOR (E_REMOTE, -1);
1057     return 0;
1058 
1059 error_return:
1060     close (h);
1061     shell_get_reply (me, shell_super->sockr, NULL, 0);
1062     return -1;
1063 }
1064 
1065 /* --------------------------------------------------------------------------------------------- */
1066 
1067 static int
1068 shell_linear_start (struct vfs_class *me, vfs_file_handler_t *fh, off_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
1069 {
1070     shell_file_handler_t *shell = SHELL_FILE_HANDLER (fh);
1071     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
1072     char *name;
1073     char *quoted_name;
1074 
1075     name = vfs_s_fullpath (me, fh->ino);
1076     if (name == NULL)
1077         return 0;
1078     quoted_name = str_shell_escape (name);
1079     g_free (name);
1080     shell->append = FALSE;
1081 
1082     /*
1083      * Check whether the remote file is readable by using 'dd' to copy
1084      * a single byte from the remote file to /dev/null. If 'dd' completes
1085      * with exit status of 0 use 'cat' to send the file contents to the
1086      * standard output (i.e. over the network).
1087      */
1088 
1089     offset = shell_command_v (me, super, WANT_STRING, SHELL_SUPER (super)->scr_get,
1090                               "SHELL_FILENAME=%s SHELL_START_OFFSET=%" PRIuMAX ";\n", quoted_name,
1091                               (uintmax_t) offset);
1092     g_free (quoted_name);
1093 
1094     if (offset != PRELIM)
1095         ERRNOR (E_REMOTE, 0);
1096     fh->linear = LS_LINEAR_OPEN;
1097     shell->got = 0;
1098     errno = 0;
1099 #if SIZEOF_OFF_T == SIZEOF_LONG
1100     shell->total = (off_t) strtol (reply_str, NULL, 10);
1101 #else
1102     shell->total = (off_t) g_ascii_strtoll (reply_str, NULL, 10);
1103 #endif
1104     if (errno != 0)
1105         ERRNOR (E_REMOTE, 0);
1106     return 1;
1107 }
1108 
1109 /* --------------------------------------------------------------------------------------------- */
1110 
1111 static void
1112 shell_linear_abort (struct vfs_class *me, vfs_file_handler_t *fh)
     /* [previous][next][first][last][top][bottom][index][help]  */
1113 {
1114     shell_file_handler_t *shell = SHELL_FILE_HANDLER (fh);
1115     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
1116     char buffer[BUF_8K];
1117     ssize_t n;
1118 
1119     vfs_print_message ("%s", _ ("Aborting transfer..."));
1120 
1121     do
1122     {
1123         n = MIN ((off_t) sizeof (buffer), (shell->total - shell->got));
1124         if (n != 0)
1125         {
1126             n = read (SHELL_SUPER (super)->sockr, buffer, n);
1127             if (n < 0)
1128                 return;
1129             shell->got += n;
1130         }
1131     }
1132     while (n != 0);
1133 
1134     if (shell_get_reply (me, SHELL_SUPER (super)->sockr, NULL, 0) != COMPLETE)
1135         vfs_print_message ("%s", _ ("Error reported after abort."));
1136     else
1137         vfs_print_message ("%s", _ ("Aborted transfer would be successful."));
1138 }
1139 
1140 /* --------------------------------------------------------------------------------------------- */
1141 
1142 static ssize_t
1143 shell_linear_read (struct vfs_class *me, vfs_file_handler_t *fh, void *buf, size_t len)
     /* [previous][next][first][last][top][bottom][index][help]  */
1144 {
1145     shell_file_handler_t *shell = SHELL_FILE_HANDLER (fh);
1146     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
1147     ssize_t n = 0;
1148 
1149     len = MIN ((size_t) (shell->total - shell->got), len);
1150     tty_disable_interrupt_key ();
1151     while (len != 0 && ((n = read (SHELL_SUPER (super)->sockr, buf, len)) < 0))
1152     {
1153         if ((errno == EINTR) && !tty_got_interrupt ())
1154             continue;
1155         break;
1156     }
1157     tty_enable_interrupt_key ();
1158 
1159     if (n > 0)
1160         shell->got += n;
1161     else if (n < 0)
1162         shell_linear_abort (me, fh);
1163     else if (shell_get_reply (me, SHELL_SUPER (super)->sockr, NULL, 0) != COMPLETE)
1164         ERRNOR (E_REMOTE, -1);
1165     ERRNOR (errno, n);
1166 }
1167 
1168 /* --------------------------------------------------------------------------------------------- */
1169 
1170 static void
1171 shell_linear_close (struct vfs_class *me, vfs_file_handler_t *fh)
     /* [previous][next][first][last][top][bottom][index][help]  */
1172 {
1173     shell_file_handler_t *shell = SHELL_FILE_HANDLER (fh);
1174 
1175     if (shell->total != shell->got)
1176         shell_linear_abort (me, fh);
1177 }
1178 
1179 /* --------------------------------------------------------------------------------------------- */
1180 
1181 static int
1182 shell_ctl (void *fh, int ctlop, void *arg)
     /* [previous][next][first][last][top][bottom][index][help]  */
1183 {
1184     (void) arg;
1185     (void) fh;
1186     (void) ctlop;
1187 
1188     return 0;
1189 
1190 #if 0
1191     switch (ctlop)
1192     {
1193     case VFS_CTL_IS_NOTREADY:
1194         {
1195             vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1196             int v;
1197 
1198             if (file->linear == LS_NOT_LINEAR)
1199                 vfs_die ("You may not do this");
1200             if (file->linear == LS_LINEAR_CLOSED || file->linear == LS_LINEAR_PREOPEN)
1201                 return 0;
1202 
1203             v = vfs_s_select_on_two (VFS_FILE_HANDLER_SUPER (fh)->u.shell.sockr, 0);
1204 
1205             return (((v < 0) && (errno == EINTR)) || v == 0) ? 1 : 0;
1206         }
1207     default:
1208         return 0;
1209     }
1210 #endif
1211 }
1212 
1213 /* --------------------------------------------------------------------------------------------- */
1214 
1215 static int
1216 shell_rename (const vfs_path_t *vpath1, const vfs_path_t *vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1217 {
1218     const char *crpath1, *crpath2;
1219     char *rpath1, *rpath2;
1220     struct vfs_s_super *super, *super2;
1221     struct vfs_class *me;
1222     int ret;
1223 
1224     crpath1 = vfs_s_get_path (vpath1, &super, 0);
1225     if (crpath1 == NULL)
1226         return -1;
1227 
1228     crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1229     if (crpath2 == NULL)
1230         return -1;
1231 
1232     rpath1 = str_shell_escape (crpath1);
1233     rpath2 = str_shell_escape (crpath2);
1234 
1235     me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath1));
1236 
1237     ret = shell_send_command (me, super2, OPT_FLUSH, SHELL_SUPER (super)->scr_mv,
1238                               "SHELL_FILEFROM=%s SHELL_FILETO=%s;\n", rpath1, rpath2);
1239 
1240     g_free (rpath1);
1241     g_free (rpath2);
1242 
1243     return ret;
1244 }
1245 
1246 /* --------------------------------------------------------------------------------------------- */
1247 
1248 static int
1249 shell_link (const vfs_path_t *vpath1, const vfs_path_t *vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1250 {
1251     const char *crpath1, *crpath2;
1252     char *rpath1, *rpath2;
1253     struct vfs_s_super *super, *super2;
1254     struct vfs_class *me;
1255     int ret;
1256 
1257     crpath1 = vfs_s_get_path (vpath1, &super, 0);
1258     if (crpath1 == NULL)
1259         return -1;
1260 
1261     crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1262     if (crpath2 == NULL)
1263         return -1;
1264 
1265     rpath1 = str_shell_escape (crpath1);
1266     rpath2 = str_shell_escape (crpath2);
1267 
1268     me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath1));
1269 
1270     ret = shell_send_command (me, super2, OPT_FLUSH, SHELL_SUPER (super)->scr_hardlink,
1271                               "SHELL_FILEFROM=%s SHELL_FILETO=%s;\n", rpath1, rpath2);
1272 
1273     g_free (rpath1);
1274     g_free (rpath2);
1275 
1276     return ret;
1277 }
1278 
1279 /* --------------------------------------------------------------------------------------------- */
1280 
1281 static int
1282 shell_symlink (const vfs_path_t *vpath1, const vfs_path_t *vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1283 {
1284     char *qsetto;
1285     const char *crpath;
1286     char *rpath;
1287     struct vfs_s_super *super;
1288     struct vfs_class *me;
1289     int ret;
1290 
1291     crpath = vfs_s_get_path (vpath2, &super, 0);
1292     if (crpath == NULL)
1293         return -1;
1294 
1295     rpath = str_shell_escape (crpath);
1296     qsetto = str_shell_escape (vfs_path_get_last_path_str (vpath1));
1297 
1298     me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath2));
1299 
1300     ret = shell_send_command (me, super, OPT_FLUSH, SHELL_SUPER (super)->scr_ln,
1301                               "SHELL_FILEFROM=%s SHELL_FILETO=%s;\n", qsetto, rpath);
1302 
1303     g_free (qsetto);
1304     g_free (rpath);
1305 
1306     return ret;
1307 }
1308 
1309 /* --------------------------------------------------------------------------------------------- */
1310 
1311 static int
1312 shell_stat (const vfs_path_t *vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1313 {
1314     int ret;
1315 
1316     ret = vfs_s_stat (vpath, buf);
1317     shell_set_blksize (buf);
1318     return ret;
1319 }
1320 
1321 /* --------------------------------------------------------------------------------------------- */
1322 
1323 static int
1324 shell_lstat (const vfs_path_t *vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1325 {
1326     int ret;
1327 
1328     ret = vfs_s_lstat (vpath, buf);
1329     shell_set_blksize (buf);
1330     return ret;
1331 }
1332 
1333 /* --------------------------------------------------------------------------------------------- */
1334 
1335 static int
1336 shell_fstat (void *vfs_info, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1337 {
1338     int ret;
1339 
1340     ret = vfs_s_fstat (vfs_info, buf);
1341     shell_set_blksize (buf);
1342     return ret;
1343 }
1344 
1345 /* --------------------------------------------------------------------------------------------- */
1346 
1347 static int
1348 shell_chmod (const vfs_path_t *vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1349 {
1350     const char *crpath;
1351     char *rpath;
1352     struct vfs_s_super *super;
1353     struct vfs_class *me;
1354     int ret;
1355 
1356     crpath = vfs_s_get_path (vpath, &super, 0);
1357     if (crpath == NULL)
1358         return -1;
1359 
1360     rpath = str_shell_escape (crpath);
1361 
1362     me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
1363 
1364     ret = shell_send_command (me, super, OPT_FLUSH, SHELL_SUPER (super)->scr_chmod,
1365                               "SHELL_FILENAME=%s SHELL_FILEMODE=%4.4o;\n", rpath,
1366                               (unsigned int) (mode & 07777));
1367 
1368     g_free (rpath);
1369 
1370     return ret;
1371     ;
1372 }
1373 
1374 /* --------------------------------------------------------------------------------------------- */
1375 
1376 static int
1377 shell_chown (const vfs_path_t *vpath, uid_t owner, gid_t group)
     /* [previous][next][first][last][top][bottom][index][help]  */
1378 {
1379     char *sowner, *sgroup;
1380     struct passwd *pw;
1381     struct group *gr;
1382     const char *crpath;
1383     char *rpath;
1384     struct vfs_s_super *super;
1385     struct vfs_class *me;
1386     int ret;
1387 
1388     pw = getpwuid (owner);
1389     if (pw == NULL)
1390         return 0;
1391 
1392     gr = getgrgid (group);
1393     if (gr == NULL)
1394         return 0;
1395 
1396     sowner = pw->pw_name;
1397     sgroup = gr->gr_name;
1398 
1399     crpath = vfs_s_get_path (vpath, &super, 0);
1400     if (crpath == NULL)
1401         return -1;
1402 
1403     rpath = str_shell_escape (crpath);
1404 
1405     me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
1406 
1407     // FIXME: what should we report if chgrp succeeds but chown fails?
1408     ret = shell_send_command (me, super, OPT_FLUSH, SHELL_SUPER (super)->scr_chown,
1409                               "SHELL_FILENAME=%s SHELL_FILEOWNER=%s SHELL_FILEGROUP=%s;\n", rpath,
1410                               sowner, sgroup);
1411 
1412     g_free (rpath);
1413 
1414     return ret;
1415 }
1416 
1417 /* --------------------------------------------------------------------------------------------- */
1418 
1419 static int
1420 shell_utime (const vfs_path_t *vpath, mc_timesbuf_t *times)
     /* [previous][next][first][last][top][bottom][index][help]  */
1421 {
1422     char utcatime[16], utcmtime[16];
1423     char utcatime_w_nsec[30], utcmtime_w_nsec[30];
1424     mc_timespec_t atime, mtime;
1425     struct tm *gmt;
1426     const char *crpath;
1427     char *rpath;
1428     struct vfs_s_super *super;
1429     struct vfs_class *me;
1430     int ret;
1431 
1432     crpath = vfs_s_get_path (vpath, &super, 0);
1433     if (crpath == NULL)
1434         return -1;
1435 
1436     rpath = str_shell_escape (crpath);
1437 
1438     vfs_get_timespecs_from_timesbuf (times, &atime, &mtime);
1439 
1440     gmt = gmtime (&atime.tv_sec);
1441     g_snprintf (utcatime, sizeof (utcatime), "%04d%02d%02d%02d%02d.%02d", gmt->tm_year + 1900,
1442                 gmt->tm_mon + 1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1443     g_snprintf (utcatime_w_nsec, sizeof (utcatime_w_nsec), "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
1444                 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min,
1445                 gmt->tm_sec, atime.tv_nsec);
1446 
1447     gmt = gmtime (&mtime.tv_sec);
1448     g_snprintf (utcmtime, sizeof (utcmtime), "%04d%02d%02d%02d%02d.%02d", gmt->tm_year + 1900,
1449                 gmt->tm_mon + 1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1450     g_snprintf (utcmtime_w_nsec, sizeof (utcmtime_w_nsec), "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
1451                 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min,
1452                 gmt->tm_sec, mtime.tv_nsec);
1453 
1454     me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
1455 
1456     ret = shell_send_command (
1457         me, super, OPT_FLUSH, SHELL_SUPER (super)->scr_utime,
1458         "SHELL_FILENAME=%s SHELL_FILEATIME=%ju SHELL_FILEMTIME=%ju "
1459         "SHELL_TOUCHATIME=%s SHELL_TOUCHMTIME=%s SHELL_TOUCHATIME_W_NSEC=\"%s\" "
1460         "SHELL_TOUCHMTIME_W_NSEC=\"%s\";\n",
1461         rpath, (uintmax_t) atime.tv_sec, (uintmax_t) mtime.tv_sec, utcatime, utcmtime,
1462         utcatime_w_nsec, utcmtime_w_nsec);
1463 
1464     g_free (rpath);
1465 
1466     return ret;
1467 }
1468 
1469 /* --------------------------------------------------------------------------------------------- */
1470 
1471 static int
1472 shell_unlink (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1473 {
1474     const char *crpath;
1475     char *rpath;
1476     struct vfs_s_super *super;
1477     struct vfs_class *me;
1478     int ret;
1479 
1480     crpath = vfs_s_get_path (vpath, &super, 0);
1481     if (crpath == NULL)
1482         return -1;
1483 
1484     rpath = str_shell_escape (crpath);
1485 
1486     me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
1487 
1488     ret = shell_send_command (me, super, OPT_FLUSH, SHELL_SUPER (super)->scr_unlink,
1489                               "SHELL_FILENAME=%s;\n", rpath);
1490 
1491     g_free (rpath);
1492 
1493     return ret;
1494 }
1495 
1496 /* --------------------------------------------------------------------------------------------- */
1497 
1498 static int
1499 shell_exists (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1500 {
1501     const char *crpath;
1502     char *rpath;
1503     struct vfs_s_super *super;
1504     struct vfs_class *me;
1505     int ret;
1506 
1507     crpath = vfs_s_get_path (vpath, &super, 0);
1508     if (crpath == NULL)
1509         return -1;
1510 
1511     rpath = str_shell_escape (crpath);
1512 
1513     me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
1514 
1515     ret = shell_send_command (me, super, OPT_FLUSH, SHELL_SUPER (super)->scr_exists,
1516                               "SHELL_FILENAME=%s;\n", rpath);
1517 
1518     g_free (rpath);
1519 
1520     return (ret == 0 ? 1 : 0);
1521 }
1522 
1523 /* --------------------------------------------------------------------------------------------- */
1524 
1525 static int
1526 shell_mkdir (const vfs_path_t *vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1527 {
1528     const char *crpath;
1529     char *rpath;
1530     struct vfs_s_super *super;
1531     struct vfs_class *me;
1532     int ret;
1533 
1534     (void) mode;
1535 
1536     crpath = vfs_s_get_path (vpath, &super, 0);
1537     if (crpath == NULL)
1538         return -1;
1539 
1540     rpath = str_shell_escape (crpath);
1541 
1542     me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
1543 
1544     ret = shell_send_command (me, super, OPT_FLUSH, SHELL_SUPER (super)->scr_mkdir,
1545                               "SHELL_FILENAME=%s;\n", rpath);
1546     g_free (rpath);
1547 
1548     if (ret != 0)
1549         return ret;
1550 
1551     if (shell_exists (vpath) == 0)
1552     {
1553         me->verrno = EACCES;
1554         return -1;
1555     }
1556     return 0;
1557 }
1558 
1559 /* --------------------------------------------------------------------------------------------- */
1560 
1561 static int
1562 shell_rmdir (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1563 {
1564     const char *crpath;
1565     char *rpath;
1566     struct vfs_s_super *super;
1567     struct vfs_class *me;
1568     int ret;
1569 
1570     crpath = vfs_s_get_path (vpath, &super, 0);
1571     if (crpath == NULL)
1572         return -1;
1573 
1574     rpath = str_shell_escape (crpath);
1575 
1576     me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
1577 
1578     ret = shell_send_command (me, super, OPT_FLUSH, SHELL_SUPER (super)->scr_rmdir,
1579                               "SHELL_FILENAME=%s;\n", rpath);
1580 
1581     g_free (rpath);
1582 
1583     return ret;
1584 }
1585 
1586 /* --------------------------------------------------------------------------------------------- */
1587 
1588 static vfs_file_handler_t *
1589 shell_fh_new (struct vfs_s_inode *ino, gboolean changed)
     /* [previous][next][first][last][top][bottom][index][help]  */
1590 {
1591     shell_file_handler_t *fh;
1592 
1593     fh = g_new0 (shell_file_handler_t, 1);
1594     vfs_s_init_fh (VFS_FILE_HANDLER (fh), ino, changed);
1595 
1596     return VFS_FILE_HANDLER (fh);
1597 }
1598 
1599 /* --------------------------------------------------------------------------------------------- */
1600 
1601 static int
1602 shell_fh_open (struct vfs_class *me, vfs_file_handler_t *fh, int flags, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1603 {
1604     shell_file_handler_t *shell = SHELL_FILE_HANDLER (fh);
1605 
1606     (void) mode;
1607 
1608     // File will be written only, so no need to retrieve it
1609     if (((flags & O_WRONLY) == O_WRONLY) && ((flags & (O_RDONLY | O_RDWR)) == 0))
1610     {
1611         // user pressed the button [ Append ] in the "Copy" dialog
1612         if ((flags & O_APPEND) != 0)
1613             shell->append = TRUE;
1614 
1615         if (fh->ino->localname == NULL)
1616         {
1617             vfs_path_t *vpath = NULL;
1618             int tmp_handle;
1619 
1620             tmp_handle = vfs_mkstemps (&vpath, me->name, fh->ino->ent->name);
1621             if (tmp_handle == -1)
1622                 return (-1);
1623 
1624             fh->ino->localname = vfs_path_free (vpath, FALSE);
1625             close (tmp_handle);
1626         }
1627         return 0;
1628     }
1629 
1630     if (fh->ino->localname == NULL && vfs_s_retrieve_file (me, fh->ino) == -1)
1631         return (-1);
1632 
1633     if (fh->ino->localname == NULL)
1634         vfs_die ("retrieve_file failed to fill in localname");
1635     return 0;
1636 }
1637 
1638 /* --------------------------------------------------------------------------------------------- */
1639 
1640 static void
1641 shell_fill_names (struct vfs_class *me, fill_names_f func)
     /* [previous][next][first][last][top][bottom][index][help]  */
1642 {
1643     GList *iter;
1644 
1645     for (iter = VFS_SUBCLASS (me)->supers; iter != NULL; iter = g_list_next (iter))
1646     {
1647         const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
1648 
1649         char *name;
1650         char gbuf[10];
1651         const char *flags = "";
1652 
1653         switch (super->path_element->port)
1654         {
1655         case SHELL_FLAG_RSH:
1656             flags = ":r";
1657             break;
1658         case SHELL_FLAG_COMPRESSED:
1659             flags = ":C";
1660             break;
1661         default:
1662             if (super->path_element->port > SHELL_FLAG_RSH)
1663             {
1664                 g_snprintf (gbuf, sizeof (gbuf), ":%d", super->path_element->port);
1665                 flags = gbuf;
1666             }
1667             break;
1668         }
1669 
1670         name = g_strconcat (vfs_shell_ops->prefix, VFS_PATH_URL_DELIMITER,
1671                             super->path_element->user, "@", super->path_element->host, flags,
1672                             PATH_SEP_STR, super->path_element->path, (char *) NULL);
1673         func (name);
1674         g_free (name);
1675     }
1676 }
1677 
1678 /* --------------------------------------------------------------------------------------------- */
1679 
1680 static void *
1681 shell_open (const vfs_path_t *vpath, int flags, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1682 {
1683     /*
1684        sorry, i've places hack here
1685        cause shell don't able to open files with O_EXCL flag
1686      */
1687     flags &= ~O_EXCL;
1688     return vfs_s_open (vpath, flags, mode);
1689 }
1690 
1691 /* --------------------------------------------------------------------------------------------- */
1692 /*** public functions ****************************************************************************/
1693 /* --------------------------------------------------------------------------------------------- */
1694 
1695 void
1696 vfs_init_shell (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1697 {
1698     tcp_init ();
1699 
1700     vfs_init_subclass (&shell_subclass, "shell", VFSF_REMOTE | VFSF_USETMP, "sh");
1701     vfs_shell_ops->fill_names = shell_fill_names;
1702     vfs_shell_ops->stat = shell_stat;
1703     vfs_shell_ops->lstat = shell_lstat;
1704     vfs_shell_ops->fstat = shell_fstat;
1705     vfs_shell_ops->chmod = shell_chmod;
1706     vfs_shell_ops->chown = shell_chown;
1707     vfs_shell_ops->utime = shell_utime;
1708     vfs_shell_ops->open = shell_open;
1709     vfs_shell_ops->symlink = shell_symlink;
1710     vfs_shell_ops->link = shell_link;
1711     vfs_shell_ops->unlink = shell_unlink;
1712     vfs_shell_ops->rename = shell_rename;
1713     vfs_shell_ops->mkdir = shell_mkdir;
1714     vfs_shell_ops->rmdir = shell_rmdir;
1715     vfs_shell_ops->ctl = shell_ctl;
1716     shell_subclass.archive_same = shell_archive_same;
1717     shell_subclass.new_archive = shell_new_archive;
1718     shell_subclass.open_archive = shell_open_archive;
1719     shell_subclass.free_archive = shell_free_archive;
1720     shell_subclass.fh_new = shell_fh_new;
1721     shell_subclass.fh_open = shell_fh_open;
1722     shell_subclass.dir_load = shell_dir_load;
1723     shell_subclass.file_store = shell_file_store;
1724     shell_subclass.linear_start = shell_linear_start;
1725     shell_subclass.linear_read = shell_linear_read;
1726     shell_subclass.linear_close = shell_linear_close;
1727     vfs_register_class (vfs_shell_ops);
1728 }
1729 
1730 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */