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.7 2001/02/08 00:04:52 markus Exp $");
29 /* XXX: commandline mode */
30 /* XXX: copy between two remote hosts (commandline) */
31 /* XXX: short-form remote directory listings (like 'ls -C') */
36 #include "pathnames.h"
39 #include "sftp-common.h"
40 #include "sftp-client.h"
44 char *ssh_program = _PATH_SSH_PROGRAM;
45 char *sftp_server = NULL;
48 connect_to_server(char **args, int *in, int *out, pid_t *sshpid)
53 if ((pipe(pin) == -1) || (pipe(pout) == -1))
54 fatal("pipe: %s", strerror(errno));
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 */
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));
79 execv(ssh_program, args);
80 fprintf(stderr, "exec: %s: %s\n", ssh_program, strerror(errno));
89 make_ssh_args(char *add_arg)
91 static char **args = NULL;
94 int i, use_subsystem = 1;
96 /* no subsystem if protocol 1 or the server-spec contains a '/' */
98 (sftp_server != NULL && strchr(sftp_server, '/') != NULL))
101 /* Init args array */
103 nargs = use_subsystem ? 6 : 5;
105 args = xmalloc(sizeof(*args) * nargs);
107 args[i++] = use_ssh1 ? "-oProtocol=1" : "-oProtocol=2";
110 args[i++] = "-oForwardAgent=no";
111 args[i++] = "-oForwardX11=no";
115 /* If asked to add args, then do so and return */
118 args = xrealloc(args, sizeof(*args) * nargs);
124 /* Otherwise finish up and return the arg array */
125 if (sftp_server != NULL)
126 make_ssh_args(sftp_server);
128 make_ssh_args("sftp");
130 /* XXX: overflow - doesn't grow debug_buf */
132 for(i = 0; args[i]; i++) {
134 strlcat(debug_buf, " ", sizeof(debug_buf));
136 strlcat(debug_buf, args[i], sizeof(debug_buf));
138 debug("SSH args \"%s\"", debug_buf);
146 fprintf(stderr, "usage: sftp [-1vC] [-osshopt=value] [user@]host\n");
151 main(int argc, char **argv)
153 int in, out, ch, debug_level, compress_flag;
155 char *host, *userhost;
160 debug_level = compress_flag = 0;
162 while ((ch = getopt(argc, argv, "1hvCo:s:S:")) != -1) {
168 debug_level = MIN(3, debug_level + 1);
172 make_ssh_args(optarg);
176 if (sftp_server == NULL)
177 sftp_server = _PATH_SFTP_SERVER;
180 sftp_server = optarg;
183 ssh_program = optarg;
191 if (optind == argc || argc > (optind + 1))
194 userhost = argv[optind];
196 if ((host = strchr(userhost, '@')) == NULL)
201 fprintf(stderr, "Missing username\n");
205 make_ssh_args(userhost);
210 fprintf(stderr, "Missing hostname\n");
214 /* Set up logging and debug '-d' arguments to ssh */
215 ll = SYSLOG_LEVEL_INFO;
216 switch (debug_level) {
218 ll = SYSLOG_LEVEL_DEBUG1;
222 ll = SYSLOG_LEVEL_DEBUG2;
227 ll = SYSLOG_LEVEL_DEBUG3;
237 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
241 fprintf(stderr, "Connecting to %s...\n", host);
243 connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid);
247 interactive_loop(in, out);
252 if (kill(sshpid, SIGHUP) == -1)
253 fatal("Couldn't terminate ssh process: %s", strerror(errno));
255 if (waitpid(sshpid, NULL, 0) == -1)
256 fatal("Couldn't wait for ssh process: %s", strerror(errno));