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