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