]> andersk Git - openssh.git/blame_incremental - sftp.c
- (dtucker) [configure.ac] Handle case where krb5-config --libs returns a
[openssh.git] / sftp.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
3 *
4 * 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.
7 *
8 * 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.
15 */
16
17#include "includes.h"
18
19RCSID("$OpenBSD: sftp.c,v 1.42 2004/02/17 05:39:51 djm Exp $");
20
21#include "buffer.h"
22#include "xmalloc.h"
23#include "log.h"
24#include "pathnames.h"
25#include "misc.h"
26
27#include "sftp.h"
28#include "sftp-common.h"
29#include "sftp-client.h"
30#include "sftp-int.h"
31
32#ifdef HAVE___PROGNAME
33extern char *__progname;
34#else
35char *__progname;
36#endif
37
38FILE* infile;
39int batchmode = 0;
40size_t copy_buffer_len = 32768;
41size_t num_requests = 16;
42static pid_t sshpid = -1;
43
44extern int showprogress;
45
46static void
47killchild(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)
57{
58 int c_in, c_out;
59
60#ifdef USE_PIPES
61 int pin[2], pout[2];
62
63 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];
71
72 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
78 if ((sshpid = fork()) == -1)
79 fatal("fork: %s", strerror(errno));
80 else if (sshpid == 0) {
81 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);
90 execv(path, args);
91 fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
92 exit(1);
93 }
94
95 signal(SIGTERM, killchild);
96 signal(SIGINT, killchild);
97 signal(SIGHUP, killchild);
98 close(c_in);
99 close(c_out);
100}
101
102static void
103usage(void)
104{
105 extern char *__progname;
106
107 fprintf(stderr,
108 "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);
114 exit(1);
115}
116
117int
118main(int argc, char **argv)
119{
120 int in, out, ch, err;
121 char *host, *userhost, *cp, *file2;
122 int debug_level = 0, sshver = 2;
123 char *file1 = NULL, *sftp_server = NULL;
124 char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
125 LogLevel ll = SYSLOG_LEVEL_INFO;
126 arglist args;
127 extern int optind;
128 extern char *optarg;
129
130 __progname = ssh_get_progname(argv[0]);
131 args.list = NULL;
132 addargs(&args, "ssh"); /* overwritten with ssh_program */
133 addargs(&args, "-oForwardX11 no");
134 addargs(&args, "-oForwardAgent no");
135 addargs(&args, "-oClearAllForwardings yes");
136
137 ll = SYSLOG_LEVEL_INFO;
138 infile = stdin;
139
140 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
141 switch (ch) {
142 case 'C':
143 addargs(&args, "-C");
144 break;
145 case 'v':
146 if (debug_level < 3) {
147 addargs(&args, "-v");
148 ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
149 }
150 debug_level++;
151 break;
152 case 'F':
153 case 'o':
154 addargs(&args, "-%c%s", ch, optarg);
155 break;
156 case '1':
157 sshver = 1;
158 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;
167 case 'b':
168 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);
175 showprogress = 0;
176 batchmode = 1;
177 break;
178 case 'P':
179 sftp_direct = optarg;
180 break;
181 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;
186 case 'R':
187 num_requests = strtol(optarg, &cp, 10);
188 if (num_requests == 0 || *cp != '\0')
189 fatal("Invalid number of requests \"%s\"",
190 optarg);
191 break;
192 case 'h':
193 default:
194 usage();
195 }
196 }
197
198 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
199
200 if (sftp_direct == NULL) {
201 if (optind == argc || argc > (optind + 2))
202 usage();
203
204 userhost = xstrdup(argv[optind]);
205 file2 = argv[optind+1];
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 if ((cp = colon(host)) != NULL) {
219 *cp++ = '\0';
220 file1 = cp;
221 }
222
223 host = cleanhostname(host);
224 if (!*host) {
225 fprintf(stderr, "Missing hostname\n");
226 usage();
227 }
228
229 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);
236 addargs(&args, "%s", (sftp_server != NULL ?
237 sftp_server : "sftp"));
238 args.list[0] = ssh_program;
239
240 if (!batchmode)
241 fprintf(stderr, "Connecting to %s...\n", host);
242 connect_to_server(ssh_program, args.list, &in, &out);
243 } else {
244 args.list = NULL;
245 addargs(&args, "sftp-server");
246
247 if (!batchmode)
248 fprintf(stderr, "Attaching to %s...\n", sftp_direct);
249 connect_to_server(sftp_direct, args.list, &in, &out);
250 }
251
252 err = interactive_loop(in, out, file1, file2);
253
254#if !defined(USE_PIPES)
255 shutdown(in, SHUT_RDWR);
256 shutdown(out, SHUT_RDWR);
257#endif
258
259 close(in);
260 close(out);
261 if (batchmode)
262 fclose(infile);
263
264 while (waitpid(sshpid, NULL, 0) == -1)
265 if (errno != EINTR)
266 fatal("Couldn't wait for ssh process: %s",
267 strerror(errno));
268
269 exit(err == 0 ? 0 : 1);
270}
This page took 0.371861 seconds and 5 git commands to generate.