]> andersk Git - openssh.git/blame - sftp.c
- (stevesk) OpenBSD sync:
[openssh.git] / sftp.c
CommitLineData
61e96248 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
c630ce76 27RCSID("$OpenBSD: sftp.c,v 1.9 2001/03/03 23:52:22 markus Exp $");
61e96248 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
0426a3b4 43int use_ssh1 = 0;
44char *ssh_program = _PATH_SSH_PROGRAM;
45char *sftp_server = NULL;
46
61e96248 47void
48connect_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);
0426a3b4 79 execv(ssh_program, args);
80 fprintf(stderr, "exec: %s: %s\n", ssh_program, strerror(errno));
61e96248 81 exit(1);
82 }
83
84 close(c_in);
85 close(c_out);
86}
87
88char **
89make_ssh_args(char *add_arg)
90{
91 static char **args = NULL;
92 static int nargs = 0;
93 char debug_buf[4096];
c630ce76 94 int i;
61e96248 95
96 /* Init args array */
97 if (args == NULL) {
c630ce76 98 nargs = 2;
61e96248 99 i = 0;
100 args = xmalloc(sizeof(*args) * nargs);
101 args[i++] = "ssh";
61e96248 102 args[i++] = NULL;
103 }
104
105 /* If asked to add args, then do so and return */
106 if (add_arg) {
107 i = nargs++ - 1;
108 args = xrealloc(args, sizeof(*args) * nargs);
109 args[i++] = add_arg;
110 args[i++] = NULL;
111 return(NULL);
112 }
113
c630ce76 114 /* no subsystem if the server-spec contains a '/' */
115 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
116 make_ssh_args("-s");
117 make_ssh_args("-oForwardX11=no");
118 make_ssh_args("-oForwardAgent=no");
119 make_ssh_args(use_ssh1 ? "-oProtocol=1" : "-oProtocol=2");
120
61e96248 121 /* Otherwise finish up and return the arg array */
0426a3b4 122 if (sftp_server != NULL)
123 make_ssh_args(sftp_server);
124 else
125 make_ssh_args("sftp");
61e96248 126
127 /* XXX: overflow - doesn't grow debug_buf */
128 debug_buf[0] = '\0';
129 for(i = 0; args[i]; i++) {
130 if (i)
131 strlcat(debug_buf, " ", sizeof(debug_buf));
132
133 strlcat(debug_buf, args[i], sizeof(debug_buf));
134 }
135 debug("SSH args \"%s\"", debug_buf);
136
137 return(args);
138}
139
2b87da3b 140void
61e96248 141usage(void)
142{
0426a3b4 143 fprintf(stderr, "usage: sftp [-1vC] [-osshopt=value] [user@]host\n");
61e96248 144 exit(1);
145}
146
2b87da3b 147int
61e96248 148main(int argc, char **argv)
149{
0426a3b4 150 int in, out, ch, debug_level, compress_flag;
61e96248 151 pid_t sshpid;
0426a3b4 152 char *host, *userhost;
61e96248 153 LogLevel ll;
0426a3b4 154 extern int optind;
155 extern char *optarg;
61e96248 156
157 debug_level = compress_flag = 0;
0426a3b4 158
159 while ((ch = getopt(argc, argv, "1hvCo:s:S:")) != -1) {
160 switch (ch) {
161 case 'C':
61e96248 162 compress_flag = 1;
0426a3b4 163 break;
164 case 'v':
165 debug_level = MIN(3, debug_level + 1);
166 break;
167 case 'o':
168 make_ssh_args("-o");
169 make_ssh_args(optarg);
170 break;
171 case '1':
172 use_ssh1 = 1;
173 if (sftp_server == NULL)
174 sftp_server = _PATH_SFTP_SERVER;
175 break;
176 case 's':
177 sftp_server = optarg;
178 break;
179 case 'S':
180 ssh_program = optarg;
181 break;
182 case 'h':
183 default:
61e96248 184 usage();
185 }
186 }
187
0426a3b4 188 if (optind == argc || argc > (optind + 1))
61e96248 189 usage();
190
0426a3b4 191 userhost = argv[optind];
192
193 if ((host = strchr(userhost, '@')) == NULL)
194 host = userhost;
61e96248 195 else {
0426a3b4 196 *host = '\0';
197 if (!userhost[0]) {
61e96248 198 fprintf(stderr, "Missing username\n");
199 usage();
200 }
201 make_ssh_args("-l");
0426a3b4 202 make_ssh_args(userhost);
203 host++;
61e96248 204 }
205
0426a3b4 206 if (!*host) {
61e96248 207 fprintf(stderr, "Missing hostname\n");
208 usage();
209 }
210
211 /* Set up logging and debug '-d' arguments to ssh */
212 ll = SYSLOG_LEVEL_INFO;
213 switch (debug_level) {
214 case 1:
215 ll = SYSLOG_LEVEL_DEBUG1;
216 make_ssh_args("-v");
217 break;
218 case 2:
219 ll = SYSLOG_LEVEL_DEBUG2;
220 make_ssh_args("-v");
221 make_ssh_args("-v");
222 break;
223 case 3:
224 ll = SYSLOG_LEVEL_DEBUG3;
225 make_ssh_args("-v");
226 make_ssh_args("-v");
227 make_ssh_args("-v");
228 break;
229 }
230
231 if (compress_flag)
232 make_ssh_args("-C");
233
234 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
235
0426a3b4 236 make_ssh_args(host);
61e96248 237
0426a3b4 238 fprintf(stderr, "Connecting to %s...\n", host);
61e96248 239
240 connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid);
241
242 do_init(in, out);
243
244 interactive_loop(in, out);
245
51fb577a 246#if !defined(USE_PIPES)
247 shutdown(in, SHUT_RDWR);
248 shutdown(out, SHUT_RDWR);
249#endif
250
61e96248 251 close(in);
252 close(out);
253
0426a3b4 254 if (waitpid(sshpid, NULL, 0) == -1)
255 fatal("Couldn't wait for ssh process: %s", strerror(errno));
61e96248 256
257 exit(0);
258}
This page took 0.105239 seconds and 5 git commands to generate.