OSDN Git Service

Update to 25262d6da000ac0d10f84c5aa75e33166f9fa05b (1.98c).
[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         memset(cmdline_password, 0, 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     strncpy(p->prompts[0]->result, cmdline_password,\r
109             p->prompts[0]->result_len);\r
110     p->prompts[0]->result[p->prompts[0]->result_len-1] = '\0';\r
111     memset(cmdline_password, 0, strlen(cmdline_password));\r
112     sfree(cmdline_password);\r
113     cmdline_password = NULL;\r
114     tried_once = 1;\r
115     return 1;\r
116 \r
117 }\r
118 \r
119 /*\r
120  * Here we have a flags word which describes the capabilities of\r
121  * the particular tool on whose behalf we're running. We will\r
122  * refuse certain command-line options if a particular tool\r
123  * inherently can't do anything sensible. For example, the file\r
124  * transfer tools (psftp, pscp) can't do a great deal with protocol\r
125  * selections (ever tried running scp over telnet?) or with port\r
126  * forwarding (even if it wasn't a hideously bad idea, they don't\r
127  * have the select() infrastructure to make them work).\r
128  */\r
129 int cmdline_tooltype = 0;\r
130 \r
131 static int cmdline_check_unavailable(int flag, char *p)\r
132 {\r
133     if (cmdline_tooltype & flag) {\r
134         cmdline_error("option \"%s\" not available in this tool", p);\r
135         return 1;\r
136     }\r
137     return 0;\r
138 }\r
139 \r
140 #define UNAVAILABLE_IN(flag) do { \\r
141     if (cmdline_check_unavailable(flag, p)) return ret; \\r
142 } while (0)\r
143 \r
144 /*\r
145  * Process a standard command-line parameter. `p' is the parameter\r
146  * in question; `value' is the subsequent element of argv, which\r
147  * may or may not be required as an operand to the parameter.\r
148  * If `need_save' is 1, arguments which need to be saved as\r
149  * described at this top of this file are, for later execution;\r
150  * if 0, they are processed normally. (-1 is a special value used\r
151  * by pterm to count arguments for a preliminary pass through the\r
152  * argument list; it causes immediate return with an appropriate\r
153  * value with no action taken.)\r
154  * Return value is 2 if both arguments were used; 1 if only p was\r
155  * used; 0 if the parameter wasn't one we recognised; -2 if it\r
156  * should have been 2 but value was NULL.\r
157  */\r
158 \r
159 #define RETURN(x) do { \\r
160     if ((x) == 2 && !value) return -2; \\r
161     ret = x; \\r
162     if (need_save < 0) return x; \\r
163 } while (0)\r
164 \r
165 int cmdline_process_param(char *p, char *value, int need_save, Config *cfg)\r
166 {\r
167     int ret = 0;\r
168 \r
169     if (!strcmp(p, "-load")) {\r
170         RETURN(2);\r
171         /* This parameter must be processed immediately rather than being\r
172          * saved. */\r
173         do_defaults(value, cfg);\r
174         loaded_session = TRUE;\r
175         cmdline_session_name = dupstr(value);\r
176         return 2;\r
177     }\r
178     if (!strcmp(p, "-ssh")) {\r
179         RETURN(1);\r
180         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
181         SAVEABLE(0);\r
182         default_protocol = cfg->protocol = PROT_SSH;\r
183         default_port = cfg->port = 22;\r
184         return 1;\r
185     }\r
186     if (!strcmp(p, "-telnet")) {\r
187         RETURN(1);\r
188         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
189         SAVEABLE(0);\r
190         default_protocol = cfg->protocol = PROT_TELNET;\r
191         default_port = cfg->port = 23;\r
192         return 1;\r
193     }\r
194     if (!strcmp(p, "-rlogin")) {\r
195         RETURN(1);\r
196         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
197         SAVEABLE(0);\r
198         default_protocol = cfg->protocol = PROT_RLOGIN;\r
199         default_port = cfg->port = 513;\r
200         return 1;\r
201     }\r
202     if (!strcmp(p, "-raw")) {\r
203         RETURN(1);\r
204         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
205         SAVEABLE(0);\r
206         default_protocol = cfg->protocol = PROT_RAW;\r
207     }\r
208     if (!strcmp(p, "-serial")) {\r
209         RETURN(1);\r
210         /* Serial is not NONNETWORK in an odd sense of the word */\r
211         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
212         SAVEABLE(0);\r
213         default_protocol = cfg->protocol = PROT_SERIAL;\r
214         /* The host parameter will already be loaded into cfg->host, so copy it across */\r
215         strncpy(cfg->serline, cfg->host, sizeof(cfg->serline) - 1);\r
216         cfg->serline[sizeof(cfg->serline) - 1] = '\0';\r
217     }\r
218     if (!strcmp(p, "-v")) {\r
219         RETURN(1);\r
220         flags |= FLAG_VERBOSE;\r
221     }\r
222     if (!strcmp(p, "-l")) {\r
223         RETURN(2);\r
224         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
225         SAVEABLE(0);\r
226         strncpy(cfg->username, value, sizeof(cfg->username));\r
227         cfg->username[sizeof(cfg->username) - 1] = '\0';\r
228     }\r
229     if (!strcmp(p, "-loghost")) {\r
230         RETURN(2);\r
231         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
232         SAVEABLE(0);\r
233         strncpy(cfg->loghost, value, sizeof(cfg->loghost));\r
234         cfg->loghost[sizeof(cfg->loghost) - 1] = '\0';\r
235     }\r
236     if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {\r
237         char *fwd, *ptr, *q, *qq;\r
238         int dynamic, i=0;\r
239         RETURN(2);\r
240         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
241         SAVEABLE(0);\r
242         dynamic = !strcmp(p, "-D");\r
243         fwd = value;\r
244         ptr = cfg->portfwd;\r
245         /* if existing forwards, find end of list */\r
246         while (*ptr) {\r
247             while (*ptr)\r
248                 ptr++;\r
249             ptr++;\r
250         }\r
251         i = ptr - cfg->portfwd;\r
252         ptr[0] = p[1];  /* insert a 'L', 'R' or 'D' at the start */\r
253         ptr++;\r
254         if (1 + strlen(fwd) + 2 > sizeof(cfg->portfwd) - i) {\r
255             cmdline_error("out of space for port forwardings");\r
256             return ret;\r
257         }\r
258         strncpy(ptr, fwd, sizeof(cfg->portfwd) - i - 2);\r
259         if (!dynamic) {\r
260             /*\r
261              * We expect _at least_ two colons in this string. The\r
262              * possible formats are `sourceport:desthost:destport',\r
263              * or `sourceip:sourceport:desthost:destport' if you're\r
264              * specifying a particular loopback address. We need to\r
265              * replace the one between source and dest with a \t;\r
266              * this means we must find the second-to-last colon in\r
267              * the string.\r
268              */\r
269             q = qq = strchr(ptr, ':');\r
270             while (qq) {\r
271                 char *qqq = strchr(qq+1, ':');\r
272                 if (qqq)\r
273                     q = qq;\r
274                 qq = qqq;\r
275             }\r
276             if (q) *q = '\t';          /* replace second-last colon with \t */\r
277         }\r
278         cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';\r
279         cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';\r
280         ptr[strlen(ptr)+1] = '\000';    /* append 2nd '\000' */\r
281     }\r
282     if ((!strcmp(p, "-nc"))) {\r
283         char *host, *portp;\r
284 \r
285         RETURN(2);\r
286         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
287         SAVEABLE(0);\r
288 \r
289         host = portp = value;\r
290         while (*portp && *portp != ':')\r
291             portp++;\r
292         if (*portp) {\r
293             unsigned len = portp - host;\r
294             if (len >= sizeof(cfg->ssh_nc_host))\r
295                 len = sizeof(cfg->ssh_nc_host) - 1;\r
296             memcpy(cfg->ssh_nc_host, value, len);\r
297             cfg->ssh_nc_host[len] = '\0';\r
298             cfg->ssh_nc_port = atoi(portp+1);\r
299         } else {\r
300             cmdline_error("-nc expects argument of form 'host:port'");\r
301             return ret;\r
302         }\r
303     }\r
304     if (!strcmp(p, "-m")) {\r
305         char *filename, *command;\r
306         int cmdlen, cmdsize;\r
307         FILE *fp;\r
308         int c, d;\r
309 \r
310         RETURN(2);\r
311         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
312         SAVEABLE(0);\r
313 \r
314         filename = value;\r
315 \r
316         cmdlen = cmdsize = 0;\r
317         command = NULL;\r
318         fp = fopen(filename, "r");\r
319         if (!fp) {\r
320             cmdline_error("unable to open command "\r
321                           "file \"%s\"", filename);\r
322             return ret;\r
323         }\r
324         do {\r
325             c = fgetc(fp);\r
326             d = c;\r
327             if (c == EOF)\r
328                 d = 0;\r
329             if (cmdlen >= cmdsize) {\r
330                 cmdsize = cmdlen + 512;\r
331                 command = sresize(command, cmdsize, char);\r
332             }\r
333             command[cmdlen++] = d;\r
334         } while (c != EOF);\r
335         cfg->remote_cmd_ptr = command;\r
336         cfg->remote_cmd_ptr2 = NULL;\r
337         cfg->nopty = TRUE;      /* command => no terminal */\r
338         fclose(fp);\r
339     }\r
340     if (!strcmp(p, "-P")) {\r
341         RETURN(2);\r
342         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
343         SAVEABLE(1);                   /* lower priority than -ssh,-telnet */\r
344         cfg->port = atoi(value);\r
345     }\r
346     if (!strcmp(p, "-pw")) {\r
347         RETURN(2);\r
348         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
349         SAVEABLE(1);\r
350         /* We delay evaluating this until after the protocol is decided,\r
351          * so that we can warn if it's of no use with the selected protocol */\r
352         if (cfg->protocol != PROT_SSH)\r
353             cmdline_error("the -pw option can only be used with the "\r
354                           "SSH protocol");\r
355         else {\r
356             cmdline_password = dupstr(value);\r
357             /* Assuming that `value' is directly from argv, make a good faith\r
358              * attempt to trample it, to stop it showing up in `ps' output\r
359              * on Unix-like systems. Not guaranteed, of course. */\r
360             memset(value, 0, strlen(value));\r
361         }\r
362     }\r
363 \r
364     if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||\r
365         !strcmp(p, "-pageant")) {\r
366         RETURN(1);\r
367         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
368         SAVEABLE(0);\r
369         cfg->tryagent = TRUE;\r
370     }\r
371     if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||\r
372         !strcmp(p, "-nopageant")) {\r
373         RETURN(1);\r
374         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
375         SAVEABLE(0);\r
376         cfg->tryagent = FALSE;\r
377     }\r
378 \r
379     if (!strcmp(p, "-A")) {\r
380         RETURN(1);\r
381         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
382         SAVEABLE(0);\r
383         cfg->agentfwd = 1;\r
384     }\r
385     if (!strcmp(p, "-a")) {\r
386         RETURN(1);\r
387         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
388         SAVEABLE(0);\r
389         cfg->agentfwd = 0;\r
390     }\r
391 \r
392     if (!strcmp(p, "-X")) {\r
393         RETURN(1);\r
394         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
395         SAVEABLE(0);\r
396         cfg->x11_forward = 1;\r
397     }\r
398     if (!strcmp(p, "-x")) {\r
399         RETURN(1);\r
400         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
401         SAVEABLE(0);\r
402         cfg->x11_forward = 0;\r
403     }\r
404 \r
405     if (!strcmp(p, "-t")) {\r
406         RETURN(1);\r
407         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
408         SAVEABLE(1);    /* lower priority than -m */\r
409         cfg->nopty = 0;\r
410     }\r
411     if (!strcmp(p, "-T")) {\r
412         RETURN(1);\r
413         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
414         SAVEABLE(1);\r
415         cfg->nopty = 1;\r
416     }\r
417 \r
418     if (!strcmp(p, "-N")) {\r
419         RETURN(1);\r
420         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
421         SAVEABLE(0);\r
422         cfg->ssh_no_shell = 1;\r
423     }\r
424 \r
425     if (!strcmp(p, "-C")) {\r
426         RETURN(1);\r
427         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
428         SAVEABLE(0);\r
429         cfg->compression = 1;\r
430     }\r
431 \r
432     if (!strcmp(p, "-1")) {\r
433         RETURN(1);\r
434         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
435         SAVEABLE(0);\r
436         cfg->sshprot = 0;              /* ssh protocol 1 only */\r
437     }\r
438     if (!strcmp(p, "-2")) {\r
439         RETURN(1);\r
440         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
441         SAVEABLE(0);\r
442         cfg->sshprot = 3;              /* ssh protocol 2 only */\r
443     }\r
444 \r
445     if (!strcmp(p, "-i")) {\r
446         RETURN(2);\r
447         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
448         SAVEABLE(0);\r
449         cfg->keyfile = filename_from_str(value);\r
450     }\r
451 \r
452     if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {\r
453         RETURN(1);\r
454         SAVEABLE(1);\r
455         cfg->addressfamily = ADDRTYPE_IPV4;\r
456     }\r
457     if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {\r
458         RETURN(1);\r
459         SAVEABLE(1);\r
460         cfg->addressfamily = ADDRTYPE_IPV6;\r
461     }\r
462     if (!strcmp(p, "-sercfg")) {\r
463         char* nextitem;\r
464         RETURN(2);\r
465         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
466         SAVEABLE(1);\r
467         if (cfg->protocol != PROT_SERIAL)\r
468             cmdline_error("the -sercfg option can only be used with the "\r
469                           "serial protocol");\r
470         /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */\r
471         nextitem = value;\r
472         while (nextitem[0] != '\0') {\r
473             int length, skip;\r
474             char *end = strchr(nextitem, ',');\r
475             if (!end) {\r
476                 length = strlen(nextitem);\r
477                 skip = 0;\r
478             } else {\r
479                 length = end - nextitem;\r
480                 nextitem[length] = '\0';\r
481                 skip = 1;\r
482             }\r
483             if (length == 1) {\r
484                 switch (*nextitem) {\r
485                   case '1':\r
486                     cfg->serstopbits = 2;\r
487                     break;\r
488                   case '2':\r
489                     cfg->serstopbits = 4;\r
490                     break;\r
491 \r
492                   case '5':\r
493                     cfg->serdatabits = 5;\r
494                     break;\r
495                   case '6':\r
496                     cfg->serdatabits = 6;\r
497                     break;\r
498                   case '7':\r
499                     cfg->serdatabits = 7;\r
500                     break;\r
501                   case '8':\r
502                     cfg->serdatabits = 8;\r
503                     break;\r
504                   case '9':\r
505                     cfg->serdatabits = 9;\r
506                     break;\r
507 \r
508                   case 'n':\r
509                     cfg->serparity = SER_PAR_NONE;\r
510                     break;\r
511                   case 'o':\r
512                     cfg->serparity = SER_PAR_ODD;\r
513                     break;\r
514                   case 'e':\r
515                     cfg->serparity = SER_PAR_EVEN;\r
516                     break;\r
517                   case 'm':\r
518                     cfg->serparity = SER_PAR_MARK;\r
519                     break;\r
520                   case 's':\r
521                     cfg->serparity = SER_PAR_SPACE;\r
522                     break;\r
523 \r
524                   case 'N':\r
525                     cfg->serflow = SER_FLOW_NONE;\r
526                     break;\r
527                   case 'X':\r
528                     cfg->serflow = SER_FLOW_XONXOFF;\r
529                     break;\r
530                   case 'R':\r
531                     cfg->serflow = SER_FLOW_RTSCTS;\r
532                     break;\r
533                   case 'D':\r
534                     cfg->serflow = SER_FLOW_DSRDTR;\r
535                     break;\r
536 \r
537                   default:\r
538                     cmdline_error("Unrecognised suboption \"-sercfg %c\"",\r
539                                   *nextitem);\r
540                 }\r
541             } else if (length == 3 && !strncmp(nextitem,"1.5",3)) {\r
542                 /* Messy special case */\r
543                 cfg->serstopbits = 3;\r
544             } else {\r
545                 int serspeed = atoi(nextitem);\r
546                 if (serspeed != 0) {\r
547                     cfg->serspeed = serspeed;\r
548                 } else {\r
549                     cmdline_error("Unrecognised suboption \"-sercfg %s\"",\r
550                                   nextitem);\r
551                 }\r
552             }\r
553             nextitem += length + skip;\r
554         }\r
555     }\r
556     return ret;                        /* unrecognised */\r
557 }\r
558 \r
559 void cmdline_run_saved(Config *cfg)\r
560 {\r
561     int pri, i;\r
562     for (pri = 0; pri < NPRIORITIES; pri++)\r
563         for (i = 0; i < saves[pri].nsaved; i++)\r
564             cmdline_process_param(saves[pri].params[i].p,\r
565                                   saves[pri].params[i].value, 0, cfg);\r
566 }\r