Replace the type-checking COMPTR macro with my current idea of best
[sgt/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) {
dfb88efd 66 smemclr(cmdline_password, strlen(cmdline_password));
b72bdf11 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
b61f81bc 108 prompt_set_result(p->prompts[0], cmdline_password);
dfb88efd 109 smemclr(cmdline_password, strlen(cmdline_password));
b72bdf11 110 sfree(cmdline_password);
111 cmdline_password = NULL;
edd0cb8a 112 tried_once = 1;
113 return 1;
dfe3332b 114}
115
116/*
117 * Here we have a flags word which describes the capabilities of
118 * the particular tool on whose behalf we're running. We will
119 * refuse certain command-line options if a particular tool
120 * inherently can't do anything sensible. For example, the file
121 * transfer tools (psftp, pscp) can't do a great deal with protocol
122 * selections (ever tried running scp over telnet?) or with port
123 * forwarding (even if it wasn't a hideously bad idea, they don't
124 * have the select() infrastructure to make them work).
125 */
126int cmdline_tooltype = 0;
127
128static int cmdline_check_unavailable(int flag, char *p)
129{
130 if (cmdline_tooltype & flag) {
131 cmdline_error("option \"%s\" not available in this tool", p);
132 return 1;
133 }
134 return 0;
135}
136
137#define UNAVAILABLE_IN(flag) do { \
138 if (cmdline_check_unavailable(flag, p)) return ret; \
139} while (0)
140
141/*
142 * Process a standard command-line parameter. `p' is the parameter
143 * in question; `value' is the subsequent element of argv, which
144 * may or may not be required as an operand to the parameter.
b552f500 145 * If `need_save' is 1, arguments which need to be saved as
146 * described at this top of this file are, for later execution;
147 * if 0, they are processed normally. (-1 is a special value used
148 * by pterm to count arguments for a preliminary pass through the
149 * argument list; it causes immediate return with an appropriate
150 * value with no action taken.)
dfe3332b 151 * Return value is 2 if both arguments were used; 1 if only p was
152 * used; 0 if the parameter wasn't one we recognised; -2 if it
153 * should have been 2 but value was NULL.
154 */
155
156#define RETURN(x) do { \
157 if ((x) == 2 && !value) return -2; \
158 ret = x; \
46a3419b 159 if (need_save < 0) return x; \
dfe3332b 160} while (0)
161
4a693cfc 162int cmdline_process_param(char *p, char *value, int need_save, Conf *conf)
dfe3332b 163{
164 int ret = 0;
165
166 if (!strcmp(p, "-load")) {
167 RETURN(2);
c725e24c 168 /* This parameter must be processed immediately rather than being
169 * saved. */
4a693cfc 170 do_defaults(value, conf);
18e62ad8 171 loaded_session = TRUE;
073e9f42 172 cmdline_session_name = dupstr(value);
dfe3332b 173 return 2;
174 }
175 if (!strcmp(p, "-ssh")) {
176 RETURN(1);
46a3419b 177 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 178 SAVEABLE(0);
4a693cfc 179 default_protocol = PROT_SSH;
180 default_port = 22;
181 conf_set_int(conf, CONF_protocol, default_protocol);
182 conf_set_int(conf, CONF_port, default_port);
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);
4a693cfc 189 default_protocol = PROT_TELNET;
190 default_port = 23;
191 conf_set_int(conf, CONF_protocol, default_protocol);
192 conf_set_int(conf, CONF_port, default_port);
dfe3332b 193 return 1;
194 }
195 if (!strcmp(p, "-rlogin")) {
196 RETURN(1);
46a3419b 197 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 198 SAVEABLE(0);
4a693cfc 199 default_protocol = PROT_RLOGIN;
200 default_port = 513;
201 conf_set_int(conf, CONF_protocol, default_protocol);
202 conf_set_int(conf, CONF_port, default_port);
dfe3332b 203 return 1;
204 }
205 if (!strcmp(p, "-raw")) {
206 RETURN(1);
46a3419b 207 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 208 SAVEABLE(0);
4a693cfc 209 default_protocol = PROT_RAW;
210 conf_set_int(conf, CONF_protocol, default_protocol);
dfe3332b 211 }
9621bbab 212 if (!strcmp(p, "-serial")) {
213 RETURN(1);
214 /* Serial is not NONNETWORK in an odd sense of the word */
215 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
216 SAVEABLE(0);
4a693cfc 217 default_protocol = PROT_SERIAL;
218 conf_set_int(conf, CONF_protocol, default_protocol);
219 /* The host parameter will already be loaded into CONF_host,
220 * so copy it across */
221 conf_set_str(conf, CONF_serline, conf_get_str(conf, CONF_host));
9621bbab 222 }
dfe3332b 223 if (!strcmp(p, "-v")) {
224 RETURN(1);
225 flags |= FLAG_VERBOSE;
226 }
227 if (!strcmp(p, "-l")) {
228 RETURN(2);
46a3419b 229 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 230 SAVEABLE(0);
4a693cfc 231 conf_set_str(conf, CONF_username, value);
dfe3332b 232 }
881da168 233 if (!strcmp(p, "-loghost")) {
234 RETURN(2);
235 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
236 SAVEABLE(0);
4a693cfc 237 conf_set_str(conf, CONF_loghost, value);
881da168 238 }
820ebe3b 239 if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
4a693cfc 240 char type, *q, *qq, *key, *val;
dfe3332b 241 RETURN(2);
46a3419b 242 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 243 SAVEABLE(0);
4a693cfc 244 if (strcmp(p, "-D")) {
820ebe3b 245 /*
4a693cfc 246 * For -L or -R forwarding types:
247 *
820ebe3b 248 * We expect _at least_ two colons in this string. The
249 * possible formats are `sourceport:desthost:destport',
250 * or `sourceip:sourceport:desthost:destport' if you're
251 * specifying a particular loopback address. We need to
252 * replace the one between source and dest with a \t;
253 * this means we must find the second-to-last colon in
254 * the string.
4a693cfc 255 *
256 * (This looks like a foolish way of doing it given the
257 * existence of strrchr, but it's more efficient than
258 * two strrchrs - not to mention that the second strrchr
259 * would require us to modify the input string!)
820ebe3b 260 */
4a693cfc 261
262 type = p[1]; /* 'L' or 'R' */
263
264 q = qq = strchr(value, ':');
820ebe3b 265 while (qq) {
266 char *qqq = strchr(qq+1, ':');
267 if (qqq)
268 q = qq;
269 qq = qqq;
270 }
4a693cfc 271
272 if (!q) {
273 cmdline_error("-%c expects at least two colons in its"
274 " argument", type);
275 return ret;
276 }
277
278 key = dupprintf("%c%.*s", type, q - value, value);
279 val = dupstr(q+1);
280 } else {
281 /*
282 * Dynamic port forwardings are entered under the same key
283 * as if they were local (because they occupy the same
284 * port space - a local and a dynamic forwarding on the
285 * same local port are mutually exclusive), with the
286 * special value "D" (which can be distinguished from
287 * anything in the ordinary -L case by containing no
288 * colon).
289 */
290 key = dupprintf("L%s", value);
291 val = dupstr("D");
6ee9b735 292 }
4a693cfc 293 conf_set_str_str(conf, CONF_portfwd, key, val);
294 sfree(key);
295 sfree(val);
dfe3332b 296 }
feb02b4e 297 if ((!strcmp(p, "-nc"))) {
298 char *host, *portp;
299
300 RETURN(2);
301 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
302 SAVEABLE(0);
303
4a693cfc 304 portp = strchr(value, ':');
305 if (!portp) {
feb02b4e 306 cmdline_error("-nc expects argument of form 'host:port'");
307 return ret;
308 }
4a693cfc 309
310 host = dupprintf("%.*s", portp - value, value);
311 conf_set_str(conf, CONF_ssh_nc_host, host);
312 conf_set_int(conf, CONF_ssh_nc_port, atoi(portp + 1));
783c82b3 313 sfree(host);
feb02b4e 314 }
dfe3332b 315 if (!strcmp(p, "-m")) {
316 char *filename, *command;
317 int cmdlen, cmdsize;
318 FILE *fp;
319 int c, d;
320
321 RETURN(2);
46a3419b 322 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 323 SAVEABLE(0);
dfe3332b 324
325 filename = value;
326
327 cmdlen = cmdsize = 0;
328 command = NULL;
329 fp = fopen(filename, "r");
330 if (!fp) {
4a693cfc 331 cmdline_error("unable to open command file \"%s\"", filename);
dfe3332b 332 return ret;
333 }
334 do {
335 c = fgetc(fp);
336 d = c;
337 if (c == EOF)
338 d = 0;
339 if (cmdlen >= cmdsize) {
340 cmdsize = cmdlen + 512;
3d88e64d 341 command = sresize(command, cmdsize, char);
dfe3332b 342 }
343 command[cmdlen++] = d;
344 } while (c != EOF);
2fa6ce2c 345 fclose(fp);
4a693cfc 346 conf_set_str(conf, CONF_remote_cmd, command);
347 conf_set_str(conf, CONF_remote_cmd2, "");
348 conf_set_int(conf, CONF_nopty, TRUE); /* command => no terminal */
349 sfree(command);
dfe3332b 350 }
351 if (!strcmp(p, "-P")) {
352 RETURN(2);
46a3419b 353 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 354 SAVEABLE(1); /* lower priority than -ssh,-telnet */
4a693cfc 355 conf_set_int(conf, CONF_port, atoi(value));
dfe3332b 356 }
357 if (!strcmp(p, "-pw")) {
358 RETURN(2);
46a3419b 359 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
01ccff1b 360 SAVEABLE(1);
361 /* We delay evaluating this until after the protocol is decided,
362 * so that we can warn if it's of no use with the selected protocol */
4a693cfc 363 if (conf_get_int(conf, CONF_protocol) != PROT_SSH)
4858529b 364 cmdline_error("the -pw option can only be used with the "
01ccff1b 365 "SSH protocol");
4858529b 366 else {
367 cmdline_password = dupstr(value);
368 /* Assuming that `value' is directly from argv, make a good faith
369 * attempt to trample it, to stop it showing up in `ps' output
370 * on Unix-like systems. Not guaranteed, of course. */
dfb88efd 371 smemclr(value, strlen(value));
4858529b 372 }
dfe3332b 373 }
374
973612f5 375 if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||
376 !strcmp(p, "-pageant")) {
377 RETURN(1);
378 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
a3a48ced 379 SAVEABLE(0);
4a693cfc 380 conf_set_int(conf, CONF_tryagent, TRUE);
973612f5 381 }
382 if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||
383 !strcmp(p, "-nopageant")) {
384 RETURN(1);
385 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
a3a48ced 386 SAVEABLE(0);
4a693cfc 387 conf_set_int(conf, CONF_tryagent, FALSE);
973612f5 388 }
389
dfe3332b 390 if (!strcmp(p, "-A")) {
391 RETURN(1);
46a3419b 392 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 393 SAVEABLE(0);
4a693cfc 394 conf_set_int(conf, CONF_agentfwd, 1);
dfe3332b 395 }
396 if (!strcmp(p, "-a")) {
397 RETURN(1);
46a3419b 398 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 399 SAVEABLE(0);
4a693cfc 400 conf_set_int(conf, CONF_agentfwd, 0);
dfe3332b 401 }
402
403 if (!strcmp(p, "-X")) {
404 RETURN(1);
46a3419b 405 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 406 SAVEABLE(0);
4a693cfc 407 conf_set_int(conf, CONF_x11_forward, 1);
dfe3332b 408 }
409 if (!strcmp(p, "-x")) {
410 RETURN(1);
46a3419b 411 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 412 SAVEABLE(0);
4a693cfc 413 conf_set_int(conf, CONF_x11_forward, 0);
dfe3332b 414 }
415
416 if (!strcmp(p, "-t")) {
417 RETURN(1);
46a3419b 418 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
e7cf9486 419 SAVEABLE(1); /* lower priority than -m */
4a693cfc 420 conf_set_int(conf, CONF_nopty, 0);
dfe3332b 421 }
422 if (!strcmp(p, "-T")) {
423 RETURN(1);
46a3419b 424 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
e7cf9486 425 SAVEABLE(1);
4a693cfc 426 conf_set_int(conf, CONF_nopty, 1);
dfe3332b 427 }
428
0ed48730 429 if (!strcmp(p, "-N")) {
430 RETURN(1);
431 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
432 SAVEABLE(0);
4a693cfc 433 conf_set_int(conf, CONF_ssh_no_shell, 1);
0ed48730 434 }
435
dfe3332b 436 if (!strcmp(p, "-C")) {
437 RETURN(1);
46a3419b 438 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 439 SAVEABLE(0);
4a693cfc 440 conf_set_int(conf, CONF_compression, 1);
dfe3332b 441 }
442
443 if (!strcmp(p, "-1")) {
444 RETURN(1);
46a3419b 445 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 446 SAVEABLE(0);
4a693cfc 447 conf_set_int(conf, CONF_sshprot, 0); /* ssh protocol 1 only */
dfe3332b 448 }
449 if (!strcmp(p, "-2")) {
450 RETURN(1);
46a3419b 451 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 452 SAVEABLE(0);
4a693cfc 453 conf_set_int(conf, CONF_sshprot, 3); /* ssh protocol 2 only */
dfe3332b 454 }
455
456 if (!strcmp(p, "-i")) {
962468d4 457 Filename *fn;
dfe3332b 458 RETURN(2);
46a3419b 459 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 460 SAVEABLE(0);
4a693cfc 461 fn = filename_from_str(value);
962468d4 462 conf_set_filename(conf, CONF_keyfile, fn);
463 filename_free(fn);
dfe3332b 464 }
465
05581745 466 if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
467 RETURN(1);
468 SAVEABLE(1);
4a693cfc 469 conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV4);
05581745 470 }
471 if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {
472 RETURN(1);
473 SAVEABLE(1);
4a693cfc 474 conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV6);
05581745 475 }
9621bbab 476 if (!strcmp(p, "-sercfg")) {
477 char* nextitem;
478 RETURN(2);
479 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
480 SAVEABLE(1);
4a693cfc 481 if (conf_get_int(conf, CONF_protocol) != PROT_SERIAL)
9621bbab 482 cmdline_error("the -sercfg option can only be used with the "
483 "serial protocol");
484 /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */
485 nextitem = value;
486 while (nextitem[0] != '\0') {
487 int length, skip;
488 char *end = strchr(nextitem, ',');
489 if (!end) {
490 length = strlen(nextitem);
491 skip = 0;
492 } else {
493 length = end - nextitem;
494 nextitem[length] = '\0';
495 skip = 1;
496 }
497 if (length == 1) {
498 switch (*nextitem) {
499 case '1':
9621bbab 500 case '2':
4a693cfc 501 conf_set_int(conf, CONF_serstopbits, 2 * (*nextitem-'0'));
9621bbab 502 break;
503
504 case '5':
a3e17bf8 505 case '6':
506 case '7':
507 case '8':
508 case '9':
4a693cfc 509 conf_set_int(conf, CONF_serdatabits, *nextitem-'0');
9621bbab 510 break;
511
512 case 'n':
4a693cfc 513 conf_set_int(conf, CONF_serparity, SER_PAR_NONE);
9621bbab 514 break;
515 case 'o':
4a693cfc 516 conf_set_int(conf, CONF_serparity, SER_PAR_ODD);
9621bbab 517 break;
518 case 'e':
4a693cfc 519 conf_set_int(conf, CONF_serparity, SER_PAR_EVEN);
9621bbab 520 break;
521 case 'm':
4a693cfc 522 conf_set_int(conf, CONF_serparity, SER_PAR_MARK);
9621bbab 523 break;
524 case 's':
4a693cfc 525 conf_set_int(conf, CONF_serparity, SER_PAR_SPACE);
9621bbab 526 break;
527
528 case 'N':
4a693cfc 529 conf_set_int(conf, CONF_serflow, SER_FLOW_NONE);
9621bbab 530 break;
531 case 'X':
4a693cfc 532 conf_set_int(conf, CONF_serflow, SER_FLOW_XONXOFF);
9621bbab 533 break;
534 case 'R':
4a693cfc 535 conf_set_int(conf, CONF_serflow, SER_FLOW_RTSCTS);
9621bbab 536 break;
537 case 'D':
4a693cfc 538 conf_set_int(conf, CONF_serflow, SER_FLOW_DSRDTR);
9621bbab 539 break;
540
541 default:
542 cmdline_error("Unrecognised suboption \"-sercfg %c\"",
543 *nextitem);
544 }
545 } else if (length == 3 && !strncmp(nextitem,"1.5",3)) {
546 /* Messy special case */
4a693cfc 547 conf_set_int(conf, CONF_serstopbits, 3);
9621bbab 548 } else {
549 int serspeed = atoi(nextitem);
550 if (serspeed != 0) {
4a693cfc 551 conf_set_int(conf, CONF_serspeed, serspeed);
9621bbab 552 } else {
553 cmdline_error("Unrecognised suboption \"-sercfg %s\"",
554 nextitem);
555 }
556 }
557 nextitem += length + skip;
558 }
559 }
dfe3332b 560 return ret; /* unrecognised */
561}
562
4a693cfc 563void cmdline_run_saved(Conf *conf)
dfe3332b 564{
565 int pri, i;
566 for (pri = 0; pri < NPRIORITIES; pri++)
567 for (i = 0; i < saves[pri].nsaved; i++)
568 cmdline_process_param(saves[pri].params[i].p,
4a693cfc 569 saves[pri].params[i].value, 0, conf);
dfe3332b 570}