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
;
192 ssh_get_line
= console_get_line
;
195 skcount
= sksize
= 0;
197 * Initialise port and protocol to sensible defaults. (These
198 * will be overridden by more or less anything.)
200 default_protocol
= PROT_SSH
;
205 * Process the command line.
207 do_defaults(NULL
, &cfg
);
208 default_protocol
= cfg
.protocol
;
209 default_port
= cfg
.port
;
213 * Override the default protocol if PLINK_PROTOCOL is set.
215 char *p
= getenv("PLINK_PROTOCOL");
218 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
219 if (!strcmp(backends
[i
].name
, p
)) {
220 default_protocol
= cfg
.protocol
= backends
[i
].protocol
;
221 default_port
= cfg
.port
=
222 backends
[i
].backend
->default_port
;
231 int ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
), 1);
234 "plink: option \"%s\" requires an argument\n", p
);
236 } else if (ret
== 2) {
238 } else if (ret
== 1) {
240 } else if (!strcmp(p
, "-batch")) {
241 console_batch_mode
= 1;
242 } else if (!strcmp(p
, "-o")) {
245 "plink: option \"-o\" requires an argument\n");
249 provide_xrm_string(*++argv
);
252 fprintf(stderr
, "plink: unknown option \"%s\"\n", p
);
259 do_defaults(NULL
, &cfg
);
262 * If the hostname starts with "telnet:", set the
263 * protocol to Telnet and process the string as a
266 if (!strncmp(q
, "telnet:", 7)) {
270 if (q
[0] == '/' && q
[1] == '/')
272 cfg
.protocol
= PROT_TELNET
;
274 while (*p
&& *p
!= ':' && *p
!= '/')
283 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
284 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
288 * Before we process the [user@]host string, we
289 * first check for the presence of a protocol
290 * prefix (a protocol name followed by ",").
295 for (i
= 0; backends
[i
].backend
!= NULL
; i
++) {
296 j
= strlen(backends
[i
].name
);
298 !memcmp(backends
[i
].name
, p
, j
)) {
299 default_protocol
= cfg
.protocol
=
300 backends
[i
].protocol
;
302 backends
[i
].backend
->default_port
;
310 * Three cases. Either (a) there's a nonzero
311 * length string followed by an @, in which
312 * case that's user and the remainder is host.
313 * Or (b) there's only one string, not counting
314 * a potential initial @, and it exists in the
315 * saved-sessions database. Or (c) only one
316 * string and it _doesn't_ exist in the
321 p
++, r
= NULL
; /* discount initial @ */
327 do_defaults(p
, &cfg2
);
328 if (cfg2
.host
[0] == '\0') {
329 /* No settings for this host; use defaults */
330 strncpy(cfg
.host
, p
, sizeof(cfg
.host
) - 1);
331 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
332 cfg
.port
= default_port
;
335 cfg
.remote_cmd_ptr
= cfg
.remote_cmd
;
339 strncpy(cfg
.username
, p
, sizeof(cfg
.username
) - 1);
340 cfg
.username
[sizeof(cfg
.username
) - 1] = '\0';
341 strncpy(cfg
.host
, r
, sizeof(cfg
.host
) - 1);
342 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
343 cfg
.port
= default_port
;
349 cmdlen
= cmdsize
= 0;
354 if (cmdlen
>= cmdsize
) {
355 cmdsize
= cmdlen
+ 512;
356 command
= srealloc(command
, cmdsize
);
358 command
[cmdlen
++]=*p
++;
360 if (cmdlen
>= cmdsize
) {
361 cmdsize
= cmdlen
+ 512;
362 command
= srealloc(command
, cmdsize
);
364 command
[cmdlen
++]=' '; /* always add trailing space */
365 if (--argc
) p
= *++argv
;
367 if (cmdlen
) command
[--cmdlen
]='\0';
368 /* change trailing blank to NUL */
369 cfg
.remote_cmd_ptr
= command
;
370 cfg
.remote_cmd_ptr2
= NULL
;
371 cfg
.nopty
= TRUE
; /* command => no terminal */
373 break; /* done with cmdline */
386 * Trim leading whitespace off the hostname if it's there.
389 int space
= strspn(cfg
.host
, " \t");
390 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
393 /* See if host is of the form user@host */
394 if (cfg
.host
[0] != '\0') {
395 char *atsign
= strchr(cfg
.host
, '@');
396 /* Make sure we're not overflowing the user field */
398 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
399 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
400 cfg
.username
[atsign
- cfg
.host
] = '\0';
402 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
407 * Perform command-line overrides on session configuration.
412 * Trim a colon suffix off the hostname if it's there.
414 cfg
.host
[strcspn(cfg
.host
, ":")] = '\0';
417 * Remove any remaining whitespace from the hostname.
421 while (cfg
.host
[p2
] != '\0') {
422 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
423 cfg
.host
[p1
] = cfg
.host
[p2
];
431 if (!*cfg
.remote_cmd_ptr
)
432 flags
|= FLAG_INTERACTIVE
;
435 * Select protocol. This is farmed out into a table in a
436 * separate file to enable an ssh-free variant.
441 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
442 if (backends
[i
].protocol
== cfg
.protocol
) {
443 back
= backends
[i
].backend
;
448 "Internal fault: Unsupported protocol found\n");
456 if (portnumber
!= -1)
457 cfg
.port
= portnumber
;
462 * Start up the connection.
464 logctx
= log_init(NULL
);
468 /* nodelay is only useful if stdin is a terminal device */
469 int nodelay
= cfg
.tcp_nodelay
&& isatty(0);
471 error
= back
->init(NULL
, &backhandle
, cfg
.host
, cfg
.port
,
474 fprintf(stderr
, "Unable to open connection:\n%s\n", error
);
477 back
->provide_logctx(backhandle
, logctx
);
478 ldisc
= ldisc_create(&cfg
, NULL
, back
, backhandle
, NULL
);
484 * Set up the initial console mode. We don't care if this call
485 * fails, because we know we aren't necessarily running in a
488 tcgetattr(0, &orig_termios
);
489 atexit(cleanup_termios
);
490 ldisc_update(NULL
, 1, 1);
494 fd_set rset
, wset
, xset
;
504 if (connopen
&& !sending
&&
505 back
->socket(backhandle
) != NULL
&&
506 back
->sendok(backhandle
) &&
507 back
->sendbuffer(backhandle
) < MAX_STDIN_BACKLOG
) {
508 /* If we're OK to send, then try to read from stdin. */
509 FD_SET_MAX(0, maxfd
, rset
);
512 if (bufchain_size(&stdout_data
) > 0) {
513 /* If we have data for stdout, try to write to stdout. */
514 FD_SET_MAX(1, maxfd
, wset
);
517 if (bufchain_size(&stderr_data
) > 0) {
518 /* If we have data for stderr, try to write to stderr. */
519 FD_SET_MAX(2, maxfd
, wset
);
522 /* Count the currently active sockets. */
524 for (socket
= first_socket(&socketstate
, &rwx
); socket
>= 0;
525 socket
= next_socket(&socketstate
, &rwx
)) i
++;
527 /* Expand the sklist buffer if necessary. */
530 sklist
= srealloc(sklist
, sksize
* sizeof(*sklist
));
534 * Add all currently open sockets to the select sets, and
535 * store them in sklist as well.
538 for (socket
= first_socket(&socketstate
, &rwx
); socket
>= 0;
539 socket
= next_socket(&socketstate
, &rwx
)) {
540 sklist
[skcount
++] = socket
;
542 FD_SET_MAX(socket
, maxfd
, rset
);
544 FD_SET_MAX(socket
, maxfd
, wset
);
546 FD_SET_MAX(socket
, maxfd
, xset
);
549 ret
= select(maxfd
, &rset
, &wset
, &xset
, NULL
);
556 for (i
= 0; i
< skcount
; i
++) {
559 * We must process exceptional notifications before
560 * ordinary readability ones, or we may go straight
561 * past the urgent marker.
563 if (FD_ISSET(socket
, &xset
))
564 select_result(socket
, 4);
565 if (FD_ISSET(socket
, &rset
))
566 select_result(socket
, 1);
567 if (FD_ISSET(socket
, &wset
))
568 select_result(socket
, 2);
571 if (FD_ISSET(0, &rset
)) {
575 if (connopen
&& back
->socket(backhandle
) != NULL
) {
576 ret
= read(0, buf
, sizeof(buf
));
578 perror("stdin: read");
580 } else if (ret
== 0) {
581 back
->special(backhandle
, TS_EOF
);
582 sending
= FALSE
; /* send nothing further after this */
584 back
->send(backhandle
, buf
, ret
);
589 if (FD_ISSET(1, &wset
)) {
593 if (FD_ISSET(2, &wset
)) {
597 if ((!connopen
|| back
->socket(backhandle
) == NULL
) &&
598 bufchain_size(&stdout_data
) == 0 &&
599 bufchain_size(&stderr_data
) == 0)
600 break; /* we closed the connection */
602 exitcode
= back
->exitcode(backhandle
);
604 fprintf(stderr
, "Remote process exit code unavailable\n");
605 exitcode
= 1; /* this is an error condition */
607 cleanup_exit(exitcode
);
608 return exitcode
; /* shouldn't happen, but placates gcc */