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