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