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