blob: aa69d802d266b8955c2b66567ca9020cee59ddf3 [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 Lindstrom7527f8b2001-03-24 00:39:12 +000029RCSID("$OpenBSD: sftp-int.c,v 1.32 2001/03/23 13:10:57 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[] = {
Damien Millerd7686fd2001-02-10 00:40:03 +110081 { "cd", I_CHDIR },
82 { "chdir", I_CHDIR },
83 { "chgrp", I_CHGRP },
84 { "chmod", I_CHMOD },
85 { "chown", I_CHOWN },
86 { "dir", I_LS },
87 { "exit", I_QUIT },
88 { "get", I_GET },
89 { "help", I_HELP },
90 { "lcd", I_LCHDIR },
91 { "lchdir", I_LCHDIR },
92 { "lls", I_LLS },
93 { "lmkdir", I_LMKDIR },
Damien Miller058316f2001-03-08 10:08:49 +110094 { "ln", I_SYMLINK },
Damien Millerd7686fd2001-02-10 00:40:03 +110095 { "lpwd", I_LPWD },
96 { "ls", I_LS },
97 { "lumask", I_LUMASK },
98 { "mkdir", I_MKDIR },
99 { "put", I_PUT },
100 { "pwd", I_PWD },
101 { "quit", I_QUIT },
102 { "rename", I_RENAME },
103 { "rm", I_RM },
104 { "rmdir", I_RMDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100105 { "symlink", I_SYMLINK },
Ben Lindstromf78682d2001-03-14 21:26:27 +0000106 { "version", I_VERSION },
Kevin Steves62c45db2001-02-05 13:42:43 +0000107 { "!", I_SHELL },
108 { "?", I_HELP },
109 { NULL, -1}
Damien Miller33804262001-02-04 23:20:18 +1100110};
111
112void
113help(void)
114{
115 printf("Available commands:\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100116 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");
Damien Miller058316f2001-03-08 10:08:49 +1100124 printf("ln oldpath newpath Symlink remote file\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100125 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");
Damien Miller058316f2001-03-08 10:08:49 +1100137 printf("symlink oldpath newpath Symlink remote file\n");
Ben Lindstromf78682d2001-03-14 21:26:27 +0000138 printf("version Show SFTP version\n");
Damien Miller33804262001-02-04 23:20:18 +1100139 printf("!command Execute 'command' in local shell\n");
140 printf("! Escape to local shell\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100141 printf("? Synonym for help\n");
Damien Miller33804262001-02-04 23:20:18 +1100142}
143
144void
145local_do_shell(const char *args)
146{
147 int ret, status;
148 char *shell;
149 pid_t pid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000150
Damien Miller33804262001-02-04 23:20:18 +1100151 if (!*args)
152 args = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000153
Damien Miller33804262001-02-04 23:20:18 +1100154 if ((shell = getenv("SHELL")) == NULL)
155 shell = _PATH_BSHELL;
156
157 if ((pid = fork()) == -1)
158 fatal("Couldn't fork: %s", strerror(errno));
159
160 if (pid == 0) {
161 /* XXX: child has pipe fds to ssh subproc open - issue? */
162 if (args) {
163 debug3("Executing %s -c \"%s\"", shell, args);
164 ret = execl(shell, shell, "-c", args, NULL);
165 } else {
166 debug3("Executing %s", shell);
167 ret = execl(shell, shell, NULL);
168 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000169 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
Damien Miller33804262001-02-04 23:20:18 +1100170 strerror(errno));
171 _exit(1);
172 }
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));
179}
180
Kevin Stevesef4eea92001-02-05 12:42:17 +0000181void
Damien Miller33804262001-02-04 23:20:18 +1100182local_do_ls(const char *args)
183{
184 if (!args || !*args)
Damien Millerd7686fd2001-02-10 00:40:03 +1100185 local_do_shell(_PATH_LS);
Damien Miller33804262001-02-04 23:20:18 +1100186 else {
Damien Millerd7686fd2001-02-10 00:40:03 +1100187 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
188 char *buf = xmalloc(len);
Damien Miller33804262001-02-04 23:20:18 +1100189
190 /* XXX: quoting - rip quoting code from ftp? */
Damien Millerd7686fd2001-02-10 00:40:03 +1100191 snprintf(buf, len, _PATH_LS " %s", args);
Damien Miller33804262001-02-04 23:20:18 +1100192 local_do_shell(buf);
Damien Millerd7686fd2001-02-10 00:40:03 +1100193 xfree(buf);
Damien Miller33804262001-02-04 23:20:18 +1100194 }
195}
196
197char *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000198path_append(char *p1, char *p2)
199{
200 char *ret;
Ben Lindstromcf00df62001-03-17 00:37:31 +0000201 int len = strlen(p1) + strlen(p2) + 2;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000202
Ben Lindstromcf00df62001-03-17 00:37:31 +0000203 ret = xmalloc(len);
204 strlcpy(ret, p1, len);
205 strlcat(ret, "/", len);
206 strlcat(ret, p2, len);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000207
208 return(ret);
209}
210
211char *
Damien Miller33804262001-02-04 23:20:18 +1100212make_absolute(char *p, char *pwd)
213{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000214 char *abs;
Damien Miller33804262001-02-04 23:20:18 +1100215
216 /* Derelativise */
217 if (p && p[0] != '/') {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000218 abs = path_append(pwd, p);
Damien Miller33804262001-02-04 23:20:18 +1100219 xfree(p);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000220 return(abs);
221 } else
222 return(p);
223}
224
225int
226infer_path(const char *p, char **ifp)
227{
228 char *cp;
229
230 cp = strrchr(p, '/');
231 if (cp == NULL) {
232 *ifp = xstrdup(p);
233 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100234 }
235
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000236 if (!cp[1]) {
237 error("Invalid path");
238 return(-1);
239 }
240
241 *ifp = xstrdup(cp + 1);
242 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100243}
244
245int
246parse_getput_flags(const char **cpp, int *pflag)
247{
248 const char *cp = *cpp;
249
250 /* Check for flags */
251 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100252 switch (cp[1]) {
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000253 case 'p':
Damien Miller33804262001-02-04 23:20:18 +1100254 case 'P':
255 *pflag = 1;
256 break;
257 default:
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000258 error("Invalid flag -%c", cp[1]);
Damien Miller33804262001-02-04 23:20:18 +1100259 return(-1);
260 }
261 cp += 2;
262 *cpp = cp + strspn(cp, WHITESPACE);
263 }
264
265 return(0);
266}
267
268int
269get_pathname(const char **cpp, char **path)
270{
Damien Millerd7686fd2001-02-10 00:40:03 +1100271 const char *cp = *cpp, *end;
272 char quot;
Damien Miller33804262001-02-04 23:20:18 +1100273 int i;
274
275 cp += strspn(cp, WHITESPACE);
276 if (!*cp) {
277 *cpp = cp;
278 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100279 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100280 }
281
282 /* Check for quoted filenames */
283 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100284 quot = *cp++;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000285
Damien Millerd7686fd2001-02-10 00:40:03 +1100286 end = strchr(cp, quot);
287 if (end == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100288 error("Unterminated quote");
Damien Millerd7686fd2001-02-10 00:40:03 +1100289 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100290 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100291 if (cp == end) {
Damien Miller33804262001-02-04 23:20:18 +1100292 error("Empty quotes");
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 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
296 } else {
297 /* Read to end of filename */
298 end = strpbrk(cp, WHITESPACE);
299 if (end == NULL)
300 end = strchr(cp, '\0');
301 *cpp = end + strspn(end, WHITESPACE);
Damien Miller33804262001-02-04 23:20:18 +1100302 }
303
Damien Millerd7686fd2001-02-10 00:40:03 +1100304 i = end - cp;
Damien Miller33804262001-02-04 23:20:18 +1100305
306 *path = xmalloc(i + 1);
307 memcpy(*path, cp, i);
308 (*path)[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100309 return(0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100310
311 fail:
312 *path = NULL;
313 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100314}
315
316int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000317is_dir(char *path)
Damien Miller33804262001-02-04 23:20:18 +1100318{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000319 struct stat sb;
Damien Miller33804262001-02-04 23:20:18 +1100320
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000321 /* XXX: report errors? */
322 if (stat(path, &sb) == -1)
Damien Miller33804262001-02-04 23:20:18 +1100323 return(0);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000324
325 return(sb.st_mode & S_IFDIR);
326}
327
328int
329remote_is_dir(int in, int out, char *path)
330{
331 Attrib *a;
332
333 /* XXX: report errors? */
334 if ((a = do_stat(in, out, path, 1)) == NULL)
335 return(0);
336 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
337 return(0);
338 return(a->perm & S_IFDIR);
339}
340
341int
342process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
343{
344 char *abs_src = NULL;
345 char *abs_dst = NULL;
346 char *tmp;
347 glob_t g;
348 int err = 0;
349 int i;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000350
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000351 abs_src = xstrdup(src);
352 abs_src = make_absolute(abs_src, pwd);
353
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000354 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000355 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);
358 err = -1;
359 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100360 }
361
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000362 /* Only one match, dst may be file, directory or unspecified */
363 if (g.gl_pathv[0] && g.gl_matchc == 1) {
364 if (dst) {
365 /* If directory specified, append filename */
366 if (is_dir(dst)) {
367 if (infer_path(g.gl_pathv[0], &tmp)) {
368 err = 1;
369 goto out;
370 }
371 abs_dst = path_append(dst, tmp);
372 xfree(tmp);
373 } else
374 abs_dst = xstrdup(dst);
375 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
376 err = -1;
377 goto out;
378 }
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);
381 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100382 }
383
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000384 /* Multiple matches, dst may be directory or unspecified */
385 if (dst && !is_dir(dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000386 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000387 dst);
388 err = -1;
389 goto out;
390 }
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000391
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000392 for(i = 0; g.gl_pathv[i]; i++) {
393 if (infer_path(g.gl_pathv[i], &tmp)) {
394 err = -1;
395 goto out;
396 }
397 if (dst) {
398 abs_dst = path_append(dst, tmp);
399 xfree(tmp);
400 } else
401 abs_dst = tmp;
402
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)
405 err = -1;
406 xfree(abs_dst);
407 abs_dst = NULL;
408 }
409
410out:
411 xfree(abs_src);
412 if (abs_dst)
413 xfree(abs_dst);
414 globfree(&g);
415 return(err);
416}
417
418int
419process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
420{
421 char *tmp_dst = NULL;
422 char *abs_dst = NULL;
423 char *tmp;
424 glob_t g;
425 int err = 0;
426 int i;
427
428 if (dst) {
429 tmp_dst = xstrdup(dst);
430 tmp_dst = make_absolute(tmp_dst, pwd);
431 }
432
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000433 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000434 debug3("Looking up %s", src);
435 if (glob(src, 0, NULL, &g)) {
436 error("File \"%s\" not found.", src);
437 err = -1;
438 goto out;
439 }
440
441 /* Only one match, dst may be file, directory or unspecified */
442 if (g.gl_pathv[0] && g.gl_matchc == 1) {
443 if (tmp_dst) {
444 /* If directory specified, append filename */
445 if (remote_is_dir(in, out, tmp_dst)) {
446 if (infer_path(g.gl_pathv[0], &tmp)) {
447 err = 1;
448 goto out;
449 }
450 abs_dst = path_append(tmp_dst, tmp);
451 xfree(tmp);
452 } else
453 abs_dst = xstrdup(tmp_dst);
Ben Lindstrom7527f8b2001-03-24 00:39:12 +0000454 } else {
455 if (infer_path(g.gl_pathv[0], &abs_dst)) {
456 err = -1;
457 goto out;
458 }
459 abs_dst = make_absolute(abs_dst, pwd);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000460 }
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);
463 goto out;
464 }
465
466 /* Multiple matches, dst may be directory or unspecified */
467 if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000468 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000469 tmp_dst);
470 err = -1;
471 goto out;
472 }
473
474 for(i = 0; g.gl_pathv[i]; i++) {
475 if (infer_path(g.gl_pathv[i], &tmp)) {
476 err = -1;
477 goto out;
478 }
479 if (tmp_dst) {
480 abs_dst = path_append(tmp_dst, tmp);
481 xfree(tmp);
482 } else
483 abs_dst = make_absolute(tmp, pwd);
484
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)
487 err = -1;
488 }
489
490out:
491 if (abs_dst)
492 xfree(abs_dst);
493 if (tmp_dst)
494 xfree(tmp_dst);
495 return(err);
Damien Miller33804262001-02-04 23:20:18 +1100496}
497
498int
499parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
500 char **path1, char **path2)
501{
502 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000503 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000504 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000505 long l;
Damien Miller33804262001-02-04 23:20:18 +1100506 int i, cmdnum;
507
508 /* Skip leading whitespace */
509 cp = cp + strspn(cp, WHITESPACE);
510
511 /* Ignore blank lines */
512 if (!*cp)
513 return(-1);
514
515 /* Figure out which command we have */
516 for(i = 0; cmds[i].c; i++) {
517 int cmdlen = strlen(cmds[i].c);
518
519 /* Check for command followed by whitespace */
520 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
521 strchr(WHITESPACE, cp[cmdlen])) {
522 cp += cmdlen;
523 cp = cp + strspn(cp, WHITESPACE);
524 break;
525 }
526 }
527 cmdnum = cmds[i].n;
528 cmd = cmds[i].c;
529
530 /* Special case */
531 if (*cp == '!') {
532 cp++;
533 cmdnum = I_SHELL;
534 } else if (cmdnum == -1) {
535 error("Invalid command.");
536 return(-1);
537 }
538
539 /* Get arguments and parse flags */
540 *pflag = *n_arg = 0;
541 *path1 = *path2 = NULL;
542 switch (cmdnum) {
543 case I_GET:
544 case I_PUT:
545 if (parse_getput_flags(&cp, pflag))
546 return(-1);
547 /* Get first pathname (mandatory) */
548 if (get_pathname(&cp, path1))
549 return(-1);
550 if (*path1 == NULL) {
551 error("You must specify at least one path after a "
552 "%s command.", cmd);
553 return(-1);
554 }
555 /* Try to get second pathname (optional) */
556 if (get_pathname(&cp, path2))
557 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100558 break;
559 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100560 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100561 if (get_pathname(&cp, path1))
562 return(-1);
563 if (get_pathname(&cp, path2))
564 return(-1);
565 if (!*path1 || !*path2) {
566 error("You must specify two paths after a %s "
567 "command.", cmd);
568 return(-1);
569 }
570 break;
571 case I_RM:
572 case I_MKDIR:
573 case I_RMDIR:
574 case I_CHDIR:
575 case I_LCHDIR:
576 case I_LMKDIR:
577 /* Get pathname (mandatory) */
578 if (get_pathname(&cp, path1))
579 return(-1);
580 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000581 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100582 cmd);
583 return(-1);
584 }
585 break;
586 case I_LS:
587 /* Path is optional */
588 if (get_pathname(&cp, path1))
589 return(-1);
590 break;
591 case I_LLS:
592 case I_SHELL:
593 /* Uses the rest of the line */
594 break;
595 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000596 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100597 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000598 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100599 case I_CHOWN:
600 case I_CHGRP:
601 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000602 l = strtol(cp, &cp2, base);
603 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
604 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100605 error("You must supply a numeric argument "
606 "to the %s command.", cmd);
607 return(-1);
608 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000609 cp = cp2;
610 *n_arg = l;
611 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
612 break;
613 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100614 error("You must supply a numeric argument "
615 "to the %s command.", cmd);
616 return(-1);
617 }
618 cp += strspn(cp, WHITESPACE);
619
620 /* Get pathname (mandatory) */
621 if (get_pathname(&cp, path1))
622 return(-1);
623 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000624 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100625 cmd);
626 return(-1);
627 }
628 break;
629 case I_QUIT:
630 case I_PWD:
631 case I_LPWD:
632 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000633 case I_VERSION:
Damien Miller33804262001-02-04 23:20:18 +1100634 break;
635 default:
636 fatal("Command not implemented");
637 }
638
639 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100640 return(cmdnum);
641}
642
643int
644parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
645{
Damien Millerd7686fd2001-02-10 00:40:03 +1100646 char *path1, *path2, *tmp;
Damien Miller4870afd2001-03-14 10:27:09 +1100647 int pflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100648 unsigned long n_arg;
649 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000650 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000651 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100652 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100653
654 path1 = path2 = NULL;
655 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
656
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000657 memset(&g, 0, sizeof(g));
658
Damien Miller33804262001-02-04 23:20:18 +1100659 /* Perform command */
660 switch (cmdnum) {
661 case -1:
662 break;
663 case I_GET:
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000664 err = process_get(in, out, path1, path2, *pwd, pflag);
Damien Miller33804262001-02-04 23:20:18 +1100665 break;
666 case I_PUT:
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000667 err = process_put(in, out, path1, path2, *pwd, pflag);
Damien Miller4870afd2001-03-14 10:27:09 +1100668 break;
669 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100670 path1 = make_absolute(path1, *pwd);
671 path2 = make_absolute(path2, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000672 err = do_rename(in, out, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100673 break;
Damien Miller058316f2001-03-08 10:08:49 +1100674 case I_SYMLINK:
675 if (version < 3) {
676 error("The server (version %d) does not support "
677 "this operation", version);
678 err = -1;
679 } else {
680 path2 = make_absolute(path2, *pwd);
681 err = do_symlink(in, out, path1, path2);
682 }
683 break;
Damien Miller33804262001-02-04 23:20:18 +1100684 case I_RM:
685 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100686 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)
690 err = -1;
691 }
Damien Miller33804262001-02-04 23:20:18 +1100692 break;
693 case I_MKDIR:
694 path1 = make_absolute(path1, *pwd);
695 attrib_clear(&a);
696 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
697 a.perm = 0777;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000698 err = do_mkdir(in, out, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100699 break;
700 case I_RMDIR:
701 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000702 err = do_rmdir(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100703 break;
704 case I_CHDIR:
705 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000706 if ((tmp = do_realpath(in, out, path1)) == NULL) {
707 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100708 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000709 }
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000710 if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100711 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000712 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100713 break;
714 }
715 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
716 error("Can't change directory: Can't check target");
717 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000718 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100719 break;
720 }
721 if (!S_ISDIR(aa->perm)) {
722 error("Can't change directory: \"%s\" is not "
723 "a directory", tmp);
724 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000725 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100726 break;
727 }
Damien Miller33804262001-02-04 23:20:18 +1100728 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100729 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100730 break;
731 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100732 if (!path1) {
733 do_ls(in, out, *pwd);
734 break;
735 }
Damien Miller33804262001-02-04 23:20:18 +1100736 path1 = make_absolute(path1, *pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100737 if ((tmp = do_realpath(in, out, path1)) == NULL)
738 break;
739 xfree(path1);
740 path1 = tmp;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000741 if ((aa = do_stat(in, out, path1, 0)) == NULL)
Damien Millerd7686fd2001-02-10 00:40:03 +1100742 break;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000743 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Millerd7686fd2001-02-10 00:40:03 +1100744 !S_ISDIR(aa->perm)) {
745 error("Can't ls: \"%s\" is not a directory", path1);
746 break;
747 }
748 do_ls(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100749 break;
750 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000751 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100752 error("Couldn't change local directory to "
753 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000754 err = 1;
755 }
Damien Miller33804262001-02-04 23:20:18 +1100756 break;
757 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000758 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100759 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100760 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000761 err = 1;
762 }
Damien Miller33804262001-02-04 23:20:18 +1100763 break;
764 case I_LLS:
765 local_do_ls(cmd);
766 break;
767 case I_SHELL:
768 local_do_shell(cmd);
769 break;
770 case I_LUMASK:
771 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +0000772 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +1100773 break;
774 case I_CHMOD:
775 path1 = make_absolute(path1, *pwd);
776 attrib_clear(&a);
777 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
778 a.perm = n_arg;
Damien Miller4870afd2001-03-14 10:27:09 +1100779 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);
783 }
Kevin Steves62c45db2001-02-05 13:42:43 +0000784 break;
Damien Miller33804262001-02-04 23:20:18 +1100785 case I_CHOWN:
786 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100787 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
788 for(i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000789 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100790 continue;
791 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
792 error("Can't get current ownership of "
793 "remote file \"%s\"", g.gl_pathv[i]);
794 continue;
795 }
796 printf("Changing owner on %s\n", g.gl_pathv[i]);
797 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
798 aa->uid = n_arg;
799 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100800 }
Damien Miller33804262001-02-04 23:20:18 +1100801 break;
802 case I_CHGRP:
803 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100804 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
805 for(i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000806 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100807 continue;
808 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
809 error("Can't get current ownership of "
810 "remote file \"%s\"", g.gl_pathv[i]);
811 continue;
812 }
813 printf("Changing group on %s\n", g.gl_pathv[i]);
814 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
815 aa->gid = n_arg;
816 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100817 }
Damien Miller33804262001-02-04 23:20:18 +1100818 break;
819 case I_PWD:
820 printf("Remote working directory: %s\n", *pwd);
821 break;
822 case I_LPWD:
823 if (!getcwd(path_buf, sizeof(path_buf)))
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000824 error("Couldn't get local cwd: %s",
Damien Miller33804262001-02-04 23:20:18 +1100825 strerror(errno));
826 else
827 printf("Local working directory: %s\n",
828 path_buf);
829 break;
830 case I_QUIT:
831 return(-1);
832 case I_HELP:
833 help();
834 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +0000835 case I_VERSION:
836 printf("SFTP protocol version %d\n", version);
837 break;
Damien Miller33804262001-02-04 23:20:18 +1100838 default:
839 fatal("%d is not implemented", cmdnum);
840 }
841
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000842 if (g.gl_pathc)
843 globfree(&g);
Damien Miller33804262001-02-04 23:20:18 +1100844 if (path1)
845 xfree(path1);
846 if (path2)
847 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000848
849 /* If an error occurs in batch mode we should abort. */
850 if (infile != stdin && err > 0)
851 return -1;
852
Damien Miller33804262001-02-04 23:20:18 +1100853 return(0);
854}
855
856void
857interactive_loop(int fd_in, int fd_out)
858{
859 char *pwd;
860 char cmd[2048];
861
Damien Miller058316f2001-03-08 10:08:49 +1100862 version = do_init(fd_in, fd_out);
863 if (version == -1)
864 fatal("Couldn't initialise connection to server");
865
Damien Miller33804262001-02-04 23:20:18 +1100866 pwd = do_realpath(fd_in, fd_out, ".");
867 if (pwd == NULL)
868 fatal("Need cwd");
869
Damien Millerd7686fd2001-02-10 00:40:03 +1100870 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000871 setvbuf(infile, NULL, _IOLBF, 0);
Damien Miller33804262001-02-04 23:20:18 +1100872
873 for(;;) {
874 char *cp;
875
876 printf("sftp> ");
877
878 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000879 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100880 printf("\n");
881 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000882 } else if (infile != stdin) /* Bluff typing */
883 printf("%s", cmd);
884
Damien Miller33804262001-02-04 23:20:18 +1100885 cp = strrchr(cmd, '\n');
886 if (cp)
887 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000888
Damien Miller33804262001-02-04 23:20:18 +1100889 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
890 break;
891 }
892 xfree(pwd);
893}