2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
13 /* More helpful version of the FD_SET macro, to also handle maxfd. */
14 #define FD_SET_MAX(fd, max, set) do { \
16 if (max < fd + 1) max = fd + 1; \
19 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
24 #define MAX_STDIN_BACKLOG 4096
26 void fatalbox(char *p, ...)
29 fprintf(stderr, "FATAL ERROR: ");
31 vfprintf(stderr, p, ap);
36 void modalfatalbox(char *p, ...)
39 fprintf(stderr, "FATAL ERROR: ");
41 vfprintf(stderr, p, ap);
46 void connection_fatal(void *frontend, char *p, ...)
49 fprintf(stderr, "FATAL ERROR: ");
51 vfprintf(stderr, p, ap);
56 void cmdline_error(char *p, ...)
59 fprintf(stderr, "plink: ");
61 vfprintf(stderr, p, ap);
67 struct termios orig_termios;
70 static void *backhandle;
72 char *x_get_default(char *key)
74 return NULL; /* this is a stub */
76 int term_ldisc(Terminal *term, int mode)
80 void ldisc_update(void *frontend, int echo, int edit)
82 /* Update stdin read mode to reflect changes in line discipline. */
90 mode.c_lflag &= ~ECHO;
93 mode.c_lflag |= ISIG | ICANON;
95 mode.c_lflag &= ~(ISIG | ICANON);
97 tcsetattr(0, TCSANOW, &mode);
100 void cleanup_termios(void)
102 tcsetattr(0, TCSANOW, &orig_termios);
105 bufchain stdout_data, stderr_data;
107 void try_output(int is_stderr)
109 bufchain *chain = (is_stderr ? &stderr_data : &stdout_data);
110 int fd = (is_stderr ? 2 : 1);
114 if (bufchain_size(chain) == 0)
117 bufchain_prefix(chain, &senddata, &sendlen);
118 ret = write(fd, senddata, sendlen);
120 bufchain_consume(chain, ret);
122 perror(is_stderr ? "stderr: write" : "stdout: write");
127 int from_backend(void *frontend_handle, int is_stderr, char *data, int len)
134 bufchain_add(&stderr_data, data, len);
137 bufchain_add(&stdout_data, data, len);
141 osize = bufchain_size(&stdout_data);
142 esize = bufchain_size(&stderr_data);
144 return osize + esize;
148 * Short description of parameters.
150 static void usage(void)
152 printf("PuTTY Link: command-line connection utility\n");
154 printf("Usage: plink [options] [user@]host [command]\n");
155 printf(" (\"host\" can also be a PuTTY saved session name)\n");
156 printf("Options:\n");
157 printf(" -v show verbose messages\n");
158 printf(" -load sessname Load settings from saved session\n");
159 printf(" -ssh -telnet -rlogin -raw\n");
160 printf(" force use of a particular protocol (default SSH)\n");
161 printf(" -P port connect to specified port\n");
162 printf(" -l user connect with specified username\n");
163 printf(" -m file read remote command(s) from file\n");
164 printf(" -batch disable all interactive prompts\n");
165 printf("The following options only apply to SSH connections:\n");
166 printf(" -pw passw login with specified password\n");
167 printf(" -L listen-port:host:port Forward local port to "
169 printf(" -R listen-port:host:port Forward remote port to"
171 printf(" -X -x enable / disable X11 forwarding\n");
172 printf(" -A -a enable / disable agent forwarding\n");
173 printf(" -t -T enable / disable pty allocation\n");
174 printf(" -1 -2 force use of particular protocol version\n");
175 printf(" -C enable compression\n");
176 printf(" -i key private key file for authentication\n");
180 int main(int argc, char **argv)
186 int i, skcount, sksize, socketstate;
192 ssh_get_line = console_get_line;
195 skcount = sksize = 0;
197 * Initialise port and protocol to sensible defaults. (These
198 * will be overridden by more or less anything.)
200 default_protocol = PROT_SSH;
205 * Process the command line.
207 do_defaults(NULL, &cfg);
208 default_protocol = cfg.protocol;
209 default_port = cfg.port;
212 * Override the default protocol if PLINK_PROTOCOL is set.
214 char *p = getenv("PLINK_PROTOCOL");
217 for (i = 0; backends[i].backend != NULL; i++) {
218 if (!strcmp(backends[i].name, p)) {
219 default_protocol = cfg.protocol = backends[i].protocol;
220 default_port = cfg.port =
221 backends[i].backend->default_port;
230 int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), 1);
233 "plink: option \"%s\" requires an argument\n", p);
234 } else if (ret == 2) {
236 } else if (ret == 1) {
238 } else if (!strcmp(p, "-batch")) {
239 console_batch_mode = 1;
245 * If the hostname starts with "telnet:", set the
246 * protocol to Telnet and process the string as a
249 if (!strncmp(q, "telnet:", 7)) {
253 if (q[0] == '/' && q[1] == '/')
255 cfg.protocol = PROT_TELNET;
257 while (*p && *p != ':' && *p != '/')
266 strncpy(cfg.host, q, sizeof(cfg.host) - 1);
267 cfg.host[sizeof(cfg.host) - 1] = '\0';
271 * Before we process the [user@]host string, we
272 * first check for the presence of a protocol
273 * prefix (a protocol name followed by ",").
278 for (i = 0; backends[i].backend != NULL; i++) {
279 j = strlen(backends[i].name);
281 !memcmp(backends[i].name, p, j)) {
282 default_protocol = cfg.protocol =
283 backends[i].protocol;
285 backends[i].backend->default_port;
293 * Three cases. Either (a) there's a nonzero
294 * length string followed by an @, in which
295 * case that's user and the remainder is host.
296 * Or (b) there's only one string, not counting
297 * a potential initial @, and it exists in the
298 * saved-sessions database. Or (c) only one
299 * string and it _doesn't_ exist in the
304 p++, r = NULL; /* discount initial @ */
310 do_defaults(p, &cfg2);
311 if (cfg2.host[0] == '\0') {
312 /* No settings for this host; use defaults */
313 strncpy(cfg.host, p, sizeof(cfg.host) - 1);
314 cfg.host[sizeof(cfg.host) - 1] = '\0';
315 cfg.port = default_port;
318 cfg.remote_cmd_ptr = cfg.remote_cmd;
322 strncpy(cfg.username, p, sizeof(cfg.username) - 1);
323 cfg.username[sizeof(cfg.username) - 1] = '\0';
324 strncpy(cfg.host, r, sizeof(cfg.host) - 1);
325 cfg.host[sizeof(cfg.host) - 1] = '\0';
326 cfg.port = default_port;
332 cmdlen = cmdsize = 0;
337 if (cmdlen >= cmdsize) {
338 cmdsize = cmdlen + 512;
339 command = srealloc(command, cmdsize);
341 command[cmdlen++]=*p++;
343 if (cmdlen >= cmdsize) {
344 cmdsize = cmdlen + 512;
345 command = srealloc(command, cmdsize);
347 command[cmdlen++]=' '; /* always add trailing space */
348 if (--argc) p = *++argv;
350 if (cmdlen) command[--cmdlen]='\0';
351 /* change trailing blank to NUL */
352 cfg.remote_cmd_ptr = command;
353 cfg.remote_cmd_ptr2 = NULL;
354 cfg.nopty = TRUE; /* command => no terminal */
356 break; /* done with cmdline */
366 * Trim leading whitespace off the hostname if it's there.
369 int space = strspn(cfg.host, " \t");
370 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
373 /* See if host is of the form user@host */
374 if (cfg.host[0] != '\0') {
375 char *atsign = strchr(cfg.host, '@');
376 /* Make sure we're not overflowing the user field */
378 if (atsign - cfg.host < sizeof cfg.username) {
379 strncpy(cfg.username, cfg.host, atsign - cfg.host);
380 cfg.username[atsign - cfg.host] = '\0';
382 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
387 * Perform command-line overrides on session configuration.
392 * Trim a colon suffix off the hostname if it's there.
394 cfg.host[strcspn(cfg.host, ":")] = '\0';
397 * Remove any remaining whitespace from the hostname.
401 while (cfg.host[p2] != '\0') {
402 if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
403 cfg.host[p1] = cfg.host[p2];
411 if (!*cfg.remote_cmd_ptr)
412 flags |= FLAG_INTERACTIVE;
415 * Select protocol. This is farmed out into a table in a
416 * separate file to enable an ssh-free variant.
421 for (i = 0; backends[i].backend != NULL; i++)
422 if (backends[i].protocol == cfg.protocol) {
423 back = backends[i].backend;
428 "Internal fault: Unsupported protocol found\n");
436 if (portnumber != -1)
437 cfg.port = portnumber;
442 * Start up the connection.
447 /* nodelay is only useful if stdin is a terminal device */
448 int nodelay = cfg.tcp_nodelay && isatty(0);
450 error = back->init(NULL, &backhandle, cfg.host, cfg.port,
453 fprintf(stderr, "Unable to open connection:\n%s\n", error);
456 logctx = log_init(NULL);
457 back->provide_logctx(backhandle, logctx);
458 ldisc = ldisc_create(NULL, back, backhandle, NULL);
464 * Set up the initial console mode. We don't care if this call
465 * fails, because we know we aren't necessarily running in a
468 tcgetattr(0, &orig_termios);
469 atexit(cleanup_termios);
470 ldisc_update(NULL, 1, 1);
474 fd_set rset, wset, xset;
484 if (connopen && !sending &&
485 back->socket(backhandle) != NULL &&
486 back->sendok(backhandle) &&
487 back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) {
488 /* If we're OK to send, then try to read from stdin. */
489 FD_SET_MAX(0, maxfd, rset);
492 if (bufchain_size(&stdout_data) > 0) {
493 /* If we have data for stdout, try to write to stdout. */
494 FD_SET_MAX(1, maxfd, wset);
497 if (bufchain_size(&stderr_data) > 0) {
498 /* If we have data for stderr, try to write to stderr. */
499 FD_SET_MAX(2, maxfd, wset);
502 /* Count the currently active sockets. */
504 for (socket = first_socket(&socketstate, &rwx); socket >= 0;
505 socket = next_socket(&socketstate, &rwx)) i++;
507 /* Expand the sklist buffer if necessary. */
510 sklist = srealloc(sklist, sksize * sizeof(*sklist));
514 * Add all currently open sockets to the select sets, and
515 * store them in sklist as well.
518 for (socket = first_socket(&socketstate, &rwx); socket >= 0;
519 socket = next_socket(&socketstate, &rwx)) {
520 sklist[skcount++] = socket;
522 FD_SET_MAX(socket, maxfd, rset);
524 FD_SET_MAX(socket, maxfd, wset);
526 FD_SET_MAX(socket, maxfd, xset);
529 ret = select(maxfd, &rset, &wset, &xset, NULL);
536 for (i = 0; i < skcount; i++) {
539 * We must process exceptional notifications before
540 * ordinary readability ones, or we may go straight
541 * past the urgent marker.
543 if (FD_ISSET(socket, &xset))
544 select_result(socket, 4);
545 if (FD_ISSET(socket, &rset))
546 select_result(socket, 1);
547 if (FD_ISSET(socket, &wset))
548 select_result(socket, 2);
551 if (FD_ISSET(0, &rset)) {
555 if (connopen && back->socket(backhandle) != NULL) {
556 ret = read(0, buf, sizeof(buf));
558 perror("stdin: read");
560 } else if (ret == 0) {
561 back->special(backhandle, TS_EOF);
562 sending = FALSE; /* send nothing further after this */
564 back->send(backhandle, buf, ret);
569 if (FD_ISSET(1, &wset)) {
573 if (FD_ISSET(2, &wset)) {
577 if ((!connopen || back->socket(backhandle) == NULL) &&
578 bufchain_size(&stdout_data) == 0 &&
579 bufchain_size(&stderr_data) == 0)
580 break; /* we closed the connection */
582 exitcode = back->exitcode(backhandle);
584 fprintf(stderr, "Remote process exit code unavailable\n");
585 exitcode = 1; /* this is an error condition */