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
;
77 * Default settings that are specific to pterm.
79 char *platform_default_s(char *name
)
81 if (!strcmp(name
, "X11Display"))
82 return getenv("DISPLAY");
83 if (!strcmp(name
, "TermType"))
84 return getenv("TERM");
85 if (!strcmp(name
, "UserName")) {
87 * Remote login username will default to the local username.
91 char *user
, *ret
= NULL
;
94 * First, find who we think we are using getlogin. If this
95 * agrees with our uid, we'll go along with it. This should
96 * allow sharing of uids between several login names whilst
97 * coping correctly with people who have su'ed.
105 if (p
&& p
->pw_uid
== uid
) {
107 * The result of getlogin() really does correspond to
113 * If that didn't work, for whatever reason, we'll do
114 * the simpler version: look up our uid in the password
115 * file and map it straight to a name.
127 int platform_default_i(char *name
, int def
)
129 if (!strcmp(name
, "TermWidth") ||
130 !strcmp(name
, "TermHeight")) {
132 if (ioctl(0, TIOCGWINSZ
, (void *)&size
) >= 0)
133 return (!strcmp(name
, "TermWidth") ? size
.ws_col
: size
.ws_row
);
138 char *x_get_default(char *key
)
140 return NULL
; /* this is a stub */
142 int term_ldisc(Terminal
*term
, int mode
)
146 void ldisc_update(void *frontend
, int echo
, int edit
)
148 /* Update stdin read mode to reflect changes in line discipline. */
154 mode
.c_lflag
|= ECHO
;
156 mode
.c_lflag
&= ~ECHO
;
159 mode
.c_lflag
|= ISIG
| ICANON
;
161 mode
.c_lflag
&= ~(ISIG
| ICANON
);
163 tcsetattr(0, TCSANOW
, &mode
);
166 void cleanup_termios(void)
168 tcsetattr(0, TCSANOW
, &orig_termios
);
171 bufchain stdout_data
, stderr_data
;
173 void try_output(int is_stderr
)
175 bufchain
*chain
= (is_stderr ?
&stderr_data
: &stdout_data
);
176 int fd
= (is_stderr ?
2 : 1);
180 if (bufchain_size(chain
) == 0)
183 bufchain_prefix(chain
, &senddata
, &sendlen
);
184 ret
= write(fd
, senddata
, sendlen
);
186 bufchain_consume(chain
, ret
);
188 perror(is_stderr ?
"stderr: write" : "stdout: write");
193 int from_backend(void *frontend_handle
, int is_stderr
, char *data
, int len
)
200 bufchain_add(&stderr_data
, data
, len
);
203 bufchain_add(&stdout_data
, data
, len
);
207 osize
= bufchain_size(&stdout_data
);
208 esize
= bufchain_size(&stderr_data
);
210 return osize
+ esize
;
215 void sigwinch(int signum
)
217 write(signalpipe
[1], "x", 1);
221 * Short description of parameters.
223 static void usage(void)
225 printf("PuTTY Link: command-line connection utility\n");
227 printf("Usage: plink [options] [user@]host [command]\n");
228 printf(" (\"host\" can also be a PuTTY saved session name)\n");
229 printf("Options:\n");
230 printf(" -v show verbose messages\n");
231 printf(" -load sessname Load settings from saved session\n");
232 printf(" -ssh -telnet -rlogin -raw\n");
233 printf(" force use of a particular protocol (default SSH)\n");
234 printf(" -P port connect to specified port\n");
235 printf(" -l user connect with specified username\n");
236 printf(" -m file read remote command(s) from file\n");
237 printf(" -batch disable all interactive prompts\n");
238 printf("The following options only apply to SSH connections:\n");
239 printf(" -pw passw login with specified password\n");
240 printf(" -L listen-port:host:port Forward local port to "
242 printf(" -R listen-port:host:port Forward remote port to"
244 printf(" -X -x enable / disable X11 forwarding\n");
245 printf(" -A -a enable / disable agent forwarding\n");
246 printf(" -t -T enable / disable pty allocation\n");
247 printf(" -1 -2 force use of particular protocol version\n");
248 printf(" -C enable compression\n");
249 printf(" -i key private key file for authentication\n");
253 int main(int argc
, char **argv
)
259 int i
, skcount
, sksize
, socketstate
;
265 ssh_get_line
= console_get_line
;
268 skcount
= sksize
= 0;
270 * Initialise port and protocol to sensible defaults. (These
271 * will be overridden by more or less anything.)
273 default_protocol
= PROT_SSH
;
278 * Process the command line.
280 do_defaults(NULL
, &cfg
);
281 default_protocol
= cfg
.protocol
;
282 default_port
= cfg
.port
;
286 * Override the default protocol if PLINK_PROTOCOL is set.
288 char *p
= getenv("PLINK_PROTOCOL");
291 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
292 if (!strcmp(backends
[i
].name
, p
)) {
293 default_protocol
= cfg
.protocol
= backends
[i
].protocol
;
294 default_port
= cfg
.port
=
295 backends
[i
].backend
->default_port
;
304 int ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
308 "plink: option \"%s\" requires an argument\n", p
);
310 } else if (ret
== 2) {
312 } else if (ret
== 1) {
314 } else if (!strcmp(p
, "-batch")) {
315 console_batch_mode
= 1;
316 } else if (!strcmp(p
, "-o")) {
319 "plink: option \"-o\" requires an argument\n");
323 provide_xrm_string(*++argv
);
326 fprintf(stderr
, "plink: unknown option \"%s\"\n", p
);
333 do_defaults(NULL
, &cfg
);
336 * If the hostname starts with "telnet:", set the
337 * protocol to Telnet and process the string as a
340 if (!strncmp(q
, "telnet:", 7)) {
344 if (q
[0] == '/' && q
[1] == '/')
346 cfg
.protocol
= PROT_TELNET
;
348 while (*p
&& *p
!= ':' && *p
!= '/')
357 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
358 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
362 * Before we process the [user@]host string, we
363 * first check for the presence of a protocol
364 * prefix (a protocol name followed by ",").
369 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
370 j
= strlen(backends
[i
].name
);
372 !memcmp(backends
[i
].name
, p
, j
)) {
373 default_protocol
= cfg
.protocol
=
374 backends
[i
].protocol
;
376 backends
[i
].backend
->default_port
;
384 * Three cases. Either (a) there's a nonzero
385 * length string followed by an @, in which
386 * case that's user and the remainder is host.
387 * Or (b) there's only one string, not counting
388 * a potential initial @, and it exists in the
389 * saved-sessions database. Or (c) only one
390 * string and it _doesn't_ exist in the
395 p
++, r
= NULL
; /* discount initial @ */
401 do_defaults(p
, &cfg2
);
402 if (cfg2
.host
[0] == '\0') {
403 /* No settings for this host; use defaults */
404 strncpy(cfg
.host
, p
, sizeof(cfg
.host
) - 1);
405 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
406 cfg
.port
= default_port
;
409 cfg
.remote_cmd_ptr
= cfg
.remote_cmd
;
413 strncpy(cfg
.username
, p
, sizeof(cfg
.username
) - 1);
414 cfg
.username
[sizeof(cfg
.username
) - 1] = '\0';
415 strncpy(cfg
.host
, r
, sizeof(cfg
.host
) - 1);
416 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
417 cfg
.port
= default_port
;
423 cmdlen
= cmdsize
= 0;
428 if (cmdlen
>= cmdsize
) {
429 cmdsize
= cmdlen
+ 512;
430 command
= srealloc(command
, cmdsize
);
432 command
[cmdlen
++]=*p
++;
434 if (cmdlen
>= cmdsize
) {
435 cmdsize
= cmdlen
+ 512;
436 command
= srealloc(command
, cmdsize
);
438 command
[cmdlen
++]=' '; /* always add trailing space */
439 if (--argc
) p
= *++argv
;
441 if (cmdlen
) command
[--cmdlen
]='\0';
442 /* change trailing blank to NUL */
443 cfg
.remote_cmd_ptr
= command
;
444 cfg
.remote_cmd_ptr2
= NULL
;
445 cfg
.nopty
= TRUE
; /* command => no terminal */
447 break; /* done with cmdline */
460 * Trim leading whitespace off the hostname if it's there.
463 int space
= strspn(cfg
.host
, " \t");
464 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
467 /* See if host is of the form user@host */
468 if (cfg
.host
[0] != '\0') {
469 char *atsign
= strchr(cfg
.host
, '@');
470 /* Make sure we're not overflowing the user field */
472 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
473 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
474 cfg
.username
[atsign
- cfg
.host
] = '\0';
476 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
481 * Perform command-line overrides on session configuration.
483 cmdline_run_saved(&cfg
);
486 * Trim a colon suffix off the hostname if it's there.
488 cfg
.host
[strcspn(cfg
.host
, ":")] = '\0';
491 * Remove any remaining whitespace from the hostname.
495 while (cfg
.host
[p2
] != '\0') {
496 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
497 cfg
.host
[p1
] = cfg
.host
[p2
];
505 if (!*cfg
.remote_cmd_ptr
)
506 flags
|= FLAG_INTERACTIVE
;
509 * Select protocol. This is farmed out into a table in a
510 * separate file to enable an ssh-free variant.
515 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
516 if (backends
[i
].protocol
== cfg
.protocol
) {
517 back
= backends
[i
].backend
;
522 "Internal fault: Unsupported protocol found\n");
530 if (portnumber
!= -1)
531 cfg
.port
= portnumber
;
534 * Set up the pipe we'll use to tell us about SIGWINCH.
536 if (pipe(signalpipe
) < 0) {
540 putty_signal(SIGWINCH
, sigwinch
);
545 * Start up the connection.
547 logctx
= log_init(NULL
);
551 /* nodelay is only useful if stdin is a terminal device */
552 int nodelay
= cfg
.tcp_nodelay
&& isatty(0);
554 error
= back
->init(NULL
, &backhandle
, cfg
.host
, cfg
.port
,
557 fprintf(stderr
, "Unable to open connection:\n%s\n", error
);
560 back
->provide_logctx(backhandle
, logctx
);
561 ldisc
= ldisc_create(&cfg
, NULL
, back
, backhandle
, NULL
);
567 * Set up the initial console mode. We don't care if this call
568 * fails, because we know we aren't necessarily running in a
571 tcgetattr(0, &orig_termios
);
572 atexit(cleanup_termios
);
573 ldisc_update(NULL
, 1, 1);
577 fd_set rset
, wset
, xset
;
587 FD_SET_MAX(signalpipe
[0], maxfd
, rset
);
589 if (connopen
&& !sending
&&
590 back
->socket(backhandle
) != NULL
&&
591 back
->sendok(backhandle
) &&
592 back
->sendbuffer(backhandle
) < MAX_STDIN_BACKLOG
) {
593 /* If we're OK to send, then try to read from stdin. */
594 FD_SET_MAX(0, maxfd
, rset
);
597 if (bufchain_size(&stdout_data
) > 0) {
598 /* If we have data for stdout, try to write to stdout. */
599 FD_SET_MAX(1, maxfd
, wset
);
602 if (bufchain_size(&stderr_data
) > 0) {
603 /* If we have data for stderr, try to write to stderr. */
604 FD_SET_MAX(2, maxfd
, wset
);
607 /* Count the currently active sockets. */
609 for (socket
= first_socket(&socketstate
, &rwx
); socket
>= 0;
610 socket
= next_socket(&socketstate
, &rwx
)) i
++;
612 /* Expand the sklist buffer if necessary. */
615 sklist
= srealloc(sklist
, sksize
* sizeof(*sklist
));
619 * Add all currently open sockets to the select sets, and
620 * store them in sklist as well.
623 for (socket
= first_socket(&socketstate
, &rwx
); socket
>= 0;
624 socket
= next_socket(&socketstate
, &rwx
)) {
625 sklist
[skcount
++] = socket
;
627 FD_SET_MAX(socket
, maxfd
, rset
);
629 FD_SET_MAX(socket
, maxfd
, wset
);
631 FD_SET_MAX(socket
, maxfd
, xset
);
635 ret
= select(maxfd
, &rset
, &wset
, &xset
, NULL
);
636 } while (ret
< 0 && errno
== EINTR
);
643 for (i
= 0; i
< skcount
; i
++) {
646 * We must process exceptional notifications before
647 * ordinary readability ones, or we may go straight
648 * past the urgent marker.
650 if (FD_ISSET(socket
, &xset
))
651 select_result(socket
, 4);
652 if (FD_ISSET(socket
, &rset
))
653 select_result(socket
, 1);
654 if (FD_ISSET(socket
, &wset
))
655 select_result(socket
, 2);
658 if (FD_ISSET(signalpipe
[0], &rset
)) {
661 read(signalpipe
[0], c
, 1); /* ignore its value; it'll be `x' */
662 if (ioctl(0, TIOCGWINSZ
, (void *)&size
) >= 0)
663 back
->size(backhandle
, size
.ws_col
, size
.ws_row
);
666 if (FD_ISSET(0, &rset
)) {
670 if (connopen
&& back
->socket(backhandle
) != NULL
) {
671 ret
= read(0, buf
, sizeof(buf
));
673 perror("stdin: read");
675 } else if (ret
== 0) {
676 back
->special(backhandle
, TS_EOF
);
677 sending
= FALSE
; /* send nothing further after this */
679 back
->send(backhandle
, buf
, ret
);
684 if (FD_ISSET(1, &wset
)) {
688 if (FD_ISSET(2, &wset
)) {
692 if ((!connopen
|| back
->socket(backhandle
) == NULL
) &&
693 bufchain_size(&stdout_data
) == 0 &&
694 bufchain_size(&stderr_data
) == 0)
695 break; /* we closed the connection */
697 exitcode
= back
->exitcode(backhandle
);
699 fprintf(stderr
, "Remote process exit code unavailable\n");
700 exitcode
= 1; /* this is an error condition */
702 cleanup_exit(exitcode
);
703 return exitcode
; /* shouldn't happen, but placates gcc */