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