]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - cmdline.c
first pass
[PuTTY.git] / cmdline.c
1 /*
2  * cmdline.c - command-line parsing shared between many of the
3  * PuTTY applications
4  */
5
6 #include <stdio.h>
7 #include <assert.h>
8 #include <stdlib.h>
9 #include "putty.h"
10
11 /*
12  * Some command-line parameters need to be saved up until after
13  * we've loaded the saved session which will form the basis of our
14  * eventual running configuration. For this we use the macro
15  * SAVEABLE, which notices if the `need_save' parameter is set and
16  * saves the parameter and value on a list.
17  * 
18  * We also assign priorities to saved parameters, just to slightly
19  * ameliorate silly ordering problems. For example, if you specify
20  * a saved session to load, it will be loaded _before_ all your
21  * local modifications such as -L are evaluated; and if you specify
22  * a protocol and a port, the protocol is set up first so that the
23  * port can override its choice of port number.
24  * 
25  * (In fact -load is not saved at all, since in at least Plink the
26  * processing of further command-line options depends on whether or
27  * not the loaded session contained a hostname. So it must be
28  * executed immediately.)
29  */
30
31 #define NPRIORITIES 2
32
33 struct cmdline_saved_param {
34     char *p, *value;
35 };
36 struct cmdline_saved_param_set {
37     struct cmdline_saved_param *params;
38     int nsaved, savesize;
39 };
40
41 /*
42  * C guarantees this structure will be initialised to all zero at
43  * program start, which is exactly what we want.
44  */
45 static struct cmdline_saved_param_set saves[NPRIORITIES];
46
47 static void cmdline_save_param(const char *p, const char *value, int pri)
48 {
49     if (saves[pri].nsaved >= saves[pri].savesize) {
50         saves[pri].savesize = saves[pri].nsaved + 32;
51         saves[pri].params = sresize(saves[pri].params, saves[pri].savesize,
52                                     struct cmdline_saved_param);
53     }
54     saves[pri].params[saves[pri].nsaved].p = dupstr(p);
55     saves[pri].params[saves[pri].nsaved].value = dupstr(value);
56     saves[pri].nsaved++;
57 }
58
59 static char *cmdline_password = NULL;
60
61 void cmdline_cleanup(void)
62 {
63     int pri;
64
65     if (cmdline_password) {
66         smemclr(cmdline_password, strlen(cmdline_password));
67         sfree(cmdline_password);
68         cmdline_password = NULL;
69     }
70     
71     for (pri = 0; pri < NPRIORITIES; pri++) {
72         sfree(saves[pri].params);
73         saves[pri].params = NULL;
74         saves[pri].savesize = 0;
75         saves[pri].nsaved = 0;
76     }
77 }
78
79 #define SAVEABLE(pri) do { \
80     if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
81 } while (0)
82
83 /*
84  * Similar interface to get_userpass_input(), except that here a -1
85  * return means that we aren't capable of processing the prompt and
86  * someone else should do it.
87  */
88 int cmdline_get_passwd_input(prompts_t *p, const unsigned char *in, int inlen)
89 {
90     static int tried_once = 0;
91
92     /*
93      * We only handle prompts which don't echo (which we assume to be
94      * passwords), and (currently) we only cope with a password prompt
95      * that comes in a prompt-set on its own.
96      */
97     if (!cmdline_password || in || p->n_prompts != 1 || p->prompts[0]->echo) {
98         return -1;
99     }
100
101     /*
102      * If we've tried once, return utter failure (no more passwords left
103      * to try).
104      */
105     if (tried_once)
106         return 0;
107
108     prompt_set_result(p->prompts[0], cmdline_password);
109     smemclr(cmdline_password, strlen(cmdline_password));
110     sfree(cmdline_password);
111     cmdline_password = NULL;
112     tried_once = 1;
113     return 1;
114 }
115
116 /*
117  * Here we have a flags word which describes the capabilities of
118  * the particular tool on whose behalf we're running. We will
119  * refuse certain command-line options if a particular tool
120  * inherently can't do anything sensible. For example, the file
121  * transfer tools (psftp, pscp) can't do a great deal with protocol
122  * selections (ever tried running scp over telnet?) or with port
123  * forwarding (even if it wasn't a hideously bad idea, they don't
124  * have the select() infrastructure to make them work).
125  */
126 int cmdline_tooltype = 0;
127
128 static int cmdline_check_unavailable(int flag, const char *p)
129 {
130     if (cmdline_tooltype & flag) {
131         cmdline_error("option \"%s\" not available in this tool", p);
132         return 1;
133     }
134     return 0;
135 }
136
137 #define UNAVAILABLE_IN(flag) do { \
138     if (cmdline_check_unavailable(flag, p)) return ret; \
139 } while (0)
140
141 /*
142  * Process a standard command-line parameter. `p' is the parameter
143  * in question; `value' is the subsequent element of argv, which
144  * may or may not be required as an operand to the parameter.
145  * If `need_save' is 1, arguments which need to be saved as
146  * described at this top of this file are, for later execution;
147  * if 0, they are processed normally. (-1 is a special value used
148  * by pterm to count arguments for a preliminary pass through the
149  * argument list; it causes immediate return with an appropriate
150  * value with no action taken.)
151  * Return value is 2 if both arguments were used; 1 if only p was
152  * used; 0 if the parameter wasn't one we recognised; -2 if it
153  * should have been 2 but value was NULL.
154  */
155
156 #define RETURN(x) do { \
157     if ((x) == 2 && !value) return -2; \
158     ret = x; \
159     if (need_save < 0) return x; \
160 } while (0)
161
162 int cmdline_process_param(const char *p, char *value,
163                           int need_save, Conf *conf)
164 {
165     int ret = 0;
166
167     if (!strcmp(p, "-load")) {
168         RETURN(2);
169         /* This parameter must be processed immediately rather than being
170          * saved. */
171         do_defaults(value, conf);
172         loaded_session = TRUE;
173         cmdline_session_name = dupstr(value);
174         return 2;
175     }
176     if (!strcmp(p, "-ssh")) {
177         RETURN(1);
178         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
179         SAVEABLE(0);
180         default_protocol = PROT_SSH;
181         default_port = 22;
182         conf_set_int(conf, CONF_protocol, default_protocol);
183         conf_set_int(conf, CONF_port, default_port);
184         return 1;
185     }
186     if (!strcmp(p, "-telnet")) {
187         RETURN(1);
188         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
189         SAVEABLE(0);
190         default_protocol = PROT_TELNET;
191         default_port = 23;
192         conf_set_int(conf, CONF_protocol, default_protocol);
193         conf_set_int(conf, CONF_port, default_port);
194         return 1;
195     }
196     if (!strcmp(p, "-rlogin")) {
197         RETURN(1);
198         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
199         SAVEABLE(0);
200         default_protocol = PROT_RLOGIN;
201         default_port = 513;
202         conf_set_int(conf, CONF_protocol, default_protocol);
203         conf_set_int(conf, CONF_port, default_port);
204         return 1;
205     }
206     if (!strcmp(p, "-raw")) {
207         RETURN(1);
208         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
209         SAVEABLE(0);
210         default_protocol = PROT_RAW;
211         conf_set_int(conf, CONF_protocol, default_protocol);
212     }
213     if (!strcmp(p, "-serial")) {
214         RETURN(1);
215         /* Serial is not NONNETWORK in an odd sense of the word */
216         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
217         SAVEABLE(0);
218         default_protocol = PROT_SERIAL;
219         conf_set_int(conf, CONF_protocol, default_protocol);
220         /* The host parameter will already be loaded into CONF_host,
221          * so copy it across */
222         conf_set_str(conf, CONF_serline, conf_get_str(conf, CONF_host));
223     }
224     if (!strcmp(p, "-v")) {
225         RETURN(1);
226         flags |= FLAG_VERBOSE;
227     }
228     if (!strcmp(p, "-l")) {
229         RETURN(2);
230         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
231         SAVEABLE(0);
232         conf_set_str(conf, CONF_username, value);
233     }
234     if (!strcmp(p, "-loghost")) {
235         RETURN(2);
236         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
237         SAVEABLE(0);
238         conf_set_str(conf, CONF_loghost, value);
239     }
240     if (!strcmp(p, "-hostkey")) {
241         char *dup;
242         RETURN(2);
243         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
244         SAVEABLE(0);
245         dup = dupstr(value);
246         if (!validate_manual_hostkey(dup)) {
247             cmdline_error("'%s' is not a valid format for a manual host "
248                           "key specification", value);
249             sfree(dup);
250             return ret;
251         }
252         conf_set_str_str(conf, CONF_ssh_manual_hostkeys, dup, "");
253         sfree(dup);
254     }
255     if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
256         char type, *q, *qq, *key, *val;
257         RETURN(2);
258         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
259         SAVEABLE(0);
260         if (strcmp(p, "-D")) {
261             /*
262              * For -L or -R forwarding types:
263              *
264              * We expect _at least_ two colons in this string. The
265              * possible formats are `sourceport:desthost:destport',
266              * or `sourceip:sourceport:desthost:destport' if you're
267              * specifying a particular loopback address. We need to
268              * replace the one between source and dest with a \t;
269              * this means we must find the second-to-last colon in
270              * the string.
271              *
272              * (This looks like a foolish way of doing it given the
273              * existence of strrchr, but it's more efficient than
274              * two strrchrs - not to mention that the second strrchr
275              * would require us to modify the input string!)
276              */
277
278             type = p[1];               /* 'L' or 'R' */
279
280             q = qq = host_strchr(value, ':');
281             while (qq) {
282                 char *qqq = host_strchr(qq+1, ':');
283                 if (qqq)
284                     q = qq;
285                 qq = qqq;
286             }
287
288             if (!q) {
289                 cmdline_error("-%c expects at least two colons in its"
290                               " argument", type);
291                 return ret;
292             }
293
294             key = dupprintf("%c%.*s", type, (int)(q - value), value);
295             val = dupstr(q+1);
296         } else {
297             /*
298              * Dynamic port forwardings are entered under the same key
299              * as if they were local (because they occupy the same
300              * port space - a local and a dynamic forwarding on the
301              * same local port are mutually exclusive), with the
302              * special value "D" (which can be distinguished from
303              * anything in the ordinary -L case by containing no
304              * colon).
305              */
306             key = dupprintf("L%s", value);
307             val = dupstr("D");
308         }
309         conf_set_str_str(conf, CONF_portfwd, key, val);
310         sfree(key);
311         sfree(val);
312     }
313     if ((!strcmp(p, "-nc"))) {
314         char *host, *portp;
315
316         RETURN(2);
317         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
318         SAVEABLE(0);
319
320         portp = host_strchr(value, ':');
321         if (!portp) {
322             cmdline_error("-nc expects argument of form 'host:port'");
323             return ret;
324         }
325
326         host = dupprintf("%.*s", (int)(portp - value), value);
327         conf_set_str(conf, CONF_ssh_nc_host, host);
328         conf_set_int(conf, CONF_ssh_nc_port, atoi(portp + 1));
329         sfree(host);
330     }
331     if (!strcmp(p, "-m")) {
332         const char *filename;
333         char *command;
334         int cmdlen, cmdsize;
335         FILE *fp;
336         int c, d;
337
338         RETURN(2);
339         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
340         SAVEABLE(0);
341
342         filename = value;
343
344         cmdlen = cmdsize = 0;
345         command = NULL;
346         fp = fopen(filename, "r");
347         if (!fp) {
348             cmdline_error("unable to open command file \"%s\"", filename);
349             return ret;
350         }
351         do {
352             c = fgetc(fp);
353             d = c;
354             if (c == EOF)
355                 d = 0;
356             if (cmdlen >= cmdsize) {
357                 cmdsize = cmdlen + 512;
358                 command = sresize(command, cmdsize, char);
359             }
360             command[cmdlen++] = d;
361         } while (c != EOF);
362         fclose(fp);
363         conf_set_str(conf, CONF_remote_cmd, command);
364         conf_set_str(conf, CONF_remote_cmd2, "");
365         conf_set_int(conf, CONF_nopty, TRUE);   /* command => no terminal */
366         sfree(command);
367     }
368     if (!strcmp(p, "-P")) {
369         RETURN(2);
370         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
371         SAVEABLE(1);                   /* lower priority than -ssh,-telnet */
372         conf_set_int(conf, CONF_port, atoi(value));
373     }
374     if (!strcmp(p, "-pw")) {
375         RETURN(2);
376         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
377         SAVEABLE(1);
378         /* We delay evaluating this until after the protocol is decided,
379          * so that we can warn if it's of no use with the selected protocol */
380         if (conf_get_int(conf, CONF_protocol) != PROT_SSH)
381             cmdline_error("the -pw option can only be used with the "
382                           "SSH protocol");
383         else {
384             cmdline_password = dupstr(value);
385             /* Assuming that `value' is directly from argv, make a good faith
386              * attempt to trample it, to stop it showing up in `ps' output
387              * on Unix-like systems. Not guaranteed, of course. */
388             smemclr(value, strlen(value));
389         }
390     }
391
392     if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||
393         !strcmp(p, "-pageant")) {
394         RETURN(1);
395         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
396         SAVEABLE(0);
397         conf_set_int(conf, CONF_tryagent, TRUE);
398     }
399     if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||
400         !strcmp(p, "-nopageant")) {
401         RETURN(1);
402         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
403         SAVEABLE(0);
404         conf_set_int(conf, CONF_tryagent, FALSE);
405     }
406
407     if (!strcmp(p, "-A")) {
408         RETURN(1);
409         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
410         SAVEABLE(0);
411         conf_set_int(conf, CONF_agentfwd, 1);
412     }
413     if (!strcmp(p, "-a")) {
414         RETURN(1);
415         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
416         SAVEABLE(0);
417         conf_set_int(conf, CONF_agentfwd, 0);
418     }
419
420     if (!strcmp(p, "-X")) {
421         RETURN(1);
422         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
423         SAVEABLE(0);
424         conf_set_int(conf, CONF_x11_forward, 1);
425     }
426     if (!strcmp(p, "-x")) {
427         RETURN(1);
428         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
429         SAVEABLE(0);
430         conf_set_int(conf, CONF_x11_forward, 0);
431     }
432
433     if (!strcmp(p, "-t")) {
434         RETURN(1);
435         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
436         SAVEABLE(1);    /* lower priority than -m */
437         conf_set_int(conf, CONF_nopty, 0);
438     }
439     if (!strcmp(p, "-T")) {
440         RETURN(1);
441         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
442         SAVEABLE(1);
443         conf_set_int(conf, CONF_nopty, 1);
444     }
445
446     if (!strcmp(p, "-N")) {
447         RETURN(1);
448         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
449         SAVEABLE(0);
450         conf_set_int(conf, CONF_ssh_no_shell, 1);
451     }
452
453     if (!strcmp(p, "-C")) {
454         RETURN(1);
455         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
456         SAVEABLE(0);
457         conf_set_int(conf, CONF_compression, 1);
458     }
459
460     if (!strcmp(p, "-1")) {
461         RETURN(1);
462         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
463         SAVEABLE(0);
464         conf_set_int(conf, CONF_sshprot, 0);   /* ssh protocol 1 only */
465     }
466     if (!strcmp(p, "-2")) {
467         RETURN(1);
468         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
469         SAVEABLE(0);
470         conf_set_int(conf, CONF_sshprot, 3);   /* ssh protocol 2 only */
471     }
472
473     if (!strcmp(p, "-i")) {
474         Filename *fn;
475         RETURN(2);
476         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
477         SAVEABLE(0);
478         fn = filename_from_str(value);
479         conf_set_filename(conf, CONF_keyfile, fn);
480         filename_free(fn);
481     }
482
483     if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
484         RETURN(1);
485         SAVEABLE(1);
486         conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV4);
487     }
488     if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {
489         RETURN(1);
490         SAVEABLE(1);
491         conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV6);
492     }
493     if (!strcmp(p, "-sercfg")) {
494         char* nextitem;
495         RETURN(2);
496         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
497         SAVEABLE(1);
498         if (conf_get_int(conf, CONF_protocol) != PROT_SERIAL)
499             cmdline_error("the -sercfg option can only be used with the "
500                           "serial protocol");
501         /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */
502         nextitem = value;
503         while (nextitem[0] != '\0') {
504             int length, skip;
505             char *end = strchr(nextitem, ',');
506             if (!end) {
507                 length = strlen(nextitem);
508                 skip = 0;
509             } else {
510                 length = end - nextitem;
511                 nextitem[length] = '\0';
512                 skip = 1;
513             }
514             if (length == 1) {
515                 switch (*nextitem) {
516                   case '1':
517                   case '2':
518                     conf_set_int(conf, CONF_serstopbits, 2 * (*nextitem-'0'));
519                     break;
520
521                   case '5':
522                   case '6':
523                   case '7':
524                   case '8':
525                   case '9':
526                     conf_set_int(conf, CONF_serdatabits, *nextitem-'0');
527                     break;
528
529                   case 'n':
530                     conf_set_int(conf, CONF_serparity, SER_PAR_NONE);
531                     break;
532                   case 'o':
533                     conf_set_int(conf, CONF_serparity, SER_PAR_ODD);
534                     break;
535                   case 'e':
536                     conf_set_int(conf, CONF_serparity, SER_PAR_EVEN);
537                     break;
538                   case 'm':
539                     conf_set_int(conf, CONF_serparity, SER_PAR_MARK);
540                     break;
541                   case 's':
542                     conf_set_int(conf, CONF_serparity, SER_PAR_SPACE);
543                     break;
544
545                   case 'N':
546                     conf_set_int(conf, CONF_serflow, SER_FLOW_NONE);
547                     break;
548                   case 'X':
549                     conf_set_int(conf, CONF_serflow, SER_FLOW_XONXOFF);
550                     break;
551                   case 'R':
552                     conf_set_int(conf, CONF_serflow, SER_FLOW_RTSCTS);
553                     break;
554                   case 'D':
555                     conf_set_int(conf, CONF_serflow, SER_FLOW_DSRDTR);
556                     break;
557
558                   default:
559                     cmdline_error("Unrecognised suboption \"-sercfg %c\"",
560                                   *nextitem);
561                 }
562             } else if (length == 3 && !strncmp(nextitem,"1.5",3)) {
563                 /* Messy special case */
564                 conf_set_int(conf, CONF_serstopbits, 3);
565             } else {
566                 int serspeed = atoi(nextitem);
567                 if (serspeed != 0) {
568                     conf_set_int(conf, CONF_serspeed, serspeed);
569                 } else {
570                     cmdline_error("Unrecognised suboption \"-sercfg %s\"",
571                                   nextitem);
572                 }
573             }
574             nextitem += length + skip;
575         }
576     }
577
578     if (!strcmp(p, "-sessionlog")) {
579         Filename *fn;
580         RETURN(2);
581         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
582         /* but available even in TOOLTYPE_NONNETWORK, cf pterm "-log" */
583         SAVEABLE(0);
584         fn = filename_from_str(value);
585         conf_set_filename(conf, CONF_logfilename, fn);
586         conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
587         filename_free(fn);
588     }
589
590     if (!strcmp(p, "-sshlog") ||
591         !strcmp(p, "-sshrawlog")) {
592         Filename *fn;
593         RETURN(2);
594         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
595         SAVEABLE(0);
596         fn = filename_from_str(value);
597         conf_set_filename(conf, CONF_logfilename, fn);
598         conf_set_int(conf, CONF_logtype,
599                      !strcmp(p, "-sshlog") ? LGTYP_PACKETS :
600                      /* !strcmp(p, "-sshrawlog") ? */ LGTYP_SSHRAW);
601         filename_free(fn);
602     }
603
604     if (!strcmp(p, "-proxycmd")) {
605         RETURN(2);
606         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
607         SAVEABLE(0);
608         conf_set_int(conf, CONF_proxy_type, PROXY_CMD);
609         conf_set_str(conf, CONF_proxy_telnet_command, value);
610     }
611
612 #ifdef _WINDOWS
613     /*
614      * Cross-tool options only available on Windows.
615      */
616     if (!strcmp(p, "-restrict-acl") || !strcmp(p, "-restrict_acl") ||
617         !strcmp(p, "-restrictacl")) {
618         RETURN(1);
619         restrict_process_acl();
620         restricted_acl = TRUE;
621     }
622 #endif
623
624     return ret;                        /* unrecognised */
625 }
626
627 void cmdline_run_saved(Conf *conf)
628 {
629     int pri, i;
630     for (pri = 0; pri < NPRIORITIES; pri++) {
631         for (i = 0; i < saves[pri].nsaved; i++) {
632             cmdline_process_param(saves[pri].params[i].p,
633                                   saves[pri].params[i].value, 0, conf);
634             sfree(saves[pri].params[i].p);
635             sfree(saves[pri].params[i].value);
636         }
637         saves[pri].nsaved = 0;
638     }
639 }