]> andersk Git - openssh.git/blobdiff - sftp.c
[configure.ac] Make sure -lcrypto is before -lsocket for sco3. ok mouring@
[openssh.git] / sftp.c
diff --git a/sftp.c b/sftp.c
index c173e58cb6b12ae40abc03d516a204b3495fa3cf..fef28c3b7cbf4b5120449379fe692283cc386400 100644 (file)
--- a/sftp.c
+++ b/sftp.c
@@ -24,9 +24,7 @@
 
 #include "includes.h"
 
-RCSID("$OpenBSD: sftp.c,v 1.32 2002/11/27 17:53:35 markus Exp $");
-
-/* XXX: short-form remote directory listings (like 'ls -C') */
+RCSID("$OpenBSD: sftp.c,v 1.41 2004/01/27 10:08:10 djm Exp $");
 
 #include "buffer.h"
 #include "xmalloc.h"
@@ -46,11 +44,24 @@ char *__progname;
 #endif
 
 FILE* infile;
+int batchmode = 0;
 size_t copy_buffer_len = 32768;
 size_t num_requests = 16;
+static pid_t sshpid = -1;
+
+extern int showprogress;
+
+static void
+killchild(int signo)
+{
+       if (sshpid > 1)
+               kill(sshpid, signo);
+
+       _exit(1);
+}
 
 static void
-connect_to_server(char *path, char **args, int *in, int *out, pid_t *sshpid)
+connect_to_server(char *path, char **args, int *in, int *out)
 {
        int c_in, c_out;
 
@@ -72,9 +83,9 @@ connect_to_server(char *path, char **args, int *in, int *out, pid_t *sshpid)
        c_in = c_out = inout[1];
 #endif /* USE_PIPES */
 
-       if ((*sshpid = fork()) == -1)
+       if ((sshpid = fork()) == -1)
                fatal("fork: %s", strerror(errno));
-       else if (*sshpid == 0) {
+       else if (sshpid == 0) {
                if ((dup2(c_in, STDIN_FILENO) == -1) ||
                    (dup2(c_out, STDOUT_FILENO) == -1)) {
                        fprintf(stderr, "dup2: %s\n", strerror(errno));
@@ -89,6 +100,9 @@ connect_to_server(char *path, char **args, int *in, int *out, pid_t *sshpid)
                exit(1);
        }
 
+       signal(SIGTERM, killchild);
+       signal(SIGINT, killchild);
+       signal(SIGHUP, killchild);
        close(c_in);
        close(c_out);
 }
@@ -99,17 +113,19 @@ usage(void)
        extern char *__progname;
 
        fprintf(stderr,
-           "usage: %s [-vC1] [-b batchfile] [-o option] [-s subsystem|path] [-B buffer_size]\n"
-           "            [-F config] [-P direct server path] [-S program]\n"
-           "            [user@]host[:file [file]]\n", __progname);
+           "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n"
+           "            [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n"
+           "            [-S program] [-s subsystem | sftp_server] host\n"
+           "       %s [[user@]host[:file [file]]]\n"
+           "       %s [[user@]host[:dir[/]]]\n"
+           "       %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname);
        exit(1);
 }
 
 int
 main(int argc, char **argv)
 {
-       int in, out, ch;
-       pid_t sshpid;
+       int in, out, ch, err;
        char *host, *userhost, *cp, *file2;
        int debug_level = 0, sshver = 2;
        char *file1 = NULL, *sftp_server = NULL;
@@ -119,14 +135,15 @@ main(int argc, char **argv)
        extern int optind;
        extern char *optarg;
 
-       __progname = get_progname(argv[0]);
+       __progname = ssh_get_progname(argv[0]);
        args.list = NULL;
        addargs(&args, "ssh");          /* overwritten with ssh_program */
        addargs(&args, "-oForwardX11 no");
        addargs(&args, "-oForwardAgent no");
        addargs(&args, "-oClearAllForwardings yes");
+
        ll = SYSLOG_LEVEL_INFO;
-       infile = stdin;         /* Read from STDIN unless changed by -b */
+       infile = stdin;
 
        while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
                switch (ch) {
@@ -156,12 +173,15 @@ main(int argc, char **argv)
                        ssh_program = optarg;
                        break;
                case 'b':
-                       if (infile == stdin) {
-                               infile = fopen(optarg, "r");
-                               if (infile == NULL)
-                                       fatal("%s (%s).", strerror(errno), optarg);
-                       } else
-                               fatal("Filename already specified.");
+                       if (batchmode)
+                               fatal("Batch file already specified.");
+
+                       /* Allow "-" as stdin */
+                       if (strcmp(optarg, "-") != 0 && 
+                          (infile = fopen(optarg, "r")) == NULL)
+                               fatal("%s (%s).", strerror(errno), optarg);
+                       showprogress = 0;
+                       batchmode = 1;
                        break;
                case 'P':
                        sftp_direct = optarg;
@@ -192,11 +212,6 @@ main(int argc, char **argv)
                userhost = xstrdup(argv[optind]);
                file2 = argv[optind+1];
 
-               if ((cp = colon(userhost)) != NULL) {
-                       *cp++ = '\0';
-                       file1 = cp;
-               }
-
                if ((host = strrchr(userhost, '@')) == NULL)
                        host = userhost;
                else {
@@ -208,6 +223,11 @@ main(int argc, char **argv)
                        addargs(&args, "-l%s",userhost);
                }
 
+               if ((cp = colon(host)) != NULL) {
+                       *cp++ = '\0';
+                       file1 = cp;
+               }
+
                host = cleanhostname(host);
                if (!*host) {
                        fprintf(stderr, "Missing hostname\n");
@@ -225,19 +245,19 @@ main(int argc, char **argv)
                    sftp_server : "sftp"));
                args.list[0] = ssh_program;
 
-               fprintf(stderr, "Connecting to %s...\n", host);
-               connect_to_server(ssh_program, args.list, &in, &out,
-                   &sshpid);
+               if (!batchmode)
+                       fprintf(stderr, "Connecting to %s...\n", host);
+               connect_to_server(ssh_program, args.list, &in, &out);
        } else {
                args.list = NULL;
                addargs(&args, "sftp-server");
 
-               fprintf(stderr, "Attaching to %s...\n", sftp_direct);
-               connect_to_server(sftp_direct, args.list, &in, &out,
-                   &sshpid);
+               if (!batchmode)
+                       fprintf(stderr, "Attaching to %s...\n", sftp_direct);
+               connect_to_server(sftp_direct, args.list, &in, &out);
        }
 
-       interactive_loop(in, out, file1, file2);
+       err = interactive_loop(in, out, file1, file2);
 
 #if !defined(USE_PIPES)
        shutdown(in, SHUT_RDWR);
@@ -246,7 +266,7 @@ main(int argc, char **argv)
 
        close(in);
        close(out);
-       if (infile != stdin)
+       if (batchmode)
                fclose(infile);
 
        while (waitpid(sshpid, NULL, 0) == -1)
@@ -254,5 +274,5 @@ main(int argc, char **argv)
                        fatal("Couldn't wait for ssh process: %s",
                            strerror(errno));
 
-       exit(0);
+       exit(err == 0 ? 0 : 1);
 }
This page took 0.040251 seconds and 4 git commands to generate.