blob: d350e398deb081da9b20cb7648115e861261fbc5 [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
2 * Copyright (c) 2001 Damien Miller. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
Damien Millerd7686fd2001-02-10 00:40:03 +110025/* XXX: globbed ls */
Damien Miller33804262001-02-04 23:20:18 +110026/* XXX: recursive operations */
27
28#include "includes.h"
Damien Miller4870afd2001-03-14 10:27:09 +110029RCSID("$OpenBSD: sftp-int.c,v 1.27 2001/03/13 22:42:54 djm Exp $");
30
31#include <glob.h>
Damien Miller33804262001-02-04 23:20:18 +110032
33#include "buffer.h"
34#include "xmalloc.h"
35#include "log.h"
36#include "pathnames.h"
37
38#include "sftp.h"
39#include "sftp-common.h"
Damien Miller4870afd2001-03-14 10:27:09 +110040#include "sftp-glob.h"
Damien Miller33804262001-02-04 23:20:18 +110041#include "sftp-client.h"
42#include "sftp-int.h"
43
Damien Miller058316f2001-03-08 10:08:49 +110044/* File to read commands from */
45extern FILE *infile;
46
47/* Version of server we are speaking to */
48int version;
Ben Lindstrom562c26b2001-03-07 01:26:48 +000049
Damien Miller33804262001-02-04 23:20:18 +110050/* Seperators for interactive commands */
51#define WHITESPACE " \t\r\n"
52
53/* Commands for interactive mode */
54#define I_CHDIR 1
55#define I_CHGRP 2
56#define I_CHMOD 3
57#define I_CHOWN 4
58#define I_GET 5
59#define I_HELP 6
60#define I_LCHDIR 7
61#define I_LLS 8
62#define I_LMKDIR 9
63#define I_LPWD 10
64#define I_LS 11
65#define I_LUMASK 12
66#define I_MKDIR 13
67#define I_PUT 14
68#define I_PWD 15
69#define I_QUIT 16
70#define I_RENAME 17
71#define I_RM 18
72#define I_RMDIR 19
73#define I_SHELL 20
Damien Miller058316f2001-03-08 10:08:49 +110074#define I_SYMLINK 21
Damien Miller33804262001-02-04 23:20:18 +110075
76struct CMD {
Damien Miller33804262001-02-04 23:20:18 +110077 const char *c;
Kevin Steves62c45db2001-02-05 13:42:43 +000078 const int n;
Damien Miller33804262001-02-04 23:20:18 +110079};
80
81const struct CMD cmds[] = {
Damien Millerd7686fd2001-02-10 00:40:03 +110082 { "cd", I_CHDIR },
83 { "chdir", I_CHDIR },
84 { "chgrp", I_CHGRP },
85 { "chmod", I_CHMOD },
86 { "chown", I_CHOWN },
87 { "dir", I_LS },
88 { "exit", I_QUIT },
89 { "get", I_GET },
90 { "help", I_HELP },
91 { "lcd", I_LCHDIR },
92 { "lchdir", I_LCHDIR },
93 { "lls", I_LLS },
94 { "lmkdir", I_LMKDIR },
Damien Miller058316f2001-03-08 10:08:49 +110095 { "ln", I_SYMLINK },
Damien Millerd7686fd2001-02-10 00:40:03 +110096 { "lpwd", I_LPWD },
97 { "ls", I_LS },
98 { "lumask", I_LUMASK },
99 { "mkdir", I_MKDIR },
100 { "put", I_PUT },
101 { "pwd", I_PWD },
102 { "quit", I_QUIT },
103 { "rename", I_RENAME },
104 { "rm", I_RM },
105 { "rmdir", I_RMDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100106 { "symlink", I_SYMLINK },
Kevin Steves62c45db2001-02-05 13:42:43 +0000107 { "!", I_SHELL },
108 { "?", I_HELP },
109 { NULL, -1}
Damien Miller33804262001-02-04 23:20:18 +1100110};
111
112void
113help(void)
114{
115 printf("Available commands:\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100116 printf("cd path Change remote directory to 'path'\n");
117 printf("lcd path Change local directory to 'path'\n");
118 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
119 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
120 printf("chown own path Change owner of file 'path' to 'own'\n");
121 printf("help Display this help text\n");
122 printf("get remote-path [local-path] Download file\n");
123 printf("lls [ls-options [path]] Display local directory listing\n");
Damien Miller058316f2001-03-08 10:08:49 +1100124 printf("ln oldpath newpath Symlink remote file\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100125 printf("lmkdir path Create local directory\n");
126 printf("lpwd Print local working directory\n");
127 printf("ls [path] Display remote directory listing\n");
128 printf("lumask umask Set local umask to 'umask'\n");
129 printf("mkdir path Create remote directory\n");
130 printf("put local-path [remote-path] Upload file\n");
131 printf("pwd Display remote working directory\n");
132 printf("exit Quit sftp\n");
133 printf("quit Quit sftp\n");
134 printf("rename oldpath newpath Rename remote file\n");
135 printf("rmdir path Remove remote directory\n");
136 printf("rm path Delete remote file\n");
Damien Miller058316f2001-03-08 10:08:49 +1100137 printf("symlink oldpath newpath Symlink remote file\n");
Damien Miller33804262001-02-04 23:20:18 +1100138 printf("!command Execute 'command' in local shell\n");
139 printf("! Escape to local shell\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100140 printf("? Synonym for help\n");
Damien Miller33804262001-02-04 23:20:18 +1100141}
142
143void
144local_do_shell(const char *args)
145{
146 int ret, status;
147 char *shell;
148 pid_t pid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000149
Damien Miller33804262001-02-04 23:20:18 +1100150 if (!*args)
151 args = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000152
Damien Miller33804262001-02-04 23:20:18 +1100153 if ((shell = getenv("SHELL")) == NULL)
154 shell = _PATH_BSHELL;
155
156 if ((pid = fork()) == -1)
157 fatal("Couldn't fork: %s", strerror(errno));
158
159 if (pid == 0) {
160 /* XXX: child has pipe fds to ssh subproc open - issue? */
161 if (args) {
162 debug3("Executing %s -c \"%s\"", shell, args);
163 ret = execl(shell, shell, "-c", args, NULL);
164 } else {
165 debug3("Executing %s", shell);
166 ret = execl(shell, shell, NULL);
167 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000168 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
Damien Miller33804262001-02-04 23:20:18 +1100169 strerror(errno));
170 _exit(1);
171 }
172 if (waitpid(pid, &status, 0) == -1)
173 fatal("Couldn't wait for child: %s", strerror(errno));
174 if (!WIFEXITED(status))
175 error("Shell exited abormally");
176 else if (WEXITSTATUS(status))
177 error("Shell exited with status %d", WEXITSTATUS(status));
178}
179
Kevin Stevesef4eea92001-02-05 12:42:17 +0000180void
Damien Miller33804262001-02-04 23:20:18 +1100181local_do_ls(const char *args)
182{
183 if (!args || !*args)
Damien Millerd7686fd2001-02-10 00:40:03 +1100184 local_do_shell(_PATH_LS);
Damien Miller33804262001-02-04 23:20:18 +1100185 else {
Damien Millerd7686fd2001-02-10 00:40:03 +1100186 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
187 char *buf = xmalloc(len);
Damien Miller33804262001-02-04 23:20:18 +1100188
189 /* XXX: quoting - rip quoting code from ftp? */
Damien Millerd7686fd2001-02-10 00:40:03 +1100190 snprintf(buf, len, _PATH_LS " %s", args);
Damien Miller33804262001-02-04 23:20:18 +1100191 local_do_shell(buf);
Damien Millerd7686fd2001-02-10 00:40:03 +1100192 xfree(buf);
Damien Miller33804262001-02-04 23:20:18 +1100193 }
194}
195
196char *
197make_absolute(char *p, char *pwd)
198{
199 char buf[2048];
200
201 /* Derelativise */
202 if (p && p[0] != '/') {
203 snprintf(buf, sizeof(buf), "%s/%s", pwd, p);
204 xfree(p);
205 p = xstrdup(buf);
206 }
207
208 return(p);
209}
210
211int
212parse_getput_flags(const char **cpp, int *pflag)
213{
214 const char *cp = *cpp;
215
216 /* Check for flags */
217 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100218 switch (cp[1]) {
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000219 case 'p':
Damien Miller33804262001-02-04 23:20:18 +1100220 case 'P':
221 *pflag = 1;
222 break;
223 default:
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000224 error("Invalid flag -%c", cp[1]);
Damien Miller33804262001-02-04 23:20:18 +1100225 return(-1);
226 }
227 cp += 2;
228 *cpp = cp + strspn(cp, WHITESPACE);
229 }
230
231 return(0);
232}
233
234int
235get_pathname(const char **cpp, char **path)
236{
Damien Millerd7686fd2001-02-10 00:40:03 +1100237 const char *cp = *cpp, *end;
238 char quot;
Damien Miller33804262001-02-04 23:20:18 +1100239 int i;
240
241 cp += strspn(cp, WHITESPACE);
242 if (!*cp) {
243 *cpp = cp;
244 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100245 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100246 }
247
248 /* Check for quoted filenames */
249 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100250 quot = *cp++;
251
252 end = strchr(cp, quot);
253 if (end == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100254 error("Unterminated quote");
Damien Millerd7686fd2001-02-10 00:40:03 +1100255 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100256 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100257 if (cp == end) {
Damien Miller33804262001-02-04 23:20:18 +1100258 error("Empty quotes");
Damien Millerd7686fd2001-02-10 00:40:03 +1100259 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100260 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100261 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
262 } else {
263 /* Read to end of filename */
264 end = strpbrk(cp, WHITESPACE);
265 if (end == NULL)
266 end = strchr(cp, '\0');
267 *cpp = end + strspn(end, WHITESPACE);
Damien Miller33804262001-02-04 23:20:18 +1100268 }
269
Damien Millerd7686fd2001-02-10 00:40:03 +1100270 i = end - cp;
Damien Miller33804262001-02-04 23:20:18 +1100271
272 *path = xmalloc(i + 1);
273 memcpy(*path, cp, i);
274 (*path)[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100275 return(0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100276
277 fail:
278 *path = NULL;
279 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100280}
281
282int
283infer_path(const char *p, char **ifp)
284{
285 char *cp;
286
Damien Miller33804262001-02-04 23:20:18 +1100287 cp = strrchr(p, '/');
Damien Miller33804262001-02-04 23:20:18 +1100288 if (cp == NULL) {
289 *ifp = xstrdup(p);
290 return(0);
291 }
292
293 if (!cp[1]) {
294 error("Invalid path");
295 return(-1);
296 }
297
298 *ifp = xstrdup(cp + 1);
299 return(0);
300}
301
302int
303parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
304 char **path1, char **path2)
305{
306 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000307 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000308 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000309 long l;
Damien Miller33804262001-02-04 23:20:18 +1100310 int i, cmdnum;
311
312 /* Skip leading whitespace */
313 cp = cp + strspn(cp, WHITESPACE);
314
315 /* Ignore blank lines */
316 if (!*cp)
317 return(-1);
318
319 /* Figure out which command we have */
320 for(i = 0; cmds[i].c; i++) {
321 int cmdlen = strlen(cmds[i].c);
322
323 /* Check for command followed by whitespace */
324 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
325 strchr(WHITESPACE, cp[cmdlen])) {
326 cp += cmdlen;
327 cp = cp + strspn(cp, WHITESPACE);
328 break;
329 }
330 }
331 cmdnum = cmds[i].n;
332 cmd = cmds[i].c;
333
334 /* Special case */
335 if (*cp == '!') {
336 cp++;
337 cmdnum = I_SHELL;
338 } else if (cmdnum == -1) {
339 error("Invalid command.");
340 return(-1);
341 }
342
343 /* Get arguments and parse flags */
344 *pflag = *n_arg = 0;
345 *path1 = *path2 = NULL;
346 switch (cmdnum) {
347 case I_GET:
348 case I_PUT:
349 if (parse_getput_flags(&cp, pflag))
350 return(-1);
351 /* Get first pathname (mandatory) */
352 if (get_pathname(&cp, path1))
353 return(-1);
354 if (*path1 == NULL) {
355 error("You must specify at least one path after a "
356 "%s command.", cmd);
357 return(-1);
358 }
359 /* Try to get second pathname (optional) */
360 if (get_pathname(&cp, path2))
361 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100362 break;
363 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100364 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100365 if (get_pathname(&cp, path1))
366 return(-1);
367 if (get_pathname(&cp, path2))
368 return(-1);
369 if (!*path1 || !*path2) {
370 error("You must specify two paths after a %s "
371 "command.", cmd);
372 return(-1);
373 }
374 break;
375 case I_RM:
376 case I_MKDIR:
377 case I_RMDIR:
378 case I_CHDIR:
379 case I_LCHDIR:
380 case I_LMKDIR:
381 /* Get pathname (mandatory) */
382 if (get_pathname(&cp, path1))
383 return(-1);
384 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000385 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100386 cmd);
387 return(-1);
388 }
389 break;
390 case I_LS:
391 /* Path is optional */
392 if (get_pathname(&cp, path1))
393 return(-1);
394 break;
395 case I_LLS:
396 case I_SHELL:
397 /* Uses the rest of the line */
398 break;
399 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000400 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100401 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000402 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100403 case I_CHOWN:
404 case I_CHGRP:
405 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000406 l = strtol(cp, &cp2, base);
407 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
408 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100409 error("You must supply a numeric argument "
410 "to the %s command.", cmd);
411 return(-1);
412 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000413 cp = cp2;
414 *n_arg = l;
415 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
416 break;
417 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100418 error("You must supply a numeric argument "
419 "to the %s command.", cmd);
420 return(-1);
421 }
422 cp += strspn(cp, WHITESPACE);
423
424 /* Get pathname (mandatory) */
425 if (get_pathname(&cp, path1))
426 return(-1);
427 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000428 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100429 cmd);
430 return(-1);
431 }
432 break;
433 case I_QUIT:
434 case I_PWD:
435 case I_LPWD:
436 case I_HELP:
437 break;
438 default:
439 fatal("Command not implemented");
440 }
441
442 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100443 return(cmdnum);
444}
445
446int
447parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
448{
Damien Millerd7686fd2001-02-10 00:40:03 +1100449 char *path1, *path2, *tmp;
Damien Miller4870afd2001-03-14 10:27:09 +1100450 int pflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100451 unsigned long n_arg;
452 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000453 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000454 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100455 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100456
457 path1 = path2 = NULL;
458 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
459
460 /* Perform command */
461 switch (cmdnum) {
462 case -1:
463 break;
464 case I_GET:
Damien Miller4870afd2001-03-14 10:27:09 +1100465 memset(&g, 0, sizeof(g));
466 if (!remote_glob(in, out, path1, 0, NULL, &g)) {
467 if (path2) {
468 /* XXX: target should be directory */
469 error("You cannot specify a target when "
470 "downloading multiple files");
471 err = -1;
472 break;
473 }
474 for(i = 0; g.gl_pathv[i]; i++) {
475 if (!infer_path(g.gl_pathv[i], &path2)) {
476 printf("Fetching %s\n", g.gl_pathv[i]);
477 if (do_download(in, out, g.gl_pathv[i],
478 path2, pflag) == -1)
479 err = -1;
480 free(path2);
481 path2 = NULL;
482 } else
483 err = -1;
484 }
485 } else {
486 if (!path2 && infer_path(path1, &path2)) {
487 err = -1;
488 break;
489 }
490 err = do_download(in, out, path1, path2, pflag);
491 }
Damien Miller33804262001-02-04 23:20:18 +1100492 break;
493 case I_PUT:
Damien Miller4870afd2001-03-14 10:27:09 +1100494 if (!glob(path1, 0, NULL, &g)) {
495 if (path2) {
496 error("You cannot specify a target when "
497 "uploading multiple files");
498 err = -1;
499 break;
500 }
501 for(i = 0; g.gl_pathv[i]; i++) {
502 if (!infer_path(g.gl_pathv[i], &path2)) {
503 path2 = make_absolute(path2, *pwd);
504 printf("Uploading %s\n", g.gl_pathv[i]);
505 if (do_upload(in, out, g.gl_pathv[i],
506 path2, pflag) == -1)
507 err = -1;
508 free(path2);
509 path2 = NULL;
510 } else
511 err = -1;
512 }
513 } else {
514 if (!path2 && infer_path(path1, &path2)) {
515 err = -1;
516 break;
517 }
518 err = do_upload(in, out, path1, path2, pflag);
519 }
520 break;
521 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100522 path1 = make_absolute(path1, *pwd);
523 path2 = make_absolute(path2, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000524 err = do_rename(in, out, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100525 break;
Damien Miller058316f2001-03-08 10:08:49 +1100526 case I_SYMLINK:
527 if (version < 3) {
528 error("The server (version %d) does not support "
529 "this operation", version);
530 err = -1;
531 } else {
532 path2 = make_absolute(path2, *pwd);
533 err = do_symlink(in, out, path1, path2);
534 }
535 break;
Damien Miller33804262001-02-04 23:20:18 +1100536 case I_RM:
537 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100538 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
539 for(i = 0; g.gl_pathv[i]; i++) {
540 printf("Removing %s\n", g.gl_pathv[i]);
541 if (do_rm(in, out, g.gl_pathv[i]) == -1)
542 err = -1;
543 }
Damien Miller33804262001-02-04 23:20:18 +1100544 break;
545 case I_MKDIR:
546 path1 = make_absolute(path1, *pwd);
547 attrib_clear(&a);
548 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
549 a.perm = 0777;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000550 err = do_mkdir(in, out, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100551 break;
552 case I_RMDIR:
553 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000554 err = do_rmdir(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100555 break;
556 case I_CHDIR:
557 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000558 if ((tmp = do_realpath(in, out, path1)) == NULL) {
559 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100560 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000561 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100562 if ((aa = do_stat(in, out, tmp)) == NULL) {
563 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000564 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100565 break;
566 }
567 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
568 error("Can't change directory: Can't check target");
569 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000570 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100571 break;
572 }
573 if (!S_ISDIR(aa->perm)) {
574 error("Can't change directory: \"%s\" is not "
575 "a directory", tmp);
576 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000577 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100578 break;
579 }
Damien Miller33804262001-02-04 23:20:18 +1100580 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100581 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100582 break;
583 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100584 if (!path1) {
585 do_ls(in, out, *pwd);
586 break;
587 }
Damien Miller33804262001-02-04 23:20:18 +1100588 path1 = make_absolute(path1, *pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100589 if ((tmp = do_realpath(in, out, path1)) == NULL)
590 break;
591 xfree(path1);
592 path1 = tmp;
593 if ((aa = do_stat(in, out, path1)) == NULL)
594 break;
595 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
596 !S_ISDIR(aa->perm)) {
597 error("Can't ls: \"%s\" is not a directory", path1);
598 break;
599 }
600 do_ls(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100601 break;
602 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000603 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100604 error("Couldn't change local directory to "
605 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000606 err = 1;
607 }
Damien Miller33804262001-02-04 23:20:18 +1100608 break;
609 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000610 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100611 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100612 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000613 err = 1;
614 }
Damien Miller33804262001-02-04 23:20:18 +1100615 break;
616 case I_LLS:
617 local_do_ls(cmd);
618 break;
619 case I_SHELL:
620 local_do_shell(cmd);
621 break;
622 case I_LUMASK:
623 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +0000624 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +1100625 break;
626 case I_CHMOD:
627 path1 = make_absolute(path1, *pwd);
628 attrib_clear(&a);
629 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
630 a.perm = n_arg;
Damien Miller4870afd2001-03-14 10:27:09 +1100631 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
632 for(i = 0; g.gl_pathv[i]; i++) {
633 printf("Changing mode on %s\n", g.gl_pathv[i]);
634 do_setstat(in, out, g.gl_pathv[i], &a);
635 }
Kevin Steves62c45db2001-02-05 13:42:43 +0000636 break;
Damien Miller33804262001-02-04 23:20:18 +1100637 case I_CHOWN:
638 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100639 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
640 for(i = 0; g.gl_pathv[i]; i++) {
641 if (!(aa = do_stat(in, out, g.gl_pathv[i])))
642 continue;
643 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
644 error("Can't get current ownership of "
645 "remote file \"%s\"", g.gl_pathv[i]);
646 continue;
647 }
648 printf("Changing owner on %s\n", g.gl_pathv[i]);
649 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
650 aa->uid = n_arg;
651 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100652 }
Damien Miller33804262001-02-04 23:20:18 +1100653 break;
654 case I_CHGRP:
655 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100656 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
657 for(i = 0; g.gl_pathv[i]; i++) {
658 if (!(aa = do_stat(in, out, g.gl_pathv[i])))
659 continue;
660 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
661 error("Can't get current ownership of "
662 "remote file \"%s\"", g.gl_pathv[i]);
663 continue;
664 }
665 printf("Changing group on %s\n", g.gl_pathv[i]);
666 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
667 aa->gid = n_arg;
668 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100669 }
Damien Miller33804262001-02-04 23:20:18 +1100670 break;
671 case I_PWD:
672 printf("Remote working directory: %s\n", *pwd);
673 break;
674 case I_LPWD:
675 if (!getcwd(path_buf, sizeof(path_buf)))
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000676 error("Couldn't get local cwd: %s",
Damien Miller33804262001-02-04 23:20:18 +1100677 strerror(errno));
678 else
679 printf("Local working directory: %s\n",
680 path_buf);
681 break;
682 case I_QUIT:
683 return(-1);
684 case I_HELP:
685 help();
686 break;
687 default:
688 fatal("%d is not implemented", cmdnum);
689 }
690
691 if (path1)
692 xfree(path1);
693 if (path2)
694 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000695
696 /* If an error occurs in batch mode we should abort. */
697 if (infile != stdin && err > 0)
698 return -1;
699
Damien Miller33804262001-02-04 23:20:18 +1100700 return(0);
701}
702
703void
704interactive_loop(int fd_in, int fd_out)
705{
706 char *pwd;
707 char cmd[2048];
708
Damien Miller058316f2001-03-08 10:08:49 +1100709 version = do_init(fd_in, fd_out);
710 if (version == -1)
711 fatal("Couldn't initialise connection to server");
712
Damien Miller33804262001-02-04 23:20:18 +1100713 pwd = do_realpath(fd_in, fd_out, ".");
714 if (pwd == NULL)
715 fatal("Need cwd");
716
Damien Millerd7686fd2001-02-10 00:40:03 +1100717 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000718 setvbuf(infile, NULL, _IOLBF, 0);
Damien Miller33804262001-02-04 23:20:18 +1100719
720 for(;;) {
721 char *cp;
722
723 printf("sftp> ");
724
725 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000726 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100727 printf("\n");
728 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000729 } else if (infile != stdin) /* Bluff typing */
730 printf("%s", cmd);
731
Damien Miller33804262001-02-04 23:20:18 +1100732 cp = strrchr(cmd, '\n');
733 if (cp)
734 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000735
Damien Miller33804262001-02-04 23:20:18 +1100736 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
737 break;
738 }
739 xfree(pwd);
740}