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