]> andersk Git - openssh.git/blobdiff - sftp-int.c
- markus@cvs.openbsd.org 2001/11/07 22:53:21
[openssh.git] / sftp-int.c
index cf86012eaf3b0e564086588850322a9c861d7e40..841e562e1de21bc7a433ae8d4f6cc4b6ecae83f8 100644 (file)
@@ -26,7 +26,7 @@
 /* XXX: recursive operations */
 
 #include "includes.h"
-RCSID("$OpenBSD: sftp-int.c,v 1.28 2001/03/14 15:15:58 markus Exp $");
+RCSID("$OpenBSD: sftp-int.c,v 1.40 2001/08/14 09:23:02 markus Exp $");
 
 #include "buffer.h"
 #include "xmalloc.h"
@@ -78,6 +78,7 @@ struct CMD {
 };
 
 const struct CMD cmds[] = {
+       { "bye",        I_QUIT },
        { "cd",         I_CHDIR },
        { "chdir",      I_CHDIR },
        { "chgrp",      I_CHGRP },
@@ -86,6 +87,7 @@ const struct CMD cmds[] = {
        { "dir",        I_LS },
        { "exit",       I_QUIT },
        { "get",        I_GET },
+       { "mget",       I_GET },
        { "help",       I_HELP },
        { "lcd",        I_LCHDIR },
        { "lchdir",     I_LCHDIR },
@@ -97,6 +99,7 @@ const struct CMD cmds[] = {
        { "lumask",     I_LUMASK },
        { "mkdir",      I_MKDIR },
        { "put",        I_PUT },
+       { "mput",       I_PUT },
        { "pwd",        I_PWD },
        { "quit",       I_QUIT },
        { "rename",     I_RENAME },
@@ -109,7 +112,7 @@ const struct CMD cmds[] = {
        { NULL,                 -1}
 };
 
-void
+static void
 help(void)
 {
        printf("Available commands:\n");
@@ -141,10 +144,10 @@ help(void)
        printf("?                             Synonym for help\n");
 }
 
-void
+static void
 local_do_shell(const char *args)
 {
-       int ret, status;
+       int status;
        char *shell;
        pid_t pid;
 
@@ -161,10 +164,10 @@ local_do_shell(const char *args)
                /* XXX: child has pipe fds to ssh subproc open - issue? */
                if (args) {
                        debug3("Executing %s -c \"%s\"", shell, args);
-                       ret = execl(shell, shell, "-c", args, NULL);
+                       execl(shell, shell, "-c", args, (char *)NULL);
                } else {
                        debug3("Executing %s", shell);
-                       ret = execl(shell, shell, NULL);
+                       execl(shell, shell, (char *)NULL);
                }
                fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
                    strerror(errno));
@@ -178,7 +181,7 @@ local_do_shell(const char *args)
                error("Shell exited with status %d", WEXITSTATUS(status));
 }
 
-void
+static void
 local_do_ls(const char *args)
 {
        if (!args || !*args)
@@ -194,22 +197,56 @@ local_do_ls(const char *args)
        }
 }
 
-char *
+static char *
+path_append(char *p1, char *p2)
+{
+       char *ret;
+       int len = strlen(p1) + strlen(p2) + 2;
+
+       ret = xmalloc(len);
+       strlcpy(ret, p1, len);
+       if (strcmp(p1, "/") != 0) 
+               strlcat(ret, "/", len);
+       strlcat(ret, p2, len);
+
+       return(ret);
+}
+
+static char *
 make_absolute(char *p, char *pwd)
 {
-       char buf[2048];
+       char *abs;
 
        /* Derelativise */
        if (p && p[0] != '/') {
-               snprintf(buf, sizeof(buf), "%s/%s", pwd, p);
+               abs = path_append(pwd, p);
                xfree(p);
-               p = xstrdup(buf);
+               return(abs);
+       } else
+               return(p);
+}
+
+static int
+infer_path(const char *p, char **ifp)
+{
+       char *cp;
+
+       cp = strrchr(p, '/');
+       if (cp == NULL) {
+               *ifp = xstrdup(p);
+               return(0);
+       }
+
+       if (!cp[1]) {
+               error("Invalid path");
+               return(-1);
        }
 
-       return(p);
+       *ifp = xstrdup(cp + 1);
+       return(0);
 }
 
-int
+static int
 parse_getput_flags(const char **cpp, int *pflag)
 {
        const char *cp = *cpp;
@@ -232,7 +269,7 @@ parse_getput_flags(const char **cpp, int *pflag)
        return(0);
 }
 
-int
+static int
 get_pathname(const char **cpp, char **path)
 {
        const char *cp = *cpp, *end;
@@ -249,7 +286,7 @@ get_pathname(const char **cpp, char **path)
        /* Check for quoted filenames */
        if (*cp == '\"' || *cp == '\'') {
                quot = *cp++;
-               
+
                end = strchr(cp, quot);
                if (end == NULL) {
                        error("Unterminated quote");
@@ -280,27 +317,189 @@ get_pathname(const char **cpp, char **path)
        return (-1);
 }
 
-int
-infer_path(const char *p, char **ifp)
+static int
+is_dir(char *path)
 {
-       char *cp;
+       struct stat sb;
 
-       cp = strrchr(p, '/');
-       if (cp == NULL) {
-               *ifp = xstrdup(p);
+       /* XXX: report errors? */
+       if (stat(path, &sb) == -1)
+               return(0);
+
+       return(sb.st_mode & S_IFDIR);
+}
+
+static int
+remote_is_dir(int in, int out, char *path)
+{
+       Attrib *a;
+
+       /* XXX: report errors? */
+       if ((a = do_stat(in, out, path, 1)) == NULL)
                return(0);
+       if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
+               return(0);
+       return(a->perm & S_IFDIR);
+}
+
+static int
+process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
+{
+       char *abs_src = NULL;
+       char *abs_dst = NULL;
+       char *tmp;
+       glob_t g;
+       int err = 0;
+       int i;
+
+       abs_src = xstrdup(src);
+       abs_src = make_absolute(abs_src, pwd);
+
+       memset(&g, 0, sizeof(g));
+       debug3("Looking up %s", abs_src);
+       if (remote_glob(in, out, abs_src, 0, NULL, &g)) {
+               error("File \"%s\" not found.", abs_src);
+               err = -1;
+               goto out;
        }
 
-       if (!cp[1]) {
-               error("Invalid path");
-               return(-1);
+       /* Only one match, dst may be file, directory or unspecified */
+       if (g.gl_pathv[0] && g.gl_matchc == 1) {
+               if (dst) {
+                       /* If directory specified, append filename */
+                       if (is_dir(dst)) {
+                               if (infer_path(g.gl_pathv[0], &tmp)) {
+                                       err = 1;
+                                       goto out;
+                               }
+                               abs_dst = path_append(dst, tmp);
+                               xfree(tmp);
+                       } else
+                               abs_dst = xstrdup(dst);
+               } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
+                       err = -1;
+                       goto out;
+               }
+               printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
+               err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag);
+               goto out;
        }
 
-       *ifp = xstrdup(cp + 1);
-       return(0);
+       /* Multiple matches, dst may be directory or unspecified */
+       if (dst && !is_dir(dst)) {
+               error("Multiple files match, but \"%s\" is not a directory",
+                   dst);
+               err = -1;
+               goto out;
+       }
+
+       for(i = 0; g.gl_pathv[i]; i++) {
+               if (infer_path(g.gl_pathv[i], &tmp)) {
+                       err = -1;
+                       goto out;
+               }
+               if (dst) {
+                       abs_dst = path_append(dst, tmp);
+                       xfree(tmp);
+               } else
+                       abs_dst = tmp;
+
+               printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
+               if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
+                       err = -1;
+               xfree(abs_dst);
+               abs_dst = NULL;
+       }
+
+out:
+       xfree(abs_src);
+       if (abs_dst)
+               xfree(abs_dst);
+       globfree(&g);
+       return(err);
+}
+
+static int
+process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
+{
+       char *tmp_dst = NULL;
+       char *abs_dst = NULL;
+       char *tmp;
+       glob_t g;
+       int err = 0;
+       int i;
+
+       if (dst) {
+               tmp_dst = xstrdup(dst);
+               tmp_dst = make_absolute(tmp_dst, pwd);
+       }
+
+       memset(&g, 0, sizeof(g));
+       debug3("Looking up %s", src);
+       if (glob(src, 0, NULL, &g)) {
+               error("File \"%s\" not found.", src);
+               err = -1;
+               goto out;
+       }
+
+       /* Only one match, dst may be file, directory or unspecified */
+       if (g.gl_pathv[0] && g.gl_matchc == 1) {
+               if (tmp_dst) {
+                       /* If directory specified, append filename */
+                       if (remote_is_dir(in, out, tmp_dst)) {
+                               if (infer_path(g.gl_pathv[0], &tmp)) {
+                                       err = 1;
+                                       goto out;
+                               }
+                               abs_dst = path_append(tmp_dst, tmp);
+                               xfree(tmp);
+                       } else
+                               abs_dst = xstrdup(tmp_dst);
+               } else {
+                       if (infer_path(g.gl_pathv[0], &abs_dst)) {
+                               err = -1;
+                               goto out;
+                       }
+                       abs_dst = make_absolute(abs_dst, pwd);
+               }
+               printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
+               err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag);
+               goto out;
+       }
+
+       /* Multiple matches, dst may be directory or unspecified */
+       if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
+               error("Multiple files match, but \"%s\" is not a directory",
+                   tmp_dst);
+               err = -1;
+               goto out;
+       }
+
+       for(i = 0; g.gl_pathv[i]; i++) {
+               if (infer_path(g.gl_pathv[i], &tmp)) {
+                       err = -1;
+                       goto out;
+               }
+               if (tmp_dst) {
+                       abs_dst = path_append(tmp_dst, tmp);
+                       xfree(tmp);
+               } else
+                       abs_dst = make_absolute(tmp, pwd);
+
+               printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
+               if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
+                       err = -1;
+       }
+
+out:
+       if (abs_dst)
+               xfree(abs_dst);
+       if (tmp_dst)
+               xfree(tmp_dst);
+       return(err);
 }
 
-int
+static int
 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
     char **path1, char **path2)
 {
@@ -445,7 +644,7 @@ parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
        return(cmdnum);
 }
 
-int
+static int
 parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
 {
        char *path1, *path2, *tmp;
@@ -459,68 +658,19 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
        path1 = path2 = NULL;
        cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
 
+       memset(&g, 0, sizeof(g));
+
        /* Perform command */
        switch (cmdnum) {
        case -1:
                break;
        case I_GET:
-               memset(&g, 0, sizeof(g));
-               if (!remote_glob(in, out, path1, 0, NULL, &g)) {
-                       if (path2) {
-                               /* XXX: target should be directory */
-                               error("You cannot specify a target when "
-                                   "downloading multiple files");
-                               err = -1;
-                               break;
-                       }
-                       for(i = 0; g.gl_pathv[i]; i++) {
-                               if (!infer_path(g.gl_pathv[i], &path2)) {
-                                       printf("Fetching %s\n", g.gl_pathv[i]);
-                                       if (do_download(in, out, g.gl_pathv[i],
-                                           path2, pflag) == -1)
-                                               err = -1;
-                                       free(path2);
-                                       path2 = NULL;
-                               } else
-                                       err = -1;
-                       }
-               } else {
-                       if (!path2 && infer_path(path1, &path2)) {
-                               err = -1;
-                               break;
-                       }
-                       err = do_download(in, out, path1, path2, pflag);
-               }
+               err = process_get(in, out, path1, path2, *pwd, pflag);
                break;
        case I_PUT:
-               if (!glob(path1, 0, NULL, &g)) {
-                       if (path2) {
-                               error("You cannot specify a target when "
-                                   "uploading multiple files");
-                               err = -1;
-                               break;
-                       }
-                       for(i = 0; g.gl_pathv[i]; i++) {
-                               if (!infer_path(g.gl_pathv[i], &path2)) {
-                                       path2 = make_absolute(path2, *pwd);
-                                       printf("Uploading %s\n", g.gl_pathv[i]);
-                                       if (do_upload(in, out, g.gl_pathv[i],
-                                           path2, pflag) == -1)
-                                               err = -1;
-                                       free(path2);
-                                       path2 = NULL;
-                               } else
-                                       err = -1;
-                       }
-               } else {
-                       if (!path2 && infer_path(path1, &path2)) {
-                               err = -1;
-                               break;
-                       }
-                       err = do_upload(in, out, path1, path2, pflag);
-               }
-               break;
-       case I_RENAME:
+               err = process_put(in, out, path1, path2, *pwd, pflag);
+               break;
+       case I_RENAME:
                path1 = make_absolute(path1, *pwd);
                path2 = make_absolute(path2, *pwd);
                err = do_rename(in, out, path1, path2);
@@ -561,7 +711,7 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
                        err = 1;
                        break;
                }
-               if ((aa = do_stat(in, out, tmp)) == NULL) {
+               if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
                        xfree(tmp);
                        err = 1;
                        break;
@@ -592,9 +742,9 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
                        break;
                xfree(path1);
                path1 = tmp;
-               if ((aa = do_stat(in, out, path1)) == NULL)
+               if ((aa = do_stat(in, out, path1, 0)) == NULL)
                        break;
-               if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && 
+               if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
                    !S_ISDIR(aa->perm)) {
                        error("Can't ls: \"%s\" is not a directory", path1);
                        break;
@@ -640,7 +790,7 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
                path1 = make_absolute(path1, *pwd);
                remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
                for(i = 0; g.gl_pathv[i]; i++) {
-                       if (!(aa = do_stat(in, out, g.gl_pathv[i])))
+                       if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
                                continue;
                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                                error("Can't get current ownership of "
@@ -657,7 +807,7 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
                path1 = make_absolute(path1, *pwd);
                remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
                for(i = 0; g.gl_pathv[i]; i++) {
-                       if (!(aa = do_stat(in, out, g.gl_pathv[i])))
+                       if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
                                continue;
                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                                error("Can't get current ownership of "
@@ -693,6 +843,8 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
                fatal("%d is not implemented", cmdnum);
        }
 
+       if (g.gl_pathc)
+               globfree(&g);
        if (path1)
                xfree(path1);
        if (path2)
@@ -706,9 +858,10 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
 }
 
 void
-interactive_loop(int fd_in, int fd_out)
+interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
 {
        char *pwd;
+       char *dir = NULL;
        char cmd[2048];
 
        version = do_init(fd_in, fd_out);
@@ -719,8 +872,32 @@ interactive_loop(int fd_in, int fd_out)
        if (pwd == NULL)
                fatal("Need cwd");
 
+       if (file1 != NULL) {
+               dir = xstrdup(file1);
+               dir = make_absolute(dir, pwd);
+
+               if (remote_is_dir(fd_in, fd_out, dir) && file2 == NULL) {
+                       printf("Changing to: %s\n", dir);
+                       snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
+                       parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
+               } else {
+                       if (file2 == NULL)
+                               snprintf(cmd, sizeof cmd, "get %s", dir);
+                       else
+                               snprintf(cmd, sizeof cmd, "get %s %s", dir,
+                                   file2);
+
+                       parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
+                       return;
+               }
+       }
+#if HAVE_SETVBUF
        setvbuf(stdout, NULL, _IOLBF, 0);
        setvbuf(infile, NULL, _IOLBF, 0);
+#else
+       setlinebuf(stdout);
+       setlinebuf(infile);
+#endif
 
        for(;;) {
                char *cp;
This page took 0.060416 seconds and 4 git commands to generate.