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