2 * cmdline.c - command-line parsing shared between many of the
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.
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.
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.)
33 struct cmdline_saved_param
{
36 struct cmdline_saved_param_set
{
37 struct cmdline_saved_param
*params
;
42 * C guarantees this structure will be initialised to all zero at
43 * program start, which is exactly what we want.
45 static struct cmdline_saved_param_set saves
[NPRIORITIES
];
47 static void cmdline_save_param(char *p
, char *value
, int pri
)
49 if (saves
[pri
].nsaved
>= saves
[pri
].savesize
) {
50 saves
[pri
].savesize
= saves
[pri
].nsaved
+ 32;
51 saves
[pri
].params
= sresize(saves
[pri
].params
, saves
[pri
].savesize
,
52 struct cmdline_saved_param
);
54 saves
[pri
].params
[saves
[pri
].nsaved
].p
= p
;
55 saves
[pri
].params
[saves
[pri
].nsaved
].value
= value
;
59 static char *cmdline_password
= NULL
;
61 void cmdline_cleanup(void)
65 if (cmdline_password
) {
66 memset(cmdline_password
, 0, strlen(cmdline_password
));
67 sfree(cmdline_password
);
68 cmdline_password
= NULL
;
71 for (pri
= 0; pri
< NPRIORITIES
; pri
++) {
72 sfree(saves
[pri
].params
);
73 saves
[pri
].params
= NULL
;
74 saves
[pri
].savesize
= 0;
75 saves
[pri
].nsaved
= 0;
79 #define SAVEABLE(pri) do { \
80 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
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.
88 int cmdline_get_passwd_input(prompts_t
*p
, unsigned char *in
, int inlen
) {
90 static int tried_once
= 0;
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.
97 if (!cmdline_password
|| in
|| p
->n_prompts
!= 1 || p
->prompts
[0]->echo
) {
102 * If we've tried once, return utter failure (no more passwords left
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
));
112 sfree(cmdline_password
);
113 cmdline_password
= NULL
;
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).
129 int cmdline_tooltype
= 0;
131 static int cmdline_check_unavailable(int flag
, char *p
)
133 if (cmdline_tooltype
& flag
) {
134 cmdline_error("option \"%s\" not available in this tool", p
);
140 #define UNAVAILABLE_IN(flag) do { \
141 if (cmdline_check_unavailable(flag, p)) return ret; \
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.
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.)
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.
159 #define RETURN(x) do { \
160 if ((x) == 2 && !value) return -2; \
162 if (need_save < 0) return x; \
165 int cmdline_process_param(char *p
, char *value
, int need_save
, Conf
*conf
)
169 if (!strcmp(p
, "-load")) {
171 /* This parameter must be processed immediately rather than being
173 do_defaults(value
, conf
);
174 loaded_session
= TRUE
;
175 cmdline_session_name
= dupstr(value
);
178 if (!strcmp(p
, "-ssh")) {
180 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
182 default_protocol
= PROT_SSH
;
184 conf_set_int(conf
, CONF_protocol
, default_protocol
);
185 conf_set_int(conf
, CONF_port
, default_port
);
188 if (!strcmp(p
, "-telnet")) {
190 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
192 default_protocol
= PROT_TELNET
;
194 conf_set_int(conf
, CONF_protocol
, default_protocol
);
195 conf_set_int(conf
, CONF_port
, default_port
);
198 if (!strcmp(p
, "-rlogin")) {
200 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
202 default_protocol
= PROT_RLOGIN
;
204 conf_set_int(conf
, CONF_protocol
, default_protocol
);
205 conf_set_int(conf
, CONF_port
, default_port
);
208 if (!strcmp(p
, "-raw")) {
210 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
212 default_protocol
= PROT_RAW
;
213 conf_set_int(conf
, CONF_protocol
, default_protocol
);
215 if (!strcmp(p
, "-serial")) {
217 /* Serial is not NONNETWORK in an odd sense of the word */
218 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
220 default_protocol
= PROT_SERIAL
;
221 conf_set_int(conf
, CONF_protocol
, default_protocol
);
222 /* The host parameter will already be loaded into CONF_host,
223 * so copy it across */
224 conf_set_str(conf
, CONF_serline
, conf_get_str(conf
, CONF_host
));
226 if (!strcmp(p
, "-v")) {
228 flags
|= FLAG_VERBOSE
;
230 if (!strcmp(p
, "-l")) {
232 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
234 conf_set_str(conf
, CONF_username
, value
);
236 if (!strcmp(p
, "-loghost")) {
238 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
240 conf_set_str(conf
, CONF_loghost
, value
);
242 if ((!strcmp(p
, "-L") || !strcmp(p
, "-R") || !strcmp(p
, "-D"))) {
243 char type
, *q
, *qq
, *key
, *val
;
245 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
247 if (strcmp(p
, "-D")) {
249 * For -L or -R forwarding types:
251 * We expect _at least_ two colons in this string. The
252 * possible formats are `sourceport:desthost:destport',
253 * or `sourceip:sourceport:desthost:destport' if you're
254 * specifying a particular loopback address. We need to
255 * replace the one between source and dest with a \t;
256 * this means we must find the second-to-last colon in
259 * (This looks like a foolish way of doing it given the
260 * existence of strrchr, but it's more efficient than
261 * two strrchrs - not to mention that the second strrchr
262 * would require us to modify the input string!)
265 type
= p
[1]; /* 'L' or 'R' */
267 q
= qq
= strchr(value
, ':');
269 char *qqq
= strchr(qq
+1, ':');
276 cmdline_error("-%c expects at least two colons in its"
281 key
= dupprintf("%c%.*s", type
, q
- value
, value
);
285 * Dynamic port forwardings are entered under the same key
286 * as if they were local (because they occupy the same
287 * port space - a local and a dynamic forwarding on the
288 * same local port are mutually exclusive), with the
289 * special value "D" (which can be distinguished from
290 * anything in the ordinary -L case by containing no
293 key
= dupprintf("L%s", value
);
296 conf_set_str_str(conf
, CONF_portfwd
, key
, val
);
300 if ((!strcmp(p
, "-nc"))) {
304 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
307 portp
= strchr(value
, ':');
309 cmdline_error("-nc expects argument of form 'host:port'");
313 host
= dupprintf("%.*s", portp
- value
, value
);
314 conf_set_str(conf
, CONF_ssh_nc_host
, host
);
315 conf_set_int(conf
, CONF_ssh_nc_port
, atoi(portp
+ 1));
317 if (!strcmp(p
, "-m")) {
318 char *filename
, *command
;
324 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
329 cmdlen
= cmdsize
= 0;
331 fp
= fopen(filename
, "r");
333 cmdline_error("unable to open command file \"%s\"", filename
);
341 if (cmdlen
>= cmdsize
) {
342 cmdsize
= cmdlen
+ 512;
343 command
= sresize(command
, cmdsize
, char);
345 command
[cmdlen
++] = d
;
348 conf_set_str(conf
, CONF_remote_cmd
, command
);
349 conf_set_str(conf
, CONF_remote_cmd2
, "");
350 conf_set_int(conf
, CONF_nopty
, TRUE
); /* command => no terminal */
353 if (!strcmp(p
, "-P")) {
355 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
356 SAVEABLE(1); /* lower priority than -ssh,-telnet */
357 conf_set_int(conf
, CONF_port
, atoi(value
));
359 if (!strcmp(p
, "-pw")) {
361 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
363 /* We delay evaluating this until after the protocol is decided,
364 * so that we can warn if it's of no use with the selected protocol */
365 if (conf_get_int(conf
, CONF_protocol
) != PROT_SSH
)
366 cmdline_error("the -pw option can only be used with the "
369 cmdline_password
= dupstr(value
);
370 /* Assuming that `value' is directly from argv, make a good faith
371 * attempt to trample it, to stop it showing up in `ps' output
372 * on Unix-like systems. Not guaranteed, of course. */
373 memset(value
, 0, strlen(value
));
377 if (!strcmp(p
, "-agent") || !strcmp(p
, "-pagent") ||
378 !strcmp(p
, "-pageant")) {
380 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
382 conf_set_int(conf
, CONF_tryagent
, TRUE
);
384 if (!strcmp(p
, "-noagent") || !strcmp(p
, "-nopagent") ||
385 !strcmp(p
, "-nopageant")) {
387 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
389 conf_set_int(conf
, CONF_tryagent
, FALSE
);
392 if (!strcmp(p
, "-A")) {
394 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
396 conf_set_int(conf
, CONF_agentfwd
, 1);
398 if (!strcmp(p
, "-a")) {
400 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
402 conf_set_int(conf
, CONF_agentfwd
, 0);
405 if (!strcmp(p
, "-X")) {
407 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
409 conf_set_int(conf
, CONF_x11_forward
, 1);
411 if (!strcmp(p
, "-x")) {
413 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
415 conf_set_int(conf
, CONF_x11_forward
, 0);
418 if (!strcmp(p
, "-t")) {
420 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
421 SAVEABLE(1); /* lower priority than -m */
422 conf_set_int(conf
, CONF_nopty
, 0);
424 if (!strcmp(p
, "-T")) {
426 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
428 conf_set_int(conf
, CONF_nopty
, 1);
431 if (!strcmp(p
, "-N")) {
433 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
435 conf_set_int(conf
, CONF_ssh_no_shell
, 1);
438 if (!strcmp(p
, "-C")) {
440 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
442 conf_set_int(conf
, CONF_compression
, 1);
445 if (!strcmp(p
, "-1")) {
447 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
449 conf_set_int(conf
, CONF_sshprot
, 0); /* ssh protocol 1 only */
451 if (!strcmp(p
, "-2")) {
453 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
455 conf_set_int(conf
, CONF_sshprot
, 3); /* ssh protocol 2 only */
458 if (!strcmp(p
, "-i")) {
461 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
463 fn
= filename_from_str(value
);
464 conf_set_filename(conf
, CONF_keyfile
, &fn
);
467 if (!strcmp(p
, "-4") || !strcmp(p
, "-ipv4")) {
470 conf_set_int(conf
, CONF_addressfamily
, ADDRTYPE_IPV4
);
472 if (!strcmp(p
, "-6") || !strcmp(p
, "-ipv6")) {
475 conf_set_int(conf
, CONF_addressfamily
, ADDRTYPE_IPV6
);
477 if (!strcmp(p
, "-sercfg")) {
480 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
482 if (conf_get_int(conf
, CONF_protocol
) != PROT_SERIAL
)
483 cmdline_error("the -sercfg option can only be used with the "
485 /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */
487 while (nextitem
[0] != '\0') {
489 char *end
= strchr(nextitem
, ',');
491 length
= strlen(nextitem
);
494 length
= end
- nextitem
;
495 nextitem
[length
] = '\0';
502 conf_set_int(conf
, CONF_serstopbits
, 2 * (*nextitem
-'0'));
506 conf_set_int(conf
, CONF_serdatabits
, *nextitem
-'0');
510 conf_set_int(conf
, CONF_serparity
, SER_PAR_NONE
);
513 conf_set_int(conf
, CONF_serparity
, SER_PAR_ODD
);
516 conf_set_int(conf
, CONF_serparity
, SER_PAR_EVEN
);
519 conf_set_int(conf
, CONF_serparity
, SER_PAR_MARK
);
522 conf_set_int(conf
, CONF_serparity
, SER_PAR_SPACE
);
526 conf_set_int(conf
, CONF_serflow
, SER_FLOW_NONE
);
529 conf_set_int(conf
, CONF_serflow
, SER_FLOW_XONXOFF
);
532 conf_set_int(conf
, CONF_serflow
, SER_FLOW_RTSCTS
);
535 conf_set_int(conf
, CONF_serflow
, SER_FLOW_DSRDTR
);
539 cmdline_error("Unrecognised suboption \"-sercfg %c\"",
542 } else if (length
== 3 && !strncmp(nextitem
,"1.5",3)) {
543 /* Messy special case */
544 conf_set_int(conf
, CONF_serstopbits
, 3);
546 int serspeed
= atoi(nextitem
);
548 conf_set_int(conf
, CONF_serspeed
, serspeed
);
550 cmdline_error("Unrecognised suboption \"-sercfg %s\"",
554 nextitem
+= length
+ skip
;
557 return ret
; /* unrecognised */
560 void cmdline_run_saved(Conf
*conf
)
563 for (pri
= 0; pri
< NPRIORITIES
; pri
++)
564 for (i
= 0; i
< saves
[pri
].nsaved
; i
++)
565 cmdline_process_param(saves
[pri
].params
[i
].p
,
566 saves
[pri
].params
[i
].value
, 0, conf
);