blob: babc0ed60d06a034d0f336e512fb12ab93cb9f7c [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 Miller16a13332002-02-13 14:03:56 +110029RCSID("$OpenBSD: sftp-int.c,v 1.43 2002/02/12 12:32:27 djm 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
Damien Miller8829d362002-02-08 22:04:05 +110045/* Size of buffer used when copying files */
46extern size_t copy_buffer_len;
47
Damien Miller16a13332002-02-13 14:03:56 +110048/* Number of concurrent outstanding requests */
49extern int num_requests;
50
Damien Miller058316f2001-03-08 10:08:49 +110051/* Version of server we are speaking to */
52int version;
Ben Lindstrom562c26b2001-03-07 01:26:48 +000053
Damien Miller33804262001-02-04 23:20:18 +110054/* Seperators for interactive commands */
55#define WHITESPACE " \t\r\n"
56
57/* Commands for interactive mode */
58#define I_CHDIR 1
59#define I_CHGRP 2
60#define I_CHMOD 3
61#define I_CHOWN 4
62#define I_GET 5
63#define I_HELP 6
64#define I_LCHDIR 7
65#define I_LLS 8
66#define I_LMKDIR 9
67#define I_LPWD 10
68#define I_LS 11
69#define I_LUMASK 12
70#define I_MKDIR 13
71#define I_PUT 14
72#define I_PWD 15
73#define I_QUIT 16
74#define I_RENAME 17
75#define I_RM 18
76#define I_RMDIR 19
77#define I_SHELL 20
Damien Miller058316f2001-03-08 10:08:49 +110078#define I_SYMLINK 21
Ben Lindstromf78682d2001-03-14 21:26:27 +000079#define I_VERSION 22
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 },
107 { "put", I_PUT },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +0000108 { "mput", I_PUT },
Damien Millerd7686fd2001-02-10 00:40:03 +1100109 { "pwd", I_PWD },
110 { "quit", I_QUIT },
111 { "rename", I_RENAME },
112 { "rm", I_RM },
113 { "rmdir", I_RMDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100114 { "symlink", I_SYMLINK },
Ben Lindstromf78682d2001-03-14 21:26:27 +0000115 { "version", I_VERSION },
Kevin Steves62c45db2001-02-05 13:42:43 +0000116 { "!", I_SHELL },
117 { "?", I_HELP },
118 { NULL, -1}
Damien Miller33804262001-02-04 23:20:18 +1100119};
120
Ben Lindstrombba81212001-06-25 05:01:22 +0000121static void
Damien Miller33804262001-02-04 23:20:18 +1100122help(void)
123{
124 printf("Available commands:\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100125 printf("cd path Change remote directory to 'path'\n");
126 printf("lcd path Change local directory to 'path'\n");
127 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
128 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
129 printf("chown own path Change owner of file 'path' to 'own'\n");
130 printf("help Display this help text\n");
131 printf("get remote-path [local-path] Download file\n");
132 printf("lls [ls-options [path]] Display local directory listing\n");
Damien Miller058316f2001-03-08 10:08:49 +1100133 printf("ln oldpath newpath Symlink remote file\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100134 printf("lmkdir path Create local directory\n");
135 printf("lpwd Print local working directory\n");
136 printf("ls [path] Display remote directory listing\n");
137 printf("lumask umask Set local umask to 'umask'\n");
138 printf("mkdir path Create remote directory\n");
139 printf("put local-path [remote-path] Upload file\n");
140 printf("pwd Display remote working directory\n");
141 printf("exit Quit sftp\n");
142 printf("quit Quit sftp\n");
143 printf("rename oldpath newpath Rename remote file\n");
144 printf("rmdir path Remove remote directory\n");
145 printf("rm path Delete remote file\n");
Damien Miller058316f2001-03-08 10:08:49 +1100146 printf("symlink oldpath newpath Symlink remote file\n");
Ben Lindstromf78682d2001-03-14 21:26:27 +0000147 printf("version Show SFTP version\n");
Damien Miller33804262001-02-04 23:20:18 +1100148 printf("!command Execute 'command' in local shell\n");
149 printf("! Escape to local shell\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100150 printf("? Synonym for help\n");
Damien Miller33804262001-02-04 23:20:18 +1100151}
152
Ben Lindstrombba81212001-06-25 05:01:22 +0000153static void
Damien Miller33804262001-02-04 23:20:18 +1100154local_do_shell(const char *args)
155{
Ben Lindstrom206941f2001-04-15 14:27:16 +0000156 int status;
Damien Miller33804262001-02-04 23:20:18 +1100157 char *shell;
158 pid_t pid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000159
Damien Miller33804262001-02-04 23:20:18 +1100160 if (!*args)
161 args = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000162
Damien Miller33804262001-02-04 23:20:18 +1100163 if ((shell = getenv("SHELL")) == NULL)
164 shell = _PATH_BSHELL;
165
166 if ((pid = fork()) == -1)
167 fatal("Couldn't fork: %s", strerror(errno));
168
169 if (pid == 0) {
170 /* XXX: child has pipe fds to ssh subproc open - issue? */
171 if (args) {
172 debug3("Executing %s -c \"%s\"", shell, args);
Damien Millerefb1edf2001-07-14 12:19:36 +1000173 execl(shell, shell, "-c", args, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100174 } else {
175 debug3("Executing %s", shell);
Damien Millerefb1edf2001-07-14 12:19:36 +1000176 execl(shell, shell, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100177 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000178 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
Damien Miller33804262001-02-04 23:20:18 +1100179 strerror(errno));
180 _exit(1);
181 }
182 if (waitpid(pid, &status, 0) == -1)
183 fatal("Couldn't wait for child: %s", strerror(errno));
184 if (!WIFEXITED(status))
185 error("Shell exited abormally");
186 else if (WEXITSTATUS(status))
187 error("Shell exited with status %d", WEXITSTATUS(status));
188}
189
Ben Lindstrombba81212001-06-25 05:01:22 +0000190static void
Damien Miller33804262001-02-04 23:20:18 +1100191local_do_ls(const char *args)
192{
193 if (!args || !*args)
Damien Millerd7686fd2001-02-10 00:40:03 +1100194 local_do_shell(_PATH_LS);
Damien Miller33804262001-02-04 23:20:18 +1100195 else {
Damien Millerd7686fd2001-02-10 00:40:03 +1100196 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
197 char *buf = xmalloc(len);
Damien Miller33804262001-02-04 23:20:18 +1100198
199 /* XXX: quoting - rip quoting code from ftp? */
Damien Millerd7686fd2001-02-10 00:40:03 +1100200 snprintf(buf, len, _PATH_LS " %s", args);
Damien Miller33804262001-02-04 23:20:18 +1100201 local_do_shell(buf);
Damien Millerd7686fd2001-02-10 00:40:03 +1100202 xfree(buf);
Damien Miller33804262001-02-04 23:20:18 +1100203 }
204}
205
Ben Lindstrombba81212001-06-25 05:01:22 +0000206static char *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000207path_append(char *p1, char *p2)
208{
209 char *ret;
Ben Lindstromcf00df62001-03-17 00:37:31 +0000210 int len = strlen(p1) + strlen(p2) + 2;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000211
Ben Lindstromcf00df62001-03-17 00:37:31 +0000212 ret = xmalloc(len);
213 strlcpy(ret, p1, len);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100214 if (strcmp(p1, "/") != 0)
Ben Lindstrom95148e32001-08-06 21:30:53 +0000215 strlcat(ret, "/", len);
Ben Lindstromcf00df62001-03-17 00:37:31 +0000216 strlcat(ret, p2, len);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000217
218 return(ret);
219}
220
Ben Lindstrombba81212001-06-25 05:01:22 +0000221static char *
Damien Miller33804262001-02-04 23:20:18 +1100222make_absolute(char *p, char *pwd)
223{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000224 char *abs;
Damien Miller33804262001-02-04 23:20:18 +1100225
226 /* Derelativise */
227 if (p && p[0] != '/') {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000228 abs = path_append(pwd, p);
Damien Miller33804262001-02-04 23:20:18 +1100229 xfree(p);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000230 return(abs);
231 } else
232 return(p);
233}
234
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000236infer_path(const char *p, char **ifp)
237{
238 char *cp;
239
240 cp = strrchr(p, '/');
241 if (cp == NULL) {
242 *ifp = xstrdup(p);
243 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100244 }
245
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000246 if (!cp[1]) {
247 error("Invalid path");
248 return(-1);
249 }
250
251 *ifp = xstrdup(cp + 1);
252 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100253}
254
Ben Lindstrombba81212001-06-25 05:01:22 +0000255static int
Damien Miller33804262001-02-04 23:20:18 +1100256parse_getput_flags(const char **cpp, int *pflag)
257{
258 const char *cp = *cpp;
259
260 /* Check for flags */
261 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100262 switch (cp[1]) {
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000263 case 'p':
Damien Miller33804262001-02-04 23:20:18 +1100264 case 'P':
265 *pflag = 1;
266 break;
267 default:
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000268 error("Invalid flag -%c", cp[1]);
Damien Miller33804262001-02-04 23:20:18 +1100269 return(-1);
270 }
271 cp += 2;
272 *cpp = cp + strspn(cp, WHITESPACE);
273 }
274
275 return(0);
276}
277
Ben Lindstrombba81212001-06-25 05:01:22 +0000278static int
Damien Miller33804262001-02-04 23:20:18 +1100279get_pathname(const char **cpp, char **path)
280{
Damien Millerd7686fd2001-02-10 00:40:03 +1100281 const char *cp = *cpp, *end;
282 char quot;
Damien Miller33804262001-02-04 23:20:18 +1100283 int i;
284
285 cp += strspn(cp, WHITESPACE);
286 if (!*cp) {
287 *cpp = cp;
288 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100289 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100290 }
291
292 /* Check for quoted filenames */
293 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100294 quot = *cp++;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000295
Damien Millerd7686fd2001-02-10 00:40:03 +1100296 end = strchr(cp, quot);
297 if (end == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100298 error("Unterminated quote");
Damien Millerd7686fd2001-02-10 00:40:03 +1100299 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100300 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100301 if (cp == end) {
Damien Miller33804262001-02-04 23:20:18 +1100302 error("Empty quotes");
Damien Millerd7686fd2001-02-10 00:40:03 +1100303 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100304 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100305 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
306 } else {
307 /* Read to end of filename */
308 end = strpbrk(cp, WHITESPACE);
309 if (end == NULL)
310 end = strchr(cp, '\0');
311 *cpp = end + strspn(end, WHITESPACE);
Damien Miller33804262001-02-04 23:20:18 +1100312 }
313
Damien Millerd7686fd2001-02-10 00:40:03 +1100314 i = end - cp;
Damien Miller33804262001-02-04 23:20:18 +1100315
316 *path = xmalloc(i + 1);
317 memcpy(*path, cp, i);
318 (*path)[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100319 return(0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100320
321 fail:
322 *path = NULL;
323 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100324}
325
Ben Lindstrombba81212001-06-25 05:01:22 +0000326static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000327is_dir(char *path)
Damien Miller33804262001-02-04 23:20:18 +1100328{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000329 struct stat sb;
Damien Miller33804262001-02-04 23:20:18 +1100330
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000331 /* XXX: report errors? */
332 if (stat(path, &sb) == -1)
Damien Miller33804262001-02-04 23:20:18 +1100333 return(0);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000334
335 return(sb.st_mode & S_IFDIR);
336}
337
Ben Lindstrombba81212001-06-25 05:01:22 +0000338static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000339remote_is_dir(int in, int out, char *path)
340{
341 Attrib *a;
342
343 /* XXX: report errors? */
344 if ((a = do_stat(in, out, path, 1)) == NULL)
345 return(0);
346 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
347 return(0);
348 return(a->perm & S_IFDIR);
349}
350
Ben Lindstrombba81212001-06-25 05:01:22 +0000351static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000352process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
353{
354 char *abs_src = NULL;
355 char *abs_dst = NULL;
356 char *tmp;
357 glob_t g;
358 int err = 0;
359 int i;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000360
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000361 abs_src = xstrdup(src);
362 abs_src = make_absolute(abs_src, pwd);
363
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000364 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000365 debug3("Looking up %s", abs_src);
366 if (remote_glob(in, out, abs_src, 0, NULL, &g)) {
367 error("File \"%s\" not found.", abs_src);
368 err = -1;
369 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100370 }
371
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000372 /* Only one match, dst may be file, directory or unspecified */
373 if (g.gl_pathv[0] && g.gl_matchc == 1) {
374 if (dst) {
375 /* If directory specified, append filename */
376 if (is_dir(dst)) {
377 if (infer_path(g.gl_pathv[0], &tmp)) {
378 err = 1;
379 goto out;
380 }
381 abs_dst = path_append(dst, tmp);
382 xfree(tmp);
383 } else
384 abs_dst = xstrdup(dst);
385 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
386 err = -1;
387 goto out;
388 }
389 printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
Damien Miller8829d362002-02-08 22:04:05 +1100390 err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag,
Damien Miller16a13332002-02-13 14:03:56 +1100391 copy_buffer_len, num_requests);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000392 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100393 }
394
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000395 /* Multiple matches, dst may be directory or unspecified */
396 if (dst && !is_dir(dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000397 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000398 dst);
399 err = -1;
400 goto out;
401 }
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000402
Damien Miller9f0f5c62001-12-21 14:45:46 +1100403 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000404 if (infer_path(g.gl_pathv[i], &tmp)) {
405 err = -1;
406 goto out;
407 }
408 if (dst) {
409 abs_dst = path_append(dst, tmp);
410 xfree(tmp);
411 } else
412 abs_dst = tmp;
413
414 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller8829d362002-02-08 22:04:05 +1100415 if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag,
Damien Miller16a13332002-02-13 14:03:56 +1100416 copy_buffer_len, num_requests) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000417 err = -1;
418 xfree(abs_dst);
419 abs_dst = NULL;
420 }
421
422out:
423 xfree(abs_src);
424 if (abs_dst)
425 xfree(abs_dst);
426 globfree(&g);
427 return(err);
428}
429
Ben Lindstrombba81212001-06-25 05:01:22 +0000430static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000431process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
432{
433 char *tmp_dst = NULL;
434 char *abs_dst = NULL;
435 char *tmp;
436 glob_t g;
437 int err = 0;
438 int i;
439
440 if (dst) {
441 tmp_dst = xstrdup(dst);
442 tmp_dst = make_absolute(tmp_dst, pwd);
443 }
444
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000445 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000446 debug3("Looking up %s", src);
447 if (glob(src, 0, NULL, &g)) {
448 error("File \"%s\" not found.", src);
449 err = -1;
450 goto out;
451 }
452
453 /* Only one match, dst may be file, directory or unspecified */
454 if (g.gl_pathv[0] && g.gl_matchc == 1) {
455 if (tmp_dst) {
456 /* If directory specified, append filename */
457 if (remote_is_dir(in, out, tmp_dst)) {
458 if (infer_path(g.gl_pathv[0], &tmp)) {
459 err = 1;
460 goto out;
461 }
462 abs_dst = path_append(tmp_dst, tmp);
463 xfree(tmp);
464 } else
465 abs_dst = xstrdup(tmp_dst);
Ben Lindstrom7527f8b2001-03-24 00:39:12 +0000466 } else {
467 if (infer_path(g.gl_pathv[0], &abs_dst)) {
468 err = -1;
469 goto out;
470 }
471 abs_dst = make_absolute(abs_dst, pwd);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000472 }
473 printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
Damien Miller8829d362002-02-08 22:04:05 +1100474 err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag,
Damien Miller16a13332002-02-13 14:03:56 +1100475 copy_buffer_len, num_requests);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000476 goto out;
477 }
478
479 /* Multiple matches, dst may be directory or unspecified */
480 if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000481 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000482 tmp_dst);
483 err = -1;
484 goto out;
485 }
486
Damien Miller9f0f5c62001-12-21 14:45:46 +1100487 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000488 if (infer_path(g.gl_pathv[i], &tmp)) {
489 err = -1;
490 goto out;
491 }
492 if (tmp_dst) {
493 abs_dst = path_append(tmp_dst, tmp);
494 xfree(tmp);
495 } else
496 abs_dst = make_absolute(tmp, pwd);
497
498 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller8829d362002-02-08 22:04:05 +1100499 if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag,
Damien Miller16a13332002-02-13 14:03:56 +1100500 copy_buffer_len, num_requests) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000501 err = -1;
502 }
503
504out:
505 if (abs_dst)
506 xfree(abs_dst);
507 if (tmp_dst)
508 xfree(tmp_dst);
509 return(err);
Damien Miller33804262001-02-04 23:20:18 +1100510}
511
Ben Lindstrombba81212001-06-25 05:01:22 +0000512static int
Damien Miller33804262001-02-04 23:20:18 +1100513parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
514 char **path1, char **path2)
515{
516 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000517 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000518 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000519 long l;
Damien Miller33804262001-02-04 23:20:18 +1100520 int i, cmdnum;
521
522 /* Skip leading whitespace */
523 cp = cp + strspn(cp, WHITESPACE);
524
525 /* Ignore blank lines */
526 if (!*cp)
527 return(-1);
528
529 /* Figure out which command we have */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100530 for (i = 0; cmds[i].c; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100531 int cmdlen = strlen(cmds[i].c);
532
533 /* Check for command followed by whitespace */
534 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
535 strchr(WHITESPACE, cp[cmdlen])) {
536 cp += cmdlen;
537 cp = cp + strspn(cp, WHITESPACE);
538 break;
539 }
540 }
541 cmdnum = cmds[i].n;
542 cmd = cmds[i].c;
543
544 /* Special case */
545 if (*cp == '!') {
546 cp++;
547 cmdnum = I_SHELL;
548 } else if (cmdnum == -1) {
549 error("Invalid command.");
550 return(-1);
551 }
552
553 /* Get arguments and parse flags */
554 *pflag = *n_arg = 0;
555 *path1 = *path2 = NULL;
556 switch (cmdnum) {
557 case I_GET:
558 case I_PUT:
559 if (parse_getput_flags(&cp, pflag))
560 return(-1);
561 /* Get first pathname (mandatory) */
562 if (get_pathname(&cp, path1))
563 return(-1);
564 if (*path1 == NULL) {
565 error("You must specify at least one path after a "
566 "%s command.", cmd);
567 return(-1);
568 }
569 /* Try to get second pathname (optional) */
570 if (get_pathname(&cp, path2))
571 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100572 break;
573 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100574 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100575 if (get_pathname(&cp, path1))
576 return(-1);
577 if (get_pathname(&cp, path2))
578 return(-1);
579 if (!*path1 || !*path2) {
580 error("You must specify two paths after a %s "
581 "command.", cmd);
582 return(-1);
583 }
584 break;
585 case I_RM:
586 case I_MKDIR:
587 case I_RMDIR:
588 case I_CHDIR:
589 case I_LCHDIR:
590 case I_LMKDIR:
591 /* Get pathname (mandatory) */
592 if (get_pathname(&cp, path1))
593 return(-1);
594 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000595 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100596 cmd);
597 return(-1);
598 }
599 break;
600 case I_LS:
601 /* Path is optional */
602 if (get_pathname(&cp, path1))
603 return(-1);
604 break;
605 case I_LLS:
606 case I_SHELL:
607 /* Uses the rest of the line */
608 break;
609 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000610 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100611 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000612 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100613 case I_CHOWN:
614 case I_CHGRP:
615 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000616 l = strtol(cp, &cp2, base);
617 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
618 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100619 error("You must supply a numeric argument "
620 "to the %s command.", cmd);
621 return(-1);
622 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000623 cp = cp2;
624 *n_arg = l;
625 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
626 break;
627 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100628 error("You must supply a numeric argument "
629 "to the %s command.", cmd);
630 return(-1);
631 }
632 cp += strspn(cp, WHITESPACE);
633
634 /* Get pathname (mandatory) */
635 if (get_pathname(&cp, path1))
636 return(-1);
637 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000638 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100639 cmd);
640 return(-1);
641 }
642 break;
643 case I_QUIT:
644 case I_PWD:
645 case I_LPWD:
646 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000647 case I_VERSION:
Damien Miller33804262001-02-04 23:20:18 +1100648 break;
649 default:
650 fatal("Command not implemented");
651 }
652
653 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100654 return(cmdnum);
655}
656
Ben Lindstrombba81212001-06-25 05:01:22 +0000657static int
Damien Miller33804262001-02-04 23:20:18 +1100658parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
659{
Damien Millerd7686fd2001-02-10 00:40:03 +1100660 char *path1, *path2, *tmp;
Damien Miller4870afd2001-03-14 10:27:09 +1100661 int pflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100662 unsigned long n_arg;
663 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000664 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000665 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100666 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100667
668 path1 = path2 = NULL;
669 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
670
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000671 memset(&g, 0, sizeof(g));
672
Damien Miller33804262001-02-04 23:20:18 +1100673 /* Perform command */
674 switch (cmdnum) {
675 case -1:
676 break;
677 case I_GET:
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000678 err = process_get(in, out, path1, path2, *pwd, pflag);
Damien Miller33804262001-02-04 23:20:18 +1100679 break;
680 case I_PUT:
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000681 err = process_put(in, out, path1, path2, *pwd, pflag);
Ben Lindstroma3700052001-04-05 23:26:32 +0000682 break;
683 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100684 path1 = make_absolute(path1, *pwd);
685 path2 = make_absolute(path2, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000686 err = do_rename(in, out, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100687 break;
Damien Miller058316f2001-03-08 10:08:49 +1100688 case I_SYMLINK:
689 if (version < 3) {
690 error("The server (version %d) does not support "
691 "this operation", version);
692 err = -1;
693 } else {
694 path2 = make_absolute(path2, *pwd);
695 err = do_symlink(in, out, path1, path2);
696 }
697 break;
Damien Miller33804262001-02-04 23:20:18 +1100698 case I_RM:
699 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100700 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100701 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100702 printf("Removing %s\n", g.gl_pathv[i]);
703 if (do_rm(in, out, g.gl_pathv[i]) == -1)
704 err = -1;
705 }
Damien Miller33804262001-02-04 23:20:18 +1100706 break;
707 case I_MKDIR:
708 path1 = make_absolute(path1, *pwd);
709 attrib_clear(&a);
710 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
711 a.perm = 0777;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000712 err = do_mkdir(in, out, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100713 break;
714 case I_RMDIR:
715 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000716 err = do_rmdir(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100717 break;
718 case I_CHDIR:
719 path1 = make_absolute(path1, *pwd);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000720 if ((tmp = do_realpath(in, out, path1)) == NULL) {
721 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100722 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000723 }
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000724 if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100725 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000726 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100727 break;
728 }
729 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
730 error("Can't change directory: Can't check target");
731 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000732 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100733 break;
734 }
735 if (!S_ISDIR(aa->perm)) {
736 error("Can't change directory: \"%s\" is not "
737 "a directory", tmp);
738 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000739 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100740 break;
741 }
Damien Miller33804262001-02-04 23:20:18 +1100742 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100743 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100744 break;
745 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100746 if (!path1) {
747 do_ls(in, out, *pwd);
748 break;
749 }
Damien Miller33804262001-02-04 23:20:18 +1100750 path1 = make_absolute(path1, *pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100751 if ((tmp = do_realpath(in, out, path1)) == NULL)
752 break;
753 xfree(path1);
754 path1 = tmp;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000755 if ((aa = do_stat(in, out, path1, 0)) == NULL)
Damien Millerd7686fd2001-02-10 00:40:03 +1100756 break;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000757 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Millerd7686fd2001-02-10 00:40:03 +1100758 !S_ISDIR(aa->perm)) {
759 error("Can't ls: \"%s\" is not a directory", path1);
760 break;
761 }
762 do_ls(in, out, path1);
Damien Miller33804262001-02-04 23:20:18 +1100763 break;
764 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000765 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100766 error("Couldn't change local directory to "
767 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000768 err = 1;
769 }
Damien Miller33804262001-02-04 23:20:18 +1100770 break;
771 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000772 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100773 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100774 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000775 err = 1;
776 }
Damien Miller33804262001-02-04 23:20:18 +1100777 break;
778 case I_LLS:
779 local_do_ls(cmd);
780 break;
781 case I_SHELL:
782 local_do_shell(cmd);
783 break;
784 case I_LUMASK:
785 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +0000786 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +1100787 break;
788 case I_CHMOD:
789 path1 = make_absolute(path1, *pwd);
790 attrib_clear(&a);
791 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
792 a.perm = n_arg;
Damien Miller4870afd2001-03-14 10:27:09 +1100793 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100794 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100795 printf("Changing mode on %s\n", g.gl_pathv[i]);
796 do_setstat(in, out, g.gl_pathv[i], &a);
797 }
Kevin Steves62c45db2001-02-05 13:42:43 +0000798 break;
Damien Miller33804262001-02-04 23:20:18 +1100799 case I_CHOWN:
800 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100801 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100802 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000803 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100804 continue;
805 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
806 error("Can't get current ownership of "
807 "remote file \"%s\"", g.gl_pathv[i]);
808 continue;
809 }
810 printf("Changing owner on %s\n", g.gl_pathv[i]);
811 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
812 aa->uid = n_arg;
813 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100814 }
Damien Miller33804262001-02-04 23:20:18 +1100815 break;
816 case I_CHGRP:
817 path1 = make_absolute(path1, *pwd);
Damien Miller4870afd2001-03-14 10:27:09 +1100818 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100819 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000820 if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100821 continue;
822 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
823 error("Can't get current ownership of "
824 "remote file \"%s\"", g.gl_pathv[i]);
825 continue;
826 }
827 printf("Changing group on %s\n", g.gl_pathv[i]);
828 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
829 aa->gid = n_arg;
830 do_setstat(in, out, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100831 }
Damien Miller33804262001-02-04 23:20:18 +1100832 break;
833 case I_PWD:
834 printf("Remote working directory: %s\n", *pwd);
835 break;
836 case I_LPWD:
837 if (!getcwd(path_buf, sizeof(path_buf)))
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000838 error("Couldn't get local cwd: %s",
Damien Miller33804262001-02-04 23:20:18 +1100839 strerror(errno));
840 else
841 printf("Local working directory: %s\n",
842 path_buf);
843 break;
844 case I_QUIT:
845 return(-1);
846 case I_HELP:
847 help();
848 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +0000849 case I_VERSION:
850 printf("SFTP protocol version %d\n", version);
851 break;
Damien Miller33804262001-02-04 23:20:18 +1100852 default:
853 fatal("%d is not implemented", cmdnum);
854 }
855
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000856 if (g.gl_pathc)
857 globfree(&g);
Damien Miller33804262001-02-04 23:20:18 +1100858 if (path1)
859 xfree(path1);
860 if (path2)
861 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000862
863 /* If an error occurs in batch mode we should abort. */
864 if (infile != stdin && err > 0)
865 return -1;
866
Damien Miller33804262001-02-04 23:20:18 +1100867 return(0);
868}
869
870void
Ben Lindstrom63667f62001-04-13 00:00:14 +0000871interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
Damien Miller33804262001-02-04 23:20:18 +1100872{
873 char *pwd;
Ben Lindstrom63667f62001-04-13 00:00:14 +0000874 char *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100875 char cmd[2048];
876
Damien Miller058316f2001-03-08 10:08:49 +1100877 version = do_init(fd_in, fd_out);
878 if (version == -1)
879 fatal("Couldn't initialise connection to server");
880
Damien Miller33804262001-02-04 23:20:18 +1100881 pwd = do_realpath(fd_in, fd_out, ".");
882 if (pwd == NULL)
883 fatal("Need cwd");
884
Ben Lindstrom63667f62001-04-13 00:00:14 +0000885 if (file1 != NULL) {
886 dir = xstrdup(file1);
887 dir = make_absolute(dir, pwd);
888
889 if (remote_is_dir(fd_in, fd_out, dir) && file2 == NULL) {
890 printf("Changing to: %s\n", dir);
891 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
892 parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
893 } else {
894 if (file2 == NULL)
895 snprintf(cmd, sizeof cmd, "get %s", dir);
896 else
897 snprintf(cmd, sizeof cmd, "get %s %s", dir,
898 file2);
899
900 parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
901 return;
902 }
903 }
Ben Lindstrom6aebb342001-05-09 00:38:19 +0000904#if HAVE_SETVBUF
Damien Millerd7686fd2001-02-10 00:40:03 +1100905 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000906 setvbuf(infile, NULL, _IOLBF, 0);
Ben Lindstrom6aebb342001-05-09 00:38:19 +0000907#else
908 setlinebuf(stdout);
909 setlinebuf(infile);
910#endif
Damien Miller33804262001-02-04 23:20:18 +1100911
Damien Miller9f0f5c62001-12-21 14:45:46 +1100912 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +1100913 char *cp;
914
915 printf("sftp> ");
916
917 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000918 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100919 printf("\n");
920 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000921 } else if (infile != stdin) /* Bluff typing */
922 printf("%s", cmd);
923
Damien Miller33804262001-02-04 23:20:18 +1100924 cp = strrchr(cmd, '\n');
925 if (cp)
926 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000927
Damien Miller33804262001-02-04 23:20:18 +1100928 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
929 break;
930 }
931 xfree(pwd);
932}