blob: c68fb2cf08fde443566b703cd3118ce3f1edc0e8 [file] [log] [blame]
Damien Miller02e87802013-08-21 02:38:51 +10001/* $OpenBSD: sftp.c,v 1.149 2013/08/06 23:03:49 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 Tucker1b0dd172009-10-07 08:37:48 +110038#ifdef HAVE_LIBGEN_H
39#include <libgen.h>
40#endif
Darren Tuckerae133d42013-06-06 08:30:20 +100041#ifdef HAVE_LOCALE_H
42# include <locale.h>
43#endif
Darren Tucker2d963d82004-11-07 20:04:10 +110044#ifdef USE_LIBEDIT
45#include <histedit.h>
46#else
47typedef void EditLine;
48#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110049#include <signal.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100050#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100051#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100052#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100053#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100054#include <stdarg.h>
Damien Miller33804262001-02-04 23:20:18 +110055
Damien Millera7058ec2008-05-20 08:57:06 +100056#ifdef HAVE_UTIL_H
57# include <util.h>
58#endif
59
Damien Miller33804262001-02-04 23:20:18 +110060#include "xmalloc.h"
61#include "log.h"
62#include "pathnames.h"
Ben Lindstrom4529b702001-05-03 23:39:53 +000063#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110064
65#include "sftp.h"
Damien Millerd7834352006-08-05 12:39:39 +100066#include "buffer.h"
Damien Miller33804262001-02-04 23:20:18 +110067#include "sftp-common.h"
68#include "sftp-client.h"
Damien Millerd7d46bb2004-02-18 14:11:13 +110069
Darren Tucker21063192010-01-08 17:10:36 +110070#define DEFAULT_COPY_BUFLEN 32768 /* Size of buffer for up/download */
71#define DEFAULT_NUM_REQUESTS 64 /* # concurrent outstanding requests */
72
Damien Miller20e1fab2004-02-18 14:30:55 +110073/* File to read commands from */
74FILE* infile;
75
76/* Are we in batchfile mode? */
77int batchmode = 0;
78
Damien Miller20e1fab2004-02-18 14:30:55 +110079/* PID of ssh transport process */
80static pid_t sshpid = -1;
81
Damien Miller9303e652013-04-23 15:22:40 +100082/* Suppress diagnositic messages */
83int quiet = 0;
84
Damien Miller20e1fab2004-02-18 14:30:55 +110085/* This is set to 0 if the progressmeter is not desired. */
Damien Millerc0f27d82004-03-08 23:12:19 +110086int showprogress = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +110087
Darren Tucker1b0dd172009-10-07 08:37:48 +110088/* When this option is set, we always recursively download/upload directories */
89int global_rflag = 0;
90
Damien Miller0d032412013-07-25 11:56:52 +100091/* When this option is set, we resume download if possible */
92int global_aflag = 0;
93
Darren Tucker1b0dd172009-10-07 08:37:48 +110094/* When this option is set, the file transfers will always preserve times */
95int global_pflag = 0;
96
Darren Tuckercdf547a2004-05-24 10:12:19 +100097/* SIGINT received during command processing */
98volatile sig_atomic_t interrupted = 0;
99
Darren Tuckerb9123452004-06-22 13:06:45 +1000100/* I wish qsort() took a separate ctx for the comparison function...*/
101int sort_flag;
102
Darren Tucker909d8582010-01-08 19:02:40 +1100103/* Context used for commandline completion */
104struct complete_ctx {
105 struct sftp_conn *conn;
106 char **remote_pathp;
107};
108
Damien Miller20e1fab2004-02-18 14:30:55 +1100109int remote_glob(struct sftp_conn *, const char *, int,
110 int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
Damien Miller33804262001-02-04 23:20:18 +1100111
Kevin Steves12888d12001-03-05 19:50:57 +0000112extern char *__progname;
Kevin Steves12888d12001-03-05 19:50:57 +0000113
Damien Miller20e1fab2004-02-18 14:30:55 +1100114/* Separators for interactive commands */
115#define WHITESPACE " \t\r\n"
Damien Millerd7686fd2001-02-10 00:40:03 +1100116
Darren Tuckerb9123452004-06-22 13:06:45 +1000117/* ls flags */
Darren Tucker2901e2d2010-01-13 22:44:06 +1100118#define LS_LONG_VIEW 0x0001 /* Full view ala ls -l */
119#define LS_SHORT_VIEW 0x0002 /* Single row view ala ls -1 */
120#define LS_NUMERIC_VIEW 0x0004 /* Long view with numeric uid/gid */
121#define LS_NAME_SORT 0x0008 /* Sort by name (default) */
122#define LS_TIME_SORT 0x0010 /* Sort by mtime */
123#define LS_SIZE_SORT 0x0020 /* Sort by file size */
124#define LS_REVERSE_SORT 0x0040 /* Reverse sort order */
125#define LS_SHOW_ALL 0x0080 /* Don't skip filenames starting with '.' */
126#define LS_SI_UNITS 0x0100 /* Display sizes as K, M, G, etc. */
Darren Tuckerb9123452004-06-22 13:06:45 +1000127
Darren Tucker2901e2d2010-01-13 22:44:06 +1100128#define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW|LS_SI_UNITS)
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000129#define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
Damien Miller20e1fab2004-02-18 14:30:55 +1100130
131/* Commands for interactive mode */
Damien Miller02e87802013-08-21 02:38:51 +1000132enum sftp_command {
133 I_CHDIR = 1,
134 I_CHGRP,
135 I_CHMOD,
136 I_CHOWN,
137 I_DF,
138 I_GET,
139 I_HELP,
140 I_LCHDIR,
141 I_LINK,
142 I_LLS,
143 I_LMKDIR,
144 I_LPWD,
145 I_LS,
146 I_LUMASK,
147 I_MKDIR,
148 I_PUT,
149 I_PWD,
150 I_QUIT,
151 I_RENAME,
152 I_RM,
153 I_RMDIR,
154 I_SHELL,
155 I_SYMLINK,
156 I_VERSION,
157 I_PROGRESS,
158 I_REGET,
159};
Damien Miller20e1fab2004-02-18 14:30:55 +1100160
161struct CMD {
162 const char *c;
163 const int n;
Darren Tucker909d8582010-01-08 19:02:40 +1100164 const int t;
Damien Miller20e1fab2004-02-18 14:30:55 +1100165};
166
Darren Tucker909d8582010-01-08 19:02:40 +1100167/* Type of completion */
168#define NOARGS 0
169#define REMOTE 1
170#define LOCAL 2
171
Damien Miller20e1fab2004-02-18 14:30:55 +1100172static const struct CMD cmds[] = {
Darren Tucker909d8582010-01-08 19:02:40 +1100173 { "bye", I_QUIT, NOARGS },
174 { "cd", I_CHDIR, REMOTE },
175 { "chdir", I_CHDIR, REMOTE },
176 { "chgrp", I_CHGRP, REMOTE },
177 { "chmod", I_CHMOD, REMOTE },
178 { "chown", I_CHOWN, REMOTE },
179 { "df", I_DF, REMOTE },
180 { "dir", I_LS, REMOTE },
181 { "exit", I_QUIT, NOARGS },
182 { "get", I_GET, REMOTE },
183 { "help", I_HELP, NOARGS },
184 { "lcd", I_LCHDIR, LOCAL },
185 { "lchdir", I_LCHDIR, LOCAL },
186 { "lls", I_LLS, LOCAL },
187 { "lmkdir", I_LMKDIR, LOCAL },
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100188 { "ln", I_LINK, REMOTE },
Darren Tucker909d8582010-01-08 19:02:40 +1100189 { "lpwd", I_LPWD, LOCAL },
190 { "ls", I_LS, REMOTE },
191 { "lumask", I_LUMASK, NOARGS },
192 { "mkdir", I_MKDIR, REMOTE },
Damien Miller099fc162010-05-10 11:56:50 +1000193 { "mget", I_GET, REMOTE },
194 { "mput", I_PUT, LOCAL },
Darren Tucker909d8582010-01-08 19:02:40 +1100195 { "progress", I_PROGRESS, NOARGS },
196 { "put", I_PUT, LOCAL },
197 { "pwd", I_PWD, REMOTE },
198 { "quit", I_QUIT, NOARGS },
Damien Miller0d032412013-07-25 11:56:52 +1000199 { "reget", I_REGET, REMOTE },
Darren Tucker909d8582010-01-08 19:02:40 +1100200 { "rename", I_RENAME, REMOTE },
201 { "rm", I_RM, REMOTE },
202 { "rmdir", I_RMDIR, REMOTE },
203 { "symlink", I_SYMLINK, REMOTE },
204 { "version", I_VERSION, NOARGS },
205 { "!", I_SHELL, NOARGS },
206 { "?", I_HELP, NOARGS },
207 { NULL, -1, -1 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100208};
209
Darren Tucker21063192010-01-08 17:10:36 +1100210int interactive_loop(struct sftp_conn *, char *file1, char *file2);
Damien Miller20e1fab2004-02-18 14:30:55 +1100211
Damien Millerb6c85fc2007-01-05 16:30:41 +1100212/* ARGSUSED */
Damien Miller20e1fab2004-02-18 14:30:55 +1100213static void
Darren Tuckercdf547a2004-05-24 10:12:19 +1000214killchild(int signo)
215{
Darren Tuckerba66df82005-01-24 21:57:40 +1100216 if (sshpid > 1) {
Darren Tuckercdf547a2004-05-24 10:12:19 +1000217 kill(sshpid, SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100218 waitpid(sshpid, NULL, 0);
219 }
Darren Tuckercdf547a2004-05-24 10:12:19 +1000220
221 _exit(1);
222}
223
Damien Millerb6c85fc2007-01-05 16:30:41 +1100224/* ARGSUSED */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000225static void
226cmd_interrupt(int signo)
227{
228 const char msg[] = "\rInterrupt \n";
Darren Tuckere2f189a2004-12-06 22:45:53 +1100229 int olderrno = errno;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000230
Darren Tuckerdbee3082013-05-16 20:32:29 +1000231 (void)write(STDERR_FILENO, msg, sizeof(msg) - 1);
Darren Tuckercdf547a2004-05-24 10:12:19 +1000232 interrupted = 1;
Darren Tuckere2f189a2004-12-06 22:45:53 +1100233 errno = olderrno;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000234}
235
236static void
Damien Miller20e1fab2004-02-18 14:30:55 +1100237help(void)
238{
Damien Miller62fd18a2009-01-28 16:14:09 +1100239 printf("Available commands:\n"
240 "bye Quit sftp\n"
241 "cd path Change remote directory to 'path'\n"
242 "chgrp grp path Change group of file 'path' to 'grp'\n"
243 "chmod mode path Change permissions of file 'path' to 'mode'\n"
244 "chown own path Change owner of file 'path' to 'own'\n"
245 "df [-hi] [path] Display statistics for current directory or\n"
246 " filesystem containing 'path'\n"
247 "exit Quit sftp\n"
Darren Tucker75fe6262010-01-15 11:42:51 +1100248 "get [-Ppr] remote [local] Download file\n"
Damien Miller0d032412013-07-25 11:56:52 +1000249 "reget remote [local] Resume download file\n"
Damien Miller62fd18a2009-01-28 16:14:09 +1100250 "help Display this help text\n"
251 "lcd path Change local directory to 'path'\n"
252 "lls [ls-options [path]] Display local directory listing\n"
253 "lmkdir path Create local directory\n"
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100254 "ln [-s] oldpath newpath Link remote file (-s for symlink)\n"
Damien Miller62fd18a2009-01-28 16:14:09 +1100255 "lpwd Print local working directory\n"
Darren Tucker75fe6262010-01-15 11:42:51 +1100256 "ls [-1afhlnrSt] [path] Display remote directory listing\n"
Damien Miller62fd18a2009-01-28 16:14:09 +1100257 "lumask umask Set local umask to 'umask'\n"
258 "mkdir path Create remote directory\n"
259 "progress Toggle display of progress meter\n"
Darren Tucker75fe6262010-01-15 11:42:51 +1100260 "put [-Ppr] local [remote] Upload file\n"
Damien Miller62fd18a2009-01-28 16:14:09 +1100261 "pwd Display remote working directory\n"
262 "quit Quit sftp\n"
263 "rename oldpath newpath Rename remote file\n"
264 "rm path Delete remote file\n"
265 "rmdir path Remove remote directory\n"
266 "symlink oldpath newpath Symlink remote file\n"
267 "version Show SFTP version\n"
268 "!command Execute 'command' in local shell\n"
269 "! Escape to local shell\n"
270 "? Synonym for help\n");
Damien Miller20e1fab2004-02-18 14:30:55 +1100271}
272
273static void
274local_do_shell(const char *args)
275{
276 int status;
277 char *shell;
278 pid_t pid;
279
280 if (!*args)
281 args = NULL;
282
Damien Miller38d9a962010-10-07 22:07:11 +1100283 if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
Damien Miller20e1fab2004-02-18 14:30:55 +1100284 shell = _PATH_BSHELL;
285
286 if ((pid = fork()) == -1)
287 fatal("Couldn't fork: %s", strerror(errno));
288
289 if (pid == 0) {
290 /* XXX: child has pipe fds to ssh subproc open - issue? */
291 if (args) {
292 debug3("Executing %s -c \"%s\"", shell, args);
293 execl(shell, shell, "-c", args, (char *)NULL);
294 } else {
295 debug3("Executing %s", shell);
296 execl(shell, shell, (char *)NULL);
297 }
298 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
299 strerror(errno));
300 _exit(1);
301 }
302 while (waitpid(pid, &status, 0) == -1)
303 if (errno != EINTR)
304 fatal("Couldn't wait for child: %s", strerror(errno));
305 if (!WIFEXITED(status))
Damien Miller55b04f12006-03-26 14:23:17 +1100306 error("Shell exited abnormally");
Damien Miller20e1fab2004-02-18 14:30:55 +1100307 else if (WEXITSTATUS(status))
308 error("Shell exited with status %d", WEXITSTATUS(status));
309}
310
311static void
312local_do_ls(const char *args)
313{
314 if (!args || !*args)
315 local_do_shell(_PATH_LS);
316 else {
317 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
318 char *buf = xmalloc(len);
319
320 /* XXX: quoting - rip quoting code from ftp? */
321 snprintf(buf, len, _PATH_LS " %s", args);
322 local_do_shell(buf);
Darren Tuckera627d422013-06-02 07:31:17 +1000323 free(buf);
Damien Miller20e1fab2004-02-18 14:30:55 +1100324 }
325}
326
327/* Strip one path (usually the pwd) from the start of another */
328static char *
329path_strip(char *path, char *strip)
330{
331 size_t len;
332
333 if (strip == NULL)
334 return (xstrdup(path));
335
336 len = strlen(strip);
Darren Tuckere2f189a2004-12-06 22:45:53 +1100337 if (strncmp(path, strip, len) == 0) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100338 if (strip[len - 1] != '/' && path[len] == '/')
339 len++;
340 return (xstrdup(path + len));
341 }
342
343 return (xstrdup(path));
344}
345
346static char *
Damien Miller20e1fab2004-02-18 14:30:55 +1100347make_absolute(char *p, char *pwd)
348{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000349 char *abs_str;
Damien Miller20e1fab2004-02-18 14:30:55 +1100350
351 /* Derelativise */
352 if (p && p[0] != '/') {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000353 abs_str = path_append(pwd, p);
Darren Tuckera627d422013-06-02 07:31:17 +1000354 free(p);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000355 return(abs_str);
Damien Miller20e1fab2004-02-18 14:30:55 +1100356 } else
357 return(p);
358}
359
360static int
Damien Miller0d032412013-07-25 11:56:52 +1000361parse_getput_flags(const char *cmd, char **argv, int argc,
362 int *aflag, int *pflag, int *rflag)
Damien Miller20e1fab2004-02-18 14:30:55 +1100363{
Damien Millerf184bcf2008-06-29 22:45:13 +1000364 extern int opterr, optind, optopt, optreset;
Damien Miller1cbc2922007-10-26 14:27:45 +1000365 int ch;
Damien Miller20e1fab2004-02-18 14:30:55 +1100366
Damien Miller1cbc2922007-10-26 14:27:45 +1000367 optind = optreset = 1;
368 opterr = 0;
369
Damien Miller0d032412013-07-25 11:56:52 +1000370 *aflag = *rflag = *pflag = 0;
371 while ((ch = getopt(argc, argv, "aPpRr")) != -1) {
Damien Miller1cbc2922007-10-26 14:27:45 +1000372 switch (ch) {
Damien Miller0d032412013-07-25 11:56:52 +1000373 case 'a':
374 *aflag = 1;
375 break;
Damien Miller20e1fab2004-02-18 14:30:55 +1100376 case 'p':
377 case 'P':
378 *pflag = 1;
379 break;
Darren Tucker1b0dd172009-10-07 08:37:48 +1100380 case 'r':
381 case 'R':
382 *rflag = 1;
383 break;
Damien Miller20e1fab2004-02-18 14:30:55 +1100384 default:
Damien Millerf184bcf2008-06-29 22:45:13 +1000385 error("%s: Invalid flag -%c", cmd, optopt);
Damien Miller1cbc2922007-10-26 14:27:45 +1000386 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100387 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100388 }
389
Damien Miller1cbc2922007-10-26 14:27:45 +1000390 return optind;
Damien Miller20e1fab2004-02-18 14:30:55 +1100391}
392
393static int
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100394parse_link_flags(const char *cmd, char **argv, int argc, int *sflag)
395{
396 extern int opterr, optind, optopt, optreset;
397 int ch;
398
399 optind = optreset = 1;
400 opterr = 0;
401
402 *sflag = 0;
403 while ((ch = getopt(argc, argv, "s")) != -1) {
404 switch (ch) {
405 case 's':
406 *sflag = 1;
407 break;
408 default:
409 error("%s: Invalid flag -%c", cmd, optopt);
410 return -1;
411 }
412 }
413
414 return optind;
415}
416
417static int
Damien Miller1cbc2922007-10-26 14:27:45 +1000418parse_ls_flags(char **argv, int argc, int *lflag)
Damien Miller20e1fab2004-02-18 14:30:55 +1100419{
Damien Millerf184bcf2008-06-29 22:45:13 +1000420 extern int opterr, optind, optopt, optreset;
Damien Miller1cbc2922007-10-26 14:27:45 +1000421 int ch;
Damien Miller20e1fab2004-02-18 14:30:55 +1100422
Damien Miller1cbc2922007-10-26 14:27:45 +1000423 optind = optreset = 1;
424 opterr = 0;
425
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000426 *lflag = LS_NAME_SORT;
Darren Tucker2901e2d2010-01-13 22:44:06 +1100427 while ((ch = getopt(argc, argv, "1Safhlnrt")) != -1) {
Damien Miller1cbc2922007-10-26 14:27:45 +1000428 switch (ch) {
429 case '1':
430 *lflag &= ~VIEW_FLAGS;
431 *lflag |= LS_SHORT_VIEW;
432 break;
433 case 'S':
434 *lflag &= ~SORT_FLAGS;
435 *lflag |= LS_SIZE_SORT;
436 break;
437 case 'a':
438 *lflag |= LS_SHOW_ALL;
439 break;
440 case 'f':
441 *lflag &= ~SORT_FLAGS;
442 break;
Darren Tucker2901e2d2010-01-13 22:44:06 +1100443 case 'h':
444 *lflag |= LS_SI_UNITS;
445 break;
Damien Miller1cbc2922007-10-26 14:27:45 +1000446 case 'l':
Darren Tucker2901e2d2010-01-13 22:44:06 +1100447 *lflag &= ~LS_SHORT_VIEW;
Damien Miller1cbc2922007-10-26 14:27:45 +1000448 *lflag |= LS_LONG_VIEW;
449 break;
450 case 'n':
Darren Tucker2901e2d2010-01-13 22:44:06 +1100451 *lflag &= ~LS_SHORT_VIEW;
Damien Miller1cbc2922007-10-26 14:27:45 +1000452 *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
453 break;
454 case 'r':
455 *lflag |= LS_REVERSE_SORT;
456 break;
457 case 't':
458 *lflag &= ~SORT_FLAGS;
459 *lflag |= LS_TIME_SORT;
460 break;
461 default:
Damien Millerf184bcf2008-06-29 22:45:13 +1000462 error("ls: Invalid flag -%c", optopt);
Damien Miller1cbc2922007-10-26 14:27:45 +1000463 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100464 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100465 }
466
Damien Miller1cbc2922007-10-26 14:27:45 +1000467 return optind;
Damien Miller20e1fab2004-02-18 14:30:55 +1100468}
469
470static int
Damien Millerd671e5a2008-05-19 14:53:33 +1000471parse_df_flags(const char *cmd, char **argv, int argc, int *hflag, int *iflag)
472{
Damien Millerf184bcf2008-06-29 22:45:13 +1000473 extern int opterr, optind, optopt, optreset;
Damien Millerd671e5a2008-05-19 14:53:33 +1000474 int ch;
475
476 optind = optreset = 1;
477 opterr = 0;
478
479 *hflag = *iflag = 0;
480 while ((ch = getopt(argc, argv, "hi")) != -1) {
481 switch (ch) {
482 case 'h':
483 *hflag = 1;
484 break;
485 case 'i':
486 *iflag = 1;
487 break;
488 default:
Damien Millerf184bcf2008-06-29 22:45:13 +1000489 error("%s: Invalid flag -%c", cmd, optopt);
Damien Millerd671e5a2008-05-19 14:53:33 +1000490 return -1;
491 }
492 }
493
494 return optind;
495}
496
497static int
Damien Miller20e1fab2004-02-18 14:30:55 +1100498is_dir(char *path)
499{
500 struct stat sb;
501
502 /* XXX: report errors? */
503 if (stat(path, &sb) == -1)
504 return(0);
505
Darren Tucker1e80e402006-09-21 12:59:33 +1000506 return(S_ISDIR(sb.st_mode));
Damien Miller20e1fab2004-02-18 14:30:55 +1100507}
508
509static int
Damien Miller20e1fab2004-02-18 14:30:55 +1100510remote_is_dir(struct sftp_conn *conn, char *path)
511{
512 Attrib *a;
513
514 /* XXX: report errors? */
515 if ((a = do_stat(conn, path, 1)) == NULL)
516 return(0);
517 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
518 return(0);
Darren Tucker1e80e402006-09-21 12:59:33 +1000519 return(S_ISDIR(a->perm));
Damien Miller20e1fab2004-02-18 14:30:55 +1100520}
521
Darren Tucker1b0dd172009-10-07 08:37:48 +1100522/* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
Damien Miller20e1fab2004-02-18 14:30:55 +1100523static int
Darren Tucker1b0dd172009-10-07 08:37:48 +1100524pathname_is_dir(char *pathname)
525{
526 size_t l = strlen(pathname);
527
528 return l > 0 && pathname[l - 1] == '/';
529}
530
531static int
532process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd,
Damien Miller0d032412013-07-25 11:56:52 +1000533 int pflag, int rflag, int resume)
Damien Miller20e1fab2004-02-18 14:30:55 +1100534{
535 char *abs_src = NULL;
536 char *abs_dst = NULL;
Damien Miller20e1fab2004-02-18 14:30:55 +1100537 glob_t g;
Darren Tucker1b0dd172009-10-07 08:37:48 +1100538 char *filename, *tmp=NULL;
539 int i, err = 0;
Damien Miller20e1fab2004-02-18 14:30:55 +1100540
541 abs_src = xstrdup(src);
542 abs_src = make_absolute(abs_src, pwd);
Damien Miller20e1fab2004-02-18 14:30:55 +1100543 memset(&g, 0, sizeof(g));
Darren Tucker1b0dd172009-10-07 08:37:48 +1100544
Damien Miller20e1fab2004-02-18 14:30:55 +1100545 debug3("Looking up %s", abs_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +1100546 if (remote_glob(conn, abs_src, GLOB_MARK, NULL, &g)) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100547 error("File \"%s\" not found.", abs_src);
548 err = -1;
549 goto out;
550 }
551
Darren Tucker1b0dd172009-10-07 08:37:48 +1100552 /*
553 * If multiple matches then dst must be a directory or
554 * unspecified.
555 */
556 if (g.gl_matchc > 1 && dst != NULL && !is_dir(dst)) {
557 error("Multiple source paths, but destination "
558 "\"%s\" is not a directory", dst);
Damien Miller20e1fab2004-02-18 14:30:55 +1100559 err = -1;
560 goto out;
561 }
562
Darren Tuckercdf547a2004-05-24 10:12:19 +1000563 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Darren Tucker1b0dd172009-10-07 08:37:48 +1100564 tmp = xstrdup(g.gl_pathv[i]);
565 if ((filename = basename(tmp)) == NULL) {
566 error("basename %s: %s", tmp, strerror(errno));
Darren Tuckera627d422013-06-02 07:31:17 +1000567 free(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +1100568 err = -1;
569 goto out;
570 }
571
572 if (g.gl_matchc == 1 && dst) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100573 if (is_dir(dst)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +1100574 abs_dst = path_append(dst, filename);
575 } else {
Damien Miller20e1fab2004-02-18 14:30:55 +1100576 abs_dst = xstrdup(dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +1100577 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100578 } else if (dst) {
Darren Tucker1b0dd172009-10-07 08:37:48 +1100579 abs_dst = path_append(dst, filename);
580 } else {
581 abs_dst = xstrdup(filename);
582 }
Darren Tuckera627d422013-06-02 07:31:17 +1000583 free(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +1100584
Damien Miller0d032412013-07-25 11:56:52 +1000585 resume |= global_aflag;
586 if (!quiet && resume)
587 printf("Resuming %s to %s\n", g.gl_pathv[i], abs_dst);
588 else if (!quiet && !resume)
Damien Miller9303e652013-04-23 15:22:40 +1000589 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +1100590 if (pathname_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
Damien Miller0d032412013-07-25 11:56:52 +1000591 if (download_dir(conn, g.gl_pathv[i], abs_dst, NULL,
592 pflag || global_pflag, 1, resume) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +1100593 err = -1;
594 } else {
595 if (do_download(conn, g.gl_pathv[i], abs_dst, NULL,
Damien Miller0d032412013-07-25 11:56:52 +1000596 pflag || global_pflag, resume) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +1100597 err = -1;
598 }
Darren Tuckera627d422013-06-02 07:31:17 +1000599 free(abs_dst);
Damien Miller20e1fab2004-02-18 14:30:55 +1100600 abs_dst = NULL;
601 }
602
603out:
Darren Tuckera627d422013-06-02 07:31:17 +1000604 free(abs_src);
Damien Miller20e1fab2004-02-18 14:30:55 +1100605 globfree(&g);
606 return(err);
607}
608
609static int
Darren Tucker1b0dd172009-10-07 08:37:48 +1100610process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd,
611 int pflag, int rflag)
Damien Miller20e1fab2004-02-18 14:30:55 +1100612{
613 char *tmp_dst = NULL;
614 char *abs_dst = NULL;
Darren Tucker1b0dd172009-10-07 08:37:48 +1100615 char *tmp = NULL, *filename = NULL;
Damien Miller20e1fab2004-02-18 14:30:55 +1100616 glob_t g;
617 int err = 0;
Darren Tucker1b0dd172009-10-07 08:37:48 +1100618 int i, dst_is_dir = 1;
Damien Milleraec5cf82008-02-10 22:26:24 +1100619 struct stat sb;
Damien Miller20e1fab2004-02-18 14:30:55 +1100620
621 if (dst) {
622 tmp_dst = xstrdup(dst);
623 tmp_dst = make_absolute(tmp_dst, pwd);
624 }
625
626 memset(&g, 0, sizeof(g));
627 debug3("Looking up %s", src);
Darren Tucker1b0dd172009-10-07 08:37:48 +1100628 if (glob(src, GLOB_NOCHECK | GLOB_MARK, NULL, &g)) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100629 error("File \"%s\" not found.", src);
630 err = -1;
631 goto out;
632 }
633
Darren Tucker1b0dd172009-10-07 08:37:48 +1100634 /* If we aren't fetching to pwd then stash this status for later */
635 if (tmp_dst != NULL)
636 dst_is_dir = remote_is_dir(conn, tmp_dst);
637
Damien Miller20e1fab2004-02-18 14:30:55 +1100638 /* If multiple matches, dst may be directory or unspecified */
Darren Tucker1b0dd172009-10-07 08:37:48 +1100639 if (g.gl_matchc > 1 && tmp_dst && !dst_is_dir) {
640 error("Multiple paths match, but destination "
641 "\"%s\" is not a directory", tmp_dst);
Damien Miller20e1fab2004-02-18 14:30:55 +1100642 err = -1;
643 goto out;
644 }
645
Darren Tuckercdf547a2004-05-24 10:12:19 +1000646 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Milleraec5cf82008-02-10 22:26:24 +1100647 if (stat(g.gl_pathv[i], &sb) == -1) {
648 err = -1;
649 error("stat %s: %s", g.gl_pathv[i], strerror(errno));
650 continue;
651 }
Damien Miller02e87802013-08-21 02:38:51 +1000652
Darren Tucker1b0dd172009-10-07 08:37:48 +1100653 tmp = xstrdup(g.gl_pathv[i]);
654 if ((filename = basename(tmp)) == NULL) {
655 error("basename %s: %s", tmp, strerror(errno));
Darren Tuckera627d422013-06-02 07:31:17 +1000656 free(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +1100657 err = -1;
658 goto out;
659 }
660
661 if (g.gl_matchc == 1 && tmp_dst) {
662 /* If directory specified, append filename */
Darren Tucker1b0dd172009-10-07 08:37:48 +1100663 if (dst_is_dir)
664 abs_dst = path_append(tmp_dst, filename);
665 else
Damien Miller20e1fab2004-02-18 14:30:55 +1100666 abs_dst = xstrdup(tmp_dst);
Damien Miller20e1fab2004-02-18 14:30:55 +1100667 } else if (tmp_dst) {
Darren Tucker1b0dd172009-10-07 08:37:48 +1100668 abs_dst = path_append(tmp_dst, filename);
669 } else {
670 abs_dst = make_absolute(xstrdup(filename), pwd);
671 }
Darren Tuckera627d422013-06-02 07:31:17 +1000672 free(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +1100673
Damien Miller9303e652013-04-23 15:22:40 +1000674 if (!quiet)
675 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
Darren Tucker1b0dd172009-10-07 08:37:48 +1100676 if (pathname_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
677 if (upload_dir(conn, g.gl_pathv[i], abs_dst,
678 pflag || global_pflag, 1) == -1)
679 err = -1;
680 } else {
681 if (do_upload(conn, g.gl_pathv[i], abs_dst,
682 pflag || global_pflag) == -1)
683 err = -1;
684 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100685 }
686
687out:
Darren Tuckera627d422013-06-02 07:31:17 +1000688 free(abs_dst);
689 free(tmp_dst);
Damien Miller20e1fab2004-02-18 14:30:55 +1100690 globfree(&g);
691 return(err);
692}
693
694static int
695sdirent_comp(const void *aa, const void *bb)
696{
697 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
698 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000699 int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100700
Darren Tuckerb9123452004-06-22 13:06:45 +1000701#define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000702 if (sort_flag & LS_NAME_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000703 return (rmul * strcmp(a->filename, b->filename));
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000704 else if (sort_flag & LS_TIME_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000705 return (rmul * NCMP(a->a.mtime, b->a.mtime));
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000706 else if (sort_flag & LS_SIZE_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000707 return (rmul * NCMP(a->a.size, b->a.size));
708
709 fatal("Unknown ls sort type");
Damien Miller20e1fab2004-02-18 14:30:55 +1100710}
711
712/* sftp ls.1 replacement for directories */
713static int
714do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
715{
Damien Millereccb9de2005-06-17 12:59:34 +1000716 int n;
717 u_int c = 1, colspace = 0, columns = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100718 SFTP_DIRENT **d;
719
720 if ((n = do_readdir(conn, path, &d)) != 0)
721 return (n);
722
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000723 if (!(lflag & LS_SHORT_VIEW)) {
Damien Millereccb9de2005-06-17 12:59:34 +1000724 u_int m = 0, width = 80;
Damien Miller20e1fab2004-02-18 14:30:55 +1100725 struct winsize ws;
726 char *tmp;
727
728 /* Count entries for sort and find longest filename */
Darren Tucker9a526452004-06-22 13:09:55 +1000729 for (n = 0; d[n] != NULL; n++) {
730 if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
731 m = MAX(m, strlen(d[n]->filename));
732 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100733
734 /* Add any subpath that also needs to be counted */
735 tmp = path_strip(path, strip_path);
736 m += strlen(tmp);
Darren Tuckera627d422013-06-02 07:31:17 +1000737 free(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +1100738
739 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
740 width = ws.ws_col;
741
742 columns = width / (m + 2);
743 columns = MAX(columns, 1);
744 colspace = width / columns;
745 colspace = MIN(colspace, width);
746 }
747
Darren Tuckerb9123452004-06-22 13:06:45 +1000748 if (lflag & SORT_FLAGS) {
Damien Miller653b93b2005-11-05 15:15:23 +1100749 for (n = 0; d[n] != NULL; n++)
750 ; /* count entries */
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000751 sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
Darren Tuckerb9123452004-06-22 13:06:45 +1000752 qsort(d, n, sizeof(*d), sdirent_comp);
753 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100754
Darren Tuckercdf547a2004-05-24 10:12:19 +1000755 for (n = 0; d[n] != NULL && !interrupted; n++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100756 char *tmp, *fname;
757
Darren Tucker9a526452004-06-22 13:09:55 +1000758 if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
759 continue;
760
Damien Miller20e1fab2004-02-18 14:30:55 +1100761 tmp = path_append(path, d[n]->filename);
762 fname = path_strip(tmp, strip_path);
Darren Tuckera627d422013-06-02 07:31:17 +1000763 free(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +1100764
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000765 if (lflag & LS_LONG_VIEW) {
Darren Tucker2901e2d2010-01-13 22:44:06 +1100766 if (lflag & (LS_NUMERIC_VIEW|LS_SI_UNITS)) {
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000767 char *lname;
768 struct stat sb;
Damien Miller20e1fab2004-02-18 14:30:55 +1100769
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000770 memset(&sb, 0, sizeof(sb));
771 attrib_to_stat(&d[n]->a, &sb);
Darren Tucker2901e2d2010-01-13 22:44:06 +1100772 lname = ls_file(fname, &sb, 1,
773 (lflag & LS_SI_UNITS));
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000774 printf("%s\n", lname);
Darren Tuckera627d422013-06-02 07:31:17 +1000775 free(lname);
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000776 } else
777 printf("%s\n", d[n]->longname);
Damien Miller20e1fab2004-02-18 14:30:55 +1100778 } else {
779 printf("%-*s", colspace, fname);
780 if (c >= columns) {
781 printf("\n");
782 c = 1;
783 } else
784 c++;
785 }
786
Darren Tuckera627d422013-06-02 07:31:17 +1000787 free(fname);
Damien Miller20e1fab2004-02-18 14:30:55 +1100788 }
789
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000790 if (!(lflag & LS_LONG_VIEW) && (c != 1))
Damien Miller20e1fab2004-02-18 14:30:55 +1100791 printf("\n");
792
793 free_sftp_dirents(d);
794 return (0);
795}
796
797/* sftp ls.1 replacement which handles path globs */
798static int
799do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
800 int lflag)
801{
Damien Millera6e121a2010-10-07 21:39:17 +1100802 char *fname, *lname;
Damien Miller68e2e562010-10-07 21:39:55 +1100803 glob_t g;
804 int err;
805 struct winsize ws;
806 u_int i, c = 1, colspace = 0, columns = 1, m = 0, width = 80;
Damien Miller20e1fab2004-02-18 14:30:55 +1100807
808 memset(&g, 0, sizeof(g));
809
Damien Millera6e121a2010-10-07 21:39:17 +1100810 if (remote_glob(conn, path,
Damien Millerd7be70d2011-09-22 21:43:06 +1000811 GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE|GLOB_KEEPSTAT|GLOB_NOSORT,
812 NULL, &g) ||
Damien Millera6e121a2010-10-07 21:39:17 +1100813 (g.gl_pathc && !g.gl_matchc)) {
Darren Tucker596dcfa2004-12-11 13:37:22 +1100814 if (g.gl_pathc)
815 globfree(&g);
Damien Miller20e1fab2004-02-18 14:30:55 +1100816 error("Can't ls: \"%s\" not found", path);
Damien Millera6e121a2010-10-07 21:39:17 +1100817 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100818 }
819
Darren Tuckercdf547a2004-05-24 10:12:19 +1000820 if (interrupted)
821 goto out;
822
Damien Miller20e1fab2004-02-18 14:30:55 +1100823 /*
Darren Tucker596dcfa2004-12-11 13:37:22 +1100824 * If the glob returns a single match and it is a directory,
825 * then just list its contents.
Damien Miller20e1fab2004-02-18 14:30:55 +1100826 */
Damien Millera6e121a2010-10-07 21:39:17 +1100827 if (g.gl_matchc == 1 && g.gl_statv[0] != NULL &&
828 S_ISDIR(g.gl_statv[0]->st_mode)) {
829 err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
830 globfree(&g);
831 return err;
Damien Miller20e1fab2004-02-18 14:30:55 +1100832 }
833
Damien Miller68e2e562010-10-07 21:39:55 +1100834 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
835 width = ws.ws_col;
Damien Miller20e1fab2004-02-18 14:30:55 +1100836
Damien Miller68e2e562010-10-07 21:39:55 +1100837 if (!(lflag & LS_SHORT_VIEW)) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100838 /* Count entries for sort and find longest filename */
839 for (i = 0; g.gl_pathv[i]; i++)
840 m = MAX(m, strlen(g.gl_pathv[i]));
841
Damien Miller20e1fab2004-02-18 14:30:55 +1100842 columns = width / (m + 2);
843 columns = MAX(columns, 1);
844 colspace = width / columns;
845 }
846
Damien Millerea858292012-06-30 08:33:32 +1000847 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100848 fname = path_strip(g.gl_pathv[i], strip_path);
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000849 if (lflag & LS_LONG_VIEW) {
Damien Millera6e121a2010-10-07 21:39:17 +1100850 if (g.gl_statv[i] == NULL) {
851 error("no stat information for %s", fname);
852 continue;
853 }
854 lname = ls_file(fname, g.gl_statv[i], 1,
855 (lflag & LS_SI_UNITS));
Damien Miller20e1fab2004-02-18 14:30:55 +1100856 printf("%s\n", lname);
Darren Tuckera627d422013-06-02 07:31:17 +1000857 free(lname);
Damien Miller20e1fab2004-02-18 14:30:55 +1100858 } else {
859 printf("%-*s", colspace, fname);
860 if (c >= columns) {
861 printf("\n");
862 c = 1;
863 } else
864 c++;
865 }
Darren Tuckera627d422013-06-02 07:31:17 +1000866 free(fname);
Damien Miller20e1fab2004-02-18 14:30:55 +1100867 }
868
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000869 if (!(lflag & LS_LONG_VIEW) && (c != 1))
Damien Miller20e1fab2004-02-18 14:30:55 +1100870 printf("\n");
871
Darren Tuckercdf547a2004-05-24 10:12:19 +1000872 out:
Damien Miller20e1fab2004-02-18 14:30:55 +1100873 if (g.gl_pathc)
874 globfree(&g);
875
Damien Millera6e121a2010-10-07 21:39:17 +1100876 return 0;
Damien Miller20e1fab2004-02-18 14:30:55 +1100877}
878
Damien Millerd671e5a2008-05-19 14:53:33 +1000879static int
880do_df(struct sftp_conn *conn, char *path, int hflag, int iflag)
881{
Darren Tucker7b598892008-06-09 22:49:36 +1000882 struct sftp_statvfs st;
Damien Millerd671e5a2008-05-19 14:53:33 +1000883 char s_used[FMT_SCALED_STRSIZE];
884 char s_avail[FMT_SCALED_STRSIZE];
885 char s_root[FMT_SCALED_STRSIZE];
886 char s_total[FMT_SCALED_STRSIZE];
Darren Tuckerb5082e92010-01-08 18:51:47 +1100887 unsigned long long ffree;
Damien Millerd671e5a2008-05-19 14:53:33 +1000888
889 if (do_statvfs(conn, path, &st, 1) == -1)
890 return -1;
891 if (iflag) {
Darren Tuckerb5082e92010-01-08 18:51:47 +1100892 ffree = st.f_files ? (100 * (st.f_files - st.f_ffree) / st.f_files) : 0;
Damien Millerd671e5a2008-05-19 14:53:33 +1000893 printf(" Inodes Used Avail "
894 "(root) %%Capacity\n");
895 printf("%11llu %11llu %11llu %11llu %3llu%%\n",
896 (unsigned long long)st.f_files,
897 (unsigned long long)(st.f_files - st.f_ffree),
898 (unsigned long long)st.f_favail,
Darren Tuckerb5082e92010-01-08 18:51:47 +1100899 (unsigned long long)st.f_ffree, ffree);
Damien Millerd671e5a2008-05-19 14:53:33 +1000900 } else if (hflag) {
901 strlcpy(s_used, "error", sizeof(s_used));
902 strlcpy(s_avail, "error", sizeof(s_avail));
903 strlcpy(s_root, "error", sizeof(s_root));
904 strlcpy(s_total, "error", sizeof(s_total));
905 fmt_scaled((st.f_blocks - st.f_bfree) * st.f_frsize, s_used);
906 fmt_scaled(st.f_bavail * st.f_frsize, s_avail);
907 fmt_scaled(st.f_bfree * st.f_frsize, s_root);
908 fmt_scaled(st.f_blocks * st.f_frsize, s_total);
909 printf(" Size Used Avail (root) %%Capacity\n");
910 printf("%7sB %7sB %7sB %7sB %3llu%%\n",
911 s_total, s_used, s_avail, s_root,
912 (unsigned long long)(100 * (st.f_blocks - st.f_bfree) /
913 st.f_blocks));
914 } else {
915 printf(" Size Used Avail "
916 "(root) %%Capacity\n");
917 printf("%12llu %12llu %12llu %12llu %3llu%%\n",
918 (unsigned long long)(st.f_frsize * st.f_blocks / 1024),
919 (unsigned long long)(st.f_frsize *
920 (st.f_blocks - st.f_bfree) / 1024),
921 (unsigned long long)(st.f_frsize * st.f_bavail / 1024),
922 (unsigned long long)(st.f_frsize * st.f_bfree / 1024),
923 (unsigned long long)(100 * (st.f_blocks - st.f_bfree) /
924 st.f_blocks));
925 }
926 return 0;
927}
928
Damien Miller1cbc2922007-10-26 14:27:45 +1000929/*
930 * Undo escaping of glob sequences in place. Used to undo extra escaping
931 * applied in makeargv() when the string is destined for a function that
932 * does not glob it.
933 */
934static void
935undo_glob_escape(char *s)
936{
937 size_t i, j;
938
939 for (i = j = 0;;) {
940 if (s[i] == '\0') {
941 s[j] = '\0';
942 return;
943 }
944 if (s[i] != '\\') {
945 s[j++] = s[i++];
946 continue;
947 }
948 /* s[i] == '\\' */
949 ++i;
950 switch (s[i]) {
951 case '?':
952 case '[':
953 case '*':
954 case '\\':
955 s[j++] = s[i++];
956 break;
957 case '\0':
958 s[j++] = '\\';
959 s[j] = '\0';
960 return;
961 default:
962 s[j++] = '\\';
963 s[j++] = s[i++];
964 break;
965 }
966 }
967}
968
969/*
970 * Split a string into an argument vector using sh(1)-style quoting,
971 * comment and escaping rules, but with some tweaks to handle glob(3)
972 * wildcards.
Darren Tucker909d8582010-01-08 19:02:40 +1100973 * The "sloppy" flag allows for recovery from missing terminating quote, for
974 * use in parsing incomplete commandlines during tab autocompletion.
975 *
Damien Miller1cbc2922007-10-26 14:27:45 +1000976 * Returns NULL on error or a NULL-terminated array of arguments.
Darren Tucker909d8582010-01-08 19:02:40 +1100977 *
978 * If "lastquote" is not NULL, the quoting character used for the last
979 * argument is placed in *lastquote ("\0", "'" or "\"").
Damien Miller02e87802013-08-21 02:38:51 +1000980 *
Darren Tucker909d8582010-01-08 19:02:40 +1100981 * If "terminated" is not NULL, *terminated will be set to 1 when the
982 * last argument's quote has been properly terminated or 0 otherwise.
983 * This parameter is only of use if "sloppy" is set.
Damien Miller1cbc2922007-10-26 14:27:45 +1000984 */
985#define MAXARGS 128
986#define MAXARGLEN 8192
987static char **
Darren Tucker909d8582010-01-08 19:02:40 +1100988makeargv(const char *arg, int *argcp, int sloppy, char *lastquote,
989 u_int *terminated)
Damien Miller1cbc2922007-10-26 14:27:45 +1000990{
991 int argc, quot;
992 size_t i, j;
993 static char argvs[MAXARGLEN];
994 static char *argv[MAXARGS + 1];
995 enum { MA_START, MA_SQUOTE, MA_DQUOTE, MA_UNQUOTED } state, q;
996
997 *argcp = argc = 0;
998 if (strlen(arg) > sizeof(argvs) - 1) {
999 args_too_longs:
1000 error("string too long");
1001 return NULL;
1002 }
Darren Tucker909d8582010-01-08 19:02:40 +11001003 if (terminated != NULL)
1004 *terminated = 1;
1005 if (lastquote != NULL)
1006 *lastquote = '\0';
Damien Miller1cbc2922007-10-26 14:27:45 +10001007 state = MA_START;
1008 i = j = 0;
1009 for (;;) {
Damien Miller07daed52012-10-31 08:57:55 +11001010 if ((size_t)argc >= sizeof(argv) / sizeof(*argv)){
Darren Tucker063018d2012-10-05 10:43:58 +10001011 error("Too many arguments.");
1012 return NULL;
1013 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001014 if (isspace(arg[i])) {
1015 if (state == MA_UNQUOTED) {
1016 /* Terminate current argument */
1017 argvs[j++] = '\0';
1018 argc++;
1019 state = MA_START;
1020 } else if (state != MA_START)
1021 argvs[j++] = arg[i];
1022 } else if (arg[i] == '"' || arg[i] == '\'') {
1023 q = arg[i] == '"' ? MA_DQUOTE : MA_SQUOTE;
1024 if (state == MA_START) {
1025 argv[argc] = argvs + j;
1026 state = q;
Darren Tucker909d8582010-01-08 19:02:40 +11001027 if (lastquote != NULL)
1028 *lastquote = arg[i];
Damien Miller02e87802013-08-21 02:38:51 +10001029 } else if (state == MA_UNQUOTED)
Damien Miller1cbc2922007-10-26 14:27:45 +10001030 state = q;
1031 else if (state == q)
1032 state = MA_UNQUOTED;
1033 else
1034 argvs[j++] = arg[i];
1035 } else if (arg[i] == '\\') {
1036 if (state == MA_SQUOTE || state == MA_DQUOTE) {
1037 quot = state == MA_SQUOTE ? '\'' : '"';
1038 /* Unescape quote we are in */
1039 /* XXX support \n and friends? */
1040 if (arg[i + 1] == quot) {
1041 i++;
1042 argvs[j++] = arg[i];
1043 } else if (arg[i + 1] == '?' ||
1044 arg[i + 1] == '[' || arg[i + 1] == '*') {
1045 /*
1046 * Special case for sftp: append
1047 * double-escaped glob sequence -
1048 * glob will undo one level of
1049 * escaping. NB. string can grow here.
1050 */
1051 if (j >= sizeof(argvs) - 5)
1052 goto args_too_longs;
1053 argvs[j++] = '\\';
1054 argvs[j++] = arg[i++];
1055 argvs[j++] = '\\';
1056 argvs[j++] = arg[i];
1057 } else {
1058 argvs[j++] = arg[i++];
1059 argvs[j++] = arg[i];
1060 }
1061 } else {
1062 if (state == MA_START) {
1063 argv[argc] = argvs + j;
1064 state = MA_UNQUOTED;
Darren Tucker909d8582010-01-08 19:02:40 +11001065 if (lastquote != NULL)
1066 *lastquote = '\0';
Damien Miller1cbc2922007-10-26 14:27:45 +10001067 }
1068 if (arg[i + 1] == '?' || arg[i + 1] == '[' ||
1069 arg[i + 1] == '*' || arg[i + 1] == '\\') {
1070 /*
1071 * Special case for sftp: append
1072 * escaped glob sequence -
1073 * glob will undo one level of
1074 * escaping.
1075 */
1076 argvs[j++] = arg[i++];
1077 argvs[j++] = arg[i];
1078 } else {
1079 /* Unescape everything */
1080 /* XXX support \n and friends? */
1081 i++;
1082 argvs[j++] = arg[i];
1083 }
1084 }
1085 } else if (arg[i] == '#') {
1086 if (state == MA_SQUOTE || state == MA_DQUOTE)
1087 argvs[j++] = arg[i];
1088 else
1089 goto string_done;
1090 } else if (arg[i] == '\0') {
1091 if (state == MA_SQUOTE || state == MA_DQUOTE) {
Darren Tucker909d8582010-01-08 19:02:40 +11001092 if (sloppy) {
1093 state = MA_UNQUOTED;
1094 if (terminated != NULL)
1095 *terminated = 0;
1096 goto string_done;
1097 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001098 error("Unterminated quoted argument");
1099 return NULL;
1100 }
1101 string_done:
1102 if (state == MA_UNQUOTED) {
1103 argvs[j++] = '\0';
1104 argc++;
1105 }
1106 break;
1107 } else {
1108 if (state == MA_START) {
1109 argv[argc] = argvs + j;
1110 state = MA_UNQUOTED;
Darren Tucker909d8582010-01-08 19:02:40 +11001111 if (lastquote != NULL)
1112 *lastquote = '\0';
Damien Miller1cbc2922007-10-26 14:27:45 +10001113 }
1114 if ((state == MA_SQUOTE || state == MA_DQUOTE) &&
1115 (arg[i] == '?' || arg[i] == '[' || arg[i] == '*')) {
1116 /*
1117 * Special case for sftp: escape quoted
1118 * glob(3) wildcards. NB. string can grow
1119 * here.
1120 */
1121 if (j >= sizeof(argvs) - 3)
1122 goto args_too_longs;
1123 argvs[j++] = '\\';
1124 argvs[j++] = arg[i];
1125 } else
1126 argvs[j++] = arg[i];
1127 }
1128 i++;
1129 }
1130 *argcp = argc;
1131 return argv;
1132}
1133
Damien Miller20e1fab2004-02-18 14:30:55 +11001134static int
Damien Miller0d032412013-07-25 11:56:52 +10001135parse_args(const char **cpp, int *aflag, int *hflag, int *iflag, int *lflag,
1136 int *pflag, int *rflag, int *sflag, unsigned long *n_arg,
1137 char **path1, char **path2)
Damien Miller20e1fab2004-02-18 14:30:55 +11001138{
1139 const char *cmd, *cp = *cpp;
Damien Miller1cbc2922007-10-26 14:27:45 +10001140 char *cp2, **argv;
Damien Miller20e1fab2004-02-18 14:30:55 +11001141 int base = 0;
1142 long l;
Damien Miller1cbc2922007-10-26 14:27:45 +10001143 int i, cmdnum, optidx, argc;
Damien Miller20e1fab2004-02-18 14:30:55 +11001144
1145 /* Skip leading whitespace */
1146 cp = cp + strspn(cp, WHITESPACE);
1147
Damien Miller20e1fab2004-02-18 14:30:55 +11001148 /* Check for leading '-' (disable error processing) */
1149 *iflag = 0;
1150 if (*cp == '-') {
1151 *iflag = 1;
1152 cp++;
Darren Tucker70cc0922010-01-09 22:28:03 +11001153 cp = cp + strspn(cp, WHITESPACE);
Damien Miller20e1fab2004-02-18 14:30:55 +11001154 }
1155
Darren Tucker70cc0922010-01-09 22:28:03 +11001156 /* Ignore blank lines and lines which begin with comment '#' char */
1157 if (*cp == '\0' || *cp == '#')
1158 return (0);
1159
Darren Tucker909d8582010-01-08 19:02:40 +11001160 if ((argv = makeargv(cp, &argc, 0, NULL, NULL)) == NULL)
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 /* Figure out which command we have */
1164 for (i = 0; cmds[i].c != NULL; i++) {
Damien Millerd6d9fa02013-02-12 11:02:46 +11001165 if (argv[0] != NULL && strcasecmp(cmds[i].c, argv[0]) == 0)
Damien Miller20e1fab2004-02-18 14:30:55 +11001166 break;
Damien Miller20e1fab2004-02-18 14:30:55 +11001167 }
1168 cmdnum = cmds[i].n;
1169 cmd = cmds[i].c;
1170
1171 /* Special case */
1172 if (*cp == '!') {
1173 cp++;
1174 cmdnum = I_SHELL;
1175 } else if (cmdnum == -1) {
1176 error("Invalid command.");
Damien Miller1cbc2922007-10-26 14:27:45 +10001177 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001178 }
1179
1180 /* Get arguments and parse flags */
Damien Miller0d032412013-07-25 11:56:52 +10001181 *aflag = *lflag = *pflag = *rflag = *hflag = *n_arg = 0;
Damien Miller20e1fab2004-02-18 14:30:55 +11001182 *path1 = *path2 = NULL;
Damien Miller1cbc2922007-10-26 14:27:45 +10001183 optidx = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001184 switch (cmdnum) {
1185 case I_GET:
Damien Miller0d032412013-07-25 11:56:52 +10001186 case I_REGET:
Damien Miller20e1fab2004-02-18 14:30:55 +11001187 case I_PUT:
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001188 if ((optidx = parse_getput_flags(cmd, argv, argc,
Damien Miller0d032412013-07-25 11:56:52 +10001189 aflag, pflag, rflag)) == -1)
Damien Miller1cbc2922007-10-26 14:27:45 +10001190 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001191 /* Get first pathname (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001192 if (argc - optidx < 1) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001193 error("You must specify at least one path after a "
1194 "%s command.", cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001195 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001196 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001197 *path1 = xstrdup(argv[optidx]);
1198 /* Get second pathname (optional) */
1199 if (argc - optidx > 1) {
1200 *path2 = xstrdup(argv[optidx + 1]);
1201 /* Destination is not globbed */
1202 undo_glob_escape(*path2);
1203 }
Damien Miller0d032412013-07-25 11:56:52 +10001204 if (*aflag && cmdnum == I_PUT) {
1205 /* XXX implement resume for uploads */
1206 error("Resume is not supported for uploads");
1207 return -1;
1208 }
Damien Miller20e1fab2004-02-18 14:30:55 +11001209 break;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001210 case I_LINK:
1211 if ((optidx = parse_link_flags(cmd, argv, argc, sflag)) == -1)
1212 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001213 case I_SYMLINK:
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001214 case I_RENAME:
Damien Miller1cbc2922007-10-26 14:27:45 +10001215 if (argc - optidx < 2) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001216 error("You must specify two paths after a %s "
1217 "command.", 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]);
1221 *path2 = xstrdup(argv[optidx + 1]);
1222 /* Paths are not globbed */
1223 undo_glob_escape(*path1);
1224 undo_glob_escape(*path2);
Damien Miller20e1fab2004-02-18 14:30:55 +11001225 break;
1226 case I_RM:
1227 case I_MKDIR:
1228 case I_RMDIR:
1229 case I_CHDIR:
1230 case I_LCHDIR:
1231 case I_LMKDIR:
1232 /* Get pathname (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001233 if (argc - optidx < 1) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001234 error("You must specify a path after a %s command.",
1235 cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001236 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001237 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001238 *path1 = xstrdup(argv[optidx]);
1239 /* Only "rm" globs */
1240 if (cmdnum != I_RM)
1241 undo_glob_escape(*path1);
Damien Miller20e1fab2004-02-18 14:30:55 +11001242 break;
Damien Millerd671e5a2008-05-19 14:53:33 +10001243 case I_DF:
1244 if ((optidx = parse_df_flags(cmd, argv, argc, hflag,
1245 iflag)) == -1)
1246 return -1;
1247 /* Default to current directory if no path specified */
1248 if (argc - optidx < 1)
1249 *path1 = NULL;
1250 else {
1251 *path1 = xstrdup(argv[optidx]);
1252 undo_glob_escape(*path1);
1253 }
1254 break;
Damien Miller20e1fab2004-02-18 14:30:55 +11001255 case I_LS:
Damien Miller1cbc2922007-10-26 14:27:45 +10001256 if ((optidx = parse_ls_flags(argv, argc, lflag)) == -1)
Damien Miller20e1fab2004-02-18 14:30:55 +11001257 return(-1);
1258 /* Path is optional */
Damien Miller1cbc2922007-10-26 14:27:45 +10001259 if (argc - optidx > 0)
1260 *path1 = xstrdup(argv[optidx]);
Damien Miller20e1fab2004-02-18 14:30:55 +11001261 break;
1262 case I_LLS:
Darren Tucker88b976f2007-12-29 02:40:43 +11001263 /* Skip ls command and following whitespace */
1264 cp = cp + strlen(cmd) + strspn(cp, WHITESPACE);
Damien Miller20e1fab2004-02-18 14:30:55 +11001265 case I_SHELL:
1266 /* Uses the rest of the line */
1267 break;
1268 case I_LUMASK:
Damien Miller20e1fab2004-02-18 14:30:55 +11001269 case I_CHMOD:
1270 base = 8;
1271 case I_CHOWN:
1272 case I_CHGRP:
1273 /* Get numeric arg (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001274 if (argc - optidx < 1)
1275 goto need_num_arg;
Damien Millere7658a52006-10-24 03:00:12 +10001276 errno = 0;
Damien Miller1cbc2922007-10-26 14:27:45 +10001277 l = strtol(argv[optidx], &cp2, base);
1278 if (cp2 == argv[optidx] || *cp2 != '\0' ||
1279 ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
1280 l < 0) {
1281 need_num_arg:
Damien Miller20e1fab2004-02-18 14:30:55 +11001282 error("You must supply a numeric argument "
1283 "to the %s command.", cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001284 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001285 }
Damien Miller20e1fab2004-02-18 14:30:55 +11001286 *n_arg = l;
Damien Miller1cbc2922007-10-26 14:27:45 +10001287 if (cmdnum == I_LUMASK)
Damien Miller20e1fab2004-02-18 14:30:55 +11001288 break;
Damien Miller20e1fab2004-02-18 14:30:55 +11001289 /* Get pathname (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001290 if (argc - optidx < 2) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001291 error("You must specify a path after a %s command.",
1292 cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001293 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001294 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001295 *path1 = xstrdup(argv[optidx + 1]);
Damien Miller20e1fab2004-02-18 14:30:55 +11001296 break;
1297 case I_QUIT:
1298 case I_PWD:
1299 case I_LPWD:
1300 case I_HELP:
1301 case I_VERSION:
1302 case I_PROGRESS:
1303 break;
1304 default:
1305 fatal("Command not implemented");
1306 }
1307
1308 *cpp = cp;
1309 return(cmdnum);
1310}
1311
1312static int
1313parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
1314 int err_abort)
1315{
1316 char *path1, *path2, *tmp;
Damien Miller0d032412013-07-25 11:56:52 +10001317 int aflag = 0, hflag = 0, iflag = 0, lflag = 0, pflag = 0;
1318 int rflag = 0, sflag = 0;
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001319 int cmdnum, i;
Damien Millerfdd66fc2009-02-14 16:26:19 +11001320 unsigned long n_arg = 0;
Damien Miller20e1fab2004-02-18 14:30:55 +11001321 Attrib a, *aa;
1322 char path_buf[MAXPATHLEN];
1323 int err = 0;
1324 glob_t g;
1325
1326 path1 = path2 = NULL;
Damien Miller0d032412013-07-25 11:56:52 +10001327 cmdnum = parse_args(&cmd, &aflag, &hflag, &iflag, &lflag, &pflag,
1328 &rflag, &sflag, &n_arg, &path1, &path2);
Damien Miller20e1fab2004-02-18 14:30:55 +11001329 if (iflag != 0)
1330 err_abort = 0;
1331
1332 memset(&g, 0, sizeof(g));
1333
1334 /* Perform command */
1335 switch (cmdnum) {
1336 case 0:
1337 /* Blank line */
1338 break;
1339 case -1:
1340 /* Unrecognized command */
1341 err = -1;
1342 break;
Damien Miller0d032412013-07-25 11:56:52 +10001343 case I_REGET:
1344 aflag = 1;
1345 /* FALLTHROUGH */
Damien Miller20e1fab2004-02-18 14:30:55 +11001346 case I_GET:
Damien Miller0d032412013-07-25 11:56:52 +10001347 err = process_get(conn, path1, path2, *pwd, pflag,
1348 rflag, aflag);
Damien Miller20e1fab2004-02-18 14:30:55 +11001349 break;
1350 case I_PUT:
Darren Tucker1b0dd172009-10-07 08:37:48 +11001351 err = process_put(conn, path1, path2, *pwd, pflag, rflag);
Damien Miller20e1fab2004-02-18 14:30:55 +11001352 break;
1353 case I_RENAME:
1354 path1 = make_absolute(path1, *pwd);
1355 path2 = make_absolute(path2, *pwd);
1356 err = do_rename(conn, path1, path2);
1357 break;
1358 case I_SYMLINK:
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001359 sflag = 1;
1360 case I_LINK:
1361 path1 = make_absolute(path1, *pwd);
Damien Miller20e1fab2004-02-18 14:30:55 +11001362 path2 = make_absolute(path2, *pwd);
Darren Tuckeraf1f9092010-12-05 09:02:47 +11001363 err = (sflag ? do_symlink : do_hardlink)(conn, path1, path2);
Damien Miller20e1fab2004-02-18 14:30:55 +11001364 break;
1365 case I_RM:
1366 path1 = make_absolute(path1, *pwd);
1367 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001368 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller9303e652013-04-23 15:22:40 +10001369 if (!quiet)
1370 printf("Removing %s\n", g.gl_pathv[i]);
Damien Miller20e1fab2004-02-18 14:30:55 +11001371 err = do_rm(conn, g.gl_pathv[i]);
1372 if (err != 0 && err_abort)
1373 break;
1374 }
1375 break;
1376 case I_MKDIR:
1377 path1 = make_absolute(path1, *pwd);
1378 attrib_clear(&a);
1379 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1380 a.perm = 0777;
Darren Tucker1b0dd172009-10-07 08:37:48 +11001381 err = do_mkdir(conn, path1, &a, 1);
Damien Miller20e1fab2004-02-18 14:30:55 +11001382 break;
1383 case I_RMDIR:
1384 path1 = make_absolute(path1, *pwd);
1385 err = do_rmdir(conn, path1);
1386 break;
1387 case I_CHDIR:
1388 path1 = make_absolute(path1, *pwd);
1389 if ((tmp = do_realpath(conn, path1)) == NULL) {
1390 err = 1;
1391 break;
1392 }
1393 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +10001394 free(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +11001395 err = 1;
1396 break;
1397 }
1398 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
1399 error("Can't change directory: Can't check target");
Darren Tuckera627d422013-06-02 07:31:17 +10001400 free(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +11001401 err = 1;
1402 break;
1403 }
1404 if (!S_ISDIR(aa->perm)) {
1405 error("Can't change directory: \"%s\" is not "
1406 "a directory", tmp);
Darren Tuckera627d422013-06-02 07:31:17 +10001407 free(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +11001408 err = 1;
1409 break;
1410 }
Darren Tuckera627d422013-06-02 07:31:17 +10001411 free(*pwd);
Damien Miller20e1fab2004-02-18 14:30:55 +11001412 *pwd = tmp;
1413 break;
1414 case I_LS:
1415 if (!path1) {
Damien Miller99ac4e92010-06-26 09:36:58 +10001416 do_ls_dir(conn, *pwd, *pwd, lflag);
Damien Miller20e1fab2004-02-18 14:30:55 +11001417 break;
1418 }
1419
1420 /* Strip pwd off beginning of non-absolute paths */
1421 tmp = NULL;
1422 if (*path1 != '/')
1423 tmp = *pwd;
1424
1425 path1 = make_absolute(path1, *pwd);
1426 err = do_globbed_ls(conn, path1, tmp, lflag);
1427 break;
Damien Millerd671e5a2008-05-19 14:53:33 +10001428 case I_DF:
1429 /* Default to current directory if no path specified */
1430 if (path1 == NULL)
1431 path1 = xstrdup(*pwd);
1432 path1 = make_absolute(path1, *pwd);
1433 err = do_df(conn, path1, hflag, iflag);
1434 break;
Damien Miller20e1fab2004-02-18 14:30:55 +11001435 case I_LCHDIR:
1436 if (chdir(path1) == -1) {
1437 error("Couldn't change local directory to "
1438 "\"%s\": %s", path1, strerror(errno));
1439 err = 1;
1440 }
1441 break;
1442 case I_LMKDIR:
1443 if (mkdir(path1, 0777) == -1) {
1444 error("Couldn't create local directory "
1445 "\"%s\": %s", path1, strerror(errno));
1446 err = 1;
1447 }
1448 break;
1449 case I_LLS:
1450 local_do_ls(cmd);
1451 break;
1452 case I_SHELL:
1453 local_do_shell(cmd);
1454 break;
1455 case I_LUMASK:
1456 umask(n_arg);
1457 printf("Local umask: %03lo\n", n_arg);
1458 break;
1459 case I_CHMOD:
1460 path1 = make_absolute(path1, *pwd);
1461 attrib_clear(&a);
1462 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1463 a.perm = n_arg;
1464 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001465 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller9303e652013-04-23 15:22:40 +10001466 if (!quiet)
1467 printf("Changing mode on %s\n", g.gl_pathv[i]);
Damien Miller20e1fab2004-02-18 14:30:55 +11001468 err = do_setstat(conn, g.gl_pathv[i], &a);
1469 if (err != 0 && err_abort)
1470 break;
1471 }
1472 break;
1473 case I_CHOWN:
1474 case I_CHGRP:
1475 path1 = make_absolute(path1, *pwd);
1476 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001477 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001478 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
Damien Miller1be2cc42008-12-09 14:11:49 +11001479 if (err_abort) {
1480 err = -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001481 break;
Damien Miller1be2cc42008-12-09 14:11:49 +11001482 } else
Damien Miller20e1fab2004-02-18 14:30:55 +11001483 continue;
1484 }
1485 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1486 error("Can't get current ownership of "
1487 "remote file \"%s\"", g.gl_pathv[i]);
Damien Miller1be2cc42008-12-09 14:11:49 +11001488 if (err_abort) {
1489 err = -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001490 break;
Damien Miller1be2cc42008-12-09 14:11:49 +11001491 } else
Damien Miller20e1fab2004-02-18 14:30:55 +11001492 continue;
1493 }
1494 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1495 if (cmdnum == I_CHOWN) {
Damien Miller9303e652013-04-23 15:22:40 +10001496 if (!quiet)
1497 printf("Changing owner on %s\n",
1498 g.gl_pathv[i]);
Damien Miller20e1fab2004-02-18 14:30:55 +11001499 aa->uid = n_arg;
1500 } else {
Damien Miller9303e652013-04-23 15:22:40 +10001501 if (!quiet)
1502 printf("Changing group on %s\n",
1503 g.gl_pathv[i]);
Damien Miller20e1fab2004-02-18 14:30:55 +11001504 aa->gid = n_arg;
1505 }
1506 err = do_setstat(conn, g.gl_pathv[i], aa);
1507 if (err != 0 && err_abort)
1508 break;
1509 }
1510 break;
1511 case I_PWD:
1512 printf("Remote working directory: %s\n", *pwd);
1513 break;
1514 case I_LPWD:
1515 if (!getcwd(path_buf, sizeof(path_buf))) {
1516 error("Couldn't get local cwd: %s", strerror(errno));
1517 err = -1;
1518 break;
1519 }
1520 printf("Local working directory: %s\n", path_buf);
1521 break;
1522 case I_QUIT:
1523 /* Processed below */
1524 break;
1525 case I_HELP:
1526 help();
1527 break;
1528 case I_VERSION:
1529 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1530 break;
1531 case I_PROGRESS:
1532 showprogress = !showprogress;
1533 if (showprogress)
1534 printf("Progress meter enabled\n");
1535 else
1536 printf("Progress meter disabled\n");
1537 break;
1538 default:
1539 fatal("%d is not implemented", cmdnum);
1540 }
1541
1542 if (g.gl_pathc)
1543 globfree(&g);
Darren Tuckera627d422013-06-02 07:31:17 +10001544 free(path1);
1545 free(path2);
Damien Miller20e1fab2004-02-18 14:30:55 +11001546
1547 /* If an unignored error occurs in batch mode we should abort. */
1548 if (err_abort && err != 0)
1549 return (-1);
1550 else if (cmdnum == I_QUIT)
1551 return (1);
1552
1553 return (0);
1554}
1555
Darren Tucker2d963d82004-11-07 20:04:10 +11001556#ifdef USE_LIBEDIT
1557static char *
1558prompt(EditLine *el)
1559{
1560 return ("sftp> ");
1561}
Darren Tucker2d963d82004-11-07 20:04:10 +11001562
Darren Tucker909d8582010-01-08 19:02:40 +11001563/* Display entries in 'list' after skipping the first 'len' chars */
1564static void
1565complete_display(char **list, u_int len)
1566{
1567 u_int y, m = 0, width = 80, columns = 1, colspace = 0, llen;
1568 struct winsize ws;
1569 char *tmp;
1570
1571 /* Count entries for sort and find longest */
Damien Miller02e87802013-08-21 02:38:51 +10001572 for (y = 0; list[y]; y++)
Darren Tucker909d8582010-01-08 19:02:40 +11001573 m = MAX(m, strlen(list[y]));
1574
1575 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
1576 width = ws.ws_col;
1577
1578 m = m > len ? m - len : 0;
1579 columns = width / (m + 2);
1580 columns = MAX(columns, 1);
1581 colspace = width / columns;
1582 colspace = MIN(colspace, width);
1583
1584 printf("\n");
1585 m = 1;
1586 for (y = 0; list[y]; y++) {
1587 llen = strlen(list[y]);
1588 tmp = llen > len ? list[y] + len : "";
1589 printf("%-*s", colspace, tmp);
1590 if (m >= columns) {
1591 printf("\n");
1592 m = 1;
1593 } else
1594 m++;
1595 }
1596 printf("\n");
1597}
1598
1599/*
1600 * Given a "list" of words that begin with a common prefix of "word",
1601 * attempt to find an autocompletion to extends "word" by the next
1602 * characters common to all entries in "list".
1603 */
1604static char *
1605complete_ambiguous(const char *word, char **list, size_t count)
1606{
1607 if (word == NULL)
1608 return NULL;
1609
1610 if (count > 0) {
1611 u_int y, matchlen = strlen(list[0]);
1612
1613 /* Find length of common stem */
1614 for (y = 1; list[y]; y++) {
1615 u_int x;
1616
Damien Miller02e87802013-08-21 02:38:51 +10001617 for (x = 0; x < matchlen; x++)
1618 if (list[0][x] != list[y][x])
Darren Tucker909d8582010-01-08 19:02:40 +11001619 break;
1620
1621 matchlen = x;
1622 }
1623
1624 if (matchlen > strlen(word)) {
1625 char *tmp = xstrdup(list[0]);
1626
Darren Tucker340d1682010-01-09 08:54:31 +11001627 tmp[matchlen] = '\0';
Darren Tucker909d8582010-01-08 19:02:40 +11001628 return tmp;
1629 }
Damien Miller02e87802013-08-21 02:38:51 +10001630 }
Darren Tucker909d8582010-01-08 19:02:40 +11001631
1632 return xstrdup(word);
1633}
1634
1635/* Autocomplete a sftp command */
1636static int
1637complete_cmd_parse(EditLine *el, char *cmd, int lastarg, char quote,
1638 int terminated)
1639{
1640 u_int y, count = 0, cmdlen, tmplen;
1641 char *tmp, **list, argterm[3];
1642 const LineInfo *lf;
1643
1644 list = xcalloc((sizeof(cmds) / sizeof(*cmds)) + 1, sizeof(char *));
1645
1646 /* No command specified: display all available commands */
1647 if (cmd == NULL) {
1648 for (y = 0; cmds[y].c; y++)
1649 list[count++] = xstrdup(cmds[y].c);
Damien Miller02e87802013-08-21 02:38:51 +10001650
Darren Tucker909d8582010-01-08 19:02:40 +11001651 list[count] = NULL;
1652 complete_display(list, 0);
1653
Damien Miller02e87802013-08-21 02:38:51 +10001654 for (y = 0; list[y] != NULL; y++)
1655 free(list[y]);
Darren Tuckera627d422013-06-02 07:31:17 +10001656 free(list);
Darren Tucker909d8582010-01-08 19:02:40 +11001657 return count;
1658 }
1659
1660 /* Prepare subset of commands that start with "cmd" */
1661 cmdlen = strlen(cmd);
1662 for (y = 0; cmds[y].c; y++) {
Damien Miller02e87802013-08-21 02:38:51 +10001663 if (!strncasecmp(cmd, cmds[y].c, cmdlen))
Darren Tucker909d8582010-01-08 19:02:40 +11001664 list[count++] = xstrdup(cmds[y].c);
1665 }
1666 list[count] = NULL;
1667
Damien Miller47d81152011-11-25 13:53:48 +11001668 if (count == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001669 free(list);
Darren Tucker909d8582010-01-08 19:02:40 +11001670 return 0;
Damien Miller47d81152011-11-25 13:53:48 +11001671 }
Darren Tucker909d8582010-01-08 19:02:40 +11001672
1673 /* Complete ambigious command */
1674 tmp = complete_ambiguous(cmd, list, count);
1675 if (count > 1)
1676 complete_display(list, 0);
1677
Damien Miller02e87802013-08-21 02:38:51 +10001678 for (y = 0; list[y]; y++)
1679 free(list[y]);
Darren Tuckera627d422013-06-02 07:31:17 +10001680 free(list);
Darren Tucker909d8582010-01-08 19:02:40 +11001681
1682 if (tmp != NULL) {
1683 tmplen = strlen(tmp);
1684 cmdlen = strlen(cmd);
1685 /* If cmd may be extended then do so */
1686 if (tmplen > cmdlen)
1687 if (el_insertstr(el, tmp + cmdlen) == -1)
1688 fatal("el_insertstr failed.");
1689 lf = el_line(el);
1690 /* Terminate argument cleanly */
1691 if (count == 1) {
1692 y = 0;
1693 if (!terminated)
1694 argterm[y++] = quote;
1695 if (lastarg || *(lf->cursor) != ' ')
1696 argterm[y++] = ' ';
1697 argterm[y] = '\0';
1698 if (y > 0 && el_insertstr(el, argterm) == -1)
1699 fatal("el_insertstr failed.");
1700 }
Darren Tuckera627d422013-06-02 07:31:17 +10001701 free(tmp);
Darren Tucker909d8582010-01-08 19:02:40 +11001702 }
1703
1704 return count;
1705}
1706
1707/*
1708 * Determine whether a particular sftp command's arguments (if any)
1709 * represent local or remote files.
1710 */
1711static int
1712complete_is_remote(char *cmd) {
1713 int i;
1714
1715 if (cmd == NULL)
1716 return -1;
1717
1718 for (i = 0; cmds[i].c; i++) {
Damien Miller02e87802013-08-21 02:38:51 +10001719 if (!strncasecmp(cmd, cmds[i].c, strlen(cmds[i].c)))
Darren Tucker909d8582010-01-08 19:02:40 +11001720 return cmds[i].t;
1721 }
1722
1723 return -1;
1724}
1725
1726/* Autocomplete a filename "file" */
1727static int
1728complete_match(EditLine *el, struct sftp_conn *conn, char *remote_path,
1729 char *file, int remote, int lastarg, char quote, int terminated)
1730{
1731 glob_t g;
Darren Tuckerea647212013-06-06 08:19:09 +10001732 char *tmp, *tmp2, ins[8];
Darren Tucker17146d32012-10-05 10:46:16 +10001733 u_int i, hadglob, pwdlen, len, tmplen, filelen, cesc, isesc, isabs;
Darren Tuckerea647212013-06-06 08:19:09 +10001734 int clen;
Darren Tucker909d8582010-01-08 19:02:40 +11001735 const LineInfo *lf;
Damien Miller02e87802013-08-21 02:38:51 +10001736
Darren Tucker909d8582010-01-08 19:02:40 +11001737 /* Glob from "file" location */
1738 if (file == NULL)
1739 tmp = xstrdup("*");
1740 else
1741 xasprintf(&tmp, "%s*", file);
1742
Darren Tucker191fcc62012-10-05 10:45:01 +10001743 /* Check if the path is absolute. */
1744 isabs = tmp[0] == '/';
1745
Darren Tucker909d8582010-01-08 19:02:40 +11001746 memset(&g, 0, sizeof(g));
1747 if (remote != LOCAL) {
1748 tmp = make_absolute(tmp, remote_path);
1749 remote_glob(conn, tmp, GLOB_DOOFFS|GLOB_MARK, NULL, &g);
Damien Miller02e87802013-08-21 02:38:51 +10001750 } else
Darren Tucker909d8582010-01-08 19:02:40 +11001751 glob(tmp, GLOB_DOOFFS|GLOB_MARK, NULL, &g);
Damien Miller02e87802013-08-21 02:38:51 +10001752
Darren Tucker909d8582010-01-08 19:02:40 +11001753 /* Determine length of pwd so we can trim completion display */
1754 for (hadglob = tmplen = pwdlen = 0; tmp[tmplen] != 0; tmplen++) {
1755 /* Terminate counting on first unescaped glob metacharacter */
1756 if (tmp[tmplen] == '*' || tmp[tmplen] == '?') {
1757 if (tmp[tmplen] != '*' || tmp[tmplen + 1] != '\0')
1758 hadglob = 1;
1759 break;
1760 }
1761 if (tmp[tmplen] == '\\' && tmp[tmplen + 1] != '\0')
1762 tmplen++;
1763 if (tmp[tmplen] == '/')
1764 pwdlen = tmplen + 1; /* track last seen '/' */
1765 }
Darren Tuckera627d422013-06-02 07:31:17 +10001766 free(tmp);
Darren Tucker909d8582010-01-08 19:02:40 +11001767
Damien Miller02e87802013-08-21 02:38:51 +10001768 if (g.gl_matchc == 0)
Darren Tucker909d8582010-01-08 19:02:40 +11001769 goto out;
1770
1771 if (g.gl_matchc > 1)
1772 complete_display(g.gl_pathv, pwdlen);
1773
1774 tmp = NULL;
1775 /* Don't try to extend globs */
1776 if (file == NULL || hadglob)
1777 goto out;
1778
1779 tmp2 = complete_ambiguous(file, g.gl_pathv, g.gl_matchc);
Darren Tucker191fcc62012-10-05 10:45:01 +10001780 tmp = path_strip(tmp2, isabs ? NULL : remote_path);
Darren Tuckera627d422013-06-02 07:31:17 +10001781 free(tmp2);
Darren Tucker909d8582010-01-08 19:02:40 +11001782
1783 if (tmp == NULL)
1784 goto out;
1785
1786 tmplen = strlen(tmp);
1787 filelen = strlen(file);
1788
Darren Tucker17146d32012-10-05 10:46:16 +10001789 /* Count the number of escaped characters in the input string. */
1790 cesc = isesc = 0;
1791 for (i = 0; i < filelen; i++) {
1792 if (!isesc && file[i] == '\\' && i + 1 < filelen){
1793 isesc = 1;
1794 cesc++;
1795 } else
1796 isesc = 0;
1797 }
1798
1799 if (tmplen > (filelen - cesc)) {
1800 tmp2 = tmp + filelen - cesc;
Damien Miller02e87802013-08-21 02:38:51 +10001801 len = strlen(tmp2);
Darren Tucker909d8582010-01-08 19:02:40 +11001802 /* quote argument on way out */
Darren Tuckerea647212013-06-06 08:19:09 +10001803 for (i = 0; i < len; i += clen) {
1804 if ((clen = mblen(tmp2 + i, len - i)) < 0 ||
1805 (size_t)clen > sizeof(ins) - 2)
1806 fatal("invalid multibyte character");
Darren Tucker909d8582010-01-08 19:02:40 +11001807 ins[0] = '\\';
Darren Tuckerea647212013-06-06 08:19:09 +10001808 memcpy(ins + 1, tmp2 + i, clen);
1809 ins[clen + 1] = '\0';
Darren Tucker909d8582010-01-08 19:02:40 +11001810 switch (tmp2[i]) {
1811 case '\'':
1812 case '"':
1813 case '\\':
1814 case '\t':
Darren Tuckerd78739a2010-10-24 10:56:32 +11001815 case '[':
Darren Tucker909d8582010-01-08 19:02:40 +11001816 case ' ':
Darren Tucker17146d32012-10-05 10:46:16 +10001817 case '#':
1818 case '*':
Darren Tucker909d8582010-01-08 19:02:40 +11001819 if (quote == '\0' || tmp2[i] == quote) {
1820 if (el_insertstr(el, ins) == -1)
1821 fatal("el_insertstr "
1822 "failed.");
1823 break;
1824 }
1825 /* FALLTHROUGH */
1826 default:
1827 if (el_insertstr(el, ins + 1) == -1)
1828 fatal("el_insertstr failed.");
1829 break;
1830 }
1831 }
1832 }
1833
1834 lf = el_line(el);
Darren Tucker909d8582010-01-08 19:02:40 +11001835 if (g.gl_matchc == 1) {
1836 i = 0;
1837 if (!terminated)
1838 ins[i++] = quote;
Darren Tucker9c3ba072010-01-13 22:45:03 +11001839 if (*(lf->cursor - 1) != '/' &&
1840 (lastarg || *(lf->cursor) != ' '))
Darren Tucker909d8582010-01-08 19:02:40 +11001841 ins[i++] = ' ';
1842 ins[i] = '\0';
1843 if (i > 0 && el_insertstr(el, ins) == -1)
1844 fatal("el_insertstr failed.");
1845 }
Darren Tuckera627d422013-06-02 07:31:17 +10001846 free(tmp);
Darren Tucker909d8582010-01-08 19:02:40 +11001847
1848 out:
1849 globfree(&g);
1850 return g.gl_matchc;
1851}
1852
1853/* tab-completion hook function, called via libedit */
1854static unsigned char
1855complete(EditLine *el, int ch)
1856{
Damien Miller02e87802013-08-21 02:38:51 +10001857 char **argv, *line, quote;
Damien Miller746d1a62013-07-18 16:13:02 +10001858 int argc, carg;
1859 u_int cursor, len, terminated, ret = CC_ERROR;
Darren Tucker909d8582010-01-08 19:02:40 +11001860 const LineInfo *lf;
1861 struct complete_ctx *complete_ctx;
1862
1863 lf = el_line(el);
1864 if (el_get(el, EL_CLIENTDATA, (void**)&complete_ctx) != 0)
1865 fatal("%s: el_get failed", __func__);
1866
1867 /* Figure out which argument the cursor points to */
1868 cursor = lf->cursor - lf->buffer;
1869 line = (char *)xmalloc(cursor + 1);
1870 memcpy(line, lf->buffer, cursor);
1871 line[cursor] = '\0';
1872 argv = makeargv(line, &carg, 1, &quote, &terminated);
Darren Tuckera627d422013-06-02 07:31:17 +10001873 free(line);
Darren Tucker909d8582010-01-08 19:02:40 +11001874
1875 /* Get all the arguments on the line */
1876 len = lf->lastchar - lf->buffer;
1877 line = (char *)xmalloc(len + 1);
1878 memcpy(line, lf->buffer, len);
1879 line[len] = '\0';
1880 argv = makeargv(line, &argc, 1, NULL, NULL);
1881
1882 /* Ensure cursor is at EOL or a argument boundary */
1883 if (line[cursor] != ' ' && line[cursor] != '\0' &&
1884 line[cursor] != '\n') {
Darren Tuckera627d422013-06-02 07:31:17 +10001885 free(line);
Darren Tucker909d8582010-01-08 19:02:40 +11001886 return ret;
1887 }
1888
1889 if (carg == 0) {
1890 /* Show all available commands */
1891 complete_cmd_parse(el, NULL, argc == carg, '\0', 1);
1892 ret = CC_REDISPLAY;
1893 } else if (carg == 1 && cursor > 0 && line[cursor - 1] != ' ') {
1894 /* Handle the command parsing */
1895 if (complete_cmd_parse(el, argv[0], argc == carg,
Damien Miller02e87802013-08-21 02:38:51 +10001896 quote, terminated) != 0)
Darren Tucker909d8582010-01-08 19:02:40 +11001897 ret = CC_REDISPLAY;
1898 } else if (carg >= 1) {
1899 /* Handle file parsing */
1900 int remote = complete_is_remote(argv[0]);
1901 char *filematch = NULL;
1902
1903 if (carg > 1 && line[cursor-1] != ' ')
1904 filematch = argv[carg - 1];
1905
1906 if (remote != 0 &&
1907 complete_match(el, complete_ctx->conn,
1908 *complete_ctx->remote_pathp, filematch,
Damien Miller02e87802013-08-21 02:38:51 +10001909 remote, carg == argc, quote, terminated) != 0)
Darren Tucker909d8582010-01-08 19:02:40 +11001910 ret = CC_REDISPLAY;
1911 }
1912
Damien Miller02e87802013-08-21 02:38:51 +10001913 free(line);
Darren Tucker909d8582010-01-08 19:02:40 +11001914 return ret;
1915}
Darren Tuckere67f7db2010-01-08 19:50:02 +11001916#endif /* USE_LIBEDIT */
Darren Tucker909d8582010-01-08 19:02:40 +11001917
Damien Miller20e1fab2004-02-18 14:30:55 +11001918int
Darren Tucker21063192010-01-08 17:10:36 +11001919interactive_loop(struct sftp_conn *conn, char *file1, char *file2)
Damien Miller20e1fab2004-02-18 14:30:55 +11001920{
Darren Tucker909d8582010-01-08 19:02:40 +11001921 char *remote_path;
Damien Miller20e1fab2004-02-18 14:30:55 +11001922 char *dir = NULL;
1923 char cmd[2048];
Damien Miller0e2c1022005-08-12 22:16:22 +10001924 int err, interactive;
Darren Tucker2d963d82004-11-07 20:04:10 +11001925 EditLine *el = NULL;
1926#ifdef USE_LIBEDIT
1927 History *hl = NULL;
1928 HistEvent hev;
1929 extern char *__progname;
Darren Tucker909d8582010-01-08 19:02:40 +11001930 struct complete_ctx complete_ctx;
Darren Tucker2d963d82004-11-07 20:04:10 +11001931
1932 if (!batchmode && isatty(STDIN_FILENO)) {
1933 if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL)
1934 fatal("Couldn't initialise editline");
1935 if ((hl = history_init()) == NULL)
1936 fatal("Couldn't initialise editline history");
1937 history(hl, &hev, H_SETSIZE, 100);
1938 el_set(el, EL_HIST, history, hl);
1939
1940 el_set(el, EL_PROMPT, prompt);
1941 el_set(el, EL_EDITOR, "emacs");
1942 el_set(el, EL_TERMINAL, NULL);
1943 el_set(el, EL_SIGNAL, 1);
1944 el_source(el, NULL);
Darren Tucker909d8582010-01-08 19:02:40 +11001945
1946 /* Tab Completion */
Damien Miller02e87802013-08-21 02:38:51 +10001947 el_set(el, EL_ADDFN, "ftp-complete",
Darren Tuckerd78739a2010-10-24 10:56:32 +11001948 "Context sensitive argument completion", complete);
Darren Tucker909d8582010-01-08 19:02:40 +11001949 complete_ctx.conn = conn;
1950 complete_ctx.remote_pathp = &remote_path;
1951 el_set(el, EL_CLIENTDATA, (void*)&complete_ctx);
1952 el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
Darren Tucker2d963d82004-11-07 20:04:10 +11001953 }
1954#endif /* USE_LIBEDIT */
Damien Miller20e1fab2004-02-18 14:30:55 +11001955
Darren Tucker909d8582010-01-08 19:02:40 +11001956 remote_path = do_realpath(conn, ".");
1957 if (remote_path == NULL)
Damien Miller20e1fab2004-02-18 14:30:55 +11001958 fatal("Need cwd");
1959
1960 if (file1 != NULL) {
1961 dir = xstrdup(file1);
Darren Tucker909d8582010-01-08 19:02:40 +11001962 dir = make_absolute(dir, remote_path);
Damien Miller20e1fab2004-02-18 14:30:55 +11001963
1964 if (remote_is_dir(conn, dir) && file2 == NULL) {
Damien Miller9303e652013-04-23 15:22:40 +10001965 if (!quiet)
1966 printf("Changing to: %s\n", dir);
Damien Miller20e1fab2004-02-18 14:30:55 +11001967 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
Darren Tucker909d8582010-01-08 19:02:40 +11001968 if (parse_dispatch_command(conn, cmd,
1969 &remote_path, 1) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001970 free(dir);
1971 free(remote_path);
1972 free(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001973 return (-1);
Darren Tuckercd516ef2004-12-06 22:43:43 +11001974 }
Damien Miller20e1fab2004-02-18 14:30:55 +11001975 } else {
Darren Tucker0af24052012-10-05 10:41:25 +10001976 /* XXX this is wrong wrt quoting */
Damien Miller0d032412013-07-25 11:56:52 +10001977 snprintf(cmd, sizeof cmd, "get%s %s%s%s",
1978 global_aflag ? " -a" : "", dir,
1979 file2 == NULL ? "" : " ",
1980 file2 == NULL ? "" : file2);
Darren Tucker909d8582010-01-08 19:02:40 +11001981 err = parse_dispatch_command(conn, cmd,
1982 &remote_path, 1);
Darren Tuckera627d422013-06-02 07:31:17 +10001983 free(dir);
1984 free(remote_path);
1985 free(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001986 return (err);
1987 }
Darren Tuckera627d422013-06-02 07:31:17 +10001988 free(dir);
Damien Miller20e1fab2004-02-18 14:30:55 +11001989 }
1990
Damien Miller37294fb2005-07-17 17:18:49 +10001991 setlinebuf(stdout);
1992 setlinebuf(infile);
Damien Miller20e1fab2004-02-18 14:30:55 +11001993
Damien Miller0e2c1022005-08-12 22:16:22 +10001994 interactive = !batchmode && isatty(STDIN_FILENO);
Damien Miller20e1fab2004-02-18 14:30:55 +11001995 err = 0;
1996 for (;;) {
1997 char *cp;
1998
Darren Tuckercdf547a2004-05-24 10:12:19 +10001999 signal(SIGINT, SIG_IGN);
2000
Darren Tucker2d963d82004-11-07 20:04:10 +11002001 if (el == NULL) {
Damien Miller0e2c1022005-08-12 22:16:22 +10002002 if (interactive)
2003 printf("sftp> ");
Darren Tucker2d963d82004-11-07 20:04:10 +11002004 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller0e2c1022005-08-12 22:16:22 +10002005 if (interactive)
2006 printf("\n");
Darren Tucker2d963d82004-11-07 20:04:10 +11002007 break;
2008 }
Damien Miller0e2c1022005-08-12 22:16:22 +10002009 if (!interactive) { /* Echo command */
2010 printf("sftp> %s", cmd);
2011 if (strlen(cmd) > 0 &&
2012 cmd[strlen(cmd) - 1] != '\n')
2013 printf("\n");
2014 }
Darren Tucker2d963d82004-11-07 20:04:10 +11002015 } else {
2016#ifdef USE_LIBEDIT
2017 const char *line;
2018 int count = 0;
Damien Miller20e1fab2004-02-18 14:30:55 +11002019
Darren Tucker909d8582010-01-08 19:02:40 +11002020 if ((line = el_gets(el, &count)) == NULL ||
2021 count <= 0) {
Damien Miller0e2c1022005-08-12 22:16:22 +10002022 printf("\n");
2023 break;
2024 }
Darren Tucker2d963d82004-11-07 20:04:10 +11002025 history(hl, &hev, H_ENTER, line);
2026 if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
2027 fprintf(stderr, "Error: input line too long\n");
2028 continue;
2029 }
2030#endif /* USE_LIBEDIT */
Damien Miller20e1fab2004-02-18 14:30:55 +11002031 }
2032
Damien Miller20e1fab2004-02-18 14:30:55 +11002033 cp = strrchr(cmd, '\n');
2034 if (cp)
2035 *cp = '\0';
2036
Darren Tuckercdf547a2004-05-24 10:12:19 +10002037 /* Handle user interrupts gracefully during commands */
2038 interrupted = 0;
2039 signal(SIGINT, cmd_interrupt);
2040
Darren Tucker909d8582010-01-08 19:02:40 +11002041 err = parse_dispatch_command(conn, cmd, &remote_path,
2042 batchmode);
Damien Miller20e1fab2004-02-18 14:30:55 +11002043 if (err != 0)
2044 break;
2045 }
Darren Tuckera627d422013-06-02 07:31:17 +10002046 free(remote_path);
2047 free(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11002048
Tim Rice027e8b12005-08-15 14:52:50 -07002049#ifdef USE_LIBEDIT
Damien Miller0e2c1022005-08-12 22:16:22 +10002050 if (el != NULL)
2051 el_end(el);
Tim Rice027e8b12005-08-15 14:52:50 -07002052#endif /* USE_LIBEDIT */
Damien Miller0e2c1022005-08-12 22:16:22 +10002053
Damien Miller20e1fab2004-02-18 14:30:55 +11002054 /* err == 1 signifies normal "quit" exit */
2055 return (err >= 0 ? 0 : -1);
2056}
Damien Miller62d57f62003-01-10 21:43:24 +11002057
Ben Lindstrombba81212001-06-25 05:01:22 +00002058static void
Damien Millercc685c12003-06-04 22:51:38 +10002059connect_to_server(char *path, char **args, int *in, int *out)
Damien Miller33804262001-02-04 23:20:18 +11002060{
2061 int c_in, c_out;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00002062
Damien Miller33804262001-02-04 23:20:18 +11002063#ifdef USE_PIPES
2064 int pin[2], pout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +00002065
Damien Miller33804262001-02-04 23:20:18 +11002066 if ((pipe(pin) == -1) || (pipe(pout) == -1))
2067 fatal("pipe: %s", strerror(errno));
2068 *in = pin[0];
2069 *out = pout[1];
2070 c_in = pout[0];
2071 c_out = pin[1];
2072#else /* USE_PIPES */
2073 int inout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +00002074
Damien Miller33804262001-02-04 23:20:18 +11002075 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
2076 fatal("socketpair: %s", strerror(errno));
2077 *in = *out = inout[0];
2078 c_in = c_out = inout[1];
2079#endif /* USE_PIPES */
2080
Damien Millercc685c12003-06-04 22:51:38 +10002081 if ((sshpid = fork()) == -1)
Damien Miller33804262001-02-04 23:20:18 +11002082 fatal("fork: %s", strerror(errno));
Damien Millercc685c12003-06-04 22:51:38 +10002083 else if (sshpid == 0) {
Damien Miller33804262001-02-04 23:20:18 +11002084 if ((dup2(c_in, STDIN_FILENO) == -1) ||
2085 (dup2(c_out, STDOUT_FILENO) == -1)) {
2086 fprintf(stderr, "dup2: %s\n", strerror(errno));
Damien Miller350327c2004-06-15 10:24:13 +10002087 _exit(1);
Damien Miller33804262001-02-04 23:20:18 +11002088 }
2089 close(*in);
2090 close(*out);
2091 close(c_in);
2092 close(c_out);
Darren Tuckercdf547a2004-05-24 10:12:19 +10002093
2094 /*
2095 * The underlying ssh is in the same process group, so we must
Darren Tuckerfc959702004-07-17 16:12:08 +10002096 * ignore SIGINT if we want to gracefully abort commands,
2097 * otherwise the signal will make it to the ssh process and
Darren Tuckerb8b17e92010-01-15 11:46:03 +11002098 * kill it too. Contrawise, since sftp sends SIGTERMs to the
2099 * underlying ssh, it must *not* ignore that signal.
Darren Tuckercdf547a2004-05-24 10:12:19 +10002100 */
2101 signal(SIGINT, SIG_IGN);
Darren Tuckerb8b17e92010-01-15 11:46:03 +11002102 signal(SIGTERM, SIG_DFL);
Darren Tuckerbd12f172004-06-18 16:23:43 +10002103 execvp(path, args);
Damien Millerd14ee1e2002-02-05 12:27:31 +11002104 fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
Damien Miller350327c2004-06-15 10:24:13 +10002105 _exit(1);
Damien Miller33804262001-02-04 23:20:18 +11002106 }
2107
Damien Millercc685c12003-06-04 22:51:38 +10002108 signal(SIGTERM, killchild);
2109 signal(SIGINT, killchild);
2110 signal(SIGHUP, killchild);
Damien Miller33804262001-02-04 23:20:18 +11002111 close(c_in);
2112 close(c_out);
2113}
2114
Ben Lindstrombba81212001-06-25 05:01:22 +00002115static void
Damien Miller33804262001-02-04 23:20:18 +11002116usage(void)
2117{
Damien Miller025e01c2002-02-08 22:06:29 +11002118 extern char *__progname;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002119
Ben Lindstrom1e243242001-09-18 05:38:44 +00002120 fprintf(stderr,
Damien Miller02e87802013-08-21 02:38:51 +10002121 "usage: %s [-1246Capqrv] [-B buffer_size] [-b batchfile] [-c cipher]\n"
Darren Tuckerc07138e2009-10-07 08:23:44 +11002122 " [-D sftp_server_path] [-F ssh_config] "
Damien Miller56883e12010-09-24 22:15:39 +10002123 "[-i identity_file] [-l limit]\n"
Darren Tuckerc07138e2009-10-07 08:23:44 +11002124 " [-o ssh_option] [-P port] [-R num_requests] "
2125 "[-S program]\n"
Darren Tucker46bbbe32009-10-07 08:21:48 +11002126 " [-s subsystem | sftp_server] host\n"
Damien Miller7ebfad72008-12-09 14:12:33 +11002127 " %s [user@]host[:file ...]\n"
2128 " %s [user@]host[:dir[/]]\n"
Darren Tucker46bbbe32009-10-07 08:21:48 +11002129 " %s -b batchfile [user@]host\n",
2130 __progname, __progname, __progname, __progname);
Damien Miller33804262001-02-04 23:20:18 +11002131 exit(1);
2132}
2133
Kevin Stevesef4eea92001-02-05 12:42:17 +00002134int
Damien Miller33804262001-02-04 23:20:18 +11002135main(int argc, char **argv)
2136{
Damien Miller956f3fb2003-01-10 21:40:00 +11002137 int in, out, ch, err;
Darren Tucker340d1682010-01-09 08:54:31 +11002138 char *host = NULL, *userhost, *cp, *file2 = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +00002139 int debug_level = 0, sshver = 2;
2140 char *file1 = NULL, *sftp_server = NULL;
Damien Millerd14ee1e2002-02-05 12:27:31 +11002141 char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
Damien Miller65e42f82010-09-24 22:15:11 +10002142 const char *errstr;
Ben Lindstrom387c4722001-05-08 20:27:25 +00002143 LogLevel ll = SYSLOG_LEVEL_INFO;
2144 arglist args;
Damien Millerd7686fd2001-02-10 00:40:03 +11002145 extern int optind;
2146 extern char *optarg;
Darren Tucker21063192010-01-08 17:10:36 +11002147 struct sftp_conn *conn;
2148 size_t copy_buffer_len = DEFAULT_COPY_BUFLEN;
2149 size_t num_requests = DEFAULT_NUM_REQUESTS;
Damien Miller65e42f82010-09-24 22:15:11 +10002150 long long limit_kbps = 0;
Damien Miller33804262001-02-04 23:20:18 +11002151
Darren Tuckerce321d82005-10-03 18:11:24 +10002152 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2153 sanitise_stdfd();
Darren Tuckerea647212013-06-06 08:19:09 +10002154 setlocale(LC_CTYPE, "");
Darren Tuckerce321d82005-10-03 18:11:24 +10002155
Damien Miller59d3d5b2003-08-22 09:34:41 +10002156 __progname = ssh_get_progname(argv[0]);
Damien Miller3eec6b72006-01-31 21:49:27 +11002157 memset(&args, '\0', sizeof(args));
Ben Lindstrom387c4722001-05-08 20:27:25 +00002158 args.list = NULL;
Damien Miller2b5a0de2006-03-31 23:10:31 +11002159 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +00002160 addargs(&args, "-oForwardX11 no");
2161 addargs(&args, "-oForwardAgent no");
Damien Millerd27b9472005-12-13 19:29:02 +11002162 addargs(&args, "-oPermitLocalCommand no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00002163 addargs(&args, "-oClearAllForwardings yes");
Damien Millere4f5a822004-01-21 14:11:05 +11002164
Ben Lindstrom387c4722001-05-08 20:27:25 +00002165 ll = SYSLOG_LEVEL_INFO;
Damien Millere4f5a822004-01-21 14:11:05 +11002166 infile = stdin;
Damien Millerd7686fd2001-02-10 00:40:03 +11002167
Darren Tucker282b4022009-10-07 08:23:06 +11002168 while ((ch = getopt(argc, argv,
Damien Miller0d032412013-07-25 11:56:52 +10002169 "1246ahpqrvCc:D:i:l:o:s:S:b:B:F:P:R:")) != -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +11002170 switch (ch) {
Darren Tucker46bbbe32009-10-07 08:21:48 +11002171 /* Passed through to ssh(1) */
2172 case '4':
2173 case '6':
Damien Millerd7686fd2001-02-10 00:40:03 +11002174 case 'C':
Darren Tucker46bbbe32009-10-07 08:21:48 +11002175 addargs(&args, "-%c", ch);
2176 break;
2177 /* Passed through to ssh(1) with argument */
2178 case 'F':
2179 case 'c':
2180 case 'i':
2181 case 'o':
Darren Tuckerc4dc4f52010-01-08 18:50:04 +11002182 addargs(&args, "-%c", ch);
2183 addargs(&args, "%s", optarg);
Darren Tucker46bbbe32009-10-07 08:21:48 +11002184 break;
2185 case 'q':
Damien Miller9303e652013-04-23 15:22:40 +10002186 ll = SYSLOG_LEVEL_ERROR;
2187 quiet = 1;
Darren Tucker46bbbe32009-10-07 08:21:48 +11002188 showprogress = 0;
2189 addargs(&args, "-%c", ch);
Damien Millerd7686fd2001-02-10 00:40:03 +11002190 break;
Darren Tucker282b4022009-10-07 08:23:06 +11002191 case 'P':
2192 addargs(&args, "-oPort %s", optarg);
2193 break;
Damien Millerd7686fd2001-02-10 00:40:03 +11002194 case 'v':
Ben Lindstrom387c4722001-05-08 20:27:25 +00002195 if (debug_level < 3) {
2196 addargs(&args, "-v");
2197 ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
2198 }
2199 debug_level++;
Damien Millerd7686fd2001-02-10 00:40:03 +11002200 break;
Damien Millerd7686fd2001-02-10 00:40:03 +11002201 case '1':
Ben Lindstrom387c4722001-05-08 20:27:25 +00002202 sshver = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +11002203 if (sftp_server == NULL)
2204 sftp_server = _PATH_SFTP_SERVER;
2205 break;
Darren Tucker46bbbe32009-10-07 08:21:48 +11002206 case '2':
2207 sshver = 2;
Damien Millerd7686fd2001-02-10 00:40:03 +11002208 break;
Damien Miller0d032412013-07-25 11:56:52 +10002209 case 'a':
2210 global_aflag = 1;
2211 break;
Darren Tucker46bbbe32009-10-07 08:21:48 +11002212 case 'B':
2213 copy_buffer_len = strtol(optarg, &cp, 10);
2214 if (copy_buffer_len == 0 || *cp != '\0')
2215 fatal("Invalid buffer size \"%s\"", optarg);
Damien Millerd7686fd2001-02-10 00:40:03 +11002216 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +00002217 case 'b':
Damien Miller44f75c12004-01-21 10:58:47 +11002218 if (batchmode)
2219 fatal("Batch file already specified.");
2220
2221 /* Allow "-" as stdin */
Darren Tuckerfc959702004-07-17 16:12:08 +10002222 if (strcmp(optarg, "-") != 0 &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10002223 (infile = fopen(optarg, "r")) == NULL)
Damien Miller44f75c12004-01-21 10:58:47 +11002224 fatal("%s (%s).", strerror(errno), optarg);
Damien Miller62d57f62003-01-10 21:43:24 +11002225 showprogress = 0;
Damien Miller9303e652013-04-23 15:22:40 +10002226 quiet = batchmode = 1;
Damien Miller64e8d442005-03-01 21:16:47 +11002227 addargs(&args, "-obatchmode yes");
Ben Lindstrom562c26b2001-03-07 01:26:48 +00002228 break;
Darren Tucker1b0dd172009-10-07 08:37:48 +11002229 case 'p':
2230 global_pflag = 1;
2231 break;
Darren Tucker282b4022009-10-07 08:23:06 +11002232 case 'D':
Damien Millerd14ee1e2002-02-05 12:27:31 +11002233 sftp_direct = optarg;
2234 break;
Damien Miller65e42f82010-09-24 22:15:11 +10002235 case 'l':
2236 limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
2237 &errstr);
2238 if (errstr != NULL)
2239 usage();
2240 limit_kbps *= 1024; /* kbps */
2241 break;
Darren Tucker1b0dd172009-10-07 08:37:48 +11002242 case 'r':
2243 global_rflag = 1;
2244 break;
Damien Miller16a13332002-02-13 14:03:56 +11002245 case 'R':
2246 num_requests = strtol(optarg, &cp, 10);
2247 if (num_requests == 0 || *cp != '\0')
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002248 fatal("Invalid number of requests \"%s\"",
Damien Miller16a13332002-02-13 14:03:56 +11002249 optarg);
2250 break;
Darren Tucker46bbbe32009-10-07 08:21:48 +11002251 case 's':
2252 sftp_server = optarg;
2253 break;
2254 case 'S':
2255 ssh_program = optarg;
2256 replacearg(&args, 0, "%s", ssh_program);
2257 break;
Damien Millerd7686fd2001-02-10 00:40:03 +11002258 case 'h':
2259 default:
Damien Miller33804262001-02-04 23:20:18 +11002260 usage();
2261 }
2262 }
2263
Damien Millerc0f27d82004-03-08 23:12:19 +11002264 if (!isatty(STDERR_FILENO))
2265 showprogress = 0;
2266
Ben Lindstrom2f3d52a2002-04-02 21:06:18 +00002267 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
2268
Damien Millerd14ee1e2002-02-05 12:27:31 +11002269 if (sftp_direct == NULL) {
2270 if (optind == argc || argc > (optind + 2))
2271 usage();
Damien Miller33804262001-02-04 23:20:18 +11002272
Damien Millerd14ee1e2002-02-05 12:27:31 +11002273 userhost = xstrdup(argv[optind]);
2274 file2 = argv[optind+1];
Ben Lindstrom63667f62001-04-13 00:00:14 +00002275
Ben Lindstromc276c122002-12-23 02:14:51 +00002276 if ((host = strrchr(userhost, '@')) == NULL)
Damien Millerd14ee1e2002-02-05 12:27:31 +11002277 host = userhost;
2278 else {
2279 *host++ = '\0';
2280 if (!userhost[0]) {
2281 fprintf(stderr, "Missing username\n");
2282 usage();
2283 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +11002284 addargs(&args, "-l");
2285 addargs(&args, "%s", userhost);
Damien Millerd14ee1e2002-02-05 12:27:31 +11002286 }
2287
Damien Millerec692032004-01-27 21:22:00 +11002288 if ((cp = colon(host)) != NULL) {
2289 *cp++ = '\0';
2290 file1 = cp;
2291 }
2292
Damien Millerd14ee1e2002-02-05 12:27:31 +11002293 host = cleanhostname(host);
2294 if (!*host) {
2295 fprintf(stderr, "Missing hostname\n");
Damien Miller33804262001-02-04 23:20:18 +11002296 usage();
2297 }
Damien Millerd14ee1e2002-02-05 12:27:31 +11002298
Damien Millerd14ee1e2002-02-05 12:27:31 +11002299 addargs(&args, "-oProtocol %d", sshver);
2300
2301 /* no subsystem if the server-spec contains a '/' */
2302 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
2303 addargs(&args, "-s");
2304
Darren Tuckerb8c884a2010-01-08 18:53:43 +11002305 addargs(&args, "--");
Damien Millerd14ee1e2002-02-05 12:27:31 +11002306 addargs(&args, "%s", host);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00002307 addargs(&args, "%s", (sftp_server != NULL ?
Damien Millerd14ee1e2002-02-05 12:27:31 +11002308 sftp_server : "sftp"));
Damien Millerd14ee1e2002-02-05 12:27:31 +11002309
Damien Millercc685c12003-06-04 22:51:38 +10002310 connect_to_server(ssh_program, args.list, &in, &out);
Damien Millerd14ee1e2002-02-05 12:27:31 +11002311 } else {
2312 args.list = NULL;
2313 addargs(&args, "sftp-server");
2314
Damien Millercc685c12003-06-04 22:51:38 +10002315 connect_to_server(sftp_direct, args.list, &in, &out);
Damien Miller33804262001-02-04 23:20:18 +11002316 }
Damien Miller3eec6b72006-01-31 21:49:27 +11002317 freeargs(&args);
Damien Miller33804262001-02-04 23:20:18 +11002318
Damien Miller65e42f82010-09-24 22:15:11 +10002319 conn = do_init(in, out, copy_buffer_len, num_requests, limit_kbps);
Darren Tucker21063192010-01-08 17:10:36 +11002320 if (conn == NULL)
2321 fatal("Couldn't initialise connection to server");
2322
Damien Miller9303e652013-04-23 15:22:40 +10002323 if (!quiet) {
Darren Tucker21063192010-01-08 17:10:36 +11002324 if (sftp_direct == NULL)
2325 fprintf(stderr, "Connected to %s.\n", host);
2326 else
2327 fprintf(stderr, "Attached to %s.\n", sftp_direct);
2328 }
2329
2330 err = interactive_loop(conn, file1, file2);
Damien Miller33804262001-02-04 23:20:18 +11002331
Ben Lindstrom10b9bf92001-02-26 20:04:45 +00002332#if !defined(USE_PIPES)
Damien Miller37294fb2005-07-17 17:18:49 +10002333 shutdown(in, SHUT_RDWR);
2334 shutdown(out, SHUT_RDWR);
Ben Lindstrom10b9bf92001-02-26 20:04:45 +00002335#endif
2336
Damien Miller33804262001-02-04 23:20:18 +11002337 close(in);
2338 close(out);
Damien Miller44f75c12004-01-21 10:58:47 +11002339 if (batchmode)
Ben Lindstrom562c26b2001-03-07 01:26:48 +00002340 fclose(infile);
Damien Miller33804262001-02-04 23:20:18 +11002341
Ben Lindstrom47fd8112002-04-02 20:48:19 +00002342 while (waitpid(sshpid, NULL, 0) == -1)
2343 if (errno != EINTR)
2344 fatal("Couldn't wait for ssh process: %s",
2345 strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +11002346
Damien Miller956f3fb2003-01-10 21:40:00 +11002347 exit(err == 0 ? 0 : 1);
Damien Miller33804262001-02-04 23:20:18 +11002348}