]>
Commit | Line | Data |
---|---|---|
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 | ||
e54b3d7c | 27 | RCSID("$OpenBSD: sftp.c,v 1.31 2002/07/25 01:16:59 mouring 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 | |
43 | extern char *__progname; | |
44 | #else | |
45 | char *__progname; | |
46 | #endif | |
47 | ||
3c0ef626 | 48 | FILE* infile; |
e9a17296 | 49 | size_t copy_buffer_len = 32768; |
50 | size_t num_requests = 16; | |
3c0ef626 | 51 | |
52 | static void | |
e9a17296 | 53 | connect_to_server(char *path, char **args, int *in, int *out, pid_t *sshpid) |
3c0ef626 | 54 | { |
55 | int c_in, c_out; | |
ff2d7a98 | 56 | |
3c0ef626 | 57 | #ifdef USE_PIPES |
58 | int pin[2], pout[2]; | |
ff2d7a98 | 59 | |
3c0ef626 | 60 | if ((pipe(pin) == -1) || (pipe(pout) == -1)) |
61 | fatal("pipe: %s", strerror(errno)); | |
62 | *in = pin[0]; | |
63 | *out = pout[1]; | |
64 | c_in = pout[0]; | |
65 | c_out = pin[1]; | |
66 | #else /* USE_PIPES */ | |
67 | int inout[2]; | |
ff2d7a98 | 68 | |
3c0ef626 | 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 */ | |
74 | ||
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)); | |
81 | exit(1); | |
82 | } | |
83 | close(*in); | |
84 | close(*out); | |
85 | close(c_in); | |
86 | close(c_out); | |
e9a17296 | 87 | execv(path, args); |
88 | fprintf(stderr, "exec: %s: %s\n", path, strerror(errno)); | |
3c0ef626 | 89 | exit(1); |
90 | } | |
91 | ||
92 | close(c_in); | |
93 | close(c_out); | |
94 | } | |
95 | ||
96 | static void | |
97 | usage(void) | |
98 | { | |
e9a17296 | 99 | extern char *__progname; |
2980ea68 | 100 | |
3c0ef626 | 101 | fprintf(stderr, |
e9a17296 | 102 | "usage: %s [-vC1] [-b batchfile] [-o option] [-s subsystem|path] [-B buffer_size]\n" |
103 | " [-F config] [-P direct server path] [-S program]\n" | |
104 | " [user@]host[:file [file]]\n", __progname); | |
3c0ef626 | 105 | exit(1); |
106 | } | |
107 | ||
108 | int | |
109 | main(int argc, char **argv) | |
110 | { | |
111 | int in, out, ch; | |
112 | pid_t sshpid; | |
113 | char *host, *userhost, *cp, *file2; | |
114 | int debug_level = 0, sshver = 2; | |
115 | char *file1 = NULL, *sftp_server = NULL; | |
ae43c103 | 116 | char *ssh_program = NULL, *sftp_direct = NULL; |
3c0ef626 | 117 | LogLevel ll = SYSLOG_LEVEL_INFO; |
118 | arglist args; | |
119 | extern int optind; | |
120 | extern char *optarg; | |
121 | ||
122 | __progname = get_progname(argv[0]); | |
ae43c103 | 123 | init_pathnames(); |
124 | ssh_program = _PATH_SSH_PROGRAM; | |
3c0ef626 | 125 | args.list = NULL; |
e9a17296 | 126 | addargs(&args, "ssh"); /* overwritten with ssh_program */ |
3c0ef626 | 127 | addargs(&args, "-oForwardX11 no"); |
128 | addargs(&args, "-oForwardAgent no"); | |
129 | addargs(&args, "-oClearAllForwardings yes"); | |
130 | ll = SYSLOG_LEVEL_INFO; | |
131 | infile = stdin; /* Read from STDIN unless changed by -b */ | |
132 | ||
e9a17296 | 133 | while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) { |
3c0ef626 | 134 | switch (ch) { |
135 | case 'C': | |
136 | addargs(&args, "-C"); | |
137 | break; | |
138 | case 'v': | |
139 | if (debug_level < 3) { | |
140 | addargs(&args, "-v"); | |
141 | ll = SYSLOG_LEVEL_DEBUG1 + debug_level; | |
142 | } | |
143 | debug_level++; | |
144 | break; | |
145 | case 'F': | |
146 | case 'o': | |
147 | addargs(&args, "-%c%s", ch, optarg); | |
148 | break; | |
149 | case '1': | |
150 | sshver = 1; | |
151 | if (sftp_server == NULL) | |
152 | sftp_server = _PATH_SFTP_SERVER; | |
153 | break; | |
154 | case 's': | |
155 | sftp_server = optarg; | |
156 | break; | |
157 | case 'S': | |
158 | ssh_program = optarg; | |
159 | break; | |
160 | case 'b': | |
161 | if (infile == stdin) { | |
162 | infile = fopen(optarg, "r"); | |
163 | if (infile == NULL) | |
164 | fatal("%s (%s).", strerror(errno), optarg); | |
165 | } else | |
166 | fatal("Filename already specified."); | |
167 | break; | |
e9a17296 | 168 | case 'P': |
169 | sftp_direct = optarg; | |
170 | break; | |
171 | case 'B': | |
172 | copy_buffer_len = strtol(optarg, &cp, 10); | |
173 | if (copy_buffer_len == 0 || *cp != '\0') | |
174 | fatal("Invalid buffer size \"%s\"", optarg); | |
175 | break; | |
176 | case 'R': | |
177 | num_requests = strtol(optarg, &cp, 10); | |
178 | if (num_requests == 0 || *cp != '\0') | |
2980ea68 | 179 | fatal("Invalid number of requests \"%s\"", |
e9a17296 | 180 | optarg); |
181 | break; | |
3c0ef626 | 182 | case 'h': |
183 | default: | |
184 | usage(); | |
185 | } | |
186 | } | |
187 | ||
2980ea68 | 188 | log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); |
189 | ||
e9a17296 | 190 | if (sftp_direct == NULL) { |
191 | if (optind == argc || argc > (optind + 2)) | |
192 | usage(); | |
3c0ef626 | 193 | |
e9a17296 | 194 | userhost = xstrdup(argv[optind]); |
195 | file2 = argv[optind+1]; | |
3c0ef626 | 196 | |
e9a17296 | 197 | if ((cp = colon(userhost)) != NULL) { |
198 | *cp++ = '\0'; | |
199 | file1 = cp; | |
200 | } | |
3c0ef626 | 201 | |
e9a17296 | 202 | if ((host = strchr(userhost, '@')) == NULL) |
203 | host = userhost; | |
204 | else { | |
205 | *host++ = '\0'; | |
206 | if (!userhost[0]) { | |
207 | fprintf(stderr, "Missing username\n"); | |
208 | usage(); | |
209 | } | |
210 | addargs(&args, "-l%s",userhost); | |
3c0ef626 | 211 | } |
3c0ef626 | 212 | |
e9a17296 | 213 | host = cleanhostname(host); |
214 | if (!*host) { | |
215 | fprintf(stderr, "Missing hostname\n"); | |
216 | usage(); | |
217 | } | |
3c0ef626 | 218 | |
e9a17296 | 219 | addargs(&args, "-oProtocol %d", sshver); |
3c0ef626 | 220 | |
e9a17296 | 221 | /* no subsystem if the server-spec contains a '/' */ |
222 | if (sftp_server == NULL || strchr(sftp_server, '/') == NULL) | |
223 | addargs(&args, "-s"); | |
3c0ef626 | 224 | |
e9a17296 | 225 | addargs(&args, "%s", host); |
2980ea68 | 226 | addargs(&args, "%s", (sftp_server != NULL ? |
e9a17296 | 227 | sftp_server : "sftp")); |
228 | args.list[0] = ssh_program; | |
3c0ef626 | 229 | |
e9a17296 | 230 | fprintf(stderr, "Connecting to %s...\n", host); |
2980ea68 | 231 | connect_to_server(ssh_program, args.list, &in, &out, |
e9a17296 | 232 | &sshpid); |
233 | } else { | |
234 | args.list = NULL; | |
235 | addargs(&args, "sftp-server"); | |
3c0ef626 | 236 | |
e9a17296 | 237 | fprintf(stderr, "Attaching to %s...\n", sftp_direct); |
2980ea68 | 238 | connect_to_server(sftp_direct, args.list, &in, &out, |
e9a17296 | 239 | &sshpid); |
240 | } | |
3c0ef626 | 241 | |
242 | interactive_loop(in, out, file1, file2); | |
243 | ||
244 | #if !defined(USE_PIPES) | |
ff2d7a98 | 245 | shutdown(in, SHUT_RDWR); |
246 | shutdown(out, SHUT_RDWR); | |
3c0ef626 | 247 | #endif |
248 | ||
249 | close(in); | |
250 | close(out); | |
251 | if (infile != stdin) | |
252 | fclose(infile); | |
253 | ||
2980ea68 | 254 | while (waitpid(sshpid, NULL, 0) == -1) |
255 | if (errno != EINTR) | |
256 | fatal("Couldn't wait for ssh process: %s", | |
257 | strerror(errno)); | |
3c0ef626 | 258 | |
259 | exit(0); | |
260 | } |