blob: 841e562e1de21bc7a433ae8d4f6cc4b6ecae83f8 [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
2 * Copyright (c) 2001 Damien Miller. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
23 */
24
Damien Millerd7686fd2001-02-10 00:40:03 +110025/* XXX: globbed ls */
Damien Miller33804262001-02-04 23:20:18 +110026/* XXX: recursive operations */
27
28#include "includes.h"
Ben Lindstrom59e12492001-08-15 23:22:56 +000029RCSID("$OpenBSD: sftp-int.c,v 1.40 2001/08/14 09:23:02 markus Exp $");
Damien Miller4870afd2001-03-14 10:27:09 +110030
Damien Miller33804262001-02-04 23:20:18 +110031#include "buffer.h"
32#include "xmalloc.h"
33#include "log.h"
34#include "pathnames.h"
35
36#include "sftp.h"
37#include "sftp-common.h"
Damien Miller4870afd2001-03-14 10:27:09 +110038#include "sftp-glob.h"
Damien Miller33804262001-02-04 23:20:18 +110039#include "sftp-client.h"
40#include "sftp-int.h"
41
Damien Miller058316f2001-03-08 10:08:49 +110042/* File to read commands from */
43extern FILE *infile;
44
45/* Version of server we are speaking to */
46int version;
Ben Lindstrom562c26b2001-03-07 01:26:48 +000047
Damien Miller33804262001-02-04 23:20:18 +110048/* Seperators for interactive commands */
49#define WHITESPACE " \t\r\n"
50
51/* Commands for interactive mode */
52#define I_CHDIR 1
53#define I_CHGRP 2
54#define I_CHMOD 3
55#define I_CHOWN 4
56#define I_GET 5
57#define I_HELP 6
58#define I_LCHDIR 7
59#define I_LLS 8
60#define I_LMKDIR 9
61#define I_LPWD 10
62#define I_LS 11
63#define I_LUMASK 12
64#define I_MKDIR 13
65#define I_PUT 14
66#define I_PWD 15
67#define I_QUIT 16
68#define I_RENAME 17
69#define I_RM 18
70#define I_RMDIR 19
71#define I_SHELL 20
Damien Miller058316f2001-03-08 10:08:49 +110072#define I_SYMLINK 21
Ben Lindstromf78682d2001-03-14 21:26:27 +000073#define I_VERSION 22
Damien Miller33804262001-02-04 23:20:18 +110074
75struct CMD {
Damien Miller33804262001-02-04 23:20:18 +110076 const char *c;
Kevin Steves62c45db2001-02-05 13:42:43 +000077 const int n;
Damien Miller33804262001-02-04 23:20:18 +110078};
79
80const struct CMD cmds[] = {
Ben Lindstrom59e12492001-08-15 23:22:56 +000081 { "bye", I_QUIT },
Damien Millerd7686fd2001-02-10 00:40:03 +110082 { "cd", I_CHDIR },
83 { "chdir", I_CHDIR },
84 { "chgrp", I_CHGRP },
85 { "chmod", I_CHMOD },
86 { "chown", I_CHOWN },
87 { "dir", I_LS },
88 { "exit", I_QUIT },
89 { "get", I_GET },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +000090 { "mget", I_GET },
Damien Millerd7686fd2001-02-10 00:40:03 +110091 { "help", I_HELP },
92 { "lcd", I_LCHDIR },
93 { "lchdir", I_LCHDIR },
94 { "lls", I_LLS },
95 { "lmkdir", I_LMKDIR },
Damien Miller058316f2001-03-08 10:08:49 +110096 { "ln", I_SYMLINK },
Damien Millerd7686fd2001-02-10 00:40:03 +110097 { "lpwd", I_LPWD },
98 { "ls", I_LS },
99 { "lumask", I_LUMASK },
100 { "mkdir", I_MKDIR },
101 { "put", I_PUT },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +0000102 { "mput", I_PUT },
Damien Millerd7686fd2001-02-10 00:40:03 +1100103 { "pwd", I_PWD },
104 { "quit", I_QUIT },
105 { "rename", I_RENAME },
106 { "rm", I_RM },
107 { "rmdir", I_RMDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100108 { "symlink", I_SYMLINK },
Ben Lindstromf78682d2001-03-14 21:26:27 +0000109 { "version", I_VERSION },
Kevin Steves62c45db2001-02-05 13:42:43 +0000110 { "!", I_SHELL },
111 { "?", I_HELP },
112 { NULL, -1}
Damien Miller33804262001-02-04 23:20:18 +1100113};
114
Ben Lindstrombba81212001-06-25 05:01:22 +0000115static void
Damien Miller33804262001-02-04 23:20:18 +1100116help(void)
117{
118 printf("Available commands:\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100119 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");
Damien Miller058316f2001-03-08 10:08:49 +1100127 printf("ln oldpath newpath Symlink remote file\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100128 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");
Damien Miller058316f2001-03-08 10:08:49 +1100140 printf("symlink oldpath newpath Symlink remote file\n");
Ben Lindstromf78682d2001-03-14 21:26:27 +0000141 printf("version Show SFTP version\n");
Damien Miller33804262001-02-04 23:20:18 +1100142 printf("!command Execute 'command' in local shell\n");
143 printf("! Escape to local shell\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100144 printf("? Synonym for help\n");
Damien Miller33804262001-02-04 23:20:18 +1100145}
146
Ben Lindstrombba81212001-06-25 05:01:22 +0000147static void
Damien Miller33804262001-02-04 23:20:18 +1100148local_do_shell(const char *args)
149{
Ben Lindstrom206941f2001-04-15 14:27:16 +0000150 int status;
Damien Miller33804262001-02-04 23:20:18 +1100151 char *shell;
152 pid_t pid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000153
Damien Miller33804262001-02-04 23:20:18 +1100154 if (!*args)
155 args = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000156
Damien Miller33804262001-02-04 23:20:18 +1100157 if ((shell = getenv("SHELL")) == NULL)
158 shell = _PATH_BSHELL;
159
160 if ((pid = fork()) == -1)
161 fatal("Couldn't fork: %s", strerror(errno));
162
163 if (pid == 0) {
164 /* XXX: child has pipe fds to ssh subproc open - issue? */
165 if (args) {
166 debug3("Executing %s -c \"%s\"", shell, args);
Damien Millerefb1edf2001-07-14 12:19:36 +1000167 execl(shell, shell, "-c", args, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100168 } else {
169 debug3("Executing %s", shell);
Damien Millerefb1edf2001-07-14 12:19:36 +1000170 execl(shell, shell, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100171 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000172 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
Damien Miller33804262001-02-04 23:20:18 +1100173 strerror(errno));
174 _exit(1);
175 }
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));
182}
183
Ben Lindstrombba81212001-06-25 05:01:22 +0000184static void
Damien Miller33804262001-02-04 23:20:18 +1100185local_do_ls(const char *args)
186{
187 if (!args || !*args)
Damien Millerd7686fd2001-02-10 00:40:03 +1100188 local_do_shell(_PATH_LS);
Damien Miller33804262001-02-04 23:20:18 +1100189 else {
Damien Millerd7686fd2001-02-10 00:40:03 +1100190 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
191 char *buf = xmalloc(len);
Damien Miller33804262001-02-04 23:20:18 +1100192
193 /* XXX: quoting - rip quoting code from ftp? */
Damien Millerd7686fd2001-02-10 00:40:03 +1100194 snprintf(buf, len, _PATH_LS " %s", args);
Damien Miller33804262001-02-04 23:20:18 +1100195 local_do_shell(buf);
Damien Millerd7686fd2001-02-10 00:40:03 +1100196 xfree(buf);
Damien Miller33804262001-02-04 23:20:18 +1100197 }
198}
199
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static char *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000201path_append(char *p1, char *p2)
202{
203 char *ret;
Ben Lindstromcf00df62001-03-17 00:37:31 +0000204 int len = strlen(p1) + strlen(p2) + 2;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000205
Ben Lindstromcf00df62001-03-17 00:37:31 +0000206 ret = xmalloc(len);
207 strlcpy(ret, p1, len);
Ben Lindstrom95148e32001-08-06 21:30:53 +0000208 if (strcmp(p1, "/") != 0)
209 strlcat(ret, "/", len);
Ben Lindstromcf00df62001-03-17 00:37:31 +0000210 strlcat(ret, p2, len);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000211
212 return(ret);
213}
214
Ben Lindstrombba81212001-06-25 05:01:22 +0000215static char *
Damien Miller33804262001-02-04 23:20:18 +1100216make_absolute(char *p, char *pwd)
217{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000218 char *abs;
Damien Miller33804262001-02-04 23:20:18 +1100219
220 /* Derelativise */
221 if (p && p[0] != '/') {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000222 abs = path_append(pwd, p);
Damien Miller33804262001-02-04 23:20:18 +1100223 xfree(p);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000224 return(abs);
225 } else
226 return(p);
227}
228
Ben Lindstrombba81212001-06-25 05:01:22 +0000229static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000230infer_path(const char *p, char **ifp)
231{
232 char *cp;
233
234 cp = strrchr(p, '/');
235 if (cp == NULL) {
236 *ifp = xstrdup(p);
237 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100238 }
239
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000240 if (!cp[1]) {
241 error("Invalid path");
242 return(-1);
243 }
244
245 *ifp = xstrdup(cp + 1);
246 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100247}
248
Ben Lindstrombba81212001-06-25 05:01:22 +0000249static int
Damien Miller33804262001-02-04 23:20:18 +1100250parse_getput_flags(const char **cpp, int *pflag)
251{
252 const char *cp = *cpp;
253
254 /* Check for flags */
255 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100256 switch (cp[1]) {
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000257 case 'p':
Damien Miller33804262001-02-04 23:20:18 +1100258 case 'P':
259 *pflag = 1;
260 break;
261 default:
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000262 error("Invalid flag -%c", cp[1]);
Damien Miller33804262001-02-04 23:20:18 +1100263 return(-1);
264 }
265 cp += 2;
266 *cpp = cp + strspn(cp, WHITESPACE);
267 }
268
269 return(0);
270}
271
Ben Lindstrombba81212001-06-25 05:01:22 +0000272static int
Damien Miller33804262001-02-04 23:20:18 +1100273get_pathname(const char **cpp, char **path)
274{
Damien Millerd7686fd2001-02-10 00:40:03 +1100275 const char *cp = *cpp, *end;
276 char quot;
Damien Miller33804262001-02-04 23:20:18 +1100277 int i;
278
279 cp += strspn(cp, WHITESPACE);
280 if (!*cp) {
281 *cpp = cp;
282 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100283 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100284 }
285
286 /* Check for quoted filenames */
287 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100288 quot = *cp++;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000289
Damien Millerd7686fd2001-02-10 00:40:03 +1100290 end = strchr(cp, quot);
291 if (end == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100292 error("Unterminated quote");
Damien Millerd7686fd2001-02-10 00:40:03 +1100293 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100294 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100295 if (cp == end) {
Damien Miller33804262001-02-04 23:20:18 +1100296 error("Empty quotes");
Damien Millerd7686fd2001-02-10 00:40:03 +1100297 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100298 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100299 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
300 } else {
301 /* Read to end of filename */
302 end = strpbrk(cp, WHITESPACE);
303 if (end == NULL)
304 end = strchr(cp, '\0');
305 *cpp = end + strspn(end, WHITESPACE);
Damien Miller33804262001-02-04 23:20:18 +1100306 }
307
Damien Millerd7686fd2001-02-10 00:40:03 +1100308 i = end - cp;
Damien Miller33804262001-02-04 23:20:18 +1100309
310 *path = xmalloc(i + 1);
311 memcpy(*path, cp, i);
312 (*path)[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100313 return(0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100314
315 fail:
316 *path = NULL;
317 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100318}
319
Ben Lindstrombba81212001-06-25 05:01:22 +0000320static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000321is_dir(char *path)
Damien Miller33804262001-02-04 23:20:18 +1100322{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000323 struct stat sb;
Damien Miller33804262001-02-04 23:20:18 +1100324
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000325 /* XXX: report errors? */
326 if (stat(path, &sb) == -1)
Damien Miller33804262001-02-04 23:20:18 +1100327 return(0);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000328
329 return(sb.st_mode & S_IFDIR);
330}
331
Ben Lindstrombba81212001-06-25 05:01:22 +0000332static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000333remote_is_dir(int in, int out, char *path)
334{
335 Attrib *a;
336
337 /* XXX: report errors? */
338 if ((a = do_stat(in, out, path, 1)) == NULL)
339 return(0);
340 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
341 return(0);
342 return(a->perm & S_IFDIR);
343}
344
Ben Lindstrombba81212001-06-25 05:01:22 +0000345static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000346process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
347{
348 char *abs_src = NULL;
349 char *abs_dst = NULL;
350 char *tmp;
351 glob_t g;
352 int err = 0;
353 int i;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000354
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000355 abs_src = xstrdup(src);
356 abs_src = make_absolute(abs_src, pwd);
357
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000358 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000359 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);
362 err = -1;
363 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100364 }
365
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000366 /* Only one match, dst may be file, directory or unspecified */
367 if (g.gl_pathv[0] && g.gl_matchc == 1) {
368 if (dst) {
369 /* If directory specified, append filename */
370 if (is_dir(dst)) {
371 if (infer_path(g.gl_pathv[0], &tmp)) {
372 err = 1;
373 goto out;
374 }
375 abs_dst = path_append(dst, tmp);
376 xfree(tmp);
377 } else
378 abs_dst = xstrdup(dst);
379 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
380 err = -1;
381 goto out;
382 }
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);
385 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100386 }
387
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000388 /* Multiple matches, dst may be directory or unspecified */
389 if (dst && !is_dir(dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000390 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000391 dst);
392 err = -1;
393 goto out;
394 }
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000395
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000396 for(i = 0; g.gl_pathv[i]; i++) {
397 if (infer_path(g.gl_pathv[i], &tmp)) {
398 err = -1;
399 goto out;
400 }
401 if (dst) {
402 abs_dst = path_append(dst, tmp);
403 xfree(tmp);
404 } else
405 abs_dst = tmp;
406
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)
409 err = -1;
410 xfree(abs_dst);
411 abs_dst = NULL;
412 }
413
414out:
415 xfree(abs_src);
416 if (abs_dst)
417 xfree(abs_dst);
418 globfree(&g);
419 return(err);
420}
421
Ben Lindstrombba81212001-06-25 05:01:22 +0000422static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000423process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
424{
425 char *tmp_dst = NULL;
426 char *abs_dst = NULL;
427 char *tmp;
428 glob_t g;
429 int err = 0;
430 int i;
431
432 if (dst) {
433 tmp_dst = xstrdup(dst);
434 tmp_dst = make_absolute(tmp_dst, pwd);
435 }
436
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000437 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000438 debug3("Looking up %s", src);
439 if (glob(src, 0, NULL, &g)) {
440 error("File \"%s\" not found.", src);
441 err = -1;
442 goto out;
443 }
444
445 /* Only one match, dst may be file, directory or unspecified */
446 if (g.gl_pathv[0] && g.gl_matchc == 1) {
447 if (tmp_dst) {
448 /* If directory specified, append filename */
449 if (remote_is_dir(in, out, tmp_dst)) {
450 if (infer_path(g.gl_pathv[0], &tmp)) {
451 err = 1;
452 goto out;
453 }
454 abs_dst = path_append(tmp_dst, tmp);
455 xfree(tmp);
456 } else
457 abs_dst = xstrdup(tmp_dst);
Ben Lindstrom7527f8b2001-03-24 00:39:12 +0000458 } else {
459 if (infer_path(g.gl_pathv[0], &abs_dst)) {
460 err = -1;
461 goto out;
462 }
463 abs_dst = make_absolute(abs_dst, pwd);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000464 }
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);
467 goto out;
468 }
469
470 /* Multiple matches, dst may be directory or unspecified */
471 if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000472 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000473 tmp_dst);
474 err = -1;
475 goto out;
476 }
477
478 for(i = 0; g.gl_pathv[i]; i++) {
479 if (infer_path(g.gl_pathv[i], &tmp)) {
480 err = -1;
481 goto out;
482 }
483 if (tmp_dst) {
484 abs_dst = path_append(tmp_dst, tmp);
485 xfree(tmp);
486 } else
487 abs_dst = make_absolute(tmp, pwd);
488
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)
491 err = -1;
492 }
493
494out:
495 if (abs_dst)
496 xfree(abs_dst);
497 if (tmp_dst)
498 xfree(tmp_dst);
499 return(err);
Damien Miller33804262001-02-04 23:20:18 +1100500}
501
Ben Lindstrombba81212001-06-25 05:01:22 +0000502static int
Damien Miller33804262001-02-04 23:20:18 +1100503parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
504 char **path1, char **path2)
505{
506 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000507 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000508 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000509 long l;
Damien Miller33804262001-02-04 23:20:18 +1100510 int i, cmdnum;
511
512 /* Skip leading whitespace */
513 cp = cp + strspn(cp, WHITESPACE);
514
515 /* Ignore blank lines */
516 if (!*cp)
517 return(-1);
518
519 /* Figure out which command we have */
520 for(i = 0; cmds[i].c; i++) {
521 int cmdlen = strlen(cmds[i].c);
522
523 /* Check for command followed by whitespace */
524 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
525 strchr(WHITESPACE, cp[cmdlen])) {
526 cp += cmdlen;
527 cp = cp + strspn(cp, WHITESPACE);
528 break;
529 }
530 }
531 cmdnum = cmds[i].n;
532 cmd = cmds[i].c;
533
534 /* Special case */
535 if (*cp == '!') {
536 cp++;
537 cmdnum = I_SHELL;
538 } else if (cmdnum == -1) {
539 error("Invalid command.");
540 return(-1);
541 }
542
543 /* Get arguments and parse flags */
544 *pflag = *n_arg = 0;
545 *path1 = *path2 = NULL;
546 switch (cmdnum) {
547 case I_GET:
548 case I_PUT:
549 if (parse_getput_flags(&cp, pflag))
550 return(-1);
551 /* Get first pathname (mandatory) */
552 if (get_pathname(&cp, path1))
553 return(-1);
554 if (*path1 == NULL) {
555 error("You must specify at least one path after a "
556 "%s command.", cmd);
557 return(-1);
558 }
559 /* Try to get second pathname (optional) */
560 if (get_pathname(&cp, path2))
561 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100562 break;
563 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100564 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100565 if (get_pathname(&cp, path1))
566 return(-1);
567 if (get_pathname(&cp, path2))
568 return(-1);
569 if (!*path1 || !*path2) {
570 error("You must specify two paths after a %s "
571 "command.", cmd);
572 return(-1);
573 }
574 break;
575 case I_RM:
576 case I_MKDIR:
577 case I_RMDIR:
578 case I_CHDIR:
579 case I_LCHDIR:
580 case I_LMKDIR:
581 /* Get pathname (mandatory) */
582 if (get_pathname(&cp, path1))
583 return(-1);
584 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000585 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100586 cmd);
587 return(-1);
588 }
589 break;
590 case I_LS:
591 /* Path is optional */
592 if (get_pathname(&cp, path1))
593 return(-1);
594 break;
595 case I_LLS:
596 case I_SHELL:
597 /* Uses the rest of the line */
598 break;
599 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000600 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100601 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000602 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100603 case I_CHOWN:
604 case I_CHGRP:
605 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000606 l = strtol(cp, &cp2, base);
607 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
608 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100609 error("You must supply a numeric argument "
610 "to the %s command.", cmd);
611 return(-1);
612 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000613 cp = cp2;
614 *n_arg = l;
615 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
616 break;
617 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100618 error("You must supply a numeric argument "
619 "to the %s command.", cmd);
620 return(-1);
621 }
622 cp += strspn(cp, WHITESPACE);
623
624 /* Get pathname (mandatory) */
625 if (get_pathname(&cp, path1))
626 return(-1);
627 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000628 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100629 cmd);
630 return(-1);
631 }
632 break;
633 case I_QUIT:
634 case I_PWD:
635 case I_LPWD:
636 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000637 case I_VERSION:
Damien Miller33804262001-02-04 23:20:18 +1100638 break;
639 default:
640 fatal("Command not implemented");
641 }
642
643 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100644 return(cmdnum);
645}
646
Ben Lindstrombba81212001-06-25 05:01:22 +0000647static int
Damien Miller33804262001-02-04 23:20:18 +1100648parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
649{
Damien Millerd7686fd2001-02-10 00:40:03 +1100650 char *path1, *path2, *tmp;
Damien Miller4870afd2001-03-14 10:27:09 +1100651 int pflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100652 unsigned long n_arg;
653 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000654 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000655 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100656 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100657
658 path1 = path2 = NULL;
659 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
660
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000661 memset(&g, 0, sizeof(g));
662
Damien Miller33804262001-02-04 23:20:18 +1100663 /* Perform command */
664 switch (cmdnum) {
665 case -1:
666 break;
667 case I_GET:
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000668 err = process_get(in, out, path1, path2, *pwd, pflag);
Damien Miller33804262001-02-04 23:20:18 +1100669 break;
670 case I_PUT:
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000671 err = process_put(in, out, path1, path2, *pwd, pflag);
Ben Lindstroma3700052001-04-05 23:26:32 +0000672 break;
673 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100674 path1 = make_absolute(path1, *pwd);
675 path2 = make_absolute(path2, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000676 err = do_rename(in, out, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100677 break;
Damien Miller058316f2001-03-08 10:08:49 +1100678 case I_SYMLINK:
679 if (version < 3) {
680 error("The server (version %d) does not support "
681 "this operation", version);
682 err = -1;
683 } else {
684 path2 = make_absolute(path2, *pwd);
685 err = do_symlink(in, out, path1, path2);
686 }
687 break;
Damien Miller33804262001-02-04 23:20:18 +1100688 case I_RM:
689 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100690 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)
694 err = -1;
695 }
Damien Miller33804262001-02-04 23:20:18 +1100696 break;
697 case I_MKDIR:
698 path1 = make_absolute(path1, *pwd);
699 attrib_clear(&a);
700 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
701 a.perm = 0777;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000702 err = do_mkdir(in, out, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100703 break;
704 case I_RMDIR:
705 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000706 err = do_rmdir(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100707 break;
708 case I_CHDIR:
709 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000710 if ((tmp = do_realpath(in, out, path1)) == NULL) {
711 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100712 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000713 }
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000714 if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100715 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000716 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100717 break;
718 }
719 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
720 error("Can't change directory: Can't check target");
721 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000722 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100723 break;
724 }
725 if (!S_ISDIR(aa->perm)) {
726 error("Can't change directory: \"%s\" is not "
727 "a directory", tmp);
728 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000729 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100730 break;
731 }
Damien Miller33804262001-02-04 23:20:18 +1100732 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100733 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100734 break;
735 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100736 if (!path1) {
737 do_ls(in, out, *pwd);
738 break;
739 }
Damien Miller33804262001-02-04 23:20:18 +1100740 path1 = make_absolute(path1, *pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100741 if ((tmp = do_realpath(in, out, path1)) == NULL)
742 break;
743 xfree(path1);
744 path1 = tmp;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000745 if ((aa = do_stat(in, out, path1, 0)) == NULL)
Damien Millerd7686fd2001-02-10 00:40:03 +1100746 break;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000747 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Millerd7686fd2001-02-10 00:40:03 +1100748 !S_ISDIR(aa->perm)) {
749 error("Can't ls: \"%s\" is not a directory", path1);
750 break;
751 }
752 do_ls(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100753 break;
754 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000755 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100756 error("Couldn't change local directory to "
757 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000758 err = 1;
759 }
Damien Miller33804262001-02-04 23:20:18 +1100760 break;
761 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000762 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100763 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100764 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000765 err = 1;
766 }
Damien Miller33804262001-02-04 23:20:18 +1100767 break;
768 case I_LLS:
769 local_do_ls(cmd);
770 break;
771 case I_SHELL:
772 local_do_shell(cmd);
773 break;
774 case I_LUMASK:
775 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +0000776 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +1100777 break;
778 case I_CHMOD:
779 path1 = make_absolute(path1, *pwd);
780 attrib_clear(&a);
781 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
782 a.perm = n_arg;
Damien Miller4870afd2001-03-14 10:27:09 +1100783 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);
787 }
Kevin Steves62c45db2001-02-05 13:42:43 +0000788 break;
Damien Miller33804262001-02-04 23:20:18 +1100789 case I_CHOWN:
790 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100791 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
792 for(i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000793 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100794 continue;
795 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
796 error("Can't get current ownership of "
797 "remote file \"%s\"", g.gl_pathv[i]);
798 continue;
799 }
800 printf("Changing owner on %s\n", g.gl_pathv[i]);
801 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
802 aa->uid = n_arg;
803 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100804 }
Damien Miller33804262001-02-04 23:20:18 +1100805 break;
806 case I_CHGRP:
807 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100808 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
809 for(i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000810 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100811 continue;
812 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
813 error("Can't get current ownership of "
814 "remote file \"%s\"", g.gl_pathv[i]);
815 continue;
816 }
817 printf("Changing group on %s\n", g.gl_pathv[i]);
818 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
819 aa->gid = n_arg;
820 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100821 }
Damien Miller33804262001-02-04 23:20:18 +1100822 break;
823 case I_PWD:
824 printf("Remote working directory: %s\n", *pwd);
825 break;
826 case I_LPWD:
827 if (!getcwd(path_buf, sizeof(path_buf)))
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000828 error("Couldn't get local cwd: %s",
Damien Miller33804262001-02-04 23:20:18 +1100829 strerror(errno));
830 else
831 printf("Local working directory: %s\n",
832 path_buf);
833 break;
834 case I_QUIT:
835 return(-1);
836 case I_HELP:
837 help();
838 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +0000839 case I_VERSION:
840 printf("SFTP protocol version %d\n", version);
841 break;
Damien Miller33804262001-02-04 23:20:18 +1100842 default:
843 fatal("%d is not implemented", cmdnum);
844 }
845
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000846 if (g.gl_pathc)
847 globfree(&g);
Damien Miller33804262001-02-04 23:20:18 +1100848 if (path1)
849 xfree(path1);
850 if (path2)
851 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000852
853 /* If an error occurs in batch mode we should abort. */
854 if (infile != stdin && err > 0)
855 return -1;
856
Damien Miller33804262001-02-04 23:20:18 +1100857 return(0);
858}
859
860void
Ben Lindstrom63667f62001-04-13 00:00:14 +0000861interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
Damien Miller33804262001-02-04 23:20:18 +1100862{
863 char *pwd;
Ben Lindstrom63667f62001-04-13 00:00:14 +0000864 char *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100865 char cmd[2048];
866
Damien Miller058316f2001-03-08 10:08:49 +1100867 version = do_init(fd_in, fd_out);
868 if (version == -1)
869 fatal("Couldn't initialise connection to server");
870
Damien Miller33804262001-02-04 23:20:18 +1100871 pwd = do_realpath(fd_in, fd_out, ".");
872 if (pwd == NULL)
873 fatal("Need cwd");
874
Ben Lindstrom63667f62001-04-13 00:00:14 +0000875 if (file1 != NULL) {
876 dir = xstrdup(file1);
877 dir = make_absolute(dir, pwd);
878
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);
883 } else {
884 if (file2 == NULL)
885 snprintf(cmd, sizeof cmd, "get %s", dir);
886 else
887 snprintf(cmd, sizeof cmd, "get %s %s", dir,
888 file2);
889
890 parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
891 return;
892 }
893 }
Ben Lindstrom6aebb342001-05-09 00:38:19 +0000894#if HAVE_SETVBUF
Damien Millerd7686fd2001-02-10 00:40:03 +1100895 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000896 setvbuf(infile, NULL, _IOLBF, 0);
Ben Lindstrom6aebb342001-05-09 00:38:19 +0000897#else
898 setlinebuf(stdout);
899 setlinebuf(infile);
900#endif
Damien Miller33804262001-02-04 23:20:18 +1100901
902 for(;;) {
903 char *cp;
904
905 printf("sftp> ");
906
907 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000908 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100909 printf("\n");
910 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000911 } else if (infile != stdin) /* Bluff typing */
912 printf("%s", cmd);
913
Damien Miller33804262001-02-04 23:20:18 +1100914 cp = strrchr(cmd, '\n');
915 if (cp)
916 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000917
Damien Miller33804262001-02-04 23:20:18 +1100918 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
919 break;
920 }
921 xfree(pwd);
922}