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