summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--misc.c25
-rw-r--r--misc.h14
-rw-r--r--scp.c55
-rw-r--r--sftp.c123
5 files changed, 83 insertions, 139 deletions
diff --git a/ChangeLog b/ChangeLog
index 0d9d70353..ce6b6b631 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,9 @@
8 adds correct error reporting to async connect()s 8 adds correct error reporting to async connect()s
9 fixes the server-discards-data-before-connected-bug found by 9 fixes the server-discards-data-before-connected-bug found by
10 onoe@sm.sony.co.jp 10 onoe@sm.sony.co.jp
11 - mouring@cvs.openbsd.org 2001/05/08 19:45:25
12 [misc.c misc.h scp.c sftp.c]
13 Use addargs() in sftp plus some clean up of addargs(). OK Markus
11 14
1220010508 1520010508
13 - (bal) Fixed configure test for USE_SIA. 16 - (bal) Fixed configure test for USE_SIA.
@@ -5368,4 +5371,4 @@
5368 - Wrote replacements for strlcpy and mkdtemp 5371 - Wrote replacements for strlcpy and mkdtemp
5369 - Released 1.0pre1 5372 - Released 1.0pre1
5370 5373
5371$Id: ChangeLog,v 1.1207 2001/05/08 20:07:39 mouring Exp $ 5374$Id: ChangeLog,v 1.1208 2001/05/08 20:27:25 mouring Exp $
diff --git a/misc.c b/misc.c
index e949ded89..d12bcefe5 100644
--- a/misc.c
+++ b/misc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: misc.c,v 1.6 2001/05/03 23:09:52 mouring Exp $ */ 1/* $OpenBSD: misc.c,v 1.7 2001/05/08 19:45:24 mouring Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2000 Markus Friedl. All rights reserved. 4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
@@ -25,7 +25,7 @@
25 */ 25 */
26 26
27#include "includes.h" 27#include "includes.h"
28RCSID("$OpenBSD: misc.c,v 1.6 2001/05/03 23:09:52 mouring Exp $"); 28RCSID("$OpenBSD: misc.c,v 1.7 2001/05/08 19:45:24 mouring Exp $");
29 29
30#include "misc.h" 30#include "misc.h"
31#include "log.h" 31#include "log.h"
@@ -164,6 +164,27 @@ colon(char *cp)
164 return (0); 164 return (0);
165} 165}
166 166
167void
168addargs(arglist *args, char *fmt, ...)
169{
170 va_list ap;
171 char buf[1024];
172
173 va_start(ap, fmt);
174 vsnprintf(buf, sizeof(buf), fmt, ap);
175 va_end(ap);
176
177 if (args->list == NULL) {
178 args->nalloc = 32;
179 args->num = 0;
180 } else if (args->num+2 >= args->nalloc)
181 args->nalloc *= 2;
182
183 args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
184 args->list[args->num++] = xstrdup(buf);
185 args->list[args->num] = NULL;
186}
187
167mysig_t 188mysig_t
168mysignal(int sig, mysig_t act) 189mysignal(int sig, mysig_t act)
169{ 190{
diff --git a/misc.h b/misc.h
index a307df3a0..b30fe4884 100644
--- a/misc.h
+++ b/misc.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: misc.h,v 1.5 2001/05/03 23:09:52 mouring Exp $ */ 1/* $OpenBSD: misc.h,v 1.6 2001/05/08 19:45:24 mouring Exp $ */
2 2
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -28,11 +28,21 @@ struct passwd * pwcopy(struct passwd *pw);
28 * Return 0 if invalid. 28 * Return 0 if invalid.
29 */ 29 */
30int a2port(const char *s); 30int a2port(const char *s);
31 31
32/* code from scp.c/rcp.c */ 32/* code from scp.c/rcp.c */
33char *cleanhostname(char *host); 33char *cleanhostname(char *host);
34char *colon(char *cp); 34char *colon(char *cp);
35 35
36/* function to assist building execv() arguments */
37typedef struct arglist arglist;
38struct arglist {
39 char **list;
40 int num;
41 int nalloc;
42};
43
44void addargs(arglist *args, char *fmt, ...) __attribute__((format(printf, 2, 3)));
45
36/* wrapper for signal interface */ 46/* wrapper for signal interface */
37typedef void (*mysig_t)(int); 47typedef void (*mysig_t)(int);
38mysig_t mysignal(int sig, mysig_t act); 48mysig_t mysignal(int sig, mysig_t act);
diff --git a/scp.c b/scp.c
index 10b77af6d..52fd513c6 100644
--- a/scp.c
+++ b/scp.c
@@ -75,7 +75,7 @@
75 */ 75 */
76 76
77#include "includes.h" 77#include "includes.h"
78RCSID("$OpenBSD: scp.c,v 1.69 2001/05/03 23:09:53 mouring Exp $"); 78RCSID("$OpenBSD: scp.c,v 1.70 2001/05/08 19:45:24 mouring Exp $");
79 79
80#include "xmalloc.h" 80#include "xmalloc.h"
81#include "atomicio.h" 81#include "atomicio.h"
@@ -107,8 +107,8 @@ void progressmeter(int);
107int getttywidth(void); 107int getttywidth(void);
108int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc); 108int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
109 109
110/* setup arguments for the call to ssh */ 110/* Struct for addargs */
111void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2))); 111arglist args;
112 112
113/* Time a transfer started. */ 113/* Time a transfer started. */
114static struct timeval start; 114static struct timeval start;
@@ -131,13 +131,6 @@ int showprogress = 1;
131/* This is the program to execute for the secured connection. ("ssh" or -S) */ 131/* This is the program to execute for the secured connection. ("ssh" or -S) */
132char *ssh_program = _PATH_SSH_PROGRAM; 132char *ssh_program = _PATH_SSH_PROGRAM;
133 133
134/* This is the list of arguments that scp passes to ssh */
135struct {
136 char **list;
137 int num;
138 int nalloc;
139} args;
140
141/* 134/*
142 * This function executes the given command as the specified user on the 135 * This function executes the given command as the specified user on the
143 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This 136 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
@@ -181,9 +174,9 @@ do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
181 174
182 args.list[0] = ssh_program; 175 args.list[0] = ssh_program;
183 if (remuser != NULL) 176 if (remuser != NULL)
184 addargs("-l%s", remuser); 177 addargs(&args, "-l%s", remuser);
185 addargs("%s", host); 178 addargs(&args, "%s", host);
186 addargs("%s", cmd); 179 addargs(&args, "%s", cmd);
187 180
188 execvp(ssh_program, args.list); 181 execvp(ssh_program, args.list);
189 perror(ssh_program); 182 perror(ssh_program);
@@ -238,9 +231,9 @@ main(argc, argv)
238 __progname = get_progname(argv[0]); 231 __progname = get_progname(argv[0]);
239 232
240 args.list = NULL; 233 args.list = NULL;
241 addargs("ssh"); /* overwritten with ssh_program */ 234 addargs(&args, "ssh"); /* overwritten with ssh_program */
242 addargs("-x"); 235 addargs(&args, "-x");
243 addargs("-oFallBackToRsh no"); 236 addargs(&args, "-oFallBackToRsh no");
244 237
245 fflag = tflag = 0; 238 fflag = tflag = 0;
246 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1) 239 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
@@ -249,18 +242,18 @@ main(argc, argv)
249 case '4': 242 case '4':
250 case '6': 243 case '6':
251 case 'C': 244 case 'C':
252 addargs("-%c", ch); 245 addargs(&args, "-%c", ch);
253 break; 246 break;
254 case 'o': 247 case 'o':
255 case 'c': 248 case 'c':
256 case 'i': 249 case 'i':
257 addargs("-%c%s", ch, optarg); 250 addargs(&args, "-%c%s", ch, optarg);
258 break; 251 break;
259 case 'P': 252 case 'P':
260 addargs("-p%s", optarg); 253 addargs(&args, "-p%s", optarg);
261 break; 254 break;
262 case 'B': 255 case 'B':
263 addargs("-oBatchmode yes"); 256 addargs(&args, "-oBatchmode yes");
264 break; 257 break;
265 case 'p': 258 case 'p':
266 pflag = 1; 259 pflag = 1;
@@ -1212,25 +1205,3 @@ getttywidth(void)
1212 else 1205 else
1213 return (80); 1206 return (80);
1214} 1207}
1215
1216void
1217addargs(char *fmt, ...)
1218{
1219 va_list ap;
1220 char buf[1024];
1221
1222 va_start(ap, fmt);
1223 vsnprintf(buf, sizeof(buf), fmt, ap);
1224 va_end(ap);
1225
1226 if (args.list == NULL) {
1227 args.nalloc = 32;
1228 args.num = 0;
1229 args.list = xmalloc(args.nalloc * sizeof(char *));
1230 } else if (args.num+2 >= args.nalloc) {
1231 args.nalloc *= 2;
1232 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1233 }
1234 args.list[args.num++] = xstrdup(buf);
1235 args.list[args.num] = NULL;
1236}
diff --git a/sftp.c b/sftp.c
index 895e0e125..dd8c08b13 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.16 2001/05/03 23:09:53 mouring Exp $"); 27RCSID("$OpenBSD: sftp.c,v 1.17 2001/05/08 19:45:25 mouring Exp $");
28 28
29/* XXX: commandline mode */ 29/* XXX: commandline mode */
30/* XXX: short-form remote directory listings (like 'ls -C') */ 30/* XXX: short-form remote directory listings (like 'ls -C') */
@@ -46,9 +46,7 @@ extern char *__progname;
46char *__progname; 46char *__progname;
47#endif 47#endif
48 48
49int use_ssh1 = 0;
50char *ssh_program = _PATH_SSH_PROGRAM; 49char *ssh_program = _PATH_SSH_PROGRAM;
51char *sftp_server = NULL;
52FILE* infile; 50FILE* infile;
53 51
54void 52void
@@ -92,58 +90,6 @@ connect_to_server(char **args, int *in, int *out, pid_t *sshpid)
92 close(c_out); 90 close(c_out);
93} 91}
94 92
95char **
96make_ssh_args(char *add_arg)
97{
98 static char **args = NULL;
99 static int nargs = 0;
100 char debug_buf[4096];
101 int i;
102
103 /* Init args array */
104 if (args == NULL) {
105 nargs = 2;
106 i = 0;
107 args = xmalloc(sizeof(*args) * nargs);
108 args[i++] = "ssh";
109 args[i++] = NULL;
110 }
111
112 /* If asked to add args, then do so and return */
113 if (add_arg) {
114 i = nargs++ - 1;
115 args = xrealloc(args, sizeof(*args) * nargs);
116 args[i++] = add_arg;
117 args[i++] = NULL;
118 return(NULL);
119 }
120
121 /* no subsystem if the server-spec contains a '/' */
122 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
123 make_ssh_args("-s");
124 make_ssh_args("-oForwardX11=no");
125 make_ssh_args("-oForwardAgent=no");
126 make_ssh_args(use_ssh1 ? "-oProtocol=1" : "-oProtocol=2");
127
128 /* Otherwise finish up and return the arg array */
129 if (sftp_server != NULL)
130 make_ssh_args(sftp_server);
131 else
132 make_ssh_args("sftp");
133
134 /* XXX: overflow - doesn't grow debug_buf */
135 debug_buf[0] = '\0';
136 for(i = 0; args[i]; i++) {
137 if (i)
138 strlcat(debug_buf, " ", sizeof(debug_buf));
139
140 strlcat(debug_buf, args[i], sizeof(debug_buf));
141 }
142 debug("SSH args \"%s\"", debug_buf);
143
144 return(args);
145}
146
147void 93void
148usage(void) 94usage(void)
149{ 95{
@@ -154,32 +100,42 @@ usage(void)
154int 100int
155main(int argc, char **argv) 101main(int argc, char **argv)
156{ 102{
157 int in, out, ch, debug_level, compress_flag; 103 int in, out, ch;
158 pid_t sshpid; 104 pid_t sshpid;
159 char *file1 = NULL;
160 char *host, *userhost, *cp, *file2; 105 char *host, *userhost, *cp, *file2;
161 LogLevel ll; 106 int debug_level = 0, sshver = 2;
107 char *file1 = NULL, *sftp_server = NULL;
108 LogLevel ll = SYSLOG_LEVEL_INFO;
109 arglist args;
162 extern int optind; 110 extern int optind;
163 extern char *optarg; 111 extern char *optarg;
164 112
165 __progname = get_progname(argv[0]); 113 __progname = get_progname(argv[0]);
166 infile = stdin; /* Read from STDIN unless changed by -b */ 114 args.list = NULL;
167 debug_level = compress_flag = 0; 115 addargs(&args, "ssh"); /* overwritten with ssh_program */
116 addargs(&args, "-oFallBackToRsh no");
117 addargs(&args, "-oForwardX11 no");
118 addargs(&args, "-oForwardAgent no");
119 ll = SYSLOG_LEVEL_INFO;
120 infile = stdin; /* Read from STDIN unless changed by -b */
168 121
169 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:")) != -1) { 122 while ((ch = getopt(argc, argv, "1hvCo:s:S:b:")) != -1) {
170 switch (ch) { 123 switch (ch) {
171 case 'C': 124 case 'C':
172 compress_flag = 1; 125 addargs(&args, "-C");
173 break; 126 break;
174 case 'v': 127 case 'v':
175 debug_level = MIN(3, debug_level + 1); 128 if (debug_level < 3) {
129 addargs(&args, "-v");
130 ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
131 }
132 debug_level++;
176 break; 133 break;
177 case 'o': 134 case 'o':
178 make_ssh_args("-o"); 135 addargs(&args, "-o%s", optarg);
179 make_ssh_args(optarg);
180 break; 136 break;
181 case '1': 137 case '1':
182 use_ssh1 = 1; 138 sshver = 1;
183 if (sftp_server == NULL) 139 if (sftp_server == NULL)
184 sftp_server = _PATH_SFTP_SERVER; 140 sftp_server = _PATH_SFTP_SERVER;
185 break; 141 break;
@@ -222,8 +178,7 @@ main(int argc, char **argv)
222 fprintf(stderr, "Missing username\n"); 178 fprintf(stderr, "Missing username\n");
223 usage(); 179 usage();
224 } 180 }
225 make_ssh_args("-l"); 181 addargs(&args, "-l%s",userhost);
226 make_ssh_args(userhost);
227 } 182 }
228 183
229 host = cleanhostname(host); 184 host = cleanhostname(host);
@@ -232,36 +187,20 @@ main(int argc, char **argv)
232 usage(); 187 usage();
233 } 188 }
234 189
235 /* Set up logging and debug '-d' arguments to ssh */
236 ll = SYSLOG_LEVEL_INFO;
237 switch (debug_level) {
238 case 1:
239 ll = SYSLOG_LEVEL_DEBUG1;
240 make_ssh_args("-v");
241 break;
242 case 2:
243 ll = SYSLOG_LEVEL_DEBUG2;
244 make_ssh_args("-v");
245 make_ssh_args("-v");
246 break;
247 case 3:
248 ll = SYSLOG_LEVEL_DEBUG3;
249 make_ssh_args("-v");
250 make_ssh_args("-v");
251 make_ssh_args("-v");
252 break;
253 }
254
255 if (compress_flag)
256 make_ssh_args("-C");
257
258 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); 190 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
191 addargs(&args, "-oProtocol %d", sshver);
192
193 /* no subsystem if the server-spec contains a '/' */
194 if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
195 addargs(&args, "-s");
259 196
260 make_ssh_args(host); 197 addargs(&args, "%s", host);
198 addargs(&args, "%s", (sftp_server != NULL ? sftp_server : "sftp"));
199 args.list[0] = ssh_program;
261 200
262 fprintf(stderr, "Connecting to %s...\n", host); 201 fprintf(stderr, "Connecting to %s...\n", host);
263 202
264 connect_to_server(make_ssh_args(NULL), &in, &out, &sshpid); 203 connect_to_server(args.list, &in, &out, &sshpid);
265 204
266 interactive_loop(in, out, file1, file2); 205 interactive_loop(in, out, file1, file2);
267 206