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 char *cmdline_password
= NULL
;
68 static int cmdline_get_line(const char *prompt
, char *str
,
69 int maxlen
, int is_pw
)
71 static int tried_once
= 0;
73 assert(is_pw
&& cmdline_password
);
78 strncpy(str
, cmdline_password
, maxlen
);
79 str
[maxlen
- 1] = '\0';
86 * Here we have a flags word which describes the capabilities of
87 * the particular tool on whose behalf we're running. We will
88 * refuse certain command-line options if a particular tool
89 * inherently can't do anything sensible. For example, the file
90 * transfer tools (psftp, pscp) can't do a great deal with protocol
91 * selections (ever tried running scp over telnet?) or with port
92 * forwarding (even if it wasn't a hideously bad idea, they don't
93 * have the select() infrastructure to make them work).
95 int cmdline_tooltype
= 0;
97 static int cmdline_check_unavailable(int flag
, char *p
)
99 if (cmdline_tooltype
& flag
) {
100 cmdline_error("option \"%s\" not available in this tool", p
);
106 #define UNAVAILABLE_IN(flag) do { \
107 if (cmdline_check_unavailable(flag, p)) return ret; \
111 * Process a standard command-line parameter. `p' is the parameter
112 * in question; `value' is the subsequent element of argv, which
113 * may or may not be required as an operand to the parameter.
114 * If `need_save' is 1, arguments which need to be saved as
115 * described at this top of this file are, for later execution;
116 * if 0, they are processed normally. (-1 is a special value used
117 * by pterm to count arguments for a preliminary pass through the
118 * argument list; it causes immediate return with an appropriate
119 * value with no action taken.)
120 * Return value is 2 if both arguments were used; 1 if only p was
121 * used; 0 if the parameter wasn't one we recognised; -2 if it
122 * should have been 2 but value was NULL.
125 #define RETURN(x) do { \
126 if ((x) == 2 && !value) return -2; \
128 if (need_save < 0) return x; \
131 int cmdline_process_param(char *p
, char *value
, int need_save
, Config
*cfg
)
135 if (!strcmp(p
, "-load")) {
137 /* This parameter must be processed immediately rather than being
139 do_defaults(value
, cfg
);
140 loaded_session
= TRUE
;
143 if (!strcmp(p
, "-ssh")) {
145 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
147 default_protocol
= cfg
->protocol
= PROT_SSH
;
148 default_port
= cfg
->port
= 22;
151 if (!strcmp(p
, "-telnet")) {
153 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
155 default_protocol
= cfg
->protocol
= PROT_TELNET
;
156 default_port
= cfg
->port
= 23;
159 if (!strcmp(p
, "-rlogin")) {
161 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
163 default_protocol
= cfg
->protocol
= PROT_RLOGIN
;
164 default_port
= cfg
->port
= 513;
167 if (!strcmp(p
, "-raw")) {
169 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
171 default_protocol
= cfg
->protocol
= PROT_RAW
;
173 if (!strcmp(p
, "-v")) {
175 flags
|= FLAG_VERBOSE
;
177 if (!strcmp(p
, "-l")) {
179 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
181 strncpy(cfg
->username
, value
, sizeof(cfg
->username
));
182 cfg
->username
[sizeof(cfg
->username
) - 1] = '\0';
184 if ((!strcmp(p
, "-L") || !strcmp(p
, "-R") || !strcmp(p
, "-D"))) {
185 char *fwd
, *ptr
, *q
, *qq
;
188 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
190 dynamic
= !strcmp(p
, "-D");
193 /* if existing forwards, find end of list */
199 i
= ptr
- cfg
->portfwd
;
200 ptr
[0] = p
[1]; /* insert a 'L', 'R' or 'D' at the start */
202 if (1 + strlen(fwd
) + 2 > sizeof(cfg
->portfwd
) - i
) {
203 cmdline_error("out of space for port forwardings");
206 strncpy(ptr
, fwd
, sizeof(cfg
->portfwd
) - i
- 2);
209 * We expect _at least_ two colons in this string. The
210 * possible formats are `sourceport:desthost:destport',
211 * or `sourceip:sourceport:desthost:destport' if you're
212 * specifying a particular loopback address. We need to
213 * replace the one between source and dest with a \t;
214 * this means we must find the second-to-last colon in
217 q
= qq
= strchr(ptr
, ':');
219 char *qqq
= strchr(qq
+1, ':');
224 if (q
) *q
= '\t'; /* replace second-last colon with \t */
226 cfg
->portfwd
[sizeof(cfg
->portfwd
) - 1] = '\0';
227 cfg
->portfwd
[sizeof(cfg
->portfwd
) - 2] = '\0';
228 ptr
[strlen(ptr
)+1] = '\000'; /* append 2nd '\000' */
230 if (!strcmp(p
, "-m")) {
231 char *filename
, *command
;
237 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
242 cmdlen
= cmdsize
= 0;
244 fp
= fopen(filename
, "r");
246 cmdline_error("unable to open command "
247 "file \"%s\"", filename
);
255 if (cmdlen
>= cmdsize
) {
256 cmdsize
= cmdlen
+ 512;
257 command
= sresize(command
, cmdsize
, char);
259 command
[cmdlen
++] = d
;
261 cfg
->remote_cmd_ptr
= command
;
262 cfg
->remote_cmd_ptr2
= NULL
;
263 cfg
->nopty
= TRUE
; /* command => no terminal */
265 if (!strcmp(p
, "-P")) {
267 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
268 SAVEABLE(1); /* lower priority than -ssh,-telnet */
269 cfg
->port
= atoi(value
);
271 if (!strcmp(p
, "-pw")) {
273 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
274 cmdline_password
= value
;
275 ssh_get_line
= cmdline_get_line
;
276 ssh_getline_pw_only
= TRUE
;
279 if (!strcmp(p
, "-A")) {
281 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
285 if (!strcmp(p
, "-a")) {
287 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
292 if (!strcmp(p
, "-X")) {
294 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
296 cfg
->x11_forward
= 1;
298 if (!strcmp(p
, "-x")) {
300 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
302 cfg
->x11_forward
= 0;
305 if (!strcmp(p
, "-t")) {
307 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
311 if (!strcmp(p
, "-T")) {
313 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
318 if (!strcmp(p
, "-N")) {
320 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER
| TOOLTYPE_NONNETWORK
);
322 cfg
->ssh_no_shell
= 1;
325 if (!strcmp(p
, "-C")) {
327 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
329 cfg
->compression
= 1;
332 if (!strcmp(p
, "-1")) {
334 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
336 cfg
->sshprot
= 0; /* ssh protocol 1 only */
338 if (!strcmp(p
, "-2")) {
340 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
342 cfg
->sshprot
= 3; /* ssh protocol 2 only */
345 if (!strcmp(p
, "-i")) {
347 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK
);
349 cfg
->keyfile
= filename_from_str(value
);
352 if (!strcmp(p
, "-4") || !strcmp(p
, "-ipv4")) {
355 cfg
->addressfamily
= ADDRTYPE_IPV4
;
357 if (!strcmp(p
, "-6") || !strcmp(p
, "-ipv6")) {
360 cfg
->addressfamily
= ADDRTYPE_IPV6
;
363 return ret
; /* unrecognised */
366 void cmdline_run_saved(Config
*cfg
)
369 for (pri
= 0; pri
< NPRIORITIES
; pri
++)
370 for (i
= 0; i
< saves
[pri
].nsaved
; i
++)
371 cmdline_process_param(saves
[pri
].params
[i
].p
,
372 saves
[pri
].params
[i
].value
, 0, cfg
);