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.20 2001/02/10 00:45:26 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])) {
211 error("Invalid flag -%c", *cp);
215 *cpp = cp + strspn(cp, WHITESPACE);
222 get_pathname(const char **cpp, char **path)
224 const char *cp = *cpp, *end;
228 cp += strspn(cp, WHITESPACE);
235 /* Check for quoted filenames */
236 if (*cp == '\"' || *cp == '\'') {
239 end = strchr(cp, quot);
241 error("Unterminated quote");
245 error("Empty quotes");
248 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
250 /* Read to end of filename */
251 end = strpbrk(cp, WHITESPACE);
253 end = strchr(cp, '\0');
254 *cpp = end + strspn(end, WHITESPACE);
259 *path = xmalloc(i + 1);
260 memcpy(*path, cp, i);
270 infer_path(const char *p, char **ifp)
274 debug("XXX: P = \"%s\"", p);
276 cp = strrchr(p, '/');
283 error("Invalid path");
287 *ifp = xstrdup(cp + 1);
292 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
293 char **path1, char **path2)
295 const char *cmd, *cp = *cpp;
299 /* Skip leading whitespace */
300 cp = cp + strspn(cp, WHITESPACE);
302 /* Ignore blank lines */
306 /* Figure out which command we have */
307 for(i = 0; cmds[i].c; i++) {
308 int cmdlen = strlen(cmds[i].c);
310 /* Check for command followed by whitespace */
311 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
312 strchr(WHITESPACE, cp[cmdlen])) {
314 cp = cp + strspn(cp, WHITESPACE);
325 } else if (cmdnum == -1) {
326 error("Invalid command.");
330 /* Get arguments and parse flags */
332 *path1 = *path2 = NULL;
336 if (parse_getput_flags(&cp, pflag))
338 /* Get first pathname (mandatory) */
339 if (get_pathname(&cp, path1))
341 if (*path1 == NULL) {
342 error("You must specify at least one path after a "
346 /* Try to get second pathname (optional) */
347 if (get_pathname(&cp, path2))
349 /* Otherwise try to guess it from first path */
350 if (*path2 == NULL && infer_path(*path1, path2))
354 /* Get first pathname (mandatory) */
355 if (get_pathname(&cp, path1))
357 if (get_pathname(&cp, path2))
359 if (!*path1 || !*path2) {
360 error("You must specify two paths after a %s "
371 /* Get pathname (mandatory) */
372 if (get_pathname(&cp, path1))
374 if (*path1 == NULL) {
375 error("You must specify a path after a %s command.",
381 /* Path is optional */
382 if (get_pathname(&cp, path1))
387 /* Uses the rest of the line */
394 /* Get numeric arg (mandatory) */
395 if (*cp < '0' && *cp > '9') {
396 error("You must supply a numeric argument "
397 "to the %s command.", cmd);
400 *n_arg = strtoul(cp, (char**)&cp, base);
401 if (!*cp || !strchr(WHITESPACE, *cp)) {
402 error("You must supply a numeric argument "
403 "to the %s command.", cmd);
406 cp += strspn(cp, WHITESPACE);
408 /* Get pathname (mandatory) */
409 if (get_pathname(&cp, path1))
411 if (*path1 == NULL) {
412 error("You must specify a path after a %s command.",
423 fatal("Command not implemented");
431 parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
433 char *path1, *path2, *tmp;
437 char path_buf[PATH_MAX];
439 path1 = path2 = NULL;
440 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
442 /* Perform command */
447 path1 = make_absolute(path1, *pwd);
448 do_download(in, out, path1, path2, pflag);
451 path2 = make_absolute(path2, *pwd);
452 do_upload(in, out, path1, path2, pflag);
455 path1 = make_absolute(path1, *pwd);
456 path2 = make_absolute(path2, *pwd);
457 do_rename(in, out, path1, path2);
460 path1 = make_absolute(path1, *pwd);
461 do_rm(in, out, path1);
464 path1 = make_absolute(path1, *pwd);
466 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
468 do_mkdir(in, out, path1, &a);
471 path1 = make_absolute(path1, *pwd);
472 do_rmdir(in, out, path1);
475 path1 = make_absolute(path1, *pwd);
476 if ((tmp = do_realpath(in, out, path1)) == NULL)
478 if ((aa = do_stat(in, out, tmp)) == NULL) {
482 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
483 error("Can't change directory: Can't check target");
487 if (!S_ISDIR(aa->perm)) {
488 error("Can't change directory: \"%s\" is not "
498 do_ls(in, out, *pwd);
501 path1 = make_absolute(path1, *pwd);
502 if ((tmp = do_realpath(in, out, path1)) == NULL)
506 if ((aa = do_stat(in, out, path1)) == NULL)
508 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
509 !S_ISDIR(aa->perm)) {
510 error("Can't ls: \"%s\" is not a directory", path1);
513 do_ls(in, out, path1);
516 if (chdir(path1) == -1)
517 error("Couldn't change local directory to "
518 "\"%s\": %s", path1, strerror(errno));
521 if (mkdir(path1, 0777) == -1)
522 error("Couldn't create local directory "
523 "\"%s\": %s", path1, strerror(errno));
535 path1 = make_absolute(path1, *pwd);
537 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
539 do_setstat(in, out, path1, &a);
542 path1 = make_absolute(path1, *pwd);
543 if (!(aa = do_stat(in, out, path1)))
545 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
546 error("Can't get current ownership of "
547 "remote file \"%s\"", path1);
550 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
552 do_setstat(in, out, path1, aa);
555 path1 = make_absolute(path1, *pwd);
556 if (!(aa = do_stat(in, out, path1)))
558 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
559 error("Can't get current ownership of "
560 "remote file \"%s\"", path1);
563 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
565 do_setstat(in, out, path1, aa);
568 printf("Remote working directory: %s\n", *pwd);
571 if (!getcwd(path_buf, sizeof(path_buf)))
572 error("Couldn't get local cwd: %s\n",
575 printf("Local working directory: %s\n",
584 fatal("%d is not implemented", cmdnum);
595 interactive_loop(int fd_in, int fd_out)
600 pwd = do_realpath(fd_in, fd_out, ".");
604 setvbuf(stdout, NULL, _IOLBF, 0);
605 setvbuf(stdin, NULL, _IOLBF, 0);
612 /* XXX: use libedit */
613 if (fgets(cmd, sizeof(cmd), stdin) == NULL) {
617 cp = strrchr(cmd, '\n');
620 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))