blob: a6e976e58c8878fe07505ff98f40df45a3ded244 [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
Damien Miller4e60ed72004-02-17 17:07:59 +11002 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller33804262001-02-04 23:20:18 +11003 *
Damien Miller4e60ed72004-02-17 17:07:59 +11004 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
Damien Miller33804262001-02-04 23:20:18 +11007 *
Damien Miller4e60ed72004-02-17 17:07:59 +11008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller33804262001-02-04 23:20:18 +110015 */
16
17#include "includes.h"
18
Damien Miller4e60ed72004-02-17 17:07:59 +110019RCSID("$OpenBSD: sftp.c,v 1.42 2004/02/17 05:39:51 djm Exp $");
Damien Miller33804262001-02-04 23:20:18 +110020
21#include "buffer.h"
22#include "xmalloc.h"
23#include "log.h"
24#include "pathnames.h"
Ben Lindstrom4529b702001-05-03 23:39:53 +000025#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110026
27#include "sftp.h"
28#include "sftp-common.h"
29#include "sftp-client.h"
30#include "sftp-int.h"
31
Kevin Steves12888d12001-03-05 19:50:57 +000032#ifdef HAVE___PROGNAME
33extern char *__progname;
34#else
35char *__progname;
36#endif
37
Damien Millere4f5a822004-01-21 14:11:05 +110038FILE* infile;
Damien Miller44f75c12004-01-21 10:58:47 +110039int batchmode = 0;
Damien Miller8829d362002-02-08 22:04:05 +110040size_t copy_buffer_len = 32768;
Damien Miller16a13332002-02-13 14:03:56 +110041size_t num_requests = 16;
Damien Millercc685c12003-06-04 22:51:38 +100042static pid_t sshpid = -1;
Damien Millerd7686fd2001-02-10 00:40:03 +110043
Damien Miller62d57f62003-01-10 21:43:24 +110044extern int showprogress;
45
Ben Lindstrombba81212001-06-25 05:01:22 +000046static void
Damien Millercc685c12003-06-04 22:51:38 +100047killchild(int signo)
48{
49 if (sshpid > 1)
50 kill(sshpid, signo);
51
52 _exit(1);
53}
54
55static void
56connect_to_server(char *path, char **args, int *in, int *out)
Damien Miller33804262001-02-04 23:20:18 +110057{
58 int c_in, c_out;
Ben Lindstromb1f483f2002-06-23 21:27:18 +000059
Damien Miller33804262001-02-04 23:20:18 +110060#ifdef USE_PIPES
61 int pin[2], pout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +000062
Damien Miller33804262001-02-04 23:20:18 +110063 if ((pipe(pin) == -1) || (pipe(pout) == -1))
64 fatal("pipe: %s", strerror(errno));
65 *in = pin[0];
66 *out = pout[1];
67 c_in = pout[0];
68 c_out = pin[1];
69#else /* USE_PIPES */
70 int inout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +000071
Damien Miller33804262001-02-04 23:20:18 +110072 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
73 fatal("socketpair: %s", strerror(errno));
74 *in = *out = inout[0];
75 c_in = c_out = inout[1];
76#endif /* USE_PIPES */
77
Damien Millercc685c12003-06-04 22:51:38 +100078 if ((sshpid = fork()) == -1)
Damien Miller33804262001-02-04 23:20:18 +110079 fatal("fork: %s", strerror(errno));
Damien Millercc685c12003-06-04 22:51:38 +100080 else if (sshpid == 0) {
Damien Miller33804262001-02-04 23:20:18 +110081 if ((dup2(c_in, STDIN_FILENO) == -1) ||
82 (dup2(c_out, STDOUT_FILENO) == -1)) {
83 fprintf(stderr, "dup2: %s\n", strerror(errno));
84 exit(1);
85 }
86 close(*in);
87 close(*out);
88 close(c_in);
89 close(c_out);
Damien Millerd14ee1e2002-02-05 12:27:31 +110090 execv(path, args);
91 fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +110092 exit(1);
93 }
94
Damien Millercc685c12003-06-04 22:51:38 +100095 signal(SIGTERM, killchild);
96 signal(SIGINT, killchild);
97 signal(SIGHUP, killchild);
Damien Miller33804262001-02-04 23:20:18 +110098 close(c_in);
99 close(c_out);
100}
101
Ben Lindstrombba81212001-06-25 05:01:22 +0000102static void
Damien Miller33804262001-02-04 23:20:18 +1100103usage(void)
104{
Damien Miller025e01c2002-02-08 22:06:29 +1100105 extern char *__progname;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000106
Ben Lindstrom1e243242001-09-18 05:38:44 +0000107 fprintf(stderr,
Darren Tucker1f203942003-10-15 15:50:42 +1000108 "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n"
109 " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n"
110 " [-S program] [-s subsystem | sftp_server] host\n"
111 " %s [[user@]host[:file [file]]]\n"
112 " %s [[user@]host[:dir[/]]]\n"
113 " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname);
Damien Miller33804262001-02-04 23:20:18 +1100114 exit(1);
115}
116
Kevin Stevesef4eea92001-02-05 12:42:17 +0000117int
Damien Miller33804262001-02-04 23:20:18 +1100118main(int argc, char **argv)
119{
Damien Miller956f3fb2003-01-10 21:40:00 +1100120 int in, out, ch, err;
Ben Lindstrom63667f62001-04-13 00:00:14 +0000121 char *host, *userhost, *cp, *file2;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000122 int debug_level = 0, sshver = 2;
123 char *file1 = NULL, *sftp_server = NULL;
Damien Millerd14ee1e2002-02-05 12:27:31 +1100124 char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000125 LogLevel ll = SYSLOG_LEVEL_INFO;
126 arglist args;
Damien Millerd7686fd2001-02-10 00:40:03 +1100127 extern int optind;
128 extern char *optarg;
Damien Miller33804262001-02-04 23:20:18 +1100129
Damien Miller59d3d5b2003-08-22 09:34:41 +1000130 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000131 args.list = NULL;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100132 addargs(&args, "ssh"); /* overwritten with ssh_program */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000133 addargs(&args, "-oForwardX11 no");
134 addargs(&args, "-oForwardAgent no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000135 addargs(&args, "-oClearAllForwardings yes");
Damien Millere4f5a822004-01-21 14:11:05 +1100136
Ben Lindstrom387c4722001-05-08 20:27:25 +0000137 ll = SYSLOG_LEVEL_INFO;
Damien Millere4f5a822004-01-21 14:11:05 +1100138 infile = stdin;
Damien Millerd7686fd2001-02-10 00:40:03 +1100139
Damien Miller16a13332002-02-13 14:03:56 +1100140 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100141 switch (ch) {
142 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000143 addargs(&args, "-C");
Damien Millerd7686fd2001-02-10 00:40:03 +1100144 break;
145 case 'v':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000146 if (debug_level < 3) {
147 addargs(&args, "-v");
148 ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
149 }
150 debug_level++;
Damien Millerd7686fd2001-02-10 00:40:03 +1100151 break;
Ben Lindstrom1e243242001-09-18 05:38:44 +0000152 case 'F':
Damien Millerd7686fd2001-02-10 00:40:03 +1100153 case 'o':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000154 addargs(&args, "-%c%s", ch, optarg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100155 break;
156 case '1':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000157 sshver = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100158 if (sftp_server == NULL)
159 sftp_server = _PATH_SFTP_SERVER;
160 break;
161 case 's':
162 sftp_server = optarg;
163 break;
164 case 'S':
165 ssh_program = optarg;
166 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000167 case 'b':
Damien Miller44f75c12004-01-21 10:58:47 +1100168 if (batchmode)
169 fatal("Batch file already specified.");
170
171 /* Allow "-" as stdin */
172 if (strcmp(optarg, "-") != 0 &&
173 (infile = fopen(optarg, "r")) == NULL)
174 fatal("%s (%s).", strerror(errno), optarg);
Damien Miller62d57f62003-01-10 21:43:24 +1100175 showprogress = 0;
Damien Miller44f75c12004-01-21 10:58:47 +1100176 batchmode = 1;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000177 break;
Damien Millerd14ee1e2002-02-05 12:27:31 +1100178 case 'P':
179 sftp_direct = optarg;
180 break;
Damien Miller8829d362002-02-08 22:04:05 +1100181 case 'B':
182 copy_buffer_len = strtol(optarg, &cp, 10);
183 if (copy_buffer_len == 0 || *cp != '\0')
184 fatal("Invalid buffer size \"%s\"", optarg);
185 break;
Damien Miller16a13332002-02-13 14:03:56 +1100186 case 'R':
187 num_requests = strtol(optarg, &cp, 10);
188 if (num_requests == 0 || *cp != '\0')
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000189 fatal("Invalid number of requests \"%s\"",
Damien Miller16a13332002-02-13 14:03:56 +1100190 optarg);
191 break;
Damien Millerd7686fd2001-02-10 00:40:03 +1100192 case 'h':
193 default:
Damien Miller33804262001-02-04 23:20:18 +1100194 usage();
195 }
196 }
197
Ben Lindstrom2f3d52a2002-04-02 21:06:18 +0000198 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
199
Damien Millerd14ee1e2002-02-05 12:27:31 +1100200 if (sftp_direct == NULL) {
201 if (optind == argc || argc > (optind + 2))
202 usage();
Damien Miller33804262001-02-04 23:20:18 +1100203
Damien Millerd14ee1e2002-02-05 12:27:31 +1100204 userhost = xstrdup(argv[optind]);
205 file2 = argv[optind+1];
Ben Lindstrom63667f62001-04-13 00:00:14 +0000206
Ben Lindstromc276c122002-12-23 02:14:51 +0000207 if ((host = strrchr(userhost, '@')) == NULL)
Damien Millerd14ee1e2002-02-05 12:27:31 +1100208 host = userhost;
209 else {
210 *host++ = '\0';
211 if (!userhost[0]) {
212 fprintf(stderr, "Missing username\n");
213 usage();
214 }
215 addargs(&args, "-l%s",userhost);
216 }
217
Damien Millerec692032004-01-27 21:22:00 +1100218 if ((cp = colon(host)) != NULL) {
219 *cp++ = '\0';
220 file1 = cp;
221 }
222
Damien Millerd14ee1e2002-02-05 12:27:31 +1100223 host = cleanhostname(host);
224 if (!*host) {
225 fprintf(stderr, "Missing hostname\n");
Damien Miller33804262001-02-04 23:20:18 +1100226 usage();
227 }
Damien Millerd14ee1e2002-02-05 12:27:31 +1100228
Damien Millerd14ee1e2002-02-05 12:27:31 +1100229 addargs(&args, "-oProtocol %d", sshver);
230
231 /* no subsystem if the server-spec contains a '/' */
232 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
233 addargs(&args, "-s");
234
235 addargs(&args, "%s", host);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000236 addargs(&args, "%s", (sftp_server != NULL ?
Damien Millerd14ee1e2002-02-05 12:27:31 +1100237 sftp_server : "sftp"));
238 args.list[0] = ssh_program;
239
Damien Miller44f75c12004-01-21 10:58:47 +1100240 if (!batchmode)
241 fprintf(stderr, "Connecting to %s...\n", host);
Damien Millercc685c12003-06-04 22:51:38 +1000242 connect_to_server(ssh_program, args.list, &in, &out);
Damien Millerd14ee1e2002-02-05 12:27:31 +1100243 } else {
244 args.list = NULL;
245 addargs(&args, "sftp-server");
246
Damien Miller44f75c12004-01-21 10:58:47 +1100247 if (!batchmode)
248 fprintf(stderr, "Attaching to %s...\n", sftp_direct);
Damien Millercc685c12003-06-04 22:51:38 +1000249 connect_to_server(sftp_direct, args.list, &in, &out);
Damien Miller33804262001-02-04 23:20:18 +1100250 }
251
Damien Miller956f3fb2003-01-10 21:40:00 +1100252 err = interactive_loop(in, out, file1, file2);
Damien Miller33804262001-02-04 23:20:18 +1100253
Ben Lindstrom10b9bf92001-02-26 20:04:45 +0000254#if !defined(USE_PIPES)
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000255 shutdown(in, SHUT_RDWR);
256 shutdown(out, SHUT_RDWR);
Ben Lindstrom10b9bf92001-02-26 20:04:45 +0000257#endif
258
Damien Miller33804262001-02-04 23:20:18 +1100259 close(in);
260 close(out);
Damien Miller44f75c12004-01-21 10:58:47 +1100261 if (batchmode)
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000262 fclose(infile);
Damien Miller33804262001-02-04 23:20:18 +1100263
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000264 while (waitpid(sshpid, NULL, 0) == -1)
265 if (errno != EINTR)
266 fatal("Couldn't wait for ssh process: %s",
267 strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +1100268
Damien Miller956f3fb2003-01-10 21:40:00 +1100269 exit(err == 0 ? 0 : 1);
Damien Miller33804262001-02-04 23:20:18 +1100270}