blob: cf86012eaf3b0e564086588850322a9c861d7e40 [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"
Ben Lindstromf78682d2001-03-14 21:26:27 +000029RCSID("$OpenBSD: sftp-int.c,v 1.28 2001/03/14 15:15:58 markus Exp $");
Damien Miller4870afd2001-03-14 10:27:09 +110030
Damien Miller33804262001-02-04 23:20:18 +110031#include "buffer.h"
32#include "xmalloc.h"
33#include "log.h"
34#include "pathnames.h"
35
36#include "sftp.h"
37#include "sftp-common.h"
Damien Miller4870afd2001-03-14 10:27:09 +110038#include "sftp-glob.h"
Damien Miller33804262001-02-04 23:20:18 +110039#include "sftp-client.h"
40#include "sftp-int.h"
41
Damien Miller058316f2001-03-08 10:08:49 +110042/* File to read commands from */
43extern FILE *infile;
44
45/* Version of server we are speaking to */
46int version;
Ben Lindstrom562c26b2001-03-07 01:26:48 +000047
Damien Miller33804262001-02-04 23:20:18 +110048/* Seperators for interactive commands */
49#define WHITESPACE " \t\r\n"
50
51/* Commands for interactive mode */
52#define I_CHDIR 1
53#define I_CHGRP 2
54#define I_CHMOD 3
55#define I_CHOWN 4
56#define I_GET 5
57#define I_HELP 6
58#define I_LCHDIR 7
59#define I_LLS 8
60#define I_LMKDIR 9
61#define I_LPWD 10
62#define I_LS 11
63#define I_LUMASK 12
64#define I_MKDIR 13
65#define I_PUT 14
66#define I_PWD 15
67#define I_QUIT 16
68#define I_RENAME 17
69#define I_RM 18
70#define I_RMDIR 19
71#define I_SHELL 20
Damien Miller058316f2001-03-08 10:08:49 +110072#define I_SYMLINK 21
Ben Lindstromf78682d2001-03-14 21:26:27 +000073#define I_VERSION 22
Damien Miller33804262001-02-04 23:20:18 +110074
75struct CMD {
Damien Miller33804262001-02-04 23:20:18 +110076 const char *c;
Kevin Steves62c45db2001-02-05 13:42:43 +000077 const int n;
Damien Miller33804262001-02-04 23:20:18 +110078};
79
80const struct CMD cmds[] = {
Damien Millerd7686fd2001-02-10 00:40:03 +110081 { "cd", I_CHDIR },
82 { "chdir", I_CHDIR },
83 { "chgrp", I_CHGRP },
84 { "chmod", I_CHMOD },
85 { "chown", I_CHOWN },
86 { "dir", I_LS },
87 { "exit", I_QUIT },
88 { "get", I_GET },
89 { "help", I_HELP },
90 { "lcd", I_LCHDIR },
91 { "lchdir", I_LCHDIR },
92 { "lls", I_LLS },
93 { "lmkdir", I_LMKDIR },
Damien Miller058316f2001-03-08 10:08:49 +110094 { "ln", I_SYMLINK },
Damien Millerd7686fd2001-02-10 00:40:03 +110095 { "lpwd", I_LPWD },
96 { "ls", I_LS },
97 { "lumask", I_LUMASK },
98 { "mkdir", I_MKDIR },
99 { "put", I_PUT },
100 { "pwd", I_PWD },
101 { "quit", I_QUIT },
102 { "rename", I_RENAME },
103 { "rm", I_RM },
104 { "rmdir", I_RMDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100105 { "symlink", I_SYMLINK },
Ben Lindstromf78682d2001-03-14 21:26:27 +0000106 { "version", I_VERSION },
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");
Ben Lindstromf78682d2001-03-14 21:26:27 +0000138 printf("version Show SFTP version\n");
Damien Miller33804262001-02-04 23:20:18 +1100139 printf("!command Execute 'command' in local shell\n");
140 printf("! Escape to local shell\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100141 printf("? Synonym for help\n");
Damien Miller33804262001-02-04 23:20:18 +1100142}
143
144void
145local_do_shell(const char *args)
146{
147 int ret, status;
148 char *shell;
149 pid_t pid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000150
Damien Miller33804262001-02-04 23:20:18 +1100151 if (!*args)
152 args = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000153
Damien Miller33804262001-02-04 23:20:18 +1100154 if ((shell = getenv("SHELL")) == NULL)
155 shell = _PATH_BSHELL;
156
157 if ((pid = fork()) == -1)
158 fatal("Couldn't fork: %s", strerror(errno));
159
160 if (pid == 0) {
161 /* XXX: child has pipe fds to ssh subproc open - issue? */
162 if (args) {
163 debug3("Executing %s -c \"%s\"", shell, args);
164 ret = execl(shell, shell, "-c", args, NULL);
165 } else {
166 debug3("Executing %s", shell);
167 ret = execl(shell, shell, NULL);
168 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000169 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
Damien Miller33804262001-02-04 23:20:18 +1100170 strerror(errno));
171 _exit(1);
172 }
173 if (waitpid(pid, &status, 0) == -1)
174 fatal("Couldn't wait for child: %s", strerror(errno));
175 if (!WIFEXITED(status))
176 error("Shell exited abormally");
177 else if (WEXITSTATUS(status))
178 error("Shell exited with status %d", WEXITSTATUS(status));
179}
180
Kevin Stevesef4eea92001-02-05 12:42:17 +0000181void
Damien Miller33804262001-02-04 23:20:18 +1100182local_do_ls(const char *args)
183{
184 if (!args || !*args)
Damien Millerd7686fd2001-02-10 00:40:03 +1100185 local_do_shell(_PATH_LS);
Damien Miller33804262001-02-04 23:20:18 +1100186 else {
Damien Millerd7686fd2001-02-10 00:40:03 +1100187 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
188 char *buf = xmalloc(len);
Damien Miller33804262001-02-04 23:20:18 +1100189
190 /* XXX: quoting - rip quoting code from ftp? */
Damien Millerd7686fd2001-02-10 00:40:03 +1100191 snprintf(buf, len, _PATH_LS " %s", args);
Damien Miller33804262001-02-04 23:20:18 +1100192 local_do_shell(buf);
Damien Millerd7686fd2001-02-10 00:40:03 +1100193 xfree(buf);
Damien Miller33804262001-02-04 23:20:18 +1100194 }
195}
196
197char *
198make_absolute(char *p, char *pwd)
199{
200 char buf[2048];
201
202 /* Derelativise */
203 if (p && p[0] != '/') {
204 snprintf(buf, sizeof(buf), "%s/%s", pwd, p);
205 xfree(p);
206 p = xstrdup(buf);
207 }
208
209 return(p);
210}
211
212int
213parse_getput_flags(const char **cpp, int *pflag)
214{
215 const char *cp = *cpp;
216
217 /* Check for flags */
218 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100219 switch (cp[1]) {
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000220 case 'p':
Damien Miller33804262001-02-04 23:20:18 +1100221 case 'P':
222 *pflag = 1;
223 break;
224 default:
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000225 error("Invalid flag -%c", cp[1]);
Damien Miller33804262001-02-04 23:20:18 +1100226 return(-1);
227 }
228 cp += 2;
229 *cpp = cp + strspn(cp, WHITESPACE);
230 }
231
232 return(0);
233}
234
235int
236get_pathname(const char **cpp, char **path)
237{
Damien Millerd7686fd2001-02-10 00:40:03 +1100238 const char *cp = *cpp, *end;
239 char quot;
Damien Miller33804262001-02-04 23:20:18 +1100240 int i;
241
242 cp += strspn(cp, WHITESPACE);
243 if (!*cp) {
244 *cpp = cp;
245 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100246 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100247 }
248
249 /* Check for quoted filenames */
250 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100251 quot = *cp++;
252
253 end = strchr(cp, quot);
254 if (end == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100255 error("Unterminated quote");
Damien Millerd7686fd2001-02-10 00:40:03 +1100256 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100257 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100258 if (cp == end) {
Damien Miller33804262001-02-04 23:20:18 +1100259 error("Empty quotes");
Damien Millerd7686fd2001-02-10 00:40:03 +1100260 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100261 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100262 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
263 } else {
264 /* Read to end of filename */
265 end = strpbrk(cp, WHITESPACE);
266 if (end == NULL)
267 end = strchr(cp, '\0');
268 *cpp = end + strspn(end, WHITESPACE);
Damien Miller33804262001-02-04 23:20:18 +1100269 }
270
Damien Millerd7686fd2001-02-10 00:40:03 +1100271 i = end - cp;
Damien Miller33804262001-02-04 23:20:18 +1100272
273 *path = xmalloc(i + 1);
274 memcpy(*path, cp, i);
275 (*path)[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100276 return(0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100277
278 fail:
279 *path = NULL;
280 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100281}
282
283int
284infer_path(const char *p, char **ifp)
285{
286 char *cp;
287
Damien Miller33804262001-02-04 23:20:18 +1100288 cp = strrchr(p, '/');
Damien Miller33804262001-02-04 23:20:18 +1100289 if (cp == NULL) {
290 *ifp = xstrdup(p);
291 return(0);
292 }
293
294 if (!cp[1]) {
295 error("Invalid path");
296 return(-1);
297 }
298
299 *ifp = xstrdup(cp + 1);
300 return(0);
301}
302
303int
304parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
305 char **path1, char **path2)
306{
307 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000308 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000309 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000310 long l;
Damien Miller33804262001-02-04 23:20:18 +1100311 int i, cmdnum;
312
313 /* Skip leading whitespace */
314 cp = cp + strspn(cp, WHITESPACE);
315
316 /* Ignore blank lines */
317 if (!*cp)
318 return(-1);
319
320 /* Figure out which command we have */
321 for(i = 0; cmds[i].c; i++) {
322 int cmdlen = strlen(cmds[i].c);
323
324 /* Check for command followed by whitespace */
325 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
326 strchr(WHITESPACE, cp[cmdlen])) {
327 cp += cmdlen;
328 cp = cp + strspn(cp, WHITESPACE);
329 break;
330 }
331 }
332 cmdnum = cmds[i].n;
333 cmd = cmds[i].c;
334
335 /* Special case */
336 if (*cp == '!') {
337 cp++;
338 cmdnum = I_SHELL;
339 } else if (cmdnum == -1) {
340 error("Invalid command.");
341 return(-1);
342 }
343
344 /* Get arguments and parse flags */
345 *pflag = *n_arg = 0;
346 *path1 = *path2 = NULL;
347 switch (cmdnum) {
348 case I_GET:
349 case I_PUT:
350 if (parse_getput_flags(&cp, pflag))
351 return(-1);
352 /* Get first pathname (mandatory) */
353 if (get_pathname(&cp, path1))
354 return(-1);
355 if (*path1 == NULL) {
356 error("You must specify at least one path after a "
357 "%s command.", cmd);
358 return(-1);
359 }
360 /* Try to get second pathname (optional) */
361 if (get_pathname(&cp, path2))
362 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100363 break;
364 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100365 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100366 if (get_pathname(&cp, path1))
367 return(-1);
368 if (get_pathname(&cp, path2))
369 return(-1);
370 if (!*path1 || !*path2) {
371 error("You must specify two paths after a %s "
372 "command.", cmd);
373 return(-1);
374 }
375 break;
376 case I_RM:
377 case I_MKDIR:
378 case I_RMDIR:
379 case I_CHDIR:
380 case I_LCHDIR:
381 case I_LMKDIR:
382 /* Get pathname (mandatory) */
383 if (get_pathname(&cp, path1))
384 return(-1);
385 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000386 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100387 cmd);
388 return(-1);
389 }
390 break;
391 case I_LS:
392 /* Path is optional */
393 if (get_pathname(&cp, path1))
394 return(-1);
395 break;
396 case I_LLS:
397 case I_SHELL:
398 /* Uses the rest of the line */
399 break;
400 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000401 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100402 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000403 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100404 case I_CHOWN:
405 case I_CHGRP:
406 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000407 l = strtol(cp, &cp2, base);
408 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
409 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100410 error("You must supply a numeric argument "
411 "to the %s command.", cmd);
412 return(-1);
413 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000414 cp = cp2;
415 *n_arg = l;
416 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
417 break;
418 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100419 error("You must supply a numeric argument "
420 "to the %s command.", cmd);
421 return(-1);
422 }
423 cp += strspn(cp, WHITESPACE);
424
425 /* Get pathname (mandatory) */
426 if (get_pathname(&cp, path1))
427 return(-1);
428 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000429 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100430 cmd);
431 return(-1);
432 }
433 break;
434 case I_QUIT:
435 case I_PWD:
436 case I_LPWD:
437 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000438 case I_VERSION:
Damien Miller33804262001-02-04 23:20:18 +1100439 break;
440 default:
441 fatal("Command not implemented");
442 }
443
444 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100445 return(cmdnum);
446}
447
448int
449parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
450{
Damien Millerd7686fd2001-02-10 00:40:03 +1100451 char *path1, *path2, *tmp;
Damien Miller4870afd2001-03-14 10:27:09 +1100452 int pflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100453 unsigned long n_arg;
454 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000455 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000456 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100457 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100458
459 path1 = path2 = NULL;
460 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
461
462 /* Perform command */
463 switch (cmdnum) {
464 case -1:
465 break;
466 case I_GET:
Damien Miller4870afd2001-03-14 10:27:09 +1100467 memset(&g, 0, sizeof(g));
468 if (!remote_glob(in, out, path1, 0, NULL, &g)) {
469 if (path2) {
470 /* XXX: target should be directory */
471 error("You cannot specify a target when "
472 "downloading multiple files");
473 err = -1;
474 break;
475 }
476 for(i = 0; g.gl_pathv[i]; i++) {
477 if (!infer_path(g.gl_pathv[i], &path2)) {
478 printf("Fetching %s\n", g.gl_pathv[i]);
479 if (do_download(in, out, g.gl_pathv[i],
480 path2, pflag) == -1)
481 err = -1;
482 free(path2);
483 path2 = NULL;
484 } else
485 err = -1;
486 }
487 } else {
488 if (!path2 && infer_path(path1, &path2)) {
489 err = -1;
490 break;
491 }
492 err = do_download(in, out, path1, path2, pflag);
493 }
Damien Miller33804262001-02-04 23:20:18 +1100494 break;
495 case I_PUT:
Damien Miller4870afd2001-03-14 10:27:09 +1100496 if (!glob(path1, 0, NULL, &g)) {
497 if (path2) {
498 error("You cannot specify a target when "
499 "uploading multiple files");
500 err = -1;
501 break;
502 }
503 for(i = 0; g.gl_pathv[i]; i++) {
504 if (!infer_path(g.gl_pathv[i], &path2)) {
505 path2 = make_absolute(path2, *pwd);
506 printf("Uploading %s\n", g.gl_pathv[i]);
507 if (do_upload(in, out, g.gl_pathv[i],
508 path2, pflag) == -1)
509 err = -1;
510 free(path2);
511 path2 = NULL;
512 } else
513 err = -1;
514 }
515 } else {
516 if (!path2 && infer_path(path1, &path2)) {
517 err = -1;
518 break;
519 }
520 err = do_upload(in, out, path1, path2, pflag);
521 }
522 break;
523 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100524 path1 = make_absolute(path1, *pwd);
525 path2 = make_absolute(path2, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000526 err = do_rename(in, out, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100527 break;
Damien Miller058316f2001-03-08 10:08:49 +1100528 case I_SYMLINK:
529 if (version < 3) {
530 error("The server (version %d) does not support "
531 "this operation", version);
532 err = -1;
533 } else {
534 path2 = make_absolute(path2, *pwd);
535 err = do_symlink(in, out, path1, path2);
536 }
537 break;
Damien Miller33804262001-02-04 23:20:18 +1100538 case I_RM:
539 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100540 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
541 for(i = 0; g.gl_pathv[i]; i++) {
542 printf("Removing %s\n", g.gl_pathv[i]);
543 if (do_rm(in, out, g.gl_pathv[i]) == -1)
544 err = -1;
545 }
Damien Miller33804262001-02-04 23:20:18 +1100546 break;
547 case I_MKDIR:
548 path1 = make_absolute(path1, *pwd);
549 attrib_clear(&a);
550 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
551 a.perm = 0777;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000552 err = do_mkdir(in, out, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100553 break;
554 case I_RMDIR:
555 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000556 err = do_rmdir(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100557 break;
558 case I_CHDIR:
559 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000560 if ((tmp = do_realpath(in, out, path1)) == NULL) {
561 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100562 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000563 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100564 if ((aa = do_stat(in, out, tmp)) == NULL) {
565 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000566 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100567 break;
568 }
569 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
570 error("Can't change directory: Can't check target");
571 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000572 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100573 break;
574 }
575 if (!S_ISDIR(aa->perm)) {
576 error("Can't change directory: \"%s\" is not "
577 "a directory", tmp);
578 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000579 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100580 break;
581 }
Damien Miller33804262001-02-04 23:20:18 +1100582 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100583 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100584 break;
585 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100586 if (!path1) {
587 do_ls(in, out, *pwd);
588 break;
589 }
Damien Miller33804262001-02-04 23:20:18 +1100590 path1 = make_absolute(path1, *pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100591 if ((tmp = do_realpath(in, out, path1)) == NULL)
592 break;
593 xfree(path1);
594 path1 = tmp;
595 if ((aa = do_stat(in, out, path1)) == NULL)
596 break;
597 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
598 !S_ISDIR(aa->perm)) {
599 error("Can't ls: \"%s\" is not a directory", path1);
600 break;
601 }
602 do_ls(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100603 break;
604 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000605 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100606 error("Couldn't change local directory to "
607 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000608 err = 1;
609 }
Damien Miller33804262001-02-04 23:20:18 +1100610 break;
611 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000612 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100613 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100614 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000615 err = 1;
616 }
Damien Miller33804262001-02-04 23:20:18 +1100617 break;
618 case I_LLS:
619 local_do_ls(cmd);
620 break;
621 case I_SHELL:
622 local_do_shell(cmd);
623 break;
624 case I_LUMASK:
625 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +0000626 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +1100627 break;
628 case I_CHMOD:
629 path1 = make_absolute(path1, *pwd);
630 attrib_clear(&a);
631 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
632 a.perm = n_arg;
Damien Miller4870afd2001-03-14 10:27:09 +1100633 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
634 for(i = 0; g.gl_pathv[i]; i++) {
635 printf("Changing mode on %s\n", g.gl_pathv[i]);
636 do_setstat(in, out, g.gl_pathv[i], &a);
637 }
Kevin Steves62c45db2001-02-05 13:42:43 +0000638 break;
Damien Miller33804262001-02-04 23:20:18 +1100639 case I_CHOWN:
640 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100641 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
642 for(i = 0; g.gl_pathv[i]; i++) {
643 if (!(aa = do_stat(in, out, g.gl_pathv[i])))
644 continue;
645 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
646 error("Can't get current ownership of "
647 "remote file \"%s\"", g.gl_pathv[i]);
648 continue;
649 }
650 printf("Changing owner on %s\n", g.gl_pathv[i]);
651 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
652 aa->uid = n_arg;
653 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100654 }
Damien Miller33804262001-02-04 23:20:18 +1100655 break;
656 case I_CHGRP:
657 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100658 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
659 for(i = 0; g.gl_pathv[i]; i++) {
660 if (!(aa = do_stat(in, out, g.gl_pathv[i])))
661 continue;
662 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
663 error("Can't get current ownership of "
664 "remote file \"%s\"", g.gl_pathv[i]);
665 continue;
666 }
667 printf("Changing group on %s\n", g.gl_pathv[i]);
668 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
669 aa->gid = n_arg;
670 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100671 }
Damien Miller33804262001-02-04 23:20:18 +1100672 break;
673 case I_PWD:
674 printf("Remote working directory: %s\n", *pwd);
675 break;
676 case I_LPWD:
677 if (!getcwd(path_buf, sizeof(path_buf)))
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000678 error("Couldn't get local cwd: %s",
Damien Miller33804262001-02-04 23:20:18 +1100679 strerror(errno));
680 else
681 printf("Local working directory: %s\n",
682 path_buf);
683 break;
684 case I_QUIT:
685 return(-1);
686 case I_HELP:
687 help();
688 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +0000689 case I_VERSION:
690 printf("SFTP protocol version %d\n", version);
691 break;
Damien Miller33804262001-02-04 23:20:18 +1100692 default:
693 fatal("%d is not implemented", cmdnum);
694 }
695
696 if (path1)
697 xfree(path1);
698 if (path2)
699 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000700
701 /* If an error occurs in batch mode we should abort. */
702 if (infile != stdin && err > 0)
703 return -1;
704
Damien Miller33804262001-02-04 23:20:18 +1100705 return(0);
706}
707
708void
709interactive_loop(int fd_in, int fd_out)
710{
711 char *pwd;
712 char cmd[2048];
713
Damien Miller058316f2001-03-08 10:08:49 +1100714 version = do_init(fd_in, fd_out);
715 if (version == -1)
716 fatal("Couldn't initialise connection to server");
717
Damien Miller33804262001-02-04 23:20:18 +1100718 pwd = do_realpath(fd_in, fd_out, ".");
719 if (pwd == NULL)
720 fatal("Need cwd");
721
Damien Millerd7686fd2001-02-10 00:40:03 +1100722 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000723 setvbuf(infile, NULL, _IOLBF, 0);
Damien Miller33804262001-02-04 23:20:18 +1100724
725 for(;;) {
726 char *cp;
727
728 printf("sftp> ");
729
730 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000731 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100732 printf("\n");
733 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000734 } else if (infile != stdin) /* Bluff typing */
735 printf("%s", cmd);
736
Damien Miller33804262001-02-04 23:20:18 +1100737 cp = strrchr(cmd, '\n');
738 if (cp)
739 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000740
Damien Miller33804262001-02-04 23:20:18 +1100741 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
742 break;
743 }
744 xfree(pwd);
745}