blob: f2d3f94689ef349efe050a4903c44735c369c53b [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 Miller19c8f2b2003-05-15 13:49:21 +100028RCSID("$OpenBSD: sftp-int.c,v 1.60 2003/05/15 03:43:59 mouring 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 Miller33804262001-02-04 23:20:18 +110053/* Seperators for interactive commands */
54#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;
Damien Miller33804262001-02-04 23:20:18 +1100335 int i;
336
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
344 /* Check for quoted filenames */
345 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100346 quot = *cp++;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000347
Damien Millerd7686fd2001-02-10 00:40:03 +1100348 end = strchr(cp, quot);
349 if (end == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100350 error("Unterminated quote");
Damien Millerd7686fd2001-02-10 00:40:03 +1100351 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100352 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100353 if (cp == end) {
Damien Miller33804262001-02-04 23:20:18 +1100354 error("Empty quotes");
Damien Millerd7686fd2001-02-10 00:40:03 +1100355 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100356 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100357 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
358 } else {
359 /* Read to end of filename */
360 end = strpbrk(cp, WHITESPACE);
361 if (end == NULL)
362 end = strchr(cp, '\0');
363 *cpp = end + strspn(end, WHITESPACE);
Damien Miller33804262001-02-04 23:20:18 +1100364 }
365
Damien Millerd7686fd2001-02-10 00:40:03 +1100366 i = end - cp;
Damien Miller33804262001-02-04 23:20:18 +1100367
368 *path = xmalloc(i + 1);
369 memcpy(*path, cp, i);
370 (*path)[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100371 return(0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100372
373 fail:
374 *path = NULL;
375 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100376}
377
Ben Lindstrombba81212001-06-25 05:01:22 +0000378static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000379is_dir(char *path)
Damien Miller33804262001-02-04 23:20:18 +1100380{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000381 struct stat sb;
Damien Miller33804262001-02-04 23:20:18 +1100382
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000383 /* XXX: report errors? */
384 if (stat(path, &sb) == -1)
Damien Miller33804262001-02-04 23:20:18 +1100385 return(0);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000386
387 return(sb.st_mode & S_IFDIR);
388}
389
Ben Lindstrombba81212001-06-25 05:01:22 +0000390static int
Damien Miller5fa01fd2003-01-14 22:24:47 +1100391is_reg(char *path)
392{
393 struct stat sb;
394
395 if (stat(path, &sb) == -1)
396 fatal("stat %s: %s", path, strerror(errno));
397
398 return(S_ISREG(sb.st_mode));
399}
400
401static int
Damien Miller3db5f532002-02-13 14:10:32 +1100402remote_is_dir(struct sftp_conn *conn, char *path)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000403{
404 Attrib *a;
405
406 /* XXX: report errors? */
Damien Miller3db5f532002-02-13 14:10:32 +1100407 if ((a = do_stat(conn, path, 1)) == NULL)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000408 return(0);
409 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
410 return(0);
411 return(a->perm & S_IFDIR);
412}
413
Ben Lindstrombba81212001-06-25 05:01:22 +0000414static int
Damien Miller3db5f532002-02-13 14:10:32 +1100415process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000416{
417 char *abs_src = NULL;
418 char *abs_dst = NULL;
419 char *tmp;
420 glob_t g;
421 int err = 0;
422 int i;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000423
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000424 abs_src = xstrdup(src);
425 abs_src = make_absolute(abs_src, pwd);
426
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000427 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000428 debug3("Looking up %s", abs_src);
Damien Miller3db5f532002-02-13 14:10:32 +1100429 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000430 error("File \"%s\" not found.", abs_src);
431 err = -1;
432 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100433 }
434
Damien Miller4962ed62003-05-15 13:48:59 +1000435 /* If multiple matches, dst must be a directory or unspecified */
436 if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000437 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000438 dst);
439 err = -1;
440 goto out;
441 }
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000442
Damien Miller9f0f5c62001-12-21 14:45:46 +1100443 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000444 if (infer_path(g.gl_pathv[i], &tmp)) {
445 err = -1;
446 goto out;
447 }
Damien Miller4962ed62003-05-15 13:48:59 +1000448
449 if (g.gl_matchc == 1 && dst) {
450 /* If directory specified, append filename */
451 if (is_dir(dst)) {
452 if (infer_path(g.gl_pathv[0], &tmp)) {
453 err = 1;
454 goto out;
455 }
456 abs_dst = path_append(dst, tmp);
457 xfree(tmp);
458 } else
459 abs_dst = xstrdup(dst);
460 } else if (dst) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000461 abs_dst = path_append(dst, tmp);
462 xfree(tmp);
463 } else
464 abs_dst = tmp;
465
466 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100467 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000468 err = -1;
469 xfree(abs_dst);
470 abs_dst = NULL;
471 }
472
473out:
474 xfree(abs_src);
475 if (abs_dst)
476 xfree(abs_dst);
477 globfree(&g);
478 return(err);
479}
480
Ben Lindstrombba81212001-06-25 05:01:22 +0000481static int
Damien Miller3db5f532002-02-13 14:10:32 +1100482process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000483{
484 char *tmp_dst = NULL;
485 char *abs_dst = NULL;
486 char *tmp;
487 glob_t g;
488 int err = 0;
489 int i;
490
491 if (dst) {
492 tmp_dst = xstrdup(dst);
493 tmp_dst = make_absolute(tmp_dst, pwd);
494 }
495
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000496 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000497 debug3("Looking up %s", src);
498 if (glob(src, 0, NULL, &g)) {
499 error("File \"%s\" not found.", src);
500 err = -1;
501 goto out;
502 }
503
Damien Miller4962ed62003-05-15 13:48:59 +1000504 /* If multiple matches, dst may be directory or unspecified */
505 if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000506 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000507 tmp_dst);
508 err = -1;
509 goto out;
510 }
511
Damien Miller9f0f5c62001-12-21 14:45:46 +1100512 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller5fa01fd2003-01-14 22:24:47 +1100513 if (!is_reg(g.gl_pathv[i])) {
514 error("skipping non-regular file %s",
515 g.gl_pathv[i]);
516 continue;
517 }
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000518 if (infer_path(g.gl_pathv[i], &tmp)) {
519 err = -1;
520 goto out;
521 }
Damien Miller4962ed62003-05-15 13:48:59 +1000522
523 if (g.gl_matchc == 1 && tmp_dst) {
524 /* If directory specified, append filename */
525 if (remote_is_dir(conn, tmp_dst)) {
526 if (infer_path(g.gl_pathv[0], &tmp)) {
527 err = 1;
528 goto out;
529 }
530 abs_dst = path_append(tmp_dst, tmp);
531 xfree(tmp);
532 } else
533 abs_dst = xstrdup(tmp_dst);
534
535 } else if (tmp_dst) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000536 abs_dst = path_append(tmp_dst, tmp);
537 xfree(tmp);
538 } else
539 abs_dst = make_absolute(tmp, pwd);
540
541 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100542 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000543 err = -1;
544 }
545
546out:
547 if (abs_dst)
548 xfree(abs_dst);
549 if (tmp_dst)
550 xfree(tmp_dst);
Damien Miller5d421c02003-05-14 13:42:40 +1000551 globfree(&g);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000552 return(err);
Damien Miller33804262001-02-04 23:20:18 +1100553}
554
Ben Lindstrombba81212001-06-25 05:01:22 +0000555static int
Damien Millere1a49812002-09-12 09:54:25 +1000556sdirent_comp(const void *aa, const void *bb)
557{
558 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
559 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
560
Ben Lindstrom93576d92002-12-23 02:06:19 +0000561 return (strcmp(a->filename, b->filename));
Damien Millere1a49812002-09-12 09:54:25 +1000562}
563
564/* sftp ls.1 replacement for directories */
565static int
566do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
567{
Damien Miller19c8f2b2003-05-15 13:49:21 +1000568 int n, c = 1, colspace = 0, columns = 1;
Damien Millere1a49812002-09-12 09:54:25 +1000569 SFTP_DIRENT **d;
570
571 if ((n = do_readdir(conn, path, &d)) != 0)
572 return (n);
573
Damien Miller19c8f2b2003-05-15 13:49:21 +1000574 if (!(lflag & SHORT_VIEW)) {
575 int m = 0, width = 80;
576 struct winsize ws;
577
578 /* Count entries for sort and find longest filename */
579 for (n = 0; d[n] != NULL; n++)
580 m = MAX(m, strlen(d[n]->filename));
581
582 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
583 width = ws.ws_col;
584
585 columns = width / (m + 2);
586 colspace = width / columns;
587 }
Damien Millere1a49812002-09-12 09:54:25 +1000588
589 qsort(d, n, sizeof(*d), sdirent_comp);
590
591 for (n = 0; d[n] != NULL; n++) {
592 char *tmp, *fname;
Ben Lindstrom93576d92002-12-23 02:06:19 +0000593
Damien Millere1a49812002-09-12 09:54:25 +1000594 tmp = path_append(path, d[n]->filename);
595 fname = path_strip(tmp, strip_path);
596 xfree(tmp);
597
Damien Miller19c8f2b2003-05-15 13:49:21 +1000598 if (lflag & LONG_VIEW) {
Damien Millere1a49812002-09-12 09:54:25 +1000599 char *lname;
600 struct stat sb;
601
602 memset(&sb, 0, sizeof(sb));
603 attrib_to_stat(&d[n]->a, &sb);
604 lname = ls_file(fname, &sb, 1);
605 printf("%s\n", lname);
606 xfree(lname);
607 } else {
Damien Miller19c8f2b2003-05-15 13:49:21 +1000608 printf("%-*s", colspace, fname);
609 if (c >= columns) {
610 printf("\n");
611 c = 1;
612 } else
613 c++;
Damien Millere1a49812002-09-12 09:54:25 +1000614 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000615
Damien Millere1a49812002-09-12 09:54:25 +1000616 xfree(fname);
617 }
618
Damien Miller19c8f2b2003-05-15 13:49:21 +1000619 if (!(lflag & LONG_VIEW) && (c != 1))
620 printf("\n");
621
Damien Millere1a49812002-09-12 09:54:25 +1000622 free_sftp_dirents(d);
623 return (0);
624}
625
626/* sftp ls.1 replacement which handles path globs */
627static int
Ben Lindstrom93576d92002-12-23 02:06:19 +0000628do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
Damien Millere1a49812002-09-12 09:54:25 +1000629 int lflag)
630{
631 glob_t g;
Damien Miller19c8f2b2003-05-15 13:49:21 +1000632 int i, c = 1, colspace = 0, columns = 1;
Damien Millere1a49812002-09-12 09:54:25 +1000633 Attrib *a;
Damien Millere1a49812002-09-12 09:54:25 +1000634
635 memset(&g, 0, sizeof(g));
636
Ben Lindstrom93576d92002-12-23 02:06:19 +0000637 if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
Damien Millere1a49812002-09-12 09:54:25 +1000638 NULL, &g)) {
639 error("Can't ls: \"%s\" not found", path);
640 return (-1);
641 }
642
643 /*
Ben Lindstrom93576d92002-12-23 02:06:19 +0000644 * If the glob returns a single match, which is the same as the
Damien Millere1a49812002-09-12 09:54:25 +1000645 * input glob, and it is a directory, then just list its contents
646 */
Ben Lindstrom93576d92002-12-23 02:06:19 +0000647 if (g.gl_pathc == 1 &&
Damien Millere1a49812002-09-12 09:54:25 +1000648 strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
649 if ((a = do_lstat(conn, path, 1)) == NULL) {
650 globfree(&g);
651 return (-1);
652 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000653 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Millere1a49812002-09-12 09:54:25 +1000654 S_ISDIR(a->perm)) {
655 globfree(&g);
656 return (do_ls_dir(conn, path, strip_path, lflag));
657 }
658 }
659
Damien Miller19c8f2b2003-05-15 13:49:21 +1000660 if (!(lflag & SHORT_VIEW)) {
661 int m = 0, width = 80;
662 struct winsize ws;
663
664 /* Count entries for sort and find longest filename */
665 for (i = 0; g.gl_pathv[i]; i++)
666 m = MAX(m, strlen(g.gl_pathv[i]));
667
668 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
669 width = ws.ws_col;
670
671 columns = width / (m + 2);
672 colspace = width / columns;
673 }
674
Damien Millere1a49812002-09-12 09:54:25 +1000675 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller19c8f2b2003-05-15 13:49:21 +1000676 char *fname;
Damien Millere1a49812002-09-12 09:54:25 +1000677
678 fname = path_strip(g.gl_pathv[i], strip_path);
679
Damien Miller19c8f2b2003-05-15 13:49:21 +1000680 if (lflag & LONG_VIEW) {
681 char *lname;
682 struct stat sb;
683
Damien Millere1a49812002-09-12 09:54:25 +1000684 /*
685 * XXX: this is slow - 1 roundtrip per path
Ben Lindstrom93576d92002-12-23 02:06:19 +0000686 * A solution to this is to fork glob() and
687 * build a sftp specific version which keeps the
Damien Millere1a49812002-09-12 09:54:25 +1000688 * attribs (which currently get thrown away)
689 * that the server returns as well as the filenames.
690 */
691 memset(&sb, 0, sizeof(sb));
692 a = do_lstat(conn, g.gl_pathv[i], 1);
693 if (a != NULL)
694 attrib_to_stat(a, &sb);
695 lname = ls_file(fname, &sb, 1);
696 printf("%s\n", lname);
697 xfree(lname);
698 } else {
Damien Miller19c8f2b2003-05-15 13:49:21 +1000699 printf("%-*s", colspace, fname);
700 if (c >= columns) {
701 printf("\n");
702 c = 1;
703 } else
704 c++;
Damien Millere1a49812002-09-12 09:54:25 +1000705 }
706 xfree(fname);
707 }
708
Damien Miller19c8f2b2003-05-15 13:49:21 +1000709 if (!(lflag & LONG_VIEW) && (c != 1))
710 printf("\n");
711
Damien Millere1a49812002-09-12 09:54:25 +1000712 if (g.gl_pathc)
713 globfree(&g);
714
715 return (0);
716}
717
718static int
Damien Miller956f3fb2003-01-10 21:40:00 +1100719parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
Damien Millere1a49812002-09-12 09:54:25 +1000720 unsigned long *n_arg, char **path1, char **path2)
Damien Miller33804262001-02-04 23:20:18 +1100721{
722 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000723 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000724 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000725 long l;
Damien Miller33804262001-02-04 23:20:18 +1100726 int i, cmdnum;
727
728 /* Skip leading whitespace */
729 cp = cp + strspn(cp, WHITESPACE);
730
Damien Miller956f3fb2003-01-10 21:40:00 +1100731 /* Ignore blank lines and lines which begin with comment '#' char */
732 if (*cp == '\0' || *cp == '#')
733 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100734
Damien Miller956f3fb2003-01-10 21:40:00 +1100735 /* Check for leading '-' (disable error processing) */
736 *iflag = 0;
737 if (*cp == '-') {
738 *iflag = 1;
739 cp++;
740 }
741
Damien Miller33804262001-02-04 23:20:18 +1100742 /* Figure out which command we have */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100743 for (i = 0; cmds[i].c; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100744 int cmdlen = strlen(cmds[i].c);
745
746 /* Check for command followed by whitespace */
747 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
748 strchr(WHITESPACE, cp[cmdlen])) {
749 cp += cmdlen;
750 cp = cp + strspn(cp, WHITESPACE);
751 break;
752 }
753 }
754 cmdnum = cmds[i].n;
755 cmd = cmds[i].c;
756
757 /* Special case */
758 if (*cp == '!') {
759 cp++;
760 cmdnum = I_SHELL;
761 } else if (cmdnum == -1) {
762 error("Invalid command.");
Damien Miller956f3fb2003-01-10 21:40:00 +1100763 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100764 }
765
766 /* Get arguments and parse flags */
Damien Millere1a49812002-09-12 09:54:25 +1000767 *lflag = *pflag = *n_arg = 0;
Damien Miller33804262001-02-04 23:20:18 +1100768 *path1 = *path2 = NULL;
769 switch (cmdnum) {
770 case I_GET:
771 case I_PUT:
772 if (parse_getput_flags(&cp, pflag))
773 return(-1);
774 /* Get first pathname (mandatory) */
775 if (get_pathname(&cp, path1))
776 return(-1);
777 if (*path1 == NULL) {
778 error("You must specify at least one path after a "
779 "%s command.", cmd);
780 return(-1);
781 }
782 /* Try to get second pathname (optional) */
783 if (get_pathname(&cp, path2))
784 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100785 break;
786 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100787 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100788 if (get_pathname(&cp, path1))
789 return(-1);
790 if (get_pathname(&cp, path2))
791 return(-1);
792 if (!*path1 || !*path2) {
793 error("You must specify two paths after a %s "
794 "command.", cmd);
795 return(-1);
796 }
797 break;
798 case I_RM:
799 case I_MKDIR:
800 case I_RMDIR:
801 case I_CHDIR:
802 case I_LCHDIR:
803 case I_LMKDIR:
804 /* Get pathname (mandatory) */
805 if (get_pathname(&cp, path1))
806 return(-1);
807 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000808 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100809 cmd);
810 return(-1);
811 }
812 break;
813 case I_LS:
Damien Millere1a49812002-09-12 09:54:25 +1000814 if (parse_ls_flags(&cp, lflag))
815 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100816 /* Path is optional */
817 if (get_pathname(&cp, path1))
818 return(-1);
819 break;
820 case I_LLS:
821 case I_SHELL:
822 /* Uses the rest of the line */
823 break;
824 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000825 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100826 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000827 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100828 case I_CHOWN:
829 case I_CHGRP:
830 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000831 l = strtol(cp, &cp2, base);
832 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
833 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100834 error("You must supply a numeric argument "
835 "to the %s command.", cmd);
836 return(-1);
837 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000838 cp = cp2;
839 *n_arg = l;
840 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
841 break;
842 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100843 error("You must supply a numeric argument "
844 "to the %s command.", cmd);
845 return(-1);
846 }
847 cp += strspn(cp, WHITESPACE);
848
849 /* Get pathname (mandatory) */
850 if (get_pathname(&cp, path1))
851 return(-1);
852 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000853 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100854 cmd);
855 return(-1);
856 }
857 break;
858 case I_QUIT:
859 case I_PWD:
860 case I_LPWD:
861 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000862 case I_VERSION:
Damien Miller62d57f62003-01-10 21:43:24 +1100863 case I_PROGRESS:
Damien Miller33804262001-02-04 23:20:18 +1100864 break;
865 default:
866 fatal("Command not implemented");
867 }
868
869 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100870 return(cmdnum);
871}
872
Ben Lindstrombba81212001-06-25 05:01:22 +0000873static int
Damien Miller956f3fb2003-01-10 21:40:00 +1100874parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
875 int err_abort)
Damien Miller33804262001-02-04 23:20:18 +1100876{
Damien Millerd7686fd2001-02-10 00:40:03 +1100877 char *path1, *path2, *tmp;
Damien Miller956f3fb2003-01-10 21:40:00 +1100878 int pflag, lflag, iflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100879 unsigned long n_arg;
880 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000881 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000882 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100883 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100884
885 path1 = path2 = NULL;
Damien Miller956f3fb2003-01-10 21:40:00 +1100886 cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
Damien Millere1a49812002-09-12 09:54:25 +1000887 &path1, &path2);
Damien Miller33804262001-02-04 23:20:18 +1100888
Damien Miller956f3fb2003-01-10 21:40:00 +1100889 if (iflag != 0)
890 err_abort = 0;
891
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000892 memset(&g, 0, sizeof(g));
893
Damien Miller33804262001-02-04 23:20:18 +1100894 /* Perform command */
895 switch (cmdnum) {
Damien Miller956f3fb2003-01-10 21:40:00 +1100896 case 0:
897 /* Blank line */
898 break;
Damien Miller33804262001-02-04 23:20:18 +1100899 case -1:
Damien Miller956f3fb2003-01-10 21:40:00 +1100900 /* Unrecognized command */
901 err = -1;
Damien Miller33804262001-02-04 23:20:18 +1100902 break;
903 case I_GET:
Damien Miller3db5f532002-02-13 14:10:32 +1100904 err = process_get(conn, path1, path2, *pwd, pflag);
Damien Miller33804262001-02-04 23:20:18 +1100905 break;
906 case I_PUT:
Damien Miller3db5f532002-02-13 14:10:32 +1100907 err = process_put(conn, path1, path2, *pwd, pflag);
Ben Lindstroma3700052001-04-05 23:26:32 +0000908 break;
909 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100910 path1 = make_absolute(path1, *pwd);
911 path2 = make_absolute(path2, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100912 err = do_rename(conn, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100913 break;
Damien Miller058316f2001-03-08 10:08:49 +1100914 case I_SYMLINK:
Damien Miller3db5f532002-02-13 14:10:32 +1100915 path2 = make_absolute(path2, *pwd);
916 err = do_symlink(conn, path1, path2);
Damien Miller058316f2001-03-08 10:08:49 +1100917 break;
Damien Miller33804262001-02-04 23:20:18 +1100918 case I_RM:
919 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100920 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100921 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100922 printf("Removing %s\n", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +1100923 err = do_rm(conn, g.gl_pathv[i]);
924 if (err != 0 && err_abort)
925 break;
Damien Miller4870afd2001-03-14 10:27:09 +1100926 }
Damien Miller33804262001-02-04 23:20:18 +1100927 break;
928 case I_MKDIR:
929 path1 = make_absolute(path1, *pwd);
930 attrib_clear(&a);
931 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
932 a.perm = 0777;
Damien Miller3db5f532002-02-13 14:10:32 +1100933 err = do_mkdir(conn, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100934 break;
935 case I_RMDIR:
936 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100937 err = do_rmdir(conn, path1);
Damien Miller33804262001-02-04 23:20:18 +1100938 break;
939 case I_CHDIR:
940 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100941 if ((tmp = do_realpath(conn, path1)) == NULL) {
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000942 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100943 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000944 }
Damien Miller3db5f532002-02-13 14:10:32 +1100945 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100946 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000947 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100948 break;
949 }
950 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
951 error("Can't change directory: Can't check target");
952 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000953 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100954 break;
955 }
956 if (!S_ISDIR(aa->perm)) {
957 error("Can't change directory: \"%s\" is not "
958 "a directory", tmp);
959 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000960 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100961 break;
962 }
Damien Miller33804262001-02-04 23:20:18 +1100963 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100964 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100965 break;
966 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100967 if (!path1) {
Damien Millere1a49812002-09-12 09:54:25 +1000968 do_globbed_ls(conn, *pwd, *pwd, lflag);
Damien Millerd7686fd2001-02-10 00:40:03 +1100969 break;
970 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000971
Damien Millere1a49812002-09-12 09:54:25 +1000972 /* Strip pwd off beginning of non-absolute paths */
973 tmp = NULL;
974 if (*path1 != '/')
975 tmp = *pwd;
976
Damien Miller33804262001-02-04 23:20:18 +1100977 path1 = make_absolute(path1, *pwd);
Damien Miller956f3fb2003-01-10 21:40:00 +1100978 err = do_globbed_ls(conn, path1, tmp, lflag);
Damien Miller33804262001-02-04 23:20:18 +1100979 break;
980 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000981 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100982 error("Couldn't change local directory to "
983 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000984 err = 1;
985 }
Damien Miller33804262001-02-04 23:20:18 +1100986 break;
987 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000988 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100989 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100990 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000991 err = 1;
992 }
Damien Miller33804262001-02-04 23:20:18 +1100993 break;
994 case I_LLS:
995 local_do_ls(cmd);
996 break;
997 case I_SHELL:
998 local_do_shell(cmd);
999 break;
1000 case I_LUMASK:
1001 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +00001002 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +11001003 break;
1004 case I_CHMOD:
1005 path1 = make_absolute(path1, *pwd);
1006 attrib_clear(&a);
1007 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1008 a.perm = n_arg;
Damien Miller3db5f532002-02-13 14:10:32 +11001009 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001010 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +11001011 printf("Changing mode on %s\n", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +11001012 err = do_setstat(conn, g.gl_pathv[i], &a);
1013 if (err != 0 && err_abort)
1014 break;
Damien Miller4870afd2001-03-14 10:27:09 +11001015 }
Kevin Steves62c45db2001-02-05 13:42:43 +00001016 break;
Damien Miller33804262001-02-04 23:20:18 +11001017 case I_CHOWN:
Damien Miller33804262001-02-04 23:20:18 +11001018 case I_CHGRP:
1019 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +11001020 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001021 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller956f3fb2003-01-10 21:40:00 +11001022 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1023 if (err != 0 && err_abort)
1024 break;
1025 else
1026 continue;
1027 }
Damien Miller4870afd2001-03-14 10:27:09 +11001028 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1029 error("Can't get current ownership of "
1030 "remote file \"%s\"", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +11001031 if (err != 0 && err_abort)
1032 break;
1033 else
1034 continue;
Damien Miller4870afd2001-03-14 10:27:09 +11001035 }
Damien Miller4870afd2001-03-14 10:27:09 +11001036 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
Damien Miller956f3fb2003-01-10 21:40:00 +11001037 if (cmdnum == I_CHOWN) {
1038 printf("Changing owner on %s\n", g.gl_pathv[i]);
1039 aa->uid = n_arg;
1040 } else {
1041 printf("Changing group on %s\n", g.gl_pathv[i]);
1042 aa->gid = n_arg;
1043 }
1044 err = do_setstat(conn, g.gl_pathv[i], aa);
1045 if (err != 0 && err_abort)
1046 break;
Damien Miller33804262001-02-04 23:20:18 +11001047 }
Damien Miller33804262001-02-04 23:20:18 +11001048 break;
1049 case I_PWD:
1050 printf("Remote working directory: %s\n", *pwd);
1051 break;
1052 case I_LPWD:
Damien Miller956f3fb2003-01-10 21:40:00 +11001053 if (!getcwd(path_buf, sizeof(path_buf))) {
1054 error("Couldn't get local cwd: %s", strerror(errno));
1055 err = -1;
1056 break;
1057 }
1058 printf("Local working directory: %s\n", path_buf);
Damien Miller33804262001-02-04 23:20:18 +11001059 break;
1060 case I_QUIT:
Damien Miller956f3fb2003-01-10 21:40:00 +11001061 /* Processed below */
1062 break;
Damien Miller33804262001-02-04 23:20:18 +11001063 case I_HELP:
1064 help();
1065 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +00001066 case I_VERSION:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001067 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
Ben Lindstromf78682d2001-03-14 21:26:27 +00001068 break;
Damien Miller62d57f62003-01-10 21:43:24 +11001069 case I_PROGRESS:
1070 showprogress = !showprogress;
1071 if (showprogress)
1072 printf("Progress meter enabled\n");
1073 else
1074 printf("Progress meter disabled\n");
1075 break;
Damien Miller33804262001-02-04 23:20:18 +11001076 default:
1077 fatal("%d is not implemented", cmdnum);
1078 }
1079
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001080 if (g.gl_pathc)
1081 globfree(&g);
Damien Miller33804262001-02-04 23:20:18 +11001082 if (path1)
1083 xfree(path1);
1084 if (path2)
1085 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001086
Damien Miller956f3fb2003-01-10 21:40:00 +11001087 /* If an unignored error occurs in batch mode we should abort. */
1088 if (err_abort && err != 0)
1089 return (-1);
1090 else if (cmdnum == I_QUIT)
1091 return (1);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001092
Damien Miller956f3fb2003-01-10 21:40:00 +11001093 return (0);
Damien Miller33804262001-02-04 23:20:18 +11001094}
1095
Damien Miller956f3fb2003-01-10 21:40:00 +11001096int
Ben Lindstrom63667f62001-04-13 00:00:14 +00001097interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
Damien Miller33804262001-02-04 23:20:18 +11001098{
1099 char *pwd;
Ben Lindstrom63667f62001-04-13 00:00:14 +00001100 char *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +11001101 char cmd[2048];
Damien Miller3db5f532002-02-13 14:10:32 +11001102 struct sftp_conn *conn;
Damien Miller956f3fb2003-01-10 21:40:00 +11001103 int err;
Damien Miller33804262001-02-04 23:20:18 +11001104
Damien Miller3db5f532002-02-13 14:10:32 +11001105 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1106 if (conn == NULL)
Damien Miller058316f2001-03-08 10:08:49 +11001107 fatal("Couldn't initialise connection to server");
1108
Damien Miller3db5f532002-02-13 14:10:32 +11001109 pwd = do_realpath(conn, ".");
Damien Miller33804262001-02-04 23:20:18 +11001110 if (pwd == NULL)
1111 fatal("Need cwd");
1112
Ben Lindstrom63667f62001-04-13 00:00:14 +00001113 if (file1 != NULL) {
1114 dir = xstrdup(file1);
1115 dir = make_absolute(dir, pwd);
1116
Damien Miller3db5f532002-02-13 14:10:32 +11001117 if (remote_is_dir(conn, dir) && file2 == NULL) {
Ben Lindstrom63667f62001-04-13 00:00:14 +00001118 printf("Changing to: %s\n", dir);
1119 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
Damien Miller956f3fb2003-01-10 21:40:00 +11001120 if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0)
1121 return (-1);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001122 } else {
1123 if (file2 == NULL)
1124 snprintf(cmd, sizeof cmd, "get %s", dir);
1125 else
1126 snprintf(cmd, sizeof cmd, "get %s %s", dir,
1127 file2);
1128
Damien Miller956f3fb2003-01-10 21:40:00 +11001129 err = parse_dispatch_command(conn, cmd, &pwd, 1);
Ben Lindstromcb1f60e2002-03-22 02:47:28 +00001130 xfree(dir);
Damien Miller00111382003-03-10 11:21:17 +11001131 xfree(pwd);
Damien Miller956f3fb2003-01-10 21:40:00 +11001132 return (err);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001133 }
Ben Lindstromcb1f60e2002-03-22 02:47:28 +00001134 xfree(dir);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001135 }
Damien Miller956f3fb2003-01-10 21:40:00 +11001136
Ben Lindstrom6aebb342001-05-09 00:38:19 +00001137#if HAVE_SETVBUF
Damien Millerd7686fd2001-02-10 00:40:03 +11001138 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001139 setvbuf(infile, NULL, _IOLBF, 0);
Ben Lindstrom6aebb342001-05-09 00:38:19 +00001140#else
1141 setlinebuf(stdout);
1142 setlinebuf(infile);
1143#endif
Damien Miller33804262001-02-04 23:20:18 +11001144
Damien Miller956f3fb2003-01-10 21:40:00 +11001145 err = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001146 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001147 char *cp;
1148
1149 printf("sftp> ");
1150
1151 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001152 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +11001153 printf("\n");
1154 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001155 } else if (infile != stdin) /* Bluff typing */
1156 printf("%s", cmd);
1157
Damien Miller33804262001-02-04 23:20:18 +11001158 cp = strrchr(cmd, '\n');
1159 if (cp)
1160 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001161
Damien Miller956f3fb2003-01-10 21:40:00 +11001162 err = parse_dispatch_command(conn, cmd, &pwd, infile != stdin);
1163 if (err != 0)
Damien Miller33804262001-02-04 23:20:18 +11001164 break;
1165 }
1166 xfree(pwd);
Damien Miller956f3fb2003-01-10 21:40:00 +11001167
1168 /* err == 1 signifies normal "quit" exit */
1169 return (err >= 0 ? 0 : -1);
Damien Miller33804262001-02-04 23:20:18 +11001170}
Damien Miller956f3fb2003-01-10 21:40:00 +11001171