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