]> andersk Git - gssapi-openssh.git/blob - openssh/sftp-int.c
841e562e1de21bc7a433ae8d4f6cc4b6ecae83f8
[gssapi-openssh.git] / openssh / sftp-int.c
1 /*
2  * Copyright (c) 2001 Damien Miller.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 /* XXX: globbed ls */
26 /* XXX: recursive operations */
27
28 #include "includes.h"
29 RCSID("$OpenBSD: sftp-int.c,v 1.40 2001/08/14 09:23:02 markus Exp $");
30
31 #include "buffer.h"
32 #include "xmalloc.h"
33 #include "log.h"
34 #include "pathnames.h"
35
36 #include "sftp.h"
37 #include "sftp-common.h"
38 #include "sftp-glob.h"
39 #include "sftp-client.h"
40 #include "sftp-int.h"
41
42 /* File to read commands from */
43 extern FILE *infile;
44
45 /* Version of server we are speaking to */
46 int version;
47
48 /* Seperators for interactive commands */
49 #define WHITESPACE " \t\r\n"
50
51 /* Commands for interactive mode */
52 #define I_CHDIR         1
53 #define I_CHGRP         2
54 #define I_CHMOD         3
55 #define I_CHOWN         4
56 #define I_GET           5
57 #define I_HELP          6
58 #define I_LCHDIR        7
59 #define I_LLS           8
60 #define I_LMKDIR        9
61 #define I_LPWD          10
62 #define I_LS            11
63 #define I_LUMASK        12
64 #define I_MKDIR         13
65 #define I_PUT           14
66 #define I_PWD           15
67 #define I_QUIT          16
68 #define I_RENAME        17
69 #define I_RM            18
70 #define I_RMDIR         19
71 #define I_SHELL         20
72 #define I_SYMLINK       21
73 #define I_VERSION       22
74
75 struct CMD {
76         const char *c;
77         const int n;
78 };
79
80 const struct CMD cmds[] = {
81         { "bye",        I_QUIT },
82         { "cd",         I_CHDIR },
83         { "chdir",      I_CHDIR },
84         { "chgrp",      I_CHGRP },
85         { "chmod",      I_CHMOD },
86         { "chown",      I_CHOWN },
87         { "dir",        I_LS },
88         { "exit",       I_QUIT },
89         { "get",        I_GET },
90         { "mget",       I_GET },
91         { "help",       I_HELP },
92         { "lcd",        I_LCHDIR },
93         { "lchdir",     I_LCHDIR },
94         { "lls",        I_LLS },
95         { "lmkdir",     I_LMKDIR },
96         { "ln",         I_SYMLINK },
97         { "lpwd",       I_LPWD },
98         { "ls",         I_LS },
99         { "lumask",     I_LUMASK },
100         { "mkdir",      I_MKDIR },
101         { "put",        I_PUT },
102         { "mput",       I_PUT },
103         { "pwd",        I_PWD },
104         { "quit",       I_QUIT },
105         { "rename",     I_RENAME },
106         { "rm",         I_RM },
107         { "rmdir",      I_RMDIR },
108         { "symlink",    I_SYMLINK },
109         { "version",    I_VERSION },
110         { "!",          I_SHELL },
111         { "?",          I_HELP },
112         { NULL,                 -1}
113 };
114
115 static void
116 help(void)
117 {
118         printf("Available commands:\n");
119         printf("cd path                       Change remote directory to 'path'\n");
120         printf("lcd path                      Change local directory to 'path'\n");
121         printf("chgrp grp path                Change group of file 'path' to 'grp'\n");
122         printf("chmod mode path               Change permissions of file 'path' to 'mode'\n");
123         printf("chown own path                Change owner of file 'path' to 'own'\n");
124         printf("help                          Display this help text\n");
125         printf("get remote-path [local-path]  Download file\n");
126         printf("lls [ls-options [path]]       Display local directory listing\n");
127         printf("ln oldpath newpath            Symlink remote file\n");
128         printf("lmkdir path                   Create local directory\n");
129         printf("lpwd                          Print local working directory\n");
130         printf("ls [path]                     Display remote directory listing\n");
131         printf("lumask umask                  Set local umask to 'umask'\n");
132         printf("mkdir path                    Create remote directory\n");
133         printf("put local-path [remote-path]  Upload file\n");
134         printf("pwd                           Display remote working directory\n");
135         printf("exit                          Quit sftp\n");
136         printf("quit                          Quit sftp\n");
137         printf("rename oldpath newpath        Rename remote file\n");
138         printf("rmdir path                    Remove remote directory\n");
139         printf("rm path                       Delete remote file\n");
140         printf("symlink oldpath newpath       Symlink remote file\n");
141         printf("version                       Show SFTP version\n");
142         printf("!command                      Execute 'command' in local shell\n");
143         printf("!                             Escape to local shell\n");
144         printf("?                             Synonym for help\n");
145 }
146
147 static void
148 local_do_shell(const char *args)
149 {
150         int status;
151         char *shell;
152         pid_t pid;
153
154         if (!*args)
155                 args = NULL;
156
157         if ((shell = getenv("SHELL")) == NULL)
158                 shell = _PATH_BSHELL;
159
160         if ((pid = fork()) == -1)
161                 fatal("Couldn't fork: %s", strerror(errno));
162
163         if (pid == 0) {
164                 /* XXX: child has pipe fds to ssh subproc open - issue? */
165                 if (args) {
166                         debug3("Executing %s -c \"%s\"", shell, args);
167                         execl(shell, shell, "-c", args, (char *)NULL);
168                 } else {
169                         debug3("Executing %s", shell);
170                         execl(shell, shell, (char *)NULL);
171                 }
172                 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
173                     strerror(errno));
174                 _exit(1);
175         }
176         if (waitpid(pid, &status, 0) == -1)
177                 fatal("Couldn't wait for child: %s", strerror(errno));
178         if (!WIFEXITED(status))
179                 error("Shell exited abormally");
180         else if (WEXITSTATUS(status))
181                 error("Shell exited with status %d", WEXITSTATUS(status));
182 }
183
184 static void
185 local_do_ls(const char *args)
186 {
187         if (!args || !*args)
188                 local_do_shell(_PATH_LS);
189         else {
190                 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
191                 char *buf = xmalloc(len);
192
193                 /* XXX: quoting - rip quoting code from ftp? */
194                 snprintf(buf, len, _PATH_LS " %s", args);
195                 local_do_shell(buf);
196                 xfree(buf);
197         }
198 }
199
200 static char *
201 path_append(char *p1, char *p2)
202 {
203         char *ret;
204         int len = strlen(p1) + strlen(p2) + 2;
205
206         ret = xmalloc(len);
207         strlcpy(ret, p1, len);
208         if (strcmp(p1, "/") != 0) 
209                 strlcat(ret, "/", len);
210         strlcat(ret, p2, len);
211
212         return(ret);
213 }
214
215 static char *
216 make_absolute(char *p, char *pwd)
217 {
218         char *abs;
219
220         /* Derelativise */
221         if (p && p[0] != '/') {
222                 abs = path_append(pwd, p);
223                 xfree(p);
224                 return(abs);
225         } else
226                 return(p);
227 }
228
229 static int
230 infer_path(const char *p, char **ifp)
231 {
232         char *cp;
233
234         cp = strrchr(p, '/');
235         if (cp == NULL) {
236                 *ifp = xstrdup(p);
237                 return(0);
238         }
239
240         if (!cp[1]) {
241                 error("Invalid path");
242                 return(-1);
243         }
244
245         *ifp = xstrdup(cp + 1);
246         return(0);
247 }
248
249 static int
250 parse_getput_flags(const char **cpp, int *pflag)
251 {
252         const char *cp = *cpp;
253
254         /* Check for flags */
255         if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
256                 switch (cp[1]) {
257                 case 'p':
258                 case 'P':
259                         *pflag = 1;
260                         break;
261                 default:
262                         error("Invalid flag -%c", cp[1]);
263                         return(-1);
264                 }
265                 cp += 2;
266                 *cpp = cp + strspn(cp, WHITESPACE);
267         }
268
269         return(0);
270 }
271
272 static int
273 get_pathname(const char **cpp, char **path)
274 {
275         const char *cp = *cpp, *end;
276         char quot;
277         int i;
278
279         cp += strspn(cp, WHITESPACE);
280         if (!*cp) {
281                 *cpp = cp;
282                 *path = NULL;
283                 return (0);
284         }
285
286         /* Check for quoted filenames */
287         if (*cp == '\"' || *cp == '\'') {
288                 quot = *cp++;
289
290                 end = strchr(cp, quot);
291                 if (end == NULL) {
292                         error("Unterminated quote");
293                         goto fail;
294                 }
295                 if (cp == end) {
296                         error("Empty quotes");
297                         goto fail;
298                 }
299                 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
300         } else {
301                 /* Read to end of filename */
302                 end = strpbrk(cp, WHITESPACE);
303                 if (end == NULL)
304                         end = strchr(cp, '\0');
305                 *cpp = end + strspn(end, WHITESPACE);
306         }
307
308         i = end - cp;
309
310         *path = xmalloc(i + 1);
311         memcpy(*path, cp, i);
312         (*path)[i] = '\0';
313         return(0);
314
315  fail:
316         *path = NULL;
317         return (-1);
318 }
319
320 static int
321 is_dir(char *path)
322 {
323         struct stat sb;
324
325         /* XXX: report errors? */
326         if (stat(path, &sb) == -1)
327                 return(0);
328
329         return(sb.st_mode & S_IFDIR);
330 }
331
332 static int
333 remote_is_dir(int in, int out, char *path)
334 {
335         Attrib *a;
336
337         /* XXX: report errors? */
338         if ((a = do_stat(in, out, path, 1)) == NULL)
339                 return(0);
340         if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
341                 return(0);
342         return(a->perm & S_IFDIR);
343 }
344
345 static int
346 process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
347 {
348         char *abs_src = NULL;
349         char *abs_dst = NULL;
350         char *tmp;
351         glob_t g;
352         int err = 0;
353         int i;
354
355         abs_src = xstrdup(src);
356         abs_src = make_absolute(abs_src, pwd);
357
358         memset(&g, 0, sizeof(g));
359         debug3("Looking up %s", abs_src);
360         if (remote_glob(in, out, abs_src, 0, NULL, &g)) {
361                 error("File \"%s\" not found.", abs_src);
362                 err = -1;
363                 goto out;
364         }
365
366         /* Only one match, dst may be file, directory or unspecified */
367         if (g.gl_pathv[0] && g.gl_matchc == 1) {
368                 if (dst) {
369                         /* If directory specified, append filename */
370                         if (is_dir(dst)) {
371                                 if (infer_path(g.gl_pathv[0], &tmp)) {
372                                         err = 1;
373                                         goto out;
374                                 }
375                                 abs_dst = path_append(dst, tmp);
376                                 xfree(tmp);
377                         } else
378                                 abs_dst = xstrdup(dst);
379                 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
380                         err = -1;
381                         goto out;
382                 }
383                 printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
384                 err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag);
385                 goto out;
386         }
387
388         /* Multiple matches, dst may be directory or unspecified */
389         if (dst && !is_dir(dst)) {
390                 error("Multiple files match, but \"%s\" is not a directory",
391                     dst);
392                 err = -1;
393                 goto out;
394         }
395
396         for(i = 0; g.gl_pathv[i]; i++) {
397                 if (infer_path(g.gl_pathv[i], &tmp)) {
398                         err = -1;
399                         goto out;
400                 }
401                 if (dst) {
402                         abs_dst = path_append(dst, tmp);
403                         xfree(tmp);
404                 } else
405                         abs_dst = tmp;
406
407                 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
408                 if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
409                         err = -1;
410                 xfree(abs_dst);
411                 abs_dst = NULL;
412         }
413
414 out:
415         xfree(abs_src);
416         if (abs_dst)
417                 xfree(abs_dst);
418         globfree(&g);
419         return(err);
420 }
421
422 static int
423 process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
424 {
425         char *tmp_dst = NULL;
426         char *abs_dst = NULL;
427         char *tmp;
428         glob_t g;
429         int err = 0;
430         int i;
431
432         if (dst) {
433                 tmp_dst = xstrdup(dst);
434                 tmp_dst = make_absolute(tmp_dst, pwd);
435         }
436
437         memset(&g, 0, sizeof(g));
438         debug3("Looking up %s", src);
439         if (glob(src, 0, NULL, &g)) {
440                 error("File \"%s\" not found.", src);
441                 err = -1;
442                 goto out;
443         }
444
445         /* Only one match, dst may be file, directory or unspecified */
446         if (g.gl_pathv[0] && g.gl_matchc == 1) {
447                 if (tmp_dst) {
448                         /* If directory specified, append filename */
449                         if (remote_is_dir(in, out, tmp_dst)) {
450                                 if (infer_path(g.gl_pathv[0], &tmp)) {
451                                         err = 1;
452                                         goto out;
453                                 }
454                                 abs_dst = path_append(tmp_dst, tmp);
455                                 xfree(tmp);
456                         } else
457                                 abs_dst = xstrdup(tmp_dst);
458                 } else {
459                         if (infer_path(g.gl_pathv[0], &abs_dst)) {
460                                 err = -1;
461                                 goto out;
462                         }
463                         abs_dst = make_absolute(abs_dst, pwd);
464                 }
465                 printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
466                 err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag);
467                 goto out;
468         }
469
470         /* Multiple matches, dst may be directory or unspecified */
471         if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
472                 error("Multiple files match, but \"%s\" is not a directory",
473                     tmp_dst);
474                 err = -1;
475                 goto out;
476         }
477
478         for(i = 0; g.gl_pathv[i]; i++) {
479                 if (infer_path(g.gl_pathv[i], &tmp)) {
480                         err = -1;
481                         goto out;
482                 }
483                 if (tmp_dst) {
484                         abs_dst = path_append(tmp_dst, tmp);
485                         xfree(tmp);
486                 } else
487                         abs_dst = make_absolute(tmp, pwd);
488
489                 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
490                 if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
491                         err = -1;
492         }
493
494 out:
495         if (abs_dst)
496                 xfree(abs_dst);
497         if (tmp_dst)
498                 xfree(tmp_dst);
499         return(err);
500 }
501
502 static int
503 parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
504     char **path1, char **path2)
505 {
506         const char *cmd, *cp = *cpp;
507         char *cp2;
508         int base = 0;
509         long l;
510         int i, cmdnum;
511
512         /* Skip leading whitespace */
513         cp = cp + strspn(cp, WHITESPACE);
514
515         /* Ignore blank lines */
516         if (!*cp)
517                 return(-1);
518
519         /* Figure out which command we have */
520         for(i = 0; cmds[i].c; i++) {
521                 int cmdlen = strlen(cmds[i].c);
522
523                 /* Check for command followed by whitespace */
524                 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
525                     strchr(WHITESPACE, cp[cmdlen])) {
526                         cp += cmdlen;
527                         cp = cp + strspn(cp, WHITESPACE);
528                         break;
529                 }
530         }
531         cmdnum = cmds[i].n;
532         cmd = cmds[i].c;
533
534         /* Special case */
535         if (*cp == '!') {
536                 cp++;
537                 cmdnum = I_SHELL;
538         } else if (cmdnum == -1) {
539                 error("Invalid command.");
540                 return(-1);
541         }
542
543         /* Get arguments and parse flags */
544         *pflag = *n_arg = 0;
545         *path1 = *path2 = NULL;
546         switch (cmdnum) {
547         case I_GET:
548         case I_PUT:
549                 if (parse_getput_flags(&cp, pflag))
550                         return(-1);
551                 /* Get first pathname (mandatory) */
552                 if (get_pathname(&cp, path1))
553                         return(-1);
554                 if (*path1 == NULL) {
555                         error("You must specify at least one path after a "
556                             "%s command.", cmd);
557                         return(-1);
558                 }
559                 /* Try to get second pathname (optional) */
560                 if (get_pathname(&cp, path2))
561                         return(-1);
562                 break;
563         case I_RENAME:
564         case I_SYMLINK:
565                 if (get_pathname(&cp, path1))
566                         return(-1);
567                 if (get_pathname(&cp, path2))
568                         return(-1);
569                 if (!*path1 || !*path2) {
570                         error("You must specify two paths after a %s "
571                             "command.", cmd);
572                         return(-1);
573                 }
574                 break;
575         case I_RM:
576         case I_MKDIR:
577         case I_RMDIR:
578         case I_CHDIR:
579         case I_LCHDIR:
580         case I_LMKDIR:
581                 /* Get pathname (mandatory) */
582                 if (get_pathname(&cp, path1))
583                         return(-1);
584                 if (*path1 == NULL) {
585                         error("You must specify a path after a %s command.",
586                             cmd);
587                         return(-1);
588                 }
589                 break;
590         case I_LS:
591                 /* Path is optional */
592                 if (get_pathname(&cp, path1))
593                         return(-1);
594                 break;
595         case I_LLS:
596         case I_SHELL:
597                 /* Uses the rest of the line */
598                 break;
599         case I_LUMASK:
600                 base = 8;
601         case I_CHMOD:
602                 base = 8;
603         case I_CHOWN:
604         case I_CHGRP:
605                 /* Get numeric arg (mandatory) */
606                 l = strtol(cp, &cp2, base);
607                 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
608                     errno == ERANGE) || l < 0) {
609                         error("You must supply a numeric argument "
610                             "to the %s command.", cmd);
611                         return(-1);
612                 }
613                 cp = cp2;
614                 *n_arg = l;
615                 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
616                         break;
617                 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
618                         error("You must supply a numeric argument "
619                             "to the %s command.", cmd);
620                         return(-1);
621                 }
622                 cp += strspn(cp, WHITESPACE);
623
624                 /* Get pathname (mandatory) */
625                 if (get_pathname(&cp, path1))
626                         return(-1);
627                 if (*path1 == NULL) {
628                         error("You must specify a path after a %s command.",
629                             cmd);
630                         return(-1);
631                 }
632                 break;
633         case I_QUIT:
634         case I_PWD:
635         case I_LPWD:
636         case I_HELP:
637         case I_VERSION:
638                 break;
639         default:
640                 fatal("Command not implemented");
641         }
642
643         *cpp = cp;
644         return(cmdnum);
645 }
646
647 static int
648 parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
649 {
650         char *path1, *path2, *tmp;
651         int pflag, cmdnum, i;
652         unsigned long n_arg;
653         Attrib a, *aa;
654         char path_buf[MAXPATHLEN];
655         int err = 0;
656         glob_t g;
657
658         path1 = path2 = NULL;
659         cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
660
661         memset(&g, 0, sizeof(g));
662
663         /* Perform command */
664         switch (cmdnum) {
665         case -1:
666                 break;
667         case I_GET:
668                 err = process_get(in, out, path1, path2, *pwd, pflag);
669                 break;
670         case I_PUT:
671                 err = process_put(in, out, path1, path2, *pwd, pflag);
672                 break;
673         case I_RENAME:
674                 path1 = make_absolute(path1, *pwd);
675                 path2 = make_absolute(path2, *pwd);
676                 err = do_rename(in, out, path1, path2);
677                 break;
678         case I_SYMLINK:
679                 if (version < 3) {
680                         error("The server (version %d) does not support "
681                             "this operation", version);
682                         err = -1;
683                 } else {
684                         path2 = make_absolute(path2, *pwd);
685                         err = do_symlink(in, out, path1, path2);
686                 }
687                 break;
688         case I_RM:
689                 path1 = make_absolute(path1, *pwd);
690                 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
691                 for(i = 0; g.gl_pathv[i]; i++) {
692                         printf("Removing %s\n", g.gl_pathv[i]);
693                         if (do_rm(in, out, g.gl_pathv[i]) == -1)
694                                 err = -1;
695                 }
696                 break;
697         case I_MKDIR:
698                 path1 = make_absolute(path1, *pwd);
699                 attrib_clear(&a);
700                 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
701                 a.perm = 0777;
702                 err = do_mkdir(in, out, path1, &a);
703                 break;
704         case I_RMDIR:
705                 path1 = make_absolute(path1, *pwd);
706                 err = do_rmdir(in, out, path1);
707                 break;
708         case I_CHDIR:
709                 path1 = make_absolute(path1, *pwd);
710                 if ((tmp = do_realpath(in, out, path1)) == NULL) {
711                         err = 1;
712                         break;
713                 }
714                 if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
715                         xfree(tmp);
716                         err = 1;
717                         break;
718                 }
719                 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
720                         error("Can't change directory: Can't check target");
721                         xfree(tmp);
722                         err = 1;
723                         break;
724                 }
725                 if (!S_ISDIR(aa->perm)) {
726                         error("Can't change directory: \"%s\" is not "
727                             "a directory", tmp);
728                         xfree(tmp);
729                         err = 1;
730                         break;
731                 }
732                 xfree(*pwd);
733                 *pwd = tmp;
734                 break;
735         case I_LS:
736                 if (!path1) {
737                         do_ls(in, out, *pwd);
738                         break;
739                 }
740                 path1 = make_absolute(path1, *pwd);
741                 if ((tmp = do_realpath(in, out, path1)) == NULL)
742                         break;
743                 xfree(path1);
744                 path1 = tmp;
745                 if ((aa = do_stat(in, out, path1, 0)) == NULL)
746                         break;
747                 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
748                     !S_ISDIR(aa->perm)) {
749                         error("Can't ls: \"%s\" is not a directory", path1);
750                         break;
751                 }
752                 do_ls(in, out, path1);
753                 break;
754         case I_LCHDIR:
755                 if (chdir(path1) == -1) {
756                         error("Couldn't change local directory to "
757                             "\"%s\": %s", path1, strerror(errno));
758                         err = 1;
759                 }
760                 break;
761         case I_LMKDIR:
762                 if (mkdir(path1, 0777) == -1) {
763                         error("Couldn't create local directory "
764                             "\"%s\": %s", path1, strerror(errno));
765                         err = 1;
766                 }
767                 break;
768         case I_LLS:
769                 local_do_ls(cmd);
770                 break;
771         case I_SHELL:
772                 local_do_shell(cmd);
773                 break;
774         case I_LUMASK:
775                 umask(n_arg);
776                 printf("Local umask: %03lo\n", n_arg);
777                 break;
778         case I_CHMOD:
779                 path1 = make_absolute(path1, *pwd);
780                 attrib_clear(&a);
781                 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
782                 a.perm = n_arg;
783                 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
784                 for(i = 0; g.gl_pathv[i]; i++) {
785                         printf("Changing mode on %s\n", g.gl_pathv[i]);
786                         do_setstat(in, out, g.gl_pathv[i], &a);
787                 }
788                 break;
789         case I_CHOWN:
790                 path1 = make_absolute(path1, *pwd);
791                 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
792                 for(i = 0; g.gl_pathv[i]; i++) {
793                         if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
794                                 continue;
795                         if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
796                                 error("Can't get current ownership of "
797                                     "remote file \"%s\"", g.gl_pathv[i]);
798                                 continue;
799                         }
800                         printf("Changing owner on %s\n", g.gl_pathv[i]);
801                         aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
802                         aa->uid = n_arg;
803                         do_setstat(in, out, g.gl_pathv[i], aa);
804                 }
805                 break;
806         case I_CHGRP:
807                 path1 = make_absolute(path1, *pwd);
808                 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
809                 for(i = 0; g.gl_pathv[i]; i++) {
810                         if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
811                                 continue;
812                         if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
813                                 error("Can't get current ownership of "
814                                     "remote file \"%s\"", g.gl_pathv[i]);
815                                 continue;
816                         }
817                         printf("Changing group on %s\n", g.gl_pathv[i]);
818                         aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
819                         aa->gid = n_arg;
820                         do_setstat(in, out, g.gl_pathv[i], aa);
821                 }
822                 break;
823         case I_PWD:
824                 printf("Remote working directory: %s\n", *pwd);
825                 break;
826         case I_LPWD:
827                 if (!getcwd(path_buf, sizeof(path_buf)))
828                         error("Couldn't get local cwd: %s",
829                             strerror(errno));
830                 else
831                         printf("Local working directory: %s\n",
832                             path_buf);
833                 break;
834         case I_QUIT:
835                 return(-1);
836         case I_HELP:
837                 help();
838                 break;
839         case I_VERSION:
840                 printf("SFTP protocol version %d\n", version);
841                 break;
842         default:
843                 fatal("%d is not implemented", cmdnum);
844         }
845
846         if (g.gl_pathc)
847                 globfree(&g);
848         if (path1)
849                 xfree(path1);
850         if (path2)
851                 xfree(path2);
852
853         /* If an error occurs in batch mode we should abort. */
854         if (infile != stdin && err > 0)
855                 return -1;
856
857         return(0);
858 }
859
860 void
861 interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
862 {
863         char *pwd;
864         char *dir = NULL;
865         char cmd[2048];
866
867         version = do_init(fd_in, fd_out);
868         if (version == -1)
869                 fatal("Couldn't initialise connection to server");
870
871         pwd = do_realpath(fd_in, fd_out, ".");
872         if (pwd == NULL)
873                 fatal("Need cwd");
874
875         if (file1 != NULL) {
876                 dir = xstrdup(file1);
877                 dir = make_absolute(dir, pwd);
878
879                 if (remote_is_dir(fd_in, fd_out, dir) && file2 == NULL) {
880                         printf("Changing to: %s\n", dir);
881                         snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
882                         parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
883                 } else {
884                         if (file2 == NULL)
885                                 snprintf(cmd, sizeof cmd, "get %s", dir);
886                         else
887                                 snprintf(cmd, sizeof cmd, "get %s %s", dir,
888                                     file2);
889
890                         parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
891                         return;
892                 }
893         }
894 #if HAVE_SETVBUF
895         setvbuf(stdout, NULL, _IOLBF, 0);
896         setvbuf(infile, NULL, _IOLBF, 0);
897 #else
898         setlinebuf(stdout);
899         setlinebuf(infile);
900 #endif
901
902         for(;;) {
903                 char *cp;
904
905                 printf("sftp> ");
906
907                 /* XXX: use libedit */
908                 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
909                         printf("\n");
910                         break;
911                 } else if (infile != stdin) /* Bluff typing */
912                         printf("%s", cmd);
913
914                 cp = strrchr(cmd, '\n');
915                 if (cp)
916                         *cp = '\0';
917
918                 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
919                         break;
920         }
921         xfree(pwd);
922 }
This page took 0.107096 seconds and 3 git commands to generate.