]>
Commit | Line | Data |
---|---|---|
61e96248 | 1 | /* |
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 | ||
0426a3b4 | 25 | /* XXX: globbed ls */ |
61e96248 | 26 | /* XXX: recursive operations */ |
27 | ||
28 | #include "includes.h" | |
184eed6a | 29 | RCSID("$OpenBSD: sftp-int.c,v 1.41 2001/12/19 07:18:56 deraadt Exp $"); |
2e4fb373 | 30 | |
61e96248 | 31 | #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" | |
2e4fb373 | 38 | #include "sftp-glob.h" |
61e96248 | 39 | #include "sftp-client.h" |
40 | #include "sftp-int.h" | |
41 | ||
3a7fe5ba | 42 | /* File to read commands from */ |
43 | extern FILE *infile; | |
44 | ||
45 | /* Version of server we are speaking to */ | |
46 | int version; | |
a5ec8a3d | 47 | |
61e96248 | 48 | /* 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 | |
3a7fe5ba | 72 | #define I_SYMLINK 21 |
85cf5827 | 73 | #define I_VERSION 22 |
61e96248 | 74 | |
75 | struct CMD { | |
61e96248 | 76 | const char *c; |
ec2a033a | 77 | const int n; |
61e96248 | 78 | }; |
79 | ||
80 | const struct CMD cmds[] = { | |
68874d2b | 81 | { "bye", I_QUIT }, |
0426a3b4 | 82 | { "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 }, | |
45a2e669 | 90 | { "mget", I_GET }, |
0426a3b4 | 91 | { "help", I_HELP }, |
92 | { "lcd", I_LCHDIR }, | |
93 | { "lchdir", I_LCHDIR }, | |
94 | { "lls", I_LLS }, | |
95 | { "lmkdir", I_LMKDIR }, | |
3a7fe5ba | 96 | { "ln", I_SYMLINK }, |
0426a3b4 | 97 | { "lpwd", I_LPWD }, |
98 | { "ls", I_LS }, | |
99 | { "lumask", I_LUMASK }, | |
100 | { "mkdir", I_MKDIR }, | |
101 | { "put", I_PUT }, | |
45a2e669 | 102 | { "mput", I_PUT }, |
0426a3b4 | 103 | { "pwd", I_PWD }, |
104 | { "quit", I_QUIT }, | |
105 | { "rename", I_RENAME }, | |
106 | { "rm", I_RM }, | |
107 | { "rmdir", I_RMDIR }, | |
3a7fe5ba | 108 | { "symlink", I_SYMLINK }, |
85cf5827 | 109 | { "version", I_VERSION }, |
ec2a033a | 110 | { "!", I_SHELL }, |
111 | { "?", I_HELP }, | |
112 | { NULL, -1} | |
61e96248 | 113 | }; |
114 | ||
396c147e | 115 | static void |
61e96248 | 116 | help(void) |
117 | { | |
118 | printf("Available commands:\n"); | |
0426a3b4 | 119 | printf("cd path Change remote directory to 'path'\n"); |
120 | printf("lcd path Change local directory to 'path'\n"); | |
121 | printf("chgrp grp path Change group of file 'path' to 'grp'\n"); | |
122 | printf("chmod mode path Change permissions of file 'path' to 'mode'\n"); | |
123 | printf("chown own path Change owner of file 'path' to 'own'\n"); | |
124 | printf("help Display this help text\n"); | |
125 | printf("get remote-path [local-path] Download file\n"); | |
126 | printf("lls [ls-options [path]] Display local directory listing\n"); | |
3a7fe5ba | 127 | printf("ln oldpath newpath Symlink remote file\n"); |
0426a3b4 | 128 | printf("lmkdir path Create local directory\n"); |
129 | printf("lpwd Print local working directory\n"); | |
130 | printf("ls [path] Display remote directory listing\n"); | |
131 | printf("lumask umask Set local umask to 'umask'\n"); | |
132 | printf("mkdir path Create remote directory\n"); | |
133 | printf("put local-path [remote-path] Upload file\n"); | |
134 | printf("pwd Display remote working directory\n"); | |
135 | printf("exit Quit sftp\n"); | |
136 | printf("quit Quit sftp\n"); | |
137 | printf("rename oldpath newpath Rename remote file\n"); | |
138 | printf("rmdir path Remove remote directory\n"); | |
139 | printf("rm path Delete remote file\n"); | |
3a7fe5ba | 140 | printf("symlink oldpath newpath Symlink remote file\n"); |
85cf5827 | 141 | printf("version Show SFTP version\n"); |
61e96248 | 142 | printf("!command Execute 'command' in local shell\n"); |
143 | printf("! Escape to local shell\n"); | |
0426a3b4 | 144 | printf("? Synonym for help\n"); |
61e96248 | 145 | } |
146 | ||
396c147e | 147 | static void |
61e96248 | 148 | local_do_shell(const char *args) |
149 | { | |
ec1f12d3 | 150 | int status; |
61e96248 | 151 | char *shell; |
152 | pid_t pid; | |
2b87da3b | 153 | |
61e96248 | 154 | if (!*args) |
155 | args = NULL; | |
2b87da3b | 156 | |
61e96248 | 157 | if ((shell = getenv("SHELL")) == NULL) |
158 | shell = _PATH_BSHELL; | |
159 | ||
160 | if ((pid = fork()) == -1) | |
161 | fatal("Couldn't fork: %s", strerror(errno)); | |
162 | ||
163 | if (pid == 0) { | |
164 | /* XXX: child has pipe fds to ssh subproc open - issue? */ | |
165 | if (args) { | |
166 | debug3("Executing %s -c \"%s\"", shell, args); | |
ed916b28 | 167 | execl(shell, shell, "-c", args, (char *)NULL); |
61e96248 | 168 | } else { |
169 | debug3("Executing %s", shell); | |
ed916b28 | 170 | execl(shell, shell, (char *)NULL); |
61e96248 | 171 | } |
2b87da3b | 172 | fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell, |
61e96248 | 173 | strerror(errno)); |
174 | _exit(1); | |
175 | } | |
176 | if (waitpid(pid, &status, 0) == -1) | |
177 | fatal("Couldn't wait for child: %s", strerror(errno)); | |
178 | if (!WIFEXITED(status)) | |
179 | error("Shell exited abormally"); | |
180 | else if (WEXITSTATUS(status)) | |
181 | error("Shell exited with status %d", WEXITSTATUS(status)); | |
182 | } | |
183 | ||
396c147e | 184 | static void |
61e96248 | 185 | local_do_ls(const char *args) |
186 | { | |
187 | if (!args || !*args) | |
0426a3b4 | 188 | local_do_shell(_PATH_LS); |
61e96248 | 189 | else { |
0426a3b4 | 190 | int len = strlen(_PATH_LS " ") + strlen(args) + 1; |
191 | char *buf = xmalloc(len); | |
61e96248 | 192 | |
193 | /* XXX: quoting - rip quoting code from ftp? */ | |
0426a3b4 | 194 | snprintf(buf, len, _PATH_LS " %s", args); |
61e96248 | 195 | local_do_shell(buf); |
0426a3b4 | 196 | xfree(buf); |
61e96248 | 197 | } |
198 | } | |
199 | ||
396c147e | 200 | static char * |
d50d9b63 | 201 | path_append(char *p1, char *p2) |
202 | { | |
203 | char *ret; | |
6a8496e4 | 204 | int len = strlen(p1) + strlen(p2) + 2; |
d50d9b63 | 205 | |
6a8496e4 | 206 | ret = xmalloc(len); |
207 | strlcpy(ret, p1, len); | |
184eed6a | 208 | if (strcmp(p1, "/") != 0) |
88690211 | 209 | strlcat(ret, "/", len); |
6a8496e4 | 210 | strlcat(ret, p2, len); |
d50d9b63 | 211 | |
212 | return(ret); | |
213 | } | |
214 | ||
396c147e | 215 | static char * |
61e96248 | 216 | make_absolute(char *p, char *pwd) |
217 | { | |
d50d9b63 | 218 | char *abs; |
61e96248 | 219 | |
220 | /* Derelativise */ | |
221 | if (p && p[0] != '/') { | |
d50d9b63 | 222 | abs = path_append(pwd, p); |
61e96248 | 223 | xfree(p); |
d50d9b63 | 224 | return(abs); |
225 | } else | |
226 | return(p); | |
227 | } | |
228 | ||
396c147e | 229 | static int |
d50d9b63 | 230 | infer_path(const char *p, char **ifp) |
231 | { | |
232 | char *cp; | |
233 | ||
234 | cp = strrchr(p, '/'); | |
235 | if (cp == NULL) { | |
236 | *ifp = xstrdup(p); | |
237 | return(0); | |
238 | } | |
239 | ||
240 | if (!cp[1]) { | |
241 | error("Invalid path"); | |
242 | return(-1); | |
61e96248 | 243 | } |
244 | ||
d50d9b63 | 245 | *ifp = xstrdup(cp + 1); |
246 | return(0); | |
61e96248 | 247 | } |
248 | ||
396c147e | 249 | static int |
61e96248 | 250 | parse_getput_flags(const char **cpp, int *pflag) |
251 | { | |
252 | const char *cp = *cpp; | |
253 | ||
254 | /* Check for flags */ | |
255 | if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) { | |
0426a3b4 | 256 | switch (cp[1]) { |
58c54a79 | 257 | case 'p': |
61e96248 | 258 | case 'P': |
259 | *pflag = 1; | |
260 | break; | |
261 | default: | |
58c54a79 | 262 | error("Invalid flag -%c", cp[1]); |
61e96248 | 263 | return(-1); |
264 | } | |
265 | cp += 2; | |
266 | *cpp = cp + strspn(cp, WHITESPACE); | |
267 | } | |
268 | ||
269 | return(0); | |
270 | } | |
271 | ||
396c147e | 272 | static int |
61e96248 | 273 | get_pathname(const char **cpp, char **path) |
274 | { | |
0426a3b4 | 275 | const char *cp = *cpp, *end; |
276 | char quot; | |
61e96248 | 277 | int i; |
278 | ||
279 | cp += strspn(cp, WHITESPACE); | |
280 | if (!*cp) { | |
281 | *cpp = cp; | |
282 | *path = NULL; | |
0426a3b4 | 283 | return (0); |
61e96248 | 284 | } |
285 | ||
286 | /* Check for quoted filenames */ | |
287 | if (*cp == '\"' || *cp == '\'') { | |
0426a3b4 | 288 | quot = *cp++; |
f55d1b5f | 289 | |
0426a3b4 | 290 | end = strchr(cp, quot); |
291 | if (end == NULL) { | |
61e96248 | 292 | error("Unterminated quote"); |
0426a3b4 | 293 | goto fail; |
61e96248 | 294 | } |
0426a3b4 | 295 | if (cp == end) { |
61e96248 | 296 | error("Empty quotes"); |
0426a3b4 | 297 | goto fail; |
61e96248 | 298 | } |
0426a3b4 | 299 | *cpp = end + 1 + strspn(end + 1, WHITESPACE); |
300 | } else { | |
301 | /* Read to end of filename */ | |
302 | end = strpbrk(cp, WHITESPACE); | |
303 | if (end == NULL) | |
304 | end = strchr(cp, '\0'); | |
305 | *cpp = end + strspn(end, WHITESPACE); | |
61e96248 | 306 | } |
307 | ||
0426a3b4 | 308 | i = end - cp; |
61e96248 | 309 | |
310 | *path = xmalloc(i + 1); | |
311 | memcpy(*path, cp, i); | |
312 | (*path)[i] = '\0'; | |
61e96248 | 313 | return(0); |
0426a3b4 | 314 | |
315 | fail: | |
316 | *path = NULL; | |
317 | return (-1); | |
61e96248 | 318 | } |
319 | ||
396c147e | 320 | static int |
d50d9b63 | 321 | is_dir(char *path) |
61e96248 | 322 | { |
d50d9b63 | 323 | struct stat sb; |
61e96248 | 324 | |
d50d9b63 | 325 | /* XXX: report errors? */ |
326 | if (stat(path, &sb) == -1) | |
61e96248 | 327 | return(0); |
d50d9b63 | 328 | |
329 | return(sb.st_mode & S_IFDIR); | |
330 | } | |
331 | ||
396c147e | 332 | static int |
d50d9b63 | 333 | remote_is_dir(int in, int out, char *path) |
334 | { | |
335 | Attrib *a; | |
336 | ||
337 | /* XXX: report errors? */ | |
338 | if ((a = do_stat(in, out, path, 1)) == NULL) | |
339 | return(0); | |
340 | if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) | |
341 | return(0); | |
342 | return(a->perm & S_IFDIR); | |
343 | } | |
344 | ||
396c147e | 345 | static int |
d50d9b63 | 346 | process_get(int in, int out, char *src, char *dst, char *pwd, int pflag) |
347 | { | |
348 | char *abs_src = NULL; | |
349 | char *abs_dst = NULL; | |
350 | char *tmp; | |
351 | glob_t g; | |
352 | int err = 0; | |
353 | int i; | |
f55d1b5f | 354 | |
d50d9b63 | 355 | abs_src = xstrdup(src); |
356 | abs_src = make_absolute(abs_src, pwd); | |
357 | ||
f55d1b5f | 358 | memset(&g, 0, sizeof(g)); |
d50d9b63 | 359 | debug3("Looking up %s", abs_src); |
360 | if (remote_glob(in, out, abs_src, 0, NULL, &g)) { | |
361 | error("File \"%s\" not found.", abs_src); | |
362 | err = -1; | |
363 | goto out; | |
61e96248 | 364 | } |
365 | ||
d50d9b63 | 366 | /* Only one match, dst may be file, directory or unspecified */ |
367 | if (g.gl_pathv[0] && g.gl_matchc == 1) { | |
368 | if (dst) { | |
369 | /* If directory specified, append filename */ | |
370 | if (is_dir(dst)) { | |
371 | if (infer_path(g.gl_pathv[0], &tmp)) { | |
372 | err = 1; | |
373 | goto out; | |
374 | } | |
375 | abs_dst = path_append(dst, tmp); | |
376 | xfree(tmp); | |
377 | } else | |
378 | abs_dst = xstrdup(dst); | |
379 | } else if (infer_path(g.gl_pathv[0], &abs_dst)) { | |
380 | err = -1; | |
381 | goto out; | |
382 | } | |
383 | printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst); | |
384 | err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag); | |
385 | goto out; | |
61e96248 | 386 | } |
387 | ||
d50d9b63 | 388 | /* Multiple matches, dst may be directory or unspecified */ |
389 | if (dst && !is_dir(dst)) { | |
f55d1b5f | 390 | error("Multiple files match, but \"%s\" is not a directory", |
d50d9b63 | 391 | dst); |
392 | err = -1; | |
393 | goto out; | |
394 | } | |
f55d1b5f | 395 | |
184eed6a | 396 | for (i = 0; g.gl_pathv[i]; i++) { |
d50d9b63 | 397 | if (infer_path(g.gl_pathv[i], &tmp)) { |
398 | err = -1; | |
399 | goto out; | |
400 | } | |
401 | if (dst) { | |
402 | abs_dst = path_append(dst, tmp); | |
403 | xfree(tmp); | |
404 | } else | |
405 | abs_dst = tmp; | |
406 | ||
407 | printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst); | |
408 | if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag) == -1) | |
409 | err = -1; | |
410 | xfree(abs_dst); | |
411 | abs_dst = NULL; | |
412 | } | |
413 | ||
414 | out: | |
415 | xfree(abs_src); | |
416 | if (abs_dst) | |
417 | xfree(abs_dst); | |
418 | globfree(&g); | |
419 | return(err); | |
420 | } | |
421 | ||
396c147e | 422 | static int |
d50d9b63 | 423 | process_put(int in, int out, char *src, char *dst, char *pwd, int pflag) |
424 | { | |
425 | char *tmp_dst = NULL; | |
426 | char *abs_dst = NULL; | |
427 | char *tmp; | |
428 | glob_t g; | |
429 | int err = 0; | |
430 | int i; | |
431 | ||
432 | if (dst) { | |
433 | tmp_dst = xstrdup(dst); | |
434 | tmp_dst = make_absolute(tmp_dst, pwd); | |
435 | } | |
436 | ||
f55d1b5f | 437 | memset(&g, 0, sizeof(g)); |
d50d9b63 | 438 | debug3("Looking up %s", src); |
439 | if (glob(src, 0, NULL, &g)) { | |
440 | error("File \"%s\" not found.", src); | |
441 | err = -1; | |
442 | goto out; | |
443 | } | |
444 | ||
445 | /* Only one match, dst may be file, directory or unspecified */ | |
446 | if (g.gl_pathv[0] && g.gl_matchc == 1) { | |
447 | if (tmp_dst) { | |
448 | /* If directory specified, append filename */ | |
449 | if (remote_is_dir(in, out, tmp_dst)) { | |
450 | if (infer_path(g.gl_pathv[0], &tmp)) { | |
451 | err = 1; | |
452 | goto out; | |
453 | } | |
454 | abs_dst = path_append(tmp_dst, tmp); | |
455 | xfree(tmp); | |
456 | } else | |
457 | abs_dst = xstrdup(tmp_dst); | |
e285053e | 458 | } else { |
459 | if (infer_path(g.gl_pathv[0], &abs_dst)) { | |
460 | err = -1; | |
461 | goto out; | |
462 | } | |
463 | abs_dst = make_absolute(abs_dst, pwd); | |
d50d9b63 | 464 | } |
465 | printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst); | |
466 | err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag); | |
467 | goto out; | |
468 | } | |
469 | ||
470 | /* Multiple matches, dst may be directory or unspecified */ | |
471 | if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) { | |
f55d1b5f | 472 | error("Multiple files match, but \"%s\" is not a directory", |
d50d9b63 | 473 | tmp_dst); |
474 | err = -1; | |
475 | goto out; | |
476 | } | |
477 | ||
184eed6a | 478 | for (i = 0; g.gl_pathv[i]; i++) { |
d50d9b63 | 479 | if (infer_path(g.gl_pathv[i], &tmp)) { |
480 | err = -1; | |
481 | goto out; | |
482 | } | |
483 | if (tmp_dst) { | |
484 | abs_dst = path_append(tmp_dst, tmp); | |
485 | xfree(tmp); | |
486 | } else | |
487 | abs_dst = make_absolute(tmp, pwd); | |
488 | ||
489 | printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst); | |
490 | if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag) == -1) | |
491 | err = -1; | |
492 | } | |
493 | ||
494 | out: | |
495 | if (abs_dst) | |
496 | xfree(abs_dst); | |
497 | if (tmp_dst) | |
498 | xfree(tmp_dst); | |
499 | return(err); | |
61e96248 | 500 | } |
501 | ||
396c147e | 502 | static int |
61e96248 | 503 | parse_args(const char **cpp, int *pflag, unsigned long *n_arg, |
504 | char **path1, char **path2) | |
505 | { | |
506 | const char *cmd, *cp = *cpp; | |
877546eb | 507 | char *cp2; |
ec2a033a | 508 | int base = 0; |
877546eb | 509 | long l; |
61e96248 | 510 | int i, cmdnum; |
511 | ||
512 | /* Skip leading whitespace */ | |
513 | cp = cp + strspn(cp, WHITESPACE); | |
514 | ||
515 | /* Ignore blank lines */ | |
516 | if (!*cp) | |
517 | return(-1); | |
518 | ||
519 | /* Figure out which command we have */ | |
184eed6a | 520 | for (i = 0; cmds[i].c; i++) { |
61e96248 | 521 | int cmdlen = strlen(cmds[i].c); |
522 | ||
523 | /* Check for command followed by whitespace */ | |
524 | if (!strncasecmp(cp, cmds[i].c, cmdlen) && | |
525 | strchr(WHITESPACE, cp[cmdlen])) { | |
526 | cp += cmdlen; | |
527 | cp = cp + strspn(cp, WHITESPACE); | |
528 | break; | |
529 | } | |
530 | } | |
531 | cmdnum = cmds[i].n; | |
532 | cmd = cmds[i].c; | |
533 | ||
534 | /* Special case */ | |
535 | if (*cp == '!') { | |
536 | cp++; | |
537 | cmdnum = I_SHELL; | |
538 | } else if (cmdnum == -1) { | |
539 | error("Invalid command."); | |
540 | return(-1); | |
541 | } | |
542 | ||
543 | /* Get arguments and parse flags */ | |
544 | *pflag = *n_arg = 0; | |
545 | *path1 = *path2 = NULL; | |
546 | switch (cmdnum) { | |
547 | case I_GET: | |
548 | case I_PUT: | |
549 | if (parse_getput_flags(&cp, pflag)) | |
550 | return(-1); | |
551 | /* Get first pathname (mandatory) */ | |
552 | if (get_pathname(&cp, path1)) | |
553 | return(-1); | |
554 | if (*path1 == NULL) { | |
555 | error("You must specify at least one path after a " | |
556 | "%s command.", cmd); | |
557 | return(-1); | |
558 | } | |
559 | /* Try to get second pathname (optional) */ | |
560 | if (get_pathname(&cp, path2)) | |
561 | return(-1); | |
61e96248 | 562 | break; |
563 | case I_RENAME: | |
3a7fe5ba | 564 | case I_SYMLINK: |
61e96248 | 565 | if (get_pathname(&cp, path1)) |
566 | return(-1); | |
567 | if (get_pathname(&cp, path2)) | |
568 | return(-1); | |
569 | if (!*path1 || !*path2) { | |
570 | error("You must specify two paths after a %s " | |
571 | "command.", cmd); | |
572 | return(-1); | |
573 | } | |
574 | break; | |
575 | case I_RM: | |
576 | case I_MKDIR: | |
577 | case I_RMDIR: | |
578 | case I_CHDIR: | |
579 | case I_LCHDIR: | |
580 | case I_LMKDIR: | |
581 | /* Get pathname (mandatory) */ | |
582 | if (get_pathname(&cp, path1)) | |
583 | return(-1); | |
584 | if (*path1 == NULL) { | |
2b87da3b | 585 | error("You must specify a path after a %s command.", |
61e96248 | 586 | cmd); |
587 | return(-1); | |
588 | } | |
589 | break; | |
590 | case I_LS: | |
591 | /* Path is optional */ | |
592 | if (get_pathname(&cp, path1)) | |
593 | return(-1); | |
594 | break; | |
595 | case I_LLS: | |
596 | case I_SHELL: | |
597 | /* Uses the rest of the line */ | |
598 | break; | |
599 | case I_LUMASK: | |
877546eb | 600 | base = 8; |
61e96248 | 601 | case I_CHMOD: |
ec2a033a | 602 | base = 8; |
61e96248 | 603 | case I_CHOWN: |
604 | case I_CHGRP: | |
605 | /* Get numeric arg (mandatory) */ | |
877546eb | 606 | l = strtol(cp, &cp2, base); |
607 | if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) && | |
608 | errno == ERANGE) || l < 0) { | |
61e96248 | 609 | error("You must supply a numeric argument " |
610 | "to the %s command.", cmd); | |
611 | return(-1); | |
612 | } | |
877546eb | 613 | cp = cp2; |
614 | *n_arg = l; | |
615 | if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp)) | |
616 | break; | |
617 | if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) { | |
61e96248 | 618 | error("You must supply a numeric argument " |
619 | "to the %s command.", cmd); | |
620 | return(-1); | |
621 | } | |
622 | cp += strspn(cp, WHITESPACE); | |
623 | ||
624 | /* Get pathname (mandatory) */ | |
625 | if (get_pathname(&cp, path1)) | |
626 | return(-1); | |
627 | if (*path1 == NULL) { | |
2b87da3b | 628 | error("You must specify a path after a %s command.", |
61e96248 | 629 | cmd); |
630 | return(-1); | |
631 | } | |
632 | break; | |
633 | case I_QUIT: | |
634 | case I_PWD: | |
635 | case I_LPWD: | |
636 | case I_HELP: | |
85cf5827 | 637 | case I_VERSION: |
61e96248 | 638 | break; |
639 | default: | |
640 | fatal("Command not implemented"); | |
641 | } | |
642 | ||
643 | *cpp = cp; | |
61e96248 | 644 | return(cmdnum); |
645 | } | |
646 | ||
396c147e | 647 | static int |
61e96248 | 648 | parse_dispatch_command(int in, int out, const char *cmd, char **pwd) |
649 | { | |
0426a3b4 | 650 | char *path1, *path2, *tmp; |
2e4fb373 | 651 | int pflag, cmdnum, i; |
61e96248 | 652 | unsigned long n_arg; |
653 | Attrib a, *aa; | |
7cc4cf0a | 654 | char path_buf[MAXPATHLEN]; |
a5ec8a3d | 655 | int err = 0; |
2e4fb373 | 656 | glob_t g; |
61e96248 | 657 | |
658 | path1 = path2 = NULL; | |
659 | cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2); | |
660 | ||
d50d9b63 | 661 | memset(&g, 0, sizeof(g)); |
662 | ||
61e96248 | 663 | /* Perform command */ |
664 | switch (cmdnum) { | |
665 | case -1: | |
666 | break; | |
667 | case I_GET: | |
d50d9b63 | 668 | err = process_get(in, out, path1, path2, *pwd, pflag); |
61e96248 | 669 | break; |
670 | case I_PUT: | |
d50d9b63 | 671 | err = process_put(in, out, path1, path2, *pwd, pflag); |
cd332296 | 672 | break; |
673 | case I_RENAME: | |
61e96248 | 674 | path1 = make_absolute(path1, *pwd); |
675 | path2 = make_absolute(path2, *pwd); | |
a5ec8a3d | 676 | err = do_rename(in, out, path1, path2); |
61e96248 | 677 | break; |
3a7fe5ba | 678 | case I_SYMLINK: |
679 | if (version < 3) { | |
680 | error("The server (version %d) does not support " | |
681 | "this operation", version); | |
682 | err = -1; | |
683 | } else { | |
684 | path2 = make_absolute(path2, *pwd); | |
685 | err = do_symlink(in, out, path1, path2); | |
686 | } | |
687 | break; | |
61e96248 | 688 | case I_RM: |
689 | path1 = make_absolute(path1, *pwd); | |
2e4fb373 | 690 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
184eed6a | 691 | for (i = 0; g.gl_pathv[i]; i++) { |
2e4fb373 | 692 | printf("Removing %s\n", g.gl_pathv[i]); |
693 | if (do_rm(in, out, g.gl_pathv[i]) == -1) | |
694 | err = -1; | |
695 | } | |
61e96248 | 696 | break; |
697 | case I_MKDIR: | |
698 | path1 = make_absolute(path1, *pwd); | |
699 | attrib_clear(&a); | |
700 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; | |
701 | a.perm = 0777; | |
a5ec8a3d | 702 | err = do_mkdir(in, out, path1, &a); |
61e96248 | 703 | break; |
704 | case I_RMDIR: | |
705 | path1 = make_absolute(path1, *pwd); | |
a5ec8a3d | 706 | err = do_rmdir(in, out, path1); |
61e96248 | 707 | break; |
708 | case I_CHDIR: | |
709 | path1 = make_absolute(path1, *pwd); | |
a5ec8a3d | 710 | if ((tmp = do_realpath(in, out, path1)) == NULL) { |
711 | err = 1; | |
0426a3b4 | 712 | break; |
a5ec8a3d | 713 | } |
d50d9b63 | 714 | if ((aa = do_stat(in, out, tmp, 0)) == NULL) { |
0426a3b4 | 715 | xfree(tmp); |
a5ec8a3d | 716 | err = 1; |
0426a3b4 | 717 | break; |
718 | } | |
719 | if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { | |
720 | error("Can't change directory: Can't check target"); | |
721 | xfree(tmp); | |
a5ec8a3d | 722 | err = 1; |
0426a3b4 | 723 | break; |
724 | } | |
725 | if (!S_ISDIR(aa->perm)) { | |
726 | error("Can't change directory: \"%s\" is not " | |
727 | "a directory", tmp); | |
728 | xfree(tmp); | |
a5ec8a3d | 729 | err = 1; |
0426a3b4 | 730 | break; |
731 | } | |
61e96248 | 732 | xfree(*pwd); |
0426a3b4 | 733 | *pwd = tmp; |
61e96248 | 734 | break; |
735 | case I_LS: | |
0426a3b4 | 736 | if (!path1) { |
737 | do_ls(in, out, *pwd); | |
738 | break; | |
739 | } | |
61e96248 | 740 | path1 = make_absolute(path1, *pwd); |
0426a3b4 | 741 | if ((tmp = do_realpath(in, out, path1)) == NULL) |
742 | break; | |
743 | xfree(path1); | |
744 | path1 = tmp; | |
d50d9b63 | 745 | if ((aa = do_stat(in, out, path1, 0)) == NULL) |
0426a3b4 | 746 | break; |
f55d1b5f | 747 | if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && |
0426a3b4 | 748 | !S_ISDIR(aa->perm)) { |
749 | error("Can't ls: \"%s\" is not a directory", path1); | |
750 | break; | |
751 | } | |
752 | do_ls(in, out, path1); | |
61e96248 | 753 | break; |
754 | case I_LCHDIR: | |
a5ec8a3d | 755 | if (chdir(path1) == -1) { |
61e96248 | 756 | error("Couldn't change local directory to " |
757 | "\"%s\": %s", path1, strerror(errno)); | |
a5ec8a3d | 758 | err = 1; |
759 | } | |
61e96248 | 760 | break; |
761 | case I_LMKDIR: | |
a5ec8a3d | 762 | if (mkdir(path1, 0777) == -1) { |
0426a3b4 | 763 | error("Couldn't create local directory " |
61e96248 | 764 | "\"%s\": %s", path1, strerror(errno)); |
a5ec8a3d | 765 | err = 1; |
766 | } | |
61e96248 | 767 | break; |
768 | case I_LLS: | |
769 | local_do_ls(cmd); | |
770 | break; | |
771 | case I_SHELL: | |
772 | local_do_shell(cmd); | |
773 | break; | |
774 | case I_LUMASK: | |
775 | umask(n_arg); | |
877546eb | 776 | printf("Local umask: %03lo\n", n_arg); |
61e96248 | 777 | break; |
778 | case I_CHMOD: | |
779 | path1 = make_absolute(path1, *pwd); | |
780 | attrib_clear(&a); | |
781 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; | |
782 | a.perm = n_arg; | |
2e4fb373 | 783 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
184eed6a | 784 | for (i = 0; g.gl_pathv[i]; i++) { |
2e4fb373 | 785 | printf("Changing mode on %s\n", g.gl_pathv[i]); |
786 | do_setstat(in, out, g.gl_pathv[i], &a); | |
787 | } | |
ec2a033a | 788 | break; |
61e96248 | 789 | case I_CHOWN: |
790 | path1 = make_absolute(path1, *pwd); | |
2e4fb373 | 791 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
184eed6a | 792 | for (i = 0; g.gl_pathv[i]; i++) { |
d50d9b63 | 793 | if (!(aa = do_stat(in, out, g.gl_pathv[i], 0))) |
2e4fb373 | 794 | continue; |
795 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { | |
796 | error("Can't get current ownership of " | |
797 | "remote file \"%s\"", g.gl_pathv[i]); | |
798 | continue; | |
799 | } | |
800 | printf("Changing owner on %s\n", g.gl_pathv[i]); | |
801 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; | |
802 | aa->uid = n_arg; | |
803 | do_setstat(in, out, g.gl_pathv[i], aa); | |
61e96248 | 804 | } |
61e96248 | 805 | break; |
806 | case I_CHGRP: | |
807 | path1 = make_absolute(path1, *pwd); | |
2e4fb373 | 808 | remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g); |
184eed6a | 809 | for (i = 0; g.gl_pathv[i]; i++) { |
d50d9b63 | 810 | if (!(aa = do_stat(in, out, g.gl_pathv[i], 0))) |
2e4fb373 | 811 | continue; |
812 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { | |
813 | error("Can't get current ownership of " | |
814 | "remote file \"%s\"", g.gl_pathv[i]); | |
815 | continue; | |
816 | } | |
817 | printf("Changing group on %s\n", g.gl_pathv[i]); | |
818 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; | |
819 | aa->gid = n_arg; | |
820 | do_setstat(in, out, g.gl_pathv[i], aa); | |
61e96248 | 821 | } |
61e96248 | 822 | break; |
823 | case I_PWD: | |
824 | printf("Remote working directory: %s\n", *pwd); | |
825 | break; | |
826 | case I_LPWD: | |
827 | if (!getcwd(path_buf, sizeof(path_buf))) | |
54b974dc | 828 | error("Couldn't get local cwd: %s", |
61e96248 | 829 | strerror(errno)); |
830 | else | |
831 | printf("Local working directory: %s\n", | |
832 | path_buf); | |
833 | break; | |
834 | case I_QUIT: | |
835 | return(-1); | |
836 | case I_HELP: | |
837 | help(); | |
838 | break; | |
85cf5827 | 839 | case I_VERSION: |
840 | printf("SFTP protocol version %d\n", version); | |
841 | break; | |
61e96248 | 842 | default: |
843 | fatal("%d is not implemented", cmdnum); | |
844 | } | |
845 | ||
d50d9b63 | 846 | if (g.gl_pathc) |
847 | globfree(&g); | |
61e96248 | 848 | if (path1) |
849 | xfree(path1); | |
850 | if (path2) | |
851 | xfree(path2); | |
a5ec8a3d | 852 | |
853 | /* If an error occurs in batch mode we should abort. */ | |
854 | if (infile != stdin && err > 0) | |
855 | return -1; | |
856 | ||
61e96248 | 857 | return(0); |
858 | } | |
859 | ||
860 | void | |
edeeab1e | 861 | interactive_loop(int fd_in, int fd_out, char *file1, char *file2) |
61e96248 | 862 | { |
863 | char *pwd; | |
edeeab1e | 864 | char *dir = NULL; |
61e96248 | 865 | char cmd[2048]; |
866 | ||
3a7fe5ba | 867 | version = do_init(fd_in, fd_out); |
868 | if (version == -1) | |
869 | fatal("Couldn't initialise connection to server"); | |
870 | ||
61e96248 | 871 | pwd = do_realpath(fd_in, fd_out, "."); |
872 | if (pwd == NULL) | |
873 | fatal("Need cwd"); | |
874 | ||
edeeab1e | 875 | if (file1 != NULL) { |
876 | dir = xstrdup(file1); | |
877 | dir = make_absolute(dir, pwd); | |
878 | ||
879 | if (remote_is_dir(fd_in, fd_out, dir) && file2 == NULL) { | |
880 | printf("Changing to: %s\n", dir); | |
881 | snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); | |
882 | parse_dispatch_command(fd_in, fd_out, cmd, &pwd); | |
883 | } else { | |
884 | if (file2 == NULL) | |
885 | snprintf(cmd, sizeof cmd, "get %s", dir); | |
886 | else | |
887 | snprintf(cmd, sizeof cmd, "get %s %s", dir, | |
888 | file2); | |
889 | ||
890 | parse_dispatch_command(fd_in, fd_out, cmd, &pwd); | |
891 | return; | |
892 | } | |
893 | } | |
b81c369b | 894 | #if HAVE_SETVBUF |
0426a3b4 | 895 | setvbuf(stdout, NULL, _IOLBF, 0); |
a5ec8a3d | 896 | setvbuf(infile, NULL, _IOLBF, 0); |
b81c369b | 897 | #else |
898 | setlinebuf(stdout); | |
899 | setlinebuf(infile); | |
900 | #endif | |
61e96248 | 901 | |
184eed6a | 902 | for (;;) { |
61e96248 | 903 | char *cp; |
904 | ||
905 | printf("sftp> "); | |
906 | ||
907 | /* XXX: use libedit */ | |
a5ec8a3d | 908 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { |
61e96248 | 909 | printf("\n"); |
910 | break; | |
a5ec8a3d | 911 | } else if (infile != stdin) /* Bluff typing */ |
912 | printf("%s", cmd); | |
913 | ||
61e96248 | 914 | cp = strrchr(cmd, '\n'); |
915 | if (cp) | |
916 | *cp = '\0'; | |
a5ec8a3d | 917 | |
61e96248 | 918 | if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd)) |
919 | break; | |
920 | } | |
921 | xfree(pwd); | |
922 | } |