]>
Commit | Line | Data |
---|---|---|
61e96248 | 1 | /* |
ab3932ab | 2 | * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> |
61e96248 | 3 | * |
ab3932ab | 4 | * Permission to use, copy, modify, and distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
61e96248 | 7 | * |
ab3932ab | 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
61e96248 | 15 | */ |
16 | ||
17 | #include "includes.h" | |
18 | ||
ab3932ab | 19 | RCSID("$OpenBSD: sftp.c,v 1.42 2004/02/17 05:39:51 djm Exp $"); |
61e96248 | 20 | |
21 | #include "buffer.h" | |
22 | #include "xmalloc.h" | |
23 | #include "log.h" | |
24 | #include "pathnames.h" | |
1fcde3fe | 25 | #include "misc.h" |
61e96248 | 26 | |
27 | #include "sftp.h" | |
28 | #include "sftp-common.h" | |
29 | #include "sftp-client.h" | |
30 | #include "sftp-int.h" | |
31 | ||
5152d46f | 32 | #ifdef HAVE___PROGNAME |
33 | extern char *__progname; | |
34 | #else | |
35 | char *__progname; | |
36 | #endif | |
37 | ||
ac414e17 | 38 | FILE* infile; |
a8b64bb8 | 39 | int batchmode = 0; |
375f867e | 40 | size_t copy_buffer_len = 32768; |
c25d3df7 | 41 | size_t num_requests = 16; |
1b558925 | 42 | static pid_t sshpid = -1; |
0426a3b4 | 43 | |
b65c3807 | 44 | extern int showprogress; |
45 | ||
396c147e | 46 | static void |
1b558925 | 47 | killchild(int signo) |
48 | { | |
49 | if (sshpid > 1) | |
50 | kill(sshpid, signo); | |
51 | ||
52 | _exit(1); | |
53 | } | |
54 | ||
55 | static void | |
56 | connect_to_server(char *path, char **args, int *in, int *out) | |
61e96248 | 57 | { |
58 | int c_in, c_out; | |
9906a836 | 59 | |
61e96248 | 60 | #ifdef USE_PIPES |
61 | int pin[2], pout[2]; | |
9906a836 | 62 | |
61e96248 | 63 | if ((pipe(pin) == -1) || (pipe(pout) == -1)) |
64 | fatal("pipe: %s", strerror(errno)); | |
65 | *in = pin[0]; | |
66 | *out = pout[1]; | |
67 | c_in = pout[0]; | |
68 | c_out = pin[1]; | |
69 | #else /* USE_PIPES */ | |
70 | int inout[2]; | |
9906a836 | 71 | |
61e96248 | 72 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1) |
73 | fatal("socketpair: %s", strerror(errno)); | |
74 | *in = *out = inout[0]; | |
75 | c_in = c_out = inout[1]; | |
76 | #endif /* USE_PIPES */ | |
77 | ||
1b558925 | 78 | if ((sshpid = fork()) == -1) |
61e96248 | 79 | fatal("fork: %s", strerror(errno)); |
1b558925 | 80 | else if (sshpid == 0) { |
61e96248 | 81 | if ((dup2(c_in, STDIN_FILENO) == -1) || |
82 | (dup2(c_out, STDOUT_FILENO) == -1)) { | |
83 | fprintf(stderr, "dup2: %s\n", strerror(errno)); | |
84 | exit(1); | |
85 | } | |
86 | close(*in); | |
87 | close(*out); | |
88 | close(c_in); | |
89 | close(c_out); | |
a96fd7c2 | 90 | execv(path, args); |
91 | fprintf(stderr, "exec: %s: %s\n", path, strerror(errno)); | |
61e96248 | 92 | exit(1); |
93 | } | |
94 | ||
1b558925 | 95 | signal(SIGTERM, killchild); |
96 | signal(SIGINT, killchild); | |
97 | signal(SIGHUP, killchild); | |
61e96248 | 98 | close(c_in); |
99 | close(c_out); | |
100 | } | |
101 | ||
396c147e | 102 | static void |
61e96248 | 103 | usage(void) |
104 | { | |
22be05a5 | 105 | extern char *__progname; |
762715ce | 106 | |
f1278af7 | 107 | fprintf(stderr, |
433e60ac | 108 | "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n" |
109 | " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n" | |
110 | " [-S program] [-s subsystem | sftp_server] host\n" | |
111 | " %s [[user@]host[:file [file]]]\n" | |
112 | " %s [[user@]host[:dir[/]]]\n" | |
113 | " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname); | |
61e96248 | 114 | exit(1); |
115 | } | |
116 | ||
2b87da3b | 117 | int |
61e96248 | 118 | main(int argc, char **argv) |
119 | { | |
9a36208d | 120 | int in, out, ch, err; |
edeeab1e | 121 | char *host, *userhost, *cp, *file2; |
8a624ebf | 122 | int debug_level = 0, sshver = 2; |
123 | char *file1 = NULL, *sftp_server = NULL; | |
a96fd7c2 | 124 | char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL; |
8a624ebf | 125 | LogLevel ll = SYSLOG_LEVEL_INFO; |
126 | arglist args; | |
0426a3b4 | 127 | extern int optind; |
128 | extern char *optarg; | |
61e96248 | 129 | |
fda04d7d | 130 | __progname = ssh_get_progname(argv[0]); |
8a624ebf | 131 | args.list = NULL; |
184eed6a | 132 | addargs(&args, "ssh"); /* overwritten with ssh_program */ |
8a624ebf | 133 | addargs(&args, "-oForwardX11 no"); |
134 | addargs(&args, "-oForwardAgent no"); | |
e1c5bfaf | 135 | addargs(&args, "-oClearAllForwardings yes"); |
ac414e17 | 136 | |
8a624ebf | 137 | ll = SYSLOG_LEVEL_INFO; |
ac414e17 | 138 | infile = stdin; |
0426a3b4 | 139 | |
c25d3df7 | 140 | while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) { |
0426a3b4 | 141 | switch (ch) { |
142 | case 'C': | |
8a624ebf | 143 | addargs(&args, "-C"); |
0426a3b4 | 144 | break; |
145 | case 'v': | |
8a624ebf | 146 | if (debug_level < 3) { |
147 | addargs(&args, "-v"); | |
148 | ll = SYSLOG_LEVEL_DEBUG1 + debug_level; | |
149 | } | |
150 | debug_level++; | |
0426a3b4 | 151 | break; |
f1278af7 | 152 | case 'F': |
0426a3b4 | 153 | case 'o': |
f1278af7 | 154 | addargs(&args, "-%c%s", ch, optarg); |
0426a3b4 | 155 | break; |
156 | case '1': | |
8a624ebf | 157 | sshver = 1; |
0426a3b4 | 158 | if (sftp_server == NULL) |
159 | sftp_server = _PATH_SFTP_SERVER; | |
160 | break; | |
161 | case 's': | |
162 | sftp_server = optarg; | |
163 | break; | |
164 | case 'S': | |
165 | ssh_program = optarg; | |
166 | break; | |
a5ec8a3d | 167 | case 'b': |
a8b64bb8 | 168 | if (batchmode) |
169 | fatal("Batch file already specified."); | |
170 | ||
171 | /* Allow "-" as stdin */ | |
172 | if (strcmp(optarg, "-") != 0 && | |
173 | (infile = fopen(optarg, "r")) == NULL) | |
174 | fatal("%s (%s).", strerror(errno), optarg); | |
b65c3807 | 175 | showprogress = 0; |
a8b64bb8 | 176 | batchmode = 1; |
a5ec8a3d | 177 | break; |
a96fd7c2 | 178 | case 'P': |
179 | sftp_direct = optarg; | |
180 | break; | |
375f867e | 181 | case 'B': |
182 | copy_buffer_len = strtol(optarg, &cp, 10); | |
183 | if (copy_buffer_len == 0 || *cp != '\0') | |
184 | fatal("Invalid buffer size \"%s\"", optarg); | |
185 | break; | |
c25d3df7 | 186 | case 'R': |
187 | num_requests = strtol(optarg, &cp, 10); | |
188 | if (num_requests == 0 || *cp != '\0') | |
762715ce | 189 | fatal("Invalid number of requests \"%s\"", |
c25d3df7 | 190 | optarg); |
191 | break; | |
0426a3b4 | 192 | case 'h': |
193 | default: | |
61e96248 | 194 | usage(); |
195 | } | |
196 | } | |
197 | ||
b69145c2 | 198 | log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); |
199 | ||
a96fd7c2 | 200 | if (sftp_direct == NULL) { |
201 | if (optind == argc || argc > (optind + 2)) | |
202 | usage(); | |
61e96248 | 203 | |
a96fd7c2 | 204 | userhost = xstrdup(argv[optind]); |
205 | file2 = argv[optind+1]; | |
edeeab1e | 206 | |
15748b4d | 207 | if ((host = strrchr(userhost, '@')) == NULL) |
a96fd7c2 | 208 | host = userhost; |
209 | else { | |
210 | *host++ = '\0'; | |
211 | if (!userhost[0]) { | |
212 | fprintf(stderr, "Missing username\n"); | |
213 | usage(); | |
214 | } | |
215 | addargs(&args, "-l%s",userhost); | |
61e96248 | 216 | } |
61e96248 | 217 | |
02de7c6e | 218 | if ((cp = colon(host)) != NULL) { |
219 | *cp++ = '\0'; | |
220 | file1 = cp; | |
221 | } | |
222 | ||
a96fd7c2 | 223 | host = cleanhostname(host); |
224 | if (!*host) { | |
225 | fprintf(stderr, "Missing hostname\n"); | |
226 | usage(); | |
227 | } | |
61e96248 | 228 | |
a96fd7c2 | 229 | addargs(&args, "-oProtocol %d", sshver); |
8a624ebf | 230 | |
a96fd7c2 | 231 | /* no subsystem if the server-spec contains a '/' */ |
232 | if (sftp_server == NULL || strchr(sftp_server, '/') == NULL) | |
233 | addargs(&args, "-s"); | |
61e96248 | 234 | |
a96fd7c2 | 235 | addargs(&args, "%s", host); |
762715ce | 236 | addargs(&args, "%s", (sftp_server != NULL ? |
a96fd7c2 | 237 | sftp_server : "sftp")); |
238 | args.list[0] = ssh_program; | |
61e96248 | 239 | |
a8b64bb8 | 240 | if (!batchmode) |
241 | fprintf(stderr, "Connecting to %s...\n", host); | |
1b558925 | 242 | connect_to_server(ssh_program, args.list, &in, &out); |
a96fd7c2 | 243 | } else { |
244 | args.list = NULL; | |
245 | addargs(&args, "sftp-server"); | |
61e96248 | 246 | |
a8b64bb8 | 247 | if (!batchmode) |
248 | fprintf(stderr, "Attaching to %s...\n", sftp_direct); | |
1b558925 | 249 | connect_to_server(sftp_direct, args.list, &in, &out); |
a96fd7c2 | 250 | } |
61e96248 | 251 | |
9a36208d | 252 | err = interactive_loop(in, out, file1, file2); |
61e96248 | 253 | |
51fb577a | 254 | #if !defined(USE_PIPES) |
875ec275 | 255 | shutdown(in, SHUT_RDWR); |
256 | shutdown(out, SHUT_RDWR); | |
51fb577a | 257 | #endif |
258 | ||
61e96248 | 259 | close(in); |
260 | close(out); | |
a8b64bb8 | 261 | if (batchmode) |
a5ec8a3d | 262 | fclose(infile); |
61e96248 | 263 | |
8c38e88b | 264 | while (waitpid(sshpid, NULL, 0) == -1) |
265 | if (errno != EINTR) | |
266 | fatal("Couldn't wait for ssh process: %s", | |
267 | strerror(errno)); | |
61e96248 | 268 | |
9a36208d | 269 | exit(err == 0 ? 0 : 1); |
61e96248 | 270 | } |