2 * Copyright (c) 2001,2002 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.45 2002/03/19 06:32:56 mpech 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 /* Size of buffer used when copying files */
46 extern size_t copy_buffer_len;
48 /* Number of concurrent outstanding requests */
49 extern int num_requests;
51 /* Seperators for interactive commands */
52 #define WHITESPACE " \t\r\n"
54 /* Commands for interactive mode */
83 const struct CMD cmds[] = {
96 { "lchdir", I_LCHDIR },
98 { "lmkdir", I_LMKDIR },
102 { "lumask", I_LUMASK },
103 { "mkdir", I_MKDIR },
108 { "rename", I_RENAME },
110 { "rmdir", I_RMDIR },
111 { "symlink", I_SYMLINK },
112 { "version", I_VERSION },
121 printf("Available commands:\n");
122 printf("cd path Change remote directory to 'path'\n");
123 printf("lcd path Change local directory to 'path'\n");
124 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
125 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
126 printf("chown own path Change owner of file 'path' to 'own'\n");
127 printf("help Display this help text\n");
128 printf("get remote-path [local-path] Download file\n");
129 printf("lls [ls-options [path]] Display local directory listing\n");
130 printf("ln oldpath newpath Symlink remote file\n");
131 printf("lmkdir path Create local directory\n");
132 printf("lpwd Print local working directory\n");
133 printf("ls [path] Display remote directory listing\n");
134 printf("lumask umask Set local umask to 'umask'\n");
135 printf("mkdir path Create remote directory\n");
136 printf("put local-path [remote-path] Upload file\n");
137 printf("pwd Display remote working directory\n");
138 printf("exit Quit sftp\n");
139 printf("quit Quit sftp\n");
140 printf("rename oldpath newpath Rename remote file\n");
141 printf("rmdir path Remove remote directory\n");
142 printf("rm path Delete remote file\n");
143 printf("symlink oldpath newpath Symlink remote file\n");
144 printf("version Show SFTP version\n");
145 printf("!command Execute 'command' in local shell\n");
146 printf("! Escape to local shell\n");
147 printf("? Synonym for help\n");
151 local_do_shell(const char *args)
160 if ((shell = getenv("SHELL")) == NULL)
161 shell = _PATH_BSHELL;
163 if ((pid = fork()) == -1)
164 fatal("Couldn't fork: %s", strerror(errno));
167 /* XXX: child has pipe fds to ssh subproc open - issue? */
169 debug3("Executing %s -c \"%s\"", shell, args);
170 execl(shell, shell, "-c", args, (char *)NULL);
172 debug3("Executing %s", shell);
173 execl(shell, shell, (char *)NULL);
175 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
179 if (waitpid(pid, &status, 0) == -1)
180 fatal("Couldn't wait for child: %s", strerror(errno));
181 if (!WIFEXITED(status))
182 error("Shell exited abormally");
183 else if (WEXITSTATUS(status))
184 error("Shell exited with status %d", WEXITSTATUS(status));
188 local_do_ls(const char *args)
191 local_do_shell(_PATH_LS);
193 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
194 char *buf = xmalloc(len);
196 /* XXX: quoting - rip quoting code from ftp? */
197 snprintf(buf, len, _PATH_LS " %s", args);
204 path_append(char *p1, char *p2)
207 int len = strlen(p1) + strlen(p2) + 2;
210 strlcpy(ret, p1, len);
211 if (strcmp(p1, "/") != 0)
212 strlcat(ret, "/", len);
213 strlcat(ret, p2, len);
219 make_absolute(char *p, char *pwd)
224 if (p && p[0] != '/') {
225 abs = path_append(pwd, p);
233 infer_path(const char *p, char **ifp)
237 cp = strrchr(p, '/');
244 error("Invalid path");
248 *ifp = xstrdup(cp + 1);
253 parse_getput_flags(const char **cpp, int *pflag)
255 const char *cp = *cpp;
257 /* Check for flags */
258 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
265 error("Invalid flag -%c", cp[1]);
269 *cpp = cp + strspn(cp, WHITESPACE);
276 get_pathname(const char **cpp, char **path)
278 const char *cp = *cpp, *end;
282 cp += strspn(cp, WHITESPACE);
289 /* Check for quoted filenames */
290 if (*cp == '\"' || *cp == '\'') {
293 end = strchr(cp, quot);
295 error("Unterminated quote");
299 error("Empty quotes");
302 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
304 /* Read to end of filename */
305 end = strpbrk(cp, WHITESPACE);
307 end = strchr(cp, '\0');
308 *cpp = end + strspn(end, WHITESPACE);
313 *path = xmalloc(i + 1);
314 memcpy(*path, cp, i);
328 /* XXX: report errors? */
329 if (stat(path, &sb) == -1)
332 return(sb.st_mode & S_IFDIR);
336 remote_is_dir(struct sftp_conn *conn, char *path)
340 /* XXX: report errors? */
341 if ((a = do_stat(conn, path, 1)) == NULL)
343 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
345 return(a->perm & S_IFDIR);
349 process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
351 char *abs_src = NULL;
352 char *abs_dst = NULL;
358 abs_src = xstrdup(src);
359 abs_src = make_absolute(abs_src, pwd);
361 memset(&g, 0, sizeof(g));
362 debug3("Looking up %s", abs_src);
363 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
364 error("File \"%s\" not found.", abs_src);
369 /* Only one match, dst may be file, directory or unspecified */
370 if (g.gl_pathv[0] && g.gl_matchc == 1) {
372 /* If directory specified, append filename */
374 if (infer_path(g.gl_pathv[0], &tmp)) {
378 abs_dst = path_append(dst, tmp);
381 abs_dst = xstrdup(dst);
382 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
386 printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
387 err = do_download(conn, g.gl_pathv[0], abs_dst, pflag);
391 /* Multiple matches, dst may be directory or unspecified */
392 if (dst && !is_dir(dst)) {
393 error("Multiple files match, but \"%s\" is not a directory",
399 for (i = 0; g.gl_pathv[i]; i++) {
400 if (infer_path(g.gl_pathv[i], &tmp)) {
405 abs_dst = path_append(dst, tmp);
410 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
411 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
426 process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
428 char *tmp_dst = NULL;
429 char *abs_dst = NULL;
436 tmp_dst = xstrdup(dst);
437 tmp_dst = make_absolute(tmp_dst, pwd);
440 memset(&g, 0, sizeof(g));
441 debug3("Looking up %s", src);
442 if (glob(src, 0, NULL, &g)) {
443 error("File \"%s\" not found.", src);
448 /* Only one match, dst may be file, directory or unspecified */
449 if (g.gl_pathv[0] && g.gl_matchc == 1) {
451 /* If directory specified, append filename */
452 if (remote_is_dir(conn, tmp_dst)) {
453 if (infer_path(g.gl_pathv[0], &tmp)) {
457 abs_dst = path_append(tmp_dst, tmp);
460 abs_dst = xstrdup(tmp_dst);
462 if (infer_path(g.gl_pathv[0], &abs_dst)) {
466 abs_dst = make_absolute(abs_dst, pwd);
468 printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
469 err = do_upload(conn, g.gl_pathv[0], abs_dst, pflag);
473 /* Multiple matches, dst may be directory or unspecified */
474 if (tmp_dst && !remote_is_dir(conn, tmp_dst)) {
475 error("Multiple files match, but \"%s\" is not a directory",
481 for (i = 0; g.gl_pathv[i]; i++) {
482 if (infer_path(g.gl_pathv[i], &tmp)) {
487 abs_dst = path_append(tmp_dst, tmp);
490 abs_dst = make_absolute(tmp, pwd);
492 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
493 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
506 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
507 char **path1, char **path2)
509 const char *cmd, *cp = *cpp;
515 /* Skip leading whitespace */
516 cp = cp + strspn(cp, WHITESPACE);
518 /* Ignore blank lines */
522 /* Figure out which command we have */
523 for (i = 0; cmds[i].c; i++) {
524 int cmdlen = strlen(cmds[i].c);
526 /* Check for command followed by whitespace */
527 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
528 strchr(WHITESPACE, cp[cmdlen])) {
530 cp = cp + strspn(cp, WHITESPACE);
541 } else if (cmdnum == -1) {
542 error("Invalid command.");
546 /* Get arguments and parse flags */
548 *path1 = *path2 = NULL;
552 if (parse_getput_flags(&cp, pflag))
554 /* Get first pathname (mandatory) */
555 if (get_pathname(&cp, path1))
557 if (*path1 == NULL) {
558 error("You must specify at least one path after a "
562 /* Try to get second pathname (optional) */
563 if (get_pathname(&cp, path2))
568 if (get_pathname(&cp, path1))
570 if (get_pathname(&cp, path2))
572 if (!*path1 || !*path2) {
573 error("You must specify two paths after a %s "
584 /* Get pathname (mandatory) */
585 if (get_pathname(&cp, path1))
587 if (*path1 == NULL) {
588 error("You must specify a path after a %s command.",
594 /* Path is optional */
595 if (get_pathname(&cp, path1))
600 /* Uses the rest of the line */
608 /* Get numeric arg (mandatory) */
609 l = strtol(cp, &cp2, base);
610 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
611 errno == ERANGE) || l < 0) {
612 error("You must supply a numeric argument "
613 "to the %s command.", cmd);
618 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
620 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
621 error("You must supply a numeric argument "
622 "to the %s command.", cmd);
625 cp += strspn(cp, WHITESPACE);
627 /* Get pathname (mandatory) */
628 if (get_pathname(&cp, path1))
630 if (*path1 == NULL) {
631 error("You must specify a path after a %s command.",
643 fatal("Command not implemented");
651 parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd)
653 char *path1, *path2, *tmp;
654 int pflag, cmdnum, i;
657 char path_buf[MAXPATHLEN];
661 path1 = path2 = NULL;
662 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
664 memset(&g, 0, sizeof(g));
666 /* Perform command */
671 err = process_get(conn, path1, path2, *pwd, pflag);
674 err = process_put(conn, path1, path2, *pwd, pflag);
677 path1 = make_absolute(path1, *pwd);
678 path2 = make_absolute(path2, *pwd);
679 err = do_rename(conn, path1, path2);
682 path2 = make_absolute(path2, *pwd);
683 err = do_symlink(conn, path1, path2);
686 path1 = make_absolute(path1, *pwd);
687 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
688 for (i = 0; g.gl_pathv[i]; i++) {
689 printf("Removing %s\n", g.gl_pathv[i]);
690 if (do_rm(conn, g.gl_pathv[i]) == -1)
695 path1 = make_absolute(path1, *pwd);
697 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
699 err = do_mkdir(conn, path1, &a);
702 path1 = make_absolute(path1, *pwd);
703 err = do_rmdir(conn, path1);
706 path1 = make_absolute(path1, *pwd);
707 if ((tmp = do_realpath(conn, path1)) == NULL) {
711 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
716 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
717 error("Can't change directory: Can't check target");
722 if (!S_ISDIR(aa->perm)) {
723 error("Can't change directory: \"%s\" is not "
737 path1 = make_absolute(path1, *pwd);
738 if ((tmp = do_realpath(conn, path1)) == NULL)
742 if ((aa = do_stat(conn, path1, 0)) == NULL)
744 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
745 !S_ISDIR(aa->perm)) {
746 error("Can't ls: \"%s\" is not a directory", path1);
752 if (chdir(path1) == -1) {
753 error("Couldn't change local directory to "
754 "\"%s\": %s", path1, strerror(errno));
759 if (mkdir(path1, 0777) == -1) {
760 error("Couldn't create local directory "
761 "\"%s\": %s", path1, strerror(errno));
773 printf("Local umask: %03lo\n", n_arg);
776 path1 = make_absolute(path1, *pwd);
778 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
780 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
781 for (i = 0; g.gl_pathv[i]; i++) {
782 printf("Changing mode on %s\n", g.gl_pathv[i]);
783 do_setstat(conn, g.gl_pathv[i], &a);
787 path1 = make_absolute(path1, *pwd);
788 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
789 for (i = 0; g.gl_pathv[i]; i++) {
790 if (!(aa = do_stat(conn, g.gl_pathv[i], 0)))
792 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
793 error("Can't get current ownership of "
794 "remote file \"%s\"", g.gl_pathv[i]);
797 printf("Changing owner on %s\n", g.gl_pathv[i]);
798 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
800 do_setstat(conn, g.gl_pathv[i], aa);
804 path1 = make_absolute(path1, *pwd);
805 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
806 for (i = 0; g.gl_pathv[i]; i++) {
807 if (!(aa = do_stat(conn, g.gl_pathv[i], 0)))
809 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
810 error("Can't get current ownership of "
811 "remote file \"%s\"", g.gl_pathv[i]);
814 printf("Changing group on %s\n", g.gl_pathv[i]);
815 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
817 do_setstat(conn, g.gl_pathv[i], aa);
821 printf("Remote working directory: %s\n", *pwd);
824 if (!getcwd(path_buf, sizeof(path_buf)))
825 error("Couldn't get local cwd: %s",
828 printf("Local working directory: %s\n",
837 printf("SFTP protocol version %d\n", sftp_proto_version(conn));
840 fatal("%d is not implemented", cmdnum);
850 /* If an error occurs in batch mode we should abort. */
851 if (infile != stdin && err > 0)
858 interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
863 struct sftp_conn *conn;
865 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
867 fatal("Couldn't initialise connection to server");
869 pwd = do_realpath(conn, ".");
874 dir = xstrdup(file1);
875 dir = make_absolute(dir, pwd);
877 if (remote_is_dir(conn, dir) && file2 == NULL) {
878 printf("Changing to: %s\n", dir);
879 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
880 parse_dispatch_command(conn, cmd, &pwd);
883 snprintf(cmd, sizeof cmd, "get %s", dir);
885 snprintf(cmd, sizeof cmd, "get %s %s", dir,
888 parse_dispatch_command(conn, 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(conn, cmd, &pwd))