OSDN Git Service

Add options for disabling weak encryption methods.
[ffftp/ffftp.git] / putty / CMDLINE.C
1 /*\r
2  * cmdline.c - command-line parsing shared between many of the\r
3  * PuTTY applications\r
4  */\r
5 \r
6 #include <stdio.h>\r
7 #include <assert.h>\r
8 #include <stdlib.h>\r
9 #include "putty.h"\r
10 \r
11 /*\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
17  * \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
24  * \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
29  */\r
30 \r
31 #define NPRIORITIES 2\r
32 \r
33 struct cmdline_saved_param {\r
34     char *p, *value;\r
35 };\r
36 struct cmdline_saved_param_set {\r
37     struct cmdline_saved_param *params;\r
38     int nsaved, savesize;\r
39 };\r
40 \r
41 /*\r
42  * C guarantees this structure will be initialised to all zero at\r
43  * program start, which is exactly what we want.\r
44  */\r
45 static struct cmdline_saved_param_set saves[NPRIORITIES];\r
46 \r
47 static void cmdline_save_param(char *p, char *value, int pri)\r
48 {\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
53     }\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
57 }\r
58 \r
59 static char *cmdline_password = NULL;\r
60 \r
61 void cmdline_cleanup(void)\r
62 {\r
63     int pri;\r
64 \r
65     if (cmdline_password) {\r
66         smemclr(cmdline_password, strlen(cmdline_password));\r
67         sfree(cmdline_password);\r
68         cmdline_password = NULL;\r
69     }\r
70     \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
76     }\r
77 }\r
78 \r
79 #define SAVEABLE(pri) do { \\r
80     if (need_save) { cmdline_save_param(p, value, pri); return ret; } \\r
81 } while (0)\r
82 \r
83 /*\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
87  */\r
88 int cmdline_get_passwd_input(prompts_t *p, unsigned char *in, int inlen) {\r
89 \r
90     static int tried_once = 0;\r
91 \r
92     /*\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
96      */\r
97     if (!cmdline_password || in || p->n_prompts != 1 || p->prompts[0]->echo) {\r
98         return -1;\r
99     }\r
100 \r
101     /*\r
102      * If we've tried once, return utter failure (no more passwords left\r
103      * to try).\r
104      */\r
105     if (tried_once)\r
106         return 0;\r
107 \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
112     tried_once = 1;\r
113     return 1;\r
114 }\r
115 \r
116 /*\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
125  */\r
126 int cmdline_tooltype = 0;\r
127 \r
128 static int cmdline_check_unavailable(int flag, char *p)\r
129 {\r
130     if (cmdline_tooltype & flag) {\r
131         cmdline_error("option \"%s\" not available in this tool", p);\r
132         return 1;\r
133     }\r
134     return 0;\r
135 }\r
136 \r
137 #define UNAVAILABLE_IN(flag) do { \\r
138     if (cmdline_check_unavailable(flag, p)) return ret; \\r
139 } while (0)\r
140 \r
141 /*\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
154  */\r
155 \r
156 #define RETURN(x) do { \\r
157     if ((x) == 2 && !value) return -2; \\r
158     ret = x; \\r
159     if (need_save < 0) return x; \\r
160 } while (0)\r
161 \r
162 int cmdline_process_param(char *p, char *value, int need_save, Conf *conf)\r
163 {\r
164     int ret = 0;\r
165 \r
166     if (!strcmp(p, "-load")) {\r
167         RETURN(2);\r
168         /* This parameter must be processed immediately rather than being\r
169          * saved. */\r
170         do_defaults(value, conf);\r
171         loaded_session = TRUE;\r
172         cmdline_session_name = dupstr(value);\r
173         return 2;\r
174     }\r
175     if (!strcmp(p, "-ssh")) {\r
176         RETURN(1);\r
177         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
178         SAVEABLE(0);\r
179         default_protocol = PROT_SSH;\r
180         default_port = 22;\r
181         conf_set_int(conf, CONF_protocol, default_protocol);\r
182         conf_set_int(conf, CONF_port, default_port);\r
183         return 1;\r
184     }\r
185     if (!strcmp(p, "-telnet")) {\r
186         RETURN(1);\r
187         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
188         SAVEABLE(0);\r
189         default_protocol = PROT_TELNET;\r
190         default_port = 23;\r
191         conf_set_int(conf, CONF_protocol, default_protocol);\r
192         conf_set_int(conf, CONF_port, default_port);\r
193         return 1;\r
194     }\r
195     if (!strcmp(p, "-rlogin")) {\r
196         RETURN(1);\r
197         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
198         SAVEABLE(0);\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
203         return 1;\r
204     }\r
205     if (!strcmp(p, "-raw")) {\r
206         RETURN(1);\r
207         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
208         SAVEABLE(0);\r
209         default_protocol = PROT_RAW;\r
210         conf_set_int(conf, CONF_protocol, default_protocol);\r
211     }\r
212     if (!strcmp(p, "-serial")) {\r
213         RETURN(1);\r
214         /* Serial is not NONNETWORK in an odd sense of the word */\r
215         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
216         SAVEABLE(0);\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
222     }\r
223     if (!strcmp(p, "-v")) {\r
224         RETURN(1);\r
225         flags |= FLAG_VERBOSE;\r
226     }\r
227     if (!strcmp(p, "-l")) {\r
228         RETURN(2);\r
229         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
230         SAVEABLE(0);\r
231         conf_set_str(conf, CONF_username, value);\r
232     }\r
233     if (!strcmp(p, "-loghost")) {\r
234         RETURN(2);\r
235         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
236         SAVEABLE(0);\r
237         conf_set_str(conf, CONF_loghost, value);\r
238     }\r
239     if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {\r
240         char type, *q, *qq, *key, *val;\r
241         RETURN(2);\r
242         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
243         SAVEABLE(0);\r
244         if (strcmp(p, "-D")) {\r
245             /*\r
246              * For -L or -R forwarding types:\r
247              *\r
248              * We expect _at least_ two colons in this string. The\r
249              * possible formats are `sourceport:desthost:destport',\r
250              * or `sourceip:sourceport:desthost:destport' if you're\r
251              * specifying a particular loopback address. We need to\r
252              * replace the one between source and dest with a \t;\r
253              * this means we must find the second-to-last colon in\r
254              * the string.\r
255              *\r
256              * (This looks like a foolish way of doing it given the\r
257              * existence of strrchr, but it's more efficient than\r
258              * two strrchrs - not to mention that the second strrchr\r
259              * would require us to modify the input string!)\r
260              */\r
261 \r
262             type = p[1];               /* 'L' or 'R' */\r
263 \r
264             q = qq = strchr(value, ':');\r
265             while (qq) {\r
266                 char *qqq = strchr(qq+1, ':');\r
267                 if (qqq)\r
268                     q = qq;\r
269                 qq = qqq;\r
270             }\r
271 \r
272             if (!q) {\r
273                 cmdline_error("-%c expects at least two colons in its"\r
274                               " argument", type);\r
275                 return ret;\r
276             }\r
277 \r
278             key = dupprintf("%c%.*s", type, q - value, value);\r
279             val = dupstr(q+1);\r
280         } else {\r
281             /*\r
282              * Dynamic port forwardings are entered under the same key\r
283              * as if they were local (because they occupy the same\r
284              * port space - a local and a dynamic forwarding on the\r
285              * same local port are mutually exclusive), with the\r
286              * special value "D" (which can be distinguished from\r
287              * anything in the ordinary -L case by containing no\r
288              * colon).\r
289              */\r
290             key = dupprintf("L%s", value);\r
291             val = dupstr("D");\r
292         }\r
293         conf_set_str_str(conf, CONF_portfwd, key, val);\r
294         sfree(key);\r
295         sfree(val);\r
296     }\r
297     if ((!strcmp(p, "-nc"))) {\r
298         char *host, *portp;\r
299 \r
300         RETURN(2);\r
301         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
302         SAVEABLE(0);\r
303 \r
304         portp = strchr(value, ':');\r
305         if (!portp) {\r
306             cmdline_error("-nc expects argument of form 'host:port'");\r
307             return ret;\r
308         }\r
309 \r
310         host = dupprintf("%.*s", portp - value, value);\r
311         conf_set_str(conf, CONF_ssh_nc_host, host);\r
312         conf_set_int(conf, CONF_ssh_nc_port, atoi(portp + 1));\r
313         sfree(host);\r
314     }\r
315     if (!strcmp(p, "-m")) {\r
316         char *filename, *command;\r
317         int cmdlen, cmdsize;\r
318         FILE *fp;\r
319         int c, d;\r
320 \r
321         RETURN(2);\r
322         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
323         SAVEABLE(0);\r
324 \r
325         filename = value;\r
326 \r
327         cmdlen = cmdsize = 0;\r
328         command = NULL;\r
329         fp = fopen(filename, "r");\r
330         if (!fp) {\r
331             cmdline_error("unable to open command file \"%s\"", filename);\r
332             return ret;\r
333         }\r
334         do {\r
335             c = fgetc(fp);\r
336             d = c;\r
337             if (c == EOF)\r
338                 d = 0;\r
339             if (cmdlen >= cmdsize) {\r
340                 cmdsize = cmdlen + 512;\r
341                 command = sresize(command, cmdsize, char);\r
342             }\r
343             command[cmdlen++] = d;\r
344         } while (c != EOF);\r
345         fclose(fp);\r
346         conf_set_str(conf, CONF_remote_cmd, command);\r
347         conf_set_str(conf, CONF_remote_cmd2, "");\r
348         conf_set_int(conf, CONF_nopty, TRUE);   /* command => no terminal */\r
349         sfree(command);\r
350     }\r
351     if (!strcmp(p, "-P")) {\r
352         RETURN(2);\r
353         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
354         SAVEABLE(1);                   /* lower priority than -ssh,-telnet */\r
355         conf_set_int(conf, CONF_port, atoi(value));\r
356     }\r
357     if (!strcmp(p, "-pw")) {\r
358         RETURN(2);\r
359         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
360         SAVEABLE(1);\r
361         /* We delay evaluating this until after the protocol is decided,\r
362          * so that we can warn if it's of no use with the selected protocol */\r
363         if (conf_get_int(conf, CONF_protocol) != PROT_SSH)\r
364             cmdline_error("the -pw option can only be used with the "\r
365                           "SSH protocol");\r
366         else {\r
367             cmdline_password = dupstr(value);\r
368             /* Assuming that `value' is directly from argv, make a good faith\r
369              * attempt to trample it, to stop it showing up in `ps' output\r
370              * on Unix-like systems. Not guaranteed, of course. */\r
371             smemclr(value, strlen(value));\r
372         }\r
373     }\r
374 \r
375     if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||\r
376         !strcmp(p, "-pageant")) {\r
377         RETURN(1);\r
378         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
379         SAVEABLE(0);\r
380         conf_set_int(conf, CONF_tryagent, TRUE);\r
381     }\r
382     if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||\r
383         !strcmp(p, "-nopageant")) {\r
384         RETURN(1);\r
385         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
386         SAVEABLE(0);\r
387         conf_set_int(conf, CONF_tryagent, FALSE);\r
388     }\r
389 \r
390     if (!strcmp(p, "-A")) {\r
391         RETURN(1);\r
392         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
393         SAVEABLE(0);\r
394         conf_set_int(conf, CONF_agentfwd, 1);\r
395     }\r
396     if (!strcmp(p, "-a")) {\r
397         RETURN(1);\r
398         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
399         SAVEABLE(0);\r
400         conf_set_int(conf, CONF_agentfwd, 0);\r
401     }\r
402 \r
403     if (!strcmp(p, "-X")) {\r
404         RETURN(1);\r
405         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
406         SAVEABLE(0);\r
407         conf_set_int(conf, CONF_x11_forward, 1);\r
408     }\r
409     if (!strcmp(p, "-x")) {\r
410         RETURN(1);\r
411         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
412         SAVEABLE(0);\r
413         conf_set_int(conf, CONF_x11_forward, 0);\r
414     }\r
415 \r
416     if (!strcmp(p, "-t")) {\r
417         RETURN(1);\r
418         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
419         SAVEABLE(1);    /* lower priority than -m */\r
420         conf_set_int(conf, CONF_nopty, 0);\r
421     }\r
422     if (!strcmp(p, "-T")) {\r
423         RETURN(1);\r
424         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
425         SAVEABLE(1);\r
426         conf_set_int(conf, CONF_nopty, 1);\r
427     }\r
428 \r
429     if (!strcmp(p, "-N")) {\r
430         RETURN(1);\r
431         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
432         SAVEABLE(0);\r
433         conf_set_int(conf, CONF_ssh_no_shell, 1);\r
434     }\r
435 \r
436     if (!strcmp(p, "-C")) {\r
437         RETURN(1);\r
438         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
439         SAVEABLE(0);\r
440         conf_set_int(conf, CONF_compression, 1);\r
441     }\r
442 \r
443     if (!strcmp(p, "-1")) {\r
444         RETURN(1);\r
445         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
446         SAVEABLE(0);\r
447         conf_set_int(conf, CONF_sshprot, 0);   /* ssh protocol 1 only */\r
448     }\r
449     if (!strcmp(p, "-2")) {\r
450         RETURN(1);\r
451         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
452         SAVEABLE(0);\r
453         conf_set_int(conf, CONF_sshprot, 3);   /* ssh protocol 2 only */\r
454     }\r
455 \r
456     if (!strcmp(p, "-i")) {\r
457         Filename *fn;\r
458         RETURN(2);\r
459         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
460         SAVEABLE(0);\r
461         fn = filename_from_str(value);\r
462         conf_set_filename(conf, CONF_keyfile, fn);\r
463         filename_free(fn);\r
464     }\r
465 \r
466     if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {\r
467         RETURN(1);\r
468         SAVEABLE(1);\r
469         conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV4);\r
470     }\r
471     if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {\r
472         RETURN(1);\r
473         SAVEABLE(1);\r
474         conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV6);\r
475     }\r
476     if (!strcmp(p, "-sercfg")) {\r
477         char* nextitem;\r
478         RETURN(2);\r
479         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
480         SAVEABLE(1);\r
481         if (conf_get_int(conf, CONF_protocol) != PROT_SERIAL)\r
482             cmdline_error("the -sercfg option can only be used with the "\r
483                           "serial protocol");\r
484         /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */\r
485         nextitem = value;\r
486         while (nextitem[0] != '\0') {\r
487             int length, skip;\r
488             char *end = strchr(nextitem, ',');\r
489             if (!end) {\r
490                 length = strlen(nextitem);\r
491                 skip = 0;\r
492             } else {\r
493                 length = end - nextitem;\r
494                 nextitem[length] = '\0';\r
495                 skip = 1;\r
496             }\r
497             if (length == 1) {\r
498                 switch (*nextitem) {\r
499                   case '1':\r
500                   case '2':\r
501                     conf_set_int(conf, CONF_serstopbits, 2 * (*nextitem-'0'));\r
502                     break;\r
503 \r
504                   case '5':\r
505                   case '6':\r
506                   case '7':\r
507                   case '8':\r
508                   case '9':\r
509                     conf_set_int(conf, CONF_serdatabits, *nextitem-'0');\r
510                     break;\r
511 \r
512                   case 'n':\r
513                     conf_set_int(conf, CONF_serparity, SER_PAR_NONE);\r
514                     break;\r
515                   case 'o':\r
516                     conf_set_int(conf, CONF_serparity, SER_PAR_ODD);\r
517                     break;\r
518                   case 'e':\r
519                     conf_set_int(conf, CONF_serparity, SER_PAR_EVEN);\r
520                     break;\r
521                   case 'm':\r
522                     conf_set_int(conf, CONF_serparity, SER_PAR_MARK);\r
523                     break;\r
524                   case 's':\r
525                     conf_set_int(conf, CONF_serparity, SER_PAR_SPACE);\r
526                     break;\r
527 \r
528                   case 'N':\r
529                     conf_set_int(conf, CONF_serflow, SER_FLOW_NONE);\r
530                     break;\r
531                   case 'X':\r
532                     conf_set_int(conf, CONF_serflow, SER_FLOW_XONXOFF);\r
533                     break;\r
534                   case 'R':\r
535                     conf_set_int(conf, CONF_serflow, SER_FLOW_RTSCTS);\r
536                     break;\r
537                   case 'D':\r
538                     conf_set_int(conf, CONF_serflow, SER_FLOW_DSRDTR);\r
539                     break;\r
540 \r
541                   default:\r
542                     cmdline_error("Unrecognised suboption \"-sercfg %c\"",\r
543                                   *nextitem);\r
544                 }\r
545             } else if (length == 3 && !strncmp(nextitem,"1.5",3)) {\r
546                 /* Messy special case */\r
547                 conf_set_int(conf, CONF_serstopbits, 3);\r
548             } else {\r
549                 int serspeed = atoi(nextitem);\r
550                 if (serspeed != 0) {\r
551                     conf_set_int(conf, CONF_serspeed, serspeed);\r
552                 } else {\r
553                     cmdline_error("Unrecognised suboption \"-sercfg %s\"",\r
554                                   nextitem);\r
555                 }\r
556             }\r
557             nextitem += length + skip;\r
558         }\r
559     }\r
560     return ret;                        /* unrecognised */\r
561 }\r
562 \r
563 void cmdline_run_saved(Conf *conf)\r
564 {\r
565     int pri, i;\r
566     for (pri = 0; pri < NPRIORITIES; pri++)\r
567         for (i = 0; i < saves[pri].nsaved; i++)\r
568             cmdline_process_param(saves[pri].params[i].p,\r
569                                   saves[pri].params[i].value, 0, conf);\r
570 }\r