summaryrefslogtreecommitdiff
path: root/sftp.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2001-02-10 00:40:03 +1100
committerDamien Miller <djm@mindrot.org>2001-02-10 00:40:03 +1100
commitd7686fd1fbe842baa8ce77f018c040d5e1d3438a (patch)
treea5d6ad5b4b232e06185a49b1b5aabe4761a3d8ed /sftp.c
parent4192c467916f96668fad5b53d90d83dfbfdcacb5 (diff)
- (djm) Sync sftp and scp stuff from OpenBSD:
- djm@cvs.openbsd.org 2001/02/07 03:55:13 [sftp-client.c] Don't free handles before we are done with them. Based on work from Corinna Vinschen <vinschen@redhat.com>. ok markus@ - djm@cvs.openbsd.org 2001/02/06 22:32:53 [sftp.1] Punctuation fix from Pekka Savola <pekkas@netcore.fi> - deraadt@cvs.openbsd.org 2001/02/07 04:07:29 [sftp.1] pretty up significantly - itojun@cvs.openbsd.org 2001/02/07 06:49:42 [sftp.1] .Bl-.El mismatch. markus ok - djm@cvs.openbsd.org 2001/02/07 06:12:30 [sftp-int.c] Check that target is a directory before doing ls; ok markus@ - itojun@cvs.openbsd.org 2001/02/07 11:01:18 [scp.c sftp-client.c sftp-server.c] unsigned long long -> %llu, not %qu. markus ok - stevesk@cvs.openbsd.org 2001/02/07 11:10:39 [sftp.1 sftp-int.c] more man page cleanup and sync of help text with man page; ok markus@ - markus@cvs.openbsd.org 2001/02/07 14:58:34 [sftp-client.c] older servers reply with SSH2_FXP_NAME + count==0 instead of EOF - djm@cvs.openbsd.org 2001/02/07 15:27:19 [sftp.c] Don't forward agent and X11 in sftp. Suggestion from Roumen Petrov <roumen.petrov@skalasoft.com> - stevesk@cvs.openbsd.org 2001/02/07 15:36:04 [sftp-int.c] portable; ok markus@ - stevesk@cvs.openbsd.org 2001/02/07 15:55:47 [sftp-int.c] lowercase cmds[].c also; ok markus@ - markus@cvs.openbsd.org 2001/02/07 17:04:52 [pathnames.h sftp.c] allow sftp over ssh protocol 1; ok djm@ - deraadt@cvs.openbsd.org 2001/02/08 07:38:55 [scp.c] memory leak fix, and snprintf throughout - deraadt@cvs.openbsd.org 2001/02/08 08:02:02 [sftp-int.c] plug a memory leak - stevesk@cvs.openbsd.org 2001/02/08 10:11:23 [session.c sftp-client.c] %i -> %d - stevesk@cvs.openbsd.org 2001/02/08 10:57:59 [sftp-int.c] typo - stevesk@cvs.openbsd.org 2001/02/08 15:28:07 [sftp-int.c pathnames.h] _PATH_LS; ok markus@ - djm@cvs.openbsd.org 2001/02/09 04:46:25 [sftp-int.c] Check for NULL attribs for chown, chmod & chgrp operations, only send relevant attribs back to server; ok markus@ - (djm) Update makefile.in for _PATH_SFTP_SERVER
Diffstat (limited to 'sftp.c')
-rw-r--r--sftp.c97
1 files changed, 67 insertions, 30 deletions
diff --git a/sftp.c b/sftp.c
index 27873d034..b4775c02f 100644
--- a/sftp.c
+++ b/sftp.c
@@ -24,7 +24,7 @@
24 24
25#include "includes.h" 25#include "includes.h"
26 26
27RCSID("$OpenBSD: sftp.c,v 1.2 2001/02/04 15:32:25 stevesk Exp $"); 27RCSID("$OpenBSD: sftp.c,v 1.7 2001/02/08 00:04:52 markus Exp $");
28 28
29/* XXX: commandline mode */ 29/* XXX: commandline mode */
30/* XXX: copy between two remote hosts (commandline) */ 30/* XXX: copy between two remote hosts (commandline) */
@@ -40,6 +40,10 @@ RCSID("$OpenBSD: sftp.c,v 1.2 2001/02/04 15:32:25 stevesk Exp $");
40#include "sftp-client.h" 40#include "sftp-client.h"
41#include "sftp-int.h" 41#include "sftp-int.h"
42 42
43int use_ssh1 = 0;
44char *ssh_program = _PATH_SSH_PROGRAM;
45char *sftp_server = NULL;
46
43void 47void
44connect_to_server(char **args, int *in, int *out, pid_t *sshpid) 48connect_to_server(char **args, int *in, int *out, pid_t *sshpid)
45{ 49{
@@ -72,8 +76,8 @@ connect_to_server(char **args, int *in, int *out, pid_t *sshpid)
72 close(*out); 76 close(*out);
73 close(c_in); 77 close(c_in);
74 close(c_out); 78 close(c_out);
75 execv(_PATH_SSH_PROGRAM, args); 79 execv(ssh_program, args);
76 fprintf(stderr, "exec: %s", strerror(errno)); 80 fprintf(stderr, "exec: %s: %s\n", ssh_program, strerror(errno));
77 exit(1); 81 exit(1);
78 } 82 }
79 83
@@ -87,16 +91,24 @@ make_ssh_args(char *add_arg)
87 static char **args = NULL; 91 static char **args = NULL;
88 static int nargs = 0; 92 static int nargs = 0;
89 char debug_buf[4096]; 93 char debug_buf[4096];
90 int i; 94 int i, use_subsystem = 1;
95
96 /* no subsystem if protocol 1 or the server-spec contains a '/' */
97 if (use_ssh1 ||
98 (sftp_server != NULL && strchr(sftp_server, '/') != NULL))
99 use_subsystem = 0;
91 100
92 /* Init args array */ 101 /* Init args array */
93 if (args == NULL) { 102 if (args == NULL) {
94 nargs = 4; 103 nargs = use_subsystem ? 6 : 5;
95 i = 0; 104 i = 0;
96 args = xmalloc(sizeof(*args) * nargs); 105 args = xmalloc(sizeof(*args) * nargs);
97 args[i++] = "ssh"; 106 args[i++] = "ssh";
98 args[i++] = "-oProtocol=2"; 107 args[i++] = use_ssh1 ? "-oProtocol=1" : "-oProtocol=2";
99 args[i++] = "-s"; 108 if (use_subsystem)
109 args[i++] = "-s";
110 args[i++] = "-oForwardAgent=no";
111 args[i++] = "-oForwardX11=no";
100 args[i++] = NULL; 112 args[i++] = NULL;
101 } 113 }
102 114
@@ -110,7 +122,10 @@ make_ssh_args(char *add_arg)
110 } 122 }
111 123
112 /* Otherwise finish up and return the arg array */ 124 /* Otherwise finish up and return the arg array */
113 make_ssh_args("sftp"); 125 if (sftp_server != NULL)
126 make_ssh_args(sftp_server);
127 else
128 make_ssh_args("sftp");
114 129
115 /* XXX: overflow - doesn't grow debug_buf */ 130 /* XXX: overflow - doesn't grow debug_buf */
116 debug_buf[0] = '\0'; 131 debug_buf[0] = '\0';
@@ -128,49 +143,70 @@ make_ssh_args(char *add_arg)
128void 143void
129usage(void) 144usage(void)
130{ 145{
131 fprintf(stderr, "usage: sftp [-vC] [-osshopt=value] [user@]host\n"); 146 fprintf(stderr, "usage: sftp [-1vC] [-osshopt=value] [user@]host\n");
132 exit(1); 147 exit(1);
133} 148}
134 149
135int 150int
136main(int argc, char **argv) 151main(int argc, char **argv)
137{ 152{
138 int in, out, i, debug_level, compress_flag; 153 int in, out, ch, debug_level, compress_flag;
139 pid_t sshpid; 154 pid_t sshpid;
140 char *cp; 155 char *host, *userhost;
141 LogLevel ll; 156 LogLevel ll;
157 extern int optind;
158 extern char *optarg;
142 159
143 debug_level = compress_flag = 0; 160 debug_level = compress_flag = 0;
144 for(i = 1; i < argc && argv[i][0] == '-'; i++) { 161
145 if (!strcmp(argv[i], "-v")) 162 while ((ch = getopt(argc, argv, "1hvCo:s:S:")) != -1) {
146 debug_level = MIN(3, debug_level + 1); 163 switch (ch) {
147 else if (!strcmp(argv[i], "-C")) 164 case 'C':
148 compress_flag = 1; 165 compress_flag = 1;
149 else if (!strncmp(argv[i], "-o", 2)) { 166 break;
150 make_ssh_args(argv[i]); 167 case 'v':
151 } else { 168 debug_level = MIN(3, debug_level + 1);
152 fprintf(stderr, "Unknown option \"%s\"\n", argv[i]); 169 break;
170 case 'o':
171 make_ssh_args("-o");
172 make_ssh_args(optarg);
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;
184 break;
185 case 'h':
186 default:
153 usage(); 187 usage();
154 } 188 }
155 } 189 }
156 190
157 if (i == argc || argc > (i + 1)) 191 if (optind == argc || argc > (optind + 1))
158 usage(); 192 usage();
159 193
160 if ((cp = strchr(argv[i], '@')) == NULL) 194 userhost = argv[optind];
161 cp = argv[i]; 195
196 if ((host = strchr(userhost, '@')) == NULL)
197 host = userhost;
162 else { 198 else {
163 *cp = '\0'; 199 *host = '\0';
164 if (!argv[i][0]) { 200 if (!userhost[0]) {
165 fprintf(stderr, "Missing username\n"); 201 fprintf(stderr, "Missing username\n");
166 usage(); 202 usage();
167 } 203 }
168 make_ssh_args("-l"); 204 make_ssh_args("-l");
169 make_ssh_args(argv[i]); 205 make_ssh_args(userhost);
170 cp++; 206 host++;
171 } 207 }
172 208
173 if (!*cp) { 209 if (!*host) {
174 fprintf(stderr, "Missing hostname\n"); 210 fprintf(stderr, "Missing hostname\n");
175 usage(); 211 usage();
176 } 212 }
@@ -200,9 +236,9 @@ main(int argc, char **argv)
200 236
201 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); 237 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
202 238
203 make_ssh_args(cp); 239 make_ssh_args(host);
204 240
205 fprintf(stderr, "Connecting to %s...\n", cp); 241 fprintf(stderr, "Connecting to %s...\n", host);
206 242
207 connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid); 243 connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid);
208 244
@@ -216,7 +252,8 @@ main(int argc, char **argv)
216 if (kill(sshpid, SIGHUP) == -1) 252 if (kill(sshpid, SIGHUP) == -1)
217 fatal("Couldn't terminate ssh process: %s", strerror(errno)); 253 fatal("Couldn't terminate ssh process: %s", strerror(errno));
218 254
219 /* XXX: wait? */ 255 if (waitpid(sshpid, NULL, 0) == -1)
256 fatal("Couldn't wait for ssh process: %s", strerror(errno));
220 257
221 exit(0); 258 exit(0);
222} 259}