Annotation of src/usr.bin/ssh/sftp.c, Revision 1.24
1.1 djm 1: /*
1.23 djm 2: * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
1.1 djm 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: #include "includes.h"
26:
1.24 ! djm 27: RCSID("$OpenBSD: sftp.c,v 1.23 2002/02/04 21:53:12 djm Exp $");
1.1 djm 28:
29: /* XXX: short-form remote directory listings (like 'ls -C') */
30:
31: #include "buffer.h"
32: #include "xmalloc.h"
33: #include "log.h"
34: #include "pathnames.h"
1.16 mouring 35: #include "misc.h"
1.1 djm 36:
37: #include "sftp.h"
38: #include "sftp-common.h"
39: #include "sftp-client.h"
40: #include "sftp-int.h"
1.15 mouring 41:
1.10 deraadt 42: FILE* infile;
1.24 ! djm 43: size_t copy_buffer_len = 32768;
1.7 markus 44:
1.18 itojun 45: static void
1.23 djm 46: connect_to_server(char *path, char **args, int *in, int *out, pid_t *sshpid)
1.1 djm 47: {
48: int c_in, c_out;
49: #ifdef USE_PIPES
50: int pin[2], pout[2];
51: if ((pipe(pin) == -1) || (pipe(pout) == -1))
52: fatal("pipe: %s", strerror(errno));
53: *in = pin[0];
54: *out = pout[1];
55: c_in = pout[0];
56: c_out = pin[1];
57: #else /* USE_PIPES */
58: int inout[2];
59: if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
60: fatal("socketpair: %s", strerror(errno));
61: *in = *out = inout[0];
62: c_in = c_out = inout[1];
63: #endif /* USE_PIPES */
64:
65: if ((*sshpid = fork()) == -1)
66: fatal("fork: %s", strerror(errno));
67: else if (*sshpid == 0) {
68: if ((dup2(c_in, STDIN_FILENO) == -1) ||
69: (dup2(c_out, STDOUT_FILENO) == -1)) {
70: fprintf(stderr, "dup2: %s\n", strerror(errno));
71: exit(1);
72: }
73: close(*in);
74: close(*out);
75: close(c_in);
76: close(c_out);
1.23 djm 77: execv(path, args);
78: fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
1.1 djm 79: exit(1);
80: }
81:
82: close(c_in);
83: close(c_out);
84: }
85:
1.18 itojun 86: static void
1.1 djm 87: usage(void)
88: {
1.19 stevesk 89: fprintf(stderr,
1.20 stevesk 90: "usage: sftp [-1Cv] [-b batchfile] [-F config] [-o option] [-s subsystem|path]\n"
1.24 ! djm 91: " [-P direct server path] [-S program] \n"
! 92: " [-B buffer_size] [user@]host[:file [file]]\n");
1.1 djm 93: exit(1);
94: }
95:
1.2 stevesk 96: int
1.1 djm 97: main(int argc, char **argv)
98: {
1.17 mouring 99: int in, out, ch;
1.1 djm 100: pid_t sshpid;
1.14 mouring 101: char *host, *userhost, *cp, *file2;
1.17 mouring 102: int debug_level = 0, sshver = 2;
103: char *file1 = NULL, *sftp_server = NULL;
1.23 djm 104: char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
1.17 mouring 105: LogLevel ll = SYSLOG_LEVEL_INFO;
106: arglist args;
1.3 djm 107: extern int optind;
108: extern char *optarg;
1.1 djm 109:
1.17 mouring 110: args.list = NULL;
1.22 deraadt 111: addargs(&args, "ssh"); /* overwritten with ssh_program */
1.17 mouring 112: addargs(&args, "-oFallBackToRsh no");
113: addargs(&args, "-oForwardX11 no");
114: addargs(&args, "-oForwardAgent no");
1.21 stevesk 115: addargs(&args, "-oClearAllForwardings yes");
1.17 mouring 116: ll = SYSLOG_LEVEL_INFO;
1.10 deraadt 117: infile = stdin; /* Read from STDIN unless changed by -b */
1.3 djm 118:
1.24 ! djm 119: while ((ch = getopt(argc, argv, "1hvCo:s:S:b:B:F:P:")) != -1) {
1.3 djm 120: switch (ch) {
121: case 'C':
1.17 mouring 122: addargs(&args, "-C");
1.3 djm 123: break;
124: case 'v':
1.17 mouring 125: if (debug_level < 3) {
126: addargs(&args, "-v");
127: ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
128: }
129: debug_level++;
1.3 djm 130: break;
1.19 stevesk 131: case 'F':
1.3 djm 132: case 'o':
1.19 stevesk 133: addargs(&args, "-%c%s", ch, optarg);
1.7 markus 134: break;
135: case '1':
1.17 mouring 136: sshver = 1;
1.7 markus 137: if (sftp_server == NULL)
138: sftp_server = _PATH_SFTP_SERVER;
139: break;
140: case 's':
141: sftp_server = optarg;
142: break;
143: case 'S':
144: ssh_program = optarg;
1.3 djm 145: break;
1.10 deraadt 146: case 'b':
147: if (infile == stdin) {
148: infile = fopen(optarg, "r");
1.12 markus 149: if (infile == NULL)
1.10 deraadt 150: fatal("%s (%s).", strerror(errno), optarg);
1.12 markus 151: } else
1.10 deraadt 152: fatal("Filename already specified.");
153: break;
1.23 djm 154: case 'P':
155: sftp_direct = optarg;
1.24 ! djm 156: break;
! 157: case 'B':
! 158: copy_buffer_len = strtol(optarg, &cp, 10);
! 159: if (copy_buffer_len == 0 || *cp != '\0')
! 160: fatal("Invalid buffer size \"%s\"", optarg);
1.23 djm 161: break;
1.3 djm 162: case 'h':
163: default:
1.1 djm 164: usage();
165: }
166: }
167:
1.23 djm 168: if (sftp_direct == NULL) {
169: if (optind == argc || argc > (optind + 2))
170: usage();
171:
172: userhost = xstrdup(argv[optind]);
173: file2 = argv[optind+1];
1.1 djm 174:
1.23 djm 175: if ((cp = colon(userhost)) != NULL) {
176: *cp++ = '\0';
177: file1 = cp;
178: }
1.14 mouring 179:
1.23 djm 180: if ((host = strchr(userhost, '@')) == NULL)
181: host = userhost;
182: else {
183: *host++ = '\0';
184: if (!userhost[0]) {
185: fprintf(stderr, "Missing username\n");
186: usage();
187: }
188: addargs(&args, "-l%s",userhost);
189: }
1.3 djm 190:
1.23 djm 191: host = cleanhostname(host);
192: if (!*host) {
193: fprintf(stderr, "Missing hostname\n");
1.1 djm 194: usage();
195: }
196:
1.23 djm 197: log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
198: addargs(&args, "-oProtocol %d", sshver);
199:
200: /* no subsystem if the server-spec contains a '/' */
201: if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
202: addargs(&args, "-s");
203:
204: addargs(&args, "%s", host);
205: addargs(&args, "%s", (sftp_server != NULL ?
206: sftp_server : "sftp"));
207: args.list[0] = ssh_program;
208:
209: fprintf(stderr, "Connecting to %s...\n", host);
210: connect_to_server(ssh_program, args.list, &in, &out,
211: &sshpid);
212: } else {
213: args.list = NULL;
214: addargs(&args, "sftp-server");
215:
216: fprintf(stderr, "Attaching to %s...\n", sftp_direct);
217: connect_to_server(sftp_direct, args.list, &in, &out,
218: &sshpid);
1.1 djm 219: }
220:
1.14 mouring 221: interactive_loop(in, out, file1, file2);
1.1 djm 222:
223: close(in);
224: close(out);
1.10 deraadt 225: if (infile != stdin)
226: fclose(infile);
1.1 djm 227:
1.4 djm 228: if (waitpid(sshpid, NULL, 0) == -1)
229: fatal("Couldn't wait for ssh process: %s", strerror(errno));
1.1 djm 230:
231: exit(0);
232: }