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.1 2001/02/04 11:11:54 djm 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 connect_to_server(char **args, int *in, int *out, pid_t *sshpid)
49 if ((pipe(pin) == -1) || (pipe(pout) == -1))
50 fatal("pipe: %s", strerror(errno));
57 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
58 fatal("socketpair: %s", strerror(errno));
59 *in = *out = inout[0];
60 c_in = c_out = inout[1];
61 #endif /* USE_PIPES */
63 if ((*sshpid = fork()) == -1)
64 fatal("fork: %s", strerror(errno));
65 else if (*sshpid == 0) {
66 if ((dup2(c_in, STDIN_FILENO) == -1) ||
67 (dup2(c_out, STDOUT_FILENO) == -1)) {
68 fprintf(stderr, "dup2: %s\n", strerror(errno));
75 execv(_PATH_SSH_PROGRAM, args);
76 fprintf(stderr, "exec: %s", strerror(errno));
85 make_ssh_args(char *add_arg)
87 static char **args = NULL;
96 args = xmalloc(sizeof(*args) * nargs);
98 args[i++] = "-oProtocol=2";
103 /* If asked to add args, then do so and return */
106 args = xrealloc(args, sizeof(*args) * nargs);
112 /* Otherwise finish up and return the arg array */
113 make_ssh_args("sftp");
115 /* XXX: overflow - doesn't grow debug_buf */
117 for(i = 0; args[i]; i++) {
119 strlcat(debug_buf, " ", sizeof(debug_buf));
121 strlcat(debug_buf, args[i], sizeof(debug_buf));
123 debug("SSH args \"%s\"", debug_buf);
131 fprintf(stderr, "usage: sftp [-vC] [-osshopt=value] [user@]host\n");
136 main(int argc, char **argv)
138 int in, out, i, debug_level, compress_flag;
143 debug_level = compress_flag = 0;
144 for(i = 1; i < argc && argv[i][0] == '-'; i++) {
145 if (!strcmp(argv[i], "-v"))
146 debug_level = MIN(3, debug_level + 1);
147 else if (!strcmp(argv[i], "-C"))
149 else if (!strncmp(argv[i], "-o", 2)) {
150 make_ssh_args(argv[i]);
152 fprintf(stderr, "Unknown option \"%s\"\n", argv[i]);
157 if (i == argc || argc > (i + 1))
160 if ((cp = strchr(argv[i], '@')) == NULL)
165 fprintf(stderr, "Missing username\n");
169 make_ssh_args(argv[i]);
174 fprintf(stderr, "Missing hostname\n");
178 /* Set up logging and debug '-d' arguments to ssh */
179 ll = SYSLOG_LEVEL_INFO;
180 switch (debug_level) {
182 ll = SYSLOG_LEVEL_DEBUG1;
186 ll = SYSLOG_LEVEL_DEBUG2;
191 ll = SYSLOG_LEVEL_DEBUG3;
201 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
205 fprintf(stderr, "Connecting to %s...\n", cp);
207 connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid);
211 interactive_loop(in, out);
216 if (kill(sshpid, SIGHUP) == -1)
217 fatal("Couldn't terminate ssh process: %s", strerror(errno));