xref: /openbsd-src/usr.bin/ssh/sftp.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
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 
27 RCSID("$OpenBSD: sftp.c,v 1.37 2003/07/10 20:05:55 markus Exp $");
28 
29 #include "buffer.h"
30 #include "xmalloc.h"
31 #include "log.h"
32 #include "pathnames.h"
33 #include "misc.h"
34 
35 #include "sftp.h"
36 #include "sftp-common.h"
37 #include "sftp-client.h"
38 #include "sftp-int.h"
39 
40 FILE* infile;
41 size_t copy_buffer_len = 32768;
42 size_t num_requests = 16;
43 static pid_t sshpid = -1;
44 
45 extern int showprogress;
46 
47 static void
48 killchild(int signo)
49 {
50 	if (sshpid > 1)
51 		kill(sshpid, signo);
52 
53 	_exit(1);
54 }
55 
56 static void
57 connect_to_server(char *path, char **args, int *in, int *out)
58 {
59 	int c_in, c_out;
60 
61 #ifdef USE_PIPES
62 	int pin[2], pout[2];
63 
64 	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];
72 
73 	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 
79 	if ((sshpid = fork()) == -1)
80 		fatal("fork: %s", strerror(errno));
81 	else if (sshpid == 0) {
82 		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);
91 		execv(path, args);
92 		fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
93 		exit(1);
94 	}
95 
96 	signal(SIGTERM, killchild);
97 	signal(SIGINT, killchild);
98 	signal(SIGHUP, killchild);
99 	close(c_in);
100 	close(c_out);
101 }
102 
103 static void
104 usage(void)
105 {
106 	extern char *__progname;
107 
108 	fprintf(stderr,
109 	    "usage: %s [-vC1] [-b batchfile] [-o ssh_option] [-s subsystem | sftp_server]\n"
110 	    "            [-B buffer_size] [-F ssh_config] [-P sftp_server path]\n"
111 	    "            [-R num_requests] [-S program]\n"
112 	    "            [user@]host[:file [file]]\n", __progname);
113 	exit(1);
114 }
115 
116 int
117 main(int argc, char **argv)
118 {
119 	int in, out, ch, err;
120 	char *host, *userhost, *cp, *file2;
121 	int debug_level = 0, sshver = 2;
122 	char *file1 = NULL, *sftp_server = NULL;
123 	char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
124 	LogLevel ll = SYSLOG_LEVEL_INFO;
125 	arglist args;
126 	extern int optind;
127 	extern char *optarg;
128 
129 	args.list = NULL;
130 	addargs(&args, "ssh");		/* overwritten with ssh_program */
131 	addargs(&args, "-oForwardX11 no");
132 	addargs(&args, "-oForwardAgent no");
133 	addargs(&args, "-oClearAllForwardings yes");
134 	ll = SYSLOG_LEVEL_INFO;
135 	infile = stdin;		/* Read from STDIN unless changed by -b */
136 
137 	while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
138 		switch (ch) {
139 		case 'C':
140 			addargs(&args, "-C");
141 			break;
142 		case 'v':
143 			if (debug_level < 3) {
144 				addargs(&args, "-v");
145 				ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
146 			}
147 			debug_level++;
148 			break;
149 		case 'F':
150 		case 'o':
151 			addargs(&args, "-%c%s", ch, optarg);
152 			break;
153 		case '1':
154 			sshver = 1;
155 			if (sftp_server == NULL)
156 				sftp_server = _PATH_SFTP_SERVER;
157 			break;
158 		case 's':
159 			sftp_server = optarg;
160 			break;
161 		case 'S':
162 			ssh_program = optarg;
163 			break;
164 		case 'b':
165 			if (infile == stdin) {
166 				infile = fopen(optarg, "r");
167 				if (infile == NULL)
168 					fatal("%s (%s).", strerror(errno), optarg);
169 			} else
170 				fatal("Filename already specified.");
171 			showprogress = 0;
172 			break;
173 		case 'P':
174 			sftp_direct = optarg;
175 			break;
176 		case 'B':
177 			copy_buffer_len = strtol(optarg, &cp, 10);
178 			if (copy_buffer_len == 0 || *cp != '\0')
179 				fatal("Invalid buffer size \"%s\"", optarg);
180 			break;
181 		case 'R':
182 			num_requests = strtol(optarg, &cp, 10);
183 			if (num_requests == 0 || *cp != '\0')
184 				fatal("Invalid number of requests \"%s\"",
185 				    optarg);
186 			break;
187 		case 'h':
188 		default:
189 			usage();
190 		}
191 	}
192 
193 	log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
194 
195 	if (sftp_direct == NULL) {
196 		if (optind == argc || argc > (optind + 2))
197 			usage();
198 
199 		userhost = xstrdup(argv[optind]);
200 		file2 = argv[optind+1];
201 
202 		if ((cp = colon(userhost)) != NULL) {
203 			*cp++ = '\0';
204 			file1 = cp;
205 		}
206 
207 		if ((host = strrchr(userhost, '@')) == NULL)
208 			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 
218 		host = cleanhostname(host);
219 		if (!*host) {
220 			fprintf(stderr, "Missing hostname\n");
221 			usage();
222 		}
223 
224 		addargs(&args, "-oProtocol %d", sshver);
225 
226 		/* no subsystem if the server-spec contains a '/' */
227 		if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
228 			addargs(&args, "-s");
229 
230 		addargs(&args, "%s", host);
231 		addargs(&args, "%s", (sftp_server != NULL ?
232 		    sftp_server : "sftp"));
233 		args.list[0] = ssh_program;
234 
235 		fprintf(stderr, "Connecting to %s...\n", host);
236 		connect_to_server(ssh_program, args.list, &in, &out);
237 	} else {
238 		args.list = NULL;
239 		addargs(&args, "sftp-server");
240 
241 		fprintf(stderr, "Attaching to %s...\n", sftp_direct);
242 		connect_to_server(sftp_direct, args.list, &in, &out);
243 	}
244 
245 	err = interactive_loop(in, out, file1, file2);
246 
247 	close(in);
248 	close(out);
249 	if (infile != stdin)
250 		fclose(infile);
251 
252 	while (waitpid(sshpid, NULL, 0) == -1)
253 		if (errno != EINTR)
254 			fatal("Couldn't wait for ssh process: %s",
255 			    strerror(errno));
256 
257 	exit(err == 0 ? 0 : 1);
258 }
259