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.1 2001/02/04 11:11:54 djm 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[] = {
81 { I_LCHDIR, "LCHDIR" },
83 { I_LMKDIR, "LMKDIR" },
86 { I_LUMASK, "LUMASK" },
92 { I_RENAME, "RENAME" },
102 printf("Available commands:\n");
103 printf("CD path Change remote directory to 'path'\n");
104 printf("LCD path Change local directory to 'path'\n");
105 printf("CHGRP grp path Change group of file 'path' to 'grp'\n");
106 printf("CHMOD mode path Change permissions of file 'path' to 'mode'\n");
107 printf("CHOWN own path Change owner of file 'path' to 'own'\n");
108 printf("HELP Display this help text\n");
109 printf("GET remote-path [local-path] Download file\n");
110 printf("LLS [ls options] [path] Display local directory listing\n");
111 printf("LMKDIR path Create local directory\n");
112 printf("LPWD Print local working directory\n");
113 printf("LS [path] Display remote directory listing\n");
114 printf("LUMASK umask Set local umask to 'umask'\n");
115 printf("MKDIR path Create remote directory\n");
116 printf("PUT local-path [remote-path] Upload file\n");
117 printf("PWD Display remote working directory\n");
118 printf("EXIT Quit sftp\n");
119 printf("QUIT Quit sftp\n");
120 printf("RENAME oldpath newpath Rename remote file\n");
121 printf("RMDIR path Remove remote directory\n");
122 printf("RM path Delete remote file\n");
123 printf("!command Execute 'command' in local shell\n");
124 printf("! Escape to local shell\n");
128 local_do_shell(const char *args)
137 if ((shell = getenv("SHELL")) == NULL)
138 shell = _PATH_BSHELL;
140 if ((pid = fork()) == -1)
141 fatal("Couldn't fork: %s", strerror(errno));
144 /* XXX: child has pipe fds to ssh subproc open - issue? */
146 debug3("Executing %s -c \"%s\"", shell, args);
147 ret = execl(shell, shell, "-c", args, NULL);
149 debug3("Executing %s", shell);
150 ret = execl(shell, shell, NULL);
152 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
156 if (waitpid(pid, &status, 0) == -1)
157 fatal("Couldn't wait for child: %s", strerror(errno));
158 if (!WIFEXITED(status))
159 error("Shell exited abormally");
160 else if (WEXITSTATUS(status))
161 error("Shell exited with status %d", WEXITSTATUS(status));
165 local_do_ls(const char *args)
168 local_do_shell("ls");
170 char *buf = xmalloc(8 + strlen(args) + 1);
172 /* XXX: quoting - rip quoting code from ftp? */
173 sprintf(buf, "/bin/ls %s", args);
179 make_absolute(char *p, char *pwd)
184 if (p && p[0] != '/') {
185 snprintf(buf, sizeof(buf), "%s/%s", pwd, p);
194 parse_getput_flags(const char **cpp, int *pflag)
196 const char *cp = *cpp;
198 /* Check for flags */
199 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
205 error("Invalid flag -%c", *cp);
209 *cpp = cp + strspn(cp, WHITESPACE);
216 get_pathname(const char **cpp, char **path)
218 const char *quot, *cp = *cpp;
221 cp += strspn(cp, WHITESPACE);
228 /* Check for quoted filenames */
229 if (*cp == '\"' || *cp == '\'') {
231 for(i = 0; cp[i] && cp[i] != *quot; i++)
234 error("Unterminated quote");
239 error("Empty quotes");
243 *path = xmalloc(i + 1);
244 memcpy(*path, cp, i);
247 *cpp = cp + strspn(cp, WHITESPACE);
251 /* Read to end of filename */
252 for(i = 0; cp[i] && cp[i] != ' '; i++)
255 *path = xmalloc(i + 1);
256 memcpy(*path, cp, i);
259 *cpp = cp + strspn(cp, WHITESPACE);
265 infer_path(const char *p, char **ifp)
269 debug("XXX: P = \"%s\"", p);
271 cp = strrchr(p, '/');
279 error("Invalid path");
283 *ifp = xstrdup(cp + 1);
288 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
289 char **path1, char **path2)
291 const char *cmd, *cp = *cpp;
294 /* Skip leading whitespace */
295 cp = cp + strspn(cp, WHITESPACE);
297 /* Ignore blank lines */
301 /* Figure out which command we have */
302 for(i = 0; cmds[i].c; i++) {
303 int cmdlen = strlen(cmds[i].c);
305 /* Check for command followed by whitespace */
306 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
307 strchr(WHITESPACE, cp[cmdlen])) {
309 cp = cp + strspn(cp, WHITESPACE);
320 } else if (cmdnum == -1) {
321 error("Invalid command.");
325 /* Get arguments and parse flags */
327 *path1 = *path2 = NULL;
331 if (parse_getput_flags(&cp, pflag))
333 /* Get first pathname (mandatory) */
334 if (get_pathname(&cp, path1))
336 if (*path1 == NULL) {
337 error("You must specify at least one path after a "
341 /* Try to get second pathname (optional) */
342 if (get_pathname(&cp, path2))
344 /* Otherwise try to guess it from first path */
345 if (*path2 == NULL && infer_path(*path1, path2))
349 /* Get first pathname (mandatory) */
350 if (get_pathname(&cp, path1))
352 if (get_pathname(&cp, path2))
354 if (!*path1 || !*path2) {
355 error("You must specify two paths after a %s "
366 /* Get pathname (mandatory) */
367 if (get_pathname(&cp, path1))
369 if (*path1 == NULL) {
370 error("You must specify a path after a %s command.",
376 /* Path is optional */
377 if (get_pathname(&cp, path1))
382 /* Uses the rest of the line */
388 /* Get numeric arg (mandatory) */
389 if (*cp < '0' && *cp > '9') {
390 error("You must supply a numeric argument "
391 "to the %s command.", cmd);
394 *n_arg = strtoul(cp, (char**)&cp, 0);
395 if (!*cp || !strchr(WHITESPACE, *cp)) {
396 error("You must supply a numeric argument "
397 "to the %s command.", cmd);
400 cp += strspn(cp, WHITESPACE);
402 /* Get pathname (mandatory) */
403 if (get_pathname(&cp, path1))
405 if (*path1 == NULL) {
406 error("You must specify a path after a %s command.",
417 fatal("Command not implemented");
426 parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
432 char path_buf[PATH_MAX];
434 path1 = path2 = NULL;
435 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
437 /* Perform command */
442 path1 = make_absolute(path1, *pwd);
443 do_download(in, out, path1, path2, pflag);
446 path2 = make_absolute(path2, *pwd);
447 do_upload(in, out, path1, path2, pflag);
450 path1 = make_absolute(path1, *pwd);
451 path2 = make_absolute(path2, *pwd);
452 do_rename(in, out, path1, path2);
455 path1 = make_absolute(path1, *pwd);
456 do_rm(in, out, path1);
459 path1 = make_absolute(path1, *pwd);
461 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
463 do_mkdir(in, out, path1, &a);
466 path1 = make_absolute(path1, *pwd);
467 do_rmdir(in, out, path1);
470 path1 = make_absolute(path1, *pwd);
472 *pwd = do_realpath(in, out, path1);
475 path1 = make_absolute(path1, *pwd);
476 do_ls(in, out, path1?path1:*pwd);
479 if (chdir(path1) == -1)
480 error("Couldn't change local directory to "
481 "\"%s\": %s", path1, strerror(errno));
484 if (mkdir(path1, 0777) == -1)
485 error("Couldn't create local directory to "
486 "\"%s\": %s", path1, strerror(errno));
498 path1 = make_absolute(path1, *pwd);
500 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
502 do_setstat(in, out, path1, &a);
504 path1 = make_absolute(path1, *pwd);
505 aa = do_stat(in, out, path1);
506 if (!aa->flags & SSH2_FILEXFER_ATTR_UIDGID) {
507 error("Can't get current ownership of "
508 "remote file \"%s\"", path1);
512 do_setstat(in, out, path1, aa);
515 path1 = make_absolute(path1, *pwd);
516 aa = do_stat(in, out, path1);
517 if (!aa->flags & SSH2_FILEXFER_ATTR_UIDGID) {
518 error("Can't get current ownership of "
519 "remote file \"%s\"", path1);
523 do_setstat(in, out, path1, aa);
526 printf("Remote working directory: %s\n", *pwd);
529 if (!getcwd(path_buf, sizeof(path_buf)))
530 error("Couldn't get local cwd: %s\n",
533 printf("Local working directory: %s\n",
542 fatal("%d is not implemented", cmdnum);
554 interactive_loop(int fd_in, int fd_out)
559 pwd = do_realpath(fd_in, fd_out, ".");
563 setvbuf(stdout, (char *)NULL, _IOLBF, 0);
564 setvbuf(stdin, (char *)NULL, _IOLBF, 0);
571 /* XXX: use libedit */
572 if (fgets(cmd, sizeof(cmd), stdin) == NULL) {
576 cp = strrchr(cmd, '\n');
579 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))