summaryrefslogtreecommitdiff
path: root/sftp.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2002-02-05 12:27:31 +1100
committerDamien Miller <djm@mindrot.org>2002-02-05 12:27:31 +1100
commitd14ee1e29c73c79a120f129c84a89158e8dff936 (patch)
tree5ca0b232ad8340fcc23a9b845c43c7d17a496f27 /sftp.c
parentab57f351147ff8c1fb1ca5320f11674bd918d66d (diff)
- djm@cvs.openbsd.org 2002/02/04 21:53:12
[sftp.1 sftp.c] Add "-P" option to directly connect to a local sftp-server. Should be useful for regression testing; ok markus@
Diffstat (limited to 'sftp.c')
-rw-r--r--sftp.c92
1 files changed, 52 insertions, 40 deletions
diff --git a/sftp.c b/sftp.c
index 97f670d6e..3748d055d 100644
--- a/sftp.c
+++ b/sftp.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2001 Damien Miller. All rights reserved. 2 * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
@@ -24,9 +24,8 @@
24 24
25#include "includes.h" 25#include "includes.h"
26 26
27RCSID("$OpenBSD: sftp.c,v 1.22 2001/12/19 07:18:56 deraadt Exp $"); 27RCSID("$OpenBSD: sftp.c,v 1.23 2002/02/04 21:53:12 djm Exp $");
28 28
29/* XXX: commandline mode */
30/* XXX: short-form remote directory listings (like 'ls -C') */ 29/* XXX: short-form remote directory listings (like 'ls -C') */
31 30
32#include "buffer.h" 31#include "buffer.h"
@@ -46,11 +45,10 @@ extern char *__progname;
46char *__progname; 45char *__progname;
47#endif 46#endif
48 47
49char *ssh_program = _PATH_SSH_PROGRAM;
50FILE* infile; 48FILE* infile;
51 49
52static void 50static void
53connect_to_server(char **args, int *in, int *out, pid_t *sshpid) 51connect_to_server(char *path, char **args, int *in, int *out, pid_t *sshpid)
54{ 52{
55 int c_in, c_out; 53 int c_in, c_out;
56#ifdef USE_PIPES 54#ifdef USE_PIPES
@@ -81,8 +79,8 @@ connect_to_server(char **args, int *in, int *out, pid_t *sshpid)
81 close(*out); 79 close(*out);
82 close(c_in); 80 close(c_in);
83 close(c_out); 81 close(c_out);
84 execv(ssh_program, args); 82 execv(path, args);
85 fprintf(stderr, "exec: %s: %s\n", ssh_program, strerror(errno)); 83 fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
86 exit(1); 84 exit(1);
87 } 85 }
88 86
@@ -107,6 +105,7 @@ main(int argc, char **argv)
107 char *host, *userhost, *cp, *file2; 105 char *host, *userhost, *cp, *file2;
108 int debug_level = 0, sshver = 2; 106 int debug_level = 0, sshver = 2;
109 char *file1 = NULL, *sftp_server = NULL; 107 char *file1 = NULL, *sftp_server = NULL;
108 char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
110 LogLevel ll = SYSLOG_LEVEL_INFO; 109 LogLevel ll = SYSLOG_LEVEL_INFO;
111 arglist args; 110 arglist args;
112 extern int optind; 111 extern int optind;
@@ -122,7 +121,7 @@ main(int argc, char **argv)
122 ll = SYSLOG_LEVEL_INFO; 121 ll = SYSLOG_LEVEL_INFO;
123 infile = stdin; /* Read from STDIN unless changed by -b */ 122 infile = stdin; /* Read from STDIN unless changed by -b */
124 123
125 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:F:")) != -1) { 124 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:F:P:")) != -1) {
126 switch (ch) { 125 switch (ch) {
127 case 'C': 126 case 'C':
128 addargs(&args, "-C"); 127 addargs(&args, "-C");
@@ -157,54 +156,67 @@ main(int argc, char **argv)
157 } else 156 } else
158 fatal("Filename already specified."); 157 fatal("Filename already specified.");
159 break; 158 break;
159 case 'P':
160 sftp_direct = optarg;
161 break;
160 case 'h': 162 case 'h':
161 default: 163 default:
162 usage(); 164 usage();
163 } 165 }
164 } 166 }
165 167
166 if (optind == argc || argc > (optind + 2)) 168 if (sftp_direct == NULL) {
167 usage(); 169 if (optind == argc || argc > (optind + 2))
170 usage();
168 171
169 userhost = xstrdup(argv[optind]); 172 userhost = xstrdup(argv[optind]);
170 file2 = argv[optind+1]; 173 file2 = argv[optind+1];
171 174
172 if ((cp = colon(userhost)) != NULL) { 175 if ((cp = colon(userhost)) != NULL) {
173 *cp++ = '\0'; 176 *cp++ = '\0';
174 file1 = cp; 177 file1 = cp;
175 } 178 }
176 179
177 if ((host = strchr(userhost, '@')) == NULL) 180 if ((host = strchr(userhost, '@')) == NULL)
178 host = userhost; 181 host = userhost;
179 else { 182 else {
180 *host++ = '\0'; 183 *host++ = '\0';
181 if (!userhost[0]) { 184 if (!userhost[0]) {
182 fprintf(stderr, "Missing username\n"); 185 fprintf(stderr, "Missing username\n");
183 usage(); 186 usage();
187 }
188 addargs(&args, "-l%s",userhost);
184 } 189 }
185 addargs(&args, "-l%s",userhost);
186 }
187 190
188 host = cleanhostname(host); 191 host = cleanhostname(host);
189 if (!*host) { 192 if (!*host) {
190 fprintf(stderr, "Missing hostname\n"); 193 fprintf(stderr, "Missing hostname\n");
191 usage(); 194 usage();
192 } 195 }
193 196
194 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); 197 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
195 addargs(&args, "-oProtocol %d", sshver); 198 addargs(&args, "-oProtocol %d", sshver);
196 199
197 /* no subsystem if the server-spec contains a '/' */ 200 /* no subsystem if the server-spec contains a '/' */
198 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL) 201 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
199 addargs(&args, "-s"); 202 addargs(&args, "-s");
200 203
201 addargs(&args, "%s", host); 204 addargs(&args, "%s", host);
202 addargs(&args, "%s", (sftp_server != NULL ? sftp_server : "sftp")); 205 addargs(&args, "%s", (sftp_server != NULL ?
203 args.list[0] = ssh_program; 206 sftp_server : "sftp"));
207 args.list[0] = ssh_program;
204 208
205 fprintf(stderr, "Connecting to %s...\n", host); 209 fprintf(stderr, "Connecting to %s...\n", host);
210 connect_to_server(ssh_program, args.list, &in, &out,
211 &sshpid);
212 } else {
213 args.list = NULL;
214 addargs(&args, "sftp-server");
206 215
207 connect_to_server(args.list, &in, &out, &sshpid); 216 fprintf(stderr, "Attaching to %s...\n", sftp_direct);
217 connect_to_server(sftp_direct, args.list, &in, &out,
218 &sshpid);
219 }
208 220
209 interactive_loop(in, out, file1, file2); 221 interactive_loop(in, out, file1, file2);
210 222