]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - cmdline.c
Command-line options to log sessions.
[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(char *p, 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 = p;
55     saves[pri].params[saves[pri].nsaved].value = 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, 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, 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(char *p, char *value, int need_save, Conf *conf)
163 {
164     int ret = 0;
165
166     if (!strcmp(p, "-load")) {
167         RETURN(2);
168         /* This parameter must be processed immediately rather than being
169          * saved. */
170         do_defaults(value, conf);
171         loaded_session = TRUE;
172         cmdline_session_name = dupstr(value);
173         return 2;
174     }
175     if (!strcmp(p, "-ssh")) {
176         RETURN(1);
177         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
178         SAVEABLE(0);
179         default_protocol = PROT_SSH;
180         default_port = 22;
181         conf_set_int(conf, CONF_protocol, default_protocol);
182         conf_set_int(conf, CONF_port, default_port);
183         return 1;
184     }
185     if (!strcmp(p, "-telnet")) {
186         RETURN(1);
187         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
188         SAVEABLE(0);
189         default_protocol = PROT_TELNET;
190         default_port = 23;
191         conf_set_int(conf, CONF_protocol, default_protocol);
192         conf_set_int(conf, CONF_port, default_port);
193         return 1;
194     }
195     if (!strcmp(p, "-rlogin")) {
196         RETURN(1);
197         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
198         SAVEABLE(0);
199         default_protocol = PROT_RLOGIN;
200         default_port = 513;
201         conf_set_int(conf, CONF_protocol, default_protocol);
202         conf_set_int(conf, CONF_port, default_port);
203         return 1;
204     }
205     if (!strcmp(p, "-raw")) {
206         RETURN(1);
207         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
208         SAVEABLE(0);
209         default_protocol = PROT_RAW;
210         conf_set_int(conf, CONF_protocol, default_protocol);
211     }
212     if (!strcmp(p, "-serial")) {
213         RETURN(1);
214         /* Serial is not NONNETWORK in an odd sense of the word */
215         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
216         SAVEABLE(0);
217         default_protocol = PROT_SERIAL;
218         conf_set_int(conf, CONF_protocol, default_protocol);
219         /* The host parameter will already be loaded into CONF_host,
220          * so copy it across */
221         conf_set_str(conf, CONF_serline, conf_get_str(conf, CONF_host));
222     }
223     if (!strcmp(p, "-v")) {
224         RETURN(1);
225         flags |= FLAG_VERBOSE;
226     }
227     if (!strcmp(p, "-l")) {
228         RETURN(2);
229         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
230         SAVEABLE(0);
231         conf_set_str(conf, CONF_username, value);
232     }
233     if (!strcmp(p, "-loghost")) {
234         RETURN(2);
235         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
236         SAVEABLE(0);
237         conf_set_str(conf, CONF_loghost, value);
238     }
239     if (!strcmp(p, "-hostkey")) {
240         char *dup;
241         RETURN(2);
242         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
243         SAVEABLE(0);
244         dup = dupstr(value);
245         if (!validate_manual_hostkey(dup)) {
246             cmdline_error("'%s' is not a valid format for a manual host "
247                           "key specification", value);
248             sfree(dup);
249             return ret;
250         }
251         conf_set_str_str(conf, CONF_ssh_manual_hostkeys, dup, "");
252         sfree(dup);
253     }
254     if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
255         char type, *q, *qq, *key, *val;
256         RETURN(2);
257         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
258         SAVEABLE(0);
259         if (strcmp(p, "-D")) {
260             /*
261              * For -L or -R forwarding types:
262              *
263              * We expect _at least_ two colons in this string. The
264              * possible formats are `sourceport:desthost:destport',
265              * or `sourceip:sourceport:desthost:destport' if you're
266              * specifying a particular loopback address. We need to
267              * replace the one between source and dest with a \t;
268              * this means we must find the second-to-last colon in
269              * the string.
270              *
271              * (This looks like a foolish way of doing it given the
272              * existence of strrchr, but it's more efficient than
273              * two strrchrs - not to mention that the second strrchr
274              * would require us to modify the input string!)
275              */
276
277             type = p[1];               /* 'L' or 'R' */
278
279             q = qq = host_strchr(value, ':');
280             while (qq) {
281                 char *qqq = host_strchr(qq+1, ':');
282                 if (qqq)
283                     q = qq;
284                 qq = qqq;
285             }
286
287             if (!q) {
288                 cmdline_error("-%c expects at least two colons in its"
289                               " argument", type);
290                 return ret;
291             }
292
293             key = dupprintf("%c%.*s", type, (int)(q - value), value);
294             val = dupstr(q+1);
295         } else {
296             /*
297              * Dynamic port forwardings are entered under the same key
298              * as if they were local (because they occupy the same
299              * port space - a local and a dynamic forwarding on the
300              * same local port are mutually exclusive), with the
301              * special value "D" (which can be distinguished from
302              * anything in the ordinary -L case by containing no
303              * colon).
304              */
305             key = dupprintf("L%s", value);
306             val = dupstr("D");
307         }
308         conf_set_str_str(conf, CONF_portfwd, key, val);
309         sfree(key);
310         sfree(val);
311     }
312     if ((!strcmp(p, "-nc"))) {
313         char *host, *portp;
314
315         RETURN(2);
316         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
317         SAVEABLE(0);
318
319         portp = host_strchr(value, ':');
320         if (!portp) {
321             cmdline_error("-nc expects argument of form 'host:port'");
322             return ret;
323         }
324
325         host = dupprintf("%.*s", (int)(portp - value), value);
326         conf_set_str(conf, CONF_ssh_nc_host, host);
327         conf_set_int(conf, CONF_ssh_nc_port, atoi(portp + 1));
328         sfree(host);
329     }
330     if (!strcmp(p, "-m")) {
331         char *filename, *command;
332         int cmdlen, cmdsize;
333         FILE *fp;
334         int c, d;
335
336         RETURN(2);
337         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
338         SAVEABLE(0);
339
340         filename = value;
341
342         cmdlen = cmdsize = 0;
343         command = NULL;
344         fp = fopen(filename, "r");
345         if (!fp) {
346             cmdline_error("unable to open command file \"%s\"", filename);
347             return ret;
348         }
349         do {
350             c = fgetc(fp);
351             d = c;
352             if (c == EOF)
353                 d = 0;
354             if (cmdlen >= cmdsize) {
355                 cmdsize = cmdlen + 512;
356                 command = sresize(command, cmdsize, char);
357             }
358             command[cmdlen++] = d;
359         } while (c != EOF);
360         fclose(fp);
361         conf_set_str(conf, CONF_remote_cmd, command);
362         conf_set_str(conf, CONF_remote_cmd2, "");
363         conf_set_int(conf, CONF_nopty, TRUE);   /* command => no terminal */
364         sfree(command);
365     }
366     if (!strcmp(p, "-P")) {
367         RETURN(2);
368         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
369         SAVEABLE(1);                   /* lower priority than -ssh,-telnet */
370         conf_set_int(conf, CONF_port, atoi(value));
371     }
372     if (!strcmp(p, "-pw")) {
373         RETURN(2);
374         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
375         SAVEABLE(1);
376         /* We delay evaluating this until after the protocol is decided,
377          * so that we can warn if it's of no use with the selected protocol */
378         if (conf_get_int(conf, CONF_protocol) != PROT_SSH)
379             cmdline_error("the -pw option can only be used with the "
380                           "SSH protocol");
381         else {
382             cmdline_password = dupstr(value);
383             /* Assuming that `value' is directly from argv, make a good faith
384              * attempt to trample it, to stop it showing up in `ps' output
385              * on Unix-like systems. Not guaranteed, of course. */
386             smemclr(value, strlen(value));
387         }
388     }
389
390     if (!strcmp(p, "-agent") || !strcmp(p, "-pagent") ||
391         !strcmp(p, "-pageant")) {
392         RETURN(1);
393         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
394         SAVEABLE(0);
395         conf_set_int(conf, CONF_tryagent, TRUE);
396     }
397     if (!strcmp(p, "-noagent") || !strcmp(p, "-nopagent") ||
398         !strcmp(p, "-nopageant")) {
399         RETURN(1);
400         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
401         SAVEABLE(0);
402         conf_set_int(conf, CONF_tryagent, FALSE);
403     }
404
405     if (!strcmp(p, "-A")) {
406         RETURN(1);
407         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
408         SAVEABLE(0);
409         conf_set_int(conf, CONF_agentfwd, 1);
410     }
411     if (!strcmp(p, "-a")) {
412         RETURN(1);
413         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
414         SAVEABLE(0);
415         conf_set_int(conf, CONF_agentfwd, 0);
416     }
417
418     if (!strcmp(p, "-X")) {
419         RETURN(1);
420         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
421         SAVEABLE(0);
422         conf_set_int(conf, CONF_x11_forward, 1);
423     }
424     if (!strcmp(p, "-x")) {
425         RETURN(1);
426         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
427         SAVEABLE(0);
428         conf_set_int(conf, CONF_x11_forward, 0);
429     }
430
431     if (!strcmp(p, "-t")) {
432         RETURN(1);
433         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
434         SAVEABLE(1);    /* lower priority than -m */
435         conf_set_int(conf, CONF_nopty, 0);
436     }
437     if (!strcmp(p, "-T")) {
438         RETURN(1);
439         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
440         SAVEABLE(1);
441         conf_set_int(conf, CONF_nopty, 1);
442     }
443
444     if (!strcmp(p, "-N")) {
445         RETURN(1);
446         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
447         SAVEABLE(0);
448         conf_set_int(conf, CONF_ssh_no_shell, 1);
449     }
450
451     if (!strcmp(p, "-C")) {
452         RETURN(1);
453         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
454         SAVEABLE(0);
455         conf_set_int(conf, CONF_compression, 1);
456     }
457
458     if (!strcmp(p, "-1")) {
459         RETURN(1);
460         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
461         SAVEABLE(0);
462         conf_set_int(conf, CONF_sshprot, 0);   /* ssh protocol 1 only */
463     }
464     if (!strcmp(p, "-2")) {
465         RETURN(1);
466         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
467         SAVEABLE(0);
468         conf_set_int(conf, CONF_sshprot, 3);   /* ssh protocol 2 only */
469     }
470
471     if (!strcmp(p, "-i")) {
472         Filename *fn;
473         RETURN(2);
474         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
475         SAVEABLE(0);
476         fn = filename_from_str(value);
477         conf_set_filename(conf, CONF_keyfile, fn);
478         filename_free(fn);
479     }
480
481     if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) {
482         RETURN(1);
483         SAVEABLE(1);
484         conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV4);
485     }
486     if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) {
487         RETURN(1);
488         SAVEABLE(1);
489         conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV6);
490     }
491     if (!strcmp(p, "-sercfg")) {
492         char* nextitem;
493         RETURN(2);
494         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
495         SAVEABLE(1);
496         if (conf_get_int(conf, CONF_protocol) != PROT_SERIAL)
497             cmdline_error("the -sercfg option can only be used with the "
498                           "serial protocol");
499         /* Value[0] contains one or more , separated values, like 19200,8,n,1,X */
500         nextitem = value;
501         while (nextitem[0] != '\0') {
502             int length, skip;
503             char *end = strchr(nextitem, ',');
504             if (!end) {
505                 length = strlen(nextitem);
506                 skip = 0;
507             } else {
508                 length = end - nextitem;
509                 nextitem[length] = '\0';
510                 skip = 1;
511             }
512             if (length == 1) {
513                 switch (*nextitem) {
514                   case '1':
515                   case '2':
516                     conf_set_int(conf, CONF_serstopbits, 2 * (*nextitem-'0'));
517                     break;
518
519                   case '5':
520                   case '6':
521                   case '7':
522                   case '8':
523                   case '9':
524                     conf_set_int(conf, CONF_serdatabits, *nextitem-'0');
525                     break;
526
527                   case 'n':
528                     conf_set_int(conf, CONF_serparity, SER_PAR_NONE);
529                     break;
530                   case 'o':
531                     conf_set_int(conf, CONF_serparity, SER_PAR_ODD);
532                     break;
533                   case 'e':
534                     conf_set_int(conf, CONF_serparity, SER_PAR_EVEN);
535                     break;
536                   case 'm':
537                     conf_set_int(conf, CONF_serparity, SER_PAR_MARK);
538                     break;
539                   case 's':
540                     conf_set_int(conf, CONF_serparity, SER_PAR_SPACE);
541                     break;
542
543                   case 'N':
544                     conf_set_int(conf, CONF_serflow, SER_FLOW_NONE);
545                     break;
546                   case 'X':
547                     conf_set_int(conf, CONF_serflow, SER_FLOW_XONXOFF);
548                     break;
549                   case 'R':
550                     conf_set_int(conf, CONF_serflow, SER_FLOW_RTSCTS);
551                     break;
552                   case 'D':
553                     conf_set_int(conf, CONF_serflow, SER_FLOW_DSRDTR);
554                     break;
555
556                   default:
557                     cmdline_error("Unrecognised suboption \"-sercfg %c\"",
558                                   *nextitem);
559                 }
560             } else if (length == 3 && !strncmp(nextitem,"1.5",3)) {
561                 /* Messy special case */
562                 conf_set_int(conf, CONF_serstopbits, 3);
563             } else {
564                 int serspeed = atoi(nextitem);
565                 if (serspeed != 0) {
566                     conf_set_int(conf, CONF_serspeed, serspeed);
567                 } else {
568                     cmdline_error("Unrecognised suboption \"-sercfg %s\"",
569                                   nextitem);
570                 }
571             }
572             nextitem += length + skip;
573         }
574     }
575
576     if (!strcmp(p, "-sessionlog") ||
577         !strcmp(p, "-sshlog") ||
578         !strcmp(p, "-sshrawlog")) {
579         Filename *fn;
580         RETURN(2);
581         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
582         SAVEABLE(0);
583         fn = filename_from_str(value);
584         conf_set_filename(conf, CONF_logfilename, fn);
585         conf_set_int(conf, CONF_logtype,
586                      !strcmp(p, "-sessionlog") ? LGTYP_DEBUG :
587                      !strcmp(p, "-sshlog") ? LGTYP_PACKETS :
588                      /* !strcmp(p, "-sshrawlog") ? */ LGTYP_SSHRAW);
589         filename_free(fn);
590     }
591
592     return ret;                        /* unrecognised */
593 }
594
595 void cmdline_run_saved(Conf *conf)
596 {
597     int pri, i;
598     for (pri = 0; pri < NPRIORITIES; pri++)
599         for (i = 0; i < saves[pri].nsaved; i++)
600             cmdline_process_param(saves[pri].params[i].p,
601                                   saves[pri].params[i].value, 0, conf);
602 }