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 bufchain_prefix(chain
, &senddata
, &sendlen
);
115 ret
= write(fd
, senddata
, sendlen
);
117 bufchain_consume(chain
, ret
);
119 perror(is_stderr ?
"stderr: write" : "stdout: write");
124 int from_backend(void *frontend_handle
, int is_stderr
, char *data
, int len
)
131 bufchain_add(&stderr_data
, data
, len
);
134 bufchain_add(&stdout_data
, data
, len
);
138 osize
= bufchain_size(&stdout_data
);
139 esize
= bufchain_size(&stderr_data
);
141 return osize
+ esize
;
145 * Short description of parameters.
147 static void usage(void)
149 printf("PuTTY Link: command-line connection utility\n");
151 printf("Usage: plink [options] [user@]host [command]\n");
152 printf(" (\"host\" can also be a PuTTY saved session name)\n");
153 printf("Options:\n");
154 printf(" -v show verbose messages\n");
155 printf(" -load sessname Load settings from saved session\n");
156 printf(" -ssh -telnet -rlogin -raw\n");
157 printf(" force use of a particular protocol (default SSH)\n");
158 printf(" -P port connect to specified port\n");
159 printf(" -l user connect with specified username\n");
160 printf(" -m file read remote command(s) from file\n");
161 printf(" -batch disable all interactive prompts\n");
162 printf("The following options only apply to SSH connections:\n");
163 printf(" -pw passw login with specified password\n");
164 printf(" -L listen-port:host:port Forward local port to "
166 printf(" -R listen-port:host:port Forward remote port to"
168 printf(" -X -x enable / disable X11 forwarding\n");
169 printf(" -A -a enable / disable agent forwarding\n");
170 printf(" -t -T enable / disable pty allocation\n");
171 printf(" -1 -2 force use of particular protocol version\n");
172 printf(" -C enable compression\n");
173 printf(" -i key private key file for authentication\n");
177 int main(int argc
, char **argv
)
183 int i
, skcount
, sksize
, socketstate
;
189 ssh_get_line
= console_get_line
;
192 skcount
= sksize
= 0;
194 * Initialise port and protocol to sensible defaults. (These
195 * will be overridden by more or less anything.)
197 default_protocol
= PROT_SSH
;
202 * Process the command line.
204 do_defaults(NULL
, &cfg
);
205 default_protocol
= cfg
.protocol
;
206 default_port
= cfg
.port
;
209 * Override the default protocol if PLINK_PROTOCOL is set.
211 char *p
= getenv("PLINK_PROTOCOL");
214 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
215 if (!strcmp(backends
[i
].name
, p
)) {
216 default_protocol
= cfg
.protocol
= backends
[i
].protocol
;
217 default_port
= cfg
.port
=
218 backends
[i
].backend
->default_port
;
227 int ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
), 1);
230 "plink: option \"%s\" requires an argument\n", p
);
231 } else if (ret
== 2) {
233 } else if (ret
== 1) {
235 } else if (!strcmp(p
, "-batch")) {
236 console_batch_mode
= 1;
242 * If the hostname starts with "telnet:", set the
243 * protocol to Telnet and process the string as a
246 if (!strncmp(q
, "telnet:", 7)) {
250 if (q
[0] == '/' && q
[1] == '/')
252 cfg
.protocol
= PROT_TELNET
;
254 while (*p
&& *p
!= ':' && *p
!= '/')
263 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
264 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
268 * Before we process the [user@]host string, we
269 * first check for the presence of a protocol
270 * prefix (a protocol name followed by ",").
275 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
276 j
= strlen(backends
[i
].name
);
278 !memcmp(backends
[i
].name
, p
, j
)) {
279 default_protocol
= cfg
.protocol
=
280 backends
[i
].protocol
;
282 backends
[i
].backend
->default_port
;
290 * Three cases. Either (a) there's a nonzero
291 * length string followed by an @, in which
292 * case that's user and the remainder is host.
293 * Or (b) there's only one string, not counting
294 * a potential initial @, and it exists in the
295 * saved-sessions database. Or (c) only one
296 * string and it _doesn't_ exist in the
301 p
++, r
= NULL
; /* discount initial @ */
307 do_defaults(p
, &cfg2
);
308 if (cfg2
.host
[0] == '\0') {
309 /* No settings for this host; use defaults */
310 strncpy(cfg
.host
, p
, sizeof(cfg
.host
) - 1);
311 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
312 cfg
.port
= default_port
;
315 cfg
.remote_cmd_ptr
= cfg
.remote_cmd
;
319 strncpy(cfg
.username
, p
, sizeof(cfg
.username
) - 1);
320 cfg
.username
[sizeof(cfg
.username
) - 1] = '\0';
321 strncpy(cfg
.host
, r
, sizeof(cfg
.host
) - 1);
322 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
323 cfg
.port
= default_port
;
329 cmdlen
= cmdsize
= 0;
334 if (cmdlen
>= cmdsize
) {
335 cmdsize
= cmdlen
+ 512;
336 command
= srealloc(command
, cmdsize
);
338 command
[cmdlen
++]=*p
++;
340 if (cmdlen
>= cmdsize
) {
341 cmdsize
= cmdlen
+ 512;
342 command
= srealloc(command
, cmdsize
);
344 command
[cmdlen
++]=' '; /* always add trailing space */
345 if (--argc
) p
= *++argv
;
347 if (cmdlen
) command
[--cmdlen
]='\0';
348 /* change trailing blank to NUL */
349 cfg
.remote_cmd_ptr
= command
;
350 cfg
.remote_cmd_ptr2
= NULL
;
351 cfg
.nopty
= TRUE
; /* command => no terminal */
353 break; /* done with cmdline */
363 * Trim leading whitespace off the hostname if it's there.
366 int space
= strspn(cfg
.host
, " \t");
367 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
370 /* See if host is of the form user@host */
371 if (cfg
.host
[0] != '\0') {
372 char *atsign
= strchr(cfg
.host
, '@');
373 /* Make sure we're not overflowing the user field */
375 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
376 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
377 cfg
.username
[atsign
- cfg
.host
] = '\0';
379 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
384 * Perform command-line overrides on session configuration.
389 * Trim a colon suffix off the hostname if it's there.
391 cfg
.host
[strcspn(cfg
.host
, ":")] = '\0';
394 * Remove any remaining whitespace from the hostname.
398 while (cfg
.host
[p2
] != '\0') {
399 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
400 cfg
.host
[p1
] = cfg
.host
[p2
];
408 if (!*cfg
.remote_cmd_ptr
)
409 flags
|= FLAG_INTERACTIVE
;
412 * Select protocol. This is farmed out into a table in a
413 * separate file to enable an ssh-free variant.
418 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
419 if (backends
[i
].protocol
== cfg
.protocol
) {
420 back
= backends
[i
].backend
;
425 "Internal fault: Unsupported protocol found\n");
433 if (portnumber
!= -1)
434 cfg
.port
= portnumber
;
439 * Start up the connection.
444 /* nodelay is only useful if stdin is a terminal device */
445 int nodelay
= cfg
.tcp_nodelay
&& isatty(0);
447 error
= back
->init(NULL
, &backhandle
, cfg
.host
, cfg
.port
,
450 fprintf(stderr
, "Unable to open connection:\n%s", error
);
453 logctx
= log_init(NULL
);
454 back
->provide_logctx(backhandle
, logctx
);
455 ldisc
= ldisc_create(NULL
, back
, backhandle
, NULL
);
461 * Set up the initial console mode. We don't care if this call
462 * fails, because we know we aren't necessarily running in a
465 tcgetattr(0, &orig_termios
);
466 atexit(cleanup_termios
);
467 ldisc_update(NULL
, 1, 1);
471 fd_set rset
, wset
, xset
;
481 if (connopen
&& !sending
&&
482 back
->socket(backhandle
) != NULL
&&
483 back
->sendok(backhandle
) &&
484 back
->sendbuffer(backhandle
) < MAX_STDIN_BACKLOG
) {
485 /* If we're OK to send, then try to read from stdin. */
486 FD_SET_MAX(0, maxfd
, rset
);
489 if (bufchain_size(&stdout_data
) > 0) {
490 /* If we have data for stdout, try to write to stdout. */
491 FD_SET_MAX(1, maxfd
, wset
);
494 if (bufchain_size(&stderr_data
) > 0) {
495 /* If we have data for stderr, try to write to stderr. */
496 FD_SET_MAX(2, maxfd
, wset
);
499 /* Count the currently active sockets. */
501 for (socket
= first_socket(&socketstate
, &rwx
); socket
>= 0;
502 socket
= next_socket(&socketstate
, &rwx
)) i
++;
504 /* Expand the sklist buffer if necessary. */
507 sklist
= srealloc(sklist
, sksize
* sizeof(*sklist
));
511 * Add all currently open sockets to the select sets, and
512 * store them in sklist as well.
515 for (socket
= first_socket(&socketstate
, &rwx
); socket
>= 0;
516 socket
= next_socket(&socketstate
, &rwx
)) {
517 sklist
[skcount
++] = socket
;
519 FD_SET_MAX(socket
, maxfd
, rset
);
521 FD_SET_MAX(socket
, maxfd
, wset
);
523 FD_SET_MAX(socket
, maxfd
, xset
);
526 ret
= select(maxfd
, &rset
, &wset
, &xset
, NULL
);
533 for (i
= 0; i
< skcount
; i
++) {
535 if (FD_ISSET(socket
, &rset
))
536 select_result(socket
, 1);
537 if (FD_ISSET(socket
, &wset
))
538 select_result(socket
, 2);
539 if (FD_ISSET(socket
, &xset
))
540 select_result(socket
, 4);
543 if (FD_ISSET(0, &rset
)) {
547 if (connopen
&& back
->socket(backhandle
) != NULL
) {
548 ret
= read(0, buf
, sizeof(buf
));
550 perror("stdin: read");
552 } else if (ret
== 0) {
553 back
->special(backhandle
, TS_EOF
);
554 sending
= FALSE
; /* send nothing further after this */
556 back
->send(backhandle
, buf
, ret
);
561 if (FD_ISSET(1, &wset
)) {
565 if (FD_ISSET(2, &wset
)) {
569 if ((!connopen
|| back
->socket(backhandle
) == NULL
) &&
570 bufchain_size(&stdout_data
) == 0 &&
571 bufchain_size(&stderr_data
) == 0)
572 break; /* we closed the connection */
574 exitcode
= back
->exitcode(backhandle
);
576 fprintf(stderr
, "Remote process exit code unavailable\n");
577 exitcode
= 1; /* this is an error condition */