]>
Commit | Line | Data |
---|---|---|
a733c71c | 1 | /* $OpenBSD: sftp.c,v 1.97 2007/10/24 03:30:02 djm Exp $ */ |
61e96248 | 2 | /* |
ab3932ab | 3 | * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> |
61e96248 | 4 | * |
ab3932ab | 5 | * Permission to use, copy, modify, and distribute this software for any |
6 | * purpose with or without fee is hereby granted, provided that the above | |
7 | * copyright notice and this permission notice appear in all copies. | |
61e96248 | 8 | * |
ab3932ab | 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
61e96248 | 16 | */ |
17 | ||
18 | #include "includes.h" | |
19 | ||
aa2eae64 | 20 | #include <sys/types.h> |
31652869 | 21 | #include <sys/ioctl.h> |
4095f623 | 22 | #ifdef HAVE_SYS_STAT_H |
23 | # include <sys/stat.h> | |
24 | #endif | |
536c14e8 | 25 | #include <sys/param.h> |
5b04a8bf | 26 | #include <sys/socket.h> |
aa2eae64 | 27 | #include <sys/wait.h> |
5132eac0 | 28 | |
a733c71c | 29 | #include <ctype.h> |
028094f4 | 30 | #include <errno.h> |
31 | ||
a75f5360 | 32 | #ifdef HAVE_PATHS_H |
6f61e0ec | 33 | # include <paths.h> |
a75f5360 | 34 | #endif |
5132eac0 | 35 | #ifdef USE_LIBEDIT |
36 | #include <histedit.h> | |
37 | #else | |
38 | typedef void EditLine; | |
39 | #endif | |
ada68823 | 40 | #include <signal.h> |
ffa517a8 | 41 | #include <stdlib.h> |
cf851879 | 42 | #include <stdio.h> |
00146caa | 43 | #include <string.h> |
5188ba17 | 44 | #include <unistd.h> |
31652869 | 45 | #include <stdarg.h> |
61e96248 | 46 | |
61e96248 | 47 | #include "xmalloc.h" |
48 | #include "log.h" | |
49 | #include "pathnames.h" | |
1fcde3fe | 50 | #include "misc.h" |
61e96248 | 51 | |
52 | #include "sftp.h" | |
31652869 | 53 | #include "buffer.h" |
61e96248 | 54 | #include "sftp-common.h" |
55 | #include "sftp-client.h" | |
caf1e9f0 | 56 | |
2cda7d6b | 57 | /* File to read commands from */ |
58 | FILE* infile; | |
59 | ||
60 | /* Are we in batchfile mode? */ | |
61 | int batchmode = 0; | |
62 | ||
63 | /* Size of buffer used when copying files */ | |
64 | size_t copy_buffer_len = 32768; | |
65 | ||
66 | /* Number of concurrent outstanding requests */ | |
67 | size_t num_requests = 16; | |
68 | ||
69 | /* PID of ssh transport process */ | |
70 | static pid_t sshpid = -1; | |
71 | ||
72 | /* This is set to 0 if the progressmeter is not desired. */ | |
06abcf97 | 73 | int showprogress = 1; |
2cda7d6b | 74 | |
0e5de6f8 | 75 | /* SIGINT received during command processing */ |
76 | volatile sig_atomic_t interrupted = 0; | |
77 | ||
95cbd340 | 78 | /* I wish qsort() took a separate ctx for the comparison function...*/ |
79 | int sort_flag; | |
80 | ||
2cda7d6b | 81 | int remote_glob(struct sftp_conn *, const char *, int, |
82 | int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */ | |
61e96248 | 83 | |
5152d46f | 84 | extern char *__progname; |
5152d46f | 85 | |
2cda7d6b | 86 | /* Separators for interactive commands */ |
87 | #define WHITESPACE " \t\r\n" | |
88 | ||
95cbd340 | 89 | /* ls flags */ |
ae7daec3 | 90 | #define LS_LONG_VIEW 0x01 /* Full view ala ls -l */ |
91 | #define LS_SHORT_VIEW 0x02 /* Single row view ala ls -1 */ | |
92 | #define LS_NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */ | |
93 | #define LS_NAME_SORT 0x08 /* Sort by name (default) */ | |
94 | #define LS_TIME_SORT 0x10 /* Sort by mtime */ | |
95 | #define LS_SIZE_SORT 0x20 /* Sort by file size */ | |
96 | #define LS_REVERSE_SORT 0x40 /* Reverse sort order */ | |
cc4ff6c4 | 97 | #define LS_SHOW_ALL 0x80 /* Don't skip filenames starting with '.' */ |
95cbd340 | 98 | |
ae7daec3 | 99 | #define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW) |
100 | #define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT) | |
2cda7d6b | 101 | |
102 | /* Commands for interactive mode */ | |
103 | #define I_CHDIR 1 | |
104 | #define I_CHGRP 2 | |
105 | #define I_CHMOD 3 | |
106 | #define I_CHOWN 4 | |
107 | #define I_GET 5 | |
108 | #define I_HELP 6 | |
109 | #define I_LCHDIR 7 | |
110 | #define I_LLS 8 | |
111 | #define I_LMKDIR 9 | |
112 | #define I_LPWD 10 | |
113 | #define I_LS 11 | |
114 | #define I_LUMASK 12 | |
115 | #define I_MKDIR 13 | |
116 | #define I_PUT 14 | |
117 | #define I_PWD 15 | |
118 | #define I_QUIT 16 | |
119 | #define I_RENAME 17 | |
120 | #define I_RM 18 | |
121 | #define I_RMDIR 19 | |
122 | #define I_SHELL 20 | |
123 | #define I_SYMLINK 21 | |
124 | #define I_VERSION 22 | |
125 | #define I_PROGRESS 23 | |
126 | ||
127 | struct CMD { | |
128 | const char *c; | |
129 | const int n; | |
130 | }; | |
131 | ||
132 | static const struct CMD cmds[] = { | |
133 | { "bye", I_QUIT }, | |
134 | { "cd", I_CHDIR }, | |
135 | { "chdir", I_CHDIR }, | |
136 | { "chgrp", I_CHGRP }, | |
137 | { "chmod", I_CHMOD }, | |
138 | { "chown", I_CHOWN }, | |
139 | { "dir", I_LS }, | |
140 | { "exit", I_QUIT }, | |
141 | { "get", I_GET }, | |
142 | { "mget", I_GET }, | |
143 | { "help", I_HELP }, | |
144 | { "lcd", I_LCHDIR }, | |
145 | { "lchdir", I_LCHDIR }, | |
146 | { "lls", I_LLS }, | |
147 | { "lmkdir", I_LMKDIR }, | |
148 | { "ln", I_SYMLINK }, | |
149 | { "lpwd", I_LPWD }, | |
150 | { "ls", I_LS }, | |
151 | { "lumask", I_LUMASK }, | |
152 | { "mkdir", I_MKDIR }, | |
153 | { "progress", I_PROGRESS }, | |
154 | { "put", I_PUT }, | |
155 | { "mput", I_PUT }, | |
156 | { "pwd", I_PWD }, | |
157 | { "quit", I_QUIT }, | |
158 | { "rename", I_RENAME }, | |
159 | { "rm", I_RM }, | |
160 | { "rmdir", I_RMDIR }, | |
161 | { "symlink", I_SYMLINK }, | |
162 | { "version", I_VERSION }, | |
163 | { "!", I_SHELL }, | |
164 | { "?", I_HELP }, | |
165 | { NULL, -1} | |
166 | }; | |
167 | ||
168 | int interactive_loop(int fd_in, int fd_out, char *file1, char *file2); | |
169 | ||
c9625a42 | 170 | /* ARGSUSED */ |
0e5de6f8 | 171 | static void |
172 | killchild(int signo) | |
173 | { | |
3c03ad3f | 174 | if (sshpid > 1) { |
0e5de6f8 | 175 | kill(sshpid, SIGTERM); |
3c03ad3f | 176 | waitpid(sshpid, NULL, 0); |
177 | } | |
0e5de6f8 | 178 | |
179 | _exit(1); | |
180 | } | |
181 | ||
c9625a42 | 182 | /* ARGSUSED */ |
0e5de6f8 | 183 | static void |
184 | cmd_interrupt(int signo) | |
185 | { | |
186 | const char msg[] = "\rInterrupt \n"; | |
47460206 | 187 | int olderrno = errno; |
0e5de6f8 | 188 | |
189 | write(STDERR_FILENO, msg, sizeof(msg) - 1); | |
190 | interrupted = 1; | |
47460206 | 191 | errno = olderrno; |
0e5de6f8 | 192 | } |
193 | ||
2cda7d6b | 194 | static void |
195 | help(void) | |
196 | { | |
197 | printf("Available commands:\n"); | |
198 | printf("cd path Change remote directory to 'path'\n"); | |
199 | printf("lcd path Change local directory to 'path'\n"); | |
200 | printf("chgrp grp path Change group of file 'path' to 'grp'\n"); | |
201 | printf("chmod mode path Change permissions of file 'path' to 'mode'\n"); | |
202 | printf("chown own path Change owner of file 'path' to 'own'\n"); | |
203 | printf("help Display this help text\n"); | |
204 | printf("get remote-path [local-path] Download file\n"); | |
205 | printf("lls [ls-options [path]] Display local directory listing\n"); | |
206 | printf("ln oldpath newpath Symlink remote file\n"); | |
207 | printf("lmkdir path Create local directory\n"); | |
208 | printf("lpwd Print local working directory\n"); | |
209 | printf("ls [path] Display remote directory listing\n"); | |
210 | printf("lumask umask Set local umask to 'umask'\n"); | |
211 | printf("mkdir path Create remote directory\n"); | |
212 | printf("progress Toggle display of progress meter\n"); | |
213 | printf("put local-path [remote-path] Upload file\n"); | |
214 | printf("pwd Display remote working directory\n"); | |
215 | printf("exit Quit sftp\n"); | |
216 | printf("quit Quit sftp\n"); | |
217 | printf("rename oldpath newpath Rename remote file\n"); | |
218 | printf("rmdir path Remove remote directory\n"); | |
219 | printf("rm path Delete remote file\n"); | |
220 | printf("symlink oldpath newpath Symlink remote file\n"); | |
221 | printf("version Show SFTP version\n"); | |
222 | printf("!command Execute 'command' in local shell\n"); | |
223 | printf("! Escape to local shell\n"); | |
224 | printf("? Synonym for help\n"); | |
225 | } | |
226 | ||
227 | static void | |
228 | local_do_shell(const char *args) | |
229 | { | |
230 | int status; | |
231 | char *shell; | |
232 | pid_t pid; | |
233 | ||
234 | if (!*args) | |
235 | args = NULL; | |
236 | ||
237 | if ((shell = getenv("SHELL")) == NULL) | |
238 | shell = _PATH_BSHELL; | |
239 | ||
240 | if ((pid = fork()) == -1) | |
241 | fatal("Couldn't fork: %s", strerror(errno)); | |
242 | ||
243 | if (pid == 0) { | |
244 | /* XXX: child has pipe fds to ssh subproc open - issue? */ | |
245 | if (args) { | |
246 | debug3("Executing %s -c \"%s\"", shell, args); | |
247 | execl(shell, shell, "-c", args, (char *)NULL); | |
248 | } else { | |
249 | debug3("Executing %s", shell); | |
250 | execl(shell, shell, (char *)NULL); | |
251 | } | |
252 | fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell, | |
253 | strerror(errno)); | |
254 | _exit(1); | |
255 | } | |
256 | while (waitpid(pid, &status, 0) == -1) | |
257 | if (errno != EINTR) | |
258 | fatal("Couldn't wait for child: %s", strerror(errno)); | |
259 | if (!WIFEXITED(status)) | |
522327f5 | 260 | error("Shell exited abnormally"); |
2cda7d6b | 261 | else if (WEXITSTATUS(status)) |
262 | error("Shell exited with status %d", WEXITSTATUS(status)); | |
263 | } | |
264 | ||
265 | static void | |
266 | local_do_ls(const char *args) | |
267 | { | |
268 | if (!args || !*args) | |
269 | local_do_shell(_PATH_LS); | |
270 | else { | |
271 | int len = strlen(_PATH_LS " ") + strlen(args) + 1; | |
272 | char *buf = xmalloc(len); | |
273 | ||
274 | /* XXX: quoting - rip quoting code from ftp? */ | |
275 | snprintf(buf, len, _PATH_LS " %s", args); | |
276 | local_do_shell(buf); | |
277 | xfree(buf); | |
278 | } | |
279 | } | |
280 | ||
281 | /* Strip one path (usually the pwd) from the start of another */ | |
282 | static char * | |
283 | path_strip(char *path, char *strip) | |
284 | { | |
285 | size_t len; | |
0426a3b4 | 286 | |
2cda7d6b | 287 | if (strip == NULL) |
288 | return (xstrdup(path)); | |
289 | ||
290 | len = strlen(strip); | |
47460206 | 291 | if (strncmp(path, strip, len) == 0) { |
2cda7d6b | 292 | if (strip[len - 1] != '/' && path[len] == '/') |
293 | len++; | |
294 | return (xstrdup(path + len)); | |
295 | } | |
296 | ||
297 | return (xstrdup(path)); | |
298 | } | |
299 | ||
300 | static char * | |
301 | path_append(char *p1, char *p2) | |
302 | { | |
303 | char *ret; | |
ce4cf693 | 304 | size_t len = strlen(p1) + strlen(p2) + 2; |
2cda7d6b | 305 | |
306 | ret = xmalloc(len); | |
307 | strlcpy(ret, p1, len); | |
ce4cf693 | 308 | if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/') |
2cda7d6b | 309 | strlcat(ret, "/", len); |
310 | strlcat(ret, p2, len); | |
311 | ||
312 | return(ret); | |
313 | } | |
314 | ||
315 | static char * | |
316 | make_absolute(char *p, char *pwd) | |
317 | { | |
ca75d7de | 318 | char *abs_str; |
2cda7d6b | 319 | |
320 | /* Derelativise */ | |
321 | if (p && p[0] != '/') { | |
ca75d7de | 322 | abs_str = path_append(pwd, p); |
2cda7d6b | 323 | xfree(p); |
ca75d7de | 324 | return(abs_str); |
2cda7d6b | 325 | } else |
326 | return(p); | |
327 | } | |
328 | ||
329 | static int | |
330 | infer_path(const char *p, char **ifp) | |
331 | { | |
332 | char *cp; | |
333 | ||
334 | cp = strrchr(p, '/'); | |
335 | if (cp == NULL) { | |
336 | *ifp = xstrdup(p); | |
337 | return(0); | |
338 | } | |
339 | ||
340 | if (!cp[1]) { | |
341 | error("Invalid path"); | |
342 | return(-1); | |
343 | } | |
344 | ||
345 | *ifp = xstrdup(cp + 1); | |
346 | return(0); | |
347 | } | |
348 | ||
349 | static int | |
a733c71c | 350 | parse_getput_flags(const char *cmd, char **argv, int argc, int *pflag) |
2cda7d6b | 351 | { |
a733c71c | 352 | extern int optind, optreset, opterr; |
353 | int ch; | |
2cda7d6b | 354 | |
a733c71c | 355 | optind = optreset = 1; |
356 | opterr = 0; | |
357 | ||
358 | *pflag = 0; | |
359 | while ((ch = getopt(argc, argv, "Pp")) != -1) { | |
360 | switch (ch) { | |
2cda7d6b | 361 | case 'p': |
362 | case 'P': | |
363 | *pflag = 1; | |
364 | break; | |
365 | default: | |
a733c71c | 366 | error("%s: Invalid flag -%c", cmd, ch); |
367 | return -1; | |
2cda7d6b | 368 | } |
2cda7d6b | 369 | } |
370 | ||
a733c71c | 371 | return optind; |
2cda7d6b | 372 | } |
373 | ||
374 | static int | |
a733c71c | 375 | parse_ls_flags(char **argv, int argc, int *lflag) |
2cda7d6b | 376 | { |
a733c71c | 377 | extern int optind, optreset, opterr; |
378 | int ch; | |
2cda7d6b | 379 | |
a733c71c | 380 | optind = optreset = 1; |
381 | opterr = 0; | |
2cda7d6b | 382 | |
a733c71c | 383 | *lflag = LS_NAME_SORT; |
384 | while ((ch = getopt(argc, argv, "1Saflnrt")) != -1) { | |
385 | switch (ch) { | |
386 | case '1': | |
387 | *lflag &= ~VIEW_FLAGS; | |
388 | *lflag |= LS_SHORT_VIEW; | |
389 | break; | |
390 | case 'S': | |
391 | *lflag &= ~SORT_FLAGS; | |
392 | *lflag |= LS_SIZE_SORT; | |
393 | break; | |
394 | case 'a': | |
395 | *lflag |= LS_SHOW_ALL; | |
396 | break; | |
397 | case 'f': | |
398 | *lflag &= ~SORT_FLAGS; | |
399 | break; | |
400 | case 'l': | |
401 | *lflag &= ~VIEW_FLAGS; | |
402 | *lflag |= LS_LONG_VIEW; | |
403 | break; | |
404 | case 'n': | |
405 | *lflag &= ~VIEW_FLAGS; | |
406 | *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW; | |
407 | break; | |
408 | case 'r': | |
409 | *lflag |= LS_REVERSE_SORT; | |
410 | break; | |
411 | case 't': | |
412 | *lflag &= ~SORT_FLAGS; | |
413 | *lflag |= LS_TIME_SORT; | |
414 | break; | |
415 | default: | |
416 | error("ls: Invalid flag -%c", ch); | |
417 | return -1; | |
2cda7d6b | 418 | } |
2cda7d6b | 419 | } |
2cda7d6b | 420 | |
a733c71c | 421 | return optind; |
2cda7d6b | 422 | } |
423 | ||
424 | static int | |
425 | is_dir(char *path) | |
426 | { | |
427 | struct stat sb; | |
428 | ||
429 | /* XXX: report errors? */ | |
430 | if (stat(path, &sb) == -1) | |
431 | return(0); | |
432 | ||
d3fc2aa3 | 433 | return(S_ISDIR(sb.st_mode)); |
2cda7d6b | 434 | } |
435 | ||
436 | static int | |
437 | is_reg(char *path) | |
438 | { | |
439 | struct stat sb; | |
440 | ||
441 | if (stat(path, &sb) == -1) | |
442 | fatal("stat %s: %s", path, strerror(errno)); | |
443 | ||
444 | return(S_ISREG(sb.st_mode)); | |
445 | } | |
446 | ||
447 | static int | |
448 | remote_is_dir(struct sftp_conn *conn, char *path) | |
449 | { | |
450 | Attrib *a; | |
451 | ||
452 | /* XXX: report errors? */ | |
453 | if ((a = do_stat(conn, path, 1)) == NULL) | |
454 | return(0); | |
455 | if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) | |
456 | return(0); | |
d3fc2aa3 | 457 | return(S_ISDIR(a->perm)); |
2cda7d6b | 458 | } |
459 | ||
460 | static int | |
461 | process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) | |
462 | { | |
463 | char *abs_src = NULL; | |
464 | char *abs_dst = NULL; | |
465 | char *tmp; | |
466 | glob_t g; | |
467 | int err = 0; | |
468 | int i; | |
469 | ||
470 | abs_src = xstrdup(src); | |
471 | abs_src = make_absolute(abs_src, pwd); | |
472 | ||
473 | memset(&g, 0, sizeof(g)); | |
474 | debug3("Looking up %s", abs_src); | |
475 | if (remote_glob(conn, abs_src, 0, NULL, &g)) { | |
476 | error("File \"%s\" not found.", abs_src); | |
477 | err = -1; | |
478 | goto out; | |
479 | } | |
480 | ||
481 | /* If multiple matches, dst must be a directory or unspecified */ | |
482 | if (g.gl_matchc > 1 && dst && !is_dir(dst)) { | |
483 | error("Multiple files match, but \"%s\" is not a directory", | |
484 | dst); | |
485 | err = -1; | |
486 | goto out; | |
487 | } | |
488 | ||
0e5de6f8 | 489 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 490 | if (infer_path(g.gl_pathv[i], &tmp)) { |
491 | err = -1; | |
492 | goto out; | |
493 | } | |
494 | ||
495 | if (g.gl_matchc == 1 && dst) { | |
496 | /* If directory specified, append filename */ | |
2b8dc5e3 | 497 | xfree(tmp); |
2cda7d6b | 498 | if (is_dir(dst)) { |
499 | if (infer_path(g.gl_pathv[0], &tmp)) { | |
500 | err = 1; | |
501 | goto out; | |
502 | } | |
503 | abs_dst = path_append(dst, tmp); | |
504 | xfree(tmp); | |
505 | } else | |
506 | abs_dst = xstrdup(dst); | |
507 | } else if (dst) { | |
508 | abs_dst = path_append(dst, tmp); | |
509 | xfree(tmp); | |
510 | } else | |
511 | abs_dst = tmp; | |
512 | ||
513 | printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst); | |
514 | if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1) | |
515 | err = -1; | |
516 | xfree(abs_dst); | |
517 | abs_dst = NULL; | |
518 | } | |
519 | ||
520 | out: | |
521 | xfree(abs_src); | |
2cda7d6b | 522 | globfree(&g); |
523 | return(err); | |
524 | } | |
525 | ||
526 | static int | |
527 | process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) | |
528 | { | |
529 | char *tmp_dst = NULL; | |
530 | char *abs_dst = NULL; | |
531 | char *tmp; | |
532 | glob_t g; | |
533 | int err = 0; | |
534 | int i; | |
535 | ||
536 | if (dst) { | |
537 | tmp_dst = xstrdup(dst); | |
538 | tmp_dst = make_absolute(tmp_dst, pwd); | |
539 | } | |
540 | ||
541 | memset(&g, 0, sizeof(g)); | |
542 | debug3("Looking up %s", src); | |
543 | if (glob(src, 0, NULL, &g)) { | |
544 | error("File \"%s\" not found.", src); | |
545 | err = -1; | |
546 | goto out; | |
547 | } | |
548 | ||
549 | /* If multiple matches, dst may be directory or unspecified */ | |
550 | if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) { | |
551 | error("Multiple files match, but \"%s\" is not a directory", | |
552 | tmp_dst); | |
553 | err = -1; | |
554 | goto out; | |
555 | } | |
556 | ||
0e5de6f8 | 557 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 558 | if (!is_reg(g.gl_pathv[i])) { |
559 | error("skipping non-regular file %s", | |
560 | g.gl_pathv[i]); | |
561 | continue; | |
562 | } | |
563 | if (infer_path(g.gl_pathv[i], &tmp)) { | |
564 | err = -1; | |
565 | goto out; | |
566 | } | |
567 | ||
568 | if (g.gl_matchc == 1 && tmp_dst) { | |
569 | /* If directory specified, append filename */ | |
570 | if (remote_is_dir(conn, tmp_dst)) { | |
571 | if (infer_path(g.gl_pathv[0], &tmp)) { | |
572 | err = 1; | |
573 | goto out; | |
574 | } | |
575 | abs_dst = path_append(tmp_dst, tmp); | |
576 | xfree(tmp); | |
577 | } else | |
578 | abs_dst = xstrdup(tmp_dst); | |
579 | ||
580 | } else if (tmp_dst) { | |
581 | abs_dst = path_append(tmp_dst, tmp); | |
582 | xfree(tmp); | |
583 | } else | |
584 | abs_dst = make_absolute(tmp, pwd); | |
585 | ||
586 | printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst); | |
587 | if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1) | |
588 | err = -1; | |
589 | } | |
590 | ||
591 | out: | |
592 | if (abs_dst) | |
593 | xfree(abs_dst); | |
594 | if (tmp_dst) | |
595 | xfree(tmp_dst); | |
596 | globfree(&g); | |
597 | return(err); | |
598 | } | |
599 | ||
600 | static int | |
601 | sdirent_comp(const void *aa, const void *bb) | |
602 | { | |
603 | SFTP_DIRENT *a = *(SFTP_DIRENT **)aa; | |
604 | SFTP_DIRENT *b = *(SFTP_DIRENT **)bb; | |
ae7daec3 | 605 | int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1; |
2cda7d6b | 606 | |
95cbd340 | 607 | #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1)) |
ae7daec3 | 608 | if (sort_flag & LS_NAME_SORT) |
95cbd340 | 609 | return (rmul * strcmp(a->filename, b->filename)); |
ae7daec3 | 610 | else if (sort_flag & LS_TIME_SORT) |
95cbd340 | 611 | return (rmul * NCMP(a->a.mtime, b->a.mtime)); |
ae7daec3 | 612 | else if (sort_flag & LS_SIZE_SORT) |
95cbd340 | 613 | return (rmul * NCMP(a->a.size, b->a.size)); |
614 | ||
615 | fatal("Unknown ls sort type"); | |
2cda7d6b | 616 | } |
617 | ||
618 | /* sftp ls.1 replacement for directories */ | |
619 | static int | |
620 | do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) | |
621 | { | |
2ceb8101 | 622 | int n; |
623 | u_int c = 1, colspace = 0, columns = 1; | |
2cda7d6b | 624 | SFTP_DIRENT **d; |
625 | ||
626 | if ((n = do_readdir(conn, path, &d)) != 0) | |
627 | return (n); | |
628 | ||
ae7daec3 | 629 | if (!(lflag & LS_SHORT_VIEW)) { |
2ceb8101 | 630 | u_int m = 0, width = 80; |
2cda7d6b | 631 | struct winsize ws; |
632 | char *tmp; | |
633 | ||
634 | /* Count entries for sort and find longest filename */ | |
cc4ff6c4 | 635 | for (n = 0; d[n] != NULL; n++) { |
636 | if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL)) | |
637 | m = MAX(m, strlen(d[n]->filename)); | |
638 | } | |
2cda7d6b | 639 | |
640 | /* Add any subpath that also needs to be counted */ | |
641 | tmp = path_strip(path, strip_path); | |
642 | m += strlen(tmp); | |
643 | xfree(tmp); | |
644 | ||
645 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) | |
646 | width = ws.ws_col; | |
647 | ||
648 | columns = width / (m + 2); | |
649 | columns = MAX(columns, 1); | |
650 | colspace = width / columns; | |
651 | colspace = MIN(colspace, width); | |
652 | } | |
653 | ||
95cbd340 | 654 | if (lflag & SORT_FLAGS) { |
bdd3b323 | 655 | for (n = 0; d[n] != NULL; n++) |
656 | ; /* count entries */ | |
ae7daec3 | 657 | sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT); |
95cbd340 | 658 | qsort(d, n, sizeof(*d), sdirent_comp); |
659 | } | |
2cda7d6b | 660 | |
0e5de6f8 | 661 | for (n = 0; d[n] != NULL && !interrupted; n++) { |
2cda7d6b | 662 | char *tmp, *fname; |
663 | ||
cc4ff6c4 | 664 | if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL)) |
665 | continue; | |
666 | ||
2cda7d6b | 667 | tmp = path_append(path, d[n]->filename); |
668 | fname = path_strip(tmp, strip_path); | |
669 | xfree(tmp); | |
670 | ||
ae7daec3 | 671 | if (lflag & LS_LONG_VIEW) { |
672 | if (lflag & LS_NUMERIC_VIEW) { | |
48925711 | 673 | char *lname; |
674 | struct stat sb; | |
675 | ||
676 | memset(&sb, 0, sizeof(sb)); | |
677 | attrib_to_stat(&d[n]->a, &sb); | |
678 | lname = ls_file(fname, &sb, 1); | |
679 | printf("%s\n", lname); | |
680 | xfree(lname); | |
681 | } else | |
682 | printf("%s\n", d[n]->longname); | |
2cda7d6b | 683 | } else { |
684 | printf("%-*s", colspace, fname); | |
685 | if (c >= columns) { | |
686 | printf("\n"); | |
687 | c = 1; | |
688 | } else | |
689 | c++; | |
690 | } | |
691 | ||
692 | xfree(fname); | |
693 | } | |
694 | ||
ae7daec3 | 695 | if (!(lflag & LS_LONG_VIEW) && (c != 1)) |
2cda7d6b | 696 | printf("\n"); |
697 | ||
698 | free_sftp_dirents(d); | |
699 | return (0); | |
700 | } | |
701 | ||
702 | /* sftp ls.1 replacement which handles path globs */ | |
703 | static int | |
704 | do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, | |
705 | int lflag) | |
706 | { | |
707 | glob_t g; | |
2ceb8101 | 708 | u_int i, c = 1, colspace = 0, columns = 1; |
2bd204e5 | 709 | Attrib *a = NULL; |
2cda7d6b | 710 | |
711 | memset(&g, 0, sizeof(g)); | |
712 | ||
713 | if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE, | |
2bd204e5 | 714 | NULL, &g) || (g.gl_pathc && !g.gl_matchc)) { |
715 | if (g.gl_pathc) | |
716 | globfree(&g); | |
2cda7d6b | 717 | error("Can't ls: \"%s\" not found", path); |
718 | return (-1); | |
719 | } | |
720 | ||
0e5de6f8 | 721 | if (interrupted) |
722 | goto out; | |
723 | ||
2cda7d6b | 724 | /* |
2bd204e5 | 725 | * If the glob returns a single match and it is a directory, |
726 | * then just list its contents. | |
2cda7d6b | 727 | */ |
2bd204e5 | 728 | if (g.gl_matchc == 1) { |
729 | if ((a = do_lstat(conn, g.gl_pathv[0], 1)) == NULL) { | |
2cda7d6b | 730 | globfree(&g); |
731 | return (-1); | |
732 | } | |
733 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && | |
734 | S_ISDIR(a->perm)) { | |
2bd204e5 | 735 | int err; |
736 | ||
737 | err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag); | |
2cda7d6b | 738 | globfree(&g); |
2bd204e5 | 739 | return (err); |
2cda7d6b | 740 | } |
741 | } | |
742 | ||
ae7daec3 | 743 | if (!(lflag & LS_SHORT_VIEW)) { |
2ceb8101 | 744 | u_int m = 0, width = 80; |
2cda7d6b | 745 | struct winsize ws; |
746 | ||
747 | /* Count entries for sort and find longest filename */ | |
748 | for (i = 0; g.gl_pathv[i]; i++) | |
749 | m = MAX(m, strlen(g.gl_pathv[i])); | |
750 | ||
751 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) | |
752 | width = ws.ws_col; | |
753 | ||
754 | columns = width / (m + 2); | |
755 | columns = MAX(columns, 1); | |
756 | colspace = width / columns; | |
757 | } | |
758 | ||
2bd204e5 | 759 | for (i = 0; g.gl_pathv[i] && !interrupted; i++, a = NULL) { |
2cda7d6b | 760 | char *fname; |
761 | ||
762 | fname = path_strip(g.gl_pathv[i], strip_path); | |
763 | ||
ae7daec3 | 764 | if (lflag & LS_LONG_VIEW) { |
2cda7d6b | 765 | char *lname; |
766 | struct stat sb; | |
767 | ||
768 | /* | |
769 | * XXX: this is slow - 1 roundtrip per path | |
770 | * A solution to this is to fork glob() and | |
771 | * build a sftp specific version which keeps the | |
772 | * attribs (which currently get thrown away) | |
773 | * that the server returns as well as the filenames. | |
774 | */ | |
775 | memset(&sb, 0, sizeof(sb)); | |
2bd204e5 | 776 | if (a == NULL) |
777 | a = do_lstat(conn, g.gl_pathv[i], 1); | |
2cda7d6b | 778 | if (a != NULL) |
779 | attrib_to_stat(a, &sb); | |
780 | lname = ls_file(fname, &sb, 1); | |
781 | printf("%s\n", lname); | |
782 | xfree(lname); | |
783 | } else { | |
784 | printf("%-*s", colspace, fname); | |
785 | if (c >= columns) { | |
786 | printf("\n"); | |
787 | c = 1; | |
788 | } else | |
789 | c++; | |
790 | } | |
791 | xfree(fname); | |
792 | } | |
793 | ||
ae7daec3 | 794 | if (!(lflag & LS_LONG_VIEW) && (c != 1)) |
2cda7d6b | 795 | printf("\n"); |
796 | ||
0e5de6f8 | 797 | out: |
2cda7d6b | 798 | if (g.gl_pathc) |
799 | globfree(&g); | |
800 | ||
801 | return (0); | |
802 | } | |
803 | ||
a733c71c | 804 | /* |
805 | * Undo escaping of glob sequences in place. Used to undo extra escaping | |
806 | * applied in makeargv() when the string is destined for a function that | |
807 | * does not glob it. | |
808 | */ | |
809 | static void | |
810 | undo_glob_escape(char *s) | |
811 | { | |
812 | size_t i, j; | |
813 | ||
814 | for (i = j = 0;;) { | |
815 | if (s[i] == '\0') { | |
816 | s[j] = '\0'; | |
817 | return; | |
818 | } | |
819 | if (s[i] != '\\') { | |
820 | s[j++] = s[i++]; | |
821 | continue; | |
822 | } | |
823 | /* s[i] == '\\' */ | |
824 | ++i; | |
825 | switch (s[i]) { | |
826 | case '?': | |
827 | case '[': | |
828 | case '*': | |
829 | case '\\': | |
830 | s[j++] = s[i++]; | |
831 | break; | |
832 | case '\0': | |
833 | s[j++] = '\\'; | |
834 | s[j] = '\0'; | |
835 | return; | |
836 | default: | |
837 | s[j++] = '\\'; | |
838 | s[j++] = s[i++]; | |
839 | break; | |
840 | } | |
841 | } | |
842 | } | |
843 | ||
844 | /* | |
845 | * Split a string into an argument vector using sh(1)-style quoting, | |
846 | * comment and escaping rules, but with some tweaks to handle glob(3) | |
847 | * wildcards. | |
848 | * Returns NULL on error or a NULL-terminated array of arguments. | |
849 | */ | |
850 | #define MAXARGS 128 | |
851 | #define MAXARGLEN 8192 | |
852 | static char ** | |
853 | makeargv(const char *arg, int *argcp) | |
854 | { | |
855 | int argc, quot; | |
856 | size_t i, j; | |
857 | static char argvs[MAXARGLEN]; | |
858 | static char *argv[MAXARGS + 1]; | |
859 | enum { MA_START, MA_SQUOTE, MA_DQUOTE, MA_UNQUOTED } state, q; | |
860 | ||
861 | *argcp = argc = 0; | |
862 | if (strlen(arg) > sizeof(argvs) - 1) { | |
863 | args_too_longs: | |
864 | error("string too long"); | |
865 | return NULL; | |
866 | } | |
867 | state = MA_START; | |
868 | i = j = 0; | |
869 | for (;;) { | |
870 | if (isspace(arg[i])) { | |
871 | if (state == MA_UNQUOTED) { | |
872 | /* Terminate current argument */ | |
873 | argvs[j++] = '\0'; | |
874 | argc++; | |
875 | state = MA_START; | |
876 | } else if (state != MA_START) | |
877 | argvs[j++] = arg[i]; | |
878 | } else if (arg[i] == '"' || arg[i] == '\'') { | |
879 | q = arg[i] == '"' ? MA_DQUOTE : MA_SQUOTE; | |
880 | if (state == MA_START) { | |
881 | argv[argc] = argvs + j; | |
882 | state = q; | |
883 | } else if (state == MA_UNQUOTED) | |
884 | state = q; | |
885 | else if (state == q) | |
886 | state = MA_UNQUOTED; | |
887 | else | |
888 | argvs[j++] = arg[i]; | |
889 | } else if (arg[i] == '\\') { | |
890 | if (state == MA_SQUOTE || state == MA_DQUOTE) { | |
891 | quot = state == MA_SQUOTE ? '\'' : '"'; | |
892 | /* Unescape quote we are in */ | |
893 | /* XXX support \n and friends? */ | |
894 | if (arg[i + 1] == quot) { | |
895 | i++; | |
896 | argvs[j++] = arg[i]; | |
897 | } else if (arg[i + 1] == '?' || | |
898 | arg[i + 1] == '[' || arg[i + 1] == '*') { | |
899 | /* | |
900 | * Special case for sftp: append | |
901 | * double-escaped glob sequence - | |
902 | * glob will undo one level of | |
903 | * escaping. NB. string can grow here. | |
904 | */ | |
905 | if (j >= sizeof(argvs) - 5) | |
906 | goto args_too_longs; | |
907 | argvs[j++] = '\\'; | |
908 | argvs[j++] = arg[i++]; | |
909 | argvs[j++] = '\\'; | |
910 | argvs[j++] = arg[i]; | |
911 | } else { | |
912 | argvs[j++] = arg[i++]; | |
913 | argvs[j++] = arg[i]; | |
914 | } | |
915 | } else { | |
916 | if (state == MA_START) { | |
917 | argv[argc] = argvs + j; | |
918 | state = MA_UNQUOTED; | |
919 | } | |
920 | if (arg[i + 1] == '?' || arg[i + 1] == '[' || | |
921 | arg[i + 1] == '*' || arg[i + 1] == '\\') { | |
922 | /* | |
923 | * Special case for sftp: append | |
924 | * escaped glob sequence - | |
925 | * glob will undo one level of | |
926 | * escaping. | |
927 | */ | |
928 | argvs[j++] = arg[i++]; | |
929 | argvs[j++] = arg[i]; | |
930 | } else { | |
931 | /* Unescape everything */ | |
932 | /* XXX support \n and friends? */ | |
933 | i++; | |
934 | argvs[j++] = arg[i]; | |
935 | } | |
936 | } | |
937 | } else if (arg[i] == '#') { | |
938 | if (state == MA_SQUOTE || state == MA_DQUOTE) | |
939 | argvs[j++] = arg[i]; | |
940 | else | |
941 | goto string_done; | |
942 | } else if (arg[i] == '\0') { | |
943 | if (state == MA_SQUOTE || state == MA_DQUOTE) { | |
944 | error("Unterminated quoted argument"); | |
945 | return NULL; | |
946 | } | |
947 | string_done: | |
948 | if (state == MA_UNQUOTED) { | |
949 | argvs[j++] = '\0'; | |
950 | argc++; | |
951 | } | |
952 | break; | |
953 | } else { | |
954 | if (state == MA_START) { | |
955 | argv[argc] = argvs + j; | |
956 | state = MA_UNQUOTED; | |
957 | } | |
958 | if ((state == MA_SQUOTE || state == MA_DQUOTE) && | |
959 | (arg[i] == '?' || arg[i] == '[' || arg[i] == '*')) { | |
960 | /* | |
961 | * Special case for sftp: escape quoted | |
962 | * glob(3) wildcards. NB. string can grow | |
963 | * here. | |
964 | */ | |
965 | if (j >= sizeof(argvs) - 3) | |
966 | goto args_too_longs; | |
967 | argvs[j++] = '\\'; | |
968 | argvs[j++] = arg[i]; | |
969 | } else | |
970 | argvs[j++] = arg[i]; | |
971 | } | |
972 | i++; | |
973 | } | |
974 | *argcp = argc; | |
975 | return argv; | |
976 | } | |
977 | ||
2cda7d6b | 978 | static int |
979 | parse_args(const char **cpp, int *pflag, int *lflag, int *iflag, | |
980 | unsigned long *n_arg, char **path1, char **path2) | |
981 | { | |
982 | const char *cmd, *cp = *cpp; | |
a733c71c | 983 | char *cp2, **argv; |
2cda7d6b | 984 | int base = 0; |
985 | long l; | |
a733c71c | 986 | int i, cmdnum, optidx, argc; |
2cda7d6b | 987 | |
988 | /* Skip leading whitespace */ | |
989 | cp = cp + strspn(cp, WHITESPACE); | |
990 | ||
991 | /* Ignore blank lines and lines which begin with comment '#' char */ | |
992 | if (*cp == '\0' || *cp == '#') | |
993 | return (0); | |
994 | ||
995 | /* Check for leading '-' (disable error processing) */ | |
996 | *iflag = 0; | |
997 | if (*cp == '-') { | |
998 | *iflag = 1; | |
999 | cp++; | |
1000 | } | |
1001 | ||
a733c71c | 1002 | if ((argv = makeargv(cp, &argc)) == NULL) |
1003 | return -1; | |
1004 | ||
2cda7d6b | 1005 | /* Figure out which command we have */ |
a733c71c | 1006 | for (i = 0; cmds[i].c != NULL; i++) { |
1007 | if (strcasecmp(cmds[i].c, argv[0]) == 0) | |
2cda7d6b | 1008 | break; |
2cda7d6b | 1009 | } |
1010 | cmdnum = cmds[i].n; | |
1011 | cmd = cmds[i].c; | |
1012 | ||
1013 | /* Special case */ | |
1014 | if (*cp == '!') { | |
1015 | cp++; | |
1016 | cmdnum = I_SHELL; | |
1017 | } else if (cmdnum == -1) { | |
1018 | error("Invalid command."); | |
a733c71c | 1019 | return -1; |
2cda7d6b | 1020 | } |
1021 | ||
1022 | /* Get arguments and parse flags */ | |
1023 | *lflag = *pflag = *n_arg = 0; | |
1024 | *path1 = *path2 = NULL; | |
a733c71c | 1025 | optidx = 1; |
2cda7d6b | 1026 | switch (cmdnum) { |
1027 | case I_GET: | |
1028 | case I_PUT: | |
a733c71c | 1029 | if ((optidx = parse_getput_flags(cmd, argv, argc, pflag)) == -1) |
1030 | return -1; | |
2cda7d6b | 1031 | /* Get first pathname (mandatory) */ |
a733c71c | 1032 | if (argc - optidx < 1) { |
2cda7d6b | 1033 | error("You must specify at least one path after a " |
1034 | "%s command.", cmd); | |
a733c71c | 1035 | return -1; |
1036 | } | |
1037 | *path1 = xstrdup(argv[optidx]); | |
1038 | /* Get second pathname (optional) */ | |
1039 | if (argc - optidx > 1) { | |
1040 | *path2 = xstrdup(argv[optidx + 1]); | |
1041 | /* Destination is not globbed */ | |
1042 | undo_glob_escape(*path2); | |
2cda7d6b | 1043 | } |
2cda7d6b | 1044 | break; |
1045 | case I_RENAME: | |
1046 | case I_SYMLINK: | |
a733c71c | 1047 | if (argc - optidx < 2) { |
2cda7d6b | 1048 | error("You must specify two paths after a %s " |
1049 | "command.", cmd); | |
a733c71c | 1050 | return -1; |
2cda7d6b | 1051 | } |
a733c71c | 1052 | *path1 = xstrdup(argv[optidx]); |
1053 | *path2 = xstrdup(argv[optidx + 1]); | |
1054 | /* Paths are not globbed */ | |
1055 | undo_glob_escape(*path1); | |
1056 | undo_glob_escape(*path2); | |
2cda7d6b | 1057 | break; |
1058 | case I_RM: | |
1059 | case I_MKDIR: | |
1060 | case I_RMDIR: | |
1061 | case I_CHDIR: | |
1062 | case I_LCHDIR: | |
1063 | case I_LMKDIR: | |
1064 | /* Get pathname (mandatory) */ | |
a733c71c | 1065 | if (argc - optidx < 1) { |
2cda7d6b | 1066 | error("You must specify a path after a %s command.", |
1067 | cmd); | |
a733c71c | 1068 | return -1; |
2cda7d6b | 1069 | } |
a733c71c | 1070 | *path1 = xstrdup(argv[optidx]); |
1071 | /* Only "rm" globs */ | |
1072 | if (cmdnum != I_RM) | |
1073 | undo_glob_escape(*path1); | |
2cda7d6b | 1074 | break; |
1075 | case I_LS: | |
a733c71c | 1076 | if ((optidx = parse_ls_flags(argv, argc, lflag)) == -1) |
2cda7d6b | 1077 | return(-1); |
1078 | /* Path is optional */ | |
a733c71c | 1079 | if (argc - optidx > 0) |
1080 | *path1 = xstrdup(argv[optidx]); | |
2cda7d6b | 1081 | break; |
1082 | case I_LLS: | |
1083 | case I_SHELL: | |
1084 | /* Uses the rest of the line */ | |
1085 | break; | |
1086 | case I_LUMASK: | |
2cda7d6b | 1087 | case I_CHMOD: |
1088 | base = 8; | |
1089 | case I_CHOWN: | |
1090 | case I_CHGRP: | |
1091 | /* Get numeric arg (mandatory) */ | |
a733c71c | 1092 | if (argc - optidx < 1) |
1093 | goto need_num_arg; | |
b3c338b7 | 1094 | errno = 0; |
a733c71c | 1095 | l = strtol(argv[optidx], &cp2, base); |
1096 | if (cp2 == argv[optidx] || *cp2 != '\0' || | |
1097 | ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) || | |
1098 | l < 0) { | |
1099 | need_num_arg: | |
2cda7d6b | 1100 | error("You must supply a numeric argument " |
1101 | "to the %s command.", cmd); | |
a733c71c | 1102 | return -1; |
2cda7d6b | 1103 | } |
2cda7d6b | 1104 | *n_arg = l; |
a733c71c | 1105 | if (cmdnum == I_LUMASK) |
2cda7d6b | 1106 | break; |
2cda7d6b | 1107 | /* Get pathname (mandatory) */ |
a733c71c | 1108 | if (argc - optidx < 2) { |
2cda7d6b | 1109 | error("You must specify a path after a %s command.", |
1110 | cmd); | |
a733c71c | 1111 | return -1; |
2cda7d6b | 1112 | } |
a733c71c | 1113 | *path1 = xstrdup(argv[optidx + 1]); |
2cda7d6b | 1114 | break; |
1115 | case I_QUIT: | |
1116 | case I_PWD: | |
1117 | case I_LPWD: | |
1118 | case I_HELP: | |
1119 | case I_VERSION: | |
1120 | case I_PROGRESS: | |
1121 | break; | |
1122 | default: | |
1123 | fatal("Command not implemented"); | |
1124 | } | |
1125 | ||
1126 | *cpp = cp; | |
1127 | return(cmdnum); | |
1128 | } | |
1129 | ||
1130 | static int | |
1131 | parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, | |
1132 | int err_abort) | |
1133 | { | |
1134 | char *path1, *path2, *tmp; | |
1135 | int pflag, lflag, iflag, cmdnum, i; | |
1136 | unsigned long n_arg; | |
1137 | Attrib a, *aa; | |
1138 | char path_buf[MAXPATHLEN]; | |
1139 | int err = 0; | |
1140 | glob_t g; | |
1141 | ||
1142 | path1 = path2 = NULL; | |
1143 | cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg, | |
1144 | &path1, &path2); | |
1145 | ||
1146 | if (iflag != 0) | |
1147 | err_abort = 0; | |
1148 | ||
1149 | memset(&g, 0, sizeof(g)); | |
1150 | ||
1151 | /* Perform command */ | |
1152 | switch (cmdnum) { | |
1153 | case 0: | |
1154 | /* Blank line */ | |
1155 | break; | |
1156 | case -1: | |
1157 | /* Unrecognized command */ | |
1158 | err = -1; | |
1159 | break; | |
1160 | case I_GET: | |
1161 | err = process_get(conn, path1, path2, *pwd, pflag); | |
1162 | break; | |
1163 | case I_PUT: | |
1164 | err = process_put(conn, path1, path2, *pwd, pflag); | |
1165 | break; | |
1166 | case I_RENAME: | |
1167 | path1 = make_absolute(path1, *pwd); | |
1168 | path2 = make_absolute(path2, *pwd); | |
1169 | err = do_rename(conn, path1, path2); | |
1170 | break; | |
1171 | case I_SYMLINK: | |
1172 | path2 = make_absolute(path2, *pwd); | |
1173 | err = do_symlink(conn, path1, path2); | |
1174 | break; | |
1175 | case I_RM: | |
1176 | path1 = make_absolute(path1, *pwd); | |
1177 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); | |
0e5de6f8 | 1178 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 1179 | printf("Removing %s\n", g.gl_pathv[i]); |
1180 | err = do_rm(conn, g.gl_pathv[i]); | |
1181 | if (err != 0 && err_abort) | |
1182 | break; | |
1183 | } | |
1184 | break; | |
1185 | case I_MKDIR: | |
1186 | path1 = make_absolute(path1, *pwd); | |
1187 | attrib_clear(&a); | |
1188 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; | |
1189 | a.perm = 0777; | |
1190 | err = do_mkdir(conn, path1, &a); | |
1191 | break; | |
1192 | case I_RMDIR: | |
1193 | path1 = make_absolute(path1, *pwd); | |
1194 | err = do_rmdir(conn, path1); | |
1195 | break; | |
1196 | case I_CHDIR: | |
1197 | path1 = make_absolute(path1, *pwd); | |
1198 | if ((tmp = do_realpath(conn, path1)) == NULL) { | |
1199 | err = 1; | |
1200 | break; | |
1201 | } | |
1202 | if ((aa = do_stat(conn, tmp, 0)) == NULL) { | |
1203 | xfree(tmp); | |
1204 | err = 1; | |
1205 | break; | |
1206 | } | |
1207 | if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { | |
1208 | error("Can't change directory: Can't check target"); | |
1209 | xfree(tmp); | |
1210 | err = 1; | |
1211 | break; | |
1212 | } | |
1213 | if (!S_ISDIR(aa->perm)) { | |
1214 | error("Can't change directory: \"%s\" is not " | |
1215 | "a directory", tmp); | |
1216 | xfree(tmp); | |
1217 | err = 1; | |
1218 | break; | |
1219 | } | |
1220 | xfree(*pwd); | |
1221 | *pwd = tmp; | |
1222 | break; | |
1223 | case I_LS: | |
1224 | if (!path1) { | |
1225 | do_globbed_ls(conn, *pwd, *pwd, lflag); | |
1226 | break; | |
1227 | } | |
1228 | ||
1229 | /* Strip pwd off beginning of non-absolute paths */ | |
1230 | tmp = NULL; | |
1231 | if (*path1 != '/') | |
1232 | tmp = *pwd; | |
1233 | ||
1234 | path1 = make_absolute(path1, *pwd); | |
1235 | err = do_globbed_ls(conn, path1, tmp, lflag); | |
1236 | break; | |
1237 | case I_LCHDIR: | |
1238 | if (chdir(path1) == -1) { | |
1239 | error("Couldn't change local directory to " | |
1240 | "\"%s\": %s", path1, strerror(errno)); | |
1241 | err = 1; | |
1242 | } | |
1243 | break; | |
1244 | case I_LMKDIR: | |
1245 | if (mkdir(path1, 0777) == -1) { | |
1246 | error("Couldn't create local directory " | |
1247 | "\"%s\": %s", path1, strerror(errno)); | |
1248 | err = 1; | |
1249 | } | |
1250 | break; | |
1251 | case I_LLS: | |
1252 | local_do_ls(cmd); | |
1253 | break; | |
1254 | case I_SHELL: | |
1255 | local_do_shell(cmd); | |
1256 | break; | |
1257 | case I_LUMASK: | |
1258 | umask(n_arg); | |
1259 | printf("Local umask: %03lo\n", n_arg); | |
1260 | break; | |
1261 | case I_CHMOD: | |
1262 | path1 = make_absolute(path1, *pwd); | |
1263 | attrib_clear(&a); | |
1264 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; | |
1265 | a.perm = n_arg; | |
1266 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); | |
0e5de6f8 | 1267 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 1268 | printf("Changing mode on %s\n", g.gl_pathv[i]); |
1269 | err = do_setstat(conn, g.gl_pathv[i], &a); | |
1270 | if (err != 0 && err_abort) | |
1271 | break; | |
1272 | } | |
1273 | break; | |
1274 | case I_CHOWN: | |
1275 | case I_CHGRP: | |
1276 | path1 = make_absolute(path1, *pwd); | |
1277 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); | |
0e5de6f8 | 1278 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 1279 | if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) { |
1280 | if (err != 0 && err_abort) | |
1281 | break; | |
1282 | else | |
1283 | continue; | |
1284 | } | |
1285 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { | |
1286 | error("Can't get current ownership of " | |
1287 | "remote file \"%s\"", g.gl_pathv[i]); | |
1288 | if (err != 0 && err_abort) | |
1289 | break; | |
1290 | else | |
1291 | continue; | |
1292 | } | |
1293 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; | |
1294 | if (cmdnum == I_CHOWN) { | |
1295 | printf("Changing owner on %s\n", g.gl_pathv[i]); | |
1296 | aa->uid = n_arg; | |
1297 | } else { | |
1298 | printf("Changing group on %s\n", g.gl_pathv[i]); | |
1299 | aa->gid = n_arg; | |
1300 | } | |
1301 | err = do_setstat(conn, g.gl_pathv[i], aa); | |
1302 | if (err != 0 && err_abort) | |
1303 | break; | |
1304 | } | |
1305 | break; | |
1306 | case I_PWD: | |
1307 | printf("Remote working directory: %s\n", *pwd); | |
1308 | break; | |
1309 | case I_LPWD: | |
1310 | if (!getcwd(path_buf, sizeof(path_buf))) { | |
1311 | error("Couldn't get local cwd: %s", strerror(errno)); | |
1312 | err = -1; | |
1313 | break; | |
1314 | } | |
1315 | printf("Local working directory: %s\n", path_buf); | |
1316 | break; | |
1317 | case I_QUIT: | |
1318 | /* Processed below */ | |
1319 | break; | |
1320 | case I_HELP: | |
1321 | help(); | |
1322 | break; | |
1323 | case I_VERSION: | |
1324 | printf("SFTP protocol version %u\n", sftp_proto_version(conn)); | |
1325 | break; | |
1326 | case I_PROGRESS: | |
1327 | showprogress = !showprogress; | |
1328 | if (showprogress) | |
1329 | printf("Progress meter enabled\n"); | |
1330 | else | |
1331 | printf("Progress meter disabled\n"); | |
1332 | break; | |
1333 | default: | |
1334 | fatal("%d is not implemented", cmdnum); | |
1335 | } | |
1336 | ||
1337 | if (g.gl_pathc) | |
1338 | globfree(&g); | |
1339 | if (path1) | |
1340 | xfree(path1); | |
1341 | if (path2) | |
1342 | xfree(path2); | |
1343 | ||
1344 | /* If an unignored error occurs in batch mode we should abort. */ | |
1345 | if (err_abort && err != 0) | |
1346 | return (-1); | |
1347 | else if (cmdnum == I_QUIT) | |
1348 | return (1); | |
1349 | ||
1350 | return (0); | |
1351 | } | |
1352 | ||
5132eac0 | 1353 | #ifdef USE_LIBEDIT |
1354 | static char * | |
1355 | prompt(EditLine *el) | |
1356 | { | |
1357 | return ("sftp> "); | |
1358 | } | |
1359 | #endif | |
1360 | ||
2cda7d6b | 1361 | int |
1362 | interactive_loop(int fd_in, int fd_out, char *file1, char *file2) | |
1363 | { | |
1364 | char *pwd; | |
1365 | char *dir = NULL; | |
1366 | char cmd[2048]; | |
1367 | struct sftp_conn *conn; | |
0aa1cc4b | 1368 | int err, interactive; |
5132eac0 | 1369 | EditLine *el = NULL; |
1370 | #ifdef USE_LIBEDIT | |
1371 | History *hl = NULL; | |
1372 | HistEvent hev; | |
1373 | extern char *__progname; | |
1374 | ||
1375 | if (!batchmode && isatty(STDIN_FILENO)) { | |
1376 | if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL) | |
1377 | fatal("Couldn't initialise editline"); | |
1378 | if ((hl = history_init()) == NULL) | |
1379 | fatal("Couldn't initialise editline history"); | |
1380 | history(hl, &hev, H_SETSIZE, 100); | |
1381 | el_set(el, EL_HIST, history, hl); | |
1382 | ||
1383 | el_set(el, EL_PROMPT, prompt); | |
1384 | el_set(el, EL_EDITOR, "emacs"); | |
1385 | el_set(el, EL_TERMINAL, NULL); | |
1386 | el_set(el, EL_SIGNAL, 1); | |
1387 | el_source(el, NULL); | |
1388 | } | |
1389 | #endif /* USE_LIBEDIT */ | |
2cda7d6b | 1390 | |
1391 | conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests); | |
1392 | if (conn == NULL) | |
1393 | fatal("Couldn't initialise connection to server"); | |
1394 | ||
1395 | pwd = do_realpath(conn, "."); | |
1396 | if (pwd == NULL) | |
1397 | fatal("Need cwd"); | |
1398 | ||
1399 | if (file1 != NULL) { | |
1400 | dir = xstrdup(file1); | |
1401 | dir = make_absolute(dir, pwd); | |
1402 | ||
1403 | if (remote_is_dir(conn, dir) && file2 == NULL) { | |
1404 | printf("Changing to: %s\n", dir); | |
1405 | snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); | |
aa41be57 | 1406 | if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) { |
1407 | xfree(dir); | |
1408 | xfree(pwd); | |
e6a3cfb5 | 1409 | xfree(conn); |
2cda7d6b | 1410 | return (-1); |
aa41be57 | 1411 | } |
2cda7d6b | 1412 | } else { |
1413 | if (file2 == NULL) | |
1414 | snprintf(cmd, sizeof cmd, "get %s", dir); | |
1415 | else | |
1416 | snprintf(cmd, sizeof cmd, "get %s %s", dir, | |
1417 | file2); | |
1418 | ||
1419 | err = parse_dispatch_command(conn, cmd, &pwd, 1); | |
1420 | xfree(dir); | |
1421 | xfree(pwd); | |
e6a3cfb5 | 1422 | xfree(conn); |
2cda7d6b | 1423 | return (err); |
1424 | } | |
1425 | xfree(dir); | |
1426 | } | |
1427 | ||
157b6700 | 1428 | #if defined(HAVE_SETVBUF) && !defined(BROKEN_SETVBUF) |
2cda7d6b | 1429 | setvbuf(stdout, NULL, _IOLBF, 0); |
1430 | setvbuf(infile, NULL, _IOLBF, 0); | |
1431 | #else | |
ed9e8be3 | 1432 | setlinebuf(stdout); |
1433 | setlinebuf(infile); | |
2cda7d6b | 1434 | #endif |
1435 | ||
0aa1cc4b | 1436 | interactive = !batchmode && isatty(STDIN_FILENO); |
2cda7d6b | 1437 | err = 0; |
1438 | for (;;) { | |
1439 | char *cp; | |
1440 | ||
0e5de6f8 | 1441 | signal(SIGINT, SIG_IGN); |
1442 | ||
5132eac0 | 1443 | if (el == NULL) { |
0aa1cc4b | 1444 | if (interactive) |
1445 | printf("sftp> "); | |
5132eac0 | 1446 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { |
0aa1cc4b | 1447 | if (interactive) |
1448 | printf("\n"); | |
5132eac0 | 1449 | break; |
1450 | } | |
0aa1cc4b | 1451 | if (!interactive) { /* Echo command */ |
1452 | printf("sftp> %s", cmd); | |
1453 | if (strlen(cmd) > 0 && | |
1454 | cmd[strlen(cmd) - 1] != '\n') | |
1455 | printf("\n"); | |
1456 | } | |
5132eac0 | 1457 | } else { |
1458 | #ifdef USE_LIBEDIT | |
1459 | const char *line; | |
1460 | int count = 0; | |
2cda7d6b | 1461 | |
0aa1cc4b | 1462 | if ((line = el_gets(el, &count)) == NULL || count <= 0) { |
1463 | printf("\n"); | |
1464 | break; | |
1465 | } | |
5132eac0 | 1466 | history(hl, &hev, H_ENTER, line); |
1467 | if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) { | |
1468 | fprintf(stderr, "Error: input line too long\n"); | |
1469 | continue; | |
1470 | } | |
1471 | #endif /* USE_LIBEDIT */ | |
2cda7d6b | 1472 | } |
1473 | ||
2cda7d6b | 1474 | cp = strrchr(cmd, '\n'); |
1475 | if (cp) | |
1476 | *cp = '\0'; | |
1477 | ||
0e5de6f8 | 1478 | /* Handle user interrupts gracefully during commands */ |
1479 | interrupted = 0; | |
1480 | signal(SIGINT, cmd_interrupt); | |
1481 | ||
2cda7d6b | 1482 | err = parse_dispatch_command(conn, cmd, &pwd, batchmode); |
1483 | if (err != 0) | |
1484 | break; | |
1485 | } | |
1486 | xfree(pwd); | |
e6a3cfb5 | 1487 | xfree(conn); |
2cda7d6b | 1488 | |
a345f787 | 1489 | #ifdef USE_LIBEDIT |
0aa1cc4b | 1490 | if (el != NULL) |
1491 | el_end(el); | |
a345f787 | 1492 | #endif /* USE_LIBEDIT */ |
0aa1cc4b | 1493 | |
2cda7d6b | 1494 | /* err == 1 signifies normal "quit" exit */ |
1495 | return (err >= 0 ? 0 : -1); | |
1496 | } | |
b65c3807 | 1497 | |
1b558925 | 1498 | static void |
1499 | connect_to_server(char *path, char **args, int *in, int *out) | |
61e96248 | 1500 | { |
1501 | int c_in, c_out; | |
9906a836 | 1502 | |
61e96248 | 1503 | #ifdef USE_PIPES |
1504 | int pin[2], pout[2]; | |
9906a836 | 1505 | |
61e96248 | 1506 | if ((pipe(pin) == -1) || (pipe(pout) == -1)) |
1507 | fatal("pipe: %s", strerror(errno)); | |
1508 | *in = pin[0]; | |
1509 | *out = pout[1]; | |
1510 | c_in = pout[0]; | |
1511 | c_out = pin[1]; | |
1512 | #else /* USE_PIPES */ | |
1513 | int inout[2]; | |
9906a836 | 1514 | |
61e96248 | 1515 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1) |
1516 | fatal("socketpair: %s", strerror(errno)); | |
1517 | *in = *out = inout[0]; | |
1518 | c_in = c_out = inout[1]; | |
1519 | #endif /* USE_PIPES */ | |
1520 | ||
1b558925 | 1521 | if ((sshpid = fork()) == -1) |
61e96248 | 1522 | fatal("fork: %s", strerror(errno)); |
1b558925 | 1523 | else if (sshpid == 0) { |
61e96248 | 1524 | if ((dup2(c_in, STDIN_FILENO) == -1) || |
1525 | (dup2(c_out, STDOUT_FILENO) == -1)) { | |
1526 | fprintf(stderr, "dup2: %s\n", strerror(errno)); | |
8dbffee9 | 1527 | _exit(1); |
61e96248 | 1528 | } |
1529 | close(*in); | |
1530 | close(*out); | |
1531 | close(c_in); | |
1532 | close(c_out); | |
0e5de6f8 | 1533 | |
1534 | /* | |
1535 | * The underlying ssh is in the same process group, so we must | |
f2107e97 | 1536 | * ignore SIGINT if we want to gracefully abort commands, |
1537 | * otherwise the signal will make it to the ssh process and | |
0e5de6f8 | 1538 | * kill it too |
1539 | */ | |
1540 | signal(SIGINT, SIG_IGN); | |
35e49915 | 1541 | execvp(path, args); |
a96fd7c2 | 1542 | fprintf(stderr, "exec: %s: %s\n", path, strerror(errno)); |
8dbffee9 | 1543 | _exit(1); |
61e96248 | 1544 | } |
1545 | ||
1b558925 | 1546 | signal(SIGTERM, killchild); |
1547 | signal(SIGINT, killchild); | |
1548 | signal(SIGHUP, killchild); | |
61e96248 | 1549 | close(c_in); |
1550 | close(c_out); | |
1551 | } | |
1552 | ||
396c147e | 1553 | static void |
61e96248 | 1554 | usage(void) |
1555 | { | |
22be05a5 | 1556 | extern char *__progname; |
762715ce | 1557 | |
f1278af7 | 1558 | fprintf(stderr, |
433e60ac | 1559 | "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n" |
1560 | " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n" | |
1561 | " [-S program] [-s subsystem | sftp_server] host\n" | |
1562 | " %s [[user@]host[:file [file]]]\n" | |
1563 | " %s [[user@]host[:dir[/]]]\n" | |
1564 | " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname); | |
61e96248 | 1565 | exit(1); |
1566 | } | |
1567 | ||
2b87da3b | 1568 | int |
61e96248 | 1569 | main(int argc, char **argv) |
1570 | { | |
9a36208d | 1571 | int in, out, ch, err; |
6e007f08 | 1572 | char *host, *userhost, *cp, *file2 = NULL; |
8a624ebf | 1573 | int debug_level = 0, sshver = 2; |
1574 | char *file1 = NULL, *sftp_server = NULL; | |
a96fd7c2 | 1575 | char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL; |
8a624ebf | 1576 | LogLevel ll = SYSLOG_LEVEL_INFO; |
1577 | arglist args; | |
0426a3b4 | 1578 | extern int optind; |
1579 | extern char *optarg; | |
61e96248 | 1580 | |
fd6168c1 | 1581 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
1582 | sanitise_stdfd(); | |
1583 | ||
fda04d7d | 1584 | __progname = ssh_get_progname(argv[0]); |
4116f5c0 | 1585 | memset(&args, '\0', sizeof(args)); |
8a624ebf | 1586 | args.list = NULL; |
e56b07ea | 1587 | addargs(&args, "%s", ssh_program); |
8a624ebf | 1588 | addargs(&args, "-oForwardX11 no"); |
1589 | addargs(&args, "-oForwardAgent no"); | |
d20f3c9e | 1590 | addargs(&args, "-oPermitLocalCommand no"); |
e1c5bfaf | 1591 | addargs(&args, "-oClearAllForwardings yes"); |
ac414e17 | 1592 | |
8a624ebf | 1593 | ll = SYSLOG_LEVEL_INFO; |
ac414e17 | 1594 | infile = stdin; |
0426a3b4 | 1595 | |
c25d3df7 | 1596 | while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) { |
0426a3b4 | 1597 | switch (ch) { |
1598 | case 'C': | |
8a624ebf | 1599 | addargs(&args, "-C"); |
0426a3b4 | 1600 | break; |
1601 | case 'v': | |
8a624ebf | 1602 | if (debug_level < 3) { |
1603 | addargs(&args, "-v"); | |
1604 | ll = SYSLOG_LEVEL_DEBUG1 + debug_level; | |
1605 | } | |
1606 | debug_level++; | |
0426a3b4 | 1607 | break; |
f1278af7 | 1608 | case 'F': |
0426a3b4 | 1609 | case 'o': |
f1278af7 | 1610 | addargs(&args, "-%c%s", ch, optarg); |
0426a3b4 | 1611 | break; |
1612 | case '1': | |
8a624ebf | 1613 | sshver = 1; |
0426a3b4 | 1614 | if (sftp_server == NULL) |
1615 | sftp_server = _PATH_SFTP_SERVER; | |
1616 | break; | |
1617 | case 's': | |
1618 | sftp_server = optarg; | |
1619 | break; | |
1620 | case 'S': | |
1621 | ssh_program = optarg; | |
4116f5c0 | 1622 | replacearg(&args, 0, "%s", ssh_program); |
0426a3b4 | 1623 | break; |
a5ec8a3d | 1624 | case 'b': |
a8b64bb8 | 1625 | if (batchmode) |
1626 | fatal("Batch file already specified."); | |
1627 | ||
1628 | /* Allow "-" as stdin */ | |
f2107e97 | 1629 | if (strcmp(optarg, "-") != 0 && |
4e2e5cfd | 1630 | (infile = fopen(optarg, "r")) == NULL) |
a8b64bb8 | 1631 | fatal("%s (%s).", strerror(errno), optarg); |
b65c3807 | 1632 | showprogress = 0; |
a8b64bb8 | 1633 | batchmode = 1; |
0b73a454 | 1634 | addargs(&args, "-obatchmode yes"); |
a5ec8a3d | 1635 | break; |
a96fd7c2 | 1636 | case 'P': |
1637 | sftp_direct = optarg; | |
1638 | break; | |
375f867e | 1639 | case 'B': |
1640 | copy_buffer_len = strtol(optarg, &cp, 10); | |
1641 | if (copy_buffer_len == 0 || *cp != '\0') | |
1642 | fatal("Invalid buffer size \"%s\"", optarg); | |
1643 | break; | |
c25d3df7 | 1644 | case 'R': |
1645 | num_requests = strtol(optarg, &cp, 10); | |
1646 | if (num_requests == 0 || *cp != '\0') | |
762715ce | 1647 | fatal("Invalid number of requests \"%s\"", |
c25d3df7 | 1648 | optarg); |
1649 | break; | |
0426a3b4 | 1650 | case 'h': |
1651 | default: | |
61e96248 | 1652 | usage(); |
1653 | } | |
1654 | } | |
1655 | ||
06abcf97 | 1656 | if (!isatty(STDERR_FILENO)) |
1657 | showprogress = 0; | |
1658 | ||
b69145c2 | 1659 | log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); |
1660 | ||
a96fd7c2 | 1661 | if (sftp_direct == NULL) { |
1662 | if (optind == argc || argc > (optind + 2)) | |
1663 | usage(); | |
61e96248 | 1664 | |
a96fd7c2 | 1665 | userhost = xstrdup(argv[optind]); |
1666 | file2 = argv[optind+1]; | |
edeeab1e | 1667 | |
15748b4d | 1668 | if ((host = strrchr(userhost, '@')) == NULL) |
a96fd7c2 | 1669 | host = userhost; |
1670 | else { | |
1671 | *host++ = '\0'; | |
1672 | if (!userhost[0]) { | |
1673 | fprintf(stderr, "Missing username\n"); | |
1674 | usage(); | |
1675 | } | |
4f6e2ba9 | 1676 | addargs(&args, "-l%s", userhost); |
61e96248 | 1677 | } |
61e96248 | 1678 | |
02de7c6e | 1679 | if ((cp = colon(host)) != NULL) { |
1680 | *cp++ = '\0'; | |
1681 | file1 = cp; | |
1682 | } | |
1683 | ||
a96fd7c2 | 1684 | host = cleanhostname(host); |
1685 | if (!*host) { | |
1686 | fprintf(stderr, "Missing hostname\n"); | |
1687 | usage(); | |
1688 | } | |
61e96248 | 1689 | |
a96fd7c2 | 1690 | addargs(&args, "-oProtocol %d", sshver); |
8a624ebf | 1691 | |
a96fd7c2 | 1692 | /* no subsystem if the server-spec contains a '/' */ |
1693 | if (sftp_server == NULL || strchr(sftp_server, '/') == NULL) | |
1694 | addargs(&args, "-s"); | |
61e96248 | 1695 | |
a96fd7c2 | 1696 | addargs(&args, "%s", host); |
762715ce | 1697 | addargs(&args, "%s", (sftp_server != NULL ? |
a96fd7c2 | 1698 | sftp_server : "sftp")); |
61e96248 | 1699 | |
a8b64bb8 | 1700 | if (!batchmode) |
1701 | fprintf(stderr, "Connecting to %s...\n", host); | |
1b558925 | 1702 | connect_to_server(ssh_program, args.list, &in, &out); |
a96fd7c2 | 1703 | } else { |
1704 | args.list = NULL; | |
1705 | addargs(&args, "sftp-server"); | |
61e96248 | 1706 | |
a8b64bb8 | 1707 | if (!batchmode) |
1708 | fprintf(stderr, "Attaching to %s...\n", sftp_direct); | |
1b558925 | 1709 | connect_to_server(sftp_direct, args.list, &in, &out); |
a96fd7c2 | 1710 | } |
4116f5c0 | 1711 | freeargs(&args); |
61e96248 | 1712 | |
9a36208d | 1713 | err = interactive_loop(in, out, file1, file2); |
61e96248 | 1714 | |
51fb577a | 1715 | #if !defined(USE_PIPES) |
ed9e8be3 | 1716 | shutdown(in, SHUT_RDWR); |
1717 | shutdown(out, SHUT_RDWR); | |
51fb577a | 1718 | #endif |
1719 | ||
61e96248 | 1720 | close(in); |
1721 | close(out); | |
a8b64bb8 | 1722 | if (batchmode) |
a5ec8a3d | 1723 | fclose(infile); |
61e96248 | 1724 | |
8c38e88b | 1725 | while (waitpid(sshpid, NULL, 0) == -1) |
1726 | if (errno != EINTR) | |
1727 | fatal("Couldn't wait for ssh process: %s", | |
1728 | strerror(errno)); | |
61e96248 | 1729 | |
9a36208d | 1730 | exit(err == 0 ? 0 : 1); |
61e96248 | 1731 | } |