]> andersk Git - openssh.git/blobdiff - sftp.c
- (tim) [configure.ac] Move CHECK_HEADERS test before platform specific
[openssh.git] / sftp.c
diff --git a/sftp.c b/sftp.c
index 9e29cb02e14ff3e258dffb2b34e54851bdb09b49..c018615ae61b13f27d9884e0755f317e52d37250 100644 (file)
--- a/sftp.c
+++ b/sftp.c
@@ -1,3 +1,4 @@
+/* $OpenBSD: sftp.c,v 1.92 2006/09/19 05:52:23 otto Exp $ */
 /*
  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
  *
 
 #include "includes.h"
 
-RCSID("$OpenBSD: sftp.c,v 1.60 2004/12/10 03:10:42 fgsch Exp $");
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#ifdef HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+
+#include <errno.h>
 
+#ifdef HAVE_PATHS_H
+# include <paths.h>
+#endif
 #ifdef USE_LIBEDIT
 #include <histedit.h>
 #else
 typedef void EditLine;
 #endif
+#include <signal.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdarg.h>
 
-#include "buffer.h"
 #include "xmalloc.h"
 #include "log.h"
 #include "pathnames.h"
 #include "misc.h"
 
 #include "sftp.h"
+#include "buffer.h"
 #include "sftp-common.h"
 #include "sftp-client.h"
 
@@ -150,8 +169,10 @@ int interactive_loop(int fd_in, int fd_out, char *file1, char *file2);
 static void
 killchild(int signo)
 {
-       if (sshpid > 1)
+       if (sshpid > 1) {
                kill(sshpid, SIGTERM);
+               waitpid(sshpid, NULL, 0);
+       }
 
        _exit(1);
 }
@@ -233,7 +254,7 @@ local_do_shell(const char *args)
                if (errno != EINTR)
                        fatal("Couldn't wait for child: %s", strerror(errno));
        if (!WIFEXITED(status))
-               error("Shell exited abormally");
+               error("Shell exited abnormally");
        else if (WEXITSTATUS(status))
                error("Shell exited with status %d", WEXITSTATUS(status));
 }
@@ -355,7 +376,7 @@ parse_ls_flags(const char **cpp, int *lflag)
 
        /* Check for flags */
        if (cp++[0] == '-') {
-               for(; strchr(WHITESPACE, *cp) == NULL; cp++) {
+               for (; strchr(WHITESPACE, *cp) == NULL; cp++) {
                        switch (*cp) {
                        case 'l':
                                *lflag &= ~VIEW_FLAGS;
@@ -402,7 +423,7 @@ get_pathname(const char **cpp, char **path)
 {
        const char *cp = *cpp, *end;
        char quot;
-       int i, j;
+       u_int i, j;
 
        cp += strspn(cp, WHITESPACE);
        if (!*cp) {
@@ -472,7 +493,7 @@ is_dir(char *path)
        if (stat(path, &sb) == -1)
                return(0);
 
-       return(sb.st_mode & S_IFDIR);
+       return(S_ISDIR(sb.st_mode));
 }
 
 static int
@@ -496,7 +517,7 @@ remote_is_dir(struct sftp_conn *conn, char *path)
                return(0);
        if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
                return(0);
-       return(a->perm & S_IFDIR);
+       return(S_ISDIR(a->perm));
 }
 
 static int
@@ -536,6 +557,7 @@ process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
 
                if (g.gl_matchc == 1 && dst) {
                        /* If directory specified, append filename */
+                       xfree(tmp);
                        if (is_dir(dst)) {
                                if (infer_path(g.gl_pathv[0], &tmp)) {
                                        err = 1;
@@ -560,8 +582,6 @@ process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
 
 out:
        xfree(abs_src);
-       if (abs_dst)
-               xfree(abs_dst);
        globfree(&g);
        return(err);
 }
@@ -662,14 +682,15 @@ sdirent_comp(const void *aa, const void *bb)
 static int
 do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
 {
-       int n, c = 1, colspace = 0, columns = 1;
+       int n;
+       u_int c = 1, colspace = 0, columns = 1;
        SFTP_DIRENT **d;
 
        if ((n = do_readdir(conn, path, &d)) != 0)
                return (n);
 
        if (!(lflag & LS_SHORT_VIEW)) {
-               int m = 0, width = 80;
+               u_int m = 0, width = 80;
                struct winsize ws;
                char *tmp;
 
@@ -694,6 +715,8 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
        }
 
        if (lflag & SORT_FLAGS) {
+               for (n = 0; d[n] != NULL; n++)
+                       ;       /* count entries */
                sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
                qsort(d, n, sizeof(*d), sdirent_comp);
        }
@@ -745,7 +768,7 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
     int lflag)
 {
        glob_t g;
-       int i, c = 1, colspace = 0, columns = 1;
+       u_int i, c = 1, colspace = 0, columns = 1;
        Attrib *a = NULL;
 
        memset(&g, 0, sizeof(g));
@@ -781,7 +804,7 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
        }
 
        if (!(lflag & LS_SHORT_VIEW)) {
-               int m = 0, width = 80;
+               u_int m = 0, width = 80;
                struct winsize ws;
 
                /* Count entries for sort and find longest filename */
@@ -1234,7 +1257,7 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
        char *dir = NULL;
        char cmd[2048];
        struct sftp_conn *conn;
-       int err;
+       int err, interactive;
        EditLine *el = NULL;
 #ifdef USE_LIBEDIT
        History *hl = NULL;
@@ -1275,6 +1298,7 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
                        if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) {
                                xfree(dir);
                                xfree(pwd);
+                               xfree(conn);
                                return (-1);
                        }
                } else {
@@ -1287,19 +1311,21 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
                        err = parse_dispatch_command(conn, cmd, &pwd, 1);
                        xfree(dir);
                        xfree(pwd);
+                       xfree(conn);
                        return (err);
                }
                xfree(dir);
        }
 
-#if HAVE_SETVBUF
+#if defined(HAVE_SETVBUF) && !defined(BROKEN_SETVBUF)
        setvbuf(stdout, NULL, _IOLBF, 0);
        setvbuf(infile, NULL, _IOLBF, 0);
 #else
-       setlinebuf(stdout);
-       setlinebuf(infile);
+       setlinebuf(stdout);
+       setlinebuf(infile);
 #endif
 
+       interactive = !batchmode && isatty(STDIN_FILENO);
        err = 0;
        for (;;) {
                char *cp;
@@ -1307,20 +1333,28 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
                signal(SIGINT, SIG_IGN);
 
                if (el == NULL) {
-                       printf("sftp> ");
+                       if (interactive)
+                               printf("sftp> ");
                        if (fgets(cmd, sizeof(cmd), infile) == NULL) {
-                               printf("\n");
+                               if (interactive)
+                                       printf("\n");
                                break;
                        }
-                       if (batchmode) /* Echo command */
-                               printf("%s", cmd);
+                       if (!interactive) { /* Echo command */
+                               printf("sftp> %s", cmd);
+                               if (strlen(cmd) > 0 &&
+                                   cmd[strlen(cmd) - 1] != '\n')
+                                       printf("\n");
+                       }
                } else {
 #ifdef USE_LIBEDIT
                        const char *line;
                        int count = 0;
 
-                       if ((line = el_gets(el, &count)) == NULL || count <= 0)
-                               break;
+                       if ((line = el_gets(el, &count)) == NULL || count <= 0) {
+                               printf("\n");
+                               break;
+                       }
                        history(hl, &hev, H_ENTER, line);
                        if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
                                fprintf(stderr, "Error: input line too long\n");
@@ -1342,6 +1376,12 @@ interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
                        break;
        }
        xfree(pwd);
+       xfree(conn);
+
+#ifdef USE_LIBEDIT
+       if (el != NULL)
+               el_end(el);
+#endif /* USE_LIBEDIT */
 
        /* err == 1 signifies normal "quit" exit */
        return (err >= 0 ? 0 : -1);
@@ -1430,11 +1470,16 @@ main(int argc, char **argv)
        extern int optind;
        extern char *optarg;
 
+       /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
+       sanitise_stdfd();
+
        __progname = ssh_get_progname(argv[0]);
+       memset(&args, '\0', sizeof(args));
        args.list = NULL;
-       addargs(&args, "ssh");          /* overwritten with ssh_program */
+       addargs(&args, "%s", ssh_program);
        addargs(&args, "-oForwardX11 no");
        addargs(&args, "-oForwardAgent no");
+       addargs(&args, "-oPermitLocalCommand no");
        addargs(&args, "-oClearAllForwardings yes");
 
        ll = SYSLOG_LEVEL_INFO;
@@ -1466,6 +1511,7 @@ main(int argc, char **argv)
                        break;
                case 'S':
                        ssh_program = optarg;
+                       replacearg(&args, 0, "%s", ssh_program);
                        break;
                case 'b':
                        if (batchmode)
@@ -1473,10 +1519,11 @@ main(int argc, char **argv)
 
                        /* Allow "-" as stdin */
                        if (strcmp(optarg, "-") != 0 &&
-                          (infile = fopen(optarg, "r")) == NULL)
+                           (infile = fopen(optarg, "r")) == NULL)
                                fatal("%s (%s).", strerror(errno), optarg);
                        showprogress = 0;
                        batchmode = 1;
+                       addargs(&args, "-obatchmode yes");
                        break;
                case 'P':
                        sftp_direct = optarg;
@@ -1541,7 +1588,6 @@ main(int argc, char **argv)
                addargs(&args, "%s", host);
                addargs(&args, "%s", (sftp_server != NULL ?
                    sftp_server : "sftp"));
-               args.list[0] = ssh_program;
 
                if (!batchmode)
                        fprintf(stderr, "Connecting to %s...\n", host);
@@ -1554,12 +1600,13 @@ main(int argc, char **argv)
                        fprintf(stderr, "Attaching to %s...\n", sftp_direct);
                connect_to_server(sftp_direct, args.list, &in, &out);
        }
+       freeargs(&args);
 
        err = interactive_loop(in, out, file1, file2);
 
 #if !defined(USE_PIPES)
-       shutdown(in, SHUT_RDWR);
-       shutdown(out, SHUT_RDWR);
+       shutdown(in, SHUT_RDWR);
+       shutdown(out, SHUT_RDWR);
 #endif
 
        close(in);
This page took 0.05064 seconds and 4 git commands to generate.