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