2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
12 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
15 void fatalbox (char *p, ...) {
17 fprintf(stderr, "FATAL ERROR: ", p);
19 vfprintf(stderr, p, ap);
25 void connection_fatal (char *p, ...) {
27 fprintf(stderr, "FATAL ERROR: ", p);
29 vfprintf(stderr, p, ap);
36 static char *password = NULL;
39 * Stubs for linking with other modules.
41 void write_clip (void *data, int len) { }
42 void term_deselect(void) { }
45 DWORD orig_console_mode;
47 void begin_session(void) {
49 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);
51 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), orig_console_mode);
59 while (reap < inbuf_head) {
60 if (!WriteFile(outhandle, inbuf+reap, inbuf_head-reap, &ret, NULL))
61 return; /* give up in panic */
73 static int get_password(const char *prompt, char *str, int maxlen)
79 static int tried_once = 0;
84 strncpy(str, password, maxlen);
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");
98 GetConsoleMode(hin, &savemode);
99 SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) |
100 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT);
102 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
103 ReadFile(hin, str, maxlen-1, &i, NULL);
105 SetConsoleMode(hin, savemode);
107 if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
110 WriteFile(hout, "\r\n", 2, &i, NULL);
115 int WINAPI stdin_read_thread(void *param) {
116 struct input_data *idata = (struct input_data *)param;
119 inhandle = GetStdHandle(STD_INPUT_HANDLE);
121 while (ReadFile(inhandle, idata->buffer, sizeof(idata->buffer),
122 &idata->len, NULL)) {
123 SetEvent(idata->event);
127 SetEvent(idata->event);
133 * Short description of parameters.
135 static void usage(void)
137 printf("PuTTY Link: command-line connection utility\n");
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");
148 int main(int argc, char **argv) {
151 WSAEVENT netevent, stdinevent;
155 struct input_data idata;
159 ssh_get_password = get_password;
163 * Process the command line.
165 default_protocol = DEFAULT_PROTOCOL;
166 default_port = DEFAULT_PORT;
171 if (!strcmp(p, "-ssh")) {
172 default_protocol = cfg.protocol = PROT_SSH;
173 default_port = cfg.port = 22;
174 } else if (!strcmp(p, "-v")) {
175 flags |= FLAG_VERBOSE;
176 } else if (!strcmp(p, "-log")) {
177 logfile = "putty.log";
178 } else if (!strcmp(p, "-pw") && argc > 1) {
179 --argc, password = *++argv;
180 printf("pw is %s\n", password);
181 } else if (!strcmp(p, "-P") && argc > 1) {
182 --argc, portnumber = atoi(*++argv);
188 * If the hostname starts with "telnet:", set the
189 * protocol to Telnet and process the string as a
192 if (!strncmp(q, "telnet:", 7)) {
196 if (q[0] == '/' && q[1] == '/')
198 cfg.protocol = PROT_TELNET;
200 while (*p && *p != ':' && *p != '/') p++;
208 strncpy (cfg.host, q, sizeof(cfg.host)-1);
209 cfg.host[sizeof(cfg.host)-1] = '\0';
212 * Three cases. Either (a) there's a nonzero
213 * length string followed by an @, in which
214 * case that's user and the remainder is host.
215 * Or (b) there's only one string, not counting
216 * a potential initial @, and it exists in the
217 * saved-sessions database. Or (c) only one
218 * string and it _doesn't_ exist in the
221 char *r = strrchr(p, '@');
222 if (r == p) p++, r = NULL; /* discount initial @ */
228 if (cfg.host[0] == '\0') {
229 /* No settings for this host; use defaults */
230 strncpy(cfg.host, p, sizeof(cfg.host)-1);
231 cfg.host[sizeof(cfg.host)-1] = '\0';
236 strncpy(cfg.username, p, sizeof(cfg.username)-1);
237 cfg.username[sizeof(cfg.username)-1] = '\0';
238 strncpy(cfg.host, r, sizeof(cfg.host)-1);
239 cfg.host[sizeof(cfg.host)-1] = '\0';
244 int len = sizeof(cfg.remote_cmd) - 1;
245 char *cp = cfg.remote_cmd;
248 strncpy(cp, p, len); cp[len] = '\0';
249 len2 = strlen(cp); len -= len2; cp += len2;
253 strncpy(cp, *++argv, len); cp[len] = '\0';
254 len2 = strlen(cp); len -= len2; cp += len2;
256 cfg.nopty = TRUE; /* command => no terminal */
257 cfg.ldisc_term = TRUE; /* use stdin like a line buffer */
258 break; /* done with cmdline */
266 if (portnumber != -1)
267 cfg.port = portnumber;
269 if (!*cfg.remote_cmd)
270 flags |= FLAG_INTERACTIVE;
273 * Select protocol. This is farmed out into a table in a
274 * separate file to enable an ssh-free variant.
279 for (i = 0; backends[i].backend != NULL; i++)
280 if (backends[i].protocol == cfg.protocol) {
281 back = backends[i].backend;
285 fprintf(stderr, "Internal fault: Unsupported protocol found\n");
291 * Initialise WinSock.
293 winsock_ver = MAKEWORD(2, 0);
294 if (WSAStartup(winsock_ver, &wsadata)) {
295 MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error",
296 MB_OK | MB_ICONEXCLAMATION);
299 if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 0) {
300 MessageBox(NULL, "WinSock version is incompatible with 2.0",
301 "WinSock Error", MB_OK | MB_ICONEXCLAMATION);
307 * Start up the connection.
313 error = back->init (NULL, cfg.host, cfg.port, &realhost);
315 fprintf(stderr, "Unable to open connection:\n%s", error);
320 netevent = CreateEvent(NULL, FALSE, FALSE, NULL);
321 stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL);
323 GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &orig_console_mode);
324 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);
325 outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
328 * Now we must send the back end oodles of stuff.
330 socket = back->socket();
332 * Turn off ECHO and LINE input modes. We don't care if this
333 * call fails, because we know we aren't necessarily running in
336 WSAEventSelect(socket, netevent, FD_READ | FD_CLOSE);
337 handles[0] = netevent;
338 handles[1] = stdinevent;
342 n = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
344 WSANETWORKEVENTS things;
345 if (!WSAEnumNetworkEvents(socket, netevent, &things)) {
346 if (things.lNetworkEvents & FD_READ)
347 back->msg(0, FD_READ);
348 if (things.lNetworkEvents & FD_CLOSE) {
349 back->msg(0, FD_CLOSE);
354 if (!sending && back->sendok()) {
356 * Create a separate thread to read from stdin.
357 * This is a total pain, but I can't find another
360 * - an overlapped ReadFile or ReadFileEx just
361 * doesn't happen; we get failure from
362 * ReadFileEx, and ReadFile blocks despite being
363 * given an OVERLAPPED structure. Perhaps we
364 * can't do overlapped reads on consoles. WHY
367 * - WaitForMultipleObjects(netevent, console)
368 * doesn't work, because it signals the console
369 * when _anything_ happens, including mouse
370 * motions and other things that don't cause
371 * data to be readable - so we're back to
374 idata.event = stdinevent;
375 if (!CreateThread(NULL, 0, stdin_read_thread,
376 &idata, 0, &threadid)) {
377 fprintf(stderr, "Unable to create second thread\n");
384 back->send(idata.buffer, idata.len);
386 back->special(TS_EOF);