blob: bc5cce8de4b6427eb567fbd4d91a90497741399b [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
Damien Miller3db5f532002-02-13 14:10:32 +11002 * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
Damien Miller33804262001-02-04 23:20:18 +11003 *
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 Miller33804262001-02-04 23:20:18 +110025/* XXX: recursive operations */
26
27#include "includes.h"
Damien Miller939cd382003-11-17 21:17:24 +110028RCSID("$OpenBSD: sftp-int.c,v 1.64 2003/11/08 19:17:29 jmc Exp $");
Damien Miller4870afd2001-03-14 10:27:09 +110029
Damien Miller33804262001-02-04 23:20:18 +110030#include "buffer.h"
31#include "xmalloc.h"
32#include "log.h"
33#include "pathnames.h"
34
35#include "sftp.h"
36#include "sftp-common.h"
Damien Miller4870afd2001-03-14 10:27:09 +110037#include "sftp-glob.h"
Damien Miller33804262001-02-04 23:20:18 +110038#include "sftp-client.h"
39#include "sftp-int.h"
40
Damien Miller058316f2001-03-08 10:08:49 +110041/* File to read commands from */
42extern FILE *infile;
43
Damien Miller8829d362002-02-08 22:04:05 +110044/* Size of buffer used when copying files */
45extern size_t copy_buffer_len;
46
Damien Miller16a13332002-02-13 14:03:56 +110047/* Number of concurrent outstanding requests */
48extern int num_requests;
49
Damien Miller62d57f62003-01-10 21:43:24 +110050/* This is set to 0 if the progressmeter is not desired. */
51int showprogress = 1;
52
Damien Miller939cd382003-11-17 21:17:24 +110053/* Separators for interactive commands */
Damien Miller33804262001-02-04 23:20:18 +110054#define WHITESPACE " \t\r\n"
55
Damien Miller19c8f2b2003-05-15 13:49:21 +100056/* Define what type of ls view (0 - multi-column) */
57#define LONG_VIEW 1 /* Full view ala ls -l */
58#define SHORT_VIEW 2 /* Single row view ala ls -1 */
59
Damien Miller33804262001-02-04 23:20:18 +110060/* Commands for interactive mode */
61#define I_CHDIR 1
62#define I_CHGRP 2
63#define I_CHMOD 3
64#define I_CHOWN 4
65#define I_GET 5
66#define I_HELP 6
67#define I_LCHDIR 7
68#define I_LLS 8
69#define I_LMKDIR 9
70#define I_LPWD 10
71#define I_LS 11
72#define I_LUMASK 12
73#define I_MKDIR 13
74#define I_PUT 14
75#define I_PWD 15
76#define I_QUIT 16
77#define I_RENAME 17
78#define I_RM 18
79#define I_RMDIR 19
80#define I_SHELL 20
Damien Miller058316f2001-03-08 10:08:49 +110081#define I_SYMLINK 21
Ben Lindstromf78682d2001-03-14 21:26:27 +000082#define I_VERSION 22
Damien Miller62d57f62003-01-10 21:43:24 +110083#define I_PROGRESS 23
Damien Miller33804262001-02-04 23:20:18 +110084
85struct CMD {
Damien Miller33804262001-02-04 23:20:18 +110086 const char *c;
Kevin Steves62c45db2001-02-05 13:42:43 +000087 const int n;
Damien Miller33804262001-02-04 23:20:18 +110088};
89
Damien Millerdc708572003-01-14 22:24:05 +110090static const struct CMD cmds[] = {
Ben Lindstrom59e12492001-08-15 23:22:56 +000091 { "bye", I_QUIT },
Damien Millerd7686fd2001-02-10 00:40:03 +110092 { "cd", I_CHDIR },
93 { "chdir", I_CHDIR },
94 { "chgrp", I_CHGRP },
95 { "chmod", I_CHMOD },
96 { "chown", I_CHOWN },
97 { "dir", I_LS },
98 { "exit", I_QUIT },
99 { "get", I_GET },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +0000100 { "mget", I_GET },
Damien Millerd7686fd2001-02-10 00:40:03 +1100101 { "help", I_HELP },
102 { "lcd", I_LCHDIR },
103 { "lchdir", I_LCHDIR },
104 { "lls", I_LLS },
105 { "lmkdir", I_LMKDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100106 { "ln", I_SYMLINK },
Damien Millerd7686fd2001-02-10 00:40:03 +1100107 { "lpwd", I_LPWD },
108 { "ls", I_LS },
109 { "lumask", I_LUMASK },
110 { "mkdir", I_MKDIR },
Damien Miller62d57f62003-01-10 21:43:24 +1100111 { "progress", I_PROGRESS },
Damien Millerd7686fd2001-02-10 00:40:03 +1100112 { "put", I_PUT },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +0000113 { "mput", I_PUT },
Damien Millerd7686fd2001-02-10 00:40:03 +1100114 { "pwd", I_PWD },
115 { "quit", I_QUIT },
116 { "rename", I_RENAME },
117 { "rm", I_RM },
118 { "rmdir", I_RMDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100119 { "symlink", I_SYMLINK },
Ben Lindstromf78682d2001-03-14 21:26:27 +0000120 { "version", I_VERSION },
Kevin Steves62c45db2001-02-05 13:42:43 +0000121 { "!", I_SHELL },
122 { "?", I_HELP },
123 { NULL, -1}
Damien Miller33804262001-02-04 23:20:18 +1100124};
125
Ben Lindstrombba81212001-06-25 05:01:22 +0000126static void
Damien Miller33804262001-02-04 23:20:18 +1100127help(void)
128{
129 printf("Available commands:\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100130 printf("cd path Change remote directory to 'path'\n");
131 printf("lcd path Change local directory to 'path'\n");
132 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
133 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
134 printf("chown own path Change owner of file 'path' to 'own'\n");
135 printf("help Display this help text\n");
136 printf("get remote-path [local-path] Download file\n");
137 printf("lls [ls-options [path]] Display local directory listing\n");
Damien Miller058316f2001-03-08 10:08:49 +1100138 printf("ln oldpath newpath Symlink remote file\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100139 printf("lmkdir path Create local directory\n");
140 printf("lpwd Print local working directory\n");
141 printf("ls [path] Display remote directory listing\n");
142 printf("lumask umask Set local umask to 'umask'\n");
143 printf("mkdir path Create remote directory\n");
Damien Miller01413192003-01-14 22:22:11 +1100144 printf("progress Toggle display of progress meter\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100145 printf("put local-path [remote-path] Upload file\n");
146 printf("pwd Display remote working directory\n");
147 printf("exit Quit sftp\n");
148 printf("quit Quit sftp\n");
149 printf("rename oldpath newpath Rename remote file\n");
150 printf("rmdir path Remove remote directory\n");
151 printf("rm path Delete remote file\n");
Damien Miller058316f2001-03-08 10:08:49 +1100152 printf("symlink oldpath newpath Symlink remote file\n");
Ben Lindstromf78682d2001-03-14 21:26:27 +0000153 printf("version Show SFTP version\n");
Damien Miller33804262001-02-04 23:20:18 +1100154 printf("!command Execute 'command' in local shell\n");
155 printf("! Escape to local shell\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100156 printf("? Synonym for help\n");
Damien Miller33804262001-02-04 23:20:18 +1100157}
158
Ben Lindstrombba81212001-06-25 05:01:22 +0000159static void
Damien Miller33804262001-02-04 23:20:18 +1100160local_do_shell(const char *args)
161{
Ben Lindstrom206941f2001-04-15 14:27:16 +0000162 int status;
Damien Miller33804262001-02-04 23:20:18 +1100163 char *shell;
164 pid_t pid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000165
Damien Miller33804262001-02-04 23:20:18 +1100166 if (!*args)
167 args = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000168
Damien Miller33804262001-02-04 23:20:18 +1100169 if ((shell = getenv("SHELL")) == NULL)
170 shell = _PATH_BSHELL;
171
172 if ((pid = fork()) == -1)
173 fatal("Couldn't fork: %s", strerror(errno));
174
175 if (pid == 0) {
176 /* XXX: child has pipe fds to ssh subproc open - issue? */
177 if (args) {
178 debug3("Executing %s -c \"%s\"", shell, args);
Damien Millerefb1edf2001-07-14 12:19:36 +1000179 execl(shell, shell, "-c", args, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100180 } else {
181 debug3("Executing %s", shell);
Damien Millerefb1edf2001-07-14 12:19:36 +1000182 execl(shell, shell, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100183 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000184 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
Damien Miller33804262001-02-04 23:20:18 +1100185 strerror(errno));
186 _exit(1);
187 }
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000188 while (waitpid(pid, &status, 0) == -1)
189 if (errno != EINTR)
190 fatal("Couldn't wait for child: %s", strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +1100191 if (!WIFEXITED(status))
192 error("Shell exited abormally");
193 else if (WEXITSTATUS(status))
194 error("Shell exited with status %d", WEXITSTATUS(status));
195}
196
Ben Lindstrombba81212001-06-25 05:01:22 +0000197static void
Damien Miller33804262001-02-04 23:20:18 +1100198local_do_ls(const char *args)
199{
200 if (!args || !*args)
Damien Millerd7686fd2001-02-10 00:40:03 +1100201 local_do_shell(_PATH_LS);
Damien Miller33804262001-02-04 23:20:18 +1100202 else {
Damien Millerd7686fd2001-02-10 00:40:03 +1100203 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
204 char *buf = xmalloc(len);
Damien Miller33804262001-02-04 23:20:18 +1100205
206 /* XXX: quoting - rip quoting code from ftp? */
Damien Millerd7686fd2001-02-10 00:40:03 +1100207 snprintf(buf, len, _PATH_LS " %s", args);
Damien Miller33804262001-02-04 23:20:18 +1100208 local_do_shell(buf);
Damien Millerd7686fd2001-02-10 00:40:03 +1100209 xfree(buf);
Damien Miller33804262001-02-04 23:20:18 +1100210 }
211}
212
Damien Millere1a49812002-09-12 09:54:25 +1000213/* Strip one path (usually the pwd) from the start of another */
214static char *
215path_strip(char *path, char *strip)
216{
217 size_t len;
Damien Millere1a49812002-09-12 09:54:25 +1000218
219 if (strip == NULL)
220 return (xstrdup(path));
221
222 len = strlen(strip);
223 if (strip != NULL && strncmp(path, strip, len) == 0) {
224 if (strip[len - 1] != '/' && path[len] == '/')
225 len++;
226 return (xstrdup(path + len));
227 }
228
229 return (xstrdup(path));
230}
231
Ben Lindstrombba81212001-06-25 05:01:22 +0000232static char *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000233path_append(char *p1, char *p2)
234{
235 char *ret;
Ben Lindstromcf00df62001-03-17 00:37:31 +0000236 int len = strlen(p1) + strlen(p2) + 2;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000237
Ben Lindstromcf00df62001-03-17 00:37:31 +0000238 ret = xmalloc(len);
239 strlcpy(ret, p1, len);
Damien Millere1a49812002-09-12 09:54:25 +1000240 if (p1[strlen(p1) - 1] != '/')
Ben Lindstrom95148e32001-08-06 21:30:53 +0000241 strlcat(ret, "/", len);
Ben Lindstromcf00df62001-03-17 00:37:31 +0000242 strlcat(ret, p2, len);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000243
244 return(ret);
245}
246
Ben Lindstrombba81212001-06-25 05:01:22 +0000247static char *
Damien Miller33804262001-02-04 23:20:18 +1100248make_absolute(char *p, char *pwd)
249{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000250 char *abs;
Damien Miller33804262001-02-04 23:20:18 +1100251
252 /* Derelativise */
253 if (p && p[0] != '/') {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000254 abs = path_append(pwd, p);
Damien Miller33804262001-02-04 23:20:18 +1100255 xfree(p);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000256 return(abs);
257 } else
258 return(p);
259}
260
Ben Lindstrombba81212001-06-25 05:01:22 +0000261static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000262infer_path(const char *p, char **ifp)
263{
264 char *cp;
265
266 cp = strrchr(p, '/');
267 if (cp == NULL) {
268 *ifp = xstrdup(p);
269 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100270 }
271
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000272 if (!cp[1]) {
273 error("Invalid path");
274 return(-1);
275 }
276
277 *ifp = xstrdup(cp + 1);
278 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100279}
280
Ben Lindstrombba81212001-06-25 05:01:22 +0000281static int
Damien Miller33804262001-02-04 23:20:18 +1100282parse_getput_flags(const char **cpp, int *pflag)
283{
284 const char *cp = *cpp;
285
286 /* Check for flags */
287 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100288 switch (cp[1]) {
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000289 case 'p':
Damien Miller33804262001-02-04 23:20:18 +1100290 case 'P':
291 *pflag = 1;
292 break;
293 default:
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000294 error("Invalid flag -%c", cp[1]);
Damien Miller33804262001-02-04 23:20:18 +1100295 return(-1);
296 }
297 cp += 2;
298 *cpp = cp + strspn(cp, WHITESPACE);
299 }
300
301 return(0);
302}
303
Ben Lindstrombba81212001-06-25 05:01:22 +0000304static int
Damien Millere1a49812002-09-12 09:54:25 +1000305parse_ls_flags(const char **cpp, int *lflag)
306{
307 const char *cp = *cpp;
308
309 /* Check for flags */
310 if (cp++[0] == '-') {
311 for(; strchr(WHITESPACE, *cp) == NULL; cp++) {
312 switch (*cp) {
313 case 'l':
Damien Miller19c8f2b2003-05-15 13:49:21 +1000314 *lflag = LONG_VIEW;
315 break;
316 case '1':
317 *lflag = SHORT_VIEW;
Damien Millere1a49812002-09-12 09:54:25 +1000318 break;
319 default:
320 error("Invalid flag -%c", *cp);
321 return(-1);
322 }
323 }
324 *cpp = cp + strspn(cp, WHITESPACE);
325 }
326
327 return(0);
328}
329
330static int
Damien Miller33804262001-02-04 23:20:18 +1100331get_pathname(const char **cpp, char **path)
332{
Damien Millerd7686fd2001-02-10 00:40:03 +1100333 const char *cp = *cpp, *end;
334 char quot;
Darren Tucker554d5b52003-07-19 20:09:21 +1000335 int i, j;
Damien Miller33804262001-02-04 23:20:18 +1100336
337 cp += strspn(cp, WHITESPACE);
338 if (!*cp) {
339 *cpp = cp;
340 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100341 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100342 }
343
Darren Tucker554d5b52003-07-19 20:09:21 +1000344 *path = xmalloc(strlen(cp) + 1);
345
Damien Miller33804262001-02-04 23:20:18 +1100346 /* Check for quoted filenames */
347 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100348 quot = *cp++;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000349
Darren Tucker554d5b52003-07-19 20:09:21 +1000350 /* Search for terminating quote, unescape some chars */
351 for (i = j = 0; i <= strlen(cp); i++) {
352 if (cp[i] == quot) { /* Found quote */
Darren Tucker64dbccc2003-10-08 17:34:38 +1000353 i++;
Darren Tucker554d5b52003-07-19 20:09:21 +1000354 (*path)[j] = '\0';
Damien Miller5c3a5582003-09-23 22:12:38 +1000355 i++;
Darren Tucker554d5b52003-07-19 20:09:21 +1000356 break;
357 }
358 if (cp[i] == '\0') { /* End of string */
359 error("Unterminated quote");
360 goto fail;
361 }
362 if (cp[i] == '\\') { /* Escaped characters */
363 i++;
364 if (cp[i] != '\'' && cp[i] != '\"' &&
365 cp[i] != '\\') {
366 error("Bad escaped character '\%c'",
367 cp[i]);
368 goto fail;
369 }
370 }
371 (*path)[j++] = cp[i];
Damien Miller33804262001-02-04 23:20:18 +1100372 }
Darren Tucker554d5b52003-07-19 20:09:21 +1000373
374 if (j == 0) {
Damien Miller33804262001-02-04 23:20:18 +1100375 error("Empty quotes");
Damien Millerd7686fd2001-02-10 00:40:03 +1100376 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100377 }
Darren Tucker554d5b52003-07-19 20:09:21 +1000378 *cpp = cp + i + strspn(cp + i, WHITESPACE);
Damien Millerd7686fd2001-02-10 00:40:03 +1100379 } else {
380 /* Read to end of filename */
381 end = strpbrk(cp, WHITESPACE);
382 if (end == NULL)
383 end = strchr(cp, '\0');
384 *cpp = end + strspn(end, WHITESPACE);
Darren Tucker554d5b52003-07-19 20:09:21 +1000385
386 memcpy(*path, cp, end - cp);
387 (*path)[end - cp] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100388 }
Darren Tucker554d5b52003-07-19 20:09:21 +1000389 return (0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100390
391 fail:
Darren Tucker554d5b52003-07-19 20:09:21 +1000392 xfree(*path);
393 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100394 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100395}
396
Ben Lindstrombba81212001-06-25 05:01:22 +0000397static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000398is_dir(char *path)
Damien Miller33804262001-02-04 23:20:18 +1100399{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000400 struct stat sb;
Damien Miller33804262001-02-04 23:20:18 +1100401
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000402 /* XXX: report errors? */
403 if (stat(path, &sb) == -1)
Damien Miller33804262001-02-04 23:20:18 +1100404 return(0);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000405
406 return(sb.st_mode & S_IFDIR);
407}
408
Ben Lindstrombba81212001-06-25 05:01:22 +0000409static int
Damien Miller5fa01fd2003-01-14 22:24:47 +1100410is_reg(char *path)
411{
412 struct stat sb;
413
414 if (stat(path, &sb) == -1)
415 fatal("stat %s: %s", path, strerror(errno));
416
417 return(S_ISREG(sb.st_mode));
418}
419
420static int
Damien Miller3db5f532002-02-13 14:10:32 +1100421remote_is_dir(struct sftp_conn *conn, char *path)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000422{
423 Attrib *a;
424
425 /* XXX: report errors? */
Damien Miller3db5f532002-02-13 14:10:32 +1100426 if ((a = do_stat(conn, path, 1)) == NULL)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000427 return(0);
428 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
429 return(0);
430 return(a->perm & S_IFDIR);
431}
432
Ben Lindstrombba81212001-06-25 05:01:22 +0000433static int
Damien Miller3db5f532002-02-13 14:10:32 +1100434process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000435{
436 char *abs_src = NULL;
437 char *abs_dst = NULL;
438 char *tmp;
439 glob_t g;
440 int err = 0;
441 int i;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000442
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000443 abs_src = xstrdup(src);
444 abs_src = make_absolute(abs_src, pwd);
445
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000446 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000447 debug3("Looking up %s", abs_src);
Damien Miller3db5f532002-02-13 14:10:32 +1100448 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000449 error("File \"%s\" not found.", abs_src);
450 err = -1;
451 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100452 }
453
Damien Miller4962ed62003-05-15 13:48:59 +1000454 /* If multiple matches, dst must be a directory or unspecified */
455 if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000456 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000457 dst);
458 err = -1;
459 goto out;
460 }
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000461
Damien Miller9f0f5c62001-12-21 14:45:46 +1100462 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000463 if (infer_path(g.gl_pathv[i], &tmp)) {
464 err = -1;
465 goto out;
466 }
Damien Miller4962ed62003-05-15 13:48:59 +1000467
468 if (g.gl_matchc == 1 && dst) {
469 /* If directory specified, append filename */
470 if (is_dir(dst)) {
471 if (infer_path(g.gl_pathv[0], &tmp)) {
472 err = 1;
473 goto out;
474 }
475 abs_dst = path_append(dst, tmp);
476 xfree(tmp);
477 } else
478 abs_dst = xstrdup(dst);
479 } else if (dst) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000480 abs_dst = path_append(dst, tmp);
481 xfree(tmp);
482 } else
483 abs_dst = tmp;
484
485 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100486 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000487 err = -1;
488 xfree(abs_dst);
489 abs_dst = NULL;
490 }
491
492out:
493 xfree(abs_src);
494 if (abs_dst)
495 xfree(abs_dst);
496 globfree(&g);
497 return(err);
498}
499
Ben Lindstrombba81212001-06-25 05:01:22 +0000500static int
Damien Miller3db5f532002-02-13 14:10:32 +1100501process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000502{
503 char *tmp_dst = NULL;
504 char *abs_dst = NULL;
505 char *tmp;
506 glob_t g;
507 int err = 0;
508 int i;
509
510 if (dst) {
511 tmp_dst = xstrdup(dst);
512 tmp_dst = make_absolute(tmp_dst, pwd);
513 }
514
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000515 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000516 debug3("Looking up %s", src);
517 if (glob(src, 0, NULL, &g)) {
518 error("File \"%s\" not found.", src);
519 err = -1;
520 goto out;
521 }
522
Damien Miller4962ed62003-05-15 13:48:59 +1000523 /* If multiple matches, dst may be directory or unspecified */
524 if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000525 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000526 tmp_dst);
527 err = -1;
528 goto out;
529 }
530
Damien Miller9f0f5c62001-12-21 14:45:46 +1100531 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller5fa01fd2003-01-14 22:24:47 +1100532 if (!is_reg(g.gl_pathv[i])) {
533 error("skipping non-regular file %s",
534 g.gl_pathv[i]);
535 continue;
536 }
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000537 if (infer_path(g.gl_pathv[i], &tmp)) {
538 err = -1;
539 goto out;
540 }
Damien Miller4962ed62003-05-15 13:48:59 +1000541
542 if (g.gl_matchc == 1 && tmp_dst) {
543 /* If directory specified, append filename */
544 if (remote_is_dir(conn, tmp_dst)) {
545 if (infer_path(g.gl_pathv[0], &tmp)) {
546 err = 1;
547 goto out;
548 }
549 abs_dst = path_append(tmp_dst, tmp);
550 xfree(tmp);
551 } else
552 abs_dst = xstrdup(tmp_dst);
553
554 } else if (tmp_dst) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000555 abs_dst = path_append(tmp_dst, tmp);
556 xfree(tmp);
557 } else
558 abs_dst = make_absolute(tmp, pwd);
559
560 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100561 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000562 err = -1;
563 }
564
565out:
566 if (abs_dst)
567 xfree(abs_dst);
568 if (tmp_dst)
569 xfree(tmp_dst);
Damien Miller5d421c02003-05-14 13:42:40 +1000570 globfree(&g);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000571 return(err);
Damien Miller33804262001-02-04 23:20:18 +1100572}
573
Ben Lindstrombba81212001-06-25 05:01:22 +0000574static int
Damien Millere1a49812002-09-12 09:54:25 +1000575sdirent_comp(const void *aa, const void *bb)
576{
577 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
578 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
579
Ben Lindstrom93576d92002-12-23 02:06:19 +0000580 return (strcmp(a->filename, b->filename));
Damien Millere1a49812002-09-12 09:54:25 +1000581}
582
583/* sftp ls.1 replacement for directories */
584static int
585do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
586{
Damien Miller19c8f2b2003-05-15 13:49:21 +1000587 int n, c = 1, colspace = 0, columns = 1;
Damien Millere1a49812002-09-12 09:54:25 +1000588 SFTP_DIRENT **d;
589
590 if ((n = do_readdir(conn, path, &d)) != 0)
591 return (n);
592
Damien Miller19c8f2b2003-05-15 13:49:21 +1000593 if (!(lflag & SHORT_VIEW)) {
594 int m = 0, width = 80;
595 struct winsize ws;
596
597 /* Count entries for sort and find longest filename */
598 for (n = 0; d[n] != NULL; n++)
599 m = MAX(m, strlen(d[n]->filename));
600
601 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
602 width = ws.ws_col;
603
604 columns = width / (m + 2);
Darren Tucker78587262003-08-26 12:12:56 +1000605 columns = MAX(columns, 1);
Damien Miller19c8f2b2003-05-15 13:49:21 +1000606 colspace = width / columns;
607 }
Damien Millere1a49812002-09-12 09:54:25 +1000608
609 qsort(d, n, sizeof(*d), sdirent_comp);
610
611 for (n = 0; d[n] != NULL; n++) {
612 char *tmp, *fname;
Ben Lindstrom93576d92002-12-23 02:06:19 +0000613
Damien Millere1a49812002-09-12 09:54:25 +1000614 tmp = path_append(path, d[n]->filename);
615 fname = path_strip(tmp, strip_path);
616 xfree(tmp);
617
Damien Miller19c8f2b2003-05-15 13:49:21 +1000618 if (lflag & LONG_VIEW) {
Damien Millere1a49812002-09-12 09:54:25 +1000619 char *lname;
620 struct stat sb;
621
622 memset(&sb, 0, sizeof(sb));
623 attrib_to_stat(&d[n]->a, &sb);
624 lname = ls_file(fname, &sb, 1);
625 printf("%s\n", lname);
626 xfree(lname);
627 } else {
Damien Miller19c8f2b2003-05-15 13:49:21 +1000628 printf("%-*s", colspace, fname);
629 if (c >= columns) {
630 printf("\n");
631 c = 1;
632 } else
633 c++;
Damien Millere1a49812002-09-12 09:54:25 +1000634 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000635
Damien Millere1a49812002-09-12 09:54:25 +1000636 xfree(fname);
637 }
638
Damien Miller19c8f2b2003-05-15 13:49:21 +1000639 if (!(lflag & LONG_VIEW) && (c != 1))
640 printf("\n");
641
Damien Millere1a49812002-09-12 09:54:25 +1000642 free_sftp_dirents(d);
643 return (0);
644}
645
646/* sftp ls.1 replacement which handles path globs */
647static int
Ben Lindstrom93576d92002-12-23 02:06:19 +0000648do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
Damien Millere1a49812002-09-12 09:54:25 +1000649 int lflag)
650{
651 glob_t g;
Damien Miller19c8f2b2003-05-15 13:49:21 +1000652 int i, c = 1, colspace = 0, columns = 1;
Damien Millere1a49812002-09-12 09:54:25 +1000653 Attrib *a;
Damien Millere1a49812002-09-12 09:54:25 +1000654
655 memset(&g, 0, sizeof(g));
656
Ben Lindstrom93576d92002-12-23 02:06:19 +0000657 if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
Damien Millere1a49812002-09-12 09:54:25 +1000658 NULL, &g)) {
659 error("Can't ls: \"%s\" not found", path);
660 return (-1);
661 }
662
663 /*
Ben Lindstrom93576d92002-12-23 02:06:19 +0000664 * If the glob returns a single match, which is the same as the
Damien Millere1a49812002-09-12 09:54:25 +1000665 * input glob, and it is a directory, then just list its contents
666 */
Ben Lindstrom93576d92002-12-23 02:06:19 +0000667 if (g.gl_pathc == 1 &&
Damien Millere1a49812002-09-12 09:54:25 +1000668 strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
669 if ((a = do_lstat(conn, path, 1)) == NULL) {
670 globfree(&g);
671 return (-1);
672 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000673 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Millere1a49812002-09-12 09:54:25 +1000674 S_ISDIR(a->perm)) {
675 globfree(&g);
676 return (do_ls_dir(conn, path, strip_path, lflag));
677 }
678 }
679
Damien Miller19c8f2b2003-05-15 13:49:21 +1000680 if (!(lflag & SHORT_VIEW)) {
681 int m = 0, width = 80;
682 struct winsize ws;
683
684 /* Count entries for sort and find longest filename */
685 for (i = 0; g.gl_pathv[i]; i++)
686 m = MAX(m, strlen(g.gl_pathv[i]));
687
688 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
689 width = ws.ws_col;
690
691 columns = width / (m + 2);
Darren Tucker78587262003-08-26 12:12:56 +1000692 columns = MAX(columns, 1);
Damien Miller19c8f2b2003-05-15 13:49:21 +1000693 colspace = width / columns;
694 }
695
Damien Millere1a49812002-09-12 09:54:25 +1000696 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller19c8f2b2003-05-15 13:49:21 +1000697 char *fname;
Damien Millere1a49812002-09-12 09:54:25 +1000698
699 fname = path_strip(g.gl_pathv[i], strip_path);
700
Damien Miller19c8f2b2003-05-15 13:49:21 +1000701 if (lflag & LONG_VIEW) {
702 char *lname;
703 struct stat sb;
704
Damien Millere1a49812002-09-12 09:54:25 +1000705 /*
706 * XXX: this is slow - 1 roundtrip per path
Ben Lindstrom93576d92002-12-23 02:06:19 +0000707 * A solution to this is to fork glob() and
708 * build a sftp specific version which keeps the
Damien Millere1a49812002-09-12 09:54:25 +1000709 * attribs (which currently get thrown away)
710 * that the server returns as well as the filenames.
711 */
712 memset(&sb, 0, sizeof(sb));
713 a = do_lstat(conn, g.gl_pathv[i], 1);
714 if (a != NULL)
715 attrib_to_stat(a, &sb);
716 lname = ls_file(fname, &sb, 1);
717 printf("%s\n", lname);
718 xfree(lname);
719 } else {
Damien Miller19c8f2b2003-05-15 13:49:21 +1000720 printf("%-*s", colspace, fname);
721 if (c >= columns) {
722 printf("\n");
723 c = 1;
724 } else
725 c++;
Damien Millere1a49812002-09-12 09:54:25 +1000726 }
727 xfree(fname);
728 }
729
Damien Miller19c8f2b2003-05-15 13:49:21 +1000730 if (!(lflag & LONG_VIEW) && (c != 1))
731 printf("\n");
732
Damien Millere1a49812002-09-12 09:54:25 +1000733 if (g.gl_pathc)
734 globfree(&g);
735
736 return (0);
737}
738
739static int
Damien Miller956f3fb2003-01-10 21:40:00 +1100740parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
Damien Millere1a49812002-09-12 09:54:25 +1000741 unsigned long *n_arg, char **path1, char **path2)
Damien Miller33804262001-02-04 23:20:18 +1100742{
743 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000744 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000745 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000746 long l;
Damien Miller33804262001-02-04 23:20:18 +1100747 int i, cmdnum;
748
749 /* Skip leading whitespace */
750 cp = cp + strspn(cp, WHITESPACE);
751
Damien Miller956f3fb2003-01-10 21:40:00 +1100752 /* Ignore blank lines and lines which begin with comment '#' char */
753 if (*cp == '\0' || *cp == '#')
754 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100755
Damien Miller956f3fb2003-01-10 21:40:00 +1100756 /* Check for leading '-' (disable error processing) */
757 *iflag = 0;
758 if (*cp == '-') {
759 *iflag = 1;
760 cp++;
761 }
762
Damien Miller33804262001-02-04 23:20:18 +1100763 /* Figure out which command we have */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100764 for (i = 0; cmds[i].c; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100765 int cmdlen = strlen(cmds[i].c);
766
767 /* Check for command followed by whitespace */
768 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
769 strchr(WHITESPACE, cp[cmdlen])) {
770 cp += cmdlen;
771 cp = cp + strspn(cp, WHITESPACE);
772 break;
773 }
774 }
775 cmdnum = cmds[i].n;
776 cmd = cmds[i].c;
777
778 /* Special case */
779 if (*cp == '!') {
780 cp++;
781 cmdnum = I_SHELL;
782 } else if (cmdnum == -1) {
783 error("Invalid command.");
Damien Miller956f3fb2003-01-10 21:40:00 +1100784 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100785 }
786
787 /* Get arguments and parse flags */
Damien Millere1a49812002-09-12 09:54:25 +1000788 *lflag = *pflag = *n_arg = 0;
Damien Miller33804262001-02-04 23:20:18 +1100789 *path1 = *path2 = NULL;
790 switch (cmdnum) {
791 case I_GET:
792 case I_PUT:
793 if (parse_getput_flags(&cp, pflag))
794 return(-1);
795 /* Get first pathname (mandatory) */
796 if (get_pathname(&cp, path1))
797 return(-1);
798 if (*path1 == NULL) {
799 error("You must specify at least one path after a "
800 "%s command.", cmd);
801 return(-1);
802 }
803 /* Try to get second pathname (optional) */
804 if (get_pathname(&cp, path2))
805 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100806 break;
807 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100808 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100809 if (get_pathname(&cp, path1))
810 return(-1);
811 if (get_pathname(&cp, path2))
812 return(-1);
813 if (!*path1 || !*path2) {
814 error("You must specify two paths after a %s "
815 "command.", cmd);
816 return(-1);
817 }
818 break;
819 case I_RM:
820 case I_MKDIR:
821 case I_RMDIR:
822 case I_CHDIR:
823 case I_LCHDIR:
824 case I_LMKDIR:
825 /* Get pathname (mandatory) */
826 if (get_pathname(&cp, path1))
827 return(-1);
828 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000829 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100830 cmd);
831 return(-1);
832 }
833 break;
834 case I_LS:
Damien Millere1a49812002-09-12 09:54:25 +1000835 if (parse_ls_flags(&cp, lflag))
836 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100837 /* Path is optional */
838 if (get_pathname(&cp, path1))
839 return(-1);
840 break;
841 case I_LLS:
842 case I_SHELL:
843 /* Uses the rest of the line */
844 break;
845 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000846 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100847 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000848 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100849 case I_CHOWN:
850 case I_CHGRP:
851 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000852 l = strtol(cp, &cp2, base);
853 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
854 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100855 error("You must supply a numeric argument "
856 "to the %s command.", cmd);
857 return(-1);
858 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000859 cp = cp2;
860 *n_arg = l;
861 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
862 break;
863 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100864 error("You must supply a numeric argument "
865 "to the %s command.", cmd);
866 return(-1);
867 }
868 cp += strspn(cp, WHITESPACE);
869
870 /* Get pathname (mandatory) */
871 if (get_pathname(&cp, path1))
872 return(-1);
873 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000874 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100875 cmd);
876 return(-1);
877 }
878 break;
879 case I_QUIT:
880 case I_PWD:
881 case I_LPWD:
882 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000883 case I_VERSION:
Damien Miller62d57f62003-01-10 21:43:24 +1100884 case I_PROGRESS:
Damien Miller33804262001-02-04 23:20:18 +1100885 break;
886 default:
887 fatal("Command not implemented");
888 }
889
890 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100891 return(cmdnum);
892}
893
Ben Lindstrombba81212001-06-25 05:01:22 +0000894static int
Damien Miller956f3fb2003-01-10 21:40:00 +1100895parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
896 int err_abort)
Damien Miller33804262001-02-04 23:20:18 +1100897{
Damien Millerd7686fd2001-02-10 00:40:03 +1100898 char *path1, *path2, *tmp;
Damien Miller956f3fb2003-01-10 21:40:00 +1100899 int pflag, lflag, iflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100900 unsigned long n_arg;
901 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000902 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000903 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100904 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100905
906 path1 = path2 = NULL;
Damien Miller956f3fb2003-01-10 21:40:00 +1100907 cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
Damien Millere1a49812002-09-12 09:54:25 +1000908 &path1, &path2);
Damien Miller33804262001-02-04 23:20:18 +1100909
Damien Miller956f3fb2003-01-10 21:40:00 +1100910 if (iflag != 0)
911 err_abort = 0;
912
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000913 memset(&g, 0, sizeof(g));
914
Damien Miller33804262001-02-04 23:20:18 +1100915 /* Perform command */
916 switch (cmdnum) {
Damien Miller956f3fb2003-01-10 21:40:00 +1100917 case 0:
918 /* Blank line */
919 break;
Damien Miller33804262001-02-04 23:20:18 +1100920 case -1:
Damien Miller956f3fb2003-01-10 21:40:00 +1100921 /* Unrecognized command */
922 err = -1;
Damien Miller33804262001-02-04 23:20:18 +1100923 break;
924 case I_GET:
Damien Miller3db5f532002-02-13 14:10:32 +1100925 err = process_get(conn, path1, path2, *pwd, pflag);
Damien Miller33804262001-02-04 23:20:18 +1100926 break;
927 case I_PUT:
Damien Miller3db5f532002-02-13 14:10:32 +1100928 err = process_put(conn, path1, path2, *pwd, pflag);
Ben Lindstroma3700052001-04-05 23:26:32 +0000929 break;
930 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100931 path1 = make_absolute(path1, *pwd);
932 path2 = make_absolute(path2, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100933 err = do_rename(conn, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100934 break;
Damien Miller058316f2001-03-08 10:08:49 +1100935 case I_SYMLINK:
Damien Miller3db5f532002-02-13 14:10:32 +1100936 path2 = make_absolute(path2, *pwd);
937 err = do_symlink(conn, path1, path2);
Damien Miller058316f2001-03-08 10:08:49 +1100938 break;
Damien Miller33804262001-02-04 23:20:18 +1100939 case I_RM:
940 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100941 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100942 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100943 printf("Removing %s\n", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +1100944 err = do_rm(conn, g.gl_pathv[i]);
945 if (err != 0 && err_abort)
946 break;
Damien Miller4870afd2001-03-14 10:27:09 +1100947 }
Damien Miller33804262001-02-04 23:20:18 +1100948 break;
949 case I_MKDIR:
950 path1 = make_absolute(path1, *pwd);
951 attrib_clear(&a);
952 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
953 a.perm = 0777;
Damien Miller3db5f532002-02-13 14:10:32 +1100954 err = do_mkdir(conn, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100955 break;
956 case I_RMDIR:
957 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100958 err = do_rmdir(conn, path1);
Damien Miller33804262001-02-04 23:20:18 +1100959 break;
960 case I_CHDIR:
961 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100962 if ((tmp = do_realpath(conn, path1)) == NULL) {
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000963 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100964 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000965 }
Damien Miller3db5f532002-02-13 14:10:32 +1100966 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100967 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000968 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100969 break;
970 }
971 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
972 error("Can't change directory: Can't check target");
973 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000974 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100975 break;
976 }
977 if (!S_ISDIR(aa->perm)) {
978 error("Can't change directory: \"%s\" is not "
979 "a directory", tmp);
980 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000981 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100982 break;
983 }
Damien Miller33804262001-02-04 23:20:18 +1100984 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100985 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100986 break;
987 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100988 if (!path1) {
Damien Millere1a49812002-09-12 09:54:25 +1000989 do_globbed_ls(conn, *pwd, *pwd, lflag);
Damien Millerd7686fd2001-02-10 00:40:03 +1100990 break;
991 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000992
Damien Millere1a49812002-09-12 09:54:25 +1000993 /* Strip pwd off beginning of non-absolute paths */
994 tmp = NULL;
995 if (*path1 != '/')
996 tmp = *pwd;
997
Damien Miller33804262001-02-04 23:20:18 +1100998 path1 = make_absolute(path1, *pwd);
Damien Miller956f3fb2003-01-10 21:40:00 +1100999 err = do_globbed_ls(conn, path1, tmp, lflag);
Damien Miller33804262001-02-04 23:20:18 +11001000 break;
1001 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001002 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +11001003 error("Couldn't change local directory to "
1004 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001005 err = 1;
1006 }
Damien Miller33804262001-02-04 23:20:18 +11001007 break;
1008 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001009 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +11001010 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +11001011 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001012 err = 1;
1013 }
Damien Miller33804262001-02-04 23:20:18 +11001014 break;
1015 case I_LLS:
1016 local_do_ls(cmd);
1017 break;
1018 case I_SHELL:
1019 local_do_shell(cmd);
1020 break;
1021 case I_LUMASK:
1022 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +00001023 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +11001024 break;
1025 case I_CHMOD:
1026 path1 = make_absolute(path1, *pwd);
1027 attrib_clear(&a);
1028 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1029 a.perm = n_arg;
Damien Miller3db5f532002-02-13 14:10:32 +11001030 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001031 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +11001032 printf("Changing mode on %s\n", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +11001033 err = do_setstat(conn, g.gl_pathv[i], &a);
1034 if (err != 0 && err_abort)
1035 break;
Damien Miller4870afd2001-03-14 10:27:09 +11001036 }
Kevin Steves62c45db2001-02-05 13:42:43 +00001037 break;
Damien Miller33804262001-02-04 23:20:18 +11001038 case I_CHOWN:
Damien Miller33804262001-02-04 23:20:18 +11001039 case I_CHGRP:
1040 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +11001041 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001042 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller956f3fb2003-01-10 21:40:00 +11001043 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1044 if (err != 0 && err_abort)
1045 break;
1046 else
1047 continue;
1048 }
Damien Miller4870afd2001-03-14 10:27:09 +11001049 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1050 error("Can't get current ownership of "
1051 "remote file \"%s\"", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +11001052 if (err != 0 && err_abort)
1053 break;
1054 else
1055 continue;
Damien Miller4870afd2001-03-14 10:27:09 +11001056 }
Damien Miller4870afd2001-03-14 10:27:09 +11001057 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
Damien Miller956f3fb2003-01-10 21:40:00 +11001058 if (cmdnum == I_CHOWN) {
1059 printf("Changing owner on %s\n", g.gl_pathv[i]);
1060 aa->uid = n_arg;
1061 } else {
1062 printf("Changing group on %s\n", g.gl_pathv[i]);
1063 aa->gid = n_arg;
1064 }
1065 err = do_setstat(conn, g.gl_pathv[i], aa);
1066 if (err != 0 && err_abort)
1067 break;
Damien Miller33804262001-02-04 23:20:18 +11001068 }
Damien Miller33804262001-02-04 23:20:18 +11001069 break;
1070 case I_PWD:
1071 printf("Remote working directory: %s\n", *pwd);
1072 break;
1073 case I_LPWD:
Damien Miller956f3fb2003-01-10 21:40:00 +11001074 if (!getcwd(path_buf, sizeof(path_buf))) {
1075 error("Couldn't get local cwd: %s", strerror(errno));
1076 err = -1;
1077 break;
1078 }
1079 printf("Local working directory: %s\n", path_buf);
Damien Miller33804262001-02-04 23:20:18 +11001080 break;
1081 case I_QUIT:
Damien Miller956f3fb2003-01-10 21:40:00 +11001082 /* Processed below */
1083 break;
Damien Miller33804262001-02-04 23:20:18 +11001084 case I_HELP:
1085 help();
1086 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +00001087 case I_VERSION:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001088 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
Ben Lindstromf78682d2001-03-14 21:26:27 +00001089 break;
Damien Miller62d57f62003-01-10 21:43:24 +11001090 case I_PROGRESS:
1091 showprogress = !showprogress;
1092 if (showprogress)
1093 printf("Progress meter enabled\n");
1094 else
1095 printf("Progress meter disabled\n");
1096 break;
Damien Miller33804262001-02-04 23:20:18 +11001097 default:
1098 fatal("%d is not implemented", cmdnum);
1099 }
1100
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001101 if (g.gl_pathc)
1102 globfree(&g);
Damien Miller33804262001-02-04 23:20:18 +11001103 if (path1)
1104 xfree(path1);
1105 if (path2)
1106 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001107
Damien Miller956f3fb2003-01-10 21:40:00 +11001108 /* If an unignored error occurs in batch mode we should abort. */
1109 if (err_abort && err != 0)
1110 return (-1);
1111 else if (cmdnum == I_QUIT)
1112 return (1);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001113
Damien Miller956f3fb2003-01-10 21:40:00 +11001114 return (0);
Damien Miller33804262001-02-04 23:20:18 +11001115}
1116
Damien Miller956f3fb2003-01-10 21:40:00 +11001117int
Ben Lindstrom63667f62001-04-13 00:00:14 +00001118interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
Damien Miller33804262001-02-04 23:20:18 +11001119{
1120 char *pwd;
Ben Lindstrom63667f62001-04-13 00:00:14 +00001121 char *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +11001122 char cmd[2048];
Damien Miller3db5f532002-02-13 14:10:32 +11001123 struct sftp_conn *conn;
Damien Miller956f3fb2003-01-10 21:40:00 +11001124 int err;
Damien Miller33804262001-02-04 23:20:18 +11001125
Damien Miller3db5f532002-02-13 14:10:32 +11001126 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1127 if (conn == NULL)
Damien Miller058316f2001-03-08 10:08:49 +11001128 fatal("Couldn't initialise connection to server");
1129
Damien Miller3db5f532002-02-13 14:10:32 +11001130 pwd = do_realpath(conn, ".");
Damien Miller33804262001-02-04 23:20:18 +11001131 if (pwd == NULL)
1132 fatal("Need cwd");
1133
Ben Lindstrom63667f62001-04-13 00:00:14 +00001134 if (file1 != NULL) {
1135 dir = xstrdup(file1);
1136 dir = make_absolute(dir, pwd);
1137
Damien Miller3db5f532002-02-13 14:10:32 +11001138 if (remote_is_dir(conn, dir) && file2 == NULL) {
Ben Lindstrom63667f62001-04-13 00:00:14 +00001139 printf("Changing to: %s\n", dir);
1140 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
Damien Miller956f3fb2003-01-10 21:40:00 +11001141 if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0)
1142 return (-1);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001143 } else {
1144 if (file2 == NULL)
1145 snprintf(cmd, sizeof cmd, "get %s", dir);
1146 else
1147 snprintf(cmd, sizeof cmd, "get %s %s", dir,
1148 file2);
1149
Damien Miller956f3fb2003-01-10 21:40:00 +11001150 err = parse_dispatch_command(conn, cmd, &pwd, 1);
Ben Lindstromcb1f60e2002-03-22 02:47:28 +00001151 xfree(dir);
Damien Miller00111382003-03-10 11:21:17 +11001152 xfree(pwd);
Damien Miller956f3fb2003-01-10 21:40:00 +11001153 return (err);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001154 }
Ben Lindstromcb1f60e2002-03-22 02:47:28 +00001155 xfree(dir);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001156 }
Damien Miller956f3fb2003-01-10 21:40:00 +11001157
Ben Lindstrom6aebb342001-05-09 00:38:19 +00001158#if HAVE_SETVBUF
Damien Millerd7686fd2001-02-10 00:40:03 +11001159 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001160 setvbuf(infile, NULL, _IOLBF, 0);
Ben Lindstrom6aebb342001-05-09 00:38:19 +00001161#else
1162 setlinebuf(stdout);
1163 setlinebuf(infile);
1164#endif
Damien Miller33804262001-02-04 23:20:18 +11001165
Damien Miller956f3fb2003-01-10 21:40:00 +11001166 err = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001167 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001168 char *cp;
1169
1170 printf("sftp> ");
1171
1172 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001173 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +11001174 printf("\n");
1175 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001176 } else if (infile != stdin) /* Bluff typing */
1177 printf("%s", cmd);
1178
Damien Miller33804262001-02-04 23:20:18 +11001179 cp = strrchr(cmd, '\n');
1180 if (cp)
1181 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001182
Damien Miller956f3fb2003-01-10 21:40:00 +11001183 err = parse_dispatch_command(conn, cmd, &pwd, infile != stdin);
1184 if (err != 0)
Damien Miller33804262001-02-04 23:20:18 +11001185 break;
1186 }
1187 xfree(pwd);
Damien Miller956f3fb2003-01-10 21:40:00 +11001188
1189 /* err == 1 signifies normal "quit" exit */
1190 return (err >= 0 ? 0 : -1);
Damien Miller33804262001-02-04 23:20:18 +11001191}
Damien Miller956f3fb2003-01-10 21:40:00 +11001192