]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2001,2002 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 | ||
25 | /* XXX: recursive operations */ | |
26 | ||
27 | #include "includes.h" | |
28 | RCSID("$OpenBSD: sftp-int.c,v 1.61 2003/07/19 00:45:53 djm Exp $"); | |
29 | ||
30 | #include "buffer.h" | |
31 | #include "xmalloc.h" | |
32 | #include "log.h" | |
33 | #include "pathnames.h" | |
34 | ||
35 | #include "sftp.h" | |
36 | #include "sftp-common.h" | |
37 | #include "sftp-glob.h" | |
38 | #include "sftp-client.h" | |
39 | #include "sftp-int.h" | |
40 | ||
41 | /* File to read commands from */ | |
42 | extern FILE *infile; | |
43 | ||
44 | /* Size of buffer used when copying files */ | |
45 | extern size_t copy_buffer_len; | |
46 | ||
47 | /* Number of concurrent outstanding requests */ | |
48 | extern int num_requests; | |
49 | ||
50 | /* This is set to 0 if the progressmeter is not desired. */ | |
51 | int showprogress = 1; | |
52 | ||
53 | /* Seperators for interactive commands */ | |
54 | #define WHITESPACE " \t\r\n" | |
55 | ||
56 | /* Define what type of ls view (0 - multi-column) */ | |
57 | #define LONG_VIEW 1 /* Full view ala ls -l */ | |
58 | #define SHORT_VIEW 2 /* Single row view ala ls -1 */ | |
59 | ||
60 | /* Commands for interactive mode */ | |
61 | #define I_CHDIR 1 | |
62 | #define I_CHGRP 2 | |
63 | #define I_CHMOD 3 | |
64 | #define I_CHOWN 4 | |
65 | #define I_GET 5 | |
66 | #define I_HELP 6 | |
67 | #define I_LCHDIR 7 | |
68 | #define I_LLS 8 | |
69 | #define I_LMKDIR 9 | |
70 | #define I_LPWD 10 | |
71 | #define I_LS 11 | |
72 | #define I_LUMASK 12 | |
73 | #define I_MKDIR 13 | |
74 | #define I_PUT 14 | |
75 | #define I_PWD 15 | |
76 | #define I_QUIT 16 | |
77 | #define I_RENAME 17 | |
78 | #define I_RM 18 | |
79 | #define I_RMDIR 19 | |
80 | #define I_SHELL 20 | |
81 | #define I_SYMLINK 21 | |
82 | #define I_VERSION 22 | |
83 | #define I_PROGRESS 23 | |
84 | ||
85 | struct CMD { | |
86 | const char *c; | |
87 | const int n; | |
88 | }; | |
89 | ||
90 | static const struct CMD cmds[] = { | |
91 | { "bye", I_QUIT }, | |
92 | { "cd", I_CHDIR }, | |
93 | { "chdir", I_CHDIR }, | |
94 | { "chgrp", I_CHGRP }, | |
95 | { "chmod", I_CHMOD }, | |
96 | { "chown", I_CHOWN }, | |
97 | { "dir", I_LS }, | |
98 | { "exit", I_QUIT }, | |
99 | { "get", I_GET }, | |
100 | { "mget", I_GET }, | |
101 | { "help", I_HELP }, | |
102 | { "lcd", I_LCHDIR }, | |
103 | { "lchdir", I_LCHDIR }, | |
104 | { "lls", I_LLS }, | |
105 | { "lmkdir", I_LMKDIR }, | |
106 | { "ln", I_SYMLINK }, | |
107 | { "lpwd", I_LPWD }, | |
108 | { "ls", I_LS }, | |
109 | { "lumask", I_LUMASK }, | |
110 | { "mkdir", I_MKDIR }, | |
111 | { "progress", I_PROGRESS }, | |
112 | { "put", I_PUT }, | |
113 | { "mput", I_PUT }, | |
114 | { "pwd", I_PWD }, | |
115 | { "quit", I_QUIT }, | |
116 | { "rename", I_RENAME }, | |
117 | { "rm", I_RM }, | |
118 | { "rmdir", I_RMDIR }, | |
119 | { "symlink", I_SYMLINK }, | |
120 | { "version", I_VERSION }, | |
121 | { "!", I_SHELL }, | |
122 | { "?", I_HELP }, | |
123 | { NULL, -1} | |
124 | }; | |
125 | ||
126 | static void | |
127 | help(void) | |
128 | { | |
129 | printf("Available commands:\n"); | |
130 | printf("cd path Change remote directory to 'path'\n"); | |
131 | printf("lcd path Change local directory to 'path'\n"); | |
132 | printf("chgrp grp path Change group of file 'path' to 'grp'\n"); | |
133 | printf("chmod mode path Change permissions of file 'path' to 'mode'\n"); | |
134 | printf("chown own path Change owner of file 'path' to 'own'\n"); | |
135 | printf("help Display this help text\n"); | |
136 | printf("get remote-path [local-path] Download file\n"); | |
137 | printf("lls [ls-options [path]] Display local directory listing\n"); | |
138 | printf("ln oldpath newpath Symlink remote file\n"); | |
139 | printf("lmkdir path Create local directory\n"); | |
140 | printf("lpwd Print local working directory\n"); | |
141 | printf("ls [path] Display remote directory listing\n"); | |
142 | printf("lumask umask Set local umask to 'umask'\n"); | |
143 | printf("mkdir path Create remote directory\n"); | |
144 | printf("progress Toggle display of progress meter\n"); | |
145 | printf("put local-path [remote-path] Upload file\n"); | |
146 | printf("pwd Display remote working directory\n"); | |
147 | printf("exit Quit sftp\n"); | |
148 | printf("quit Quit sftp\n"); | |
149 | printf("rename oldpath newpath Rename remote file\n"); | |
150 | printf("rmdir path Remove remote directory\n"); | |
151 | printf("rm path Delete remote file\n"); | |
152 | printf("symlink oldpath newpath Symlink remote file\n"); | |
153 | printf("version Show SFTP version\n"); | |
154 | printf("!command Execute 'command' in local shell\n"); | |
155 | printf("! Escape to local shell\n"); | |
156 | printf("? Synonym for help\n"); | |
157 | } | |
158 | ||
159 | static void | |
160 | local_do_shell(const char *args) | |
161 | { | |
162 | int status; | |
163 | char *shell; | |
164 | pid_t pid; | |
165 | ||
166 | if (!*args) | |
167 | args = NULL; | |
168 | ||
169 | if ((shell = getenv("SHELL")) == NULL) | |
170 | shell = _PATH_BSHELL; | |
171 | ||
172 | if ((pid = fork()) == -1) | |
173 | fatal("Couldn't fork: %s", strerror(errno)); | |
174 | ||
175 | if (pid == 0) { | |
176 | /* XXX: child has pipe fds to ssh subproc open - issue? */ | |
177 | if (args) { | |
178 | debug3("Executing %s -c \"%s\"", shell, args); | |
179 | execl(shell, shell, "-c", args, (char *)NULL); | |
180 | } else { | |
181 | debug3("Executing %s", shell); | |
182 | execl(shell, shell, (char *)NULL); | |
183 | } | |
184 | fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell, | |
185 | strerror(errno)); | |
186 | _exit(1); | |
187 | } | |
188 | while (waitpid(pid, &status, 0) == -1) | |
189 | if (errno != EINTR) | |
190 | fatal("Couldn't wait for child: %s", strerror(errno)); | |
191 | if (!WIFEXITED(status)) | |
192 | error("Shell exited abormally"); | |
193 | else if (WEXITSTATUS(status)) | |
194 | error("Shell exited with status %d", WEXITSTATUS(status)); | |
195 | } | |
196 | ||
197 | static void | |
198 | local_do_ls(const char *args) | |
199 | { | |
200 | if (!args || !*args) | |
201 | local_do_shell(_PATH_LS); | |
202 | else { | |
203 | int len = strlen(_PATH_LS " ") + strlen(args) + 1; | |
204 | char *buf = xmalloc(len); | |
205 | ||
206 | /* XXX: quoting - rip quoting code from ftp? */ | |
207 | snprintf(buf, len, _PATH_LS " %s", args); | |
208 | local_do_shell(buf); | |
209 | xfree(buf); | |
210 | } | |
211 | } | |
212 | ||
213 | /* Strip one path (usually the pwd) from the start of another */ | |
214 | static char * | |
215 | path_strip(char *path, char *strip) | |
216 | { | |
217 | size_t len; | |
218 | ||
219 | if (strip == NULL) | |
220 | return (xstrdup(path)); | |
221 | ||
222 | len = strlen(strip); | |
223 | if (strip != NULL && strncmp(path, strip, len) == 0) { | |
224 | if (strip[len - 1] != '/' && path[len] == '/') | |
225 | len++; | |
226 | return (xstrdup(path + len)); | |
227 | } | |
228 | ||
229 | return (xstrdup(path)); | |
230 | } | |
231 | ||
232 | static char * | |
233 | path_append(char *p1, char *p2) | |
234 | { | |
235 | char *ret; | |
236 | int len = strlen(p1) + strlen(p2) + 2; | |
237 | ||
238 | ret = xmalloc(len); | |
239 | strlcpy(ret, p1, len); | |
240 | if (p1[strlen(p1) - 1] != '/') | |
241 | strlcat(ret, "/", len); | |
242 | strlcat(ret, p2, len); | |
243 | ||
244 | return(ret); | |
245 | } | |
246 | ||
247 | static char * | |
248 | make_absolute(char *p, char *pwd) | |
249 | { | |
250 | char *abs; | |
251 | ||
252 | /* Derelativise */ | |
253 | if (p && p[0] != '/') { | |
254 | abs = path_append(pwd, p); | |
255 | xfree(p); | |
256 | return(abs); | |
257 | } else | |
258 | return(p); | |
259 | } | |
260 | ||
261 | static int | |
262 | infer_path(const char *p, char **ifp) | |
263 | { | |
264 | char *cp; | |
265 | ||
266 | cp = strrchr(p, '/'); | |
267 | if (cp == NULL) { | |
268 | *ifp = xstrdup(p); | |
269 | return(0); | |
270 | } | |
271 | ||
272 | if (!cp[1]) { | |
273 | error("Invalid path"); | |
274 | return(-1); | |
275 | } | |
276 | ||
277 | *ifp = xstrdup(cp + 1); | |
278 | return(0); | |
279 | } | |
280 | ||
281 | static int | |
282 | parse_getput_flags(const char **cpp, int *pflag) | |
283 | { | |
284 | const char *cp = *cpp; | |
285 | ||
286 | /* Check for flags */ | |
287 | if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) { | |
288 | switch (cp[1]) { | |
289 | case 'p': | |
290 | case 'P': | |
291 | *pflag = 1; | |
292 | break; | |
293 | default: | |
294 | error("Invalid flag -%c", cp[1]); | |
295 | return(-1); | |
296 | } | |
297 | cp += 2; | |
298 | *cpp = cp + strspn(cp, WHITESPACE); | |
299 | } | |
300 | ||
301 | return(0); | |
302 | } | |
303 | ||
304 | static int | |
305 | parse_ls_flags(const char **cpp, int *lflag) | |
306 | { | |
307 | const char *cp = *cpp; | |
308 | ||
309 | /* Check for flags */ | |
310 | if (cp++[0] == '-') { | |
311 | for(; strchr(WHITESPACE, *cp) == NULL; cp++) { | |
312 | switch (*cp) { | |
313 | case 'l': | |
314 | *lflag = LONG_VIEW; | |
315 | break; | |
316 | case '1': | |
317 | *lflag = SHORT_VIEW; | |
318 | break; | |
319 | default: | |
320 | error("Invalid flag -%c", *cp); | |
321 | return(-1); | |
322 | } | |
323 | } | |
324 | *cpp = cp + strspn(cp, WHITESPACE); | |
325 | } | |
326 | ||
327 | return(0); | |
328 | } | |
329 | ||
330 | static int | |
331 | get_pathname(const char **cpp, char **path) | |
332 | { | |
333 | const char *cp = *cpp, *end; | |
334 | char quot; | |
335 | int i, j; | |
336 | ||
337 | cp += strspn(cp, WHITESPACE); | |
338 | if (!*cp) { | |
339 | *cpp = cp; | |
340 | *path = NULL; | |
341 | return (0); | |
342 | } | |
343 | ||
344 | *path = xmalloc(strlen(cp) + 1); | |
345 | ||
346 | /* Check for quoted filenames */ | |
347 | if (*cp == '\"' || *cp == '\'') { | |
348 | quot = *cp++; | |
349 | ||
350 | /* Search for terminating quote, unescape some chars */ | |
351 | for (i = j = 0; i <= strlen(cp); i++) { | |
352 | if (cp[i] == quot) { /* Found quote */ | |
353 | (*path)[j] = '\0'; | |
354 | break; | |
355 | } | |
356 | if (cp[i] == '\0') { /* End of string */ | |
357 | error("Unterminated quote"); | |
358 | goto fail; | |
359 | } | |
360 | if (cp[i] == '\\') { /* Escaped characters */ | |
361 | i++; | |
362 | if (cp[i] != '\'' && cp[i] != '\"' && | |
363 | cp[i] != '\\') { | |
364 | error("Bad escaped character '\%c'", | |
365 | cp[i]); | |
366 | goto fail; | |
367 | } | |
368 | } | |
369 | (*path)[j++] = cp[i]; | |
370 | } | |
371 | ||
372 | if (j == 0) { | |
373 | error("Empty quotes"); | |
374 | goto fail; | |
375 | } | |
376 | *cpp = cp + i + strspn(cp + i, WHITESPACE); | |
377 | } else { | |
378 | /* Read to end of filename */ | |
379 | end = strpbrk(cp, WHITESPACE); | |
380 | if (end == NULL) | |
381 | end = strchr(cp, '\0'); | |
382 | *cpp = end + strspn(end, WHITESPACE); | |
383 | ||
384 | memcpy(*path, cp, end - cp); | |
385 | (*path)[end - cp] = '\0'; | |
386 | } | |
387 | return (0); | |
388 | ||
389 | fail: | |
390 | xfree(*path); | |
391 | *path = NULL; | |
392 | return (-1); | |
393 | } | |
394 | ||
395 | static int | |
396 | is_dir(char *path) | |
397 | { | |
398 | struct stat sb; | |
399 | ||
400 | /* XXX: report errors? */ | |
401 | if (stat(path, &sb) == -1) | |
402 | return(0); | |
403 | ||
404 | return(sb.st_mode & S_IFDIR); | |
405 | } | |
406 | ||
407 | static int | |
408 | is_reg(char *path) | |
409 | { | |
410 | struct stat sb; | |
411 | ||
412 | if (stat(path, &sb) == -1) | |
413 | fatal("stat %s: %s", path, strerror(errno)); | |
414 | ||
415 | return(S_ISREG(sb.st_mode)); | |
416 | } | |
417 | ||
418 | static int | |
419 | remote_is_dir(struct sftp_conn *conn, char *path) | |
420 | { | |
421 | Attrib *a; | |
422 | ||
423 | /* XXX: report errors? */ | |
424 | if ((a = do_stat(conn, path, 1)) == NULL) | |
425 | return(0); | |
426 | if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) | |
427 | return(0); | |
428 | return(a->perm & S_IFDIR); | |
429 | } | |
430 | ||
431 | static int | |
432 | process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) | |
433 | { | |
434 | char *abs_src = NULL; | |
435 | char *abs_dst = NULL; | |
436 | char *tmp; | |
437 | glob_t g; | |
438 | int err = 0; | |
439 | int i; | |
440 | ||
441 | abs_src = xstrdup(src); | |
442 | abs_src = make_absolute(abs_src, pwd); | |
443 | ||
444 | memset(&g, 0, sizeof(g)); | |
445 | debug3("Looking up %s", abs_src); | |
446 | if (remote_glob(conn, abs_src, 0, NULL, &g)) { | |
447 | error("File \"%s\" not found.", abs_src); | |
448 | err = -1; | |
449 | goto out; | |
450 | } | |
451 | ||
452 | /* If multiple matches, dst must be a directory or unspecified */ | |
453 | if (g.gl_matchc > 1 && dst && !is_dir(dst)) { | |
454 | error("Multiple files match, but \"%s\" is not a directory", | |
455 | dst); | |
456 | err = -1; | |
457 | goto out; | |
458 | } | |
459 | ||
460 | for (i = 0; g.gl_pathv[i]; i++) { | |
461 | if (infer_path(g.gl_pathv[i], &tmp)) { | |
462 | err = -1; | |
463 | goto out; | |
464 | } | |
465 | ||
466 | if (g.gl_matchc == 1 && dst) { | |
467 | /* If directory specified, append filename */ | |
468 | if (is_dir(dst)) { | |
469 | if (infer_path(g.gl_pathv[0], &tmp)) { | |
470 | err = 1; | |
471 | goto out; | |
472 | } | |
473 | abs_dst = path_append(dst, tmp); | |
474 | xfree(tmp); | |
475 | } else | |
476 | abs_dst = xstrdup(dst); | |
477 | } else if (dst) { | |
478 | abs_dst = path_append(dst, tmp); | |
479 | xfree(tmp); | |
480 | } else | |
481 | abs_dst = tmp; | |
482 | ||
483 | printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst); | |
484 | if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1) | |
485 | err = -1; | |
486 | xfree(abs_dst); | |
487 | abs_dst = NULL; | |
488 | } | |
489 | ||
490 | out: | |
491 | xfree(abs_src); | |
492 | if (abs_dst) | |
493 | xfree(abs_dst); | |
494 | globfree(&g); | |
495 | return(err); | |
496 | } | |
497 | ||
498 | static int | |
499 | process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) | |
500 | { | |
501 | char *tmp_dst = NULL; | |
502 | char *abs_dst = NULL; | |
503 | char *tmp; | |
504 | glob_t g; | |
505 | int err = 0; | |
506 | int i; | |
507 | ||
508 | if (dst) { | |
509 | tmp_dst = xstrdup(dst); | |
510 | tmp_dst = make_absolute(tmp_dst, pwd); | |
511 | } | |
512 | ||
513 | memset(&g, 0, sizeof(g)); | |
514 | debug3("Looking up %s", src); | |
515 | if (glob(src, 0, NULL, &g)) { | |
516 | error("File \"%s\" not found.", src); | |
517 | err = -1; | |
518 | goto out; | |
519 | } | |
520 | ||
521 | /* If multiple matches, dst may be directory or unspecified */ | |
522 | if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) { | |
523 | error("Multiple files match, but \"%s\" is not a directory", | |
524 | tmp_dst); | |
525 | err = -1; | |
526 | goto out; | |
527 | } | |
528 | ||
529 | for (i = 0; g.gl_pathv[i]; i++) { | |
530 | if (!is_reg(g.gl_pathv[i])) { | |
531 | error("skipping non-regular file %s", | |
532 | g.gl_pathv[i]); | |
533 | continue; | |
534 | } | |
535 | if (infer_path(g.gl_pathv[i], &tmp)) { | |
536 | err = -1; | |
537 | goto out; | |
538 | } | |
539 | ||
540 | if (g.gl_matchc == 1 && tmp_dst) { | |
541 | /* If directory specified, append filename */ | |
542 | if (remote_is_dir(conn, tmp_dst)) { | |
543 | if (infer_path(g.gl_pathv[0], &tmp)) { | |
544 | err = 1; | |
545 | goto out; | |
546 | } | |
547 | abs_dst = path_append(tmp_dst, tmp); | |
548 | xfree(tmp); | |
549 | } else | |
550 | abs_dst = xstrdup(tmp_dst); | |
551 | ||
552 | } else if (tmp_dst) { | |
553 | abs_dst = path_append(tmp_dst, tmp); | |
554 | xfree(tmp); | |
555 | } else | |
556 | abs_dst = make_absolute(tmp, pwd); | |
557 | ||
558 | printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst); | |
559 | if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1) | |
560 | err = -1; | |
561 | } | |
562 | ||
563 | out: | |
564 | if (abs_dst) | |
565 | xfree(abs_dst); | |
566 | if (tmp_dst) | |
567 | xfree(tmp_dst); | |
568 | globfree(&g); | |
569 | return(err); | |
570 | } | |
571 | ||
572 | static int | |
573 | sdirent_comp(const void *aa, const void *bb) | |
574 | { | |
575 | SFTP_DIRENT *a = *(SFTP_DIRENT **)aa; | |
576 | SFTP_DIRENT *b = *(SFTP_DIRENT **)bb; | |
577 | ||
578 | return (strcmp(a->filename, b->filename)); | |
579 | } | |
580 | ||
581 | /* sftp ls.1 replacement for directories */ | |
582 | static int | |
583 | do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) | |
584 | { | |
585 | int n, c = 1, colspace = 0, columns = 1; | |
586 | SFTP_DIRENT **d; | |
587 | ||
588 | if ((n = do_readdir(conn, path, &d)) != 0) | |
589 | return (n); | |
590 | ||
591 | if (!(lflag & SHORT_VIEW)) { | |
592 | int m = 0, width = 80; | |
593 | struct winsize ws; | |
594 | ||
595 | /* Count entries for sort and find longest filename */ | |
596 | for (n = 0; d[n] != NULL; n++) | |
597 | m = MAX(m, strlen(d[n]->filename)); | |
598 | ||
599 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) | |
600 | width = ws.ws_col; | |
601 | ||
602 | columns = width / (m + 2); | |
603 | colspace = width / columns; | |
604 | } | |
605 | ||
606 | qsort(d, n, sizeof(*d), sdirent_comp); | |
607 | ||
608 | for (n = 0; d[n] != NULL; n++) { | |
609 | char *tmp, *fname; | |
610 | ||
611 | tmp = path_append(path, d[n]->filename); | |
612 | fname = path_strip(tmp, strip_path); | |
613 | xfree(tmp); | |
614 | ||
615 | if (lflag & LONG_VIEW) { | |
616 | char *lname; | |
617 | struct stat sb; | |
618 | ||
619 | memset(&sb, 0, sizeof(sb)); | |
620 | attrib_to_stat(&d[n]->a, &sb); | |
621 | lname = ls_file(fname, &sb, 1); | |
622 | printf("%s\n", lname); | |
623 | xfree(lname); | |
624 | } else { | |
625 | printf("%-*s", colspace, fname); | |
626 | if (c >= columns) { | |
627 | printf("\n"); | |
628 | c = 1; | |
629 | } else | |
630 | c++; | |
631 | } | |
632 | ||
633 | xfree(fname); | |
634 | } | |
635 | ||
636 | if (!(lflag & LONG_VIEW) && (c != 1)) | |
637 | printf("\n"); | |
638 | ||
639 | free_sftp_dirents(d); | |
640 | return (0); | |
641 | } | |
642 | ||
643 | /* sftp ls.1 replacement which handles path globs */ | |
644 | static int | |
645 | do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, | |
646 | int lflag) | |
647 | { | |
648 | glob_t g; | |
649 | int i, c = 1, colspace = 0, columns = 1; | |
650 | Attrib *a; | |
651 | ||
652 | memset(&g, 0, sizeof(g)); | |
653 | ||
654 | if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE, | |
655 | NULL, &g)) { | |
656 | error("Can't ls: \"%s\" not found", path); | |
657 | return (-1); | |
658 | } | |
659 | ||
660 | /* | |
661 | * If the glob returns a single match, which is the same as the | |
662 | * input glob, and it is a directory, then just list its contents | |
663 | */ | |
664 | if (g.gl_pathc == 1 && | |
665 | strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) { | |
666 | if ((a = do_lstat(conn, path, 1)) == NULL) { | |
667 | globfree(&g); | |
668 | return (-1); | |
669 | } | |
670 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && | |
671 | S_ISDIR(a->perm)) { | |
672 | globfree(&g); | |
673 | return (do_ls_dir(conn, path, strip_path, lflag)); | |
674 | } | |
675 | } | |
676 | ||
677 | if (!(lflag & SHORT_VIEW)) { | |
678 | int m = 0, width = 80; | |
679 | struct winsize ws; | |
680 | ||
681 | /* Count entries for sort and find longest filename */ | |
682 | for (i = 0; g.gl_pathv[i]; i++) | |
683 | m = MAX(m, strlen(g.gl_pathv[i])); | |
684 | ||
685 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) | |
686 | width = ws.ws_col; | |
687 | ||
688 | columns = width / (m + 2); | |
689 | colspace = width / columns; | |
690 | } | |
691 | ||
692 | for (i = 0; g.gl_pathv[i]; i++) { | |
693 | char *fname; | |
694 | ||
695 | fname = path_strip(g.gl_pathv[i], strip_path); | |
696 | ||
697 | if (lflag & LONG_VIEW) { | |
698 | char *lname; | |
699 | struct stat sb; | |
700 | ||
701 | /* | |
702 | * XXX: this is slow - 1 roundtrip per path | |
703 | * A solution to this is to fork glob() and | |
704 | * build a sftp specific version which keeps the | |
705 | * attribs (which currently get thrown away) | |
706 | * that the server returns as well as the filenames. | |
707 | */ | |
708 | memset(&sb, 0, sizeof(sb)); | |
709 | a = do_lstat(conn, g.gl_pathv[i], 1); | |
710 | if (a != NULL) | |
711 | attrib_to_stat(a, &sb); | |
712 | lname = ls_file(fname, &sb, 1); | |
713 | printf("%s\n", lname); | |
714 | xfree(lname); | |
715 | } else { | |
716 | printf("%-*s", colspace, fname); | |
717 | if (c >= columns) { | |
718 | printf("\n"); | |
719 | c = 1; | |
720 | } else | |
721 | c++; | |
722 | } | |
723 | xfree(fname); | |
724 | } | |
725 | ||
726 | if (!(lflag & LONG_VIEW) && (c != 1)) | |
727 | printf("\n"); | |
728 | ||
729 | if (g.gl_pathc) | |
730 | globfree(&g); | |
731 | ||
732 | return (0); | |
733 | } | |
734 | ||
735 | static int | |
736 | parse_args(const char **cpp, int *pflag, int *lflag, int *iflag, | |
737 | unsigned long *n_arg, char **path1, char **path2) | |
738 | { | |
739 | const char *cmd, *cp = *cpp; | |
740 | char *cp2; | |
741 | int base = 0; | |
742 | long l; | |
743 | int i, cmdnum; | |
744 | ||
745 | /* Skip leading whitespace */ | |
746 | cp = cp + strspn(cp, WHITESPACE); | |
747 | ||
748 | /* Ignore blank lines and lines which begin with comment '#' char */ | |
749 | if (*cp == '\0' || *cp == '#') | |
750 | return (0); | |
751 | ||
752 | /* Check for leading '-' (disable error processing) */ | |
753 | *iflag = 0; | |
754 | if (*cp == '-') { | |
755 | *iflag = 1; | |
756 | cp++; | |
757 | } | |
758 | ||
759 | /* Figure out which command we have */ | |
760 | for (i = 0; cmds[i].c; i++) { | |
761 | int cmdlen = strlen(cmds[i].c); | |
762 | ||
763 | /* Check for command followed by whitespace */ | |
764 | if (!strncasecmp(cp, cmds[i].c, cmdlen) && | |
765 | strchr(WHITESPACE, cp[cmdlen])) { | |
766 | cp += cmdlen; | |
767 | cp = cp + strspn(cp, WHITESPACE); | |
768 | break; | |
769 | } | |
770 | } | |
771 | cmdnum = cmds[i].n; | |
772 | cmd = cmds[i].c; | |
773 | ||
774 | /* Special case */ | |
775 | if (*cp == '!') { | |
776 | cp++; | |
777 | cmdnum = I_SHELL; | |
778 | } else if (cmdnum == -1) { | |
779 | error("Invalid command."); | |
780 | return (-1); | |
781 | } | |
782 | ||
783 | /* Get arguments and parse flags */ | |
784 | *lflag = *pflag = *n_arg = 0; | |
785 | *path1 = *path2 = NULL; | |
786 | switch (cmdnum) { | |
787 | case I_GET: | |
788 | case I_PUT: | |
789 | if (parse_getput_flags(&cp, pflag)) | |
790 | return(-1); | |
791 | /* Get first pathname (mandatory) */ | |
792 | if (get_pathname(&cp, path1)) | |
793 | return(-1); | |
794 | if (*path1 == NULL) { | |
795 | error("You must specify at least one path after a " | |
796 | "%s command.", cmd); | |
797 | return(-1); | |
798 | } | |
799 | /* Try to get second pathname (optional) */ | |
800 | if (get_pathname(&cp, path2)) | |
801 | return(-1); | |
802 | break; | |
803 | case I_RENAME: | |
804 | case I_SYMLINK: | |
805 | if (get_pathname(&cp, path1)) | |
806 | return(-1); | |
807 | if (get_pathname(&cp, path2)) | |
808 | return(-1); | |
809 | if (!*path1 || !*path2) { | |
810 | error("You must specify two paths after a %s " | |
811 | "command.", cmd); | |
812 | return(-1); | |
813 | } | |
814 | break; | |
815 | case I_RM: | |
816 | case I_MKDIR: | |
817 | case I_RMDIR: | |
818 | case I_CHDIR: | |
819 | case I_LCHDIR: | |
820 | case I_LMKDIR: | |
821 | /* Get pathname (mandatory) */ | |
822 | if (get_pathname(&cp, path1)) | |
823 | return(-1); | |
824 | if (*path1 == NULL) { | |
825 | error("You must specify a path after a %s command.", | |
826 | cmd); | |
827 | return(-1); | |
828 | } | |
829 | break; | |
830 | case I_LS: | |
831 | if (parse_ls_flags(&cp, lflag)) | |
832 | return(-1); | |
833 | /* Path is optional */ | |
834 | if (get_pathname(&cp, path1)) | |
835 | return(-1); | |
836 | break; | |
837 | case I_LLS: | |
838 | case I_SHELL: | |
839 | /* Uses the rest of the line */ | |
840 | break; | |
841 | case I_LUMASK: | |
842 | base = 8; | |
843 | case I_CHMOD: | |
844 | base = 8; | |
845 | case I_CHOWN: | |
846 | case I_CHGRP: | |
847 | /* Get numeric arg (mandatory) */ | |
848 | l = strtol(cp, &cp2, base); | |
849 | if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) && | |
850 | errno == ERANGE) || l < 0) { | |
851 | error("You must supply a numeric argument " | |
852 | "to the %s command.", cmd); | |
853 | return(-1); | |
854 | } | |
855 | cp = cp2; | |
856 | *n_arg = l; | |
857 | if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp)) | |
858 | break; | |
859 | if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) { | |
860 | error("You must supply a numeric argument " | |
861 | "to the %s command.", cmd); | |
862 | return(-1); | |
863 | } | |
864 | cp += strspn(cp, WHITESPACE); | |
865 | ||
866 | /* Get pathname (mandatory) */ | |
867 | if (get_pathname(&cp, path1)) | |
868 | return(-1); | |
869 | if (*path1 == NULL) { | |
870 | error("You must specify a path after a %s command.", | |
871 | cmd); | |
872 | return(-1); | |
873 | } | |
874 | break; | |
875 | case I_QUIT: | |
876 | case I_PWD: | |
877 | case I_LPWD: | |
878 | case I_HELP: | |
879 | case I_VERSION: | |
880 | case I_PROGRESS: | |
881 | break; | |
882 | default: | |
883 | fatal("Command not implemented"); | |
884 | } | |
885 | ||
886 | *cpp = cp; | |
887 | return(cmdnum); | |
888 | } | |
889 | ||
890 | static int | |
891 | parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, | |
892 | int err_abort) | |
893 | { | |
894 | char *path1, *path2, *tmp; | |
895 | int pflag, lflag, iflag, cmdnum, i; | |
896 | unsigned long n_arg; | |
897 | Attrib a, *aa; | |
898 | char path_buf[MAXPATHLEN]; | |
899 | int err = 0; | |
900 | glob_t g; | |
901 | ||
902 | path1 = path2 = NULL; | |
903 | cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg, | |
904 | &path1, &path2); | |
905 | ||
906 | if (iflag != 0) | |
907 | err_abort = 0; | |
908 | ||
909 | memset(&g, 0, sizeof(g)); | |
910 | ||
911 | /* Perform command */ | |
912 | switch (cmdnum) { | |
913 | case 0: | |
914 | /* Blank line */ | |
915 | break; | |
916 | case -1: | |
917 | /* Unrecognized command */ | |
918 | err = -1; | |
919 | break; | |
920 | case I_GET: | |
921 | err = process_get(conn, path1, path2, *pwd, pflag); | |
922 | break; | |
923 | case I_PUT: | |
924 | err = process_put(conn, path1, path2, *pwd, pflag); | |
925 | break; | |
926 | case I_RENAME: | |
927 | path1 = make_absolute(path1, *pwd); | |
928 | path2 = make_absolute(path2, *pwd); | |
929 | err = do_rename(conn, path1, path2); | |
930 | break; | |
931 | case I_SYMLINK: | |
932 | path2 = make_absolute(path2, *pwd); | |
933 | err = do_symlink(conn, path1, path2); | |
934 | break; | |
935 | case I_RM: | |
936 | path1 = make_absolute(path1, *pwd); | |
937 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); | |
938 | for (i = 0; g.gl_pathv[i]; i++) { | |
939 | printf("Removing %s\n", g.gl_pathv[i]); | |
940 | err = do_rm(conn, g.gl_pathv[i]); | |
941 | if (err != 0 && err_abort) | |
942 | break; | |
943 | } | |
944 | break; | |
945 | case I_MKDIR: | |
946 | path1 = make_absolute(path1, *pwd); | |
947 | attrib_clear(&a); | |
948 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; | |
949 | a.perm = 0777; | |
950 | err = do_mkdir(conn, path1, &a); | |
951 | break; | |
952 | case I_RMDIR: | |
953 | path1 = make_absolute(path1, *pwd); | |
954 | err = do_rmdir(conn, path1); | |
955 | break; | |
956 | case I_CHDIR: | |
957 | path1 = make_absolute(path1, *pwd); | |
958 | if ((tmp = do_realpath(conn, path1)) == NULL) { | |
959 | err = 1; | |
960 | break; | |
961 | } | |
962 | if ((aa = do_stat(conn, tmp, 0)) == NULL) { | |
963 | xfree(tmp); | |
964 | err = 1; | |
965 | break; | |
966 | } | |
967 | if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { | |
968 | error("Can't change directory: Can't check target"); | |
969 | xfree(tmp); | |
970 | err = 1; | |
971 | break; | |
972 | } | |
973 | if (!S_ISDIR(aa->perm)) { | |
974 | error("Can't change directory: \"%s\" is not " | |
975 | "a directory", tmp); | |
976 | xfree(tmp); | |
977 | err = 1; | |
978 | break; | |
979 | } | |
980 | xfree(*pwd); | |
981 | *pwd = tmp; | |
982 | break; | |
983 | case I_LS: | |
984 | if (!path1) { | |
985 | do_globbed_ls(conn, *pwd, *pwd, lflag); | |
986 | break; | |
987 | } | |
988 | ||
989 | /* Strip pwd off beginning of non-absolute paths */ | |
990 | tmp = NULL; | |
991 | if (*path1 != '/') | |
992 | tmp = *pwd; | |
993 | ||
994 | path1 = make_absolute(path1, *pwd); | |
995 | err = do_globbed_ls(conn, path1, tmp, lflag); | |
996 | break; | |
997 | case I_LCHDIR: | |
998 | if (chdir(path1) == -1) { | |
999 | error("Couldn't change local directory to " | |
1000 | "\"%s\": %s", path1, strerror(errno)); | |
1001 | err = 1; | |
1002 | } | |
1003 | break; | |
1004 | case I_LMKDIR: | |
1005 | if (mkdir(path1, 0777) == -1) { | |
1006 | error("Couldn't create local directory " | |
1007 | "\"%s\": %s", path1, strerror(errno)); | |
1008 | err = 1; | |
1009 | } | |
1010 | break; | |
1011 | case I_LLS: | |
1012 | local_do_ls(cmd); | |
1013 | break; | |
1014 | case I_SHELL: | |
1015 | local_do_shell(cmd); | |
1016 | break; | |
1017 | case I_LUMASK: | |
1018 | umask(n_arg); | |
1019 | printf("Local umask: %03lo\n", n_arg); | |
1020 | break; | |
1021 | case I_CHMOD: | |
1022 | path1 = make_absolute(path1, *pwd); | |
1023 | attrib_clear(&a); | |
1024 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; | |
1025 | a.perm = n_arg; | |
1026 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); | |
1027 | for (i = 0; g.gl_pathv[i]; i++) { | |
1028 | printf("Changing mode on %s\n", g.gl_pathv[i]); | |
1029 | err = do_setstat(conn, g.gl_pathv[i], &a); | |
1030 | if (err != 0 && err_abort) | |
1031 | break; | |
1032 | } | |
1033 | break; | |
1034 | case I_CHOWN: | |
1035 | case I_CHGRP: | |
1036 | path1 = make_absolute(path1, *pwd); | |
1037 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); | |
1038 | for (i = 0; g.gl_pathv[i]; i++) { | |
1039 | if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) { | |
1040 | if (err != 0 && err_abort) | |
1041 | break; | |
1042 | else | |
1043 | continue; | |
1044 | } | |
1045 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { | |
1046 | error("Can't get current ownership of " | |
1047 | "remote file \"%s\"", g.gl_pathv[i]); | |
1048 | if (err != 0 && err_abort) | |
1049 | break; | |
1050 | else | |
1051 | continue; | |
1052 | } | |
1053 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; | |
1054 | if (cmdnum == I_CHOWN) { | |
1055 | printf("Changing owner on %s\n", g.gl_pathv[i]); | |
1056 | aa->uid = n_arg; | |
1057 | } else { | |
1058 | printf("Changing group on %s\n", g.gl_pathv[i]); | |
1059 | aa->gid = n_arg; | |
1060 | } | |
1061 | err = do_setstat(conn, g.gl_pathv[i], aa); | |
1062 | if (err != 0 && err_abort) | |
1063 | break; | |
1064 | } | |
1065 | break; | |
1066 | case I_PWD: | |
1067 | printf("Remote working directory: %s\n", *pwd); | |
1068 | break; | |
1069 | case I_LPWD: | |
1070 | if (!getcwd(path_buf, sizeof(path_buf))) { | |
1071 | error("Couldn't get local cwd: %s", strerror(errno)); | |
1072 | err = -1; | |
1073 | break; | |
1074 | } | |
1075 | printf("Local working directory: %s\n", path_buf); | |
1076 | break; | |
1077 | case I_QUIT: | |
1078 | /* Processed below */ | |
1079 | break; | |
1080 | case I_HELP: | |
1081 | help(); | |
1082 | break; | |
1083 | case I_VERSION: | |
1084 | printf("SFTP protocol version %u\n", sftp_proto_version(conn)); | |
1085 | break; | |
1086 | case I_PROGRESS: | |
1087 | showprogress = !showprogress; | |
1088 | if (showprogress) | |
1089 | printf("Progress meter enabled\n"); | |
1090 | else | |
1091 | printf("Progress meter disabled\n"); | |
1092 | break; | |
1093 | default: | |
1094 | fatal("%d is not implemented", cmdnum); | |
1095 | } | |
1096 | ||
1097 | if (g.gl_pathc) | |
1098 | globfree(&g); | |
1099 | if (path1) | |
1100 | xfree(path1); | |
1101 | if (path2) | |
1102 | xfree(path2); | |
1103 | ||
1104 | /* If an unignored error occurs in batch mode we should abort. */ | |
1105 | if (err_abort && err != 0) | |
1106 | return (-1); | |
1107 | else if (cmdnum == I_QUIT) | |
1108 | return (1); | |
1109 | ||
1110 | return (0); | |
1111 | } | |
1112 | ||
1113 | int | |
1114 | interactive_loop(int fd_in, int fd_out, char *file1, char *file2) | |
1115 | { | |
1116 | char *pwd; | |
1117 | char *dir = NULL; | |
1118 | char cmd[2048]; | |
1119 | struct sftp_conn *conn; | |
1120 | int err; | |
1121 | ||
1122 | conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests); | |
1123 | if (conn == NULL) | |
1124 | fatal("Couldn't initialise connection to server"); | |
1125 | ||
1126 | pwd = do_realpath(conn, "."); | |
1127 | if (pwd == NULL) | |
1128 | fatal("Need cwd"); | |
1129 | ||
1130 | if (file1 != NULL) { | |
1131 | dir = xstrdup(file1); | |
1132 | dir = make_absolute(dir, pwd); | |
1133 | ||
1134 | if (remote_is_dir(conn, dir) && file2 == NULL) { | |
1135 | printf("Changing to: %s\n", dir); | |
1136 | snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); | |
1137 | if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) | |
1138 | return (-1); | |
1139 | } else { | |
1140 | if (file2 == NULL) | |
1141 | snprintf(cmd, sizeof cmd, "get %s", dir); | |
1142 | else | |
1143 | snprintf(cmd, sizeof cmd, "get %s %s", dir, | |
1144 | file2); | |
1145 | ||
1146 | err = parse_dispatch_command(conn, cmd, &pwd, 1); | |
1147 | xfree(dir); | |
1148 | xfree(pwd); | |
1149 | return (err); | |
1150 | } | |
1151 | xfree(dir); | |
1152 | } | |
1153 | ||
1154 | #if HAVE_SETVBUF | |
1155 | setvbuf(stdout, NULL, _IOLBF, 0); | |
1156 | setvbuf(infile, NULL, _IOLBF, 0); | |
1157 | #else | |
1158 | setlinebuf(stdout); | |
1159 | setlinebuf(infile); | |
1160 | #endif | |
1161 | ||
1162 | err = 0; | |
1163 | for (;;) { | |
1164 | char *cp; | |
1165 | ||
1166 | printf("sftp> "); | |
1167 | ||
1168 | /* XXX: use libedit */ | |
1169 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { | |
1170 | printf("\n"); | |
1171 | break; | |
1172 | } else if (infile != stdin) /* Bluff typing */ | |
1173 | printf("%s", cmd); | |
1174 | ||
1175 | cp = strrchr(cmd, '\n'); | |
1176 | if (cp) | |
1177 | *cp = '\0'; | |
1178 | ||
1179 | err = parse_dispatch_command(conn, cmd, &pwd, infile != stdin); | |
1180 | if (err != 0) | |
1181 | break; | |
1182 | } | |
1183 | xfree(pwd); | |
1184 | ||
1185 | /* err == 1 signifies normal "quit" exit */ | |
1186 | return (err >= 0 ? 0 : -1); | |
1187 | } | |
1188 |