Update libgit2 AutoCRLF patches
[TortoiseGit.git] / src / TortoisePlink / CMDLINE.C
blob307945b8fea0b0066cb6901d5f1d36adb3659909
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
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
31 #define NPRIORITIES 2\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
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
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
59 static char *cmdline_password = NULL;\r
61 void cmdline_cleanup(void)\r
62 {\r
63     int pri;\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
79 #define SAVEABLE(pri) do { \\r
80     if (need_save) { cmdline_save_param(p, value, pri); return ret; } \\r
81 } while (0)\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
90     static int tried_once = 0;\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
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
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
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
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
132         return 1;\r
133     }\r
134     return 0;\r
137 #define UNAVAILABLE_IN(flag) do { \\r
138     if (cmdline_check_unavailable(flag, p)) return ret; \\r
139 } while (0)\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
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
162 int cmdline_process_param(char *p, char *value, int need_save, Conf *conf)\r
164     int ret = 0;\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, "-hostkey")) {\r
240         char *dup;\r
241         RETURN(2);\r
242         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
243         SAVEABLE(0);\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
248             sfree(dup);\r
249             return ret;\r
250         }\r
251         conf_set_str_str(conf, CONF_ssh_manual_hostkeys, dup, "");\r
252         sfree(dup);\r
253     }\r
254     if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {\r
255         char type, *q, *qq, *key, *val;\r
256         RETURN(2);\r
257         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
258         SAVEABLE(0);\r
259         if (strcmp(p, "-D")) {\r
260             /*\r
261              * For -L or -R forwarding types:\r
262              *\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
269              * the string.\r
270              *\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
275              */\r
277             type = p[1];               /* 'L' or 'R' */\r
279             q = qq = host_strchr(value, ':');\r
280             while (qq) {\r
281                 char *qqq = host_strchr(qq+1, ':');\r
282                 if (qqq)\r
283                     q = qq;\r
284                 qq = qqq;\r
285             }\r
287             if (!q) {\r
288                 cmdline_error("-%c expects at least two colons in its"\r
289                               " argument", type);\r
290                 return ret;\r
291             }\r
293             key = dupprintf("%c%.*s", type, (int)(q - value), value);\r
294             val = dupstr(q+1);\r
295         } else {\r
296             /*\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
303              * colon).\r
304              */\r
305             key = dupprintf("L%s", value);\r
306             val = dupstr("D");\r
307         }\r
308         conf_set_str_str(conf, CONF_portfwd, key, val);\r
309         sfree(key);\r
310         sfree(val);\r
311     }\r
312     if ((!strcmp(p, "-nc"))) {\r
313         char *host, *portp;\r
315         RETURN(2);\r
316         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
317         SAVEABLE(0);\r
319         portp = host_strchr(value, ':');\r
320         if (!portp) {\r
321             cmdline_error("-nc expects argument of form 'host:port'");\r
322             return ret;\r
323         }\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
328         sfree(host);\r
329     }\r
330     if (!strcmp(p, "-m")) {\r
331         char *filename, *command;\r
332         int cmdlen, cmdsize;\r
333         FILE *fp;\r
334         int c, d;\r
336         RETURN(2);\r
337         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
338         SAVEABLE(0);\r
340         filename = value;\r
342         cmdlen = cmdsize = 0;\r
343         command = NULL;\r
344         fp = fopen(filename, "r");\r
345         if (!fp) {\r
346             cmdline_error("unable to open command file \"%s\"", filename);\r
347             return ret;\r
348         }\r
349         do {\r
350             c = fgetc(fp);\r
351             d = c;\r
352             if (c == EOF)\r
353                 d = 0;\r
354             if (cmdlen >= cmdsize) {\r
355                 cmdsize = cmdlen + 512;\r
356                 command = sresize(command, cmdsize, char);\r
357             }\r
358             command[cmdlen++] = d;\r
359         } while (c != EOF);\r
360         fclose(fp);\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
364         sfree(command);\r
365     }\r
366     if ((!strcmp(p, "-P"))||(!strcmp(p, "-p"))) {\r
367         RETURN(2);\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
371     }\r
372     if (!strcmp(p, "-pw")) {\r
373         RETURN(2);\r
374         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
375         SAVEABLE(1);\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
380                           "SSH protocol");\r
381         else {\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
387         }\r
388     }\r
390     if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||\r
391         !strcmp(p, "-pageant")) {\r
392         RETURN(1);\r
393         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
394         SAVEABLE(0);\r
395         conf_set_int(conf, CONF_tryagent, TRUE);\r
396     }\r
397     if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||\r
398         !strcmp(p, "-nopageant")) {\r
399         RETURN(1);\r
400         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
401         SAVEABLE(0);\r
402         conf_set_int(conf, CONF_tryagent, FALSE);\r
403     }\r
405     if (!strcmp(p, "-A")) {\r
406         RETURN(1);\r
407         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
408         SAVEABLE(0);\r
409         conf_set_int(conf, CONF_agentfwd, 1);\r
410     }\r
411     if (!strcmp(p, "-a")) {\r
412         RETURN(1);\r
413         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
414         SAVEABLE(0);\r
415         conf_set_int(conf, CONF_agentfwd, 0);\r
416     }\r
418     if (!strcmp(p, "-X")) {\r
419         RETURN(1);\r
420         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
421         SAVEABLE(0);\r
422         conf_set_int(conf, CONF_x11_forward, 1);\r
423     }\r
424     if (!strcmp(p, "-x")) {\r
425         RETURN(1);\r
426         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
427         SAVEABLE(0);\r
428         conf_set_int(conf, CONF_x11_forward, 0);\r
429     }\r
431     if (!strcmp(p, "-t")) {\r
432         RETURN(1);\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
436     }\r
437     if (!strcmp(p, "-T")) {\r
438         RETURN(1);\r
439         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
440         SAVEABLE(1);\r
441         conf_set_int(conf, CONF_nopty, 1);\r
442     }\r
444     if (!strcmp(p, "-N")) {\r
445         RETURN(1);\r
446         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
447         SAVEABLE(0);\r
448         conf_set_int(conf, CONF_ssh_no_shell, 1);\r
449     }\r
451     if (!strcmp(p, "-C")) {\r
452         RETURN(1);\r
453         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
454         SAVEABLE(0);\r
455         conf_set_int(conf, CONF_compression, 1);\r
456     }\r
458     if (!strcmp(p, "-1")) {\r
459         RETURN(1);\r
460         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
461         SAVEABLE(0);\r
462         conf_set_int(conf, CONF_sshprot, 0);   /* ssh protocol 1 only */\r
463     }\r
464     if (!strcmp(p, "-2")) {\r
465         RETURN(1);\r
466         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
467         SAVEABLE(0);\r
468         conf_set_int(conf, CONF_sshprot, 3);   /* ssh protocol 2 only */\r
469     }\r
471     if (!strcmp(p, "-i")) {\r
472         Filename *fn;\r
473         RETURN(2);\r
474         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);\r
475         SAVEABLE(0);\r
476         fn = filename_from_str(value);\r
477         conf_set_filename(conf, CONF_keyfile, fn);\r
478         filename_free(fn);\r
479     }\r
481     if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {\r
482         RETURN(1);\r
483         SAVEABLE(1);\r
484         conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV4);\r
485     }\r
486     if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {\r
487         RETURN(1);\r
488         SAVEABLE(1);\r
489         conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV6);\r
490     }\r
491     if (!strcmp(p, "-sercfg")) {\r
492         char* nextitem;\r
493         RETURN(2);\r
494         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);\r
495         SAVEABLE(1);\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
500         nextitem = value;\r
501         while (nextitem[0] != '\0') {\r
502             int length, skip;\r
503             char *end = strchr(nextitem, ',');\r
504             if (!end) {\r
505                 length = strlen(nextitem);\r
506                 skip = 0;\r
507             } else {\r
508                 length = end - nextitem;\r
509                 nextitem[length] = '\0';\r
510                 skip = 1;\r
511             }\r
512             if (length == 1) {\r
513                 switch (*nextitem) {\r
514                   case '1':\r
515                   case '2':\r
516                     conf_set_int(conf, CONF_serstopbits, 2 * (*nextitem-'0'));\r
517                     break;\r
519                   case '5':\r
520                   case '6':\r
521                   case '7':\r
522                   case '8':\r
523                   case '9':\r
524                     conf_set_int(conf, CONF_serdatabits, *nextitem-'0');\r
525                     break;\r
527                   case 'n':\r
528                     conf_set_int(conf, CONF_serparity, SER_PAR_NONE);\r
529                     break;\r
530                   case 'o':\r
531                     conf_set_int(conf, CONF_serparity, SER_PAR_ODD);\r
532                     break;\r
533                   case 'e':\r
534                     conf_set_int(conf, CONF_serparity, SER_PAR_EVEN);\r
535                     break;\r
536                   case 'm':\r
537                     conf_set_int(conf, CONF_serparity, SER_PAR_MARK);\r
538                     break;\r
539                   case 's':\r
540                     conf_set_int(conf, CONF_serparity, SER_PAR_SPACE);\r
541                     break;\r
543                   case 'N':\r
544                     conf_set_int(conf, CONF_serflow, SER_FLOW_NONE);\r
545                     break;\r
546                   case 'X':\r
547                     conf_set_int(conf, CONF_serflow, SER_FLOW_XONXOFF);\r
548                     break;\r
549                   case 'R':\r
550                     conf_set_int(conf, CONF_serflow, SER_FLOW_RTSCTS);\r
551                     break;\r
552                   case 'D':\r
553                     conf_set_int(conf, CONF_serflow, SER_FLOW_DSRDTR);\r
554                     break;\r
556                   default:\r
557                     cmdline_error("Unrecognised suboption \"-sercfg %c\"",\r
558                                   *nextitem);\r
559                 }\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
563             } else {\r
564                 int serspeed = atoi(nextitem);\r
565                 if (serspeed != 0) {\r
566                     conf_set_int(conf, CONF_serspeed, serspeed);\r
567                 } else {\r
568                     cmdline_error("Unrecognised suboption \"-sercfg %s\"",\r
569                                   nextitem);\r
570                 }\r
571             }\r
572             nextitem += length + skip;\r
573         }\r
574     }\r
575     return ret;                        /* unrecognised */\r
578 void cmdline_run_saved(Conf *conf)\r
580     int pri, i;\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