Since r7265, a user could not launch a PuTTY session to a specific host by
[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 }
195 if (!strcmp(p, "-v")) {
196 RETURN(1);
197 flags |= FLAG_VERBOSE;
198 }
199 if (!strcmp(p, "-l")) {
200 RETURN(2);
46a3419b 201 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 202 SAVEABLE(0);
5555d393 203 strncpy(cfg->username, value, sizeof(cfg->username));
204 cfg->username[sizeof(cfg->username) - 1] = '\0';
dfe3332b 205 }
820ebe3b 206 if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
6ee9b735 207 char *fwd, *ptr, *q, *qq;
820ebe3b 208 int dynamic, i=0;
dfe3332b 209 RETURN(2);
46a3419b 210 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 211 SAVEABLE(0);
820ebe3b 212 dynamic = !strcmp(p, "-D");
dfe3332b 213 fwd = value;
5555d393 214 ptr = cfg->portfwd;
116f2044 215 /* if existing forwards, find end of list */
216 while (*ptr) {
217 while (*ptr)
218 ptr++;
219 ptr++;
dfe3332b 220 }
116f2044 221 i = ptr - cfg->portfwd;
820ebe3b 222 ptr[0] = p[1]; /* insert a 'L', 'R' or 'D' at the start */
116f2044 223 ptr++;
224 if (1 + strlen(fwd) + 2 > sizeof(cfg->portfwd) - i) {
dfe3332b 225 cmdline_error("out of space for port forwardings");
226 return ret;
227 }
116f2044 228 strncpy(ptr, fwd, sizeof(cfg->portfwd) - i - 2);
820ebe3b 229 if (!dynamic) {
230 /*
231 * We expect _at least_ two colons in this string. The
232 * possible formats are `sourceport:desthost:destport',
233 * or `sourceip:sourceport:desthost:destport' if you're
234 * specifying a particular loopback address. We need to
235 * replace the one between source and dest with a \t;
236 * this means we must find the second-to-last colon in
237 * the string.
238 */
239 q = qq = strchr(ptr, ':');
240 while (qq) {
241 char *qqq = strchr(qq+1, ':');
242 if (qqq)
243 q = qq;
244 qq = qqq;
245 }
246 if (q) *q = '\t'; /* replace second-last colon with \t */
6ee9b735 247 }
5555d393 248 cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';
249 cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';
116f2044 250 ptr[strlen(ptr)+1] = '\000'; /* append 2nd '\000' */
dfe3332b 251 }
feb02b4e 252 if ((!strcmp(p, "-nc"))) {
253 char *host, *portp;
254
255 RETURN(2);
256 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
257 SAVEABLE(0);
258
259 host = portp = value;
260 while (*portp && *portp != ':')
261 portp++;
262 if (*portp) {
263 unsigned len = portp - host;
264 if (len >= sizeof(cfg->ssh_nc_host))
265 len = sizeof(cfg->ssh_nc_host) - 1;
266 strncpy(cfg->ssh_nc_host, value, len);
267 cfg->ssh_nc_host[sizeof(cfg->ssh_nc_host) - 1] = '\0';
268 cfg->ssh_nc_port = atoi(portp+1);
269 } else {
270 cmdline_error("-nc expects argument of form 'host:port'");
271 return ret;
272 }
273 }
dfe3332b 274 if (!strcmp(p, "-m")) {
275 char *filename, *command;
276 int cmdlen, cmdsize;
277 FILE *fp;
278 int c, d;
279
280 RETURN(2);
46a3419b 281 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 282 SAVEABLE(0);
dfe3332b 283
284 filename = value;
285
286 cmdlen = cmdsize = 0;
287 command = NULL;
288 fp = fopen(filename, "r");
289 if (!fp) {
290 cmdline_error("unable to open command "
291 "file \"%s\"", filename);
292 return ret;
293 }
294 do {
295 c = fgetc(fp);
296 d = c;
297 if (c == EOF)
298 d = 0;
299 if (cmdlen >= cmdsize) {
300 cmdsize = cmdlen + 512;
3d88e64d 301 command = sresize(command, cmdsize, char);
dfe3332b 302 }
303 command[cmdlen++] = d;
304 } while (c != EOF);
5555d393 305 cfg->remote_cmd_ptr = command;
306 cfg->remote_cmd_ptr2 = NULL;
307 cfg->nopty = TRUE; /* command => no terminal */
dfe3332b 308 }
309 if (!strcmp(p, "-P")) {
310 RETURN(2);
46a3419b 311 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 312 SAVEABLE(1); /* lower priority than -ssh,-telnet */
5555d393 313 cfg->port = atoi(value);
dfe3332b 314 }
315 if (!strcmp(p, "-pw")) {
316 RETURN(2);
46a3419b 317 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
dfe3332b 318 cmdline_password = value;
dfe3332b 319 }
320
973612f5 321 if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||
322 !strcmp(p, "-pageant")) {
323 RETURN(1);
324 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
a3a48ced 325 SAVEABLE(0);
973612f5 326 cfg->tryagent = TRUE;
327 }
328 if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||
329 !strcmp(p, "-nopageant")) {
330 RETURN(1);
331 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
a3a48ced 332 SAVEABLE(0);
973612f5 333 cfg->tryagent = FALSE;
334 }
335
dfe3332b 336 if (!strcmp(p, "-A")) {
337 RETURN(1);
46a3419b 338 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 339 SAVEABLE(0);
5555d393 340 cfg->agentfwd = 1;
dfe3332b 341 }
342 if (!strcmp(p, "-a")) {
343 RETURN(1);
46a3419b 344 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 345 SAVEABLE(0);
5555d393 346 cfg->agentfwd = 0;
dfe3332b 347 }
348
349 if (!strcmp(p, "-X")) {
350 RETURN(1);
46a3419b 351 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 352 SAVEABLE(0);
5555d393 353 cfg->x11_forward = 1;
dfe3332b 354 }
355 if (!strcmp(p, "-x")) {
356 RETURN(1);
46a3419b 357 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 358 SAVEABLE(0);
5555d393 359 cfg->x11_forward = 0;
dfe3332b 360 }
361
362 if (!strcmp(p, "-t")) {
363 RETURN(1);
46a3419b 364 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 365 SAVEABLE(0);
5555d393 366 cfg->nopty = 0;
dfe3332b 367 }
368 if (!strcmp(p, "-T")) {
369 RETURN(1);
46a3419b 370 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 371 SAVEABLE(0);
5555d393 372 cfg->nopty = 1;
dfe3332b 373 }
374
0ed48730 375 if (!strcmp(p, "-N")) {
376 RETURN(1);
377 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
378 SAVEABLE(0);
379 cfg->ssh_no_shell = 1;
380 }
381
dfe3332b 382 if (!strcmp(p, "-C")) {
383 RETURN(1);
46a3419b 384 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 385 SAVEABLE(0);
5555d393 386 cfg->compression = 1;
dfe3332b 387 }
388
389 if (!strcmp(p, "-1")) {
390 RETURN(1);
46a3419b 391 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 392 SAVEABLE(0);
5555d393 393 cfg->sshprot = 0; /* ssh protocol 1 only */
dfe3332b 394 }
395 if (!strcmp(p, "-2")) {
396 RETURN(1);
46a3419b 397 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 398 SAVEABLE(0);
5555d393 399 cfg->sshprot = 3; /* ssh protocol 2 only */
dfe3332b 400 }
401
402 if (!strcmp(p, "-i")) {
403 RETURN(2);
46a3419b 404 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 405 SAVEABLE(0);
9a30e26b 406 cfg->keyfile = filename_from_str(value);
dfe3332b 407 }
408
05581745 409 if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
410 RETURN(1);
411 SAVEABLE(1);
412 cfg->addressfamily = ADDRTYPE_IPV4;
413 }
414 if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {
415 RETURN(1);
416 SAVEABLE(1);
417 cfg->addressfamily = ADDRTYPE_IPV6;
418 }
419
dfe3332b 420 return ret; /* unrecognised */
421}
422
5555d393 423void cmdline_run_saved(Config *cfg)
dfe3332b 424{
425 int pri, i;
426 for (pri = 0; pri < NPRIORITIES; pri++)
427 for (i = 0; i < saves[pri].nsaved; i++)
428 cmdline_process_param(saves[pri].params[i].p,
5555d393 429 saves[pri].params[i].value, 0, cfg);
dfe3332b 430}