Readjust checklist, because actually the section on updating 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;
073e9f42 175 cmdline_session_name = dupstr(value);
dfe3332b 176 return 2;
177 }
178 if (!strcmp(p, "-ssh")) {
179 RETURN(1);
46a3419b 180 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 181 SAVEABLE(0);
5555d393 182 default_protocol = cfg->protocol = PROT_SSH;
183 default_port = cfg->port = 22;
dfe3332b 184 return 1;
185 }
186 if (!strcmp(p, "-telnet")) {
187 RETURN(1);
46a3419b 188 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 189 SAVEABLE(0);
5555d393 190 default_protocol = cfg->protocol = PROT_TELNET;
191 default_port = cfg->port = 23;
dfe3332b 192 return 1;
193 }
194 if (!strcmp(p, "-rlogin")) {
195 RETURN(1);
46a3419b 196 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 197 SAVEABLE(0);
5555d393 198 default_protocol = cfg->protocol = PROT_RLOGIN;
199 default_port = cfg->port = 513;
dfe3332b 200 return 1;
201 }
202 if (!strcmp(p, "-raw")) {
203 RETURN(1);
46a3419b 204 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 205 SAVEABLE(0);
5555d393 206 default_protocol = cfg->protocol = PROT_RAW;
dfe3332b 207 }
9621bbab 208 if (!strcmp(p, "-serial")) {
209 RETURN(1);
210 /* Serial is not NONNETWORK in an odd sense of the word */
211 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
212 SAVEABLE(0);
213 default_protocol = cfg->protocol = PROT_SERIAL;
214 /* The host parameter will already be loaded into cfg->host, so copy it across */
215 strncpy(cfg->serline, cfg->host, sizeof(cfg->serline) - 1);
216 cfg->serline[sizeof(cfg->serline) - 1] = '\0';
217 }
dfe3332b 218 if (!strcmp(p, "-v")) {
219 RETURN(1);
220 flags |= FLAG_VERBOSE;
221 }
222 if (!strcmp(p, "-l")) {
223 RETURN(2);
46a3419b 224 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 225 SAVEABLE(0);
5555d393 226 strncpy(cfg->username, value, sizeof(cfg->username));
227 cfg->username[sizeof(cfg->username) - 1] = '\0';
dfe3332b 228 }
881da168 229 if (!strcmp(p, "-loghost")) {
230 RETURN(2);
231 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
232 SAVEABLE(0);
233 strncpy(cfg->loghost, value, sizeof(cfg->loghost));
234 cfg->loghost[sizeof(cfg->loghost) - 1] = '\0';
235 }
820ebe3b 236 if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
6ee9b735 237 char *fwd, *ptr, *q, *qq;
820ebe3b 238 int dynamic, i=0;
dfe3332b 239 RETURN(2);
46a3419b 240 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 241 SAVEABLE(0);
820ebe3b 242 dynamic = !strcmp(p, "-D");
dfe3332b 243 fwd = value;
5555d393 244 ptr = cfg->portfwd;
116f2044 245 /* if existing forwards, find end of list */
246 while (*ptr) {
247 while (*ptr)
248 ptr++;
249 ptr++;
dfe3332b 250 }
116f2044 251 i = ptr - cfg->portfwd;
820ebe3b 252 ptr[0] = p[1]; /* insert a 'L', 'R' or 'D' at the start */
116f2044 253 ptr++;
254 if (1 + strlen(fwd) + 2 > sizeof(cfg->portfwd) - i) {
dfe3332b 255 cmdline_error("out of space for port forwardings");
256 return ret;
257 }
116f2044 258 strncpy(ptr, fwd, sizeof(cfg->portfwd) - i - 2);
820ebe3b 259 if (!dynamic) {
260 /*
261 * We expect _at least_ two colons in this string. The
262 * possible formats are `sourceport:desthost:destport',
263 * or `sourceip:sourceport:desthost:destport' if you're
264 * specifying a particular loopback address. We need to
265 * replace the one between source and dest with a \t;
266 * this means we must find the second-to-last colon in
267 * the string.
268 */
269 q = qq = strchr(ptr, ':');
270 while (qq) {
271 char *qqq = strchr(qq+1, ':');
272 if (qqq)
273 q = qq;
274 qq = qqq;
275 }
276 if (q) *q = '\t'; /* replace second-last colon with \t */
6ee9b735 277 }
5555d393 278 cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';
279 cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';
116f2044 280 ptr[strlen(ptr)+1] = '\000'; /* append 2nd '\000' */
dfe3332b 281 }
feb02b4e 282 if ((!strcmp(p, "-nc"))) {
283 char *host, *portp;
284
285 RETURN(2);
286 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
287 SAVEABLE(0);
288
289 host = portp = value;
290 while (*portp && *portp != ':')
291 portp++;
292 if (*portp) {
293 unsigned len = portp - host;
294 if (len >= sizeof(cfg->ssh_nc_host))
295 len = sizeof(cfg->ssh_nc_host) - 1;
786476b9 296 memcpy(cfg->ssh_nc_host, value, len);
297 cfg->ssh_nc_host[len] = '\0';
feb02b4e 298 cfg->ssh_nc_port = atoi(portp+1);
299 } else {
300 cmdline_error("-nc expects argument of form 'host:port'");
301 return ret;
302 }
303 }
dfe3332b 304 if (!strcmp(p, "-m")) {
305 char *filename, *command;
306 int cmdlen, cmdsize;
307 FILE *fp;
308 int c, d;
309
310 RETURN(2);
46a3419b 311 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 312 SAVEABLE(0);
dfe3332b 313
314 filename = value;
315
316 cmdlen = cmdsize = 0;
317 command = NULL;
318 fp = fopen(filename, "r");
319 if (!fp) {
320 cmdline_error("unable to open command "
321 "file \"%s\"", filename);
322 return ret;
323 }
324 do {
325 c = fgetc(fp);
326 d = c;
327 if (c == EOF)
328 d = 0;
329 if (cmdlen >= cmdsize) {
330 cmdsize = cmdlen + 512;
3d88e64d 331 command = sresize(command, cmdsize, char);
dfe3332b 332 }
333 command[cmdlen++] = d;
334 } while (c != EOF);
5555d393 335 cfg->remote_cmd_ptr = command;
336 cfg->remote_cmd_ptr2 = NULL;
337 cfg->nopty = TRUE; /* command => no terminal */
2fa6ce2c 338 fclose(fp);
dfe3332b 339 }
340 if (!strcmp(p, "-P")) {
341 RETURN(2);
46a3419b 342 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 343 SAVEABLE(1); /* lower priority than -ssh,-telnet */
5555d393 344 cfg->port = atoi(value);
dfe3332b 345 }
346 if (!strcmp(p, "-pw")) {
347 RETURN(2);
46a3419b 348 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
01ccff1b 349 SAVEABLE(1);
350 /* We delay evaluating this until after the protocol is decided,
351 * so that we can warn if it's of no use with the selected protocol */
352 if (cfg->protocol != PROT_SSH)
4858529b 353 cmdline_error("the -pw option can only be used with the "
01ccff1b 354 "SSH protocol");
4858529b 355 else {
356 cmdline_password = dupstr(value);
357 /* Assuming that `value' is directly from argv, make a good faith
358 * attempt to trample it, to stop it showing up in `ps' output
359 * on Unix-like systems. Not guaranteed, of course. */
360 memset(value, 0, strlen(value));
361 }
dfe3332b 362 }
363
973612f5 364 if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||
365 !strcmp(p, "-pageant")) {
366 RETURN(1);
367 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
a3a48ced 368 SAVEABLE(0);
973612f5 369 cfg->tryagent = TRUE;
370 }
371 if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||
372 !strcmp(p, "-nopageant")) {
373 RETURN(1);
374 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
a3a48ced 375 SAVEABLE(0);
973612f5 376 cfg->tryagent = FALSE;
377 }
378
dfe3332b 379 if (!strcmp(p, "-A")) {
380 RETURN(1);
46a3419b 381 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 382 SAVEABLE(0);
5555d393 383 cfg->agentfwd = 1;
dfe3332b 384 }
385 if (!strcmp(p, "-a")) {
386 RETURN(1);
46a3419b 387 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 388 SAVEABLE(0);
5555d393 389 cfg->agentfwd = 0;
dfe3332b 390 }
391
392 if (!strcmp(p, "-X")) {
393 RETURN(1);
46a3419b 394 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 395 SAVEABLE(0);
5555d393 396 cfg->x11_forward = 1;
dfe3332b 397 }
398 if (!strcmp(p, "-x")) {
399 RETURN(1);
46a3419b 400 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 401 SAVEABLE(0);
5555d393 402 cfg->x11_forward = 0;
dfe3332b 403 }
404
405 if (!strcmp(p, "-t")) {
406 RETURN(1);
46a3419b 407 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
e7cf9486 408 SAVEABLE(1); /* lower priority than -m */
5555d393 409 cfg->nopty = 0;
dfe3332b 410 }
411 if (!strcmp(p, "-T")) {
412 RETURN(1);
46a3419b 413 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
e7cf9486 414 SAVEABLE(1);
5555d393 415 cfg->nopty = 1;
dfe3332b 416 }
417
0ed48730 418 if (!strcmp(p, "-N")) {
419 RETURN(1);
420 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
421 SAVEABLE(0);
422 cfg->ssh_no_shell = 1;
423 }
424
dfe3332b 425 if (!strcmp(p, "-C")) {
426 RETURN(1);
46a3419b 427 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 428 SAVEABLE(0);
5555d393 429 cfg->compression = 1;
dfe3332b 430 }
431
432 if (!strcmp(p, "-1")) {
433 RETURN(1);
46a3419b 434 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 435 SAVEABLE(0);
5555d393 436 cfg->sshprot = 0; /* ssh protocol 1 only */
dfe3332b 437 }
438 if (!strcmp(p, "-2")) {
439 RETURN(1);
46a3419b 440 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 441 SAVEABLE(0);
5555d393 442 cfg->sshprot = 3; /* ssh protocol 2 only */
dfe3332b 443 }
444
445 if (!strcmp(p, "-i")) {
446 RETURN(2);
46a3419b 447 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 448 SAVEABLE(0);
9a30e26b 449 cfg->keyfile = filename_from_str(value);
dfe3332b 450 }
451
05581745 452 if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
453 RETURN(1);
454 SAVEABLE(1);
455 cfg->addressfamily = ADDRTYPE_IPV4;
456 }
457 if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {
458 RETURN(1);
459 SAVEABLE(1);
460 cfg->addressfamily = ADDRTYPE_IPV6;
461 }
9621bbab 462 if (!strcmp(p, "-sercfg")) {
463 char* nextitem;
464 RETURN(2);
465 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
466 SAVEABLE(1);
467 if (cfg->protocol != PROT_SERIAL)
468 cmdline_error("the -sercfg option can only be used with the "
469 "serial protocol");
470 /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */
471 nextitem = value;
472 while (nextitem[0] != '\0') {
473 int length, skip;
474 char *end = strchr(nextitem, ',');
475 if (!end) {
476 length = strlen(nextitem);
477 skip = 0;
478 } else {
479 length = end - nextitem;
480 nextitem[length] = '\0';
481 skip = 1;
482 }
483 if (length == 1) {
484 switch (*nextitem) {
485 case '1':
486 cfg->serstopbits = 2;
487 break;
488 case '2':
489 cfg->serstopbits = 4;
490 break;
491
492 case '5':
493 cfg->serdatabits = 5;
494 break;
495 case '6':
496 cfg->serdatabits = 6;
497 break;
498 case '7':
499 cfg->serdatabits = 7;
500 break;
501 case '8':
502 cfg->serdatabits = 8;
503 break;
504 case '9':
505 cfg->serdatabits = 9;
506 break;
507
508 case 'n':
509 cfg->serparity = SER_PAR_NONE;
510 break;
511 case 'o':
512 cfg->serparity = SER_PAR_ODD;
513 break;
514 case 'e':
515 cfg->serparity = SER_PAR_EVEN;
516 break;
517 case 'm':
518 cfg->serparity = SER_PAR_MARK;
519 break;
520 case 's':
521 cfg->serparity = SER_PAR_SPACE;
522 break;
523
524 case 'N':
525 cfg->serflow = SER_FLOW_NONE;
526 break;
527 case 'X':
528 cfg->serflow = SER_FLOW_XONXOFF;
529 break;
530 case 'R':
531 cfg->serflow = SER_FLOW_RTSCTS;
532 break;
533 case 'D':
534 cfg->serflow = SER_FLOW_DSRDTR;
535 break;
536
537 default:
538 cmdline_error("Unrecognised suboption \"-sercfg %c\"",
539 *nextitem);
540 }
541 } else if (length == 3 && !strncmp(nextitem,"1.5",3)) {
542 /* Messy special case */
543 cfg->serstopbits = 3;
544 } else {
545 int serspeed = atoi(nextitem);
546 if (serspeed != 0) {
547 cfg->serspeed = serspeed;
548 } else {
549 cmdline_error("Unrecognised suboption \"-sercfg %s\"",
550 nextitem);
551 }
552 }
553 nextitem += length + skip;
554 }
555 }
dfe3332b 556 return ret; /* unrecognised */
557}
558
5555d393 559void cmdline_run_saved(Config *cfg)
dfe3332b 560{
561 int pri, i;
562 for (pri = 0; pri < NPRIORITIES; pri++)
563 for (i = 0; i < saves[pri].nsaved; i++)
564 cmdline_process_param(saves[pri].params[i].p,
5555d393 565 saves[pri].params[i].value, 0, cfg);
dfe3332b 566}