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