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