| 1 | /* |
| 2 | * PLink - a command-line (stdin/stdout) variant of PuTTY. |
| 3 | */ |
| 4 | |
| 5 | #ifndef AUTO_WINSOCK |
| 6 | #include <winsock2.h> |
| 7 | #endif |
| 8 | #include <windows.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdarg.h> |
| 11 | |
| 12 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
| 13 | #include "putty.h" |
| 14 | |
| 15 | void fatalbox (char *p, ...) { |
| 16 | va_list ap; |
| 17 | fprintf(stderr, "FATAL ERROR: ", p); |
| 18 | va_start(ap, p); |
| 19 | vfprintf(stderr, p, ap); |
| 20 | va_end(ap); |
| 21 | fputc('\n', stderr); |
| 22 | WSACleanup(); |
| 23 | exit(1); |
| 24 | } |
| 25 | void connection_fatal (char *p, ...) { |
| 26 | va_list ap; |
| 27 | fprintf(stderr, "FATAL ERROR: ", p); |
| 28 | va_start(ap, p); |
| 29 | vfprintf(stderr, p, ap); |
| 30 | va_end(ap); |
| 31 | fputc('\n', stderr); |
| 32 | WSACleanup(); |
| 33 | exit(1); |
| 34 | } |
| 35 | |
| 36 | static char *password = NULL; |
| 37 | |
| 38 | /* |
| 39 | * Stubs for linking with other modules. |
| 40 | */ |
| 41 | void write_clip (void *data, int len) { } |
| 42 | void term_deselect(void) { } |
| 43 | |
| 44 | HANDLE outhandle; |
| 45 | DWORD orig_console_mode; |
| 46 | |
| 47 | void begin_session(void) { |
| 48 | if (!cfg.ldisc_term) |
| 49 | SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT); |
| 50 | else |
| 51 | SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), orig_console_mode); |
| 52 | } |
| 53 | |
| 54 | void term_out(void) |
| 55 | { |
| 56 | int reap; |
| 57 | DWORD ret; |
| 58 | reap = 0; |
| 59 | while (reap < inbuf_head) { |
| 60 | if (!WriteFile(outhandle, inbuf+reap, inbuf_head-reap, &ret, NULL)) |
| 61 | return; /* give up in panic */ |
| 62 | reap += ret; |
| 63 | } |
| 64 | inbuf_head = 0; |
| 65 | } |
| 66 | |
| 67 | struct input_data { |
| 68 | DWORD len; |
| 69 | char buffer[4096]; |
| 70 | HANDLE event; |
| 71 | }; |
| 72 | |
| 73 | static int get_password(const char *prompt, char *str, int maxlen) |
| 74 | { |
| 75 | HANDLE hin, hout; |
| 76 | DWORD savemode, i; |
| 77 | |
| 78 | if (password) { |
| 79 | static int tried_once = 0; |
| 80 | |
| 81 | if (tried_once) { |
| 82 | return 0; |
| 83 | } else { |
| 84 | strncpy(str, password, maxlen); |
| 85 | str[maxlen-1] = '\0'; |
| 86 | tried_once = 1; |
| 87 | return 1; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | hin = GetStdHandle(STD_INPUT_HANDLE); |
| 92 | hout = GetStdHandle(STD_OUTPUT_HANDLE); |
| 93 | if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) { |
| 94 | fprintf(stderr, "Cannot get standard input/output handles"); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | GetConsoleMode(hin, &savemode); |
| 99 | SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) | |
| 100 | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT); |
| 101 | |
| 102 | WriteFile(hout, prompt, strlen(prompt), &i, NULL); |
| 103 | ReadFile(hin, str, maxlen-1, &i, NULL); |
| 104 | |
| 105 | SetConsoleMode(hin, savemode); |
| 106 | |
| 107 | if ((int)i > maxlen) i = maxlen-1; else i = i - 2; |
| 108 | str[i] = '\0'; |
| 109 | |
| 110 | WriteFile(hout, "\r\n", 2, &i, NULL); |
| 111 | |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | int WINAPI stdin_read_thread(void *param) { |
| 116 | struct input_data *idata = (struct input_data *)param; |
| 117 | HANDLE inhandle; |
| 118 | |
| 119 | inhandle = GetStdHandle(STD_INPUT_HANDLE); |
| 120 | |
| 121 | while (ReadFile(inhandle, idata->buffer, sizeof(idata->buffer), |
| 122 | &idata->len, NULL)) { |
| 123 | SetEvent(idata->event); |
| 124 | } |
| 125 | |
| 126 | idata->len = 0; |
| 127 | SetEvent(idata->event); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Short description of parameters. |
| 134 | */ |
| 135 | static void usage(void) |
| 136 | { |
| 137 | printf("PuTTY Link: command-line connection utility\n"); |
| 138 | printf("%s\n", ver); |
| 139 | printf("Usage: plink [options] [user@]host [command]\n"); |
| 140 | printf("Options:\n"); |
| 141 | printf(" -v show verbose messages\n"); |
| 142 | printf(" -ssh force use of ssh protocol\n"); |
| 143 | printf(" -P port connect to specified port\n"); |
| 144 | printf(" -pw passw login with specified password\n"); |
| 145 | exit(1); |
| 146 | } |
| 147 | |
| 148 | int main(int argc, char **argv) { |
| 149 | WSADATA wsadata; |
| 150 | WORD winsock_ver; |
| 151 | WSAEVENT netevent, stdinevent; |
| 152 | HANDLE handles[2]; |
| 153 | SOCKET socket; |
| 154 | DWORD threadid; |
| 155 | struct input_data idata; |
| 156 | int sending; |
| 157 | int portnumber = -1; |
| 158 | |
| 159 | ssh_get_password = get_password; |
| 160 | |
| 161 | flags = FLAG_STDERR; |
| 162 | /* |
| 163 | * Process the command line. |
| 164 | */ |
| 165 | do_defaults(NULL); |
| 166 | default_protocol = cfg.protocol; |
| 167 | default_port = cfg.port; |
| 168 | while (--argc) { |
| 169 | char *p = *++argv; |
| 170 | if (*p == '-') { |
| 171 | if (!strcmp(p, "-ssh")) { |
| 172 | default_protocol = cfg.protocol = PROT_SSH; |
| 173 | default_port = cfg.port = 22; |
| 174 | } else if (!strcmp(p, "-telnet")) { |
| 175 | default_protocol = cfg.protocol = PROT_TELNET; |
| 176 | default_port = cfg.port = 23; |
| 177 | } else if (!strcmp(p, "-raw")) { |
| 178 | default_protocol = cfg.protocol = PROT_RAW; |
| 179 | } else if (!strcmp(p, "-v")) { |
| 180 | flags |= FLAG_VERBOSE; |
| 181 | } else if (!strcmp(p, "-log")) { |
| 182 | logfile = "putty.log"; |
| 183 | } else if (!strcmp(p, "-pw") && argc > 1) { |
| 184 | --argc, password = *++argv; |
| 185 | } else if (!strcmp(p, "-l") && argc > 1) { |
| 186 | char *username; |
| 187 | --argc, username = *++argv; |
| 188 | strncpy(cfg.username, username, sizeof(cfg.username)); |
| 189 | cfg.username[sizeof(cfg.username)-1] = '\0'; |
| 190 | } else if (!strcmp(p, "-P") && argc > 1) { |
| 191 | --argc, portnumber = atoi(*++argv); |
| 192 | } |
| 193 | } else if (*p) { |
| 194 | if (!*cfg.host) { |
| 195 | char *q = p; |
| 196 | /* |
| 197 | * If the hostname starts with "telnet:", set the |
| 198 | * protocol to Telnet and process the string as a |
| 199 | * Telnet URL. |
| 200 | */ |
| 201 | if (!strncmp(q, "telnet:", 7)) { |
| 202 | char c; |
| 203 | |
| 204 | q += 7; |
| 205 | if (q[0] == '/' && q[1] == '/') |
| 206 | q += 2; |
| 207 | cfg.protocol = PROT_TELNET; |
| 208 | p = q; |
| 209 | while (*p && *p != ':' && *p != '/') p++; |
| 210 | c = *p; |
| 211 | if (*p) |
| 212 | *p++ = '\0'; |
| 213 | if (c == ':') |
| 214 | cfg.port = atoi(p); |
| 215 | else |
| 216 | cfg.port = -1; |
| 217 | strncpy (cfg.host, q, sizeof(cfg.host)-1); |
| 218 | cfg.host[sizeof(cfg.host)-1] = '\0'; |
| 219 | } else { |
| 220 | char *r; |
| 221 | /* |
| 222 | * Before we process the [user@]host string, we |
| 223 | * first check for the presence of a protocol |
| 224 | * prefix (a protocol name followed by ","). |
| 225 | */ |
| 226 | r = strchr(p, ','); |
| 227 | if (r) { |
| 228 | int i, j; |
| 229 | for (i = 0; backends[i].backend != NULL; i++) { |
| 230 | j = strlen(backends[i].name); |
| 231 | if (j == r-p && |
| 232 | !memcmp(backends[i].name, p, j)) { |
| 233 | default_protocol = cfg.protocol = backends[i].protocol; |
| 234 | portnumber = backends[i].backend->default_port; |
| 235 | p = r+1; |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Three cases. Either (a) there's a nonzero |
| 243 | * length string followed by an @, in which |
| 244 | * case that's user and the remainder is host. |
| 245 | * Or (b) there's only one string, not counting |
| 246 | * a potential initial @, and it exists in the |
| 247 | * saved-sessions database. Or (c) only one |
| 248 | * string and it _doesn't_ exist in the |
| 249 | * database. |
| 250 | */ |
| 251 | r = strrchr(p, '@'); |
| 252 | if (r == p) p++, r = NULL; /* discount initial @ */ |
| 253 | if (r == NULL) { |
| 254 | /* |
| 255 | * One string. |
| 256 | */ |
| 257 | do_defaults (p); |
| 258 | if (cfg.host[0] == '\0') { |
| 259 | /* No settings for this host; use defaults */ |
| 260 | strncpy(cfg.host, p, sizeof(cfg.host)-1); |
| 261 | cfg.host[sizeof(cfg.host)-1] = '\0'; |
| 262 | cfg.port = 22; |
| 263 | } |
| 264 | } else { |
| 265 | *r++ = '\0'; |
| 266 | strncpy(cfg.username, p, sizeof(cfg.username)-1); |
| 267 | cfg.username[sizeof(cfg.username)-1] = '\0'; |
| 268 | strncpy(cfg.host, r, sizeof(cfg.host)-1); |
| 269 | cfg.host[sizeof(cfg.host)-1] = '\0'; |
| 270 | cfg.port = 22; |
| 271 | } |
| 272 | } |
| 273 | } else { |
| 274 | int len = sizeof(cfg.remote_cmd) - 1; |
| 275 | char *cp = cfg.remote_cmd; |
| 276 | int len2; |
| 277 | |
| 278 | strncpy(cp, p, len); cp[len] = '\0'; |
| 279 | len2 = strlen(cp); len -= len2; cp += len2; |
| 280 | while (--argc) { |
| 281 | if (len > 0) |
| 282 | len--, *cp++ = ' '; |
| 283 | strncpy(cp, *++argv, len); cp[len] = '\0'; |
| 284 | len2 = strlen(cp); len -= len2; cp += len2; |
| 285 | } |
| 286 | cfg.nopty = TRUE; /* command => no terminal */ |
| 287 | cfg.ldisc_term = TRUE; /* use stdin like a line buffer */ |
| 288 | break; /* done with cmdline */ |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (!*cfg.host) { |
| 294 | usage(); |
| 295 | } |
| 296 | if (portnumber != -1) |
| 297 | cfg.port = portnumber; |
| 298 | |
| 299 | if (!*cfg.remote_cmd) |
| 300 | flags |= FLAG_INTERACTIVE; |
| 301 | |
| 302 | /* |
| 303 | * Select protocol. This is farmed out into a table in a |
| 304 | * separate file to enable an ssh-free variant. |
| 305 | */ |
| 306 | { |
| 307 | int i; |
| 308 | back = NULL; |
| 309 | for (i = 0; backends[i].backend != NULL; i++) |
| 310 | if (backends[i].protocol == cfg.protocol) { |
| 311 | back = backends[i].backend; |
| 312 | break; |
| 313 | } |
| 314 | if (back == NULL) { |
| 315 | fprintf(stderr, "Internal fault: Unsupported protocol found\n"); |
| 316 | return 1; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Initialise WinSock. |
| 322 | */ |
| 323 | winsock_ver = MAKEWORD(2, 0); |
| 324 | if (WSAStartup(winsock_ver, &wsadata)) { |
| 325 | MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error", |
| 326 | MB_OK | MB_ICONEXCLAMATION); |
| 327 | return 1; |
| 328 | } |
| 329 | if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 0) { |
| 330 | MessageBox(NULL, "WinSock version is incompatible with 2.0", |
| 331 | "WinSock Error", MB_OK | MB_ICONEXCLAMATION); |
| 332 | WSACleanup(); |
| 333 | return 1; |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * Start up the connection. |
| 338 | */ |
| 339 | { |
| 340 | char *error; |
| 341 | char *realhost; |
| 342 | |
| 343 | error = back->init (NULL, cfg.host, cfg.port, &realhost); |
| 344 | if (error) { |
| 345 | fprintf(stderr, "Unable to open connection:\n%s", error); |
| 346 | return 1; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | netevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 351 | stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 352 | |
| 353 | GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &orig_console_mode); |
| 354 | SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT); |
| 355 | outhandle = GetStdHandle(STD_OUTPUT_HANDLE); |
| 356 | |
| 357 | /* |
| 358 | * Now we must send the back end oodles of stuff. |
| 359 | */ |
| 360 | socket = back->socket(); |
| 361 | /* |
| 362 | * Turn off ECHO and LINE input modes. We don't care if this |
| 363 | * call fails, because we know we aren't necessarily running in |
| 364 | * a console. |
| 365 | */ |
| 366 | WSAEventSelect(socket, netevent, FD_READ | FD_CLOSE); |
| 367 | handles[0] = netevent; |
| 368 | handles[1] = stdinevent; |
| 369 | sending = FALSE; |
| 370 | while (1) { |
| 371 | int n; |
| 372 | n = WaitForMultipleObjects(2, handles, FALSE, INFINITE); |
| 373 | if (n == 0) { |
| 374 | WSANETWORKEVENTS things; |
| 375 | if (!WSAEnumNetworkEvents(socket, netevent, &things)) { |
| 376 | if (things.lNetworkEvents & FD_READ) |
| 377 | back->msg(0, FD_READ); |
| 378 | if (things.lNetworkEvents & FD_CLOSE) { |
| 379 | back->msg(0, FD_CLOSE); |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | term_out(); |
| 384 | if (!sending && back->sendok()) { |
| 385 | /* |
| 386 | * Create a separate thread to read from stdin. |
| 387 | * This is a total pain, but I can't find another |
| 388 | * way to do it: |
| 389 | * |
| 390 | * - an overlapped ReadFile or ReadFileEx just |
| 391 | * doesn't happen; we get failure from |
| 392 | * ReadFileEx, and ReadFile blocks despite being |
| 393 | * given an OVERLAPPED structure. Perhaps we |
| 394 | * can't do overlapped reads on consoles. WHY |
| 395 | * THE HELL NOT? |
| 396 | * |
| 397 | * - WaitForMultipleObjects(netevent, console) |
| 398 | * doesn't work, because it signals the console |
| 399 | * when _anything_ happens, including mouse |
| 400 | * motions and other things that don't cause |
| 401 | * data to be readable - so we're back to |
| 402 | * ReadFile blocking. |
| 403 | */ |
| 404 | idata.event = stdinevent; |
| 405 | if (!CreateThread(NULL, 0, stdin_read_thread, |
| 406 | &idata, 0, &threadid)) { |
| 407 | fprintf(stderr, "Unable to create second thread\n"); |
| 408 | exit(1); |
| 409 | } |
| 410 | sending = TRUE; |
| 411 | } |
| 412 | } else if (n == 1) { |
| 413 | if (idata.len > 0) { |
| 414 | back->send(idata.buffer, idata.len); |
| 415 | } else { |
| 416 | back->special(TS_EOF); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | WSACleanup(); |
| 421 | return 0; |
| 422 | } |