PocketPuTTY website moved.
[u/mdw/putty] / cmdline.c
CommitLineData
dfe3332b 1#include <stdio.h>
2#include <assert.h>
3#include <stdlib.h>
4#include "putty.h"
5
6/*
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.
12 *
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.
c725e24c 19 *
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.)
dfe3332b 24 */
25
c725e24c 26#define NPRIORITIES 2
dfe3332b 27
28struct cmdline_saved_param {
29 char *p, *value;
30};
31struct cmdline_saved_param_set {
32 struct cmdline_saved_param *params;
33 int nsaved, savesize;
34};
35
36/*
37 * C guarantees this structure will be initialised to all zero at
38 * program start, which is exactly what we want.
39 */
40static struct cmdline_saved_param_set saves[NPRIORITIES];
41
42static void cmdline_save_param(char *p, char *value, int pri)
43{
44 if (saves[pri].nsaved >= saves[pri].savesize) {
45 saves[pri].savesize = saves[pri].nsaved + 32;
3d88e64d 46 saves[pri].params = sresize(saves[pri].params, saves[pri].savesize,
47 struct cmdline_saved_param);
dfe3332b 48 }
49 saves[pri].params[saves[pri].nsaved].p = p;
50 saves[pri].params[saves[pri].nsaved].value = value;
51 saves[pri].nsaved++;
52}
53
679539d7 54void cmdline_cleanup(void)
55{
56 int pri;
57
58 for (pri = 0; pri < NPRIORITIES; pri++)
59 sfree(saves[pri].params);
60}
61
dfe3332b 62#define SAVEABLE(pri) do { \
63 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
64} while (0)
65
edd0cb8a 66static char *cmdline_password = NULL;
67
68/*
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.
72 */
73int cmdline_get_passwd_input(prompts_t *p, unsigned char *in, int inlen) {
dfe3332b 74
dfe3332b 75 static int tried_once = 0;
76
edd0cb8a 77 /*
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.
81 */
82 if (!cmdline_password || in || p->n_prompts != 1 || p->prompts[0]->echo) {
83 return -1;
84 }
dfe3332b 85
edd0cb8a 86 /*
87 * If we've tried once, return utter failure (no more passwords left
88 * to try).
89 */
90 if (tried_once)
dfe3332b 91 return 0;
edd0cb8a 92
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));
97 tried_once = 1;
98 return 1;
99
dfe3332b 100}
101
102/*
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).
111 */
112int cmdline_tooltype = 0;
113
114static int cmdline_check_unavailable(int flag, char *p)
115{
116 if (cmdline_tooltype & flag) {
117 cmdline_error("option \"%s\" not available in this tool", p);
118 return 1;
119 }
120 return 0;
121}
122
123#define UNAVAILABLE_IN(flag) do { \
124 if (cmdline_check_unavailable(flag, p)) return ret; \
125} while (0)
126
127/*
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.
b552f500 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.)
dfe3332b 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.
140 */
141
142#define RETURN(x) do { \
143 if ((x) == 2 && !value) return -2; \
144 ret = x; \
46a3419b 145 if (need_save < 0) return x; \
dfe3332b 146} while (0)
147
5555d393 148int cmdline_process_param(char *p, char *value, int need_save, Config *cfg)
dfe3332b 149{
150 int ret = 0;
151
152 if (!strcmp(p, "-load")) {
153 RETURN(2);
c725e24c 154 /* This parameter must be processed immediately rather than being
155 * saved. */
5555d393 156 do_defaults(value, cfg);
18e62ad8 157 loaded_session = TRUE;
dfe3332b 158 return 2;
159 }
160 if (!strcmp(p, "-ssh")) {
161 RETURN(1);
46a3419b 162 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 163 SAVEABLE(0);
5555d393 164 default_protocol = cfg->protocol = PROT_SSH;
165 default_port = cfg->port = 22;
dfe3332b 166 return 1;
167 }
168 if (!strcmp(p, "-telnet")) {
169 RETURN(1);
46a3419b 170 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 171 SAVEABLE(0);
5555d393 172 default_protocol = cfg->protocol = PROT_TELNET;
173 default_port = cfg->port = 23;
dfe3332b 174 return 1;
175 }
176 if (!strcmp(p, "-rlogin")) {
177 RETURN(1);
46a3419b 178 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 179 SAVEABLE(0);
5555d393 180 default_protocol = cfg->protocol = PROT_RLOGIN;
181 default_port = cfg->port = 513;
dfe3332b 182 return 1;
183 }
184 if (!strcmp(p, "-raw")) {
185 RETURN(1);
46a3419b 186 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 187 SAVEABLE(0);
5555d393 188 default_protocol = cfg->protocol = PROT_RAW;
dfe3332b 189 }
190 if (!strcmp(p, "-v")) {
191 RETURN(1);
192 flags |= FLAG_VERBOSE;
193 }
194 if (!strcmp(p, "-l")) {
195 RETURN(2);
46a3419b 196 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 197 SAVEABLE(0);
5555d393 198 strncpy(cfg->username, value, sizeof(cfg->username));
199 cfg->username[sizeof(cfg->username) - 1] = '\0';
dfe3332b 200 }
820ebe3b 201 if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
6ee9b735 202 char *fwd, *ptr, *q, *qq;
820ebe3b 203 int dynamic, i=0;
dfe3332b 204 RETURN(2);
46a3419b 205 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 206 SAVEABLE(0);
820ebe3b 207 dynamic = !strcmp(p, "-D");
dfe3332b 208 fwd = value;
5555d393 209 ptr = cfg->portfwd;
116f2044 210 /* if existing forwards, find end of list */
211 while (*ptr) {
212 while (*ptr)
213 ptr++;
214 ptr++;
dfe3332b 215 }
116f2044 216 i = ptr - cfg->portfwd;
820ebe3b 217 ptr[0] = p[1]; /* insert a 'L', 'R' or 'D' at the start */
116f2044 218 ptr++;
219 if (1 + strlen(fwd) + 2 > sizeof(cfg->portfwd) - i) {
dfe3332b 220 cmdline_error("out of space for port forwardings");
221 return ret;
222 }
116f2044 223 strncpy(ptr, fwd, sizeof(cfg->portfwd) - i - 2);
820ebe3b 224 if (!dynamic) {
225 /*
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
232 * the string.
233 */
234 q = qq = strchr(ptr, ':');
235 while (qq) {
236 char *qqq = strchr(qq+1, ':');
237 if (qqq)
238 q = qq;
239 qq = qqq;
240 }
241 if (q) *q = '\t'; /* replace second-last colon with \t */
6ee9b735 242 }
5555d393 243 cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';
244 cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';
116f2044 245 ptr[strlen(ptr)+1] = '\000'; /* append 2nd '\000' */
dfe3332b 246 }
247 if (!strcmp(p, "-m")) {
248 char *filename, *command;
249 int cmdlen, cmdsize;
250 FILE *fp;
251 int c, d;
252
253 RETURN(2);
46a3419b 254 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 255 SAVEABLE(0);
dfe3332b 256
257 filename = value;
258
259 cmdlen = cmdsize = 0;
260 command = NULL;
261 fp = fopen(filename, "r");
262 if (!fp) {
263 cmdline_error("unable to open command "
264 "file \"%s\"", filename);
265 return ret;
266 }
267 do {
268 c = fgetc(fp);
269 d = c;
270 if (c == EOF)
271 d = 0;
272 if (cmdlen >= cmdsize) {
273 cmdsize = cmdlen + 512;
3d88e64d 274 command = sresize(command, cmdsize, char);
dfe3332b 275 }
276 command[cmdlen++] = d;
277 } while (c != EOF);
5555d393 278 cfg->remote_cmd_ptr = command;
279 cfg->remote_cmd_ptr2 = NULL;
280 cfg->nopty = TRUE; /* command => no terminal */
dfe3332b 281 }
282 if (!strcmp(p, "-P")) {
283 RETURN(2);
46a3419b 284 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 285 SAVEABLE(1); /* lower priority than -ssh,-telnet */
5555d393 286 cfg->port = atoi(value);
dfe3332b 287 }
288 if (!strcmp(p, "-pw")) {
289 RETURN(2);
46a3419b 290 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
dfe3332b 291 cmdline_password = value;
dfe3332b 292 }
293
973612f5 294 if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||
295 !strcmp(p, "-pageant")) {
296 RETURN(1);
297 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
298 cfg->tryagent = TRUE;
299 }
300 if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||
301 !strcmp(p, "-nopageant")) {
302 RETURN(1);
303 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
304 cfg->tryagent = FALSE;
305 }
306
dfe3332b 307 if (!strcmp(p, "-A")) {
308 RETURN(1);
46a3419b 309 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 310 SAVEABLE(0);
5555d393 311 cfg->agentfwd = 1;
dfe3332b 312 }
313 if (!strcmp(p, "-a")) {
314 RETURN(1);
46a3419b 315 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 316 SAVEABLE(0);
5555d393 317 cfg->agentfwd = 0;
dfe3332b 318 }
319
320 if (!strcmp(p, "-X")) {
321 RETURN(1);
46a3419b 322 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 323 SAVEABLE(0);
5555d393 324 cfg->x11_forward = 1;
dfe3332b 325 }
326 if (!strcmp(p, "-x")) {
327 RETURN(1);
46a3419b 328 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 329 SAVEABLE(0);
5555d393 330 cfg->x11_forward = 0;
dfe3332b 331 }
332
333 if (!strcmp(p, "-t")) {
334 RETURN(1);
46a3419b 335 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 336 SAVEABLE(0);
5555d393 337 cfg->nopty = 0;
dfe3332b 338 }
339 if (!strcmp(p, "-T")) {
340 RETURN(1);
46a3419b 341 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
c725e24c 342 SAVEABLE(0);
5555d393 343 cfg->nopty = 1;
dfe3332b 344 }
345
0ed48730 346 if (!strcmp(p, "-N")) {
347 RETURN(1);
348 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
349 SAVEABLE(0);
350 cfg->ssh_no_shell = 1;
351 }
352
dfe3332b 353 if (!strcmp(p, "-C")) {
354 RETURN(1);
46a3419b 355 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 356 SAVEABLE(0);
5555d393 357 cfg->compression = 1;
dfe3332b 358 }
359
360 if (!strcmp(p, "-1")) {
361 RETURN(1);
46a3419b 362 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 363 SAVEABLE(0);
5555d393 364 cfg->sshprot = 0; /* ssh protocol 1 only */
dfe3332b 365 }
366 if (!strcmp(p, "-2")) {
367 RETURN(1);
46a3419b 368 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 369 SAVEABLE(0);
5555d393 370 cfg->sshprot = 3; /* ssh protocol 2 only */
dfe3332b 371 }
372
373 if (!strcmp(p, "-i")) {
374 RETURN(2);
46a3419b 375 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
c725e24c 376 SAVEABLE(0);
9a30e26b 377 cfg->keyfile = filename_from_str(value);
dfe3332b 378 }
379
05581745 380 if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
381 RETURN(1);
382 SAVEABLE(1);
383 cfg->addressfamily = ADDRTYPE_IPV4;
384 }
385 if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {
386 RETURN(1);
387 SAVEABLE(1);
388 cfg->addressfamily = ADDRTYPE_IPV6;
389 }
390
dfe3332b 391 return ret; /* unrecognised */
392}
393
5555d393 394void cmdline_run_saved(Config *cfg)
dfe3332b 395{
396 int pri, i;
397 for (pri = 0; pri < NPRIORITIES; pri++)
398 for (i = 0; i < saves[pri].nsaved; i++)
399 cmdline_process_param(saves[pri].params[i].p,
5555d393 400 saves[pri].params[i].value, 0, cfg);
dfe3332b 401}