blob: c5c3b1443244d55e1e50b487f9b6bebb94889e13 [file] [log] [blame]
Damien Millerd671e5a2008-05-19 14:53:33 +10001/* $OpenBSD: sftp.c,v 1.100 2008/04/18 12:32:11 djm 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 Tucker5b2e2ba2008-06-08 09:25:28 +100028#ifdef HAVE_SYS_STATVFS_H
Damien Millerd671e5a2008-05-19 14:53:33 +100029#include <sys/statvfs.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100030#endif
Darren Tucker2d963d82004-11-07 20:04:10 +110031
Damien Miller1cbc2922007-10-26 14:27:45 +100032#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100033#include <errno.h>
34
Damien Miller03e20032006-03-15 11:16:59 +110035#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110036# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110037#endif
Darren Tucker2d963d82004-11-07 20:04:10 +110038#ifdef USE_LIBEDIT
39#include <histedit.h>
40#else
41typedef void EditLine;
42#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110043#include <signal.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100044#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100045#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100046#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100047#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100048#include <stdarg.h>
Damien Miller33804262001-02-04 23:20:18 +110049
Damien Millera7058ec2008-05-20 08:57:06 +100050#ifdef HAVE_UTIL_H
51# include <util.h>
52#endif
53
54#ifdef HAVE_LIBUTIL_H
55# include <libutil.h>
56#endif
57
Damien Miller33804262001-02-04 23:20:18 +110058#include "xmalloc.h"
59#include "log.h"
60#include "pathnames.h"
Ben Lindstrom4529b702001-05-03 23:39:53 +000061#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110062
63#include "sftp.h"
Damien Millerd7834352006-08-05 12:39:39 +100064#include "buffer.h"
Damien Miller33804262001-02-04 23:20:18 +110065#include "sftp-common.h"
66#include "sftp-client.h"
Damien Millerd7d46bb2004-02-18 14:11:13 +110067
Damien Miller20e1fab2004-02-18 14:30:55 +110068/* File to read commands from */
69FILE* infile;
70
71/* Are we in batchfile mode? */
72int batchmode = 0;
73
74/* Size of buffer used when copying files */
75size_t copy_buffer_len = 32768;
76
77/* Number of concurrent outstanding requests */
78size_t num_requests = 16;
79
80/* PID of ssh transport process */
81static pid_t sshpid = -1;
82
83/* This is set to 0 if the progressmeter is not desired. */
Damien Millerc0f27d82004-03-08 23:12:19 +110084int showprogress = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +110085
Darren Tuckercdf547a2004-05-24 10:12:19 +100086/* SIGINT received during command processing */
87volatile sig_atomic_t interrupted = 0;
88
Darren Tuckerb9123452004-06-22 13:06:45 +100089/* I wish qsort() took a separate ctx for the comparison function...*/
90int sort_flag;
91
Damien Miller20e1fab2004-02-18 14:30:55 +110092int remote_glob(struct sftp_conn *, const char *, int,
93 int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
Damien Miller33804262001-02-04 23:20:18 +110094
Kevin Steves12888d12001-03-05 19:50:57 +000095extern char *__progname;
Kevin Steves12888d12001-03-05 19:50:57 +000096
Damien Miller20e1fab2004-02-18 14:30:55 +110097/* Separators for interactive commands */
98#define WHITESPACE " \t\r\n"
Damien Millerd7686fd2001-02-10 00:40:03 +110099
Darren Tuckerb9123452004-06-22 13:06:45 +1000100/* ls flags */
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000101#define LS_LONG_VIEW 0x01 /* Full view ala ls -l */
102#define LS_SHORT_VIEW 0x02 /* Single row view ala ls -1 */
103#define LS_NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */
104#define LS_NAME_SORT 0x08 /* Sort by name (default) */
105#define LS_TIME_SORT 0x10 /* Sort by mtime */
106#define LS_SIZE_SORT 0x20 /* Sort by file size */
107#define LS_REVERSE_SORT 0x40 /* Reverse sort order */
Darren Tucker9a526452004-06-22 13:09:55 +1000108#define LS_SHOW_ALL 0x80 /* Don't skip filenames starting with '.' */
Darren Tuckerb9123452004-06-22 13:06:45 +1000109
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000110#define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW)
111#define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
Damien Miller20e1fab2004-02-18 14:30:55 +1100112
113/* Commands for interactive mode */
114#define I_CHDIR 1
115#define I_CHGRP 2
116#define I_CHMOD 3
117#define I_CHOWN 4
Damien Millerd671e5a2008-05-19 14:53:33 +1000118#define I_DF 24
Damien Miller20e1fab2004-02-18 14:30:55 +1100119#define I_GET 5
120#define I_HELP 6
121#define I_LCHDIR 7
122#define I_LLS 8
123#define I_LMKDIR 9
124#define I_LPWD 10
125#define I_LS 11
126#define I_LUMASK 12
127#define I_MKDIR 13
128#define I_PUT 14
129#define I_PWD 15
130#define I_QUIT 16
131#define I_RENAME 17
132#define I_RM 18
133#define I_RMDIR 19
134#define I_SHELL 20
135#define I_SYMLINK 21
136#define I_VERSION 22
137#define I_PROGRESS 23
138
139struct CMD {
140 const char *c;
141 const int n;
142};
143
144static const struct CMD cmds[] = {
145 { "bye", I_QUIT },
146 { "cd", I_CHDIR },
147 { "chdir", I_CHDIR },
148 { "chgrp", I_CHGRP },
149 { "chmod", I_CHMOD },
150 { "chown", I_CHOWN },
Damien Millerd671e5a2008-05-19 14:53:33 +1000151 { "df", I_DF },
Damien Miller20e1fab2004-02-18 14:30:55 +1100152 { "dir", I_LS },
153 { "exit", I_QUIT },
154 { "get", I_GET },
155 { "mget", I_GET },
156 { "help", I_HELP },
157 { "lcd", I_LCHDIR },
158 { "lchdir", I_LCHDIR },
159 { "lls", I_LLS },
160 { "lmkdir", I_LMKDIR },
161 { "ln", I_SYMLINK },
162 { "lpwd", I_LPWD },
163 { "ls", I_LS },
164 { "lumask", I_LUMASK },
165 { "mkdir", I_MKDIR },
166 { "progress", I_PROGRESS },
167 { "put", I_PUT },
168 { "mput", I_PUT },
169 { "pwd", I_PWD },
170 { "quit", I_QUIT },
171 { "rename", I_RENAME },
172 { "rm", I_RM },
173 { "rmdir", I_RMDIR },
174 { "symlink", I_SYMLINK },
175 { "version", I_VERSION },
176 { "!", I_SHELL },
177 { "?", I_HELP },
178 { NULL, -1}
179};
180
181int interactive_loop(int fd_in, int fd_out, char *file1, char *file2);
182
Damien Millerb6c85fc2007-01-05 16:30:41 +1100183/* ARGSUSED */
Damien Miller20e1fab2004-02-18 14:30:55 +1100184static void
Darren Tuckercdf547a2004-05-24 10:12:19 +1000185killchild(int signo)
186{
Darren Tuckerba66df82005-01-24 21:57:40 +1100187 if (sshpid > 1) {
Darren Tuckercdf547a2004-05-24 10:12:19 +1000188 kill(sshpid, SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100189 waitpid(sshpid, NULL, 0);
190 }
Darren Tuckercdf547a2004-05-24 10:12:19 +1000191
192 _exit(1);
193}
194
Damien Millerb6c85fc2007-01-05 16:30:41 +1100195/* ARGSUSED */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000196static void
197cmd_interrupt(int signo)
198{
199 const char msg[] = "\rInterrupt \n";
Darren Tuckere2f189a2004-12-06 22:45:53 +1100200 int olderrno = errno;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000201
202 write(STDERR_FILENO, msg, sizeof(msg) - 1);
203 interrupted = 1;
Darren Tuckere2f189a2004-12-06 22:45:53 +1100204 errno = olderrno;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000205}
206
207static void
Damien Miller20e1fab2004-02-18 14:30:55 +1100208help(void)
209{
210 printf("Available commands:\n");
211 printf("cd path Change remote directory to 'path'\n");
212 printf("lcd path Change local directory to 'path'\n");
213 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
214 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
215 printf("chown own path Change owner of file 'path' to 'own'\n");
Damien Millerd671e5a2008-05-19 14:53:33 +1000216 printf("df [path] Display statistics for current directory or\n");
217 printf(" filesystem containing 'path'\n");
Damien Miller20e1fab2004-02-18 14:30:55 +1100218 printf("help Display this help text\n");
219 printf("get remote-path [local-path] Download file\n");
220 printf("lls [ls-options [path]] Display local directory listing\n");
221 printf("ln oldpath newpath Symlink remote file\n");
222 printf("lmkdir path Create local directory\n");
223 printf("lpwd Print local working directory\n");
224 printf("ls [path] Display remote directory listing\n");
225 printf("lumask umask Set local umask to 'umask'\n");
226 printf("mkdir path Create remote directory\n");
227 printf("progress Toggle display of progress meter\n");
228 printf("put local-path [remote-path] Upload file\n");
229 printf("pwd Display remote working directory\n");
230 printf("exit Quit sftp\n");
231 printf("quit Quit sftp\n");
232 printf("rename oldpath newpath Rename remote file\n");
233 printf("rmdir path Remove remote directory\n");
234 printf("rm path Delete remote file\n");
235 printf("symlink oldpath newpath Symlink remote file\n");
236 printf("version Show SFTP version\n");
237 printf("!command Execute 'command' in local shell\n");
238 printf("! Escape to local shell\n");
239 printf("? Synonym for help\n");
240}
241
242static void
243local_do_shell(const char *args)
244{
245 int status;
246 char *shell;
247 pid_t pid;
248
249 if (!*args)
250 args = NULL;
251
252 if ((shell = getenv("SHELL")) == NULL)
253 shell = _PATH_BSHELL;
254
255 if ((pid = fork()) == -1)
256 fatal("Couldn't fork: %s", strerror(errno));
257
258 if (pid == 0) {
259 /* XXX: child has pipe fds to ssh subproc open - issue? */
260 if (args) {
261 debug3("Executing %s -c \"%s\"", shell, args);
262 execl(shell, shell, "-c", args, (char *)NULL);
263 } else {
264 debug3("Executing %s", shell);
265 execl(shell, shell, (char *)NULL);
266 }
267 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
268 strerror(errno));
269 _exit(1);
270 }
271 while (waitpid(pid, &status, 0) == -1)
272 if (errno != EINTR)
273 fatal("Couldn't wait for child: %s", strerror(errno));
274 if (!WIFEXITED(status))
Damien Miller55b04f12006-03-26 14:23:17 +1100275 error("Shell exited abnormally");
Damien Miller20e1fab2004-02-18 14:30:55 +1100276 else if (WEXITSTATUS(status))
277 error("Shell exited with status %d", WEXITSTATUS(status));
278}
279
280static void
281local_do_ls(const char *args)
282{
283 if (!args || !*args)
284 local_do_shell(_PATH_LS);
285 else {
286 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
287 char *buf = xmalloc(len);
288
289 /* XXX: quoting - rip quoting code from ftp? */
290 snprintf(buf, len, _PATH_LS " %s", args);
291 local_do_shell(buf);
292 xfree(buf);
293 }
294}
295
296/* Strip one path (usually the pwd) from the start of another */
297static char *
298path_strip(char *path, char *strip)
299{
300 size_t len;
301
302 if (strip == NULL)
303 return (xstrdup(path));
304
305 len = strlen(strip);
Darren Tuckere2f189a2004-12-06 22:45:53 +1100306 if (strncmp(path, strip, len) == 0) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100307 if (strip[len - 1] != '/' && path[len] == '/')
308 len++;
309 return (xstrdup(path + len));
310 }
311
312 return (xstrdup(path));
313}
314
315static char *
316path_append(char *p1, char *p2)
317{
318 char *ret;
Damien Miller3ca8b772007-01-05 16:24:47 +1100319 size_t len = strlen(p1) + strlen(p2) + 2;
Damien Miller20e1fab2004-02-18 14:30:55 +1100320
321 ret = xmalloc(len);
322 strlcpy(ret, p1, len);
Damien Miller3ca8b772007-01-05 16:24:47 +1100323 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
Damien Miller20e1fab2004-02-18 14:30:55 +1100324 strlcat(ret, "/", len);
325 strlcat(ret, p2, len);
326
327 return(ret);
328}
329
330static char *
331make_absolute(char *p, char *pwd)
332{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000333 char *abs_str;
Damien Miller20e1fab2004-02-18 14:30:55 +1100334
335 /* Derelativise */
336 if (p && p[0] != '/') {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000337 abs_str = path_append(pwd, p);
Damien Miller20e1fab2004-02-18 14:30:55 +1100338 xfree(p);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000339 return(abs_str);
Damien Miller20e1fab2004-02-18 14:30:55 +1100340 } else
341 return(p);
342}
343
344static int
345infer_path(const char *p, char **ifp)
346{
347 char *cp;
348
349 cp = strrchr(p, '/');
350 if (cp == NULL) {
351 *ifp = xstrdup(p);
352 return(0);
353 }
354
355 if (!cp[1]) {
356 error("Invalid path");
357 return(-1);
358 }
359
360 *ifp = xstrdup(cp + 1);
361 return(0);
362}
363
364static int
Damien Miller1cbc2922007-10-26 14:27:45 +1000365parse_getput_flags(const char *cmd, char **argv, int argc, int *pflag)
Damien Miller20e1fab2004-02-18 14:30:55 +1100366{
Damien Miller1cbc2922007-10-26 14:27:45 +1000367 extern int optind, optreset, opterr;
368 int ch;
Damien Miller20e1fab2004-02-18 14:30:55 +1100369
Damien Miller1cbc2922007-10-26 14:27:45 +1000370 optind = optreset = 1;
371 opterr = 0;
372
373 *pflag = 0;
374 while ((ch = getopt(argc, argv, "Pp")) != -1) {
375 switch (ch) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100376 case 'p':
377 case 'P':
378 *pflag = 1;
379 break;
380 default:
Damien Miller1cbc2922007-10-26 14:27:45 +1000381 error("%s: Invalid flag -%c", cmd, ch);
382 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100383 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100384 }
385
Damien Miller1cbc2922007-10-26 14:27:45 +1000386 return optind;
Damien Miller20e1fab2004-02-18 14:30:55 +1100387}
388
389static int
Damien Miller1cbc2922007-10-26 14:27:45 +1000390parse_ls_flags(char **argv, int argc, int *lflag)
Damien Miller20e1fab2004-02-18 14:30:55 +1100391{
Damien Miller1cbc2922007-10-26 14:27:45 +1000392 extern int optind, optreset, opterr;
393 int ch;
Damien Miller20e1fab2004-02-18 14:30:55 +1100394
Damien Miller1cbc2922007-10-26 14:27:45 +1000395 optind = optreset = 1;
396 opterr = 0;
397
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000398 *lflag = LS_NAME_SORT;
Damien Miller1cbc2922007-10-26 14:27:45 +1000399 while ((ch = getopt(argc, argv, "1Saflnrt")) != -1) {
400 switch (ch) {
401 case '1':
402 *lflag &= ~VIEW_FLAGS;
403 *lflag |= LS_SHORT_VIEW;
404 break;
405 case 'S':
406 *lflag &= ~SORT_FLAGS;
407 *lflag |= LS_SIZE_SORT;
408 break;
409 case 'a':
410 *lflag |= LS_SHOW_ALL;
411 break;
412 case 'f':
413 *lflag &= ~SORT_FLAGS;
414 break;
415 case 'l':
416 *lflag &= ~VIEW_FLAGS;
417 *lflag |= LS_LONG_VIEW;
418 break;
419 case 'n':
420 *lflag &= ~VIEW_FLAGS;
421 *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
422 break;
423 case 'r':
424 *lflag |= LS_REVERSE_SORT;
425 break;
426 case 't':
427 *lflag &= ~SORT_FLAGS;
428 *lflag |= LS_TIME_SORT;
429 break;
430 default:
431 error("ls: Invalid flag -%c", ch);
432 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100433 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100434 }
435
Damien Miller1cbc2922007-10-26 14:27:45 +1000436 return optind;
Damien Miller20e1fab2004-02-18 14:30:55 +1100437}
438
439static int
Damien Millerd671e5a2008-05-19 14:53:33 +1000440parse_df_flags(const char *cmd, char **argv, int argc, int *hflag, int *iflag)
441{
442 extern int optind, optreset, opterr;
443 int ch;
444
445 optind = optreset = 1;
446 opterr = 0;
447
448 *hflag = *iflag = 0;
449 while ((ch = getopt(argc, argv, "hi")) != -1) {
450 switch (ch) {
451 case 'h':
452 *hflag = 1;
453 break;
454 case 'i':
455 *iflag = 1;
456 break;
457 default:
458 error("%s: Invalid flag -%c", cmd, ch);
459 return -1;
460 }
461 }
462
463 return optind;
464}
465
466static int
Damien Miller20e1fab2004-02-18 14:30:55 +1100467is_dir(char *path)
468{
469 struct stat sb;
470
471 /* XXX: report errors? */
472 if (stat(path, &sb) == -1)
473 return(0);
474
Darren Tucker1e80e402006-09-21 12:59:33 +1000475 return(S_ISDIR(sb.st_mode));
Damien Miller20e1fab2004-02-18 14:30:55 +1100476}
477
478static int
Damien Miller20e1fab2004-02-18 14:30:55 +1100479remote_is_dir(struct sftp_conn *conn, char *path)
480{
481 Attrib *a;
482
483 /* XXX: report errors? */
484 if ((a = do_stat(conn, path, 1)) == NULL)
485 return(0);
486 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
487 return(0);
Darren Tucker1e80e402006-09-21 12:59:33 +1000488 return(S_ISDIR(a->perm));
Damien Miller20e1fab2004-02-18 14:30:55 +1100489}
490
491static int
492process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
493{
494 char *abs_src = NULL;
495 char *abs_dst = NULL;
496 char *tmp;
497 glob_t g;
498 int err = 0;
499 int i;
500
501 abs_src = xstrdup(src);
502 abs_src = make_absolute(abs_src, pwd);
503
504 memset(&g, 0, sizeof(g));
505 debug3("Looking up %s", abs_src);
506 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
507 error("File \"%s\" not found.", abs_src);
508 err = -1;
509 goto out;
510 }
511
512 /* If multiple matches, dst must be a directory or unspecified */
513 if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
514 error("Multiple files match, but \"%s\" is not a directory",
515 dst);
516 err = -1;
517 goto out;
518 }
519
Darren Tuckercdf547a2004-05-24 10:12:19 +1000520 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100521 if (infer_path(g.gl_pathv[i], &tmp)) {
522 err = -1;
523 goto out;
524 }
525
526 if (g.gl_matchc == 1 && dst) {
527 /* If directory specified, append filename */
Damien Miller40b59852006-06-13 13:00:25 +1000528 xfree(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +1100529 if (is_dir(dst)) {
530 if (infer_path(g.gl_pathv[0], &tmp)) {
531 err = 1;
532 goto out;
533 }
534 abs_dst = path_append(dst, tmp);
535 xfree(tmp);
536 } else
537 abs_dst = xstrdup(dst);
538 } else if (dst) {
539 abs_dst = path_append(dst, tmp);
540 xfree(tmp);
541 } else
542 abs_dst = tmp;
543
544 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
545 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
546 err = -1;
547 xfree(abs_dst);
548 abs_dst = NULL;
549 }
550
551out:
552 xfree(abs_src);
Damien Miller20e1fab2004-02-18 14:30:55 +1100553 globfree(&g);
554 return(err);
555}
556
557static int
558process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
559{
560 char *tmp_dst = NULL;
561 char *abs_dst = NULL;
562 char *tmp;
563 glob_t g;
564 int err = 0;
565 int i;
Damien Milleraec5cf82008-02-10 22:26:24 +1100566 struct stat sb;
Damien Miller20e1fab2004-02-18 14:30:55 +1100567
568 if (dst) {
569 tmp_dst = xstrdup(dst);
570 tmp_dst = make_absolute(tmp_dst, pwd);
571 }
572
573 memset(&g, 0, sizeof(g));
574 debug3("Looking up %s", src);
Damien Milleraec5cf82008-02-10 22:26:24 +1100575 if (glob(src, GLOB_NOCHECK, NULL, &g)) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100576 error("File \"%s\" not found.", src);
577 err = -1;
578 goto out;
579 }
580
581 /* If multiple matches, dst may be directory or unspecified */
582 if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
583 error("Multiple files match, but \"%s\" is not a directory",
584 tmp_dst);
585 err = -1;
586 goto out;
587 }
588
Darren Tuckercdf547a2004-05-24 10:12:19 +1000589 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Milleraec5cf82008-02-10 22:26:24 +1100590 if (stat(g.gl_pathv[i], &sb) == -1) {
591 err = -1;
592 error("stat %s: %s", g.gl_pathv[i], strerror(errno));
593 continue;
594 }
595
596 if (!S_ISREG(sb.st_mode)) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100597 error("skipping non-regular file %s",
598 g.gl_pathv[i]);
599 continue;
600 }
601 if (infer_path(g.gl_pathv[i], &tmp)) {
602 err = -1;
603 goto out;
604 }
605
606 if (g.gl_matchc == 1 && tmp_dst) {
607 /* If directory specified, append filename */
608 if (remote_is_dir(conn, tmp_dst)) {
609 if (infer_path(g.gl_pathv[0], &tmp)) {
610 err = 1;
611 goto out;
612 }
613 abs_dst = path_append(tmp_dst, tmp);
614 xfree(tmp);
615 } else
616 abs_dst = xstrdup(tmp_dst);
617
618 } else if (tmp_dst) {
619 abs_dst = path_append(tmp_dst, tmp);
620 xfree(tmp);
621 } else
622 abs_dst = make_absolute(tmp, pwd);
623
624 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
625 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
626 err = -1;
627 }
628
629out:
630 if (abs_dst)
631 xfree(abs_dst);
632 if (tmp_dst)
633 xfree(tmp_dst);
634 globfree(&g);
635 return(err);
636}
637
638static int
639sdirent_comp(const void *aa, const void *bb)
640{
641 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
642 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000643 int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100644
Darren Tuckerb9123452004-06-22 13:06:45 +1000645#define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000646 if (sort_flag & LS_NAME_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000647 return (rmul * strcmp(a->filename, b->filename));
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000648 else if (sort_flag & LS_TIME_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000649 return (rmul * NCMP(a->a.mtime, b->a.mtime));
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000650 else if (sort_flag & LS_SIZE_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000651 return (rmul * NCMP(a->a.size, b->a.size));
652
653 fatal("Unknown ls sort type");
Damien Miller20e1fab2004-02-18 14:30:55 +1100654}
655
656/* sftp ls.1 replacement for directories */
657static int
658do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
659{
Damien Millereccb9de2005-06-17 12:59:34 +1000660 int n;
661 u_int c = 1, colspace = 0, columns = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100662 SFTP_DIRENT **d;
663
664 if ((n = do_readdir(conn, path, &d)) != 0)
665 return (n);
666
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000667 if (!(lflag & LS_SHORT_VIEW)) {
Damien Millereccb9de2005-06-17 12:59:34 +1000668 u_int m = 0, width = 80;
Damien Miller20e1fab2004-02-18 14:30:55 +1100669 struct winsize ws;
670 char *tmp;
671
672 /* Count entries for sort and find longest filename */
Darren Tucker9a526452004-06-22 13:09:55 +1000673 for (n = 0; d[n] != NULL; n++) {
674 if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
675 m = MAX(m, strlen(d[n]->filename));
676 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100677
678 /* Add any subpath that also needs to be counted */
679 tmp = path_strip(path, strip_path);
680 m += strlen(tmp);
681 xfree(tmp);
682
683 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
684 width = ws.ws_col;
685
686 columns = width / (m + 2);
687 columns = MAX(columns, 1);
688 colspace = width / columns;
689 colspace = MIN(colspace, width);
690 }
691
Darren Tuckerb9123452004-06-22 13:06:45 +1000692 if (lflag & SORT_FLAGS) {
Damien Miller653b93b2005-11-05 15:15:23 +1100693 for (n = 0; d[n] != NULL; n++)
694 ; /* count entries */
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000695 sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
Darren Tuckerb9123452004-06-22 13:06:45 +1000696 qsort(d, n, sizeof(*d), sdirent_comp);
697 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100698
Darren Tuckercdf547a2004-05-24 10:12:19 +1000699 for (n = 0; d[n] != NULL && !interrupted; n++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100700 char *tmp, *fname;
701
Darren Tucker9a526452004-06-22 13:09:55 +1000702 if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
703 continue;
704
Damien Miller20e1fab2004-02-18 14:30:55 +1100705 tmp = path_append(path, d[n]->filename);
706 fname = path_strip(tmp, strip_path);
707 xfree(tmp);
708
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000709 if (lflag & LS_LONG_VIEW) {
710 if (lflag & LS_NUMERIC_VIEW) {
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000711 char *lname;
712 struct stat sb;
Damien Miller20e1fab2004-02-18 14:30:55 +1100713
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000714 memset(&sb, 0, sizeof(sb));
715 attrib_to_stat(&d[n]->a, &sb);
716 lname = ls_file(fname, &sb, 1);
717 printf("%s\n", lname);
718 xfree(lname);
719 } else
720 printf("%s\n", d[n]->longname);
Damien Miller20e1fab2004-02-18 14:30:55 +1100721 } else {
722 printf("%-*s", colspace, fname);
723 if (c >= columns) {
724 printf("\n");
725 c = 1;
726 } else
727 c++;
728 }
729
730 xfree(fname);
731 }
732
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000733 if (!(lflag & LS_LONG_VIEW) && (c != 1))
Damien Miller20e1fab2004-02-18 14:30:55 +1100734 printf("\n");
735
736 free_sftp_dirents(d);
737 return (0);
738}
739
740/* sftp ls.1 replacement which handles path globs */
741static int
742do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
743 int lflag)
744{
745 glob_t g;
Damien Millereccb9de2005-06-17 12:59:34 +1000746 u_int i, c = 1, colspace = 0, columns = 1;
Darren Tucker596dcfa2004-12-11 13:37:22 +1100747 Attrib *a = NULL;
Damien Miller20e1fab2004-02-18 14:30:55 +1100748
749 memset(&g, 0, sizeof(g));
750
751 if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
Darren Tucker596dcfa2004-12-11 13:37:22 +1100752 NULL, &g) || (g.gl_pathc && !g.gl_matchc)) {
753 if (g.gl_pathc)
754 globfree(&g);
Damien Miller20e1fab2004-02-18 14:30:55 +1100755 error("Can't ls: \"%s\" not found", path);
756 return (-1);
757 }
758
Darren Tuckercdf547a2004-05-24 10:12:19 +1000759 if (interrupted)
760 goto out;
761
Damien Miller20e1fab2004-02-18 14:30:55 +1100762 /*
Darren Tucker596dcfa2004-12-11 13:37:22 +1100763 * If the glob returns a single match and it is a directory,
764 * then just list its contents.
Damien Miller20e1fab2004-02-18 14:30:55 +1100765 */
Darren Tucker596dcfa2004-12-11 13:37:22 +1100766 if (g.gl_matchc == 1) {
767 if ((a = do_lstat(conn, g.gl_pathv[0], 1)) == NULL) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100768 globfree(&g);
769 return (-1);
770 }
771 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
772 S_ISDIR(a->perm)) {
Darren Tucker596dcfa2004-12-11 13:37:22 +1100773 int err;
774
775 err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
Damien Miller20e1fab2004-02-18 14:30:55 +1100776 globfree(&g);
Darren Tucker596dcfa2004-12-11 13:37:22 +1100777 return (err);
Damien Miller20e1fab2004-02-18 14:30:55 +1100778 }
779 }
780
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000781 if (!(lflag & LS_SHORT_VIEW)) {
Damien Millereccb9de2005-06-17 12:59:34 +1000782 u_int m = 0, width = 80;
Damien Miller20e1fab2004-02-18 14:30:55 +1100783 struct winsize ws;
784
785 /* Count entries for sort and find longest filename */
786 for (i = 0; g.gl_pathv[i]; i++)
787 m = MAX(m, strlen(g.gl_pathv[i]));
788
789 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
790 width = ws.ws_col;
791
792 columns = width / (m + 2);
793 columns = MAX(columns, 1);
794 colspace = width / columns;
795 }
796
Darren Tucker596dcfa2004-12-11 13:37:22 +1100797 for (i = 0; g.gl_pathv[i] && !interrupted; i++, a = NULL) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100798 char *fname;
799
800 fname = path_strip(g.gl_pathv[i], strip_path);
801
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000802 if (lflag & LS_LONG_VIEW) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100803 char *lname;
804 struct stat sb;
805
806 /*
807 * XXX: this is slow - 1 roundtrip per path
808 * A solution to this is to fork glob() and
809 * build a sftp specific version which keeps the
810 * attribs (which currently get thrown away)
811 * that the server returns as well as the filenames.
812 */
813 memset(&sb, 0, sizeof(sb));
Darren Tucker596dcfa2004-12-11 13:37:22 +1100814 if (a == NULL)
815 a = do_lstat(conn, g.gl_pathv[i], 1);
Damien Miller20e1fab2004-02-18 14:30:55 +1100816 if (a != NULL)
817 attrib_to_stat(a, &sb);
818 lname = ls_file(fname, &sb, 1);
819 printf("%s\n", lname);
820 xfree(lname);
821 } else {
822 printf("%-*s", colspace, fname);
823 if (c >= columns) {
824 printf("\n");
825 c = 1;
826 } else
827 c++;
828 }
829 xfree(fname);
830 }
831
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000832 if (!(lflag & LS_LONG_VIEW) && (c != 1))
Damien Miller20e1fab2004-02-18 14:30:55 +1100833 printf("\n");
834
Darren Tuckercdf547a2004-05-24 10:12:19 +1000835 out:
Damien Miller20e1fab2004-02-18 14:30:55 +1100836 if (g.gl_pathc)
837 globfree(&g);
838
839 return (0);
840}
841
Damien Millerd671e5a2008-05-19 14:53:33 +1000842static int
843do_df(struct sftp_conn *conn, char *path, int hflag, int iflag)
844{
Darren Tucker5b2e2ba2008-06-08 09:25:28 +1000845#ifdef USE_STATVFS
Damien Millerd671e5a2008-05-19 14:53:33 +1000846 struct statvfs st;
847 char s_used[FMT_SCALED_STRSIZE];
848 char s_avail[FMT_SCALED_STRSIZE];
849 char s_root[FMT_SCALED_STRSIZE];
850 char s_total[FMT_SCALED_STRSIZE];
851
852 if (do_statvfs(conn, path, &st, 1) == -1)
853 return -1;
854 if (iflag) {
855 printf(" Inodes Used Avail "
856 "(root) %%Capacity\n");
857 printf("%11llu %11llu %11llu %11llu %3llu%%\n",
858 (unsigned long long)st.f_files,
859 (unsigned long long)(st.f_files - st.f_ffree),
860 (unsigned long long)st.f_favail,
861 (unsigned long long)st.f_ffree,
862 (unsigned long long)(100 * (st.f_files - st.f_ffree) /
863 st.f_files));
864 } else if (hflag) {
865 strlcpy(s_used, "error", sizeof(s_used));
866 strlcpy(s_avail, "error", sizeof(s_avail));
867 strlcpy(s_root, "error", sizeof(s_root));
868 strlcpy(s_total, "error", sizeof(s_total));
869 fmt_scaled((st.f_blocks - st.f_bfree) * st.f_frsize, s_used);
870 fmt_scaled(st.f_bavail * st.f_frsize, s_avail);
871 fmt_scaled(st.f_bfree * st.f_frsize, s_root);
872 fmt_scaled(st.f_blocks * st.f_frsize, s_total);
873 printf(" Size Used Avail (root) %%Capacity\n");
874 printf("%7sB %7sB %7sB %7sB %3llu%%\n",
875 s_total, s_used, s_avail, s_root,
876 (unsigned long long)(100 * (st.f_blocks - st.f_bfree) /
877 st.f_blocks));
878 } else {
879 printf(" Size Used Avail "
880 "(root) %%Capacity\n");
881 printf("%12llu %12llu %12llu %12llu %3llu%%\n",
882 (unsigned long long)(st.f_frsize * st.f_blocks / 1024),
883 (unsigned long long)(st.f_frsize *
884 (st.f_blocks - st.f_bfree) / 1024),
885 (unsigned long long)(st.f_frsize * st.f_bavail / 1024),
886 (unsigned long long)(st.f_frsize * st.f_bfree / 1024),
887 (unsigned long long)(100 * (st.f_blocks - st.f_bfree) /
888 st.f_blocks));
889 }
890 return 0;
Darren Tucker5b2e2ba2008-06-08 09:25:28 +1000891#else
892 error("client does not support statvfs extension");
893 return -1;
894#endif
Damien Millerd671e5a2008-05-19 14:53:33 +1000895}
896
Damien Miller1cbc2922007-10-26 14:27:45 +1000897/*
898 * Undo escaping of glob sequences in place. Used to undo extra escaping
899 * applied in makeargv() when the string is destined for a function that
900 * does not glob it.
901 */
902static void
903undo_glob_escape(char *s)
904{
905 size_t i, j;
906
907 for (i = j = 0;;) {
908 if (s[i] == '\0') {
909 s[j] = '\0';
910 return;
911 }
912 if (s[i] != '\\') {
913 s[j++] = s[i++];
914 continue;
915 }
916 /* s[i] == '\\' */
917 ++i;
918 switch (s[i]) {
919 case '?':
920 case '[':
921 case '*':
922 case '\\':
923 s[j++] = s[i++];
924 break;
925 case '\0':
926 s[j++] = '\\';
927 s[j] = '\0';
928 return;
929 default:
930 s[j++] = '\\';
931 s[j++] = s[i++];
932 break;
933 }
934 }
935}
936
937/*
938 * Split a string into an argument vector using sh(1)-style quoting,
939 * comment and escaping rules, but with some tweaks to handle glob(3)
940 * wildcards.
941 * Returns NULL on error or a NULL-terminated array of arguments.
942 */
943#define MAXARGS 128
944#define MAXARGLEN 8192
945static char **
946makeargv(const char *arg, int *argcp)
947{
948 int argc, quot;
949 size_t i, j;
950 static char argvs[MAXARGLEN];
951 static char *argv[MAXARGS + 1];
952 enum { MA_START, MA_SQUOTE, MA_DQUOTE, MA_UNQUOTED } state, q;
953
954 *argcp = argc = 0;
955 if (strlen(arg) > sizeof(argvs) - 1) {
956 args_too_longs:
957 error("string too long");
958 return NULL;
959 }
960 state = MA_START;
961 i = j = 0;
962 for (;;) {
963 if (isspace(arg[i])) {
964 if (state == MA_UNQUOTED) {
965 /* Terminate current argument */
966 argvs[j++] = '\0';
967 argc++;
968 state = MA_START;
969 } else if (state != MA_START)
970 argvs[j++] = arg[i];
971 } else if (arg[i] == '"' || arg[i] == '\'') {
972 q = arg[i] == '"' ? MA_DQUOTE : MA_SQUOTE;
973 if (state == MA_START) {
974 argv[argc] = argvs + j;
975 state = q;
976 } else if (state == MA_UNQUOTED)
977 state = q;
978 else if (state == q)
979 state = MA_UNQUOTED;
980 else
981 argvs[j++] = arg[i];
982 } else if (arg[i] == '\\') {
983 if (state == MA_SQUOTE || state == MA_DQUOTE) {
984 quot = state == MA_SQUOTE ? '\'' : '"';
985 /* Unescape quote we are in */
986 /* XXX support \n and friends? */
987 if (arg[i + 1] == quot) {
988 i++;
989 argvs[j++] = arg[i];
990 } else if (arg[i + 1] == '?' ||
991 arg[i + 1] == '[' || arg[i + 1] == '*') {
992 /*
993 * Special case for sftp: append
994 * double-escaped glob sequence -
995 * glob will undo one level of
996 * escaping. NB. string can grow here.
997 */
998 if (j >= sizeof(argvs) - 5)
999 goto args_too_longs;
1000 argvs[j++] = '\\';
1001 argvs[j++] = arg[i++];
1002 argvs[j++] = '\\';
1003 argvs[j++] = arg[i];
1004 } else {
1005 argvs[j++] = arg[i++];
1006 argvs[j++] = arg[i];
1007 }
1008 } else {
1009 if (state == MA_START) {
1010 argv[argc] = argvs + j;
1011 state = MA_UNQUOTED;
1012 }
1013 if (arg[i + 1] == '?' || arg[i + 1] == '[' ||
1014 arg[i + 1] == '*' || arg[i + 1] == '\\') {
1015 /*
1016 * Special case for sftp: append
1017 * escaped glob sequence -
1018 * glob will undo one level of
1019 * escaping.
1020 */
1021 argvs[j++] = arg[i++];
1022 argvs[j++] = arg[i];
1023 } else {
1024 /* Unescape everything */
1025 /* XXX support \n and friends? */
1026 i++;
1027 argvs[j++] = arg[i];
1028 }
1029 }
1030 } else if (arg[i] == '#') {
1031 if (state == MA_SQUOTE || state == MA_DQUOTE)
1032 argvs[j++] = arg[i];
1033 else
1034 goto string_done;
1035 } else if (arg[i] == '\0') {
1036 if (state == MA_SQUOTE || state == MA_DQUOTE) {
1037 error("Unterminated quoted argument");
1038 return NULL;
1039 }
1040 string_done:
1041 if (state == MA_UNQUOTED) {
1042 argvs[j++] = '\0';
1043 argc++;
1044 }
1045 break;
1046 } else {
1047 if (state == MA_START) {
1048 argv[argc] = argvs + j;
1049 state = MA_UNQUOTED;
1050 }
1051 if ((state == MA_SQUOTE || state == MA_DQUOTE) &&
1052 (arg[i] == '?' || arg[i] == '[' || arg[i] == '*')) {
1053 /*
1054 * Special case for sftp: escape quoted
1055 * glob(3) wildcards. NB. string can grow
1056 * here.
1057 */
1058 if (j >= sizeof(argvs) - 3)
1059 goto args_too_longs;
1060 argvs[j++] = '\\';
1061 argvs[j++] = arg[i];
1062 } else
1063 argvs[j++] = arg[i];
1064 }
1065 i++;
1066 }
1067 *argcp = argc;
1068 return argv;
1069}
1070
Damien Miller20e1fab2004-02-18 14:30:55 +11001071static int
Damien Millerd671e5a2008-05-19 14:53:33 +10001072parse_args(const char **cpp, int *pflag, int *lflag, int *iflag, int *hflag,
Damien Miller20e1fab2004-02-18 14:30:55 +11001073 unsigned long *n_arg, char **path1, char **path2)
1074{
1075 const char *cmd, *cp = *cpp;
Damien Miller1cbc2922007-10-26 14:27:45 +10001076 char *cp2, **argv;
Damien Miller20e1fab2004-02-18 14:30:55 +11001077 int base = 0;
1078 long l;
Damien Miller1cbc2922007-10-26 14:27:45 +10001079 int i, cmdnum, optidx, argc;
Damien Miller20e1fab2004-02-18 14:30:55 +11001080
1081 /* Skip leading whitespace */
1082 cp = cp + strspn(cp, WHITESPACE);
1083
1084 /* Ignore blank lines and lines which begin with comment '#' char */
1085 if (*cp == '\0' || *cp == '#')
1086 return (0);
1087
1088 /* Check for leading '-' (disable error processing) */
1089 *iflag = 0;
1090 if (*cp == '-') {
1091 *iflag = 1;
1092 cp++;
1093 }
1094
Damien Miller1cbc2922007-10-26 14:27:45 +10001095 if ((argv = makeargv(cp, &argc)) == NULL)
1096 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001097
Damien Miller1cbc2922007-10-26 14:27:45 +10001098 /* Figure out which command we have */
1099 for (i = 0; cmds[i].c != NULL; i++) {
1100 if (strcasecmp(cmds[i].c, argv[0]) == 0)
Damien Miller20e1fab2004-02-18 14:30:55 +11001101 break;
Damien Miller20e1fab2004-02-18 14:30:55 +11001102 }
1103 cmdnum = cmds[i].n;
1104 cmd = cmds[i].c;
1105
1106 /* Special case */
1107 if (*cp == '!') {
1108 cp++;
1109 cmdnum = I_SHELL;
1110 } else if (cmdnum == -1) {
1111 error("Invalid command.");
Damien Miller1cbc2922007-10-26 14:27:45 +10001112 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001113 }
1114
1115 /* Get arguments and parse flags */
Damien Millerd671e5a2008-05-19 14:53:33 +10001116 *lflag = *pflag = *hflag = *n_arg = 0;
Damien Miller20e1fab2004-02-18 14:30:55 +11001117 *path1 = *path2 = NULL;
Damien Miller1cbc2922007-10-26 14:27:45 +10001118 optidx = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001119 switch (cmdnum) {
1120 case I_GET:
1121 case I_PUT:
Damien Miller1cbc2922007-10-26 14:27:45 +10001122 if ((optidx = parse_getput_flags(cmd, argv, argc, pflag)) == -1)
1123 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001124 /* Get first pathname (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001125 if (argc - optidx < 1) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001126 error("You must specify at least one path after a "
1127 "%s command.", cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001128 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001129 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001130 *path1 = xstrdup(argv[optidx]);
1131 /* Get second pathname (optional) */
1132 if (argc - optidx > 1) {
1133 *path2 = xstrdup(argv[optidx + 1]);
1134 /* Destination is not globbed */
1135 undo_glob_escape(*path2);
1136 }
Damien Miller20e1fab2004-02-18 14:30:55 +11001137 break;
1138 case I_RENAME:
1139 case I_SYMLINK:
Damien Miller1cbc2922007-10-26 14:27:45 +10001140 if (argc - optidx < 2) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001141 error("You must specify two paths after a %s "
1142 "command.", cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001143 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001144 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001145 *path1 = xstrdup(argv[optidx]);
1146 *path2 = xstrdup(argv[optidx + 1]);
1147 /* Paths are not globbed */
1148 undo_glob_escape(*path1);
1149 undo_glob_escape(*path2);
Damien Miller20e1fab2004-02-18 14:30:55 +11001150 break;
1151 case I_RM:
1152 case I_MKDIR:
1153 case I_RMDIR:
1154 case I_CHDIR:
1155 case I_LCHDIR:
1156 case I_LMKDIR:
1157 /* Get pathname (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001158 if (argc - optidx < 1) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001159 error("You must specify a path after a %s command.",
1160 cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001161 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001162 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001163 *path1 = xstrdup(argv[optidx]);
1164 /* Only "rm" globs */
1165 if (cmdnum != I_RM)
1166 undo_glob_escape(*path1);
Damien Miller20e1fab2004-02-18 14:30:55 +11001167 break;
Damien Millerd671e5a2008-05-19 14:53:33 +10001168 case I_DF:
1169 if ((optidx = parse_df_flags(cmd, argv, argc, hflag,
1170 iflag)) == -1)
1171 return -1;
1172 /* Default to current directory if no path specified */
1173 if (argc - optidx < 1)
1174 *path1 = NULL;
1175 else {
1176 *path1 = xstrdup(argv[optidx]);
1177 undo_glob_escape(*path1);
1178 }
1179 break;
Damien Miller20e1fab2004-02-18 14:30:55 +11001180 case I_LS:
Damien Miller1cbc2922007-10-26 14:27:45 +10001181 if ((optidx = parse_ls_flags(argv, argc, lflag)) == -1)
Damien Miller20e1fab2004-02-18 14:30:55 +11001182 return(-1);
1183 /* Path is optional */
Damien Miller1cbc2922007-10-26 14:27:45 +10001184 if (argc - optidx > 0)
1185 *path1 = xstrdup(argv[optidx]);
Damien Miller20e1fab2004-02-18 14:30:55 +11001186 break;
1187 case I_LLS:
Darren Tucker88b976f2007-12-29 02:40:43 +11001188 /* Skip ls command and following whitespace */
1189 cp = cp + strlen(cmd) + strspn(cp, WHITESPACE);
Damien Miller20e1fab2004-02-18 14:30:55 +11001190 case I_SHELL:
1191 /* Uses the rest of the line */
1192 break;
1193 case I_LUMASK:
Damien Miller20e1fab2004-02-18 14:30:55 +11001194 case I_CHMOD:
1195 base = 8;
1196 case I_CHOWN:
1197 case I_CHGRP:
1198 /* Get numeric arg (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001199 if (argc - optidx < 1)
1200 goto need_num_arg;
Damien Millere7658a52006-10-24 03:00:12 +10001201 errno = 0;
Damien Miller1cbc2922007-10-26 14:27:45 +10001202 l = strtol(argv[optidx], &cp2, base);
1203 if (cp2 == argv[optidx] || *cp2 != '\0' ||
1204 ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
1205 l < 0) {
1206 need_num_arg:
Damien Miller20e1fab2004-02-18 14:30:55 +11001207 error("You must supply a numeric argument "
1208 "to the %s command.", cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001209 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001210 }
Damien Miller20e1fab2004-02-18 14:30:55 +11001211 *n_arg = l;
Damien Miller1cbc2922007-10-26 14:27:45 +10001212 if (cmdnum == I_LUMASK)
Damien Miller20e1fab2004-02-18 14:30:55 +11001213 break;
Damien Miller20e1fab2004-02-18 14:30:55 +11001214 /* Get pathname (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001215 if (argc - optidx < 2) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001216 error("You must specify a path after a %s command.",
1217 cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001218 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001219 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001220 *path1 = xstrdup(argv[optidx + 1]);
Damien Miller20e1fab2004-02-18 14:30:55 +11001221 break;
1222 case I_QUIT:
1223 case I_PWD:
1224 case I_LPWD:
1225 case I_HELP:
1226 case I_VERSION:
1227 case I_PROGRESS:
1228 break;
1229 default:
1230 fatal("Command not implemented");
1231 }
1232
1233 *cpp = cp;
1234 return(cmdnum);
1235}
1236
1237static int
1238parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
1239 int err_abort)
1240{
1241 char *path1, *path2, *tmp;
Damien Millerd671e5a2008-05-19 14:53:33 +10001242 int pflag, lflag, iflag, hflag, cmdnum, i;
Damien Miller20e1fab2004-02-18 14:30:55 +11001243 unsigned long n_arg;
1244 Attrib a, *aa;
1245 char path_buf[MAXPATHLEN];
1246 int err = 0;
1247 glob_t g;
1248
1249 path1 = path2 = NULL;
Damien Millerd671e5a2008-05-19 14:53:33 +10001250 cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &hflag, &n_arg,
Damien Miller20e1fab2004-02-18 14:30:55 +11001251 &path1, &path2);
1252
1253 if (iflag != 0)
1254 err_abort = 0;
1255
1256 memset(&g, 0, sizeof(g));
1257
1258 /* Perform command */
1259 switch (cmdnum) {
1260 case 0:
1261 /* Blank line */
1262 break;
1263 case -1:
1264 /* Unrecognized command */
1265 err = -1;
1266 break;
1267 case I_GET:
1268 err = process_get(conn, path1, path2, *pwd, pflag);
1269 break;
1270 case I_PUT:
1271 err = process_put(conn, path1, path2, *pwd, pflag);
1272 break;
1273 case I_RENAME:
1274 path1 = make_absolute(path1, *pwd);
1275 path2 = make_absolute(path2, *pwd);
1276 err = do_rename(conn, path1, path2);
1277 break;
1278 case I_SYMLINK:
1279 path2 = make_absolute(path2, *pwd);
1280 err = do_symlink(conn, path1, path2);
1281 break;
1282 case I_RM:
1283 path1 = make_absolute(path1, *pwd);
1284 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001285 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001286 printf("Removing %s\n", g.gl_pathv[i]);
1287 err = do_rm(conn, g.gl_pathv[i]);
1288 if (err != 0 && err_abort)
1289 break;
1290 }
1291 break;
1292 case I_MKDIR:
1293 path1 = make_absolute(path1, *pwd);
1294 attrib_clear(&a);
1295 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1296 a.perm = 0777;
1297 err = do_mkdir(conn, path1, &a);
1298 break;
1299 case I_RMDIR:
1300 path1 = make_absolute(path1, *pwd);
1301 err = do_rmdir(conn, path1);
1302 break;
1303 case I_CHDIR:
1304 path1 = make_absolute(path1, *pwd);
1305 if ((tmp = do_realpath(conn, path1)) == NULL) {
1306 err = 1;
1307 break;
1308 }
1309 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1310 xfree(tmp);
1311 err = 1;
1312 break;
1313 }
1314 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
1315 error("Can't change directory: Can't check target");
1316 xfree(tmp);
1317 err = 1;
1318 break;
1319 }
1320 if (!S_ISDIR(aa->perm)) {
1321 error("Can't change directory: \"%s\" is not "
1322 "a directory", tmp);
1323 xfree(tmp);
1324 err = 1;
1325 break;
1326 }
1327 xfree(*pwd);
1328 *pwd = tmp;
1329 break;
1330 case I_LS:
1331 if (!path1) {
1332 do_globbed_ls(conn, *pwd, *pwd, lflag);
1333 break;
1334 }
1335
1336 /* Strip pwd off beginning of non-absolute paths */
1337 tmp = NULL;
1338 if (*path1 != '/')
1339 tmp = *pwd;
1340
1341 path1 = make_absolute(path1, *pwd);
1342 err = do_globbed_ls(conn, path1, tmp, lflag);
1343 break;
Damien Millerd671e5a2008-05-19 14:53:33 +10001344 case I_DF:
1345 /* Default to current directory if no path specified */
1346 if (path1 == NULL)
1347 path1 = xstrdup(*pwd);
1348 path1 = make_absolute(path1, *pwd);
1349 err = do_df(conn, path1, hflag, iflag);
1350 break;
Damien Miller20e1fab2004-02-18 14:30:55 +11001351 case I_LCHDIR:
1352 if (chdir(path1) == -1) {
1353 error("Couldn't change local directory to "
1354 "\"%s\": %s", path1, strerror(errno));
1355 err = 1;
1356 }
1357 break;
1358 case I_LMKDIR:
1359 if (mkdir(path1, 0777) == -1) {
1360 error("Couldn't create local directory "
1361 "\"%s\": %s", path1, strerror(errno));
1362 err = 1;
1363 }
1364 break;
1365 case I_LLS:
1366 local_do_ls(cmd);
1367 break;
1368 case I_SHELL:
1369 local_do_shell(cmd);
1370 break;
1371 case I_LUMASK:
1372 umask(n_arg);
1373 printf("Local umask: %03lo\n", n_arg);
1374 break;
1375 case I_CHMOD:
1376 path1 = make_absolute(path1, *pwd);
1377 attrib_clear(&a);
1378 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1379 a.perm = n_arg;
1380 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001381 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001382 printf("Changing mode on %s\n", g.gl_pathv[i]);
1383 err = do_setstat(conn, g.gl_pathv[i], &a);
1384 if (err != 0 && err_abort)
1385 break;
1386 }
1387 break;
1388 case I_CHOWN:
1389 case I_CHGRP:
1390 path1 = make_absolute(path1, *pwd);
1391 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001392 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001393 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1394 if (err != 0 && err_abort)
1395 break;
1396 else
1397 continue;
1398 }
1399 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1400 error("Can't get current ownership of "
1401 "remote file \"%s\"", g.gl_pathv[i]);
1402 if (err != 0 && err_abort)
1403 break;
1404 else
1405 continue;
1406 }
1407 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1408 if (cmdnum == I_CHOWN) {
1409 printf("Changing owner on %s\n", g.gl_pathv[i]);
1410 aa->uid = n_arg;
1411 } else {
1412 printf("Changing group on %s\n", g.gl_pathv[i]);
1413 aa->gid = n_arg;
1414 }
1415 err = do_setstat(conn, g.gl_pathv[i], aa);
1416 if (err != 0 && err_abort)
1417 break;
1418 }
1419 break;
1420 case I_PWD:
1421 printf("Remote working directory: %s\n", *pwd);
1422 break;
1423 case I_LPWD:
1424 if (!getcwd(path_buf, sizeof(path_buf))) {
1425 error("Couldn't get local cwd: %s", strerror(errno));
1426 err = -1;
1427 break;
1428 }
1429 printf("Local working directory: %s\n", path_buf);
1430 break;
1431 case I_QUIT:
1432 /* Processed below */
1433 break;
1434 case I_HELP:
1435 help();
1436 break;
1437 case I_VERSION:
1438 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1439 break;
1440 case I_PROGRESS:
1441 showprogress = !showprogress;
1442 if (showprogress)
1443 printf("Progress meter enabled\n");
1444 else
1445 printf("Progress meter disabled\n");
1446 break;
1447 default:
1448 fatal("%d is not implemented", cmdnum);
1449 }
1450
1451 if (g.gl_pathc)
1452 globfree(&g);
1453 if (path1)
1454 xfree(path1);
1455 if (path2)
1456 xfree(path2);
1457
1458 /* If an unignored error occurs in batch mode we should abort. */
1459 if (err_abort && err != 0)
1460 return (-1);
1461 else if (cmdnum == I_QUIT)
1462 return (1);
1463
1464 return (0);
1465}
1466
Darren Tucker2d963d82004-11-07 20:04:10 +11001467#ifdef USE_LIBEDIT
1468static char *
1469prompt(EditLine *el)
1470{
1471 return ("sftp> ");
1472}
1473#endif
1474
Damien Miller20e1fab2004-02-18 14:30:55 +11001475int
1476interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1477{
1478 char *pwd;
1479 char *dir = NULL;
1480 char cmd[2048];
1481 struct sftp_conn *conn;
Damien Miller0e2c1022005-08-12 22:16:22 +10001482 int err, interactive;
Darren Tucker2d963d82004-11-07 20:04:10 +11001483 EditLine *el = NULL;
1484#ifdef USE_LIBEDIT
1485 History *hl = NULL;
1486 HistEvent hev;
1487 extern char *__progname;
1488
1489 if (!batchmode && isatty(STDIN_FILENO)) {
1490 if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL)
1491 fatal("Couldn't initialise editline");
1492 if ((hl = history_init()) == NULL)
1493 fatal("Couldn't initialise editline history");
1494 history(hl, &hev, H_SETSIZE, 100);
1495 el_set(el, EL_HIST, history, hl);
1496
1497 el_set(el, EL_PROMPT, prompt);
1498 el_set(el, EL_EDITOR, "emacs");
1499 el_set(el, EL_TERMINAL, NULL);
1500 el_set(el, EL_SIGNAL, 1);
1501 el_source(el, NULL);
1502 }
1503#endif /* USE_LIBEDIT */
Damien Miller20e1fab2004-02-18 14:30:55 +11001504
1505 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1506 if (conn == NULL)
1507 fatal("Couldn't initialise connection to server");
1508
1509 pwd = do_realpath(conn, ".");
1510 if (pwd == NULL)
1511 fatal("Need cwd");
1512
1513 if (file1 != NULL) {
1514 dir = xstrdup(file1);
1515 dir = make_absolute(dir, pwd);
1516
1517 if (remote_is_dir(conn, dir) && file2 == NULL) {
1518 printf("Changing to: %s\n", dir);
1519 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
Darren Tuckercd516ef2004-12-06 22:43:43 +11001520 if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) {
1521 xfree(dir);
1522 xfree(pwd);
Damien Millere0b90a62006-03-26 13:51:44 +11001523 xfree(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001524 return (-1);
Darren Tuckercd516ef2004-12-06 22:43:43 +11001525 }
Damien Miller20e1fab2004-02-18 14:30:55 +11001526 } else {
1527 if (file2 == NULL)
1528 snprintf(cmd, sizeof cmd, "get %s", dir);
1529 else
1530 snprintf(cmd, sizeof cmd, "get %s %s", dir,
1531 file2);
1532
1533 err = parse_dispatch_command(conn, cmd, &pwd, 1);
1534 xfree(dir);
1535 xfree(pwd);
Damien Millere0b90a62006-03-26 13:51:44 +11001536 xfree(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001537 return (err);
1538 }
1539 xfree(dir);
1540 }
1541
Darren Tucker93e7e8f2005-08-23 08:06:55 +10001542#if defined(HAVE_SETVBUF) && !defined(BROKEN_SETVBUF)
Damien Miller20e1fab2004-02-18 14:30:55 +11001543 setvbuf(stdout, NULL, _IOLBF, 0);
1544 setvbuf(infile, NULL, _IOLBF, 0);
1545#else
Damien Miller37294fb2005-07-17 17:18:49 +10001546 setlinebuf(stdout);
1547 setlinebuf(infile);
Damien Miller20e1fab2004-02-18 14:30:55 +11001548#endif
1549
Damien Miller0e2c1022005-08-12 22:16:22 +10001550 interactive = !batchmode && isatty(STDIN_FILENO);
Damien Miller20e1fab2004-02-18 14:30:55 +11001551 err = 0;
1552 for (;;) {
1553 char *cp;
1554
Darren Tuckercdf547a2004-05-24 10:12:19 +10001555 signal(SIGINT, SIG_IGN);
1556
Darren Tucker2d963d82004-11-07 20:04:10 +11001557 if (el == NULL) {
Damien Miller0e2c1022005-08-12 22:16:22 +10001558 if (interactive)
1559 printf("sftp> ");
Darren Tucker2d963d82004-11-07 20:04:10 +11001560 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller0e2c1022005-08-12 22:16:22 +10001561 if (interactive)
1562 printf("\n");
Darren Tucker2d963d82004-11-07 20:04:10 +11001563 break;
1564 }
Damien Miller0e2c1022005-08-12 22:16:22 +10001565 if (!interactive) { /* Echo command */
1566 printf("sftp> %s", cmd);
1567 if (strlen(cmd) > 0 &&
1568 cmd[strlen(cmd) - 1] != '\n')
1569 printf("\n");
1570 }
Darren Tucker2d963d82004-11-07 20:04:10 +11001571 } else {
1572#ifdef USE_LIBEDIT
1573 const char *line;
1574 int count = 0;
Damien Miller20e1fab2004-02-18 14:30:55 +11001575
Damien Miller0e2c1022005-08-12 22:16:22 +10001576 if ((line = el_gets(el, &count)) == NULL || count <= 0) {
1577 printf("\n");
1578 break;
1579 }
Darren Tucker2d963d82004-11-07 20:04:10 +11001580 history(hl, &hev, H_ENTER, line);
1581 if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
1582 fprintf(stderr, "Error: input line too long\n");
1583 continue;
1584 }
1585#endif /* USE_LIBEDIT */
Damien Miller20e1fab2004-02-18 14:30:55 +11001586 }
1587
Damien Miller20e1fab2004-02-18 14:30:55 +11001588 cp = strrchr(cmd, '\n');
1589 if (cp)
1590 *cp = '\0';
1591
Darren Tuckercdf547a2004-05-24 10:12:19 +10001592 /* Handle user interrupts gracefully during commands */
1593 interrupted = 0;
1594 signal(SIGINT, cmd_interrupt);
1595
Damien Miller20e1fab2004-02-18 14:30:55 +11001596 err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
1597 if (err != 0)
1598 break;
1599 }
1600 xfree(pwd);
Damien Millere0b90a62006-03-26 13:51:44 +11001601 xfree(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001602
Tim Rice027e8b12005-08-15 14:52:50 -07001603#ifdef USE_LIBEDIT
Damien Miller0e2c1022005-08-12 22:16:22 +10001604 if (el != NULL)
1605 el_end(el);
Tim Rice027e8b12005-08-15 14:52:50 -07001606#endif /* USE_LIBEDIT */
Damien Miller0e2c1022005-08-12 22:16:22 +10001607
Damien Miller20e1fab2004-02-18 14:30:55 +11001608 /* err == 1 signifies normal "quit" exit */
1609 return (err >= 0 ? 0 : -1);
1610}
Damien Miller62d57f62003-01-10 21:43:24 +11001611
Ben Lindstrombba81212001-06-25 05:01:22 +00001612static void
Damien Millercc685c12003-06-04 22:51:38 +10001613connect_to_server(char *path, char **args, int *in, int *out)
Damien Miller33804262001-02-04 23:20:18 +11001614{
1615 int c_in, c_out;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001616
Damien Miller33804262001-02-04 23:20:18 +11001617#ifdef USE_PIPES
1618 int pin[2], pout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001619
Damien Miller33804262001-02-04 23:20:18 +11001620 if ((pipe(pin) == -1) || (pipe(pout) == -1))
1621 fatal("pipe: %s", strerror(errno));
1622 *in = pin[0];
1623 *out = pout[1];
1624 c_in = pout[0];
1625 c_out = pin[1];
1626#else /* USE_PIPES */
1627 int inout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001628
Damien Miller33804262001-02-04 23:20:18 +11001629 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
1630 fatal("socketpair: %s", strerror(errno));
1631 *in = *out = inout[0];
1632 c_in = c_out = inout[1];
1633#endif /* USE_PIPES */
1634
Damien Millercc685c12003-06-04 22:51:38 +10001635 if ((sshpid = fork()) == -1)
Damien Miller33804262001-02-04 23:20:18 +11001636 fatal("fork: %s", strerror(errno));
Damien Millercc685c12003-06-04 22:51:38 +10001637 else if (sshpid == 0) {
Damien Miller33804262001-02-04 23:20:18 +11001638 if ((dup2(c_in, STDIN_FILENO) == -1) ||
1639 (dup2(c_out, STDOUT_FILENO) == -1)) {
1640 fprintf(stderr, "dup2: %s\n", strerror(errno));
Damien Miller350327c2004-06-15 10:24:13 +10001641 _exit(1);
Damien Miller33804262001-02-04 23:20:18 +11001642 }
1643 close(*in);
1644 close(*out);
1645 close(c_in);
1646 close(c_out);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001647
1648 /*
1649 * The underlying ssh is in the same process group, so we must
Darren Tuckerfc959702004-07-17 16:12:08 +10001650 * ignore SIGINT if we want to gracefully abort commands,
1651 * otherwise the signal will make it to the ssh process and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001652 * kill it too
1653 */
1654 signal(SIGINT, SIG_IGN);
Darren Tuckerbd12f172004-06-18 16:23:43 +10001655 execvp(path, args);
Damien Millerd14ee1e2002-02-05 12:27:31 +11001656 fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
Damien Miller350327c2004-06-15 10:24:13 +10001657 _exit(1);
Damien Miller33804262001-02-04 23:20:18 +11001658 }
1659
Damien Millercc685c12003-06-04 22:51:38 +10001660 signal(SIGTERM, killchild);
1661 signal(SIGINT, killchild);
1662 signal(SIGHUP, killchild);
Damien Miller33804262001-02-04 23:20:18 +11001663 close(c_in);
1664 close(c_out);
1665}
1666
Ben Lindstrombba81212001-06-25 05:01:22 +00001667static void
Damien Miller33804262001-02-04 23:20:18 +11001668usage(void)
1669{
Damien Miller025e01c2002-02-08 22:06:29 +11001670 extern char *__progname;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001671
Ben Lindstrom1e243242001-09-18 05:38:44 +00001672 fprintf(stderr,
Darren Tucker1f203942003-10-15 15:50:42 +10001673 "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n"
1674 " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n"
1675 " [-S program] [-s subsystem | sftp_server] host\n"
1676 " %s [[user@]host[:file [file]]]\n"
1677 " %s [[user@]host[:dir[/]]]\n"
1678 " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname);
Damien Miller33804262001-02-04 23:20:18 +11001679 exit(1);
1680}
1681
Kevin Stevesef4eea92001-02-05 12:42:17 +00001682int
Damien Miller33804262001-02-04 23:20:18 +11001683main(int argc, char **argv)
1684{
Damien Miller956f3fb2003-01-10 21:40:00 +11001685 int in, out, ch, err;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001686 char *host, *userhost, *cp, *file2 = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +00001687 int debug_level = 0, sshver = 2;
1688 char *file1 = NULL, *sftp_server = NULL;
Damien Millerd14ee1e2002-02-05 12:27:31 +11001689 char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +00001690 LogLevel ll = SYSLOG_LEVEL_INFO;
1691 arglist args;
Damien Millerd7686fd2001-02-10 00:40:03 +11001692 extern int optind;
1693 extern char *optarg;
Damien Miller33804262001-02-04 23:20:18 +11001694
Darren Tuckerce321d82005-10-03 18:11:24 +10001695 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1696 sanitise_stdfd();
1697
Damien Miller59d3d5b2003-08-22 09:34:41 +10001698 __progname = ssh_get_progname(argv[0]);
Damien Miller3eec6b72006-01-31 21:49:27 +11001699 memset(&args, '\0', sizeof(args));
Ben Lindstrom387c4722001-05-08 20:27:25 +00001700 args.list = NULL;
Damien Miller2b5a0de2006-03-31 23:10:31 +11001701 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +00001702 addargs(&args, "-oForwardX11 no");
1703 addargs(&args, "-oForwardAgent no");
Damien Millerd27b9472005-12-13 19:29:02 +11001704 addargs(&args, "-oPermitLocalCommand no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001705 addargs(&args, "-oClearAllForwardings yes");
Damien Millere4f5a822004-01-21 14:11:05 +11001706
Ben Lindstrom387c4722001-05-08 20:27:25 +00001707 ll = SYSLOG_LEVEL_INFO;
Damien Millere4f5a822004-01-21 14:11:05 +11001708 infile = stdin;
Damien Millerd7686fd2001-02-10 00:40:03 +11001709
Damien Miller16a13332002-02-13 14:03:56 +11001710 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +11001711 switch (ch) {
1712 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +00001713 addargs(&args, "-C");
Damien Millerd7686fd2001-02-10 00:40:03 +11001714 break;
1715 case 'v':
Ben Lindstrom387c4722001-05-08 20:27:25 +00001716 if (debug_level < 3) {
1717 addargs(&args, "-v");
1718 ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
1719 }
1720 debug_level++;
Damien Millerd7686fd2001-02-10 00:40:03 +11001721 break;
Ben Lindstrom1e243242001-09-18 05:38:44 +00001722 case 'F':
Damien Millerd7686fd2001-02-10 00:40:03 +11001723 case 'o':
Ben Lindstrom1e243242001-09-18 05:38:44 +00001724 addargs(&args, "-%c%s", ch, optarg);
Damien Millerd7686fd2001-02-10 00:40:03 +11001725 break;
1726 case '1':
Ben Lindstrom387c4722001-05-08 20:27:25 +00001727 sshver = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +11001728 if (sftp_server == NULL)
1729 sftp_server = _PATH_SFTP_SERVER;
1730 break;
1731 case 's':
1732 sftp_server = optarg;
1733 break;
1734 case 'S':
1735 ssh_program = optarg;
Damien Miller3eec6b72006-01-31 21:49:27 +11001736 replacearg(&args, 0, "%s", ssh_program);
Damien Millerd7686fd2001-02-10 00:40:03 +11001737 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001738 case 'b':
Damien Miller44f75c12004-01-21 10:58:47 +11001739 if (batchmode)
1740 fatal("Batch file already specified.");
1741
1742 /* Allow "-" as stdin */
Darren Tuckerfc959702004-07-17 16:12:08 +10001743 if (strcmp(optarg, "-") != 0 &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001744 (infile = fopen(optarg, "r")) == NULL)
Damien Miller44f75c12004-01-21 10:58:47 +11001745 fatal("%s (%s).", strerror(errno), optarg);
Damien Miller62d57f62003-01-10 21:43:24 +11001746 showprogress = 0;
Damien Miller44f75c12004-01-21 10:58:47 +11001747 batchmode = 1;
Damien Miller64e8d442005-03-01 21:16:47 +11001748 addargs(&args, "-obatchmode yes");
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001749 break;
Damien Millerd14ee1e2002-02-05 12:27:31 +11001750 case 'P':
1751 sftp_direct = optarg;
1752 break;
Damien Miller8829d362002-02-08 22:04:05 +11001753 case 'B':
1754 copy_buffer_len = strtol(optarg, &cp, 10);
1755 if (copy_buffer_len == 0 || *cp != '\0')
1756 fatal("Invalid buffer size \"%s\"", optarg);
1757 break;
Damien Miller16a13332002-02-13 14:03:56 +11001758 case 'R':
1759 num_requests = strtol(optarg, &cp, 10);
1760 if (num_requests == 0 || *cp != '\0')
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001761 fatal("Invalid number of requests \"%s\"",
Damien Miller16a13332002-02-13 14:03:56 +11001762 optarg);
1763 break;
Damien Millerd7686fd2001-02-10 00:40:03 +11001764 case 'h':
1765 default:
Damien Miller33804262001-02-04 23:20:18 +11001766 usage();
1767 }
1768 }
1769
Damien Millerc0f27d82004-03-08 23:12:19 +11001770 if (!isatty(STDERR_FILENO))
1771 showprogress = 0;
1772
Ben Lindstrom2f3d52a2002-04-02 21:06:18 +00001773 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
1774
Damien Millerd14ee1e2002-02-05 12:27:31 +11001775 if (sftp_direct == NULL) {
1776 if (optind == argc || argc > (optind + 2))
1777 usage();
Damien Miller33804262001-02-04 23:20:18 +11001778
Damien Millerd14ee1e2002-02-05 12:27:31 +11001779 userhost = xstrdup(argv[optind]);
1780 file2 = argv[optind+1];
Ben Lindstrom63667f62001-04-13 00:00:14 +00001781
Ben Lindstromc276c122002-12-23 02:14:51 +00001782 if ((host = strrchr(userhost, '@')) == NULL)
Damien Millerd14ee1e2002-02-05 12:27:31 +11001783 host = userhost;
1784 else {
1785 *host++ = '\0';
1786 if (!userhost[0]) {
1787 fprintf(stderr, "Missing username\n");
1788 usage();
1789 }
Damien Miller80163902007-01-05 16:30:16 +11001790 addargs(&args, "-l%s", userhost);
Damien Millerd14ee1e2002-02-05 12:27:31 +11001791 }
1792
Damien Millerec692032004-01-27 21:22:00 +11001793 if ((cp = colon(host)) != NULL) {
1794 *cp++ = '\0';
1795 file1 = cp;
1796 }
1797
Damien Millerd14ee1e2002-02-05 12:27:31 +11001798 host = cleanhostname(host);
1799 if (!*host) {
1800 fprintf(stderr, "Missing hostname\n");
Damien Miller33804262001-02-04 23:20:18 +11001801 usage();
1802 }
Damien Millerd14ee1e2002-02-05 12:27:31 +11001803
Damien Millerd14ee1e2002-02-05 12:27:31 +11001804 addargs(&args, "-oProtocol %d", sshver);
1805
1806 /* no subsystem if the server-spec contains a '/' */
1807 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
1808 addargs(&args, "-s");
1809
1810 addargs(&args, "%s", host);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001811 addargs(&args, "%s", (sftp_server != NULL ?
Damien Millerd14ee1e2002-02-05 12:27:31 +11001812 sftp_server : "sftp"));
Damien Millerd14ee1e2002-02-05 12:27:31 +11001813
Damien Miller44f75c12004-01-21 10:58:47 +11001814 if (!batchmode)
1815 fprintf(stderr, "Connecting to %s...\n", host);
Damien Millercc685c12003-06-04 22:51:38 +10001816 connect_to_server(ssh_program, args.list, &in, &out);
Damien Millerd14ee1e2002-02-05 12:27:31 +11001817 } else {
1818 args.list = NULL;
1819 addargs(&args, "sftp-server");
1820
Damien Miller44f75c12004-01-21 10:58:47 +11001821 if (!batchmode)
1822 fprintf(stderr, "Attaching to %s...\n", sftp_direct);
Damien Millercc685c12003-06-04 22:51:38 +10001823 connect_to_server(sftp_direct, args.list, &in, &out);
Damien Miller33804262001-02-04 23:20:18 +11001824 }
Damien Miller3eec6b72006-01-31 21:49:27 +11001825 freeargs(&args);
Damien Miller33804262001-02-04 23:20:18 +11001826
Damien Miller956f3fb2003-01-10 21:40:00 +11001827 err = interactive_loop(in, out, file1, file2);
Damien Miller33804262001-02-04 23:20:18 +11001828
Ben Lindstrom10b9bf92001-02-26 20:04:45 +00001829#if !defined(USE_PIPES)
Damien Miller37294fb2005-07-17 17:18:49 +10001830 shutdown(in, SHUT_RDWR);
1831 shutdown(out, SHUT_RDWR);
Ben Lindstrom10b9bf92001-02-26 20:04:45 +00001832#endif
1833
Damien Miller33804262001-02-04 23:20:18 +11001834 close(in);
1835 close(out);
Damien Miller44f75c12004-01-21 10:58:47 +11001836 if (batchmode)
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001837 fclose(infile);
Damien Miller33804262001-02-04 23:20:18 +11001838
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001839 while (waitpid(sshpid, NULL, 0) == -1)
1840 if (errno != EINTR)
1841 fatal("Couldn't wait for ssh process: %s",
1842 strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +11001843
Damien Miller956f3fb2003-01-10 21:40:00 +11001844 exit(err == 0 ? 0 : 1);
Damien Miller33804262001-02-04 23:20:18 +11001845}