blob: 013ea84e0f02c090170b442fc6545c06299f1c78 [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 Millercb3e3c82003-01-16 15:39:13 +110028RCSID("$OpenBSD: sftp-int.c,v 1.56 2003/01/16 03:41:55 djm 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
56/* Commands for interactive mode */
57#define I_CHDIR 1
58#define I_CHGRP 2
59#define I_CHMOD 3
60#define I_CHOWN 4
61#define I_GET 5
62#define I_HELP 6
63#define I_LCHDIR 7
64#define I_LLS 8
65#define I_LMKDIR 9
66#define I_LPWD 10
67#define I_LS 11
68#define I_LUMASK 12
69#define I_MKDIR 13
70#define I_PUT 14
71#define I_PWD 15
72#define I_QUIT 16
73#define I_RENAME 17
74#define I_RM 18
75#define I_RMDIR 19
76#define I_SHELL 20
Damien Miller058316f2001-03-08 10:08:49 +110077#define I_SYMLINK 21
Ben Lindstromf78682d2001-03-14 21:26:27 +000078#define I_VERSION 22
Damien Miller62d57f62003-01-10 21:43:24 +110079#define I_PROGRESS 23
Damien Miller33804262001-02-04 23:20:18 +110080
81struct CMD {
Damien Miller33804262001-02-04 23:20:18 +110082 const char *c;
Kevin Steves62c45db2001-02-05 13:42:43 +000083 const int n;
Damien Miller33804262001-02-04 23:20:18 +110084};
85
Damien Millerdc708572003-01-14 22:24:05 +110086static const struct CMD cmds[] = {
Ben Lindstrom59e12492001-08-15 23:22:56 +000087 { "bye", I_QUIT },
Damien Millerd7686fd2001-02-10 00:40:03 +110088 { "cd", I_CHDIR },
89 { "chdir", I_CHDIR },
90 { "chgrp", I_CHGRP },
91 { "chmod", I_CHMOD },
92 { "chown", I_CHOWN },
93 { "dir", I_LS },
94 { "exit", I_QUIT },
95 { "get", I_GET },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +000096 { "mget", I_GET },
Damien Millerd7686fd2001-02-10 00:40:03 +110097 { "help", I_HELP },
98 { "lcd", I_LCHDIR },
99 { "lchdir", I_LCHDIR },
100 { "lls", I_LLS },
101 { "lmkdir", I_LMKDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100102 { "ln", I_SYMLINK },
Damien Millerd7686fd2001-02-10 00:40:03 +1100103 { "lpwd", I_LPWD },
104 { "ls", I_LS },
105 { "lumask", I_LUMASK },
106 { "mkdir", I_MKDIR },
Damien Miller62d57f62003-01-10 21:43:24 +1100107 { "progress", I_PROGRESS },
Damien Millerd7686fd2001-02-10 00:40:03 +1100108 { "put", I_PUT },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +0000109 { "mput", I_PUT },
Damien Millerd7686fd2001-02-10 00:40:03 +1100110 { "pwd", I_PWD },
111 { "quit", I_QUIT },
112 { "rename", I_RENAME },
113 { "rm", I_RM },
114 { "rmdir", I_RMDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100115 { "symlink", I_SYMLINK },
Ben Lindstromf78682d2001-03-14 21:26:27 +0000116 { "version", I_VERSION },
Kevin Steves62c45db2001-02-05 13:42:43 +0000117 { "!", I_SHELL },
118 { "?", I_HELP },
119 { NULL, -1}
Damien Miller33804262001-02-04 23:20:18 +1100120};
121
Ben Lindstrombba81212001-06-25 05:01:22 +0000122static void
Damien Miller33804262001-02-04 23:20:18 +1100123help(void)
124{
125 printf("Available commands:\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100126 printf("cd path Change remote directory to 'path'\n");
127 printf("lcd path Change local directory to 'path'\n");
128 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
129 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
130 printf("chown own path Change owner of file 'path' to 'own'\n");
131 printf("help Display this help text\n");
132 printf("get remote-path [local-path] Download file\n");
133 printf("lls [ls-options [path]] Display local directory listing\n");
Damien Miller058316f2001-03-08 10:08:49 +1100134 printf("ln oldpath newpath Symlink remote file\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100135 printf("lmkdir path Create local directory\n");
136 printf("lpwd Print local working directory\n");
137 printf("ls [path] Display remote directory listing\n");
138 printf("lumask umask Set local umask to 'umask'\n");
139 printf("mkdir path Create remote directory\n");
Damien Miller01413192003-01-14 22:22:11 +1100140 printf("progress Toggle display of progress meter\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100141 printf("put local-path [remote-path] Upload file\n");
142 printf("pwd Display remote working directory\n");
143 printf("exit Quit sftp\n");
144 printf("quit Quit sftp\n");
145 printf("rename oldpath newpath Rename remote file\n");
146 printf("rmdir path Remove remote directory\n");
147 printf("rm path Delete remote file\n");
Damien Miller058316f2001-03-08 10:08:49 +1100148 printf("symlink oldpath newpath Symlink remote file\n");
Ben Lindstromf78682d2001-03-14 21:26:27 +0000149 printf("version Show SFTP version\n");
Damien Miller33804262001-02-04 23:20:18 +1100150 printf("!command Execute 'command' in local shell\n");
151 printf("! Escape to local shell\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100152 printf("? Synonym for help\n");
Damien Miller33804262001-02-04 23:20:18 +1100153}
154
Ben Lindstrombba81212001-06-25 05:01:22 +0000155static void
Damien Miller33804262001-02-04 23:20:18 +1100156local_do_shell(const char *args)
157{
Ben Lindstrom206941f2001-04-15 14:27:16 +0000158 int status;
Damien Miller33804262001-02-04 23:20:18 +1100159 char *shell;
160 pid_t pid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000161
Damien Miller33804262001-02-04 23:20:18 +1100162 if (!*args)
163 args = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000164
Damien Miller33804262001-02-04 23:20:18 +1100165 if ((shell = getenv("SHELL")) == NULL)
166 shell = _PATH_BSHELL;
167
168 if ((pid = fork()) == -1)
169 fatal("Couldn't fork: %s", strerror(errno));
170
171 if (pid == 0) {
172 /* XXX: child has pipe fds to ssh subproc open - issue? */
173 if (args) {
174 debug3("Executing %s -c \"%s\"", shell, args);
Damien Millerefb1edf2001-07-14 12:19:36 +1000175 execl(shell, shell, "-c", args, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100176 } else {
177 debug3("Executing %s", shell);
Damien Millerefb1edf2001-07-14 12:19:36 +1000178 execl(shell, shell, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100179 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000180 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
Damien Miller33804262001-02-04 23:20:18 +1100181 strerror(errno));
182 _exit(1);
183 }
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000184 while (waitpid(pid, &status, 0) == -1)
185 if (errno != EINTR)
186 fatal("Couldn't wait for child: %s", strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +1100187 if (!WIFEXITED(status))
188 error("Shell exited abormally");
189 else if (WEXITSTATUS(status))
190 error("Shell exited with status %d", WEXITSTATUS(status));
191}
192
Ben Lindstrombba81212001-06-25 05:01:22 +0000193static void
Damien Miller33804262001-02-04 23:20:18 +1100194local_do_ls(const char *args)
195{
196 if (!args || !*args)
Damien Millerd7686fd2001-02-10 00:40:03 +1100197 local_do_shell(_PATH_LS);
Damien Miller33804262001-02-04 23:20:18 +1100198 else {
Damien Millerd7686fd2001-02-10 00:40:03 +1100199 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
200 char *buf = xmalloc(len);
Damien Miller33804262001-02-04 23:20:18 +1100201
202 /* XXX: quoting - rip quoting code from ftp? */
Damien Millerd7686fd2001-02-10 00:40:03 +1100203 snprintf(buf, len, _PATH_LS " %s", args);
Damien Miller33804262001-02-04 23:20:18 +1100204 local_do_shell(buf);
Damien Millerd7686fd2001-02-10 00:40:03 +1100205 xfree(buf);
Damien Miller33804262001-02-04 23:20:18 +1100206 }
207}
208
Damien Millere1a49812002-09-12 09:54:25 +1000209/* Strip one path (usually the pwd) from the start of another */
210static char *
211path_strip(char *path, char *strip)
212{
213 size_t len;
Damien Millere1a49812002-09-12 09:54:25 +1000214
215 if (strip == NULL)
216 return (xstrdup(path));
217
218 len = strlen(strip);
219 if (strip != NULL && strncmp(path, strip, len) == 0) {
220 if (strip[len - 1] != '/' && path[len] == '/')
221 len++;
222 return (xstrdup(path + len));
223 }
224
225 return (xstrdup(path));
226}
227
Ben Lindstrombba81212001-06-25 05:01:22 +0000228static char *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000229path_append(char *p1, char *p2)
230{
231 char *ret;
Ben Lindstromcf00df62001-03-17 00:37:31 +0000232 int len = strlen(p1) + strlen(p2) + 2;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000233
Ben Lindstromcf00df62001-03-17 00:37:31 +0000234 ret = xmalloc(len);
235 strlcpy(ret, p1, len);
Damien Millere1a49812002-09-12 09:54:25 +1000236 if (p1[strlen(p1) - 1] != '/')
Ben Lindstrom95148e32001-08-06 21:30:53 +0000237 strlcat(ret, "/", len);
Ben Lindstromcf00df62001-03-17 00:37:31 +0000238 strlcat(ret, p2, len);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000239
240 return(ret);
241}
242
Ben Lindstrombba81212001-06-25 05:01:22 +0000243static char *
Damien Miller33804262001-02-04 23:20:18 +1100244make_absolute(char *p, char *pwd)
245{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000246 char *abs;
Damien Miller33804262001-02-04 23:20:18 +1100247
248 /* Derelativise */
249 if (p && p[0] != '/') {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000250 abs = path_append(pwd, p);
Damien Miller33804262001-02-04 23:20:18 +1100251 xfree(p);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000252 return(abs);
253 } else
254 return(p);
255}
256
Ben Lindstrombba81212001-06-25 05:01:22 +0000257static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000258infer_path(const char *p, char **ifp)
259{
260 char *cp;
261
262 cp = strrchr(p, '/');
263 if (cp == NULL) {
264 *ifp = xstrdup(p);
265 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100266 }
267
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000268 if (!cp[1]) {
269 error("Invalid path");
270 return(-1);
271 }
272
273 *ifp = xstrdup(cp + 1);
274 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100275}
276
Ben Lindstrombba81212001-06-25 05:01:22 +0000277static int
Damien Miller33804262001-02-04 23:20:18 +1100278parse_getput_flags(const char **cpp, int *pflag)
279{
280 const char *cp = *cpp;
281
282 /* Check for flags */
283 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100284 switch (cp[1]) {
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000285 case 'p':
Damien Miller33804262001-02-04 23:20:18 +1100286 case 'P':
287 *pflag = 1;
288 break;
289 default:
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000290 error("Invalid flag -%c", cp[1]);
Damien Miller33804262001-02-04 23:20:18 +1100291 return(-1);
292 }
293 cp += 2;
294 *cpp = cp + strspn(cp, WHITESPACE);
295 }
296
297 return(0);
298}
299
Ben Lindstrombba81212001-06-25 05:01:22 +0000300static int
Damien Millere1a49812002-09-12 09:54:25 +1000301parse_ls_flags(const char **cpp, int *lflag)
302{
303 const char *cp = *cpp;
304
305 /* Check for flags */
306 if (cp++[0] == '-') {
307 for(; strchr(WHITESPACE, *cp) == NULL; cp++) {
308 switch (*cp) {
309 case 'l':
310 *lflag = 1;
311 break;
312 default:
313 error("Invalid flag -%c", *cp);
314 return(-1);
315 }
316 }
317 *cpp = cp + strspn(cp, WHITESPACE);
318 }
319
320 return(0);
321}
322
323static int
Damien Miller33804262001-02-04 23:20:18 +1100324get_pathname(const char **cpp, char **path)
325{
Damien Millerd7686fd2001-02-10 00:40:03 +1100326 const char *cp = *cpp, *end;
327 char quot;
Damien Miller33804262001-02-04 23:20:18 +1100328 int i;
329
330 cp += strspn(cp, WHITESPACE);
331 if (!*cp) {
332 *cpp = cp;
333 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100334 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100335 }
336
337 /* Check for quoted filenames */
338 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100339 quot = *cp++;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000340
Damien Millerd7686fd2001-02-10 00:40:03 +1100341 end = strchr(cp, quot);
342 if (end == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100343 error("Unterminated quote");
Damien Millerd7686fd2001-02-10 00:40:03 +1100344 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100345 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100346 if (cp == end) {
Damien Miller33804262001-02-04 23:20:18 +1100347 error("Empty quotes");
Damien Millerd7686fd2001-02-10 00:40:03 +1100348 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100349 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100350 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
351 } else {
352 /* Read to end of filename */
353 end = strpbrk(cp, WHITESPACE);
354 if (end == NULL)
355 end = strchr(cp, '\0');
356 *cpp = end + strspn(end, WHITESPACE);
Damien Miller33804262001-02-04 23:20:18 +1100357 }
358
Damien Millerd7686fd2001-02-10 00:40:03 +1100359 i = end - cp;
Damien Miller33804262001-02-04 23:20:18 +1100360
361 *path = xmalloc(i + 1);
362 memcpy(*path, cp, i);
363 (*path)[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100364 return(0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100365
366 fail:
367 *path = NULL;
368 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100369}
370
Ben Lindstrombba81212001-06-25 05:01:22 +0000371static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000372is_dir(char *path)
Damien Miller33804262001-02-04 23:20:18 +1100373{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000374 struct stat sb;
Damien Miller33804262001-02-04 23:20:18 +1100375
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000376 /* XXX: report errors? */
377 if (stat(path, &sb) == -1)
Damien Miller33804262001-02-04 23:20:18 +1100378 return(0);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000379
380 return(sb.st_mode & S_IFDIR);
381}
382
Ben Lindstrombba81212001-06-25 05:01:22 +0000383static int
Damien Miller5fa01fd2003-01-14 22:24:47 +1100384is_reg(char *path)
385{
386 struct stat sb;
387
388 if (stat(path, &sb) == -1)
389 fatal("stat %s: %s", path, strerror(errno));
390
391 return(S_ISREG(sb.st_mode));
392}
393
394static int
Damien Miller3db5f532002-02-13 14:10:32 +1100395remote_is_dir(struct sftp_conn *conn, char *path)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000396{
397 Attrib *a;
398
399 /* XXX: report errors? */
Damien Miller3db5f532002-02-13 14:10:32 +1100400 if ((a = do_stat(conn, path, 1)) == NULL)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000401 return(0);
402 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
403 return(0);
404 return(a->perm & S_IFDIR);
405}
406
Ben Lindstrombba81212001-06-25 05:01:22 +0000407static int
Damien Miller3db5f532002-02-13 14:10:32 +1100408process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000409{
410 char *abs_src = NULL;
411 char *abs_dst = NULL;
412 char *tmp;
413 glob_t g;
414 int err = 0;
415 int i;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000416
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000417 abs_src = xstrdup(src);
418 abs_src = make_absolute(abs_src, pwd);
419
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000420 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000421 debug3("Looking up %s", abs_src);
Damien Miller3db5f532002-02-13 14:10:32 +1100422 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000423 error("File \"%s\" not found.", abs_src);
424 err = -1;
425 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100426 }
427
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000428 /* Only one match, dst may be file, directory or unspecified */
429 if (g.gl_pathv[0] && g.gl_matchc == 1) {
430 if (dst) {
431 /* If directory specified, append filename */
432 if (is_dir(dst)) {
433 if (infer_path(g.gl_pathv[0], &tmp)) {
434 err = 1;
435 goto out;
436 }
437 abs_dst = path_append(dst, tmp);
438 xfree(tmp);
439 } else
440 abs_dst = xstrdup(dst);
441 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
442 err = -1;
443 goto out;
444 }
Damien Miller3db5f532002-02-13 14:10:32 +1100445 err = do_download(conn, g.gl_pathv[0], abs_dst, pflag);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000446 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100447 }
448
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000449 /* Multiple matches, dst may be directory or unspecified */
450 if (dst && !is_dir(dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000451 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000452 dst);
453 err = -1;
454 goto out;
455 }
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000456
Damien Miller9f0f5c62001-12-21 14:45:46 +1100457 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000458 if (infer_path(g.gl_pathv[i], &tmp)) {
459 err = -1;
460 goto out;
461 }
462 if (dst) {
463 abs_dst = path_append(dst, tmp);
464 xfree(tmp);
465 } else
466 abs_dst = tmp;
467
468 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100469 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000470 err = -1;
471 xfree(abs_dst);
472 abs_dst = NULL;
473 }
474
475out:
476 xfree(abs_src);
477 if (abs_dst)
478 xfree(abs_dst);
479 globfree(&g);
480 return(err);
481}
482
Ben Lindstrombba81212001-06-25 05:01:22 +0000483static int
Damien Miller3db5f532002-02-13 14:10:32 +1100484process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000485{
486 char *tmp_dst = NULL;
487 char *abs_dst = NULL;
488 char *tmp;
489 glob_t g;
490 int err = 0;
491 int i;
492
493 if (dst) {
494 tmp_dst = xstrdup(dst);
495 tmp_dst = make_absolute(tmp_dst, pwd);
496 }
497
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000498 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000499 debug3("Looking up %s", src);
500 if (glob(src, 0, NULL, &g)) {
501 error("File \"%s\" not found.", src);
502 err = -1;
503 goto out;
504 }
505
506 /* Only one match, dst may be file, directory or unspecified */
507 if (g.gl_pathv[0] && g.gl_matchc == 1) {
Damien Millercb3e3c82003-01-16 15:39:13 +1100508 if (!is_reg(g.gl_pathv[0])) {
Damien Miller5fa01fd2003-01-14 22:24:47 +1100509 error("Can't upload %s: not a regular file",
510 g.gl_pathv[0]);
511 err = 1;
512 goto out;
513 }
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000514 if (tmp_dst) {
515 /* If directory specified, append filename */
Damien Miller3db5f532002-02-13 14:10:32 +1100516 if (remote_is_dir(conn, tmp_dst)) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000517 if (infer_path(g.gl_pathv[0], &tmp)) {
518 err = 1;
519 goto out;
520 }
521 abs_dst = path_append(tmp_dst, tmp);
522 xfree(tmp);
523 } else
524 abs_dst = xstrdup(tmp_dst);
Ben Lindstrom7527f8b2001-03-24 00:39:12 +0000525 } else {
526 if (infer_path(g.gl_pathv[0], &abs_dst)) {
527 err = -1;
528 goto out;
529 }
530 abs_dst = make_absolute(abs_dst, pwd);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000531 }
Damien Miller3db5f532002-02-13 14:10:32 +1100532 err = do_upload(conn, g.gl_pathv[0], abs_dst, pflag);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000533 goto out;
534 }
535
536 /* Multiple matches, dst may be directory or unspecified */
Damien Miller3db5f532002-02-13 14:10:32 +1100537 if (tmp_dst && !remote_is_dir(conn, tmp_dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000538 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000539 tmp_dst);
540 err = -1;
541 goto out;
542 }
543
Damien Miller9f0f5c62001-12-21 14:45:46 +1100544 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller5fa01fd2003-01-14 22:24:47 +1100545 if (!is_reg(g.gl_pathv[i])) {
546 error("skipping non-regular file %s",
547 g.gl_pathv[i]);
548 continue;
549 }
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000550 if (infer_path(g.gl_pathv[i], &tmp)) {
551 err = -1;
552 goto out;
553 }
554 if (tmp_dst) {
555 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);
570 return(err);
Damien Miller33804262001-02-04 23:20:18 +1100571}
572
Ben Lindstrombba81212001-06-25 05:01:22 +0000573static int
Damien Millere1a49812002-09-12 09:54:25 +1000574sdirent_comp(const void *aa, const void *bb)
575{
576 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
577 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
578
Ben Lindstrom93576d92002-12-23 02:06:19 +0000579 return (strcmp(a->filename, b->filename));
Damien Millere1a49812002-09-12 09:54:25 +1000580}
581
582/* sftp ls.1 replacement for directories */
583static int
584do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
585{
586 int n;
587 SFTP_DIRENT **d;
588
589 if ((n = do_readdir(conn, path, &d)) != 0)
590 return (n);
591
Ben Lindstrom93576d92002-12-23 02:06:19 +0000592 /* Count entries for sort */
Damien Millere1a49812002-09-12 09:54:25 +1000593 for (n = 0; d[n] != NULL; n++)
594 ;
595
596 qsort(d, n, sizeof(*d), sdirent_comp);
597
598 for (n = 0; d[n] != NULL; n++) {
599 char *tmp, *fname;
Ben Lindstrom93576d92002-12-23 02:06:19 +0000600
Damien Millere1a49812002-09-12 09:54:25 +1000601 tmp = path_append(path, d[n]->filename);
602 fname = path_strip(tmp, strip_path);
603 xfree(tmp);
604
605 if (lflag) {
606 char *lname;
607 struct stat sb;
608
609 memset(&sb, 0, sizeof(sb));
610 attrib_to_stat(&d[n]->a, &sb);
611 lname = ls_file(fname, &sb, 1);
612 printf("%s\n", lname);
613 xfree(lname);
614 } else {
615 /* XXX - multicolumn display would be nice here */
616 printf("%s\n", fname);
617 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000618
Damien Millere1a49812002-09-12 09:54:25 +1000619 xfree(fname);
620 }
621
622 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;
632 int i;
633 Attrib *a;
634 struct stat sb;
635
636 memset(&g, 0, sizeof(g));
637
Ben Lindstrom93576d92002-12-23 02:06:19 +0000638 if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
Damien Millere1a49812002-09-12 09:54:25 +1000639 NULL, &g)) {
640 error("Can't ls: \"%s\" not found", path);
641 return (-1);
642 }
643
644 /*
Ben Lindstrom93576d92002-12-23 02:06:19 +0000645 * If the glob returns a single match, which is the same as the
Damien Millere1a49812002-09-12 09:54:25 +1000646 * input glob, and it is a directory, then just list its contents
647 */
Ben Lindstrom93576d92002-12-23 02:06:19 +0000648 if (g.gl_pathc == 1 &&
Damien Millere1a49812002-09-12 09:54:25 +1000649 strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
650 if ((a = do_lstat(conn, path, 1)) == NULL) {
651 globfree(&g);
652 return (-1);
653 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000654 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Millere1a49812002-09-12 09:54:25 +1000655 S_ISDIR(a->perm)) {
656 globfree(&g);
657 return (do_ls_dir(conn, path, strip_path, lflag));
658 }
659 }
660
661 for (i = 0; g.gl_pathv[i]; i++) {
662 char *fname, *lname;
663
664 fname = path_strip(g.gl_pathv[i], strip_path);
665
666 if (lflag) {
667 /*
668 * XXX: this is slow - 1 roundtrip per path
Ben Lindstrom93576d92002-12-23 02:06:19 +0000669 * A solution to this is to fork glob() and
670 * build a sftp specific version which keeps the
Damien Millere1a49812002-09-12 09:54:25 +1000671 * attribs (which currently get thrown away)
672 * that the server returns as well as the filenames.
673 */
674 memset(&sb, 0, sizeof(sb));
675 a = do_lstat(conn, g.gl_pathv[i], 1);
676 if (a != NULL)
677 attrib_to_stat(a, &sb);
678 lname = ls_file(fname, &sb, 1);
679 printf("%s\n", lname);
680 xfree(lname);
681 } else {
682 /* XXX - multicolumn display would be nice here */
683 printf("%s\n", fname);
684 }
685 xfree(fname);
686 }
687
688 if (g.gl_pathc)
689 globfree(&g);
690
691 return (0);
692}
693
694static int
Damien Miller956f3fb2003-01-10 21:40:00 +1100695parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
Damien Millere1a49812002-09-12 09:54:25 +1000696 unsigned long *n_arg, char **path1, char **path2)
Damien Miller33804262001-02-04 23:20:18 +1100697{
698 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000699 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000700 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000701 long l;
Damien Miller33804262001-02-04 23:20:18 +1100702 int i, cmdnum;
703
704 /* Skip leading whitespace */
705 cp = cp + strspn(cp, WHITESPACE);
706
Damien Miller956f3fb2003-01-10 21:40:00 +1100707 /* Ignore blank lines and lines which begin with comment '#' char */
708 if (*cp == '\0' || *cp == '#')
709 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100710
Damien Miller956f3fb2003-01-10 21:40:00 +1100711 /* Check for leading '-' (disable error processing) */
712 *iflag = 0;
713 if (*cp == '-') {
714 *iflag = 1;
715 cp++;
716 }
717
Damien Miller33804262001-02-04 23:20:18 +1100718 /* Figure out which command we have */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100719 for (i = 0; cmds[i].c; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100720 int cmdlen = strlen(cmds[i].c);
721
722 /* Check for command followed by whitespace */
723 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
724 strchr(WHITESPACE, cp[cmdlen])) {
725 cp += cmdlen;
726 cp = cp + strspn(cp, WHITESPACE);
727 break;
728 }
729 }
730 cmdnum = cmds[i].n;
731 cmd = cmds[i].c;
732
733 /* Special case */
734 if (*cp == '!') {
735 cp++;
736 cmdnum = I_SHELL;
737 } else if (cmdnum == -1) {
738 error("Invalid command.");
Damien Miller956f3fb2003-01-10 21:40:00 +1100739 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100740 }
741
742 /* Get arguments and parse flags */
Damien Millere1a49812002-09-12 09:54:25 +1000743 *lflag = *pflag = *n_arg = 0;
Damien Miller33804262001-02-04 23:20:18 +1100744 *path1 = *path2 = NULL;
745 switch (cmdnum) {
746 case I_GET:
747 case I_PUT:
748 if (parse_getput_flags(&cp, pflag))
749 return(-1);
750 /* Get first pathname (mandatory) */
751 if (get_pathname(&cp, path1))
752 return(-1);
753 if (*path1 == NULL) {
754 error("You must specify at least one path after a "
755 "%s command.", cmd);
756 return(-1);
757 }
758 /* Try to get second pathname (optional) */
759 if (get_pathname(&cp, path2))
760 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100761 break;
762 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100763 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100764 if (get_pathname(&cp, path1))
765 return(-1);
766 if (get_pathname(&cp, path2))
767 return(-1);
768 if (!*path1 || !*path2) {
769 error("You must specify two paths after a %s "
770 "command.", cmd);
771 return(-1);
772 }
773 break;
774 case I_RM:
775 case I_MKDIR:
776 case I_RMDIR:
777 case I_CHDIR:
778 case I_LCHDIR:
779 case I_LMKDIR:
780 /* Get pathname (mandatory) */
781 if (get_pathname(&cp, path1))
782 return(-1);
783 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000784 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100785 cmd);
786 return(-1);
787 }
788 break;
789 case I_LS:
Damien Millere1a49812002-09-12 09:54:25 +1000790 if (parse_ls_flags(&cp, lflag))
791 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100792 /* Path is optional */
793 if (get_pathname(&cp, path1))
794 return(-1);
795 break;
796 case I_LLS:
797 case I_SHELL:
798 /* Uses the rest of the line */
799 break;
800 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000801 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100802 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000803 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100804 case I_CHOWN:
805 case I_CHGRP:
806 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000807 l = strtol(cp, &cp2, base);
808 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
809 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100810 error("You must supply a numeric argument "
811 "to the %s command.", cmd);
812 return(-1);
813 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000814 cp = cp2;
815 *n_arg = l;
816 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
817 break;
818 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100819 error("You must supply a numeric argument "
820 "to the %s command.", cmd);
821 return(-1);
822 }
823 cp += strspn(cp, WHITESPACE);
824
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_QUIT:
835 case I_PWD:
836 case I_LPWD:
837 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000838 case I_VERSION:
Damien Miller62d57f62003-01-10 21:43:24 +1100839 case I_PROGRESS:
Damien Miller33804262001-02-04 23:20:18 +1100840 break;
841 default:
842 fatal("Command not implemented");
843 }
844
845 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100846 return(cmdnum);
847}
848
Ben Lindstrombba81212001-06-25 05:01:22 +0000849static int
Damien Miller956f3fb2003-01-10 21:40:00 +1100850parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
851 int err_abort)
Damien Miller33804262001-02-04 23:20:18 +1100852{
Damien Millerd7686fd2001-02-10 00:40:03 +1100853 char *path1, *path2, *tmp;
Damien Miller956f3fb2003-01-10 21:40:00 +1100854 int pflag, lflag, iflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100855 unsigned long n_arg;
856 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000857 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000858 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100859 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100860
861 path1 = path2 = NULL;
Damien Miller956f3fb2003-01-10 21:40:00 +1100862 cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
Damien Millere1a49812002-09-12 09:54:25 +1000863 &path1, &path2);
Damien Miller33804262001-02-04 23:20:18 +1100864
Damien Miller956f3fb2003-01-10 21:40:00 +1100865 if (iflag != 0)
866 err_abort = 0;
867
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000868 memset(&g, 0, sizeof(g));
869
Damien Miller33804262001-02-04 23:20:18 +1100870 /* Perform command */
871 switch (cmdnum) {
Damien Miller956f3fb2003-01-10 21:40:00 +1100872 case 0:
873 /* Blank line */
874 break;
Damien Miller33804262001-02-04 23:20:18 +1100875 case -1:
Damien Miller956f3fb2003-01-10 21:40:00 +1100876 /* Unrecognized command */
877 err = -1;
Damien Miller33804262001-02-04 23:20:18 +1100878 break;
879 case I_GET:
Damien Miller3db5f532002-02-13 14:10:32 +1100880 err = process_get(conn, path1, path2, *pwd, pflag);
Damien Miller33804262001-02-04 23:20:18 +1100881 break;
882 case I_PUT:
Damien Miller3db5f532002-02-13 14:10:32 +1100883 err = process_put(conn, path1, path2, *pwd, pflag);
Ben Lindstroma3700052001-04-05 23:26:32 +0000884 break;
885 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100886 path1 = make_absolute(path1, *pwd);
887 path2 = make_absolute(path2, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100888 err = do_rename(conn, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100889 break;
Damien Miller058316f2001-03-08 10:08:49 +1100890 case I_SYMLINK:
Damien Miller3db5f532002-02-13 14:10:32 +1100891 path2 = make_absolute(path2, *pwd);
892 err = do_symlink(conn, path1, path2);
Damien Miller058316f2001-03-08 10:08:49 +1100893 break;
Damien Miller33804262001-02-04 23:20:18 +1100894 case I_RM:
895 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100896 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100897 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100898 printf("Removing %s\n", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +1100899 err = do_rm(conn, g.gl_pathv[i]);
900 if (err != 0 && err_abort)
901 break;
Damien Miller4870afd2001-03-14 10:27:09 +1100902 }
Damien Miller33804262001-02-04 23:20:18 +1100903 break;
904 case I_MKDIR:
905 path1 = make_absolute(path1, *pwd);
906 attrib_clear(&a);
907 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
908 a.perm = 0777;
Damien Miller3db5f532002-02-13 14:10:32 +1100909 err = do_mkdir(conn, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100910 break;
911 case I_RMDIR:
912 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100913 err = do_rmdir(conn, path1);
Damien Miller33804262001-02-04 23:20:18 +1100914 break;
915 case I_CHDIR:
916 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100917 if ((tmp = do_realpath(conn, path1)) == NULL) {
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000918 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100919 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000920 }
Damien Miller3db5f532002-02-13 14:10:32 +1100921 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100922 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000923 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100924 break;
925 }
926 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
927 error("Can't change directory: Can't check target");
928 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000929 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100930 break;
931 }
932 if (!S_ISDIR(aa->perm)) {
933 error("Can't change directory: \"%s\" is not "
934 "a directory", tmp);
935 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000936 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100937 break;
938 }
Damien Miller33804262001-02-04 23:20:18 +1100939 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100940 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100941 break;
942 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100943 if (!path1) {
Damien Millere1a49812002-09-12 09:54:25 +1000944 do_globbed_ls(conn, *pwd, *pwd, lflag);
Damien Millerd7686fd2001-02-10 00:40:03 +1100945 break;
946 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000947
Damien Millere1a49812002-09-12 09:54:25 +1000948 /* Strip pwd off beginning of non-absolute paths */
949 tmp = NULL;
950 if (*path1 != '/')
951 tmp = *pwd;
952
Damien Miller33804262001-02-04 23:20:18 +1100953 path1 = make_absolute(path1, *pwd);
Damien Miller956f3fb2003-01-10 21:40:00 +1100954 err = do_globbed_ls(conn, path1, tmp, lflag);
Damien Miller33804262001-02-04 23:20:18 +1100955 break;
956 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000957 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100958 error("Couldn't change local directory to "
959 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000960 err = 1;
961 }
Damien Miller33804262001-02-04 23:20:18 +1100962 break;
963 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000964 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100965 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100966 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000967 err = 1;
968 }
Damien Miller33804262001-02-04 23:20:18 +1100969 break;
970 case I_LLS:
971 local_do_ls(cmd);
972 break;
973 case I_SHELL:
974 local_do_shell(cmd);
975 break;
976 case I_LUMASK:
977 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +0000978 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +1100979 break;
980 case I_CHMOD:
981 path1 = make_absolute(path1, *pwd);
982 attrib_clear(&a);
983 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
984 a.perm = n_arg;
Damien Miller3db5f532002-02-13 14:10:32 +1100985 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100986 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100987 printf("Changing mode on %s\n", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +1100988 err = do_setstat(conn, g.gl_pathv[i], &a);
989 if (err != 0 && err_abort)
990 break;
Damien Miller4870afd2001-03-14 10:27:09 +1100991 }
Kevin Steves62c45db2001-02-05 13:42:43 +0000992 break;
Damien Miller33804262001-02-04 23:20:18 +1100993 case I_CHOWN:
Damien Miller33804262001-02-04 23:20:18 +1100994 case I_CHGRP:
995 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100996 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100997 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller956f3fb2003-01-10 21:40:00 +1100998 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
999 if (err != 0 && err_abort)
1000 break;
1001 else
1002 continue;
1003 }
Damien Miller4870afd2001-03-14 10:27:09 +11001004 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1005 error("Can't get current ownership of "
1006 "remote file \"%s\"", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +11001007 if (err != 0 && err_abort)
1008 break;
1009 else
1010 continue;
Damien Miller4870afd2001-03-14 10:27:09 +11001011 }
Damien Miller4870afd2001-03-14 10:27:09 +11001012 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
Damien Miller956f3fb2003-01-10 21:40:00 +11001013 if (cmdnum == I_CHOWN) {
1014 printf("Changing owner on %s\n", g.gl_pathv[i]);
1015 aa->uid = n_arg;
1016 } else {
1017 printf("Changing group on %s\n", g.gl_pathv[i]);
1018 aa->gid = n_arg;
1019 }
1020 err = do_setstat(conn, g.gl_pathv[i], aa);
1021 if (err != 0 && err_abort)
1022 break;
Damien Miller33804262001-02-04 23:20:18 +11001023 }
Damien Miller33804262001-02-04 23:20:18 +11001024 break;
1025 case I_PWD:
1026 printf("Remote working directory: %s\n", *pwd);
1027 break;
1028 case I_LPWD:
Damien Miller956f3fb2003-01-10 21:40:00 +11001029 if (!getcwd(path_buf, sizeof(path_buf))) {
1030 error("Couldn't get local cwd: %s", strerror(errno));
1031 err = -1;
1032 break;
1033 }
1034 printf("Local working directory: %s\n", path_buf);
Damien Miller33804262001-02-04 23:20:18 +11001035 break;
1036 case I_QUIT:
Damien Miller956f3fb2003-01-10 21:40:00 +11001037 /* Processed below */
1038 break;
Damien Miller33804262001-02-04 23:20:18 +11001039 case I_HELP:
1040 help();
1041 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +00001042 case I_VERSION:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001043 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
Ben Lindstromf78682d2001-03-14 21:26:27 +00001044 break;
Damien Miller62d57f62003-01-10 21:43:24 +11001045 case I_PROGRESS:
1046 showprogress = !showprogress;
1047 if (showprogress)
1048 printf("Progress meter enabled\n");
1049 else
1050 printf("Progress meter disabled\n");
1051 break;
Damien Miller33804262001-02-04 23:20:18 +11001052 default:
1053 fatal("%d is not implemented", cmdnum);
1054 }
1055
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001056 if (g.gl_pathc)
1057 globfree(&g);
Damien Miller33804262001-02-04 23:20:18 +11001058 if (path1)
1059 xfree(path1);
1060 if (path2)
1061 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001062
Damien Miller956f3fb2003-01-10 21:40:00 +11001063 /* If an unignored error occurs in batch mode we should abort. */
1064 if (err_abort && err != 0)
1065 return (-1);
1066 else if (cmdnum == I_QUIT)
1067 return (1);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001068
Damien Miller956f3fb2003-01-10 21:40:00 +11001069 return (0);
Damien Miller33804262001-02-04 23:20:18 +11001070}
1071
Damien Miller956f3fb2003-01-10 21:40:00 +11001072int
Ben Lindstrom63667f62001-04-13 00:00:14 +00001073interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
Damien Miller33804262001-02-04 23:20:18 +11001074{
1075 char *pwd;
Ben Lindstrom63667f62001-04-13 00:00:14 +00001076 char *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +11001077 char cmd[2048];
Damien Miller3db5f532002-02-13 14:10:32 +11001078 struct sftp_conn *conn;
Damien Miller956f3fb2003-01-10 21:40:00 +11001079 int err;
Damien Miller33804262001-02-04 23:20:18 +11001080
Damien Miller3db5f532002-02-13 14:10:32 +11001081 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1082 if (conn == NULL)
Damien Miller058316f2001-03-08 10:08:49 +11001083 fatal("Couldn't initialise connection to server");
1084
Damien Miller3db5f532002-02-13 14:10:32 +11001085 pwd = do_realpath(conn, ".");
Damien Miller33804262001-02-04 23:20:18 +11001086 if (pwd == NULL)
1087 fatal("Need cwd");
1088
Ben Lindstrom63667f62001-04-13 00:00:14 +00001089 if (file1 != NULL) {
1090 dir = xstrdup(file1);
1091 dir = make_absolute(dir, pwd);
1092
Damien Miller3db5f532002-02-13 14:10:32 +11001093 if (remote_is_dir(conn, dir) && file2 == NULL) {
Ben Lindstrom63667f62001-04-13 00:00:14 +00001094 printf("Changing to: %s\n", dir);
1095 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
Damien Miller956f3fb2003-01-10 21:40:00 +11001096 if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0)
1097 return (-1);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001098 } else {
1099 if (file2 == NULL)
1100 snprintf(cmd, sizeof cmd, "get %s", dir);
1101 else
1102 snprintf(cmd, sizeof cmd, "get %s %s", dir,
1103 file2);
1104
Damien Miller956f3fb2003-01-10 21:40:00 +11001105 err = parse_dispatch_command(conn, cmd, &pwd, 1);
Ben Lindstromcb1f60e2002-03-22 02:47:28 +00001106 xfree(dir);
Damien Miller956f3fb2003-01-10 21:40:00 +11001107 return (err);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001108 }
Ben Lindstromcb1f60e2002-03-22 02:47:28 +00001109 xfree(dir);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001110 }
Damien Miller956f3fb2003-01-10 21:40:00 +11001111
Ben Lindstrom6aebb342001-05-09 00:38:19 +00001112#if HAVE_SETVBUF
Damien Millerd7686fd2001-02-10 00:40:03 +11001113 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001114 setvbuf(infile, NULL, _IOLBF, 0);
Ben Lindstrom6aebb342001-05-09 00:38:19 +00001115#else
1116 setlinebuf(stdout);
1117 setlinebuf(infile);
1118#endif
Damien Miller33804262001-02-04 23:20:18 +11001119
Damien Miller956f3fb2003-01-10 21:40:00 +11001120 err = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001121 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001122 char *cp;
1123
1124 printf("sftp> ");
1125
1126 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001127 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +11001128 printf("\n");
1129 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001130 } else if (infile != stdin) /* Bluff typing */
1131 printf("%s", cmd);
1132
Damien Miller33804262001-02-04 23:20:18 +11001133 cp = strrchr(cmd, '\n');
1134 if (cp)
1135 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001136
Damien Miller956f3fb2003-01-10 21:40:00 +11001137 err = parse_dispatch_command(conn, cmd, &pwd, infile != stdin);
1138 if (err != 0)
Damien Miller33804262001-02-04 23:20:18 +11001139 break;
1140 }
1141 xfree(pwd);
Damien Miller956f3fb2003-01-10 21:40:00 +11001142
1143 /* err == 1 signifies normal "quit" exit */
1144 return (err >= 0 ? 0 : -1);
Damien Miller33804262001-02-04 23:20:18 +11001145}
Damien Miller956f3fb2003-01-10 21:40:00 +11001146