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