sftp.c revision 98675
1/*
2 * Copyright (c) 2001,2002 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
27RCSID("$OpenBSD: sftp.c,v 1.29 2002/04/02 17:37:48 markus Exp $");
28
29/* XXX: short-form remote directory listings (like 'ls -C') */
30
31#include "buffer.h"
32#include "xmalloc.h"
33#include "log.h"
34#include "pathnames.h"
35#include "misc.h"
36
37#include "sftp.h"
38#include "sftp-common.h"
39#include "sftp-client.h"
40#include "sftp-int.h"
41
42FILE* infile;
43size_t copy_buffer_len = 32768;
44size_t num_requests = 16;
45
46static void
47connect_to_server(char *path, char **args, int *in, int *out, pid_t *sshpid)
48{
49	int c_in, c_out;
50#ifdef USE_PIPES
51	int pin[2], pout[2];
52	if ((pipe(pin) == -1) || (pipe(pout) == -1))
53		fatal("pipe: %s", strerror(errno));
54	*in = pin[0];
55	*out = pout[1];
56	c_in = pout[0];
57	c_out = pin[1];
58#else /* USE_PIPES */
59	int inout[2];
60	if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
61		fatal("socketpair: %s", strerror(errno));
62	*in = *out = inout[0];
63	c_in = c_out = inout[1];
64#endif /* USE_PIPES */
65
66	if ((*sshpid = fork()) == -1)
67		fatal("fork: %s", strerror(errno));
68	else if (*sshpid == 0) {
69		if ((dup2(c_in, STDIN_FILENO) == -1) ||
70		    (dup2(c_out, STDOUT_FILENO) == -1)) {
71			fprintf(stderr, "dup2: %s\n", strerror(errno));
72			exit(1);
73		}
74		close(*in);
75		close(*out);
76		close(c_in);
77		close(c_out);
78		execv(path, args);
79		fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
80		exit(1);
81	}
82
83	close(c_in);
84	close(c_out);
85}
86
87static void
88usage(void)
89{
90	extern char *__progname;
91
92	fprintf(stderr,
93	    "usage: %s [-vC1] [-b batchfile] [-o option] [-s subsystem|path] [-B buffer_size]\n"
94	    "            [-F config] [-P direct server path] [-S program]\n"
95	    "            [user@]host[:file [file]]\n", __progname);
96	exit(1);
97}
98
99int
100main(int argc, char **argv)
101{
102	int in, out, ch;
103	pid_t sshpid;
104	char *host, *userhost, *cp, *file2;
105	int debug_level = 0, sshver = 2;
106	char *file1 = NULL, *sftp_server = NULL;
107	char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
108	LogLevel ll = SYSLOG_LEVEL_INFO;
109	arglist args;
110	extern int optind;
111	extern char *optarg;
112
113	args.list = NULL;
114	addargs(&args, "ssh");		/* overwritten with ssh_program */
115	addargs(&args, "-oFallBackToRsh no");
116	addargs(&args, "-oForwardX11 no");
117	addargs(&args, "-oForwardAgent no");
118	addargs(&args, "-oClearAllForwardings yes");
119	ll = SYSLOG_LEVEL_INFO;
120	infile = stdin;		/* Read from STDIN unless changed by -b */
121
122	while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
123		switch (ch) {
124		case 'C':
125			addargs(&args, "-C");
126			break;
127		case 'v':
128			if (debug_level < 3) {
129				addargs(&args, "-v");
130				ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
131			}
132			debug_level++;
133			break;
134		case 'F':
135		case 'o':
136			addargs(&args, "-%c%s", ch, optarg);
137			break;
138		case '1':
139			sshver = 1;
140			if (sftp_server == NULL)
141				sftp_server = _PATH_SFTP_SERVER;
142			break;
143		case 's':
144			sftp_server = optarg;
145			break;
146		case 'S':
147			ssh_program = optarg;
148			break;
149		case 'b':
150			if (infile == stdin) {
151				infile = fopen(optarg, "r");
152				if (infile == NULL)
153					fatal("%s (%s).", strerror(errno), optarg);
154			} else
155				fatal("Filename already specified.");
156			break;
157		case 'P':
158			sftp_direct = optarg;
159			break;
160		case 'B':
161			copy_buffer_len = strtol(optarg, &cp, 10);
162			if (copy_buffer_len == 0 || *cp != '\0')
163				fatal("Invalid buffer size \"%s\"", optarg);
164			break;
165		case 'R':
166			num_requests = strtol(optarg, &cp, 10);
167			if (num_requests == 0 || *cp != '\0')
168				fatal("Invalid number of requests \"%s\"",
169				    optarg);
170			break;
171		case 'h':
172		default:
173			usage();
174		}
175	}
176
177	log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
178
179	if (sftp_direct == NULL) {
180		if (optind == argc || argc > (optind + 2))
181			usage();
182
183		userhost = xstrdup(argv[optind]);
184		file2 = argv[optind+1];
185
186		if ((cp = colon(userhost)) != NULL) {
187			*cp++ = '\0';
188			file1 = cp;
189		}
190
191		if ((host = strchr(userhost, '@')) == NULL)
192			host = userhost;
193		else {
194			*host++ = '\0';
195			if (!userhost[0]) {
196				fprintf(stderr, "Missing username\n");
197				usage();
198			}
199			addargs(&args, "-l%s",userhost);
200		}
201
202		host = cleanhostname(host);
203		if (!*host) {
204			fprintf(stderr, "Missing hostname\n");
205			usage();
206		}
207
208		addargs(&args, "-oProtocol %d", sshver);
209
210		/* no subsystem if the server-spec contains a '/' */
211		if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
212			addargs(&args, "-s");
213
214		addargs(&args, "%s", host);
215		addargs(&args, "%s", (sftp_server != NULL ?
216		    sftp_server : "sftp"));
217		args.list[0] = ssh_program;
218
219		fprintf(stderr, "Connecting to %s...\n", host);
220		connect_to_server(ssh_program, args.list, &in, &out,
221		    &sshpid);
222	} else {
223		args.list = NULL;
224		addargs(&args, "sftp-server");
225
226		fprintf(stderr, "Attaching to %s...\n", sftp_direct);
227		connect_to_server(sftp_direct, args.list, &in, &out,
228		    &sshpid);
229	}
230
231	interactive_loop(in, out, file1, file2);
232
233	close(in);
234	close(out);
235	if (infile != stdin)
236		fclose(infile);
237
238	while (waitpid(sshpid, NULL, 0) == -1)
239		if (errno != EINTR)
240			fatal("Couldn't wait for ssh process: %s",
241			    strerror(errno));
242
243	exit(0);
244}
245