OSDN Git Service

Update Linked with static version of CRT/MFC
[tortoisegit/TortoiseGitJp.git] / src / TortoisePlink / 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 void cmdline_cleanup(void)\r
60 {\r
61     int pri;\r
62 \r
63     for (pri = 0; pri < NPRIORITIES; pri++)\r
64         sfree(saves[pri].params);\r
65 }\r
66 \r
67 #define SAVEABLE(pri) do { \\r
68     if (need_save) { cmdline_save_param(p, value, pri); return ret; } \\r
69 } while (0)\r
70 \r
71 static char *cmdline_password = NULL;\r
72 \r
73 /*\r
74  * Similar interface to get_userpass_input(), except that here a -1\r
75  * return means that we aren't capable of processing the prompt and\r
76  * someone else should do it.\r
77  */\r
78 int cmdline_get_passwd_input(prompts_t *p, unsigned char *in, int inlen) {\r
79 \r
80     static int tried_once = 0;\r
81 \r
82     /*\r
83      * We only handle prompts which don't echo (which we assume to be\r
84      * passwords), and (currently) we only cope with a password prompt\r
85      * that comes in a prompt-set on its own.\r
86      */\r
87     if (!cmdline_password || in || p->n_prompts != 1 || p->prompts[0]->echo) {\r
88         return -1;\r
89     }\r
90 \r
91     /*\r
92      * If we've tried once, return utter failure (no more passwords left\r
93      * to try).\r
94      */\r
95     if (tried_once)\r
96         return 0;\r
97 \r
98     strncpy(p->prompts[0]->result, cmdline_password,\r
99             p->prompts[0]->result_len);\r
100     p->prompts[0]->result[p->prompts[0]->result_len-1] = '\0';\r
101     memset(cmdline_password, 0, strlen(cmdline_password));\r
102     tried_once = 1;\r
103     return 1;\r
104 \r
105 }\r
106 \r
107 /*\r
108  * Here we have a flags word which describes the capabilities of\r
109  * the particular tool on whose behalf we're running. We will\r
110  * refuse certain command-line options if a particular tool\r
111  * inherently can't do anything sensible. For example, the file\r
112  * transfer tools (psftp, pscp) can't do a great deal with protocol\r
113  * selections (ever tried running scp over telnet?) or with port\r
114  * forwarding (even if it wasn't a hideously bad idea, they don't\r
115  * have the select() infrastructure to make them work).\r
116  */\r
117 int cmdline_tooltype = 0;\r
118 \r
119 static int cmdline_check_unavailable(int flag, char *p)\r
120 {\r
121     if (cmdline_tooltype & flag) {\r
122         cmdline_error("option \"%s\" not available in this tool", p);\r
123         return 1;\r
124     }\r
125     return 0;\r
126 }\r
127 \r
128 #define UNAVAILABLE_IN(flag) do { \\r
129     if (cmdline_check_unavailable(flag, p)) return ret; \\r
130 } while (0)\r
131 \r
132 /*\r
133  * Process a standard command-line parameter. `p' is the parameter\r
134  * in question; `value' is the subsequent element of argv, which\r
135  * may or may not be required as an operand to the parameter.\r
136  * If `need_save' is 1, arguments which need to be saved as\r
137  * described at this top of this file are, for later execution;\r
138  * if 0, they are processed normally. (-1 is a special value used\r
139  * by pterm to count arguments for a preliminary pass through the\r
140  * argument list; it causes immediate return with an appropriate\r
141  * value with no action taken.)\r
142  * Return value is 2 if both arguments were used; 1 if only p was\r
143  * used; 0 if the parameter wasn't one we recognised; -2 if it\r
144  * should have been 2 but value was NULL.\r
145  */\r
146 \r
147 #define RETURN(x) do { \\r
148     if ((x) == 2 && !value) return -2; \\r
149     ret = x; \\r
150     if (need_save < 0) return x; \\r
151 } while (0)\r
152 \r
153 int cmdline_process_param(char *p, char *value, int need_save, Config *cfg)\r
154 {\r
155     int ret = 0;\r
156 \r
157     if (!strcmp(p, "-load")) {\r
158         RETURN(2);\r
159         /* This parameter must be processed immediately rather than being\r
160          * saved. */\r
161         do_defaults(value, cfg);\r
162         loaded_session = TRUE;\r
163         return 2;\r
164     }\r
165     if (!strcmp(p, "-ssh")) {\r
166         RETURN(1);\r
167         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
168         SAVEABLE(0);\r
169         default_protocol = cfg->protocol = PROT_SSH;\r
170         default_port = cfg->port = 22;\r
171         return 1;\r
172     }\r
173     if (!strcmp(p, "-telnet")) {\r
174         RETURN(1);\r
175         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
176         SAVEABLE(0);\r
177         default_protocol = cfg->protocol = PROT_TELNET;\r
178         default_port = cfg->port = 23;\r
179         return 1;\r
180     }\r
181     if (!strcmp(p, "-rlogin")) {\r
182         RETURN(1);\r
183         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
184         SAVEABLE(0);\r
185         default_protocol = cfg->protocol = PROT_RLOGIN;\r
186         default_port = cfg->port = 513;\r
187         return 1;\r
188     }\r
189     if (!strcmp(p, "-raw")) {\r
190         RETURN(1);\r
191         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
192         SAVEABLE(0);\r
193         default_protocol = cfg->protocol = PROT_RAW;\r
194     }\r
195     if (!strcmp(p, "-v")) {\r
196         RETURN(1);\r
197         flags |= FLAG_VERBOSE;\r
198     }\r
199     if (!strcmp(p, "-l")) {\r
200         RETURN(2);\r
201         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
202         SAVEABLE(0);\r
203         strncpy(cfg->username, value, sizeof(cfg->username));\r
204         cfg->username[sizeof(cfg->username) - 1] = '\0';\r
205     }\r
206     if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {\r
207         char *fwd, *ptr, *q, *qq;\r
208         int dynamic, i=0;\r
209         RETURN(2);\r
210         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
211         SAVEABLE(0);\r
212         dynamic = !strcmp(p, "-D");\r
213         fwd = value;\r
214         ptr = cfg->portfwd;\r
215         /* if existing forwards, find end of list */\r
216         while (*ptr) {\r
217             while (*ptr)\r
218                 ptr++;\r
219             ptr++;\r
220         }\r
221         i = ptr - cfg->portfwd;\r
222         ptr[0] = p[1];  /* insert a 'L', 'R' or 'D' at the start */\r
223         ptr++;\r
224         if (1 + strlen(fwd) + 2 > sizeof(cfg->portfwd) - i) {\r
225             cmdline_error("out of space for port forwardings");\r
226             return ret;\r
227         }\r
228         strncpy(ptr, fwd, sizeof(cfg->portfwd) - i - 2);\r
229         if (!dynamic) {\r
230             /*\r
231              * We expect _at least_ two colons in this string. The\r
232              * possible formats are `sourceport:desthost:destport',\r
233              * or `sourceip:sourceport:desthost:destport' if you're\r
234              * specifying a particular loopback address. We need to\r
235              * replace the one between source and dest with a \t;\r
236              * this means we must find the second-to-last colon in\r
237              * the string.\r
238              */\r
239             q = qq = strchr(ptr, ':');\r
240             while (qq) {\r
241                 char *qqq = strchr(qq+1, ':');\r
242                 if (qqq)\r
243                     q = qq;\r
244                 qq = qqq;\r
245             }\r
246             if (q) *q = '\t';          /* replace second-last colon with \t */\r
247         }\r
248         cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';\r
249         cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';\r
250         ptr[strlen(ptr)+1] = '\000';    /* append 2nd '\000' */\r
251     }\r
252     if ((!strcmp(p, "-nc"))) {\r
253         char *host, *portp;\r
254 \r
255         RETURN(2);\r
256         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
257         SAVEABLE(0);\r
258 \r
259         host = portp = value;\r
260         while (*portp && *portp != ':')\r
261             portp++;\r
262         if (*portp) {\r
263             unsigned len = portp - host;\r
264             if (len >= sizeof(cfg->ssh_nc_host))\r
265                 len = sizeof(cfg->ssh_nc_host) - 1;\r
266             strncpy(cfg->ssh_nc_host, value, len);\r
267             cfg->ssh_nc_host[sizeof(cfg->ssh_nc_host) - 1] = '\0';\r
268             cfg->ssh_nc_port = atoi(portp+1);\r
269         } else {\r
270             cmdline_error("-nc expects argument of form 'host:port'");\r
271             return ret;\r
272         }\r
273     }\r
274     if (!strcmp(p, "-m")) {\r
275         char *filename, *command;\r
276         int cmdlen, cmdsize;\r
277         FILE *fp;\r
278         int c, d;\r
279 \r
280         RETURN(2);\r
281         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
282         SAVEABLE(0);\r
283 \r
284         filename = value;\r
285 \r
286         cmdlen = cmdsize = 0;\r
287         command = NULL;\r
288         fp = fopen(filename, "r");\r
289         if (!fp) {\r
290             cmdline_error("unable to open command "\r
291                           "file \"%s\"", filename);\r
292             return ret;\r
293         }\r
294         do {\r
295             c = fgetc(fp);\r
296             d = c;\r
297             if (c == EOF)\r
298                 d = 0;\r
299             if (cmdlen >= cmdsize) {\r
300                 cmdsize = cmdlen + 512;\r
301                 command = sresize(command, cmdsize, char);\r
302             }\r
303             command[cmdlen++] = d;\r
304         } while (c != EOF);\r
305         cfg->remote_cmd_ptr = command;\r
306         cfg->remote_cmd_ptr2 = NULL;\r
307         cfg->nopty = TRUE;      /* command => no terminal */\r
308     }\r
309     if (!strcmp(p, "-P")) {\r
310         RETURN(2);\r
311         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
312         SAVEABLE(1);                   /* lower priority than -ssh,-telnet */\r
313         cfg->port = atoi(value);\r
314     }\r
315     if (!strcmp(p, "-pw")) {\r
316         RETURN(2);\r
317         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
318         SAVEABLE(1);\r
319         /* We delay evaluating this until after the protocol is decided,\r
320          * so that we can warn if it's of no use with the selected protocol */\r
321         if (cfg->protocol != PROT_SSH)\r
322             cmdline_error("the -pw option can only be used with the "\r
323                           "SSH protocol");\r
324         else {\r
325             cmdline_password = dupstr(value);\r
326             /* Assuming that `value' is directly from argv, make a good faith\r
327              * attempt to trample it, to stop it showing up in `ps' output\r
328              * on Unix-like systems. Not guaranteed, of course. */\r
329             memset(value, 0, strlen(value));\r
330         }\r
331     }\r
332 \r
333     if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||\r
334         !strcmp(p, "-pageant")) {\r
335         RETURN(1);\r
336         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
337         SAVEABLE(0);\r
338         cfg->tryagent = TRUE;\r
339     }\r
340     if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||\r
341         !strcmp(p, "-nopageant")) {\r
342         RETURN(1);\r
343         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
344         SAVEABLE(0);\r
345         cfg->tryagent = FALSE;\r
346     }\r
347 \r
348     if (!strcmp(p, "-A")) {\r
349         RETURN(1);\r
350         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
351         SAVEABLE(0);\r
352         cfg->agentfwd = 1;\r
353     }\r
354     if (!strcmp(p, "-a")) {\r
355         RETURN(1);\r
356         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
357         SAVEABLE(0);\r
358         cfg->agentfwd = 0;\r
359     }\r
360 \r
361     if (!strcmp(p, "-X")) {\r
362         RETURN(1);\r
363         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
364         SAVEABLE(0);\r
365         cfg->x11_forward = 1;\r
366     }\r
367     if (!strcmp(p, "-x")) {\r
368         RETURN(1);\r
369         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
370         SAVEABLE(0);\r
371         cfg->x11_forward = 0;\r
372     }\r
373 \r
374     if (!strcmp(p, "-t")) {\r
375         RETURN(1);\r
376         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
377         SAVEABLE(1);    /* lower priority than -m */\r
378         cfg->nopty = 0;\r
379     }\r
380     if (!strcmp(p, "-T")) {\r
381         RETURN(1);\r
382         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
383         SAVEABLE(1);\r
384         cfg->nopty = 1;\r
385     }\r
386 \r
387     if (!strcmp(p, "-N")) {\r
388         RETURN(1);\r
389         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
390         SAVEABLE(0);\r
391         cfg->ssh_no_shell = 1;\r
392     }\r
393 \r
394     if (!strcmp(p, "-C")) {\r
395         RETURN(1);\r
396         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
397         SAVEABLE(0);\r
398         cfg->compression = 1;\r
399     }\r
400 \r
401     if (!strcmp(p, "-1")) {\r
402         RETURN(1);\r
403         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
404         SAVEABLE(0);\r
405         cfg->sshprot = 0;              /* ssh protocol 1 only */\r
406     }\r
407     if (!strcmp(p, "-2")) {\r
408         RETURN(1);\r
409         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
410         SAVEABLE(0);\r
411         cfg->sshprot = 3;              /* ssh protocol 2 only */\r
412     }\r
413 \r
414     if (!strcmp(p, "-i")) {\r
415         RETURN(2);\r
416         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
417         SAVEABLE(0);\r
418         cfg->keyfile = filename_from_str(value);\r
419     }\r
420 \r
421     if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {\r
422         RETURN(1);\r
423         SAVEABLE(1);\r
424         cfg->addressfamily = ADDRTYPE_IPV4;\r
425     }\r
426     if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {\r
427         RETURN(1);\r
428         SAVEABLE(1);\r
429         cfg->addressfamily = ADDRTYPE_IPV6;\r
430     }\r
431 \r
432     return ret;                        /* unrecognised */\r
433 }\r
434 \r
435 void cmdline_run_saved(Config *cfg)\r
436 {\r
437     int pri, i;\r
438     for (pri = 0; pri < NPRIORITIES; pri++)\r
439         for (i = 0; i < saves[pri].nsaved; i++)\r
440             cmdline_process_param(saves[pri].params[i].p,\r
441                                   saves[pri].params[i].value, 0, cfg);\r
442 }\r