blob: 573fea1dff2ef76cc73667cfb82fa0bf852cef8c [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"
Damien Millerefb1edf2001-07-14 12:19:36 +100029RCSID("$OpenBSD: sftp-int.c,v 1.38 2001/07/09 07:04:53 deraadt 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 },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +000089 { "mget", I_GET },
Damien Millerd7686fd2001-02-10 00:40:03 +110090 { "help", I_HELP },
91 { "lcd", I_LCHDIR },
92 { "lchdir", I_LCHDIR },
93 { "lls", I_LLS },
94 { "lmkdir", I_LMKDIR },
Damien Miller058316f2001-03-08 10:08:49 +110095 { "ln", I_SYMLINK },
Damien Millerd7686fd2001-02-10 00:40:03 +110096 { "lpwd", I_LPWD },
97 { "ls", I_LS },
98 { "lumask", I_LUMASK },
99 { "mkdir", I_MKDIR },
100 { "put", I_PUT },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +0000101 { "mput", I_PUT },
Damien Millerd7686fd2001-02-10 00:40:03 +1100102 { "pwd", I_PWD },
103 { "quit", I_QUIT },
104 { "rename", I_RENAME },
105 { "rm", I_RM },
106 { "rmdir", I_RMDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100107 { "symlink", I_SYMLINK },
Ben Lindstromf78682d2001-03-14 21:26:27 +0000108 { "version", I_VERSION },
Kevin Steves62c45db2001-02-05 13:42:43 +0000109 { "!", I_SHELL },
110 { "?", I_HELP },
111 { NULL, -1}
Damien Miller33804262001-02-04 23:20:18 +1100112};
113
Ben Lindstrombba81212001-06-25 05:01:22 +0000114static void
Damien Miller33804262001-02-04 23:20:18 +1100115help(void)
116{
117 printf("Available commands:\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100118 printf("cd path Change remote directory to 'path'\n");
119 printf("lcd path Change local directory to 'path'\n");
120 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
121 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
122 printf("chown own path Change owner of file 'path' to 'own'\n");
123 printf("help Display this help text\n");
124 printf("get remote-path [local-path] Download file\n");
125 printf("lls [ls-options [path]] Display local directory listing\n");
Damien Miller058316f2001-03-08 10:08:49 +1100126 printf("ln oldpath newpath Symlink remote file\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100127 printf("lmkdir path Create local directory\n");
128 printf("lpwd Print local working directory\n");
129 printf("ls [path] Display remote directory listing\n");
130 printf("lumask umask Set local umask to 'umask'\n");
131 printf("mkdir path Create remote directory\n");
132 printf("put local-path [remote-path] Upload file\n");
133 printf("pwd Display remote working directory\n");
134 printf("exit Quit sftp\n");
135 printf("quit Quit sftp\n");
136 printf("rename oldpath newpath Rename remote file\n");
137 printf("rmdir path Remove remote directory\n");
138 printf("rm path Delete remote file\n");
Damien Miller058316f2001-03-08 10:08:49 +1100139 printf("symlink oldpath newpath Symlink remote file\n");
Ben Lindstromf78682d2001-03-14 21:26:27 +0000140 printf("version Show SFTP version\n");
Damien Miller33804262001-02-04 23:20:18 +1100141 printf("!command Execute 'command' in local shell\n");
142 printf("! Escape to local shell\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100143 printf("? Synonym for help\n");
Damien Miller33804262001-02-04 23:20:18 +1100144}
145
Ben Lindstrombba81212001-06-25 05:01:22 +0000146static void
Damien Miller33804262001-02-04 23:20:18 +1100147local_do_shell(const char *args)
148{
Ben Lindstrom206941f2001-04-15 14:27:16 +0000149 int status;
Damien Miller33804262001-02-04 23:20:18 +1100150 char *shell;
151 pid_t pid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000152
Damien Miller33804262001-02-04 23:20:18 +1100153 if (!*args)
154 args = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000155
Damien Miller33804262001-02-04 23:20:18 +1100156 if ((shell = getenv("SHELL")) == NULL)
157 shell = _PATH_BSHELL;
158
159 if ((pid = fork()) == -1)
160 fatal("Couldn't fork: %s", strerror(errno));
161
162 if (pid == 0) {
163 /* XXX: child has pipe fds to ssh subproc open - issue? */
164 if (args) {
165 debug3("Executing %s -c \"%s\"", shell, args);
Damien Millerefb1edf2001-07-14 12:19:36 +1000166 execl(shell, shell, "-c", args, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100167 } else {
168 debug3("Executing %s", shell);
Damien Millerefb1edf2001-07-14 12:19:36 +1000169 execl(shell, shell, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100170 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000171 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
Damien Miller33804262001-02-04 23:20:18 +1100172 strerror(errno));
173 _exit(1);
174 }
175 if (waitpid(pid, &status, 0) == -1)
176 fatal("Couldn't wait for child: %s", strerror(errno));
177 if (!WIFEXITED(status))
178 error("Shell exited abormally");
179 else if (WEXITSTATUS(status))
180 error("Shell exited with status %d", WEXITSTATUS(status));
181}
182
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static void
Damien Miller33804262001-02-04 23:20:18 +1100184local_do_ls(const char *args)
185{
186 if (!args || !*args)
Damien Millerd7686fd2001-02-10 00:40:03 +1100187 local_do_shell(_PATH_LS);
Damien Miller33804262001-02-04 23:20:18 +1100188 else {
Damien Millerd7686fd2001-02-10 00:40:03 +1100189 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
190 char *buf = xmalloc(len);
Damien Miller33804262001-02-04 23:20:18 +1100191
192 /* XXX: quoting - rip quoting code from ftp? */
Damien Millerd7686fd2001-02-10 00:40:03 +1100193 snprintf(buf, len, _PATH_LS " %s", args);
Damien Miller33804262001-02-04 23:20:18 +1100194 local_do_shell(buf);
Damien Millerd7686fd2001-02-10 00:40:03 +1100195 xfree(buf);
Damien Miller33804262001-02-04 23:20:18 +1100196 }
197}
198
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static char *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000200path_append(char *p1, char *p2)
201{
202 char *ret;
Ben Lindstromcf00df62001-03-17 00:37:31 +0000203 int len = strlen(p1) + strlen(p2) + 2;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000204
Ben Lindstromcf00df62001-03-17 00:37:31 +0000205 ret = xmalloc(len);
206 strlcpy(ret, p1, len);
207 strlcat(ret, "/", len);
208 strlcat(ret, p2, len);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000209
210 return(ret);
211}
212
Ben Lindstrombba81212001-06-25 05:01:22 +0000213static char *
Damien Miller33804262001-02-04 23:20:18 +1100214make_absolute(char *p, char *pwd)
215{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000216 char *abs;
Damien Miller33804262001-02-04 23:20:18 +1100217
218 /* Derelativise */
219 if (p && p[0] != '/') {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000220 abs = path_append(pwd, p);
Damien Miller33804262001-02-04 23:20:18 +1100221 xfree(p);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000222 return(abs);
223 } else
224 return(p);
225}
226
Ben Lindstrombba81212001-06-25 05:01:22 +0000227static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000228infer_path(const char *p, char **ifp)
229{
230 char *cp;
231
232 cp = strrchr(p, '/');
233 if (cp == NULL) {
234 *ifp = xstrdup(p);
235 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100236 }
237
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000238 if (!cp[1]) {
239 error("Invalid path");
240 return(-1);
241 }
242
243 *ifp = xstrdup(cp + 1);
244 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100245}
246
Ben Lindstrombba81212001-06-25 05:01:22 +0000247static int
Damien Miller33804262001-02-04 23:20:18 +1100248parse_getput_flags(const char **cpp, int *pflag)
249{
250 const char *cp = *cpp;
251
252 /* Check for flags */
253 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100254 switch (cp[1]) {
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000255 case 'p':
Damien Miller33804262001-02-04 23:20:18 +1100256 case 'P':
257 *pflag = 1;
258 break;
259 default:
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000260 error("Invalid flag -%c", cp[1]);
Damien Miller33804262001-02-04 23:20:18 +1100261 return(-1);
262 }
263 cp += 2;
264 *cpp = cp + strspn(cp, WHITESPACE);
265 }
266
267 return(0);
268}
269
Ben Lindstrombba81212001-06-25 05:01:22 +0000270static int
Damien Miller33804262001-02-04 23:20:18 +1100271get_pathname(const char **cpp, char **path)
272{
Damien Millerd7686fd2001-02-10 00:40:03 +1100273 const char *cp = *cpp, *end;
274 char quot;
Damien Miller33804262001-02-04 23:20:18 +1100275 int i;
276
277 cp += strspn(cp, WHITESPACE);
278 if (!*cp) {
279 *cpp = cp;
280 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100281 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100282 }
283
284 /* Check for quoted filenames */
285 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100286 quot = *cp++;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000287
Damien Millerd7686fd2001-02-10 00:40:03 +1100288 end = strchr(cp, quot);
289 if (end == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100290 error("Unterminated quote");
Damien Millerd7686fd2001-02-10 00:40:03 +1100291 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100292 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100293 if (cp == end) {
Damien Miller33804262001-02-04 23:20:18 +1100294 error("Empty quotes");
Damien Millerd7686fd2001-02-10 00:40:03 +1100295 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100296 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100297 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
298 } else {
299 /* Read to end of filename */
300 end = strpbrk(cp, WHITESPACE);
301 if (end == NULL)
302 end = strchr(cp, '\0');
303 *cpp = end + strspn(end, WHITESPACE);
Damien Miller33804262001-02-04 23:20:18 +1100304 }
305
Damien Millerd7686fd2001-02-10 00:40:03 +1100306 i = end - cp;
Damien Miller33804262001-02-04 23:20:18 +1100307
308 *path = xmalloc(i + 1);
309 memcpy(*path, cp, i);
310 (*path)[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100311 return(0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100312
313 fail:
314 *path = NULL;
315 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100316}
317
Ben Lindstrombba81212001-06-25 05:01:22 +0000318static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000319is_dir(char *path)
Damien Miller33804262001-02-04 23:20:18 +1100320{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000321 struct stat sb;
Damien Miller33804262001-02-04 23:20:18 +1100322
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000323 /* XXX: report errors? */
324 if (stat(path, &sb) == -1)
Damien Miller33804262001-02-04 23:20:18 +1100325 return(0);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000326
327 return(sb.st_mode & S_IFDIR);
328}
329
Ben Lindstrombba81212001-06-25 05:01:22 +0000330static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000331remote_is_dir(int in, int out, char *path)
332{
333 Attrib *a;
334
335 /* XXX: report errors? */
336 if ((a = do_stat(in, out, path, 1)) == NULL)
337 return(0);
338 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
339 return(0);
340 return(a->perm & S_IFDIR);
341}
342
Ben Lindstrombba81212001-06-25 05:01:22 +0000343static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000344process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
345{
346 char *abs_src = NULL;
347 char *abs_dst = NULL;
348 char *tmp;
349 glob_t g;
350 int err = 0;
351 int i;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000352
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000353 abs_src = xstrdup(src);
354 abs_src = make_absolute(abs_src, pwd);
355
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000356 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000357 debug3("Looking up %s", abs_src);
358 if (remote_glob(in, out, abs_src, 0, NULL, &g)) {
359 error("File \"%s\" not found.", abs_src);
360 err = -1;
361 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100362 }
363
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000364 /* Only one match, dst may be file, directory or unspecified */
365 if (g.gl_pathv[0] && g.gl_matchc == 1) {
366 if (dst) {
367 /* If directory specified, append filename */
368 if (is_dir(dst)) {
369 if (infer_path(g.gl_pathv[0], &tmp)) {
370 err = 1;
371 goto out;
372 }
373 abs_dst = path_append(dst, tmp);
374 xfree(tmp);
375 } else
376 abs_dst = xstrdup(dst);
377 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
378 err = -1;
379 goto out;
380 }
381 printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
382 err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag);
383 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100384 }
385
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000386 /* Multiple matches, dst may be directory or unspecified */
387 if (dst && !is_dir(dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000388 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000389 dst);
390 err = -1;
391 goto out;
392 }
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000393
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000394 for(i = 0; g.gl_pathv[i]; i++) {
395 if (infer_path(g.gl_pathv[i], &tmp)) {
396 err = -1;
397 goto out;
398 }
399 if (dst) {
400 abs_dst = path_append(dst, tmp);
401 xfree(tmp);
402 } else
403 abs_dst = tmp;
404
405 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
406 if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
407 err = -1;
408 xfree(abs_dst);
409 abs_dst = NULL;
410 }
411
412out:
413 xfree(abs_src);
414 if (abs_dst)
415 xfree(abs_dst);
416 globfree(&g);
417 return(err);
418}
419
Ben Lindstrombba81212001-06-25 05:01:22 +0000420static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000421process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
422{
423 char *tmp_dst = NULL;
424 char *abs_dst = NULL;
425 char *tmp;
426 glob_t g;
427 int err = 0;
428 int i;
429
430 if (dst) {
431 tmp_dst = xstrdup(dst);
432 tmp_dst = make_absolute(tmp_dst, pwd);
433 }
434
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000435 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000436 debug3("Looking up %s", src);
437 if (glob(src, 0, NULL, &g)) {
438 error("File \"%s\" not found.", src);
439 err = -1;
440 goto out;
441 }
442
443 /* Only one match, dst may be file, directory or unspecified */
444 if (g.gl_pathv[0] && g.gl_matchc == 1) {
445 if (tmp_dst) {
446 /* If directory specified, append filename */
447 if (remote_is_dir(in, out, tmp_dst)) {
448 if (infer_path(g.gl_pathv[0], &tmp)) {
449 err = 1;
450 goto out;
451 }
452 abs_dst = path_append(tmp_dst, tmp);
453 xfree(tmp);
454 } else
455 abs_dst = xstrdup(tmp_dst);
Ben Lindstrom7527f8b2001-03-24 00:39:12 +0000456 } else {
457 if (infer_path(g.gl_pathv[0], &abs_dst)) {
458 err = -1;
459 goto out;
460 }
461 abs_dst = make_absolute(abs_dst, pwd);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000462 }
463 printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
464 err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag);
465 goto out;
466 }
467
468 /* Multiple matches, dst may be directory or unspecified */
469 if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000470 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000471 tmp_dst);
472 err = -1;
473 goto out;
474 }
475
476 for(i = 0; g.gl_pathv[i]; i++) {
477 if (infer_path(g.gl_pathv[i], &tmp)) {
478 err = -1;
479 goto out;
480 }
481 if (tmp_dst) {
482 abs_dst = path_append(tmp_dst, tmp);
483 xfree(tmp);
484 } else
485 abs_dst = make_absolute(tmp, pwd);
486
487 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
488 if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
489 err = -1;
490 }
491
492out:
493 if (abs_dst)
494 xfree(abs_dst);
495 if (tmp_dst)
496 xfree(tmp_dst);
497 return(err);
Damien Miller33804262001-02-04 23:20:18 +1100498}
499
Ben Lindstrombba81212001-06-25 05:01:22 +0000500static int
Damien Miller33804262001-02-04 23:20:18 +1100501parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
502 char **path1, char **path2)
503{
504 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000505 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000506 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000507 long l;
Damien Miller33804262001-02-04 23:20:18 +1100508 int i, cmdnum;
509
510 /* Skip leading whitespace */
511 cp = cp + strspn(cp, WHITESPACE);
512
513 /* Ignore blank lines */
514 if (!*cp)
515 return(-1);
516
517 /* Figure out which command we have */
518 for(i = 0; cmds[i].c; i++) {
519 int cmdlen = strlen(cmds[i].c);
520
521 /* Check for command followed by whitespace */
522 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
523 strchr(WHITESPACE, cp[cmdlen])) {
524 cp += cmdlen;
525 cp = cp + strspn(cp, WHITESPACE);
526 break;
527 }
528 }
529 cmdnum = cmds[i].n;
530 cmd = cmds[i].c;
531
532 /* Special case */
533 if (*cp == '!') {
534 cp++;
535 cmdnum = I_SHELL;
536 } else if (cmdnum == -1) {
537 error("Invalid command.");
538 return(-1);
539 }
540
541 /* Get arguments and parse flags */
542 *pflag = *n_arg = 0;
543 *path1 = *path2 = NULL;
544 switch (cmdnum) {
545 case I_GET:
546 case I_PUT:
547 if (parse_getput_flags(&cp, pflag))
548 return(-1);
549 /* Get first pathname (mandatory) */
550 if (get_pathname(&cp, path1))
551 return(-1);
552 if (*path1 == NULL) {
553 error("You must specify at least one path after a "
554 "%s command.", cmd);
555 return(-1);
556 }
557 /* Try to get second pathname (optional) */
558 if (get_pathname(&cp, path2))
559 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100560 break;
561 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100562 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100563 if (get_pathname(&cp, path1))
564 return(-1);
565 if (get_pathname(&cp, path2))
566 return(-1);
567 if (!*path1 || !*path2) {
568 error("You must specify two paths after a %s "
569 "command.", cmd);
570 return(-1);
571 }
572 break;
573 case I_RM:
574 case I_MKDIR:
575 case I_RMDIR:
576 case I_CHDIR:
577 case I_LCHDIR:
578 case I_LMKDIR:
579 /* Get pathname (mandatory) */
580 if (get_pathname(&cp, path1))
581 return(-1);
582 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000583 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100584 cmd);
585 return(-1);
586 }
587 break;
588 case I_LS:
589 /* Path is optional */
590 if (get_pathname(&cp, path1))
591 return(-1);
592 break;
593 case I_LLS:
594 case I_SHELL:
595 /* Uses the rest of the line */
596 break;
597 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000598 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100599 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000600 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100601 case I_CHOWN:
602 case I_CHGRP:
603 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000604 l = strtol(cp, &cp2, base);
605 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
606 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100607 error("You must supply a numeric argument "
608 "to the %s command.", cmd);
609 return(-1);
610 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000611 cp = cp2;
612 *n_arg = l;
613 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
614 break;
615 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100616 error("You must supply a numeric argument "
617 "to the %s command.", cmd);
618 return(-1);
619 }
620 cp += strspn(cp, WHITESPACE);
621
622 /* Get pathname (mandatory) */
623 if (get_pathname(&cp, path1))
624 return(-1);
625 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000626 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100627 cmd);
628 return(-1);
629 }
630 break;
631 case I_QUIT:
632 case I_PWD:
633 case I_LPWD:
634 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000635 case I_VERSION:
Damien Miller33804262001-02-04 23:20:18 +1100636 break;
637 default:
638 fatal("Command not implemented");
639 }
640
641 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100642 return(cmdnum);
643}
644
Ben Lindstrombba81212001-06-25 05:01:22 +0000645static int
Damien Miller33804262001-02-04 23:20:18 +1100646parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
647{
Damien Millerd7686fd2001-02-10 00:40:03 +1100648 char *path1, *path2, *tmp;
Damien Miller4870afd2001-03-14 10:27:09 +1100649 int pflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100650 unsigned long n_arg;
651 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000652 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000653 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100654 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100655
656 path1 = path2 = NULL;
657 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
658
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000659 memset(&g, 0, sizeof(g));
660
Damien Miller33804262001-02-04 23:20:18 +1100661 /* Perform command */
662 switch (cmdnum) {
663 case -1:
664 break;
665 case I_GET:
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000666 err = process_get(in, out, path1, path2, *pwd, pflag);
Damien Miller33804262001-02-04 23:20:18 +1100667 break;
668 case I_PUT:
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000669 err = process_put(in, out, path1, path2, *pwd, pflag);
Ben Lindstroma3700052001-04-05 23:26:32 +0000670 break;
671 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100672 path1 = make_absolute(path1, *pwd);
673 path2 = make_absolute(path2, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000674 err = do_rename(in, out, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100675 break;
Damien Miller058316f2001-03-08 10:08:49 +1100676 case I_SYMLINK:
677 if (version < 3) {
678 error("The server (version %d) does not support "
679 "this operation", version);
680 err = -1;
681 } else {
682 path2 = make_absolute(path2, *pwd);
683 err = do_symlink(in, out, path1, path2);
684 }
685 break;
Damien Miller33804262001-02-04 23:20:18 +1100686 case I_RM:
687 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100688 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
689 for(i = 0; g.gl_pathv[i]; i++) {
690 printf("Removing %s\n", g.gl_pathv[i]);
691 if (do_rm(in, out, g.gl_pathv[i]) == -1)
692 err = -1;
693 }
Damien Miller33804262001-02-04 23:20:18 +1100694 break;
695 case I_MKDIR:
696 path1 = make_absolute(path1, *pwd);
697 attrib_clear(&a);
698 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
699 a.perm = 0777;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000700 err = do_mkdir(in, out, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100701 break;
702 case I_RMDIR:
703 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000704 err = do_rmdir(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100705 break;
706 case I_CHDIR:
707 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000708 if ((tmp = do_realpath(in, out, path1)) == NULL) {
709 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100710 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000711 }
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000712 if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100713 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000714 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100715 break;
716 }
717 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
718 error("Can't change directory: Can't check target");
719 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000720 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100721 break;
722 }
723 if (!S_ISDIR(aa->perm)) {
724 error("Can't change directory: \"%s\" is not "
725 "a directory", tmp);
726 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000727 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100728 break;
729 }
Damien Miller33804262001-02-04 23:20:18 +1100730 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100731 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100732 break;
733 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100734 if (!path1) {
735 do_ls(in, out, *pwd);
736 break;
737 }
Damien Miller33804262001-02-04 23:20:18 +1100738 path1 = make_absolute(path1, *pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100739 if ((tmp = do_realpath(in, out, path1)) == NULL)
740 break;
741 xfree(path1);
742 path1 = tmp;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000743 if ((aa = do_stat(in, out, path1, 0)) == NULL)
Damien Millerd7686fd2001-02-10 00:40:03 +1100744 break;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000745 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Millerd7686fd2001-02-10 00:40:03 +1100746 !S_ISDIR(aa->perm)) {
747 error("Can't ls: \"%s\" is not a directory", path1);
748 break;
749 }
750 do_ls(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100751 break;
752 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000753 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100754 error("Couldn't change local directory to "
755 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000756 err = 1;
757 }
Damien Miller33804262001-02-04 23:20:18 +1100758 break;
759 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000760 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100761 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100762 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000763 err = 1;
764 }
Damien Miller33804262001-02-04 23:20:18 +1100765 break;
766 case I_LLS:
767 local_do_ls(cmd);
768 break;
769 case I_SHELL:
770 local_do_shell(cmd);
771 break;
772 case I_LUMASK:
773 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +0000774 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +1100775 break;
776 case I_CHMOD:
777 path1 = make_absolute(path1, *pwd);
778 attrib_clear(&a);
779 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
780 a.perm = n_arg;
Damien Miller4870afd2001-03-14 10:27:09 +1100781 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
782 for(i = 0; g.gl_pathv[i]; i++) {
783 printf("Changing mode on %s\n", g.gl_pathv[i]);
784 do_setstat(in, out, g.gl_pathv[i], &a);
785 }
Kevin Steves62c45db2001-02-05 13:42:43 +0000786 break;
Damien Miller33804262001-02-04 23:20:18 +1100787 case I_CHOWN:
788 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100789 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
790 for(i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000791 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100792 continue;
793 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
794 error("Can't get current ownership of "
795 "remote file \"%s\"", g.gl_pathv[i]);
796 continue;
797 }
798 printf("Changing owner on %s\n", g.gl_pathv[i]);
799 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
800 aa->uid = n_arg;
801 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100802 }
Damien Miller33804262001-02-04 23:20:18 +1100803 break;
804 case I_CHGRP:
805 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100806 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
807 for(i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000808 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100809 continue;
810 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
811 error("Can't get current ownership of "
812 "remote file \"%s\"", g.gl_pathv[i]);
813 continue;
814 }
815 printf("Changing group on %s\n", g.gl_pathv[i]);
816 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
817 aa->gid = n_arg;
818 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100819 }
Damien Miller33804262001-02-04 23:20:18 +1100820 break;
821 case I_PWD:
822 printf("Remote working directory: %s\n", *pwd);
823 break;
824 case I_LPWD:
825 if (!getcwd(path_buf, sizeof(path_buf)))
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000826 error("Couldn't get local cwd: %s",
Damien Miller33804262001-02-04 23:20:18 +1100827 strerror(errno));
828 else
829 printf("Local working directory: %s\n",
830 path_buf);
831 break;
832 case I_QUIT:
833 return(-1);
834 case I_HELP:
835 help();
836 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +0000837 case I_VERSION:
838 printf("SFTP protocol version %d\n", version);
839 break;
Damien Miller33804262001-02-04 23:20:18 +1100840 default:
841 fatal("%d is not implemented", cmdnum);
842 }
843
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000844 if (g.gl_pathc)
845 globfree(&g);
Damien Miller33804262001-02-04 23:20:18 +1100846 if (path1)
847 xfree(path1);
848 if (path2)
849 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000850
851 /* If an error occurs in batch mode we should abort. */
852 if (infile != stdin && err > 0)
853 return -1;
854
Damien Miller33804262001-02-04 23:20:18 +1100855 return(0);
856}
857
858void
Ben Lindstrom63667f62001-04-13 00:00:14 +0000859interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
Damien Miller33804262001-02-04 23:20:18 +1100860{
861 char *pwd;
Ben Lindstrom63667f62001-04-13 00:00:14 +0000862 char *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100863 char cmd[2048];
864
Damien Miller058316f2001-03-08 10:08:49 +1100865 version = do_init(fd_in, fd_out);
866 if (version == -1)
867 fatal("Couldn't initialise connection to server");
868
Damien Miller33804262001-02-04 23:20:18 +1100869 pwd = do_realpath(fd_in, fd_out, ".");
870 if (pwd == NULL)
871 fatal("Need cwd");
872
Ben Lindstrom63667f62001-04-13 00:00:14 +0000873 if (file1 != NULL) {
874 dir = xstrdup(file1);
875 dir = make_absolute(dir, pwd);
876
877 if (remote_is_dir(fd_in, fd_out, dir) && file2 == NULL) {
878 printf("Changing to: %s\n", dir);
879 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
880 parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
881 } else {
882 if (file2 == NULL)
883 snprintf(cmd, sizeof cmd, "get %s", dir);
884 else
885 snprintf(cmd, sizeof cmd, "get %s %s", dir,
886 file2);
887
888 parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
889 return;
890 }
891 }
Ben Lindstrom6aebb342001-05-09 00:38:19 +0000892#if HAVE_SETVBUF
Damien Millerd7686fd2001-02-10 00:40:03 +1100893 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000894 setvbuf(infile, NULL, _IOLBF, 0);
Ben Lindstrom6aebb342001-05-09 00:38:19 +0000895#else
896 setlinebuf(stdout);
897 setlinebuf(infile);
898#endif
Damien Miller33804262001-02-04 23:20:18 +1100899
900 for(;;) {
901 char *cp;
902
903 printf("sftp> ");
904
905 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000906 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100907 printf("\n");
908 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000909 } else if (infile != stdin) /* Bluff typing */
910 printf("%s", cmd);
911
Damien Miller33804262001-02-04 23:20:18 +1100912 cp = strrchr(cmd, '\n');
913 if (cp)
914 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000915
Damien Miller33804262001-02-04 23:20:18 +1100916 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
917 break;
918 }
919 xfree(pwd);
920}