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