2 * Copyright (c) 2001 Damien Miller. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 RCSID("$OpenBSD: sftp.c,v 1.15 2001/04/16 02:31:44 mouring Exp $");
29 /* XXX: commandline mode */
30 /* XXX: short-form remote directory listings (like 'ls -C') */
35 #include "pathnames.h"
38 #include "sftp-common.h"
39 #include "sftp-client.h"
42 #ifdef HAVE___PROGNAME
43 extern char *__progname;
48 #include "scp-common.h"
51 char *ssh_program = _PATH_SSH_PROGRAM;
52 char *sftp_server = NULL;
56 connect_to_server(char **args, int *in, int *out, pid_t *sshpid)
61 if ((pipe(pin) == -1) || (pipe(pout) == -1))
62 fatal("pipe: %s", strerror(errno));
69 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
70 fatal("socketpair: %s", strerror(errno));
71 *in = *out = inout[0];
72 c_in = c_out = inout[1];
73 #endif /* USE_PIPES */
75 if ((*sshpid = fork()) == -1)
76 fatal("fork: %s", strerror(errno));
77 else if (*sshpid == 0) {
78 if ((dup2(c_in, STDIN_FILENO) == -1) ||
79 (dup2(c_out, STDOUT_FILENO) == -1)) {
80 fprintf(stderr, "dup2: %s\n", strerror(errno));
87 execv(ssh_program, args);
88 fprintf(stderr, "exec: %s: %s\n", ssh_program, strerror(errno));
97 make_ssh_args(char *add_arg)
99 static char **args = NULL;
100 static int nargs = 0;
101 char debug_buf[4096];
104 /* Init args array */
108 args = xmalloc(sizeof(*args) * nargs);
113 /* If asked to add args, then do so and return */
116 args = xrealloc(args, sizeof(*args) * nargs);
122 /* no subsystem if the server-spec contains a '/' */
123 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
125 make_ssh_args("-oForwardX11=no");
126 make_ssh_args("-oForwardAgent=no");
127 make_ssh_args(use_ssh1 ? "-oProtocol=1" : "-oProtocol=2");
129 /* Otherwise finish up and return the arg array */
130 if (sftp_server != NULL)
131 make_ssh_args(sftp_server);
133 make_ssh_args("sftp");
135 /* XXX: overflow - doesn't grow debug_buf */
137 for(i = 0; args[i]; i++) {
139 strlcat(debug_buf, " ", sizeof(debug_buf));
141 strlcat(debug_buf, args[i], sizeof(debug_buf));
143 debug("SSH args \"%s\"", debug_buf);
151 fprintf(stderr, "usage: sftp [-1vC] [-b batchfile] [-osshopt=value] [user@]host[:file [file]]\n");
156 main(int argc, char **argv)
158 int in, out, ch, debug_level, compress_flag;
161 char *host, *userhost, *cp, *file2;
166 __progname = get_progname(argv[0]);
167 infile = stdin; /* Read from STDIN unless changed by -b */
168 debug_level = compress_flag = 0;
170 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:")) != -1) {
176 debug_level = MIN(3, debug_level + 1);
180 make_ssh_args(optarg);
184 if (sftp_server == NULL)
185 sftp_server = _PATH_SFTP_SERVER;
188 sftp_server = optarg;
191 ssh_program = optarg;
194 if (infile == stdin) {
195 infile = fopen(optarg, "r");
197 fatal("%s (%s).", strerror(errno), optarg);
199 fatal("Filename already specified.");
207 if (optind == argc || argc > (optind + 2))
210 userhost = xstrdup(argv[optind]);
211 file2 = argv[optind+1];
213 if ((cp = colon(userhost)) != NULL) {
218 if ((host = strchr(userhost, '@')) == NULL)
223 fprintf(stderr, "Missing username\n");
227 make_ssh_args(userhost);
230 host = cleanhostname(host);
232 fprintf(stderr, "Missing hostname\n");
236 /* Set up logging and debug '-d' arguments to ssh */
237 ll = SYSLOG_LEVEL_INFO;
238 switch (debug_level) {
240 ll = SYSLOG_LEVEL_DEBUG1;
244 ll = SYSLOG_LEVEL_DEBUG2;
249 ll = SYSLOG_LEVEL_DEBUG3;
259 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
263 fprintf(stderr, "Connecting to %s...\n", host);
265 connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid);
267 interactive_loop(in, out, file1, file2);
269 #if !defined(USE_PIPES)
270 shutdown(in, SHUT_RDWR);
271 shutdown(out, SHUT_RDWR);
279 if (waitpid(sshpid, NULL, 0) == -1)
280 fatal("Couldn't wait for ssh process: %s", strerror(errno));