The special treatment of Alt-resize (to cause resizing to affect the
[u/mdw/putty] / cmdline.c
CommitLineData
eaf1e20a 1/*
2 * cmdline.c - command-line parsing shared between many of the
3 * PuTTY applications
4 */
5
dfe3332b 6#include <stdio.h>
7#include <assert.h>
8#include <stdlib.h>
9#include "putty.h"
10
11/*
12 * Some command-line parameters need to be saved up until after
13 * we've loaded the saved session which will form the basis of our
14 * eventual running configuration. For this we use the macro
15 * SAVEABLE, which notices if the `need_save' parameter is set and
16 * saves the parameter and value on a list.
17 *
18 * We also assign priorities to saved parameters, just to slightly
19 * ameliorate silly ordering problems. For example, if you specify
20 * a saved session to load, it will be loaded _before_ all your
21 * local modifications such as -L are evaluated; and if you specify
22 * a protocol and a port, the protocol is set up first so that the
23 * port can override its choice of port number.
c725e24c 24 *
25 * (In fact -load is not saved at all, since in at least Plink the
26 * processing of further command-line options depends on whether or
27 * not the loaded session contained a hostname. So it must be
28 * executed immediately.)
dfe3332b 29 */
30
c725e24c 31#define NPRIORITIES 2
dfe3332b 32
33struct cmdline_saved_param {
34 char *p, *value;
35};
36struct cmdline_saved_param_set {
37 struct cmdline_saved_param *params;
38 int nsaved, savesize;
39};
40
41/*
42 * C guarantees this structure will be initialised to all zero at
43 * program start, which is exactly what we want.
44 */
45static struct cmdline_saved_param_set saves[NPRIORITIES];
46
47static void cmdline_save_param(char *p, char *value, int pri)
48{
49 if (saves[pri].nsaved >= saves[pri].savesize) {
50 saves[pri].savesize = saves[pri].nsaved + 32;
3d88e64d 51 saves[pri].params = sresize(saves[pri].params, saves[pri].savesize,
52 struct cmdline_saved_param);
dfe3332b 53 }
54 saves[pri].params[saves[pri].nsaved].p = p;
55 saves[pri].params[saves[pri].nsaved].value = value;
56 saves[pri].nsaved++;
57}
58
b72bdf11 59static char *cmdline_password = NULL;
60
679539d7 61void cmdline_cleanup(void)
62{
63 int pri;
64
b72bdf11 65 if (cmdline_password) {
66 memset(cmdline_password, 0, strlen(cmdline_password));
67 sfree(cmdline_password);
68 cmdline_password = NULL;
69 }
70
71 for (pri = 0; pri < NPRIORITIES; pri++) {
679539d7 72 sfree(saves[pri].params);
b72bdf11 73 saves[pri].params = NULL;
74 saves[pri].savesize = 0;
75 saves[pri].nsaved = 0;
76 }
679539d7 77}
78
dfe3332b 79#define SAVEABLE(pri) do { \
80 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
81} while (0)
82
edd0cb8a 83/*
84 * Similar interface to get_userpass_input(), except that here a -1
85 * return means that we aren't capable of processing the prompt and
86 * someone else should do it.
87 */
88int cmdline_get_passwd_input(prompts_t *p, unsigned char *in, int inlen) {
dfe3332b 89
dfe3332b 90 static int tried_once = 0;
91
edd0cb8a 92 /*
93 * We only handle prompts which don't echo (which we assume to be
94 * passwords), and (currently) we only cope with a password prompt
95 * that comes in a prompt-set on its own.
96 */
97 if (!cmdline_password || in || p->n_prompts != 1 || p->prompts[0]->echo) {
98 return -1;
99 }
dfe3332b 100
edd0cb8a 101 /*
102 * If we've tried once, return utter failure (no more passwords left
103 * to try).
104 */
105 if (tried_once)
dfe3332b 106 return 0;
edd0cb8a 107
108 strncpy(p->prompts[0]->result, cmdline_password,
109 p->prompts[0]->result_len);
110 p->prompts[0]->result[p->prompts[0]->result_len-1] = '\0';
111 memset(cmdline_password, 0, strlen(cmdline_password));
b72bdf11 112 sfree(cmdline_password);
113 cmdline_password = NULL;
edd0cb8a 114 tried_once = 1;
115 return 1;
116
dfe3332b 117}
118
119/*
120 * Here we have a flags word which describes the capabilities of
121 * the particular tool on whose behalf we're running. We will
122 * refuse certain command-line options if a particular tool
123 * inherently can't do anything sensible. For example, the file
124 * transfer tools (psftp, pscp) can't do a great deal with protocol
125 * selections (ever tried running scp over telnet?) or with port
126 * forwarding (even if it wasn't a hideously bad idea, they don't
127 * have the select() infrastructure to make them work).
128 */
129int cmdline_tooltype = 0;
130
131static int cmdline_check_unavailable(int flag, char *p)
132{
133 if (cmdline_tooltype & flag) {
134 cmdline_error("option \"%s\" not available in this tool", p);
135 return 1;
136 }
137 return 0;
138}
139
140#define UNAVAILABLE_IN(flag) do { \
141 if (cmdline_check_unavailable(flag, p)) return ret; \
142} while (0)
143
144/*
145 * Process a standard command-line parameter. `p' is the parameter
146 * in question; `value' is the subsequent element of argv, which
147 * may or may not be required as an operand to the parameter.
b552f500 148 * If `need_save' is 1, arguments which need to be saved as
149 * described at this top of this file are, for later execution;
150 * if 0, they are processed normally. (-1 is a special value used
151 * by pterm to count arguments for a preliminary pass through the
152 * argument list; it causes immediate return with an appropriate
153 * value with no action taken.)
dfe3332b 154 * Return value is 2 if both arguments were used; 1 if only p was
155 * used; 0 if the parameter wasn't one we recognised; -2 if it
156 * should have been 2 but value was NULL.
157 */
158
159#define RETURN(x) do { \
160 if ((x) == 2 && !value) return -2; \
161 ret = x; \
46a3419b 162 if (need_save < 0) return x; \
dfe3332b 163} while (0)
164
5555d393 165int cmdline_process_param(char *p, char *value, int need_save, Config *cfg)
dfe3332b 166{
167 int ret = 0;
168
169 if (!strcmp(p, "-load")) {
170 RETURN(2);
c725e24c 171 /* This parameter must be processed immediately rather than being
172 * saved. */
5555d393 173 do_defaults(value, cfg);
18e62ad8 174 loaded_session = TRUE;
dfe3332b 175 return 2;
176 }
177 if (!strcmp(p, "-ssh")) {
178 RETURN(1);
46a3419b 179 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 180 SAVEABLE(0);
5555d393 181 default_protocol = cfg->protocol = PROT_SSH;
182 default_port = cfg->port = 22;
dfe3332b 183 return 1;
184 }
185 if (!strcmp(p, "-telnet")) {
186 RETURN(1);
46a3419b 187 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 188 SAVEABLE(0);
5555d393 189 default_protocol = cfg->protocol = PROT_TELNET;
190 default_port = cfg->port = 23;
dfe3332b 191 return 1;
192 }
193 if (!strcmp(p, "-rlogin")) {
194 RETURN(1);
46a3419b 195 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 196 SAVEABLE(0);
5555d393 197 default_protocol = cfg->protocol = PROT_RLOGIN;
198 default_port = cfg->port = 513;
dfe3332b 199 return 1;
200 }
201 if (!strcmp(p, "-raw")) {
202 RETURN(1);
46a3419b 203 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 204 SAVEABLE(0);
5555d393 205 default_protocol = cfg->protocol = PROT_RAW;
dfe3332b 206 }
9621bbab 207 if (!strcmp(p, "-serial")) {
208 RETURN(1);
209 /* Serial is not NONNETWORK in an odd sense of the word */
210 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
211 SAVEABLE(0);
212 default_protocol = cfg->protocol = PROT_SERIAL;
213 /* The host parameter will already be loaded into cfg->host, so copy it across */
214 strncpy(cfg->serline, cfg->host, sizeof(cfg->serline) - 1);
215 cfg->serline[sizeof(cfg->serline) - 1] = '\0';
216 }
dfe3332b 217 if (!strcmp(p, "-v")) {
218 RETURN(1);
219 flags |= FLAG_VERBOSE;
220 }
221 if (!strcmp(p, "-l")) {
222 RETURN(2);
46a3419b 223 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 224 SAVEABLE(0);
5555d393 225 strncpy(cfg->username, value, sizeof(cfg->username));
226 cfg->username[sizeof(cfg->username) - 1] = '\0';
dfe3332b 227 }
881da168 228 if (!strcmp(p, "-loghost")) {
229 RETURN(2);
230 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
231 SAVEABLE(0);
232 strncpy(cfg->loghost, value, sizeof(cfg->loghost));
233 cfg->loghost[sizeof(cfg->loghost) - 1] = '\0';
234 }
820ebe3b 235 if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
6ee9b735 236 char *fwd, *ptr, *q, *qq;
820ebe3b 237 int dynamic, i=0;
dfe3332b 238 RETURN(2);
46a3419b 239 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 240 SAVEABLE(0);
820ebe3b 241 dynamic = !strcmp(p, "-D");
dfe3332b 242 fwd = value;
5555d393 243 ptr = cfg->portfwd;
116f2044 244 /* if existing forwards, find end of list */
245 while (*ptr) {
246 while (*ptr)
247 ptr++;
248 ptr++;
dfe3332b 249 }
116f2044 250 i = ptr - cfg->portfwd;
820ebe3b 251 ptr[0] = p[1]; /* insert a 'L', 'R' or 'D' at the start */
116f2044 252 ptr++;
253 if (1 + strlen(fwd) + 2 > sizeof(cfg->portfwd) - i) {
dfe3332b 254 cmdline_error("out of space for port forwardings");
255 return ret;
256 }
116f2044 257 strncpy(ptr, fwd, sizeof(cfg->portfwd) - i - 2);
820ebe3b 258 if (!dynamic) {
259 /*
260 * We expect _at least_ two colons in this string. The
261 * possible formats are `sourceport:desthost:destport',
262 * or `sourceip:sourceport:desthost:destport' if you're
263 * specifying a particular loopback address. We need to
264 * replace the one between source and dest with a \t;
265 * this means we must find the second-to-last colon in
266 * the string.
267 */
268 q = qq = strchr(ptr, ':');
269 while (qq) {
270 char *qqq = strchr(qq+1, ':');
271 if (qqq)
272 q = qq;
273 qq = qqq;
274 }
275 if (q) *q = '\t'; /* replace second-last colon with \t */
6ee9b735 276 }
5555d393 277 cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';
278 cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';
116f2044 279 ptr[strlen(ptr)+1] = '\000'; /* append 2nd '\000' */
dfe3332b 280 }
feb02b4e 281 if ((!strcmp(p, "-nc"))) {
282 char *host, *portp;
283
284 RETURN(2);
285 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
286 SAVEABLE(0);
287
288 host = portp = value;
289 while (*portp && *portp != ':')
290 portp++;
291 if (*portp) {
292 unsigned len = portp - host;
293 if (len >= sizeof(cfg->ssh_nc_host))
294 len = sizeof(cfg->ssh_nc_host) - 1;
786476b9 295 memcpy(cfg->ssh_nc_host, value, len);
296 cfg->ssh_nc_host[len] = '\0';
feb02b4e 297 cfg->ssh_nc_port = atoi(portp+1);
298 } else {
299 cmdline_error("-nc expects argument of form 'host:port'");
300 return ret;
301 }
302 }
dfe3332b 303 if (!strcmp(p, "-m")) {
304 char *filename, *command;
305 int cmdlen, cmdsize;
306 FILE *fp;
307 int c, d;
308
309 RETURN(2);
46a3419b 310 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 311 SAVEABLE(0);
dfe3332b 312
313 filename = value;
314
315 cmdlen = cmdsize = 0;
316 command = NULL;
317 fp = fopen(filename, "r");
318 if (!fp) {
319 cmdline_error("unable to open command "
320 "file \"%s\"", filename);
321 return ret;
322 }
323 do {
324 c = fgetc(fp);
325 d = c;
326 if (c == EOF)
327 d = 0;
328 if (cmdlen >= cmdsize) {
329 cmdsize = cmdlen + 512;
3d88e64d 330 command = sresize(command, cmdsize, char);
dfe3332b 331 }
332 command[cmdlen++] = d;
333 } while (c != EOF);
5555d393 334 cfg->remote_cmd_ptr = command;
335 cfg->remote_cmd_ptr2 = NULL;
336 cfg->nopty = TRUE; /* command => no terminal */
2fa6ce2c 337 fclose(fp);
dfe3332b 338 }
339 if (!strcmp(p, "-P")) {
340 RETURN(2);
46a3419b 341 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 342 SAVEABLE(1); /* lower priority than -ssh,-telnet */
5555d393 343 cfg->port = atoi(value);
dfe3332b 344 }
345 if (!strcmp(p, "-pw")) {
346 RETURN(2);
46a3419b 347 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
01ccff1b 348 SAVEABLE(1);
349 /* We delay evaluating this until after the protocol is decided,
350 * so that we can warn if it's of no use with the selected protocol */
351 if (cfg->protocol != PROT_SSH)
4858529b 352 cmdline_error("the -pw option can only be used with the "
01ccff1b 353 "SSH protocol");
4858529b 354 else {
355 cmdline_password = dupstr(value);
356 /* Assuming that `value' is directly from argv, make a good faith
357 * attempt to trample it, to stop it showing up in `ps' output
358 * on Unix-like systems. Not guaranteed, of course. */
359 memset(value, 0, strlen(value));
360 }
dfe3332b 361 }
362
973612f5 363 if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||
364 !strcmp(p, "-pageant")) {
365 RETURN(1);
366 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
a3a48ced 367 SAVEABLE(0);
973612f5 368 cfg->tryagent = TRUE;
369 }
370 if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||
371 !strcmp(p, "-nopageant")) {
372 RETURN(1);
373 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
a3a48ced 374 SAVEABLE(0);
973612f5 375 cfg->tryagent = FALSE;
376 }
377
dfe3332b 378 if (!strcmp(p, "-A")) {
379 RETURN(1);
46a3419b 380 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 381 SAVEABLE(0);
5555d393 382 cfg->agentfwd = 1;
dfe3332b 383 }
384 if (!strcmp(p, "-a")) {
385 RETURN(1);
46a3419b 386 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 387 SAVEABLE(0);
5555d393 388 cfg->agentfwd = 0;
dfe3332b 389 }
390
391 if (!strcmp(p, "-X")) {
392 RETURN(1);
46a3419b 393 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 394 SAVEABLE(0);
5555d393 395 cfg->x11_forward = 1;
dfe3332b 396 }
397 if (!strcmp(p, "-x")) {
398 RETURN(1);
46a3419b 399 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 400 SAVEABLE(0);
5555d393 401 cfg->x11_forward = 0;
dfe3332b 402 }
403
404 if (!strcmp(p, "-t")) {
405 RETURN(1);
46a3419b 406 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
e7cf9486 407 SAVEABLE(1); /* lower priority than -m */
5555d393 408 cfg->nopty = 0;
dfe3332b 409 }
410 if (!strcmp(p, "-T")) {
411 RETURN(1);
46a3419b 412 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
e7cf9486 413 SAVEABLE(1);
5555d393 414 cfg->nopty = 1;
dfe3332b 415 }
416
0ed48730 417 if (!strcmp(p, "-N")) {
418 RETURN(1);
419 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
420 SAVEABLE(0);
421 cfg->ssh_no_shell = 1;
422 }
423
dfe3332b 424 if (!strcmp(p, "-C")) {
425 RETURN(1);
46a3419b 426 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 427 SAVEABLE(0);
5555d393 428 cfg->compression = 1;
dfe3332b 429 }
430
431 if (!strcmp(p, "-1")) {
432 RETURN(1);
46a3419b 433 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 434 SAVEABLE(0);
5555d393 435 cfg->sshprot = 0; /* ssh protocol 1 only */
dfe3332b 436 }
437 if (!strcmp(p, "-2")) {
438 RETURN(1);
46a3419b 439 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 440 SAVEABLE(0);
5555d393 441 cfg->sshprot = 3; /* ssh protocol 2 only */
dfe3332b 442 }
443
444 if (!strcmp(p, "-i")) {
445 RETURN(2);
46a3419b 446 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 447 SAVEABLE(0);
9a30e26b 448 cfg->keyfile = filename_from_str(value);
dfe3332b 449 }
450
05581745 451 if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
452 RETURN(1);
453 SAVEABLE(1);
454 cfg->addressfamily = ADDRTYPE_IPV4;
455 }
456 if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {
457 RETURN(1);
458 SAVEABLE(1);
459 cfg->addressfamily = ADDRTYPE_IPV6;
460 }
9621bbab 461 if (!strcmp(p, "-sercfg")) {
462 char* nextitem;
463 RETURN(2);
464 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
465 SAVEABLE(1);
466 if (cfg->protocol != PROT_SERIAL)
467 cmdline_error("the -sercfg option can only be used with the "
468 "serial protocol");
469 /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */
470 nextitem = value;
471 while (nextitem[0] != '\0') {
472 int length, skip;
473 char *end = strchr(nextitem, ',');
474 if (!end) {
475 length = strlen(nextitem);
476 skip = 0;
477 } else {
478 length = end - nextitem;
479 nextitem[length] = '\0';
480 skip = 1;
481 }
482 if (length == 1) {
483 switch (*nextitem) {
484 case '1':
485 cfg->serstopbits = 2;
486 break;
487 case '2':
488 cfg->serstopbits = 4;
489 break;
490
491 case '5':
492 cfg->serdatabits = 5;
493 break;
494 case '6':
495 cfg->serdatabits = 6;
496 break;
497 case '7':
498 cfg->serdatabits = 7;
499 break;
500 case '8':
501 cfg->serdatabits = 8;
502 break;
503 case '9':
504 cfg->serdatabits = 9;
505 break;
506
507 case 'n':
508 cfg->serparity = SER_PAR_NONE;
509 break;
510 case 'o':
511 cfg->serparity = SER_PAR_ODD;
512 break;
513 case 'e':
514 cfg->serparity = SER_PAR_EVEN;
515 break;
516 case 'm':
517 cfg->serparity = SER_PAR_MARK;
518 break;
519 case 's':
520 cfg->serparity = SER_PAR_SPACE;
521 break;
522
523 case 'N':
524 cfg->serflow = SER_FLOW_NONE;
525 break;
526 case 'X':
527 cfg->serflow = SER_FLOW_XONXOFF;
528 break;
529 case 'R':
530 cfg->serflow = SER_FLOW_RTSCTS;
531 break;
532 case 'D':
533 cfg->serflow = SER_FLOW_DSRDTR;
534 break;
535
536 default:
537 cmdline_error("Unrecognised suboption \"-sercfg %c\"",
538 *nextitem);
539 }
540 } else if (length == 3 && !strncmp(nextitem,"1.5",3)) {
541 /* Messy special case */
542 cfg->serstopbits = 3;
543 } else {
544 int serspeed = atoi(nextitem);
545 if (serspeed != 0) {
546 cfg->serspeed = serspeed;
547 } else {
548 cmdline_error("Unrecognised suboption \"-sercfg %s\"",
549 nextitem);
550 }
551 }
552 nextitem += length + skip;
553 }
554 }
dfe3332b 555 return ret; /* unrecognised */
556}
557
5555d393 558void cmdline_run_saved(Config *cfg)
dfe3332b 559{
560 int pri, i;
561 for (pri = 0; pri < NPRIORITIES; pri++)
562 for (i = 0; i < saves[pri].nsaved; i++)
563 cmdline_process_param(saves[pri].params[i].p,
5555d393 564 saves[pri].params[i].value, 0, cfg);
dfe3332b 565}