Annotation of src/usr.bin/ssh/sftp-int.c, Revision 1.10
1.1 djm 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: finish implementation of all commands */
26: /* XXX: do fnmatch() instead of using raw pathname */
27: /* XXX: recursive operations */
28:
29: #include "includes.h"
1.10 ! markus 30: RCSID("$OpenBSD: sftp-int.c,v 1.9 2001/02/06 22:18:16 djm Exp $");
1.1 djm 31:
32: #include "buffer.h"
33: #include "xmalloc.h"
34: #include "log.h"
35: #include "pathnames.h"
36:
37: #include "sftp.h"
38: #include "sftp-common.h"
39: #include "sftp-client.h"
40: #include "sftp-int.h"
41:
42: /* Seperators for interactive commands */
43: #define WHITESPACE " \t\r\n"
44:
45: /* Commands for interactive mode */
46: #define I_CHDIR 1
47: #define I_CHGRP 2
48: #define I_CHMOD 3
49: #define I_CHOWN 4
50: #define I_GET 5
51: #define I_HELP 6
52: #define I_LCHDIR 7
53: #define I_LLS 8
54: #define I_LMKDIR 9
55: #define I_LPWD 10
56: #define I_LS 11
57: #define I_LUMASK 12
58: #define I_MKDIR 13
59: #define I_PUT 14
60: #define I_PWD 15
61: #define I_QUIT 16
62: #define I_RENAME 17
63: #define I_RM 18
64: #define I_RMDIR 19
65: #define I_SHELL 20
66:
67: struct CMD {
1.6 deraadt 68: const char *c;
1.1 djm 69: const int n;
70: };
71:
72: const struct CMD cmds[] = {
1.6 deraadt 73: { "CD", I_CHDIR },
74: { "CHDIR", I_CHDIR },
75: { "CHGRP", I_CHGRP },
76: { "CHMOD", I_CHMOD },
77: { "CHOWN", I_CHOWN },
1.10 ! markus 78: { "DIR", I_LS },
1.6 deraadt 79: { "EXIT", I_QUIT },
80: { "GET", I_GET },
81: { "HELP", I_HELP },
82: { "LCD", I_LCHDIR },
83: { "LCHDIR", I_LCHDIR },
84: { "LLS", I_LLS },
85: { "LMKDIR", I_LMKDIR },
86: { "LPWD", I_LPWD },
87: { "LS", I_LS },
88: { "LUMASK", I_LUMASK },
89: { "MKDIR", I_MKDIR },
90: { "PUT", I_PUT },
91: { "PWD", I_PWD },
92: { "QUIT", I_QUIT },
93: { "RENAME", I_RENAME },
94: { "RM", I_RM },
95: { "RMDIR", I_RMDIR },
96: { "!", I_SHELL },
1.7 deraadt 97: { "?", I_HELP },
1.6 deraadt 98: { NULL, -1}
1.1 djm 99: };
100:
101: void
102: help(void)
103: {
104: printf("Available commands:\n");
105: printf("CD path Change remote directory to 'path'\n");
106: printf("LCD path Change local directory to 'path'\n");
107: printf("CHGRP grp path Change group of file 'path' to 'grp'\n");
108: printf("CHMOD mode path Change permissions of file 'path' to 'mode'\n");
109: printf("CHOWN own path Change owner of file 'path' to 'own'\n");
110: printf("HELP Display this help text\n");
111: printf("GET remote-path [local-path] Download file\n");
112: printf("LLS [ls options] [path] Display local directory listing\n");
113: printf("LMKDIR path Create local directory\n");
114: printf("LPWD Print local working directory\n");
115: printf("LS [path] Display remote directory listing\n");
116: printf("LUMASK umask Set local umask to 'umask'\n");
117: printf("MKDIR path Create remote directory\n");
118: printf("PUT local-path [remote-path] Upload file\n");
119: printf("PWD Display remote working directory\n");
120: printf("EXIT Quit sftp\n");
121: printf("QUIT Quit sftp\n");
122: printf("RENAME oldpath newpath Rename remote file\n");
123: printf("RMDIR path Remove remote directory\n");
124: printf("RM path Delete remote file\n");
125: printf("!command Execute 'command' in local shell\n");
126: printf("! Escape to local shell\n");
127: }
128:
129: void
130: local_do_shell(const char *args)
131: {
132: int ret, status;
133: char *shell;
134: pid_t pid;
1.3 stevesk 135:
1.1 djm 136: if (!*args)
137: args = NULL;
1.3 stevesk 138:
1.1 djm 139: if ((shell = getenv("SHELL")) == NULL)
140: shell = _PATH_BSHELL;
141:
142: if ((pid = fork()) == -1)
143: fatal("Couldn't fork: %s", strerror(errno));
144:
145: if (pid == 0) {
146: /* XXX: child has pipe fds to ssh subproc open - issue? */
147: if (args) {
148: debug3("Executing %s -c \"%s\"", shell, args);
149: ret = execl(shell, shell, "-c", args, NULL);
150: } else {
151: debug3("Executing %s", shell);
152: ret = execl(shell, shell, NULL);
153: }
1.3 stevesk 154: fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
1.1 djm 155: strerror(errno));
156: _exit(1);
157: }
158: if (waitpid(pid, &status, 0) == -1)
159: fatal("Couldn't wait for child: %s", strerror(errno));
160: if (!WIFEXITED(status))
161: error("Shell exited abormally");
162: else if (WEXITSTATUS(status))
163: error("Shell exited with status %d", WEXITSTATUS(status));
164: }
165:
1.3 stevesk 166: void
1.1 djm 167: local_do_ls(const char *args)
168: {
169: if (!args || !*args)
170: local_do_shell("ls");
171: else {
172: char *buf = xmalloc(8 + strlen(args) + 1);
173:
174: /* XXX: quoting - rip quoting code from ftp? */
175: sprintf(buf, "/bin/ls %s", args);
176: local_do_shell(buf);
177: }
178: }
179:
180: char *
181: make_absolute(char *p, char *pwd)
182: {
183: char buf[2048];
184:
185: /* Derelativise */
186: if (p && p[0] != '/') {
187: snprintf(buf, sizeof(buf), "%s/%s", pwd, p);
188: xfree(p);
189: p = xstrdup(buf);
190: }
191:
192: return(p);
193: }
194:
195: int
196: parse_getput_flags(const char **cpp, int *pflag)
197: {
198: const char *cp = *cpp;
199:
200: /* Check for flags */
201: if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
202: switch (*cp) {
203: case 'P':
204: *pflag = 1;
205: break;
206: default:
207: error("Invalid flag -%c", *cp);
208: return(-1);
209: }
210: cp += 2;
211: *cpp = cp + strspn(cp, WHITESPACE);
212: }
213:
214: return(0);
215: }
216:
217: int
218: get_pathname(const char **cpp, char **path)
219: {
1.8 provos 220: const char *cp = *cpp, *end;
221: char quot;
1.1 djm 222: int i;
223:
224: cp += strspn(cp, WHITESPACE);
225: if (!*cp) {
226: *cpp = cp;
227: *path = NULL;
1.8 provos 228:
229: return (0);
1.1 djm 230: }
231:
232: /* Check for quoted filenames */
233: if (*cp == '\"' || *cp == '\'') {
1.8 provos 234: quot = *cp++;
235:
236: end = strchr(cp, quot);
237: if (end == NULL) {
1.1 djm 238: error("Unterminated quote");
1.8 provos 239: goto fail;
1.1 djm 240: }
1.8 provos 241:
242: if (cp == end) {
1.1 djm 243: error("Empty quotes");
1.8 provos 244: goto fail;
1.1 djm 245: }
1.8 provos 246:
247: *cpp = end + 1 + strspn(end + 1, WHITESPACE);
248: } else {
249: /* Read to end of filename */
250: end = strpbrk(cp, WHITESPACE);
251: if (end == NULL)
252: end = strchr(cp, '\0');
253:
254: *cpp = end + strspn(end, WHITESPACE);
1.1 djm 255: }
256:
1.8 provos 257: i = end - cp;
1.1 djm 258:
259: *path = xmalloc(i + 1);
260: memcpy(*path, cp, i);
261: (*path)[i] = '\0';
262:
263: return(0);
1.8 provos 264:
265: fail:
266: *path = NULL;
267:
268: return (-1);
1.1 djm 269: }
270:
271: int
272: infer_path(const char *p, char **ifp)
273: {
274: char *cp;
275:
276: debug("XXX: P = \"%s\"", p);
277:
278: cp = strrchr(p, '/');
279:
280: if (cp == NULL) {
281: *ifp = xstrdup(p);
282: return(0);
283: }
284:
285: if (!cp[1]) {
286: error("Invalid path");
287: return(-1);
288: }
289:
290: *ifp = xstrdup(cp + 1);
291: return(0);
292: }
293:
294: int
295: parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
296: char **path1, char **path2)
297: {
298: const char *cmd, *cp = *cpp;
1.4 markus 299: int base = 0;
1.1 djm 300: int i, cmdnum;
301:
302: /* Skip leading whitespace */
303: cp = cp + strspn(cp, WHITESPACE);
304:
305: /* Ignore blank lines */
306: if (!*cp)
307: return(-1);
308:
309: /* Figure out which command we have */
310: for(i = 0; cmds[i].c; i++) {
311: int cmdlen = strlen(cmds[i].c);
312:
313: /* Check for command followed by whitespace */
314: if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
315: strchr(WHITESPACE, cp[cmdlen])) {
316: cp += cmdlen;
317: cp = cp + strspn(cp, WHITESPACE);
318: break;
319: }
320: }
321: cmdnum = cmds[i].n;
322: cmd = cmds[i].c;
323:
324: /* Special case */
325: if (*cp == '!') {
326: cp++;
327: cmdnum = I_SHELL;
328: } else if (cmdnum == -1) {
329: error("Invalid command.");
330: return(-1);
331: }
332:
333: /* Get arguments and parse flags */
334: *pflag = *n_arg = 0;
335: *path1 = *path2 = NULL;
336: switch (cmdnum) {
337: case I_GET:
338: case I_PUT:
339: if (parse_getput_flags(&cp, pflag))
340: return(-1);
341: /* Get first pathname (mandatory) */
342: if (get_pathname(&cp, path1))
343: return(-1);
344: if (*path1 == NULL) {
345: error("You must specify at least one path after a "
346: "%s command.", cmd);
347: return(-1);
348: }
349: /* Try to get second pathname (optional) */
350: if (get_pathname(&cp, path2))
351: return(-1);
352: /* Otherwise try to guess it from first path */
353: if (*path2 == NULL && infer_path(*path1, path2))
354: return(-1);
355: break;
356: case I_RENAME:
357: /* Get first pathname (mandatory) */
358: if (get_pathname(&cp, path1))
359: return(-1);
360: if (get_pathname(&cp, path2))
361: return(-1);
362: if (!*path1 || !*path2) {
363: error("You must specify two paths after a %s "
364: "command.", cmd);
365: return(-1);
366: }
367: break;
368: case I_RM:
369: case I_MKDIR:
370: case I_RMDIR:
371: case I_CHDIR:
372: case I_LCHDIR:
373: case I_LMKDIR:
374: /* Get pathname (mandatory) */
375: if (get_pathname(&cp, path1))
376: return(-1);
377: if (*path1 == NULL) {
1.3 stevesk 378: error("You must specify a path after a %s command.",
1.1 djm 379: cmd);
380: return(-1);
381: }
382: break;
383: case I_LS:
384: /* Path is optional */
385: if (get_pathname(&cp, path1))
386: return(-1);
387: break;
388: case I_LLS:
389: case I_SHELL:
390: /* Uses the rest of the line */
391: break;
392: case I_LUMASK:
393: case I_CHMOD:
1.4 markus 394: base = 8;
1.1 djm 395: case I_CHOWN:
396: case I_CHGRP:
397: /* Get numeric arg (mandatory) */
398: if (*cp < '0' && *cp > '9') {
399: error("You must supply a numeric argument "
400: "to the %s command.", cmd);
401: return(-1);
402: }
1.4 markus 403: *n_arg = strtoul(cp, (char**)&cp, base);
1.1 djm 404: if (!*cp || !strchr(WHITESPACE, *cp)) {
405: error("You must supply a numeric argument "
406: "to the %s command.", cmd);
407: return(-1);
408: }
409: cp += strspn(cp, WHITESPACE);
410:
411: /* Get pathname (mandatory) */
412: if (get_pathname(&cp, path1))
413: return(-1);
414: if (*path1 == NULL) {
1.3 stevesk 415: error("You must specify a path after a %s command.",
1.1 djm 416: cmd);
417: return(-1);
418: }
419: break;
420: case I_QUIT:
421: case I_PWD:
422: case I_LPWD:
423: case I_HELP:
424: break;
425: default:
426: fatal("Command not implemented");
427: }
428:
429: *cpp = cp;
430:
431: return(cmdnum);
432: }
433:
434: int
435: parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
436: {
1.8 provos 437: char *path1, *path2, *tmp;
1.1 djm 438: int pflag, cmdnum;
439: unsigned long n_arg;
440: Attrib a, *aa;
441: char path_buf[PATH_MAX];
442:
443: path1 = path2 = NULL;
444: cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
445:
446: /* Perform command */
447: switch (cmdnum) {
448: case -1:
449: break;
450: case I_GET:
451: path1 = make_absolute(path1, *pwd);
452: do_download(in, out, path1, path2, pflag);
453: break;
454: case I_PUT:
455: path2 = make_absolute(path2, *pwd);
456: do_upload(in, out, path1, path2, pflag);
457: break;
458: case I_RENAME:
459: path1 = make_absolute(path1, *pwd);
460: path2 = make_absolute(path2, *pwd);
461: do_rename(in, out, path1, path2);
462: break;
463: case I_RM:
464: path1 = make_absolute(path1, *pwd);
465: do_rm(in, out, path1);
466: break;
467: case I_MKDIR:
468: path1 = make_absolute(path1, *pwd);
469: attrib_clear(&a);
470: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
471: a.perm = 0777;
472: do_mkdir(in, out, path1, &a);
473: break;
474: case I_RMDIR:
475: path1 = make_absolute(path1, *pwd);
476: do_rmdir(in, out, path1);
477: break;
478: case I_CHDIR:
479: path1 = make_absolute(path1, *pwd);
1.8 provos 480: tmp = do_realpath(in, out, path1);
1.9 djm 481: aa = do_stat(in, out, tmp);
482: if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
483: error("Can't change directory: Can't check target");
484: xfree(tmp);
485: break;
486: }
487: if (!S_ISDIR(aa->perm)) {
488: error("Can't change directory: \"%s\" is not "
489: "a directory", tmp);
490: xfree(tmp);
491: break;
492: }
1.8 provos 493: if (tmp) {
494: xfree(*pwd);
495: *pwd = tmp;
496: }
1.1 djm 497: break;
498: case I_LS:
499: path1 = make_absolute(path1, *pwd);
500: do_ls(in, out, path1?path1:*pwd);
501: break;
502: case I_LCHDIR:
503: if (chdir(path1) == -1)
504: error("Couldn't change local directory to "
505: "\"%s\": %s", path1, strerror(errno));
506: break;
507: case I_LMKDIR:
508: if (mkdir(path1, 0777) == -1)
509: error("Couldn't create local directory to "
510: "\"%s\": %s", path1, strerror(errno));
511: break;
512: case I_LLS:
513: local_do_ls(cmd);
514: break;
515: case I_SHELL:
516: local_do_shell(cmd);
517: break;
518: case I_LUMASK:
519: umask(n_arg);
520: break;
521: case I_CHMOD:
522: path1 = make_absolute(path1, *pwd);
523: attrib_clear(&a);
524: a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
525: a.perm = n_arg;
526: do_setstat(in, out, path1, &a);
1.5 stevesk 527: break;
1.1 djm 528: case I_CHOWN:
529: path1 = make_absolute(path1, *pwd);
530: aa = do_stat(in, out, path1);
1.5 stevesk 531: if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1.1 djm 532: error("Can't get current ownership of "
533: "remote file \"%s\"", path1);
534: break;
535: }
536: aa->uid = n_arg;
537: do_setstat(in, out, path1, aa);
538: break;
539: case I_CHGRP:
540: path1 = make_absolute(path1, *pwd);
541: aa = do_stat(in, out, path1);
1.5 stevesk 542: if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
1.1 djm 543: error("Can't get current ownership of "
544: "remote file \"%s\"", path1);
545: break;
546: }
547: aa->gid = n_arg;
548: do_setstat(in, out, path1, aa);
549: break;
550: case I_PWD:
551: printf("Remote working directory: %s\n", *pwd);
552: break;
553: case I_LPWD:
554: if (!getcwd(path_buf, sizeof(path_buf)))
555: error("Couldn't get local cwd: %s\n",
556: strerror(errno));
557: else
558: printf("Local working directory: %s\n",
559: path_buf);
560: break;
561: case I_QUIT:
562: return(-1);
563: case I_HELP:
564: help();
565: break;
566: default:
567: fatal("%d is not implemented", cmdnum);
568: }
569:
570: if (path1)
571: xfree(path1);
572: if (path2)
573: xfree(path2);
574:
575: return(0);
576: }
577:
578: void
579: interactive_loop(int fd_in, int fd_out)
580: {
581: char *pwd;
582: char cmd[2048];
583:
584: pwd = do_realpath(fd_in, fd_out, ".");
585: if (pwd == NULL)
586: fatal("Need cwd");
587:
588: setlinebuf(stdout);
589: setlinebuf(stdin);
590:
591: for(;;) {
592: char *cp;
593:
594: printf("sftp> ");
595:
596: /* XXX: use libedit */
597: if (fgets(cmd, sizeof(cmd), stdin) == NULL) {
598: printf("\n");
599: break;
600: }
601: cp = strrchr(cmd, '\n');
602: if (cp)
603: *cp = '\0';
604: if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
605: break;
606: }
607: xfree(pwd);
608: }