2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
15 #include <sys/ioctl.h>
17 /* More helpful version of the FD_SET macro, to also handle maxfd. */
18 #define FD_SET_MAX(fd, max, set) do { \
20 if (max < fd + 1) max = fd + 1; \
23 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
28 #define MAX_STDIN_BACKLOG 4096
30 void fatalbox(char *p
, ...)
33 fprintf(stderr
, "FATAL ERROR: ");
35 vfprintf(stderr
, p
, ap
);
40 void modalfatalbox(char *p
, ...)
43 fprintf(stderr
, "FATAL ERROR: ");
45 vfprintf(stderr
, p
, ap
);
50 void connection_fatal(void *frontend
, char *p
, ...)
53 fprintf(stderr
, "FATAL ERROR: ");
55 vfprintf(stderr
, p
, ap
);
60 void cmdline_error(char *p
, ...)
63 fprintf(stderr
, "plink: ");
65 vfprintf(stderr
, p
, ap
);
71 struct termios orig_termios
;
74 static void *backhandle
;
78 * Default settings that are specific to pterm.
80 char *platform_default_s(const char *name
)
82 if (!strcmp(name
, "X11Display"))
83 return getenv("DISPLAY");
84 if (!strcmp(name
, "TermType"))
85 return getenv("TERM");
86 if (!strcmp(name
, "UserName")) {
88 * Remote login username will default to the local username.
92 char *user
, *ret
= NULL
;
95 * First, find who we think we are using getlogin. If this
96 * agrees with our uid, we'll go along with it. This should
97 * allow sharing of uids between several login names whilst
98 * coping correctly with people who have su'ed.
106 if (p
&& p
->pw_uid
== uid
) {
108 * The result of getlogin() really does correspond to
114 * If that didn't work, for whatever reason, we'll do
115 * the simpler version: look up our uid in the password
116 * file and map it straight to a name.
128 int platform_default_i(const char *name
, int def
)
130 if (!strcmp(name
, "TermWidth") ||
131 !strcmp(name
, "TermHeight")) {
133 if (ioctl(0, TIOCGWINSZ
, (void *)&size
) >= 0)
134 return (!strcmp(name
, "TermWidth") ? size
.ws_col
: size
.ws_row
);
139 FontSpec
platform_default_fontspec(const char *name
)
146 Filename
platform_default_filename(const char *name
)
149 if (!strcmp(name
, "LogFileName"))
150 strcpy(ret
.path
, "putty.log");
156 char *x_get_default(const char *key
)
158 return NULL
; /* this is a stub */
160 int term_ldisc(Terminal
*term
, int mode
)
164 void ldisc_update(void *frontend
, int echo
, int edit
)
166 /* Update stdin read mode to reflect changes in line discipline. */
172 mode
.c_lflag
|= ECHO
;
174 mode
.c_lflag
&= ~ECHO
;
177 mode
.c_lflag
|= ISIG
| ICANON
;
179 mode
.c_lflag
&= ~(ISIG
| ICANON
);
181 tcsetattr(0, TCSANOW
, &mode
);
184 void cleanup_termios(void)
186 tcsetattr(0, TCSANOW
, &orig_termios
);
189 bufchain stdout_data
, stderr_data
;
191 void try_output(int is_stderr
)
193 bufchain
*chain
= (is_stderr ?
&stderr_data
: &stdout_data
);
194 int fd
= (is_stderr ?
2 : 1);
198 if (bufchain_size(chain
) == 0)
201 bufchain_prefix(chain
, &senddata
, &sendlen
);
202 ret
= write(fd
, senddata
, sendlen
);
204 bufchain_consume(chain
, ret
);
206 perror(is_stderr ?
"stderr: write" : "stdout: write");
211 int from_backend(void *frontend_handle
, int is_stderr
,
212 const char *data
, int len
)
219 bufchain_add(&stderr_data
, data
, len
);
222 bufchain_add(&stdout_data
, data
, len
);
226 osize
= bufchain_size(&stdout_data
);
227 esize
= bufchain_size(&stderr_data
);
229 return osize
+ esize
;
234 void sigwinch(int signum
)
236 write(signalpipe
[1], "x", 1);
240 * Short description of parameters.
242 static void usage(void)
244 printf("PuTTY Link: command-line connection utility\n");
246 printf("Usage: plink [options] [user@]host [command]\n");
247 printf(" (\"host\" can also be a PuTTY saved session name)\n");
248 printf("Options:\n");
249 printf(" -v show verbose messages\n");
250 printf(" -load sessname Load settings from saved session\n");
251 printf(" -ssh -telnet -rlogin -raw\n");
252 printf(" force use of a particular protocol (default SSH)\n");
253 printf(" -P port connect to specified port\n");
254 printf(" -l user connect with specified username\n");
255 printf(" -m file read remote command(s) from file\n");
256 printf(" -batch disable all interactive prompts\n");
257 printf("The following options only apply to SSH connections:\n");
258 printf(" -pw passw login with specified password\n");
259 printf(" -L listen-port:host:port Forward local port to "
261 printf(" -R listen-port:host:port Forward remote port to"
263 printf(" -X -x enable / disable X11 forwarding\n");
264 printf(" -A -a enable / disable agent forwarding\n");
265 printf(" -t -T enable / disable pty allocation\n");
266 printf(" -1 -2 force use of particular protocol version\n");
267 printf(" -C enable compression\n");
268 printf(" -i key private key file for authentication\n");
272 int main(int argc
, char **argv
)
278 int i
, skcount
, sksize
, socketstate
;
284 ssh_get_line
= console_get_line
;
287 skcount
= sksize
= 0;
289 * Initialise port and protocol to sensible defaults. (These
290 * will be overridden by more or less anything.)
292 default_protocol
= PROT_SSH
;
297 * Process the command line.
299 do_defaults(NULL
, &cfg
);
300 default_protocol
= cfg
.protocol
;
301 default_port
= cfg
.port
;
305 * Override the default protocol if PLINK_PROTOCOL is set.
307 char *p
= getenv("PLINK_PROTOCOL");
310 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
311 if (!strcmp(backends
[i
].name
, p
)) {
312 default_protocol
= cfg
.protocol
= backends
[i
].protocol
;
313 default_port
= cfg
.port
=
314 backends
[i
].backend
->default_port
;
323 int ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
327 "plink: option \"%s\" requires an argument\n", p
);
329 } else if (ret
== 2) {
331 } else if (ret
== 1) {
333 } else if (!strcmp(p
, "-batch")) {
334 console_batch_mode
= 1;
335 } else if (!strcmp(p
, "-o")) {
338 "plink: option \"-o\" requires an argument\n");
342 provide_xrm_string(*++argv
);
345 fprintf(stderr
, "plink: unknown option \"%s\"\n", p
);
352 do_defaults(NULL
, &cfg
);
355 * If the hostname starts with "telnet:", set the
356 * protocol to Telnet and process the string as a
359 if (!strncmp(q
, "telnet:", 7)) {
363 if (q
[0] == '/' && q
[1] == '/')
365 cfg
.protocol
= PROT_TELNET
;
367 while (*p
&& *p
!= ':' && *p
!= '/')
376 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
377 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
381 * Before we process the [user@]host string, we
382 * first check for the presence of a protocol
383 * prefix (a protocol name followed by ",").
388 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
389 j
= strlen(backends
[i
].name
);
391 !memcmp(backends
[i
].name
, p
, j
)) {
392 default_protocol
= cfg
.protocol
=
393 backends
[i
].protocol
;
395 backends
[i
].backend
->default_port
;
403 * Three cases. Either (a) there's a nonzero
404 * length string followed by an @, in which
405 * case that's user and the remainder is host.
406 * Or (b) there's only one string, not counting
407 * a potential initial @, and it exists in the
408 * saved-sessions database. Or (c) only one
409 * string and it _doesn't_ exist in the
414 p
++, r
= NULL
; /* discount initial @ */
420 do_defaults(p
, &cfg2
);
421 if (cfg2
.host
[0] == '\0') {
422 /* No settings for this host; use defaults */
423 strncpy(cfg
.host
, p
, sizeof(cfg
.host
) - 1);
424 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
425 cfg
.port
= default_port
;
428 cfg
.remote_cmd_ptr
= cfg
.remote_cmd
;
432 strncpy(cfg
.username
, p
, sizeof(cfg
.username
) - 1);
433 cfg
.username
[sizeof(cfg
.username
) - 1] = '\0';
434 strncpy(cfg
.host
, r
, sizeof(cfg
.host
) - 1);
435 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
436 cfg
.port
= default_port
;
442 cmdlen
= cmdsize
= 0;
447 if (cmdlen
>= cmdsize
) {
448 cmdsize
= cmdlen
+ 512;
449 command
= srealloc(command
, cmdsize
);
451 command
[cmdlen
++]=*p
++;
453 if (cmdlen
>= cmdsize
) {
454 cmdsize
= cmdlen
+ 512;
455 command
= srealloc(command
, cmdsize
);
457 command
[cmdlen
++]=' '; /* always add trailing space */
458 if (--argc
) p
= *++argv
;
460 if (cmdlen
) command
[--cmdlen
]='\0';
461 /* change trailing blank to NUL */
462 cfg
.remote_cmd_ptr
= command
;
463 cfg
.remote_cmd_ptr2
= NULL
;
464 cfg
.nopty
= TRUE
; /* command => no terminal */
466 break; /* done with cmdline */
479 * Trim leading whitespace off the hostname if it's there.
482 int space
= strspn(cfg
.host
, " \t");
483 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
486 /* See if host is of the form user@host */
487 if (cfg
.host
[0] != '\0') {
488 char *atsign
= strchr(cfg
.host
, '@');
489 /* Make sure we're not overflowing the user field */
491 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
492 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
493 cfg
.username
[atsign
- cfg
.host
] = '\0';
495 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
500 * Perform command-line overrides on session configuration.
502 cmdline_run_saved(&cfg
);
505 * Trim a colon suffix off the hostname if it's there.
507 cfg
.host
[strcspn(cfg
.host
, ":")] = '\0';
510 * Remove any remaining whitespace from the hostname.
514 while (cfg
.host
[p2
] != '\0') {
515 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
516 cfg
.host
[p1
] = cfg
.host
[p2
];
524 if (!*cfg
.remote_cmd_ptr
)
525 flags
|= FLAG_INTERACTIVE
;
528 * Select protocol. This is farmed out into a table in a
529 * separate file to enable an ssh-free variant.
534 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
535 if (backends
[i
].protocol
== cfg
.protocol
) {
536 back
= backends
[i
].backend
;
541 "Internal fault: Unsupported protocol found\n");
549 if (portnumber
!= -1)
550 cfg
.port
= portnumber
;
553 * Set up the pipe we'll use to tell us about SIGWINCH.
555 if (pipe(signalpipe
) < 0) {
559 putty_signal(SIGWINCH
, sigwinch
);
564 * Start up the connection.
566 logctx
= log_init(NULL
, &cfg
);
570 /* nodelay is only useful if stdin is a terminal device */
571 int nodelay
= cfg
.tcp_nodelay
&& isatty(0);
573 error
= back
->init(NULL
, &backhandle
, &cfg
, cfg
.host
, cfg
.port
,
576 fprintf(stderr
, "Unable to open connection:\n%s\n", error
);
579 back
->provide_logctx(backhandle
, logctx
);
580 ldisc
= ldisc_create(&cfg
, NULL
, back
, backhandle
, NULL
);
586 * Set up the initial console mode. We don't care if this call
587 * fails, because we know we aren't necessarily running in a
590 tcgetattr(0, &orig_termios
);
591 atexit(cleanup_termios
);
592 ldisc_update(NULL
, 1, 1);
596 fd_set rset
, wset
, xset
;
606 FD_SET_MAX(signalpipe
[0], maxfd
, rset
);
608 if (connopen
&& !sending
&&
609 back
->socket(backhandle
) != NULL
&&
610 back
->sendok(backhandle
) &&
611 back
->sendbuffer(backhandle
) < MAX_STDIN_BACKLOG
) {
612 /* If we're OK to send, then try to read from stdin. */
613 FD_SET_MAX(0, maxfd
, rset
);
616 if (bufchain_size(&stdout_data
) > 0) {
617 /* If we have data for stdout, try to write to stdout. */
618 FD_SET_MAX(1, maxfd
, wset
);
621 if (bufchain_size(&stderr_data
) > 0) {
622 /* If we have data for stderr, try to write to stderr. */
623 FD_SET_MAX(2, maxfd
, wset
);
626 /* Count the currently active sockets. */
628 for (socket
= first_socket(&socketstate
, &rwx
); socket
>= 0;
629 socket
= next_socket(&socketstate
, &rwx
)) i
++;
631 /* Expand the sklist buffer if necessary. */
634 sklist
= srealloc(sklist
, sksize
* sizeof(*sklist
));
638 * Add all currently open sockets to the select sets, and
639 * store them in sklist as well.
642 for (socket
= first_socket(&socketstate
, &rwx
); socket
>= 0;
643 socket
= next_socket(&socketstate
, &rwx
)) {
644 sklist
[skcount
++] = socket
;
646 FD_SET_MAX(socket
, maxfd
, rset
);
648 FD_SET_MAX(socket
, maxfd
, wset
);
650 FD_SET_MAX(socket
, maxfd
, xset
);
654 ret
= select(maxfd
, &rset
, &wset
, &xset
, NULL
);
655 } while (ret
< 0 && errno
== EINTR
);
662 for (i
= 0; i
< skcount
; i
++) {
665 * We must process exceptional notifications before
666 * ordinary readability ones, or we may go straight
667 * past the urgent marker.
669 if (FD_ISSET(socket
, &xset
))
670 select_result(socket
, 4);
671 if (FD_ISSET(socket
, &rset
))
672 select_result(socket
, 1);
673 if (FD_ISSET(socket
, &wset
))
674 select_result(socket
, 2);
677 if (FD_ISSET(signalpipe
[0], &rset
)) {
680 read(signalpipe
[0], c
, 1); /* ignore its value; it'll be `x' */
681 if (ioctl(0, TIOCGWINSZ
, (void *)&size
) >= 0)
682 back
->size(backhandle
, size
.ws_col
, size
.ws_row
);
685 if (FD_ISSET(0, &rset
)) {
689 if (connopen
&& back
->socket(backhandle
) != NULL
) {
690 ret
= read(0, buf
, sizeof(buf
));
692 perror("stdin: read");
694 } else if (ret
== 0) {
695 back
->special(backhandle
, TS_EOF
);
696 sending
= FALSE
; /* send nothing further after this */
698 back
->send(backhandle
, buf
, ret
);
703 if (FD_ISSET(1, &wset
)) {
707 if (FD_ISSET(2, &wset
)) {
711 if ((!connopen
|| back
->socket(backhandle
) == NULL
) &&
712 bufchain_size(&stdout_data
) == 0 &&
713 bufchain_size(&stderr_data
) == 0)
714 break; /* we closed the connection */
716 exitcode
= back
->exitcode(backhandle
);
718 fprintf(stderr
, "Remote process exit code unavailable\n");
719 exitcode
= 1; /* this is an error condition */
721 cleanup_exit(exitcode
);
722 return exitcode
; /* shouldn't happen, but placates gcc */