dfe3332b |
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. |
c725e24c |
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.) |
dfe3332b |
24 | */ |
25 | |
c725e24c |
26 | #define NPRIORITIES 2 |
dfe3332b |
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; |
3d88e64d |
46 | saves[pri].params = sresize(saves[pri].params, saves[pri].savesize, |
47 | struct cmdline_saved_param); |
dfe3332b |
48 | } |
49 | saves[pri].params[saves[pri].nsaved].p = p; |
50 | saves[pri].params[saves[pri].nsaved].value = value; |
51 | saves[pri].nsaved++; |
52 | } |
53 | |
679539d7 |
54 | void cmdline_cleanup(void) |
55 | { |
56 | int pri; |
57 | |
58 | for (pri = 0; pri < NPRIORITIES; pri++) |
59 | sfree(saves[pri].params); |
60 | } |
61 | |
dfe3332b |
62 | #define SAVEABLE(pri) do { \ |
63 | if (need_save) { cmdline_save_param(p, value, pri); return ret; } \ |
64 | } while (0) |
65 | |
66 | char *cmdline_password = NULL; |
67 | |
68 | static int cmdline_get_line(const char *prompt, char *str, |
69 | int maxlen, int is_pw) |
70 | { |
71 | static int tried_once = 0; |
72 | |
73 | assert(is_pw && cmdline_password); |
74 | |
75 | if (tried_once) { |
76 | return 0; |
77 | } else { |
78 | strncpy(str, cmdline_password, maxlen); |
79 | str[maxlen - 1] = '\0'; |
80 | tried_once = 1; |
81 | return 1; |
82 | } |
83 | } |
84 | |
85 | /* |
86 | * Here we have a flags word which describes the capabilities of |
87 | * the particular tool on whose behalf we're running. We will |
88 | * refuse certain command-line options if a particular tool |
89 | * inherently can't do anything sensible. For example, the file |
90 | * transfer tools (psftp, pscp) can't do a great deal with protocol |
91 | * selections (ever tried running scp over telnet?) or with port |
92 | * forwarding (even if it wasn't a hideously bad idea, they don't |
93 | * have the select() infrastructure to make them work). |
94 | */ |
95 | int cmdline_tooltype = 0; |
96 | |
97 | static int cmdline_check_unavailable(int flag, char *p) |
98 | { |
99 | if (cmdline_tooltype & flag) { |
100 | cmdline_error("option \"%s\" not available in this tool", p); |
101 | return 1; |
102 | } |
103 | return 0; |
104 | } |
105 | |
106 | #define UNAVAILABLE_IN(flag) do { \ |
107 | if (cmdline_check_unavailable(flag, p)) return ret; \ |
108 | } while (0) |
109 | |
110 | /* |
111 | * Process a standard command-line parameter. `p' is the parameter |
112 | * in question; `value' is the subsequent element of argv, which |
113 | * may or may not be required as an operand to the parameter. |
b552f500 |
114 | * If `need_save' is 1, arguments which need to be saved as |
115 | * described at this top of this file are, for later execution; |
116 | * if 0, they are processed normally. (-1 is a special value used |
117 | * by pterm to count arguments for a preliminary pass through the |
118 | * argument list; it causes immediate return with an appropriate |
119 | * value with no action taken.) |
dfe3332b |
120 | * Return value is 2 if both arguments were used; 1 if only p was |
121 | * used; 0 if the parameter wasn't one we recognised; -2 if it |
122 | * should have been 2 but value was NULL. |
123 | */ |
124 | |
125 | #define RETURN(x) do { \ |
126 | if ((x) == 2 && !value) return -2; \ |
127 | ret = x; \ |
46a3419b |
128 | if (need_save < 0) return x; \ |
dfe3332b |
129 | } while (0) |
130 | |
5555d393 |
131 | int cmdline_process_param(char *p, char *value, int need_save, Config *cfg) |
dfe3332b |
132 | { |
133 | int ret = 0; |
134 | |
135 | if (!strcmp(p, "-load")) { |
136 | RETURN(2); |
c725e24c |
137 | /* This parameter must be processed immediately rather than being |
138 | * saved. */ |
5555d393 |
139 | do_defaults(value, cfg); |
18e62ad8 |
140 | loaded_session = TRUE; |
dfe3332b |
141 | return 2; |
142 | } |
143 | if (!strcmp(p, "-ssh")) { |
144 | RETURN(1); |
46a3419b |
145 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
146 | SAVEABLE(0); |
5555d393 |
147 | default_protocol = cfg->protocol = PROT_SSH; |
148 | default_port = cfg->port = 22; |
dfe3332b |
149 | return 1; |
150 | } |
151 | if (!strcmp(p, "-telnet")) { |
152 | RETURN(1); |
46a3419b |
153 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
154 | SAVEABLE(0); |
5555d393 |
155 | default_protocol = cfg->protocol = PROT_TELNET; |
156 | default_port = cfg->port = 23; |
dfe3332b |
157 | return 1; |
158 | } |
159 | if (!strcmp(p, "-rlogin")) { |
160 | RETURN(1); |
46a3419b |
161 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
162 | SAVEABLE(0); |
5555d393 |
163 | default_protocol = cfg->protocol = PROT_RLOGIN; |
164 | default_port = cfg->port = 513; |
dfe3332b |
165 | return 1; |
166 | } |
167 | if (!strcmp(p, "-raw")) { |
168 | RETURN(1); |
46a3419b |
169 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
170 | SAVEABLE(0); |
5555d393 |
171 | default_protocol = cfg->protocol = PROT_RAW; |
dfe3332b |
172 | } |
173 | if (!strcmp(p, "-v")) { |
174 | RETURN(1); |
175 | flags |= FLAG_VERBOSE; |
176 | } |
177 | if (!strcmp(p, "-l")) { |
178 | RETURN(2); |
46a3419b |
179 | UNAVAILABLE_IN(TOOLTYPE_NONNETWORK); |
c725e24c |
180 | SAVEABLE(0); |
5555d393 |
181 | strncpy(cfg->username, value, sizeof(cfg->username)); |
182 | cfg->username[sizeof(cfg->username) - 1] = '\0'; |
dfe3332b |
183 | } |
820ebe3b |
184 | if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) { |
6ee9b735 |
185 | char *fwd, *ptr, *q, *qq; |
820ebe3b |
186 | int dynamic, i=0; |
dfe3332b |
187 | RETURN(2); |
46a3419b |
188 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
189 | SAVEABLE(0); |
820ebe3b |
190 | dynamic = !strcmp(p, "-D"); |
dfe3332b |
191 | fwd = value; |
5555d393 |
192 | ptr = cfg->portfwd; |
dfe3332b |
193 | /* if multiple forwards, find end of list */ |
820ebe3b |
194 | if (ptr[0]=='R' || ptr[0]=='L' || ptr[0] == 'D') { |
5555d393 |
195 | for (i = 0; i < sizeof(cfg->portfwd) - 2; i++) |
dfe3332b |
196 | if (ptr[i]=='\000' && ptr[i+1]=='\000') |
197 | break; |
198 | ptr = ptr + i + 1; /* point to next forward slot */ |
199 | } |
820ebe3b |
200 | ptr[0] = p[1]; /* insert a 'L', 'R' or 'D' at the start */ |
5555d393 |
201 | if (strlen(fwd) > sizeof(cfg->portfwd) - i - 2) { |
dfe3332b |
202 | cmdline_error("out of space for port forwardings"); |
203 | return ret; |
204 | } |
5555d393 |
205 | strncpy(ptr+1, fwd, sizeof(cfg->portfwd) - i); |
820ebe3b |
206 | if (!dynamic) { |
207 | /* |
208 | * We expect _at least_ two colons in this string. The |
209 | * possible formats are `sourceport:desthost:destport', |
210 | * or `sourceip:sourceport:desthost:destport' if you're |
211 | * specifying a particular loopback address. We need to |
212 | * replace the one between source and dest with a \t; |
213 | * this means we must find the second-to-last colon in |
214 | * the string. |
215 | */ |
216 | q = qq = strchr(ptr, ':'); |
217 | while (qq) { |
218 | char *qqq = strchr(qq+1, ':'); |
219 | if (qqq) |
220 | q = qq; |
221 | qq = qqq; |
222 | } |
223 | if (q) *q = '\t'; /* replace second-last colon with \t */ |
6ee9b735 |
224 | } |
5555d393 |
225 | cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0'; |
226 | cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0'; |
dfe3332b |
227 | ptr[strlen(ptr)+1] = '\000'; /* append two '\000' */ |
228 | } |
229 | if (!strcmp(p, "-m")) { |
230 | char *filename, *command; |
231 | int cmdlen, cmdsize; |
232 | FILE *fp; |
233 | int c, d; |
234 | |
235 | RETURN(2); |
46a3419b |
236 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
237 | SAVEABLE(0); |
dfe3332b |
238 | |
239 | filename = value; |
240 | |
241 | cmdlen = cmdsize = 0; |
242 | command = NULL; |
243 | fp = fopen(filename, "r"); |
244 | if (!fp) { |
245 | cmdline_error("unable to open command " |
246 | "file \"%s\"", filename); |
247 | return ret; |
248 | } |
249 | do { |
250 | c = fgetc(fp); |
251 | d = c; |
252 | if (c == EOF) |
253 | d = 0; |
254 | if (cmdlen >= cmdsize) { |
255 | cmdsize = cmdlen + 512; |
3d88e64d |
256 | command = sresize(command, cmdsize, char); |
dfe3332b |
257 | } |
258 | command[cmdlen++] = d; |
259 | } while (c != EOF); |
5555d393 |
260 | cfg->remote_cmd_ptr = command; |
261 | cfg->remote_cmd_ptr2 = NULL; |
262 | cfg->nopty = TRUE; /* command => no terminal */ |
dfe3332b |
263 | } |
264 | if (!strcmp(p, "-P")) { |
265 | RETURN(2); |
46a3419b |
266 | UNAVAILABLE_IN(TOOLTYPE_NONNETWORK); |
c725e24c |
267 | SAVEABLE(1); /* lower priority than -ssh,-telnet */ |
5555d393 |
268 | cfg->port = atoi(value); |
dfe3332b |
269 | } |
270 | if (!strcmp(p, "-pw")) { |
271 | RETURN(2); |
46a3419b |
272 | UNAVAILABLE_IN(TOOLTYPE_NONNETWORK); |
dfe3332b |
273 | cmdline_password = value; |
274 | ssh_get_line = cmdline_get_line; |
275 | ssh_getline_pw_only = TRUE; |
276 | } |
277 | |
278 | if (!strcmp(p, "-A")) { |
279 | RETURN(1); |
46a3419b |
280 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
281 | SAVEABLE(0); |
5555d393 |
282 | cfg->agentfwd = 1; |
dfe3332b |
283 | } |
284 | if (!strcmp(p, "-a")) { |
285 | RETURN(1); |
46a3419b |
286 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
287 | SAVEABLE(0); |
5555d393 |
288 | cfg->agentfwd = 0; |
dfe3332b |
289 | } |
290 | |
291 | if (!strcmp(p, "-X")) { |
292 | RETURN(1); |
46a3419b |
293 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
294 | SAVEABLE(0); |
5555d393 |
295 | cfg->x11_forward = 1; |
dfe3332b |
296 | } |
297 | if (!strcmp(p, "-x")) { |
298 | RETURN(1); |
46a3419b |
299 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
300 | SAVEABLE(0); |
5555d393 |
301 | cfg->x11_forward = 0; |
dfe3332b |
302 | } |
303 | |
304 | if (!strcmp(p, "-t")) { |
305 | RETURN(1); |
46a3419b |
306 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
307 | SAVEABLE(0); |
5555d393 |
308 | cfg->nopty = 0; |
dfe3332b |
309 | } |
310 | if (!strcmp(p, "-T")) { |
311 | RETURN(1); |
46a3419b |
312 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
c725e24c |
313 | SAVEABLE(0); |
5555d393 |
314 | cfg->nopty = 1; |
dfe3332b |
315 | } |
316 | |
0ed48730 |
317 | if (!strcmp(p, "-N")) { |
318 | RETURN(1); |
319 | UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); |
320 | SAVEABLE(0); |
321 | cfg->ssh_no_shell = 1; |
322 | } |
323 | |
dfe3332b |
324 | if (!strcmp(p, "-C")) { |
325 | RETURN(1); |
46a3419b |
326 | UNAVAILABLE_IN(TOOLTYPE_NONNETWORK); |
c725e24c |
327 | SAVEABLE(0); |
5555d393 |
328 | cfg->compression = 1; |
dfe3332b |
329 | } |
330 | |
331 | if (!strcmp(p, "-1")) { |
332 | RETURN(1); |
46a3419b |
333 | UNAVAILABLE_IN(TOOLTYPE_NONNETWORK); |
c725e24c |
334 | SAVEABLE(0); |
5555d393 |
335 | cfg->sshprot = 0; /* ssh protocol 1 only */ |
dfe3332b |
336 | } |
337 | if (!strcmp(p, "-2")) { |
338 | RETURN(1); |
46a3419b |
339 | UNAVAILABLE_IN(TOOLTYPE_NONNETWORK); |
c725e24c |
340 | SAVEABLE(0); |
5555d393 |
341 | cfg->sshprot = 3; /* ssh protocol 2 only */ |
dfe3332b |
342 | } |
343 | |
344 | if (!strcmp(p, "-i")) { |
345 | RETURN(2); |
46a3419b |
346 | UNAVAILABLE_IN(TOOLTYPE_NONNETWORK); |
c725e24c |
347 | SAVEABLE(0); |
9a30e26b |
348 | cfg->keyfile = filename_from_str(value); |
dfe3332b |
349 | } |
350 | |
05581745 |
351 | if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) { |
352 | RETURN(1); |
353 | SAVEABLE(1); |
354 | cfg->addressfamily = ADDRTYPE_IPV4; |
355 | } |
356 | if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) { |
357 | RETURN(1); |
358 | SAVEABLE(1); |
359 | cfg->addressfamily = ADDRTYPE_IPV6; |
360 | } |
361 | |
dfe3332b |
362 | return ret; /* unrecognised */ |
363 | } |
364 | |
5555d393 |
365 | void cmdline_run_saved(Config *cfg) |
dfe3332b |
366 | { |
367 | int pri, i; |
368 | for (pri = 0; pri < NPRIORITIES; pri++) |
369 | for (i = 0; i < saves[pri].nsaved; i++) |
370 | cmdline_process_param(saves[pri].params[i].p, |
5555d393 |
371 | saves[pri].params[i].value, 0, cfg); |
dfe3332b |
372 | } |