c5e438ec |
1 | /* |
2 | * PLink - a command-line (stdin/stdout) variant of PuTTY. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
5673d44e |
7 | #include <errno.h> |
c5e438ec |
8 | #include <assert.h> |
9 | #include <stdarg.h> |
5673d44e |
10 | #include <signal.h> |
c5e438ec |
11 | #include <unistd.h> |
12 | #include <fcntl.h> |
13 | #include <termios.h> |
5a9eb105 |
14 | #include <pwd.h> |
15 | #include <sys/ioctl.h> |
154c73d5 |
16 | #include <sys/time.h> |
17 | #ifndef HAVE_NO_SYS_SELECT_H |
2a6848cc |
18 | #include <sys/select.h> |
154c73d5 |
19 | #endif |
c5e438ec |
20 | |
c5e438ec |
21 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
22 | #include "putty.h" |
23 | #include "storage.h" |
24 | #include "tree234.h" |
25 | |
26 | #define MAX_STDIN_BACKLOG 4096 |
27 | |
7bfc5fd0 |
28 | void *logctx; |
29 | |
f7397dc6 |
30 | static struct termios orig_termios; |
31 | |
c5e438ec |
32 | void fatalbox(char *p, ...) |
33 | { |
f7397dc6 |
34 | struct termios cf; |
c5e438ec |
35 | va_list ap; |
f7397dc6 |
36 | premsg(&cf); |
c5e438ec |
37 | fprintf(stderr, "FATAL ERROR: "); |
38 | va_start(ap, p); |
39 | vfprintf(stderr, p, ap); |
40 | va_end(ap); |
41 | fputc('\n', stderr); |
f7397dc6 |
42 | postmsg(&cf); |
7bfc5fd0 |
43 | if (logctx) { |
44 | log_free(logctx); |
45 | logctx = NULL; |
46 | } |
c5e438ec |
47 | cleanup_exit(1); |
48 | } |
49 | void modalfatalbox(char *p, ...) |
50 | { |
f7397dc6 |
51 | struct termios cf; |
c5e438ec |
52 | va_list ap; |
f7397dc6 |
53 | premsg(&cf); |
c5e438ec |
54 | fprintf(stderr, "FATAL ERROR: "); |
55 | va_start(ap, p); |
56 | vfprintf(stderr, p, ap); |
57 | va_end(ap); |
58 | fputc('\n', stderr); |
f7397dc6 |
59 | postmsg(&cf); |
7bfc5fd0 |
60 | if (logctx) { |
61 | log_free(logctx); |
62 | logctx = NULL; |
63 | } |
c5e438ec |
64 | cleanup_exit(1); |
65 | } |
33f4bde2 |
66 | void nonfatal(char *p, ...) |
67 | { |
68 | struct termios cf; |
69 | va_list ap; |
70 | premsg(&cf); |
71 | fprintf(stderr, "ERROR: "); |
72 | va_start(ap, p); |
73 | vfprintf(stderr, p, ap); |
74 | va_end(ap); |
75 | fputc('\n', stderr); |
76 | postmsg(&cf); |
77 | if (logctx) { |
78 | log_free(logctx); |
79 | logctx = NULL; |
80 | } |
81 | } |
c5e438ec |
82 | void connection_fatal(void *frontend, char *p, ...) |
83 | { |
f7397dc6 |
84 | struct termios cf; |
c5e438ec |
85 | va_list ap; |
f7397dc6 |
86 | premsg(&cf); |
c5e438ec |
87 | fprintf(stderr, "FATAL ERROR: "); |
88 | va_start(ap, p); |
89 | vfprintf(stderr, p, ap); |
90 | va_end(ap); |
91 | fputc('\n', stderr); |
f7397dc6 |
92 | postmsg(&cf); |
7bfc5fd0 |
93 | if (logctx) { |
94 | log_free(logctx); |
95 | logctx = NULL; |
96 | } |
c5e438ec |
97 | cleanup_exit(1); |
98 | } |
99 | void cmdline_error(char *p, ...) |
100 | { |
f7397dc6 |
101 | struct termios cf; |
c5e438ec |
102 | va_list ap; |
f7397dc6 |
103 | premsg(&cf); |
c5e438ec |
104 | fprintf(stderr, "plink: "); |
105 | va_start(ap, p); |
106 | vfprintf(stderr, p, ap); |
107 | va_end(ap); |
108 | fputc('\n', stderr); |
f7397dc6 |
109 | postmsg(&cf); |
c5e438ec |
110 | exit(1); |
111 | } |
112 | |
21226fd7 |
113 | static int local_tty = FALSE; /* do we have a local tty? */ |
c5e438ec |
114 | |
115 | static Backend *back; |
116 | static void *backhandle; |
4a693cfc |
117 | static Conf *conf; |
c5e438ec |
118 | |
5a9eb105 |
119 | /* |
120 | * Default settings that are specific to pterm. |
121 | */ |
c85623f9 |
122 | char *platform_default_s(const char *name) |
5a9eb105 |
123 | { |
5a9eb105 |
124 | if (!strcmp(name, "TermType")) |
799dfcfa |
125 | return dupstr(getenv("TERM")); |
42af6a67 |
126 | if (!strcmp(name, "UserName")) |
127 | return get_username(); |
aef05b78 |
128 | if (!strcmp(name, "SerialLine")) |
129 | return dupstr("/dev/ttyS0"); |
5a9eb105 |
130 | return NULL; |
131 | } |
132 | |
c85623f9 |
133 | int platform_default_i(const char *name, int def) |
5a9eb105 |
134 | { |
5a9eb105 |
135 | return def; |
136 | } |
137 | |
ae62eaeb |
138 | FontSpec *platform_default_fontspec(const char *name) |
9a30e26b |
139 | { |
ae62eaeb |
140 | return fontspec_new(""); |
9a30e26b |
141 | } |
142 | |
962468d4 |
143 | Filename *platform_default_filename(const char *name) |
9a30e26b |
144 | { |
9a30e26b |
145 | if (!strcmp(name, "LogFileName")) |
962468d4 |
146 | return filename_from_str("putty.log"); |
9a30e26b |
147 | else |
962468d4 |
148 | return filename_from_str(""); |
9a30e26b |
149 | } |
150 | |
c85623f9 |
151 | char *x_get_default(const char *key) |
c5e438ec |
152 | { |
153 | return NULL; /* this is a stub */ |
154 | } |
155 | int term_ldisc(Terminal *term, int mode) |
156 | { |
157 | return FALSE; |
158 | } |
159 | void ldisc_update(void *frontend, int echo, int edit) |
160 | { |
161 | /* Update stdin read mode to reflect changes in line discipline. */ |
162 | struct termios mode; |
163 | |
c6ccd5c2 |
164 | if (!local_tty) return; |
165 | |
c5e438ec |
166 | mode = orig_termios; |
167 | |
168 | if (echo) |
169 | mode.c_lflag |= ECHO; |
170 | else |
171 | mode.c_lflag &= ~ECHO; |
172 | |
8cdf0e5f |
173 | if (edit) { |
174 | mode.c_iflag |= ICRNL; |
c5e438ec |
175 | mode.c_lflag |= ISIG | ICANON; |
e8269d30 |
176 | mode.c_oflag |= OPOST; |
8cdf0e5f |
177 | } else { |
178 | mode.c_iflag &= ~ICRNL; |
c5e438ec |
179 | mode.c_lflag &= ~(ISIG | ICANON); |
e8269d30 |
180 | mode.c_oflag &= ~OPOST; |
c6ccd5c2 |
181 | /* Solaris sets these to unhelpful values */ |
259d0428 |
182 | mode.c_cc[VMIN] = 1; |
183 | mode.c_cc[VTIME] = 0; |
c6ccd5c2 |
184 | /* FIXME: perhaps what we do with IXON/IXOFF should be an |
185 | * argument to ldisc_update(), to allow implementation of SSH-2 |
186 | * "xon-xoff" and Rlogin's equivalent? */ |
187 | mode.c_iflag &= ~IXON; |
188 | mode.c_iflag &= ~IXOFF; |
8cdf0e5f |
189 | } |
d0cb8c0c |
190 | /* |
191 | * Mark parity errors and (more important) BREAK on input. This |
192 | * is more complex than it need be because POSIX-2001 suggests |
193 | * that escaping of valid 0xff in the input stream is dependent on |
194 | * IGNPAR being clear even though marking of BREAK isn't. NetBSD |
195 | * 2.0 goes one worse and makes it dependent on INPCK too. We |
196 | * deal with this by forcing these flags into a useful state and |
197 | * then faking the state in which we found them in from_tty() if |
198 | * we get passed a parity or framing error. |
199 | */ |
200 | mode.c_iflag = (mode.c_iflag | INPCK | PARMRK) & ~IGNPAR; |
c5e438ec |
201 | |
21226fd7 |
202 | tcsetattr(STDIN_FILENO, TCSANOW, &mode); |
c5e438ec |
203 | } |
204 | |
c6ccd5c2 |
205 | /* Helper function to extract a special character from a termios. */ |
206 | static char *get_ttychar(struct termios *t, int index) |
207 | { |
208 | cc_t c = t->c_cc[index]; |
209 | #if defined(_POSIX_VDISABLE) |
210 | if (c == _POSIX_VDISABLE) |
211 | return dupprintf(""); |
212 | #endif |
213 | return dupprintf("^<%d>", c); |
214 | } |
215 | |
216 | char *get_ttymode(void *frontend, const char *mode) |
217 | { |
218 | /* |
219 | * Propagate appropriate terminal modes from the local terminal, |
220 | * if any. |
221 | */ |
222 | if (!local_tty) return NULL; |
223 | |
224 | #define GET_CHAR(ourname, uxname) \ |
225 | do { \ |
226 | if (strcmp(mode, ourname) == 0) \ |
227 | return get_ttychar(&orig_termios, uxname); \ |
228 | } while(0) |
229 | #define GET_BOOL(ourname, uxname, uxmemb, transform) \ |
230 | do { \ |
231 | if (strcmp(mode, ourname) == 0) { \ |
232 | int b = (orig_termios.uxmemb & uxname) != 0; \ |
233 | transform; \ |
234 | return dupprintf("%d", b); \ |
235 | } \ |
236 | } while (0) |
237 | |
238 | /* |
239 | * Modes that want to be the same on all terminal devices involved. |
240 | */ |
241 | /* All the special characters supported by SSH */ |
242 | #if defined(VINTR) |
243 | GET_CHAR("INTR", VINTR); |
244 | #endif |
245 | #if defined(VQUIT) |
246 | GET_CHAR("QUIT", VQUIT); |
247 | #endif |
248 | #if defined(VERASE) |
249 | GET_CHAR("ERASE", VERASE); |
250 | #endif |
251 | #if defined(VKILL) |
252 | GET_CHAR("KILL", VKILL); |
253 | #endif |
254 | #if defined(VEOF) |
255 | GET_CHAR("EOF", VEOF); |
256 | #endif |
257 | #if defined(VEOL) |
258 | GET_CHAR("EOL", VEOL); |
259 | #endif |
260 | #if defined(VEOL2) |
261 | GET_CHAR("EOL2", VEOL2); |
262 | #endif |
263 | #if defined(VSTART) |
264 | GET_CHAR("START", VSTART); |
265 | #endif |
266 | #if defined(VSTOP) |
267 | GET_CHAR("STOP", VSTOP); |
268 | #endif |
269 | #if defined(VSUSP) |
270 | GET_CHAR("SUSP", VSUSP); |
271 | #endif |
272 | #if defined(VDSUSP) |
273 | GET_CHAR("DSUSP", VDSUSP); |
274 | #endif |
275 | #if defined(VREPRINT) |
276 | GET_CHAR("REPRINT", VREPRINT); |
277 | #endif |
278 | #if defined(VWERASE) |
279 | GET_CHAR("WERASE", VWERASE); |
280 | #endif |
281 | #if defined(VLNEXT) |
282 | GET_CHAR("LNEXT", VLNEXT); |
283 | #endif |
284 | #if defined(VFLUSH) |
285 | GET_CHAR("FLUSH", VFLUSH); |
286 | #endif |
287 | #if defined(VSWTCH) |
288 | GET_CHAR("SWTCH", VSWTCH); |
289 | #endif |
290 | #if defined(VSTATUS) |
291 | GET_CHAR("STATUS", VSTATUS); |
292 | #endif |
293 | #if defined(VDISCARD) |
294 | GET_CHAR("DISCARD", VDISCARD); |
295 | #endif |
296 | /* Modes that "configure" other major modes. These should probably be |
297 | * considered as user preferences. */ |
298 | /* Configuration of ICANON */ |
299 | #if defined(ECHOK) |
300 | GET_BOOL("ECHOK", ECHOK, c_lflag, ); |
301 | #endif |
302 | #if defined(ECHOKE) |
303 | GET_BOOL("ECHOKE", ECHOKE, c_lflag, ); |
304 | #endif |
305 | #if defined(ECHOE) |
306 | GET_BOOL("ECHOE", ECHOE, c_lflag, ); |
307 | #endif |
308 | #if defined(ECHONL) |
309 | GET_BOOL("ECHONL", ECHONL, c_lflag, ); |
310 | #endif |
311 | #if defined(XCASE) |
312 | GET_BOOL("XCASE", XCASE, c_lflag, ); |
313 | #endif |
314 | /* Configuration of ECHO */ |
315 | #if defined(ECHOCTL) |
316 | GET_BOOL("ECHOCTL", ECHOCTL, c_lflag, ); |
317 | #endif |
318 | /* Configuration of IXON/IXOFF */ |
319 | #if defined(IXANY) |
320 | GET_BOOL("IXANY", IXANY, c_iflag, ); |
321 | #endif |
e8269d30 |
322 | /* Configuration of OPOST */ |
7016cde4 |
323 | #if defined(OLCUC) |
324 | GET_BOOL("OLCUC", OLCUC, c_oflag, ); |
325 | #endif |
e8269d30 |
326 | #if defined(ONLCR) |
327 | GET_BOOL("ONLCR", ONLCR, c_oflag, ); |
328 | #endif |
329 | #if defined(OCRNL) |
330 | GET_BOOL("OCRNL", OCRNL, c_oflag, ); |
331 | #endif |
332 | #if defined(ONOCR) |
333 | GET_BOOL("ONOCR", ONOCR, c_oflag, ); |
334 | #endif |
20923089 |
335 | #if defined(ONLRET) |
e8269d30 |
336 | GET_BOOL("ONLRET", ONLRET, c_oflag, ); |
337 | #endif |
c6ccd5c2 |
338 | |
339 | /* |
340 | * Modes that want to be set in only one place, and that we have |
341 | * squashed locally. |
342 | */ |
343 | #if defined(ISIG) |
344 | GET_BOOL("ISIG", ISIG, c_lflag, ); |
345 | #endif |
346 | #if defined(ICANON) |
347 | GET_BOOL("ICANON", ICANON, c_lflag, ); |
348 | #endif |
349 | #if defined(ECHO) |
350 | GET_BOOL("ECHO", ECHO, c_lflag, ); |
351 | #endif |
352 | #if defined(IXON) |
353 | GET_BOOL("IXON", IXON, c_iflag, ); |
354 | #endif |
355 | #if defined(IXOFF) |
356 | GET_BOOL("IXOFF", IXOFF, c_iflag, ); |
357 | #endif |
e8269d30 |
358 | #if defined(OPOST) |
359 | GET_BOOL("OPOST", OPOST, c_oflag, ); |
360 | #endif |
c6ccd5c2 |
361 | |
362 | /* |
363 | * We do not propagate the following modes: |
364 | * - Parity/serial settings, which are a local affair and don't |
365 | * make sense propagated over SSH's 8-bit byte-stream. |
366 | * IGNPAR PARMRK INPCK CS7 CS8 PARENB PARODD |
367 | * - Things that want to be enabled in one place that we don't |
368 | * squash locally. |
7016cde4 |
369 | * IUCLC |
c6ccd5c2 |
370 | * - Status bits. |
371 | * PENDIN |
372 | * - Things I don't know what to do with. (FIXME) |
e8269d30 |
373 | * ISTRIP IMAXBEL NOFLSH TOSTOP IEXTEN |
374 | * INLCR IGNCR ICRNL |
c6ccd5c2 |
375 | */ |
376 | |
377 | #undef GET_CHAR |
378 | #undef GET_BOOL |
379 | |
380 | /* Fall through to here for unrecognised names, or ones that are |
381 | * unsupported on this platform */ |
382 | return NULL; |
383 | } |
384 | |
c5e438ec |
385 | void cleanup_termios(void) |
386 | { |
c6ccd5c2 |
387 | if (local_tty) |
21226fd7 |
388 | tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios); |
c5e438ec |
389 | } |
390 | |
391 | bufchain stdout_data, stderr_data; |
bc06669b |
392 | enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof; |
c5e438ec |
393 | |
885f465f |
394 | int try_output(int is_stderr) |
c5e438ec |
395 | { |
396 | bufchain *chain = (is_stderr ? &stderr_data : &stdout_data); |
21226fd7 |
397 | int fd = (is_stderr ? STDERR_FILENO : STDOUT_FILENO); |
c5e438ec |
398 | void *senddata; |
7c28d8d6 |
399 | int sendlen, ret, fl; |
c5e438ec |
400 | |
bc06669b |
401 | if (bufchain_size(chain) > 0) { |
402 | fl = fcntl(fd, F_GETFL); |
403 | if (fl != -1 && !(fl & O_NONBLOCK)) |
404 | fcntl(fd, F_SETFL, fl | O_NONBLOCK); |
405 | do { |
406 | bufchain_prefix(chain, &senddata, &sendlen); |
407 | ret = write(fd, senddata, sendlen); |
408 | if (ret > 0) |
409 | bufchain_consume(chain, ret); |
410 | } while (ret == sendlen && bufchain_size(chain) != 0); |
411 | if (fl != -1 && !(fl & O_NONBLOCK)) |
412 | fcntl(fd, F_SETFL, fl); |
413 | if (ret < 0 && errno != EAGAIN) { |
414 | perror(is_stderr ? "stderr: write" : "stdout: write"); |
415 | exit(1); |
416 | } |
417 | } |
418 | if (outgoingeof == EOF_PENDING && bufchain_size(&stdout_data) == 0) { |
419 | close(STDOUT_FILENO); |
420 | outgoingeof = EOF_SENT; |
c5e438ec |
421 | } |
885f465f |
422 | return bufchain_size(&stdout_data) + bufchain_size(&stderr_data); |
c5e438ec |
423 | } |
424 | |
9fab77dc |
425 | int from_backend(void *frontend_handle, int is_stderr, |
426 | const char *data, int len) |
c5e438ec |
427 | { |
c5e438ec |
428 | if (is_stderr) { |
429 | bufchain_add(&stderr_data, data, len); |
885f465f |
430 | return try_output(TRUE); |
c5e438ec |
431 | } else { |
bc06669b |
432 | assert(outgoingeof == EOF_NO); |
c5e438ec |
433 | bufchain_add(&stdout_data, data, len); |
885f465f |
434 | return try_output(FALSE); |
c5e438ec |
435 | } |
c5e438ec |
436 | } |
437 | |
edd0cb8a |
438 | int from_backend_untrusted(void *frontend_handle, const char *data, int len) |
439 | { |
440 | /* |
441 | * No "untrusted" output should get here (the way the code is |
442 | * currently, it's all diverted by FLAG_STDERR). |
443 | */ |
444 | assert(!"Unexpected call to from_backend_untrusted()"); |
445 | return 0; /* not reached */ |
446 | } |
447 | |
bc06669b |
448 | int from_backend_eof(void *frontend_handle) |
449 | { |
450 | assert(outgoingeof == EOF_NO); |
451 | outgoingeof = EOF_PENDING; |
452 | try_output(FALSE); |
453 | return FALSE; /* do not respond to incoming EOF with outgoing */ |
454 | } |
455 | |
edd0cb8a |
456 | int get_userpass_input(prompts_t *p, unsigned char *in, int inlen) |
457 | { |
458 | int ret; |
459 | ret = cmdline_get_passwd_input(p, in, inlen); |
460 | if (ret == -1) |
461 | ret = console_get_userpass_input(p, in, inlen); |
462 | return ret; |
463 | } |
464 | |
d0cb8c0c |
465 | /* |
466 | * Handle data from a local tty in PARMRK format. |
467 | */ |
deb0e1cf |
468 | static void from_tty(void *vbuf, unsigned len) |
d0cb8c0c |
469 | { |
deb0e1cf |
470 | char *p, *q, *end, *buf = vbuf; |
d0cb8c0c |
471 | static enum {NORMAL, FF, FF00} state = NORMAL; |
472 | |
473 | p = buf; end = buf + len; |
474 | while (p < end) { |
475 | switch (state) { |
476 | case NORMAL: |
477 | if (*p == '\xff') { |
478 | p++; |
479 | state = FF; |
480 | } else { |
481 | q = memchr(p, '\xff', end - p); |
482 | if (q == NULL) q = end; |
483 | back->send(backhandle, p, q - p); |
484 | p = q; |
485 | } |
486 | break; |
487 | case FF: |
488 | if (*p == '\xff') { |
489 | back->send(backhandle, p, 1); |
490 | p++; |
491 | state = NORMAL; |
492 | } else if (*p == '\0') { |
493 | p++; |
494 | state = FF00; |
495 | } else abort(); |
496 | break; |
497 | case FF00: |
498 | if (*p == '\0') { |
499 | back->special(backhandle, TS_BRK); |
500 | } else { |
501 | /* |
502 | * Pretend that PARMRK wasn't set. This involves |
503 | * faking what INPCK and IGNPAR would have done if |
504 | * we hadn't overridden them. Unfortunately, we |
505 | * can't do this entirely correctly because INPCK |
506 | * distinguishes between framing and parity |
507 | * errors, but PARMRK format represents both in |
508 | * the same way. We assume that parity errors are |
509 | * more common than framing errors, and hence |
510 | * treat all input errors as being subject to |
511 | * INPCK. |
512 | */ |
513 | if (orig_termios.c_iflag & INPCK) { |
514 | /* If IGNPAR is set, we throw away the character. */ |
515 | if (!(orig_termios.c_iflag & IGNPAR)) { |
516 | /* PE/FE get passed on as NUL. */ |
517 | *p = 0; |
518 | back->send(backhandle, p, 1); |
519 | } |
520 | } else { |
521 | /* INPCK not set. Assume we got a parity error. */ |
522 | back->send(backhandle, p, 1); |
523 | } |
524 | } |
525 | p++; |
526 | state = NORMAL; |
527 | } |
528 | } |
529 | } |
530 | |
5673d44e |
531 | int signalpipe[2]; |
532 | |
533 | void sigwinch(int signum) |
534 | { |
ecb25722 |
535 | if (write(signalpipe[1], "x", 1) <= 0) |
536 | /* not much we can do about it */; |
5673d44e |
537 | } |
538 | |
c5e438ec |
539 | /* |
74aca06d |
540 | * In Plink our selects are synchronous, so these functions are |
541 | * empty stubs. |
542 | */ |
543 | int uxsel_input_add(int fd, int rwx) { return 0; } |
544 | void uxsel_input_remove(int id) { } |
545 | |
546 | /* |
c5e438ec |
547 | * Short description of parameters. |
548 | */ |
549 | static void usage(void) |
550 | { |
551 | printf("PuTTY Link: command-line connection utility\n"); |
552 | printf("%s\n", ver); |
553 | printf("Usage: plink [options] [user@]host [command]\n"); |
554 | printf(" (\"host\" can also be a PuTTY saved session name)\n"); |
555 | printf("Options:\n"); |
2285d016 |
556 | printf(" -V print version information and exit\n"); |
557 | printf(" -pgpfp print PGP key fingerprints and exit\n"); |
c5e438ec |
558 | printf(" -v show verbose messages\n"); |
559 | printf(" -load sessname Load settings from saved session\n"); |
f41f07bc |
560 | printf(" -ssh -telnet -rlogin -raw -serial\n"); |
afd4d0d2 |
561 | printf(" force use of a particular protocol\n"); |
c5e438ec |
562 | printf(" -P port connect to specified port\n"); |
563 | printf(" -l user connect with specified username\n"); |
c5e438ec |
564 | printf(" -batch disable all interactive prompts\n"); |
565 | printf("The following options only apply to SSH connections:\n"); |
566 | printf(" -pw passw login with specified password\n"); |
dbe6c525 |
567 | printf(" -D [listen-IP:]listen-port\n"); |
568 | printf(" Dynamic SOCKS-based port forwarding\n"); |
569 | printf(" -L [listen-IP:]listen-port:host:port\n"); |
570 | printf(" Forward local port to remote address\n"); |
571 | printf(" -R [listen-IP:]listen-port:host:port\n"); |
572 | printf(" Forward remote port to local address\n"); |
c5e438ec |
573 | printf(" -X -x enable / disable X11 forwarding\n"); |
574 | printf(" -A -a enable / disable agent forwarding\n"); |
575 | printf(" -t -T enable / disable pty allocation\n"); |
576 | printf(" -1 -2 force use of particular protocol version\n"); |
05581745 |
577 | printf(" -4 -6 force use of IPv4 or IPv6\n"); |
c5e438ec |
578 | printf(" -C enable compression\n"); |
579 | printf(" -i key private key file for authentication\n"); |
e5708bc7 |
580 | printf(" -noagent disable use of Pageant\n"); |
581 | printf(" -agent enable use of Pageant\n"); |
54018d95 |
582 | printf(" -m file read remote command(s) from file\n"); |
09bdfcbb |
583 | printf(" -s remote command is an SSH subsystem (SSH-2 only)\n"); |
b72c366d |
584 | printf(" -N don't start a shell/command (SSH-2 only)\n"); |
23828b7e |
585 | printf(" -nc host:port\n"); |
586 | printf(" open tunnel in place of session (SSH-2 only)\n"); |
9621bbab |
587 | printf(" -sercfg configuration-string (e.g. 19200,8,n,1,X)\n"); |
588 | printf(" Specify the serial configuration (serial only)\n"); |
dc108ebc |
589 | exit(1); |
590 | } |
591 | |
592 | static void version(void) |
593 | { |
594 | printf("plink: %s\n", ver); |
c5e438ec |
595 | exit(1); |
596 | } |
597 | |
43a1c4a4 |
598 | void frontend_net_error_pending(void) {} |
599 | |
c5e438ec |
600 | int main(int argc, char **argv) |
601 | { |
602 | int sending; |
603 | int portnumber = -1; |
0ff9ea38 |
604 | int *fdlist; |
605 | int fd; |
606 | int i, fdcount, fdsize, fdstate; |
c5e438ec |
607 | int connopen; |
608 | int exitcode; |
86256dc6 |
609 | int errors; |
09bdfcbb |
610 | int use_subsystem = 0; |
f0f51e90 |
611 | int got_host = FALSE; |
d719927e |
612 | unsigned long now; |
679db585 |
613 | struct winsize size; |
c5e438ec |
614 | |
0ff9ea38 |
615 | fdlist = NULL; |
616 | fdcount = fdsize = 0; |
c5e438ec |
617 | /* |
618 | * Initialise port and protocol to sensible defaults. (These |
619 | * will be overridden by more or less anything.) |
620 | */ |
621 | default_protocol = PROT_SSH; |
622 | default_port = 22; |
623 | |
bc06669b |
624 | bufchain_init(&stdout_data); |
625 | bufchain_init(&stderr_data); |
626 | outgoingeof = EOF_NO; |
627 | |
f7397dc6 |
628 | flags = FLAG_STDERR | FLAG_STDERR_TTY; |
629 | |
630 | stderr_tty_init(); |
c5e438ec |
631 | /* |
632 | * Process the command line. |
633 | */ |
4a693cfc |
634 | conf = conf_new(); |
635 | do_defaults(NULL, conf); |
18e62ad8 |
636 | loaded_session = FALSE; |
4a693cfc |
637 | default_protocol = conf_get_int(conf, CONF_protocol); |
638 | default_port = conf_get_int(conf, CONF_port); |
86256dc6 |
639 | errors = 0; |
c5e438ec |
640 | { |
641 | /* |
642 | * Override the default protocol if PLINK_PROTOCOL is set. |
643 | */ |
644 | char *p = getenv("PLINK_PROTOCOL"); |
c5e438ec |
645 | if (p) { |
9e164d82 |
646 | const Backend *b = backend_from_name(p); |
647 | if (b) { |
4a693cfc |
648 | default_protocol = b->protocol; |
649 | default_port = b->default_port; |
650 | conf_set_int(conf, CONF_protocol, default_protocol); |
651 | conf_set_int(conf, CONF_port, default_port); |
c5e438ec |
652 | } |
653 | } |
654 | } |
655 | while (--argc) { |
656 | char *p = *++argv; |
657 | if (*p == '-') { |
9b41d3a8 |
658 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), |
4a693cfc |
659 | 1, conf); |
c5e438ec |
660 | if (ret == -2) { |
661 | fprintf(stderr, |
662 | "plink: option \"%s\" requires an argument\n", p); |
86256dc6 |
663 | errors = 1; |
c5e438ec |
664 | } else if (ret == 2) { |
665 | --argc, ++argv; |
666 | } else if (ret == 1) { |
667 | continue; |
668 | } else if (!strcmp(p, "-batch")) { |
669 | console_batch_mode = 1; |
09bdfcbb |
670 | } else if (!strcmp(p, "-s")) { |
4a693cfc |
671 | /* Save status to write to conf later. */ |
09bdfcbb |
672 | use_subsystem = 1; |
bcb1823f |
673 | } else if (!strcmp(p, "-V") || !strcmp(p, "--version")) { |
dc108ebc |
674 | version(); |
bcb1823f |
675 | } else if (!strcmp(p, "--help")) { |
676 | usage(); |
677 | exit(0); |
2285d016 |
678 | } else if (!strcmp(p, "-pgpfp")) { |
679 | pgp_fingerprints(); |
680 | exit(1); |
a0e5ed33 |
681 | } else if (!strcmp(p, "-o")) { |
86256dc6 |
682 | if (argc <= 1) { |
a0e5ed33 |
683 | fprintf(stderr, |
684 | "plink: option \"-o\" requires an argument\n"); |
86256dc6 |
685 | errors = 1; |
686 | } else { |
687 | --argc; |
688 | provide_xrm_string(*++argv); |
689 | } |
690 | } else { |
691 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
692 | errors = 1; |
c5e438ec |
693 | } |
694 | } else if (*p) { |
4a693cfc |
695 | if (!conf_launchable(conf) || !(got_host || loaded_session)) { |
c5e438ec |
696 | char *q = p; |
a0e5ed33 |
697 | |
c5e438ec |
698 | /* |
699 | * If the hostname starts with "telnet:", set the |
700 | * protocol to Telnet and process the string as a |
701 | * Telnet URL. |
702 | */ |
703 | if (!strncmp(q, "telnet:", 7)) { |
704 | char c; |
705 | |
706 | q += 7; |
707 | if (q[0] == '/' && q[1] == '/') |
708 | q += 2; |
4a693cfc |
709 | conf_set_int(conf, CONF_protocol, PROT_TELNET); |
c5e438ec |
710 | p = q; |
711 | while (*p && *p != ':' && *p != '/') |
712 | p++; |
713 | c = *p; |
714 | if (*p) |
715 | *p++ = '\0'; |
716 | if (c == ':') |
4a693cfc |
717 | conf_set_int(conf, CONF_port, atoi(p)); |
c5e438ec |
718 | else |
4a693cfc |
719 | conf_set_int(conf, CONF_port, -1); |
720 | conf_set_str(conf, CONF_host, q); |
f0f51e90 |
721 | got_host = TRUE; |
c5e438ec |
722 | } else { |
3608528b |
723 | char *r, *user, *host; |
c5e438ec |
724 | /* |
725 | * Before we process the [user@]host string, we |
726 | * first check for the presence of a protocol |
727 | * prefix (a protocol name followed by ","). |
728 | */ |
729 | r = strchr(p, ','); |
730 | if (r) { |
9e164d82 |
731 | const Backend *b; |
732 | *r = '\0'; |
733 | b = backend_from_name(p); |
734 | if (b) { |
4a693cfc |
735 | default_protocol = b->protocol; |
736 | conf_set_int(conf, CONF_protocol, |
737 | default_protocol); |
9e164d82 |
738 | portnumber = b->default_port; |
c5e438ec |
739 | } |
9e164d82 |
740 | p = r + 1; |
c5e438ec |
741 | } |
742 | |
743 | /* |
3608528b |
744 | * A nonzero length string followed by an @ is treated |
745 | * as a username. (We discount an _initial_ @.) The |
746 | * rest of the string (or the whole string if no @) |
747 | * is treated as a session name and/or hostname. |
c5e438ec |
748 | */ |
749 | r = strrchr(p, '@'); |
750 | if (r == p) |
751 | p++, r = NULL; /* discount initial @ */ |
3608528b |
752 | if (r) { |
753 | *r++ = '\0'; |
754 | user = p, host = r; |
755 | } else { |
756 | user = NULL, host = p; |
757 | } |
758 | |
759 | /* |
760 | * Now attempt to load a saved session with the |
761 | * same name as the hostname. |
762 | */ |
763 | { |
4a693cfc |
764 | Conf *conf2 = conf_new(); |
765 | do_defaults(host, conf2); |
766 | if (loaded_session || !conf_launchable(conf2)) { |
c5e438ec |
767 | /* No settings for this host; use defaults */ |
18e62ad8 |
768 | /* (or session was already loaded with -load) */ |
4a693cfc |
769 | conf_set_str(conf, CONF_host, host); |
770 | conf_set_int(conf, CONF_port, default_port); |
f0f51e90 |
771 | got_host = TRUE; |
c5e438ec |
772 | } else { |
4a693cfc |
773 | conf_copy_into(conf, conf2); |
f0f51e90 |
774 | loaded_session = TRUE; |
c5e438ec |
775 | } |
4a693cfc |
776 | conf_free(conf2); |
3608528b |
777 | } |
778 | |
779 | if (user) { |
780 | /* Patch in specified username. */ |
4a693cfc |
781 | conf_set_str(conf, CONF_username, user); |
c5e438ec |
782 | } |
3608528b |
783 | |
c5e438ec |
784 | } |
785 | } else { |
786 | char *command; |
787 | int cmdlen, cmdsize; |
788 | cmdlen = cmdsize = 0; |
789 | command = NULL; |
790 | |
791 | while (argc) { |
792 | while (*p) { |
793 | if (cmdlen >= cmdsize) { |
794 | cmdsize = cmdlen + 512; |
3d88e64d |
795 | command = sresize(command, cmdsize, char); |
c5e438ec |
796 | } |
797 | command[cmdlen++]=*p++; |
798 | } |
799 | if (cmdlen >= cmdsize) { |
800 | cmdsize = cmdlen + 512; |
3d88e64d |
801 | command = sresize(command, cmdsize, char); |
c5e438ec |
802 | } |
803 | command[cmdlen++]=' '; /* always add trailing space */ |
804 | if (--argc) p = *++argv; |
805 | } |
806 | if (cmdlen) command[--cmdlen]='\0'; |
807 | /* change trailing blank to NUL */ |
4a693cfc |
808 | conf_set_str(conf, CONF_remote_cmd, command); |
809 | conf_set_str(conf, CONF_remote_cmd2, ""); |
810 | conf_set_int(conf, CONF_nopty, TRUE); /* command => no tty */ |
c5e438ec |
811 | |
812 | break; /* done with cmdline */ |
813 | } |
814 | } |
815 | } |
816 | |
86256dc6 |
817 | if (errors) |
818 | return 1; |
819 | |
4a693cfc |
820 | if (!conf_launchable(conf) || !(got_host || loaded_session)) { |
c5e438ec |
821 | usage(); |
822 | } |
823 | |
824 | /* |
4a693cfc |
825 | * Muck about with the hostname in various ways. |
c5e438ec |
826 | */ |
827 | { |
4a693cfc |
828 | char *hostbuf = dupstr(conf_get_str(conf, CONF_host)); |
829 | char *host = hostbuf; |
830 | char *p, *q; |
831 | |
832 | /* |
833 | * Trim leading whitespace. |
834 | */ |
835 | host += strspn(host, " \t"); |
c5e438ec |
836 | |
4a693cfc |
837 | /* |
838 | * See if host is of the form user@host, and separate out |
839 | * the username if so. |
840 | */ |
841 | if (host[0] != '\0') { |
842 | char *atsign = strrchr(host, '@'); |
843 | if (atsign) { |
844 | *atsign = '\0'; |
845 | conf_set_str(conf, CONF_username, host); |
846 | host = atsign + 1; |
c5e438ec |
847 | } |
c5e438ec |
848 | } |
4a693cfc |
849 | |
850 | /* |
851 | * Trim off a colon suffix if it's there. |
852 | */ |
853 | host[strcspn(host, ":")] = '\0'; |
854 | |
855 | /* |
856 | * Remove any remaining whitespace. |
857 | */ |
858 | p = hostbuf; |
859 | q = host; |
860 | while (*q) { |
861 | if (*q != ' ' && *q != '\t') |
862 | *p++ = *q; |
863 | q++; |
864 | } |
865 | *p = '\0'; |
866 | |
867 | conf_set_str(conf, CONF_host, hostbuf); |
868 | sfree(hostbuf); |
c5e438ec |
869 | } |
870 | |
871 | /* |
872 | * Perform command-line overrides on session configuration. |
873 | */ |
4a693cfc |
874 | cmdline_run_saved(conf); |
c5e438ec |
875 | |
876 | /* |
09bdfcbb |
877 | * Apply subsystem status. |
878 | */ |
879 | if (use_subsystem) |
4a693cfc |
880 | conf_set_int(conf, CONF_ssh_subsys, TRUE); |
c5e438ec |
881 | |
4a693cfc |
882 | if (!*conf_get_str(conf, CONF_remote_cmd) && |
883 | !*conf_get_str(conf, CONF_remote_cmd2) && |
884 | !*conf_get_str(conf, CONF_ssh_nc_host)) |
c5e438ec |
885 | flags |= FLAG_INTERACTIVE; |
886 | |
887 | /* |
888 | * Select protocol. This is farmed out into a table in a |
889 | * separate file to enable an ssh-free variant. |
890 | */ |
4a693cfc |
891 | back = backend_from_proto(conf_get_int(conf, CONF_protocol)); |
9e164d82 |
892 | if (back == NULL) { |
893 | fprintf(stderr, |
894 | "Internal fault: Unsupported protocol found\n"); |
895 | return 1; |
c5e438ec |
896 | } |
897 | |
898 | /* |
899 | * Select port. |
900 | */ |
901 | if (portnumber != -1) |
4a693cfc |
902 | conf_set_int(conf, CONF_port, portnumber); |
c5e438ec |
903 | |
5673d44e |
904 | /* |
1f39cbd0 |
905 | * Block SIGPIPE, so that we'll get EPIPE individually on |
906 | * particular network connections that go wrong. |
907 | */ |
908 | putty_signal(SIGPIPE, SIG_IGN); |
909 | |
910 | /* |
5673d44e |
911 | * Set up the pipe we'll use to tell us about SIGWINCH. |
912 | */ |
913 | if (pipe(signalpipe) < 0) { |
914 | perror("pipe"); |
915 | exit(1); |
916 | } |
917 | putty_signal(SIGWINCH, sigwinch); |
918 | |
679db585 |
919 | /* |
920 | * Now that we've got the SIGWINCH handler installed, try to find |
921 | * out the initial terminal size. |
922 | */ |
923 | if (ioctl(STDIN_FILENO, TIOCGWINSZ, &size) >= 0) { |
924 | conf_set_int(conf, CONF_width, size.ws_col); |
925 | conf_set_int(conf, CONF_height, size.ws_row); |
926 | } |
927 | |
c5e438ec |
928 | sk_init(); |
0ff9ea38 |
929 | uxsel_init(); |
c5e438ec |
930 | |
931 | /* |
61f62bb3 |
932 | * Unix Plink doesn't provide any way to add forwardings after the |
933 | * connection is set up, so if there are none now, we can safely set |
934 | * the "simple" flag. |
935 | */ |
4a693cfc |
936 | if (conf_get_int(conf, CONF_protocol) == PROT_SSH && |
937 | !conf_get_int(conf, CONF_x11_forward) && |
938 | !conf_get_int(conf, CONF_agentfwd) && |
939 | !conf_get_str_nthstrkey(conf, CONF_portfwd, 0)) |
940 | conf_set_int(conf, CONF_ssh_simple, TRUE); |
941 | |
61f62bb3 |
942 | /* |
c5e438ec |
943 | * Start up the connection. |
944 | */ |
4a693cfc |
945 | logctx = log_init(NULL, conf); |
b51259f6 |
946 | console_provide_logctx(logctx); |
c5e438ec |
947 | { |
cbe2d68f |
948 | const char *error; |
c5e438ec |
949 | char *realhost; |
950 | /* nodelay is only useful if stdin is a terminal device */ |
4a693cfc |
951 | int nodelay = conf_get_int(conf, CONF_tcp_nodelay) && isatty(0); |
c5e438ec |
952 | |
4a693cfc |
953 | error = back->init(NULL, &backhandle, conf, |
954 | conf_get_str(conf, CONF_host), |
955 | conf_get_int(conf, CONF_port), |
956 | &realhost, nodelay, |
957 | conf_get_int(conf, CONF_tcp_keepalives)); |
c5e438ec |
958 | if (error) { |
984992ef |
959 | fprintf(stderr, "Unable to open connection:\n%s\n", error); |
c5e438ec |
960 | return 1; |
961 | } |
c5e438ec |
962 | back->provide_logctx(backhandle, logctx); |
4a693cfc |
963 | ldisc_create(conf, NULL, back, backhandle, NULL); |
c5e438ec |
964 | sfree(realhost); |
965 | } |
966 | connopen = 1; |
967 | |
968 | /* |
969 | * Set up the initial console mode. We don't care if this call |
970 | * fails, because we know we aren't necessarily running in a |
971 | * console. |
972 | */ |
21226fd7 |
973 | local_tty = (tcgetattr(STDIN_FILENO, &orig_termios) == 0); |
c5e438ec |
974 | atexit(cleanup_termios); |
975 | ldisc_update(NULL, 1, 1); |
976 | sending = FALSE; |
39934deb |
977 | now = GETTICKCOUNT(); |
c5e438ec |
978 | |
979 | while (1) { |
980 | fd_set rset, wset, xset; |
981 | int maxfd; |
982 | int rwx; |
983 | int ret; |
984 | |
985 | FD_ZERO(&rset); |
986 | FD_ZERO(&wset); |
987 | FD_ZERO(&xset); |
988 | maxfd = 0; |
989 | |
5673d44e |
990 | FD_SET_MAX(signalpipe[0], maxfd, rset); |
991 | |
c5e438ec |
992 | if (connopen && !sending && |
6226c939 |
993 | back->connected(backhandle) && |
c5e438ec |
994 | back->sendok(backhandle) && |
995 | back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) { |
996 | /* If we're OK to send, then try to read from stdin. */ |
21226fd7 |
997 | FD_SET_MAX(STDIN_FILENO, maxfd, rset); |
c5e438ec |
998 | } |
999 | |
1000 | if (bufchain_size(&stdout_data) > 0) { |
1001 | /* If we have data for stdout, try to write to stdout. */ |
21226fd7 |
1002 | FD_SET_MAX(STDOUT_FILENO, maxfd, wset); |
c5e438ec |
1003 | } |
1004 | |
1005 | if (bufchain_size(&stderr_data) > 0) { |
1006 | /* If we have data for stderr, try to write to stderr. */ |
21226fd7 |
1007 | FD_SET_MAX(STDERR_FILENO, maxfd, wset); |
c5e438ec |
1008 | } |
1009 | |
0ff9ea38 |
1010 | /* Count the currently active fds. */ |
c5e438ec |
1011 | i = 0; |
0ff9ea38 |
1012 | for (fd = first_fd(&fdstate, &rwx); fd >= 0; |
1013 | fd = next_fd(&fdstate, &rwx)) i++; |
c5e438ec |
1014 | |
0ff9ea38 |
1015 | /* Expand the fdlist buffer if necessary. */ |
1016 | if (i > fdsize) { |
1017 | fdsize = i + 16; |
1018 | fdlist = sresize(fdlist, fdsize, int); |
c5e438ec |
1019 | } |
1020 | |
1021 | /* |
0ff9ea38 |
1022 | * Add all currently open fds to the select sets, and store |
1023 | * them in fdlist as well. |
c5e438ec |
1024 | */ |
0ff9ea38 |
1025 | fdcount = 0; |
1026 | for (fd = first_fd(&fdstate, &rwx); fd >= 0; |
1027 | fd = next_fd(&fdstate, &rwx)) { |
1028 | fdlist[fdcount++] = fd; |
c5e438ec |
1029 | if (rwx & 1) |
0ff9ea38 |
1030 | FD_SET_MAX(fd, maxfd, rset); |
c5e438ec |
1031 | if (rwx & 2) |
0ff9ea38 |
1032 | FD_SET_MAX(fd, maxfd, wset); |
c5e438ec |
1033 | if (rwx & 4) |
0ff9ea38 |
1034 | FD_SET_MAX(fd, maxfd, xset); |
c5e438ec |
1035 | } |
1036 | |
5673d44e |
1037 | do { |
d719927e |
1038 | unsigned long next, then; |
1039 | long ticks; |
39934deb |
1040 | struct timeval tv, *ptv; |
1041 | |
1042 | if (run_timers(now, &next)) { |
d719927e |
1043 | then = now; |
1044 | now = GETTICKCOUNT(); |
1045 | if (now - then > next - then) |
1046 | ticks = 0; |
1047 | else |
1048 | ticks = next - now; |
39934deb |
1049 | tv.tv_sec = ticks / 1000; |
1050 | tv.tv_usec = ticks % 1000 * 1000; |
1051 | ptv = &tv; |
1052 | } else { |
1053 | ptv = NULL; |
1054 | } |
1055 | ret = select(maxfd, &rset, &wset, &xset, ptv); |
1056 | if (ret == 0) |
1057 | now = next; |
c2eb6cb7 |
1058 | else |
1059 | now = GETTICKCOUNT(); |
5673d44e |
1060 | } while (ret < 0 && errno == EINTR); |
c5e438ec |
1061 | |
1062 | if (ret < 0) { |
1063 | perror("select"); |
1064 | exit(1); |
1065 | } |
1066 | |
0ff9ea38 |
1067 | for (i = 0; i < fdcount; i++) { |
1068 | fd = fdlist[i]; |
56e5b2db |
1069 | /* |
1070 | * We must process exceptional notifications before |
1071 | * ordinary readability ones, or we may go straight |
1072 | * past the urgent marker. |
1073 | */ |
0ff9ea38 |
1074 | if (FD_ISSET(fd, &xset)) |
1075 | select_result(fd, 4); |
1076 | if (FD_ISSET(fd, &rset)) |
1077 | select_result(fd, 1); |
1078 | if (FD_ISSET(fd, &wset)) |
1079 | select_result(fd, 2); |
c5e438ec |
1080 | } |
1081 | |
5673d44e |
1082 | if (FD_ISSET(signalpipe[0], &rset)) { |
1083 | char c[1]; |
1084 | struct winsize size; |
ecb25722 |
1085 | if (read(signalpipe[0], c, 1) <= 0) |
1086 | /* ignore error */; |
1087 | /* ignore its value; it'll be `x' */ |
679db585 |
1088 | if (ioctl(STDIN_FILENO, TIOCGWINSZ, (void *)&size) >= 0) |
5673d44e |
1089 | back->size(backhandle, size.ws_col, size.ws_row); |
1090 | } |
1091 | |
21226fd7 |
1092 | if (FD_ISSET(STDIN_FILENO, &rset)) { |
c5e438ec |
1093 | char buf[4096]; |
1094 | int ret; |
1095 | |
6226c939 |
1096 | if (connopen && back->connected(backhandle)) { |
21226fd7 |
1097 | ret = read(STDIN_FILENO, buf, sizeof(buf)); |
c5e438ec |
1098 | if (ret < 0) { |
1099 | perror("stdin: read"); |
1100 | exit(1); |
1101 | } else if (ret == 0) { |
1102 | back->special(backhandle, TS_EOF); |
1103 | sending = FALSE; /* send nothing further after this */ |
1104 | } else { |
d0cb8c0c |
1105 | if (local_tty) |
1106 | from_tty(buf, ret); |
1107 | else |
1108 | back->send(backhandle, buf, ret); |
c5e438ec |
1109 | } |
1110 | } |
1111 | } |
1112 | |
21226fd7 |
1113 | if (FD_ISSET(STDOUT_FILENO, &wset)) { |
885f465f |
1114 | back->unthrottle(backhandle, try_output(FALSE)); |
c5e438ec |
1115 | } |
1116 | |
21226fd7 |
1117 | if (FD_ISSET(STDERR_FILENO, &wset)) { |
885f465f |
1118 | back->unthrottle(backhandle, try_output(TRUE)); |
c5e438ec |
1119 | } |
1120 | |
43a1c4a4 |
1121 | net_pending_errors(); |
1122 | |
6226c939 |
1123 | if ((!connopen || !back->connected(backhandle)) && |
c5e438ec |
1124 | bufchain_size(&stdout_data) == 0 && |
1125 | bufchain_size(&stderr_data) == 0) |
1126 | break; /* we closed the connection */ |
1127 | } |
1128 | exitcode = back->exitcode(backhandle); |
1129 | if (exitcode < 0) { |
1130 | fprintf(stderr, "Remote process exit code unavailable\n"); |
1131 | exitcode = 1; /* this is an error condition */ |
1132 | } |
d9c40fd6 |
1133 | cleanup_exit(exitcode); |
1134 | return exitcode; /* shouldn't happen, but placates gcc */ |
c5e438ec |
1135 | } |