summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2004-02-18 14:30:55 +1100
committerDamien Miller <djm@mindrot.org>2004-02-18 14:30:55 +1100
commit20e1fabace87376a9868b9bd83bd57db13f5763d (patch)
tree2ceba221966302608337bbb890dae38b38bd9038
parent9a47d86661129552df18074cef60d00f682a5d17 (diff)
- djm@cvs.openbsd.org 2004/02/17 11:03:08
[sftp.c] sftp.c and sftp-int.c, together at last; ok markus@
-rw-r--r--ChangeLog5
-rw-r--r--Makefile.in6
-rw-r--r--sftp-int.c1203
-rw-r--r--sftp.c1181
4 files changed, 1178 insertions, 1217 deletions
diff --git a/ChangeLog b/ChangeLog
index 349eec382..a5d3ee4df 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,9 @@
5 - djm@cvs.openbsd.org 2004/02/17 07:17:29 5 - djm@cvs.openbsd.org 2004/02/17 07:17:29
6 [sftp-glob.c sftp.c] 6 [sftp-glob.c sftp.c]
7 Remove useless headers; ok deraadt@ 7 Remove useless headers; ok deraadt@
8 - djm@cvs.openbsd.org 2004/02/17 11:03:08
9 [sftp.c]
10 sftp.c and sftp-int.c, together at last; ok markus@
8 11
920040217 1220040217
10 - (djm) Simplify the license on code I have written. No code changes. 13 - (djm) Simplify the license on code I have written. No code changes.
@@ -1874,4 +1877,4 @@
1874 - Fix sshd BindAddress and -b options for systems using fake-getaddrinfo. 1877 - Fix sshd BindAddress and -b options for systems using fake-getaddrinfo.
1875 Report from murple@murple.net, diagnosis from dtucker@zip.com.au 1878 Report from murple@murple.net, diagnosis from dtucker@zip.com.au
1876 1879
1877$Id: ChangeLog,v 1.3235 2004/02/18 03:11:13 djm Exp $ 1880$Id: ChangeLog,v 1.3236 2004/02/18 03:30:55 djm Exp $
diff --git a/Makefile.in b/Makefile.in
index eae406aa7..919b36819 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -1,4 +1,4 @@
1# $Id: Makefile.in,v 1.256 2004/02/12 15:17:11 tim Exp $ 1# $Id: Makefile.in,v 1.257 2004/02/18 03:35:11 djm Exp $
2 2
3# uncomment if you run a non bourne compatable shell. Ie. csh 3# uncomment if you run a non bourne compatable shell. Ie. csh
4#SHELL = @SH@ 4#SHELL = @SH@
@@ -158,8 +158,8 @@ ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
158sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o 158sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o
159 $(LD) -o $@ sftp-server.o sftp-common.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) 159 $(LD) -o $@ sftp-server.o sftp-common.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
160 160
161sftp$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-client.o sftp-int.o sftp-common.o sftp-glob.o progressmeter.o 161sftp$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-client.o sftp-common.o sftp-glob.o progressmeter.o
162 $(LD) -o $@ progressmeter.o sftp.o sftp-client.o sftp-common.o sftp-int.o sftp-glob.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) 162 $(LD) -o $@ progressmeter.o sftp.o sftp-client.o sftp-common.o sftp-glob.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
163 163
164ssh-rand-helper${EXEEXT}: $(LIBCOMPAT) libssh.a ssh-rand-helper.o 164ssh-rand-helper${EXEEXT}: $(LIBCOMPAT) libssh.a ssh-rand-helper.o
165 $(LD) -o $@ ssh-rand-helper.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) 165 $(LD) -o $@ ssh-rand-helper.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
diff --git a/sftp-int.c b/sftp-int.c
deleted file mode 100644
index 3ee57515d..000000000
--- a/sftp-int.c
+++ /dev/null
@@ -1,1203 +0,0 @@
1/*
2 * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25/* XXX: recursive operations */
26
27#include "includes.h"
28RCSID("$OpenBSD: sftp-int.c,v 1.67 2004/01/23 17:57:48 mouring Exp $");
29
30#include "buffer.h"
31#include "xmalloc.h"
32#include "log.h"
33#include "pathnames.h"
34
35#include "sftp.h"
36#include "sftp-common.h"
37#include "sftp-glob.h"
38#include "sftp-client.h"
39#include "sftp-int.h"
40
41/* File to read commands from */
42extern FILE *infile;
43
44/* Are we in batchfile mode? */
45extern int batchmode;
46
47/* Size of buffer used when copying files */
48extern size_t copy_buffer_len;
49
50/* Number of concurrent outstanding requests */
51extern int num_requests;
52
53/* This is set to 0 if the progressmeter is not desired. */
54int showprogress = 1;
55
56/* Separators for interactive commands */
57#define WHITESPACE " \t\r\n"
58
59/* Define what type of ls view (0 - multi-column) */
60#define LONG_VIEW 1 /* Full view ala ls -l */
61#define SHORT_VIEW 2 /* Single row view ala ls -1 */
62
63/* Commands for interactive mode */
64#define I_CHDIR 1
65#define I_CHGRP 2
66#define I_CHMOD 3
67#define I_CHOWN 4
68#define I_GET 5
69#define I_HELP 6
70#define I_LCHDIR 7
71#define I_LLS 8
72#define I_LMKDIR 9
73#define I_LPWD 10
74#define I_LS 11
75#define I_LUMASK 12
76#define I_MKDIR 13
77#define I_PUT 14
78#define I_PWD 15
79#define I_QUIT 16
80#define I_RENAME 17
81#define I_RM 18
82#define I_RMDIR 19
83#define I_SHELL 20
84#define I_SYMLINK 21
85#define I_VERSION 22
86#define I_PROGRESS 23
87
88struct CMD {
89 const char *c;
90 const int n;
91};
92
93static const struct CMD cmds[] = {
94 { "bye", I_QUIT },
95 { "cd", I_CHDIR },
96 { "chdir", I_CHDIR },
97 { "chgrp", I_CHGRP },
98 { "chmod", I_CHMOD },
99 { "chown", I_CHOWN },
100 { "dir", I_LS },
101 { "exit", I_QUIT },
102 { "get", I_GET },
103 { "mget", I_GET },
104 { "help", I_HELP },
105 { "lcd", I_LCHDIR },
106 { "lchdir", I_LCHDIR },
107 { "lls", I_LLS },
108 { "lmkdir", I_LMKDIR },
109 { "ln", I_SYMLINK },
110 { "lpwd", I_LPWD },
111 { "ls", I_LS },
112 { "lumask", I_LUMASK },
113 { "mkdir", I_MKDIR },
114 { "progress", I_PROGRESS },
115 { "put", I_PUT },
116 { "mput", I_PUT },
117 { "pwd", I_PWD },
118 { "quit", I_QUIT },
119 { "rename", I_RENAME },
120 { "rm", I_RM },
121 { "rmdir", I_RMDIR },
122 { "symlink", I_SYMLINK },
123 { "version", I_VERSION },
124 { "!", I_SHELL },
125 { "?", I_HELP },
126 { NULL, -1}
127};
128
129static void
130help(void)
131{
132 printf("Available commands:\n");
133 printf("cd path Change remote directory to 'path'\n");
134 printf("lcd path Change local directory to 'path'\n");
135 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
136 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
137 printf("chown own path Change owner of file 'path' to 'own'\n");
138 printf("help Display this help text\n");
139 printf("get remote-path [local-path] Download file\n");
140 printf("lls [ls-options [path]] Display local directory listing\n");
141 printf("ln oldpath newpath Symlink remote file\n");
142 printf("lmkdir path Create local directory\n");
143 printf("lpwd Print local working directory\n");
144 printf("ls [path] Display remote directory listing\n");
145 printf("lumask umask Set local umask to 'umask'\n");
146 printf("mkdir path Create remote directory\n");
147 printf("progress Toggle display of progress meter\n");
148 printf("put local-path [remote-path] Upload file\n");
149 printf("pwd Display remote working directory\n");
150 printf("exit Quit sftp\n");
151 printf("quit Quit sftp\n");
152 printf("rename oldpath newpath Rename remote file\n");
153 printf("rmdir path Remove remote directory\n");
154 printf("rm path Delete remote file\n");
155 printf("symlink oldpath newpath Symlink remote file\n");
156 printf("version Show SFTP version\n");
157 printf("!command Execute 'command' in local shell\n");
158 printf("! Escape to local shell\n");
159 printf("? Synonym for help\n");
160}
161
162static void
163local_do_shell(const char *args)
164{
165 int status;
166 char *shell;
167 pid_t pid;
168
169 if (!*args)
170 args = NULL;
171
172 if ((shell = getenv("SHELL")) == NULL)
173 shell = _PATH_BSHELL;
174
175 if ((pid = fork()) == -1)
176 fatal("Couldn't fork: %s", strerror(errno));
177
178 if (pid == 0) {
179 /* XXX: child has pipe fds to ssh subproc open - issue? */
180 if (args) {
181 debug3("Executing %s -c \"%s\"", shell, args);
182 execl(shell, shell, "-c", args, (char *)NULL);
183 } else {
184 debug3("Executing %s", shell);
185 execl(shell, shell, (char *)NULL);
186 }
187 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
188 strerror(errno));
189 _exit(1);
190 }
191 while (waitpid(pid, &status, 0) == -1)
192 if (errno != EINTR)
193 fatal("Couldn't wait for child: %s", strerror(errno));
194 if (!WIFEXITED(status))
195 error("Shell exited abormally");
196 else if (WEXITSTATUS(status))
197 error("Shell exited with status %d", WEXITSTATUS(status));
198}
199
200static void
201local_do_ls(const char *args)
202{
203 if (!args || !*args)
204 local_do_shell(_PATH_LS);
205 else {
206 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
207 char *buf = xmalloc(len);
208
209 /* XXX: quoting - rip quoting code from ftp? */
210 snprintf(buf, len, _PATH_LS " %s", args);
211 local_do_shell(buf);
212 xfree(buf);
213 }
214}
215
216/* Strip one path (usually the pwd) from the start of another */
217static char *
218path_strip(char *path, char *strip)
219{
220 size_t len;
221
222 if (strip == NULL)
223 return (xstrdup(path));
224
225 len = strlen(strip);
226 if (strip != NULL && strncmp(path, strip, len) == 0) {
227 if (strip[len - 1] != '/' && path[len] == '/')
228 len++;
229 return (xstrdup(path + len));
230 }
231
232 return (xstrdup(path));
233}
234
235static char *
236path_append(char *p1, char *p2)
237{
238 char *ret;
239 int len = strlen(p1) + strlen(p2) + 2;
240
241 ret = xmalloc(len);
242 strlcpy(ret, p1, len);
243 if (p1[strlen(p1) - 1] != '/')
244 strlcat(ret, "/", len);
245 strlcat(ret, p2, len);
246
247 return(ret);
248}
249
250static char *
251make_absolute(char *p, char *pwd)
252{
253 char *abs;
254
255 /* Derelativise */
256 if (p && p[0] != '/') {
257 abs = path_append(pwd, p);
258 xfree(p);
259 return(abs);
260 } else
261 return(p);
262}
263
264static int
265infer_path(const char *p, char **ifp)
266{
267 char *cp;
268
269 cp = strrchr(p, '/');
270 if (cp == NULL) {
271 *ifp = xstrdup(p);
272 return(0);
273 }
274
275 if (!cp[1]) {
276 error("Invalid path");
277 return(-1);
278 }
279
280 *ifp = xstrdup(cp + 1);
281 return(0);
282}
283
284static int
285parse_getput_flags(const char **cpp, int *pflag)
286{
287 const char *cp = *cpp;
288
289 /* Check for flags */
290 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
291 switch (cp[1]) {
292 case 'p':
293 case 'P':
294 *pflag = 1;
295 break;
296 default:
297 error("Invalid flag -%c", cp[1]);
298 return(-1);
299 }
300 cp += 2;
301 *cpp = cp + strspn(cp, WHITESPACE);
302 }
303
304 return(0);
305}
306
307static int
308parse_ls_flags(const char **cpp, int *lflag)
309{
310 const char *cp = *cpp;
311
312 /* Check for flags */
313 if (cp++[0] == '-') {
314 for(; strchr(WHITESPACE, *cp) == NULL; cp++) {
315 switch (*cp) {
316 case 'l':
317 *lflag = LONG_VIEW;
318 break;
319 case '1':
320 *lflag = SHORT_VIEW;
321 break;
322 default:
323 error("Invalid flag -%c", *cp);
324 return(-1);
325 }
326 }
327 *cpp = cp + strspn(cp, WHITESPACE);
328 }
329
330 return(0);
331}
332
333static int
334get_pathname(const char **cpp, char **path)
335{
336 const char *cp = *cpp, *end;
337 char quot;
338 int i, j;
339
340 cp += strspn(cp, WHITESPACE);
341 if (!*cp) {
342 *cpp = cp;
343 *path = NULL;
344 return (0);
345 }
346
347 *path = xmalloc(strlen(cp) + 1);
348
349 /* Check for quoted filenames */
350 if (*cp == '\"' || *cp == '\'') {
351 quot = *cp++;
352
353 /* Search for terminating quote, unescape some chars */
354 for (i = j = 0; i <= strlen(cp); i++) {
355 if (cp[i] == quot) { /* Found quote */
356 i++;
357 (*path)[j] = '\0';
358 break;
359 }
360 if (cp[i] == '\0') { /* End of string */
361 error("Unterminated quote");
362 goto fail;
363 }
364 if (cp[i] == '\\') { /* Escaped characters */
365 i++;
366 if (cp[i] != '\'' && cp[i] != '\"' &&
367 cp[i] != '\\') {
368 error("Bad escaped character '\%c'",
369 cp[i]);
370 goto fail;
371 }
372 }
373 (*path)[j++] = cp[i];
374 }
375
376 if (j == 0) {
377 error("Empty quotes");
378 goto fail;
379 }
380 *cpp = cp + i + strspn(cp + i, WHITESPACE);
381 } else {
382 /* Read to end of filename */
383 end = strpbrk(cp, WHITESPACE);
384 if (end == NULL)
385 end = strchr(cp, '\0');
386 *cpp = end + strspn(end, WHITESPACE);
387
388 memcpy(*path, cp, end - cp);
389 (*path)[end - cp] = '\0';
390 }
391 return (0);
392
393 fail:
394 xfree(*path);
395 *path = NULL;
396 return (-1);
397}
398
399static int
400is_dir(char *path)
401{
402 struct stat sb;
403
404 /* XXX: report errors? */
405 if (stat(path, &sb) == -1)
406 return(0);
407
408 return(sb.st_mode & S_IFDIR);
409}
410
411static int
412is_reg(char *path)
413{
414 struct stat sb;
415
416 if (stat(path, &sb) == -1)
417 fatal("stat %s: %s", path, strerror(errno));
418
419 return(S_ISREG(sb.st_mode));
420}
421
422static int
423remote_is_dir(struct sftp_conn *conn, char *path)
424{
425 Attrib *a;
426
427 /* XXX: report errors? */
428 if ((a = do_stat(conn, path, 1)) == NULL)
429 return(0);
430 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
431 return(0);
432 return(a->perm & S_IFDIR);
433}
434
435static int
436process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
437{
438 char *abs_src = NULL;
439 char *abs_dst = NULL;
440 char *tmp;
441 glob_t g;
442 int err = 0;
443 int i;
444
445 abs_src = xstrdup(src);
446 abs_src = make_absolute(abs_src, pwd);
447
448 memset(&g, 0, sizeof(g));
449 debug3("Looking up %s", abs_src);
450 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
451 error("File \"%s\" not found.", abs_src);
452 err = -1;
453 goto out;
454 }
455
456 /* If multiple matches, dst must be a directory or unspecified */
457 if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
458 error("Multiple files match, but \"%s\" is not a directory",
459 dst);
460 err = -1;
461 goto out;
462 }
463
464 for (i = 0; g.gl_pathv[i]; i++) {
465 if (infer_path(g.gl_pathv[i], &tmp)) {
466 err = -1;
467 goto out;
468 }
469
470 if (g.gl_matchc == 1 && dst) {
471 /* If directory specified, append filename */
472 if (is_dir(dst)) {
473 if (infer_path(g.gl_pathv[0], &tmp)) {
474 err = 1;
475 goto out;
476 }
477 abs_dst = path_append(dst, tmp);
478 xfree(tmp);
479 } else
480 abs_dst = xstrdup(dst);
481 } else if (dst) {
482 abs_dst = path_append(dst, tmp);
483 xfree(tmp);
484 } else
485 abs_dst = tmp;
486
487 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
488 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
489 err = -1;
490 xfree(abs_dst);
491 abs_dst = NULL;
492 }
493
494out:
495 xfree(abs_src);
496 if (abs_dst)
497 xfree(abs_dst);
498 globfree(&g);
499 return(err);
500}
501
502static int
503process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
504{
505 char *tmp_dst = NULL;
506 char *abs_dst = NULL;
507 char *tmp;
508 glob_t g;
509 int err = 0;
510 int i;
511
512 if (dst) {
513 tmp_dst = xstrdup(dst);
514 tmp_dst = make_absolute(tmp_dst, pwd);
515 }
516
517 memset(&g, 0, sizeof(g));
518 debug3("Looking up %s", src);
519 if (glob(src, 0, NULL, &g)) {
520 error("File \"%s\" not found.", src);
521 err = -1;
522 goto out;
523 }
524
525 /* If multiple matches, dst may be directory or unspecified */
526 if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
527 error("Multiple files match, but \"%s\" is not a directory",
528 tmp_dst);
529 err = -1;
530 goto out;
531 }
532
533 for (i = 0; g.gl_pathv[i]; i++) {
534 if (!is_reg(g.gl_pathv[i])) {
535 error("skipping non-regular file %s",
536 g.gl_pathv[i]);
537 continue;
538 }
539 if (infer_path(g.gl_pathv[i], &tmp)) {
540 err = -1;
541 goto out;
542 }
543
544 if (g.gl_matchc == 1 && tmp_dst) {
545 /* If directory specified, append filename */
546 if (remote_is_dir(conn, tmp_dst)) {
547 if (infer_path(g.gl_pathv[0], &tmp)) {
548 err = 1;
549 goto out;
550 }
551 abs_dst = path_append(tmp_dst, tmp);
552 xfree(tmp);
553 } else
554 abs_dst = xstrdup(tmp_dst);
555
556 } else if (tmp_dst) {
557 abs_dst = path_append(tmp_dst, tmp);
558 xfree(tmp);
559 } else
560 abs_dst = make_absolute(tmp, pwd);
561
562 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
563 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
564 err = -1;
565 }
566
567out:
568 if (abs_dst)
569 xfree(abs_dst);
570 if (tmp_dst)
571 xfree(tmp_dst);
572 globfree(&g);
573 return(err);
574}
575
576static int
577sdirent_comp(const void *aa, const void *bb)
578{
579 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
580 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
581
582 return (strcmp(a->filename, b->filename));
583}
584
585/* sftp ls.1 replacement for directories */
586static int
587do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
588{
589 int n, c = 1, colspace = 0, columns = 1;
590 SFTP_DIRENT **d;
591
592 if ((n = do_readdir(conn, path, &d)) != 0)
593 return (n);
594
595 if (!(lflag & SHORT_VIEW)) {
596 int m = 0, width = 80;
597 struct winsize ws;
598 char *tmp;
599
600 /* Count entries for sort and find longest filename */
601 for (n = 0; d[n] != NULL; n++)
602 m = MAX(m, strlen(d[n]->filename));
603
604 /* Add any subpath that also needs to be counted */
605 tmp = path_strip(path, strip_path);
606 m += strlen(tmp);
607 xfree(tmp);
608
609 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
610 width = ws.ws_col;
611
612 columns = width / (m + 2);
613 columns = MAX(columns, 1);
614 colspace = width / columns;
615 colspace = MIN(colspace, width);
616 }
617
618 qsort(d, n, sizeof(*d), sdirent_comp);
619
620 for (n = 0; d[n] != NULL; n++) {
621 char *tmp, *fname;
622
623 tmp = path_append(path, d[n]->filename);
624 fname = path_strip(tmp, strip_path);
625 xfree(tmp);
626
627 if (lflag & LONG_VIEW) {
628 char *lname;
629 struct stat sb;
630
631 memset(&sb, 0, sizeof(sb));
632 attrib_to_stat(&d[n]->a, &sb);
633 lname = ls_file(fname, &sb, 1);
634 printf("%s\n", lname);
635 xfree(lname);
636 } else {
637 printf("%-*s", colspace, fname);
638 if (c >= columns) {
639 printf("\n");
640 c = 1;
641 } else
642 c++;
643 }
644
645 xfree(fname);
646 }
647
648 if (!(lflag & LONG_VIEW) && (c != 1))
649 printf("\n");
650
651 free_sftp_dirents(d);
652 return (0);
653}
654
655/* sftp ls.1 replacement which handles path globs */
656static int
657do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
658 int lflag)
659{
660 glob_t g;
661 int i, c = 1, colspace = 0, columns = 1;
662 Attrib *a;
663
664 memset(&g, 0, sizeof(g));
665
666 if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
667 NULL, &g)) {
668 error("Can't ls: \"%s\" not found", path);
669 return (-1);
670 }
671
672 /*
673 * If the glob returns a single match, which is the same as the
674 * input glob, and it is a directory, then just list its contents
675 */
676 if (g.gl_pathc == 1 &&
677 strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
678 if ((a = do_lstat(conn, path, 1)) == NULL) {
679 globfree(&g);
680 return (-1);
681 }
682 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
683 S_ISDIR(a->perm)) {
684 globfree(&g);
685 return (do_ls_dir(conn, path, strip_path, lflag));
686 }
687 }
688
689 if (!(lflag & SHORT_VIEW)) {
690 int m = 0, width = 80;
691 struct winsize ws;
692
693 /* Count entries for sort and find longest filename */
694 for (i = 0; g.gl_pathv[i]; i++)
695 m = MAX(m, strlen(g.gl_pathv[i]));
696
697 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
698 width = ws.ws_col;
699
700 columns = width / (m + 2);
701 columns = MAX(columns, 1);
702 colspace = width / columns;
703 }
704
705 for (i = 0; g.gl_pathv[i]; i++) {
706 char *fname;
707
708 fname = path_strip(g.gl_pathv[i], strip_path);
709
710 if (lflag & LONG_VIEW) {
711 char *lname;
712 struct stat sb;
713
714 /*
715 * XXX: this is slow - 1 roundtrip per path
716 * A solution to this is to fork glob() and
717 * build a sftp specific version which keeps the
718 * attribs (which currently get thrown away)
719 * that the server returns as well as the filenames.
720 */
721 memset(&sb, 0, sizeof(sb));
722 a = do_lstat(conn, g.gl_pathv[i], 1);
723 if (a != NULL)
724 attrib_to_stat(a, &sb);
725 lname = ls_file(fname, &sb, 1);
726 printf("%s\n", lname);
727 xfree(lname);
728 } else {
729 printf("%-*s", colspace, fname);
730 if (c >= columns) {
731 printf("\n");
732 c = 1;
733 } else
734 c++;
735 }
736 xfree(fname);
737 }
738
739 if (!(lflag & LONG_VIEW) && (c != 1))
740 printf("\n");
741
742 if (g.gl_pathc)
743 globfree(&g);
744
745 return (0);
746}
747
748static int
749parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
750 unsigned long *n_arg, char **path1, char **path2)
751{
752 const char *cmd, *cp = *cpp;
753 char *cp2;
754 int base = 0;
755 long l;
756 int i, cmdnum;
757
758 /* Skip leading whitespace */
759 cp = cp + strspn(cp, WHITESPACE);
760
761 /* Ignore blank lines and lines which begin with comment '#' char */
762 if (*cp == '\0' || *cp == '#')
763 return (0);
764
765 /* Check for leading '-' (disable error processing) */
766 *iflag = 0;
767 if (*cp == '-') {
768 *iflag = 1;
769 cp++;
770 }
771
772 /* Figure out which command we have */
773 for (i = 0; cmds[i].c; i++) {
774 int cmdlen = strlen(cmds[i].c);
775
776 /* Check for command followed by whitespace */
777 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
778 strchr(WHITESPACE, cp[cmdlen])) {
779 cp += cmdlen;
780 cp = cp + strspn(cp, WHITESPACE);
781 break;
782 }
783 }
784 cmdnum = cmds[i].n;
785 cmd = cmds[i].c;
786
787 /* Special case */
788 if (*cp == '!') {
789 cp++;
790 cmdnum = I_SHELL;
791 } else if (cmdnum == -1) {
792 error("Invalid command.");
793 return (-1);
794 }
795
796 /* Get arguments and parse flags */
797 *lflag = *pflag = *n_arg = 0;
798 *path1 = *path2 = NULL;
799 switch (cmdnum) {
800 case I_GET:
801 case I_PUT:
802 if (parse_getput_flags(&cp, pflag))
803 return(-1);
804 /* Get first pathname (mandatory) */
805 if (get_pathname(&cp, path1))
806 return(-1);
807 if (*path1 == NULL) {
808 error("You must specify at least one path after a "
809 "%s command.", cmd);
810 return(-1);
811 }
812 /* Try to get second pathname (optional) */
813 if (get_pathname(&cp, path2))
814 return(-1);
815 break;
816 case I_RENAME:
817 case I_SYMLINK:
818 if (get_pathname(&cp, path1))
819 return(-1);
820 if (get_pathname(&cp, path2))
821 return(-1);
822 if (!*path1 || !*path2) {
823 error("You must specify two paths after a %s "
824 "command.", cmd);
825 return(-1);
826 }
827 break;
828 case I_RM:
829 case I_MKDIR:
830 case I_RMDIR:
831 case I_CHDIR:
832 case I_LCHDIR:
833 case I_LMKDIR:
834 /* Get pathname (mandatory) */
835 if (get_pathname(&cp, path1))
836 return(-1);
837 if (*path1 == NULL) {
838 error("You must specify a path after a %s command.",
839 cmd);
840 return(-1);
841 }
842 break;
843 case I_LS:
844 if (parse_ls_flags(&cp, lflag))
845 return(-1);
846 /* Path is optional */
847 if (get_pathname(&cp, path1))
848 return(-1);
849 break;
850 case I_LLS:
851 case I_SHELL:
852 /* Uses the rest of the line */
853 break;
854 case I_LUMASK:
855 base = 8;
856 case I_CHMOD:
857 base = 8;
858 case I_CHOWN:
859 case I_CHGRP:
860 /* Get numeric arg (mandatory) */
861 l = strtol(cp, &cp2, base);
862 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
863 errno == ERANGE) || l < 0) {
864 error("You must supply a numeric argument "
865 "to the %s command.", cmd);
866 return(-1);
867 }
868 cp = cp2;
869 *n_arg = l;
870 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
871 break;
872 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
873 error("You must supply a numeric argument "
874 "to the %s command.", cmd);
875 return(-1);
876 }
877 cp += strspn(cp, WHITESPACE);
878
879 /* Get pathname (mandatory) */
880 if (get_pathname(&cp, path1))
881 return(-1);
882 if (*path1 == NULL) {
883 error("You must specify a path after a %s command.",
884 cmd);
885 return(-1);
886 }
887 break;
888 case I_QUIT:
889 case I_PWD:
890 case I_LPWD:
891 case I_HELP:
892 case I_VERSION:
893 case I_PROGRESS:
894 break;
895 default:
896 fatal("Command not implemented");
897 }
898
899 *cpp = cp;
900 return(cmdnum);
901}
902
903static int
904parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
905 int err_abort)
906{
907 char *path1, *path2, *tmp;
908 int pflag, lflag, iflag, cmdnum, i;
909 unsigned long n_arg;
910 Attrib a, *aa;
911 char path_buf[MAXPATHLEN];
912 int err = 0;
913 glob_t g;
914
915 path1 = path2 = NULL;
916 cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
917 &path1, &path2);
918
919 if (iflag != 0)
920 err_abort = 0;
921
922 memset(&g, 0, sizeof(g));
923
924 /* Perform command */
925 switch (cmdnum) {
926 case 0:
927 /* Blank line */
928 break;
929 case -1:
930 /* Unrecognized command */
931 err = -1;
932 break;
933 case I_GET:
934 err = process_get(conn, path1, path2, *pwd, pflag);
935 break;
936 case I_PUT:
937 err = process_put(conn, path1, path2, *pwd, pflag);
938 break;
939 case I_RENAME:
940 path1 = make_absolute(path1, *pwd);
941 path2 = make_absolute(path2, *pwd);
942 err = do_rename(conn, path1, path2);
943 break;
944 case I_SYMLINK:
945 path2 = make_absolute(path2, *pwd);
946 err = do_symlink(conn, path1, path2);
947 break;
948 case I_RM:
949 path1 = make_absolute(path1, *pwd);
950 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
951 for (i = 0; g.gl_pathv[i]; i++) {
952 printf("Removing %s\n", g.gl_pathv[i]);
953 err = do_rm(conn, g.gl_pathv[i]);
954 if (err != 0 && err_abort)
955 break;
956 }
957 break;
958 case I_MKDIR:
959 path1 = make_absolute(path1, *pwd);
960 attrib_clear(&a);
961 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
962 a.perm = 0777;
963 err = do_mkdir(conn, path1, &a);
964 break;
965 case I_RMDIR:
966 path1 = make_absolute(path1, *pwd);
967 err = do_rmdir(conn, path1);
968 break;
969 case I_CHDIR:
970 path1 = make_absolute(path1, *pwd);
971 if ((tmp = do_realpath(conn, path1)) == NULL) {
972 err = 1;
973 break;
974 }
975 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
976 xfree(tmp);
977 err = 1;
978 break;
979 }
980 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
981 error("Can't change directory: Can't check target");
982 xfree(tmp);
983 err = 1;
984 break;
985 }
986 if (!S_ISDIR(aa->perm)) {
987 error("Can't change directory: \"%s\" is not "
988 "a directory", tmp);
989 xfree(tmp);
990 err = 1;
991 break;
992 }
993 xfree(*pwd);
994 *pwd = tmp;
995 break;
996 case I_LS:
997 if (!path1) {
998 do_globbed_ls(conn, *pwd, *pwd, lflag);
999 break;
1000 }
1001
1002 /* Strip pwd off beginning of non-absolute paths */
1003 tmp = NULL;
1004 if (*path1 != '/')
1005 tmp = *pwd;
1006
1007 path1 = make_absolute(path1, *pwd);
1008 err = do_globbed_ls(conn, path1, tmp, lflag);
1009 break;
1010 case I_LCHDIR:
1011 if (chdir(path1) == -1) {
1012 error("Couldn't change local directory to "
1013 "\"%s\": %s", path1, strerror(errno));
1014 err = 1;
1015 }
1016 break;
1017 case I_LMKDIR:
1018 if (mkdir(path1, 0777) == -1) {
1019 error("Couldn't create local directory "
1020 "\"%s\": %s", path1, strerror(errno));
1021 err = 1;
1022 }
1023 break;
1024 case I_LLS:
1025 local_do_ls(cmd);
1026 break;
1027 case I_SHELL:
1028 local_do_shell(cmd);
1029 break;
1030 case I_LUMASK:
1031 umask(n_arg);
1032 printf("Local umask: %03lo\n", n_arg);
1033 break;
1034 case I_CHMOD:
1035 path1 = make_absolute(path1, *pwd);
1036 attrib_clear(&a);
1037 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1038 a.perm = n_arg;
1039 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1040 for (i = 0; g.gl_pathv[i]; i++) {
1041 printf("Changing mode on %s\n", g.gl_pathv[i]);
1042 err = do_setstat(conn, g.gl_pathv[i], &a);
1043 if (err != 0 && err_abort)
1044 break;
1045 }
1046 break;
1047 case I_CHOWN:
1048 case I_CHGRP:
1049 path1 = make_absolute(path1, *pwd);
1050 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1051 for (i = 0; g.gl_pathv[i]; i++) {
1052 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1053 if (err != 0 && err_abort)
1054 break;
1055 else
1056 continue;
1057 }
1058 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1059 error("Can't get current ownership of "
1060 "remote file \"%s\"", g.gl_pathv[i]);
1061 if (err != 0 && err_abort)
1062 break;
1063 else
1064 continue;
1065 }
1066 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1067 if (cmdnum == I_CHOWN) {
1068 printf("Changing owner on %s\n", g.gl_pathv[i]);
1069 aa->uid = n_arg;
1070 } else {
1071 printf("Changing group on %s\n", g.gl_pathv[i]);
1072 aa->gid = n_arg;
1073 }
1074 err = do_setstat(conn, g.gl_pathv[i], aa);
1075 if (err != 0 && err_abort)
1076 break;
1077 }
1078 break;
1079 case I_PWD:
1080 printf("Remote working directory: %s\n", *pwd);
1081 break;
1082 case I_LPWD:
1083 if (!getcwd(path_buf, sizeof(path_buf))) {
1084 error("Couldn't get local cwd: %s", strerror(errno));
1085 err = -1;
1086 break;
1087 }
1088 printf("Local working directory: %s\n", path_buf);
1089 break;
1090 case I_QUIT:
1091 /* Processed below */
1092 break;
1093 case I_HELP:
1094 help();
1095 break;
1096 case I_VERSION:
1097 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1098 break;
1099 case I_PROGRESS:
1100 showprogress = !showprogress;
1101 if (showprogress)
1102 printf("Progress meter enabled\n");
1103 else
1104 printf("Progress meter disabled\n");
1105 break;
1106 default:
1107 fatal("%d is not implemented", cmdnum);
1108 }
1109
1110 if (g.gl_pathc)
1111 globfree(&g);
1112 if (path1)
1113 xfree(path1);
1114 if (path2)
1115 xfree(path2);
1116
1117 /* If an unignored error occurs in batch mode we should abort. */
1118 if (err_abort && err != 0)
1119 return (-1);
1120 else if (cmdnum == I_QUIT)
1121 return (1);
1122
1123 return (0);
1124}
1125
1126int
1127interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1128{
1129 char *pwd;
1130 char *dir = NULL;
1131 char cmd[2048];
1132 struct sftp_conn *conn;
1133 int err;
1134
1135 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1136 if (conn == NULL)
1137 fatal("Couldn't initialise connection to server");
1138
1139 pwd = do_realpath(conn, ".");
1140 if (pwd == NULL)
1141 fatal("Need cwd");
1142
1143 if (file1 != NULL) {
1144 dir = xstrdup(file1);
1145 dir = make_absolute(dir, pwd);
1146
1147 if (remote_is_dir(conn, dir) && file2 == NULL) {
1148 printf("Changing to: %s\n", dir);
1149 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
1150 if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0)
1151 return (-1);
1152 } else {
1153 if (file2 == NULL)
1154 snprintf(cmd, sizeof cmd, "get %s", dir);
1155 else
1156 snprintf(cmd, sizeof cmd, "get %s %s", dir,
1157 file2);
1158
1159 err = parse_dispatch_command(conn, cmd, &pwd, 1);
1160 xfree(dir);
1161 xfree(pwd);
1162 return (err);
1163 }
1164 xfree(dir);
1165 }
1166
1167#if HAVE_SETVBUF
1168 setvbuf(stdout, NULL, _IOLBF, 0);
1169 setvbuf(infile, NULL, _IOLBF, 0);
1170#else
1171 setlinebuf(stdout);
1172 setlinebuf(infile);
1173#endif
1174
1175 err = 0;
1176 for (;;) {
1177 char *cp;
1178
1179 printf("sftp> ");
1180
1181 /* XXX: use libedit */
1182 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1183 printf("\n");
1184 break;
1185 }
1186
1187 if (batchmode) /* Echo command */
1188 printf("%s", cmd);
1189
1190 cp = strrchr(cmd, '\n');
1191 if (cp)
1192 *cp = '\0';
1193
1194 err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
1195 if (err != 0)
1196 break;
1197 }
1198 xfree(pwd);
1199
1200 /* err == 1 signifies normal "quit" exit */
1201 return (err >= 0 ? 0 : -1);
1202}
1203
diff --git a/sftp.c b/sftp.c
index 04b859a8d..7f7f50731 100644
--- a/sftp.c
+++ b/sftp.c
@@ -16,7 +16,7 @@
16 16
17#include "includes.h" 17#include "includes.h"
18 18
19RCSID("$OpenBSD: sftp.c,v 1.43 2004/02/17 07:17:29 djm Exp $"); 19RCSID("$OpenBSD: sftp.c,v 1.44 2004/02/17 11:03:08 djm Exp $");
20 20
21#include "buffer.h" 21#include "buffer.h"
22#include "xmalloc.h" 22#include "xmalloc.h"
@@ -28,7 +28,26 @@ RCSID("$OpenBSD: sftp.c,v 1.43 2004/02/17 07:17:29 djm Exp $");
28#include "sftp-common.h" 28#include "sftp-common.h"
29#include "sftp-client.h" 29#include "sftp-client.h"
30 30
31int interactive_loop(int, int, char *, char *); /* sftp-int.c */ 31/* File to read commands from */
32FILE* infile;
33
34/* Are we in batchfile mode? */
35int batchmode = 0;
36
37/* Size of buffer used when copying files */
38size_t copy_buffer_len = 32768;
39
40/* Number of concurrent outstanding requests */
41size_t num_requests = 16;
42
43/* PID of ssh transport process */
44static pid_t sshpid = -1;
45
46/* This is set to 0 if the progressmeter is not desired. */
47int showprogress;
48
49int remote_glob(struct sftp_conn *, const char *, int,
50 int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
32 51
33#ifdef HAVE___PROGNAME 52#ifdef HAVE___PROGNAME
34extern char *__progname; 53extern char *__progname;
@@ -36,13 +55,1155 @@ extern char *__progname;
36char *__progname; 55char *__progname;
37#endif 56#endif
38 57
39FILE* infile; 58/* Separators for interactive commands */
40int batchmode = 0; 59#define WHITESPACE " \t\r\n"
41size_t copy_buffer_len = 32768; 60
42size_t num_requests = 16; 61/* Define what type of ls view (0 - multi-column) */
43static pid_t sshpid = -1; 62#define LONG_VIEW 1 /* Full view ala ls -l */
63#define SHORT_VIEW 2 /* Single row view ala ls -1 */
64
65/* Commands for interactive mode */
66#define I_CHDIR 1
67#define I_CHGRP 2
68#define I_CHMOD 3
69#define I_CHOWN 4
70#define I_GET 5
71#define I_HELP 6
72#define I_LCHDIR 7
73#define I_LLS 8
74#define I_LMKDIR 9
75#define I_LPWD 10
76#define I_LS 11
77#define I_LUMASK 12
78#define I_MKDIR 13
79#define I_PUT 14
80#define I_PWD 15
81#define I_QUIT 16
82#define I_RENAME 17
83#define I_RM 18
84#define I_RMDIR 19
85#define I_SHELL 20
86#define I_SYMLINK 21
87#define I_VERSION 22
88#define I_PROGRESS 23
89
90struct CMD {
91 const char *c;
92 const int n;
93};
94
95static const struct CMD cmds[] = {
96 { "bye", I_QUIT },
97 { "cd", I_CHDIR },
98 { "chdir", I_CHDIR },
99 { "chgrp", I_CHGRP },
100 { "chmod", I_CHMOD },
101 { "chown", I_CHOWN },
102 { "dir", I_LS },
103 { "exit", I_QUIT },
104 { "get", I_GET },
105 { "mget", I_GET },
106 { "help", I_HELP },
107 { "lcd", I_LCHDIR },
108 { "lchdir", I_LCHDIR },
109 { "lls", I_LLS },
110 { "lmkdir", I_LMKDIR },
111 { "ln", I_SYMLINK },
112 { "lpwd", I_LPWD },
113 { "ls", I_LS },
114 { "lumask", I_LUMASK },
115 { "mkdir", I_MKDIR },
116 { "progress", I_PROGRESS },
117 { "put", I_PUT },
118 { "mput", I_PUT },
119 { "pwd", I_PWD },
120 { "quit", I_QUIT },
121 { "rename", I_RENAME },
122 { "rm", I_RM },
123 { "rmdir", I_RMDIR },
124 { "symlink", I_SYMLINK },
125 { "version", I_VERSION },
126 { "!", I_SHELL },
127 { "?", I_HELP },
128 { NULL, -1}
129};
130
131int interactive_loop(int fd_in, int fd_out, char *file1, char *file2);
132
133static void
134help(void)
135{
136 printf("Available commands:\n");
137 printf("cd path Change remote directory to 'path'\n");
138 printf("lcd path Change local directory to 'path'\n");
139 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
140 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
141 printf("chown own path Change owner of file 'path' to 'own'\n");
142 printf("help Display this help text\n");
143 printf("get remote-path [local-path] Download file\n");
144 printf("lls [ls-options [path]] Display local directory listing\n");
145 printf("ln oldpath newpath Symlink remote file\n");
146 printf("lmkdir path Create local directory\n");
147 printf("lpwd Print local working directory\n");
148 printf("ls [path] Display remote directory listing\n");
149 printf("lumask umask Set local umask to 'umask'\n");
150 printf("mkdir path Create remote directory\n");
151 printf("progress Toggle display of progress meter\n");
152 printf("put local-path [remote-path] Upload file\n");
153 printf("pwd Display remote working directory\n");
154 printf("exit Quit sftp\n");
155 printf("quit Quit sftp\n");
156 printf("rename oldpath newpath Rename remote file\n");
157 printf("rmdir path Remove remote directory\n");
158 printf("rm path Delete remote file\n");
159 printf("symlink oldpath newpath Symlink remote file\n");
160 printf("version Show SFTP version\n");
161 printf("!command Execute 'command' in local shell\n");
162 printf("! Escape to local shell\n");
163 printf("? Synonym for help\n");
164}
165
166static void
167local_do_shell(const char *args)
168{
169 int status;
170 char *shell;
171 pid_t pid;
172
173 if (!*args)
174 args = NULL;
175
176 if ((shell = getenv("SHELL")) == NULL)
177 shell = _PATH_BSHELL;
178
179 if ((pid = fork()) == -1)
180 fatal("Couldn't fork: %s", strerror(errno));
181
182 if (pid == 0) {
183 /* XXX: child has pipe fds to ssh subproc open - issue? */
184 if (args) {
185 debug3("Executing %s -c \"%s\"", shell, args);
186 execl(shell, shell, "-c", args, (char *)NULL);
187 } else {
188 debug3("Executing %s", shell);
189 execl(shell, shell, (char *)NULL);
190 }
191 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
192 strerror(errno));
193 _exit(1);
194 }
195 while (waitpid(pid, &status, 0) == -1)
196 if (errno != EINTR)
197 fatal("Couldn't wait for child: %s", strerror(errno));
198 if (!WIFEXITED(status))
199 error("Shell exited abormally");
200 else if (WEXITSTATUS(status))
201 error("Shell exited with status %d", WEXITSTATUS(status));
202}
203
204static void
205local_do_ls(const char *args)
206{
207 if (!args || !*args)
208 local_do_shell(_PATH_LS);
209 else {
210 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
211 char *buf = xmalloc(len);
212
213 /* XXX: quoting - rip quoting code from ftp? */
214 snprintf(buf, len, _PATH_LS " %s", args);
215 local_do_shell(buf);
216 xfree(buf);
217 }
218}
219
220/* Strip one path (usually the pwd) from the start of another */
221static char *
222path_strip(char *path, char *strip)
223{
224 size_t len;
44 225
45extern int showprogress; 226 if (strip == NULL)
227 return (xstrdup(path));
228
229 len = strlen(strip);
230 if (strip != NULL && strncmp(path, strip, len) == 0) {
231 if (strip[len - 1] != '/' && path[len] == '/')
232 len++;
233 return (xstrdup(path + len));
234 }
235
236 return (xstrdup(path));
237}
238
239static char *
240path_append(char *p1, char *p2)
241{
242 char *ret;
243 int len = strlen(p1) + strlen(p2) + 2;
244
245 ret = xmalloc(len);
246 strlcpy(ret, p1, len);
247 if (p1[strlen(p1) - 1] != '/')
248 strlcat(ret, "/", len);
249 strlcat(ret, p2, len);
250
251 return(ret);
252}
253
254static char *
255make_absolute(char *p, char *pwd)
256{
257 char *abs;
258
259 /* Derelativise */
260 if (p && p[0] != '/') {
261 abs = path_append(pwd, p);
262 xfree(p);
263 return(abs);
264 } else
265 return(p);
266}
267
268static int
269infer_path(const char *p, char **ifp)
270{
271 char *cp;
272
273 cp = strrchr(p, '/');
274 if (cp == NULL) {
275 *ifp = xstrdup(p);
276 return(0);
277 }
278
279 if (!cp[1]) {
280 error("Invalid path");
281 return(-1);
282 }
283
284 *ifp = xstrdup(cp + 1);
285 return(0);
286}
287
288static int
289parse_getput_flags(const char **cpp, int *pflag)
290{
291 const char *cp = *cpp;
292
293 /* Check for flags */
294 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
295 switch (cp[1]) {
296 case 'p':
297 case 'P':
298 *pflag = 1;
299 break;
300 default:
301 error("Invalid flag -%c", cp[1]);
302 return(-1);
303 }
304 cp += 2;
305 *cpp = cp + strspn(cp, WHITESPACE);
306 }
307
308 return(0);
309}
310
311static int
312parse_ls_flags(const char **cpp, int *lflag)
313{
314 const char *cp = *cpp;
315
316 /* Check for flags */
317 if (cp++[0] == '-') {
318 for(; strchr(WHITESPACE, *cp) == NULL; cp++) {
319 switch (*cp) {
320 case 'l':
321 *lflag = LONG_VIEW;
322 break;
323 case '1':
324 *lflag = SHORT_VIEW;
325 break;
326 default:
327 error("Invalid flag -%c", *cp);
328 return(-1);
329 }
330 }
331 *cpp = cp + strspn(cp, WHITESPACE);
332 }
333
334 return(0);
335}
336
337static int
338get_pathname(const char **cpp, char **path)
339{
340 const char *cp = *cpp, *end;
341 char quot;
342 int i, j;
343
344 cp += strspn(cp, WHITESPACE);
345 if (!*cp) {
346 *cpp = cp;
347 *path = NULL;
348 return (0);
349 }
350
351 *path = xmalloc(strlen(cp) + 1);
352
353 /* Check for quoted filenames */
354 if (*cp == '\"' || *cp == '\'') {
355 quot = *cp++;
356
357 /* Search for terminating quote, unescape some chars */
358 for (i = j = 0; i <= strlen(cp); i++) {
359 if (cp[i] == quot) { /* Found quote */
360 i++;
361 (*path)[j] = '\0';
362 break;
363 }
364 if (cp[i] == '\0') { /* End of string */
365 error("Unterminated quote");
366 goto fail;
367 }
368 if (cp[i] == '\\') { /* Escaped characters */
369 i++;
370 if (cp[i] != '\'' && cp[i] != '\"' &&
371 cp[i] != '\\') {
372 error("Bad escaped character '\%c'",
373 cp[i]);
374 goto fail;
375 }
376 }
377 (*path)[j++] = cp[i];
378 }
379
380 if (j == 0) {
381 error("Empty quotes");
382 goto fail;
383 }
384 *cpp = cp + i + strspn(cp + i, WHITESPACE);
385 } else {
386 /* Read to end of filename */
387 end = strpbrk(cp, WHITESPACE);
388 if (end == NULL)
389 end = strchr(cp, '\0');
390 *cpp = end + strspn(end, WHITESPACE);
391
392 memcpy(*path, cp, end - cp);
393 (*path)[end - cp] = '\0';
394 }
395 return (0);
396
397 fail:
398 xfree(*path);
399 *path = NULL;
400 return (-1);
401}
402
403static int
404is_dir(char *path)
405{
406 struct stat sb;
407
408 /* XXX: report errors? */
409 if (stat(path, &sb) == -1)
410 return(0);
411
412 return(sb.st_mode & S_IFDIR);
413}
414
415static int
416is_reg(char *path)
417{
418 struct stat sb;
419
420 if (stat(path, &sb) == -1)
421 fatal("stat %s: %s", path, strerror(errno));
422
423 return(S_ISREG(sb.st_mode));
424}
425
426static int
427remote_is_dir(struct sftp_conn *conn, char *path)
428{
429 Attrib *a;
430
431 /* XXX: report errors? */
432 if ((a = do_stat(conn, path, 1)) == NULL)
433 return(0);
434 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
435 return(0);
436 return(a->perm & S_IFDIR);
437}
438
439static int
440process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
441{
442 char *abs_src = NULL;
443 char *abs_dst = NULL;
444 char *tmp;
445 glob_t g;
446 int err = 0;
447 int i;
448
449 abs_src = xstrdup(src);
450 abs_src = make_absolute(abs_src, pwd);
451
452 memset(&g, 0, sizeof(g));
453 debug3("Looking up %s", abs_src);
454 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
455 error("File \"%s\" not found.", abs_src);
456 err = -1;
457 goto out;
458 }
459
460 /* If multiple matches, dst must be a directory or unspecified */
461 if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
462 error("Multiple files match, but \"%s\" is not a directory",
463 dst);
464 err = -1;
465 goto out;
466 }
467
468 for (i = 0; g.gl_pathv[i]; i++) {
469 if (infer_path(g.gl_pathv[i], &tmp)) {
470 err = -1;
471 goto out;
472 }
473
474 if (g.gl_matchc == 1 && dst) {
475 /* If directory specified, append filename */
476 if (is_dir(dst)) {
477 if (infer_path(g.gl_pathv[0], &tmp)) {
478 err = 1;
479 goto out;
480 }
481 abs_dst = path_append(dst, tmp);
482 xfree(tmp);
483 } else
484 abs_dst = xstrdup(dst);
485 } else if (dst) {
486 abs_dst = path_append(dst, tmp);
487 xfree(tmp);
488 } else
489 abs_dst = tmp;
490
491 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
492 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
493 err = -1;
494 xfree(abs_dst);
495 abs_dst = NULL;
496 }
497
498out:
499 xfree(abs_src);
500 if (abs_dst)
501 xfree(abs_dst);
502 globfree(&g);
503 return(err);
504}
505
506static int
507process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
508{
509 char *tmp_dst = NULL;
510 char *abs_dst = NULL;
511 char *tmp;
512 glob_t g;
513 int err = 0;
514 int i;
515
516 if (dst) {
517 tmp_dst = xstrdup(dst);
518 tmp_dst = make_absolute(tmp_dst, pwd);
519 }
520
521 memset(&g, 0, sizeof(g));
522 debug3("Looking up %s", src);
523 if (glob(src, 0, NULL, &g)) {
524 error("File \"%s\" not found.", src);
525 err = -1;
526 goto out;
527 }
528
529 /* If multiple matches, dst may be directory or unspecified */
530 if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
531 error("Multiple files match, but \"%s\" is not a directory",
532 tmp_dst);
533 err = -1;
534 goto out;
535 }
536
537 for (i = 0; g.gl_pathv[i]; i++) {
538 if (!is_reg(g.gl_pathv[i])) {
539 error("skipping non-regular file %s",
540 g.gl_pathv[i]);
541 continue;
542 }
543 if (infer_path(g.gl_pathv[i], &tmp)) {
544 err = -1;
545 goto out;
546 }
547
548 if (g.gl_matchc == 1 && tmp_dst) {
549 /* If directory specified, append filename */
550 if (remote_is_dir(conn, tmp_dst)) {
551 if (infer_path(g.gl_pathv[0], &tmp)) {
552 err = 1;
553 goto out;
554 }
555 abs_dst = path_append(tmp_dst, tmp);
556 xfree(tmp);
557 } else
558 abs_dst = xstrdup(tmp_dst);
559
560 } else if (tmp_dst) {
561 abs_dst = path_append(tmp_dst, tmp);
562 xfree(tmp);
563 } else
564 abs_dst = make_absolute(tmp, pwd);
565
566 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
567 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
568 err = -1;
569 }
570
571out:
572 if (abs_dst)
573 xfree(abs_dst);
574 if (tmp_dst)
575 xfree(tmp_dst);
576 globfree(&g);
577 return(err);
578}
579
580static int
581sdirent_comp(const void *aa, const void *bb)
582{
583 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
584 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
585
586 return (strcmp(a->filename, b->filename));
587}
588
589/* sftp ls.1 replacement for directories */
590static int
591do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
592{
593 int n, c = 1, colspace = 0, columns = 1;
594 SFTP_DIRENT **d;
595
596 if ((n = do_readdir(conn, path, &d)) != 0)
597 return (n);
598
599 if (!(lflag & SHORT_VIEW)) {
600 int m = 0, width = 80;
601 struct winsize ws;
602 char *tmp;
603
604 /* Count entries for sort and find longest filename */
605 for (n = 0; d[n] != NULL; n++)
606 m = MAX(m, strlen(d[n]->filename));
607
608 /* Add any subpath that also needs to be counted */
609 tmp = path_strip(path, strip_path);
610 m += strlen(tmp);
611 xfree(tmp);
612
613 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
614 width = ws.ws_col;
615
616 columns = width / (m + 2);
617 columns = MAX(columns, 1);
618 colspace = width / columns;
619 colspace = MIN(colspace, width);
620 }
621
622 qsort(d, n, sizeof(*d), sdirent_comp);
623
624 for (n = 0; d[n] != NULL; n++) {
625 char *tmp, *fname;
626
627 tmp = path_append(path, d[n]->filename);
628 fname = path_strip(tmp, strip_path);
629 xfree(tmp);
630
631 if (lflag & LONG_VIEW) {
632 char *lname;
633 struct stat sb;
634
635 memset(&sb, 0, sizeof(sb));
636 attrib_to_stat(&d[n]->a, &sb);
637 lname = ls_file(fname, &sb, 1);
638 printf("%s\n", lname);
639 xfree(lname);
640 } else {
641 printf("%-*s", colspace, fname);
642 if (c >= columns) {
643 printf("\n");
644 c = 1;
645 } else
646 c++;
647 }
648
649 xfree(fname);
650 }
651
652 if (!(lflag & LONG_VIEW) && (c != 1))
653 printf("\n");
654
655 free_sftp_dirents(d);
656 return (0);
657}
658
659/* sftp ls.1 replacement which handles path globs */
660static int
661do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
662 int lflag)
663{
664 glob_t g;
665 int i, c = 1, colspace = 0, columns = 1;
666 Attrib *a;
667
668 memset(&g, 0, sizeof(g));
669
670 if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
671 NULL, &g)) {
672 error("Can't ls: \"%s\" not found", path);
673 return (-1);
674 }
675
676 /*
677 * If the glob returns a single match, which is the same as the
678 * input glob, and it is a directory, then just list its contents
679 */
680 if (g.gl_pathc == 1 &&
681 strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
682 if ((a = do_lstat(conn, path, 1)) == NULL) {
683 globfree(&g);
684 return (-1);
685 }
686 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
687 S_ISDIR(a->perm)) {
688 globfree(&g);
689 return (do_ls_dir(conn, path, strip_path, lflag));
690 }
691 }
692
693 if (!(lflag & SHORT_VIEW)) {
694 int m = 0, width = 80;
695 struct winsize ws;
696
697 /* Count entries for sort and find longest filename */
698 for (i = 0; g.gl_pathv[i]; i++)
699 m = MAX(m, strlen(g.gl_pathv[i]));
700
701 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
702 width = ws.ws_col;
703
704 columns = width / (m + 2);
705 columns = MAX(columns, 1);
706 colspace = width / columns;
707 }
708
709 for (i = 0; g.gl_pathv[i]; i++) {
710 char *fname;
711
712 fname = path_strip(g.gl_pathv[i], strip_path);
713
714 if (lflag & LONG_VIEW) {
715 char *lname;
716 struct stat sb;
717
718 /*
719 * XXX: this is slow - 1 roundtrip per path
720 * A solution to this is to fork glob() and
721 * build a sftp specific version which keeps the
722 * attribs (which currently get thrown away)
723 * that the server returns as well as the filenames.
724 */
725 memset(&sb, 0, sizeof(sb));
726 a = do_lstat(conn, g.gl_pathv[i], 1);
727 if (a != NULL)
728 attrib_to_stat(a, &sb);
729 lname = ls_file(fname, &sb, 1);
730 printf("%s\n", lname);
731 xfree(lname);
732 } else {
733 printf("%-*s", colspace, fname);
734 if (c >= columns) {
735 printf("\n");
736 c = 1;
737 } else
738 c++;
739 }
740 xfree(fname);
741 }
742
743 if (!(lflag & LONG_VIEW) && (c != 1))
744 printf("\n");
745
746 if (g.gl_pathc)
747 globfree(&g);
748
749 return (0);
750}
751
752static int
753parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
754 unsigned long *n_arg, char **path1, char **path2)
755{
756 const char *cmd, *cp = *cpp;
757 char *cp2;
758 int base = 0;
759 long l;
760 int i, cmdnum;
761
762 /* Skip leading whitespace */
763 cp = cp + strspn(cp, WHITESPACE);
764
765 /* Ignore blank lines and lines which begin with comment '#' char */
766 if (*cp == '\0' || *cp == '#')
767 return (0);
768
769 /* Check for leading '-' (disable error processing) */
770 *iflag = 0;
771 if (*cp == '-') {
772 *iflag = 1;
773 cp++;
774 }
775
776 /* Figure out which command we have */
777 for (i = 0; cmds[i].c; i++) {
778 int cmdlen = strlen(cmds[i].c);
779
780 /* Check for command followed by whitespace */
781 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
782 strchr(WHITESPACE, cp[cmdlen])) {
783 cp += cmdlen;
784 cp = cp + strspn(cp, WHITESPACE);
785 break;
786 }
787 }
788 cmdnum = cmds[i].n;
789 cmd = cmds[i].c;
790
791 /* Special case */
792 if (*cp == '!') {
793 cp++;
794 cmdnum = I_SHELL;
795 } else if (cmdnum == -1) {
796 error("Invalid command.");
797 return (-1);
798 }
799
800 /* Get arguments and parse flags */
801 *lflag = *pflag = *n_arg = 0;
802 *path1 = *path2 = NULL;
803 switch (cmdnum) {
804 case I_GET:
805 case I_PUT:
806 if (parse_getput_flags(&cp, pflag))
807 return(-1);
808 /* Get first pathname (mandatory) */
809 if (get_pathname(&cp, path1))
810 return(-1);
811 if (*path1 == NULL) {
812 error("You must specify at least one path after a "
813 "%s command.", cmd);
814 return(-1);
815 }
816 /* Try to get second pathname (optional) */
817 if (get_pathname(&cp, path2))
818 return(-1);
819 break;
820 case I_RENAME:
821 case I_SYMLINK:
822 if (get_pathname(&cp, path1))
823 return(-1);
824 if (get_pathname(&cp, path2))
825 return(-1);
826 if (!*path1 || !*path2) {
827 error("You must specify two paths after a %s "
828 "command.", cmd);
829 return(-1);
830 }
831 break;
832 case I_RM:
833 case I_MKDIR:
834 case I_RMDIR:
835 case I_CHDIR:
836 case I_LCHDIR:
837 case I_LMKDIR:
838 /* Get pathname (mandatory) */
839 if (get_pathname(&cp, path1))
840 return(-1);
841 if (*path1 == NULL) {
842 error("You must specify a path after a %s command.",
843 cmd);
844 return(-1);
845 }
846 break;
847 case I_LS:
848 if (parse_ls_flags(&cp, lflag))
849 return(-1);
850 /* Path is optional */
851 if (get_pathname(&cp, path1))
852 return(-1);
853 break;
854 case I_LLS:
855 case I_SHELL:
856 /* Uses the rest of the line */
857 break;
858 case I_LUMASK:
859 base = 8;
860 case I_CHMOD:
861 base = 8;
862 case I_CHOWN:
863 case I_CHGRP:
864 /* Get numeric arg (mandatory) */
865 l = strtol(cp, &cp2, base);
866 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
867 errno == ERANGE) || l < 0) {
868 error("You must supply a numeric argument "
869 "to the %s command.", cmd);
870 return(-1);
871 }
872 cp = cp2;
873 *n_arg = l;
874 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
875 break;
876 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
877 error("You must supply a numeric argument "
878 "to the %s command.", cmd);
879 return(-1);
880 }
881 cp += strspn(cp, WHITESPACE);
882
883 /* Get pathname (mandatory) */
884 if (get_pathname(&cp, path1))
885 return(-1);
886 if (*path1 == NULL) {
887 error("You must specify a path after a %s command.",
888 cmd);
889 return(-1);
890 }
891 break;
892 case I_QUIT:
893 case I_PWD:
894 case I_LPWD:
895 case I_HELP:
896 case I_VERSION:
897 case I_PROGRESS:
898 break;
899 default:
900 fatal("Command not implemented");
901 }
902
903 *cpp = cp;
904 return(cmdnum);
905}
906
907static int
908parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
909 int err_abort)
910{
911 char *path1, *path2, *tmp;
912 int pflag, lflag, iflag, cmdnum, i;
913 unsigned long n_arg;
914 Attrib a, *aa;
915 char path_buf[MAXPATHLEN];
916 int err = 0;
917 glob_t g;
918
919 path1 = path2 = NULL;
920 cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
921 &path1, &path2);
922
923 if (iflag != 0)
924 err_abort = 0;
925
926 memset(&g, 0, sizeof(g));
927
928 /* Perform command */
929 switch (cmdnum) {
930 case 0:
931 /* Blank line */
932 break;
933 case -1:
934 /* Unrecognized command */
935 err = -1;
936 break;
937 case I_GET:
938 err = process_get(conn, path1, path2, *pwd, pflag);
939 break;
940 case I_PUT:
941 err = process_put(conn, path1, path2, *pwd, pflag);
942 break;
943 case I_RENAME:
944 path1 = make_absolute(path1, *pwd);
945 path2 = make_absolute(path2, *pwd);
946 err = do_rename(conn, path1, path2);
947 break;
948 case I_SYMLINK:
949 path2 = make_absolute(path2, *pwd);
950 err = do_symlink(conn, path1, path2);
951 break;
952 case I_RM:
953 path1 = make_absolute(path1, *pwd);
954 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
955 for (i = 0; g.gl_pathv[i]; i++) {
956 printf("Removing %s\n", g.gl_pathv[i]);
957 err = do_rm(conn, g.gl_pathv[i]);
958 if (err != 0 && err_abort)
959 break;
960 }
961 break;
962 case I_MKDIR:
963 path1 = make_absolute(path1, *pwd);
964 attrib_clear(&a);
965 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
966 a.perm = 0777;
967 err = do_mkdir(conn, path1, &a);
968 break;
969 case I_RMDIR:
970 path1 = make_absolute(path1, *pwd);
971 err = do_rmdir(conn, path1);
972 break;
973 case I_CHDIR:
974 path1 = make_absolute(path1, *pwd);
975 if ((tmp = do_realpath(conn, path1)) == NULL) {
976 err = 1;
977 break;
978 }
979 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
980 xfree(tmp);
981 err = 1;
982 break;
983 }
984 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
985 error("Can't change directory: Can't check target");
986 xfree(tmp);
987 err = 1;
988 break;
989 }
990 if (!S_ISDIR(aa->perm)) {
991 error("Can't change directory: \"%s\" is not "
992 "a directory", tmp);
993 xfree(tmp);
994 err = 1;
995 break;
996 }
997 xfree(*pwd);
998 *pwd = tmp;
999 break;
1000 case I_LS:
1001 if (!path1) {
1002 do_globbed_ls(conn, *pwd, *pwd, lflag);
1003 break;
1004 }
1005
1006 /* Strip pwd off beginning of non-absolute paths */
1007 tmp = NULL;
1008 if (*path1 != '/')
1009 tmp = *pwd;
1010
1011 path1 = make_absolute(path1, *pwd);
1012 err = do_globbed_ls(conn, path1, tmp, lflag);
1013 break;
1014 case I_LCHDIR:
1015 if (chdir(path1) == -1) {
1016 error("Couldn't change local directory to "
1017 "\"%s\": %s", path1, strerror(errno));
1018 err = 1;
1019 }
1020 break;
1021 case I_LMKDIR:
1022 if (mkdir(path1, 0777) == -1) {
1023 error("Couldn't create local directory "
1024 "\"%s\": %s", path1, strerror(errno));
1025 err = 1;
1026 }
1027 break;
1028 case I_LLS:
1029 local_do_ls(cmd);
1030 break;
1031 case I_SHELL:
1032 local_do_shell(cmd);
1033 break;
1034 case I_LUMASK:
1035 umask(n_arg);
1036 printf("Local umask: %03lo\n", n_arg);
1037 break;
1038 case I_CHMOD:
1039 path1 = make_absolute(path1, *pwd);
1040 attrib_clear(&a);
1041 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1042 a.perm = n_arg;
1043 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1044 for (i = 0; g.gl_pathv[i]; i++) {
1045 printf("Changing mode on %s\n", g.gl_pathv[i]);
1046 err = do_setstat(conn, g.gl_pathv[i], &a);
1047 if (err != 0 && err_abort)
1048 break;
1049 }
1050 break;
1051 case I_CHOWN:
1052 case I_CHGRP:
1053 path1 = make_absolute(path1, *pwd);
1054 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1055 for (i = 0; g.gl_pathv[i]; i++) {
1056 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1057 if (err != 0 && err_abort)
1058 break;
1059 else
1060 continue;
1061 }
1062 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1063 error("Can't get current ownership of "
1064 "remote file \"%s\"", g.gl_pathv[i]);
1065 if (err != 0 && err_abort)
1066 break;
1067 else
1068 continue;
1069 }
1070 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1071 if (cmdnum == I_CHOWN) {
1072 printf("Changing owner on %s\n", g.gl_pathv[i]);
1073 aa->uid = n_arg;
1074 } else {
1075 printf("Changing group on %s\n", g.gl_pathv[i]);
1076 aa->gid = n_arg;
1077 }
1078 err = do_setstat(conn, g.gl_pathv[i], aa);
1079 if (err != 0 && err_abort)
1080 break;
1081 }
1082 break;
1083 case I_PWD:
1084 printf("Remote working directory: %s\n", *pwd);
1085 break;
1086 case I_LPWD:
1087 if (!getcwd(path_buf, sizeof(path_buf))) {
1088 error("Couldn't get local cwd: %s", strerror(errno));
1089 err = -1;
1090 break;
1091 }
1092 printf("Local working directory: %s\n", path_buf);
1093 break;
1094 case I_QUIT:
1095 /* Processed below */
1096 break;
1097 case I_HELP:
1098 help();
1099 break;
1100 case I_VERSION:
1101 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1102 break;
1103 case I_PROGRESS:
1104 showprogress = !showprogress;
1105 if (showprogress)
1106 printf("Progress meter enabled\n");
1107 else
1108 printf("Progress meter disabled\n");
1109 break;
1110 default:
1111 fatal("%d is not implemented", cmdnum);
1112 }
1113
1114 if (g.gl_pathc)
1115 globfree(&g);
1116 if (path1)
1117 xfree(path1);
1118 if (path2)
1119 xfree(path2);
1120
1121 /* If an unignored error occurs in batch mode we should abort. */
1122 if (err_abort && err != 0)
1123 return (-1);
1124 else if (cmdnum == I_QUIT)
1125 return (1);
1126
1127 return (0);
1128}
1129
1130int
1131interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1132{
1133 char *pwd;
1134 char *dir = NULL;
1135 char cmd[2048];
1136 struct sftp_conn *conn;
1137 int err;
1138
1139 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1140 if (conn == NULL)
1141 fatal("Couldn't initialise connection to server");
1142
1143 pwd = do_realpath(conn, ".");
1144 if (pwd == NULL)
1145 fatal("Need cwd");
1146
1147 if (file1 != NULL) {
1148 dir = xstrdup(file1);
1149 dir = make_absolute(dir, pwd);
1150
1151 if (remote_is_dir(conn, dir) && file2 == NULL) {
1152 printf("Changing to: %s\n", dir);
1153 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
1154 if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0)
1155 return (-1);
1156 } else {
1157 if (file2 == NULL)
1158 snprintf(cmd, sizeof cmd, "get %s", dir);
1159 else
1160 snprintf(cmd, sizeof cmd, "get %s %s", dir,
1161 file2);
1162
1163 err = parse_dispatch_command(conn, cmd, &pwd, 1);
1164 xfree(dir);
1165 xfree(pwd);
1166 return (err);
1167 }
1168 xfree(dir);
1169 }
1170
1171#if HAVE_SETVBUF
1172 setvbuf(stdout, NULL, _IOLBF, 0);
1173 setvbuf(infile, NULL, _IOLBF, 0);
1174#else
1175 setlinebuf(stdout);
1176 setlinebuf(infile);
1177#endif
1178
1179 err = 0;
1180 for (;;) {
1181 char *cp;
1182
1183 printf("sftp> ");
1184
1185 /* XXX: use libedit */
1186 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1187 printf("\n");
1188 break;
1189 }
1190
1191 if (batchmode) /* Echo command */
1192 printf("%s", cmd);
1193
1194 cp = strrchr(cmd, '\n');
1195 if (cp)
1196 *cp = '\0';
1197
1198 err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
1199 if (err != 0)
1200 break;
1201 }
1202 xfree(pwd);
1203
1204 /* err == 1 signifies normal "quit" exit */
1205 return (err >= 0 ? 0 : -1);
1206}
46 1207
47static void 1208static void
48killchild(int signo) 1209killchild(int signo)
@@ -253,8 +1414,8 @@ main(int argc, char **argv)
253 err = interactive_loop(in, out, file1, file2); 1414 err = interactive_loop(in, out, file1, file2);
254 1415
255#if !defined(USE_PIPES) 1416#if !defined(USE_PIPES)
256 shutdown(in, SHUT_RDWR); 1417 shutdown(in, SHUT_RDWR);
257 shutdown(out, SHUT_RDWR); 1418 shutdown(out, SHUT_RDWR);
258#endif 1419#endif
259 1420
260 close(in); 1421 close(in);