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 void cmdline_cleanup(void)
63 for (pri = 0; pri < NPRIORITIES; pri++)
64 sfree(saves[pri].params);
67 #define SAVEABLE(pri) do { \
68 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
71 static char *cmdline_password = NULL;
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.
78 int cmdline_get_passwd_input(prompts_t *p, unsigned char *in, int inlen) {
80 static int tried_once = 0;
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.
87 if (!cmdline_password || in || p->n_prompts != 1 || p->prompts[0]->echo) {
92 * If we've tried once, return utter failure (no more passwords left
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));
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).
117 int cmdline_tooltype = 0;
119 static int cmdline_check_unavailable(int flag, char *p)
121 if (cmdline_tooltype & flag) {
122 cmdline_error("option \"%s\" not available in this tool", p);
128 #define UNAVAILABLE_IN(flag) do { \
129 if (cmdline_check_unavailable(flag, p)) return ret; \
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.
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.)
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.
147 #define RETURN(x) do { \
148 if ((x) == 2 && !value) return -2; \
150 if (need_save < 0) return x; \
153 int cmdline_process_param(char *p, char *value, int need_save, Config *cfg)
157 if (!strcmp(p, "-load")) {
159 /* This parameter must be processed immediately rather than being
161 do_defaults(value, cfg);
162 loaded_session = TRUE;
165 if (!strcmp(p, "-ssh")) {
167 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
169 default_protocol = cfg->protocol = PROT_SSH;
170 default_port = cfg->port = 22;
173 if (!strcmp(p, "-telnet")) {
175 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
177 default_protocol = cfg->protocol = PROT_TELNET;
178 default_port = cfg->port = 23;
181 if (!strcmp(p, "-rlogin")) {
183 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
185 default_protocol = cfg->protocol = PROT_RLOGIN;
186 default_port = cfg->port = 513;
189 if (!strcmp(p, "-raw")) {
191 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
193 default_protocol = cfg->protocol = PROT_RAW;
195 if (!strcmp(p, "-serial")) {
197 /* Serial is not NONNETWORK in an odd sense of the word */
198 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
200 default_protocol = cfg->protocol = PROT_SERIAL;
201 /* The host parameter will already be loaded into cfg->host, so copy it across */
202 strncpy(cfg->serline, cfg->host, sizeof(cfg->serline) - 1);
203 cfg->serline[sizeof(cfg->serline) - 1] = '\0';
205 if (!strcmp(p, "-v")) {
207 flags |= FLAG_VERBOSE;
209 if (!strcmp(p, "-l")) {
211 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
213 strncpy(cfg->username, value, sizeof(cfg->username));
214 cfg->username[sizeof(cfg->username) - 1] = '\0';
216 if (!strcmp(p, "-loghost")) {
218 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
220 strncpy(cfg->loghost, value, sizeof(cfg->loghost));
221 cfg->loghost[sizeof(cfg->loghost) - 1] = '\0';
223 if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
224 char *fwd, *ptr, *q, *qq;
227 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
229 dynamic = !strcmp(p, "-D");
232 /* if existing forwards, find end of list */
238 i = ptr - cfg->portfwd;
239 ptr[0] = p[1]; /* insert a 'L', 'R' or 'D' at the start */
241 if (1 + strlen(fwd) + 2 > sizeof(cfg->portfwd) - i) {
242 cmdline_error("out of space for port forwardings");
245 strncpy(ptr, fwd, sizeof(cfg->portfwd) - i - 2);
248 * We expect _at least_ two colons in this string. The
249 * possible formats are `sourceport:desthost:destport',
250 * or `sourceip:sourceport:desthost:destport' if you're
251 * specifying a particular loopback address. We need to
252 * replace the one between source and dest with a \t;
253 * this means we must find the second-to-last colon in
256 q = qq = strchr(ptr, ':');
258 char *qqq = strchr(qq+1, ':');
263 if (q) *q = '\t'; /* replace second-last colon with \t */
265 cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';
266 cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';
267 ptr[strlen(ptr)+1] = '\000'; /* append 2nd '\000' */
269 if ((!strcmp(p, "-nc"))) {
273 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
276 host = portp = value;
277 while (*portp && *portp != ':')
280 unsigned len = portp - host;
281 if (len >= sizeof(cfg->ssh_nc_host))
282 len = sizeof(cfg->ssh_nc_host) - 1;
283 memcpy(cfg->ssh_nc_host, value, len);
284 cfg->ssh_nc_host[len] = '\0';
285 cfg->ssh_nc_port = atoi(portp+1);
287 cmdline_error("-nc expects argument of form 'host:port'");
291 if (!strcmp(p, "-m")) {
292 char *filename, *command;
298 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
303 cmdlen = cmdsize = 0;
305 fp = fopen(filename, "r");
307 cmdline_error("unable to open command "
308 "file \"%s\"", filename);
316 if (cmdlen >= cmdsize) {
317 cmdsize = cmdlen + 512;
318 command = sresize(command, cmdsize, char);
320 command[cmdlen++] = d;
322 cfg->remote_cmd_ptr = command;
323 cfg->remote_cmd_ptr2 = NULL;
324 cfg->nopty = TRUE; /* command => no terminal */
327 if (!strcmp(p, "-P")) {
329 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
330 SAVEABLE(1); /* lower priority than -ssh,-telnet */
331 cfg->port = atoi(value);
333 if (!strcmp(p, "-pw")) {
335 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
337 /* We delay evaluating this until after the protocol is decided,
338 * so that we can warn if it's of no use with the selected protocol */
339 if (cfg->protocol != PROT_SSH)
340 cmdline_error("the -pw option can only be used with the "
343 cmdline_password = dupstr(value);
344 /* Assuming that `value' is directly from argv, make a good faith
345 * attempt to trample it, to stop it showing up in `ps' output
346 * on Unix-like systems. Not guaranteed, of course. */
347 memset(value, 0, strlen(value));
351 if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||
352 !strcmp(p, "-pageant")) {
354 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
356 cfg->tryagent = TRUE;
358 if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||
359 !strcmp(p, "-nopageant")) {
361 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
363 cfg->tryagent = FALSE;
366 if (!strcmp(p, "-A")) {
368 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
372 if (!strcmp(p, "-a")) {
374 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
379 if (!strcmp(p, "-X")) {
381 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
383 cfg->x11_forward = 1;
385 if (!strcmp(p, "-x")) {
387 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
389 cfg->x11_forward = 0;
392 if (!strcmp(p, "-t")) {
394 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
395 SAVEABLE(1); /* lower priority than -m */
398 if (!strcmp(p, "-T")) {
400 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
405 if (!strcmp(p, "-N")) {
407 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
409 cfg->ssh_no_shell = 1;
412 if (!strcmp(p, "-C")) {
414 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
416 cfg->compression = 1;
419 if (!strcmp(p, "-1")) {
421 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
423 cfg->sshprot = 0; /* ssh protocol 1 only */
425 if (!strcmp(p, "-2")) {
427 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
429 cfg->sshprot = 3; /* ssh protocol 2 only */
432 if (!strcmp(p, "-i")) {
434 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
436 cfg->keyfile = filename_from_str(value);
439 if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
442 cfg->addressfamily = ADDRTYPE_IPV4;
444 if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {
447 cfg->addressfamily = ADDRTYPE_IPV6;
449 if (!strcmp(p, "-sercfg")) {
452 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
454 if (cfg->protocol != PROT_SERIAL)
455 cmdline_error("the -sercfg option can only be used with the "
457 /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */
459 while (nextitem[0] != '\0') {
461 char *end = strchr(nextitem, ',');
463 length = strlen(nextitem);
466 length = end - nextitem;
467 nextitem[length] = '\0';
473 cfg->serstopbits = 2;
476 cfg->serstopbits = 4;
480 cfg->serdatabits = 5;
483 cfg->serdatabits = 6;
486 cfg->serdatabits = 7;
489 cfg->serdatabits = 8;
492 cfg->serdatabits = 9;
496 cfg->serparity = SER_PAR_NONE;
499 cfg->serparity = SER_PAR_ODD;
502 cfg->serparity = SER_PAR_EVEN;
505 cfg->serparity = SER_PAR_MARK;
508 cfg->serparity = SER_PAR_SPACE;
512 cfg->serflow = SER_FLOW_NONE;
515 cfg->serflow = SER_FLOW_XONXOFF;
518 cfg->serflow = SER_FLOW_RTSCTS;
521 cfg->serflow = SER_FLOW_DSRDTR;
525 cmdline_error("Unrecognised suboption \"-sercfg %c\"",
528 } else if (length == 3 && !strncmp(nextitem,"1.5",3)) {
529 /* Messy special case */
530 cfg->serstopbits = 3;
532 int serspeed = atoi(nextitem);
534 cfg->serspeed = serspeed;
536 cmdline_error("Unrecognised suboption \"-sercfg %s\"",
540 nextitem += length + skip;
543 return ret; /* unrecognised */
546 void cmdline_run_saved(Config *cfg)
549 for (pri = 0; pri < NPRIORITIES; pri++)
550 for (i = 0; i < saves[pri].nsaved; i++)
551 cmdline_process_param(saves[pri].params[i].p,
552 saves[pri].params[i].value, 0, cfg);