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