2 * Copyright (c) 2001 Damien Miller. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
25 /* XXX: finish implementation of all commands */
26 /* XXX: do fnmatch() instead of using raw pathname */
28 /* XXX: recursive operations */
31 RCSID("$OpenBSD: sftp-int.c,v 1.22 2001/02/14 09:46:03 djm Exp $");
36 #include "pathnames.h"
39 #include "sftp-common.h"
40 #include "sftp-client.h"
43 /* Seperators for interactive commands */
44 #define WHITESPACE " \t\r\n"
46 /* Commands for interactive mode */
73 const struct CMD cmds[] = {
84 { "lchdir", I_LCHDIR },
86 { "lmkdir", I_LMKDIR },
89 { "lumask", I_LUMASK },
94 { "rename", I_RENAME },
105 printf("Available commands:\n");
106 printf("cd path Change remote directory to 'path'\n");
107 printf("lcd path Change local directory to 'path'\n");
108 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
109 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
110 printf("chown own path Change owner of file 'path' to 'own'\n");
111 printf("help Display this help text\n");
112 printf("get remote-path [local-path] Download file\n");
113 printf("lls [ls-options [path]] Display local directory listing\n");
114 printf("lmkdir path Create local directory\n");
115 printf("lpwd Print local working directory\n");
116 printf("ls [path] Display remote directory listing\n");
117 printf("lumask umask Set local umask to 'umask'\n");
118 printf("mkdir path Create remote directory\n");
119 printf("put local-path [remote-path] Upload file\n");
120 printf("pwd Display remote working directory\n");
121 printf("exit Quit sftp\n");
122 printf("quit Quit sftp\n");
123 printf("rename oldpath newpath Rename remote file\n");
124 printf("rmdir path Remove remote directory\n");
125 printf("rm path Delete remote file\n");
126 printf("!command Execute 'command' in local shell\n");
127 printf("! Escape to local shell\n");
128 printf("? Synonym for help\n");
132 local_do_shell(const char *args)
141 if ((shell = getenv("SHELL")) == NULL)
142 shell = _PATH_BSHELL;
144 if ((pid = fork()) == -1)
145 fatal("Couldn't fork: %s", strerror(errno));
148 /* XXX: child has pipe fds to ssh subproc open - issue? */
150 debug3("Executing %s -c \"%s\"", shell, args);
151 ret = execl(shell, shell, "-c", args, NULL);
153 debug3("Executing %s", shell);
154 ret = execl(shell, shell, NULL);
156 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
160 if (waitpid(pid, &status, 0) == -1)
161 fatal("Couldn't wait for child: %s", strerror(errno));
162 if (!WIFEXITED(status))
163 error("Shell exited abormally");
164 else if (WEXITSTATUS(status))
165 error("Shell exited with status %d", WEXITSTATUS(status));
169 local_do_ls(const char *args)
172 local_do_shell(_PATH_LS);
174 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
175 char *buf = xmalloc(len);
177 /* XXX: quoting - rip quoting code from ftp? */
178 snprintf(buf, len, _PATH_LS " %s", args);
185 make_absolute(char *p, char *pwd)
190 if (p && p[0] != '/') {
191 snprintf(buf, sizeof(buf), "%s/%s", pwd, p);
200 parse_getput_flags(const char **cpp, int *pflag)
202 const char *cp = *cpp;
204 /* Check for flags */
205 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
212 error("Invalid flag -%c", cp[1]);
216 *cpp = cp + strspn(cp, WHITESPACE);
223 get_pathname(const char **cpp, char **path)
225 const char *cp = *cpp, *end;
229 cp += strspn(cp, WHITESPACE);
236 /* Check for quoted filenames */
237 if (*cp == '\"' || *cp == '\'') {
240 end = strchr(cp, quot);
242 error("Unterminated quote");
246 error("Empty quotes");
249 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
251 /* Read to end of filename */
252 end = strpbrk(cp, WHITESPACE);
254 end = strchr(cp, '\0');
255 *cpp = end + strspn(end, WHITESPACE);
260 *path = xmalloc(i + 1);
261 memcpy(*path, cp, i);
271 infer_path(const char *p, char **ifp)
275 debug("XXX: P = \"%s\"", p);
277 cp = strrchr(p, '/');
284 error("Invalid path");
288 *ifp = xstrdup(cp + 1);
293 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
294 char **path1, char **path2)
296 const char *cmd, *cp = *cpp;
302 /* Skip leading whitespace */
303 cp = cp + strspn(cp, WHITESPACE);
305 /* Ignore blank lines */
309 /* Figure out which command we have */
310 for(i = 0; cmds[i].c; i++) {
311 int cmdlen = strlen(cmds[i].c);
313 /* Check for command followed by whitespace */
314 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
315 strchr(WHITESPACE, cp[cmdlen])) {
317 cp = cp + strspn(cp, WHITESPACE);
328 } else if (cmdnum == -1) {
329 error("Invalid command.");
333 /* Get arguments and parse flags */
335 *path1 = *path2 = NULL;
339 if (parse_getput_flags(&cp, pflag))
341 /* Get first pathname (mandatory) */
342 if (get_pathname(&cp, path1))
344 if (*path1 == NULL) {
345 error("You must specify at least one path after a "
349 /* Try to get second pathname (optional) */
350 if (get_pathname(&cp, path2))
352 /* Otherwise try to guess it from first path */
353 if (*path2 == NULL && infer_path(*path1, path2))
357 /* Get first pathname (mandatory) */
358 if (get_pathname(&cp, path1))
360 if (get_pathname(&cp, path2))
362 if (!*path1 || !*path2) {
363 error("You must specify two paths after a %s "
374 /* Get pathname (mandatory) */
375 if (get_pathname(&cp, path1))
377 if (*path1 == NULL) {
378 error("You must specify a path after a %s command.",
384 /* Path is optional */
385 if (get_pathname(&cp, path1))
390 /* Uses the rest of the line */
398 /* Get numeric arg (mandatory) */
399 l = strtol(cp, &cp2, base);
400 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
401 errno == ERANGE) || l < 0) {
402 error("You must supply a numeric argument "
403 "to the %s command.", cmd);
408 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
410 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
411 error("You must supply a numeric argument "
412 "to the %s command.", cmd);
415 cp += strspn(cp, WHITESPACE);
417 /* Get pathname (mandatory) */
418 if (get_pathname(&cp, path1))
420 if (*path1 == NULL) {
421 error("You must specify a path after a %s command.",
432 fatal("Command not implemented");
440 parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
442 char *path1, *path2, *tmp;
446 char path_buf[PATH_MAX];
448 path1 = path2 = NULL;
449 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
451 /* Perform command */
456 path1 = make_absolute(path1, *pwd);
457 do_download(in, out, path1, path2, pflag);
460 path2 = make_absolute(path2, *pwd);
461 do_upload(in, out, path1, path2, pflag);
464 path1 = make_absolute(path1, *pwd);
465 path2 = make_absolute(path2, *pwd);
466 do_rename(in, out, path1, path2);
469 path1 = make_absolute(path1, *pwd);
470 do_rm(in, out, path1);
473 path1 = make_absolute(path1, *pwd);
475 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
477 do_mkdir(in, out, path1, &a);
480 path1 = make_absolute(path1, *pwd);
481 do_rmdir(in, out, path1);
484 path1 = make_absolute(path1, *pwd);
485 if ((tmp = do_realpath(in, out, path1)) == NULL)
487 if ((aa = do_stat(in, out, tmp)) == NULL) {
491 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
492 error("Can't change directory: Can't check target");
496 if (!S_ISDIR(aa->perm)) {
497 error("Can't change directory: \"%s\" is not "
507 do_ls(in, out, *pwd);
510 path1 = make_absolute(path1, *pwd);
511 if ((tmp = do_realpath(in, out, path1)) == NULL)
515 if ((aa = do_stat(in, out, path1)) == NULL)
517 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
518 !S_ISDIR(aa->perm)) {
519 error("Can't ls: \"%s\" is not a directory", path1);
522 do_ls(in, out, path1);
525 if (chdir(path1) == -1)
526 error("Couldn't change local directory to "
527 "\"%s\": %s", path1, strerror(errno));
530 if (mkdir(path1, 0777) == -1)
531 error("Couldn't create local directory "
532 "\"%s\": %s", path1, strerror(errno));
542 printf("Local umask: %03lo\n", n_arg);
545 path1 = make_absolute(path1, *pwd);
547 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
549 do_setstat(in, out, path1, &a);
552 path1 = make_absolute(path1, *pwd);
553 if (!(aa = do_stat(in, out, path1)))
555 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
556 error("Can't get current ownership of "
557 "remote file \"%s\"", path1);
560 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
562 do_setstat(in, out, path1, aa);
565 path1 = make_absolute(path1, *pwd);
566 if (!(aa = do_stat(in, out, path1)))
568 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
569 error("Can't get current ownership of "
570 "remote file \"%s\"", path1);
573 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
575 do_setstat(in, out, path1, aa);
578 printf("Remote working directory: %s\n", *pwd);
581 if (!getcwd(path_buf, sizeof(path_buf)))
582 error("Couldn't get local cwd: %s\n",
585 printf("Local working directory: %s\n",
594 fatal("%d is not implemented", cmdnum);
605 interactive_loop(int fd_in, int fd_out)
610 pwd = do_realpath(fd_in, fd_out, ".");
614 setvbuf(stdout, NULL, _IOLBF, 0);
615 setvbuf(stdin, NULL, _IOLBF, 0);
622 /* XXX: use libedit */
623 if (fgets(cmd, sizeof(cmd), stdin) == NULL) {
627 cp = strrchr(cmd, '\n');
630 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))