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