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.32 2001/03/23 13:10:57 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[] = {
91 { "lchdir", I_LCHDIR },
93 { "lmkdir", I_LMKDIR },
97 { "lumask", I_LUMASK },
102 { "rename", I_RENAME },
104 { "rmdir", I_RMDIR },
105 { "symlink", I_SYMLINK },
106 { "version", I_VERSION },
115 printf("Available commands:\n");
116 printf("cd path Change remote directory to 'path'\n");
117 printf("lcd path Change local directory to 'path'\n");
118 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
119 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
120 printf("chown own path Change owner of file 'path' to 'own'\n");
121 printf("help Display this help text\n");
122 printf("get remote-path [local-path] Download file\n");
123 printf("lls [ls-options [path]] Display local directory listing\n");
124 printf("ln oldpath newpath Symlink remote file\n");
125 printf("lmkdir path Create local directory\n");
126 printf("lpwd Print local working directory\n");
127 printf("ls [path] Display remote directory listing\n");
128 printf("lumask umask Set local umask to 'umask'\n");
129 printf("mkdir path Create remote directory\n");
130 printf("put local-path [remote-path] Upload file\n");
131 printf("pwd Display remote working directory\n");
132 printf("exit Quit sftp\n");
133 printf("quit Quit sftp\n");
134 printf("rename oldpath newpath Rename remote file\n");
135 printf("rmdir path Remove remote directory\n");
136 printf("rm path Delete remote file\n");
137 printf("symlink oldpath newpath Symlink remote file\n");
138 printf("version Show SFTP version\n");
139 printf("!command Execute 'command' in local shell\n");
140 printf("! Escape to local shell\n");
141 printf("? Synonym for help\n");
145 local_do_shell(const char *args)
154 if ((shell = getenv("SHELL")) == NULL)
155 shell = _PATH_BSHELL;
157 if ((pid = fork()) == -1)
158 fatal("Couldn't fork: %s", strerror(errno));
161 /* XXX: child has pipe fds to ssh subproc open - issue? */
163 debug3("Executing %s -c \"%s\"", shell, args);
164 ret = execl(shell, shell, "-c", args, NULL);
166 debug3("Executing %s", shell);
167 ret = execl(shell, shell, NULL);
169 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
173 if (waitpid(pid, &status, 0) == -1)
174 fatal("Couldn't wait for child: %s", strerror(errno));
175 if (!WIFEXITED(status))
176 error("Shell exited abormally");
177 else if (WEXITSTATUS(status))
178 error("Shell exited with status %d", WEXITSTATUS(status));
182 local_do_ls(const char *args)
185 local_do_shell(_PATH_LS);
187 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
188 char *buf = xmalloc(len);
190 /* XXX: quoting - rip quoting code from ftp? */
191 snprintf(buf, len, _PATH_LS " %s", args);
198 path_append(char *p1, char *p2)
201 int len = strlen(p1) + strlen(p2) + 2;
204 strlcpy(ret, p1, len);
205 strlcat(ret, "/", len);
206 strlcat(ret, p2, len);
212 make_absolute(char *p, char *pwd)
217 if (p && p[0] != '/') {
218 abs = path_append(pwd, p);
226 infer_path(const char *p, char **ifp)
230 cp = strrchr(p, '/');
237 error("Invalid path");
241 *ifp = xstrdup(cp + 1);
246 parse_getput_flags(const char **cpp, int *pflag)
248 const char *cp = *cpp;
250 /* Check for flags */
251 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
258 error("Invalid flag -%c", cp[1]);
262 *cpp = cp + strspn(cp, WHITESPACE);
269 get_pathname(const char **cpp, char **path)
271 const char *cp = *cpp, *end;
275 cp += strspn(cp, WHITESPACE);
282 /* Check for quoted filenames */
283 if (*cp == '\"' || *cp == '\'') {
286 end = strchr(cp, quot);
288 error("Unterminated quote");
292 error("Empty quotes");
295 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
297 /* Read to end of filename */
298 end = strpbrk(cp, WHITESPACE);
300 end = strchr(cp, '\0');
301 *cpp = end + strspn(end, WHITESPACE);
306 *path = xmalloc(i + 1);
307 memcpy(*path, cp, i);
321 /* XXX: report errors? */
322 if (stat(path, &sb) == -1)
325 return(sb.st_mode & S_IFDIR);
329 remote_is_dir(int in, int out, char *path)
333 /* XXX: report errors? */
334 if ((a = do_stat(in, out, path, 1)) == NULL)
336 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
338 return(a->perm & S_IFDIR);
342 process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
344 char *abs_src = NULL;
345 char *abs_dst = NULL;
351 abs_src = xstrdup(src);
352 abs_src = make_absolute(abs_src, pwd);
354 memset(&g, 0, sizeof(g));
355 debug3("Looking up %s", abs_src);
356 if (remote_glob(in, out, abs_src, 0, NULL, &g)) {
357 error("File \"%s\" not found.", abs_src);
362 /* Only one match, dst may be file, directory or unspecified */
363 if (g.gl_pathv[0] && g.gl_matchc == 1) {
365 /* If directory specified, append filename */
367 if (infer_path(g.gl_pathv[0], &tmp)) {
371 abs_dst = path_append(dst, tmp);
374 abs_dst = xstrdup(dst);
375 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
379 printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
380 err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag);
384 /* Multiple matches, dst may be directory or unspecified */
385 if (dst && !is_dir(dst)) {
386 error("Multiple files match, but \"%s\" is not a directory",
392 for(i = 0; g.gl_pathv[i]; i++) {
393 if (infer_path(g.gl_pathv[i], &tmp)) {
398 abs_dst = path_append(dst, tmp);
403 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
404 if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
419 process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
421 char *tmp_dst = NULL;
422 char *abs_dst = NULL;
429 tmp_dst = xstrdup(dst);
430 tmp_dst = make_absolute(tmp_dst, pwd);
433 memset(&g, 0, sizeof(g));
434 debug3("Looking up %s", src);
435 if (glob(src, 0, NULL, &g)) {
436 error("File \"%s\" not found.", src);
441 /* Only one match, dst may be file, directory or unspecified */
442 if (g.gl_pathv[0] && g.gl_matchc == 1) {
444 /* If directory specified, append filename */
445 if (remote_is_dir(in, out, tmp_dst)) {
446 if (infer_path(g.gl_pathv[0], &tmp)) {
450 abs_dst = path_append(tmp_dst, tmp);
453 abs_dst = xstrdup(tmp_dst);
455 if (infer_path(g.gl_pathv[0], &abs_dst)) {
459 abs_dst = make_absolute(abs_dst, pwd);
461 printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
462 err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag);
466 /* Multiple matches, dst may be directory or unspecified */
467 if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
468 error("Multiple files match, but \"%s\" is not a directory",
474 for(i = 0; g.gl_pathv[i]; i++) {
475 if (infer_path(g.gl_pathv[i], &tmp)) {
480 abs_dst = path_append(tmp_dst, tmp);
483 abs_dst = make_absolute(tmp, pwd);
485 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
486 if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
499 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
500 char **path1, char **path2)
502 const char *cmd, *cp = *cpp;
508 /* Skip leading whitespace */
509 cp = cp + strspn(cp, WHITESPACE);
511 /* Ignore blank lines */
515 /* Figure out which command we have */
516 for(i = 0; cmds[i].c; i++) {
517 int cmdlen = strlen(cmds[i].c);
519 /* Check for command followed by whitespace */
520 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
521 strchr(WHITESPACE, cp[cmdlen])) {
523 cp = cp + strspn(cp, WHITESPACE);
534 } else if (cmdnum == -1) {
535 error("Invalid command.");
539 /* Get arguments and parse flags */
541 *path1 = *path2 = NULL;
545 if (parse_getput_flags(&cp, pflag))
547 /* Get first pathname (mandatory) */
548 if (get_pathname(&cp, path1))
550 if (*path1 == NULL) {
551 error("You must specify at least one path after a "
555 /* Try to get second pathname (optional) */
556 if (get_pathname(&cp, path2))
561 if (get_pathname(&cp, path1))
563 if (get_pathname(&cp, path2))
565 if (!*path1 || !*path2) {
566 error("You must specify two paths after a %s "
577 /* Get pathname (mandatory) */
578 if (get_pathname(&cp, path1))
580 if (*path1 == NULL) {
581 error("You must specify a path after a %s command.",
587 /* Path is optional */
588 if (get_pathname(&cp, path1))
593 /* Uses the rest of the line */
601 /* Get numeric arg (mandatory) */
602 l = strtol(cp, &cp2, base);
603 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
604 errno == ERANGE) || l < 0) {
605 error("You must supply a numeric argument "
606 "to the %s command.", cmd);
611 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
613 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
614 error("You must supply a numeric argument "
615 "to the %s command.", cmd);
618 cp += strspn(cp, WHITESPACE);
620 /* Get pathname (mandatory) */
621 if (get_pathname(&cp, path1))
623 if (*path1 == NULL) {
624 error("You must specify a path after a %s command.",
636 fatal("Command not implemented");
644 parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
646 char *path1, *path2, *tmp;
647 int pflag, cmdnum, i;
650 char path_buf[MAXPATHLEN];
654 path1 = path2 = NULL;
655 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
657 memset(&g, 0, sizeof(g));
659 /* Perform command */
664 err = process_get(in, out, path1, path2, *pwd, pflag);
667 err = process_put(in, out, path1, path2, *pwd, pflag);
670 path1 = make_absolute(path1, *pwd);
671 path2 = make_absolute(path2, *pwd);
672 err = do_rename(in, out, path1, path2);
676 error("The server (version %d) does not support "
677 "this operation", version);
680 path2 = make_absolute(path2, *pwd);
681 err = do_symlink(in, out, path1, path2);
685 path1 = make_absolute(path1, *pwd);
686 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
687 for(i = 0; g.gl_pathv[i]; i++) {
688 printf("Removing %s\n", g.gl_pathv[i]);
689 if (do_rm(in, out, g.gl_pathv[i]) == -1)
694 path1 = make_absolute(path1, *pwd);
696 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
698 err = do_mkdir(in, out, path1, &a);
701 path1 = make_absolute(path1, *pwd);
702 err = do_rmdir(in, out, path1);
705 path1 = make_absolute(path1, *pwd);
706 if ((tmp = do_realpath(in, out, path1)) == NULL) {
710 if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
715 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
716 error("Can't change directory: Can't check target");
721 if (!S_ISDIR(aa->perm)) {
722 error("Can't change directory: \"%s\" is not "
733 do_ls(in, out, *pwd);
736 path1 = make_absolute(path1, *pwd);
737 if ((tmp = do_realpath(in, out, path1)) == NULL)
741 if ((aa = do_stat(in, out, path1, 0)) == NULL)
743 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
744 !S_ISDIR(aa->perm)) {
745 error("Can't ls: \"%s\" is not a directory", path1);
748 do_ls(in, out, path1);
751 if (chdir(path1) == -1) {
752 error("Couldn't change local directory to "
753 "\"%s\": %s", path1, strerror(errno));
758 if (mkdir(path1, 0777) == -1) {
759 error("Couldn't create local directory "
760 "\"%s\": %s", path1, strerror(errno));
772 printf("Local umask: %03lo\n", n_arg);
775 path1 = make_absolute(path1, *pwd);
777 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
779 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
780 for(i = 0; g.gl_pathv[i]; i++) {
781 printf("Changing mode on %s\n", g.gl_pathv[i]);
782 do_setstat(in, out, g.gl_pathv[i], &a);
786 path1 = make_absolute(path1, *pwd);
787 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
788 for(i = 0; g.gl_pathv[i]; i++) {
789 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
791 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
792 error("Can't get current ownership of "
793 "remote file \"%s\"", g.gl_pathv[i]);
796 printf("Changing owner on %s\n", g.gl_pathv[i]);
797 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
799 do_setstat(in, out, g.gl_pathv[i], aa);
803 path1 = make_absolute(path1, *pwd);
804 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
805 for(i = 0; g.gl_pathv[i]; i++) {
806 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
808 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
809 error("Can't get current ownership of "
810 "remote file \"%s\"", g.gl_pathv[i]);
813 printf("Changing group on %s\n", g.gl_pathv[i]);
814 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
816 do_setstat(in, out, g.gl_pathv[i], aa);
820 printf("Remote working directory: %s\n", *pwd);
823 if (!getcwd(path_buf, sizeof(path_buf)))
824 error("Couldn't get local cwd: %s",
827 printf("Local working directory: %s\n",
836 printf("SFTP protocol version %d\n", version);
839 fatal("%d is not implemented", cmdnum);
849 /* If an error occurs in batch mode we should abort. */
850 if (infile != stdin && err > 0)
857 interactive_loop(int fd_in, int fd_out)
862 version = do_init(fd_in, fd_out);
864 fatal("Couldn't initialise connection to server");
866 pwd = do_realpath(fd_in, fd_out, ".");
870 setvbuf(stdout, NULL, _IOLBF, 0);
871 setvbuf(infile, NULL, _IOLBF, 0);
878 /* XXX: use libedit */
879 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
882 } else if (infile != stdin) /* Bluff typing */
885 cp = strrchr(cmd, '\n');
889 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))