2 * cmdline.c - command-line parsing shared between many of the
\r
12 * Some command-line parameters need to be saved up until after
\r
13 * we've loaded the saved session which will form the basis of our
\r
14 * eventual running configuration. For this we use the macro
\r
15 * SAVEABLE, which notices if the `need_save' parameter is set and
\r
16 * saves the parameter and value on a list.
\r
18 * We also assign priorities to saved parameters, just to slightly
\r
19 * ameliorate silly ordering problems. For example, if you specify
\r
20 * a saved session to load, it will be loaded _before_ all your
\r
21 * local modifications such as -L are evaluated; and if you specify
\r
22 * a protocol and a port, the protocol is set up first so that the
\r
23 * port can override its choice of port number.
\r
25 * (In fact -load is not saved at all, since in at least Plink the
\r
26 * processing of further command-line options depends on whether or
\r
27 * not the loaded session contained a hostname. So it must be
\r
28 * executed immediately.)
\r
31 #define NPRIORITIES 2
\r
33 struct cmdline_saved_param {
\r
36 struct cmdline_saved_param_set {
\r
37 struct cmdline_saved_param *params;
\r
38 int nsaved, savesize;
\r
42 * C guarantees this structure will be initialised to all zero at
\r
43 * program start, which is exactly what we want.
\r
45 static struct cmdline_saved_param_set saves[NPRIORITIES];
\r
47 static void cmdline_save_param(char *p, char *value, int pri)
\r
49 if (saves[pri].nsaved >= saves[pri].savesize) {
\r
50 saves[pri].savesize = saves[pri].nsaved + 32;
\r
51 saves[pri].params = sresize(saves[pri].params, saves[pri].savesize,
\r
52 struct cmdline_saved_param);
\r
54 saves[pri].params[saves[pri].nsaved].p = p;
\r
55 saves[pri].params[saves[pri].nsaved].value = value;
\r
56 saves[pri].nsaved++;
\r
59 static char *cmdline_password = NULL;
\r
61 void cmdline_cleanup(void)
\r
65 if (cmdline_password) {
\r
66 smemclr(cmdline_password, strlen(cmdline_password));
\r
67 sfree(cmdline_password);
\r
68 cmdline_password = NULL;
\r
71 for (pri = 0; pri < NPRIORITIES; pri++) {
\r
72 sfree(saves[pri].params);
\r
73 saves[pri].params = NULL;
\r
74 saves[pri].savesize = 0;
\r
75 saves[pri].nsaved = 0;
\r
79 #define SAVEABLE(pri) do { \
\r
80 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
\r
84 * Similar interface to get_userpass_input(), except that here a -1
\r
85 * return means that we aren't capable of processing the prompt and
\r
86 * someone else should do it.
\r
88 int cmdline_get_passwd_input(prompts_t *p, unsigned char *in, int inlen) {
\r
90 static int tried_once = 0;
\r
93 * We only handle prompts which don't echo (which we assume to be
\r
94 * passwords), and (currently) we only cope with a password prompt
\r
95 * that comes in a prompt-set on its own.
\r
97 if (!cmdline_password || in || p->n_prompts != 1 || p->prompts[0]->echo) {
\r
102 * If we've tried once, return utter failure (no more passwords left
\r
108 prompt_set_result(p->prompts[0], cmdline_password);
\r
109 smemclr(cmdline_password, strlen(cmdline_password));
\r
110 sfree(cmdline_password);
\r
111 cmdline_password = NULL;
\r
117 * Here we have a flags word which describes the capabilities of
\r
118 * the particular tool on whose behalf we're running. We will
\r
119 * refuse certain command-line options if a particular tool
\r
120 * inherently can't do anything sensible. For example, the file
\r
121 * transfer tools (psftp, pscp) can't do a great deal with protocol
\r
122 * selections (ever tried running scp over telnet?) or with port
\r
123 * forwarding (even if it wasn't a hideously bad idea, they don't
\r
124 * have the select() infrastructure to make them work).
\r
126 int cmdline_tooltype = 0;
\r
128 static int cmdline_check_unavailable(int flag, char *p)
\r
130 if (cmdline_tooltype & flag) {
\r
131 cmdline_error("option \"%s\" not available in this tool", p);
\r
137 #define UNAVAILABLE_IN(flag) do { \
\r
138 if (cmdline_check_unavailable(flag, p)) return ret; \
\r
142 * Process a standard command-line parameter. `p' is the parameter
\r
143 * in question; `value' is the subsequent element of argv, which
\r
144 * may or may not be required as an operand to the parameter.
\r
145 * If `need_save' is 1, arguments which need to be saved as
\r
146 * described at this top of this file are, for later execution;
\r
147 * if 0, they are processed normally. (-1 is a special value used
\r
148 * by pterm to count arguments for a preliminary pass through the
\r
149 * argument list; it causes immediate return with an appropriate
\r
150 * value with no action taken.)
\r
151 * Return value is 2 if both arguments were used; 1 if only p was
\r
152 * used; 0 if the parameter wasn't one we recognised; -2 if it
\r
153 * should have been 2 but value was NULL.
\r
156 #define RETURN(x) do { \
\r
157 if ((x) == 2 && !value) return -2; \
\r
159 if (need_save < 0) return x; \
\r
162 int cmdline_process_param(char *p, char *value, int need_save, Conf *conf)
\r
166 if (!strcmp(p, "-load")) {
\r
168 /* This parameter must be processed immediately rather than being
\r
170 do_defaults(value, conf);
\r
171 loaded_session = TRUE;
\r
172 cmdline_session_name = dupstr(value);
\r
175 if (!strcmp(p, "-ssh")) {
\r
177 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
179 default_protocol = PROT_SSH;
\r
181 conf_set_int(conf, CONF_protocol, default_protocol);
\r
182 conf_set_int(conf, CONF_port, default_port);
\r
185 if (!strcmp(p, "-telnet")) {
\r
187 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
189 default_protocol = PROT_TELNET;
\r
191 conf_set_int(conf, CONF_protocol, default_protocol);
\r
192 conf_set_int(conf, CONF_port, default_port);
\r
195 if (!strcmp(p, "-rlogin")) {
\r
197 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
199 default_protocol = PROT_RLOGIN;
\r
200 default_port = 513;
\r
201 conf_set_int(conf, CONF_protocol, default_protocol);
\r
202 conf_set_int(conf, CONF_port, default_port);
\r
205 if (!strcmp(p, "-raw")) {
\r
207 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
209 default_protocol = PROT_RAW;
\r
210 conf_set_int(conf, CONF_protocol, default_protocol);
\r
212 if (!strcmp(p, "-serial")) {
\r
214 /* Serial is not NONNETWORK in an odd sense of the word */
\r
215 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
217 default_protocol = PROT_SERIAL;
\r
218 conf_set_int(conf, CONF_protocol, default_protocol);
\r
219 /* The host parameter will already be loaded into CONF_host,
\r
220 * so copy it across */
\r
221 conf_set_str(conf, CONF_serline, conf_get_str(conf, CONF_host));
\r
223 if (!strcmp(p, "-v")) {
\r
225 flags |= FLAG_VERBOSE;
\r
227 if (!strcmp(p, "-l")) {
\r
229 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
231 conf_set_str(conf, CONF_username, value);
\r
233 if (!strcmp(p, "-loghost")) {
\r
235 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
237 conf_set_str(conf, CONF_loghost, value);
\r
239 if (!strcmp(p, "-hostkey")) {
\r
242 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
244 dup = dupstr(value);
\r
245 if (!validate_manual_hostkey(dup)) {
\r
246 cmdline_error("'%s' is not a valid format for a manual host "
\r
247 "key specification", value);
\r
251 conf_set_str_str(conf, CONF_ssh_manual_hostkeys, dup, "");
\r
254 if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
\r
255 char type, *q, *qq, *key, *val;
\r
257 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
259 if (strcmp(p, "-D")) {
\r
261 * For -L or -R forwarding types:
\r
263 * We expect _at least_ two colons in this string. The
\r
264 * possible formats are `sourceport:desthost:destport',
\r
265 * or `sourceip:sourceport:desthost:destport' if you're
\r
266 * specifying a particular loopback address. We need to
\r
267 * replace the one between source and dest with a \t;
\r
268 * this means we must find the second-to-last colon in
\r
271 * (This looks like a foolish way of doing it given the
\r
272 * existence of strrchr, but it's more efficient than
\r
273 * two strrchrs - not to mention that the second strrchr
\r
274 * would require us to modify the input string!)
\r
277 type = p[1]; /* 'L' or 'R' */
\r
279 q = qq = host_strchr(value, ':');
\r
281 char *qqq = host_strchr(qq+1, ':');
\r
288 cmdline_error("-%c expects at least two colons in its"
\r
289 " argument", type);
\r
293 key = dupprintf("%c%.*s", type, (int)(q - value), value);
\r
297 * Dynamic port forwardings are entered under the same key
\r
298 * as if they were local (because they occupy the same
\r
299 * port space - a local and a dynamic forwarding on the
\r
300 * same local port are mutually exclusive), with the
\r
301 * special value "D" (which can be distinguished from
\r
302 * anything in the ordinary -L case by containing no
\r
305 key = dupprintf("L%s", value);
\r
308 conf_set_str_str(conf, CONF_portfwd, key, val);
\r
312 if ((!strcmp(p, "-nc"))) {
\r
313 char *host, *portp;
\r
316 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
319 portp = host_strchr(value, ':');
\r
321 cmdline_error("-nc expects argument of form 'host:port'");
\r
325 host = dupprintf("%.*s", (int)(portp - value), value);
\r
326 conf_set_str(conf, CONF_ssh_nc_host, host);
\r
327 conf_set_int(conf, CONF_ssh_nc_port, atoi(portp + 1));
\r
330 if (!strcmp(p, "-m")) {
\r
331 char *filename, *command;
\r
332 int cmdlen, cmdsize;
\r
337 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
342 cmdlen = cmdsize = 0;
\r
344 fp = fopen(filename, "r");
\r
346 cmdline_error("unable to open command file \"%s\"", filename);
\r
354 if (cmdlen >= cmdsize) {
\r
355 cmdsize = cmdlen + 512;
\r
356 command = sresize(command, cmdsize, char);
\r
358 command[cmdlen++] = d;
\r
359 } while (c != EOF);
\r
361 conf_set_str(conf, CONF_remote_cmd, command);
\r
362 conf_set_str(conf, CONF_remote_cmd2, "");
\r
363 conf_set_int(conf, CONF_nopty, TRUE); /* command => no terminal */
\r
366 if ((!strcmp(p, "-P"))||(!strcmp(p, "-p"))) {
\r
368 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
369 SAVEABLE(1); /* lower priority than -ssh,-telnet */
\r
370 conf_set_int(conf, CONF_port, atoi(value));
\r
372 if (!strcmp(p, "-pw")) {
\r
374 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
376 /* We delay evaluating this until after the protocol is decided,
\r
377 * so that we can warn if it's of no use with the selected protocol */
\r
378 if (conf_get_int(conf, CONF_protocol) != PROT_SSH)
\r
379 cmdline_error("the -pw option can only be used with the "
\r
382 cmdline_password = dupstr(value);
\r
383 /* Assuming that `value' is directly from argv, make a good faith
\r
384 * attempt to trample it, to stop it showing up in `ps' output
\r
385 * on Unix-like systems. Not guaranteed, of course. */
\r
386 smemclr(value, strlen(value));
\r
390 if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||
\r
391 !strcmp(p, "-pageant")) {
\r
393 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
395 conf_set_int(conf, CONF_tryagent, TRUE);
\r
397 if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||
\r
398 !strcmp(p, "-nopageant")) {
\r
400 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
402 conf_set_int(conf, CONF_tryagent, FALSE);
\r
405 if (!strcmp(p, "-A")) {
\r
407 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
409 conf_set_int(conf, CONF_agentfwd, 1);
\r
411 if (!strcmp(p, "-a")) {
\r
413 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
415 conf_set_int(conf, CONF_agentfwd, 0);
\r
418 if (!strcmp(p, "-X")) {
\r
420 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
422 conf_set_int(conf, CONF_x11_forward, 1);
\r
424 if (!strcmp(p, "-x")) {
\r
426 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
428 conf_set_int(conf, CONF_x11_forward, 0);
\r
431 if (!strcmp(p, "-t")) {
\r
433 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
434 SAVEABLE(1); /* lower priority than -m */
\r
435 conf_set_int(conf, CONF_nopty, 0);
\r
437 if (!strcmp(p, "-T")) {
\r
439 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
441 conf_set_int(conf, CONF_nopty, 1);
\r
444 if (!strcmp(p, "-N")) {
\r
446 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
448 conf_set_int(conf, CONF_ssh_no_shell, 1);
\r
451 if (!strcmp(p, "-C")) {
\r
453 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
455 conf_set_int(conf, CONF_compression, 1);
\r
458 if (!strcmp(p, "-1")) {
\r
460 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
462 conf_set_int(conf, CONF_sshprot, 0); /* ssh protocol 1 only */
\r
464 if (!strcmp(p, "-2")) {
\r
466 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
468 conf_set_int(conf, CONF_sshprot, 3); /* ssh protocol 2 only */
\r
471 if (!strcmp(p, "-i")) {
\r
474 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
\r
476 fn = filename_from_str(value);
\r
477 conf_set_filename(conf, CONF_keyfile, fn);
\r
481 if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
\r
484 conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV4);
\r
486 if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {
\r
489 conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV6);
\r
491 if (!strcmp(p, "-sercfg")) {
\r
494 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
\r
496 if (conf_get_int(conf, CONF_protocol) != PROT_SERIAL)
\r
497 cmdline_error("the -sercfg option can only be used with the "
\r
498 "serial protocol");
\r
499 /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */
\r
501 while (nextitem[0] != '\0') {
\r
503 char *end = strchr(nextitem, ',');
\r
505 length = strlen(nextitem);
\r
508 length = end - nextitem;
\r
509 nextitem[length] = '\0';
\r
513 switch (*nextitem) {
\r
516 conf_set_int(conf, CONF_serstopbits, 2 * (*nextitem-'0'));
\r
524 conf_set_int(conf, CONF_serdatabits, *nextitem-'0');
\r
528 conf_set_int(conf, CONF_serparity, SER_PAR_NONE);
\r
531 conf_set_int(conf, CONF_serparity, SER_PAR_ODD);
\r
534 conf_set_int(conf, CONF_serparity, SER_PAR_EVEN);
\r
537 conf_set_int(conf, CONF_serparity, SER_PAR_MARK);
\r
540 conf_set_int(conf, CONF_serparity, SER_PAR_SPACE);
\r
544 conf_set_int(conf, CONF_serflow, SER_FLOW_NONE);
\r
547 conf_set_int(conf, CONF_serflow, SER_FLOW_XONXOFF);
\r
550 conf_set_int(conf, CONF_serflow, SER_FLOW_RTSCTS);
\r
553 conf_set_int(conf, CONF_serflow, SER_FLOW_DSRDTR);
\r
557 cmdline_error("Unrecognised suboption \"-sercfg %c\"",
\r
560 } else if (length == 3 && !strncmp(nextitem,"1.5",3)) {
\r
561 /* Messy special case */
\r
562 conf_set_int(conf, CONF_serstopbits, 3);
\r
564 int serspeed = atoi(nextitem);
\r
565 if (serspeed != 0) {
\r
566 conf_set_int(conf, CONF_serspeed, serspeed);
\r
568 cmdline_error("Unrecognised suboption \"-sercfg %s\"",
\r
572 nextitem += length + skip;
\r
575 return ret; /* unrecognised */
\r
578 void cmdline_run_saved(Conf *conf)
\r
581 for (pri = 0; pri < NPRIORITIES; pri++)
\r
582 for (i = 0; i < saves[pri].nsaved; i++)
\r
583 cmdline_process_param(saves[pri].params[i].p,
\r
584 saves[pri].params[i].value, 0, conf);
\r