7 * Some command-line parameters need to be saved up until after
8 * we've loaded the saved session which will form the basis of our
9 * eventual running configuration. For this we use the macro
10 * SAVEABLE, which notices if the `need_save' parameter is set and
11 * saves the parameter and value on a list.
13 * We also assign priorities to saved parameters, just to slightly
14 * ameliorate silly ordering problems. For example, if you specify
15 * a saved session to load, it will be loaded _before_ all your
16 * local modifications such as -L are evaluated; and if you specify
17 * a protocol and a port, the protocol is set up first so that the
18 * port can override its choice of port number.
20 * (In fact -load is not saved at all, since in at least Plink the
21 * processing of further command-line options depends on whether or
22 * not the loaded session contained a hostname. So it must be
23 * executed immediately.)
28 struct cmdline_saved_param
{
31 struct cmdline_saved_param_set
{
32 struct cmdline_saved_param
*params
;
37 * C guarantees this structure will be initialised to all zero at
38 * program start, which is exactly what we want.
40 static struct cmdline_saved_param_set saves
[NPRIORITIES
];
42 static void cmdline_save_param(char *p
, char *value
, int pri
)
44 if (saves
[pri
].nsaved
>= saves
[pri
].savesize
) {
45 saves
[pri
].savesize
= saves
[pri
].nsaved
+ 32;
46 saves
[pri
].params
= sresize(saves
[pri
].params
, saves
[pri
].savesize
,
47 struct cmdline_saved_param
);
49 saves
[pri
].params
[saves
[pri
].nsaved
].p
= p
;
50 saves
[pri
].params
[saves
[pri
].nsaved
].value
= value
;
54 void cmdline_cleanup(void)
58 for (pri
= 0; pri
< NPRIORITIES
; pri
++)
59 sfree(saves
[pri
].params
);
62 #define SAVEABLE(pri) do { \
63 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
66 static char *cmdline_password
= NULL
;
69 * Similar interface to get_userpass_input(), except that here a -1
70 * return means that we aren't capable of processing the prompt and
71 * someone else should do it.
73 int cmdline_get_passwd_input(prompts_t
*p
, unsigned char *in
, int inlen
) {
75 static int tried_once
= 0;
78 * We only handle prompts which don't echo (which we assume to be
79 * passwords), and (currently) we only cope with a password prompt
80 * that comes in a prompt-set on its own.
82 if (!cmdline_password
|| in
|| p
->n_prompts
!= 1 || p
->prompts
[0]->echo
) {
87 * If we've tried once, return utter failure (no more passwords left
93 strncpy(p
->prompts
[0]->result
, cmdline_password
,
94 p
->prompts
[0]->result_len
);
95 p
->prompts
[0]->result
[p
->prompts
[0]->result_len
-1] = '\0';
96 memset(cmdline_password
, 0, strlen(cmdline_password
));
103 * Here we have a flags word which describes the capabilities of
104 * the particular tool on whose behalf we're running. We will
105 * refuse certain command-line options if a particular tool
106 * inherently can't do anything sensible. For example, the file
107 * transfer tools (psftp, pscp) can't do a great deal with protocol
108 * selections (ever tried running scp over telnet?) or with port
109 * forwarding (even if it wasn't a hideously bad idea, they don't
110 * have the select() infrastructure to make them work).
112 int cmdline_tooltype
= 0;
114 static int cmdline_check_unavailable(int flag
, char *p
)
116 if (cmdline_tooltype
& flag
) {
117 cmdline_error("option \"%s\" not available in this tool", p
);
123 #define UNAVAILABLE_IN(flag) do { \
124 if (cmdline_check_unavailable(flag, p)) return ret; \
128 * Process a standard command-line parameter. `p' is the parameter
129 * in question; `value' is the subsequent element of argv, which
130 * may or may not be required as an operand to the parameter.
131 * If `need_save' is 1, arguments which need to be saved as
132 * described at this top of this file are, for later execution;
133 * if 0, they are processed normally. (-1 is a special value used
134 * by pterm to count arguments for a preliminary pass through the
135 * argument list; it causes immediate return with an appropriate
136 * value with no action taken.)
137 * Return value is 2 if both arguments were used; 1 if only p was
138 * used; 0 if the parameter wasn't one we recognised; -2 if it
139 * should have been 2 but value was NULL.
142 #define RETURN(x) do { \
143 if ((x) == 2 && !value) return -2; \
145 if (need_save < 0) return x; \
148 int cmdline_process_param(char *p
, char *value
, int need_save
, Config
*cfg
)
152 if (!strcmp(p
, "-load")) {
154 /* This parameter must be processed immediately rather than being
156 do_defaults(value
, cfg
);
157 loaded_session
= TRUE
;
160 if (!strcmp(p
, "-ssh")) {
162 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
164 default_protocol
= cfg
->protocol
= PROT_SSH
;
165 default_port
= cfg
->port
= 22;
168 if (!strcmp(p
, "-telnet")) {
170 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
172 default_protocol
= cfg
->protocol
= PROT_TELNET
;
173 default_port
= cfg
->port
= 23;
176 if (!strcmp(p
, "-rlogin")) {
178 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
180 default_protocol
= cfg
->protocol
= PROT_RLOGIN
;
181 default_port
= cfg
->port
= 513;
184 if (!strcmp(p
, "-raw")) {
186 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
188 default_protocol
= cfg
->protocol
= PROT_RAW
;
190 if (!strcmp(p
, "-v")) {
192 flags
|= FLAG_VERBOSE
;
194 if (!strcmp(p
, "-l")) {
196 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
198 strncpy(cfg
->username
, value
, sizeof(cfg
->username
));
199 cfg
->username
[sizeof(cfg
->username
) - 1] = '\0';
201 if ((!strcmp(p
, "-L") || !strcmp(p
, "-R") || !strcmp(p
, "-D"))) {
202 char *fwd
, *ptr
, *q
, *qq
;
205 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
207 dynamic
= !strcmp(p
, "-D");
210 /* if existing forwards, find end of list */
216 i
= ptr
- cfg
->portfwd
;
217 ptr
[0] = p
[1]; /* insert a 'L', 'R' or 'D' at the start */
219 if (1 + strlen(fwd
) + 2 > sizeof(cfg
->portfwd
) - i
) {
220 cmdline_error("out of space for port forwardings");
223 strncpy(ptr
, fwd
, sizeof(cfg
->portfwd
) - i
- 2);
226 * We expect _at least_ two colons in this string. The
227 * possible formats are `sourceport:desthost:destport',
228 * or `sourceip:sourceport:desthost:destport' if you're
229 * specifying a particular loopback address. We need to
230 * replace the one between source and dest with a \t;
231 * this means we must find the second-to-last colon in
234 q
= qq
= strchr(ptr
, ':');
236 char *qqq
= strchr(qq
+1, ':');
241 if (q
) *q
= '\t'; /* replace second-last colon with \t */
243 cfg
->portfwd
[sizeof(cfg
->portfwd
) - 1] = '\0';
244 cfg
->portfwd
[sizeof(cfg
->portfwd
) - 2] = '\0';
245 ptr
[strlen(ptr
)+1] = '\000'; /* append 2nd '\000' */
247 if (!strcmp(p
, "-m")) {
248 char *filename
, *command
;
254 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
259 cmdlen
= cmdsize
= 0;
261 fp
= fopen(filename
, "r");
263 cmdline_error("unable to open command "
264 "file \"%s\"", filename
);
272 if (cmdlen
>= cmdsize
) {
273 cmdsize
= cmdlen
+ 512;
274 command
= sresize(command
, cmdsize
, char);
276 command
[cmdlen
++] = d
;
278 cfg
->remote_cmd_ptr
= command
;
279 cfg
->remote_cmd_ptr2
= NULL
;
280 cfg
->nopty
= TRUE
; /* command => no terminal */
282 if (!strcmp(p
, "-P")) {
284 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
285 SAVEABLE(1); /* lower priority than -ssh,-telnet */
286 cfg
->port
= atoi(value
);
288 if (!strcmp(p
, "-pw")) {
290 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
291 cmdline_password
= value
;
294 if (!strcmp(p
, "-A")) {
296 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
300 if (!strcmp(p
, "-a")) {
302 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
307 if (!strcmp(p
, "-X")) {
309 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
311 cfg
->x11_forward
= 1;
313 if (!strcmp(p
, "-x")) {
315 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
317 cfg
->x11_forward
= 0;
320 if (!strcmp(p
, "-t")) {
322 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
326 if (!strcmp(p
, "-T")) {
328 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
333 if (!strcmp(p
, "-N")) {
335 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
337 cfg
->ssh_no_shell
= 1;
340 if (!strcmp(p
, "-C")) {
342 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
344 cfg
->compression
= 1;
347 if (!strcmp(p
, "-1")) {
349 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
351 cfg
->sshprot
= 0; /* ssh protocol 1 only */
353 if (!strcmp(p
, "-2")) {
355 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
357 cfg
->sshprot
= 3; /* ssh protocol 2 only */
360 if (!strcmp(p
, "-i")) {
362 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
364 cfg
->keyfile
= filename_from_str(value
);
367 if (!strcmp(p
, "-4") || !strcmp(p
, "-ipv4")) {
370 cfg
->addressfamily
= ADDRTYPE_IPV4
;
372 if (!strcmp(p
, "-6") || !strcmp(p
, "-ipv6")) {
375 cfg
->addressfamily
= ADDRTYPE_IPV6
;
378 return ret
; /* unrecognised */
381 void cmdline_run_saved(Config
*cfg
)
384 for (pri
= 0; pri
< NPRIORITIES
; pri
++)
385 for (i
= 0; i
< saves
[pri
].nsaved
; i
++)
386 cmdline_process_param(saves
[pri
].params
[i
].p
,
387 saves
[pri
].params
[i
].value
, 0, cfg
);