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