]> andersk Git - openssh.git/blame - sftp-int.c
- markus@cvs.openbsd.org 2002/03/30 18:51:15
[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
0426a3b4 25/* XXX: globbed ls */
61e96248 26/* XXX: recursive operations */
27
28#include "includes.h"
8c38e88b 29RCSID("$OpenBSD: sftp-int.c,v 1.46 2002/03/30 18:51:15 markus Exp $");
2e4fb373 30
61e96248 31#include "buffer.h"
32#include "xmalloc.h"
33#include "log.h"
34#include "pathnames.h"
35
36#include "sftp.h"
37#include "sftp-common.h"
2e4fb373 38#include "sftp-glob.h"
61e96248 39#include "sftp-client.h"
40#include "sftp-int.h"
41
3a7fe5ba 42/* File to read commands from */
43extern FILE *infile;
44
375f867e 45/* Size of buffer used when copying files */
46extern size_t copy_buffer_len;
47
c25d3df7 48/* Number of concurrent outstanding requests */
49extern int num_requests;
50
61e96248 51/* Seperators for interactive commands */
52#define WHITESPACE " \t\r\n"
53
54/* Commands for interactive mode */
55#define I_CHDIR 1
56#define I_CHGRP 2
57#define I_CHMOD 3
58#define I_CHOWN 4
59#define I_GET 5
60#define I_HELP 6
61#define I_LCHDIR 7
62#define I_LLS 8
63#define I_LMKDIR 9
64#define I_LPWD 10
65#define I_LS 11
66#define I_LUMASK 12
67#define I_MKDIR 13
68#define I_PUT 14
69#define I_PWD 15
70#define I_QUIT 16
71#define I_RENAME 17
72#define I_RM 18
73#define I_RMDIR 19
74#define I_SHELL 20
3a7fe5ba 75#define I_SYMLINK 21
85cf5827 76#define I_VERSION 22
61e96248 77
78struct CMD {
61e96248 79 const char *c;
ec2a033a 80 const int n;
61e96248 81};
82
83const struct CMD cmds[] = {
68874d2b 84 { "bye", I_QUIT },
0426a3b4 85 { "cd", I_CHDIR },
86 { "chdir", I_CHDIR },
87 { "chgrp", I_CHGRP },
88 { "chmod", I_CHMOD },
89 { "chown", I_CHOWN },
90 { "dir", I_LS },
91 { "exit", I_QUIT },
92 { "get", I_GET },
45a2e669 93 { "mget", I_GET },
0426a3b4 94 { "help", I_HELP },
95 { "lcd", I_LCHDIR },
96 { "lchdir", I_LCHDIR },
97 { "lls", I_LLS },
98 { "lmkdir", I_LMKDIR },
3a7fe5ba 99 { "ln", I_SYMLINK },
0426a3b4 100 { "lpwd", I_LPWD },
101 { "ls", I_LS },
102 { "lumask", I_LUMASK },
103 { "mkdir", I_MKDIR },
104 { "put", I_PUT },
45a2e669 105 { "mput", I_PUT },
0426a3b4 106 { "pwd", I_PWD },
107 { "quit", I_QUIT },
108 { "rename", I_RENAME },
109 { "rm", I_RM },
110 { "rmdir", I_RMDIR },
3a7fe5ba 111 { "symlink", I_SYMLINK },
85cf5827 112 { "version", I_VERSION },
ec2a033a 113 { "!", I_SHELL },
114 { "?", I_HELP },
115 { NULL, -1}
61e96248 116};
117
396c147e 118static void
61e96248 119help(void)
120{
121 printf("Available commands:\n");
0426a3b4 122 printf("cd path Change remote directory to 'path'\n");
123 printf("lcd path Change local directory to 'path'\n");
124 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
125 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
126 printf("chown own path Change owner of file 'path' to 'own'\n");
127 printf("help Display this help text\n");
128 printf("get remote-path [local-path] Download file\n");
129 printf("lls [ls-options [path]] Display local directory listing\n");
3a7fe5ba 130 printf("ln oldpath newpath Symlink remote file\n");
0426a3b4 131 printf("lmkdir path Create local directory\n");
132 printf("lpwd Print local working directory\n");
133 printf("ls [path] Display remote directory listing\n");
134 printf("lumask umask Set local umask to 'umask'\n");
135 printf("mkdir path Create remote directory\n");
136 printf("put local-path [remote-path] Upload file\n");
137 printf("pwd Display remote working directory\n");
138 printf("exit Quit sftp\n");
139 printf("quit Quit sftp\n");
140 printf("rename oldpath newpath Rename remote file\n");
141 printf("rmdir path Remove remote directory\n");
142 printf("rm path Delete remote file\n");
3a7fe5ba 143 printf("symlink oldpath newpath Symlink remote file\n");
85cf5827 144 printf("version Show SFTP version\n");
61e96248 145 printf("!command Execute 'command' in local shell\n");
146 printf("! Escape to local shell\n");
0426a3b4 147 printf("? Synonym for help\n");
61e96248 148}
149
396c147e 150static void
61e96248 151local_do_shell(const char *args)
152{
ec1f12d3 153 int status;
61e96248 154 char *shell;
155 pid_t pid;
2b87da3b 156
61e96248 157 if (!*args)
158 args = NULL;
2b87da3b 159
61e96248 160 if ((shell = getenv("SHELL")) == NULL)
161 shell = _PATH_BSHELL;
162
163 if ((pid = fork()) == -1)
164 fatal("Couldn't fork: %s", strerror(errno));
165
166 if (pid == 0) {
167 /* XXX: child has pipe fds to ssh subproc open - issue? */
168 if (args) {
169 debug3("Executing %s -c \"%s\"", shell, args);
ed916b28 170 execl(shell, shell, "-c", args, (char *)NULL);
61e96248 171 } else {
172 debug3("Executing %s", shell);
ed916b28 173 execl(shell, shell, (char *)NULL);
61e96248 174 }
2b87da3b 175 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
61e96248 176 strerror(errno));
177 _exit(1);
178 }
8c38e88b 179 while (waitpid(pid, &status, 0) == -1)
180 if (errno != EINTR)
181 fatal("Couldn't wait for child: %s", strerror(errno));
61e96248 182 if (!WIFEXITED(status))
183 error("Shell exited abormally");
184 else if (WEXITSTATUS(status))
185 error("Shell exited with status %d", WEXITSTATUS(status));
186}
187
396c147e 188static void
61e96248 189local_do_ls(const char *args)
190{
191 if (!args || !*args)
0426a3b4 192 local_do_shell(_PATH_LS);
61e96248 193 else {
0426a3b4 194 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
195 char *buf = xmalloc(len);
61e96248 196
197 /* XXX: quoting - rip quoting code from ftp? */
0426a3b4 198 snprintf(buf, len, _PATH_LS " %s", args);
61e96248 199 local_do_shell(buf);
0426a3b4 200 xfree(buf);
61e96248 201 }
202}
203
396c147e 204static char *
d50d9b63 205path_append(char *p1, char *p2)
206{
207 char *ret;
6a8496e4 208 int len = strlen(p1) + strlen(p2) + 2;
d50d9b63 209
6a8496e4 210 ret = xmalloc(len);
211 strlcpy(ret, p1, len);
184eed6a 212 if (strcmp(p1, "/") != 0)
88690211 213 strlcat(ret, "/", len);
6a8496e4 214 strlcat(ret, p2, len);
d50d9b63 215
216 return(ret);
217}
218
396c147e 219static char *
61e96248 220make_absolute(char *p, char *pwd)
221{
d50d9b63 222 char *abs;
61e96248 223
224 /* Derelativise */
225 if (p && p[0] != '/') {
d50d9b63 226 abs = path_append(pwd, p);
61e96248 227 xfree(p);
d50d9b63 228 return(abs);
229 } else
230 return(p);
231}
232
396c147e 233static int
d50d9b63 234infer_path(const char *p, char **ifp)
235{
236 char *cp;
237
238 cp = strrchr(p, '/');
239 if (cp == NULL) {
240 *ifp = xstrdup(p);
241 return(0);
242 }
243
244 if (!cp[1]) {
245 error("Invalid path");
246 return(-1);
61e96248 247 }
248
d50d9b63 249 *ifp = xstrdup(cp + 1);
250 return(0);
61e96248 251}
252
396c147e 253static int
61e96248 254parse_getput_flags(const char **cpp, int *pflag)
255{
256 const char *cp = *cpp;
257
258 /* Check for flags */
259 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
0426a3b4 260 switch (cp[1]) {
58c54a79 261 case 'p':
61e96248 262 case 'P':
263 *pflag = 1;
264 break;
265 default:
58c54a79 266 error("Invalid flag -%c", cp[1]);
61e96248 267 return(-1);
268 }
269 cp += 2;
270 *cpp = cp + strspn(cp, WHITESPACE);
271 }
272
273 return(0);
274}
275
396c147e 276static int
61e96248 277get_pathname(const char **cpp, char **path)
278{
0426a3b4 279 const char *cp = *cpp, *end;
280 char quot;
61e96248 281 int i;
282
283 cp += strspn(cp, WHITESPACE);
284 if (!*cp) {
285 *cpp = cp;
286 *path = NULL;
0426a3b4 287 return (0);
61e96248 288 }
289
290 /* Check for quoted filenames */
291 if (*cp == '\"' || *cp == '\'') {
0426a3b4 292 quot = *cp++;
f55d1b5f 293
0426a3b4 294 end = strchr(cp, quot);
295 if (end == NULL) {
61e96248 296 error("Unterminated quote");
0426a3b4 297 goto fail;
61e96248 298 }
0426a3b4 299 if (cp == end) {
61e96248 300 error("Empty quotes");
0426a3b4 301 goto fail;
61e96248 302 }
0426a3b4 303 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
304 } else {
305 /* Read to end of filename */
306 end = strpbrk(cp, WHITESPACE);
307 if (end == NULL)
308 end = strchr(cp, '\0');
309 *cpp = end + strspn(end, WHITESPACE);
61e96248 310 }
311
0426a3b4 312 i = end - cp;
61e96248 313
314 *path = xmalloc(i + 1);
315 memcpy(*path, cp, i);
316 (*path)[i] = '\0';
61e96248 317 return(0);
0426a3b4 318
319 fail:
320 *path = NULL;
321 return (-1);
61e96248 322}
323
396c147e 324static int
d50d9b63 325is_dir(char *path)
61e96248 326{
d50d9b63 327 struct stat sb;
61e96248 328
d50d9b63 329 /* XXX: report errors? */
330 if (stat(path, &sb) == -1)
61e96248 331 return(0);
d50d9b63 332
333 return(sb.st_mode & S_IFDIR);
334}
335
396c147e 336static int
22e6c827 337remote_is_dir(struct sftp_conn *conn, char *path)
d50d9b63 338{
339 Attrib *a;
340
341 /* XXX: report errors? */
22e6c827 342 if ((a = do_stat(conn, path, 1)) == NULL)
d50d9b63 343 return(0);
344 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
345 return(0);
346 return(a->perm & S_IFDIR);
347}
348
396c147e 349static int
22e6c827 350process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
d50d9b63 351{
352 char *abs_src = NULL;
353 char *abs_dst = NULL;
354 char *tmp;
355 glob_t g;
356 int err = 0;
357 int i;
f55d1b5f 358
d50d9b63 359 abs_src = xstrdup(src);
360 abs_src = make_absolute(abs_src, pwd);
361
f55d1b5f 362 memset(&g, 0, sizeof(g));
d50d9b63 363 debug3("Looking up %s", abs_src);
22e6c827 364 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
d50d9b63 365 error("File \"%s\" not found.", abs_src);
366 err = -1;
367 goto out;
61e96248 368 }
369
d50d9b63 370 /* Only one match, dst may be file, directory or unspecified */
371 if (g.gl_pathv[0] && g.gl_matchc == 1) {
372 if (dst) {
373 /* If directory specified, append filename */
374 if (is_dir(dst)) {
375 if (infer_path(g.gl_pathv[0], &tmp)) {
376 err = 1;
377 goto out;
378 }
379 abs_dst = path_append(dst, tmp);
380 xfree(tmp);
381 } else
382 abs_dst = xstrdup(dst);
383 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
384 err = -1;
385 goto out;
386 }
387 printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
22e6c827 388 err = do_download(conn, g.gl_pathv[0], abs_dst, pflag);
d50d9b63 389 goto out;
61e96248 390 }
391
d50d9b63 392 /* Multiple matches, dst may be directory or unspecified */
393 if (dst && !is_dir(dst)) {
f55d1b5f 394 error("Multiple files match, but \"%s\" is not a directory",
d50d9b63 395 dst);
396 err = -1;
397 goto out;
398 }
f55d1b5f 399
184eed6a 400 for (i = 0; g.gl_pathv[i]; i++) {
d50d9b63 401 if (infer_path(g.gl_pathv[i], &tmp)) {
402 err = -1;
403 goto out;
404 }
405 if (dst) {
406 abs_dst = path_append(dst, tmp);
407 xfree(tmp);
408 } else
409 abs_dst = tmp;
410
411 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
22e6c827 412 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
d50d9b63 413 err = -1;
414 xfree(abs_dst);
415 abs_dst = NULL;
416 }
417
418out:
419 xfree(abs_src);
420 if (abs_dst)
421 xfree(abs_dst);
422 globfree(&g);
423 return(err);
424}
425
396c147e 426static int
22e6c827 427process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
d50d9b63 428{
429 char *tmp_dst = NULL;
430 char *abs_dst = NULL;
431 char *tmp;
432 glob_t g;
433 int err = 0;
434 int i;
435
436 if (dst) {
437 tmp_dst = xstrdup(dst);
438 tmp_dst = make_absolute(tmp_dst, pwd);
439 }
440
f55d1b5f 441 memset(&g, 0, sizeof(g));
d50d9b63 442 debug3("Looking up %s", src);
443 if (glob(src, 0, NULL, &g)) {
444 error("File \"%s\" not found.", src);
445 err = -1;
446 goto out;
447 }
448
449 /* Only one match, dst may be file, directory or unspecified */
450 if (g.gl_pathv[0] && g.gl_matchc == 1) {
451 if (tmp_dst) {
452 /* If directory specified, append filename */
22e6c827 453 if (remote_is_dir(conn, tmp_dst)) {
d50d9b63 454 if (infer_path(g.gl_pathv[0], &tmp)) {
455 err = 1;
456 goto out;
457 }
458 abs_dst = path_append(tmp_dst, tmp);
459 xfree(tmp);
460 } else
461 abs_dst = xstrdup(tmp_dst);
e285053e 462 } else {
463 if (infer_path(g.gl_pathv[0], &abs_dst)) {
464 err = -1;
465 goto out;
466 }
467 abs_dst = make_absolute(abs_dst, pwd);
d50d9b63 468 }
469 printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
22e6c827 470 err = do_upload(conn, g.gl_pathv[0], abs_dst, pflag);
d50d9b63 471 goto out;
472 }
473
474 /* Multiple matches, dst may be directory or unspecified */
22e6c827 475 if (tmp_dst && !remote_is_dir(conn, tmp_dst)) {
f55d1b5f 476 error("Multiple files match, but \"%s\" is not a directory",
d50d9b63 477 tmp_dst);
478 err = -1;
479 goto out;
480 }
481
184eed6a 482 for (i = 0; g.gl_pathv[i]; i++) {
d50d9b63 483 if (infer_path(g.gl_pathv[i], &tmp)) {
484 err = -1;
485 goto out;
486 }
487 if (tmp_dst) {
488 abs_dst = path_append(tmp_dst, tmp);
489 xfree(tmp);
490 } else
491 abs_dst = make_absolute(tmp, pwd);
492
493 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
22e6c827 494 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
d50d9b63 495 err = -1;
496 }
497
498out:
499 if (abs_dst)
500 xfree(abs_dst);
501 if (tmp_dst)
502 xfree(tmp_dst);
503 return(err);
61e96248 504}
505
396c147e 506static int
61e96248 507parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
508 char **path1, char **path2)
509{
510 const char *cmd, *cp = *cpp;
877546eb 511 char *cp2;
ec2a033a 512 int base = 0;
877546eb 513 long l;
61e96248 514 int i, cmdnum;
515
516 /* Skip leading whitespace */
517 cp = cp + strspn(cp, WHITESPACE);
518
519 /* Ignore blank lines */
520 if (!*cp)
521 return(-1);
522
523 /* Figure out which command we have */
184eed6a 524 for (i = 0; cmds[i].c; i++) {
61e96248 525 int cmdlen = strlen(cmds[i].c);
526
527 /* Check for command followed by whitespace */
528 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
529 strchr(WHITESPACE, cp[cmdlen])) {
530 cp += cmdlen;
531 cp = cp + strspn(cp, WHITESPACE);
532 break;
533 }
534 }
535 cmdnum = cmds[i].n;
536 cmd = cmds[i].c;
537
538 /* Special case */
539 if (*cp == '!') {
540 cp++;
541 cmdnum = I_SHELL;
542 } else if (cmdnum == -1) {
543 error("Invalid command.");
544 return(-1);
545 }
546
547 /* Get arguments and parse flags */
548 *pflag = *n_arg = 0;
549 *path1 = *path2 = NULL;
550 switch (cmdnum) {
551 case I_GET:
552 case I_PUT:
553 if (parse_getput_flags(&cp, pflag))
554 return(-1);
555 /* Get first pathname (mandatory) */
556 if (get_pathname(&cp, path1))
557 return(-1);
558 if (*path1 == NULL) {
559 error("You must specify at least one path after a "
560 "%s command.", cmd);
561 return(-1);
562 }
563 /* Try to get second pathname (optional) */
564 if (get_pathname(&cp, path2))
565 return(-1);
61e96248 566 break;
567 case I_RENAME:
3a7fe5ba 568 case I_SYMLINK:
61e96248 569 if (get_pathname(&cp, path1))
570 return(-1);
571 if (get_pathname(&cp, path2))
572 return(-1);
573 if (!*path1 || !*path2) {
574 error("You must specify two paths after a %s "
575 "command.", cmd);
576 return(-1);
577 }
578 break;
579 case I_RM:
580 case I_MKDIR:
581 case I_RMDIR:
582 case I_CHDIR:
583 case I_LCHDIR:
584 case I_LMKDIR:
585 /* Get pathname (mandatory) */
586 if (get_pathname(&cp, path1))
587 return(-1);
588 if (*path1 == NULL) {
2b87da3b 589 error("You must specify a path after a %s command.",
61e96248 590 cmd);
591 return(-1);
592 }
593 break;
594 case I_LS:
595 /* Path is optional */
596 if (get_pathname(&cp, path1))
597 return(-1);
598 break;
599 case I_LLS:
600 case I_SHELL:
601 /* Uses the rest of the line */
602 break;
603 case I_LUMASK:
877546eb 604 base = 8;
61e96248 605 case I_CHMOD:
ec2a033a 606 base = 8;
61e96248 607 case I_CHOWN:
608 case I_CHGRP:
609 /* Get numeric arg (mandatory) */
877546eb 610 l = strtol(cp, &cp2, base);
611 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
612 errno == ERANGE) || l < 0) {
61e96248 613 error("You must supply a numeric argument "
614 "to the %s command.", cmd);
615 return(-1);
616 }
877546eb 617 cp = cp2;
618 *n_arg = l;
619 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
620 break;
621 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
61e96248 622 error("You must supply a numeric argument "
623 "to the %s command.", cmd);
624 return(-1);
625 }
626 cp += strspn(cp, WHITESPACE);
627
628 /* Get pathname (mandatory) */
629 if (get_pathname(&cp, path1))
630 return(-1);
631 if (*path1 == NULL) {
2b87da3b 632 error("You must specify a path after a %s command.",
61e96248 633 cmd);
634 return(-1);
635 }
636 break;
637 case I_QUIT:
638 case I_PWD:
639 case I_LPWD:
640 case I_HELP:
85cf5827 641 case I_VERSION:
61e96248 642 break;
643 default:
644 fatal("Command not implemented");
645 }
646
647 *cpp = cp;
61e96248 648 return(cmdnum);
649}
650
396c147e 651static int
22e6c827 652parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd)
61e96248 653{
0426a3b4 654 char *path1, *path2, *tmp;
2e4fb373 655 int pflag, cmdnum, i;
61e96248 656 unsigned long n_arg;
657 Attrib a, *aa;
7cc4cf0a 658 char path_buf[MAXPATHLEN];
a5ec8a3d 659 int err = 0;
2e4fb373 660 glob_t g;
61e96248 661
662 path1 = path2 = NULL;
663 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
664
d50d9b63 665 memset(&g, 0, sizeof(g));
666
61e96248 667 /* Perform command */
668 switch (cmdnum) {
669 case -1:
670 break;
671 case I_GET:
22e6c827 672 err = process_get(conn, path1, path2, *pwd, pflag);
61e96248 673 break;
674 case I_PUT:
22e6c827 675 err = process_put(conn, path1, path2, *pwd, pflag);
cd332296 676 break;
677 case I_RENAME:
61e96248 678 path1 = make_absolute(path1, *pwd);
679 path2 = make_absolute(path2, *pwd);
22e6c827 680 err = do_rename(conn, path1, path2);
61e96248 681 break;
3a7fe5ba 682 case I_SYMLINK:
22e6c827 683 path2 = make_absolute(path2, *pwd);
684 err = do_symlink(conn, path1, path2);
3a7fe5ba 685 break;
61e96248 686 case I_RM:
687 path1 = make_absolute(path1, *pwd);
22e6c827 688 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
184eed6a 689 for (i = 0; g.gl_pathv[i]; i++) {
2e4fb373 690 printf("Removing %s\n", g.gl_pathv[i]);
22e6c827 691 if (do_rm(conn, g.gl_pathv[i]) == -1)
2e4fb373 692 err = -1;
693 }
61e96248 694 break;
695 case I_MKDIR:
696 path1 = make_absolute(path1, *pwd);
697 attrib_clear(&a);
698 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
699 a.perm = 0777;
22e6c827 700 err = do_mkdir(conn, path1, &a);
61e96248 701 break;
702 case I_RMDIR:
703 path1 = make_absolute(path1, *pwd);
22e6c827 704 err = do_rmdir(conn, path1);
61e96248 705 break;
706 case I_CHDIR:
707 path1 = make_absolute(path1, *pwd);
22e6c827 708 if ((tmp = do_realpath(conn, path1)) == NULL) {
a5ec8a3d 709 err = 1;
0426a3b4 710 break;
a5ec8a3d 711 }
22e6c827 712 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
0426a3b4 713 xfree(tmp);
a5ec8a3d 714 err = 1;
0426a3b4 715 break;
716 }
717 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
718 error("Can't change directory: Can't check target");
719 xfree(tmp);
a5ec8a3d 720 err = 1;
0426a3b4 721 break;
722 }
723 if (!S_ISDIR(aa->perm)) {
724 error("Can't change directory: \"%s\" is not "
725 "a directory", tmp);
726 xfree(tmp);
a5ec8a3d 727 err = 1;
0426a3b4 728 break;
729 }
61e96248 730 xfree(*pwd);
0426a3b4 731 *pwd = tmp;
61e96248 732 break;
733 case I_LS:
0426a3b4 734 if (!path1) {
22e6c827 735 do_ls(conn, *pwd);
0426a3b4 736 break;
737 }
61e96248 738 path1 = make_absolute(path1, *pwd);
22e6c827 739 if ((tmp = do_realpath(conn, path1)) == NULL)
0426a3b4 740 break;
741 xfree(path1);
742 path1 = tmp;
22e6c827 743 if ((aa = do_stat(conn, path1, 0)) == NULL)
0426a3b4 744 break;
f55d1b5f 745 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
0426a3b4 746 !S_ISDIR(aa->perm)) {
747 error("Can't ls: \"%s\" is not a directory", path1);
748 break;
749 }
22e6c827 750 do_ls(conn, path1);
61e96248 751 break;
752 case I_LCHDIR:
a5ec8a3d 753 if (chdir(path1) == -1) {
61e96248 754 error("Couldn't change local directory to "
755 "\"%s\": %s", path1, strerror(errno));
a5ec8a3d 756 err = 1;
757 }
61e96248 758 break;
759 case I_LMKDIR:
a5ec8a3d 760 if (mkdir(path1, 0777) == -1) {
0426a3b4 761 error("Couldn't create local directory "
61e96248 762 "\"%s\": %s", path1, strerror(errno));
a5ec8a3d 763 err = 1;
764 }
61e96248 765 break;
766 case I_LLS:
767 local_do_ls(cmd);
768 break;
769 case I_SHELL:
770 local_do_shell(cmd);
771 break;
772 case I_LUMASK:
773 umask(n_arg);
877546eb 774 printf("Local umask: %03lo\n", n_arg);
61e96248 775 break;
776 case I_CHMOD:
777 path1 = make_absolute(path1, *pwd);
778 attrib_clear(&a);
779 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
780 a.perm = n_arg;
22e6c827 781 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
184eed6a 782 for (i = 0; g.gl_pathv[i]; i++) {
2e4fb373 783 printf("Changing mode on %s\n", g.gl_pathv[i]);
22e6c827 784 do_setstat(conn, g.gl_pathv[i], &a);
2e4fb373 785 }
ec2a033a 786 break;
61e96248 787 case I_CHOWN:
788 path1 = make_absolute(path1, *pwd);
22e6c827 789 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
184eed6a 790 for (i = 0; g.gl_pathv[i]; i++) {
22e6c827 791 if (!(aa = do_stat(conn, g.gl_pathv[i], 0)))
2e4fb373 792 continue;
793 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
794 error("Can't get current ownership of "
795 "remote file \"%s\"", g.gl_pathv[i]);
796 continue;
797 }
798 printf("Changing owner on %s\n", g.gl_pathv[i]);
799 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
800 aa->uid = n_arg;
22e6c827 801 do_setstat(conn, g.gl_pathv[i], aa);
61e96248 802 }
61e96248 803 break;
804 case I_CHGRP:
805 path1 = make_absolute(path1, *pwd);
22e6c827 806 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
184eed6a 807 for (i = 0; g.gl_pathv[i]; i++) {
22e6c827 808 if (!(aa = do_stat(conn, g.gl_pathv[i], 0)))
2e4fb373 809 continue;
810 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
811 error("Can't get current ownership of "
812 "remote file \"%s\"", g.gl_pathv[i]);
813 continue;
814 }
815 printf("Changing group on %s\n", g.gl_pathv[i]);
816 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
817 aa->gid = n_arg;
22e6c827 818 do_setstat(conn, g.gl_pathv[i], aa);
61e96248 819 }
61e96248 820 break;
821 case I_PWD:
822 printf("Remote working directory: %s\n", *pwd);
823 break;
824 case I_LPWD:
825 if (!getcwd(path_buf, sizeof(path_buf)))
54b974dc 826 error("Couldn't get local cwd: %s",
61e96248 827 strerror(errno));
828 else
829 printf("Local working directory: %s\n",
830 path_buf);
831 break;
832 case I_QUIT:
833 return(-1);
834 case I_HELP:
835 help();
836 break;
85cf5827 837 case I_VERSION:
22e6c827 838 printf("SFTP protocol version %d\n", sftp_proto_version(conn));
85cf5827 839 break;
61e96248 840 default:
841 fatal("%d is not implemented", cmdnum);
842 }
843
d50d9b63 844 if (g.gl_pathc)
845 globfree(&g);
61e96248 846 if (path1)
847 xfree(path1);
848 if (path2)
849 xfree(path2);
a5ec8a3d 850
851 /* If an error occurs in batch mode we should abort. */
852 if (infile != stdin && err > 0)
853 return -1;
854
61e96248 855 return(0);
856}
857
858void
edeeab1e 859interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
61e96248 860{
861 char *pwd;
edeeab1e 862 char *dir = NULL;
61e96248 863 char cmd[2048];
22e6c827 864 struct sftp_conn *conn;
61e96248 865
22e6c827 866 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
867 if (conn == NULL)
3a7fe5ba 868 fatal("Couldn't initialise connection to server");
869
22e6c827 870 pwd = do_realpath(conn, ".");
61e96248 871 if (pwd == NULL)
872 fatal("Need cwd");
873
edeeab1e 874 if (file1 != NULL) {
875 dir = xstrdup(file1);
876 dir = make_absolute(dir, pwd);
877
22e6c827 878 if (remote_is_dir(conn, dir) && file2 == NULL) {
edeeab1e 879 printf("Changing to: %s\n", dir);
880 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
22e6c827 881 parse_dispatch_command(conn, cmd, &pwd);
edeeab1e 882 } else {
883 if (file2 == NULL)
884 snprintf(cmd, sizeof cmd, "get %s", dir);
885 else
886 snprintf(cmd, sizeof cmd, "get %s %s", dir,
887 file2);
888
22e6c827 889 parse_dispatch_command(conn, cmd, &pwd);
702b7dd8 890 xfree(dir);
edeeab1e 891 return;
892 }
702b7dd8 893 xfree(dir);
edeeab1e 894 }
b81c369b 895#if HAVE_SETVBUF
0426a3b4 896 setvbuf(stdout, NULL, _IOLBF, 0);
a5ec8a3d 897 setvbuf(infile, NULL, _IOLBF, 0);
b81c369b 898#else
899 setlinebuf(stdout);
900 setlinebuf(infile);
901#endif
61e96248 902
184eed6a 903 for (;;) {
61e96248 904 char *cp;
905
906 printf("sftp> ");
907
908 /* XXX: use libedit */
a5ec8a3d 909 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
61e96248 910 printf("\n");
911 break;
a5ec8a3d 912 } else if (infile != stdin) /* Bluff typing */
913 printf("%s", cmd);
914
61e96248 915 cp = strrchr(cmd, '\n');
916 if (cp)
917 *cp = '\0';
a5ec8a3d 918
22e6c827 919 if (parse_dispatch_command(conn, cmd, &pwd))
61e96248 920 break;
921 }
922 xfree(pwd);
923}
This page took 1.191089 seconds and 5 git commands to generate.