blob: f0d5dd557e94a42b7060ce8c1a4cd158a150643d [file] [log] [blame]
Damien Millerb6c85fc2007-01-05 16:30:41 +11001/* $OpenBSD: sftp.c,v 1.96 2007/01/03 04:09:15 stevesk Exp $ */
Damien Miller33804262001-02-04 23:20:18 +11002/*
Damien Miller4e60ed72004-02-17 17:07:59 +11003 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller33804262001-02-04 23:20:18 +11004 *
Damien Miller4e60ed72004-02-17 17:07:59 +11005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller33804262001-02-04 23:20:18 +11008 *
Damien Miller4e60ed72004-02-17 17:07:59 +11009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller33804262001-02-04 23:20:18 +110016 */
17
18#include "includes.h"
19
Damien Miller9cf6d072006-03-15 11:29:24 +110020#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100021#include <sys/ioctl.h>
Damien Millerf17883e2006-03-15 11:45:54 +110022#ifdef HAVE_SYS_STAT_H
23# include <sys/stat.h>
24#endif
Damien Miller8dbffe72006-08-05 11:02:17 +100025#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100026#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110027#include <sys/wait.h>
Darren Tucker2d963d82004-11-07 20:04:10 +110028
Darren Tucker39972492006-07-12 22:22:46 +100029#include <errno.h>
30
Damien Miller03e20032006-03-15 11:16:59 +110031#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110032# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110033#endif
Darren Tucker2d963d82004-11-07 20:04:10 +110034#ifdef USE_LIBEDIT
35#include <histedit.h>
36#else
37typedef void EditLine;
38#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110039#include <signal.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100040#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100041#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100042#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100043#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100044#include <stdarg.h>
Damien Miller33804262001-02-04 23:20:18 +110045
Damien Miller33804262001-02-04 23:20:18 +110046#include "xmalloc.h"
47#include "log.h"
48#include "pathnames.h"
Ben Lindstrom4529b702001-05-03 23:39:53 +000049#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110050
51#include "sftp.h"
Damien Millerd7834352006-08-05 12:39:39 +100052#include "buffer.h"
Damien Miller33804262001-02-04 23:20:18 +110053#include "sftp-common.h"
54#include "sftp-client.h"
Damien Millerd7d46bb2004-02-18 14:11:13 +110055
Damien Miller20e1fab2004-02-18 14:30:55 +110056/* File to read commands from */
57FILE* infile;
58
59/* Are we in batchfile mode? */
60int batchmode = 0;
61
62/* Size of buffer used when copying files */
63size_t copy_buffer_len = 32768;
64
65/* Number of concurrent outstanding requests */
66size_t num_requests = 16;
67
68/* PID of ssh transport process */
69static pid_t sshpid = -1;
70
71/* This is set to 0 if the progressmeter is not desired. */
Damien Millerc0f27d82004-03-08 23:12:19 +110072int showprogress = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +110073
Darren Tuckercdf547a2004-05-24 10:12:19 +100074/* SIGINT received during command processing */
75volatile sig_atomic_t interrupted = 0;
76
Darren Tuckerb9123452004-06-22 13:06:45 +100077/* I wish qsort() took a separate ctx for the comparison function...*/
78int sort_flag;
79
Damien Miller20e1fab2004-02-18 14:30:55 +110080int remote_glob(struct sftp_conn *, const char *, int,
81 int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
Damien Miller33804262001-02-04 23:20:18 +110082
Kevin Steves12888d12001-03-05 19:50:57 +000083extern char *__progname;
Kevin Steves12888d12001-03-05 19:50:57 +000084
Damien Miller20e1fab2004-02-18 14:30:55 +110085/* Separators for interactive commands */
86#define WHITESPACE " \t\r\n"
Damien Millerd7686fd2001-02-10 00:40:03 +110087
Darren Tuckerb9123452004-06-22 13:06:45 +100088/* ls flags */
Darren Tuckera4e9ffa2004-06-22 13:07:58 +100089#define LS_LONG_VIEW 0x01 /* Full view ala ls -l */
90#define LS_SHORT_VIEW 0x02 /* Single row view ala ls -1 */
91#define LS_NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */
92#define LS_NAME_SORT 0x08 /* Sort by name (default) */
93#define LS_TIME_SORT 0x10 /* Sort by mtime */
94#define LS_SIZE_SORT 0x20 /* Sort by file size */
95#define LS_REVERSE_SORT 0x40 /* Reverse sort order */
Darren Tucker9a526452004-06-22 13:09:55 +100096#define LS_SHOW_ALL 0x80 /* Don't skip filenames starting with '.' */
Darren Tuckerb9123452004-06-22 13:06:45 +100097
Darren Tuckera4e9ffa2004-06-22 13:07:58 +100098#define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW)
99#define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
Damien Miller20e1fab2004-02-18 14:30:55 +1100100
101/* Commands for interactive mode */
102#define I_CHDIR 1
103#define I_CHGRP 2
104#define I_CHMOD 3
105#define I_CHOWN 4
106#define I_GET 5
107#define I_HELP 6
108#define I_LCHDIR 7
109#define I_LLS 8
110#define I_LMKDIR 9
111#define I_LPWD 10
112#define I_LS 11
113#define I_LUMASK 12
114#define I_MKDIR 13
115#define I_PUT 14
116#define I_PWD 15
117#define I_QUIT 16
118#define I_RENAME 17
119#define I_RM 18
120#define I_RMDIR 19
121#define I_SHELL 20
122#define I_SYMLINK 21
123#define I_VERSION 22
124#define I_PROGRESS 23
125
126struct CMD {
127 const char *c;
128 const int n;
129};
130
131static const struct CMD cmds[] = {
132 { "bye", I_QUIT },
133 { "cd", I_CHDIR },
134 { "chdir", I_CHDIR },
135 { "chgrp", I_CHGRP },
136 { "chmod", I_CHMOD },
137 { "chown", I_CHOWN },
138 { "dir", I_LS },
139 { "exit", I_QUIT },
140 { "get", I_GET },
141 { "mget", I_GET },
142 { "help", I_HELP },
143 { "lcd", I_LCHDIR },
144 { "lchdir", I_LCHDIR },
145 { "lls", I_LLS },
146 { "lmkdir", I_LMKDIR },
147 { "ln", I_SYMLINK },
148 { "lpwd", I_LPWD },
149 { "ls", I_LS },
150 { "lumask", I_LUMASK },
151 { "mkdir", I_MKDIR },
152 { "progress", I_PROGRESS },
153 { "put", I_PUT },
154 { "mput", I_PUT },
155 { "pwd", I_PWD },
156 { "quit", I_QUIT },
157 { "rename", I_RENAME },
158 { "rm", I_RM },
159 { "rmdir", I_RMDIR },
160 { "symlink", I_SYMLINK },
161 { "version", I_VERSION },
162 { "!", I_SHELL },
163 { "?", I_HELP },
164 { NULL, -1}
165};
166
167int interactive_loop(int fd_in, int fd_out, char *file1, char *file2);
168
Damien Millerb6c85fc2007-01-05 16:30:41 +1100169/* ARGSUSED */
Damien Miller20e1fab2004-02-18 14:30:55 +1100170static void
Darren Tuckercdf547a2004-05-24 10:12:19 +1000171killchild(int signo)
172{
Darren Tuckerba66df82005-01-24 21:57:40 +1100173 if (sshpid > 1) {
Darren Tuckercdf547a2004-05-24 10:12:19 +1000174 kill(sshpid, SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100175 waitpid(sshpid, NULL, 0);
176 }
Darren Tuckercdf547a2004-05-24 10:12:19 +1000177
178 _exit(1);
179}
180
Damien Millerb6c85fc2007-01-05 16:30:41 +1100181/* ARGSUSED */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000182static void
183cmd_interrupt(int signo)
184{
185 const char msg[] = "\rInterrupt \n";
Darren Tuckere2f189a2004-12-06 22:45:53 +1100186 int olderrno = errno;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000187
188 write(STDERR_FILENO, msg, sizeof(msg) - 1);
189 interrupted = 1;
Darren Tuckere2f189a2004-12-06 22:45:53 +1100190 errno = olderrno;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000191}
192
193static void
Damien Miller20e1fab2004-02-18 14:30:55 +1100194help(void)
195{
196 printf("Available commands:\n");
197 printf("cd path Change remote directory to 'path'\n");
198 printf("lcd path Change local directory to 'path'\n");
199 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
200 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
201 printf("chown own path Change owner of file 'path' to 'own'\n");
202 printf("help Display this help text\n");
203 printf("get remote-path [local-path] Download file\n");
204 printf("lls [ls-options [path]] Display local directory listing\n");
205 printf("ln oldpath newpath Symlink remote file\n");
206 printf("lmkdir path Create local directory\n");
207 printf("lpwd Print local working directory\n");
208 printf("ls [path] Display remote directory listing\n");
209 printf("lumask umask Set local umask to 'umask'\n");
210 printf("mkdir path Create remote directory\n");
211 printf("progress Toggle display of progress meter\n");
212 printf("put local-path [remote-path] Upload file\n");
213 printf("pwd Display remote working directory\n");
214 printf("exit Quit sftp\n");
215 printf("quit Quit sftp\n");
216 printf("rename oldpath newpath Rename remote file\n");
217 printf("rmdir path Remove remote directory\n");
218 printf("rm path Delete remote file\n");
219 printf("symlink oldpath newpath Symlink remote file\n");
220 printf("version Show SFTP version\n");
221 printf("!command Execute 'command' in local shell\n");
222 printf("! Escape to local shell\n");
223 printf("? Synonym for help\n");
224}
225
226static void
227local_do_shell(const char *args)
228{
229 int status;
230 char *shell;
231 pid_t pid;
232
233 if (!*args)
234 args = NULL;
235
236 if ((shell = getenv("SHELL")) == NULL)
237 shell = _PATH_BSHELL;
238
239 if ((pid = fork()) == -1)
240 fatal("Couldn't fork: %s", strerror(errno));
241
242 if (pid == 0) {
243 /* XXX: child has pipe fds to ssh subproc open - issue? */
244 if (args) {
245 debug3("Executing %s -c \"%s\"", shell, args);
246 execl(shell, shell, "-c", args, (char *)NULL);
247 } else {
248 debug3("Executing %s", shell);
249 execl(shell, shell, (char *)NULL);
250 }
251 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
252 strerror(errno));
253 _exit(1);
254 }
255 while (waitpid(pid, &status, 0) == -1)
256 if (errno != EINTR)
257 fatal("Couldn't wait for child: %s", strerror(errno));
258 if (!WIFEXITED(status))
Damien Miller55b04f12006-03-26 14:23:17 +1100259 error("Shell exited abnormally");
Damien Miller20e1fab2004-02-18 14:30:55 +1100260 else if (WEXITSTATUS(status))
261 error("Shell exited with status %d", WEXITSTATUS(status));
262}
263
264static void
265local_do_ls(const char *args)
266{
267 if (!args || !*args)
268 local_do_shell(_PATH_LS);
269 else {
270 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
271 char *buf = xmalloc(len);
272
273 /* XXX: quoting - rip quoting code from ftp? */
274 snprintf(buf, len, _PATH_LS " %s", args);
275 local_do_shell(buf);
276 xfree(buf);
277 }
278}
279
280/* Strip one path (usually the pwd) from the start of another */
281static char *
282path_strip(char *path, char *strip)
283{
284 size_t len;
285
286 if (strip == NULL)
287 return (xstrdup(path));
288
289 len = strlen(strip);
Darren Tuckere2f189a2004-12-06 22:45:53 +1100290 if (strncmp(path, strip, len) == 0) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100291 if (strip[len - 1] != '/' && path[len] == '/')
292 len++;
293 return (xstrdup(path + len));
294 }
295
296 return (xstrdup(path));
297}
298
299static char *
300path_append(char *p1, char *p2)
301{
302 char *ret;
Damien Miller3ca8b772007-01-05 16:24:47 +1100303 size_t len = strlen(p1) + strlen(p2) + 2;
Damien Miller20e1fab2004-02-18 14:30:55 +1100304
305 ret = xmalloc(len);
306 strlcpy(ret, p1, len);
Damien Miller3ca8b772007-01-05 16:24:47 +1100307 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
Damien Miller20e1fab2004-02-18 14:30:55 +1100308 strlcat(ret, "/", len);
309 strlcat(ret, p2, len);
310
311 return(ret);
312}
313
314static char *
315make_absolute(char *p, char *pwd)
316{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000317 char *abs_str;
Damien Miller20e1fab2004-02-18 14:30:55 +1100318
319 /* Derelativise */
320 if (p && p[0] != '/') {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000321 abs_str = path_append(pwd, p);
Damien Miller20e1fab2004-02-18 14:30:55 +1100322 xfree(p);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000323 return(abs_str);
Damien Miller20e1fab2004-02-18 14:30:55 +1100324 } else
325 return(p);
326}
327
328static int
329infer_path(const char *p, char **ifp)
330{
331 char *cp;
332
333 cp = strrchr(p, '/');
334 if (cp == NULL) {
335 *ifp = xstrdup(p);
336 return(0);
337 }
338
339 if (!cp[1]) {
340 error("Invalid path");
341 return(-1);
342 }
343
344 *ifp = xstrdup(cp + 1);
345 return(0);
346}
347
348static int
349parse_getput_flags(const char **cpp, int *pflag)
350{
351 const char *cp = *cpp;
352
353 /* Check for flags */
354 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
355 switch (cp[1]) {
356 case 'p':
357 case 'P':
358 *pflag = 1;
359 break;
360 default:
361 error("Invalid flag -%c", cp[1]);
362 return(-1);
363 }
364 cp += 2;
365 *cpp = cp + strspn(cp, WHITESPACE);
366 }
367
368 return(0);
369}
370
371static int
372parse_ls_flags(const char **cpp, int *lflag)
373{
374 const char *cp = *cpp;
375
Darren Tuckerb9123452004-06-22 13:06:45 +1000376 /* Defaults */
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000377 *lflag = LS_NAME_SORT;
Darren Tuckerb9123452004-06-22 13:06:45 +1000378
Damien Miller20e1fab2004-02-18 14:30:55 +1100379 /* Check for flags */
380 if (cp++[0] == '-') {
Darren Tucker47eede72005-03-14 23:08:12 +1100381 for (; strchr(WHITESPACE, *cp) == NULL; cp++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100382 switch (*cp) {
383 case 'l':
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000384 *lflag &= ~VIEW_FLAGS;
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000385 *lflag |= LS_LONG_VIEW;
Damien Miller20e1fab2004-02-18 14:30:55 +1100386 break;
387 case '1':
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000388 *lflag &= ~VIEW_FLAGS;
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000389 *lflag |= LS_SHORT_VIEW;
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000390 break;
391 case 'n':
392 *lflag &= ~VIEW_FLAGS;
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000393 *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
Damien Miller20e1fab2004-02-18 14:30:55 +1100394 break;
Darren Tuckerb9123452004-06-22 13:06:45 +1000395 case 'S':
396 *lflag &= ~SORT_FLAGS;
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000397 *lflag |= LS_SIZE_SORT;
Darren Tuckerb9123452004-06-22 13:06:45 +1000398 break;
399 case 't':
400 *lflag &= ~SORT_FLAGS;
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000401 *lflag |= LS_TIME_SORT;
Darren Tuckerb9123452004-06-22 13:06:45 +1000402 break;
403 case 'r':
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000404 *lflag |= LS_REVERSE_SORT;
Darren Tuckerb9123452004-06-22 13:06:45 +1000405 break;
406 case 'f':
407 *lflag &= ~SORT_FLAGS;
408 break;
Darren Tucker9a526452004-06-22 13:09:55 +1000409 case 'a':
410 *lflag |= LS_SHOW_ALL;
411 break;
Damien Miller20e1fab2004-02-18 14:30:55 +1100412 default:
413 error("Invalid flag -%c", *cp);
414 return(-1);
415 }
416 }
417 *cpp = cp + strspn(cp, WHITESPACE);
418 }
419
420 return(0);
421}
422
423static int
424get_pathname(const char **cpp, char **path)
425{
426 const char *cp = *cpp, *end;
427 char quot;
Damien Millereccb9de2005-06-17 12:59:34 +1000428 u_int i, j;
Damien Miller20e1fab2004-02-18 14:30:55 +1100429
430 cp += strspn(cp, WHITESPACE);
431 if (!*cp) {
432 *cpp = cp;
433 *path = NULL;
434 return (0);
435 }
436
437 *path = xmalloc(strlen(cp) + 1);
438
439 /* Check for quoted filenames */
440 if (*cp == '\"' || *cp == '\'') {
441 quot = *cp++;
442
443 /* Search for terminating quote, unescape some chars */
444 for (i = j = 0; i <= strlen(cp); i++) {
445 if (cp[i] == quot) { /* Found quote */
446 i++;
447 (*path)[j] = '\0';
448 break;
449 }
450 if (cp[i] == '\0') { /* End of string */
451 error("Unterminated quote");
452 goto fail;
453 }
454 if (cp[i] == '\\') { /* Escaped characters */
455 i++;
456 if (cp[i] != '\'' && cp[i] != '\"' &&
457 cp[i] != '\\') {
Damien Miller96d6d7d2004-06-26 09:21:06 +1000458 error("Bad escaped character '\\%c'",
Damien Miller20e1fab2004-02-18 14:30:55 +1100459 cp[i]);
460 goto fail;
461 }
462 }
463 (*path)[j++] = cp[i];
464 }
465
466 if (j == 0) {
467 error("Empty quotes");
468 goto fail;
469 }
470 *cpp = cp + i + strspn(cp + i, WHITESPACE);
471 } else {
472 /* Read to end of filename */
473 end = strpbrk(cp, WHITESPACE);
474 if (end == NULL)
475 end = strchr(cp, '\0');
476 *cpp = end + strspn(end, WHITESPACE);
477
478 memcpy(*path, cp, end - cp);
479 (*path)[end - cp] = '\0';
480 }
481 return (0);
482
483 fail:
484 xfree(*path);
485 *path = NULL;
486 return (-1);
487}
488
489static int
490is_dir(char *path)
491{
492 struct stat sb;
493
494 /* XXX: report errors? */
495 if (stat(path, &sb) == -1)
496 return(0);
497
Darren Tucker1e80e402006-09-21 12:59:33 +1000498 return(S_ISDIR(sb.st_mode));
Damien Miller20e1fab2004-02-18 14:30:55 +1100499}
500
501static int
502is_reg(char *path)
503{
504 struct stat sb;
505
506 if (stat(path, &sb) == -1)
507 fatal("stat %s: %s", path, strerror(errno));
508
509 return(S_ISREG(sb.st_mode));
510}
511
512static int
513remote_is_dir(struct sftp_conn *conn, char *path)
514{
515 Attrib *a;
516
517 /* XXX: report errors? */
518 if ((a = do_stat(conn, path, 1)) == NULL)
519 return(0);
520 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
521 return(0);
Darren Tucker1e80e402006-09-21 12:59:33 +1000522 return(S_ISDIR(a->perm));
Damien Miller20e1fab2004-02-18 14:30:55 +1100523}
524
525static int
526process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
527{
528 char *abs_src = NULL;
529 char *abs_dst = NULL;
530 char *tmp;
531 glob_t g;
532 int err = 0;
533 int i;
534
535 abs_src = xstrdup(src);
536 abs_src = make_absolute(abs_src, pwd);
537
538 memset(&g, 0, sizeof(g));
539 debug3("Looking up %s", abs_src);
540 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
541 error("File \"%s\" not found.", abs_src);
542 err = -1;
543 goto out;
544 }
545
546 /* If multiple matches, dst must be a directory or unspecified */
547 if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
548 error("Multiple files match, but \"%s\" is not a directory",
549 dst);
550 err = -1;
551 goto out;
552 }
553
Darren Tuckercdf547a2004-05-24 10:12:19 +1000554 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100555 if (infer_path(g.gl_pathv[i], &tmp)) {
556 err = -1;
557 goto out;
558 }
559
560 if (g.gl_matchc == 1 && dst) {
561 /* If directory specified, append filename */
Damien Miller40b59852006-06-13 13:00:25 +1000562 xfree(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +1100563 if (is_dir(dst)) {
564 if (infer_path(g.gl_pathv[0], &tmp)) {
565 err = 1;
566 goto out;
567 }
568 abs_dst = path_append(dst, tmp);
569 xfree(tmp);
570 } else
571 abs_dst = xstrdup(dst);
572 } else if (dst) {
573 abs_dst = path_append(dst, tmp);
574 xfree(tmp);
575 } else
576 abs_dst = tmp;
577
578 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
579 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
580 err = -1;
581 xfree(abs_dst);
582 abs_dst = NULL;
583 }
584
585out:
586 xfree(abs_src);
Damien Miller20e1fab2004-02-18 14:30:55 +1100587 globfree(&g);
588 return(err);
589}
590
591static int
592process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
593{
594 char *tmp_dst = NULL;
595 char *abs_dst = NULL;
596 char *tmp;
597 glob_t g;
598 int err = 0;
599 int i;
600
601 if (dst) {
602 tmp_dst = xstrdup(dst);
603 tmp_dst = make_absolute(tmp_dst, pwd);
604 }
605
606 memset(&g, 0, sizeof(g));
607 debug3("Looking up %s", src);
608 if (glob(src, 0, NULL, &g)) {
609 error("File \"%s\" not found.", src);
610 err = -1;
611 goto out;
612 }
613
614 /* If multiple matches, dst may be directory or unspecified */
615 if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
616 error("Multiple files match, but \"%s\" is not a directory",
617 tmp_dst);
618 err = -1;
619 goto out;
620 }
621
Darren Tuckercdf547a2004-05-24 10:12:19 +1000622 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100623 if (!is_reg(g.gl_pathv[i])) {
624 error("skipping non-regular file %s",
625 g.gl_pathv[i]);
626 continue;
627 }
628 if (infer_path(g.gl_pathv[i], &tmp)) {
629 err = -1;
630 goto out;
631 }
632
633 if (g.gl_matchc == 1 && tmp_dst) {
634 /* If directory specified, append filename */
635 if (remote_is_dir(conn, tmp_dst)) {
636 if (infer_path(g.gl_pathv[0], &tmp)) {
637 err = 1;
638 goto out;
639 }
640 abs_dst = path_append(tmp_dst, tmp);
641 xfree(tmp);
642 } else
643 abs_dst = xstrdup(tmp_dst);
644
645 } else if (tmp_dst) {
646 abs_dst = path_append(tmp_dst, tmp);
647 xfree(tmp);
648 } else
649 abs_dst = make_absolute(tmp, pwd);
650
651 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
652 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
653 err = -1;
654 }
655
656out:
657 if (abs_dst)
658 xfree(abs_dst);
659 if (tmp_dst)
660 xfree(tmp_dst);
661 globfree(&g);
662 return(err);
663}
664
665static int
666sdirent_comp(const void *aa, const void *bb)
667{
668 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
669 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000670 int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100671
Darren Tuckerb9123452004-06-22 13:06:45 +1000672#define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000673 if (sort_flag & LS_NAME_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000674 return (rmul * strcmp(a->filename, b->filename));
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000675 else if (sort_flag & LS_TIME_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000676 return (rmul * NCMP(a->a.mtime, b->a.mtime));
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000677 else if (sort_flag & LS_SIZE_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000678 return (rmul * NCMP(a->a.size, b->a.size));
679
680 fatal("Unknown ls sort type");
Damien Miller20e1fab2004-02-18 14:30:55 +1100681}
682
683/* sftp ls.1 replacement for directories */
684static int
685do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
686{
Damien Millereccb9de2005-06-17 12:59:34 +1000687 int n;
688 u_int c = 1, colspace = 0, columns = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100689 SFTP_DIRENT **d;
690
691 if ((n = do_readdir(conn, path, &d)) != 0)
692 return (n);
693
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000694 if (!(lflag & LS_SHORT_VIEW)) {
Damien Millereccb9de2005-06-17 12:59:34 +1000695 u_int m = 0, width = 80;
Damien Miller20e1fab2004-02-18 14:30:55 +1100696 struct winsize ws;
697 char *tmp;
698
699 /* Count entries for sort and find longest filename */
Darren Tucker9a526452004-06-22 13:09:55 +1000700 for (n = 0; d[n] != NULL; n++) {
701 if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
702 m = MAX(m, strlen(d[n]->filename));
703 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100704
705 /* Add any subpath that also needs to be counted */
706 tmp = path_strip(path, strip_path);
707 m += strlen(tmp);
708 xfree(tmp);
709
710 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
711 width = ws.ws_col;
712
713 columns = width / (m + 2);
714 columns = MAX(columns, 1);
715 colspace = width / columns;
716 colspace = MIN(colspace, width);
717 }
718
Darren Tuckerb9123452004-06-22 13:06:45 +1000719 if (lflag & SORT_FLAGS) {
Damien Miller653b93b2005-11-05 15:15:23 +1100720 for (n = 0; d[n] != NULL; n++)
721 ; /* count entries */
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000722 sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
Darren Tuckerb9123452004-06-22 13:06:45 +1000723 qsort(d, n, sizeof(*d), sdirent_comp);
724 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100725
Darren Tuckercdf547a2004-05-24 10:12:19 +1000726 for (n = 0; d[n] != NULL && !interrupted; n++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100727 char *tmp, *fname;
728
Darren Tucker9a526452004-06-22 13:09:55 +1000729 if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
730 continue;
731
Damien Miller20e1fab2004-02-18 14:30:55 +1100732 tmp = path_append(path, d[n]->filename);
733 fname = path_strip(tmp, strip_path);
734 xfree(tmp);
735
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000736 if (lflag & LS_LONG_VIEW) {
737 if (lflag & LS_NUMERIC_VIEW) {
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000738 char *lname;
739 struct stat sb;
Damien Miller20e1fab2004-02-18 14:30:55 +1100740
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000741 memset(&sb, 0, sizeof(sb));
742 attrib_to_stat(&d[n]->a, &sb);
743 lname = ls_file(fname, &sb, 1);
744 printf("%s\n", lname);
745 xfree(lname);
746 } else
747 printf("%s\n", d[n]->longname);
Damien Miller20e1fab2004-02-18 14:30:55 +1100748 } else {
749 printf("%-*s", colspace, fname);
750 if (c >= columns) {
751 printf("\n");
752 c = 1;
753 } else
754 c++;
755 }
756
757 xfree(fname);
758 }
759
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000760 if (!(lflag & LS_LONG_VIEW) && (c != 1))
Damien Miller20e1fab2004-02-18 14:30:55 +1100761 printf("\n");
762
763 free_sftp_dirents(d);
764 return (0);
765}
766
767/* sftp ls.1 replacement which handles path globs */
768static int
769do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
770 int lflag)
771{
772 glob_t g;
Damien Millereccb9de2005-06-17 12:59:34 +1000773 u_int i, c = 1, colspace = 0, columns = 1;
Darren Tucker596dcfa2004-12-11 13:37:22 +1100774 Attrib *a = NULL;
Damien Miller20e1fab2004-02-18 14:30:55 +1100775
776 memset(&g, 0, sizeof(g));
777
778 if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
Darren Tucker596dcfa2004-12-11 13:37:22 +1100779 NULL, &g) || (g.gl_pathc && !g.gl_matchc)) {
780 if (g.gl_pathc)
781 globfree(&g);
Damien Miller20e1fab2004-02-18 14:30:55 +1100782 error("Can't ls: \"%s\" not found", path);
783 return (-1);
784 }
785
Darren Tuckercdf547a2004-05-24 10:12:19 +1000786 if (interrupted)
787 goto out;
788
Damien Miller20e1fab2004-02-18 14:30:55 +1100789 /*
Darren Tucker596dcfa2004-12-11 13:37:22 +1100790 * If the glob returns a single match and it is a directory,
791 * then just list its contents.
Damien Miller20e1fab2004-02-18 14:30:55 +1100792 */
Darren Tucker596dcfa2004-12-11 13:37:22 +1100793 if (g.gl_matchc == 1) {
794 if ((a = do_lstat(conn, g.gl_pathv[0], 1)) == NULL) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100795 globfree(&g);
796 return (-1);
797 }
798 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
799 S_ISDIR(a->perm)) {
Darren Tucker596dcfa2004-12-11 13:37:22 +1100800 int err;
801
802 err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
Damien Miller20e1fab2004-02-18 14:30:55 +1100803 globfree(&g);
Darren Tucker596dcfa2004-12-11 13:37:22 +1100804 return (err);
Damien Miller20e1fab2004-02-18 14:30:55 +1100805 }
806 }
807
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000808 if (!(lflag & LS_SHORT_VIEW)) {
Damien Millereccb9de2005-06-17 12:59:34 +1000809 u_int m = 0, width = 80;
Damien Miller20e1fab2004-02-18 14:30:55 +1100810 struct winsize ws;
811
812 /* Count entries for sort and find longest filename */
813 for (i = 0; g.gl_pathv[i]; i++)
814 m = MAX(m, strlen(g.gl_pathv[i]));
815
816 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
817 width = ws.ws_col;
818
819 columns = width / (m + 2);
820 columns = MAX(columns, 1);
821 colspace = width / columns;
822 }
823
Darren Tucker596dcfa2004-12-11 13:37:22 +1100824 for (i = 0; g.gl_pathv[i] && !interrupted; i++, a = NULL) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100825 char *fname;
826
827 fname = path_strip(g.gl_pathv[i], strip_path);
828
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000829 if (lflag & LS_LONG_VIEW) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100830 char *lname;
831 struct stat sb;
832
833 /*
834 * XXX: this is slow - 1 roundtrip per path
835 * A solution to this is to fork glob() and
836 * build a sftp specific version which keeps the
837 * attribs (which currently get thrown away)
838 * that the server returns as well as the filenames.
839 */
840 memset(&sb, 0, sizeof(sb));
Darren Tucker596dcfa2004-12-11 13:37:22 +1100841 if (a == NULL)
842 a = do_lstat(conn, g.gl_pathv[i], 1);
Damien Miller20e1fab2004-02-18 14:30:55 +1100843 if (a != NULL)
844 attrib_to_stat(a, &sb);
845 lname = ls_file(fname, &sb, 1);
846 printf("%s\n", lname);
847 xfree(lname);
848 } else {
849 printf("%-*s", colspace, fname);
850 if (c >= columns) {
851 printf("\n");
852 c = 1;
853 } else
854 c++;
855 }
856 xfree(fname);
857 }
858
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000859 if (!(lflag & LS_LONG_VIEW) && (c != 1))
Damien Miller20e1fab2004-02-18 14:30:55 +1100860 printf("\n");
861
Darren Tuckercdf547a2004-05-24 10:12:19 +1000862 out:
Damien Miller20e1fab2004-02-18 14:30:55 +1100863 if (g.gl_pathc)
864 globfree(&g);
865
866 return (0);
867}
868
869static int
870parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
871 unsigned long *n_arg, char **path1, char **path2)
872{
873 const char *cmd, *cp = *cpp;
874 char *cp2;
875 int base = 0;
876 long l;
877 int i, cmdnum;
878
879 /* Skip leading whitespace */
880 cp = cp + strspn(cp, WHITESPACE);
881
882 /* Ignore blank lines and lines which begin with comment '#' char */
883 if (*cp == '\0' || *cp == '#')
884 return (0);
885
886 /* Check for leading '-' (disable error processing) */
887 *iflag = 0;
888 if (*cp == '-') {
889 *iflag = 1;
890 cp++;
891 }
892
893 /* Figure out which command we have */
894 for (i = 0; cmds[i].c; i++) {
895 int cmdlen = strlen(cmds[i].c);
896
897 /* Check for command followed by whitespace */
898 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
899 strchr(WHITESPACE, cp[cmdlen])) {
900 cp += cmdlen;
901 cp = cp + strspn(cp, WHITESPACE);
902 break;
903 }
904 }
905 cmdnum = cmds[i].n;
906 cmd = cmds[i].c;
907
908 /* Special case */
909 if (*cp == '!') {
910 cp++;
911 cmdnum = I_SHELL;
912 } else if (cmdnum == -1) {
913 error("Invalid command.");
914 return (-1);
915 }
916
917 /* Get arguments and parse flags */
918 *lflag = *pflag = *n_arg = 0;
919 *path1 = *path2 = NULL;
920 switch (cmdnum) {
921 case I_GET:
922 case I_PUT:
923 if (parse_getput_flags(&cp, pflag))
924 return(-1);
925 /* Get first pathname (mandatory) */
926 if (get_pathname(&cp, path1))
927 return(-1);
928 if (*path1 == NULL) {
929 error("You must specify at least one path after a "
930 "%s command.", cmd);
931 return(-1);
932 }
933 /* Try to get second pathname (optional) */
934 if (get_pathname(&cp, path2))
935 return(-1);
936 break;
937 case I_RENAME:
938 case I_SYMLINK:
939 if (get_pathname(&cp, path1))
940 return(-1);
941 if (get_pathname(&cp, path2))
942 return(-1);
943 if (!*path1 || !*path2) {
944 error("You must specify two paths after a %s "
945 "command.", cmd);
946 return(-1);
947 }
948 break;
949 case I_RM:
950 case I_MKDIR:
951 case I_RMDIR:
952 case I_CHDIR:
953 case I_LCHDIR:
954 case I_LMKDIR:
955 /* Get pathname (mandatory) */
956 if (get_pathname(&cp, path1))
957 return(-1);
958 if (*path1 == NULL) {
959 error("You must specify a path after a %s command.",
960 cmd);
961 return(-1);
962 }
963 break;
964 case I_LS:
965 if (parse_ls_flags(&cp, lflag))
966 return(-1);
967 /* Path is optional */
968 if (get_pathname(&cp, path1))
969 return(-1);
970 break;
971 case I_LLS:
972 case I_SHELL:
973 /* Uses the rest of the line */
974 break;
975 case I_LUMASK:
976 base = 8;
977 case I_CHMOD:
978 base = 8;
979 case I_CHOWN:
980 case I_CHGRP:
981 /* Get numeric arg (mandatory) */
Damien Millere7658a52006-10-24 03:00:12 +1000982 errno = 0;
Damien Miller20e1fab2004-02-18 14:30:55 +1100983 l = strtol(cp, &cp2, base);
984 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
985 errno == ERANGE) || l < 0) {
986 error("You must supply a numeric argument "
987 "to the %s command.", cmd);
988 return(-1);
989 }
990 cp = cp2;
991 *n_arg = l;
992 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
993 break;
994 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
995 error("You must supply a numeric argument "
996 "to the %s command.", cmd);
997 return(-1);
998 }
999 cp += strspn(cp, WHITESPACE);
1000
1001 /* Get pathname (mandatory) */
1002 if (get_pathname(&cp, path1))
1003 return(-1);
1004 if (*path1 == NULL) {
1005 error("You must specify a path after a %s command.",
1006 cmd);
1007 return(-1);
1008 }
1009 break;
1010 case I_QUIT:
1011 case I_PWD:
1012 case I_LPWD:
1013 case I_HELP:
1014 case I_VERSION:
1015 case I_PROGRESS:
1016 break;
1017 default:
1018 fatal("Command not implemented");
1019 }
1020
1021 *cpp = cp;
1022 return(cmdnum);
1023}
1024
1025static int
1026parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
1027 int err_abort)
1028{
1029 char *path1, *path2, *tmp;
1030 int pflag, lflag, iflag, cmdnum, i;
1031 unsigned long n_arg;
1032 Attrib a, *aa;
1033 char path_buf[MAXPATHLEN];
1034 int err = 0;
1035 glob_t g;
1036
1037 path1 = path2 = NULL;
1038 cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
1039 &path1, &path2);
1040
1041 if (iflag != 0)
1042 err_abort = 0;
1043
1044 memset(&g, 0, sizeof(g));
1045
1046 /* Perform command */
1047 switch (cmdnum) {
1048 case 0:
1049 /* Blank line */
1050 break;
1051 case -1:
1052 /* Unrecognized command */
1053 err = -1;
1054 break;
1055 case I_GET:
1056 err = process_get(conn, path1, path2, *pwd, pflag);
1057 break;
1058 case I_PUT:
1059 err = process_put(conn, path1, path2, *pwd, pflag);
1060 break;
1061 case I_RENAME:
1062 path1 = make_absolute(path1, *pwd);
1063 path2 = make_absolute(path2, *pwd);
1064 err = do_rename(conn, path1, path2);
1065 break;
1066 case I_SYMLINK:
1067 path2 = make_absolute(path2, *pwd);
1068 err = do_symlink(conn, path1, path2);
1069 break;
1070 case I_RM:
1071 path1 = make_absolute(path1, *pwd);
1072 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001073 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001074 printf("Removing %s\n", g.gl_pathv[i]);
1075 err = do_rm(conn, g.gl_pathv[i]);
1076 if (err != 0 && err_abort)
1077 break;
1078 }
1079 break;
1080 case I_MKDIR:
1081 path1 = make_absolute(path1, *pwd);
1082 attrib_clear(&a);
1083 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1084 a.perm = 0777;
1085 err = do_mkdir(conn, path1, &a);
1086 break;
1087 case I_RMDIR:
1088 path1 = make_absolute(path1, *pwd);
1089 err = do_rmdir(conn, path1);
1090 break;
1091 case I_CHDIR:
1092 path1 = make_absolute(path1, *pwd);
1093 if ((tmp = do_realpath(conn, path1)) == NULL) {
1094 err = 1;
1095 break;
1096 }
1097 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1098 xfree(tmp);
1099 err = 1;
1100 break;
1101 }
1102 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
1103 error("Can't change directory: Can't check target");
1104 xfree(tmp);
1105 err = 1;
1106 break;
1107 }
1108 if (!S_ISDIR(aa->perm)) {
1109 error("Can't change directory: \"%s\" is not "
1110 "a directory", tmp);
1111 xfree(tmp);
1112 err = 1;
1113 break;
1114 }
1115 xfree(*pwd);
1116 *pwd = tmp;
1117 break;
1118 case I_LS:
1119 if (!path1) {
1120 do_globbed_ls(conn, *pwd, *pwd, lflag);
1121 break;
1122 }
1123
1124 /* Strip pwd off beginning of non-absolute paths */
1125 tmp = NULL;
1126 if (*path1 != '/')
1127 tmp = *pwd;
1128
1129 path1 = make_absolute(path1, *pwd);
1130 err = do_globbed_ls(conn, path1, tmp, lflag);
1131 break;
1132 case I_LCHDIR:
1133 if (chdir(path1) == -1) {
1134 error("Couldn't change local directory to "
1135 "\"%s\": %s", path1, strerror(errno));
1136 err = 1;
1137 }
1138 break;
1139 case I_LMKDIR:
1140 if (mkdir(path1, 0777) == -1) {
1141 error("Couldn't create local directory "
1142 "\"%s\": %s", path1, strerror(errno));
1143 err = 1;
1144 }
1145 break;
1146 case I_LLS:
1147 local_do_ls(cmd);
1148 break;
1149 case I_SHELL:
1150 local_do_shell(cmd);
1151 break;
1152 case I_LUMASK:
1153 umask(n_arg);
1154 printf("Local umask: %03lo\n", n_arg);
1155 break;
1156 case I_CHMOD:
1157 path1 = make_absolute(path1, *pwd);
1158 attrib_clear(&a);
1159 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1160 a.perm = n_arg;
1161 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001162 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001163 printf("Changing mode on %s\n", g.gl_pathv[i]);
1164 err = do_setstat(conn, g.gl_pathv[i], &a);
1165 if (err != 0 && err_abort)
1166 break;
1167 }
1168 break;
1169 case I_CHOWN:
1170 case I_CHGRP:
1171 path1 = make_absolute(path1, *pwd);
1172 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001173 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001174 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1175 if (err != 0 && err_abort)
1176 break;
1177 else
1178 continue;
1179 }
1180 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1181 error("Can't get current ownership of "
1182 "remote file \"%s\"", g.gl_pathv[i]);
1183 if (err != 0 && err_abort)
1184 break;
1185 else
1186 continue;
1187 }
1188 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1189 if (cmdnum == I_CHOWN) {
1190 printf("Changing owner on %s\n", g.gl_pathv[i]);
1191 aa->uid = n_arg;
1192 } else {
1193 printf("Changing group on %s\n", g.gl_pathv[i]);
1194 aa->gid = n_arg;
1195 }
1196 err = do_setstat(conn, g.gl_pathv[i], aa);
1197 if (err != 0 && err_abort)
1198 break;
1199 }
1200 break;
1201 case I_PWD:
1202 printf("Remote working directory: %s\n", *pwd);
1203 break;
1204 case I_LPWD:
1205 if (!getcwd(path_buf, sizeof(path_buf))) {
1206 error("Couldn't get local cwd: %s", strerror(errno));
1207 err = -1;
1208 break;
1209 }
1210 printf("Local working directory: %s\n", path_buf);
1211 break;
1212 case I_QUIT:
1213 /* Processed below */
1214 break;
1215 case I_HELP:
1216 help();
1217 break;
1218 case I_VERSION:
1219 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1220 break;
1221 case I_PROGRESS:
1222 showprogress = !showprogress;
1223 if (showprogress)
1224 printf("Progress meter enabled\n");
1225 else
1226 printf("Progress meter disabled\n");
1227 break;
1228 default:
1229 fatal("%d is not implemented", cmdnum);
1230 }
1231
1232 if (g.gl_pathc)
1233 globfree(&g);
1234 if (path1)
1235 xfree(path1);
1236 if (path2)
1237 xfree(path2);
1238
1239 /* If an unignored error occurs in batch mode we should abort. */
1240 if (err_abort && err != 0)
1241 return (-1);
1242 else if (cmdnum == I_QUIT)
1243 return (1);
1244
1245 return (0);
1246}
1247
Darren Tucker2d963d82004-11-07 20:04:10 +11001248#ifdef USE_LIBEDIT
1249static char *
1250prompt(EditLine *el)
1251{
1252 return ("sftp> ");
1253}
1254#endif
1255
Damien Miller20e1fab2004-02-18 14:30:55 +11001256int
1257interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1258{
1259 char *pwd;
1260 char *dir = NULL;
1261 char cmd[2048];
1262 struct sftp_conn *conn;
Damien Miller0e2c1022005-08-12 22:16:22 +10001263 int err, interactive;
Darren Tucker2d963d82004-11-07 20:04:10 +11001264 EditLine *el = NULL;
1265#ifdef USE_LIBEDIT
1266 History *hl = NULL;
1267 HistEvent hev;
1268 extern char *__progname;
1269
1270 if (!batchmode && isatty(STDIN_FILENO)) {
1271 if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL)
1272 fatal("Couldn't initialise editline");
1273 if ((hl = history_init()) == NULL)
1274 fatal("Couldn't initialise editline history");
1275 history(hl, &hev, H_SETSIZE, 100);
1276 el_set(el, EL_HIST, history, hl);
1277
1278 el_set(el, EL_PROMPT, prompt);
1279 el_set(el, EL_EDITOR, "emacs");
1280 el_set(el, EL_TERMINAL, NULL);
1281 el_set(el, EL_SIGNAL, 1);
1282 el_source(el, NULL);
1283 }
1284#endif /* USE_LIBEDIT */
Damien Miller20e1fab2004-02-18 14:30:55 +11001285
1286 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1287 if (conn == NULL)
1288 fatal("Couldn't initialise connection to server");
1289
1290 pwd = do_realpath(conn, ".");
1291 if (pwd == NULL)
1292 fatal("Need cwd");
1293
1294 if (file1 != NULL) {
1295 dir = xstrdup(file1);
1296 dir = make_absolute(dir, pwd);
1297
1298 if (remote_is_dir(conn, dir) && file2 == NULL) {
1299 printf("Changing to: %s\n", dir);
1300 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
Darren Tuckercd516ef2004-12-06 22:43:43 +11001301 if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) {
1302 xfree(dir);
1303 xfree(pwd);
Damien Millere0b90a62006-03-26 13:51:44 +11001304 xfree(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001305 return (-1);
Darren Tuckercd516ef2004-12-06 22:43:43 +11001306 }
Damien Miller20e1fab2004-02-18 14:30:55 +11001307 } else {
1308 if (file2 == NULL)
1309 snprintf(cmd, sizeof cmd, "get %s", dir);
1310 else
1311 snprintf(cmd, sizeof cmd, "get %s %s", dir,
1312 file2);
1313
1314 err = parse_dispatch_command(conn, cmd, &pwd, 1);
1315 xfree(dir);
1316 xfree(pwd);
Damien Millere0b90a62006-03-26 13:51:44 +11001317 xfree(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001318 return (err);
1319 }
1320 xfree(dir);
1321 }
1322
Darren Tucker93e7e8f2005-08-23 08:06:55 +10001323#if defined(HAVE_SETVBUF) && !defined(BROKEN_SETVBUF)
Damien Miller20e1fab2004-02-18 14:30:55 +11001324 setvbuf(stdout, NULL, _IOLBF, 0);
1325 setvbuf(infile, NULL, _IOLBF, 0);
1326#else
Damien Miller37294fb2005-07-17 17:18:49 +10001327 setlinebuf(stdout);
1328 setlinebuf(infile);
Damien Miller20e1fab2004-02-18 14:30:55 +11001329#endif
1330
Damien Miller0e2c1022005-08-12 22:16:22 +10001331 interactive = !batchmode && isatty(STDIN_FILENO);
Damien Miller20e1fab2004-02-18 14:30:55 +11001332 err = 0;
1333 for (;;) {
1334 char *cp;
1335
Darren Tuckercdf547a2004-05-24 10:12:19 +10001336 signal(SIGINT, SIG_IGN);
1337
Darren Tucker2d963d82004-11-07 20:04:10 +11001338 if (el == NULL) {
Damien Miller0e2c1022005-08-12 22:16:22 +10001339 if (interactive)
1340 printf("sftp> ");
Darren Tucker2d963d82004-11-07 20:04:10 +11001341 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller0e2c1022005-08-12 22:16:22 +10001342 if (interactive)
1343 printf("\n");
Darren Tucker2d963d82004-11-07 20:04:10 +11001344 break;
1345 }
Damien Miller0e2c1022005-08-12 22:16:22 +10001346 if (!interactive) { /* Echo command */
1347 printf("sftp> %s", cmd);
1348 if (strlen(cmd) > 0 &&
1349 cmd[strlen(cmd) - 1] != '\n')
1350 printf("\n");
1351 }
Darren Tucker2d963d82004-11-07 20:04:10 +11001352 } else {
1353#ifdef USE_LIBEDIT
1354 const char *line;
1355 int count = 0;
Damien Miller20e1fab2004-02-18 14:30:55 +11001356
Damien Miller0e2c1022005-08-12 22:16:22 +10001357 if ((line = el_gets(el, &count)) == NULL || count <= 0) {
1358 printf("\n");
1359 break;
1360 }
Darren Tucker2d963d82004-11-07 20:04:10 +11001361 history(hl, &hev, H_ENTER, line);
1362 if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
1363 fprintf(stderr, "Error: input line too long\n");
1364 continue;
1365 }
1366#endif /* USE_LIBEDIT */
Damien Miller20e1fab2004-02-18 14:30:55 +11001367 }
1368
Damien Miller20e1fab2004-02-18 14:30:55 +11001369 cp = strrchr(cmd, '\n');
1370 if (cp)
1371 *cp = '\0';
1372
Darren Tuckercdf547a2004-05-24 10:12:19 +10001373 /* Handle user interrupts gracefully during commands */
1374 interrupted = 0;
1375 signal(SIGINT, cmd_interrupt);
1376
Damien Miller20e1fab2004-02-18 14:30:55 +11001377 err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
1378 if (err != 0)
1379 break;
1380 }
1381 xfree(pwd);
Damien Millere0b90a62006-03-26 13:51:44 +11001382 xfree(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001383
Tim Rice027e8b12005-08-15 14:52:50 -07001384#ifdef USE_LIBEDIT
Damien Miller0e2c1022005-08-12 22:16:22 +10001385 if (el != NULL)
1386 el_end(el);
Tim Rice027e8b12005-08-15 14:52:50 -07001387#endif /* USE_LIBEDIT */
Damien Miller0e2c1022005-08-12 22:16:22 +10001388
Damien Miller20e1fab2004-02-18 14:30:55 +11001389 /* err == 1 signifies normal "quit" exit */
1390 return (err >= 0 ? 0 : -1);
1391}
Damien Miller62d57f62003-01-10 21:43:24 +11001392
Ben Lindstrombba81212001-06-25 05:01:22 +00001393static void
Damien Millercc685c12003-06-04 22:51:38 +10001394connect_to_server(char *path, char **args, int *in, int *out)
Damien Miller33804262001-02-04 23:20:18 +11001395{
1396 int c_in, c_out;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001397
Damien Miller33804262001-02-04 23:20:18 +11001398#ifdef USE_PIPES
1399 int pin[2], pout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001400
Damien Miller33804262001-02-04 23:20:18 +11001401 if ((pipe(pin) == -1) || (pipe(pout) == -1))
1402 fatal("pipe: %s", strerror(errno));
1403 *in = pin[0];
1404 *out = pout[1];
1405 c_in = pout[0];
1406 c_out = pin[1];
1407#else /* USE_PIPES */
1408 int inout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001409
Damien Miller33804262001-02-04 23:20:18 +11001410 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
1411 fatal("socketpair: %s", strerror(errno));
1412 *in = *out = inout[0];
1413 c_in = c_out = inout[1];
1414#endif /* USE_PIPES */
1415
Damien Millercc685c12003-06-04 22:51:38 +10001416 if ((sshpid = fork()) == -1)
Damien Miller33804262001-02-04 23:20:18 +11001417 fatal("fork: %s", strerror(errno));
Damien Millercc685c12003-06-04 22:51:38 +10001418 else if (sshpid == 0) {
Damien Miller33804262001-02-04 23:20:18 +11001419 if ((dup2(c_in, STDIN_FILENO) == -1) ||
1420 (dup2(c_out, STDOUT_FILENO) == -1)) {
1421 fprintf(stderr, "dup2: %s\n", strerror(errno));
Damien Miller350327c2004-06-15 10:24:13 +10001422 _exit(1);
Damien Miller33804262001-02-04 23:20:18 +11001423 }
1424 close(*in);
1425 close(*out);
1426 close(c_in);
1427 close(c_out);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001428
1429 /*
1430 * The underlying ssh is in the same process group, so we must
Darren Tuckerfc959702004-07-17 16:12:08 +10001431 * ignore SIGINT if we want to gracefully abort commands,
1432 * otherwise the signal will make it to the ssh process and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001433 * kill it too
1434 */
1435 signal(SIGINT, SIG_IGN);
Darren Tuckerbd12f172004-06-18 16:23:43 +10001436 execvp(path, args);
Damien Millerd14ee1e2002-02-05 12:27:31 +11001437 fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
Damien Miller350327c2004-06-15 10:24:13 +10001438 _exit(1);
Damien Miller33804262001-02-04 23:20:18 +11001439 }
1440
Damien Millercc685c12003-06-04 22:51:38 +10001441 signal(SIGTERM, killchild);
1442 signal(SIGINT, killchild);
1443 signal(SIGHUP, killchild);
Damien Miller33804262001-02-04 23:20:18 +11001444 close(c_in);
1445 close(c_out);
1446}
1447
Ben Lindstrombba81212001-06-25 05:01:22 +00001448static void
Damien Miller33804262001-02-04 23:20:18 +11001449usage(void)
1450{
Damien Miller025e01c2002-02-08 22:06:29 +11001451 extern char *__progname;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001452
Ben Lindstrom1e243242001-09-18 05:38:44 +00001453 fprintf(stderr,
Darren Tucker1f203942003-10-15 15:50:42 +10001454 "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n"
1455 " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n"
1456 " [-S program] [-s subsystem | sftp_server] host\n"
1457 " %s [[user@]host[:file [file]]]\n"
1458 " %s [[user@]host[:dir[/]]]\n"
1459 " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname);
Damien Miller33804262001-02-04 23:20:18 +11001460 exit(1);
1461}
1462
Kevin Stevesef4eea92001-02-05 12:42:17 +00001463int
Damien Miller33804262001-02-04 23:20:18 +11001464main(int argc, char **argv)
1465{
Damien Miller956f3fb2003-01-10 21:40:00 +11001466 int in, out, ch, err;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001467 char *host, *userhost, *cp, *file2 = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +00001468 int debug_level = 0, sshver = 2;
1469 char *file1 = NULL, *sftp_server = NULL;
Damien Millerd14ee1e2002-02-05 12:27:31 +11001470 char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +00001471 LogLevel ll = SYSLOG_LEVEL_INFO;
1472 arglist args;
Damien Millerd7686fd2001-02-10 00:40:03 +11001473 extern int optind;
1474 extern char *optarg;
Damien Miller33804262001-02-04 23:20:18 +11001475
Darren Tuckerce321d82005-10-03 18:11:24 +10001476 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1477 sanitise_stdfd();
1478
Damien Miller59d3d5b2003-08-22 09:34:41 +10001479 __progname = ssh_get_progname(argv[0]);
Damien Miller3eec6b72006-01-31 21:49:27 +11001480 memset(&args, '\0', sizeof(args));
Ben Lindstrom387c4722001-05-08 20:27:25 +00001481 args.list = NULL;
Damien Miller2b5a0de2006-03-31 23:10:31 +11001482 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +00001483 addargs(&args, "-oForwardX11 no");
1484 addargs(&args, "-oForwardAgent no");
Damien Millerd27b9472005-12-13 19:29:02 +11001485 addargs(&args, "-oPermitLocalCommand no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001486 addargs(&args, "-oClearAllForwardings yes");
Damien Millere4f5a822004-01-21 14:11:05 +11001487
Ben Lindstrom387c4722001-05-08 20:27:25 +00001488 ll = SYSLOG_LEVEL_INFO;
Damien Millere4f5a822004-01-21 14:11:05 +11001489 infile = stdin;
Damien Millerd7686fd2001-02-10 00:40:03 +11001490
Damien Miller16a13332002-02-13 14:03:56 +11001491 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +11001492 switch (ch) {
1493 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +00001494 addargs(&args, "-C");
Damien Millerd7686fd2001-02-10 00:40:03 +11001495 break;
1496 case 'v':
Ben Lindstrom387c4722001-05-08 20:27:25 +00001497 if (debug_level < 3) {
1498 addargs(&args, "-v");
1499 ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
1500 }
1501 debug_level++;
Damien Millerd7686fd2001-02-10 00:40:03 +11001502 break;
Ben Lindstrom1e243242001-09-18 05:38:44 +00001503 case 'F':
Damien Millerd7686fd2001-02-10 00:40:03 +11001504 case 'o':
Ben Lindstrom1e243242001-09-18 05:38:44 +00001505 addargs(&args, "-%c%s", ch, optarg);
Damien Millerd7686fd2001-02-10 00:40:03 +11001506 break;
1507 case '1':
Ben Lindstrom387c4722001-05-08 20:27:25 +00001508 sshver = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +11001509 if (sftp_server == NULL)
1510 sftp_server = _PATH_SFTP_SERVER;
1511 break;
1512 case 's':
1513 sftp_server = optarg;
1514 break;
1515 case 'S':
1516 ssh_program = optarg;
Damien Miller3eec6b72006-01-31 21:49:27 +11001517 replacearg(&args, 0, "%s", ssh_program);
Damien Millerd7686fd2001-02-10 00:40:03 +11001518 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001519 case 'b':
Damien Miller44f75c12004-01-21 10:58:47 +11001520 if (batchmode)
1521 fatal("Batch file already specified.");
1522
1523 /* Allow "-" as stdin */
Darren Tuckerfc959702004-07-17 16:12:08 +10001524 if (strcmp(optarg, "-") != 0 &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001525 (infile = fopen(optarg, "r")) == NULL)
Damien Miller44f75c12004-01-21 10:58:47 +11001526 fatal("%s (%s).", strerror(errno), optarg);
Damien Miller62d57f62003-01-10 21:43:24 +11001527 showprogress = 0;
Damien Miller44f75c12004-01-21 10:58:47 +11001528 batchmode = 1;
Damien Miller64e8d442005-03-01 21:16:47 +11001529 addargs(&args, "-obatchmode yes");
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001530 break;
Damien Millerd14ee1e2002-02-05 12:27:31 +11001531 case 'P':
1532 sftp_direct = optarg;
1533 break;
Damien Miller8829d362002-02-08 22:04:05 +11001534 case 'B':
1535 copy_buffer_len = strtol(optarg, &cp, 10);
1536 if (copy_buffer_len == 0 || *cp != '\0')
1537 fatal("Invalid buffer size \"%s\"", optarg);
1538 break;
Damien Miller16a13332002-02-13 14:03:56 +11001539 case 'R':
1540 num_requests = strtol(optarg, &cp, 10);
1541 if (num_requests == 0 || *cp != '\0')
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001542 fatal("Invalid number of requests \"%s\"",
Damien Miller16a13332002-02-13 14:03:56 +11001543 optarg);
1544 break;
Damien Millerd7686fd2001-02-10 00:40:03 +11001545 case 'h':
1546 default:
Damien Miller33804262001-02-04 23:20:18 +11001547 usage();
1548 }
1549 }
1550
Damien Millerc0f27d82004-03-08 23:12:19 +11001551 if (!isatty(STDERR_FILENO))
1552 showprogress = 0;
1553
Ben Lindstrom2f3d52a2002-04-02 21:06:18 +00001554 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
1555
Damien Millerd14ee1e2002-02-05 12:27:31 +11001556 if (sftp_direct == NULL) {
1557 if (optind == argc || argc > (optind + 2))
1558 usage();
Damien Miller33804262001-02-04 23:20:18 +11001559
Damien Millerd14ee1e2002-02-05 12:27:31 +11001560 userhost = xstrdup(argv[optind]);
1561 file2 = argv[optind+1];
Ben Lindstrom63667f62001-04-13 00:00:14 +00001562
Ben Lindstromc276c122002-12-23 02:14:51 +00001563 if ((host = strrchr(userhost, '@')) == NULL)
Damien Millerd14ee1e2002-02-05 12:27:31 +11001564 host = userhost;
1565 else {
1566 *host++ = '\0';
1567 if (!userhost[0]) {
1568 fprintf(stderr, "Missing username\n");
1569 usage();
1570 }
Damien Miller80163902007-01-05 16:30:16 +11001571 addargs(&args, "-l%s", userhost);
Damien Millerd14ee1e2002-02-05 12:27:31 +11001572 }
1573
Damien Millerec692032004-01-27 21:22:00 +11001574 if ((cp = colon(host)) != NULL) {
1575 *cp++ = '\0';
1576 file1 = cp;
1577 }
1578
Damien Millerd14ee1e2002-02-05 12:27:31 +11001579 host = cleanhostname(host);
1580 if (!*host) {
1581 fprintf(stderr, "Missing hostname\n");
Damien Miller33804262001-02-04 23:20:18 +11001582 usage();
1583 }
Damien Millerd14ee1e2002-02-05 12:27:31 +11001584
Damien Millerd14ee1e2002-02-05 12:27:31 +11001585 addargs(&args, "-oProtocol %d", sshver);
1586
1587 /* no subsystem if the server-spec contains a '/' */
1588 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
1589 addargs(&args, "-s");
1590
1591 addargs(&args, "%s", host);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001592 addargs(&args, "%s", (sftp_server != NULL ?
Damien Millerd14ee1e2002-02-05 12:27:31 +11001593 sftp_server : "sftp"));
Damien Millerd14ee1e2002-02-05 12:27:31 +11001594
Damien Miller44f75c12004-01-21 10:58:47 +11001595 if (!batchmode)
1596 fprintf(stderr, "Connecting to %s...\n", host);
Damien Millercc685c12003-06-04 22:51:38 +10001597 connect_to_server(ssh_program, args.list, &in, &out);
Damien Millerd14ee1e2002-02-05 12:27:31 +11001598 } else {
1599 args.list = NULL;
1600 addargs(&args, "sftp-server");
1601
Damien Miller44f75c12004-01-21 10:58:47 +11001602 if (!batchmode)
1603 fprintf(stderr, "Attaching to %s...\n", sftp_direct);
Damien Millercc685c12003-06-04 22:51:38 +10001604 connect_to_server(sftp_direct, args.list, &in, &out);
Damien Miller33804262001-02-04 23:20:18 +11001605 }
Damien Miller3eec6b72006-01-31 21:49:27 +11001606 freeargs(&args);
Damien Miller33804262001-02-04 23:20:18 +11001607
Damien Miller956f3fb2003-01-10 21:40:00 +11001608 err = interactive_loop(in, out, file1, file2);
Damien Miller33804262001-02-04 23:20:18 +11001609
Ben Lindstrom10b9bf92001-02-26 20:04:45 +00001610#if !defined(USE_PIPES)
Damien Miller37294fb2005-07-17 17:18:49 +10001611 shutdown(in, SHUT_RDWR);
1612 shutdown(out, SHUT_RDWR);
Ben Lindstrom10b9bf92001-02-26 20:04:45 +00001613#endif
1614
Damien Miller33804262001-02-04 23:20:18 +11001615 close(in);
1616 close(out);
Damien Miller44f75c12004-01-21 10:58:47 +11001617 if (batchmode)
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001618 fclose(infile);
Damien Miller33804262001-02-04 23:20:18 +11001619
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001620 while (waitpid(sshpid, NULL, 0) == -1)
1621 if (errno != EINTR)
1622 fatal("Couldn't wait for ssh process: %s",
1623 strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +11001624
Damien Miller956f3fb2003-01-10 21:40:00 +11001625 exit(err == 0 ? 0 : 1);
Damien Miller33804262001-02-04 23:20:18 +11001626}