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 #include "scp-common.h"
45 char *ssh_program = _PATH_SSH_PROGRAM;
46 char *sftp_server = NULL;
50 connect_to_server(char **args, int *in, int *out, pid_t *sshpid)
55 if ((pipe(pin) == -1) || (pipe(pout) == -1))
56 fatal("pipe: %s", strerror(errno));
63 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
64 fatal("socketpair: %s", strerror(errno));
65 *in = *out = inout[0];
66 c_in = c_out = inout[1];
67 #endif /* USE_PIPES */
69 if ((*sshpid = fork()) == -1)
70 fatal("fork: %s", strerror(errno));
71 else if (*sshpid == 0) {
72 if ((dup2(c_in, STDIN_FILENO) == -1) ||
73 (dup2(c_out, STDOUT_FILENO) == -1)) {
74 fprintf(stderr, "dup2: %s\n", strerror(errno));
81 execv(ssh_program, args);
82 fprintf(stderr, "exec: %s: %s\n", ssh_program, strerror(errno));
91 make_ssh_args(char *add_arg)
93 static char **args = NULL;
102 args = xmalloc(sizeof(*args) * nargs);
107 /* If asked to add args, then do so and return */
110 args = xrealloc(args, sizeof(*args) * nargs);
116 /* no subsystem if the server-spec contains a '/' */
117 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
119 make_ssh_args("-oForwardX11=no");
120 make_ssh_args("-oForwardAgent=no");
121 make_ssh_args(use_ssh1 ? "-oProtocol=1" : "-oProtocol=2");
123 /* Otherwise finish up and return the arg array */
124 if (sftp_server != NULL)
125 make_ssh_args(sftp_server);
127 make_ssh_args("sftp");
129 /* XXX: overflow - doesn't grow debug_buf */
131 for(i = 0; args[i]; i++) {
133 strlcat(debug_buf, " ", sizeof(debug_buf));
135 strlcat(debug_buf, args[i], sizeof(debug_buf));
137 debug("SSH args \"%s\"", debug_buf);
145 fprintf(stderr, "usage: sftp [-1vC] [-b batchfile] [-osshopt=value] [user@]host[:file [file]]\n");
150 main(int argc, char **argv)
152 int in, out, ch, debug_level, compress_flag;
155 char *host, *userhost, *cp, *file2;
160 infile = stdin; /* Read from STDIN unless changed by -b */
161 debug_level = compress_flag = 0;
163 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:")) != -1) {
169 debug_level = MIN(3, debug_level + 1);
173 make_ssh_args(optarg);
177 if (sftp_server == NULL)
178 sftp_server = _PATH_SFTP_SERVER;
181 sftp_server = optarg;
184 ssh_program = optarg;
187 if (infile == stdin) {
188 infile = fopen(optarg, "r");
190 fatal("%s (%s).", strerror(errno), optarg);
192 fatal("Filename already specified.");
200 if (optind == argc || argc > (optind + 2))
203 userhost = xstrdup(argv[optind]);
204 file2 = argv[optind+1];
206 if ((cp = colon(userhost)) != NULL) {
211 if ((host = strchr(userhost, '@')) == NULL)
216 fprintf(stderr, "Missing username\n");
220 make_ssh_args(userhost);
223 host = cleanhostname(host);
225 fprintf(stderr, "Missing hostname\n");
229 /* Set up logging and debug '-d' arguments to ssh */
230 ll = SYSLOG_LEVEL_INFO;
231 switch (debug_level) {
233 ll = SYSLOG_LEVEL_DEBUG1;
237 ll = SYSLOG_LEVEL_DEBUG2;
242 ll = SYSLOG_LEVEL_DEBUG3;
252 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
256 fprintf(stderr, "Connecting to %s...\n", host);
258 connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid);
260 interactive_loop(in, out, file1, file2);
267 if (waitpid(sshpid, NULL, 0) == -1)
268 fatal("Couldn't wait for ssh process: %s", strerror(errno));