]> andersk Git - openssh.git/blame - sftp-int.c
- djm@cvs.openbsd.org 2004/01/13 09:25:05
[openssh.git] / sftp-int.c
CommitLineData
61e96248 1/*
22e6c827 2 * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
61e96248 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
61e96248 25/* XXX: recursive operations */
26
27#include "includes.h"
a8b64bb8 28RCSID("$OpenBSD: sftp-int.c,v 1.66 2004/01/13 09:25:05 djm Exp $");
2e4fb373 29
61e96248 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"
2e4fb373 37#include "sftp-glob.h"
61e96248 38#include "sftp-client.h"
39#include "sftp-int.h"
40
3a7fe5ba 41/* File to read commands from */
42extern FILE *infile;
43
a8b64bb8 44/* Are we in batchfile mode? */
45extern int batchmode;
46
375f867e 47/* Size of buffer used when copying files */
48extern size_t copy_buffer_len;
49
c25d3df7 50/* Number of concurrent outstanding requests */
51extern int num_requests;
52
b65c3807 53/* This is set to 0 if the progressmeter is not desired. */
54int showprogress = 1;
55
512d319a 56/* Separators for interactive commands */
61e96248 57#define WHITESPACE " \t\r\n"
58
c44f10c6 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
61e96248 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
3a7fe5ba 84#define I_SYMLINK 21
85cf5827 85#define I_VERSION 22
b65c3807 86#define I_PROGRESS 23
61e96248 87
88struct CMD {
61e96248 89 const char *c;
ec2a033a 90 const int n;
61e96248 91};
92
c1e07c81 93static const struct CMD cmds[] = {
68874d2b 94 { "bye", I_QUIT },
0426a3b4 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 },
45a2e669 103 { "mget", I_GET },
0426a3b4 104 { "help", I_HELP },
105 { "lcd", I_LCHDIR },
106 { "lchdir", I_LCHDIR },
107 { "lls", I_LLS },
108 { "lmkdir", I_LMKDIR },
3a7fe5ba 109 { "ln", I_SYMLINK },
0426a3b4 110 { "lpwd", I_LPWD },
111 { "ls", I_LS },
112 { "lumask", I_LUMASK },
113 { "mkdir", I_MKDIR },
b65c3807 114 { "progress", I_PROGRESS },
0426a3b4 115 { "put", I_PUT },
45a2e669 116 { "mput", I_PUT },
0426a3b4 117 { "pwd", I_PWD },
118 { "quit", I_QUIT },
119 { "rename", I_RENAME },
120 { "rm", I_RM },
121 { "rmdir", I_RMDIR },
3a7fe5ba 122 { "symlink", I_SYMLINK },
85cf5827 123 { "version", I_VERSION },
ec2a033a 124 { "!", I_SHELL },
125 { "?", I_HELP },
126 { NULL, -1}
61e96248 127};
128
396c147e 129static void
61e96248 130help(void)
131{
132 printf("Available commands:\n");
0426a3b4 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");
3a7fe5ba 141 printf("ln oldpath newpath Symlink remote file\n");
0426a3b4 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");
33364f27 147 printf("progress Toggle display of progress meter\n");
0426a3b4 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");
3a7fe5ba 155 printf("symlink oldpath newpath Symlink remote file\n");
85cf5827 156 printf("version Show SFTP version\n");
61e96248 157 printf("!command Execute 'command' in local shell\n");
158 printf("! Escape to local shell\n");
0426a3b4 159 printf("? Synonym for help\n");
61e96248 160}
161
396c147e 162static void
61e96248 163local_do_shell(const char *args)
164{
ec1f12d3 165 int status;
61e96248 166 char *shell;
167 pid_t pid;
2b87da3b 168
61e96248 169 if (!*args)
170 args = NULL;
2b87da3b 171
61e96248 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);
ed916b28 182 execl(shell, shell, "-c", args, (char *)NULL);
61e96248 183 } else {
184 debug3("Executing %s", shell);
ed916b28 185 execl(shell, shell, (char *)NULL);
61e96248 186 }
2b87da3b 187 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
61e96248 188 strerror(errno));
189 _exit(1);
190 }
8c38e88b 191 while (waitpid(pid, &status, 0) == -1)
192 if (errno != EINTR)
193 fatal("Couldn't wait for child: %s", strerror(errno));
61e96248 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
396c147e 200static void
61e96248 201local_do_ls(const char *args)
202{
203 if (!args || !*args)
0426a3b4 204 local_do_shell(_PATH_LS);
61e96248 205 else {
0426a3b4 206 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
207 char *buf = xmalloc(len);
61e96248 208
209 /* XXX: quoting - rip quoting code from ftp? */
0426a3b4 210 snprintf(buf, len, _PATH_LS " %s", args);
61e96248 211 local_do_shell(buf);
0426a3b4 212 xfree(buf);
61e96248 213 }
214}
215
00b3ad3e 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;
00b3ad3e 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
396c147e 235static char *
d50d9b63 236path_append(char *p1, char *p2)
237{
238 char *ret;
6a8496e4 239 int len = strlen(p1) + strlen(p2) + 2;
d50d9b63 240
6a8496e4 241 ret = xmalloc(len);
242 strlcpy(ret, p1, len);
00b3ad3e 243 if (p1[strlen(p1) - 1] != '/')
88690211 244 strlcat(ret, "/", len);
6a8496e4 245 strlcat(ret, p2, len);
d50d9b63 246
247 return(ret);
248}
249
396c147e 250static char *
61e96248 251make_absolute(char *p, char *pwd)
252{
d50d9b63 253 char *abs;
61e96248 254
255 /* Derelativise */
256 if (p && p[0] != '/') {
d50d9b63 257 abs = path_append(pwd, p);
61e96248 258 xfree(p);
d50d9b63 259 return(abs);
260 } else
261 return(p);
262}
263
396c147e 264static int
d50d9b63 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);
61e96248 278 }
279
d50d9b63 280 *ifp = xstrdup(cp + 1);
281 return(0);
61e96248 282}
283
396c147e 284static int
61e96248 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])) {
0426a3b4 291 switch (cp[1]) {
58c54a79 292 case 'p':
61e96248 293 case 'P':
294 *pflag = 1;
295 break;
296 default:
58c54a79 297 error("Invalid flag -%c", cp[1]);
61e96248 298 return(-1);
299 }
300 cp += 2;
301 *cpp = cp + strspn(cp, WHITESPACE);
302 }
303
304 return(0);
305}
306
00b3ad3e 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':
c44f10c6 317 *lflag = LONG_VIEW;
318 break;
319 case '1':
320 *lflag = SHORT_VIEW;
00b3ad3e 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
396c147e 333static int
61e96248 334get_pathname(const char **cpp, char **path)
335{
0426a3b4 336 const char *cp = *cpp, *end;
337 char quot;
b3d04e37 338 int i, j;
61e96248 339
340 cp += strspn(cp, WHITESPACE);
341 if (!*cp) {
342 *cpp = cp;
343 *path = NULL;
0426a3b4 344 return (0);
61e96248 345 }
346
b3d04e37 347 *path = xmalloc(strlen(cp) + 1);
348
61e96248 349 /* Check for quoted filenames */
350 if (*cp == '\"' || *cp == '\'') {
0426a3b4 351 quot = *cp++;
f55d1b5f 352
b3d04e37 353 /* Search for terminating quote, unescape some chars */
354 for (i = j = 0; i <= strlen(cp); i++) {
355 if (cp[i] == quot) { /* Found quote */
7fdf5569 356 i++;
b3d04e37 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++;
aff51935 366 if (cp[i] != '\'' && cp[i] != '\"' &&
b3d04e37 367 cp[i] != '\\') {
368 error("Bad escaped character '\%c'",
369 cp[i]);
370 goto fail;
371 }
372 }
373 (*path)[j++] = cp[i];
61e96248 374 }
b3d04e37 375
376 if (j == 0) {
61e96248 377 error("Empty quotes");
0426a3b4 378 goto fail;
61e96248 379 }
b3d04e37 380 *cpp = cp + i + strspn(cp + i, WHITESPACE);
0426a3b4 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);
61e96248 387
b3d04e37 388 memcpy(*path, cp, end - cp);
389 (*path)[end - cp] = '\0';
390 }
391 return (0);
0426a3b4 392
393 fail:
aff51935 394 xfree(*path);
b6453d99 395 *path = NULL;
0426a3b4 396 return (-1);
61e96248 397}
398
396c147e 399static int
d50d9b63 400is_dir(char *path)
61e96248 401{
d50d9b63 402 struct stat sb;
61e96248 403
d50d9b63 404 /* XXX: report errors? */
405 if (stat(path, &sb) == -1)
61e96248 406 return(0);
d50d9b63 407
408 return(sb.st_mode & S_IFDIR);
409}
410
538840a2 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
396c147e 422static int
22e6c827 423remote_is_dir(struct sftp_conn *conn, char *path)
d50d9b63 424{
425 Attrib *a;
426
427 /* XXX: report errors? */
22e6c827 428 if ((a = do_stat(conn, path, 1)) == NULL)
d50d9b63 429 return(0);
430 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
431 return(0);
432 return(a->perm & S_IFDIR);
433}
434
396c147e 435static int
22e6c827 436process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
d50d9b63 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;
f55d1b5f 444
d50d9b63 445 abs_src = xstrdup(src);
446 abs_src = make_absolute(abs_src, pwd);
447
f55d1b5f 448 memset(&g, 0, sizeof(g));
d50d9b63 449 debug3("Looking up %s", abs_src);
22e6c827 450 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
d50d9b63 451 error("File \"%s\" not found.", abs_src);
452 err = -1;
453 goto out;
61e96248 454 }
455
4a26f5c5 456 /* If multiple matches, dst must be a directory or unspecified */
457 if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
f55d1b5f 458 error("Multiple files match, but \"%s\" is not a directory",
d50d9b63 459 dst);
460 err = -1;
461 goto out;
462 }
f55d1b5f 463
184eed6a 464 for (i = 0; g.gl_pathv[i]; i++) {
d50d9b63 465 if (infer_path(g.gl_pathv[i], &tmp)) {
466 err = -1;
467 goto out;
468 }
4a26f5c5 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) {
d50d9b63 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);
22e6c827 488 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
d50d9b63 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
396c147e 502static int
22e6c827 503process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
d50d9b63 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
f55d1b5f 517 memset(&g, 0, sizeof(g));
d50d9b63 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
4a26f5c5 525 /* If multiple matches, dst may be directory or unspecified */
526 if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
f55d1b5f 527 error("Multiple files match, but \"%s\" is not a directory",
d50d9b63 528 tmp_dst);
529 err = -1;
530 goto out;
531 }
532
184eed6a 533 for (i = 0; g.gl_pathv[i]; i++) {
538840a2 534 if (!is_reg(g.gl_pathv[i])) {
aff51935 535 error("skipping non-regular file %s",
538840a2 536 g.gl_pathv[i]);
537 continue;
538 }
d50d9b63 539 if (infer_path(g.gl_pathv[i], &tmp)) {
540 err = -1;
541 goto out;
542 }
4a26f5c5 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) {
d50d9b63 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);
22e6c827 563 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
d50d9b63 564 err = -1;
565 }
566
567out:
568 if (abs_dst)
569 xfree(abs_dst);
570 if (tmp_dst)
571 xfree(tmp_dst);
0f6a7901 572 globfree(&g);
d50d9b63 573 return(err);
61e96248 574}
575
396c147e 576static int
00b3ad3e 577sdirent_comp(const void *aa, const void *bb)
578{
579 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
580 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
581
b77a87e5 582 return (strcmp(a->filename, b->filename));
00b3ad3e 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{
c44f10c6 589 int n, c = 1, colspace = 0, columns = 1;
00b3ad3e 590 SFTP_DIRENT **d;
591
592 if ((n = do_readdir(conn, path, &d)) != 0)
593 return (n);
594
c44f10c6 595 if (!(lflag & SHORT_VIEW)) {
596 int m = 0, width = 80;
597 struct winsize ws;
598
599 /* Count entries for sort and find longest filename */
600 for (n = 0; d[n] != NULL; n++)
601 m = MAX(m, strlen(d[n]->filename));
602
aff51935 603 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
c44f10c6 604 width = ws.ws_col;
605
606 columns = width / (m + 2);
e68d8348 607 columns = MAX(columns, 1);
c44f10c6 608 colspace = width / columns;
609 }
00b3ad3e 610
611 qsort(d, n, sizeof(*d), sdirent_comp);
612
613 for (n = 0; d[n] != NULL; n++) {
614 char *tmp, *fname;
b77a87e5 615
00b3ad3e 616 tmp = path_append(path, d[n]->filename);
617 fname = path_strip(tmp, strip_path);
618 xfree(tmp);
619
c44f10c6 620 if (lflag & LONG_VIEW) {
00b3ad3e 621 char *lname;
622 struct stat sb;
623
624 memset(&sb, 0, sizeof(sb));
625 attrib_to_stat(&d[n]->a, &sb);
626 lname = ls_file(fname, &sb, 1);
627 printf("%s\n", lname);
628 xfree(lname);
629 } else {
c44f10c6 630 printf("%-*s", colspace, fname);
631 if (c >= columns) {
632 printf("\n");
633 c = 1;
634 } else
635 c++;
00b3ad3e 636 }
b77a87e5 637
00b3ad3e 638 xfree(fname);
639 }
640
c44f10c6 641 if (!(lflag & LONG_VIEW) && (c != 1))
642 printf("\n");
643
00b3ad3e 644 free_sftp_dirents(d);
645 return (0);
646}
647
648/* sftp ls.1 replacement which handles path globs */
649static int
b77a87e5 650do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
00b3ad3e 651 int lflag)
652{
653 glob_t g;
c44f10c6 654 int i, c = 1, colspace = 0, columns = 1;
00b3ad3e 655 Attrib *a;
00b3ad3e 656
657 memset(&g, 0, sizeof(g));
658
b77a87e5 659 if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
00b3ad3e 660 NULL, &g)) {
661 error("Can't ls: \"%s\" not found", path);
662 return (-1);
663 }
664
665 /*
b77a87e5 666 * If the glob returns a single match, which is the same as the
00b3ad3e 667 * input glob, and it is a directory, then just list its contents
668 */
b77a87e5 669 if (g.gl_pathc == 1 &&
00b3ad3e 670 strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
671 if ((a = do_lstat(conn, path, 1)) == NULL) {
672 globfree(&g);
aff51935 673 return (-1);
00b3ad3e 674 }
b77a87e5 675 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
00b3ad3e 676 S_ISDIR(a->perm)) {
677 globfree(&g);
678 return (do_ls_dir(conn, path, strip_path, lflag));
679 }
680 }
681
c44f10c6 682 if (!(lflag & SHORT_VIEW)) {
683 int m = 0, width = 80;
b6453d99 684 struct winsize ws;
c44f10c6 685
686 /* Count entries for sort and find longest filename */
aff51935 687 for (i = 0; g.gl_pathv[i]; i++)
c44f10c6 688 m = MAX(m, strlen(g.gl_pathv[i]));
689
690 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
691 width = ws.ws_col;
692
693 columns = width / (m + 2);
e68d8348 694 columns = MAX(columns, 1);
c44f10c6 695 colspace = width / columns;
696 }
697
00b3ad3e 698 for (i = 0; g.gl_pathv[i]; i++) {
c44f10c6 699 char *fname;
00b3ad3e 700
701 fname = path_strip(g.gl_pathv[i], strip_path);
702
c44f10c6 703 if (lflag & LONG_VIEW) {
704 char *lname;
705 struct stat sb;
706
00b3ad3e 707 /*
708 * XXX: this is slow - 1 roundtrip per path
b77a87e5 709 * A solution to this is to fork glob() and
710 * build a sftp specific version which keeps the
00b3ad3e 711 * attribs (which currently get thrown away)
712 * that the server returns as well as the filenames.
713 */
714 memset(&sb, 0, sizeof(sb));
715 a = do_lstat(conn, g.gl_pathv[i], 1);
716 if (a != NULL)
717 attrib_to_stat(a, &sb);
718 lname = ls_file(fname, &sb, 1);
719 printf("%s\n", lname);
720 xfree(lname);
721 } else {
c44f10c6 722 printf("%-*s", colspace, fname);
723 if (c >= columns) {
724 printf("\n");
725 c = 1;
726 } else
727 c++;
00b3ad3e 728 }
729 xfree(fname);
730 }
731
c44f10c6 732 if (!(lflag & LONG_VIEW) && (c != 1))
733 printf("\n");
734
00b3ad3e 735 if (g.gl_pathc)
736 globfree(&g);
737
738 return (0);
739}
740
741static int
9a36208d 742parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
00b3ad3e 743 unsigned long *n_arg, char **path1, char **path2)
61e96248 744{
745 const char *cmd, *cp = *cpp;
877546eb 746 char *cp2;
ec2a033a 747 int base = 0;
877546eb 748 long l;
61e96248 749 int i, cmdnum;
750
751 /* Skip leading whitespace */
752 cp = cp + strspn(cp, WHITESPACE);
753
9a36208d 754 /* Ignore blank lines and lines which begin with comment '#' char */
755 if (*cp == '\0' || *cp == '#')
756 return (0);
61e96248 757
9a36208d 758 /* Check for leading '-' (disable error processing) */
759 *iflag = 0;
760 if (*cp == '-') {
761 *iflag = 1;
762 cp++;
763 }
b6453d99 764
61e96248 765 /* Figure out which command we have */
184eed6a 766 for (i = 0; cmds[i].c; i++) {
61e96248 767 int cmdlen = strlen(cmds[i].c);
768
769 /* Check for command followed by whitespace */
770 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
771 strchr(WHITESPACE, cp[cmdlen])) {
772 cp += cmdlen;
773 cp = cp + strspn(cp, WHITESPACE);
774 break;
775 }
776 }
777 cmdnum = cmds[i].n;
778 cmd = cmds[i].c;
779
780 /* Special case */
781 if (*cp == '!') {
782 cp++;
783 cmdnum = I_SHELL;
784 } else if (cmdnum == -1) {
785 error("Invalid command.");
9a36208d 786 return (-1);
61e96248 787 }
788
789 /* Get arguments and parse flags */
00b3ad3e 790 *lflag = *pflag = *n_arg = 0;
61e96248 791 *path1 = *path2 = NULL;
792 switch (cmdnum) {
793 case I_GET:
794 case I_PUT:
795 if (parse_getput_flags(&cp, pflag))
796 return(-1);
797 /* Get first pathname (mandatory) */
798 if (get_pathname(&cp, path1))
799 return(-1);
800 if (*path1 == NULL) {
801 error("You must specify at least one path after a "
802 "%s command.", cmd);
803 return(-1);
804 }
805 /* Try to get second pathname (optional) */
806 if (get_pathname(&cp, path2))
807 return(-1);
61e96248 808 break;
809 case I_RENAME:
3a7fe5ba 810 case I_SYMLINK:
61e96248 811 if (get_pathname(&cp, path1))
812 return(-1);
813 if (get_pathname(&cp, path2))
814 return(-1);
815 if (!*path1 || !*path2) {
816 error("You must specify two paths after a %s "
817 "command.", cmd);
818 return(-1);
819 }
820 break;
821 case I_RM:
822 case I_MKDIR:
823 case I_RMDIR:
824 case I_CHDIR:
825 case I_LCHDIR:
826 case I_LMKDIR:
827 /* Get pathname (mandatory) */
828 if (get_pathname(&cp, path1))
829 return(-1);
830 if (*path1 == NULL) {
2b87da3b 831 error("You must specify a path after a %s command.",
61e96248 832 cmd);
833 return(-1);
834 }
835 break;
836 case I_LS:
00b3ad3e 837 if (parse_ls_flags(&cp, lflag))
838 return(-1);
61e96248 839 /* Path is optional */
840 if (get_pathname(&cp, path1))
841 return(-1);
842 break;
843 case I_LLS:
844 case I_SHELL:
845 /* Uses the rest of the line */
846 break;
847 case I_LUMASK:
877546eb 848 base = 8;
61e96248 849 case I_CHMOD:
ec2a033a 850 base = 8;
61e96248 851 case I_CHOWN:
852 case I_CHGRP:
853 /* Get numeric arg (mandatory) */
877546eb 854 l = strtol(cp, &cp2, base);
855 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
856 errno == ERANGE) || l < 0) {
61e96248 857 error("You must supply a numeric argument "
858 "to the %s command.", cmd);
859 return(-1);
860 }
877546eb 861 cp = cp2;
862 *n_arg = l;
863 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
864 break;
865 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
61e96248 866 error("You must supply a numeric argument "
867 "to the %s command.", cmd);
868 return(-1);
869 }
870 cp += strspn(cp, WHITESPACE);
871
872 /* Get pathname (mandatory) */
873 if (get_pathname(&cp, path1))
874 return(-1);
875 if (*path1 == NULL) {
2b87da3b 876 error("You must specify a path after a %s command.",
61e96248 877 cmd);
878 return(-1);
879 }
880 break;
881 case I_QUIT:
882 case I_PWD:
883 case I_LPWD:
884 case I_HELP:
85cf5827 885 case I_VERSION:
b65c3807 886 case I_PROGRESS:
61e96248 887 break;
888 default:
889 fatal("Command not implemented");
890 }
891
892 *cpp = cp;
61e96248 893 return(cmdnum);
894}
895
396c147e 896static int
9a36208d 897parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
898 int err_abort)
61e96248 899{
0426a3b4 900 char *path1, *path2, *tmp;
9a36208d 901 int pflag, lflag, iflag, cmdnum, i;
61e96248 902 unsigned long n_arg;
903 Attrib a, *aa;
7cc4cf0a 904 char path_buf[MAXPATHLEN];
a5ec8a3d 905 int err = 0;
2e4fb373 906 glob_t g;
61e96248 907
908 path1 = path2 = NULL;
9a36208d 909 cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
00b3ad3e 910 &path1, &path2);
61e96248 911
9a36208d 912 if (iflag != 0)
913 err_abort = 0;
914
d50d9b63 915 memset(&g, 0, sizeof(g));
916
61e96248 917 /* Perform command */
918 switch (cmdnum) {
9a36208d 919 case 0:
920 /* Blank line */
921 break;
61e96248 922 case -1:
9a36208d 923 /* Unrecognized command */
924 err = -1;
61e96248 925 break;
926 case I_GET:
22e6c827 927 err = process_get(conn, path1, path2, *pwd, pflag);
61e96248 928 break;
929 case I_PUT:
22e6c827 930 err = process_put(conn, path1, path2, *pwd, pflag);
cd332296 931 break;
932 case I_RENAME:
61e96248 933 path1 = make_absolute(path1, *pwd);
934 path2 = make_absolute(path2, *pwd);
22e6c827 935 err = do_rename(conn, path1, path2);
61e96248 936 break;
3a7fe5ba 937 case I_SYMLINK:
22e6c827 938 path2 = make_absolute(path2, *pwd);
939 err = do_symlink(conn, path1, path2);
3a7fe5ba 940 break;
61e96248 941 case I_RM:
942 path1 = make_absolute(path1, *pwd);
22e6c827 943 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
184eed6a 944 for (i = 0; g.gl_pathv[i]; i++) {
2e4fb373 945 printf("Removing %s\n", g.gl_pathv[i]);
9a36208d 946 err = do_rm(conn, g.gl_pathv[i]);
947 if (err != 0 && err_abort)
948 break;
2e4fb373 949 }
61e96248 950 break;
951 case I_MKDIR:
952 path1 = make_absolute(path1, *pwd);
953 attrib_clear(&a);
954 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
955 a.perm = 0777;
22e6c827 956 err = do_mkdir(conn, path1, &a);
61e96248 957 break;
958 case I_RMDIR:
959 path1 = make_absolute(path1, *pwd);
22e6c827 960 err = do_rmdir(conn, path1);
61e96248 961 break;
962 case I_CHDIR:
963 path1 = make_absolute(path1, *pwd);
22e6c827 964 if ((tmp = do_realpath(conn, path1)) == NULL) {
a5ec8a3d 965 err = 1;
0426a3b4 966 break;
a5ec8a3d 967 }
22e6c827 968 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
0426a3b4 969 xfree(tmp);
a5ec8a3d 970 err = 1;
0426a3b4 971 break;
972 }
973 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
974 error("Can't change directory: Can't check target");
975 xfree(tmp);
a5ec8a3d 976 err = 1;
0426a3b4 977 break;
978 }
979 if (!S_ISDIR(aa->perm)) {
980 error("Can't change directory: \"%s\" is not "
981 "a directory", tmp);
982 xfree(tmp);
a5ec8a3d 983 err = 1;
0426a3b4 984 break;
985 }
61e96248 986 xfree(*pwd);
0426a3b4 987 *pwd = tmp;
61e96248 988 break;
989 case I_LS:
0426a3b4 990 if (!path1) {
00b3ad3e 991 do_globbed_ls(conn, *pwd, *pwd, lflag);
0426a3b4 992 break;
993 }
b77a87e5 994
00b3ad3e 995 /* Strip pwd off beginning of non-absolute paths */
996 tmp = NULL;
997 if (*path1 != '/')
998 tmp = *pwd;
999
61e96248 1000 path1 = make_absolute(path1, *pwd);
9a36208d 1001 err = do_globbed_ls(conn, path1, tmp, lflag);
61e96248 1002 break;
1003 case I_LCHDIR:
a5ec8a3d 1004 if (chdir(path1) == -1) {
61e96248 1005 error("Couldn't change local directory to "
1006 "\"%s\": %s", path1, strerror(errno));
a5ec8a3d 1007 err = 1;
1008 }
61e96248 1009 break;
1010 case I_LMKDIR:
a5ec8a3d 1011 if (mkdir(path1, 0777) == -1) {
0426a3b4 1012 error("Couldn't create local directory "
61e96248 1013 "\"%s\": %s", path1, strerror(errno));
a5ec8a3d 1014 err = 1;
1015 }
61e96248 1016 break;
1017 case I_LLS:
1018 local_do_ls(cmd);
1019 break;
1020 case I_SHELL:
1021 local_do_shell(cmd);
1022 break;
1023 case I_LUMASK:
1024 umask(n_arg);
877546eb 1025 printf("Local umask: %03lo\n", n_arg);
61e96248 1026 break;
1027 case I_CHMOD:
1028 path1 = make_absolute(path1, *pwd);
1029 attrib_clear(&a);
1030 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1031 a.perm = n_arg;
22e6c827 1032 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
184eed6a 1033 for (i = 0; g.gl_pathv[i]; i++) {
2e4fb373 1034 printf("Changing mode on %s\n", g.gl_pathv[i]);
9a36208d 1035 err = do_setstat(conn, g.gl_pathv[i], &a);
1036 if (err != 0 && err_abort)
1037 break;
2e4fb373 1038 }
ec2a033a 1039 break;
61e96248 1040 case I_CHOWN:
61e96248 1041 case I_CHGRP:
1042 path1 = make_absolute(path1, *pwd);
22e6c827 1043 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
184eed6a 1044 for (i = 0; g.gl_pathv[i]; i++) {
9a36208d 1045 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1046 if (err != 0 && err_abort)
1047 break;
1048 else
1049 continue;
1050 }
2e4fb373 1051 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1052 error("Can't get current ownership of "
1053 "remote file \"%s\"", g.gl_pathv[i]);
9a36208d 1054 if (err != 0 && err_abort)
1055 break;
1056 else
1057 continue;
2e4fb373 1058 }
2e4fb373 1059 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
9a36208d 1060 if (cmdnum == I_CHOWN) {
1061 printf("Changing owner on %s\n", g.gl_pathv[i]);
1062 aa->uid = n_arg;
1063 } else {
1064 printf("Changing group on %s\n", g.gl_pathv[i]);
1065 aa->gid = n_arg;
1066 }
1067 err = do_setstat(conn, g.gl_pathv[i], aa);
1068 if (err != 0 && err_abort)
1069 break;
61e96248 1070 }
61e96248 1071 break;
1072 case I_PWD:
1073 printf("Remote working directory: %s\n", *pwd);
1074 break;
1075 case I_LPWD:
9a36208d 1076 if (!getcwd(path_buf, sizeof(path_buf))) {
1077 error("Couldn't get local cwd: %s", strerror(errno));
1078 err = -1;
1079 break;
1080 }
1081 printf("Local working directory: %s\n", path_buf);
61e96248 1082 break;
1083 case I_QUIT:
9a36208d 1084 /* Processed below */
1085 break;
61e96248 1086 case I_HELP:
1087 help();
1088 break;
85cf5827 1089 case I_VERSION:
9906a836 1090 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
85cf5827 1091 break;
b65c3807 1092 case I_PROGRESS:
1093 showprogress = !showprogress;
1094 if (showprogress)
1095 printf("Progress meter enabled\n");
1096 else
1097 printf("Progress meter disabled\n");
1098 break;
61e96248 1099 default:
1100 fatal("%d is not implemented", cmdnum);
1101 }
1102
d50d9b63 1103 if (g.gl_pathc)
1104 globfree(&g);
61e96248 1105 if (path1)
1106 xfree(path1);
1107 if (path2)
1108 xfree(path2);
a5ec8a3d 1109
9a36208d 1110 /* If an unignored error occurs in batch mode we should abort. */
1111 if (err_abort && err != 0)
1112 return (-1);
1113 else if (cmdnum == I_QUIT)
1114 return (1);
a5ec8a3d 1115
9a36208d 1116 return (0);
61e96248 1117}
1118
9a36208d 1119int
edeeab1e 1120interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
61e96248 1121{
1122 char *pwd;
edeeab1e 1123 char *dir = NULL;
61e96248 1124 char cmd[2048];
22e6c827 1125 struct sftp_conn *conn;
9a36208d 1126 int err;
61e96248 1127
22e6c827 1128 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1129 if (conn == NULL)
3a7fe5ba 1130 fatal("Couldn't initialise connection to server");
1131
22e6c827 1132 pwd = do_realpath(conn, ".");
61e96248 1133 if (pwd == NULL)
1134 fatal("Need cwd");
1135
edeeab1e 1136 if (file1 != NULL) {
1137 dir = xstrdup(file1);
1138 dir = make_absolute(dir, pwd);
1139
22e6c827 1140 if (remote_is_dir(conn, dir) && file2 == NULL) {
edeeab1e 1141 printf("Changing to: %s\n", dir);
1142 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
9a36208d 1143 if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0)
1144 return (-1);
edeeab1e 1145 } else {
1146 if (file2 == NULL)
1147 snprintf(cmd, sizeof cmd, "get %s", dir);
1148 else
1149 snprintf(cmd, sizeof cmd, "get %s %s", dir,
1150 file2);
1151
9a36208d 1152 err = parse_dispatch_command(conn, cmd, &pwd, 1);
702b7dd8 1153 xfree(dir);
3e2f2431 1154 xfree(pwd);
9a36208d 1155 return (err);
edeeab1e 1156 }
702b7dd8 1157 xfree(dir);
edeeab1e 1158 }
9a36208d 1159
b81c369b 1160#if HAVE_SETVBUF
0426a3b4 1161 setvbuf(stdout, NULL, _IOLBF, 0);
a5ec8a3d 1162 setvbuf(infile, NULL, _IOLBF, 0);
b81c369b 1163#else
1164 setlinebuf(stdout);
1165 setlinebuf(infile);
1166#endif
61e96248 1167
9a36208d 1168 err = 0;
184eed6a 1169 for (;;) {
61e96248 1170 char *cp;
1171
1172 printf("sftp> ");
1173
1174 /* XXX: use libedit */
a5ec8a3d 1175 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
61e96248 1176 printf("\n");
1177 break;
a8b64bb8 1178 }
1179
1180 if (batchmode) /* Echo command */
a5ec8a3d 1181 printf("%s", cmd);
1182
61e96248 1183 cp = strrchr(cmd, '\n');
1184 if (cp)
1185 *cp = '\0';
a5ec8a3d 1186
a8b64bb8 1187 err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
9a36208d 1188 if (err != 0)
61e96248 1189 break;
1190 }
1191 xfree(pwd);
9a36208d 1192
1193 /* err == 1 signifies normal "quit" exit */
1194 return (err >= 0 ? 0 : -1);
61e96248 1195}
9a36208d 1196
This page took 0.345421 seconds and 5 git commands to generate.