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
;
191 ssh_get_line
= console_get_line
;
194 skcount
= sksize
= 0;
196 * Initialise port and protocol to sensible defaults. (These
197 * will be overridden by more or less anything.)
199 default_protocol
= PROT_SSH
;
204 * Process the command line.
206 do_defaults(NULL
, &cfg
);
207 default_protocol
= cfg
.protocol
;
208 default_port
= cfg
.port
;
211 * Override the default protocol if PLINK_PROTOCOL is set.
213 char *p
= getenv("PLINK_PROTOCOL");
216 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
217 if (!strcmp(backends
[i
].name
, p
)) {
218 default_protocol
= cfg
.protocol
= backends
[i
].protocol
;
219 default_port
= cfg
.port
=
220 backends
[i
].backend
->default_port
;
229 int ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
), 1);
232 "plink: option \"%s\" requires an argument\n", p
);
233 } else if (ret
== 2) {
235 } else if (ret
== 1) {
237 } else if (!strcmp(p
, "-batch")) {
238 console_batch_mode
= 1;
239 } else if (!strcmp(p
, "-o")) {
242 "plink: option \"-o\" requires an argument\n");
244 --argc
, provide_xrm_string(*++argv
);
250 do_defaults(NULL
, &cfg
);
253 * If the hostname starts with "telnet:", set the
254 * protocol to Telnet and process the string as a
257 if (!strncmp(q
, "telnet:", 7)) {
261 if (q
[0] == '/' && q
[1] == '/')
263 cfg
.protocol
= PROT_TELNET
;
265 while (*p
&& *p
!= ':' && *p
!= '/')
274 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
275 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
279 * Before we process the [user@]host string, we
280 * first check for the presence of a protocol
281 * prefix (a protocol name followed by ",").
286 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
287 j
= strlen(backends
[i
].name
);
289 !memcmp(backends
[i
].name
, p
, j
)) {
290 default_protocol
= cfg
.protocol
=
291 backends
[i
].protocol
;
293 backends
[i
].backend
->default_port
;
301 * Three cases. Either (a) there's a nonzero
302 * length string followed by an @, in which
303 * case that's user and the remainder is host.
304 * Or (b) there's only one string, not counting
305 * a potential initial @, and it exists in the
306 * saved-sessions database. Or (c) only one
307 * string and it _doesn't_ exist in the
312 p
++, r
= NULL
; /* discount initial @ */
318 do_defaults(p
, &cfg2
);
319 if (cfg2
.host
[0] == '\0') {
320 /* No settings for this host; use defaults */
321 strncpy(cfg
.host
, p
, sizeof(cfg
.host
) - 1);
322 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
323 cfg
.port
= default_port
;
326 cfg
.remote_cmd_ptr
= cfg
.remote_cmd
;
330 strncpy(cfg
.username
, p
, sizeof(cfg
.username
) - 1);
331 cfg
.username
[sizeof(cfg
.username
) - 1] = '\0';
332 strncpy(cfg
.host
, r
, sizeof(cfg
.host
) - 1);
333 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
334 cfg
.port
= default_port
;
340 cmdlen
= cmdsize
= 0;
345 if (cmdlen
>= cmdsize
) {
346 cmdsize
= cmdlen
+ 512;
347 command
= srealloc(command
, cmdsize
);
349 command
[cmdlen
++]=*p
++;
351 if (cmdlen
>= cmdsize
) {
352 cmdsize
= cmdlen
+ 512;
353 command
= srealloc(command
, cmdsize
);
355 command
[cmdlen
++]=' '; /* always add trailing space */
356 if (--argc
) p
= *++argv
;
358 if (cmdlen
) command
[--cmdlen
]='\0';
359 /* change trailing blank to NUL */
360 cfg
.remote_cmd_ptr
= command
;
361 cfg
.remote_cmd_ptr2
= NULL
;
362 cfg
.nopty
= TRUE
; /* command => no terminal */
364 break; /* done with cmdline */
374 * Trim leading whitespace off the hostname if it's there.
377 int space
= strspn(cfg
.host
, " \t");
378 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
381 /* See if host is of the form user@host */
382 if (cfg
.host
[0] != '\0') {
383 char *atsign
= strchr(cfg
.host
, '@');
384 /* Make sure we're not overflowing the user field */
386 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
387 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
388 cfg
.username
[atsign
- cfg
.host
] = '\0';
390 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
395 * Perform command-line overrides on session configuration.
400 * Trim a colon suffix off the hostname if it's there.
402 cfg
.host
[strcspn(cfg
.host
, ":")] = '\0';
405 * Remove any remaining whitespace from the hostname.
409 while (cfg
.host
[p2
] != '\0') {
410 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
411 cfg
.host
[p1
] = cfg
.host
[p2
];
419 if (!*cfg
.remote_cmd_ptr
)
420 flags
|= FLAG_INTERACTIVE
;
423 * Select protocol. This is farmed out into a table in a
424 * separate file to enable an ssh-free variant.
429 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
430 if (backends
[i
].protocol
== cfg
.protocol
) {
431 back
= backends
[i
].backend
;
436 "Internal fault: Unsupported protocol found\n");
444 if (portnumber
!= -1)
445 cfg
.port
= portnumber
;
450 * Start up the connection.
452 logctx
= log_init(NULL
);
456 /* nodelay is only useful if stdin is a terminal device */
457 int nodelay
= cfg
.tcp_nodelay
&& isatty(0);
459 error
= back
->init(NULL
, &backhandle
, cfg
.host
, cfg
.port
,
462 fprintf(stderr
, "Unable to open connection:\n%s\n", error
);
465 back
->provide_logctx(backhandle
, logctx
);
466 ldisc
= ldisc_create(NULL
, back
, backhandle
, NULL
);
472 * Set up the initial console mode. We don't care if this call
473 * fails, because we know we aren't necessarily running in a
476 tcgetattr(0, &orig_termios
);
477 atexit(cleanup_termios
);
478 ldisc_update(NULL
, 1, 1);
482 fd_set rset
, wset
, xset
;
492 if (connopen
&& !sending
&&
493 back
->socket(backhandle
) != NULL
&&
494 back
->sendok(backhandle
) &&
495 back
->sendbuffer(backhandle
) < MAX_STDIN_BACKLOG
) {
496 /* If we're OK to send, then try to read from stdin. */
497 FD_SET_MAX(0, maxfd
, rset
);
500 if (bufchain_size(&stdout_data
) > 0) {
501 /* If we have data for stdout, try to write to stdout. */
502 FD_SET_MAX(1, maxfd
, wset
);
505 if (bufchain_size(&stderr_data
) > 0) {
506 /* If we have data for stderr, try to write to stderr. */
507 FD_SET_MAX(2, maxfd
, wset
);
510 /* Count the currently active sockets. */
512 for (socket
= first_socket(&socketstate
, &rwx
); socket
>= 0;
513 socket
= next_socket(&socketstate
, &rwx
)) i
++;
515 /* Expand the sklist buffer if necessary. */
518 sklist
= srealloc(sklist
, sksize
* sizeof(*sklist
));
522 * Add all currently open sockets to the select sets, and
523 * store them in sklist as well.
526 for (socket
= first_socket(&socketstate
, &rwx
); socket
>= 0;
527 socket
= next_socket(&socketstate
, &rwx
)) {
528 sklist
[skcount
++] = socket
;
530 FD_SET_MAX(socket
, maxfd
, rset
);
532 FD_SET_MAX(socket
, maxfd
, wset
);
534 FD_SET_MAX(socket
, maxfd
, xset
);
537 ret
= select(maxfd
, &rset
, &wset
, &xset
, NULL
);
544 for (i
= 0; i
< skcount
; i
++) {
547 * We must process exceptional notifications before
548 * ordinary readability ones, or we may go straight
549 * past the urgent marker.
551 if (FD_ISSET(socket
, &xset
))
552 select_result(socket
, 4);
553 if (FD_ISSET(socket
, &rset
))
554 select_result(socket
, 1);
555 if (FD_ISSET(socket
, &wset
))
556 select_result(socket
, 2);
559 if (FD_ISSET(0, &rset
)) {
563 if (connopen
&& back
->socket(backhandle
) != NULL
) {
564 ret
= read(0, buf
, sizeof(buf
));
566 perror("stdin: read");
568 } else if (ret
== 0) {
569 back
->special(backhandle
, TS_EOF
);
570 sending
= FALSE
; /* send nothing further after this */
572 back
->send(backhandle
, buf
, ret
);
577 if (FD_ISSET(1, &wset
)) {
581 if (FD_ISSET(2, &wset
)) {
585 if ((!connopen
|| back
->socket(backhandle
) == NULL
) &&
586 bufchain_size(&stdout_data
) == 0 &&
587 bufchain_size(&stderr_data
) == 0)
588 break; /* we closed the connection */
590 exitcode
= back
->exitcode(backhandle
);
592 fprintf(stderr
, "Remote process exit code unavailable\n");
593 exitcode
= 1; /* this is an error condition */
595 cleanup_exit(exitcode
);
596 return exitcode
; /* shouldn't happen, but placates gcc */