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