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