2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
15 #include <sys/ioctl.h>
17 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
22 #define MAX_STDIN_BACKLOG 4096
24 void fatalbox(char *p
, ...)
27 fprintf(stderr
, "FATAL ERROR: ");
29 vfprintf(stderr
, p
, ap
);
34 void modalfatalbox(char *p
, ...)
37 fprintf(stderr
, "FATAL ERROR: ");
39 vfprintf(stderr
, p
, ap
);
44 void connection_fatal(void *frontend
, char *p
, ...)
47 fprintf(stderr
, "FATAL ERROR: ");
49 vfprintf(stderr
, p
, ap
);
54 void cmdline_error(char *p
, ...)
57 fprintf(stderr
, "plink: ");
59 vfprintf(stderr
, p
, ap
);
65 struct termios orig_termios
;
68 static void *backhandle
;
72 * Default settings that are specific to pterm.
74 char *platform_default_s(const char *name
)
76 if (!strcmp(name
, "X11Display"))
77 return dupstr(getenv("DISPLAY"));
78 if (!strcmp(name
, "TermType"))
79 return dupstr(getenv("TERM"));
80 if (!strcmp(name
, "UserName"))
81 return get_username();
85 int platform_default_i(const char *name
, int def
)
87 if (!strcmp(name
, "TermWidth") ||
88 !strcmp(name
, "TermHeight")) {
90 if (ioctl(0, TIOCGWINSZ
, (void *)&size
) >= 0)
91 return (!strcmp(name
, "TermWidth") ? size
.ws_col
: size
.ws_row
);
96 FontSpec
platform_default_fontspec(const char *name
)
103 Filename
platform_default_filename(const char *name
)
106 if (!strcmp(name
, "LogFileName"))
107 strcpy(ret
.path
, "putty.log");
113 char *x_get_default(const char *key
)
115 return NULL
; /* this is a stub */
117 int term_ldisc(Terminal
*term
, int mode
)
121 void ldisc_update(void *frontend
, int echo
, int edit
)
123 /* Update stdin read mode to reflect changes in line discipline. */
129 mode
.c_lflag
|= ECHO
;
131 mode
.c_lflag
&= ~ECHO
;
134 mode
.c_lflag
|= ISIG
| ICANON
;
136 mode
.c_lflag
&= ~(ISIG
| ICANON
);
138 tcsetattr(0, TCSANOW
, &mode
);
141 void cleanup_termios(void)
143 tcsetattr(0, TCSANOW
, &orig_termios
);
146 bufchain stdout_data
, stderr_data
;
148 void try_output(int is_stderr
)
150 bufchain
*chain
= (is_stderr ?
&stderr_data
: &stdout_data
);
151 int fd
= (is_stderr ?
2 : 1);
155 if (bufchain_size(chain
) == 0)
158 bufchain_prefix(chain
, &senddata
, &sendlen
);
159 ret
= write(fd
, senddata
, sendlen
);
161 bufchain_consume(chain
, ret
);
163 perror(is_stderr ?
"stderr: write" : "stdout: write");
168 int from_backend(void *frontend_handle
, int is_stderr
,
169 const char *data
, int len
)
174 bufchain_add(&stderr_data
, data
, len
);
177 bufchain_add(&stdout_data
, data
, len
);
181 osize
= bufchain_size(&stdout_data
);
182 esize
= bufchain_size(&stderr_data
);
184 return osize
+ esize
;
189 void sigwinch(int signum
)
191 write(signalpipe
[1], "x", 1);
195 * In Plink our selects are synchronous, so these functions are
198 int uxsel_input_add(int fd
, int rwx
) { return 0; }
199 void uxsel_input_remove(int id
) { }
202 * Short description of parameters.
204 static void usage(void)
206 printf("PuTTY Link: command-line connection utility\n");
208 printf("Usage: plink [options] [user@]host [command]\n");
209 printf(" (\"host\" can also be a PuTTY saved session name)\n");
210 printf("Options:\n");
211 printf(" -v show verbose messages\n");
212 printf(" -load sessname Load settings from saved session\n");
213 printf(" -ssh -telnet -rlogin -raw\n");
214 printf(" force use of a particular protocol (default SSH)\n");
215 printf(" -P port connect to specified port\n");
216 printf(" -l user connect with specified username\n");
217 printf(" -m file read remote command(s) from file\n");
218 printf(" -batch disable all interactive prompts\n");
219 printf("The following options only apply to SSH connections:\n");
220 printf(" -pw passw login with specified password\n");
221 printf(" -D [listen-IP:]listen-port\n");
222 printf(" Dynamic SOCKS-based port forwarding\n");
223 printf(" -L [listen-IP:]listen-port:host:port\n");
224 printf(" Forward local port to remote address\n");
225 printf(" -R [listen-IP:]listen-port:host:port\n");
226 printf(" Forward remote port to local address\n");
227 printf(" -X -x enable / disable X11 forwarding\n");
228 printf(" -A -a enable / disable agent forwarding\n");
229 printf(" -t -T enable / disable pty allocation\n");
230 printf(" -1 -2 force use of particular protocol version\n");
231 printf(" -C enable compression\n");
232 printf(" -i key private key file for authentication\n");
233 printf(" -s remote command is an SSH subsystem (SSH-2 only)\n");
237 int main(int argc
, char **argv
)
243 int i
, fdcount
, fdsize
, fdstate
;
247 int use_subsystem
= 0;
248 void *ldisc
, *logctx
;
250 ssh_get_line
= console_get_line
;
253 fdcount
= fdsize
= 0;
255 * Initialise port and protocol to sensible defaults. (These
256 * will be overridden by more or less anything.)
258 default_protocol
= PROT_SSH
;
263 * Process the command line.
265 do_defaults(NULL
, &cfg
);
266 default_protocol
= cfg
.protocol
;
267 default_port
= cfg
.port
;
271 * Override the default protocol if PLINK_PROTOCOL is set.
273 char *p
= getenv("PLINK_PROTOCOL");
276 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
277 if (!strcmp(backends
[i
].name
, p
)) {
278 default_protocol
= cfg
.protocol
= backends
[i
].protocol
;
279 default_port
= cfg
.port
=
280 backends
[i
].backend
->default_port
;
289 int ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
293 "plink: option \"%s\" requires an argument\n", p
);
295 } else if (ret
== 2) {
297 } else if (ret
== 1) {
299 } else if (!strcmp(p
, "-batch")) {
300 console_batch_mode
= 1;
301 } else if (!strcmp(p
, "-s")) {
302 /* Save status to write to cfg later. */
304 } else if (!strcmp(p
, "-o")) {
307 "plink: option \"-o\" requires an argument\n");
311 provide_xrm_string(*++argv
);
314 fprintf(stderr
, "plink: unknown option \"%s\"\n", p
);
321 do_defaults(NULL
, &cfg
);
324 * If the hostname starts with "telnet:", set the
325 * protocol to Telnet and process the string as a
328 if (!strncmp(q
, "telnet:", 7)) {
332 if (q
[0] == '/' && q
[1] == '/')
334 cfg
.protocol
= PROT_TELNET
;
336 while (*p
&& *p
!= ':' && *p
!= '/')
345 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
346 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
350 * Before we process the [user@]host string, we
351 * first check for the presence of a protocol
352 * prefix (a protocol name followed by ",").
357 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
358 j
= strlen(backends
[i
].name
);
360 !memcmp(backends
[i
].name
, p
, j
)) {
361 default_protocol
= cfg
.protocol
=
362 backends
[i
].protocol
;
364 backends
[i
].backend
->default_port
;
372 * Three cases. Either (a) there's a nonzero
373 * length string followed by an @, in which
374 * case that's user and the remainder is host.
375 * Or (b) there's only one string, not counting
376 * a potential initial @, and it exists in the
377 * saved-sessions database. Or (c) only one
378 * string and it _doesn't_ exist in the
383 p
++, r
= NULL
; /* discount initial @ */
389 do_defaults(p
, &cfg2
);
390 if (cfg2
.host
[0] == '\0') {
391 /* No settings for this host; use defaults */
392 strncpy(cfg
.host
, p
, sizeof(cfg
.host
) - 1);
393 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
394 cfg
.port
= default_port
;
397 cfg
.remote_cmd_ptr
= cfg
.remote_cmd
;
401 strncpy(cfg
.username
, p
, sizeof(cfg
.username
) - 1);
402 cfg
.username
[sizeof(cfg
.username
) - 1] = '\0';
403 strncpy(cfg
.host
, r
, sizeof(cfg
.host
) - 1);
404 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
405 cfg
.port
= default_port
;
411 cmdlen
= cmdsize
= 0;
416 if (cmdlen
>= cmdsize
) {
417 cmdsize
= cmdlen
+ 512;
418 command
= sresize(command
, cmdsize
, char);
420 command
[cmdlen
++]=*p
++;
422 if (cmdlen
>= cmdsize
) {
423 cmdsize
= cmdlen
+ 512;
424 command
= sresize(command
, cmdsize
, char);
426 command
[cmdlen
++]=' '; /* always add trailing space */
427 if (--argc
) p
= *++argv
;
429 if (cmdlen
) command
[--cmdlen
]='\0';
430 /* change trailing blank to NUL */
431 cfg
.remote_cmd_ptr
= command
;
432 cfg
.remote_cmd_ptr2
= NULL
;
433 cfg
.nopty
= TRUE
; /* command => no terminal */
435 break; /* done with cmdline */
448 * Trim leading whitespace off the hostname if it's there.
451 int space
= strspn(cfg
.host
, " \t");
452 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
455 /* See if host is of the form user@host */
456 if (cfg
.host
[0] != '\0') {
457 char *atsign
= strchr(cfg
.host
, '@');
458 /* Make sure we're not overflowing the user field */
460 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
461 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
462 cfg
.username
[atsign
- cfg
.host
] = '\0';
464 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
469 * Perform command-line overrides on session configuration.
471 cmdline_run_saved(&cfg
);
474 * Apply subsystem status.
477 cfg
.ssh_subsys
= TRUE
;
480 * Trim a colon suffix off the hostname if it's there.
482 cfg
.host
[strcspn(cfg
.host
, ":")] = '\0';
485 * Remove any remaining whitespace from the hostname.
489 while (cfg
.host
[p2
] != '\0') {
490 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
491 cfg
.host
[p1
] = cfg
.host
[p2
];
499 if (!*cfg
.remote_cmd_ptr
)
500 flags
|= FLAG_INTERACTIVE
;
503 * Select protocol. This is farmed out into a table in a
504 * separate file to enable an ssh-free variant.
509 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
510 if (backends
[i
].protocol
== cfg
.protocol
) {
511 back
= backends
[i
].backend
;
516 "Internal fault: Unsupported protocol found\n");
524 if (portnumber
!= -1)
525 cfg
.port
= portnumber
;
528 * Set up the pipe we'll use to tell us about SIGWINCH.
530 if (pipe(signalpipe
) < 0) {
534 putty_signal(SIGWINCH
, sigwinch
);
540 * Start up the connection.
542 logctx
= log_init(NULL
, &cfg
);
543 console_provide_logctx(logctx
);
547 /* nodelay is only useful if stdin is a terminal device */
548 int nodelay
= cfg
.tcp_nodelay
&& isatty(0);
550 error
= back
->init(NULL
, &backhandle
, &cfg
, cfg
.host
, cfg
.port
,
553 fprintf(stderr
, "Unable to open connection:\n%s\n", error
);
556 back
->provide_logctx(backhandle
, logctx
);
557 ldisc
= ldisc_create(&cfg
, NULL
, back
, backhandle
, NULL
);
563 * Set up the initial console mode. We don't care if this call
564 * fails, because we know we aren't necessarily running in a
567 tcgetattr(0, &orig_termios
);
568 atexit(cleanup_termios
);
569 ldisc_update(NULL
, 1, 1);
573 fd_set rset
, wset
, xset
;
583 FD_SET_MAX(signalpipe
[0], maxfd
, rset
);
585 if (connopen
&& !sending
&&
586 back
->socket(backhandle
) != NULL
&&
587 back
->sendok(backhandle
) &&
588 back
->sendbuffer(backhandle
) < MAX_STDIN_BACKLOG
) {
589 /* If we're OK to send, then try to read from stdin. */
590 FD_SET_MAX(0, maxfd
, rset
);
593 if (bufchain_size(&stdout_data
) > 0) {
594 /* If we have data for stdout, try to write to stdout. */
595 FD_SET_MAX(1, maxfd
, wset
);
598 if (bufchain_size(&stderr_data
) > 0) {
599 /* If we have data for stderr, try to write to stderr. */
600 FD_SET_MAX(2, maxfd
, wset
);
603 /* Count the currently active fds. */
605 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
606 fd
= next_fd(&fdstate
, &rwx
)) i
++;
608 /* Expand the fdlist buffer if necessary. */
611 fdlist
= sresize(fdlist
, fdsize
, int);
615 * Add all currently open fds to the select sets, and store
616 * them in fdlist as well.
619 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
620 fd
= next_fd(&fdstate
, &rwx
)) {
621 fdlist
[fdcount
++] = fd
;
623 FD_SET_MAX(fd
, maxfd
, rset
);
625 FD_SET_MAX(fd
, maxfd
, wset
);
627 FD_SET_MAX(fd
, maxfd
, xset
);
631 ret
= select(maxfd
, &rset
, &wset
, &xset
, NULL
);
632 } while (ret
< 0 && errno
== EINTR
);
639 for (i
= 0; i
< fdcount
; i
++) {
642 * We must process exceptional notifications before
643 * ordinary readability ones, or we may go straight
644 * past the urgent marker.
646 if (FD_ISSET(fd
, &xset
))
647 select_result(fd
, 4);
648 if (FD_ISSET(fd
, &rset
))
649 select_result(fd
, 1);
650 if (FD_ISSET(fd
, &wset
))
651 select_result(fd
, 2);
654 if (FD_ISSET(signalpipe
[0], &rset
)) {
657 read(signalpipe
[0], c
, 1); /* ignore its value; it'll be `x' */
658 if (ioctl(0, TIOCGWINSZ
, (void *)&size
) >= 0)
659 back
->size(backhandle
, size
.ws_col
, size
.ws_row
);
662 if (FD_ISSET(0, &rset
)) {
666 if (connopen
&& back
->socket(backhandle
) != NULL
) {
667 ret
= read(0, buf
, sizeof(buf
));
669 perror("stdin: read");
671 } else if (ret
== 0) {
672 back
->special(backhandle
, TS_EOF
);
673 sending
= FALSE
; /* send nothing further after this */
675 back
->send(backhandle
, buf
, ret
);
680 if (FD_ISSET(1, &wset
)) {
684 if (FD_ISSET(2, &wset
)) {
688 if ((!connopen
|| back
->socket(backhandle
) == NULL
) &&
689 bufchain_size(&stdout_data
) == 0 &&
690 bufchain_size(&stderr_data
) == 0)
691 break; /* we closed the connection */
693 exitcode
= back
->exitcode(backhandle
);
695 fprintf(stderr
, "Remote process exit code unavailable\n");
696 exitcode
= 1; /* this is an error condition */
698 cleanup_exit(exitcode
);
699 return exitcode
; /* shouldn't happen, but placates gcc */