]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - cmdline.c
Fiddle with the cmdline_saved mechanism: the `-load' option is now
[PuTTY.git] / cmdline.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include "putty.h"
5
6 /*
7  * Some command-line parameters need to be saved up until after
8  * we've loaded the saved session which will form the basis of our
9  * eventual running configuration. For this we use the macro
10  * SAVEABLE, which notices if the `need_save' parameter is set and
11  * saves the parameter and value on a list.
12  * 
13  * We also assign priorities to saved parameters, just to slightly
14  * ameliorate silly ordering problems. For example, if you specify
15  * a saved session to load, it will be loaded _before_ all your
16  * local modifications such as -L are evaluated; and if you specify
17  * a protocol and a port, the protocol is set up first so that the
18  * port can override its choice of port number.
19  * 
20  * (In fact -load is not saved at all, since in at least Plink the
21  * processing of further command-line options depends on whether or
22  * not the loaded session contained a hostname. So it must be
23  * executed immediately.)
24  */
25
26 #define NPRIORITIES 2
27
28 struct cmdline_saved_param {
29     char *p, *value;
30 };
31 struct cmdline_saved_param_set {
32     struct cmdline_saved_param *params;
33     int nsaved, savesize;
34 };
35
36 /*
37  * C guarantees this structure will be initialised to all zero at
38  * program start, which is exactly what we want.
39  */
40 static struct cmdline_saved_param_set saves[NPRIORITIES];
41
42 static void cmdline_save_param(char *p, char *value, int pri)
43 {
44     if (saves[pri].nsaved >= saves[pri].savesize) {
45         saves[pri].savesize = saves[pri].nsaved + 32;
46         saves[pri].params =
47             srealloc(saves[pri].params,
48                      saves[pri].savesize*sizeof(*saves[pri].params));
49     }
50     saves[pri].params[saves[pri].nsaved].p = p;
51     saves[pri].params[saves[pri].nsaved].value = value;
52     saves[pri].nsaved++;
53 }
54
55 #define SAVEABLE(pri) do { \
56     if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
57 } while (0)
58
59 char *cmdline_password = NULL;
60
61 static int cmdline_get_line(const char *prompt, char *str,
62                             int maxlen, int is_pw)
63 {
64     static int tried_once = 0;
65
66     assert(is_pw && cmdline_password);
67
68     if (tried_once) {
69         return 0;
70     } else {
71         strncpy(str, cmdline_password, maxlen);
72         str[maxlen - 1] = '\0';
73         tried_once = 1;
74         return 1;
75     }
76 }
77
78 /*
79  * Here we have a flags word which describes the capabilities of
80  * the particular tool on whose behalf we're running. We will
81  * refuse certain command-line options if a particular tool
82  * inherently can't do anything sensible. For example, the file
83  * transfer tools (psftp, pscp) can't do a great deal with protocol
84  * selections (ever tried running scp over telnet?) or with port
85  * forwarding (even if it wasn't a hideously bad idea, they don't
86  * have the select() infrastructure to make them work).
87  */
88 int cmdline_tooltype = 0;
89
90 static int cmdline_check_unavailable(int flag, char *p)
91 {
92     if (cmdline_tooltype & flag) {
93         cmdline_error("option \"%s\" not available in this tool", p);
94         return 1;
95     }
96     return 0;
97 }
98
99 #define UNAVAILABLE_IN(flag) do { \
100     if (cmdline_check_unavailable(flag, p)) return ret; \
101 } while (0)
102
103 /*
104  * Process a standard command-line parameter. `p' is the parameter
105  * in question; `value' is the subsequent element of argv, which
106  * may or may not be required as an operand to the parameter.
107  * Return value is 2 if both arguments were used; 1 if only p was
108  * used; 0 if the parameter wasn't one we recognised; -2 if it
109  * should have been 2 but value was NULL.
110  */
111
112 #define RETURN(x) do { \
113     if ((x) == 2 && !value) return -2; \
114     ret = x; \
115 } while (0)
116
117 int cmdline_process_param(char *p, char *value, int need_save, Config *cfg)
118 {
119     int ret = 0;
120
121     if (!strcmp(p, "-load")) {
122         RETURN(2);
123         /* This parameter must be processed immediately rather than being
124          * saved. */
125         do_defaults(value, cfg);
126         return 2;
127     }
128     if (!strcmp(p, "-ssh")) {
129         RETURN(1);
130         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
131         SAVEABLE(0);
132         default_protocol = cfg->protocol = PROT_SSH;
133         default_port = cfg->port = 22;
134         return 1;
135     }
136     if (!strcmp(p, "-telnet")) {
137         RETURN(1);
138         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
139         SAVEABLE(0);
140         default_protocol = cfg->protocol = PROT_TELNET;
141         default_port = cfg->port = 23;
142         return 1;
143     }
144     if (!strcmp(p, "-rlogin")) {
145         RETURN(1);
146         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
147         SAVEABLE(0);
148         default_protocol = cfg->protocol = PROT_RLOGIN;
149         default_port = cfg->port = 513;
150         return 1;
151     }
152     if (!strcmp(p, "-raw")) {
153         RETURN(1);
154         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
155         SAVEABLE(0);
156         default_protocol = cfg->protocol = PROT_RAW;
157     }
158     if (!strcmp(p, "-v")) {
159         RETURN(1);
160         flags |= FLAG_VERBOSE;
161     }
162     if (!strcmp(p, "-l")) {
163         RETURN(2);
164         SAVEABLE(0);
165         strncpy(cfg->username, value, sizeof(cfg->username));
166         cfg->username[sizeof(cfg->username) - 1] = '\0';
167     }
168     if ((!strcmp(p, "-L") || !strcmp(p, "-R"))) {
169         char *fwd, *ptr, *q, *qq;
170         int i=0;
171         RETURN(2);
172         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
173         SAVEABLE(0);
174         fwd = value;
175         ptr = cfg->portfwd;
176         /* if multiple forwards, find end of list */
177         if (ptr[0]=='R' || ptr[0]=='L') {
178             for (i = 0; i < sizeof(cfg->portfwd) - 2; i++)
179                 if (ptr[i]=='\000' && ptr[i+1]=='\000')
180                     break;
181             ptr = ptr + i + 1;  /* point to next forward slot */
182         }
183         ptr[0] = p[1];  /* insert a 'L' or 'R' at the start */
184         if (strlen(fwd) > sizeof(cfg->portfwd) - i - 2) {
185             cmdline_error("out of space for port forwardings");
186             return ret;
187         }
188         strncpy(ptr+1, fwd, sizeof(cfg->portfwd) - i);
189         /*
190          * We expect _at least_ two colons in this string. The
191          * possible formats are `sourceport:desthost:destport', or
192          * `sourceip:sourceport:desthost:destport' if you're
193          * specifying a particular loopback address. We need to
194          * replace the one between source and dest with a \t; this
195          * means we must find the second-to-last colon in the
196          * string.
197          */
198         q = qq = strchr(ptr, ':');
199         while (qq) {
200             char *qqq = strchr(qq+1, ':');
201             if (qqq)
202                 q = qq;
203             qq = qqq;
204         }
205         if (q) *q = '\t';              /* replace second-last colon with \t */
206         cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';
207         cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';
208         ptr[strlen(ptr)+1] = '\000';    /* append two '\000' */
209     }
210     if (!strcmp(p, "-m")) {
211         char *filename, *command;
212         int cmdlen, cmdsize;
213         FILE *fp;
214         int c, d;
215
216         RETURN(2);
217         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
218         SAVEABLE(0);
219
220         filename = value;
221
222         cmdlen = cmdsize = 0;
223         command = NULL;
224         fp = fopen(filename, "r");
225         if (!fp) {
226             cmdline_error("unable to open command "
227                           "file \"%s\"", filename);
228             return ret;
229         }
230         do {
231             c = fgetc(fp);
232             d = c;
233             if (c == EOF)
234                 d = 0;
235             if (cmdlen >= cmdsize) {
236                 cmdsize = cmdlen + 512;
237                 command = srealloc(command, cmdsize);
238             }
239             command[cmdlen++] = d;
240         } while (c != EOF);
241         cfg->remote_cmd_ptr = command;
242         cfg->remote_cmd_ptr2 = NULL;
243         cfg->nopty = TRUE;      /* command => no terminal */
244     }
245     if (!strcmp(p, "-P")) {
246         RETURN(2);
247         SAVEABLE(1);                   /* lower priority than -ssh,-telnet */
248         cfg->port = atoi(value);
249     }
250     if (!strcmp(p, "-pw")) {
251         RETURN(2);
252         cmdline_password = value;
253         ssh_get_line = cmdline_get_line;
254         ssh_getline_pw_only = TRUE;
255     }
256
257     if (!strcmp(p, "-A")) {
258         RETURN(1);
259         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
260         SAVEABLE(0);
261         cfg->agentfwd = 1;
262     }
263     if (!strcmp(p, "-a")) {
264         RETURN(1);
265         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
266         SAVEABLE(0);
267         cfg->agentfwd = 0;
268     }
269
270     if (!strcmp(p, "-X")) {
271         RETURN(1);
272         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
273         SAVEABLE(0);
274         cfg->x11_forward = 1;
275     }
276     if (!strcmp(p, "-x")) {
277         RETURN(1);
278         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
279         SAVEABLE(0);
280         cfg->x11_forward = 0;
281     }
282
283     if (!strcmp(p, "-t")) {
284         RETURN(1);
285         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
286         SAVEABLE(0);
287         cfg->nopty = 0;
288     }
289     if (!strcmp(p, "-T")) {
290         RETURN(1);
291         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
292         SAVEABLE(0);
293         cfg->nopty = 1;
294     }
295
296     if (!strcmp(p, "-C")) {
297         RETURN(1);
298         SAVEABLE(0);
299         cfg->compression = 1;
300     }
301
302     if (!strcmp(p, "-1")) {
303         RETURN(1);
304         SAVEABLE(0);
305         cfg->sshprot = 0;              /* ssh protocol 1 only */
306     }
307     if (!strcmp(p, "-2")) {
308         RETURN(1);
309         SAVEABLE(0);
310         cfg->sshprot = 3;              /* ssh protocol 2 only */
311     }
312
313     if (!strcmp(p, "-i")) {
314         RETURN(2);
315         SAVEABLE(0);
316         cfg->keyfile = filename_from_str(value);
317     }
318
319     return ret;                        /* unrecognised */
320 }
321
322 void cmdline_run_saved(Config *cfg)
323 {
324     int pri, i;
325     for (pri = 0; pri < NPRIORITIES; pri++)
326         for (i = 0; i < saves[pri].nsaved; i++)
327             cmdline_process_param(saves[pri].params[i].p,
328                                   saves[pri].params[i].value, 0, cfg);
329 }