blob: db0a8d9af0ef8329f5b707ea94cda92501af83e7 [file] [log] [blame]
Darren Tucker88b976f2007-12-29 02:40:43 +11001/* $OpenBSD: sftp.c,v 1.98 2007/12/12 05:04:03 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 Tucker2d963d82004-11-07 20:04:10 +110028
Damien Miller1cbc2922007-10-26 14:27:45 +100029#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100030#include <errno.h>
31
Damien Miller03e20032006-03-15 11:16:59 +110032#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110033# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110034#endif
Darren Tucker2d963d82004-11-07 20:04:10 +110035#ifdef USE_LIBEDIT
36#include <histedit.h>
37#else
38typedef void EditLine;
39#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110040#include <signal.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100041#include <stdlib.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100042#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100043#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100044#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100045#include <stdarg.h>
Damien Miller33804262001-02-04 23:20:18 +110046
Damien Miller33804262001-02-04 23:20:18 +110047#include "xmalloc.h"
48#include "log.h"
49#include "pathnames.h"
Ben Lindstrom4529b702001-05-03 23:39:53 +000050#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110051
52#include "sftp.h"
Damien Millerd7834352006-08-05 12:39:39 +100053#include "buffer.h"
Damien Miller33804262001-02-04 23:20:18 +110054#include "sftp-common.h"
55#include "sftp-client.h"
Damien Millerd7d46bb2004-02-18 14:11:13 +110056
Damien Miller20e1fab2004-02-18 14:30:55 +110057/* File to read commands from */
58FILE* infile;
59
60/* Are we in batchfile mode? */
61int batchmode = 0;
62
63/* Size of buffer used when copying files */
64size_t copy_buffer_len = 32768;
65
66/* Number of concurrent outstanding requests */
67size_t num_requests = 16;
68
69/* PID of ssh transport process */
70static pid_t sshpid = -1;
71
72/* This is set to 0 if the progressmeter is not desired. */
Damien Millerc0f27d82004-03-08 23:12:19 +110073int showprogress = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +110074
Darren Tuckercdf547a2004-05-24 10:12:19 +100075/* SIGINT received during command processing */
76volatile sig_atomic_t interrupted = 0;
77
Darren Tuckerb9123452004-06-22 13:06:45 +100078/* I wish qsort() took a separate ctx for the comparison function...*/
79int sort_flag;
80
Damien Miller20e1fab2004-02-18 14:30:55 +110081int remote_glob(struct sftp_conn *, const char *, int,
82 int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
Damien Miller33804262001-02-04 23:20:18 +110083
Kevin Steves12888d12001-03-05 19:50:57 +000084extern char *__progname;
Kevin Steves12888d12001-03-05 19:50:57 +000085
Damien Miller20e1fab2004-02-18 14:30:55 +110086/* Separators for interactive commands */
87#define WHITESPACE " \t\r\n"
Damien Millerd7686fd2001-02-10 00:40:03 +110088
Darren Tuckerb9123452004-06-22 13:06:45 +100089/* ls flags */
Darren Tuckera4e9ffa2004-06-22 13:07:58 +100090#define LS_LONG_VIEW 0x01 /* Full view ala ls -l */
91#define LS_SHORT_VIEW 0x02 /* Single row view ala ls -1 */
92#define LS_NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */
93#define LS_NAME_SORT 0x08 /* Sort by name (default) */
94#define LS_TIME_SORT 0x10 /* Sort by mtime */
95#define LS_SIZE_SORT 0x20 /* Sort by file size */
96#define LS_REVERSE_SORT 0x40 /* Reverse sort order */
Darren Tucker9a526452004-06-22 13:09:55 +100097#define LS_SHOW_ALL 0x80 /* Don't skip filenames starting with '.' */
Darren Tuckerb9123452004-06-22 13:06:45 +100098
Darren Tuckera4e9ffa2004-06-22 13:07:58 +100099#define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW)
100#define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
Damien Miller20e1fab2004-02-18 14:30:55 +1100101
102/* Commands for interactive mode */
103#define I_CHDIR 1
104#define I_CHGRP 2
105#define I_CHMOD 3
106#define I_CHOWN 4
107#define I_GET 5
108#define I_HELP 6
109#define I_LCHDIR 7
110#define I_LLS 8
111#define I_LMKDIR 9
112#define I_LPWD 10
113#define I_LS 11
114#define I_LUMASK 12
115#define I_MKDIR 13
116#define I_PUT 14
117#define I_PWD 15
118#define I_QUIT 16
119#define I_RENAME 17
120#define I_RM 18
121#define I_RMDIR 19
122#define I_SHELL 20
123#define I_SYMLINK 21
124#define I_VERSION 22
125#define I_PROGRESS 23
126
127struct CMD {
128 const char *c;
129 const int n;
130};
131
132static const struct CMD cmds[] = {
133 { "bye", I_QUIT },
134 { "cd", I_CHDIR },
135 { "chdir", I_CHDIR },
136 { "chgrp", I_CHGRP },
137 { "chmod", I_CHMOD },
138 { "chown", I_CHOWN },
139 { "dir", I_LS },
140 { "exit", I_QUIT },
141 { "get", I_GET },
142 { "mget", I_GET },
143 { "help", I_HELP },
144 { "lcd", I_LCHDIR },
145 { "lchdir", I_LCHDIR },
146 { "lls", I_LLS },
147 { "lmkdir", I_LMKDIR },
148 { "ln", I_SYMLINK },
149 { "lpwd", I_LPWD },
150 { "ls", I_LS },
151 { "lumask", I_LUMASK },
152 { "mkdir", I_MKDIR },
153 { "progress", I_PROGRESS },
154 { "put", I_PUT },
155 { "mput", I_PUT },
156 { "pwd", I_PWD },
157 { "quit", I_QUIT },
158 { "rename", I_RENAME },
159 { "rm", I_RM },
160 { "rmdir", I_RMDIR },
161 { "symlink", I_SYMLINK },
162 { "version", I_VERSION },
163 { "!", I_SHELL },
164 { "?", I_HELP },
165 { NULL, -1}
166};
167
168int interactive_loop(int fd_in, int fd_out, char *file1, char *file2);
169
Damien Millerb6c85fc2007-01-05 16:30:41 +1100170/* ARGSUSED */
Damien Miller20e1fab2004-02-18 14:30:55 +1100171static void
Darren Tuckercdf547a2004-05-24 10:12:19 +1000172killchild(int signo)
173{
Darren Tuckerba66df82005-01-24 21:57:40 +1100174 if (sshpid > 1) {
Darren Tuckercdf547a2004-05-24 10:12:19 +1000175 kill(sshpid, SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100176 waitpid(sshpid, NULL, 0);
177 }
Darren Tuckercdf547a2004-05-24 10:12:19 +1000178
179 _exit(1);
180}
181
Damien Millerb6c85fc2007-01-05 16:30:41 +1100182/* ARGSUSED */
Darren Tuckercdf547a2004-05-24 10:12:19 +1000183static void
184cmd_interrupt(int signo)
185{
186 const char msg[] = "\rInterrupt \n";
Darren Tuckere2f189a2004-12-06 22:45:53 +1100187 int olderrno = errno;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000188
189 write(STDERR_FILENO, msg, sizeof(msg) - 1);
190 interrupted = 1;
Darren Tuckere2f189a2004-12-06 22:45:53 +1100191 errno = olderrno;
Darren Tuckercdf547a2004-05-24 10:12:19 +1000192}
193
194static void
Damien Miller20e1fab2004-02-18 14:30:55 +1100195help(void)
196{
197 printf("Available commands:\n");
198 printf("cd path Change remote directory to 'path'\n");
199 printf("lcd path Change local directory to 'path'\n");
200 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
201 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
202 printf("chown own path Change owner of file 'path' to 'own'\n");
203 printf("help Display this help text\n");
204 printf("get remote-path [local-path] Download file\n");
205 printf("lls [ls-options [path]] Display local directory listing\n");
206 printf("ln oldpath newpath Symlink remote file\n");
207 printf("lmkdir path Create local directory\n");
208 printf("lpwd Print local working directory\n");
209 printf("ls [path] Display remote directory listing\n");
210 printf("lumask umask Set local umask to 'umask'\n");
211 printf("mkdir path Create remote directory\n");
212 printf("progress Toggle display of progress meter\n");
213 printf("put local-path [remote-path] Upload file\n");
214 printf("pwd Display remote working directory\n");
215 printf("exit Quit sftp\n");
216 printf("quit Quit sftp\n");
217 printf("rename oldpath newpath Rename remote file\n");
218 printf("rmdir path Remove remote directory\n");
219 printf("rm path Delete remote file\n");
220 printf("symlink oldpath newpath Symlink remote file\n");
221 printf("version Show SFTP version\n");
222 printf("!command Execute 'command' in local shell\n");
223 printf("! Escape to local shell\n");
224 printf("? Synonym for help\n");
225}
226
227static void
228local_do_shell(const char *args)
229{
230 int status;
231 char *shell;
232 pid_t pid;
233
234 if (!*args)
235 args = NULL;
236
237 if ((shell = getenv("SHELL")) == NULL)
238 shell = _PATH_BSHELL;
239
240 if ((pid = fork()) == -1)
241 fatal("Couldn't fork: %s", strerror(errno));
242
243 if (pid == 0) {
244 /* XXX: child has pipe fds to ssh subproc open - issue? */
245 if (args) {
246 debug3("Executing %s -c \"%s\"", shell, args);
247 execl(shell, shell, "-c", args, (char *)NULL);
248 } else {
249 debug3("Executing %s", shell);
250 execl(shell, shell, (char *)NULL);
251 }
252 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
253 strerror(errno));
254 _exit(1);
255 }
256 while (waitpid(pid, &status, 0) == -1)
257 if (errno != EINTR)
258 fatal("Couldn't wait for child: %s", strerror(errno));
259 if (!WIFEXITED(status))
Damien Miller55b04f12006-03-26 14:23:17 +1100260 error("Shell exited abnormally");
Damien Miller20e1fab2004-02-18 14:30:55 +1100261 else if (WEXITSTATUS(status))
262 error("Shell exited with status %d", WEXITSTATUS(status));
263}
264
265static void
266local_do_ls(const char *args)
267{
268 if (!args || !*args)
269 local_do_shell(_PATH_LS);
270 else {
271 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
272 char *buf = xmalloc(len);
273
274 /* XXX: quoting - rip quoting code from ftp? */
275 snprintf(buf, len, _PATH_LS " %s", args);
276 local_do_shell(buf);
277 xfree(buf);
278 }
279}
280
281/* Strip one path (usually the pwd) from the start of another */
282static char *
283path_strip(char *path, char *strip)
284{
285 size_t len;
286
287 if (strip == NULL)
288 return (xstrdup(path));
289
290 len = strlen(strip);
Darren Tuckere2f189a2004-12-06 22:45:53 +1100291 if (strncmp(path, strip, len) == 0) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100292 if (strip[len - 1] != '/' && path[len] == '/')
293 len++;
294 return (xstrdup(path + len));
295 }
296
297 return (xstrdup(path));
298}
299
300static char *
301path_append(char *p1, char *p2)
302{
303 char *ret;
Damien Miller3ca8b772007-01-05 16:24:47 +1100304 size_t len = strlen(p1) + strlen(p2) + 2;
Damien Miller20e1fab2004-02-18 14:30:55 +1100305
306 ret = xmalloc(len);
307 strlcpy(ret, p1, len);
Damien Miller3ca8b772007-01-05 16:24:47 +1100308 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
Damien Miller20e1fab2004-02-18 14:30:55 +1100309 strlcat(ret, "/", len);
310 strlcat(ret, p2, len);
311
312 return(ret);
313}
314
315static char *
316make_absolute(char *p, char *pwd)
317{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000318 char *abs_str;
Damien Miller20e1fab2004-02-18 14:30:55 +1100319
320 /* Derelativise */
321 if (p && p[0] != '/') {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000322 abs_str = path_append(pwd, p);
Damien Miller20e1fab2004-02-18 14:30:55 +1100323 xfree(p);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000324 return(abs_str);
Damien Miller20e1fab2004-02-18 14:30:55 +1100325 } else
326 return(p);
327}
328
329static int
330infer_path(const char *p, char **ifp)
331{
332 char *cp;
333
334 cp = strrchr(p, '/');
335 if (cp == NULL) {
336 *ifp = xstrdup(p);
337 return(0);
338 }
339
340 if (!cp[1]) {
341 error("Invalid path");
342 return(-1);
343 }
344
345 *ifp = xstrdup(cp + 1);
346 return(0);
347}
348
349static int
Damien Miller1cbc2922007-10-26 14:27:45 +1000350parse_getput_flags(const char *cmd, char **argv, int argc, int *pflag)
Damien Miller20e1fab2004-02-18 14:30:55 +1100351{
Damien Miller1cbc2922007-10-26 14:27:45 +1000352 extern int optind, optreset, opterr;
353 int ch;
Damien Miller20e1fab2004-02-18 14:30:55 +1100354
Damien Miller1cbc2922007-10-26 14:27:45 +1000355 optind = optreset = 1;
356 opterr = 0;
357
358 *pflag = 0;
359 while ((ch = getopt(argc, argv, "Pp")) != -1) {
360 switch (ch) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100361 case 'p':
362 case 'P':
363 *pflag = 1;
364 break;
365 default:
Damien Miller1cbc2922007-10-26 14:27:45 +1000366 error("%s: Invalid flag -%c", cmd, ch);
367 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100368 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100369 }
370
Damien Miller1cbc2922007-10-26 14:27:45 +1000371 return optind;
Damien Miller20e1fab2004-02-18 14:30:55 +1100372}
373
374static int
Damien Miller1cbc2922007-10-26 14:27:45 +1000375parse_ls_flags(char **argv, int argc, int *lflag)
Damien Miller20e1fab2004-02-18 14:30:55 +1100376{
Damien Miller1cbc2922007-10-26 14:27:45 +1000377 extern int optind, optreset, opterr;
378 int ch;
Damien Miller20e1fab2004-02-18 14:30:55 +1100379
Damien Miller1cbc2922007-10-26 14:27:45 +1000380 optind = optreset = 1;
381 opterr = 0;
382
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000383 *lflag = LS_NAME_SORT;
Damien Miller1cbc2922007-10-26 14:27:45 +1000384 while ((ch = getopt(argc, argv, "1Saflnrt")) != -1) {
385 switch (ch) {
386 case '1':
387 *lflag &= ~VIEW_FLAGS;
388 *lflag |= LS_SHORT_VIEW;
389 break;
390 case 'S':
391 *lflag &= ~SORT_FLAGS;
392 *lflag |= LS_SIZE_SORT;
393 break;
394 case 'a':
395 *lflag |= LS_SHOW_ALL;
396 break;
397 case 'f':
398 *lflag &= ~SORT_FLAGS;
399 break;
400 case 'l':
401 *lflag &= ~VIEW_FLAGS;
402 *lflag |= LS_LONG_VIEW;
403 break;
404 case 'n':
405 *lflag &= ~VIEW_FLAGS;
406 *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
407 break;
408 case 'r':
409 *lflag |= LS_REVERSE_SORT;
410 break;
411 case 't':
412 *lflag &= ~SORT_FLAGS;
413 *lflag |= LS_TIME_SORT;
414 break;
415 default:
416 error("ls: Invalid flag -%c", ch);
417 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100418 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100419 }
420
Damien Miller1cbc2922007-10-26 14:27:45 +1000421 return optind;
Damien Miller20e1fab2004-02-18 14:30:55 +1100422}
423
424static int
425is_dir(char *path)
426{
427 struct stat sb;
428
429 /* XXX: report errors? */
430 if (stat(path, &sb) == -1)
431 return(0);
432
Darren Tucker1e80e402006-09-21 12:59:33 +1000433 return(S_ISDIR(sb.st_mode));
Damien Miller20e1fab2004-02-18 14:30:55 +1100434}
435
436static int
437is_reg(char *path)
438{
439 struct stat sb;
440
441 if (stat(path, &sb) == -1)
442 fatal("stat %s: %s", path, strerror(errno));
443
444 return(S_ISREG(sb.st_mode));
445}
446
447static int
448remote_is_dir(struct sftp_conn *conn, char *path)
449{
450 Attrib *a;
451
452 /* XXX: report errors? */
453 if ((a = do_stat(conn, path, 1)) == NULL)
454 return(0);
455 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
456 return(0);
Darren Tucker1e80e402006-09-21 12:59:33 +1000457 return(S_ISDIR(a->perm));
Damien Miller20e1fab2004-02-18 14:30:55 +1100458}
459
460static int
461process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
462{
463 char *abs_src = NULL;
464 char *abs_dst = NULL;
465 char *tmp;
466 glob_t g;
467 int err = 0;
468 int i;
469
470 abs_src = xstrdup(src);
471 abs_src = make_absolute(abs_src, pwd);
472
473 memset(&g, 0, sizeof(g));
474 debug3("Looking up %s", abs_src);
475 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
476 error("File \"%s\" not found.", abs_src);
477 err = -1;
478 goto out;
479 }
480
481 /* If multiple matches, dst must be a directory or unspecified */
482 if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
483 error("Multiple files match, but \"%s\" is not a directory",
484 dst);
485 err = -1;
486 goto out;
487 }
488
Darren Tuckercdf547a2004-05-24 10:12:19 +1000489 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100490 if (infer_path(g.gl_pathv[i], &tmp)) {
491 err = -1;
492 goto out;
493 }
494
495 if (g.gl_matchc == 1 && dst) {
496 /* If directory specified, append filename */
Damien Miller40b59852006-06-13 13:00:25 +1000497 xfree(tmp);
Damien Miller20e1fab2004-02-18 14:30:55 +1100498 if (is_dir(dst)) {
499 if (infer_path(g.gl_pathv[0], &tmp)) {
500 err = 1;
501 goto out;
502 }
503 abs_dst = path_append(dst, tmp);
504 xfree(tmp);
505 } else
506 abs_dst = xstrdup(dst);
507 } else if (dst) {
508 abs_dst = path_append(dst, tmp);
509 xfree(tmp);
510 } else
511 abs_dst = tmp;
512
513 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
514 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
515 err = -1;
516 xfree(abs_dst);
517 abs_dst = NULL;
518 }
519
520out:
521 xfree(abs_src);
Damien Miller20e1fab2004-02-18 14:30:55 +1100522 globfree(&g);
523 return(err);
524}
525
526static int
527process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
528{
529 char *tmp_dst = NULL;
530 char *abs_dst = NULL;
531 char *tmp;
532 glob_t g;
533 int err = 0;
534 int i;
535
536 if (dst) {
537 tmp_dst = xstrdup(dst);
538 tmp_dst = make_absolute(tmp_dst, pwd);
539 }
540
541 memset(&g, 0, sizeof(g));
542 debug3("Looking up %s", src);
543 if (glob(src, 0, NULL, &g)) {
544 error("File \"%s\" not found.", src);
545 err = -1;
546 goto out;
547 }
548
549 /* If multiple matches, dst may be directory or unspecified */
550 if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
551 error("Multiple files match, but \"%s\" is not a directory",
552 tmp_dst);
553 err = -1;
554 goto out;
555 }
556
Darren Tuckercdf547a2004-05-24 10:12:19 +1000557 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100558 if (!is_reg(g.gl_pathv[i])) {
559 error("skipping non-regular file %s",
560 g.gl_pathv[i]);
561 continue;
562 }
563 if (infer_path(g.gl_pathv[i], &tmp)) {
564 err = -1;
565 goto out;
566 }
567
568 if (g.gl_matchc == 1 && tmp_dst) {
569 /* If directory specified, append filename */
570 if (remote_is_dir(conn, tmp_dst)) {
571 if (infer_path(g.gl_pathv[0], &tmp)) {
572 err = 1;
573 goto out;
574 }
575 abs_dst = path_append(tmp_dst, tmp);
576 xfree(tmp);
577 } else
578 abs_dst = xstrdup(tmp_dst);
579
580 } else if (tmp_dst) {
581 abs_dst = path_append(tmp_dst, tmp);
582 xfree(tmp);
583 } else
584 abs_dst = make_absolute(tmp, pwd);
585
586 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
587 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
588 err = -1;
589 }
590
591out:
592 if (abs_dst)
593 xfree(abs_dst);
594 if (tmp_dst)
595 xfree(tmp_dst);
596 globfree(&g);
597 return(err);
598}
599
600static int
601sdirent_comp(const void *aa, const void *bb)
602{
603 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
604 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000605 int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100606
Darren Tuckerb9123452004-06-22 13:06:45 +1000607#define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000608 if (sort_flag & LS_NAME_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000609 return (rmul * strcmp(a->filename, b->filename));
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000610 else if (sort_flag & LS_TIME_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000611 return (rmul * NCMP(a->a.mtime, b->a.mtime));
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000612 else if (sort_flag & LS_SIZE_SORT)
Darren Tuckerb9123452004-06-22 13:06:45 +1000613 return (rmul * NCMP(a->a.size, b->a.size));
614
615 fatal("Unknown ls sort type");
Damien Miller20e1fab2004-02-18 14:30:55 +1100616}
617
618/* sftp ls.1 replacement for directories */
619static int
620do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
621{
Damien Millereccb9de2005-06-17 12:59:34 +1000622 int n;
623 u_int c = 1, colspace = 0, columns = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +1100624 SFTP_DIRENT **d;
625
626 if ((n = do_readdir(conn, path, &d)) != 0)
627 return (n);
628
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000629 if (!(lflag & LS_SHORT_VIEW)) {
Damien Millereccb9de2005-06-17 12:59:34 +1000630 u_int m = 0, width = 80;
Damien Miller20e1fab2004-02-18 14:30:55 +1100631 struct winsize ws;
632 char *tmp;
633
634 /* Count entries for sort and find longest filename */
Darren Tucker9a526452004-06-22 13:09:55 +1000635 for (n = 0; d[n] != NULL; n++) {
636 if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
637 m = MAX(m, strlen(d[n]->filename));
638 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100639
640 /* Add any subpath that also needs to be counted */
641 tmp = path_strip(path, strip_path);
642 m += strlen(tmp);
643 xfree(tmp);
644
645 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
646 width = ws.ws_col;
647
648 columns = width / (m + 2);
649 columns = MAX(columns, 1);
650 colspace = width / columns;
651 colspace = MIN(colspace, width);
652 }
653
Darren Tuckerb9123452004-06-22 13:06:45 +1000654 if (lflag & SORT_FLAGS) {
Damien Miller653b93b2005-11-05 15:15:23 +1100655 for (n = 0; d[n] != NULL; n++)
656 ; /* count entries */
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000657 sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
Darren Tuckerb9123452004-06-22 13:06:45 +1000658 qsort(d, n, sizeof(*d), sdirent_comp);
659 }
Damien Miller20e1fab2004-02-18 14:30:55 +1100660
Darren Tuckercdf547a2004-05-24 10:12:19 +1000661 for (n = 0; d[n] != NULL && !interrupted; n++) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100662 char *tmp, *fname;
663
Darren Tucker9a526452004-06-22 13:09:55 +1000664 if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
665 continue;
666
Damien Miller20e1fab2004-02-18 14:30:55 +1100667 tmp = path_append(path, d[n]->filename);
668 fname = path_strip(tmp, strip_path);
669 xfree(tmp);
670
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000671 if (lflag & LS_LONG_VIEW) {
672 if (lflag & LS_NUMERIC_VIEW) {
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000673 char *lname;
674 struct stat sb;
Damien Miller20e1fab2004-02-18 14:30:55 +1100675
Darren Tuckerb215c5d2004-06-22 12:30:53 +1000676 memset(&sb, 0, sizeof(sb));
677 attrib_to_stat(&d[n]->a, &sb);
678 lname = ls_file(fname, &sb, 1);
679 printf("%s\n", lname);
680 xfree(lname);
681 } else
682 printf("%s\n", d[n]->longname);
Damien Miller20e1fab2004-02-18 14:30:55 +1100683 } else {
684 printf("%-*s", colspace, fname);
685 if (c >= columns) {
686 printf("\n");
687 c = 1;
688 } else
689 c++;
690 }
691
692 xfree(fname);
693 }
694
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000695 if (!(lflag & LS_LONG_VIEW) && (c != 1))
Damien Miller20e1fab2004-02-18 14:30:55 +1100696 printf("\n");
697
698 free_sftp_dirents(d);
699 return (0);
700}
701
702/* sftp ls.1 replacement which handles path globs */
703static int
704do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
705 int lflag)
706{
707 glob_t g;
Damien Millereccb9de2005-06-17 12:59:34 +1000708 u_int i, c = 1, colspace = 0, columns = 1;
Darren Tucker596dcfa2004-12-11 13:37:22 +1100709 Attrib *a = NULL;
Damien Miller20e1fab2004-02-18 14:30:55 +1100710
711 memset(&g, 0, sizeof(g));
712
713 if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
Darren Tucker596dcfa2004-12-11 13:37:22 +1100714 NULL, &g) || (g.gl_pathc && !g.gl_matchc)) {
715 if (g.gl_pathc)
716 globfree(&g);
Damien Miller20e1fab2004-02-18 14:30:55 +1100717 error("Can't ls: \"%s\" not found", path);
718 return (-1);
719 }
720
Darren Tuckercdf547a2004-05-24 10:12:19 +1000721 if (interrupted)
722 goto out;
723
Damien Miller20e1fab2004-02-18 14:30:55 +1100724 /*
Darren Tucker596dcfa2004-12-11 13:37:22 +1100725 * If the glob returns a single match and it is a directory,
726 * then just list its contents.
Damien Miller20e1fab2004-02-18 14:30:55 +1100727 */
Darren Tucker596dcfa2004-12-11 13:37:22 +1100728 if (g.gl_matchc == 1) {
729 if ((a = do_lstat(conn, g.gl_pathv[0], 1)) == NULL) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100730 globfree(&g);
731 return (-1);
732 }
733 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
734 S_ISDIR(a->perm)) {
Darren Tucker596dcfa2004-12-11 13:37:22 +1100735 int err;
736
737 err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
Damien Miller20e1fab2004-02-18 14:30:55 +1100738 globfree(&g);
Darren Tucker596dcfa2004-12-11 13:37:22 +1100739 return (err);
Damien Miller20e1fab2004-02-18 14:30:55 +1100740 }
741 }
742
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000743 if (!(lflag & LS_SHORT_VIEW)) {
Damien Millereccb9de2005-06-17 12:59:34 +1000744 u_int m = 0, width = 80;
Damien Miller20e1fab2004-02-18 14:30:55 +1100745 struct winsize ws;
746
747 /* Count entries for sort and find longest filename */
748 for (i = 0; g.gl_pathv[i]; i++)
749 m = MAX(m, strlen(g.gl_pathv[i]));
750
751 if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
752 width = ws.ws_col;
753
754 columns = width / (m + 2);
755 columns = MAX(columns, 1);
756 colspace = width / columns;
757 }
758
Darren Tucker596dcfa2004-12-11 13:37:22 +1100759 for (i = 0; g.gl_pathv[i] && !interrupted; i++, a = NULL) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100760 char *fname;
761
762 fname = path_strip(g.gl_pathv[i], strip_path);
763
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000764 if (lflag & LS_LONG_VIEW) {
Damien Miller20e1fab2004-02-18 14:30:55 +1100765 char *lname;
766 struct stat sb;
767
768 /*
769 * XXX: this is slow - 1 roundtrip per path
770 * A solution to this is to fork glob() and
771 * build a sftp specific version which keeps the
772 * attribs (which currently get thrown away)
773 * that the server returns as well as the filenames.
774 */
775 memset(&sb, 0, sizeof(sb));
Darren Tucker596dcfa2004-12-11 13:37:22 +1100776 if (a == NULL)
777 a = do_lstat(conn, g.gl_pathv[i], 1);
Damien Miller20e1fab2004-02-18 14:30:55 +1100778 if (a != NULL)
779 attrib_to_stat(a, &sb);
780 lname = ls_file(fname, &sb, 1);
781 printf("%s\n", lname);
782 xfree(lname);
783 } else {
784 printf("%-*s", colspace, fname);
785 if (c >= columns) {
786 printf("\n");
787 c = 1;
788 } else
789 c++;
790 }
791 xfree(fname);
792 }
793
Darren Tuckera4e9ffa2004-06-22 13:07:58 +1000794 if (!(lflag & LS_LONG_VIEW) && (c != 1))
Damien Miller20e1fab2004-02-18 14:30:55 +1100795 printf("\n");
796
Darren Tuckercdf547a2004-05-24 10:12:19 +1000797 out:
Damien Miller20e1fab2004-02-18 14:30:55 +1100798 if (g.gl_pathc)
799 globfree(&g);
800
801 return (0);
802}
803
Damien Miller1cbc2922007-10-26 14:27:45 +1000804/*
805 * Undo escaping of glob sequences in place. Used to undo extra escaping
806 * applied in makeargv() when the string is destined for a function that
807 * does not glob it.
808 */
809static void
810undo_glob_escape(char *s)
811{
812 size_t i, j;
813
814 for (i = j = 0;;) {
815 if (s[i] == '\0') {
816 s[j] = '\0';
817 return;
818 }
819 if (s[i] != '\\') {
820 s[j++] = s[i++];
821 continue;
822 }
823 /* s[i] == '\\' */
824 ++i;
825 switch (s[i]) {
826 case '?':
827 case '[':
828 case '*':
829 case '\\':
830 s[j++] = s[i++];
831 break;
832 case '\0':
833 s[j++] = '\\';
834 s[j] = '\0';
835 return;
836 default:
837 s[j++] = '\\';
838 s[j++] = s[i++];
839 break;
840 }
841 }
842}
843
844/*
845 * Split a string into an argument vector using sh(1)-style quoting,
846 * comment and escaping rules, but with some tweaks to handle glob(3)
847 * wildcards.
848 * Returns NULL on error or a NULL-terminated array of arguments.
849 */
850#define MAXARGS 128
851#define MAXARGLEN 8192
852static char **
853makeargv(const char *arg, int *argcp)
854{
855 int argc, quot;
856 size_t i, j;
857 static char argvs[MAXARGLEN];
858 static char *argv[MAXARGS + 1];
859 enum { MA_START, MA_SQUOTE, MA_DQUOTE, MA_UNQUOTED } state, q;
860
861 *argcp = argc = 0;
862 if (strlen(arg) > sizeof(argvs) - 1) {
863 args_too_longs:
864 error("string too long");
865 return NULL;
866 }
867 state = MA_START;
868 i = j = 0;
869 for (;;) {
870 if (isspace(arg[i])) {
871 if (state == MA_UNQUOTED) {
872 /* Terminate current argument */
873 argvs[j++] = '\0';
874 argc++;
875 state = MA_START;
876 } else if (state != MA_START)
877 argvs[j++] = arg[i];
878 } else if (arg[i] == '"' || arg[i] == '\'') {
879 q = arg[i] == '"' ? MA_DQUOTE : MA_SQUOTE;
880 if (state == MA_START) {
881 argv[argc] = argvs + j;
882 state = q;
883 } else if (state == MA_UNQUOTED)
884 state = q;
885 else if (state == q)
886 state = MA_UNQUOTED;
887 else
888 argvs[j++] = arg[i];
889 } else if (arg[i] == '\\') {
890 if (state == MA_SQUOTE || state == MA_DQUOTE) {
891 quot = state == MA_SQUOTE ? '\'' : '"';
892 /* Unescape quote we are in */
893 /* XXX support \n and friends? */
894 if (arg[i + 1] == quot) {
895 i++;
896 argvs[j++] = arg[i];
897 } else if (arg[i + 1] == '?' ||
898 arg[i + 1] == '[' || arg[i + 1] == '*') {
899 /*
900 * Special case for sftp: append
901 * double-escaped glob sequence -
902 * glob will undo one level of
903 * escaping. NB. string can grow here.
904 */
905 if (j >= sizeof(argvs) - 5)
906 goto args_too_longs;
907 argvs[j++] = '\\';
908 argvs[j++] = arg[i++];
909 argvs[j++] = '\\';
910 argvs[j++] = arg[i];
911 } else {
912 argvs[j++] = arg[i++];
913 argvs[j++] = arg[i];
914 }
915 } else {
916 if (state == MA_START) {
917 argv[argc] = argvs + j;
918 state = MA_UNQUOTED;
919 }
920 if (arg[i + 1] == '?' || arg[i + 1] == '[' ||
921 arg[i + 1] == '*' || arg[i + 1] == '\\') {
922 /*
923 * Special case for sftp: append
924 * escaped glob sequence -
925 * glob will undo one level of
926 * escaping.
927 */
928 argvs[j++] = arg[i++];
929 argvs[j++] = arg[i];
930 } else {
931 /* Unescape everything */
932 /* XXX support \n and friends? */
933 i++;
934 argvs[j++] = arg[i];
935 }
936 }
937 } else if (arg[i] == '#') {
938 if (state == MA_SQUOTE || state == MA_DQUOTE)
939 argvs[j++] = arg[i];
940 else
941 goto string_done;
942 } else if (arg[i] == '\0') {
943 if (state == MA_SQUOTE || state == MA_DQUOTE) {
944 error("Unterminated quoted argument");
945 return NULL;
946 }
947 string_done:
948 if (state == MA_UNQUOTED) {
949 argvs[j++] = '\0';
950 argc++;
951 }
952 break;
953 } else {
954 if (state == MA_START) {
955 argv[argc] = argvs + j;
956 state = MA_UNQUOTED;
957 }
958 if ((state == MA_SQUOTE || state == MA_DQUOTE) &&
959 (arg[i] == '?' || arg[i] == '[' || arg[i] == '*')) {
960 /*
961 * Special case for sftp: escape quoted
962 * glob(3) wildcards. NB. string can grow
963 * here.
964 */
965 if (j >= sizeof(argvs) - 3)
966 goto args_too_longs;
967 argvs[j++] = '\\';
968 argvs[j++] = arg[i];
969 } else
970 argvs[j++] = arg[i];
971 }
972 i++;
973 }
974 *argcp = argc;
975 return argv;
976}
977
Damien Miller20e1fab2004-02-18 14:30:55 +1100978static int
979parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
980 unsigned long *n_arg, char **path1, char **path2)
981{
982 const char *cmd, *cp = *cpp;
Damien Miller1cbc2922007-10-26 14:27:45 +1000983 char *cp2, **argv;
Damien Miller20e1fab2004-02-18 14:30:55 +1100984 int base = 0;
985 long l;
Damien Miller1cbc2922007-10-26 14:27:45 +1000986 int i, cmdnum, optidx, argc;
Damien Miller20e1fab2004-02-18 14:30:55 +1100987
988 /* Skip leading whitespace */
989 cp = cp + strspn(cp, WHITESPACE);
990
991 /* Ignore blank lines and lines which begin with comment '#' char */
992 if (*cp == '\0' || *cp == '#')
993 return (0);
994
995 /* Check for leading '-' (disable error processing) */
996 *iflag = 0;
997 if (*cp == '-') {
998 *iflag = 1;
999 cp++;
1000 }
1001
Damien Miller1cbc2922007-10-26 14:27:45 +10001002 if ((argv = makeargv(cp, &argc)) == NULL)
1003 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001004
Damien Miller1cbc2922007-10-26 14:27:45 +10001005 /* Figure out which command we have */
1006 for (i = 0; cmds[i].c != NULL; i++) {
1007 if (strcasecmp(cmds[i].c, argv[0]) == 0)
Damien Miller20e1fab2004-02-18 14:30:55 +11001008 break;
Damien Miller20e1fab2004-02-18 14:30:55 +11001009 }
1010 cmdnum = cmds[i].n;
1011 cmd = cmds[i].c;
1012
1013 /* Special case */
1014 if (*cp == '!') {
1015 cp++;
1016 cmdnum = I_SHELL;
1017 } else if (cmdnum == -1) {
1018 error("Invalid command.");
Damien Miller1cbc2922007-10-26 14:27:45 +10001019 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001020 }
1021
1022 /* Get arguments and parse flags */
1023 *lflag = *pflag = *n_arg = 0;
1024 *path1 = *path2 = NULL;
Damien Miller1cbc2922007-10-26 14:27:45 +10001025 optidx = 1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001026 switch (cmdnum) {
1027 case I_GET:
1028 case I_PUT:
Damien Miller1cbc2922007-10-26 14:27:45 +10001029 if ((optidx = parse_getput_flags(cmd, argv, argc, pflag)) == -1)
1030 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001031 /* Get first pathname (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001032 if (argc - optidx < 1) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001033 error("You must specify at least one path after a "
1034 "%s command.", cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001035 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001036 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001037 *path1 = xstrdup(argv[optidx]);
1038 /* Get second pathname (optional) */
1039 if (argc - optidx > 1) {
1040 *path2 = xstrdup(argv[optidx + 1]);
1041 /* Destination is not globbed */
1042 undo_glob_escape(*path2);
1043 }
Damien Miller20e1fab2004-02-18 14:30:55 +11001044 break;
1045 case I_RENAME:
1046 case I_SYMLINK:
Damien Miller1cbc2922007-10-26 14:27:45 +10001047 if (argc - optidx < 2) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001048 error("You must specify two paths after a %s "
1049 "command.", cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001050 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001051 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001052 *path1 = xstrdup(argv[optidx]);
1053 *path2 = xstrdup(argv[optidx + 1]);
1054 /* Paths are not globbed */
1055 undo_glob_escape(*path1);
1056 undo_glob_escape(*path2);
Damien Miller20e1fab2004-02-18 14:30:55 +11001057 break;
1058 case I_RM:
1059 case I_MKDIR:
1060 case I_RMDIR:
1061 case I_CHDIR:
1062 case I_LCHDIR:
1063 case I_LMKDIR:
1064 /* Get pathname (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001065 if (argc - optidx < 1) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001066 error("You must specify a path after a %s command.",
1067 cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001068 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001069 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001070 *path1 = xstrdup(argv[optidx]);
1071 /* Only "rm" globs */
1072 if (cmdnum != I_RM)
1073 undo_glob_escape(*path1);
Damien Miller20e1fab2004-02-18 14:30:55 +11001074 break;
1075 case I_LS:
Damien Miller1cbc2922007-10-26 14:27:45 +10001076 if ((optidx = parse_ls_flags(argv, argc, lflag)) == -1)
Damien Miller20e1fab2004-02-18 14:30:55 +11001077 return(-1);
1078 /* Path is optional */
Damien Miller1cbc2922007-10-26 14:27:45 +10001079 if (argc - optidx > 0)
1080 *path1 = xstrdup(argv[optidx]);
Damien Miller20e1fab2004-02-18 14:30:55 +11001081 break;
1082 case I_LLS:
Darren Tucker88b976f2007-12-29 02:40:43 +11001083 /* Skip ls command and following whitespace */
1084 cp = cp + strlen(cmd) + strspn(cp, WHITESPACE);
Damien Miller20e1fab2004-02-18 14:30:55 +11001085 case I_SHELL:
1086 /* Uses the rest of the line */
1087 break;
1088 case I_LUMASK:
Damien Miller20e1fab2004-02-18 14:30:55 +11001089 case I_CHMOD:
1090 base = 8;
1091 case I_CHOWN:
1092 case I_CHGRP:
1093 /* Get numeric arg (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001094 if (argc - optidx < 1)
1095 goto need_num_arg;
Damien Millere7658a52006-10-24 03:00:12 +10001096 errno = 0;
Damien Miller1cbc2922007-10-26 14:27:45 +10001097 l = strtol(argv[optidx], &cp2, base);
1098 if (cp2 == argv[optidx] || *cp2 != '\0' ||
1099 ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
1100 l < 0) {
1101 need_num_arg:
Damien Miller20e1fab2004-02-18 14:30:55 +11001102 error("You must supply a numeric argument "
1103 "to the %s command.", cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001104 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001105 }
Damien Miller20e1fab2004-02-18 14:30:55 +11001106 *n_arg = l;
Damien Miller1cbc2922007-10-26 14:27:45 +10001107 if (cmdnum == I_LUMASK)
Damien Miller20e1fab2004-02-18 14:30:55 +11001108 break;
Damien Miller20e1fab2004-02-18 14:30:55 +11001109 /* Get pathname (mandatory) */
Damien Miller1cbc2922007-10-26 14:27:45 +10001110 if (argc - optidx < 2) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001111 error("You must specify a path after a %s command.",
1112 cmd);
Damien Miller1cbc2922007-10-26 14:27:45 +10001113 return -1;
Damien Miller20e1fab2004-02-18 14:30:55 +11001114 }
Damien Miller1cbc2922007-10-26 14:27:45 +10001115 *path1 = xstrdup(argv[optidx + 1]);
Damien Miller20e1fab2004-02-18 14:30:55 +11001116 break;
1117 case I_QUIT:
1118 case I_PWD:
1119 case I_LPWD:
1120 case I_HELP:
1121 case I_VERSION:
1122 case I_PROGRESS:
1123 break;
1124 default:
1125 fatal("Command not implemented");
1126 }
1127
1128 *cpp = cp;
1129 return(cmdnum);
1130}
1131
1132static int
1133parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
1134 int err_abort)
1135{
1136 char *path1, *path2, *tmp;
1137 int pflag, lflag, iflag, cmdnum, i;
1138 unsigned long n_arg;
1139 Attrib a, *aa;
1140 char path_buf[MAXPATHLEN];
1141 int err = 0;
1142 glob_t g;
1143
1144 path1 = path2 = NULL;
1145 cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
1146 &path1, &path2);
1147
1148 if (iflag != 0)
1149 err_abort = 0;
1150
1151 memset(&g, 0, sizeof(g));
1152
1153 /* Perform command */
1154 switch (cmdnum) {
1155 case 0:
1156 /* Blank line */
1157 break;
1158 case -1:
1159 /* Unrecognized command */
1160 err = -1;
1161 break;
1162 case I_GET:
1163 err = process_get(conn, path1, path2, *pwd, pflag);
1164 break;
1165 case I_PUT:
1166 err = process_put(conn, path1, path2, *pwd, pflag);
1167 break;
1168 case I_RENAME:
1169 path1 = make_absolute(path1, *pwd);
1170 path2 = make_absolute(path2, *pwd);
1171 err = do_rename(conn, path1, path2);
1172 break;
1173 case I_SYMLINK:
1174 path2 = make_absolute(path2, *pwd);
1175 err = do_symlink(conn, path1, path2);
1176 break;
1177 case I_RM:
1178 path1 = make_absolute(path1, *pwd);
1179 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001180 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001181 printf("Removing %s\n", g.gl_pathv[i]);
1182 err = do_rm(conn, g.gl_pathv[i]);
1183 if (err != 0 && err_abort)
1184 break;
1185 }
1186 break;
1187 case I_MKDIR:
1188 path1 = make_absolute(path1, *pwd);
1189 attrib_clear(&a);
1190 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1191 a.perm = 0777;
1192 err = do_mkdir(conn, path1, &a);
1193 break;
1194 case I_RMDIR:
1195 path1 = make_absolute(path1, *pwd);
1196 err = do_rmdir(conn, path1);
1197 break;
1198 case I_CHDIR:
1199 path1 = make_absolute(path1, *pwd);
1200 if ((tmp = do_realpath(conn, path1)) == NULL) {
1201 err = 1;
1202 break;
1203 }
1204 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1205 xfree(tmp);
1206 err = 1;
1207 break;
1208 }
1209 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
1210 error("Can't change directory: Can't check target");
1211 xfree(tmp);
1212 err = 1;
1213 break;
1214 }
1215 if (!S_ISDIR(aa->perm)) {
1216 error("Can't change directory: \"%s\" is not "
1217 "a directory", tmp);
1218 xfree(tmp);
1219 err = 1;
1220 break;
1221 }
1222 xfree(*pwd);
1223 *pwd = tmp;
1224 break;
1225 case I_LS:
1226 if (!path1) {
1227 do_globbed_ls(conn, *pwd, *pwd, lflag);
1228 break;
1229 }
1230
1231 /* Strip pwd off beginning of non-absolute paths */
1232 tmp = NULL;
1233 if (*path1 != '/')
1234 tmp = *pwd;
1235
1236 path1 = make_absolute(path1, *pwd);
1237 err = do_globbed_ls(conn, path1, tmp, lflag);
1238 break;
1239 case I_LCHDIR:
1240 if (chdir(path1) == -1) {
1241 error("Couldn't change local directory to "
1242 "\"%s\": %s", path1, strerror(errno));
1243 err = 1;
1244 }
1245 break;
1246 case I_LMKDIR:
1247 if (mkdir(path1, 0777) == -1) {
1248 error("Couldn't create local directory "
1249 "\"%s\": %s", path1, strerror(errno));
1250 err = 1;
1251 }
1252 break;
1253 case I_LLS:
1254 local_do_ls(cmd);
1255 break;
1256 case I_SHELL:
1257 local_do_shell(cmd);
1258 break;
1259 case I_LUMASK:
1260 umask(n_arg);
1261 printf("Local umask: %03lo\n", n_arg);
1262 break;
1263 case I_CHMOD:
1264 path1 = make_absolute(path1, *pwd);
1265 attrib_clear(&a);
1266 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1267 a.perm = n_arg;
1268 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001269 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001270 printf("Changing mode on %s\n", g.gl_pathv[i]);
1271 err = do_setstat(conn, g.gl_pathv[i], &a);
1272 if (err != 0 && err_abort)
1273 break;
1274 }
1275 break;
1276 case I_CHOWN:
1277 case I_CHGRP:
1278 path1 = make_absolute(path1, *pwd);
1279 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001280 for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
Damien Miller20e1fab2004-02-18 14:30:55 +11001281 if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
1282 if (err != 0 && err_abort)
1283 break;
1284 else
1285 continue;
1286 }
1287 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1288 error("Can't get current ownership of "
1289 "remote file \"%s\"", g.gl_pathv[i]);
1290 if (err != 0 && err_abort)
1291 break;
1292 else
1293 continue;
1294 }
1295 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1296 if (cmdnum == I_CHOWN) {
1297 printf("Changing owner on %s\n", g.gl_pathv[i]);
1298 aa->uid = n_arg;
1299 } else {
1300 printf("Changing group on %s\n", g.gl_pathv[i]);
1301 aa->gid = n_arg;
1302 }
1303 err = do_setstat(conn, g.gl_pathv[i], aa);
1304 if (err != 0 && err_abort)
1305 break;
1306 }
1307 break;
1308 case I_PWD:
1309 printf("Remote working directory: %s\n", *pwd);
1310 break;
1311 case I_LPWD:
1312 if (!getcwd(path_buf, sizeof(path_buf))) {
1313 error("Couldn't get local cwd: %s", strerror(errno));
1314 err = -1;
1315 break;
1316 }
1317 printf("Local working directory: %s\n", path_buf);
1318 break;
1319 case I_QUIT:
1320 /* Processed below */
1321 break;
1322 case I_HELP:
1323 help();
1324 break;
1325 case I_VERSION:
1326 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1327 break;
1328 case I_PROGRESS:
1329 showprogress = !showprogress;
1330 if (showprogress)
1331 printf("Progress meter enabled\n");
1332 else
1333 printf("Progress meter disabled\n");
1334 break;
1335 default:
1336 fatal("%d is not implemented", cmdnum);
1337 }
1338
1339 if (g.gl_pathc)
1340 globfree(&g);
1341 if (path1)
1342 xfree(path1);
1343 if (path2)
1344 xfree(path2);
1345
1346 /* If an unignored error occurs in batch mode we should abort. */
1347 if (err_abort && err != 0)
1348 return (-1);
1349 else if (cmdnum == I_QUIT)
1350 return (1);
1351
1352 return (0);
1353}
1354
Darren Tucker2d963d82004-11-07 20:04:10 +11001355#ifdef USE_LIBEDIT
1356static char *
1357prompt(EditLine *el)
1358{
1359 return ("sftp> ");
1360}
1361#endif
1362
Damien Miller20e1fab2004-02-18 14:30:55 +11001363int
1364interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1365{
1366 char *pwd;
1367 char *dir = NULL;
1368 char cmd[2048];
1369 struct sftp_conn *conn;
Damien Miller0e2c1022005-08-12 22:16:22 +10001370 int err, interactive;
Darren Tucker2d963d82004-11-07 20:04:10 +11001371 EditLine *el = NULL;
1372#ifdef USE_LIBEDIT
1373 History *hl = NULL;
1374 HistEvent hev;
1375 extern char *__progname;
1376
1377 if (!batchmode && isatty(STDIN_FILENO)) {
1378 if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL)
1379 fatal("Couldn't initialise editline");
1380 if ((hl = history_init()) == NULL)
1381 fatal("Couldn't initialise editline history");
1382 history(hl, &hev, H_SETSIZE, 100);
1383 el_set(el, EL_HIST, history, hl);
1384
1385 el_set(el, EL_PROMPT, prompt);
1386 el_set(el, EL_EDITOR, "emacs");
1387 el_set(el, EL_TERMINAL, NULL);
1388 el_set(el, EL_SIGNAL, 1);
1389 el_source(el, NULL);
1390 }
1391#endif /* USE_LIBEDIT */
Damien Miller20e1fab2004-02-18 14:30:55 +11001392
1393 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
1394 if (conn == NULL)
1395 fatal("Couldn't initialise connection to server");
1396
1397 pwd = do_realpath(conn, ".");
1398 if (pwd == NULL)
1399 fatal("Need cwd");
1400
1401 if (file1 != NULL) {
1402 dir = xstrdup(file1);
1403 dir = make_absolute(dir, pwd);
1404
1405 if (remote_is_dir(conn, dir) && file2 == NULL) {
1406 printf("Changing to: %s\n", dir);
1407 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
Darren Tuckercd516ef2004-12-06 22:43:43 +11001408 if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0) {
1409 xfree(dir);
1410 xfree(pwd);
Damien Millere0b90a62006-03-26 13:51:44 +11001411 xfree(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001412 return (-1);
Darren Tuckercd516ef2004-12-06 22:43:43 +11001413 }
Damien Miller20e1fab2004-02-18 14:30:55 +11001414 } else {
1415 if (file2 == NULL)
1416 snprintf(cmd, sizeof cmd, "get %s", dir);
1417 else
1418 snprintf(cmd, sizeof cmd, "get %s %s", dir,
1419 file2);
1420
1421 err = parse_dispatch_command(conn, cmd, &pwd, 1);
1422 xfree(dir);
1423 xfree(pwd);
Damien Millere0b90a62006-03-26 13:51:44 +11001424 xfree(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001425 return (err);
1426 }
1427 xfree(dir);
1428 }
1429
Darren Tucker93e7e8f2005-08-23 08:06:55 +10001430#if defined(HAVE_SETVBUF) && !defined(BROKEN_SETVBUF)
Damien Miller20e1fab2004-02-18 14:30:55 +11001431 setvbuf(stdout, NULL, _IOLBF, 0);
1432 setvbuf(infile, NULL, _IOLBF, 0);
1433#else
Damien Miller37294fb2005-07-17 17:18:49 +10001434 setlinebuf(stdout);
1435 setlinebuf(infile);
Damien Miller20e1fab2004-02-18 14:30:55 +11001436#endif
1437
Damien Miller0e2c1022005-08-12 22:16:22 +10001438 interactive = !batchmode && isatty(STDIN_FILENO);
Damien Miller20e1fab2004-02-18 14:30:55 +11001439 err = 0;
1440 for (;;) {
1441 char *cp;
1442
Darren Tuckercdf547a2004-05-24 10:12:19 +10001443 signal(SIGINT, SIG_IGN);
1444
Darren Tucker2d963d82004-11-07 20:04:10 +11001445 if (el == NULL) {
Damien Miller0e2c1022005-08-12 22:16:22 +10001446 if (interactive)
1447 printf("sftp> ");
Darren Tucker2d963d82004-11-07 20:04:10 +11001448 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller0e2c1022005-08-12 22:16:22 +10001449 if (interactive)
1450 printf("\n");
Darren Tucker2d963d82004-11-07 20:04:10 +11001451 break;
1452 }
Damien Miller0e2c1022005-08-12 22:16:22 +10001453 if (!interactive) { /* Echo command */
1454 printf("sftp> %s", cmd);
1455 if (strlen(cmd) > 0 &&
1456 cmd[strlen(cmd) - 1] != '\n')
1457 printf("\n");
1458 }
Darren Tucker2d963d82004-11-07 20:04:10 +11001459 } else {
1460#ifdef USE_LIBEDIT
1461 const char *line;
1462 int count = 0;
Damien Miller20e1fab2004-02-18 14:30:55 +11001463
Damien Miller0e2c1022005-08-12 22:16:22 +10001464 if ((line = el_gets(el, &count)) == NULL || count <= 0) {
1465 printf("\n");
1466 break;
1467 }
Darren Tucker2d963d82004-11-07 20:04:10 +11001468 history(hl, &hev, H_ENTER, line);
1469 if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
1470 fprintf(stderr, "Error: input line too long\n");
1471 continue;
1472 }
1473#endif /* USE_LIBEDIT */
Damien Miller20e1fab2004-02-18 14:30:55 +11001474 }
1475
Damien Miller20e1fab2004-02-18 14:30:55 +11001476 cp = strrchr(cmd, '\n');
1477 if (cp)
1478 *cp = '\0';
1479
Darren Tuckercdf547a2004-05-24 10:12:19 +10001480 /* Handle user interrupts gracefully during commands */
1481 interrupted = 0;
1482 signal(SIGINT, cmd_interrupt);
1483
Damien Miller20e1fab2004-02-18 14:30:55 +11001484 err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
1485 if (err != 0)
1486 break;
1487 }
1488 xfree(pwd);
Damien Millere0b90a62006-03-26 13:51:44 +11001489 xfree(conn);
Damien Miller20e1fab2004-02-18 14:30:55 +11001490
Tim Rice027e8b12005-08-15 14:52:50 -07001491#ifdef USE_LIBEDIT
Damien Miller0e2c1022005-08-12 22:16:22 +10001492 if (el != NULL)
1493 el_end(el);
Tim Rice027e8b12005-08-15 14:52:50 -07001494#endif /* USE_LIBEDIT */
Damien Miller0e2c1022005-08-12 22:16:22 +10001495
Damien Miller20e1fab2004-02-18 14:30:55 +11001496 /* err == 1 signifies normal "quit" exit */
1497 return (err >= 0 ? 0 : -1);
1498}
Damien Miller62d57f62003-01-10 21:43:24 +11001499
Ben Lindstrombba81212001-06-25 05:01:22 +00001500static void
Damien Millercc685c12003-06-04 22:51:38 +10001501connect_to_server(char *path, char **args, int *in, int *out)
Damien Miller33804262001-02-04 23:20:18 +11001502{
1503 int c_in, c_out;
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001504
Damien Miller33804262001-02-04 23:20:18 +11001505#ifdef USE_PIPES
1506 int pin[2], pout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001507
Damien Miller33804262001-02-04 23:20:18 +11001508 if ((pipe(pin) == -1) || (pipe(pout) == -1))
1509 fatal("pipe: %s", strerror(errno));
1510 *in = pin[0];
1511 *out = pout[1];
1512 c_in = pout[0];
1513 c_out = pin[1];
1514#else /* USE_PIPES */
1515 int inout[2];
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001516
Damien Miller33804262001-02-04 23:20:18 +11001517 if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
1518 fatal("socketpair: %s", strerror(errno));
1519 *in = *out = inout[0];
1520 c_in = c_out = inout[1];
1521#endif /* USE_PIPES */
1522
Damien Millercc685c12003-06-04 22:51:38 +10001523 if ((sshpid = fork()) == -1)
Damien Miller33804262001-02-04 23:20:18 +11001524 fatal("fork: %s", strerror(errno));
Damien Millercc685c12003-06-04 22:51:38 +10001525 else if (sshpid == 0) {
Damien Miller33804262001-02-04 23:20:18 +11001526 if ((dup2(c_in, STDIN_FILENO) == -1) ||
1527 (dup2(c_out, STDOUT_FILENO) == -1)) {
1528 fprintf(stderr, "dup2: %s\n", strerror(errno));
Damien Miller350327c2004-06-15 10:24:13 +10001529 _exit(1);
Damien Miller33804262001-02-04 23:20:18 +11001530 }
1531 close(*in);
1532 close(*out);
1533 close(c_in);
1534 close(c_out);
Darren Tuckercdf547a2004-05-24 10:12:19 +10001535
1536 /*
1537 * The underlying ssh is in the same process group, so we must
Darren Tuckerfc959702004-07-17 16:12:08 +10001538 * ignore SIGINT if we want to gracefully abort commands,
1539 * otherwise the signal will make it to the ssh process and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001540 * kill it too
1541 */
1542 signal(SIGINT, SIG_IGN);
Darren Tuckerbd12f172004-06-18 16:23:43 +10001543 execvp(path, args);
Damien Millerd14ee1e2002-02-05 12:27:31 +11001544 fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
Damien Miller350327c2004-06-15 10:24:13 +10001545 _exit(1);
Damien Miller33804262001-02-04 23:20:18 +11001546 }
1547
Damien Millercc685c12003-06-04 22:51:38 +10001548 signal(SIGTERM, killchild);
1549 signal(SIGINT, killchild);
1550 signal(SIGHUP, killchild);
Damien Miller33804262001-02-04 23:20:18 +11001551 close(c_in);
1552 close(c_out);
1553}
1554
Ben Lindstrombba81212001-06-25 05:01:22 +00001555static void
Damien Miller33804262001-02-04 23:20:18 +11001556usage(void)
1557{
Damien Miller025e01c2002-02-08 22:06:29 +11001558 extern char *__progname;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001559
Ben Lindstrom1e243242001-09-18 05:38:44 +00001560 fprintf(stderr,
Darren Tucker1f203942003-10-15 15:50:42 +10001561 "usage: %s [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]\n"
1562 " [-o ssh_option] [-P sftp_server_path] [-R num_requests]\n"
1563 " [-S program] [-s subsystem | sftp_server] host\n"
1564 " %s [[user@]host[:file [file]]]\n"
1565 " %s [[user@]host[:dir[/]]]\n"
1566 " %s -b batchfile [user@]host\n", __progname, __progname, __progname, __progname);
Damien Miller33804262001-02-04 23:20:18 +11001567 exit(1);
1568}
1569
Kevin Stevesef4eea92001-02-05 12:42:17 +00001570int
Damien Miller33804262001-02-04 23:20:18 +11001571main(int argc, char **argv)
1572{
Damien Miller956f3fb2003-01-10 21:40:00 +11001573 int in, out, ch, err;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001574 char *host, *userhost, *cp, *file2 = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +00001575 int debug_level = 0, sshver = 2;
1576 char *file1 = NULL, *sftp_server = NULL;
Damien Millerd14ee1e2002-02-05 12:27:31 +11001577 char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +00001578 LogLevel ll = SYSLOG_LEVEL_INFO;
1579 arglist args;
Damien Millerd7686fd2001-02-10 00:40:03 +11001580 extern int optind;
1581 extern char *optarg;
Damien Miller33804262001-02-04 23:20:18 +11001582
Darren Tuckerce321d82005-10-03 18:11:24 +10001583 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1584 sanitise_stdfd();
1585
Damien Miller59d3d5b2003-08-22 09:34:41 +10001586 __progname = ssh_get_progname(argv[0]);
Damien Miller3eec6b72006-01-31 21:49:27 +11001587 memset(&args, '\0', sizeof(args));
Ben Lindstrom387c4722001-05-08 20:27:25 +00001588 args.list = NULL;
Damien Miller2b5a0de2006-03-31 23:10:31 +11001589 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +00001590 addargs(&args, "-oForwardX11 no");
1591 addargs(&args, "-oForwardAgent no");
Damien Millerd27b9472005-12-13 19:29:02 +11001592 addargs(&args, "-oPermitLocalCommand no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +00001593 addargs(&args, "-oClearAllForwardings yes");
Damien Millere4f5a822004-01-21 14:11:05 +11001594
Ben Lindstrom387c4722001-05-08 20:27:25 +00001595 ll = SYSLOG_LEVEL_INFO;
Damien Millere4f5a822004-01-21 14:11:05 +11001596 infile = stdin;
Damien Millerd7686fd2001-02-10 00:40:03 +11001597
Damien Miller16a13332002-02-13 14:03:56 +11001598 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:R:")) != -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +11001599 switch (ch) {
1600 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +00001601 addargs(&args, "-C");
Damien Millerd7686fd2001-02-10 00:40:03 +11001602 break;
1603 case 'v':
Ben Lindstrom387c4722001-05-08 20:27:25 +00001604 if (debug_level < 3) {
1605 addargs(&args, "-v");
1606 ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
1607 }
1608 debug_level++;
Damien Millerd7686fd2001-02-10 00:40:03 +11001609 break;
Ben Lindstrom1e243242001-09-18 05:38:44 +00001610 case 'F':
Damien Millerd7686fd2001-02-10 00:40:03 +11001611 case 'o':
Ben Lindstrom1e243242001-09-18 05:38:44 +00001612 addargs(&args, "-%c%s", ch, optarg);
Damien Millerd7686fd2001-02-10 00:40:03 +11001613 break;
1614 case '1':
Ben Lindstrom387c4722001-05-08 20:27:25 +00001615 sshver = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +11001616 if (sftp_server == NULL)
1617 sftp_server = _PATH_SFTP_SERVER;
1618 break;
1619 case 's':
1620 sftp_server = optarg;
1621 break;
1622 case 'S':
1623 ssh_program = optarg;
Damien Miller3eec6b72006-01-31 21:49:27 +11001624 replacearg(&args, 0, "%s", ssh_program);
Damien Millerd7686fd2001-02-10 00:40:03 +11001625 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001626 case 'b':
Damien Miller44f75c12004-01-21 10:58:47 +11001627 if (batchmode)
1628 fatal("Batch file already specified.");
1629
1630 /* Allow "-" as stdin */
Darren Tuckerfc959702004-07-17 16:12:08 +10001631 if (strcmp(optarg, "-") != 0 &&
Damien Miller0dc1bef2005-07-17 17:22:45 +10001632 (infile = fopen(optarg, "r")) == NULL)
Damien Miller44f75c12004-01-21 10:58:47 +11001633 fatal("%s (%s).", strerror(errno), optarg);
Damien Miller62d57f62003-01-10 21:43:24 +11001634 showprogress = 0;
Damien Miller44f75c12004-01-21 10:58:47 +11001635 batchmode = 1;
Damien Miller64e8d442005-03-01 21:16:47 +11001636 addargs(&args, "-obatchmode yes");
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001637 break;
Damien Millerd14ee1e2002-02-05 12:27:31 +11001638 case 'P':
1639 sftp_direct = optarg;
1640 break;
Damien Miller8829d362002-02-08 22:04:05 +11001641 case 'B':
1642 copy_buffer_len = strtol(optarg, &cp, 10);
1643 if (copy_buffer_len == 0 || *cp != '\0')
1644 fatal("Invalid buffer size \"%s\"", optarg);
1645 break;
Damien Miller16a13332002-02-13 14:03:56 +11001646 case 'R':
1647 num_requests = strtol(optarg, &cp, 10);
1648 if (num_requests == 0 || *cp != '\0')
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001649 fatal("Invalid number of requests \"%s\"",
Damien Miller16a13332002-02-13 14:03:56 +11001650 optarg);
1651 break;
Damien Millerd7686fd2001-02-10 00:40:03 +11001652 case 'h':
1653 default:
Damien Miller33804262001-02-04 23:20:18 +11001654 usage();
1655 }
1656 }
1657
Damien Millerc0f27d82004-03-08 23:12:19 +11001658 if (!isatty(STDERR_FILENO))
1659 showprogress = 0;
1660
Ben Lindstrom2f3d52a2002-04-02 21:06:18 +00001661 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
1662
Damien Millerd14ee1e2002-02-05 12:27:31 +11001663 if (sftp_direct == NULL) {
1664 if (optind == argc || argc > (optind + 2))
1665 usage();
Damien Miller33804262001-02-04 23:20:18 +11001666
Damien Millerd14ee1e2002-02-05 12:27:31 +11001667 userhost = xstrdup(argv[optind]);
1668 file2 = argv[optind+1];
Ben Lindstrom63667f62001-04-13 00:00:14 +00001669
Ben Lindstromc276c122002-12-23 02:14:51 +00001670 if ((host = strrchr(userhost, '@')) == NULL)
Damien Millerd14ee1e2002-02-05 12:27:31 +11001671 host = userhost;
1672 else {
1673 *host++ = '\0';
1674 if (!userhost[0]) {
1675 fprintf(stderr, "Missing username\n");
1676 usage();
1677 }
Damien Miller80163902007-01-05 16:30:16 +11001678 addargs(&args, "-l%s", userhost);
Damien Millerd14ee1e2002-02-05 12:27:31 +11001679 }
1680
Damien Millerec692032004-01-27 21:22:00 +11001681 if ((cp = colon(host)) != NULL) {
1682 *cp++ = '\0';
1683 file1 = cp;
1684 }
1685
Damien Millerd14ee1e2002-02-05 12:27:31 +11001686 host = cleanhostname(host);
1687 if (!*host) {
1688 fprintf(stderr, "Missing hostname\n");
Damien Miller33804262001-02-04 23:20:18 +11001689 usage();
1690 }
Damien Millerd14ee1e2002-02-05 12:27:31 +11001691
Damien Millerd14ee1e2002-02-05 12:27:31 +11001692 addargs(&args, "-oProtocol %d", sshver);
1693
1694 /* no subsystem if the server-spec contains a '/' */
1695 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
1696 addargs(&args, "-s");
1697
1698 addargs(&args, "%s", host);
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001699 addargs(&args, "%s", (sftp_server != NULL ?
Damien Millerd14ee1e2002-02-05 12:27:31 +11001700 sftp_server : "sftp"));
Damien Millerd14ee1e2002-02-05 12:27:31 +11001701
Damien Miller44f75c12004-01-21 10:58:47 +11001702 if (!batchmode)
1703 fprintf(stderr, "Connecting to %s...\n", host);
Damien Millercc685c12003-06-04 22:51:38 +10001704 connect_to_server(ssh_program, args.list, &in, &out);
Damien Millerd14ee1e2002-02-05 12:27:31 +11001705 } else {
1706 args.list = NULL;
1707 addargs(&args, "sftp-server");
1708
Damien Miller44f75c12004-01-21 10:58:47 +11001709 if (!batchmode)
1710 fprintf(stderr, "Attaching to %s...\n", sftp_direct);
Damien Millercc685c12003-06-04 22:51:38 +10001711 connect_to_server(sftp_direct, args.list, &in, &out);
Damien Miller33804262001-02-04 23:20:18 +11001712 }
Damien Miller3eec6b72006-01-31 21:49:27 +11001713 freeargs(&args);
Damien Miller33804262001-02-04 23:20:18 +11001714
Damien Miller956f3fb2003-01-10 21:40:00 +11001715 err = interactive_loop(in, out, file1, file2);
Damien Miller33804262001-02-04 23:20:18 +11001716
Ben Lindstrom10b9bf92001-02-26 20:04:45 +00001717#if !defined(USE_PIPES)
Damien Miller37294fb2005-07-17 17:18:49 +10001718 shutdown(in, SHUT_RDWR);
1719 shutdown(out, SHUT_RDWR);
Ben Lindstrom10b9bf92001-02-26 20:04:45 +00001720#endif
1721
Damien Miller33804262001-02-04 23:20:18 +11001722 close(in);
1723 close(out);
Damien Miller44f75c12004-01-21 10:58:47 +11001724 if (batchmode)
Ben Lindstrom562c26b2001-03-07 01:26:48 +00001725 fclose(infile);
Damien Miller33804262001-02-04 23:20:18 +11001726
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001727 while (waitpid(sshpid, NULL, 0) == -1)
1728 if (errno != EINTR)
1729 fatal("Couldn't wait for ssh process: %s",
1730 strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +11001731
Damien Miller956f3fb2003-01-10 21:40:00 +11001732 exit(err == 0 ? 0 : 1);
Damien Miller33804262001-02-04 23:20:18 +11001733}