]> andersk Git - gssapi-openssh.git/blame - openssh/sftp.c
o Bugfix gsi cat page locations
[gssapi-openssh.git] / openssh / sftp.c
CommitLineData
3c0ef626 1/*
e9a17296 2 * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
3c0ef626 3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
23 */
24
25#include "includes.h"
26
1c14df9e 27RCSID("$OpenBSD: sftp.c,v 1.34 2003/01/10 08:19:07 fgsch Exp $");
3c0ef626 28
3c0ef626 29/* XXX: short-form remote directory listings (like 'ls -C') */
30
31#include "buffer.h"
32#include "xmalloc.h"
33#include "log.h"
34#include "pathnames.h"
35#include "misc.h"
36
37#include "sftp.h"
38#include "sftp-common.h"
39#include "sftp-client.h"
40#include "sftp-int.h"
41
42#ifdef HAVE___PROGNAME
43extern char *__progname;
44#else
45char *__progname;
46#endif
47
3c0ef626 48FILE* infile;
e9a17296 49size_t copy_buffer_len = 32768;
50size_t num_requests = 16;
3c0ef626 51
1c14df9e 52extern int showprogress;
53
3c0ef626 54static void
e9a17296 55connect_to_server(char *path, char **args, int *in, int *out, pid_t *sshpid)
3c0ef626 56{
57 int c_in, c_out;
ff2d7a98 58
3c0ef626 59#ifdef USE_PIPES
60 int pin[2], pout[2];
ff2d7a98 61
3c0ef626 62 if ((pipe(pin) == -1) || (pipe(pout) == -1))
63 fatal("pipe: %s", strerror(errno));
64 *in = pin[0];
65 *out = pout[1];
66 c_in = pout[0];
67 c_out = pin[1];
68#else /* USE_PIPES */
69 int inout[2];
ff2d7a98 70
3c0ef626 71 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
72 fatal("socketpair: %s", strerror(errno));
73 *in = *out = inout[0];
74 c_in = c_out = inout[1];
75#endif /* USE_PIPES */
76
77 if ((*sshpid = fork()) == -1)
78 fatal("fork: %s", strerror(errno));
79 else if (*sshpid == 0) {
80 if ((dup2(c_in, STDIN_FILENO) == -1) ||
81 (dup2(c_out, STDOUT_FILENO) == -1)) {
82 fprintf(stderr, "dup2: %s\n", strerror(errno));
83 exit(1);
84 }
85 close(*in);
86 close(*out);
87 close(c_in);
88 close(c_out);
e9a17296 89 execv(path, args);
90 fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
3c0ef626 91 exit(1);
92 }
93
94 close(c_in);
95 close(c_out);
96}
97
98static void
99usage(void)
100{
e9a17296 101 extern char *__progname;
2980ea68 102
3c0ef626 103 fprintf(stderr,
e9a17296 104 "usage: %s [-vC1] [-b batchfile] [-o option] [-s subsystem|path] [-B buffer_size]\n"
105 " [-F config] [-P direct server path] [-S program]\n"
106 " [user@]host[:file [file]]\n", __progname);
3c0ef626 107 exit(1);
108}
109
110int
111main(int argc, char **argv)
112{
1c14df9e 113 int in, out, ch, err;
3c0ef626 114 pid_t sshpid;
115 char *host, *userhost, *cp, *file2;
116 int debug_level = 0, sshver = 2;
117 char *file1 = NULL, *sftp_server = NULL;
ae43c103 118 char *ssh_program = NULL, *sftp_direct = NULL;
3c0ef626 119 LogLevel ll = SYSLOG_LEVEL_INFO;
120 arglist args;
121 extern int optind;
122 extern char *optarg;
123
124 __progname = get_progname(argv[0]);
ae43c103 125 init_pathnames();
126 ssh_program = _PATH_SSH_PROGRAM;
3c0ef626 127 args.list = NULL;
e9a17296 128 addargs(&args, "ssh"); /* overwritten with ssh_program */
3c0ef626 129 addargs(&args, "-oForwardX11 no");
130 addargs(&args, "-oForwardAgent no");
131 addargs(&args, "-oClearAllForwardings yes");
132 ll = SYSLOG_LEVEL_INFO;
133 infile = stdin; /* Read from STDIN unless changed by -b */
134
e9a17296 135 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
3c0ef626 136 switch (ch) {
137 case 'C':
138 addargs(&args, "-C");
139 break;
140 case 'v':
141 if (debug_level < 3) {
142 addargs(&args, "-v");
143 ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
144 }
145 debug_level++;
146 break;
147 case 'F':
148 case 'o':
149 addargs(&args, "-%c%s", ch, optarg);
150 break;
151 case '1':
152 sshver = 1;
153 if (sftp_server == NULL)
154 sftp_server = _PATH_SFTP_SERVER;
155 break;
156 case 's':
157 sftp_server = optarg;
158 break;
159 case 'S':
160 ssh_program = optarg;
161 break;
162 case 'b':
163 if (infile == stdin) {
164 infile = fopen(optarg, "r");
165 if (infile == NULL)
166 fatal("%s (%s).", strerror(errno), optarg);
167 } else
168 fatal("Filename already specified.");
1c14df9e 169 showprogress = 0;
3c0ef626 170 break;
e9a17296 171 case 'P':
172 sftp_direct = optarg;
173 break;
174 case 'B':
175 copy_buffer_len = strtol(optarg, &cp, 10);
176 if (copy_buffer_len == 0 || *cp != '\0')
177 fatal("Invalid buffer size \"%s\"", optarg);
178 break;
179 case 'R':
180 num_requests = strtol(optarg, &cp, 10);
181 if (num_requests == 0 || *cp != '\0')
2980ea68 182 fatal("Invalid number of requests \"%s\"",
e9a17296 183 optarg);
184 break;
3c0ef626 185 case 'h':
186 default:
187 usage();
188 }
189 }
190
2980ea68 191 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
192
e9a17296 193 if (sftp_direct == NULL) {
194 if (optind == argc || argc > (optind + 2))
195 usage();
3c0ef626 196
e9a17296 197 userhost = xstrdup(argv[optind]);
198 file2 = argv[optind+1];
3c0ef626 199
e9a17296 200 if ((cp = colon(userhost)) != NULL) {
201 *cp++ = '\0';
202 file1 = cp;
203 }
3c0ef626 204
1c14df9e 205 if ((host = strrchr(userhost, '@')) == NULL)
e9a17296 206 host = userhost;
207 else {
208 *host++ = '\0';
209 if (!userhost[0]) {
210 fprintf(stderr, "Missing username\n");
211 usage();
212 }
213 addargs(&args, "-l%s",userhost);
3c0ef626 214 }
3c0ef626 215
e9a17296 216 host = cleanhostname(host);
217 if (!*host) {
218 fprintf(stderr, "Missing hostname\n");
219 usage();
220 }
3c0ef626 221
e9a17296 222 addargs(&args, "-oProtocol %d", sshver);
3c0ef626 223
e9a17296 224 /* no subsystem if the server-spec contains a '/' */
225 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
226 addargs(&args, "-s");
3c0ef626 227
e9a17296 228 addargs(&args, "%s", host);
2980ea68 229 addargs(&args, "%s", (sftp_server != NULL ?
e9a17296 230 sftp_server : "sftp"));
231 args.list[0] = ssh_program;
3c0ef626 232
e9a17296 233 fprintf(stderr, "Connecting to %s...\n", host);
2980ea68 234 connect_to_server(ssh_program, args.list, &in, &out,
e9a17296 235 &sshpid);
236 } else {
237 args.list = NULL;
238 addargs(&args, "sftp-server");
3c0ef626 239
e9a17296 240 fprintf(stderr, "Attaching to %s...\n", sftp_direct);
2980ea68 241 connect_to_server(sftp_direct, args.list, &in, &out,
e9a17296 242 &sshpid);
243 }
3c0ef626 244
1c14df9e 245 err = interactive_loop(in, out, file1, file2);
3c0ef626 246
247#if !defined(USE_PIPES)
ff2d7a98 248 shutdown(in, SHUT_RDWR);
249 shutdown(out, SHUT_RDWR);
3c0ef626 250#endif
251
252 close(in);
253 close(out);
254 if (infile != stdin)
255 fclose(infile);
256
2980ea68 257 while (waitpid(sshpid, NULL, 0) == -1)
258 if (errno != EINTR)
259 fatal("Couldn't wait for ssh process: %s",
260 strerror(errno));
3c0ef626 261
1c14df9e 262 exit(err == 0 ? 0 : 1);
3c0ef626 263}
This page took 0.102675 seconds and 5 git commands to generate.