]>
Commit | Line | Data |
---|---|---|
61e96248 | 1 | /* |
ab3932ab | 2 | * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> |
61e96248 | 3 | * |
ab3932ab | 4 | * Permission to use, copy, modify, and distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
61e96248 | 7 | * |
ab3932ab | 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
61e96248 | 15 | */ |
16 | ||
17 | #include "includes.h" | |
18 | ||
47460206 | 19 | RCSID("$OpenBSD: sftp.c,v 1.59 2004/11/29 07:41:24 djm Exp $"); |
5132eac0 | 20 | |
21 | #ifdef USE_LIBEDIT | |
22 | #include <histedit.h> | |
23 | #else | |
24 | typedef void EditLine; | |
25 | #endif | |
61e96248 | 26 | |
27 | #include "buffer.h" | |
28 | #include "xmalloc.h" | |
29 | #include "log.h" | |
30 | #include "pathnames.h" | |
1fcde3fe | 31 | #include "misc.h" |
61e96248 | 32 | |
33 | #include "sftp.h" | |
34 | #include "sftp-common.h" | |
35 | #include "sftp-client.h" | |
caf1e9f0 | 36 | |
2cda7d6b | 37 | /* File to read commands from */ |
38 | FILE* infile; | |
39 | ||
40 | /* Are we in batchfile mode? */ | |
41 | int batchmode = 0; | |
42 | ||
43 | /* Size of buffer used when copying files */ | |
44 | size_t copy_buffer_len = 32768; | |
45 | ||
46 | /* Number of concurrent outstanding requests */ | |
47 | size_t num_requests = 16; | |
48 | ||
49 | /* PID of ssh transport process */ | |
50 | static pid_t sshpid = -1; | |
51 | ||
52 | /* This is set to 0 if the progressmeter is not desired. */ | |
06abcf97 | 53 | int showprogress = 1; |
2cda7d6b | 54 | |
0e5de6f8 | 55 | /* SIGINT received during command processing */ |
56 | volatile sig_atomic_t interrupted = 0; | |
57 | ||
95cbd340 | 58 | /* I wish qsort() took a separate ctx for the comparison function...*/ |
59 | int sort_flag; | |
60 | ||
2cda7d6b | 61 | int remote_glob(struct sftp_conn *, const char *, int, |
62 | int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */ | |
61e96248 | 63 | |
5152d46f | 64 | extern char *__progname; |
5152d46f | 65 | |
2cda7d6b | 66 | /* Separators for interactive commands */ |
67 | #define WHITESPACE " \t\r\n" | |
68 | ||
95cbd340 | 69 | /* ls flags */ |
ae7daec3 | 70 | #define LS_LONG_VIEW 0x01 /* Full view ala ls -l */ |
71 | #define LS_SHORT_VIEW 0x02 /* Single row view ala ls -1 */ | |
72 | #define LS_NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */ | |
73 | #define LS_NAME_SORT 0x08 /* Sort by name (default) */ | |
74 | #define LS_TIME_SORT 0x10 /* Sort by mtime */ | |
75 | #define LS_SIZE_SORT 0x20 /* Sort by file size */ | |
76 | #define LS_REVERSE_SORT 0x40 /* Reverse sort order */ | |
cc4ff6c4 | 77 | #define LS_SHOW_ALL 0x80 /* Don't skip filenames starting with '.' */ |
95cbd340 | 78 | |
ae7daec3 | 79 | #define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW) |
80 | #define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT) | |
2cda7d6b | 81 | |
82 | /* Commands for interactive mode */ | |
83 | #define I_CHDIR 1 | |
84 | #define I_CHGRP 2 | |
85 | #define I_CHMOD 3 | |
86 | #define I_CHOWN 4 | |
87 | #define I_GET 5 | |
88 | #define I_HELP 6 | |
89 | #define I_LCHDIR 7 | |
90 | #define I_LLS 8 | |
91 | #define I_LMKDIR 9 | |
92 | #define I_LPWD 10 | |
93 | #define I_LS 11 | |
94 | #define I_LUMASK 12 | |
95 | #define I_MKDIR 13 | |
96 | #define I_PUT 14 | |
97 | #define I_PWD 15 | |
98 | #define I_QUIT 16 | |
99 | #define I_RENAME 17 | |
100 | #define I_RM 18 | |
101 | #define I_RMDIR 19 | |
102 | #define I_SHELL 20 | |
103 | #define I_SYMLINK 21 | |
104 | #define I_VERSION 22 | |
105 | #define I_PROGRESS 23 | |
106 | ||
107 | struct CMD { | |
108 | const char *c; | |
109 | const int n; | |
110 | }; | |
111 | ||
112 | static const struct CMD cmds[] = { | |
113 | { "bye", I_QUIT }, | |
114 | { "cd", I_CHDIR }, | |
115 | { "chdir", I_CHDIR }, | |
116 | { "chgrp", I_CHGRP }, | |
117 | { "chmod", I_CHMOD }, | |
118 | { "chown", I_CHOWN }, | |
119 | { "dir", I_LS }, | |
120 | { "exit", I_QUIT }, | |
121 | { "get", I_GET }, | |
122 | { "mget", I_GET }, | |
123 | { "help", I_HELP }, | |
124 | { "lcd", I_LCHDIR }, | |
125 | { "lchdir", I_LCHDIR }, | |
126 | { "lls", I_LLS }, | |
127 | { "lmkdir", I_LMKDIR }, | |
128 | { "ln", I_SYMLINK }, | |
129 | { "lpwd", I_LPWD }, | |
130 | { "ls", I_LS }, | |
131 | { "lumask", I_LUMASK }, | |
132 | { "mkdir", I_MKDIR }, | |
133 | { "progress", I_PROGRESS }, | |
134 | { "put", I_PUT }, | |
135 | { "mput", I_PUT }, | |
136 | { "pwd", I_PWD }, | |
137 | { "quit", I_QUIT }, | |
138 | { "rename", I_RENAME }, | |
139 | { "rm", I_RM }, | |
140 | { "rmdir", I_RMDIR }, | |
141 | { "symlink", I_SYMLINK }, | |
142 | { "version", I_VERSION }, | |
143 | { "!", I_SHELL }, | |
144 | { "?", I_HELP }, | |
145 | { NULL, -1} | |
146 | }; | |
147 | ||
148 | int interactive_loop(int fd_in, int fd_out, char *file1, char *file2); | |
149 | ||
0e5de6f8 | 150 | static void |
151 | killchild(int signo) | |
152 | { | |
153 | if (sshpid > 1) | |
154 | kill(sshpid, SIGTERM); | |
155 | ||
156 | _exit(1); | |
157 | } | |
158 | ||
159 | static void | |
160 | cmd_interrupt(int signo) | |
161 | { | |
162 | const char msg[] = "\rInterrupt \n"; | |
47460206 | 163 | int olderrno = errno; |
0e5de6f8 | 164 | |
165 | write(STDERR_FILENO, msg, sizeof(msg) - 1); | |
166 | interrupted = 1; | |
47460206 | 167 | errno = olderrno; |
0e5de6f8 | 168 | } |
169 | ||
2cda7d6b | 170 | static void |
171 | help(void) | |
172 | { | |
173 | printf("Available commands:\n"); | |
174 | printf("cd path Change remote directory to 'path'\n"); | |
175 | printf("lcd path Change local directory to 'path'\n"); | |
176 | printf("chgrp grp path Change group of file 'path' to 'grp'\n"); | |
177 | printf("chmod mode path Change permissions of file 'path' to 'mode'\n"); | |
178 | printf("chown own path Change owner of file 'path' to 'own'\n"); | |
179 | printf("help Display this help text\n"); | |
180 | printf("get remote-path [local-path] Download file\n"); | |
181 | printf("lls [ls-options [path]] Display local directory listing\n"); | |
182 | printf("ln oldpath newpath Symlink remote file\n"); | |
183 | printf("lmkdir path Create local directory\n"); | |
184 | printf("lpwd Print local working directory\n"); | |
185 | printf("ls [path] Display remote directory listing\n"); | |
186 | printf("lumask umask Set local umask to 'umask'\n"); | |
187 | printf("mkdir path Create remote directory\n"); | |
188 | printf("progress Toggle display of progress meter\n"); | |
189 | printf("put local-path [remote-path] Upload file\n"); | |
190 | printf("pwd Display remote working directory\n"); | |
191 | printf("exit Quit sftp\n"); | |
192 | printf("quit Quit sftp\n"); | |
193 | printf("rename oldpath newpath Rename remote file\n"); | |
194 | printf("rmdir path Remove remote directory\n"); | |
195 | printf("rm path Delete remote file\n"); | |
196 | printf("symlink oldpath newpath Symlink remote file\n"); | |
197 | printf("version Show SFTP version\n"); | |
198 | printf("!command Execute 'command' in local shell\n"); | |
199 | printf("! Escape to local shell\n"); | |
200 | printf("? Synonym for help\n"); | |
201 | } | |
202 | ||
203 | static void | |
204 | local_do_shell(const char *args) | |
205 | { | |
206 | int status; | |
207 | char *shell; | |
208 | pid_t pid; | |
209 | ||
210 | if (!*args) | |
211 | args = NULL; | |
212 | ||
213 | if ((shell = getenv("SHELL")) == NULL) | |
214 | shell = _PATH_BSHELL; | |
215 | ||
216 | if ((pid = fork()) == -1) | |
217 | fatal("Couldn't fork: %s", strerror(errno)); | |
218 | ||
219 | if (pid == 0) { | |
220 | /* XXX: child has pipe fds to ssh subproc open - issue? */ | |
221 | if (args) { | |
222 | debug3("Executing %s -c \"%s\"", shell, args); | |
223 | execl(shell, shell, "-c", args, (char *)NULL); | |
224 | } else { | |
225 | debug3("Executing %s", shell); | |
226 | execl(shell, shell, (char *)NULL); | |
227 | } | |
228 | fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell, | |
229 | strerror(errno)); | |
230 | _exit(1); | |
231 | } | |
232 | while (waitpid(pid, &status, 0) == -1) | |
233 | if (errno != EINTR) | |
234 | fatal("Couldn't wait for child: %s", strerror(errno)); | |
235 | if (!WIFEXITED(status)) | |
236 | error("Shell exited abormally"); | |
237 | else if (WEXITSTATUS(status)) | |
238 | error("Shell exited with status %d", WEXITSTATUS(status)); | |
239 | } | |
240 | ||
241 | static void | |
242 | local_do_ls(const char *args) | |
243 | { | |
244 | if (!args || !*args) | |
245 | local_do_shell(_PATH_LS); | |
246 | else { | |
247 | int len = strlen(_PATH_LS " ") + strlen(args) + 1; | |
248 | char *buf = xmalloc(len); | |
249 | ||
250 | /* XXX: quoting - rip quoting code from ftp? */ | |
251 | snprintf(buf, len, _PATH_LS " %s", args); | |
252 | local_do_shell(buf); | |
253 | xfree(buf); | |
254 | } | |
255 | } | |
256 | ||
257 | /* Strip one path (usually the pwd) from the start of another */ | |
258 | static char * | |
259 | path_strip(char *path, char *strip) | |
260 | { | |
261 | size_t len; | |
0426a3b4 | 262 | |
2cda7d6b | 263 | if (strip == NULL) |
264 | return (xstrdup(path)); | |
265 | ||
266 | len = strlen(strip); | |
47460206 | 267 | if (strncmp(path, strip, len) == 0) { |
2cda7d6b | 268 | if (strip[len - 1] != '/' && path[len] == '/') |
269 | len++; | |
270 | return (xstrdup(path + len)); | |
271 | } | |
272 | ||
273 | return (xstrdup(path)); | |
274 | } | |
275 | ||
276 | static char * | |
277 | path_append(char *p1, char *p2) | |
278 | { | |
279 | char *ret; | |
280 | int len = strlen(p1) + strlen(p2) + 2; | |
281 | ||
282 | ret = xmalloc(len); | |
283 | strlcpy(ret, p1, len); | |
284 | if (p1[strlen(p1) - 1] != '/') | |
285 | strlcat(ret, "/", len); | |
286 | strlcat(ret, p2, len); | |
287 | ||
288 | return(ret); | |
289 | } | |
290 | ||
291 | static char * | |
292 | make_absolute(char *p, char *pwd) | |
293 | { | |
ca75d7de | 294 | char *abs_str; |
2cda7d6b | 295 | |
296 | /* Derelativise */ | |
297 | if (p && p[0] != '/') { | |
ca75d7de | 298 | abs_str = path_append(pwd, p); |
2cda7d6b | 299 | xfree(p); |
ca75d7de | 300 | return(abs_str); |
2cda7d6b | 301 | } else |
302 | return(p); | |
303 | } | |
304 | ||
305 | static int | |
306 | infer_path(const char *p, char **ifp) | |
307 | { | |
308 | char *cp; | |
309 | ||
310 | cp = strrchr(p, '/'); | |
311 | if (cp == NULL) { | |
312 | *ifp = xstrdup(p); | |
313 | return(0); | |
314 | } | |
315 | ||
316 | if (!cp[1]) { | |
317 | error("Invalid path"); | |
318 | return(-1); | |
319 | } | |
320 | ||
321 | *ifp = xstrdup(cp + 1); | |
322 | return(0); | |
323 | } | |
324 | ||
325 | static int | |
326 | parse_getput_flags(const char **cpp, int *pflag) | |
327 | { | |
328 | const char *cp = *cpp; | |
329 | ||
330 | /* Check for flags */ | |
331 | if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) { | |
332 | switch (cp[1]) { | |
333 | case 'p': | |
334 | case 'P': | |
335 | *pflag = 1; | |
336 | break; | |
337 | default: | |
338 | error("Invalid flag -%c", cp[1]); | |
339 | return(-1); | |
340 | } | |
341 | cp += 2; | |
342 | *cpp = cp + strspn(cp, WHITESPACE); | |
343 | } | |
344 | ||
345 | return(0); | |
346 | } | |
347 | ||
348 | static int | |
349 | parse_ls_flags(const char **cpp, int *lflag) | |
350 | { | |
351 | const char *cp = *cpp; | |
352 | ||
95cbd340 | 353 | /* Defaults */ |
ae7daec3 | 354 | *lflag = LS_NAME_SORT; |
95cbd340 | 355 | |
2cda7d6b | 356 | /* Check for flags */ |
357 | if (cp++[0] == '-') { | |
358 | for(; strchr(WHITESPACE, *cp) == NULL; cp++) { | |
359 | switch (*cp) { | |
360 | case 'l': | |
48925711 | 361 | *lflag &= ~VIEW_FLAGS; |
ae7daec3 | 362 | *lflag |= LS_LONG_VIEW; |
2cda7d6b | 363 | break; |
364 | case '1': | |
48925711 | 365 | *lflag &= ~VIEW_FLAGS; |
ae7daec3 | 366 | *lflag |= LS_SHORT_VIEW; |
48925711 | 367 | break; |
368 | case 'n': | |
369 | *lflag &= ~VIEW_FLAGS; | |
ae7daec3 | 370 | *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW; |
2cda7d6b | 371 | break; |
95cbd340 | 372 | case 'S': |
373 | *lflag &= ~SORT_FLAGS; | |
ae7daec3 | 374 | *lflag |= LS_SIZE_SORT; |
95cbd340 | 375 | break; |
376 | case 't': | |
377 | *lflag &= ~SORT_FLAGS; | |
ae7daec3 | 378 | *lflag |= LS_TIME_SORT; |
95cbd340 | 379 | break; |
380 | case 'r': | |
ae7daec3 | 381 | *lflag |= LS_REVERSE_SORT; |
95cbd340 | 382 | break; |
383 | case 'f': | |
384 | *lflag &= ~SORT_FLAGS; | |
385 | break; | |
cc4ff6c4 | 386 | case 'a': |
387 | *lflag |= LS_SHOW_ALL; | |
388 | break; | |
2cda7d6b | 389 | default: |
390 | error("Invalid flag -%c", *cp); | |
391 | return(-1); | |
392 | } | |
393 | } | |
394 | *cpp = cp + strspn(cp, WHITESPACE); | |
395 | } | |
396 | ||
397 | return(0); | |
398 | } | |
399 | ||
400 | static int | |
401 | get_pathname(const char **cpp, char **path) | |
402 | { | |
403 | const char *cp = *cpp, *end; | |
404 | char quot; | |
405 | int i, j; | |
406 | ||
407 | cp += strspn(cp, WHITESPACE); | |
408 | if (!*cp) { | |
409 | *cpp = cp; | |
410 | *path = NULL; | |
411 | return (0); | |
412 | } | |
413 | ||
414 | *path = xmalloc(strlen(cp) + 1); | |
415 | ||
416 | /* Check for quoted filenames */ | |
417 | if (*cp == '\"' || *cp == '\'') { | |
418 | quot = *cp++; | |
419 | ||
420 | /* Search for terminating quote, unescape some chars */ | |
421 | for (i = j = 0; i <= strlen(cp); i++) { | |
422 | if (cp[i] == quot) { /* Found quote */ | |
423 | i++; | |
424 | (*path)[j] = '\0'; | |
425 | break; | |
426 | } | |
427 | if (cp[i] == '\0') { /* End of string */ | |
428 | error("Unterminated quote"); | |
429 | goto fail; | |
430 | } | |
431 | if (cp[i] == '\\') { /* Escaped characters */ | |
432 | i++; | |
433 | if (cp[i] != '\'' && cp[i] != '\"' && | |
434 | cp[i] != '\\') { | |
7f09f717 | 435 | error("Bad escaped character '\\%c'", |
2cda7d6b | 436 | cp[i]); |
437 | goto fail; | |
438 | } | |
439 | } | |
440 | (*path)[j++] = cp[i]; | |
441 | } | |
442 | ||
443 | if (j == 0) { | |
444 | error("Empty quotes"); | |
445 | goto fail; | |
446 | } | |
447 | *cpp = cp + i + strspn(cp + i, WHITESPACE); | |
448 | } else { | |
449 | /* Read to end of filename */ | |
450 | end = strpbrk(cp, WHITESPACE); | |
451 | if (end == NULL) | |
452 | end = strchr(cp, '\0'); | |
453 | *cpp = end + strspn(end, WHITESPACE); | |
454 | ||
455 | memcpy(*path, cp, end - cp); | |
456 | (*path)[end - cp] = '\0'; | |
457 | } | |
458 | return (0); | |
459 | ||
460 | fail: | |
461 | xfree(*path); | |
462 | *path = NULL; | |
463 | return (-1); | |
464 | } | |
465 | ||
466 | static int | |
467 | is_dir(char *path) | |
468 | { | |
469 | struct stat sb; | |
470 | ||
471 | /* XXX: report errors? */ | |
472 | if (stat(path, &sb) == -1) | |
473 | return(0); | |
474 | ||
475 | return(sb.st_mode & S_IFDIR); | |
476 | } | |
477 | ||
478 | static int | |
479 | is_reg(char *path) | |
480 | { | |
481 | struct stat sb; | |
482 | ||
483 | if (stat(path, &sb) == -1) | |
484 | fatal("stat %s: %s", path, strerror(errno)); | |
485 | ||
486 | return(S_ISREG(sb.st_mode)); | |
487 | } | |
488 | ||
489 | static int | |
490 | remote_is_dir(struct sftp_conn *conn, char *path) | |
491 | { | |
492 | Attrib *a; | |
493 | ||
494 | /* XXX: report errors? */ | |
495 | if ((a = do_stat(conn, path, 1)) == NULL) | |
496 | return(0); | |
497 | if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) | |
498 | return(0); | |
499 | return(a->perm & S_IFDIR); | |
500 | } | |
501 | ||
502 | static int | |
503 | process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) | |
504 | { | |
505 | char *abs_src = NULL; | |
506 | char *abs_dst = NULL; | |
507 | char *tmp; | |
508 | glob_t g; | |
509 | int err = 0; | |
510 | int i; | |
511 | ||
512 | abs_src = xstrdup(src); | |
513 | abs_src = make_absolute(abs_src, pwd); | |
514 | ||
515 | memset(&g, 0, sizeof(g)); | |
516 | debug3("Looking up %s", abs_src); | |
517 | if (remote_glob(conn, abs_src, 0, NULL, &g)) { | |
518 | error("File \"%s\" not found.", abs_src); | |
519 | err = -1; | |
520 | goto out; | |
521 | } | |
522 | ||
523 | /* If multiple matches, dst must be a directory or unspecified */ | |
524 | if (g.gl_matchc > 1 && dst && !is_dir(dst)) { | |
525 | error("Multiple files match, but \"%s\" is not a directory", | |
526 | dst); | |
527 | err = -1; | |
528 | goto out; | |
529 | } | |
530 | ||
0e5de6f8 | 531 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 532 | if (infer_path(g.gl_pathv[i], &tmp)) { |
533 | err = -1; | |
534 | goto out; | |
535 | } | |
536 | ||
537 | if (g.gl_matchc == 1 && dst) { | |
538 | /* If directory specified, append filename */ | |
539 | if (is_dir(dst)) { | |
540 | if (infer_path(g.gl_pathv[0], &tmp)) { | |
541 | err = 1; | |
542 | goto out; | |
543 | } | |
544 | abs_dst = path_append(dst, tmp); | |
545 | xfree(tmp); | |
546 | } else | |
547 | abs_dst = xstrdup(dst); | |
548 | } else if (dst) { | |
549 | abs_dst = path_append(dst, tmp); | |
550 | xfree(tmp); | |
551 | } else | |
552 | abs_dst = tmp; | |
553 | ||
554 | printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst); | |
555 | if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1) | |
556 | err = -1; | |
557 | xfree(abs_dst); | |
558 | abs_dst = NULL; | |
559 | } | |
560 | ||
561 | out: | |
562 | xfree(abs_src); | |
563 | if (abs_dst) | |
564 | xfree(abs_dst); | |
565 | globfree(&g); | |
566 | return(err); | |
567 | } | |
568 | ||
569 | static int | |
570 | process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag) | |
571 | { | |
572 | char *tmp_dst = NULL; | |
573 | char *abs_dst = NULL; | |
574 | char *tmp; | |
575 | glob_t g; | |
576 | int err = 0; | |
577 | int i; | |
578 | ||
579 | if (dst) { | |
580 | tmp_dst = xstrdup(dst); | |
581 | tmp_dst = make_absolute(tmp_dst, pwd); | |
582 | } | |
583 | ||
584 | memset(&g, 0, sizeof(g)); | |
585 | debug3("Looking up %s", src); | |
586 | if (glob(src, 0, NULL, &g)) { | |
587 | error("File \"%s\" not found.", src); | |
588 | err = -1; | |
589 | goto out; | |
590 | } | |
591 | ||
592 | /* If multiple matches, dst may be directory or unspecified */ | |
593 | if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) { | |
594 | error("Multiple files match, but \"%s\" is not a directory", | |
595 | tmp_dst); | |
596 | err = -1; | |
597 | goto out; | |
598 | } | |
599 | ||
0e5de6f8 | 600 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 601 | if (!is_reg(g.gl_pathv[i])) { |
602 | error("skipping non-regular file %s", | |
603 | g.gl_pathv[i]); | |
604 | continue; | |
605 | } | |
606 | if (infer_path(g.gl_pathv[i], &tmp)) { | |
607 | err = -1; | |
608 | goto out; | |
609 | } | |
610 | ||
611 | if (g.gl_matchc == 1 && tmp_dst) { | |
612 | /* If directory specified, append filename */ | |
613 | if (remote_is_dir(conn, tmp_dst)) { | |
614 | if (infer_path(g.gl_pathv[0], &tmp)) { | |
615 | err = 1; | |
616 | goto out; | |
617 | } | |
618 | abs_dst = path_append(tmp_dst, tmp); | |
619 | xfree(tmp); | |
620 | } else | |
621 | abs_dst = xstrdup(tmp_dst); | |
622 | ||
623 | } else if (tmp_dst) { | |
624 | abs_dst = path_append(tmp_dst, tmp); | |
625 | xfree(tmp); | |
626 | } else | |
627 | abs_dst = make_absolute(tmp, pwd); | |
628 | ||
629 | printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst); | |
630 | if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1) | |
631 | err = -1; | |
632 | } | |
633 | ||
634 | out: | |
635 | if (abs_dst) | |
636 | xfree(abs_dst); | |
637 | if (tmp_dst) | |
638 | xfree(tmp_dst); | |
639 | globfree(&g); | |
640 | return(err); | |
641 | } | |
642 | ||
643 | static int | |
644 | sdirent_comp(const void *aa, const void *bb) | |
645 | { | |
646 | SFTP_DIRENT *a = *(SFTP_DIRENT **)aa; | |
647 | SFTP_DIRENT *b = *(SFTP_DIRENT **)bb; | |
ae7daec3 | 648 | int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1; |
2cda7d6b | 649 | |
95cbd340 | 650 | #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1)) |
ae7daec3 | 651 | if (sort_flag & LS_NAME_SORT) |
95cbd340 | 652 | return (rmul * strcmp(a->filename, b->filename)); |
ae7daec3 | 653 | else if (sort_flag & LS_TIME_SORT) |
95cbd340 | 654 | return (rmul * NCMP(a->a.mtime, b->a.mtime)); |
ae7daec3 | 655 | else if (sort_flag & LS_SIZE_SORT) |
95cbd340 | 656 | return (rmul * NCMP(a->a.size, b->a.size)); |
657 | ||
658 | fatal("Unknown ls sort type"); | |
2cda7d6b | 659 | } |
660 | ||
661 | /* sftp ls.1 replacement for directories */ | |
662 | static int | |
663 | do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) | |
664 | { | |
665 | int n, c = 1, colspace = 0, columns = 1; | |
666 | SFTP_DIRENT **d; | |
667 | ||
668 | if ((n = do_readdir(conn, path, &d)) != 0) | |
669 | return (n); | |
670 | ||
ae7daec3 | 671 | if (!(lflag & LS_SHORT_VIEW)) { |
2cda7d6b | 672 | int m = 0, width = 80; |
673 | struct winsize ws; | |
674 | char *tmp; | |
675 | ||
676 | /* Count entries for sort and find longest filename */ | |
cc4ff6c4 | 677 | for (n = 0; d[n] != NULL; n++) { |
678 | if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL)) | |
679 | m = MAX(m, strlen(d[n]->filename)); | |
680 | } | |
2cda7d6b | 681 | |
682 | /* Add any subpath that also needs to be counted */ | |
683 | tmp = path_strip(path, strip_path); | |
684 | m += strlen(tmp); | |
685 | xfree(tmp); | |
686 | ||
687 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) | |
688 | width = ws.ws_col; | |
689 | ||
690 | columns = width / (m + 2); | |
691 | columns = MAX(columns, 1); | |
692 | colspace = width / columns; | |
693 | colspace = MIN(colspace, width); | |
694 | } | |
695 | ||
95cbd340 | 696 | if (lflag & SORT_FLAGS) { |
ae7daec3 | 697 | sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT); |
95cbd340 | 698 | qsort(d, n, sizeof(*d), sdirent_comp); |
699 | } | |
2cda7d6b | 700 | |
0e5de6f8 | 701 | for (n = 0; d[n] != NULL && !interrupted; n++) { |
2cda7d6b | 702 | char *tmp, *fname; |
703 | ||
cc4ff6c4 | 704 | if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL)) |
705 | continue; | |
706 | ||
2cda7d6b | 707 | tmp = path_append(path, d[n]->filename); |
708 | fname = path_strip(tmp, strip_path); | |
709 | xfree(tmp); | |
710 | ||
ae7daec3 | 711 | if (lflag & LS_LONG_VIEW) { |
712 | if (lflag & LS_NUMERIC_VIEW) { | |
48925711 | 713 | char *lname; |
714 | struct stat sb; | |
715 | ||
716 | memset(&sb, 0, sizeof(sb)); | |
717 | attrib_to_stat(&d[n]->a, &sb); | |
718 | lname = ls_file(fname, &sb, 1); | |
719 | printf("%s\n", lname); | |
720 | xfree(lname); | |
721 | } else | |
722 | printf("%s\n", d[n]->longname); | |
2cda7d6b | 723 | } else { |
724 | printf("%-*s", colspace, fname); | |
725 | if (c >= columns) { | |
726 | printf("\n"); | |
727 | c = 1; | |
728 | } else | |
729 | c++; | |
730 | } | |
731 | ||
732 | xfree(fname); | |
733 | } | |
734 | ||
ae7daec3 | 735 | if (!(lflag & LS_LONG_VIEW) && (c != 1)) |
2cda7d6b | 736 | printf("\n"); |
737 | ||
738 | free_sftp_dirents(d); | |
739 | return (0); | |
740 | } | |
741 | ||
742 | /* sftp ls.1 replacement which handles path globs */ | |
743 | static int | |
744 | do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, | |
745 | int lflag) | |
746 | { | |
747 | glob_t g; | |
748 | int i, c = 1, colspace = 0, columns = 1; | |
749 | Attrib *a; | |
750 | ||
751 | memset(&g, 0, sizeof(g)); | |
752 | ||
753 | if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE, | |
754 | NULL, &g)) { | |
755 | error("Can't ls: \"%s\" not found", path); | |
756 | return (-1); | |
757 | } | |
758 | ||
0e5de6f8 | 759 | if (interrupted) |
760 | goto out; | |
761 | ||
2cda7d6b | 762 | /* |
763 | * If the glob returns a single match, which is the same as the | |
764 | * input glob, and it is a directory, then just list its contents | |
765 | */ | |
766 | if (g.gl_pathc == 1 && | |
767 | strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) { | |
768 | if ((a = do_lstat(conn, path, 1)) == NULL) { | |
769 | globfree(&g); | |
770 | return (-1); | |
771 | } | |
772 | if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) && | |
773 | S_ISDIR(a->perm)) { | |
774 | globfree(&g); | |
775 | return (do_ls_dir(conn, path, strip_path, lflag)); | |
776 | } | |
777 | } | |
778 | ||
ae7daec3 | 779 | if (!(lflag & LS_SHORT_VIEW)) { |
2cda7d6b | 780 | int m = 0, width = 80; |
781 | struct winsize ws; | |
782 | ||
783 | /* Count entries for sort and find longest filename */ | |
784 | for (i = 0; g.gl_pathv[i]; i++) | |
785 | m = MAX(m, strlen(g.gl_pathv[i])); | |
786 | ||
787 | if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) | |
788 | width = ws.ws_col; | |
789 | ||
790 | columns = width / (m + 2); | |
791 | columns = MAX(columns, 1); | |
792 | colspace = width / columns; | |
793 | } | |
794 | ||
0e5de6f8 | 795 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 796 | char *fname; |
797 | ||
798 | fname = path_strip(g.gl_pathv[i], strip_path); | |
799 | ||
ae7daec3 | 800 | if (lflag & LS_LONG_VIEW) { |
2cda7d6b | 801 | char *lname; |
802 | struct stat sb; | |
803 | ||
804 | /* | |
805 | * XXX: this is slow - 1 roundtrip per path | |
806 | * A solution to this is to fork glob() and | |
807 | * build a sftp specific version which keeps the | |
808 | * attribs (which currently get thrown away) | |
809 | * that the server returns as well as the filenames. | |
810 | */ | |
811 | memset(&sb, 0, sizeof(sb)); | |
812 | a = do_lstat(conn, g.gl_pathv[i], 1); | |
813 | if (a != NULL) | |
814 | attrib_to_stat(a, &sb); | |
815 | lname = ls_file(fname, &sb, 1); | |
816 | printf("%s\n", lname); | |
817 | xfree(lname); | |
818 | } else { | |
819 | printf("%-*s", colspace, fname); | |
820 | if (c >= columns) { | |
821 | printf("\n"); | |
822 | c = 1; | |
823 | } else | |
824 | c++; | |
825 | } | |
826 | xfree(fname); | |
827 | } | |
828 | ||
ae7daec3 | 829 | if (!(lflag & LS_LONG_VIEW) && (c != 1)) |
2cda7d6b | 830 | printf("\n"); |
831 | ||
0e5de6f8 | 832 | out: |
2cda7d6b | 833 | if (g.gl_pathc) |
834 | globfree(&g); | |
835 | ||
836 | return (0); | |
837 | } | |
838 | ||
839 | static int | |
840 | parse_args(const char **cpp, int *pflag, int *lflag, int *iflag, | |
841 | unsigned long *n_arg, char **path1, char **path2) | |
842 | { | |
843 | const char *cmd, *cp = *cpp; | |
844 | char *cp2; | |
845 | int base = 0; | |
846 | long l; | |
847 | int i, cmdnum; | |
848 | ||
849 | /* Skip leading whitespace */ | |
850 | cp = cp + strspn(cp, WHITESPACE); | |
851 | ||
852 | /* Ignore blank lines and lines which begin with comment '#' char */ | |
853 | if (*cp == '\0' || *cp == '#') | |
854 | return (0); | |
855 | ||
856 | /* Check for leading '-' (disable error processing) */ | |
857 | *iflag = 0; | |
858 | if (*cp == '-') { | |
859 | *iflag = 1; | |
860 | cp++; | |
861 | } | |
862 | ||
863 | /* Figure out which command we have */ | |
864 | for (i = 0; cmds[i].c; i++) { | |
865 | int cmdlen = strlen(cmds[i].c); | |
866 | ||
867 | /* Check for command followed by whitespace */ | |
868 | if (!strncasecmp(cp, cmds[i].c, cmdlen) && | |
869 | strchr(WHITESPACE, cp[cmdlen])) { | |
870 | cp += cmdlen; | |
871 | cp = cp + strspn(cp, WHITESPACE); | |
872 | break; | |
873 | } | |
874 | } | |
875 | cmdnum = cmds[i].n; | |
876 | cmd = cmds[i].c; | |
877 | ||
878 | /* Special case */ | |
879 | if (*cp == '!') { | |
880 | cp++; | |
881 | cmdnum = I_SHELL; | |
882 | } else if (cmdnum == -1) { | |
883 | error("Invalid command."); | |
884 | return (-1); | |
885 | } | |
886 | ||
887 | /* Get arguments and parse flags */ | |
888 | *lflag = *pflag = *n_arg = 0; | |
889 | *path1 = *path2 = NULL; | |
890 | switch (cmdnum) { | |
891 | case I_GET: | |
892 | case I_PUT: | |
893 | if (parse_getput_flags(&cp, pflag)) | |
894 | return(-1); | |
895 | /* Get first pathname (mandatory) */ | |
896 | if (get_pathname(&cp, path1)) | |
897 | return(-1); | |
898 | if (*path1 == NULL) { | |
899 | error("You must specify at least one path after a " | |
900 | "%s command.", cmd); | |
901 | return(-1); | |
902 | } | |
903 | /* Try to get second pathname (optional) */ | |
904 | if (get_pathname(&cp, path2)) | |
905 | return(-1); | |
906 | break; | |
907 | case I_RENAME: | |
908 | case I_SYMLINK: | |
909 | if (get_pathname(&cp, path1)) | |
910 | return(-1); | |
911 | if (get_pathname(&cp, path2)) | |
912 | return(-1); | |
913 | if (!*path1 || !*path2) { | |
914 | error("You must specify two paths after a %s " | |
915 | "command.", cmd); | |
916 | return(-1); | |
917 | } | |
918 | break; | |
919 | case I_RM: | |
920 | case I_MKDIR: | |
921 | case I_RMDIR: | |
922 | case I_CHDIR: | |
923 | case I_LCHDIR: | |
924 | case I_LMKDIR: | |
925 | /* Get pathname (mandatory) */ | |
926 | if (get_pathname(&cp, path1)) | |
927 | return(-1); | |
928 | if (*path1 == NULL) { | |
929 | error("You must specify a path after a %s command.", | |
930 | cmd); | |
931 | return(-1); | |
932 | } | |
933 | break; | |
934 | case I_LS: | |
935 | if (parse_ls_flags(&cp, lflag)) | |
936 | return(-1); | |
937 | /* Path is optional */ | |
938 | if (get_pathname(&cp, path1)) | |
939 | return(-1); | |
940 | break; | |
941 | case I_LLS: | |
942 | case I_SHELL: | |
943 | /* Uses the rest of the line */ | |
944 | break; | |
945 | case I_LUMASK: | |
946 | base = 8; | |
947 | case I_CHMOD: | |
948 | base = 8; | |
949 | case I_CHOWN: | |
950 | case I_CHGRP: | |
951 | /* Get numeric arg (mandatory) */ | |
952 | l = strtol(cp, &cp2, base); | |
953 | if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) && | |
954 | errno == ERANGE) || l < 0) { | |
955 | error("You must supply a numeric argument " | |
956 | "to the %s command.", cmd); | |
957 | return(-1); | |
958 | } | |
959 | cp = cp2; | |
960 | *n_arg = l; | |
961 | if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp)) | |
962 | break; | |
963 | if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) { | |
964 | error("You must supply a numeric argument " | |
965 | "to the %s command.", cmd); | |
966 | return(-1); | |
967 | } | |
968 | cp += strspn(cp, WHITESPACE); | |
969 | ||
970 | /* Get pathname (mandatory) */ | |
971 | if (get_pathname(&cp, path1)) | |
972 | return(-1); | |
973 | if (*path1 == NULL) { | |
974 | error("You must specify a path after a %s command.", | |
975 | cmd); | |
976 | return(-1); | |
977 | } | |
978 | break; | |
979 | case I_QUIT: | |
980 | case I_PWD: | |
981 | case I_LPWD: | |
982 | case I_HELP: | |
983 | case I_VERSION: | |
984 | case I_PROGRESS: | |
985 | break; | |
986 | default: | |
987 | fatal("Command not implemented"); | |
988 | } | |
989 | ||
990 | *cpp = cp; | |
991 | return(cmdnum); | |
992 | } | |
993 | ||
994 | static int | |
995 | parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, | |
996 | int err_abort) | |
997 | { | |
998 | char *path1, *path2, *tmp; | |
999 | int pflag, lflag, iflag, cmdnum, i; | |
1000 | unsigned long n_arg; | |
1001 | Attrib a, *aa; | |
1002 | char path_buf[MAXPATHLEN]; | |
1003 | int err = 0; | |
1004 | glob_t g; | |
1005 | ||
1006 | path1 = path2 = NULL; | |
1007 | cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg, | |
1008 | &path1, &path2); | |
1009 | ||
1010 | if (iflag != 0) | |
1011 | err_abort = 0; | |
1012 | ||
1013 | memset(&g, 0, sizeof(g)); | |
1014 | ||
1015 | /* Perform command */ | |
1016 | switch (cmdnum) { | |
1017 | case 0: | |
1018 | /* Blank line */ | |
1019 | break; | |
1020 | case -1: | |
1021 | /* Unrecognized command */ | |
1022 | err = -1; | |
1023 | break; | |
1024 | case I_GET: | |
1025 | err = process_get(conn, path1, path2, *pwd, pflag); | |
1026 | break; | |
1027 | case I_PUT: | |
1028 | err = process_put(conn, path1, path2, *pwd, pflag); | |
1029 | break; | |
1030 | case I_RENAME: | |
1031 | path1 = make_absolute(path1, *pwd); | |
1032 | path2 = make_absolute(path2, *pwd); | |
1033 | err = do_rename(conn, path1, path2); | |
1034 | break; | |
1035 | case I_SYMLINK: | |
1036 | path2 = make_absolute(path2, *pwd); | |
1037 | err = do_symlink(conn, path1, path2); | |
1038 | break; | |
1039 | case I_RM: | |
1040 | path1 = make_absolute(path1, *pwd); | |
1041 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); | |
0e5de6f8 | 1042 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 1043 | printf("Removing %s\n", g.gl_pathv[i]); |
1044 | err = do_rm(conn, g.gl_pathv[i]); | |
1045 | if (err != 0 && err_abort) | |
1046 | break; | |
1047 | } | |
1048 | break; | |
1049 | case I_MKDIR: | |
1050 | path1 = make_absolute(path1, *pwd); | |
1051 | attrib_clear(&a); | |
1052 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; | |
1053 | a.perm = 0777; | |
1054 | err = do_mkdir(conn, path1, &a); | |
1055 | break; | |
1056 | case I_RMDIR: | |
1057 | path1 = make_absolute(path1, *pwd); | |
1058 | err = do_rmdir(conn, path1); | |
1059 | break; | |
1060 | case I_CHDIR: | |
1061 | path1 = make_absolute(path1, *pwd); | |
1062 | if ((tmp = do_realpath(conn, path1)) == NULL) { | |
1063 | err = 1; | |
1064 | break; | |
1065 | } | |
1066 | if ((aa = do_stat(conn, tmp, 0)) == NULL) { | |
1067 | xfree(tmp); | |
1068 | err = 1; | |
1069 | break; | |
1070 | } | |
1071 | if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { | |
1072 | error("Can't change directory: Can't check target"); | |
1073 | xfree(tmp); | |
1074 | err = 1; | |
1075 | break; | |
1076 | } | |
1077 | if (!S_ISDIR(aa->perm)) { | |
1078 | error("Can't change directory: \"%s\" is not " | |
1079 | "a directory", tmp); | |
1080 | xfree(tmp); | |
1081 | err = 1; | |
1082 | break; | |
1083 | } | |
1084 | xfree(*pwd); | |
1085 | *pwd = tmp; | |
1086 | break; | |
1087 | case I_LS: | |
1088 | if (!path1) { | |
1089 | do_globbed_ls(conn, *pwd, *pwd, lflag); | |
1090 | break; | |
1091 | } | |
1092 | ||
1093 | /* Strip pwd off beginning of non-absolute paths */ | |
1094 | tmp = NULL; | |
1095 | if (*path1 != '/') | |
1096 | tmp = *pwd; | |
1097 | ||
1098 | path1 = make_absolute(path1, *pwd); | |
1099 | err = do_globbed_ls(conn, path1, tmp, lflag); | |
1100 | break; | |
1101 | case I_LCHDIR: | |
1102 | if (chdir(path1) == -1) { | |
1103 | error("Couldn't change local directory to " | |
1104 | "\"%s\": %s", path1, strerror(errno)); | |
1105 | err = 1; | |
1106 | } | |
1107 | break; | |
1108 | case I_LMKDIR: | |
1109 | if (mkdir(path1, 0777) == -1) { | |
1110 | error("Couldn't create local directory " | |
1111 | "\"%s\": %s", path1, strerror(errno)); | |
1112 | err = 1; | |
1113 | } | |
1114 | break; | |
1115 | case I_LLS: | |
1116 | local_do_ls(cmd); | |
1117 | break; | |
1118 | case I_SHELL: | |
1119 | local_do_shell(cmd); | |
1120 | break; | |
1121 | case I_LUMASK: | |
1122 | umask(n_arg); | |
1123 | printf("Local umask: %03lo\n", n_arg); | |
1124 | break; | |
1125 | case I_CHMOD: | |
1126 | path1 = make_absolute(path1, *pwd); | |
1127 | attrib_clear(&a); | |
1128 | a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; | |
1129 | a.perm = n_arg; | |
1130 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); | |
0e5de6f8 | 1131 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 1132 | printf("Changing mode on %s\n", g.gl_pathv[i]); |
1133 | err = do_setstat(conn, g.gl_pathv[i], &a); | |
1134 | if (err != 0 && err_abort) | |
1135 | break; | |
1136 | } | |
1137 | break; | |
1138 | case I_CHOWN: | |
1139 | case I_CHGRP: | |
1140 | path1 = make_absolute(path1, *pwd); | |
1141 | remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); | |
0e5de6f8 | 1142 | for (i = 0; g.gl_pathv[i] && !interrupted; i++) { |
2cda7d6b | 1143 | if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) { |
1144 | if (err != 0 && err_abort) | |
1145 | break; | |
1146 | else | |
1147 | continue; | |
1148 | } | |
1149 | if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { | |
1150 | error("Can't get current ownership of " | |
1151 | "remote file \"%s\"", g.gl_pathv[i]); | |
1152 | if (err != 0 && err_abort) | |
1153 | break; | |
1154 | else | |
1155 | continue; | |
1156 | } | |
1157 | aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; | |
1158 | if (cmdnum == I_CHOWN) { | |
1159 | printf("Changing owner on %s\n", g.gl_pathv[i]); | |
1160 | aa->uid = n_arg; | |
1161 | } else { | |
1162 | printf("Changing group on %s\n", g.gl_pathv[i]); | |
1163 | aa->gid = n_arg; | |
1164 | } | |
1165 | err = do_setstat(conn, g.gl_pathv[i], aa); | |
1166 | if (err != 0 && err_abort) | |
1167 | break; | |
1168 | } | |
1169 | break; | |
1170 | case I_PWD: | |
1171 | printf("Remote working directory: %s\n", *pwd); | |
1172 | break; | |
1173 | case I_LPWD: | |
1174 | if (!getcwd(path_buf, sizeof(path_buf))) { | |
1175 | error("Couldn't get local cwd: %s", strerror(errno)); | |
1176 | err = -1; | |
1177 | break; | |
1178 | } | |
1179 | printf("Local working directory: %s\n", path_buf); | |
1180 | break; | |
1181 | case I_QUIT: | |
1182 | /* Processed below */ | |
1183 | break; | |
1184 | case I_HELP: | |
1185 | help(); | |
1186 | break; | |
1187 | case I_VERSION: | |
1188 | printf("SFTP protocol version %u\n", sftp_proto_version(conn)); | |
1189 | break; | |
1190 | case I_PROGRESS: | |
1191 | showprogress = !showprogress; | |
1192 | if (showprogress) | |
1193 | printf("Progress meter enabled\n"); | |
1194 | else | |
1195 | printf("Progress meter disabled\n"); | |
1196 | break; | |
1197 | default: | |
1198 | fatal("%d is not implemented", cmdnum); | |
1199 | } | |
1200 | ||
1201 | if (g.gl_pathc) | |
1202 | globfree(&g); | |
1203 | if (path1) | |
1204 | xfree(path1); | |
1205 | if (path2) | |
1206 | xfree(path2); | |
1207 | ||
1208 | /* If an unignored error occurs in batch mode we should abort. */ | |
1209 | if (err_abort && err != 0) | |
1210 | return (-1); | |
1211 | else if (cmdnum == I_QUIT) | |
1212 | return (1); | |
1213 | ||
1214 | return (0); | |
1215 | } | |
1216 | ||
5132eac0 | 1217 | #ifdef USE_LIBEDIT |
1218 | static char * | |
1219 | prompt(EditLine *el) | |
1220 | { | |
1221 | return ("sftp> "); | |
1222 | } | |
1223 | #endif | |
1224 | ||
2cda7d6b | 1225 | int |
1226 | interactive_loop(int fd_in, int fd_out, char *file1, char *file2) | |
1227 | { | |
1228 | char *pwd; | |
1229 | char *dir = NULL; | |
1230 | char cmd[2048]; | |
1231 | struct sftp_conn *conn; | |
1232 | int err; | |
5132eac0 | 1233 | EditLine *el = NULL; |
1234 | #ifdef USE_LIBEDIT | |
1235 | History *hl = NULL; | |
1236 | HistEvent hev; | |
1237 | extern char *__progname; | |
1238 | ||
1239 | if (!batchmode && isatty(STDIN_FILENO)) { | |
1240 | if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL) | |
1241 | fatal("Couldn't initialise editline"); | |
1242 | if ((hl = history_init()) == NULL) | |
1243 | fatal("Couldn't initialise editline history"); | |
1244 | history(hl, &hev, H_SETSIZE, 100); | |
1245 | el_set(el, EL_HIST, history, hl); | |
1246 | ||
1247 | el_set(el, EL_PROMPT, prompt); | |
1248 | el_set(el, EL_EDITOR, "emacs"); | |
1249 | el_set(el, EL_TERMINAL, NULL); | |
1250 | el_set(el, EL_SIGNAL, 1); | |
1251 | el_source(el, NULL); | |
1252 | } | |
1253 | #endif /* USE_LIBEDIT */ | |
2cda7d6b | 1254 | |
1255 | conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests); | |
1256 | if (conn == NULL) | |
1257 | fatal("Couldn't initialise connection to server"); | |
1258 | ||
1259 | pwd = do_realpath(conn, "."); | |
1260 | if (pwd == NULL) | |
1261 | fatal("Need cwd"); | |
1262 | ||
1263 | if (file1 != NULL) { | |
1264 | dir = xstrdup(file1); | |
1265 | dir = make_absolute(dir, pwd); | |
1266 | ||
1267 | if (remote_is_dir(conn, dir) && file2 == NULL) { | |
1268 | printf("Changing to: %s\n", dir); | |
1269 | snprintf(cmd, sizeof cmd, "cd \"%s\"", dir); | |
aa41be57 | 1270 | if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) { |
1271 | xfree(dir); | |
1272 | xfree(pwd); | |
2cda7d6b | 1273 | return (-1); |
aa41be57 | 1274 | } |
2cda7d6b | 1275 | } else { |
1276 | if (file2 == NULL) | |
1277 | snprintf(cmd, sizeof cmd, "get %s", dir); | |
1278 | else | |
1279 | snprintf(cmd, sizeof cmd, "get %s %s", dir, | |
1280 | file2); | |
1281 | ||
1282 | err = parse_dispatch_command(conn, cmd, &pwd, 1); | |
1283 | xfree(dir); | |
1284 | xfree(pwd); | |
1285 | return (err); | |
1286 | } | |
1287 | xfree(dir); | |
1288 | } | |
1289 | ||
1290 | #if HAVE_SETVBUF | |
1291 | setvbuf(stdout, NULL, _IOLBF, 0); | |
1292 | setvbuf(infile, NULL, _IOLBF, 0); | |
1293 | #else | |
1294 | setlinebuf(stdout); | |
1295 | setlinebuf(infile); | |
1296 | #endif | |
1297 | ||
1298 | err = 0; | |
1299 | for (;;) { | |
1300 | char *cp; | |
1301 | ||
0e5de6f8 | 1302 | signal(SIGINT, SIG_IGN); |
1303 | ||
5132eac0 | 1304 | if (el == NULL) { |
1305 | printf("sftp> "); | |
1306 | if (fgets(cmd, sizeof(cmd), infile) == NULL) { | |
1307 | printf("\n"); | |
1308 | break; | |
1309 | } | |
1310 | if (batchmode) /* Echo command */ | |
1311 | printf("%s", cmd); | |
1312 | } else { | |
1313 | #ifdef USE_LIBEDIT | |
1314 | const char *line; | |
1315 | int count = 0; | |
2cda7d6b | 1316 | |
5132eac0 | 1317 | if ((line = el_gets(el, &count)) == NULL || count <= 0) |
1318 | break; | |
1319 | history(hl, &hev, H_ENTER, line); | |
1320 | if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) { | |
1321 | fprintf(stderr, "Error: input line too long\n"); | |
1322 | continue; | |
1323 | } | |
1324 | #endif /* USE_LIBEDIT */ | |
2cda7d6b | 1325 | } |
1326 | ||
2cda7d6b | 1327 | cp = strrchr(cmd, '\n'); |
1328 | if (cp) | |
1329 | *cp = '\0'; | |
1330 | ||
0e5de6f8 | 1331 | /* Handle user interrupts gracefully during commands */ |
1332 | interrupted = 0; | |
1333 | signal(SIGINT, cmd_interrupt); | |
1334 | ||
2cda7d6b | 1335 | err = parse_dispatch_command(conn, cmd, &pwd, batchmode); |
1336 | if (err != 0) | |
1337 | break; | |
1338 | } | |
1339 | xfree(pwd); | |
1340 | ||
1341 | /* err == 1 signifies normal "quit" exit */ | |
1342 | return (err >= 0 ? 0 : -1); | |
1343 | } | |
b65c3807 | 1344 | |
1b558925 | 1345 | static void |
1346 | connect_to_server(char *path, char **args, int *in, int *out) | |
61e96248 | 1347 | { |
1348 | int c_in, c_out; | |
9906a836 | 1349 | |
61e96248 | 1350 | #ifdef USE_PIPES |
1351 | int pin[2], pout[2]; | |
9906a836 | 1352 | |
61e96248 | 1353 | if ((pipe(pin) == -1) || (pipe(pout) == -1)) |
1354 | fatal("pipe: %s", strerror(errno)); | |
1355 | *in = pin[0]; | |
1356 | *out = pout[1]; | |
1357 | c_in = pout[0]; | |
1358 | c_out = pin[1]; | |
1359 | #else /* USE_PIPES */ | |
1360 | int inout[2]; | |
9906a836 | 1361 | |
61e96248 | 1362 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1) |
1363 | fatal("socketpair: %s", strerror(errno)); | |
1364 | *in = *out = inout[0]; | |
1365 | c_in = c_out = inout[1]; | |
1366 | #endif /* USE_PIPES */ | |
1367 | ||
1b558925 | 1368 | if ((sshpid = fork()) == -1) |
61e96248 | 1369 | fatal("fork: %s", strerror(errno)); |
1b558925 | 1370 | else if (sshpid == 0) { |
61e96248 | 1371 | if ((dup2(c_in, STDIN_FILENO) == -1) || |
1372 | (dup2(c_out, STDOUT_FILENO) == -1)) { | |
1373 | fprintf(stderr, "dup2: %s\n", strerror(errno)); | |
8dbffee9 | 1374 | _exit(1); |
61e96248 | 1375 | } |
1376 | close(*in); | |
1377 | close(*out); | |
1378 | close(c_in); | |
1379 | close(c_out); | |
0e5de6f8 | 1380 | |
1381 | /* | |
1382 | * The underlying ssh is in the same process group, so we must | |
f2107e97 | 1383 | * ignore SIGINT if we want to gracefully abort commands, |
1384 | * otherwise the signal will make it to the ssh process and | |
0e5de6f8 | 1385 | * kill it too |
1386 | */ | |
1387 | signal(SIGINT, SIG_IGN); | |
35e49915 | 1388 | execvp(path, args); |
a96fd7c2 | 1389 | fprintf(stderr, "exec: %s: %s\n", path, strerror(errno)); |
8dbffee9 | 1390 | _exit(1); |
61e96248 | 1391 | } |
1392 | ||
1b558925 | 1393 | signal(SIGTERM, killchild); |
1394 | signal(SIGINT, killchild); | |
1395 | signal(SIGHUP, killchild); | |
61e96248 | 1396 | close(c_in); |
1397 | close(c_out); | |
1398 | } | |
1399 | ||
396c147e | 1400 | static void |
61e96248 | 1401 | usage(void) |
1402 | { | |
22be05a5 | 1403 | extern char *__progname; |
762715ce | 1404 | |
f1278af7 | 1405 | fprintf(stderr, |
433e60ac | 1406 | "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n" |
1407 | " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n" | |
1408 | " [-S program] [-s subsystem | sftp_server] host\n" | |
1409 | " %s [[user@]host[:file [file]]]\n" | |
1410 | " %s [[user@]host[:dir[/]]]\n" | |
1411 | " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname); | |
61e96248 | 1412 | exit(1); |
1413 | } | |
1414 | ||
2b87da3b | 1415 | int |
61e96248 | 1416 | main(int argc, char **argv) |
1417 | { | |
9a36208d | 1418 | int in, out, ch, err; |
6e007f08 | 1419 | char *host, *userhost, *cp, *file2 = NULL; |
8a624ebf | 1420 | int debug_level = 0, sshver = 2; |
1421 | char *file1 = NULL, *sftp_server = NULL; | |
a96fd7c2 | 1422 | char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL; |
8a624ebf | 1423 | LogLevel ll = SYSLOG_LEVEL_INFO; |
1424 | arglist args; | |
0426a3b4 | 1425 | extern int optind; |
1426 | extern char *optarg; | |
61e96248 | 1427 | |
fda04d7d | 1428 | __progname = ssh_get_progname(argv[0]); |
8a624ebf | 1429 | args.list = NULL; |
184eed6a | 1430 | addargs(&args, "ssh"); /* overwritten with ssh_program */ |
8a624ebf | 1431 | addargs(&args, "-oForwardX11 no"); |
1432 | addargs(&args, "-oForwardAgent no"); | |
e1c5bfaf | 1433 | addargs(&args, "-oClearAllForwardings yes"); |
ac414e17 | 1434 | |
8a624ebf | 1435 | ll = SYSLOG_LEVEL_INFO; |
ac414e17 | 1436 | infile = stdin; |
0426a3b4 | 1437 | |
c25d3df7 | 1438 | while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) { |
0426a3b4 | 1439 | switch (ch) { |
1440 | case 'C': | |
8a624ebf | 1441 | addargs(&args, "-C"); |
0426a3b4 | 1442 | break; |
1443 | case 'v': | |
8a624ebf | 1444 | if (debug_level < 3) { |
1445 | addargs(&args, "-v"); | |
1446 | ll = SYSLOG_LEVEL_DEBUG1 + debug_level; | |
1447 | } | |
1448 | debug_level++; | |
0426a3b4 | 1449 | break; |
f1278af7 | 1450 | case 'F': |
0426a3b4 | 1451 | case 'o': |
f1278af7 | 1452 | addargs(&args, "-%c%s", ch, optarg); |
0426a3b4 | 1453 | break; |
1454 | case '1': | |
8a624ebf | 1455 | sshver = 1; |
0426a3b4 | 1456 | if (sftp_server == NULL) |
1457 | sftp_server = _PATH_SFTP_SERVER; | |
1458 | break; | |
1459 | case 's': | |
1460 | sftp_server = optarg; | |
1461 | break; | |
1462 | case 'S': | |
1463 | ssh_program = optarg; | |
1464 | break; | |
a5ec8a3d | 1465 | case 'b': |
a8b64bb8 | 1466 | if (batchmode) |
1467 | fatal("Batch file already specified."); | |
1468 | ||
1469 | /* Allow "-" as stdin */ | |
f2107e97 | 1470 | if (strcmp(optarg, "-") != 0 && |
a8b64bb8 | 1471 | (infile = fopen(optarg, "r")) == NULL) |
1472 | fatal("%s (%s).", strerror(errno), optarg); | |
b65c3807 | 1473 | showprogress = 0; |
a8b64bb8 | 1474 | batchmode = 1; |
a5ec8a3d | 1475 | break; |
a96fd7c2 | 1476 | case 'P': |
1477 | sftp_direct = optarg; | |
1478 | break; | |
375f867e | 1479 | case 'B': |
1480 | copy_buffer_len = strtol(optarg, &cp, 10); | |
1481 | if (copy_buffer_len == 0 || *cp != '\0') | |
1482 | fatal("Invalid buffer size \"%s\"", optarg); | |
1483 | break; | |
c25d3df7 | 1484 | case 'R': |
1485 | num_requests = strtol(optarg, &cp, 10); | |
1486 | if (num_requests == 0 || *cp != '\0') | |
762715ce | 1487 | fatal("Invalid number of requests \"%s\"", |
c25d3df7 | 1488 | optarg); |
1489 | break; | |
0426a3b4 | 1490 | case 'h': |
1491 | default: | |
61e96248 | 1492 | usage(); |
1493 | } | |
1494 | } | |
1495 | ||
06abcf97 | 1496 | if (!isatty(STDERR_FILENO)) |
1497 | showprogress = 0; | |
1498 | ||
b69145c2 | 1499 | log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); |
1500 | ||
a96fd7c2 | 1501 | if (sftp_direct == NULL) { |
1502 | if (optind == argc || argc > (optind + 2)) | |
1503 | usage(); | |
61e96248 | 1504 | |
a96fd7c2 | 1505 | userhost = xstrdup(argv[optind]); |
1506 | file2 = argv[optind+1]; | |
edeeab1e | 1507 | |
15748b4d | 1508 | if ((host = strrchr(userhost, '@')) == NULL) |
a96fd7c2 | 1509 | host = userhost; |
1510 | else { | |
1511 | *host++ = '\0'; | |
1512 | if (!userhost[0]) { | |
1513 | fprintf(stderr, "Missing username\n"); | |
1514 | usage(); | |
1515 | } | |
1516 | addargs(&args, "-l%s",userhost); | |
61e96248 | 1517 | } |
61e96248 | 1518 | |
02de7c6e | 1519 | if ((cp = colon(host)) != NULL) { |
1520 | *cp++ = '\0'; | |
1521 | file1 = cp; | |
1522 | } | |
1523 | ||
a96fd7c2 | 1524 | host = cleanhostname(host); |
1525 | if (!*host) { | |
1526 | fprintf(stderr, "Missing hostname\n"); | |
1527 | usage(); | |
1528 | } | |
61e96248 | 1529 | |
a96fd7c2 | 1530 | addargs(&args, "-oProtocol %d", sshver); |
8a624ebf | 1531 | |
a96fd7c2 | 1532 | /* no subsystem if the server-spec contains a '/' */ |
1533 | if (sftp_server == NULL || strchr(sftp_server, '/') == NULL) | |
1534 | addargs(&args, "-s"); | |
61e96248 | 1535 | |
a96fd7c2 | 1536 | addargs(&args, "%s", host); |
762715ce | 1537 | addargs(&args, "%s", (sftp_server != NULL ? |
a96fd7c2 | 1538 | sftp_server : "sftp")); |
1539 | args.list[0] = ssh_program; | |
61e96248 | 1540 | |
a8b64bb8 | 1541 | if (!batchmode) |
1542 | fprintf(stderr, "Connecting to %s...\n", host); | |
1b558925 | 1543 | connect_to_server(ssh_program, args.list, &in, &out); |
a96fd7c2 | 1544 | } else { |
1545 | args.list = NULL; | |
1546 | addargs(&args, "sftp-server"); | |
61e96248 | 1547 | |
a8b64bb8 | 1548 | if (!batchmode) |
1549 | fprintf(stderr, "Attaching to %s...\n", sftp_direct); | |
1b558925 | 1550 | connect_to_server(sftp_direct, args.list, &in, &out); |
a96fd7c2 | 1551 | } |
61e96248 | 1552 | |
9a36208d | 1553 | err = interactive_loop(in, out, file1, file2); |
61e96248 | 1554 | |
51fb577a | 1555 | #if !defined(USE_PIPES) |
2cda7d6b | 1556 | shutdown(in, SHUT_RDWR); |
1557 | shutdown(out, SHUT_RDWR); | |
51fb577a | 1558 | #endif |
1559 | ||
61e96248 | 1560 | close(in); |
1561 | close(out); | |
a8b64bb8 | 1562 | if (batchmode) |
a5ec8a3d | 1563 | fclose(infile); |
61e96248 | 1564 | |
8c38e88b | 1565 | while (waitpid(sshpid, NULL, 0) == -1) |
1566 | if (errno != EINTR) | |
1567 | fatal("Couldn't wait for ssh process: %s", | |
1568 | strerror(errno)); | |
61e96248 | 1569 | |
9a36208d | 1570 | exit(err == 0 ? 0 : 1); |
61e96248 | 1571 | } |