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.39 2001/07/31 12:42:50 jakob 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[] = {
92 { "lchdir", I_LCHDIR },
94 { "lmkdir", I_LMKDIR },
98 { "lumask", I_LUMASK },
104 { "rename", I_RENAME },
106 { "rmdir", I_RMDIR },
107 { "symlink", I_SYMLINK },
108 { "version", I_VERSION },
117 printf("Available commands:\n");
118 printf("cd path Change remote directory to 'path'\n");
119 printf("lcd path Change local directory to 'path'\n");
120 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
121 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
122 printf("chown own path Change owner of file 'path' to 'own'\n");
123 printf("help Display this help text\n");
124 printf("get remote-path [local-path] Download file\n");
125 printf("lls [ls-options [path]] Display local directory listing\n");
126 printf("ln oldpath newpath Symlink remote file\n");
127 printf("lmkdir path Create local directory\n");
128 printf("lpwd Print local working directory\n");
129 printf("ls [path] Display remote directory listing\n");
130 printf("lumask umask Set local umask to 'umask'\n");
131 printf("mkdir path Create remote directory\n");
132 printf("put local-path [remote-path] Upload file\n");
133 printf("pwd Display remote working directory\n");
134 printf("exit Quit sftp\n");
135 printf("quit Quit sftp\n");
136 printf("rename oldpath newpath Rename remote file\n");
137 printf("rmdir path Remove remote directory\n");
138 printf("rm path Delete remote file\n");
139 printf("symlink oldpath newpath Symlink remote file\n");
140 printf("version Show SFTP version\n");
141 printf("!command Execute 'command' in local shell\n");
142 printf("! Escape to local shell\n");
143 printf("? Synonym for help\n");
147 local_do_shell(const char *args)
156 if ((shell = getenv("SHELL")) == NULL)
157 shell = _PATH_BSHELL;
159 if ((pid = fork()) == -1)
160 fatal("Couldn't fork: %s", strerror(errno));
163 /* XXX: child has pipe fds to ssh subproc open - issue? */
165 debug3("Executing %s -c \"%s\"", shell, args);
166 execl(shell, shell, "-c", args, (char *)NULL);
168 debug3("Executing %s", shell);
169 execl(shell, shell, (char *)NULL);
171 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
175 if (waitpid(pid, &status, 0) == -1)
176 fatal("Couldn't wait for child: %s", strerror(errno));
177 if (!WIFEXITED(status))
178 error("Shell exited abormally");
179 else if (WEXITSTATUS(status))
180 error("Shell exited with status %d", WEXITSTATUS(status));
184 local_do_ls(const char *args)
187 local_do_shell(_PATH_LS);
189 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
190 char *buf = xmalloc(len);
192 /* XXX: quoting - rip quoting code from ftp? */
193 snprintf(buf, len, _PATH_LS " %s", args);
200 path_append(char *p1, char *p2)
203 int len = strlen(p1) + strlen(p2) + 2;
206 strlcpy(ret, p1, len);
207 if (strcmp(p1, "/") != 0)
208 strlcat(ret, "/", len);
209 strlcat(ret, p2, len);
215 make_absolute(char *p, char *pwd)
220 if (p && p[0] != '/') {
221 abs = path_append(pwd, p);
229 infer_path(const char *p, char **ifp)
233 cp = strrchr(p, '/');
240 error("Invalid path");
244 *ifp = xstrdup(cp + 1);
249 parse_getput_flags(const char **cpp, int *pflag)
251 const char *cp = *cpp;
253 /* Check for flags */
254 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
261 error("Invalid flag -%c", cp[1]);
265 *cpp = cp + strspn(cp, WHITESPACE);
272 get_pathname(const char **cpp, char **path)
274 const char *cp = *cpp, *end;
278 cp += strspn(cp, WHITESPACE);
285 /* Check for quoted filenames */
286 if (*cp == '\"' || *cp == '\'') {
289 end = strchr(cp, quot);
291 error("Unterminated quote");
295 error("Empty quotes");
298 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
300 /* Read to end of filename */
301 end = strpbrk(cp, WHITESPACE);
303 end = strchr(cp, '\0');
304 *cpp = end + strspn(end, WHITESPACE);
309 *path = xmalloc(i + 1);
310 memcpy(*path, cp, i);
324 /* XXX: report errors? */
325 if (stat(path, &sb) == -1)
328 return(sb.st_mode & S_IFDIR);
332 remote_is_dir(int in, int out, char *path)
336 /* XXX: report errors? */
337 if ((a = do_stat(in, out, path, 1)) == NULL)
339 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
341 return(a->perm & S_IFDIR);
345 process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
347 char *abs_src = NULL;
348 char *abs_dst = NULL;
354 abs_src = xstrdup(src);
355 abs_src = make_absolute(abs_src, pwd);
357 memset(&g, 0, sizeof(g));
358 debug3("Looking up %s", abs_src);
359 if (remote_glob(in, out, abs_src, 0, NULL, &g)) {
360 error("File \"%s\" not found.", abs_src);
365 /* Only one match, dst may be file, directory or unspecified */
366 if (g.gl_pathv[0] && g.gl_matchc == 1) {
368 /* If directory specified, append filename */
370 if (infer_path(g.gl_pathv[0], &tmp)) {
374 abs_dst = path_append(dst, tmp);
377 abs_dst = xstrdup(dst);
378 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
382 printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
383 err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag);
387 /* Multiple matches, dst may be directory or unspecified */
388 if (dst && !is_dir(dst)) {
389 error("Multiple files match, but \"%s\" is not a directory",
395 for(i = 0; g.gl_pathv[i]; i++) {
396 if (infer_path(g.gl_pathv[i], &tmp)) {
401 abs_dst = path_append(dst, tmp);
406 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
407 if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
422 process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
424 char *tmp_dst = NULL;
425 char *abs_dst = NULL;
432 tmp_dst = xstrdup(dst);
433 tmp_dst = make_absolute(tmp_dst, pwd);
436 memset(&g, 0, sizeof(g));
437 debug3("Looking up %s", src);
438 if (glob(src, 0, NULL, &g)) {
439 error("File \"%s\" not found.", src);
444 /* Only one match, dst may be file, directory or unspecified */
445 if (g.gl_pathv[0] && g.gl_matchc == 1) {
447 /* If directory specified, append filename */
448 if (remote_is_dir(in, out, tmp_dst)) {
449 if (infer_path(g.gl_pathv[0], &tmp)) {
453 abs_dst = path_append(tmp_dst, tmp);
456 abs_dst = xstrdup(tmp_dst);
458 if (infer_path(g.gl_pathv[0], &abs_dst)) {
462 abs_dst = make_absolute(abs_dst, pwd);
464 printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
465 err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag);
469 /* Multiple matches, dst may be directory or unspecified */
470 if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
471 error("Multiple files match, but \"%s\" is not a directory",
477 for(i = 0; g.gl_pathv[i]; i++) {
478 if (infer_path(g.gl_pathv[i], &tmp)) {
483 abs_dst = path_append(tmp_dst, tmp);
486 abs_dst = make_absolute(tmp, pwd);
488 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
489 if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
502 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
503 char **path1, char **path2)
505 const char *cmd, *cp = *cpp;
511 /* Skip leading whitespace */
512 cp = cp + strspn(cp, WHITESPACE);
514 /* Ignore blank lines */
518 /* Figure out which command we have */
519 for(i = 0; cmds[i].c; i++) {
520 int cmdlen = strlen(cmds[i].c);
522 /* Check for command followed by whitespace */
523 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
524 strchr(WHITESPACE, cp[cmdlen])) {
526 cp = cp + strspn(cp, WHITESPACE);
537 } else if (cmdnum == -1) {
538 error("Invalid command.");
542 /* Get arguments and parse flags */
544 *path1 = *path2 = NULL;
548 if (parse_getput_flags(&cp, pflag))
550 /* Get first pathname (mandatory) */
551 if (get_pathname(&cp, path1))
553 if (*path1 == NULL) {
554 error("You must specify at least one path after a "
558 /* Try to get second pathname (optional) */
559 if (get_pathname(&cp, path2))
564 if (get_pathname(&cp, path1))
566 if (get_pathname(&cp, path2))
568 if (!*path1 || !*path2) {
569 error("You must specify two paths after a %s "
580 /* Get pathname (mandatory) */
581 if (get_pathname(&cp, path1))
583 if (*path1 == NULL) {
584 error("You must specify a path after a %s command.",
590 /* Path is optional */
591 if (get_pathname(&cp, path1))
596 /* Uses the rest of the line */
604 /* Get numeric arg (mandatory) */
605 l = strtol(cp, &cp2, base);
606 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
607 errno == ERANGE) || l < 0) {
608 error("You must supply a numeric argument "
609 "to the %s command.", cmd);
614 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
616 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
617 error("You must supply a numeric argument "
618 "to the %s command.", cmd);
621 cp += strspn(cp, WHITESPACE);
623 /* Get pathname (mandatory) */
624 if (get_pathname(&cp, path1))
626 if (*path1 == NULL) {
627 error("You must specify a path after a %s command.",
639 fatal("Command not implemented");
647 parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
649 char *path1, *path2, *tmp;
650 int pflag, cmdnum, i;
653 char path_buf[MAXPATHLEN];
657 path1 = path2 = NULL;
658 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
660 memset(&g, 0, sizeof(g));
662 /* Perform command */
667 err = process_get(in, out, path1, path2, *pwd, pflag);
670 err = process_put(in, out, path1, path2, *pwd, pflag);
673 path1 = make_absolute(path1, *pwd);
674 path2 = make_absolute(path2, *pwd);
675 err = do_rename(in, out, path1, path2);
679 error("The server (version %d) does not support "
680 "this operation", version);
683 path2 = make_absolute(path2, *pwd);
684 err = do_symlink(in, out, path1, path2);
688 path1 = make_absolute(path1, *pwd);
689 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
690 for(i = 0; g.gl_pathv[i]; i++) {
691 printf("Removing %s\n", g.gl_pathv[i]);
692 if (do_rm(in, out, g.gl_pathv[i]) == -1)
697 path1 = make_absolute(path1, *pwd);
699 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
701 err = do_mkdir(in, out, path1, &a);
704 path1 = make_absolute(path1, *pwd);
705 err = do_rmdir(in, out, path1);
708 path1 = make_absolute(path1, *pwd);
709 if ((tmp = do_realpath(in, out, path1)) == NULL) {
713 if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
718 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
719 error("Can't change directory: Can't check target");
724 if (!S_ISDIR(aa->perm)) {
725 error("Can't change directory: \"%s\" is not "
736 do_ls(in, out, *pwd);
739 path1 = make_absolute(path1, *pwd);
740 if ((tmp = do_realpath(in, out, path1)) == NULL)
744 if ((aa = do_stat(in, out, path1, 0)) == NULL)
746 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
747 !S_ISDIR(aa->perm)) {
748 error("Can't ls: \"%s\" is not a directory", path1);
751 do_ls(in, out, path1);
754 if (chdir(path1) == -1) {
755 error("Couldn't change local directory to "
756 "\"%s\": %s", path1, strerror(errno));
761 if (mkdir(path1, 0777) == -1) {
762 error("Couldn't create local directory "
763 "\"%s\": %s", path1, strerror(errno));
775 printf("Local umask: %03lo\n", n_arg);
778 path1 = make_absolute(path1, *pwd);
780 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
782 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
783 for(i = 0; g.gl_pathv[i]; i++) {
784 printf("Changing mode on %s\n", g.gl_pathv[i]);
785 do_setstat(in, out, g.gl_pathv[i], &a);
789 path1 = make_absolute(path1, *pwd);
790 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
791 for(i = 0; g.gl_pathv[i]; i++) {
792 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
794 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
795 error("Can't get current ownership of "
796 "remote file \"%s\"", g.gl_pathv[i]);
799 printf("Changing owner on %s\n", g.gl_pathv[i]);
800 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
802 do_setstat(in, out, g.gl_pathv[i], aa);
806 path1 = make_absolute(path1, *pwd);
807 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
808 for(i = 0; g.gl_pathv[i]; i++) {
809 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
811 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
812 error("Can't get current ownership of "
813 "remote file \"%s\"", g.gl_pathv[i]);
816 printf("Changing group on %s\n", g.gl_pathv[i]);
817 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
819 do_setstat(in, out, g.gl_pathv[i], aa);
823 printf("Remote working directory: %s\n", *pwd);
826 if (!getcwd(path_buf, sizeof(path_buf)))
827 error("Couldn't get local cwd: %s",
830 printf("Local working directory: %s\n",
839 printf("SFTP protocol version %d\n", version);
842 fatal("%d is not implemented", cmdnum);
852 /* If an error occurs in batch mode we should abort. */
853 if (infile != stdin && err > 0)
860 interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
866 version = do_init(fd_in, fd_out);
868 fatal("Couldn't initialise connection to server");
870 pwd = do_realpath(fd_in, fd_out, ".");
875 dir = xstrdup(file1);
876 dir = make_absolute(dir, pwd);
878 if (remote_is_dir(fd_in, fd_out, dir) && file2 == NULL) {
879 printf("Changing to: %s\n", dir);
880 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
881 parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
884 snprintf(cmd, sizeof cmd, "get %s", dir);
886 snprintf(cmd, sizeof cmd, "get %s %s", dir,
889 parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
894 setvbuf(stdout, NULL, _IOLBF, 0);
895 setvbuf(infile, NULL, _IOLBF, 0);
906 /* XXX: use libedit */
907 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
910 } else if (infile != stdin) /* Bluff typing */
913 cp = strrchr(cmd, '\n');
917 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))