Annotation of src/usr.bin/ssh/sftp.c, Revision 1.16
1.1 djm 1: /*
2: * Copyright (c) 2001 Damien Miller. All rights reserved.
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.16 ! mouring 27: RCSID("$OpenBSD: sftp.c,v 1.15 2001/04/16 02:31:44 mouring Exp $");
1.1 djm 28:
29: /* XXX: commandline mode */
30: /* XXX: short-form remote directory listings (like 'ls -C') */
31:
32: #include "buffer.h"
33: #include "xmalloc.h"
34: #include "log.h"
35: #include "pathnames.h"
1.16 ! mouring 36: #include "misc.h"
1.1 djm 37:
38: #include "sftp.h"
39: #include "sftp-common.h"
40: #include "sftp-client.h"
41: #include "sftp-int.h"
1.15 mouring 42:
1.7 markus 43: int use_ssh1 = 0;
44: char *ssh_program = _PATH_SSH_PROGRAM;
45: char *sftp_server = NULL;
1.10 deraadt 46: FILE* infile;
1.7 markus 47:
1.1 djm 48: void
49: connect_to_server(char **args, int *in, int *out, pid_t *sshpid)
50: {
51: int c_in, c_out;
52: #ifdef USE_PIPES
53: int pin[2], pout[2];
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];
62: if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
63: fatal("socketpair: %s", strerror(errno));
64: *in = *out = inout[0];
65: c_in = c_out = inout[1];
66: #endif /* USE_PIPES */
67:
68: if ((*sshpid = fork()) == -1)
69: fatal("fork: %s", strerror(errno));
70: else if (*sshpid == 0) {
71: if ((dup2(c_in, STDIN_FILENO) == -1) ||
72: (dup2(c_out, STDOUT_FILENO) == -1)) {
73: fprintf(stderr, "dup2: %s\n", strerror(errno));
74: exit(1);
75: }
76: close(*in);
77: close(*out);
78: close(c_in);
79: close(c_out);
1.7 markus 80: execv(ssh_program, args);
81: fprintf(stderr, "exec: %s: %s\n", ssh_program, strerror(errno));
1.1 djm 82: exit(1);
83: }
84:
85: close(c_in);
86: close(c_out);
87: }
88:
89: char **
90: make_ssh_args(char *add_arg)
91: {
92: static char **args = NULL;
93: static int nargs = 0;
94: char debug_buf[4096];
1.9 markus 95: int i;
1.1 djm 96:
97: /* Init args array */
98: if (args == NULL) {
1.9 markus 99: nargs = 2;
1.1 djm 100: i = 0;
101: args = xmalloc(sizeof(*args) * nargs);
102: args[i++] = "ssh";
103: args[i++] = NULL;
104: }
105:
106: /* If asked to add args, then do so and return */
107: if (add_arg) {
108: i = nargs++ - 1;
109: args = xrealloc(args, sizeof(*args) * nargs);
110: args[i++] = add_arg;
111: args[i++] = NULL;
112: return(NULL);
113: }
1.9 markus 114:
115: /* no subsystem if the server-spec contains a '/' */
116: if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
117: make_ssh_args("-s");
118: make_ssh_args("-oForwardX11=no");
119: make_ssh_args("-oForwardAgent=no");
120: make_ssh_args(use_ssh1 ? "-oProtocol=1" : "-oProtocol=2");
1.1 djm 121:
122: /* Otherwise finish up and return the arg array */
1.7 markus 123: if (sftp_server != NULL)
124: make_ssh_args(sftp_server);
125: else
126: make_ssh_args("sftp");
1.1 djm 127:
128: /* XXX: overflow - doesn't grow debug_buf */
129: debug_buf[0] = '\0';
130: for(i = 0; args[i]; i++) {
131: if (i)
132: strlcat(debug_buf, " ", sizeof(debug_buf));
133:
134: strlcat(debug_buf, args[i], sizeof(debug_buf));
135: }
136: debug("SSH args \"%s\"", debug_buf);
137:
138: return(args);
139: }
140:
1.2 stevesk 141: void
1.1 djm 142: usage(void)
143: {
1.14 mouring 144: fprintf(stderr, "usage: sftp [-1vC] [-b batchfile] [-osshopt=value] [user@]host[:file [file]]\n");
1.1 djm 145: exit(1);
146: }
147:
1.2 stevesk 148: int
1.1 djm 149: main(int argc, char **argv)
150: {
1.3 djm 151: int in, out, ch, debug_level, compress_flag;
1.1 djm 152: pid_t sshpid;
1.14 mouring 153: char *file1 = NULL;
154: char *host, *userhost, *cp, *file2;
1.1 djm 155: LogLevel ll;
1.3 djm 156: extern int optind;
157: extern char *optarg;
1.1 djm 158:
1.10 deraadt 159: infile = stdin; /* Read from STDIN unless changed by -b */
1.1 djm 160: debug_level = compress_flag = 0;
1.3 djm 161:
1.10 deraadt 162: while ((ch = getopt(argc, argv, "1hvCo:s:S:b:")) != -1) {
1.3 djm 163: switch (ch) {
164: case 'C':
165: compress_flag = 1;
166: break;
167: case 'v':
1.1 djm 168: debug_level = MIN(3, debug_level + 1);
1.3 djm 169: break;
170: case 'o':
1.5 djm 171: make_ssh_args("-o");
1.3 djm 172: make_ssh_args(optarg);
1.7 markus 173: break;
174: case '1':
175: use_ssh1 = 1;
176: if (sftp_server == NULL)
177: sftp_server = _PATH_SFTP_SERVER;
178: break;
179: case 's':
180: sftp_server = optarg;
181: break;
182: case 'S':
183: ssh_program = optarg;
1.3 djm 184: break;
1.10 deraadt 185: case 'b':
186: if (infile == stdin) {
187: infile = fopen(optarg, "r");
1.12 markus 188: if (infile == NULL)
1.10 deraadt 189: fatal("%s (%s).", strerror(errno), optarg);
1.12 markus 190: } else
1.10 deraadt 191: fatal("Filename already specified.");
192: break;
1.3 djm 193: case 'h':
194: default:
1.1 djm 195: usage();
196: }
197: }
198:
1.14 mouring 199: if (optind == argc || argc > (optind + 2))
1.1 djm 200: usage();
201:
1.13 deraadt 202: userhost = xstrdup(argv[optind]);
1.14 mouring 203: file2 = argv[optind+1];
204:
1.15 mouring 205: if ((cp = colon(userhost)) != NULL) {
1.14 mouring 206: *cp++ = '\0';
207: file1 = cp;
208: }
1.3 djm 209:
210: if ((host = strchr(userhost, '@')) == NULL)
211: host = userhost;
1.1 djm 212: else {
1.14 mouring 213: *host++ = '\0';
1.3 djm 214: if (!userhost[0]) {
1.1 djm 215: fprintf(stderr, "Missing username\n");
216: usage();
217: }
218: make_ssh_args("-l");
1.3 djm 219: make_ssh_args(userhost);
1.1 djm 220: }
221:
1.15 mouring 222: host = cleanhostname(host);
1.3 djm 223: if (!*host) {
1.1 djm 224: fprintf(stderr, "Missing hostname\n");
225: usage();
226: }
227:
228: /* Set up logging and debug '-d' arguments to ssh */
229: ll = SYSLOG_LEVEL_INFO;
230: switch (debug_level) {
231: case 1:
232: ll = SYSLOG_LEVEL_DEBUG1;
233: make_ssh_args("-v");
234: break;
235: case 2:
236: ll = SYSLOG_LEVEL_DEBUG2;
237: make_ssh_args("-v");
238: make_ssh_args("-v");
239: break;
240: case 3:
241: ll = SYSLOG_LEVEL_DEBUG3;
242: make_ssh_args("-v");
243: make_ssh_args("-v");
244: make_ssh_args("-v");
245: break;
246: }
247:
248: if (compress_flag)
249: make_ssh_args("-C");
250:
251: log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
252:
1.3 djm 253: make_ssh_args(host);
1.1 djm 254:
1.3 djm 255: fprintf(stderr, "Connecting to %s...\n", host);
1.1 djm 256:
257: connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid);
258:
1.14 mouring 259: interactive_loop(in, out, file1, file2);
1.1 djm 260:
261: close(in);
262: close(out);
1.10 deraadt 263: if (infile != stdin)
264: fclose(infile);
1.1 djm 265:
1.4 djm 266: if (waitpid(sshpid, NULL, 0) == -1)
267: fatal("Couldn't wait for ssh process: %s", strerror(errno));
1.1 djm 268:
269: exit(0);
270: }