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 | |
Ben Lindstrom | 9492484 | 2001-04-10 02:40:17 +0000 | [diff] [blame] | 27 | RCSID("$OpenBSD: sftp.c,v 1.13 2001/04/08 20:52:55 deraadt Exp $"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 28 | |
| 29 | /* XXX: commandline mode */ |
| 30 | /* XXX: copy between two remote hosts (commandline) */ |
| 31 | /* XXX: short-form remote directory listings (like 'ls -C') */ |
| 32 | |
| 33 | #include "buffer.h" |
| 34 | #include "xmalloc.h" |
| 35 | #include "log.h" |
| 36 | #include "pathnames.h" |
| 37 | |
| 38 | #include "sftp.h" |
| 39 | #include "sftp-common.h" |
| 40 | #include "sftp-client.h" |
| 41 | #include "sftp-int.h" |
| 42 | |
Kevin Steves | 12888d1 | 2001-03-05 19:50:57 +0000 | [diff] [blame] | 43 | #ifdef HAVE___PROGNAME |
| 44 | extern char *__progname; |
| 45 | #else |
| 46 | char *__progname; |
| 47 | #endif |
| 48 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 49 | int use_ssh1 = 0; |
| 50 | char *ssh_program = _PATH_SSH_PROGRAM; |
| 51 | char *sftp_server = NULL; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 52 | FILE* infile; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 53 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 54 | void |
| 55 | connect_to_server(char **args, int *in, int *out, pid_t *sshpid) |
| 56 | { |
| 57 | int c_in, c_out; |
| 58 | #ifdef USE_PIPES |
| 59 | int pin[2], pout[2]; |
| 60 | if ((pipe(pin) == -1) || (pipe(pout) == -1)) |
| 61 | fatal("pipe: %s", strerror(errno)); |
| 62 | *in = pin[0]; |
| 63 | *out = pout[1]; |
| 64 | c_in = pout[0]; |
| 65 | c_out = pin[1]; |
| 66 | #else /* USE_PIPES */ |
| 67 | int inout[2]; |
| 68 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1) |
| 69 | fatal("socketpair: %s", strerror(errno)); |
| 70 | *in = *out = inout[0]; |
| 71 | c_in = c_out = inout[1]; |
| 72 | #endif /* USE_PIPES */ |
| 73 | |
| 74 | if ((*sshpid = fork()) == -1) |
| 75 | fatal("fork: %s", strerror(errno)); |
| 76 | else if (*sshpid == 0) { |
| 77 | if ((dup2(c_in, STDIN_FILENO) == -1) || |
| 78 | (dup2(c_out, STDOUT_FILENO) == -1)) { |
| 79 | fprintf(stderr, "dup2: %s\n", strerror(errno)); |
| 80 | exit(1); |
| 81 | } |
| 82 | close(*in); |
| 83 | close(*out); |
| 84 | close(c_in); |
| 85 | close(c_out); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 86 | execv(ssh_program, args); |
| 87 | fprintf(stderr, "exec: %s: %s\n", ssh_program, strerror(errno)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 88 | exit(1); |
| 89 | } |
| 90 | |
| 91 | close(c_in); |
| 92 | close(c_out); |
| 93 | } |
| 94 | |
| 95 | char ** |
| 96 | make_ssh_args(char *add_arg) |
| 97 | { |
| 98 | static char **args = NULL; |
| 99 | static int nargs = 0; |
| 100 | char debug_buf[4096]; |
Ben Lindstrom | 46d6e09 | 2001-03-05 07:10:47 +0000 | [diff] [blame] | 101 | int i; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 102 | |
| 103 | /* Init args array */ |
| 104 | if (args == NULL) { |
Ben Lindstrom | 46d6e09 | 2001-03-05 07:10:47 +0000 | [diff] [blame] | 105 | nargs = 2; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 106 | i = 0; |
| 107 | args = xmalloc(sizeof(*args) * nargs); |
| 108 | args[i++] = "ssh"; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 109 | args[i++] = NULL; |
| 110 | } |
| 111 | |
| 112 | /* If asked to add args, then do so and return */ |
| 113 | if (add_arg) { |
| 114 | i = nargs++ - 1; |
| 115 | args = xrealloc(args, sizeof(*args) * nargs); |
| 116 | args[i++] = add_arg; |
| 117 | args[i++] = NULL; |
| 118 | return(NULL); |
| 119 | } |
| 120 | |
Ben Lindstrom | 46d6e09 | 2001-03-05 07:10:47 +0000 | [diff] [blame] | 121 | /* no subsystem if the server-spec contains a '/' */ |
| 122 | if (sftp_server == NULL || strchr(sftp_server, '/') == NULL) |
| 123 | make_ssh_args("-s"); |
| 124 | make_ssh_args("-oForwardX11=no"); |
| 125 | make_ssh_args("-oForwardAgent=no"); |
| 126 | make_ssh_args(use_ssh1 ? "-oProtocol=1" : "-oProtocol=2"); |
| 127 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 128 | /* Otherwise finish up and return the arg array */ |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 129 | if (sftp_server != NULL) |
| 130 | make_ssh_args(sftp_server); |
| 131 | else |
| 132 | make_ssh_args("sftp"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 133 | |
| 134 | /* XXX: overflow - doesn't grow debug_buf */ |
| 135 | debug_buf[0] = '\0'; |
| 136 | for(i = 0; args[i]; i++) { |
| 137 | if (i) |
| 138 | strlcat(debug_buf, " ", sizeof(debug_buf)); |
| 139 | |
| 140 | strlcat(debug_buf, args[i], sizeof(debug_buf)); |
| 141 | } |
| 142 | debug("SSH args \"%s\"", debug_buf); |
| 143 | |
| 144 | return(args); |
| 145 | } |
| 146 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 147 | void |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 148 | usage(void) |
| 149 | { |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 150 | fprintf(stderr, "usage: sftp [-1vC] [-b batchfile] [-osshopt=value] [user@]host\n"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 151 | exit(1); |
| 152 | } |
| 153 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 154 | int |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 155 | main(int argc, char **argv) |
| 156 | { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 157 | int in, out, ch, debug_level, compress_flag; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 158 | pid_t sshpid; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 159 | char *host, *userhost; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 160 | LogLevel ll; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 161 | extern int optind; |
| 162 | extern char *optarg; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 163 | |
Kevin Steves | 12888d1 | 2001-03-05 19:50:57 +0000 | [diff] [blame] | 164 | __progname = get_progname(argv[0]); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 165 | infile = stdin; /* Read from STDIN unless changed by -b */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 166 | debug_level = compress_flag = 0; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 167 | |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 168 | while ((ch = getopt(argc, argv, "1hvCo:s:S:b:")) != -1) { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 169 | switch (ch) { |
| 170 | case 'C': |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 171 | compress_flag = 1; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 172 | break; |
| 173 | case 'v': |
| 174 | debug_level = MIN(3, debug_level + 1); |
| 175 | break; |
| 176 | case 'o': |
| 177 | make_ssh_args("-o"); |
| 178 | make_ssh_args(optarg); |
| 179 | break; |
| 180 | case '1': |
| 181 | use_ssh1 = 1; |
| 182 | if (sftp_server == NULL) |
| 183 | sftp_server = _PATH_SFTP_SERVER; |
| 184 | break; |
| 185 | case 's': |
| 186 | sftp_server = optarg; |
| 187 | break; |
| 188 | case 'S': |
| 189 | ssh_program = optarg; |
| 190 | break; |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 191 | case 'b': |
| 192 | if (infile == stdin) { |
| 193 | infile = fopen(optarg, "r"); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 194 | if (infile == NULL) |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 195 | fatal("%s (%s).", strerror(errno), optarg); |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 196 | } else |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 197 | fatal("Filename already specified."); |
| 198 | break; |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 199 | case 'h': |
| 200 | default: |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 201 | usage(); |
| 202 | } |
| 203 | } |
| 204 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 205 | if (optind == argc || argc > (optind + 1)) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 206 | usage(); |
| 207 | |
Ben Lindstrom | 9492484 | 2001-04-10 02:40:17 +0000 | [diff] [blame] | 208 | userhost = xstrdup(argv[optind]); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 209 | |
| 210 | if ((host = strchr(userhost, '@')) == NULL) |
| 211 | host = userhost; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 212 | else { |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 213 | *host = '\0'; |
| 214 | if (!userhost[0]) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 215 | fprintf(stderr, "Missing username\n"); |
| 216 | usage(); |
| 217 | } |
| 218 | make_ssh_args("-l"); |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 219 | make_ssh_args(userhost); |
| 220 | host++; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 221 | } |
| 222 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 223 | if (!*host) { |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 224 | fprintf(stderr, "Missing hostname\n"); |
| 225 | usage(); |
| 226 | } |
| 227 | |
| 228 | /* Set up logging and debug '-d' arguments to ssh */ |
| 229 | ll = SYSLOG_LEVEL_INFO; |
| 230 | switch (debug_level) { |
| 231 | case 1: |
| 232 | ll = SYSLOG_LEVEL_DEBUG1; |
| 233 | make_ssh_args("-v"); |
| 234 | break; |
| 235 | case 2: |
| 236 | ll = SYSLOG_LEVEL_DEBUG2; |
| 237 | make_ssh_args("-v"); |
| 238 | make_ssh_args("-v"); |
| 239 | break; |
| 240 | case 3: |
| 241 | ll = SYSLOG_LEVEL_DEBUG3; |
| 242 | make_ssh_args("-v"); |
| 243 | make_ssh_args("-v"); |
| 244 | make_ssh_args("-v"); |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | if (compress_flag) |
| 249 | make_ssh_args("-C"); |
| 250 | |
| 251 | log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); |
| 252 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 253 | make_ssh_args(host); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 254 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 255 | fprintf(stderr, "Connecting to %s...\n", host); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 256 | |
| 257 | connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid); |
| 258 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 259 | interactive_loop(in, out); |
| 260 | |
Ben Lindstrom | 10b9bf9 | 2001-02-26 20:04:45 +0000 | [diff] [blame] | 261 | #if !defined(USE_PIPES) |
| 262 | shutdown(in, SHUT_RDWR); |
| 263 | shutdown(out, SHUT_RDWR); |
| 264 | #endif |
| 265 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 266 | close(in); |
| 267 | close(out); |
Ben Lindstrom | 562c26b | 2001-03-07 01:26:48 +0000 | [diff] [blame] | 268 | if (infile != stdin) |
| 269 | fclose(infile); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 270 | |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 271 | if (waitpid(sshpid, NULL, 0) == -1) |
| 272 | fatal("Couldn't wait for ssh process: %s", strerror(errno)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 273 | |
| 274 | exit(0); |
| 275 | } |