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.
26 /* XXX: recursive operations */
29 RCSID("$OpenBSD: sftp-int.c,v 1.40 2001/08/14 09:23:02 markus Exp $");
34 #include "pathnames.h"
37 #include "sftp-common.h"
38 #include "sftp-glob.h"
39 #include "sftp-client.h"
42 /* File to read commands from */
45 /* Version of server we are speaking to */
48 /* Seperators for interactive commands */
49 #define WHITESPACE " \t\r\n"
51 /* Commands for interactive mode */
80 const struct CMD cmds[] = {
93 { "lchdir", I_LCHDIR },
95 { "lmkdir", I_LMKDIR },
99 { "lumask", I_LUMASK },
100 { "mkdir", I_MKDIR },
105 { "rename", I_RENAME },
107 { "rmdir", I_RMDIR },
108 { "symlink", I_SYMLINK },
109 { "version", I_VERSION },
118 printf("Available commands:\n");
119 printf("cd path Change remote directory to 'path'\n");
120 printf("lcd path Change local directory to 'path'\n");
121 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
122 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
123 printf("chown own path Change owner of file 'path' to 'own'\n");
124 printf("help Display this help text\n");
125 printf("get remote-path [local-path] Download file\n");
126 printf("lls [ls-options [path]] Display local directory listing\n");
127 printf("ln oldpath newpath Symlink remote file\n");
128 printf("lmkdir path Create local directory\n");
129 printf("lpwd Print local working directory\n");
130 printf("ls [path] Display remote directory listing\n");
131 printf("lumask umask Set local umask to 'umask'\n");
132 printf("mkdir path Create remote directory\n");
133 printf("put local-path [remote-path] Upload file\n");
134 printf("pwd Display remote working directory\n");
135 printf("exit Quit sftp\n");
136 printf("quit Quit sftp\n");
137 printf("rename oldpath newpath Rename remote file\n");
138 printf("rmdir path Remove remote directory\n");
139 printf("rm path Delete remote file\n");
140 printf("symlink oldpath newpath Symlink remote file\n");
141 printf("version Show SFTP version\n");
142 printf("!command Execute 'command' in local shell\n");
143 printf("! Escape to local shell\n");
144 printf("? Synonym for help\n");
148 local_do_shell(const char *args)
157 if ((shell = getenv("SHELL")) == NULL)
158 shell = _PATH_BSHELL;
160 if ((pid = fork()) == -1)
161 fatal("Couldn't fork: %s", strerror(errno));
164 /* XXX: child has pipe fds to ssh subproc open - issue? */
166 debug3("Executing %s -c \"%s\"", shell, args);
167 execl(shell, shell, "-c", args, (char *)NULL);
169 debug3("Executing %s", shell);
170 execl(shell, shell, (char *)NULL);
172 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
176 if (waitpid(pid, &status, 0) == -1)
177 fatal("Couldn't wait for child: %s", strerror(errno));
178 if (!WIFEXITED(status))
179 error("Shell exited abormally");
180 else if (WEXITSTATUS(status))
181 error("Shell exited with status %d", WEXITSTATUS(status));
185 local_do_ls(const char *args)
188 local_do_shell(_PATH_LS);
190 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
191 char *buf = xmalloc(len);
193 /* XXX: quoting - rip quoting code from ftp? */
194 snprintf(buf, len, _PATH_LS " %s", args);
201 path_append(char *p1, char *p2)
204 int len = strlen(p1) + strlen(p2) + 2;
207 strlcpy(ret, p1, len);
208 if (strcmp(p1, "/") != 0)
209 strlcat(ret, "/", len);
210 strlcat(ret, p2, len);
216 make_absolute(char *p, char *pwd)
221 if (p && p[0] != '/') {
222 abs = path_append(pwd, p);
230 infer_path(const char *p, char **ifp)
234 cp = strrchr(p, '/');
241 error("Invalid path");
245 *ifp = xstrdup(cp + 1);
250 parse_getput_flags(const char **cpp, int *pflag)
252 const char *cp = *cpp;
254 /* Check for flags */
255 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
262 error("Invalid flag -%c", cp[1]);
266 *cpp = cp + strspn(cp, WHITESPACE);
273 get_pathname(const char **cpp, char **path)
275 const char *cp = *cpp, *end;
279 cp += strspn(cp, WHITESPACE);
286 /* Check for quoted filenames */
287 if (*cp == '\"' || *cp == '\'') {
290 end = strchr(cp, quot);
292 error("Unterminated quote");
296 error("Empty quotes");
299 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
301 /* Read to end of filename */
302 end = strpbrk(cp, WHITESPACE);
304 end = strchr(cp, '\0');
305 *cpp = end + strspn(end, WHITESPACE);
310 *path = xmalloc(i + 1);
311 memcpy(*path, cp, i);
325 /* XXX: report errors? */
326 if (stat(path, &sb) == -1)
329 return(sb.st_mode & S_IFDIR);
333 remote_is_dir(int in, int out, char *path)
337 /* XXX: report errors? */
338 if ((a = do_stat(in, out, path, 1)) == NULL)
340 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
342 return(a->perm & S_IFDIR);
346 process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
348 char *abs_src = NULL;
349 char *abs_dst = NULL;
355 abs_src = xstrdup(src);
356 abs_src = make_absolute(abs_src, pwd);
358 memset(&g, 0, sizeof(g));
359 debug3("Looking up %s", abs_src);
360 if (remote_glob(in, out, abs_src, 0, NULL, &g)) {
361 error("File \"%s\" not found.", abs_src);
366 /* Only one match, dst may be file, directory or unspecified */
367 if (g.gl_pathv[0] && g.gl_matchc == 1) {
369 /* If directory specified, append filename */
371 if (infer_path(g.gl_pathv[0], &tmp)) {
375 abs_dst = path_append(dst, tmp);
378 abs_dst = xstrdup(dst);
379 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
383 printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
384 err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag);
388 /* Multiple matches, dst may be directory or unspecified */
389 if (dst && !is_dir(dst)) {
390 error("Multiple files match, but \"%s\" is not a directory",
396 for(i = 0; g.gl_pathv[i]; i++) {
397 if (infer_path(g.gl_pathv[i], &tmp)) {
402 abs_dst = path_append(dst, tmp);
407 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
408 if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
423 process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
425 char *tmp_dst = NULL;
426 char *abs_dst = NULL;
433 tmp_dst = xstrdup(dst);
434 tmp_dst = make_absolute(tmp_dst, pwd);
437 memset(&g, 0, sizeof(g));
438 debug3("Looking up %s", src);
439 if (glob(src, 0, NULL, &g)) {
440 error("File \"%s\" not found.", src);
445 /* Only one match, dst may be file, directory or unspecified */
446 if (g.gl_pathv[0] && g.gl_matchc == 1) {
448 /* If directory specified, append filename */
449 if (remote_is_dir(in, out, tmp_dst)) {
450 if (infer_path(g.gl_pathv[0], &tmp)) {
454 abs_dst = path_append(tmp_dst, tmp);
457 abs_dst = xstrdup(tmp_dst);
459 if (infer_path(g.gl_pathv[0], &abs_dst)) {
463 abs_dst = make_absolute(abs_dst, pwd);
465 printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
466 err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag);
470 /* Multiple matches, dst may be directory or unspecified */
471 if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
472 error("Multiple files match, but \"%s\" is not a directory",
478 for(i = 0; g.gl_pathv[i]; i++) {
479 if (infer_path(g.gl_pathv[i], &tmp)) {
484 abs_dst = path_append(tmp_dst, tmp);
487 abs_dst = make_absolute(tmp, pwd);
489 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
490 if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
503 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
504 char **path1, char **path2)
506 const char *cmd, *cp = *cpp;
512 /* Skip leading whitespace */
513 cp = cp + strspn(cp, WHITESPACE);
515 /* Ignore blank lines */
519 /* Figure out which command we have */
520 for(i = 0; cmds[i].c; i++) {
521 int cmdlen = strlen(cmds[i].c);
523 /* Check for command followed by whitespace */
524 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
525 strchr(WHITESPACE, cp[cmdlen])) {
527 cp = cp + strspn(cp, WHITESPACE);
538 } else if (cmdnum == -1) {
539 error("Invalid command.");
543 /* Get arguments and parse flags */
545 *path1 = *path2 = NULL;
549 if (parse_getput_flags(&cp, pflag))
551 /* Get first pathname (mandatory) */
552 if (get_pathname(&cp, path1))
554 if (*path1 == NULL) {
555 error("You must specify at least one path after a "
559 /* Try to get second pathname (optional) */
560 if (get_pathname(&cp, path2))
565 if (get_pathname(&cp, path1))
567 if (get_pathname(&cp, path2))
569 if (!*path1 || !*path2) {
570 error("You must specify two paths after a %s "
581 /* Get pathname (mandatory) */
582 if (get_pathname(&cp, path1))
584 if (*path1 == NULL) {
585 error("You must specify a path after a %s command.",
591 /* Path is optional */
592 if (get_pathname(&cp, path1))
597 /* Uses the rest of the line */
605 /* Get numeric arg (mandatory) */
606 l = strtol(cp, &cp2, base);
607 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
608 errno == ERANGE) || l < 0) {
609 error("You must supply a numeric argument "
610 "to the %s command.", cmd);
615 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
617 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
618 error("You must supply a numeric argument "
619 "to the %s command.", cmd);
622 cp += strspn(cp, WHITESPACE);
624 /* Get pathname (mandatory) */
625 if (get_pathname(&cp, path1))
627 if (*path1 == NULL) {
628 error("You must specify a path after a %s command.",
640 fatal("Command not implemented");
648 parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
650 char *path1, *path2, *tmp;
651 int pflag, cmdnum, i;
654 char path_buf[MAXPATHLEN];
658 path1 = path2 = NULL;
659 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
661 memset(&g, 0, sizeof(g));
663 /* Perform command */
668 err = process_get(in, out, path1, path2, *pwd, pflag);
671 err = process_put(in, out, path1, path2, *pwd, pflag);
674 path1 = make_absolute(path1, *pwd);
675 path2 = make_absolute(path2, *pwd);
676 err = do_rename(in, out, path1, path2);
680 error("The server (version %d) does not support "
681 "this operation", version);
684 path2 = make_absolute(path2, *pwd);
685 err = do_symlink(in, out, path1, path2);
689 path1 = make_absolute(path1, *pwd);
690 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
691 for(i = 0; g.gl_pathv[i]; i++) {
692 printf("Removing %s\n", g.gl_pathv[i]);
693 if (do_rm(in, out, g.gl_pathv[i]) == -1)
698 path1 = make_absolute(path1, *pwd);
700 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
702 err = do_mkdir(in, out, path1, &a);
705 path1 = make_absolute(path1, *pwd);
706 err = do_rmdir(in, out, path1);
709 path1 = make_absolute(path1, *pwd);
710 if ((tmp = do_realpath(in, out, path1)) == NULL) {
714 if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
719 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
720 error("Can't change directory: Can't check target");
725 if (!S_ISDIR(aa->perm)) {
726 error("Can't change directory: \"%s\" is not "
737 do_ls(in, out, *pwd);
740 path1 = make_absolute(path1, *pwd);
741 if ((tmp = do_realpath(in, out, path1)) == NULL)
745 if ((aa = do_stat(in, out, path1, 0)) == NULL)
747 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
748 !S_ISDIR(aa->perm)) {
749 error("Can't ls: \"%s\" is not a directory", path1);
752 do_ls(in, out, path1);
755 if (chdir(path1) == -1) {
756 error("Couldn't change local directory to "
757 "\"%s\": %s", path1, strerror(errno));
762 if (mkdir(path1, 0777) == -1) {
763 error("Couldn't create local directory "
764 "\"%s\": %s", path1, strerror(errno));
776 printf("Local umask: %03lo\n", n_arg);
779 path1 = make_absolute(path1, *pwd);
781 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
783 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
784 for(i = 0; g.gl_pathv[i]; i++) {
785 printf("Changing mode on %s\n", g.gl_pathv[i]);
786 do_setstat(in, out, g.gl_pathv[i], &a);
790 path1 = make_absolute(path1, *pwd);
791 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
792 for(i = 0; g.gl_pathv[i]; i++) {
793 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
795 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
796 error("Can't get current ownership of "
797 "remote file \"%s\"", g.gl_pathv[i]);
800 printf("Changing owner on %s\n", g.gl_pathv[i]);
801 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
803 do_setstat(in, out, g.gl_pathv[i], aa);
807 path1 = make_absolute(path1, *pwd);
808 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
809 for(i = 0; g.gl_pathv[i]; i++) {
810 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
812 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
813 error("Can't get current ownership of "
814 "remote file \"%s\"", g.gl_pathv[i]);
817 printf("Changing group on %s\n", g.gl_pathv[i]);
818 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
820 do_setstat(in, out, g.gl_pathv[i], aa);
824 printf("Remote working directory: %s\n", *pwd);
827 if (!getcwd(path_buf, sizeof(path_buf)))
828 error("Couldn't get local cwd: %s",
831 printf("Local working directory: %s\n",
840 printf("SFTP protocol version %d\n", version);
843 fatal("%d is not implemented", cmdnum);
853 /* If an error occurs in batch mode we should abort. */
854 if (infile != stdin && err > 0)
861 interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
867 version = do_init(fd_in, fd_out);
869 fatal("Couldn't initialise connection to server");
871 pwd = do_realpath(fd_in, fd_out, ".");
876 dir = xstrdup(file1);
877 dir = make_absolute(dir, pwd);
879 if (remote_is_dir(fd_in, fd_out, dir) && file2 == NULL) {
880 printf("Changing to: %s\n", dir);
881 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
882 parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
885 snprintf(cmd, sizeof cmd, "get %s", dir);
887 snprintf(cmd, sizeof cmd, "get %s %s", dir,
890 parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
895 setvbuf(stdout, NULL, _IOLBF, 0);
896 setvbuf(infile, NULL, _IOLBF, 0);
907 /* XXX: use libedit */
908 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
911 } else if (infile != stdin) /* Bluff typing */
914 cp = strrchr(cmd, '\n');
918 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))