2 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
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.
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.
19 RCSID("$OpenBSD: sftp.c,v 1.43 2004/02/17 07:17:29 djm Exp $");
24 #include "pathnames.h"
28 #include "sftp-common.h"
29 #include "sftp-client.h"
31 int interactive_loop(int, int, char *, char *); /* sftp-int.c */
33 #ifdef HAVE___PROGNAME
34 extern char *__progname;
41 size_t copy_buffer_len = 32768;
42 size_t num_requests = 16;
43 static pid_t sshpid = -1;
45 extern int showprogress;
57 connect_to_server(char *path, char **args, int *in, int *out)
64 if ((pipe(pin) == -1) || (pipe(pout) == -1))
65 fatal("pipe: %s", strerror(errno));
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 */
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));
92 fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
96 signal(SIGTERM, killchild);
97 signal(SIGINT, killchild);
98 signal(SIGHUP, killchild);
106 extern char *__progname;
109 "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);
119 main(int argc, char **argv)
121 int in, out, ch, err;
122 char *host, *userhost, *cp, *file2;
123 int debug_level = 0, sshver = 2;
124 char *file1 = NULL, *sftp_server = NULL;
125 char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
126 LogLevel ll = SYSLOG_LEVEL_INFO;
131 __progname = ssh_get_progname(argv[0]);
133 addargs(&args, "ssh"); /* overwritten with ssh_program */
134 addargs(&args, "-oForwardX11 no");
135 addargs(&args, "-oForwardAgent no");
136 addargs(&args, "-oClearAllForwardings yes");
138 ll = SYSLOG_LEVEL_INFO;
141 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
144 addargs(&args, "-C");
147 if (debug_level < 3) {
148 addargs(&args, "-v");
149 ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
155 addargs(&args, "-%c%s", ch, optarg);
159 if (sftp_server == NULL)
160 sftp_server = _PATH_SFTP_SERVER;
163 sftp_server = optarg;
166 ssh_program = optarg;
170 fatal("Batch file already specified.");
172 /* Allow "-" as stdin */
173 if (strcmp(optarg, "-") != 0 &&
174 (infile = fopen(optarg, "r")) == NULL)
175 fatal("%s (%s).", strerror(errno), optarg);
180 sftp_direct = optarg;
183 copy_buffer_len = strtol(optarg, &cp, 10);
184 if (copy_buffer_len == 0 || *cp != '\0')
185 fatal("Invalid buffer size \"%s\"", optarg);
188 num_requests = strtol(optarg, &cp, 10);
189 if (num_requests == 0 || *cp != '\0')
190 fatal("Invalid number of requests \"%s\"",
199 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
201 if (sftp_direct == NULL) {
202 if (optind == argc || argc > (optind + 2))
205 userhost = xstrdup(argv[optind]);
206 file2 = argv[optind+1];
208 if ((host = strrchr(userhost, '@')) == NULL)
213 fprintf(stderr, "Missing username\n");
216 addargs(&args, "-l%s",userhost);
219 if ((cp = colon(host)) != NULL) {
224 host = cleanhostname(host);
226 fprintf(stderr, "Missing hostname\n");
230 addargs(&args, "-oProtocol %d", sshver);
232 /* no subsystem if the server-spec contains a '/' */
233 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
234 addargs(&args, "-s");
236 addargs(&args, "%s", host);
237 addargs(&args, "%s", (sftp_server != NULL ?
238 sftp_server : "sftp"));
239 args.list[0] = ssh_program;
242 fprintf(stderr, "Connecting to %s...\n", host);
243 connect_to_server(ssh_program, args.list, &in, &out);
246 addargs(&args, "sftp-server");
249 fprintf(stderr, "Attaching to %s...\n", sftp_direct);
250 connect_to_server(sftp_direct, args.list, &in, &out);
253 err = interactive_loop(in, out, file1, file2);
255 #if !defined(USE_PIPES)
256 shutdown(in, SHUT_RDWR);
257 shutdown(out, SHUT_RDWR);
265 while (waitpid(sshpid, NULL, 0) == -1)
267 fatal("Couldn't wait for ssh process: %s",
270 exit(err == 0 ? 0 : 1);