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 */
27 /* XXX: recursive operations */
30 RCSID("$OpenBSD: sftp-int.c,v 1.7 2001/02/05 00:02:32 deraadt Exp $");
35 #include "pathnames.h"
38 #include "sftp-common.h"
39 #include "sftp-client.h"
42 /* Seperators for interactive commands */
43 #define WHITESPACE " \t\r\n"
45 /* Commands for interactive mode */
72 const struct CMD cmds[] = {
82 { "LCHDIR", I_LCHDIR },
84 { "LMKDIR", I_LMKDIR },
87 { "LUMASK", I_LUMASK },
92 { "RENAME", I_RENAME },
103 printf("Available commands:\n");
104 printf("CD path Change remote directory to 'path'\n");
105 printf("LCD path Change local directory to 'path'\n");
106 printf("CHGRP grp path Change group of file 'path' to 'grp'\n");
107 printf("CHMOD mode path Change permissions of file 'path' to 'mode'\n");
108 printf("CHOWN own path Change owner of file 'path' to 'own'\n");
109 printf("HELP Display this help text\n");
110 printf("GET remote-path [local-path] Download file\n");
111 printf("LLS [ls options] [path] Display local directory listing\n");
112 printf("LMKDIR path Create local directory\n");
113 printf("LPWD Print local working directory\n");
114 printf("LS [path] Display remote directory listing\n");
115 printf("LUMASK umask Set local umask to 'umask'\n");
116 printf("MKDIR path Create remote directory\n");
117 printf("PUT local-path [remote-path] Upload file\n");
118 printf("PWD Display remote working directory\n");
119 printf("EXIT Quit sftp\n");
120 printf("QUIT Quit sftp\n");
121 printf("RENAME oldpath newpath Rename remote file\n");
122 printf("RMDIR path Remove remote directory\n");
123 printf("RM path Delete remote file\n");
124 printf("!command Execute 'command' in local shell\n");
125 printf("! Escape to local shell\n");
129 local_do_shell(const char *args)
138 if ((shell = getenv("SHELL")) == NULL)
139 shell = _PATH_BSHELL;
141 if ((pid = fork()) == -1)
142 fatal("Couldn't fork: %s", strerror(errno));
145 /* XXX: child has pipe fds to ssh subproc open - issue? */
147 debug3("Executing %s -c \"%s\"", shell, args);
148 ret = execl(shell, shell, "-c", args, NULL);
150 debug3("Executing %s", shell);
151 ret = execl(shell, shell, NULL);
153 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
157 if (waitpid(pid, &status, 0) == -1)
158 fatal("Couldn't wait for child: %s", strerror(errno));
159 if (!WIFEXITED(status))
160 error("Shell exited abormally");
161 else if (WEXITSTATUS(status))
162 error("Shell exited with status %d", WEXITSTATUS(status));
166 local_do_ls(const char *args)
169 local_do_shell("ls");
171 char *buf = xmalloc(8 + strlen(args) + 1);
173 /* XXX: quoting - rip quoting code from ftp? */
174 sprintf(buf, "/bin/ls %s", args);
180 make_absolute(char *p, char *pwd)
185 if (p && p[0] != '/') {
186 snprintf(buf, sizeof(buf), "%s/%s", pwd, p);
195 parse_getput_flags(const char **cpp, int *pflag)
197 const char *cp = *cpp;
199 /* Check for flags */
200 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
206 error("Invalid flag -%c", *cp);
210 *cpp = cp + strspn(cp, WHITESPACE);
217 get_pathname(const char **cpp, char **path)
219 const char *quot, *cp = *cpp;
222 cp += strspn(cp, WHITESPACE);
229 /* Check for quoted filenames */
230 if (*cp == '\"' || *cp == '\'') {
232 for(i = 0; cp[i] && cp[i] != *quot; i++)
235 error("Unterminated quote");
240 error("Empty quotes");
244 *path = xmalloc(i + 1);
245 memcpy(*path, cp, i);
248 *cpp = cp + strspn(cp, WHITESPACE);
252 /* Read to end of filename */
253 for(i = 0; cp[i] && cp[i] != ' '; i++)
256 *path = xmalloc(i + 1);
257 memcpy(*path, cp, i);
260 *cpp = cp + strspn(cp, WHITESPACE);
266 infer_path(const char *p, char **ifp)
270 debug("XXX: P = \"%s\"", p);
272 cp = strrchr(p, '/');
280 error("Invalid path");
284 *ifp = xstrdup(cp + 1);
289 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
290 char **path1, char **path2)
292 const char *cmd, *cp = *cpp;
296 /* Skip leading whitespace */
297 cp = cp + strspn(cp, WHITESPACE);
299 /* Ignore blank lines */
303 /* Figure out which command we have */
304 for(i = 0; cmds[i].c; i++) {
305 int cmdlen = strlen(cmds[i].c);
307 /* Check for command followed by whitespace */
308 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
309 strchr(WHITESPACE, cp[cmdlen])) {
311 cp = cp + strspn(cp, WHITESPACE);
322 } else if (cmdnum == -1) {
323 error("Invalid command.");
327 /* Get arguments and parse flags */
329 *path1 = *path2 = NULL;
333 if (parse_getput_flags(&cp, pflag))
335 /* Get first pathname (mandatory) */
336 if (get_pathname(&cp, path1))
338 if (*path1 == NULL) {
339 error("You must specify at least one path after a "
343 /* Try to get second pathname (optional) */
344 if (get_pathname(&cp, path2))
346 /* Otherwise try to guess it from first path */
347 if (*path2 == NULL && infer_path(*path1, path2))
351 /* Get first pathname (mandatory) */
352 if (get_pathname(&cp, path1))
354 if (get_pathname(&cp, path2))
356 if (!*path1 || !*path2) {
357 error("You must specify two paths after a %s "
368 /* Get pathname (mandatory) */
369 if (get_pathname(&cp, path1))
371 if (*path1 == NULL) {
372 error("You must specify a path after a %s command.",
378 /* Path is optional */
379 if (get_pathname(&cp, path1))
384 /* Uses the rest of the line */
391 /* Get numeric arg (mandatory) */
392 if (*cp < '0' && *cp > '9') {
393 error("You must supply a numeric argument "
394 "to the %s command.", cmd);
397 *n_arg = strtoul(cp, (char**)&cp, base);
398 if (!*cp || !strchr(WHITESPACE, *cp)) {
399 error("You must supply a numeric argument "
400 "to the %s command.", cmd);
403 cp += strspn(cp, WHITESPACE);
405 /* Get pathname (mandatory) */
406 if (get_pathname(&cp, path1))
408 if (*path1 == NULL) {
409 error("You must specify a path after a %s command.",
420 fatal("Command not implemented");
429 parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
435 char path_buf[PATH_MAX];
437 path1 = path2 = NULL;
438 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
440 /* Perform command */
445 path1 = make_absolute(path1, *pwd);
446 do_download(in, out, path1, path2, pflag);
449 path2 = make_absolute(path2, *pwd);
450 do_upload(in, out, path1, path2, pflag);
453 path1 = make_absolute(path1, *pwd);
454 path2 = make_absolute(path2, *pwd);
455 do_rename(in, out, path1, path2);
458 path1 = make_absolute(path1, *pwd);
459 do_rm(in, out, path1);
462 path1 = make_absolute(path1, *pwd);
464 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
466 do_mkdir(in, out, path1, &a);
469 path1 = make_absolute(path1, *pwd);
470 do_rmdir(in, out, path1);
473 path1 = make_absolute(path1, *pwd);
475 *pwd = do_realpath(in, out, path1);
478 path1 = make_absolute(path1, *pwd);
479 do_ls(in, out, path1?path1:*pwd);
482 if (chdir(path1) == -1)
483 error("Couldn't change local directory to "
484 "\"%s\": %s", path1, strerror(errno));
487 if (mkdir(path1, 0777) == -1)
488 error("Couldn't create local directory to "
489 "\"%s\": %s", path1, strerror(errno));
501 path1 = make_absolute(path1, *pwd);
503 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
505 do_setstat(in, out, path1, &a);
508 path1 = make_absolute(path1, *pwd);
509 aa = do_stat(in, out, path1);
510 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
511 error("Can't get current ownership of "
512 "remote file \"%s\"", path1);
516 do_setstat(in, out, path1, aa);
519 path1 = make_absolute(path1, *pwd);
520 aa = do_stat(in, out, path1);
521 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
522 error("Can't get current ownership of "
523 "remote file \"%s\"", path1);
527 do_setstat(in, out, path1, aa);
530 printf("Remote working directory: %s\n", *pwd);
533 if (!getcwd(path_buf, sizeof(path_buf)))
534 error("Couldn't get local cwd: %s\n",
537 printf("Local working directory: %s\n",
546 fatal("%d is not implemented", cmdnum);
558 interactive_loop(int fd_in, int fd_out)
563 pwd = do_realpath(fd_in, fd_out, ".");
567 setvbuf(stdout, (char *)NULL, _IOLBF, 0);
568 setvbuf(stdin, (char *)NULL, _IOLBF, 0);
575 /* XXX: use libedit */
576 if (fgets(cmd, sizeof(cmd), stdin) == NULL) {
580 cp = strrchr(cmd, '\n');
583 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))