blob: 88b0530ab94c7c7ba32e89b22eb4a4f85507df6e [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 Miller62d57f62003-01-10 21:43:24 +110028RCSID("$OpenBSD: sftp-int.c,v 1.52 2003/01/10 08:19:07 fgsch 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
86const 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 Miller62d57f62003-01-10 21:43:24 +1100140 printf("preogress 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 Miller3db5f532002-02-13 14:10:32 +1100384remote_is_dir(struct sftp_conn *conn, char *path)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000385{
386 Attrib *a;
387
388 /* XXX: report errors? */
Damien Miller3db5f532002-02-13 14:10:32 +1100389 if ((a = do_stat(conn, path, 1)) == NULL)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000390 return(0);
391 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
392 return(0);
393 return(a->perm & S_IFDIR);
394}
395
Ben Lindstrombba81212001-06-25 05:01:22 +0000396static int
Damien Miller3db5f532002-02-13 14:10:32 +1100397process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000398{
399 char *abs_src = NULL;
400 char *abs_dst = NULL;
401 char *tmp;
402 glob_t g;
403 int err = 0;
404 int i;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000405
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000406 abs_src = xstrdup(src);
407 abs_src = make_absolute(abs_src, pwd);
408
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000409 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000410 debug3("Looking up %s", abs_src);
Damien Miller3db5f532002-02-13 14:10:32 +1100411 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000412 error("File \"%s\" not found.", abs_src);
413 err = -1;
414 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100415 }
416
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000417 /* Only one match, dst may be file, directory or unspecified */
418 if (g.gl_pathv[0] && g.gl_matchc == 1) {
419 if (dst) {
420 /* If directory specified, append filename */
421 if (is_dir(dst)) {
422 if (infer_path(g.gl_pathv[0], &tmp)) {
423 err = 1;
424 goto out;
425 }
426 abs_dst = path_append(dst, tmp);
427 xfree(tmp);
428 } else
429 abs_dst = xstrdup(dst);
430 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
431 err = -1;
432 goto out;
433 }
Damien Miller3db5f532002-02-13 14:10:32 +1100434 err = do_download(conn, g.gl_pathv[0], abs_dst, pflag);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000435 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100436 }
437
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000438 /* Multiple matches, dst may be directory or unspecified */
439 if (dst && !is_dir(dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000440 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000441 dst);
442 err = -1;
443 goto out;
444 }
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000445
Damien Miller9f0f5c62001-12-21 14:45:46 +1100446 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000447 if (infer_path(g.gl_pathv[i], &tmp)) {
448 err = -1;
449 goto out;
450 }
451 if (dst) {
452 abs_dst = path_append(dst, tmp);
453 xfree(tmp);
454 } else
455 abs_dst = tmp;
456
457 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100458 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000459 err = -1;
460 xfree(abs_dst);
461 abs_dst = NULL;
462 }
463
464out:
465 xfree(abs_src);
466 if (abs_dst)
467 xfree(abs_dst);
468 globfree(&g);
469 return(err);
470}
471
Ben Lindstrombba81212001-06-25 05:01:22 +0000472static int
Damien Miller3db5f532002-02-13 14:10:32 +1100473process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000474{
475 char *tmp_dst = NULL;
476 char *abs_dst = NULL;
477 char *tmp;
478 glob_t g;
479 int err = 0;
480 int i;
481
482 if (dst) {
483 tmp_dst = xstrdup(dst);
484 tmp_dst = make_absolute(tmp_dst, pwd);
485 }
486
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000487 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000488 debug3("Looking up %s", src);
489 if (glob(src, 0, NULL, &g)) {
490 error("File \"%s\" not found.", src);
491 err = -1;
492 goto out;
493 }
494
495 /* Only one match, dst may be file, directory or unspecified */
496 if (g.gl_pathv[0] && g.gl_matchc == 1) {
497 if (tmp_dst) {
498 /* If directory specified, append filename */
Damien Miller3db5f532002-02-13 14:10:32 +1100499 if (remote_is_dir(conn, tmp_dst)) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000500 if (infer_path(g.gl_pathv[0], &tmp)) {
501 err = 1;
502 goto out;
503 }
504 abs_dst = path_append(tmp_dst, tmp);
505 xfree(tmp);
506 } else
507 abs_dst = xstrdup(tmp_dst);
Ben Lindstrom7527f8b2001-03-24 00:39:12 +0000508 } else {
509 if (infer_path(g.gl_pathv[0], &abs_dst)) {
510 err = -1;
511 goto out;
512 }
513 abs_dst = make_absolute(abs_dst, pwd);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000514 }
Damien Miller3db5f532002-02-13 14:10:32 +1100515 err = do_upload(conn, g.gl_pathv[0], abs_dst, pflag);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000516 goto out;
517 }
518
519 /* Multiple matches, dst may be directory or unspecified */
Damien Miller3db5f532002-02-13 14:10:32 +1100520 if (tmp_dst && !remote_is_dir(conn, tmp_dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000521 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000522 tmp_dst);
523 err = -1;
524 goto out;
525 }
526
Damien Miller9f0f5c62001-12-21 14:45:46 +1100527 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000528 if (infer_path(g.gl_pathv[i], &tmp)) {
529 err = -1;
530 goto out;
531 }
532 if (tmp_dst) {
533 abs_dst = path_append(tmp_dst, tmp);
534 xfree(tmp);
535 } else
536 abs_dst = make_absolute(tmp, pwd);
537
538 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100539 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000540 err = -1;
541 }
542
543out:
544 if (abs_dst)
545 xfree(abs_dst);
546 if (tmp_dst)
547 xfree(tmp_dst);
548 return(err);
Damien Miller33804262001-02-04 23:20:18 +1100549}
550
Ben Lindstrombba81212001-06-25 05:01:22 +0000551static int
Damien Millere1a49812002-09-12 09:54:25 +1000552sdirent_comp(const void *aa, const void *bb)
553{
554 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
555 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
556
Ben Lindstrom93576d92002-12-23 02:06:19 +0000557 return (strcmp(a->filename, b->filename));
Damien Millere1a49812002-09-12 09:54:25 +1000558}
559
560/* sftp ls.1 replacement for directories */
561static int
562do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
563{
564 int n;
565 SFTP_DIRENT **d;
566
567 if ((n = do_readdir(conn, path, &d)) != 0)
568 return (n);
569
Ben Lindstrom93576d92002-12-23 02:06:19 +0000570 /* Count entries for sort */
Damien Millere1a49812002-09-12 09:54:25 +1000571 for (n = 0; d[n] != NULL; n++)
572 ;
573
574 qsort(d, n, sizeof(*d), sdirent_comp);
575
576 for (n = 0; d[n] != NULL; n++) {
577 char *tmp, *fname;
Ben Lindstrom93576d92002-12-23 02:06:19 +0000578
Damien Millere1a49812002-09-12 09:54:25 +1000579 tmp = path_append(path, d[n]->filename);
580 fname = path_strip(tmp, strip_path);
581 xfree(tmp);
582
583 if (lflag) {
584 char *lname;
585 struct stat sb;
586
587 memset(&sb, 0, sizeof(sb));
588 attrib_to_stat(&d[n]->a, &sb);
589 lname = ls_file(fname, &sb, 1);
590 printf("%s\n", lname);
591 xfree(lname);
592 } else {
593 /* XXX - multicolumn display would be nice here */
594 printf("%s\n", fname);
595 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000596
Damien Millere1a49812002-09-12 09:54:25 +1000597 xfree(fname);
598 }
599
600 free_sftp_dirents(d);
601 return (0);
602}
603
604/* sftp ls.1 replacement which handles path globs */
605static int
Ben Lindstrom93576d92002-12-23 02:06:19 +0000606do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
Damien Millere1a49812002-09-12 09:54:25 +1000607 int lflag)
608{
609 glob_t g;
610 int i;
611 Attrib *a;
612 struct stat sb;
613
614 memset(&g, 0, sizeof(g));
615
Ben Lindstrom93576d92002-12-23 02:06:19 +0000616 if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
Damien Millere1a49812002-09-12 09:54:25 +1000617 NULL, &g)) {
618 error("Can't ls: \"%s\" not found", path);
619 return (-1);
620 }
621
622 /*
Ben Lindstrom93576d92002-12-23 02:06:19 +0000623 * If the glob returns a single match, which is the same as the
Damien Millere1a49812002-09-12 09:54:25 +1000624 * input glob, and it is a directory, then just list its contents
625 */
Ben Lindstrom93576d92002-12-23 02:06:19 +0000626 if (g.gl_pathc == 1 &&
Damien Millere1a49812002-09-12 09:54:25 +1000627 strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
628 if ((a = do_lstat(conn, path, 1)) == NULL) {
629 globfree(&g);
630 return (-1);
631 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000632 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Millere1a49812002-09-12 09:54:25 +1000633 S_ISDIR(a->perm)) {
634 globfree(&g);
635 return (do_ls_dir(conn, path, strip_path, lflag));
636 }
637 }
638
639 for (i = 0; g.gl_pathv[i]; i++) {
640 char *fname, *lname;
641
642 fname = path_strip(g.gl_pathv[i], strip_path);
643
644 if (lflag) {
645 /*
646 * XXX: this is slow - 1 roundtrip per path
Ben Lindstrom93576d92002-12-23 02:06:19 +0000647 * A solution to this is to fork glob() and
648 * build a sftp specific version which keeps the
Damien Millere1a49812002-09-12 09:54:25 +1000649 * attribs (which currently get thrown away)
650 * that the server returns as well as the filenames.
651 */
652 memset(&sb, 0, sizeof(sb));
653 a = do_lstat(conn, g.gl_pathv[i], 1);
654 if (a != NULL)
655 attrib_to_stat(a, &sb);
656 lname = ls_file(fname, &sb, 1);
657 printf("%s\n", lname);
658 xfree(lname);
659 } else {
660 /* XXX - multicolumn display would be nice here */
661 printf("%s\n", fname);
662 }
663 xfree(fname);
664 }
665
666 if (g.gl_pathc)
667 globfree(&g);
668
669 return (0);
670}
671
672static int
Damien Miller956f3fb2003-01-10 21:40:00 +1100673parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
Damien Millere1a49812002-09-12 09:54:25 +1000674 unsigned long *n_arg, char **path1, char **path2)
Damien Miller33804262001-02-04 23:20:18 +1100675{
676 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000677 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000678 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000679 long l;
Damien Miller33804262001-02-04 23:20:18 +1100680 int i, cmdnum;
681
682 /* Skip leading whitespace */
683 cp = cp + strspn(cp, WHITESPACE);
684
Damien Miller956f3fb2003-01-10 21:40:00 +1100685 /* Ignore blank lines and lines which begin with comment '#' char */
686 if (*cp == '\0' || *cp == '#')
687 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100688
Damien Miller956f3fb2003-01-10 21:40:00 +1100689 /* Check for leading '-' (disable error processing) */
690 *iflag = 0;
691 if (*cp == '-') {
692 *iflag = 1;
693 cp++;
694 }
695
Damien Miller33804262001-02-04 23:20:18 +1100696 /* Figure out which command we have */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100697 for (i = 0; cmds[i].c; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100698 int cmdlen = strlen(cmds[i].c);
699
700 /* Check for command followed by whitespace */
701 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
702 strchr(WHITESPACE, cp[cmdlen])) {
703 cp += cmdlen;
704 cp = cp + strspn(cp, WHITESPACE);
705 break;
706 }
707 }
708 cmdnum = cmds[i].n;
709 cmd = cmds[i].c;
710
711 /* Special case */
712 if (*cp == '!') {
713 cp++;
714 cmdnum = I_SHELL;
715 } else if (cmdnum == -1) {
716 error("Invalid command.");
Damien Miller956f3fb2003-01-10 21:40:00 +1100717 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100718 }
719
720 /* Get arguments and parse flags */
Damien Millere1a49812002-09-12 09:54:25 +1000721 *lflag = *pflag = *n_arg = 0;
Damien Miller33804262001-02-04 23:20:18 +1100722 *path1 = *path2 = NULL;
723 switch (cmdnum) {
724 case I_GET:
725 case I_PUT:
726 if (parse_getput_flags(&cp, pflag))
727 return(-1);
728 /* Get first pathname (mandatory) */
729 if (get_pathname(&cp, path1))
730 return(-1);
731 if (*path1 == NULL) {
732 error("You must specify at least one path after a "
733 "%s command.", cmd);
734 return(-1);
735 }
736 /* Try to get second pathname (optional) */
737 if (get_pathname(&cp, path2))
738 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100739 break;
740 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100741 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100742 if (get_pathname(&cp, path1))
743 return(-1);
744 if (get_pathname(&cp, path2))
745 return(-1);
746 if (!*path1 || !*path2) {
747 error("You must specify two paths after a %s "
748 "command.", cmd);
749 return(-1);
750 }
751 break;
752 case I_RM:
753 case I_MKDIR:
754 case I_RMDIR:
755 case I_CHDIR:
756 case I_LCHDIR:
757 case I_LMKDIR:
758 /* Get pathname (mandatory) */
759 if (get_pathname(&cp, path1))
760 return(-1);
761 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000762 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100763 cmd);
764 return(-1);
765 }
766 break;
767 case I_LS:
Damien Millere1a49812002-09-12 09:54:25 +1000768 if (parse_ls_flags(&cp, lflag))
769 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100770 /* Path is optional */
771 if (get_pathname(&cp, path1))
772 return(-1);
773 break;
774 case I_LLS:
775 case I_SHELL:
776 /* Uses the rest of the line */
777 break;
778 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000779 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100780 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000781 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100782 case I_CHOWN:
783 case I_CHGRP:
784 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000785 l = strtol(cp, &cp2, base);
786 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
787 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100788 error("You must supply a numeric argument "
789 "to the %s command.", cmd);
790 return(-1);
791 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000792 cp = cp2;
793 *n_arg = l;
794 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
795 break;
796 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100797 error("You must supply a numeric argument "
798 "to the %s command.", cmd);
799 return(-1);
800 }
801 cp += strspn(cp, WHITESPACE);
802
803 /* Get pathname (mandatory) */
804 if (get_pathname(&cp, path1))
805 return(-1);
806 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000807 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100808 cmd);
809 return(-1);
810 }
811 break;
812 case I_QUIT:
813 case I_PWD:
814 case I_LPWD:
815 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000816 case I_VERSION:
Damien Miller62d57f62003-01-10 21:43:24 +1100817 case I_PROGRESS:
Damien Miller33804262001-02-04 23:20:18 +1100818 break;
819 default:
820 fatal("Command not implemented");
821 }
822
823 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100824 return(cmdnum);
825}
826
Ben Lindstrombba81212001-06-25 05:01:22 +0000827static int
Damien Miller956f3fb2003-01-10 21:40:00 +1100828parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
829 int err_abort)
Damien Miller33804262001-02-04 23:20:18 +1100830{
Damien Millerd7686fd2001-02-10 00:40:03 +1100831 char *path1, *path2, *tmp;
Damien Miller956f3fb2003-01-10 21:40:00 +1100832 int pflag, lflag, iflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100833 unsigned long n_arg;
834 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000835 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000836 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100837 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100838
839 path1 = path2 = NULL;
Damien Miller956f3fb2003-01-10 21:40:00 +1100840 cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
Damien Millere1a49812002-09-12 09:54:25 +1000841 &path1, &path2);
Damien Miller33804262001-02-04 23:20:18 +1100842
Damien Miller956f3fb2003-01-10 21:40:00 +1100843 if (iflag != 0)
844 err_abort = 0;
845
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000846 memset(&g, 0, sizeof(g));
847
Damien Miller33804262001-02-04 23:20:18 +1100848 /* Perform command */
849 switch (cmdnum) {
Damien Miller956f3fb2003-01-10 21:40:00 +1100850 case 0:
851 /* Blank line */
852 break;
Damien Miller33804262001-02-04 23:20:18 +1100853 case -1:
Damien Miller956f3fb2003-01-10 21:40:00 +1100854 /* Unrecognized command */
855 err = -1;
Damien Miller33804262001-02-04 23:20:18 +1100856 break;
857 case I_GET:
Damien Miller3db5f532002-02-13 14:10:32 +1100858 err = process_get(conn, path1, path2, *pwd, pflag);
Damien Miller33804262001-02-04 23:20:18 +1100859 break;
860 case I_PUT:
Damien Miller3db5f532002-02-13 14:10:32 +1100861 err = process_put(conn, path1, path2, *pwd, pflag);
Ben Lindstroma3700052001-04-05 23:26:32 +0000862 break;
863 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100864 path1 = make_absolute(path1, *pwd);
865 path2 = make_absolute(path2, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100866 err = do_rename(conn, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100867 break;
Damien Miller058316f2001-03-08 10:08:49 +1100868 case I_SYMLINK:
Damien Miller3db5f532002-02-13 14:10:32 +1100869 path2 = make_absolute(path2, *pwd);
870 err = do_symlink(conn, path1, path2);
Damien Miller058316f2001-03-08 10:08:49 +1100871 break;
Damien Miller33804262001-02-04 23:20:18 +1100872 case I_RM:
873 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100874 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100875 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100876 printf("Removing %s\n", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +1100877 err = do_rm(conn, g.gl_pathv[i]);
878 if (err != 0 && err_abort)
879 break;
Damien Miller4870afd2001-03-14 10:27:09 +1100880 }
Damien Miller33804262001-02-04 23:20:18 +1100881 break;
882 case I_MKDIR:
883 path1 = make_absolute(path1, *pwd);
884 attrib_clear(&a);
885 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
886 a.perm = 0777;
Damien Miller3db5f532002-02-13 14:10:32 +1100887 err = do_mkdir(conn, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100888 break;
889 case I_RMDIR:
890 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100891 err = do_rmdir(conn, path1);
Damien Miller33804262001-02-04 23:20:18 +1100892 break;
893 case I_CHDIR:
894 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100895 if ((tmp = do_realpath(conn, path1)) == NULL) {
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000896 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100897 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000898 }
Damien Miller3db5f532002-02-13 14:10:32 +1100899 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100900 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000901 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100902 break;
903 }
904 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
905 error("Can't change directory: Can't check target");
906 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000907 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100908 break;
909 }
910 if (!S_ISDIR(aa->perm)) {
911 error("Can't change directory: \"%s\" is not "
912 "a directory", tmp);
913 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000914 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100915 break;
916 }
Damien Miller33804262001-02-04 23:20:18 +1100917 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100918 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100919 break;
920 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100921 if (!path1) {
Damien Millere1a49812002-09-12 09:54:25 +1000922 do_globbed_ls(conn, *pwd, *pwd, lflag);
Damien Millerd7686fd2001-02-10 00:40:03 +1100923 break;
924 }
Ben Lindstrom93576d92002-12-23 02:06:19 +0000925
Damien Millere1a49812002-09-12 09:54:25 +1000926 /* Strip pwd off beginning of non-absolute paths */
927 tmp = NULL;
928 if (*path1 != '/')
929 tmp = *pwd;
930
Damien Miller33804262001-02-04 23:20:18 +1100931 path1 = make_absolute(path1, *pwd);
Damien Miller956f3fb2003-01-10 21:40:00 +1100932 err = do_globbed_ls(conn, path1, tmp, lflag);
Damien Miller33804262001-02-04 23:20:18 +1100933 break;
934 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000935 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100936 error("Couldn't change local directory to "
937 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000938 err = 1;
939 }
Damien Miller33804262001-02-04 23:20:18 +1100940 break;
941 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000942 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100943 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100944 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000945 err = 1;
946 }
Damien Miller33804262001-02-04 23:20:18 +1100947 break;
948 case I_LLS:
949 local_do_ls(cmd);
950 break;
951 case I_SHELL:
952 local_do_shell(cmd);
953 break;
954 case I_LUMASK:
955 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +0000956 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +1100957 break;
958 case I_CHMOD:
959 path1 = make_absolute(path1, *pwd);
960 attrib_clear(&a);
961 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
962 a.perm = n_arg;
Damien Miller3db5f532002-02-13 14:10:32 +1100963 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100964 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100965 printf("Changing mode on %s\n", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +1100966 err = do_setstat(conn, g.gl_pathv[i], &a);
967 if (err != 0 && err_abort)
968 break;
Damien Miller4870afd2001-03-14 10:27:09 +1100969 }
Kevin Steves62c45db2001-02-05 13:42:43 +0000970 break;
Damien Miller33804262001-02-04 23:20:18 +1100971 case I_CHOWN:
Damien Miller33804262001-02-04 23:20:18 +1100972 case I_CHGRP:
973 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100974 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100975 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller956f3fb2003-01-10 21:40:00 +1100976 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
977 if (err != 0 && err_abort)
978 break;
979 else
980 continue;
981 }
Damien Miller4870afd2001-03-14 10:27:09 +1100982 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
983 error("Can't get current ownership of "
984 "remote file \"%s\"", g.gl_pathv[i]);
Damien Miller956f3fb2003-01-10 21:40:00 +1100985 if (err != 0 && err_abort)
986 break;
987 else
988 continue;
Damien Miller4870afd2001-03-14 10:27:09 +1100989 }
Damien Miller4870afd2001-03-14 10:27:09 +1100990 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
Damien Miller956f3fb2003-01-10 21:40:00 +1100991 if (cmdnum == I_CHOWN) {
992 printf("Changing owner on %s\n", g.gl_pathv[i]);
993 aa->uid = n_arg;
994 } else {
995 printf("Changing group on %s\n", g.gl_pathv[i]);
996 aa->gid = n_arg;
997 }
998 err = do_setstat(conn, g.gl_pathv[i], aa);
999 if (err != 0 && err_abort)
1000 break;
Damien Miller33804262001-02-04 23:20:18 +11001001 }
Damien Miller33804262001-02-04 23:20:18 +11001002 break;
1003 case I_PWD:
1004 printf("Remote working directory: %s\n", *pwd);
1005 break;
1006 case I_LPWD:
Damien Miller956f3fb2003-01-10 21:40:00 +11001007 if (!getcwd(path_buf, sizeof(path_buf))) {
1008 error("Couldn't get local cwd: %s", strerror(errno));
1009 err = -1;
1010 break;
1011 }
1012 printf("Local working directory: %s\n", path_buf);
Damien Miller33804262001-02-04 23:20:18 +11001013 break;
1014 case I_QUIT:
Damien Miller956f3fb2003-01-10 21:40:00 +11001015 /* Processed below */
1016 break;
Damien Miller33804262001-02-04 23:20:18 +11001017 case I_HELP:
1018 help();
1019 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +00001020 case I_VERSION:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001021 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
Ben Lindstromf78682d2001-03-14 21:26:27 +00001022 break;
Damien Miller62d57f62003-01-10 21:43:24 +11001023 case I_PROGRESS:
1024 showprogress = !showprogress;
1025 if (showprogress)
1026 printf("Progress meter enabled\n");
1027 else
1028 printf("Progress meter disabled\n");
1029 break;
Damien Miller33804262001-02-04 23:20:18 +11001030 default:
1031 fatal("%d is not implemented", cmdnum);
1032 }
1033
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001034 if (g.gl_pathc)
1035 globfree(&g);
Damien Miller33804262001-02-04 23:20:18 +11001036 if (path1)
1037 xfree(path1);
1038 if (path2)
1039 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001040
Damien Miller956f3fb2003-01-10 21:40:00 +11001041 /* If an unignored error occurs in batch mode we should abort. */
1042 if (err_abort && err != 0)
1043 return (-1);
1044 else if (cmdnum == I_QUIT)
1045 return (1);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001046
Damien Miller956f3fb2003-01-10 21:40:00 +11001047 return (0);
Damien Miller33804262001-02-04 23:20:18 +11001048}
1049
Damien Miller956f3fb2003-01-10 21:40:00 +11001050int
Ben Lindstrom63667f62001-04-13 00:00:14 +00001051interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
Damien Miller33804262001-02-04 23:20:18 +11001052{
1053 char *pwd;
Ben Lindstrom63667f62001-04-13 00:00:14 +00001054 char *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +11001055 char cmd[2048];
Damien Miller3db5f532002-02-13 14:10:32 +11001056 struct sftp_conn *conn;
Damien Miller956f3fb2003-01-10 21:40:00 +11001057 int err;
Damien Miller33804262001-02-04 23:20:18 +11001058
Damien Miller3db5f532002-02-13 14:10:32 +11001059 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1060 if (conn == NULL)
Damien Miller058316f2001-03-08 10:08:49 +11001061 fatal("Couldn't initialise connection to server");
1062
Damien Miller3db5f532002-02-13 14:10:32 +11001063 pwd = do_realpath(conn, ".");
Damien Miller33804262001-02-04 23:20:18 +11001064 if (pwd == NULL)
1065 fatal("Need cwd");
1066
Ben Lindstrom63667f62001-04-13 00:00:14 +00001067 if (file1 != NULL) {
1068 dir = xstrdup(file1);
1069 dir = make_absolute(dir, pwd);
1070
Damien Miller3db5f532002-02-13 14:10:32 +11001071 if (remote_is_dir(conn, dir) && file2 == NULL) {
Ben Lindstrom63667f62001-04-13 00:00:14 +00001072 printf("Changing to: %s\n", dir);
1073 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
Damien Miller956f3fb2003-01-10 21:40:00 +11001074 if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0)
1075 return (-1);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001076 } else {
1077 if (file2 == NULL)
1078 snprintf(cmd, sizeof cmd, "get %s", dir);
1079 else
1080 snprintf(cmd, sizeof cmd, "get %s %s", dir,
1081 file2);
1082
Damien Miller956f3fb2003-01-10 21:40:00 +11001083 err = parse_dispatch_command(conn, cmd, &pwd, 1);
Ben Lindstromcb1f60e2002-03-22 02:47:28 +00001084 xfree(dir);
Damien Miller956f3fb2003-01-10 21:40:00 +11001085 return (err);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001086 }
Ben Lindstromcb1f60e2002-03-22 02:47:28 +00001087 xfree(dir);
Ben Lindstrom63667f62001-04-13 00:00:14 +00001088 }
Damien Miller956f3fb2003-01-10 21:40:00 +11001089
Ben Lindstrom6aebb342001-05-09 00:38:19 +00001090#if HAVE_SETVBUF
Damien Millerd7686fd2001-02-10 00:40:03 +11001091 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001092 setvbuf(infile, NULL, _IOLBF, 0);
Ben Lindstrom6aebb342001-05-09 00:38:19 +00001093#else
1094 setlinebuf(stdout);
1095 setlinebuf(infile);
1096#endif
Damien Miller33804262001-02-04 23:20:18 +11001097
Damien Miller956f3fb2003-01-10 21:40:00 +11001098 err = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001099 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001100 char *cp;
1101
1102 printf("sftp> ");
1103
1104 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001105 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +11001106 printf("\n");
1107 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001108 } else if (infile != stdin) /* Bluff typing */
1109 printf("%s", cmd);
1110
Damien Miller33804262001-02-04 23:20:18 +11001111 cp = strrchr(cmd, '\n');
1112 if (cp)
1113 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001114
Damien Miller956f3fb2003-01-10 21:40:00 +11001115 err = parse_dispatch_command(conn, cmd, &pwd, infile != stdin);
1116 if (err != 0)
Damien Miller33804262001-02-04 23:20:18 +11001117 break;
1118 }
1119 xfree(pwd);
Damien Miller956f3fb2003-01-10 21:40:00 +11001120
1121 /* err == 1 signifies normal "quit" exit */
1122 return (err >= 0 ? 0 : -1);
Damien Miller33804262001-02-04 23:20:18 +11001123}
Damien Miller956f3fb2003-01-10 21:40:00 +11001124