Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2001 Damien Miller. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | #include "includes.h" |
| 26 | |
Damien Miller | 21134b5 | 2001-04-16 18:26:41 +1000 | [diff] [blame] | 27 | RCSID("$OpenBSD: sftp.c,v 1.15 2001/04/16 02:31:44 mouring Exp $"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 28 | |
| 29 | /* XXX: commandline mode */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 30 | /* XXX: short-form remote directory listings (like 'ls -C') */ |
| 31 | |
| 32 | #include "buffer.h" |
| 33 | #include "xmalloc.h" |
| 34 | #include "log.h" |
| 35 | #include "pathnames.h" |
| 36 | |
| 37 | #include "sftp.h" |
| 38 | #include "sftp-common.h" |
| 39 | #include "sftp-client.h" |
| 40 | #include "sftp-int.h" |
| 41 | |
Kevin Steves | 12888d1 | 2001-03-05 19:50:57 +0000 | [diff] [blame] | 42 | #ifdef HAVE___PROGNAME |
| 43 | extern char *__progname; |
| 44 | #else |
| 45 | char *__progname; |
| 46 | #endif |
| 47 | |
Damien Miller | 21134b5 | 2001-04-16 18:26:41 +1000 | [diff] [blame] | 48 | #include "scp-common.h" |
| 49 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 50 | int use_ssh1 = 0; |
| 51 | char *ssh_program = _PATH_SSH_PROGRAM; |
| 52 | char *sftp_server = NULL; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 53 | FILE* infile; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 54 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 55 | void |
| 56 | connect_to_server(char **args, int *in, int *out, pid_t *sshpid) |
| 57 | { |
| 58 | int c_in, c_out; |
| 59 | #ifdef USE_PIPES |
| 60 | int pin[2], pout[2]; |
| 61 | if ((pipe(pin) == -1) || (pipe(pout) == -1)) |
| 62 | fatal("pipe: %s", strerror(errno)); |
| 63 | *in = pin[0]; |
| 64 | *out = pout[1]; |
| 65 | c_in = pout[0]; |
| 66 | c_out = pin[1]; |
| 67 | #else /* USE_PIPES */ |
| 68 | int inout[2]; |
| 69 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1) |
| 70 | fatal("socketpair: %s", strerror(errno)); |
| 71 | *in = *out = inout[0]; |
| 72 | c_in = c_out = inout[1]; |
| 73 | #endif /* USE_PIPES */ |
| 74 | |
| 75 | if ((*sshpid = fork()) == -1) |
| 76 | fatal("fork: %s", strerror(errno)); |
| 77 | else if (*sshpid == 0) { |
| 78 | if ((dup2(c_in, STDIN_FILENO) == -1) || |
| 79 | (dup2(c_out, STDOUT_FILENO) == -1)) { |
| 80 | fprintf(stderr, "dup2: %s\n", strerror(errno)); |
| 81 | exit(1); |
| 82 | } |
| 83 | close(*in); |
| 84 | close(*out); |
| 85 | close(c_in); |
| 86 | close(c_out); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 87 | execv(ssh_program, args); |
| 88 | fprintf(stderr, "exec: %s: %s\n", ssh_program, strerror(errno)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 89 | exit(1); |
| 90 | } |
| 91 | |
| 92 | close(c_in); |
| 93 | close(c_out); |
| 94 | } |
| 95 | |
| 96 | char ** |
| 97 | make_ssh_args(char *add_arg) |
| 98 | { |
| 99 | static char **args = NULL; |
| 100 | static int nargs = 0; |
| 101 | char debug_buf[4096]; |
Ben Lindstrom | 46d6e09 | 2001-03-05 07:10:47 +0000 | [diff] [blame] | 102 | int i; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 103 | |
| 104 | /* Init args array */ |
| 105 | if (args == NULL) { |
Ben Lindstrom | 46d6e09 | 2001-03-05 07:10:47 +0000 | [diff] [blame] | 106 | nargs = 2; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 107 | i = 0; |
| 108 | args = xmalloc(sizeof(*args) * nargs); |
| 109 | args[i++] = "ssh"; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 110 | args[i++] = NULL; |
| 111 | } |
| 112 | |
| 113 | /* If asked to add args, then do so and return */ |
| 114 | if (add_arg) { |
| 115 | i = nargs++ - 1; |
| 116 | args = xrealloc(args, sizeof(*args) * nargs); |
| 117 | args[i++] = add_arg; |
| 118 | args[i++] = NULL; |
| 119 | return(NULL); |
| 120 | } |
| 121 | |
Ben Lindstrom | 46d6e09 | 2001-03-05 07:10:47 +0000 | [diff] [blame] | 122 | /* no subsystem if the server-spec contains a '/' */ |
| 123 | if (sftp_server == NULL || strchr(sftp_server, '/') == NULL) |
| 124 | make_ssh_args("-s"); |
| 125 | make_ssh_args("-oForwardX11=no"); |
| 126 | make_ssh_args("-oForwardAgent=no"); |
| 127 | make_ssh_args(use_ssh1 ? "-oProtocol=1" : "-oProtocol=2"); |
| 128 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 129 | /* Otherwise finish up and return the arg array */ |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 130 | if (sftp_server != NULL) |
| 131 | make_ssh_args(sftp_server); |
| 132 | else |
| 133 | make_ssh_args("sftp"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 134 | |
| 135 | /* XXX: overflow - doesn't grow debug_buf */ |
| 136 | debug_buf[0] = '\0'; |
| 137 | for(i = 0; args[i]; i++) { |
| 138 | if (i) |
| 139 | strlcat(debug_buf, " ", sizeof(debug_buf)); |
| 140 | |
| 141 | strlcat(debug_buf, args[i], sizeof(debug_buf)); |
| 142 | } |
| 143 | debug("SSH args \"%s\"", debug_buf); |
| 144 | |
| 145 | return(args); |
| 146 | } |
| 147 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 148 | void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 149 | usage(void) |
| 150 | { |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 151 | fprintf(stderr, "usage: sftp [-1vC] [-b batchfile] [-osshopt=value] [user@]host[:file [file]]\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 152 | exit(1); |
| 153 | } |
| 154 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 155 | int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 156 | main(int argc, char **argv) |
| 157 | { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 158 | int in, out, ch, debug_level, compress_flag; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 159 | pid_t sshpid; |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 160 | char *file1 = NULL; |
| 161 | char *host, *userhost, *cp, *file2; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 162 | LogLevel ll; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 163 | extern int optind; |
| 164 | extern char *optarg; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 165 | |
Kevin Steves | 12888d1 | 2001-03-05 19:50:57 +0000 | [diff] [blame] | 166 | __progname = get_progname(argv[0]); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 167 | infile = stdin; /* Read from STDIN unless changed by -b */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 168 | debug_level = compress_flag = 0; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 169 | |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 170 | while ((ch = getopt(argc, argv, "1hvCo:s:S:b:")) != -1) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 171 | switch (ch) { |
| 172 | case 'C': |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 173 | compress_flag = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 174 | break; |
| 175 | case 'v': |
| 176 | debug_level = MIN(3, debug_level + 1); |
| 177 | break; |
| 178 | case 'o': |
| 179 | make_ssh_args("-o"); |
| 180 | make_ssh_args(optarg); |
| 181 | break; |
| 182 | case '1': |
| 183 | use_ssh1 = 1; |
| 184 | if (sftp_server == NULL) |
| 185 | sftp_server = _PATH_SFTP_SERVER; |
| 186 | break; |
| 187 | case 's': |
| 188 | sftp_server = optarg; |
| 189 | break; |
| 190 | case 'S': |
| 191 | ssh_program = optarg; |
| 192 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 193 | case 'b': |
| 194 | if (infile == stdin) { |
| 195 | infile = fopen(optarg, "r"); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 196 | if (infile == NULL) |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 197 | fatal("%s (%s).", strerror(errno), optarg); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 198 | } else |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 199 | fatal("Filename already specified."); |
| 200 | break; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 201 | case 'h': |
| 202 | default: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 203 | usage(); |
| 204 | } |
| 205 | } |
| 206 | |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 207 | if (optind == argc || argc > (optind + 2)) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 208 | usage(); |
| 209 | |
Ben Lindstrom | 9492484 | 2001-04-10 02:40:17 +0000 | [diff] [blame] | 210 | userhost = xstrdup(argv[optind]); |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 211 | file2 = argv[optind+1]; |
| 212 | |
Damien Miller | 21134b5 | 2001-04-16 18:26:41 +1000 | [diff] [blame] | 213 | if ((cp = colon(userhost)) != NULL) { |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 214 | *cp++ = '\0'; |
| 215 | file1 = cp; |
| 216 | } |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 217 | |
| 218 | if ((host = strchr(userhost, '@')) == NULL) |
| 219 | host = userhost; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 220 | else { |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 221 | *host++ = '\0'; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 222 | if (!userhost[0]) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 223 | fprintf(stderr, "Missing username\n"); |
| 224 | usage(); |
| 225 | } |
| 226 | make_ssh_args("-l"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 227 | make_ssh_args(userhost); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 228 | } |
| 229 | |
Damien Miller | 21134b5 | 2001-04-16 18:26:41 +1000 | [diff] [blame] | 230 | host = cleanhostname(host); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 231 | if (!*host) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 232 | fprintf(stderr, "Missing hostname\n"); |
| 233 | usage(); |
| 234 | } |
| 235 | |
| 236 | /* Set up logging and debug '-d' arguments to ssh */ |
| 237 | ll = SYSLOG_LEVEL_INFO; |
| 238 | switch (debug_level) { |
| 239 | case 1: |
| 240 | ll = SYSLOG_LEVEL_DEBUG1; |
| 241 | make_ssh_args("-v"); |
| 242 | break; |
| 243 | case 2: |
| 244 | ll = SYSLOG_LEVEL_DEBUG2; |
| 245 | make_ssh_args("-v"); |
| 246 | make_ssh_args("-v"); |
| 247 | break; |
| 248 | case 3: |
| 249 | ll = SYSLOG_LEVEL_DEBUG3; |
| 250 | make_ssh_args("-v"); |
| 251 | make_ssh_args("-v"); |
| 252 | make_ssh_args("-v"); |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | if (compress_flag) |
| 257 | make_ssh_args("-C"); |
| 258 | |
| 259 | log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); |
| 260 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 261 | make_ssh_args(host); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 262 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 263 | fprintf(stderr, "Connecting to %s...\n", host); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 264 | |
| 265 | connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid); |
| 266 | |
Ben Lindstrom | 63667f6 | 2001-04-13 00:00:14 +0000 | [diff] [blame] | 267 | interactive_loop(in, out, file1, file2); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 268 | |
Ben Lindstrom | 10b9bf9 | 2001-02-26 20:04:45 +0000 | [diff] [blame] | 269 | #if !defined(USE_PIPES) |
| 270 | shutdown(in, SHUT_RDWR); |
| 271 | shutdown(out, SHUT_RDWR); |
| 272 | #endif |
| 273 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 274 | close(in); |
| 275 | close(out); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 276 | if (infile != stdin) |
| 277 | fclose(infile); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 278 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 279 | if (waitpid(sshpid, NULL, 0) == -1) |
| 280 | fatal("Couldn't wait for ssh process: %s", strerror(errno)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 281 | |
| 282 | exit(0); |
| 283 | } |