sftp.c revision 1.29 1 1.27 christos /* $NetBSD: sftp.c,v 1.29 2020/05/28 17:05:49 christos Exp $ */
2 1.29 christos /* $OpenBSD: sftp.c,v 1.200 2020/04/03 05:53:52 jmc Exp $ */
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2001-2004 Damien Miller <djm (at) openbsd.org>
5 1.1 christos *
6 1.1 christos * Permission to use, copy, modify, and distribute this software for any
7 1.1 christos * purpose with or without fee is hereby granted, provided that the above
8 1.1 christos * copyright notice and this permission notice appear in all copies.
9 1.1 christos *
10 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 christos */
18 1.1 christos
19 1.2 christos #include "includes.h"
20 1.27 christos __RCSID("$NetBSD: sftp.c,v 1.29 2020/05/28 17:05:49 christos Exp $");
21 1.20 christos
22 1.14 christos #include <sys/param.h> /* MIN MAX */
23 1.1 christos #include <sys/types.h>
24 1.1 christos #include <sys/ioctl.h>
25 1.1 christos #include <sys/wait.h>
26 1.1 christos #include <sys/stat.h>
27 1.1 christos #include <sys/socket.h>
28 1.1 christos #include <sys/statvfs.h>
29 1.1 christos
30 1.1 christos #include <ctype.h>
31 1.1 christos #include <errno.h>
32 1.1 christos #include <glob.h>
33 1.1 christos #include <histedit.h>
34 1.1 christos #include <paths.h>
35 1.4 adam #include <libgen.h>
36 1.12 christos #include <locale.h>
37 1.1 christos #include <signal.h>
38 1.19 christos #include <stdarg.h>
39 1.1 christos #include <stdlib.h>
40 1.1 christos #include <stdio.h>
41 1.1 christos #include <string.h>
42 1.1 christos #include <unistd.h>
43 1.14 christos #include <limits.h>
44 1.1 christos #include <util.h>
45 1.1 christos
46 1.1 christos #include "xmalloc.h"
47 1.1 christos #include "log.h"
48 1.1 christos #include "pathnames.h"
49 1.1 christos #include "misc.h"
50 1.19 christos #include "utf8.h"
51 1.1 christos
52 1.1 christos #include "sftp.h"
53 1.14 christos #include "ssherr.h"
54 1.14 christos #include "sshbuf.h"
55 1.1 christos #include "sftp-common.h"
56 1.1 christos #include "sftp-client.h"
57 1.2 christos #include "fmt_scaled.h"
58 1.1 christos
59 1.4 adam #define DEFAULT_COPY_BUFLEN 32768 /* Size of buffer for up/download */
60 1.4 adam #define DEFAULT_NUM_REQUESTS 256 /* # concurrent outstanding requests */
61 1.4 adam
62 1.1 christos /* File to read commands from */
63 1.1 christos FILE* infile;
64 1.1 christos
65 1.1 christos /* Are we in batchfile mode? */
66 1.1 christos int batchmode = 0;
67 1.1 christos
68 1.1 christos /* PID of ssh transport process */
69 1.24 christos static volatile pid_t sshpid = -1;
70 1.1 christos
71 1.12 christos /* Suppress diagnositic messages */
72 1.12 christos int quiet = 0;
73 1.12 christos
74 1.1 christos /* This is set to 0 if the progressmeter is not desired. */
75 1.1 christos int showprogress = 1;
76 1.1 christos
77 1.4 adam /* When this option is set, we always recursively download/upload directories */
78 1.4 adam int global_rflag = 0;
79 1.4 adam
80 1.13 christos /* When this option is set, we resume download or upload if possible */
81 1.12 christos int global_aflag = 0;
82 1.12 christos
83 1.4 adam /* When this option is set, the file transfers will always preserve times */
84 1.4 adam int global_pflag = 0;
85 1.4 adam
86 1.13 christos /* When this option is set, transfers will have fsync() called on each file */
87 1.13 christos int global_fflag = 0;
88 1.13 christos
89 1.1 christos /* SIGINT received during command processing */
90 1.1 christos volatile sig_atomic_t interrupted = 0;
91 1.1 christos
92 1.1 christos /* I wish qsort() took a separate ctx for the comparison function...*/
93 1.1 christos int sort_flag;
94 1.22 christos glob_t *sort_glob;
95 1.1 christos
96 1.4 adam /* Context used for commandline completion */
97 1.4 adam struct complete_ctx {
98 1.4 adam struct sftp_conn *conn;
99 1.4 adam char **remote_pathp;
100 1.4 adam };
101 1.4 adam
102 1.1 christos int remote_glob(struct sftp_conn *, const char *, int,
103 1.1 christos int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
104 1.1 christos
105 1.1 christos /* Separators for interactive commands */
106 1.1 christos #define WHITESPACE " \t\r\n"
107 1.1 christos
108 1.1 christos /* ls flags */
109 1.4 adam #define LS_LONG_VIEW 0x0001 /* Full view ala ls -l */
110 1.4 adam #define LS_SHORT_VIEW 0x0002 /* Single row view ala ls -1 */
111 1.4 adam #define LS_NUMERIC_VIEW 0x0004 /* Long view with numeric uid/gid */
112 1.4 adam #define LS_NAME_SORT 0x0008 /* Sort by name (default) */
113 1.4 adam #define LS_TIME_SORT 0x0010 /* Sort by mtime */
114 1.4 adam #define LS_SIZE_SORT 0x0020 /* Sort by file size */
115 1.4 adam #define LS_REVERSE_SORT 0x0040 /* Reverse sort order */
116 1.4 adam #define LS_SHOW_ALL 0x0080 /* Don't skip filenames starting with '.' */
117 1.4 adam #define LS_SI_UNITS 0x0100 /* Display sizes as K, M, G, etc. */
118 1.1 christos
119 1.4 adam #define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW|LS_SI_UNITS)
120 1.1 christos #define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
121 1.1 christos
122 1.1 christos /* Commands for interactive mode */
123 1.13 christos enum sftp_command {
124 1.13 christos I_CHDIR = 1,
125 1.13 christos I_CHGRP,
126 1.13 christos I_CHMOD,
127 1.13 christos I_CHOWN,
128 1.13 christos I_DF,
129 1.13 christos I_GET,
130 1.13 christos I_HELP,
131 1.13 christos I_LCHDIR,
132 1.13 christos I_LINK,
133 1.13 christos I_LLS,
134 1.13 christos I_LMKDIR,
135 1.13 christos I_LPWD,
136 1.13 christos I_LS,
137 1.13 christos I_LUMASK,
138 1.13 christos I_MKDIR,
139 1.13 christos I_PUT,
140 1.13 christos I_PWD,
141 1.13 christos I_QUIT,
142 1.13 christos I_REGET,
143 1.13 christos I_RENAME,
144 1.13 christos I_REPUT,
145 1.13 christos I_RM,
146 1.13 christos I_RMDIR,
147 1.13 christos I_SHELL,
148 1.13 christos I_SYMLINK,
149 1.13 christos I_VERSION,
150 1.13 christos I_PROGRESS,
151 1.13 christos };
152 1.1 christos
153 1.1 christos struct CMD {
154 1.1 christos const char *c;
155 1.1 christos const int n;
156 1.4 adam const int t;
157 1.1 christos };
158 1.1 christos
159 1.4 adam /* Type of completion */
160 1.4 adam #define NOARGS 0
161 1.4 adam #define REMOTE 1
162 1.4 adam #define LOCAL 2
163 1.4 adam
164 1.1 christos static const struct CMD cmds[] = {
165 1.4 adam { "bye", I_QUIT, NOARGS },
166 1.4 adam { "cd", I_CHDIR, REMOTE },
167 1.4 adam { "chdir", I_CHDIR, REMOTE },
168 1.4 adam { "chgrp", I_CHGRP, REMOTE },
169 1.4 adam { "chmod", I_CHMOD, REMOTE },
170 1.4 adam { "chown", I_CHOWN, REMOTE },
171 1.4 adam { "df", I_DF, REMOTE },
172 1.4 adam { "dir", I_LS, REMOTE },
173 1.4 adam { "exit", I_QUIT, NOARGS },
174 1.4 adam { "get", I_GET, REMOTE },
175 1.4 adam { "help", I_HELP, NOARGS },
176 1.4 adam { "lcd", I_LCHDIR, LOCAL },
177 1.4 adam { "lchdir", I_LCHDIR, LOCAL },
178 1.4 adam { "lls", I_LLS, LOCAL },
179 1.4 adam { "lmkdir", I_LMKDIR, LOCAL },
180 1.7 christos { "ln", I_LINK, REMOTE },
181 1.4 adam { "lpwd", I_LPWD, LOCAL },
182 1.4 adam { "ls", I_LS, REMOTE },
183 1.4 adam { "lumask", I_LUMASK, NOARGS },
184 1.4 adam { "mkdir", I_MKDIR, REMOTE },
185 1.4 adam { "mget", I_GET, REMOTE },
186 1.4 adam { "mput", I_PUT, LOCAL },
187 1.4 adam { "progress", I_PROGRESS, NOARGS },
188 1.4 adam { "put", I_PUT, LOCAL },
189 1.4 adam { "pwd", I_PWD, REMOTE },
190 1.4 adam { "quit", I_QUIT, NOARGS },
191 1.12 christos { "reget", I_REGET, REMOTE },
192 1.4 adam { "rename", I_RENAME, REMOTE },
193 1.14 christos { "reput", I_REPUT, LOCAL },
194 1.4 adam { "rm", I_RM, REMOTE },
195 1.4 adam { "rmdir", I_RMDIR, REMOTE },
196 1.4 adam { "symlink", I_SYMLINK, REMOTE },
197 1.4 adam { "version", I_VERSION, NOARGS },
198 1.4 adam { "!", I_SHELL, NOARGS },
199 1.4 adam { "?", I_HELP, NOARGS },
200 1.4 adam { NULL, -1, -1 }
201 1.1 christos };
202 1.1 christos
203 1.1 christos /* ARGSUSED */
204 1.8 joerg __dead static void
205 1.1 christos killchild(int signo)
206 1.1 christos {
207 1.28 christos pid_t pid;
208 1.28 christos
209 1.28 christos pid = sshpid;
210 1.28 christos if (pid > 1) {
211 1.28 christos kill(pid, SIGTERM);
212 1.28 christos waitpid(pid, NULL, 0);
213 1.1 christos }
214 1.1 christos
215 1.1 christos _exit(1);
216 1.1 christos }
217 1.1 christos
218 1.1 christos /* ARGSUSED */
219 1.1 christos static void
220 1.20 christos suspchild(int signo)
221 1.20 christos {
222 1.20 christos if (sshpid > 1) {
223 1.20 christos kill(sshpid, signo);
224 1.20 christos while (waitpid(sshpid, NULL, WUNTRACED) == -1 && errno == EINTR)
225 1.20 christos continue;
226 1.20 christos }
227 1.20 christos kill(getpid(), SIGSTOP);
228 1.20 christos }
229 1.20 christos
230 1.20 christos /* ARGSUSED */
231 1.20 christos static void
232 1.1 christos cmd_interrupt(int signo)
233 1.1 christos {
234 1.1 christos const char msg[] = "\rInterrupt \n";
235 1.1 christos int olderrno = errno;
236 1.1 christos
237 1.12 christos (void)write(STDERR_FILENO, msg, sizeof(msg) - 1);
238 1.1 christos interrupted = 1;
239 1.1 christos errno = olderrno;
240 1.1 christos }
241 1.1 christos
242 1.24 christos /*ARGSUSED*/
243 1.24 christos static void
244 1.24 christos sigchld_handler(int sig)
245 1.24 christos {
246 1.24 christos int save_errno = errno;
247 1.24 christos pid_t pid;
248 1.24 christos const char msg[] = "\rConnection closed. \n";
249 1.24 christos
250 1.24 christos /* Report if ssh transport process dies. */
251 1.24 christos while ((pid = waitpid(sshpid, NULL, WNOHANG)) == -1 && errno == EINTR)
252 1.24 christos continue;
253 1.24 christos if (pid == sshpid) {
254 1.24 christos (void)write(STDERR_FILENO, msg, sizeof(msg) - 1);
255 1.24 christos sshpid = -1;
256 1.24 christos }
257 1.24 christos
258 1.24 christos errno = save_errno;
259 1.24 christos }
260 1.24 christos
261 1.1 christos static void
262 1.1 christos help(void)
263 1.1 christos {
264 1.1 christos printf("Available commands:\n"
265 1.1 christos "bye Quit sftp\n"
266 1.1 christos "cd path Change remote directory to 'path'\n"
267 1.26 christos "chgrp [-h] grp path Change group of file 'path' to 'grp'\n"
268 1.26 christos "chmod [-h] mode path Change permissions of file 'path' to 'mode'\n"
269 1.26 christos "chown [-h] own path Change owner of file 'path' to 'own'\n"
270 1.1 christos "df [-hi] [path] Display statistics for current directory or\n"
271 1.1 christos " filesystem containing 'path'\n"
272 1.1 christos "exit Quit sftp\n"
273 1.27 christos "get [-afpR] remote [local] Download file\n"
274 1.1 christos "help Display this help text\n"
275 1.1 christos "lcd path Change local directory to 'path'\n"
276 1.1 christos "lls [ls-options [path]] Display local directory listing\n"
277 1.1 christos "lmkdir path Create local directory\n"
278 1.7 christos "ln [-s] oldpath newpath Link remote file (-s for symlink)\n"
279 1.1 christos "lpwd Print local working directory\n"
280 1.4 adam "ls [-1afhlnrSt] [path] Display remote directory listing\n"
281 1.1 christos "lumask umask Set local umask to 'umask'\n"
282 1.1 christos "mkdir path Create remote directory\n"
283 1.1 christos "progress Toggle display of progress meter\n"
284 1.27 christos "put [-afpR] local [remote] Upload file\n"
285 1.1 christos "pwd Display remote working directory\n"
286 1.1 christos "quit Quit sftp\n"
287 1.27 christos "reget [-fpR] remote [local] Resume download file\n"
288 1.1 christos "rename oldpath newpath Rename remote file\n"
289 1.27 christos "reput [-fpR] local [remote] Resume upload file\n"
290 1.1 christos "rm path Delete remote file\n"
291 1.1 christos "rmdir path Remove remote directory\n"
292 1.1 christos "symlink oldpath newpath Symlink remote file\n"
293 1.1 christos "version Show SFTP version\n"
294 1.1 christos "!command Execute 'command' in local shell\n"
295 1.1 christos "! Escape to local shell\n"
296 1.1 christos "? Synonym for help\n");
297 1.1 christos }
298 1.1 christos
299 1.1 christos static void
300 1.1 christos local_do_shell(const char *args)
301 1.1 christos {
302 1.1 christos int status;
303 1.7 christos const char *shell;
304 1.1 christos pid_t pid;
305 1.1 christos
306 1.1 christos if (!*args)
307 1.1 christos args = NULL;
308 1.1 christos
309 1.7 christos if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
310 1.1 christos shell = _PATH_BSHELL;
311 1.1 christos
312 1.1 christos if ((pid = fork()) == -1)
313 1.1 christos fatal("Couldn't fork: %s", strerror(errno));
314 1.1 christos
315 1.1 christos if (pid == 0) {
316 1.1 christos /* XXX: child has pipe fds to ssh subproc open - issue? */
317 1.1 christos if (args) {
318 1.1 christos debug3("Executing %s -c \"%s\"", shell, args);
319 1.1 christos execl(shell, shell, "-c", args, (char *)NULL);
320 1.1 christos } else {
321 1.1 christos debug3("Executing %s", shell);
322 1.1 christos execl(shell, shell, (char *)NULL);
323 1.1 christos }
324 1.1 christos fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
325 1.1 christos strerror(errno));
326 1.1 christos _exit(1);
327 1.1 christos }
328 1.1 christos while (waitpid(pid, &status, 0) == -1)
329 1.1 christos if (errno != EINTR)
330 1.1 christos fatal("Couldn't wait for child: %s", strerror(errno));
331 1.1 christos if (!WIFEXITED(status))
332 1.1 christos error("Shell exited abnormally");
333 1.1 christos else if (WEXITSTATUS(status))
334 1.1 christos error("Shell exited with status %d", WEXITSTATUS(status));
335 1.1 christos }
336 1.1 christos
337 1.1 christos static void
338 1.1 christos local_do_ls(const char *args)
339 1.1 christos {
340 1.1 christos if (!args || !*args)
341 1.1 christos local_do_shell(_PATH_LS);
342 1.1 christos else {
343 1.1 christos int len = strlen(_PATH_LS " ") + strlen(args) + 1;
344 1.1 christos char *buf = xmalloc(len);
345 1.1 christos
346 1.1 christos /* XXX: quoting - rip quoting code from ftp? */
347 1.1 christos snprintf(buf, len, _PATH_LS " %s", args);
348 1.1 christos local_do_shell(buf);
349 1.12 christos free(buf);
350 1.1 christos }
351 1.1 christos }
352 1.1 christos
353 1.1 christos /* Strip one path (usually the pwd) from the start of another */
354 1.1 christos static char *
355 1.19 christos path_strip(const char *path, const char *strip)
356 1.1 christos {
357 1.1 christos size_t len;
358 1.1 christos
359 1.1 christos if (strip == NULL)
360 1.1 christos return (xstrdup(path));
361 1.1 christos
362 1.1 christos len = strlen(strip);
363 1.1 christos if (strncmp(path, strip, len) == 0) {
364 1.1 christos if (strip[len - 1] != '/' && path[len] == '/')
365 1.1 christos len++;
366 1.1 christos return (xstrdup(path + len));
367 1.1 christos }
368 1.1 christos
369 1.1 christos return (xstrdup(path));
370 1.1 christos }
371 1.1 christos
372 1.1 christos static char *
373 1.19 christos make_absolute(char *p, const char *pwd)
374 1.1 christos {
375 1.1 christos char *abs_str;
376 1.1 christos
377 1.1 christos /* Derelativise */
378 1.26 christos if (p && !path_absolute(p)) {
379 1.1 christos abs_str = path_append(pwd, p);
380 1.12 christos free(p);
381 1.1 christos return(abs_str);
382 1.1 christos } else
383 1.1 christos return(p);
384 1.1 christos }
385 1.1 christos
386 1.1 christos static int
387 1.12 christos parse_getput_flags(const char *cmd, char **argv, int argc,
388 1.13 christos int *aflag, int *fflag, int *pflag, int *rflag)
389 1.1 christos {
390 1.1 christos extern int opterr, optind, optopt, optreset;
391 1.1 christos int ch;
392 1.1 christos
393 1.1 christos optind = optreset = 1;
394 1.1 christos opterr = 0;
395 1.1 christos
396 1.13 christos *aflag = *fflag = *rflag = *pflag = 0;
397 1.13 christos while ((ch = getopt(argc, argv, "afPpRr")) != -1) {
398 1.1 christos switch (ch) {
399 1.12 christos case 'a':
400 1.12 christos *aflag = 1;
401 1.12 christos break;
402 1.13 christos case 'f':
403 1.13 christos *fflag = 1;
404 1.13 christos break;
405 1.1 christos case 'p':
406 1.1 christos case 'P':
407 1.1 christos *pflag = 1;
408 1.1 christos break;
409 1.4 adam case 'r':
410 1.4 adam case 'R':
411 1.4 adam *rflag = 1;
412 1.4 adam break;
413 1.1 christos default:
414 1.1 christos error("%s: Invalid flag -%c", cmd, optopt);
415 1.1 christos return -1;
416 1.1 christos }
417 1.1 christos }
418 1.1 christos
419 1.1 christos return optind;
420 1.1 christos }
421 1.1 christos
422 1.1 christos static int
423 1.7 christos parse_link_flags(const char *cmd, char **argv, int argc, int *sflag)
424 1.7 christos {
425 1.7 christos extern int opterr, optind, optopt, optreset;
426 1.7 christos int ch;
427 1.7 christos
428 1.7 christos optind = optreset = 1;
429 1.7 christos opterr = 0;
430 1.7 christos
431 1.7 christos *sflag = 0;
432 1.7 christos while ((ch = getopt(argc, argv, "s")) != -1) {
433 1.7 christos switch (ch) {
434 1.7 christos case 's':
435 1.7 christos *sflag = 1;
436 1.7 christos break;
437 1.7 christos default:
438 1.7 christos error("%s: Invalid flag -%c", cmd, optopt);
439 1.7 christos return -1;
440 1.7 christos }
441 1.7 christos }
442 1.7 christos
443 1.7 christos return optind;
444 1.7 christos }
445 1.7 christos
446 1.7 christos static int
447 1.13 christos parse_rename_flags(const char *cmd, char **argv, int argc, int *lflag)
448 1.13 christos {
449 1.13 christos extern int opterr, optind, optopt, optreset;
450 1.13 christos int ch;
451 1.13 christos
452 1.13 christos optind = optreset = 1;
453 1.13 christos opterr = 0;
454 1.13 christos
455 1.13 christos *lflag = 0;
456 1.13 christos while ((ch = getopt(argc, argv, "l")) != -1) {
457 1.13 christos switch (ch) {
458 1.13 christos case 'l':
459 1.13 christos *lflag = 1;
460 1.13 christos break;
461 1.13 christos default:
462 1.13 christos error("%s: Invalid flag -%c", cmd, optopt);
463 1.13 christos return -1;
464 1.13 christos }
465 1.13 christos }
466 1.13 christos
467 1.13 christos return optind;
468 1.13 christos }
469 1.13 christos
470 1.13 christos static int
471 1.1 christos parse_ls_flags(char **argv, int argc, int *lflag)
472 1.1 christos {
473 1.1 christos extern int opterr, optind, optopt, optreset;
474 1.1 christos int ch;
475 1.1 christos
476 1.1 christos optind = optreset = 1;
477 1.1 christos opterr = 0;
478 1.1 christos
479 1.1 christos *lflag = LS_NAME_SORT;
480 1.4 adam while ((ch = getopt(argc, argv, "1Safhlnrt")) != -1) {
481 1.1 christos switch (ch) {
482 1.1 christos case '1':
483 1.1 christos *lflag &= ~VIEW_FLAGS;
484 1.1 christos *lflag |= LS_SHORT_VIEW;
485 1.1 christos break;
486 1.1 christos case 'S':
487 1.1 christos *lflag &= ~SORT_FLAGS;
488 1.1 christos *lflag |= LS_SIZE_SORT;
489 1.1 christos break;
490 1.1 christos case 'a':
491 1.1 christos *lflag |= LS_SHOW_ALL;
492 1.1 christos break;
493 1.1 christos case 'f':
494 1.1 christos *lflag &= ~SORT_FLAGS;
495 1.1 christos break;
496 1.4 adam case 'h':
497 1.4 adam *lflag |= LS_SI_UNITS;
498 1.4 adam break;
499 1.1 christos case 'l':
500 1.4 adam *lflag &= ~LS_SHORT_VIEW;
501 1.1 christos *lflag |= LS_LONG_VIEW;
502 1.1 christos break;
503 1.1 christos case 'n':
504 1.4 adam *lflag &= ~LS_SHORT_VIEW;
505 1.1 christos *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
506 1.1 christos break;
507 1.1 christos case 'r':
508 1.1 christos *lflag |= LS_REVERSE_SORT;
509 1.1 christos break;
510 1.1 christos case 't':
511 1.1 christos *lflag &= ~SORT_FLAGS;
512 1.1 christos *lflag |= LS_TIME_SORT;
513 1.1 christos break;
514 1.1 christos default:
515 1.1 christos error("ls: Invalid flag -%c", optopt);
516 1.1 christos return -1;
517 1.1 christos }
518 1.1 christos }
519 1.1 christos
520 1.1 christos return optind;
521 1.1 christos }
522 1.1 christos
523 1.1 christos static int
524 1.1 christos parse_df_flags(const char *cmd, char **argv, int argc, int *hflag, int *iflag)
525 1.1 christos {
526 1.1 christos extern int opterr, optind, optopt, optreset;
527 1.1 christos int ch;
528 1.1 christos
529 1.1 christos optind = optreset = 1;
530 1.1 christos opterr = 0;
531 1.1 christos
532 1.1 christos *hflag = *iflag = 0;
533 1.1 christos while ((ch = getopt(argc, argv, "hi")) != -1) {
534 1.1 christos switch (ch) {
535 1.1 christos case 'h':
536 1.1 christos *hflag = 1;
537 1.1 christos break;
538 1.1 christos case 'i':
539 1.1 christos *iflag = 1;
540 1.1 christos break;
541 1.1 christos default:
542 1.1 christos error("%s: Invalid flag -%c", cmd, optopt);
543 1.1 christos return -1;
544 1.1 christos }
545 1.1 christos }
546 1.1 christos
547 1.1 christos return optind;
548 1.1 christos }
549 1.1 christos
550 1.1 christos static int
551 1.26 christos parse_ch_flags(const char *cmd, char **argv, int argc, int *hflag)
552 1.26 christos {
553 1.26 christos extern int opterr, optind, optopt, optreset;
554 1.26 christos int ch;
555 1.26 christos
556 1.26 christos optind = optreset = 1;
557 1.26 christos opterr = 0;
558 1.26 christos
559 1.26 christos *hflag = 0;
560 1.26 christos while ((ch = getopt(argc, argv, "h")) != -1) {
561 1.26 christos switch (ch) {
562 1.26 christos case 'h':
563 1.26 christos *hflag = 1;
564 1.26 christos break;
565 1.26 christos default:
566 1.26 christos error("%s: Invalid flag -%c", cmd, optopt);
567 1.26 christos return -1;
568 1.26 christos }
569 1.26 christos }
570 1.26 christos
571 1.26 christos return optind;
572 1.26 christos }
573 1.26 christos
574 1.26 christos static int
575 1.13 christos parse_no_flags(const char *cmd, char **argv, int argc)
576 1.13 christos {
577 1.13 christos extern int opterr, optind, optopt, optreset;
578 1.13 christos int ch;
579 1.13 christos
580 1.13 christos optind = optreset = 1;
581 1.13 christos opterr = 0;
582 1.13 christos
583 1.13 christos while ((ch = getopt(argc, argv, "")) != -1) {
584 1.13 christos switch (ch) {
585 1.13 christos default:
586 1.13 christos error("%s: Invalid flag -%c", cmd, optopt);
587 1.13 christos return -1;
588 1.13 christos }
589 1.13 christos }
590 1.13 christos
591 1.13 christos return optind;
592 1.13 christos }
593 1.13 christos
594 1.13 christos static int
595 1.19 christos is_dir(const char *path)
596 1.1 christos {
597 1.1 christos struct stat sb;
598 1.1 christos
599 1.1 christos /* XXX: report errors? */
600 1.1 christos if (stat(path, &sb) == -1)
601 1.1 christos return(0);
602 1.1 christos
603 1.1 christos return(S_ISDIR(sb.st_mode));
604 1.1 christos }
605 1.1 christos
606 1.1 christos static int
607 1.19 christos remote_is_dir(struct sftp_conn *conn, const char *path)
608 1.1 christos {
609 1.1 christos Attrib *a;
610 1.1 christos
611 1.1 christos /* XXX: report errors? */
612 1.1 christos if ((a = do_stat(conn, path, 1)) == NULL)
613 1.1 christos return(0);
614 1.1 christos if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
615 1.1 christos return(0);
616 1.1 christos return(S_ISDIR(a->perm));
617 1.1 christos }
618 1.1 christos
619 1.4 adam /* Check whether path returned from glob(..., GLOB_MARK, ...) is a directory */
620 1.4 adam static int
621 1.19 christos pathname_is_dir(const char *pathname)
622 1.4 adam {
623 1.4 adam size_t l = strlen(pathname);
624 1.4 adam
625 1.4 adam return l > 0 && pathname[l - 1] == '/';
626 1.4 adam }
627 1.4 adam
628 1.1 christos static int
629 1.19 christos process_get(struct sftp_conn *conn, const char *src, const char *dst,
630 1.19 christos const char *pwd, int pflag, int rflag, int resume, int fflag)
631 1.1 christos {
632 1.1 christos char *abs_src = NULL;
633 1.1 christos char *abs_dst = NULL;
634 1.1 christos glob_t g;
635 1.4 adam char *filename, *tmp=NULL;
636 1.13 christos int i, r, err = 0;
637 1.1 christos
638 1.1 christos abs_src = xstrdup(src);
639 1.1 christos abs_src = make_absolute(abs_src, pwd);
640 1.4 adam memset(&g, 0, sizeof(g));
641 1.1 christos
642 1.1 christos debug3("Looking up %s", abs_src);
643 1.13 christos if ((r = remote_glob(conn, abs_src, GLOB_MARK, NULL, &g)) != 0) {
644 1.13 christos if (r == GLOB_NOSPACE) {
645 1.13 christos error("Too many matches for \"%s\".", abs_src);
646 1.13 christos } else {
647 1.13 christos error("File \"%s\" not found.", abs_src);
648 1.13 christos }
649 1.1 christos err = -1;
650 1.1 christos goto out;
651 1.1 christos }
652 1.1 christos
653 1.4 adam /*
654 1.4 adam * If multiple matches then dst must be a directory or
655 1.4 adam * unspecified.
656 1.4 adam */
657 1.4 adam if (g.gl_matchc > 1 && dst != NULL && !is_dir(dst)) {
658 1.4 adam error("Multiple source paths, but destination "
659 1.4 adam "\"%s\" is not a directory", dst);
660 1.1 christos err = -1;
661 1.1 christos goto out;
662 1.1 christos }
663 1.1 christos
664 1.1 christos for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
665 1.4 adam tmp = xstrdup(g.gl_pathv[i]);
666 1.4 adam if ((filename = basename(tmp)) == NULL) {
667 1.4 adam error("basename %s: %s", tmp, strerror(errno));
668 1.12 christos free(tmp);
669 1.1 christos err = -1;
670 1.1 christos goto out;
671 1.1 christos }
672 1.1 christos
673 1.1 christos if (g.gl_matchc == 1 && dst) {
674 1.1 christos if (is_dir(dst)) {
675 1.4 adam abs_dst = path_append(dst, filename);
676 1.4 adam } else {
677 1.1 christos abs_dst = xstrdup(dst);
678 1.4 adam }
679 1.1 christos } else if (dst) {
680 1.4 adam abs_dst = path_append(dst, filename);
681 1.4 adam } else {
682 1.4 adam abs_dst = xstrdup(filename);
683 1.4 adam }
684 1.12 christos free(tmp);
685 1.1 christos
686 1.12 christos resume |= global_aflag;
687 1.12 christos if (!quiet && resume)
688 1.19 christos mprintf("Resuming %s to %s\n",
689 1.19 christos g.gl_pathv[i], abs_dst);
690 1.12 christos else if (!quiet && !resume)
691 1.19 christos mprintf("Fetching %s to %s\n",
692 1.19 christos g.gl_pathv[i], abs_dst);
693 1.4 adam if (pathname_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
694 1.12 christos if (download_dir(conn, g.gl_pathv[i], abs_dst, NULL,
695 1.13 christos pflag || global_pflag, 1, resume,
696 1.13 christos fflag || global_fflag) == -1)
697 1.4 adam err = -1;
698 1.4 adam } else {
699 1.4 adam if (do_download(conn, g.gl_pathv[i], abs_dst, NULL,
700 1.13 christos pflag || global_pflag, resume,
701 1.13 christos fflag || global_fflag) == -1)
702 1.4 adam err = -1;
703 1.4 adam }
704 1.12 christos free(abs_dst);
705 1.1 christos abs_dst = NULL;
706 1.1 christos }
707 1.1 christos
708 1.1 christos out:
709 1.12 christos free(abs_src);
710 1.1 christos globfree(&g);
711 1.1 christos return(err);
712 1.1 christos }
713 1.1 christos
714 1.1 christos static int
715 1.19 christos process_put(struct sftp_conn *conn, const char *src, const char *dst,
716 1.19 christos const char *pwd, int pflag, int rflag, int resume, int fflag)
717 1.1 christos {
718 1.1 christos char *tmp_dst = NULL;
719 1.1 christos char *abs_dst = NULL;
720 1.4 adam char *tmp = NULL, *filename = NULL;
721 1.1 christos glob_t g;
722 1.1 christos int err = 0;
723 1.4 adam int i, dst_is_dir = 1;
724 1.1 christos struct stat sb;
725 1.1 christos
726 1.1 christos if (dst) {
727 1.1 christos tmp_dst = xstrdup(dst);
728 1.1 christos tmp_dst = make_absolute(tmp_dst, pwd);
729 1.1 christos }
730 1.1 christos
731 1.1 christos memset(&g, 0, sizeof(g));
732 1.1 christos debug3("Looking up %s", src);
733 1.4 adam if (glob(src, GLOB_NOCHECK | GLOB_LIMIT | GLOB_MARK, NULL, &g)) {
734 1.1 christos error("File \"%s\" not found.", src);
735 1.1 christos err = -1;
736 1.1 christos goto out;
737 1.1 christos }
738 1.1 christos
739 1.4 adam /* If we aren't fetching to pwd then stash this status for later */
740 1.4 adam if (tmp_dst != NULL)
741 1.4 adam dst_is_dir = remote_is_dir(conn, tmp_dst);
742 1.4 adam
743 1.1 christos /* If multiple matches, dst may be directory or unspecified */
744 1.4 adam if (g.gl_matchc > 1 && tmp_dst && !dst_is_dir) {
745 1.4 adam error("Multiple paths match, but destination "
746 1.4 adam "\"%s\" is not a directory", tmp_dst);
747 1.1 christos err = -1;
748 1.1 christos goto out;
749 1.1 christos }
750 1.1 christos
751 1.1 christos for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
752 1.1 christos if (stat(g.gl_pathv[i], &sb) == -1) {
753 1.1 christos err = -1;
754 1.1 christos error("stat %s: %s", g.gl_pathv[i], strerror(errno));
755 1.1 christos continue;
756 1.1 christos }
757 1.13 christos
758 1.4 adam tmp = xstrdup(g.gl_pathv[i]);
759 1.4 adam if ((filename = basename(tmp)) == NULL) {
760 1.4 adam error("basename %s: %s", tmp, strerror(errno));
761 1.12 christos free(tmp);
762 1.1 christos err = -1;
763 1.1 christos goto out;
764 1.1 christos }
765 1.1 christos
766 1.1 christos if (g.gl_matchc == 1 && tmp_dst) {
767 1.1 christos /* If directory specified, append filename */
768 1.4 adam if (dst_is_dir)
769 1.4 adam abs_dst = path_append(tmp_dst, filename);
770 1.4 adam else
771 1.1 christos abs_dst = xstrdup(tmp_dst);
772 1.1 christos } else if (tmp_dst) {
773 1.4 adam abs_dst = path_append(tmp_dst, filename);
774 1.4 adam } else {
775 1.4 adam abs_dst = make_absolute(xstrdup(filename), pwd);
776 1.4 adam }
777 1.12 christos free(tmp);
778 1.1 christos
779 1.13 christos resume |= global_aflag;
780 1.13 christos if (!quiet && resume)
781 1.19 christos mprintf("Resuming upload of %s to %s\n",
782 1.19 christos g.gl_pathv[i], abs_dst);
783 1.13 christos else if (!quiet && !resume)
784 1.19 christos mprintf("Uploading %s to %s\n",
785 1.19 christos g.gl_pathv[i], abs_dst);
786 1.4 adam if (pathname_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
787 1.4 adam if (upload_dir(conn, g.gl_pathv[i], abs_dst,
788 1.13 christos pflag || global_pflag, 1, resume,
789 1.13 christos fflag || global_fflag) == -1)
790 1.4 adam err = -1;
791 1.4 adam } else {
792 1.4 adam if (do_upload(conn, g.gl_pathv[i], abs_dst,
793 1.13 christos pflag || global_pflag, resume,
794 1.13 christos fflag || global_fflag) == -1)
795 1.4 adam err = -1;
796 1.4 adam }
797 1.15 christos free(abs_dst);
798 1.15 christos abs_dst = NULL;
799 1.1 christos }
800 1.1 christos
801 1.1 christos out:
802 1.12 christos free(abs_dst);
803 1.12 christos free(tmp_dst);
804 1.1 christos globfree(&g);
805 1.1 christos return(err);
806 1.1 christos }
807 1.1 christos
808 1.1 christos static int
809 1.1 christos sdirent_comp(const void *aa, const void *bb)
810 1.1 christos {
811 1.7 christos const SFTP_DIRENT *a = *(const SFTP_DIRENT * const *)aa;
812 1.7 christos const SFTP_DIRENT *b = *(const SFTP_DIRENT * const *)bb;
813 1.1 christos int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
814 1.1 christos
815 1.1 christos #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
816 1.1 christos if (sort_flag & LS_NAME_SORT)
817 1.1 christos return (rmul * strcmp(a->filename, b->filename));
818 1.1 christos else if (sort_flag & LS_TIME_SORT)
819 1.1 christos return (rmul * NCMP(a->a.mtime, b->a.mtime));
820 1.1 christos else if (sort_flag & LS_SIZE_SORT)
821 1.1 christos return (rmul * NCMP(a->a.size, b->a.size));
822 1.1 christos
823 1.1 christos fatal("Unknown ls sort type");
824 1.2 christos /*NOTREACHED*/
825 1.2 christos return 0;
826 1.1 christos }
827 1.1 christos
828 1.1 christos /* sftp ls.1 replacement for directories */
829 1.1 christos static int
830 1.19 christos do_ls_dir(struct sftp_conn *conn, const char *path,
831 1.19 christos const char *strip_path, int lflag)
832 1.1 christos {
833 1.1 christos int n;
834 1.1 christos u_int c = 1, colspace = 0, columns = 1;
835 1.1 christos SFTP_DIRENT **d;
836 1.1 christos
837 1.1 christos if ((n = do_readdir(conn, path, &d)) != 0)
838 1.1 christos return (n);
839 1.1 christos
840 1.1 christos if (!(lflag & LS_SHORT_VIEW)) {
841 1.1 christos u_int m = 0, width = 80;
842 1.1 christos struct winsize ws;
843 1.1 christos char *tmp;
844 1.1 christos
845 1.1 christos /* Count entries for sort and find longest filename */
846 1.1 christos for (n = 0; d[n] != NULL; n++) {
847 1.1 christos if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
848 1.20 christos m = MAXIMUM(m, strlen(d[n]->filename));
849 1.1 christos }
850 1.1 christos
851 1.1 christos /* Add any subpath that also needs to be counted */
852 1.1 christos tmp = path_strip(path, strip_path);
853 1.1 christos m += strlen(tmp);
854 1.12 christos free(tmp);
855 1.1 christos
856 1.1 christos if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
857 1.1 christos width = ws.ws_col;
858 1.1 christos
859 1.1 christos columns = width / (m + 2);
860 1.20 christos columns = MAXIMUM(columns, 1);
861 1.1 christos colspace = width / columns;
862 1.20 christos colspace = MINIMUM(colspace, width);
863 1.1 christos }
864 1.1 christos
865 1.1 christos if (lflag & SORT_FLAGS) {
866 1.1 christos for (n = 0; d[n] != NULL; n++)
867 1.1 christos ; /* count entries */
868 1.1 christos sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
869 1.1 christos qsort(d, n, sizeof(*d), sdirent_comp);
870 1.1 christos }
871 1.1 christos
872 1.1 christos for (n = 0; d[n] != NULL && !interrupted; n++) {
873 1.1 christos char *tmp, *fname;
874 1.1 christos
875 1.1 christos if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
876 1.1 christos continue;
877 1.1 christos
878 1.1 christos tmp = path_append(path, d[n]->filename);
879 1.1 christos fname = path_strip(tmp, strip_path);
880 1.12 christos free(tmp);
881 1.1 christos
882 1.1 christos if (lflag & LS_LONG_VIEW) {
883 1.4 adam if (lflag & (LS_NUMERIC_VIEW|LS_SI_UNITS)) {
884 1.1 christos char *lname;
885 1.1 christos struct stat sb;
886 1.1 christos
887 1.1 christos memset(&sb, 0, sizeof(sb));
888 1.1 christos attrib_to_stat(&d[n]->a, &sb);
889 1.4 adam lname = ls_file(fname, &sb, 1,
890 1.4 adam (lflag & LS_SI_UNITS));
891 1.19 christos mprintf("%s\n", lname);
892 1.12 christos free(lname);
893 1.1 christos } else
894 1.19 christos mprintf("%s\n", d[n]->longname);
895 1.1 christos } else {
896 1.19 christos mprintf("%-*s", colspace, fname);
897 1.1 christos if (c >= columns) {
898 1.1 christos printf("\n");
899 1.1 christos c = 1;
900 1.1 christos } else
901 1.1 christos c++;
902 1.1 christos }
903 1.1 christos
904 1.12 christos free(fname);
905 1.1 christos }
906 1.1 christos
907 1.1 christos if (!(lflag & LS_LONG_VIEW) && (c != 1))
908 1.1 christos printf("\n");
909 1.1 christos
910 1.1 christos free_sftp_dirents(d);
911 1.1 christos return (0);
912 1.1 christos }
913 1.1 christos
914 1.22 christos static int
915 1.22 christos sglob_comp(const void *aa, const void *bb)
916 1.22 christos {
917 1.22 christos u_int a = *(const u_int *)aa;
918 1.22 christos u_int b = *(const u_int *)bb;
919 1.22 christos const char *ap = sort_glob->gl_pathv[a];
920 1.22 christos const char *bp = sort_glob->gl_pathv[b];
921 1.22 christos #if 0
922 1.22 christos const struct stat *as = sort_glob->gl_statv[a];
923 1.22 christos const struct stat *bs = sort_glob->gl_statv[b];
924 1.22 christos #else
925 1.22 christos struct stat ass, bss;
926 1.22 christos const struct stat *as = &ass;
927 1.22 christos const struct stat *bs = &bss;
928 1.22 christos #endif
929 1.22 christos int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
930 1.22 christos
931 1.22 christos #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
932 1.22 christos if (sort_flag & LS_NAME_SORT)
933 1.22 christos return (rmul * strcmp(ap, bp));
934 1.22 christos #if 1
935 1.22 christos if (stat(ap, &ass) == -1 || stat(bp, &bss) == -1)
936 1.22 christos return 0;
937 1.22 christos #endif
938 1.22 christos else if (sort_flag & LS_TIME_SORT)
939 1.22 christos return (rmul * timespeccmp(&as->st_mtim, &bs->st_mtim, <));
940 1.22 christos else if (sort_flag & LS_SIZE_SORT)
941 1.22 christos return (rmul * NCMP(as->st_size, bs->st_size));
942 1.22 christos
943 1.22 christos fatal("Unknown ls sort type");
944 1.22 christos }
945 1.22 christos
946 1.1 christos /* sftp ls.1 replacement which handles path globs */
947 1.1 christos static int
948 1.19 christos do_globbed_ls(struct sftp_conn *conn, const char *path,
949 1.19 christos const char *strip_path, int lflag)
950 1.1 christos {
951 1.7 christos char *fname, *lname;
952 1.1 christos glob_t g;
953 1.13 christos int err, r;
954 1.7 christos struct winsize ws;
955 1.22 christos u_int i, j, nentries, *indices = NULL, c = 1;
956 1.22 christos u_int colspace = 0, columns = 1, m = 0, width = 80;
957 1.7 christos struct stat *stp;
958 1.7 christos #ifndef GLOB_KEEPSTAT
959 1.7 christos struct stat st;
960 1.7 christos #define GLOB_KEEPSTAT 0
961 1.7 christos #endif
962 1.1 christos
963 1.1 christos memset(&g, 0, sizeof(g));
964 1.1 christos
965 1.13 christos if ((r = remote_glob(conn, path,
966 1.9 christos GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE|GLOB_KEEPSTAT|GLOB_NOSORT,
967 1.13 christos NULL, &g)) != 0 ||
968 1.7 christos (g.gl_pathc && !g.gl_matchc)) {
969 1.1 christos if (g.gl_pathc)
970 1.1 christos globfree(&g);
971 1.13 christos if (r == GLOB_NOSPACE) {
972 1.13 christos error("Can't ls: Too many matches for \"%s\"", path);
973 1.13 christos } else {
974 1.13 christos error("Can't ls: \"%s\" not found", path);
975 1.13 christos }
976 1.7 christos return -1;
977 1.1 christos }
978 1.1 christos
979 1.1 christos if (interrupted)
980 1.1 christos goto out;
981 1.1 christos
982 1.1 christos /*
983 1.1 christos * If the glob returns a single match and it is a directory,
984 1.1 christos * then just list its contents.
985 1.1 christos */
986 1.7 christos if (g.gl_matchc == 1 &&
987 1.7 christos #if GLOB_KEEPSTAT != 0
988 1.7 christos (stp = g.gl_statv[0]) != NULL &&
989 1.7 christos #else
990 1.7 christos lstat(g.gl_pathv[0], stp = &st) != -1 &&
991 1.7 christos #endif
992 1.7 christos S_ISDIR(stp->st_mode)) {
993 1.7 christos err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
994 1.7 christos globfree(&g);
995 1.7 christos return err;
996 1.7 christos }
997 1.1 christos
998 1.7 christos if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
999 1.7 christos width = ws.ws_col;
1000 1.1 christos
1001 1.1 christos if (!(lflag & LS_SHORT_VIEW)) {
1002 1.1 christos /* Count entries for sort and find longest filename */
1003 1.1 christos for (i = 0; g.gl_pathv[i]; i++)
1004 1.20 christos m = MAXIMUM(m, strlen(g.gl_pathv[i]));
1005 1.1 christos
1006 1.1 christos columns = width / (m + 2);
1007 1.20 christos columns = MAXIMUM(columns, 1);
1008 1.1 christos colspace = width / columns;
1009 1.1 christos }
1010 1.1 christos
1011 1.22 christos /*
1012 1.22 christos * Sorting: rather than mess with the contents of glob_t, prepare
1013 1.22 christos * an array of indices into it and sort that. For the usual
1014 1.22 christos * unsorted case, the indices are just the identity 1=1, 2=2, etc.
1015 1.22 christos */
1016 1.22 christos for (nentries = 0; g.gl_pathv[nentries] != NULL; nentries++)
1017 1.22 christos ; /* count entries */
1018 1.22 christos indices = calloc(nentries, sizeof(*indices));
1019 1.22 christos for (i = 0; i < nentries; i++)
1020 1.22 christos indices[i] = i;
1021 1.22 christos
1022 1.22 christos if (lflag & SORT_FLAGS) {
1023 1.22 christos sort_glob = &g;
1024 1.22 christos sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
1025 1.22 christos qsort(indices, nentries, sizeof(*indices), sglob_comp);
1026 1.22 christos sort_glob = NULL;
1027 1.22 christos }
1028 1.22 christos
1029 1.22 christos for (j = 0; j < nentries && !interrupted; j++) {
1030 1.22 christos i = indices[j];
1031 1.1 christos fname = path_strip(g.gl_pathv[i], strip_path);
1032 1.1 christos if (lflag & LS_LONG_VIEW) {
1033 1.7 christos #if GLOB_KEEPSTAT != 0
1034 1.7 christos stp = g.gl_statv[i];
1035 1.7 christos #else
1036 1.7 christos if (lstat(g.gl_pathv[i], stp = &st) == -1)
1037 1.7 christos stp = NULL;
1038 1.7 christos #endif
1039 1.7 christos if (stp == NULL) {
1040 1.7 christos error("no stat information for %s", fname);
1041 1.7 christos continue;
1042 1.7 christos }
1043 1.7 christos lname = ls_file(fname, stp, 1, (lflag & LS_SI_UNITS));
1044 1.19 christos mprintf("%s\n", lname);
1045 1.12 christos free(lname);
1046 1.1 christos } else {
1047 1.19 christos mprintf("%-*s", colspace, fname);
1048 1.1 christos if (c >= columns) {
1049 1.1 christos printf("\n");
1050 1.1 christos c = 1;
1051 1.1 christos } else
1052 1.1 christos c++;
1053 1.1 christos }
1054 1.12 christos free(fname);
1055 1.1 christos }
1056 1.1 christos
1057 1.1 christos if (!(lflag & LS_LONG_VIEW) && (c != 1))
1058 1.1 christos printf("\n");
1059 1.1 christos
1060 1.1 christos out:
1061 1.1 christos if (g.gl_pathc)
1062 1.1 christos globfree(&g);
1063 1.22 christos free(indices);
1064 1.1 christos
1065 1.7 christos return 0;
1066 1.1 christos }
1067 1.1 christos
1068 1.1 christos static int
1069 1.19 christos do_df(struct sftp_conn *conn, const char *path, int hflag, int iflag)
1070 1.1 christos {
1071 1.1 christos struct sftp_statvfs st;
1072 1.21 christos char s_used[FMT_SCALED_STRSIZE], s_avail[FMT_SCALED_STRSIZE];
1073 1.21 christos char s_root[FMT_SCALED_STRSIZE], s_total[FMT_SCALED_STRSIZE];
1074 1.21 christos char s_icapacity[16], s_dcapacity[16];
1075 1.1 christos
1076 1.1 christos if (do_statvfs(conn, path, &st, 1) == -1)
1077 1.1 christos return -1;
1078 1.21 christos if (st.f_files == 0)
1079 1.21 christos strlcpy(s_icapacity, "ERR", sizeof(s_icapacity));
1080 1.21 christos else {
1081 1.21 christos snprintf(s_icapacity, sizeof(s_icapacity), "%3llu%%",
1082 1.21 christos (unsigned long long)(100 * (st.f_files - st.f_ffree) /
1083 1.21 christos st.f_files));
1084 1.21 christos }
1085 1.21 christos if (st.f_blocks == 0)
1086 1.21 christos strlcpy(s_dcapacity, "ERR", sizeof(s_dcapacity));
1087 1.21 christos else {
1088 1.21 christos snprintf(s_dcapacity, sizeof(s_dcapacity), "%3llu%%",
1089 1.21 christos (unsigned long long)(100 * (st.f_blocks - st.f_bfree) /
1090 1.21 christos st.f_blocks));
1091 1.21 christos }
1092 1.1 christos if (iflag) {
1093 1.1 christos printf(" Inodes Used Avail "
1094 1.1 christos "(root) %%Capacity\n");
1095 1.21 christos printf("%11llu %11llu %11llu %11llu %s\n",
1096 1.1 christos (unsigned long long)st.f_files,
1097 1.1 christos (unsigned long long)(st.f_files - st.f_ffree),
1098 1.1 christos (unsigned long long)st.f_favail,
1099 1.21 christos (unsigned long long)st.f_ffree, s_icapacity);
1100 1.1 christos } else if (hflag) {
1101 1.1 christos strlcpy(s_used, "error", sizeof(s_used));
1102 1.1 christos strlcpy(s_avail, "error", sizeof(s_avail));
1103 1.1 christos strlcpy(s_root, "error", sizeof(s_root));
1104 1.1 christos strlcpy(s_total, "error", sizeof(s_total));
1105 1.1 christos fmt_scaled((st.f_blocks - st.f_bfree) * st.f_frsize, s_used);
1106 1.1 christos fmt_scaled(st.f_bavail * st.f_frsize, s_avail);
1107 1.1 christos fmt_scaled(st.f_bfree * st.f_frsize, s_root);
1108 1.1 christos fmt_scaled(st.f_blocks * st.f_frsize, s_total);
1109 1.1 christos printf(" Size Used Avail (root) %%Capacity\n");
1110 1.21 christos printf("%7sB %7sB %7sB %7sB %s\n",
1111 1.21 christos s_total, s_used, s_avail, s_root, s_dcapacity);
1112 1.1 christos } else {
1113 1.1 christos printf(" Size Used Avail "
1114 1.1 christos "(root) %%Capacity\n");
1115 1.21 christos printf("%12llu %12llu %12llu %12llu %s\n",
1116 1.1 christos (unsigned long long)(st.f_frsize * st.f_blocks / 1024),
1117 1.1 christos (unsigned long long)(st.f_frsize *
1118 1.1 christos (st.f_blocks - st.f_bfree) / 1024),
1119 1.1 christos (unsigned long long)(st.f_frsize * st.f_bavail / 1024),
1120 1.1 christos (unsigned long long)(st.f_frsize * st.f_bfree / 1024),
1121 1.21 christos s_dcapacity);
1122 1.1 christos }
1123 1.1 christos return 0;
1124 1.1 christos }
1125 1.1 christos
1126 1.1 christos /*
1127 1.1 christos * Undo escaping of glob sequences in place. Used to undo extra escaping
1128 1.1 christos * applied in makeargv() when the string is destined for a function that
1129 1.1 christos * does not glob it.
1130 1.1 christos */
1131 1.1 christos static void
1132 1.1 christos undo_glob_escape(char *s)
1133 1.1 christos {
1134 1.1 christos size_t i, j;
1135 1.1 christos
1136 1.1 christos for (i = j = 0;;) {
1137 1.1 christos if (s[i] == '\0') {
1138 1.1 christos s[j] = '\0';
1139 1.1 christos return;
1140 1.1 christos }
1141 1.1 christos if (s[i] != '\\') {
1142 1.1 christos s[j++] = s[i++];
1143 1.1 christos continue;
1144 1.1 christos }
1145 1.1 christos /* s[i] == '\\' */
1146 1.1 christos ++i;
1147 1.1 christos switch (s[i]) {
1148 1.1 christos case '?':
1149 1.1 christos case '[':
1150 1.1 christos case '*':
1151 1.1 christos case '\\':
1152 1.1 christos s[j++] = s[i++];
1153 1.1 christos break;
1154 1.1 christos case '\0':
1155 1.1 christos s[j++] = '\\';
1156 1.1 christos s[j] = '\0';
1157 1.1 christos return;
1158 1.1 christos default:
1159 1.1 christos s[j++] = '\\';
1160 1.1 christos s[j++] = s[i++];
1161 1.1 christos break;
1162 1.1 christos }
1163 1.1 christos }
1164 1.1 christos }
1165 1.1 christos
1166 1.1 christos /*
1167 1.1 christos * Split a string into an argument vector using sh(1)-style quoting,
1168 1.1 christos * comment and escaping rules, but with some tweaks to handle glob(3)
1169 1.1 christos * wildcards.
1170 1.4 adam * The "sloppy" flag allows for recovery from missing terminating quote, for
1171 1.4 adam * use in parsing incomplete commandlines during tab autocompletion.
1172 1.4 adam *
1173 1.1 christos * Returns NULL on error or a NULL-terminated array of arguments.
1174 1.4 adam *
1175 1.4 adam * If "lastquote" is not NULL, the quoting character used for the last
1176 1.4 adam * argument is placed in *lastquote ("\0", "'" or "\"").
1177 1.13 christos *
1178 1.4 adam * If "terminated" is not NULL, *terminated will be set to 1 when the
1179 1.4 adam * last argument's quote has been properly terminated or 0 otherwise.
1180 1.4 adam * This parameter is only of use if "sloppy" is set.
1181 1.1 christos */
1182 1.1 christos #define MAXARGS 128
1183 1.1 christos #define MAXARGLEN 8192
1184 1.1 christos static char **
1185 1.4 adam makeargv(const char *arg, int *argcp, int sloppy, char *lastquote,
1186 1.4 adam u_int *terminated)
1187 1.1 christos {
1188 1.1 christos int argc, quot;
1189 1.1 christos size_t i, j;
1190 1.1 christos static char argvs[MAXARGLEN];
1191 1.1 christos static char *argv[MAXARGS + 1];
1192 1.1 christos enum { MA_START, MA_SQUOTE, MA_DQUOTE, MA_UNQUOTED } state, q;
1193 1.1 christos
1194 1.1 christos *argcp = argc = 0;
1195 1.1 christos if (strlen(arg) > sizeof(argvs) - 1) {
1196 1.1 christos args_too_longs:
1197 1.1 christos error("string too long");
1198 1.1 christos return NULL;
1199 1.1 christos }
1200 1.4 adam if (terminated != NULL)
1201 1.4 adam *terminated = 1;
1202 1.4 adam if (lastquote != NULL)
1203 1.4 adam *lastquote = '\0';
1204 1.1 christos state = MA_START;
1205 1.1 christos i = j = 0;
1206 1.1 christos for (;;) {
1207 1.11 christos if ((size_t)argc >= sizeof(argv) / sizeof(*argv)){
1208 1.11 christos error("Too many arguments.");
1209 1.11 christos return NULL;
1210 1.11 christos }
1211 1.2 christos if (isspace((unsigned char)arg[i])) {
1212 1.1 christos if (state == MA_UNQUOTED) {
1213 1.1 christos /* Terminate current argument */
1214 1.1 christos argvs[j++] = '\0';
1215 1.1 christos argc++;
1216 1.1 christos state = MA_START;
1217 1.1 christos } else if (state != MA_START)
1218 1.1 christos argvs[j++] = arg[i];
1219 1.1 christos } else if (arg[i] == '"' || arg[i] == '\'') {
1220 1.1 christos q = arg[i] == '"' ? MA_DQUOTE : MA_SQUOTE;
1221 1.1 christos if (state == MA_START) {
1222 1.1 christos argv[argc] = argvs + j;
1223 1.1 christos state = q;
1224 1.4 adam if (lastquote != NULL)
1225 1.4 adam *lastquote = arg[i];
1226 1.13 christos } else if (state == MA_UNQUOTED)
1227 1.1 christos state = q;
1228 1.1 christos else if (state == q)
1229 1.1 christos state = MA_UNQUOTED;
1230 1.1 christos else
1231 1.1 christos argvs[j++] = arg[i];
1232 1.1 christos } else if (arg[i] == '\\') {
1233 1.1 christos if (state == MA_SQUOTE || state == MA_DQUOTE) {
1234 1.1 christos quot = state == MA_SQUOTE ? '\'' : '"';
1235 1.1 christos /* Unescape quote we are in */
1236 1.1 christos /* XXX support \n and friends? */
1237 1.1 christos if (arg[i + 1] == quot) {
1238 1.1 christos i++;
1239 1.1 christos argvs[j++] = arg[i];
1240 1.1 christos } else if (arg[i + 1] == '?' ||
1241 1.1 christos arg[i + 1] == '[' || arg[i + 1] == '*') {
1242 1.1 christos /*
1243 1.1 christos * Special case for sftp: append
1244 1.1 christos * double-escaped glob sequence -
1245 1.1 christos * glob will undo one level of
1246 1.1 christos * escaping. NB. string can grow here.
1247 1.1 christos */
1248 1.1 christos if (j >= sizeof(argvs) - 5)
1249 1.1 christos goto args_too_longs;
1250 1.1 christos argvs[j++] = '\\';
1251 1.1 christos argvs[j++] = arg[i++];
1252 1.1 christos argvs[j++] = '\\';
1253 1.1 christos argvs[j++] = arg[i];
1254 1.1 christos } else {
1255 1.1 christos argvs[j++] = arg[i++];
1256 1.1 christos argvs[j++] = arg[i];
1257 1.1 christos }
1258 1.1 christos } else {
1259 1.1 christos if (state == MA_START) {
1260 1.1 christos argv[argc] = argvs + j;
1261 1.1 christos state = MA_UNQUOTED;
1262 1.4 adam if (lastquote != NULL)
1263 1.4 adam *lastquote = '\0';
1264 1.1 christos }
1265 1.1 christos if (arg[i + 1] == '?' || arg[i + 1] == '[' ||
1266 1.1 christos arg[i + 1] == '*' || arg[i + 1] == '\\') {
1267 1.1 christos /*
1268 1.1 christos * Special case for sftp: append
1269 1.1 christos * escaped glob sequence -
1270 1.1 christos * glob will undo one level of
1271 1.1 christos * escaping.
1272 1.1 christos */
1273 1.1 christos argvs[j++] = arg[i++];
1274 1.1 christos argvs[j++] = arg[i];
1275 1.1 christos } else {
1276 1.1 christos /* Unescape everything */
1277 1.1 christos /* XXX support \n and friends? */
1278 1.1 christos i++;
1279 1.1 christos argvs[j++] = arg[i];
1280 1.1 christos }
1281 1.1 christos }
1282 1.1 christos } else if (arg[i] == '#') {
1283 1.1 christos if (state == MA_SQUOTE || state == MA_DQUOTE)
1284 1.1 christos argvs[j++] = arg[i];
1285 1.1 christos else
1286 1.1 christos goto string_done;
1287 1.1 christos } else if (arg[i] == '\0') {
1288 1.1 christos if (state == MA_SQUOTE || state == MA_DQUOTE) {
1289 1.4 adam if (sloppy) {
1290 1.4 adam state = MA_UNQUOTED;
1291 1.4 adam if (terminated != NULL)
1292 1.4 adam *terminated = 0;
1293 1.4 adam goto string_done;
1294 1.4 adam }
1295 1.1 christos error("Unterminated quoted argument");
1296 1.1 christos return NULL;
1297 1.1 christos }
1298 1.1 christos string_done:
1299 1.1 christos if (state == MA_UNQUOTED) {
1300 1.1 christos argvs[j++] = '\0';
1301 1.1 christos argc++;
1302 1.1 christos }
1303 1.1 christos break;
1304 1.1 christos } else {
1305 1.1 christos if (state == MA_START) {
1306 1.1 christos argv[argc] = argvs + j;
1307 1.1 christos state = MA_UNQUOTED;
1308 1.4 adam if (lastquote != NULL)
1309 1.4 adam *lastquote = '\0';
1310 1.1 christos }
1311 1.1 christos if ((state == MA_SQUOTE || state == MA_DQUOTE) &&
1312 1.1 christos (arg[i] == '?' || arg[i] == '[' || arg[i] == '*')) {
1313 1.1 christos /*
1314 1.1 christos * Special case for sftp: escape quoted
1315 1.1 christos * glob(3) wildcards. NB. string can grow
1316 1.1 christos * here.
1317 1.1 christos */
1318 1.1 christos if (j >= sizeof(argvs) - 3)
1319 1.1 christos goto args_too_longs;
1320 1.1 christos argvs[j++] = '\\';
1321 1.1 christos argvs[j++] = arg[i];
1322 1.1 christos } else
1323 1.1 christos argvs[j++] = arg[i];
1324 1.1 christos }
1325 1.1 christos i++;
1326 1.1 christos }
1327 1.1 christos *argcp = argc;
1328 1.1 christos return argv;
1329 1.1 christos }
1330 1.1 christos
1331 1.1 christos static int
1332 1.26 christos parse_args(const char **cpp, int *ignore_errors, int *disable_echo, int *aflag,
1333 1.19 christos int *fflag, int *hflag, int *iflag, int *lflag, int *pflag,
1334 1.13 christos int *rflag, int *sflag,
1335 1.13 christos unsigned long *n_arg, char **path1, char **path2)
1336 1.1 christos {
1337 1.1 christos const char *cmd, *cp = *cpp;
1338 1.1 christos char *cp2, **argv;
1339 1.1 christos int base = 0;
1340 1.1 christos long l;
1341 1.23 christos int path1_mandatory = 0, i, cmdnum, optidx, argc;
1342 1.1 christos
1343 1.1 christos /* Skip leading whitespace */
1344 1.1 christos cp = cp + strspn(cp, WHITESPACE);
1345 1.1 christos
1346 1.26 christos /*
1347 1.26 christos * Check for leading '-' (disable error processing) and '@' (suppress
1348 1.26 christos * command echo)
1349 1.26 christos */
1350 1.13 christos *ignore_errors = 0;
1351 1.26 christos *disable_echo = 0;
1352 1.26 christos for (;*cp != '\0'; cp++) {
1353 1.26 christos if (*cp == '-') {
1354 1.26 christos *ignore_errors = 1;
1355 1.26 christos } else if (*cp == '@') {
1356 1.26 christos *disable_echo = 1;
1357 1.26 christos } else {
1358 1.26 christos /* all other characters terminate prefix processing */
1359 1.26 christos break;
1360 1.26 christos }
1361 1.1 christos }
1362 1.26 christos cp = cp + strspn(cp, WHITESPACE);
1363 1.1 christos
1364 1.4 adam /* Ignore blank lines and lines which begin with comment '#' char */
1365 1.4 adam if (*cp == '\0' || *cp == '#')
1366 1.4 adam return (0);
1367 1.4 adam
1368 1.4 adam if ((argv = makeargv(cp, &argc, 0, NULL, NULL)) == NULL)
1369 1.1 christos return -1;
1370 1.1 christos
1371 1.1 christos /* Figure out which command we have */
1372 1.1 christos for (i = 0; cmds[i].c != NULL; i++) {
1373 1.11 christos if (argv[0] != NULL && strcasecmp(cmds[i].c, argv[0]) == 0)
1374 1.1 christos break;
1375 1.1 christos }
1376 1.1 christos cmdnum = cmds[i].n;
1377 1.1 christos cmd = cmds[i].c;
1378 1.1 christos
1379 1.1 christos /* Special case */
1380 1.1 christos if (*cp == '!') {
1381 1.1 christos cp++;
1382 1.1 christos cmdnum = I_SHELL;
1383 1.1 christos } else if (cmdnum == -1) {
1384 1.1 christos error("Invalid command.");
1385 1.1 christos return -1;
1386 1.1 christos }
1387 1.1 christos
1388 1.1 christos /* Get arguments and parse flags */
1389 1.13 christos *aflag = *fflag = *hflag = *iflag = *lflag = *pflag = 0;
1390 1.13 christos *rflag = *sflag = 0;
1391 1.1 christos *path1 = *path2 = NULL;
1392 1.1 christos optidx = 1;
1393 1.1 christos switch (cmdnum) {
1394 1.1 christos case I_GET:
1395 1.12 christos case I_REGET:
1396 1.13 christos case I_REPUT:
1397 1.1 christos case I_PUT:
1398 1.7 christos if ((optidx = parse_getput_flags(cmd, argv, argc,
1399 1.13 christos aflag, fflag, pflag, rflag)) == -1)
1400 1.1 christos return -1;
1401 1.1 christos /* Get first pathname (mandatory) */
1402 1.1 christos if (argc - optidx < 1) {
1403 1.1 christos error("You must specify at least one path after a "
1404 1.1 christos "%s command.", cmd);
1405 1.1 christos return -1;
1406 1.1 christos }
1407 1.1 christos *path1 = xstrdup(argv[optidx]);
1408 1.1 christos /* Get second pathname (optional) */
1409 1.1 christos if (argc - optidx > 1) {
1410 1.1 christos *path2 = xstrdup(argv[optidx + 1]);
1411 1.1 christos /* Destination is not globbed */
1412 1.1 christos undo_glob_escape(*path2);
1413 1.1 christos }
1414 1.1 christos break;
1415 1.7 christos case I_LINK:
1416 1.7 christos if ((optidx = parse_link_flags(cmd, argv, argc, sflag)) == -1)
1417 1.7 christos return -1;
1418 1.13 christos goto parse_two_paths;
1419 1.13 christos case I_RENAME:
1420 1.13 christos if ((optidx = parse_rename_flags(cmd, argv, argc, lflag)) == -1)
1421 1.13 christos return -1;
1422 1.13 christos goto parse_two_paths;
1423 1.7 christos case I_SYMLINK:
1424 1.13 christos if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
1425 1.13 christos return -1;
1426 1.13 christos parse_two_paths:
1427 1.1 christos if (argc - optidx < 2) {
1428 1.1 christos error("You must specify two paths after a %s "
1429 1.1 christos "command.", cmd);
1430 1.1 christos return -1;
1431 1.1 christos }
1432 1.1 christos *path1 = xstrdup(argv[optidx]);
1433 1.1 christos *path2 = xstrdup(argv[optidx + 1]);
1434 1.1 christos /* Paths are not globbed */
1435 1.1 christos undo_glob_escape(*path1);
1436 1.1 christos undo_glob_escape(*path2);
1437 1.1 christos break;
1438 1.1 christos case I_RM:
1439 1.1 christos case I_MKDIR:
1440 1.1 christos case I_RMDIR:
1441 1.23 christos case I_LMKDIR:
1442 1.23 christos path1_mandatory = 1;
1443 1.23 christos /* FALLTHROUGH */
1444 1.1 christos case I_CHDIR:
1445 1.1 christos case I_LCHDIR:
1446 1.13 christos if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
1447 1.13 christos return -1;
1448 1.1 christos /* Get pathname (mandatory) */
1449 1.1 christos if (argc - optidx < 1) {
1450 1.23 christos if (!path1_mandatory)
1451 1.23 christos break; /* return a NULL path1 */
1452 1.1 christos error("You must specify a path after a %s command.",
1453 1.1 christos cmd);
1454 1.1 christos return -1;
1455 1.1 christos }
1456 1.1 christos *path1 = xstrdup(argv[optidx]);
1457 1.1 christos /* Only "rm" globs */
1458 1.1 christos if (cmdnum != I_RM)
1459 1.1 christos undo_glob_escape(*path1);
1460 1.1 christos break;
1461 1.1 christos case I_DF:
1462 1.1 christos if ((optidx = parse_df_flags(cmd, argv, argc, hflag,
1463 1.1 christos iflag)) == -1)
1464 1.1 christos return -1;
1465 1.1 christos /* Default to current directory if no path specified */
1466 1.1 christos if (argc - optidx < 1)
1467 1.1 christos *path1 = NULL;
1468 1.1 christos else {
1469 1.1 christos *path1 = xstrdup(argv[optidx]);
1470 1.1 christos undo_glob_escape(*path1);
1471 1.1 christos }
1472 1.1 christos break;
1473 1.1 christos case I_LS:
1474 1.1 christos if ((optidx = parse_ls_flags(argv, argc, lflag)) == -1)
1475 1.1 christos return(-1);
1476 1.1 christos /* Path is optional */
1477 1.1 christos if (argc - optidx > 0)
1478 1.1 christos *path1 = xstrdup(argv[optidx]);
1479 1.1 christos break;
1480 1.1 christos case I_LLS:
1481 1.1 christos /* Skip ls command and following whitespace */
1482 1.1 christos cp = cp + strlen(cmd) + strspn(cp, WHITESPACE);
1483 1.1 christos case I_SHELL:
1484 1.1 christos /* Uses the rest of the line */
1485 1.1 christos break;
1486 1.1 christos case I_LUMASK:
1487 1.1 christos case I_CHMOD:
1488 1.1 christos base = 8;
1489 1.26 christos /* FALLTHROUGH */
1490 1.1 christos case I_CHOWN:
1491 1.1 christos case I_CHGRP:
1492 1.26 christos if ((optidx = parse_ch_flags(cmd, argv, argc, hflag)) == -1)
1493 1.13 christos return -1;
1494 1.1 christos /* Get numeric arg (mandatory) */
1495 1.1 christos if (argc - optidx < 1)
1496 1.1 christos goto need_num_arg;
1497 1.1 christos errno = 0;
1498 1.1 christos l = strtol(argv[optidx], &cp2, base);
1499 1.1 christos if (cp2 == argv[optidx] || *cp2 != '\0' ||
1500 1.1 christos ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
1501 1.1 christos l < 0) {
1502 1.1 christos need_num_arg:
1503 1.1 christos error("You must supply a numeric argument "
1504 1.1 christos "to the %s command.", cmd);
1505 1.1 christos return -1;
1506 1.1 christos }
1507 1.1 christos *n_arg = l;
1508 1.1 christos if (cmdnum == I_LUMASK)
1509 1.1 christos break;
1510 1.1 christos /* Get pathname (mandatory) */
1511 1.1 christos if (argc - optidx < 2) {
1512 1.1 christos error("You must specify a path after a %s command.",
1513 1.1 christos cmd);
1514 1.1 christos return -1;
1515 1.1 christos }
1516 1.1 christos *path1 = xstrdup(argv[optidx + 1]);
1517 1.1 christos break;
1518 1.1 christos case I_QUIT:
1519 1.1 christos case I_PWD:
1520 1.1 christos case I_LPWD:
1521 1.1 christos case I_HELP:
1522 1.1 christos case I_VERSION:
1523 1.1 christos case I_PROGRESS:
1524 1.13 christos if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
1525 1.13 christos return -1;
1526 1.1 christos break;
1527 1.1 christos default:
1528 1.1 christos fatal("Command not implemented");
1529 1.1 christos }
1530 1.1 christos
1531 1.1 christos *cpp = cp;
1532 1.1 christos return(cmdnum);
1533 1.1 christos }
1534 1.1 christos
1535 1.1 christos static int
1536 1.1 christos parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
1537 1.26 christos const char *startdir, int err_abort, int echo_command)
1538 1.1 christos {
1539 1.26 christos const char *ocmd = cmd;
1540 1.1 christos char *path1, *path2, *tmp;
1541 1.26 christos int ignore_errors = 0, disable_echo = 1;
1542 1.26 christos int aflag = 0, fflag = 0, hflag = 0, iflag = 0;
1543 1.13 christos int lflag = 0, pflag = 0, rflag = 0, sflag = 0;
1544 1.7 christos int cmdnum, i;
1545 1.1 christos unsigned long n_arg = 0;
1546 1.1 christos Attrib a, *aa;
1547 1.14 christos char path_buf[PATH_MAX];
1548 1.1 christos int err = 0;
1549 1.1 christos glob_t g;
1550 1.1 christos
1551 1.2 christos pflag = 0; /* XXX gcc */
1552 1.2 christos lflag = 0; /* XXX gcc */
1553 1.2 christos iflag = 0; /* XXX gcc */
1554 1.2 christos hflag = 0; /* XXX gcc */
1555 1.2 christos n_arg = 0; /* XXX gcc */
1556 1.2 christos
1557 1.1 christos path1 = path2 = NULL;
1558 1.26 christos cmdnum = parse_args(&cmd, &ignore_errors, &disable_echo, &aflag, &fflag,
1559 1.26 christos &hflag, &iflag, &lflag, &pflag, &rflag, &sflag, &n_arg,
1560 1.26 christos &path1, &path2);
1561 1.13 christos if (ignore_errors != 0)
1562 1.1 christos err_abort = 0;
1563 1.1 christos
1564 1.26 christos if (echo_command && !disable_echo)
1565 1.26 christos mprintf("sftp> %s\n", ocmd);
1566 1.26 christos
1567 1.1 christos memset(&g, 0, sizeof(g));
1568 1.1 christos
1569 1.1 christos /* Perform command */
1570 1.1 christos switch (cmdnum) {
1571 1.1 christos case 0:
1572 1.1 christos /* Blank line */
1573 1.1 christos break;
1574 1.1 christos case -1:
1575 1.1 christos /* Unrecognized command */
1576 1.1 christos err = -1;
1577 1.1 christos break;
1578 1.12 christos case I_REGET:
1579 1.12 christos aflag = 1;
1580 1.12 christos /* FALLTHROUGH */
1581 1.1 christos case I_GET:
1582 1.12 christos err = process_get(conn, path1, path2, *pwd, pflag,
1583 1.13 christos rflag, aflag, fflag);
1584 1.1 christos break;
1585 1.13 christos case I_REPUT:
1586 1.13 christos aflag = 1;
1587 1.13 christos /* FALLTHROUGH */
1588 1.1 christos case I_PUT:
1589 1.13 christos err = process_put(conn, path1, path2, *pwd, pflag,
1590 1.13 christos rflag, aflag, fflag);
1591 1.1 christos break;
1592 1.1 christos case I_RENAME:
1593 1.1 christos path1 = make_absolute(path1, *pwd);
1594 1.1 christos path2 = make_absolute(path2, *pwd);
1595 1.13 christos err = do_rename(conn, path1, path2, lflag);
1596 1.1 christos break;
1597 1.1 christos case I_SYMLINK:
1598 1.7 christos sflag = 1;
1599 1.26 christos /* FALLTHROUGH */
1600 1.7 christos case I_LINK:
1601 1.13 christos if (!sflag)
1602 1.13 christos path1 = make_absolute(path1, *pwd);
1603 1.1 christos path2 = make_absolute(path2, *pwd);
1604 1.7 christos err = (sflag ? do_symlink : do_hardlink)(conn, path1, path2);
1605 1.1 christos break;
1606 1.1 christos case I_RM:
1607 1.1 christos path1 = make_absolute(path1, *pwd);
1608 1.1 christos remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1609 1.1 christos for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1610 1.12 christos if (!quiet)
1611 1.19 christos mprintf("Removing %s\n", g.gl_pathv[i]);
1612 1.1 christos err = do_rm(conn, g.gl_pathv[i]);
1613 1.1 christos if (err != 0 && err_abort)
1614 1.1 christos break;
1615 1.1 christos }
1616 1.1 christos break;
1617 1.1 christos case I_MKDIR:
1618 1.1 christos path1 = make_absolute(path1, *pwd);
1619 1.1 christos attrib_clear(&a);
1620 1.1 christos a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1621 1.1 christos a.perm = 0777;
1622 1.4 adam err = do_mkdir(conn, path1, &a, 1);
1623 1.1 christos break;
1624 1.1 christos case I_RMDIR:
1625 1.1 christos path1 = make_absolute(path1, *pwd);
1626 1.1 christos err = do_rmdir(conn, path1);
1627 1.1 christos break;
1628 1.1 christos case I_CHDIR:
1629 1.23 christos if (path1 == NULL || *path1 == '\0')
1630 1.23 christos path1 = xstrdup(startdir);
1631 1.1 christos path1 = make_absolute(path1, *pwd);
1632 1.1 christos if ((tmp = do_realpath(conn, path1)) == NULL) {
1633 1.1 christos err = 1;
1634 1.1 christos break;
1635 1.1 christos }
1636 1.1 christos if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1637 1.12 christos free(tmp);
1638 1.1 christos err = 1;
1639 1.1 christos break;
1640 1.1 christos }
1641 1.1 christos if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
1642 1.1 christos error("Can't change directory: Can't check target");
1643 1.12 christos free(tmp);
1644 1.1 christos err = 1;
1645 1.1 christos break;
1646 1.1 christos }
1647 1.1 christos if (!S_ISDIR(aa->perm)) {
1648 1.1 christos error("Can't change directory: \"%s\" is not "
1649 1.1 christos "a directory", tmp);
1650 1.12 christos free(tmp);
1651 1.1 christos err = 1;
1652 1.1 christos break;
1653 1.1 christos }
1654 1.12 christos free(*pwd);
1655 1.1 christos *pwd = tmp;
1656 1.1 christos break;
1657 1.1 christos case I_LS:
1658 1.1 christos if (!path1) {
1659 1.4 adam do_ls_dir(conn, *pwd, *pwd, lflag);
1660 1.1 christos break;
1661 1.1 christos }
1662 1.1 christos
1663 1.1 christos /* Strip pwd off beginning of non-absolute paths */
1664 1.1 christos tmp = NULL;
1665 1.26 christos if (!path_absolute(path1))
1666 1.1 christos tmp = *pwd;
1667 1.1 christos
1668 1.1 christos path1 = make_absolute(path1, *pwd);
1669 1.1 christos err = do_globbed_ls(conn, path1, tmp, lflag);
1670 1.1 christos break;
1671 1.1 christos case I_DF:
1672 1.1 christos /* Default to current directory if no path specified */
1673 1.1 christos if (path1 == NULL)
1674 1.1 christos path1 = xstrdup(*pwd);
1675 1.1 christos path1 = make_absolute(path1, *pwd);
1676 1.1 christos err = do_df(conn, path1, hflag, iflag);
1677 1.1 christos break;
1678 1.1 christos case I_LCHDIR:
1679 1.23 christos if (path1 == NULL || *path1 == '\0')
1680 1.23 christos path1 = xstrdup("~");
1681 1.14 christos tmp = tilde_expand_filename(path1, getuid());
1682 1.14 christos free(path1);
1683 1.14 christos path1 = tmp;
1684 1.1 christos if (chdir(path1) == -1) {
1685 1.1 christos error("Couldn't change local directory to "
1686 1.1 christos "\"%s\": %s", path1, strerror(errno));
1687 1.1 christos err = 1;
1688 1.1 christos }
1689 1.1 christos break;
1690 1.1 christos case I_LMKDIR:
1691 1.1 christos if (mkdir(path1, 0777) == -1) {
1692 1.1 christos error("Couldn't create local directory "
1693 1.1 christos "\"%s\": %s", path1, strerror(errno));
1694 1.1 christos err = 1;
1695 1.1 christos }
1696 1.1 christos break;
1697 1.1 christos case I_LLS:
1698 1.1 christos local_do_ls(cmd);
1699 1.1 christos break;
1700 1.1 christos case I_SHELL:
1701 1.1 christos local_do_shell(cmd);
1702 1.1 christos break;
1703 1.1 christos case I_LUMASK:
1704 1.1 christos umask(n_arg);
1705 1.1 christos printf("Local umask: %03lo\n", n_arg);
1706 1.1 christos break;
1707 1.1 christos case I_CHMOD:
1708 1.1 christos path1 = make_absolute(path1, *pwd);
1709 1.1 christos attrib_clear(&a);
1710 1.1 christos a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
1711 1.1 christos a.perm = n_arg;
1712 1.1 christos remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1713 1.1 christos for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1714 1.12 christos if (!quiet)
1715 1.19 christos mprintf("Changing mode on %s\n",
1716 1.19 christos g.gl_pathv[i]);
1717 1.26 christos err = (hflag ? do_lsetstat : do_setstat)(conn,
1718 1.26 christos g.gl_pathv[i], &a);
1719 1.1 christos if (err != 0 && err_abort)
1720 1.1 christos break;
1721 1.1 christos }
1722 1.1 christos break;
1723 1.1 christos case I_CHOWN:
1724 1.1 christos case I_CHGRP:
1725 1.1 christos path1 = make_absolute(path1, *pwd);
1726 1.1 christos remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1727 1.1 christos for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1728 1.26 christos if (!(aa = (hflag ? do_lstat : do_stat)(conn,
1729 1.26 christos g.gl_pathv[i], 0))) {
1730 1.1 christos if (err_abort) {
1731 1.1 christos err = -1;
1732 1.1 christos break;
1733 1.1 christos } else
1734 1.1 christos continue;
1735 1.1 christos }
1736 1.1 christos if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1737 1.1 christos error("Can't get current ownership of "
1738 1.1 christos "remote file \"%s\"", g.gl_pathv[i]);
1739 1.1 christos if (err_abort) {
1740 1.1 christos err = -1;
1741 1.1 christos break;
1742 1.1 christos } else
1743 1.1 christos continue;
1744 1.1 christos }
1745 1.1 christos aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1746 1.1 christos if (cmdnum == I_CHOWN) {
1747 1.12 christos if (!quiet)
1748 1.19 christos mprintf("Changing owner on %s\n",
1749 1.12 christos g.gl_pathv[i]);
1750 1.1 christos aa->uid = n_arg;
1751 1.1 christos } else {
1752 1.12 christos if (!quiet)
1753 1.19 christos mprintf("Changing group on %s\n",
1754 1.12 christos g.gl_pathv[i]);
1755 1.1 christos aa->gid = n_arg;
1756 1.1 christos }
1757 1.26 christos err = (hflag ? do_lsetstat : do_setstat)(conn,
1758 1.26 christos g.gl_pathv[i], aa);
1759 1.1 christos if (err != 0 && err_abort)
1760 1.1 christos break;
1761 1.1 christos }
1762 1.1 christos break;
1763 1.1 christos case I_PWD:
1764 1.19 christos mprintf("Remote working directory: %s\n", *pwd);
1765 1.1 christos break;
1766 1.1 christos case I_LPWD:
1767 1.1 christos if (!getcwd(path_buf, sizeof(path_buf))) {
1768 1.1 christos error("Couldn't get local cwd: %s", strerror(errno));
1769 1.1 christos err = -1;
1770 1.1 christos break;
1771 1.1 christos }
1772 1.19 christos mprintf("Local working directory: %s\n", path_buf);
1773 1.1 christos break;
1774 1.1 christos case I_QUIT:
1775 1.1 christos /* Processed below */
1776 1.1 christos break;
1777 1.1 christos case I_HELP:
1778 1.1 christos help();
1779 1.1 christos break;
1780 1.1 christos case I_VERSION:
1781 1.1 christos printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1782 1.1 christos break;
1783 1.1 christos case I_PROGRESS:
1784 1.1 christos showprogress = !showprogress;
1785 1.1 christos if (showprogress)
1786 1.1 christos printf("Progress meter enabled\n");
1787 1.1 christos else
1788 1.1 christos printf("Progress meter disabled\n");
1789 1.1 christos break;
1790 1.1 christos default:
1791 1.1 christos fatal("%d is not implemented", cmdnum);
1792 1.1 christos }
1793 1.1 christos
1794 1.1 christos if (g.gl_pathc)
1795 1.1 christos globfree(&g);
1796 1.12 christos free(path1);
1797 1.12 christos free(path2);
1798 1.1 christos
1799 1.1 christos /* If an unignored error occurs in batch mode we should abort. */
1800 1.1 christos if (err_abort && err != 0)
1801 1.1 christos return (-1);
1802 1.1 christos else if (cmdnum == I_QUIT)
1803 1.1 christos return (1);
1804 1.1 christos
1805 1.1 christos return (0);
1806 1.1 christos }
1807 1.1 christos
1808 1.7 christos static const char *
1809 1.1 christos prompt(EditLine *el)
1810 1.1 christos {
1811 1.1 christos return ("sftp> ");
1812 1.1 christos }
1813 1.1 christos
1814 1.4 adam /* Display entries in 'list' after skipping the first 'len' chars */
1815 1.4 adam static void
1816 1.4 adam complete_display(char **list, u_int len)
1817 1.4 adam {
1818 1.4 adam u_int y, m = 0, width = 80, columns = 1, colspace = 0, llen;
1819 1.4 adam struct winsize ws;
1820 1.7 christos const char *tmp;
1821 1.4 adam
1822 1.4 adam /* Count entries for sort and find longest */
1823 1.13 christos for (y = 0; list[y]; y++)
1824 1.20 christos m = MAXIMUM(m, strlen(list[y]));
1825 1.4 adam
1826 1.4 adam if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
1827 1.4 adam width = ws.ws_col;
1828 1.4 adam
1829 1.4 adam m = m > len ? m - len : 0;
1830 1.4 adam columns = width / (m + 2);
1831 1.20 christos columns = MAXIMUM(columns, 1);
1832 1.4 adam colspace = width / columns;
1833 1.20 christos colspace = MINIMUM(colspace, width);
1834 1.4 adam
1835 1.4 adam printf("\n");
1836 1.4 adam m = 1;
1837 1.4 adam for (y = 0; list[y]; y++) {
1838 1.4 adam llen = strlen(list[y]);
1839 1.4 adam tmp = llen > len ? list[y] + len : "";
1840 1.19 christos mprintf("%-*s", colspace, tmp);
1841 1.4 adam if (m >= columns) {
1842 1.4 adam printf("\n");
1843 1.4 adam m = 1;
1844 1.4 adam } else
1845 1.4 adam m++;
1846 1.4 adam }
1847 1.4 adam printf("\n");
1848 1.4 adam }
1849 1.4 adam
1850 1.4 adam /*
1851 1.4 adam * Given a "list" of words that begin with a common prefix of "word",
1852 1.4 adam * attempt to find an autocompletion to extends "word" by the next
1853 1.4 adam * characters common to all entries in "list".
1854 1.4 adam */
1855 1.4 adam static char *
1856 1.4 adam complete_ambiguous(const char *word, char **list, size_t count)
1857 1.4 adam {
1858 1.4 adam if (word == NULL)
1859 1.4 adam return NULL;
1860 1.4 adam
1861 1.4 adam if (count > 0) {
1862 1.4 adam u_int y, matchlen = strlen(list[0]);
1863 1.4 adam
1864 1.4 adam /* Find length of common stem */
1865 1.4 adam for (y = 1; list[y]; y++) {
1866 1.4 adam u_int x;
1867 1.4 adam
1868 1.13 christos for (x = 0; x < matchlen; x++)
1869 1.13 christos if (list[0][x] != list[y][x])
1870 1.4 adam break;
1871 1.4 adam
1872 1.4 adam matchlen = x;
1873 1.4 adam }
1874 1.4 adam
1875 1.4 adam if (matchlen > strlen(word)) {
1876 1.4 adam char *tmp = xstrdup(list[0]);
1877 1.4 adam
1878 1.4 adam tmp[matchlen] = '\0';
1879 1.4 adam return tmp;
1880 1.4 adam }
1881 1.13 christos }
1882 1.4 adam
1883 1.4 adam return xstrdup(word);
1884 1.4 adam }
1885 1.4 adam
1886 1.4 adam /* Autocomplete a sftp command */
1887 1.4 adam static int
1888 1.4 adam complete_cmd_parse(EditLine *el, char *cmd, int lastarg, char quote,
1889 1.4 adam int terminated)
1890 1.4 adam {
1891 1.4 adam u_int y, count = 0, cmdlen, tmplen;
1892 1.4 adam char *tmp, **list, argterm[3];
1893 1.4 adam const LineInfo *lf;
1894 1.4 adam
1895 1.4 adam list = xcalloc((sizeof(cmds) / sizeof(*cmds)) + 1, sizeof(char *));
1896 1.4 adam
1897 1.4 adam /* No command specified: display all available commands */
1898 1.4 adam if (cmd == NULL) {
1899 1.4 adam for (y = 0; cmds[y].c; y++)
1900 1.4 adam list[count++] = xstrdup(cmds[y].c);
1901 1.13 christos
1902 1.4 adam list[count] = NULL;
1903 1.4 adam complete_display(list, 0);
1904 1.4 adam
1905 1.13 christos for (y = 0; list[y] != NULL; y++)
1906 1.13 christos free(list[y]);
1907 1.12 christos free(list);
1908 1.4 adam return count;
1909 1.4 adam }
1910 1.4 adam
1911 1.4 adam /* Prepare subset of commands that start with "cmd" */
1912 1.4 adam cmdlen = strlen(cmd);
1913 1.4 adam for (y = 0; cmds[y].c; y++) {
1914 1.13 christos if (!strncasecmp(cmd, cmds[y].c, cmdlen))
1915 1.4 adam list[count++] = xstrdup(cmds[y].c);
1916 1.4 adam }
1917 1.4 adam list[count] = NULL;
1918 1.4 adam
1919 1.9 christos if (count == 0) {
1920 1.12 christos free(list);
1921 1.4 adam return 0;
1922 1.9 christos }
1923 1.4 adam
1924 1.24 christos /* Complete ambiguous command */
1925 1.4 adam tmp = complete_ambiguous(cmd, list, count);
1926 1.4 adam if (count > 1)
1927 1.4 adam complete_display(list, 0);
1928 1.4 adam
1929 1.13 christos for (y = 0; list[y]; y++)
1930 1.13 christos free(list[y]);
1931 1.12 christos free(list);
1932 1.4 adam
1933 1.4 adam if (tmp != NULL) {
1934 1.4 adam tmplen = strlen(tmp);
1935 1.4 adam cmdlen = strlen(cmd);
1936 1.4 adam /* If cmd may be extended then do so */
1937 1.4 adam if (tmplen > cmdlen)
1938 1.4 adam if (el_insertstr(el, tmp + cmdlen) == -1)
1939 1.4 adam fatal("el_insertstr failed.");
1940 1.4 adam lf = el_line(el);
1941 1.4 adam /* Terminate argument cleanly */
1942 1.4 adam if (count == 1) {
1943 1.4 adam y = 0;
1944 1.4 adam if (!terminated)
1945 1.4 adam argterm[y++] = quote;
1946 1.4 adam if (lastarg || *(lf->cursor) != ' ')
1947 1.4 adam argterm[y++] = ' ';
1948 1.4 adam argterm[y] = '\0';
1949 1.4 adam if (y > 0 && el_insertstr(el, argterm) == -1)
1950 1.4 adam fatal("el_insertstr failed.");
1951 1.4 adam }
1952 1.12 christos free(tmp);
1953 1.4 adam }
1954 1.4 adam
1955 1.4 adam return count;
1956 1.4 adam }
1957 1.4 adam
1958 1.4 adam /*
1959 1.4 adam * Determine whether a particular sftp command's arguments (if any)
1960 1.4 adam * represent local or remote files.
1961 1.4 adam */
1962 1.4 adam static int
1963 1.4 adam complete_is_remote(char *cmd) {
1964 1.4 adam int i;
1965 1.4 adam
1966 1.4 adam if (cmd == NULL)
1967 1.4 adam return -1;
1968 1.4 adam
1969 1.4 adam for (i = 0; cmds[i].c; i++) {
1970 1.13 christos if (!strncasecmp(cmd, cmds[i].c, strlen(cmds[i].c)))
1971 1.4 adam return cmds[i].t;
1972 1.4 adam }
1973 1.4 adam
1974 1.4 adam return -1;
1975 1.4 adam }
1976 1.4 adam
1977 1.4 adam /* Autocomplete a filename "file" */
1978 1.4 adam static int
1979 1.4 adam complete_match(EditLine *el, struct sftp_conn *conn, char *remote_path,
1980 1.4 adam char *file, int remote, int lastarg, char quote, int terminated)
1981 1.4 adam {
1982 1.4 adam glob_t g;
1983 1.12 christos char *tmp, *tmp2, ins[8];
1984 1.11 christos u_int i, hadglob, pwdlen, len, tmplen, filelen, cesc, isesc, isabs;
1985 1.12 christos int clen;
1986 1.4 adam const LineInfo *lf;
1987 1.13 christos
1988 1.4 adam /* Glob from "file" location */
1989 1.4 adam if (file == NULL)
1990 1.4 adam tmp = xstrdup("*");
1991 1.4 adam else
1992 1.4 adam xasprintf(&tmp, "%s*", file);
1993 1.4 adam
1994 1.11 christos /* Check if the path is absolute. */
1995 1.26 christos isabs = path_absolute(tmp);
1996 1.11 christos
1997 1.4 adam memset(&g, 0, sizeof(g));
1998 1.4 adam if (remote != LOCAL) {
1999 1.4 adam tmp = make_absolute(tmp, remote_path);
2000 1.4 adam remote_glob(conn, tmp, GLOB_DOOFFS|GLOB_MARK, NULL, &g);
2001 1.17 christos } else
2002 1.6 christos glob(tmp, GLOB_LIMIT|GLOB_DOOFFS|GLOB_MARK, NULL, &g);
2003 1.4 adam
2004 1.4 adam /* Determine length of pwd so we can trim completion display */
2005 1.4 adam for (hadglob = tmplen = pwdlen = 0; tmp[tmplen] != 0; tmplen++) {
2006 1.4 adam /* Terminate counting on first unescaped glob metacharacter */
2007 1.4 adam if (tmp[tmplen] == '*' || tmp[tmplen] == '?') {
2008 1.4 adam if (tmp[tmplen] != '*' || tmp[tmplen + 1] != '\0')
2009 1.4 adam hadglob = 1;
2010 1.4 adam break;
2011 1.4 adam }
2012 1.4 adam if (tmp[tmplen] == '\\' && tmp[tmplen + 1] != '\0')
2013 1.4 adam tmplen++;
2014 1.4 adam if (tmp[tmplen] == '/')
2015 1.4 adam pwdlen = tmplen + 1; /* track last seen '/' */
2016 1.4 adam }
2017 1.12 christos free(tmp);
2018 1.13 christos tmp = NULL;
2019 1.4 adam
2020 1.13 christos if (g.gl_matchc == 0)
2021 1.4 adam goto out;
2022 1.4 adam
2023 1.4 adam if (g.gl_matchc > 1)
2024 1.4 adam complete_display(g.gl_pathv, pwdlen);
2025 1.4 adam
2026 1.4 adam /* Don't try to extend globs */
2027 1.4 adam if (file == NULL || hadglob)
2028 1.4 adam goto out;
2029 1.4 adam
2030 1.4 adam tmp2 = complete_ambiguous(file, g.gl_pathv, g.gl_matchc);
2031 1.11 christos tmp = path_strip(tmp2, isabs ? NULL : remote_path);
2032 1.12 christos free(tmp2);
2033 1.4 adam
2034 1.4 adam if (tmp == NULL)
2035 1.4 adam goto out;
2036 1.4 adam
2037 1.4 adam tmplen = strlen(tmp);
2038 1.4 adam filelen = strlen(file);
2039 1.4 adam
2040 1.11 christos /* Count the number of escaped characters in the input string. */
2041 1.11 christos cesc = isesc = 0;
2042 1.11 christos for (i = 0; i < filelen; i++) {
2043 1.11 christos if (!isesc && file[i] == '\\' && i + 1 < filelen){
2044 1.11 christos isesc = 1;
2045 1.11 christos cesc++;
2046 1.11 christos } else
2047 1.11 christos isesc = 0;
2048 1.11 christos }
2049 1.11 christos
2050 1.11 christos if (tmplen > (filelen - cesc)) {
2051 1.11 christos tmp2 = tmp + filelen - cesc;
2052 1.13 christos len = strlen(tmp2);
2053 1.4 adam /* quote argument on way out */
2054 1.12 christos for (i = 0; i < len; i += clen) {
2055 1.12 christos if ((clen = mblen(tmp2 + i, len - i)) < 0 ||
2056 1.12 christos (size_t)clen > sizeof(ins) - 2)
2057 1.12 christos fatal("invalid multibyte character");
2058 1.4 adam ins[0] = '\\';
2059 1.12 christos memcpy(ins + 1, tmp2 + i, clen);
2060 1.12 christos ins[clen + 1] = '\0';
2061 1.4 adam switch (tmp2[i]) {
2062 1.4 adam case '\'':
2063 1.4 adam case '"':
2064 1.4 adam case '\\':
2065 1.4 adam case '\t':
2066 1.7 christos case '[':
2067 1.4 adam case ' ':
2068 1.11 christos case '#':
2069 1.11 christos case '*':
2070 1.4 adam if (quote == '\0' || tmp2[i] == quote) {
2071 1.4 adam if (el_insertstr(el, ins) == -1)
2072 1.4 adam fatal("el_insertstr "
2073 1.4 adam "failed.");
2074 1.4 adam break;
2075 1.4 adam }
2076 1.4 adam /* FALLTHROUGH */
2077 1.4 adam default:
2078 1.4 adam if (el_insertstr(el, ins + 1) == -1)
2079 1.4 adam fatal("el_insertstr failed.");
2080 1.4 adam break;
2081 1.4 adam }
2082 1.4 adam }
2083 1.4 adam }
2084 1.4 adam
2085 1.4 adam lf = el_line(el);
2086 1.4 adam if (g.gl_matchc == 1) {
2087 1.4 adam i = 0;
2088 1.13 christos if (!terminated && quote != '\0')
2089 1.4 adam ins[i++] = quote;
2090 1.4 adam if (*(lf->cursor - 1) != '/' &&
2091 1.4 adam (lastarg || *(lf->cursor) != ' '))
2092 1.4 adam ins[i++] = ' ';
2093 1.4 adam ins[i] = '\0';
2094 1.4 adam if (i > 0 && el_insertstr(el, ins) == -1)
2095 1.4 adam fatal("el_insertstr failed.");
2096 1.4 adam }
2097 1.12 christos free(tmp);
2098 1.4 adam
2099 1.4 adam out:
2100 1.4 adam globfree(&g);
2101 1.4 adam return g.gl_matchc;
2102 1.4 adam }
2103 1.4 adam
2104 1.4 adam /* tab-completion hook function, called via libedit */
2105 1.4 adam static unsigned char
2106 1.4 adam complete(EditLine *el, int ch)
2107 1.4 adam {
2108 1.13 christos char **argv, *line, quote;
2109 1.12 christos int argc, carg;
2110 1.12 christos u_int cursor, len, terminated, ret = CC_ERROR;
2111 1.4 adam const LineInfo *lf;
2112 1.4 adam struct complete_ctx *complete_ctx;
2113 1.4 adam
2114 1.4 adam lf = el_line(el);
2115 1.5 adam if (el_get(el, EL_CLIENTDATA, &complete_ctx) != 0)
2116 1.4 adam fatal("%s: el_get failed", __func__);
2117 1.4 adam
2118 1.4 adam /* Figure out which argument the cursor points to */
2119 1.4 adam cursor = lf->cursor - lf->buffer;
2120 1.16 christos line = xmalloc(cursor + 1);
2121 1.4 adam memcpy(line, lf->buffer, cursor);
2122 1.4 adam line[cursor] = '\0';
2123 1.4 adam argv = makeargv(line, &carg, 1, "e, &terminated);
2124 1.12 christos free(line);
2125 1.4 adam
2126 1.4 adam /* Get all the arguments on the line */
2127 1.4 adam len = lf->lastchar - lf->buffer;
2128 1.16 christos line = xmalloc(len + 1);
2129 1.4 adam memcpy(line, lf->buffer, len);
2130 1.4 adam line[len] = '\0';
2131 1.4 adam argv = makeargv(line, &argc, 1, NULL, NULL);
2132 1.4 adam
2133 1.4 adam /* Ensure cursor is at EOL or a argument boundary */
2134 1.4 adam if (line[cursor] != ' ' && line[cursor] != '\0' &&
2135 1.4 adam line[cursor] != '\n') {
2136 1.12 christos free(line);
2137 1.4 adam return ret;
2138 1.4 adam }
2139 1.4 adam
2140 1.4 adam if (carg == 0) {
2141 1.4 adam /* Show all available commands */
2142 1.4 adam complete_cmd_parse(el, NULL, argc == carg, '\0', 1);
2143 1.4 adam ret = CC_REDISPLAY;
2144 1.4 adam } else if (carg == 1 && cursor > 0 && line[cursor - 1] != ' ') {
2145 1.4 adam /* Handle the command parsing */
2146 1.4 adam if (complete_cmd_parse(el, argv[0], argc == carg,
2147 1.13 christos quote, terminated) != 0)
2148 1.4 adam ret = CC_REDISPLAY;
2149 1.4 adam } else if (carg >= 1) {
2150 1.4 adam /* Handle file parsing */
2151 1.4 adam int remote = complete_is_remote(argv[0]);
2152 1.4 adam char *filematch = NULL;
2153 1.4 adam
2154 1.4 adam if (carg > 1 && line[cursor-1] != ' ')
2155 1.4 adam filematch = argv[carg - 1];
2156 1.4 adam
2157 1.4 adam if (remote != 0 &&
2158 1.4 adam complete_match(el, complete_ctx->conn,
2159 1.4 adam *complete_ctx->remote_pathp, filematch,
2160 1.13 christos remote, carg == argc, quote, terminated) != 0)
2161 1.4 adam ret = CC_REDISPLAY;
2162 1.4 adam }
2163 1.4 adam
2164 1.13 christos free(line);
2165 1.4 adam return ret;
2166 1.4 adam }
2167 1.4 adam
2168 1.23 christos static int
2169 1.7 christos interactive_loop(struct sftp_conn *conn, const char *file1, const char *file2)
2170 1.1 christos {
2171 1.4 adam char *remote_path;
2172 1.23 christos char *dir = NULL, *startdir = NULL;
2173 1.1 christos char cmd[2048];
2174 1.1 christos int err, interactive;
2175 1.1 christos EditLine *el = NULL;
2176 1.1 christos History *hl = NULL;
2177 1.1 christos HistEvent hev;
2178 1.1 christos extern char *__progname;
2179 1.4 adam struct complete_ctx complete_ctx;
2180 1.1 christos
2181 1.1 christos if (!batchmode && isatty(STDIN_FILENO)) {
2182 1.1 christos if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL)
2183 1.1 christos fatal("Couldn't initialise editline");
2184 1.1 christos if ((hl = history_init()) == NULL)
2185 1.1 christos fatal("Couldn't initialise editline history");
2186 1.1 christos history(hl, &hev, H_SETSIZE, 100);
2187 1.1 christos el_set(el, EL_HIST, history, hl);
2188 1.1 christos
2189 1.1 christos el_set(el, EL_PROMPT, prompt);
2190 1.1 christos el_set(el, EL_EDITOR, "emacs");
2191 1.1 christos el_set(el, EL_TERMINAL, NULL);
2192 1.1 christos el_set(el, EL_SIGNAL, 1);
2193 1.1 christos el_source(el, NULL);
2194 1.4 adam
2195 1.4 adam /* Tab Completion */
2196 1.13 christos el_set(el, EL_ADDFN, "ftp-complete",
2197 1.7 christos "Context sensitive argument completion", complete);
2198 1.4 adam complete_ctx.conn = conn;
2199 1.4 adam complete_ctx.remote_pathp = &remote_path;
2200 1.4 adam el_set(el, EL_CLIENTDATA, (void*)&complete_ctx);
2201 1.4 adam el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
2202 1.13 christos /* enable ctrl-left-arrow and ctrl-right-arrow */
2203 1.13 christos el_set(el, EL_BIND, "\\e[1;5C", "em-next-word", NULL);
2204 1.27 christos el_set(el, EL_BIND, "\\e\\e[C", "em-next-word", NULL);
2205 1.13 christos el_set(el, EL_BIND, "\\e[1;5D", "ed-prev-word", NULL);
2206 1.13 christos el_set(el, EL_BIND, "\\e\\e[D", "ed-prev-word", NULL);
2207 1.13 christos /* make ^w match ksh behaviour */
2208 1.13 christos el_set(el, EL_BIND, "^w", "ed-delete-prev-word", NULL);
2209 1.1 christos }
2210 1.1 christos
2211 1.4 adam remote_path = do_realpath(conn, ".");
2212 1.4 adam if (remote_path == NULL)
2213 1.1 christos fatal("Need cwd");
2214 1.23 christos startdir = xstrdup(remote_path);
2215 1.1 christos
2216 1.1 christos if (file1 != NULL) {
2217 1.1 christos dir = xstrdup(file1);
2218 1.4 adam dir = make_absolute(dir, remote_path);
2219 1.1 christos
2220 1.1 christos if (remote_is_dir(conn, dir) && file2 == NULL) {
2221 1.12 christos if (!quiet)
2222 1.19 christos mprintf("Changing to: %s\n", dir);
2223 1.1 christos snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
2224 1.4 adam if (parse_dispatch_command(conn, cmd,
2225 1.26 christos &remote_path, startdir, 1, 0) != 0) {
2226 1.12 christos free(dir);
2227 1.23 christos free(startdir);
2228 1.12 christos free(remote_path);
2229 1.12 christos free(conn);
2230 1.1 christos return (-1);
2231 1.1 christos }
2232 1.1 christos } else {
2233 1.11 christos /* XXX this is wrong wrt quoting */
2234 1.12 christos snprintf(cmd, sizeof cmd, "get%s %s%s%s",
2235 1.12 christos global_aflag ? " -a" : "", dir,
2236 1.12 christos file2 == NULL ? "" : " ",
2237 1.12 christos file2 == NULL ? "" : file2);
2238 1.4 adam err = parse_dispatch_command(conn, cmd,
2239 1.26 christos &remote_path, startdir, 1, 0);
2240 1.12 christos free(dir);
2241 1.23 christos free(startdir);
2242 1.12 christos free(remote_path);
2243 1.12 christos free(conn);
2244 1.1 christos return (err);
2245 1.1 christos }
2246 1.12 christos free(dir);
2247 1.1 christos }
2248 1.1 christos
2249 1.14 christos setvbuf(stdout, NULL, _IOLBF, 0);
2250 1.14 christos setvbuf(infile, NULL, _IOLBF, 0);
2251 1.1 christos
2252 1.1 christos interactive = !batchmode && isatty(STDIN_FILENO);
2253 1.1 christos err = 0;
2254 1.1 christos for (;;) {
2255 1.1 christos const char *line;
2256 1.1 christos int count = 0;
2257 1.1 christos
2258 1.28 christos ssh_signal(SIGINT, SIG_IGN);
2259 1.1 christos
2260 1.1 christos if (el == NULL) {
2261 1.1 christos if (interactive)
2262 1.1 christos printf("sftp> ");
2263 1.1 christos if (fgets(cmd, sizeof(cmd), infile) == NULL) {
2264 1.1 christos if (interactive)
2265 1.1 christos printf("\n");
2266 1.1 christos break;
2267 1.1 christos }
2268 1.1 christos } else {
2269 1.4 adam if ((line = el_gets(el, &count)) == NULL ||
2270 1.4 adam count <= 0) {
2271 1.1 christos printf("\n");
2272 1.1 christos break;
2273 1.1 christos }
2274 1.1 christos history(hl, &hev, H_ENTER, line);
2275 1.1 christos if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
2276 1.1 christos fprintf(stderr, "Error: input line too long\n");
2277 1.1 christos continue;
2278 1.1 christos }
2279 1.1 christos }
2280 1.1 christos
2281 1.26 christos cmd[strcspn(cmd, "\n")] = '\0';
2282 1.1 christos
2283 1.1 christos /* Handle user interrupts gracefully during commands */
2284 1.1 christos interrupted = 0;
2285 1.28 christos ssh_signal(SIGINT, cmd_interrupt);
2286 1.1 christos
2287 1.4 adam err = parse_dispatch_command(conn, cmd, &remote_path,
2288 1.26 christos startdir, batchmode, !interactive && el == NULL);
2289 1.1 christos if (err != 0)
2290 1.1 christos break;
2291 1.1 christos }
2292 1.28 christos ssh_signal(SIGCHLD, SIG_DFL);
2293 1.12 christos free(remote_path);
2294 1.23 christos free(startdir);
2295 1.12 christos free(conn);
2296 1.1 christos
2297 1.1 christos if (el != NULL)
2298 1.1 christos el_end(el);
2299 1.1 christos
2300 1.1 christos /* err == 1 signifies normal "quit" exit */
2301 1.1 christos return (err >= 0 ? 0 : -1);
2302 1.1 christos }
2303 1.1 christos
2304 1.1 christos static void
2305 1.7 christos connect_to_server(const char *path, char **args, int *in, int *out)
2306 1.1 christos {
2307 1.1 christos int c_in, c_out;
2308 1.1 christos
2309 1.1 christos int inout[2];
2310 1.1 christos
2311 1.1 christos if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
2312 1.1 christos fatal("socketpair: %s", strerror(errno));
2313 1.1 christos *in = *out = inout[0];
2314 1.1 christos c_in = c_out = inout[1];
2315 1.1 christos
2316 1.1 christos if ((sshpid = fork()) == -1)
2317 1.1 christos fatal("fork: %s", strerror(errno));
2318 1.1 christos else if (sshpid == 0) {
2319 1.1 christos if ((dup2(c_in, STDIN_FILENO) == -1) ||
2320 1.1 christos (dup2(c_out, STDOUT_FILENO) == -1)) {
2321 1.1 christos fprintf(stderr, "dup2: %s\n", strerror(errno));
2322 1.1 christos _exit(1);
2323 1.1 christos }
2324 1.1 christos close(*in);
2325 1.1 christos close(*out);
2326 1.1 christos close(c_in);
2327 1.1 christos close(c_out);
2328 1.1 christos
2329 1.1 christos /*
2330 1.1 christos * The underlying ssh is in the same process group, so we must
2331 1.1 christos * ignore SIGINT if we want to gracefully abort commands,
2332 1.1 christos * otherwise the signal will make it to the ssh process and
2333 1.4 adam * kill it too. Contrawise, since sftp sends SIGTERMs to the
2334 1.4 adam * underlying ssh, it must *not* ignore that signal.
2335 1.1 christos */
2336 1.28 christos ssh_signal(SIGINT, SIG_IGN);
2337 1.28 christos ssh_signal(SIGTERM, SIG_DFL);
2338 1.1 christos execvp(path, args);
2339 1.1 christos fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
2340 1.1 christos _exit(1);
2341 1.1 christos }
2342 1.1 christos
2343 1.28 christos ssh_signal(SIGTERM, killchild);
2344 1.28 christos ssh_signal(SIGINT, killchild);
2345 1.28 christos ssh_signal(SIGHUP, killchild);
2346 1.28 christos ssh_signal(SIGTSTP, suspchild);
2347 1.28 christos ssh_signal(SIGTTIN, suspchild);
2348 1.28 christos ssh_signal(SIGTTOU, suspchild);
2349 1.28 christos ssh_signal(SIGCHLD, sigchld_handler);
2350 1.1 christos close(c_in);
2351 1.1 christos close(c_out);
2352 1.1 christos }
2353 1.1 christos
2354 1.8 joerg __dead static void
2355 1.1 christos usage(void)
2356 1.1 christos {
2357 1.1 christos extern char *__progname;
2358 1.1 christos
2359 1.1 christos fprintf(stderr,
2360 1.29 christos "usage: %s [-46aCfNpqrv] [-B buffer_size] [-b batchfile] [-c cipher]\n"
2361 1.26 christos " [-D sftp_server_path] [-F ssh_config] [-i identity_file]\n"
2362 1.26 christos " [-J destination] [-l limit] [-o ssh_option] [-P port]\n"
2363 1.26 christos " [-R num_requests] [-S program] [-s subsystem | sftp_server]\n"
2364 1.26 christos " destination\n",
2365 1.23 christos __progname);
2366 1.1 christos exit(1);
2367 1.1 christos }
2368 1.1 christos
2369 1.1 christos int
2370 1.1 christos main(int argc, char **argv)
2371 1.1 christos {
2372 1.29 christos int in, out, ch, err, tmp, port = -1, noisy = 0;
2373 1.23 christos char *host = NULL, *user, *cp, *file2 = NULL;
2374 1.29 christos int debug_level = 0;
2375 1.7 christos const char *file1 = NULL, *sftp_server = NULL;
2376 1.7 christos const char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
2377 1.7 christos const char *errstr;
2378 1.1 christos LogLevel ll = SYSLOG_LEVEL_INFO;
2379 1.1 christos arglist args;
2380 1.1 christos extern int optind;
2381 1.1 christos extern char *optarg;
2382 1.4 adam struct sftp_conn *conn;
2383 1.4 adam size_t copy_buffer_len = DEFAULT_COPY_BUFLEN;
2384 1.4 adam size_t num_requests = DEFAULT_NUM_REQUESTS;
2385 1.7 christos long long limit_kbps = 0;
2386 1.1 christos
2387 1.1 christos /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
2388 1.1 christos sanitise_stdfd();
2389 1.12 christos setlocale(LC_CTYPE, "");
2390 1.1 christos
2391 1.1 christos memset(&args, '\0', sizeof(args));
2392 1.1 christos args.list = NULL;
2393 1.1 christos addargs(&args, "%s", ssh_program);
2394 1.1 christos addargs(&args, "-oForwardX11 no");
2395 1.1 christos addargs(&args, "-oForwardAgent no");
2396 1.1 christos addargs(&args, "-oPermitLocalCommand no");
2397 1.1 christos addargs(&args, "-oClearAllForwardings yes");
2398 1.1 christos
2399 1.1 christos ll = SYSLOG_LEVEL_INFO;
2400 1.1 christos infile = stdin;
2401 1.1 christos
2402 1.4 adam while ((ch = getopt(argc, argv,
2403 1.29 christos "1246afhNpqrvCc:D:i:l:o:s:S:b:B:F:J:P:R:")) != -1) {
2404 1.1 christos switch (ch) {
2405 1.4 adam /* Passed through to ssh(1) */
2406 1.4 adam case '4':
2407 1.4 adam case '6':
2408 1.1 christos case 'C':
2409 1.4 adam addargs(&args, "-%c", ch);
2410 1.4 adam break;
2411 1.4 adam /* Passed through to ssh(1) with argument */
2412 1.4 adam case 'F':
2413 1.26 christos case 'J':
2414 1.4 adam case 'c':
2415 1.4 adam case 'i':
2416 1.4 adam case 'o':
2417 1.4 adam addargs(&args, "-%c", ch);
2418 1.4 adam addargs(&args, "%s", optarg);
2419 1.4 adam break;
2420 1.4 adam case 'q':
2421 1.12 christos ll = SYSLOG_LEVEL_ERROR;
2422 1.12 christos quiet = 1;
2423 1.4 adam showprogress = 0;
2424 1.4 adam addargs(&args, "-%c", ch);
2425 1.4 adam break;
2426 1.4 adam case 'P':
2427 1.23 christos port = a2port(optarg);
2428 1.23 christos if (port <= 0)
2429 1.23 christos fatal("Bad port \"%s\"\n", optarg);
2430 1.1 christos break;
2431 1.1 christos case 'v':
2432 1.1 christos if (debug_level < 3) {
2433 1.1 christos addargs(&args, "-v");
2434 1.1 christos ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
2435 1.1 christos }
2436 1.1 christos debug_level++;
2437 1.1 christos break;
2438 1.1 christos case '1':
2439 1.29 christos fatal("SSH protocol v.1 is no longer supported");
2440 1.1 christos break;
2441 1.4 adam case '2':
2442 1.29 christos /* accept silently */
2443 1.1 christos break;
2444 1.12 christos case 'a':
2445 1.12 christos global_aflag = 1;
2446 1.12 christos break;
2447 1.4 adam case 'B':
2448 1.4 adam copy_buffer_len = strtol(optarg, &cp, 10);
2449 1.4 adam if (copy_buffer_len == 0 || *cp != '\0')
2450 1.4 adam fatal("Invalid buffer size \"%s\"", optarg);
2451 1.1 christos break;
2452 1.1 christos case 'b':
2453 1.1 christos if (batchmode)
2454 1.1 christos fatal("Batch file already specified.");
2455 1.1 christos
2456 1.1 christos /* Allow "-" as stdin */
2457 1.1 christos if (strcmp(optarg, "-") != 0 &&
2458 1.1 christos (infile = fopen(optarg, "r")) == NULL)
2459 1.1 christos fatal("%s (%s).", strerror(errno), optarg);
2460 1.1 christos showprogress = 0;
2461 1.12 christos quiet = batchmode = 1;
2462 1.1 christos addargs(&args, "-obatchmode yes");
2463 1.1 christos break;
2464 1.13 christos case 'f':
2465 1.13 christos global_fflag = 1;
2466 1.13 christos break;
2467 1.29 christos case 'N':
2468 1.29 christos noisy = 1; /* Used to clear quiet mode after getopt */
2469 1.29 christos break;
2470 1.4 adam case 'p':
2471 1.4 adam global_pflag = 1;
2472 1.4 adam break;
2473 1.4 adam case 'D':
2474 1.1 christos sftp_direct = optarg;
2475 1.1 christos break;
2476 1.7 christos case 'l':
2477 1.7 christos limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
2478 1.7 christos &errstr);
2479 1.7 christos if (errstr != NULL)
2480 1.7 christos usage();
2481 1.7 christos limit_kbps *= 1024; /* kbps */
2482 1.7 christos break;
2483 1.4 adam case 'r':
2484 1.4 adam global_rflag = 1;
2485 1.1 christos break;
2486 1.1 christos case 'R':
2487 1.1 christos num_requests = strtol(optarg, &cp, 10);
2488 1.1 christos if (num_requests == 0 || *cp != '\0')
2489 1.1 christos fatal("Invalid number of requests \"%s\"",
2490 1.1 christos optarg);
2491 1.1 christos break;
2492 1.4 adam case 's':
2493 1.4 adam sftp_server = optarg;
2494 1.4 adam break;
2495 1.4 adam case 'S':
2496 1.4 adam ssh_program = optarg;
2497 1.4 adam replacearg(&args, 0, "%s", ssh_program);
2498 1.4 adam break;
2499 1.1 christos case 'h':
2500 1.1 christos default:
2501 1.1 christos usage();
2502 1.1 christos }
2503 1.1 christos }
2504 1.1 christos
2505 1.1 christos if (!isatty(STDERR_FILENO))
2506 1.1 christos showprogress = 0;
2507 1.1 christos
2508 1.29 christos if (noisy)
2509 1.29 christos quiet = 0;
2510 1.29 christos
2511 1.1 christos log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
2512 1.1 christos
2513 1.1 christos if (sftp_direct == NULL) {
2514 1.1 christos if (optind == argc || argc > (optind + 2))
2515 1.1 christos usage();
2516 1.23 christos argv += optind;
2517 1.1 christos
2518 1.23 christos switch (parse_uri("sftp", *argv, &user, &host, &tmp, &file1)) {
2519 1.23 christos case -1:
2520 1.23 christos usage();
2521 1.23 christos break;
2522 1.23 christos case 0:
2523 1.23 christos if (tmp != -1)
2524 1.23 christos port = tmp;
2525 1.23 christos break;
2526 1.23 christos default:
2527 1.27 christos /* Try with user, host and path. */
2528 1.23 christos if (parse_user_host_path(*argv, &user, &host,
2529 1.27 christos &file1) == 0)
2530 1.27 christos break;
2531 1.27 christos /* Try with user and host. */
2532 1.27 christos if (parse_user_host_port(*argv, &user, &host, NULL)
2533 1.27 christos == 0)
2534 1.27 christos break;
2535 1.27 christos /* Treat as a plain hostname. */
2536 1.27 christos host = xstrdup(*argv);
2537 1.27 christos host = cleanhostname(host);
2538 1.23 christos break;
2539 1.1 christos }
2540 1.23 christos file2 = *(argv + 1);
2541 1.1 christos
2542 1.1 christos if (!*host) {
2543 1.1 christos fprintf(stderr, "Missing hostname\n");
2544 1.1 christos usage();
2545 1.1 christos }
2546 1.1 christos
2547 1.23 christos if (port != -1)
2548 1.23 christos addargs(&args, "-oPort %d", port);
2549 1.23 christos if (user != NULL) {
2550 1.23 christos addargs(&args, "-l");
2551 1.23 christos addargs(&args, "%s", user);
2552 1.23 christos }
2553 1.1 christos
2554 1.1 christos /* no subsystem if the server-spec contains a '/' */
2555 1.1 christos if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
2556 1.1 christos addargs(&args, "-s");
2557 1.1 christos
2558 1.4 adam addargs(&args, "--");
2559 1.1 christos addargs(&args, "%s", host);
2560 1.1 christos addargs(&args, "%s", (sftp_server != NULL ?
2561 1.1 christos sftp_server : "sftp"));
2562 1.1 christos
2563 1.1 christos connect_to_server(ssh_program, args.list, &in, &out);
2564 1.1 christos } else {
2565 1.1 christos args.list = NULL;
2566 1.1 christos addargs(&args, "sftp-server");
2567 1.1 christos
2568 1.1 christos connect_to_server(sftp_direct, args.list, &in, &out);
2569 1.1 christos }
2570 1.1 christos freeargs(&args);
2571 1.1 christos
2572 1.7 christos conn = do_init(in, out, copy_buffer_len, num_requests, limit_kbps);
2573 1.4 adam if (conn == NULL)
2574 1.4 adam fatal("Couldn't initialise connection to server");
2575 1.4 adam
2576 1.12 christos if (!quiet) {
2577 1.4 adam if (sftp_direct == NULL)
2578 1.4 adam fprintf(stderr, "Connected to %s.\n", host);
2579 1.4 adam else
2580 1.4 adam fprintf(stderr, "Attached to %s.\n", sftp_direct);
2581 1.4 adam }
2582 1.4 adam
2583 1.4 adam err = interactive_loop(conn, file1, file2);
2584 1.1 christos
2585 1.1 christos close(in);
2586 1.1 christos close(out);
2587 1.1 christos if (batchmode)
2588 1.1 christos fclose(infile);
2589 1.1 christos
2590 1.24 christos while (waitpid(sshpid, NULL, 0) == -1 && sshpid > 1)
2591 1.1 christos if (errno != EINTR)
2592 1.1 christos fatal("Couldn't wait for ssh process: %s",
2593 1.1 christos strerror(errno));
2594 1.1 christos
2595 1.1 christos exit(err == 0 ? 0 : 1);
2596 1.1 christos }
2597