blob: 04b859a8d6746edff7dfb80c23355c0c4eddfbb9 [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 Millerd7d46bb2004-02-18 14:11:13 +110019RCSID("$OpenBSD: sftp.c,v 1.43 2004/02/17 07:17:29 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"
Damien Millerd7d46bb2004-02-18 14:11:13 +110030
31int interactive_loop(int, int, char *, char *); /* sftp-int.c */
Damien Miller33804262001-02-04 23:20:18 +110032
Kevin Steves12888d12001-03-05 19:50:57 +000033#ifdef HAVE___PROGNAME
34extern char *__progname;
35#else
36char *__progname;
37#endif
38
Damien Millere4f5a822004-01-21 14:11:05 +110039FILE* infile;
Damien Miller44f75c12004-01-21 10:58:47 +110040int batchmode = 0;
Damien Miller8829d362002-02-08 22:04:05 +110041size_t copy_buffer_len = 32768;
Damien Miller16a13332002-02-13 14:03:56 +110042size_t num_requests = 16;
Damien Millercc685c12003-06-04 22:51:38 +100043static pid_t sshpid = -1;
Damien Millerd7686fd2001-02-10 00:40:03 +110044
Damien Miller62d57f62003-01-10 21:43:24 +110045extern int showprogress;
46
Ben Lindstrombba81212001-06-25 05:01:22 +000047static void
Damien Millercc685c12003-06-04 22:51:38 +100048killchild(int signo)
49{
50 if (sshpid > 1)
51 kill(sshpid, signo);
52
53 _exit(1);
54}
55
56static void
57connect_to_server(char *path, char **args, int *in, int *out)
Damien Miller33804262001-02-04 23:20:18 +110058{
59 int c_in, c_out;
Ben Lindstromb1f483f2002-06-23 21:27:18 +000060
Damien Miller33804262001-02-04 23:20:18 +110061#ifdef USE_PIPES
62 int pin[2], pout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +000063
Damien Miller33804262001-02-04 23:20:18 +110064 if ((pipe(pin) == -1) || (pipe(pout) == -1))
65 fatal("pipe: %s", strerror(errno));
66 *in = pin[0];
67 *out = pout[1];
68 c_in = pout[0];
69 c_out = pin[1];
70#else /* USE_PIPES */
71 int inout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +000072
Damien Miller33804262001-02-04 23:20:18 +110073 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
74 fatal("socketpair: %s", strerror(errno));
75 *in = *out = inout[0];
76 c_in = c_out = inout[1];
77#endif /* USE_PIPES */
78
Damien Millercc685c12003-06-04 22:51:38 +100079 if ((sshpid = fork()) == -1)
Damien Miller33804262001-02-04 23:20:18 +110080 fatal("fork: %s", strerror(errno));
Damien Millercc685c12003-06-04 22:51:38 +100081 else if (sshpid == 0) {
Damien Miller33804262001-02-04 23:20:18 +110082 if ((dup2(c_in, STDIN_FILENO) == -1) ||
83 (dup2(c_out, STDOUT_FILENO) == -1)) {
84 fprintf(stderr, "dup2: %s\n", strerror(errno));
85 exit(1);
86 }
87 close(*in);
88 close(*out);
89 close(c_in);
90 close(c_out);
Damien Millerd14ee1e2002-02-05 12:27:31 +110091 execv(path, args);
92 fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +110093 exit(1);
94 }
95
Damien Millercc685c12003-06-04 22:51:38 +100096 signal(SIGTERM, killchild);
97 signal(SIGINT, killchild);
98 signal(SIGHUP, killchild);
Damien Miller33804262001-02-04 23:20:18 +110099 close(c_in);
100 close(c_out);
101}
102
Ben Lindstrombba81212001-06-25 05:01:22 +0000103static void
Damien Miller33804262001-02-04 23:20:18 +1100104usage(void)
105{
Damien Miller025e01c2002-02-08 22:06:29 +1100106 extern char *__progname;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000107
Ben Lindstrom1e243242001-09-18 05:38:44 +0000108 fprintf(stderr,
Darren Tucker1f203942003-10-15 15:50:42 +1000109 "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n"
110 " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n"
111 " [-S program] [-s subsystem | sftp_server] host\n"
112 " %s [[user@]host[:file [file]]]\n"
113 " %s [[user@]host[:dir[/]]]\n"
114 " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname);
Damien Miller33804262001-02-04 23:20:18 +1100115 exit(1);
116}
117
Kevin Stevesef4eea92001-02-05 12:42:17 +0000118int
Damien Miller33804262001-02-04 23:20:18 +1100119main(int argc, char **argv)
120{
Damien Miller956f3fb2003-01-10 21:40:00 +1100121 int in, out, ch, err;
Ben Lindstrom63667f62001-04-13 00:00:14 +0000122 char *host, *userhost, *cp, *file2;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000123 int debug_level = 0, sshver = 2;
124 char *file1 = NULL, *sftp_server = NULL;
Damien Millerd14ee1e2002-02-05 12:27:31 +1100125 char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000126 LogLevel ll = SYSLOG_LEVEL_INFO;
127 arglist args;
Damien Millerd7686fd2001-02-10 00:40:03 +1100128 extern int optind;
129 extern char *optarg;
Damien Miller33804262001-02-04 23:20:18 +1100130
Damien Miller59d3d5b2003-08-22 09:34:41 +1000131 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000132 args.list = NULL;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100133 addargs(&args, "ssh"); /* overwritten with ssh_program */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000134 addargs(&args, "-oForwardX11 no");
135 addargs(&args, "-oForwardAgent no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000136 addargs(&args, "-oClearAllForwardings yes");
Damien Millere4f5a822004-01-21 14:11:05 +1100137
Ben Lindstrom387c4722001-05-08 20:27:25 +0000138 ll = SYSLOG_LEVEL_INFO;
Damien Millere4f5a822004-01-21 14:11:05 +1100139 infile = stdin;
Damien Millerd7686fd2001-02-10 00:40:03 +1100140
Damien Miller16a13332002-02-13 14:03:56 +1100141 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100142 switch (ch) {
143 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000144 addargs(&args, "-C");
Damien Millerd7686fd2001-02-10 00:40:03 +1100145 break;
146 case 'v':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000147 if (debug_level < 3) {
148 addargs(&args, "-v");
149 ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
150 }
151 debug_level++;
Damien Millerd7686fd2001-02-10 00:40:03 +1100152 break;
Ben Lindstrom1e243242001-09-18 05:38:44 +0000153 case 'F':
Damien Millerd7686fd2001-02-10 00:40:03 +1100154 case 'o':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000155 addargs(&args, "-%c%s", ch, optarg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100156 break;
157 case '1':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000158 sshver = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100159 if (sftp_server == NULL)
160 sftp_server = _PATH_SFTP_SERVER;
161 break;
162 case 's':
163 sftp_server = optarg;
164 break;
165 case 'S':
166 ssh_program = optarg;
167 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000168 case 'b':
Damien Miller44f75c12004-01-21 10:58:47 +1100169 if (batchmode)
170 fatal("Batch file already specified.");
171
172 /* Allow "-" as stdin */
173 if (strcmp(optarg, "-") != 0 &&
174 (infile = fopen(optarg, "r")) == NULL)
175 fatal("%s (%s).", strerror(errno), optarg);
Damien Miller62d57f62003-01-10 21:43:24 +1100176 showprogress = 0;
Damien Miller44f75c12004-01-21 10:58:47 +1100177 batchmode = 1;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000178 break;
Damien Millerd14ee1e2002-02-05 12:27:31 +1100179 case 'P':
180 sftp_direct = optarg;
181 break;
Damien Miller8829d362002-02-08 22:04:05 +1100182 case 'B':
183 copy_buffer_len = strtol(optarg, &cp, 10);
184 if (copy_buffer_len == 0 || *cp != '\0')
185 fatal("Invalid buffer size \"%s\"", optarg);
186 break;
Damien Miller16a13332002-02-13 14:03:56 +1100187 case 'R':
188 num_requests = strtol(optarg, &cp, 10);
189 if (num_requests == 0 || *cp != '\0')
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000190 fatal("Invalid number of requests \"%s\"",
Damien Miller16a13332002-02-13 14:03:56 +1100191 optarg);
192 break;
Damien Millerd7686fd2001-02-10 00:40:03 +1100193 case 'h':
194 default:
Damien Miller33804262001-02-04 23:20:18 +1100195 usage();
196 }
197 }
198
Ben Lindstrom2f3d52a2002-04-02 21:06:18 +0000199 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
200
Damien Millerd14ee1e2002-02-05 12:27:31 +1100201 if (sftp_direct == NULL) {
202 if (optind == argc || argc > (optind + 2))
203 usage();
Damien Miller33804262001-02-04 23:20:18 +1100204
Damien Millerd14ee1e2002-02-05 12:27:31 +1100205 userhost = xstrdup(argv[optind]);
206 file2 = argv[optind+1];
Ben Lindstrom63667f62001-04-13 00:00:14 +0000207
Ben Lindstromc276c122002-12-23 02:14:51 +0000208 if ((host = strrchr(userhost, '@')) == NULL)
Damien Millerd14ee1e2002-02-05 12:27:31 +1100209 host = userhost;
210 else {
211 *host++ = '\0';
212 if (!userhost[0]) {
213 fprintf(stderr, "Missing username\n");
214 usage();
215 }
216 addargs(&args, "-l%s",userhost);
217 }
218
Damien Millerec692032004-01-27 21:22:00 +1100219 if ((cp = colon(host)) != NULL) {
220 *cp++ = '\0';
221 file1 = cp;
222 }
223
Damien Millerd14ee1e2002-02-05 12:27:31 +1100224 host = cleanhostname(host);
225 if (!*host) {
226 fprintf(stderr, "Missing hostname\n");
Damien Miller33804262001-02-04 23:20:18 +1100227 usage();
228 }
Damien Millerd14ee1e2002-02-05 12:27:31 +1100229
Damien Millerd14ee1e2002-02-05 12:27:31 +1100230 addargs(&args, "-oProtocol %d", sshver);
231
232 /* no subsystem if the server-spec contains a '/' */
233 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
234 addargs(&args, "-s");
235
236 addargs(&args, "%s", host);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000237 addargs(&args, "%s", (sftp_server != NULL ?
Damien Millerd14ee1e2002-02-05 12:27:31 +1100238 sftp_server : "sftp"));
239 args.list[0] = ssh_program;
240
Damien Miller44f75c12004-01-21 10:58:47 +1100241 if (!batchmode)
242 fprintf(stderr, "Connecting to %s...\n", host);
Damien Millercc685c12003-06-04 22:51:38 +1000243 connect_to_server(ssh_program, args.list, &in, &out);
Damien Millerd14ee1e2002-02-05 12:27:31 +1100244 } else {
245 args.list = NULL;
246 addargs(&args, "sftp-server");
247
Damien Miller44f75c12004-01-21 10:58:47 +1100248 if (!batchmode)
249 fprintf(stderr, "Attaching to %s...\n", sftp_direct);
Damien Millercc685c12003-06-04 22:51:38 +1000250 connect_to_server(sftp_direct, args.list, &in, &out);
Damien Miller33804262001-02-04 23:20:18 +1100251 }
252
Damien Miller956f3fb2003-01-10 21:40:00 +1100253 err = interactive_loop(in, out, file1, file2);
Damien Miller33804262001-02-04 23:20:18 +1100254
Ben Lindstrom10b9bf92001-02-26 20:04:45 +0000255#if !defined(USE_PIPES)
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000256 shutdown(in, SHUT_RDWR);
257 shutdown(out, SHUT_RDWR);
Ben Lindstrom10b9bf92001-02-26 20:04:45 +0000258#endif
259
Damien Miller33804262001-02-04 23:20:18 +1100260 close(in);
261 close(out);
Damien Miller44f75c12004-01-21 10:58:47 +1100262 if (batchmode)
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000263 fclose(infile);
Damien Miller33804262001-02-04 23:20:18 +1100264
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000265 while (waitpid(sshpid, NULL, 0) == -1)
266 if (errno != EINTR)
267 fatal("Couldn't wait for ssh process: %s",
268 strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +1100269
Damien Miller956f3fb2003-01-10 21:40:00 +1100270 exit(err == 0 ? 0 : 1);
Damien Miller33804262001-02-04 23:20:18 +1100271}