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