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