Joe Yates's memory leak patches.
[u/mdw/putty] / cmdline.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include "putty.h"
5
6 /*
7 * Some command-line parameters need to be saved up until after
8 * we've loaded the saved session which will form the basis of our
9 * eventual running configuration. For this we use the macro
10 * SAVEABLE, which notices if the `need_save' parameter is set and
11 * saves the parameter and value on a list.
12 *
13 * We also assign priorities to saved parameters, just to slightly
14 * ameliorate silly ordering problems. For example, if you specify
15 * a saved session to load, it will be loaded _before_ all your
16 * local modifications such as -L are evaluated; and if you specify
17 * a protocol and a port, the protocol is set up first so that the
18 * port can override its choice of port number.
19 *
20 * (In fact -load is not saved at all, since in at least Plink the
21 * processing of further command-line options depends on whether or
22 * not the loaded session contained a hostname. So it must be
23 * executed immediately.)
24 */
25
26 #define NPRIORITIES 2
27
28 struct cmdline_saved_param {
29 char *p, *value;
30 };
31 struct cmdline_saved_param_set {
32 struct cmdline_saved_param *params;
33 int nsaved, savesize;
34 };
35
36 /*
37 * C guarantees this structure will be initialised to all zero at
38 * program start, which is exactly what we want.
39 */
40 static struct cmdline_saved_param_set saves[NPRIORITIES];
41
42 static void cmdline_save_param(char *p, char *value, int pri)
43 {
44 if (saves[pri].nsaved >= saves[pri].savesize) {
45 saves[pri].savesize = saves[pri].nsaved + 32;
46 saves[pri].params = sresize(saves[pri].params, saves[pri].savesize,
47 struct cmdline_saved_param);
48 }
49 saves[pri].params[saves[pri].nsaved].p = p;
50 saves[pri].params[saves[pri].nsaved].value = value;
51 saves[pri].nsaved++;
52 }
53
54 void cmdline_cleanup(void)
55 {
56 int pri;
57
58 for (pri = 0; pri < NPRIORITIES; pri++)
59 sfree(saves[pri].params);
60 }
61
62 #define SAVEABLE(pri) do { \
63 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
64 } while (0)
65
66 char *cmdline_password = NULL;
67
68 static int cmdline_get_line(const char *prompt, char *str,
69 int maxlen, int is_pw)
70 {
71 static int tried_once = 0;
72
73 assert(is_pw && cmdline_password);
74
75 if (tried_once) {
76 return 0;
77 } else {
78 strncpy(str, cmdline_password, maxlen);
79 str[maxlen - 1] = '\0';
80 tried_once = 1;
81 return 1;
82 }
83 }
84
85 /*
86 * Here we have a flags word which describes the capabilities of
87 * the particular tool on whose behalf we're running. We will
88 * refuse certain command-line options if a particular tool
89 * inherently can't do anything sensible. For example, the file
90 * transfer tools (psftp, pscp) can't do a great deal with protocol
91 * selections (ever tried running scp over telnet?) or with port
92 * forwarding (even if it wasn't a hideously bad idea, they don't
93 * have the select() infrastructure to make them work).
94 */
95 int cmdline_tooltype = 0;
96
97 static int cmdline_check_unavailable(int flag, char *p)
98 {
99 if (cmdline_tooltype & flag) {
100 cmdline_error("option \"%s\" not available in this tool", p);
101 return 1;
102 }
103 return 0;
104 }
105
106 #define UNAVAILABLE_IN(flag) do { \
107 if (cmdline_check_unavailable(flag, p)) return ret; \
108 } while (0)
109
110 /*
111 * Process a standard command-line parameter. `p' is the parameter
112 * in question; `value' is the subsequent element of argv, which
113 * may or may not be required as an operand to the parameter.
114 * Return value is 2 if both arguments were used; 1 if only p was
115 * used; 0 if the parameter wasn't one we recognised; -2 if it
116 * should have been 2 but value was NULL.
117 */
118
119 #define RETURN(x) do { \
120 if ((x) == 2 && !value) return -2; \
121 ret = x; \
122 if (need_save < 0) return x; \
123 } while (0)
124
125 int cmdline_process_param(char *p, char *value, int need_save, Config *cfg)
126 {
127 int ret = 0;
128
129 if (!strcmp(p, "-load")) {
130 RETURN(2);
131 /* This parameter must be processed immediately rather than being
132 * saved. */
133 do_defaults(value, cfg);
134 return 2;
135 }
136 if (!strcmp(p, "-ssh")) {
137 RETURN(1);
138 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
139 SAVEABLE(0);
140 default_protocol = cfg->protocol = PROT_SSH;
141 default_port = cfg->port = 22;
142 return 1;
143 }
144 if (!strcmp(p, "-telnet")) {
145 RETURN(1);
146 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
147 SAVEABLE(0);
148 default_protocol = cfg->protocol = PROT_TELNET;
149 default_port = cfg->port = 23;
150 return 1;
151 }
152 if (!strcmp(p, "-rlogin")) {
153 RETURN(1);
154 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
155 SAVEABLE(0);
156 default_protocol = cfg->protocol = PROT_RLOGIN;
157 default_port = cfg->port = 513;
158 return 1;
159 }
160 if (!strcmp(p, "-raw")) {
161 RETURN(1);
162 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
163 SAVEABLE(0);
164 default_protocol = cfg->protocol = PROT_RAW;
165 }
166 if (!strcmp(p, "-v")) {
167 RETURN(1);
168 flags |= FLAG_VERBOSE;
169 }
170 if (!strcmp(p, "-l")) {
171 RETURN(2);
172 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
173 SAVEABLE(0);
174 strncpy(cfg->username, value, sizeof(cfg->username));
175 cfg->username[sizeof(cfg->username) - 1] = '\0';
176 }
177 if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
178 char *fwd, *ptr, *q, *qq;
179 int dynamic, i=0;
180 RETURN(2);
181 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
182 SAVEABLE(0);
183 dynamic = !strcmp(p, "-D");
184 fwd = value;
185 ptr = cfg->portfwd;
186 /* if multiple forwards, find end of list */
187 if (ptr[0]=='R' || ptr[0]=='L' || ptr[0] == 'D') {
188 for (i = 0; i < sizeof(cfg->portfwd) - 2; i++)
189 if (ptr[i]=='\000' && ptr[i+1]=='\000')
190 break;
191 ptr = ptr + i + 1; /* point to next forward slot */
192 }
193 ptr[0] = p[1]; /* insert a 'L', 'R' or 'D' at the start */
194 if (strlen(fwd) > sizeof(cfg->portfwd) - i - 2) {
195 cmdline_error("out of space for port forwardings");
196 return ret;
197 }
198 strncpy(ptr+1, fwd, sizeof(cfg->portfwd) - i);
199 if (!dynamic) {
200 /*
201 * We expect _at least_ two colons in this string. The
202 * possible formats are `sourceport:desthost:destport',
203 * or `sourceip:sourceport:desthost:destport' if you're
204 * specifying a particular loopback address. We need to
205 * replace the one between source and dest with a \t;
206 * this means we must find the second-to-last colon in
207 * the string.
208 */
209 q = qq = strchr(ptr, ':');
210 while (qq) {
211 char *qqq = strchr(qq+1, ':');
212 if (qqq)
213 q = qq;
214 qq = qqq;
215 }
216 if (q) *q = '\t'; /* replace second-last colon with \t */
217 }
218 cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';
219 cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';
220 ptr[strlen(ptr)+1] = '\000'; /* append two '\000' */
221 }
222 if (!strcmp(p, "-m")) {
223 char *filename, *command;
224 int cmdlen, cmdsize;
225 FILE *fp;
226 int c, d;
227
228 RETURN(2);
229 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
230 SAVEABLE(0);
231
232 filename = value;
233
234 cmdlen = cmdsize = 0;
235 command = NULL;
236 fp = fopen(filename, "r");
237 if (!fp) {
238 cmdline_error("unable to open command "
239 "file \"%s\"", filename);
240 return ret;
241 }
242 do {
243 c = fgetc(fp);
244 d = c;
245 if (c == EOF)
246 d = 0;
247 if (cmdlen >= cmdsize) {
248 cmdsize = cmdlen + 512;
249 command = sresize(command, cmdsize, char);
250 }
251 command[cmdlen++] = d;
252 } while (c != EOF);
253 cfg->remote_cmd_ptr = command;
254 cfg->remote_cmd_ptr2 = NULL;
255 cfg->nopty = TRUE; /* command => no terminal */
256 }
257 if (!strcmp(p, "-P")) {
258 RETURN(2);
259 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
260 SAVEABLE(1); /* lower priority than -ssh,-telnet */
261 cfg->port = atoi(value);
262 }
263 if (!strcmp(p, "-pw")) {
264 RETURN(2);
265 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
266 cmdline_password = value;
267 ssh_get_line = cmdline_get_line;
268 ssh_getline_pw_only = TRUE;
269 }
270
271 if (!strcmp(p, "-A")) {
272 RETURN(1);
273 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
274 SAVEABLE(0);
275 cfg->agentfwd = 1;
276 }
277 if (!strcmp(p, "-a")) {
278 RETURN(1);
279 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
280 SAVEABLE(0);
281 cfg->agentfwd = 0;
282 }
283
284 if (!strcmp(p, "-X")) {
285 RETURN(1);
286 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
287 SAVEABLE(0);
288 cfg->x11_forward = 1;
289 }
290 if (!strcmp(p, "-x")) {
291 RETURN(1);
292 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
293 SAVEABLE(0);
294 cfg->x11_forward = 0;
295 }
296
297 if (!strcmp(p, "-t")) {
298 RETURN(1);
299 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
300 SAVEABLE(0);
301 cfg->nopty = 0;
302 }
303 if (!strcmp(p, "-T")) {
304 RETURN(1);
305 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
306 SAVEABLE(0);
307 cfg->nopty = 1;
308 }
309
310 if (!strcmp(p, "-C")) {
311 RETURN(1);
312 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
313 SAVEABLE(0);
314 cfg->compression = 1;
315 }
316
317 if (!strcmp(p, "-1")) {
318 RETURN(1);
319 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
320 SAVEABLE(0);
321 cfg->sshprot = 0; /* ssh protocol 1 only */
322 }
323 if (!strcmp(p, "-2")) {
324 RETURN(1);
325 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
326 SAVEABLE(0);
327 cfg->sshprot = 3; /* ssh protocol 2 only */
328 }
329
330 if (!strcmp(p, "-i")) {
331 RETURN(2);
332 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
333 SAVEABLE(0);
334 cfg->keyfile = filename_from_str(value);
335 }
336
337 return ret; /* unrecognised */
338 }
339
340 void cmdline_run_saved(Config *cfg)
341 {
342 int pri, i;
343 for (pri = 0; pri < NPRIORITIES; pri++)
344 for (i = 0; i < saves[pri].nsaved; i++)
345 cmdline_process_param(saves[pri].params[i].p,
346 saves[pri].params[i].value, 0, cfg);
347 }