-
Notifications
You must be signed in to change notification settings - Fork 38
/
passh.c
1017 lines (873 loc) Β· 27.1 KB
/
passh.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* passh - automate ssh password authentication
Copyright (C) 2017-2020 Clark Wang <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* NOTE:
* - Interactive only when stdin is a tty.
* - In interactive mode, will not send passwords any more after user starts
* inputting from the keyboard.
*/
/*
* - On OS X EI Capitan (10.11.6), definging _XOPEN_SOURCE=600 would cause
* SIGWINCH to be undefined.
*/
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(_AIX)
#define _XOPEN_SOURCE 600 /* for posix_openpt() */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <regex.h>
#include <time.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#define BUFFSIZE (8 * 1024)
#define DEFAULT_COUNT 0
#define DEFAULT_TIMEOUT 0
#define DEFAULT_PASSWD "password"
#define DEFAULT_PROMPT "[Pp]assword: \\{0,1\\}$"
#define DEFAULT_YESNO "(yes/no)? \\{0,1\\}$"
#define ERROR_GENERAL (200 + 1)
#define ERROR_USAGE (200 + 2)
#define ERROR_TIMEOUT (200 + 3)
#define ERROR_SYS (200 + 4)
#define ERROR_MAX_TRIES (200 + 5)
char * const MY_NAME = "passh";
char * const VERSION_ = "1.0.2";
static struct {
char *progname;
bool reset_on_exit;
struct termios save_termios;
bool SIGCHLDed;
bool received_winch;
bool stdin_is_tty;
bool now_interactive;
int fd_ptym;
struct {
bool ignore_case;
bool nohup_child;
bool fatal_no_prompt;
bool auto_yesno;
char *password;
char *passwd_prompt;
char *yesno_prompt;
regex_t re_prompt;
regex_t re_yesno;
int timeout;
int tries;
bool fatal_more_tries;
char **command;
char *log_to_pty;
char *log_from_pty;
} opt;
} g;
void
show_version(void)
{
printf("%s %s\n", MY_NAME, VERSION_);
exit(0);
}
void
usage(int exitcode)
{
printf("Usage: %s [OPTION]... COMMAND...\n"
"\n"
" -c <N> Send at most <N> passwords (0 means infinite. Default: %d)\n"
" -C Exit if prompted for the <N+1>th password\n"
" -h Help\n"
" -i Case insensitive for password prompt matching\n"
" -n Nohup the child (e.g. used for `ssh -f')\n"
" -p <password> The password (Default: `" DEFAULT_PASSWD "')\n"
" -p env:<var> Read password from env var\n"
" -p file:<file> Read password from file\n"
" -P <prompt> Regexp (BRE) for the password prompt\n"
" (Default: `" DEFAULT_PROMPT "')\n"
" -l <file> Save data written to the pty\n"
" -L <file> Save data read from the pty\n"
" -t <timeout> Timeout waiting for next password prompt\n"
" (0 means no timeout. Default: %d)\n"
" -T Exit if timed out waiting for password prompt\n"
" -V Show version\n"
" -y Auto answer `(yes/no)?' questions\n"
#if 0
" -Y <pattern> Regexp (BRE) for the `yes/no' prompt\n"
" (Default: `" DEFAULT_YESNO "')\n"
#endif
"\n"
"Report bugs to Clark Wang <[email protected]>\n"
"", g.progname, DEFAULT_COUNT, DEFAULT_TIMEOUT);
exit(exitcode);
}
void
fatal(int rcode, const char *fmt, ...)
{
va_list ap;
char buf[1024];
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
/* in case stdout and stderr are the same */
fflush(stdout);
fprintf(stderr, "!! %s\r\n", buf);
/* flush all open files */
fflush(NULL);
exit(rcode);
}
void
fatal_sys(const char *fmt, ...)
{
va_list ap;
char buf[1024];
int error = errno;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
fatal(ERROR_SYS, "%s: %s (%d)", buf, strerror(error), error);
}
void
startup()
{
g.opt.passwd_prompt = DEFAULT_PROMPT;
g.opt.yesno_prompt = DEFAULT_YESNO;
g.opt.password = DEFAULT_PASSWD;
g.opt.tries = DEFAULT_COUNT;
g.opt.timeout = DEFAULT_TIMEOUT;
}
char *
arg2pass(char *optarg)
{
char *pass = NULL;
if (strncmp(optarg, "file:", 5) == 0) {
FILE *fp = fopen(optarg + 5, "r");
char buf[1024] = "";
if (fp == NULL) {
fatal_sys("failed to open file %s", optarg + 5);
}
if (fgets(buf, sizeof(buf), fp) == NULL) {
fatal(ERROR_GENERAL, "failed to read the file");
}
fclose(fp);
pass = strtok(buf, "\r\n");
if (pass) {
pass = strdup(pass);
} else {
pass = strdup("");
}
} else if (strncmp(optarg, "env:", 4) == 0) {
pass = getenv(optarg + 4);
if (pass) {
pass = strdup(pass);
} else {
fatal(ERROR_GENERAL, "env var not found: %s", optarg + 4);
}
} else {
pass = strdup(optarg);
}
return pass;
}
void
getargs(int argc, char **argv)
{
int ch, i, r, reflag;
if ((g.progname = strrchr(argv[0], '/')) != NULL) {
++g.progname;
} else {
g.progname = argv[0];
}
if (argc == 1 || (argc == 2 && strcmp("--help", argv[1]) == 0) ) {
usage(0);
}
if (argc == 2 && strcmp("--version", argv[1]) == 0) {
show_version();
}
/*
* If the first character of optstring is '+' or the environment variable
* POSIXLY_CORRECT is set, then option processing stops as soon as a
* nonoption argument is encountered.
*/
while ((ch = getopt(argc, argv, "+:c:Chil:L:np:P:t:TVy")) != -1) {
switch (ch) {
case 'c':
g.opt.tries = atoi(optarg);
break;
case 'C':
g.opt.fatal_more_tries = true;
break;
case 'h':
usage(0);
case 'i':
g.opt.ignore_case = true;
break;
case 'l':
g.opt.log_to_pty = optarg;
break;
case 'L':
g.opt.log_from_pty = optarg;
break;
case 'n':
g.opt.nohup_child = true;
break;
case 'p':
g.opt.password = arg2pass(optarg);
for (i = 0; i < strlen(optarg); ++i) {
optarg[i] = '*';
}
if (g.opt.password == NULL) {
fatal(ERROR_USAGE, "Error: failed to get password");
}
break;
case 'P':
g.opt.passwd_prompt = optarg;
break;
case 't':
g.opt.timeout = atoi(optarg);
break;
case 'T':
g.opt.fatal_no_prompt = true;
break;
case 'V':
show_version();
break;
case 'y':
g.opt.auto_yesno = true;
break;
#if 0
case 'Y':
g.opt.yesno_prompt = optarg;
break;
#endif
case ':':
fatal(ERROR_USAGE, "Error: option '-%c' requires an argument", optopt);
break;
case '?':
default:
fatal(ERROR_USAGE, "Error: unknown option '-%c'", optopt);
}
}
argc -= optind;
argv += optind;
if (0 == argc) {
fatal(ERROR_USAGE, "Error: no command specified");
}
g.opt.command = argv;
if (0 == strlen(g.opt.passwd_prompt) ) {
fatal(ERROR_USAGE, "Error: empty prompt");
}
/* Password: */
reflag = 0;
reflag |= g.opt.ignore_case ? REG_ICASE : 0;
r = regcomp(&g.opt.re_prompt, g.opt.passwd_prompt, reflag);
if (r != 0) {
fatal(ERROR_USAGE, "Error: invalid RE for password prompt");
}
/* (yes/no)? */
r = regcomp(&g.opt.re_yesno, g.opt.yesno_prompt, reflag);
if (r != 0) {
fatal(ERROR_USAGE, "Error: invalid RE for yes/no prompt");
}
}
int
ptym_open(char *pts_name, int pts_namesz)
{
char *ptr;
int fdm;
snprintf(pts_name, pts_namesz, "/dev/ptmx");
fdm = posix_openpt(O_RDWR);
if (fdm < 0)
return (-1);
if (grantpt(fdm) < 0) {
close(fdm);
return (-2);
}
if (unlockpt(fdm) < 0) {
close(fdm);
return (-3);
}
if ((ptr = ptsname(fdm)) == NULL) {
close(fdm);
return (-4);
}
snprintf(pts_name, pts_namesz, "%s", ptr);
return (fdm);
}
int
ptys_open(char *pts_name)
{
int fds;
if ((fds = open(pts_name, O_RDWR)) < 0)
return (-5);
return (fds);
}
pid_t
pty_fork(int *ptrfdm, char *slave_name, int slave_namesz,
const struct termios *slave_termios,
const struct winsize *slave_winsize)
{
int fdm, fds;
pid_t pid;
char pts_name[32];
if ((fdm = ptym_open(pts_name, sizeof(pts_name))) < 0)
fatal_sys("can't open master pty: %s, error %d", pts_name, fdm);
if (slave_name != NULL) {
/*
* Return name of slave. Null terminate to handle case
* where strlen(pts_name) > slave_namesz.
*/
snprintf(slave_name, slave_namesz, "%s", pts_name);
}
if ((pid = fork()) < 0) {
return (-1);
} else if (pid == 0) {
/*
* child
*/
if (setsid() < 0)
fatal_sys("setsid error");
/*
* System V acquires controlling terminal on open().
*/
if ((fds = ptys_open(pts_name)) < 0)
fatal_sys("can't open slave pty");
/* all done with master in child */
close(fdm);
#if defined(TIOCSCTTY)
/*
* TIOCSCTTY is the BSD way to acquire a controlling terminal.
*
* Don't check the return code. It would fail in Cygwin.
*/
ioctl(fds, TIOCSCTTY, (char *)0);
#endif
/*
* Set slave's termios and window size.
*/
if (slave_termios != NULL) {
if (tcsetattr(fds, TCSANOW, slave_termios) < 0)
fatal_sys("tcsetattr error on slave pty");
}
if (slave_winsize != NULL) {
if (ioctl(fds, TIOCSWINSZ, slave_winsize) < 0)
fatal_sys("TIOCSWINSZ error on slave pty");
}
/*
* Slave becomes stdin/stdout/stderr of child.
*/
if (dup2(fds, STDIN_FILENO) != STDIN_FILENO)
fatal_sys("dup2 error to stdin");
if (dup2(fds, STDOUT_FILENO) != STDOUT_FILENO)
fatal_sys("dup2 error to stdout");
if (dup2(fds, STDERR_FILENO) != STDERR_FILENO)
fatal_sys("dup2 error to stderr");
if (fds != STDIN_FILENO && fds != STDOUT_FILENO &&
fds != STDERR_FILENO) {
close(fds);
}
return (0);
} else {
/*
* parent
*/
*ptrfdm = fdm;
return (pid);
}
}
int
tty_raw(int fd, struct termios *save_termios)
{
int err;
struct termios buf;
if (tcgetattr(fd, &buf) < 0)
return (-1);
*save_termios = buf;
/*
* Echo off, canonical mode off, extended input
* processing off, signal chars off.
*/
buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
/*
* No SIGINT on BREAK, CR-to-NL off, input parity
* check off, don't strip 8th bit on input, output
* flow control off.
*/
buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/*
* Clear size bits, parity checking off.
*/
buf.c_cflag &= ~(CSIZE | PARENB);
/*
* Set 8 bits/char.
*/
buf.c_cflag |= CS8;
/*
* Output processing off.
*/
buf.c_oflag &= ~(OPOST);
/*
* Case B: 1 byte at a time, no timer.
*/
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSAFLUSH, &buf) < 0)
return (-1);
/*
* Verify that the changes stuck. tcsetattr can return 0 on
* partial success.
*/
if (tcgetattr(fd, &buf) < 0) {
err = errno;
tcsetattr(fd, TCSAFLUSH, save_termios);
errno = err;
return (-1);
}
if ((buf.c_lflag & (ECHO | ICANON | IEXTEN | ISIG)) ||
(buf.c_iflag & (BRKINT | ICRNL | INPCK | ISTRIP | IXON)) ||
(buf.c_cflag & (CSIZE | PARENB | CS8)) != CS8 ||
(buf.c_oflag & OPOST) || buf.c_cc[VMIN] != 1 ||
buf.c_cc[VTIME] != 0) {
/*
* Only some of the changes were made. Restore the
* original settings.
*/
tcsetattr(fd, TCSAFLUSH, save_termios);
errno = EINVAL;
return (-1);
}
return (0);
}
int
tty_reset(int fd, struct termios *termio)
{
if (tcsetattr(fd, TCSAFLUSH, termio) < 0)
return (-1);
return (0);
}
void
tty_atexit(void)
{
if (g.reset_on_exit) {
tty_reset(STDIN_FILENO, &g.save_termios);
}
}
ssize_t
read_if_ready(int fd, char *buf, size_t n)
{
struct timeval timeout;
fd_set fds;
int nread;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (select(fd + 1, &fds, NULL, NULL, &timeout) < 0) {
return -1;
}
if (! FD_ISSET(fd, &fds) ) {
return 0;
}
if ((nread = read(fd, buf, n) ) < 0) {
return -1;
}
return nread;
}
ssize_t
writen(int fd, const void *ptr, size_t n)
{
size_t nleft;
ssize_t nwritten;
nleft = n;
while (nleft > 0) {
if ((nwritten = write(fd, ptr, nleft)) < 0) {
if (nleft == n) {
return (-1);
} else {
/* error, return amount written so far */
break;
}
} else if (nwritten == 0) {
break;
}
nleft -= nwritten;
ptr += nwritten;
}
return (n - nleft);
}
/*
* The only portable use of signal() is to set a signal's disposition
* to SIG_DFL or SIG_IGN. The semantics when using signal() to
* establish a signal handler vary across systems (and POSIX.1
* explicitly permits this variation); do not use it for this purpose.
*
* POSIX.1 solved the portability mess by specifying sigaction(2),
* which provides explicit control of the semantics when a signal
* handler is invoked; use that interface instead of signal().
*
* In the original UNIX systems, when a handler that was established
* using signal() was invoked by the delivery of a signal, the
* disposition of the signal would be reset to SIG_DFL, and the system
* did not block delivery of further instances of the signal. This is
* equivalent to calling sigaction(2) with the following flags:
*
* sa.sa_flags = SA_RESETHAND | SA_NODEFER;
*/
void
sig_handle(int signo, void (*handler)(int) )
{
struct sigaction act;
memset(&act, 0, sizeof(act) );
act.sa_handler = handler;
sigaction(signo, &act, NULL);
}
void
sig_child(int signo)
{
g.SIGCHLDed = true;
}
void
sig_winch(int signum)
{
g.received_winch = true;
return;
}
#define write2(fd1, fd2, buf, len) \
do { \
int fds[2] = { fd1, fd2 }; \
int i; \
for (i = 0; i < 2; ++i) { \
if (fds[i] < 0) { \
continue; \
} \
if (writen(fds[i], buf, (len) ) != (len) ) { \
fatal_sys("write: fd %d", fds[i]); \
} \
} \
} while (0)
void
big_loop()
{
char buf1[BUFFSIZE]; /* for read() from stdin */
char buf2[2 * BUFFSIZE + 1]; /* for read() from ptym, `+1' for adding the '\000' */
char *cache = buf2;
int nread, ncache = 0;
struct timeval select_timeout;
fd_set readfds;
int i, r, status;
regmatch_t re_match[1];
time_t last_time = time(NULL);
bool given_up = false;
int passwords_seen = 0;
int fd_to_pty = -1, fd_from_pty = -1;
bool stdin_eof = false;
int exit_code = -1;
pid_t wait_return;
if (g.opt.log_to_pty != NULL) {
fd_to_pty = open(g.opt.log_to_pty, O_CREAT | O_WRONLY | O_TRUNC, 0600);
if (fd_to_pty < 0) {
fatal_sys("open: %s", g.opt.log_to_pty);
}
}
if (g.opt.log_from_pty != NULL) {
fd_from_pty = open(g.opt.log_from_pty, O_CREAT | O_WRONLY | O_TRUNC, 0600);
if (fd_from_pty < 0) {
fatal_sys("open: %s", g.opt.log_from_pty);
}
}
/*
* wait for the child to open the pty
*/
do {
/*
* On Mac, fcntl(O_NONBLOCK) may fail before the child opens the pty
* slave side. So wait a while for the child to open the pty slave.
*/
fd_set writefds;
select_timeout.tv_sec = 1;
select_timeout.tv_usec = 0;
FD_ZERO(&writefds);
FD_SET(g.fd_ptym, &writefds);
select(g.fd_ptym + 1, NULL, &writefds, NULL, &select_timeout);
if (! FD_ISSET(g.fd_ptym, &writefds) ) {
fatal(ERROR_GENERAL, "failed to wait for ptym to be writable");
}
} while (0);
while (true) {
L_chk_sigchld:
if (g.SIGCHLDed) {
/*
* NOTE:
* - WCONTINUED does not work on macOS (10.12.5)
* - On macOS, SIGCHLD can be generated when
* 1. child process has terminated/exited
* 2. the currently *running* child process is stopped (e.g. by `kill -STOP')
* - On Linux, SIGCHLD can be generated when
* 1. child process has terminated/exited
* 2. the currently *running* child process is stopped (e.g. by `kill -STOP')
* 3. the currently *stopped* child process is continued (e.g. by `kill -CONT')
* - waitpid(WCONTINUED) works on Linux but not on macOS.
*/
wait_return = waitpid(-1, &status, WUNTRACED | WCONTINUED);
if (wait_return < 0) {
fatal_sys("received SIGCHLD but waitpid() failed");
}
g.SIGCHLDed = false;
if (WIFEXITED(status) ) {
exit_code = WEXITSTATUS(status);
goto L_done;
} else if (WIFSIGNALED(status) ) {
exit_code = status + 128;
goto L_done;
} else if (WIFSTOPPED(status) ) {
/* Do nothing. Just wait for the child to be continued and wait
* for the next SIGCHLD. */
} else if (WIFCONTINUED(status) ) {
/* */
} else {
/* This should not happen. */
goto L_done;
}
}
if (g.opt.timeout != 0 && g.opt.fatal_no_prompt && passwords_seen == 0
&& labs(time(NULL) - last_time) > g.opt.timeout) {
fatal(ERROR_TIMEOUT, "timeout waiting for password prompt");
}
if (g.received_winch && g.stdin_is_tty) {
struct winsize ttysize;
static int ourtty = -1;
g.received_winch = false;
if (ourtty < 0) {
#if 0
ourtty = open("/dev/tty", 0);
#else
ourtty = STDIN_FILENO;
#endif
}
if (ioctl(ourtty, TIOCGWINSZ, &ttysize) == 0) {
ioctl(g.fd_ptym, TIOCSWINSZ, &ttysize);
}
}
/* Keep sending EOF until the child exits
* - See http://lists.gnu.org/archive/html/help-bash/2016-11/msg00002.html
* (EOF ('\004') was lost if it's sent to bash too quickly)
* - We cannot simply close(fd_ptym) or the child will get SIGHUP. */
while (stdin_eof) {
struct termios term;
char eof_char;
static struct timeval last;
struct timeval now;
double diff;
if (last.tv_sec == 0) {
gettimeofday(&last, NULL);
break;
}
gettimeofday(&now, NULL);
diff = now.tv_sec + now.tv_usec / 1e6 - (last.tv_sec + last.tv_usec / 1e6);
if (diff > -0.05 && diff < 0.05) {
break;
}
last = now;
if (tcgetattr(g.fd_ptym, &term) < 0) {
goto L_done;
}
eof_char = term.c_cc[VEOF];
if (write(g.fd_ptym, &eof_char, 1) < 0) {
goto L_done;
}
write(fd_to_pty, &eof_char, 1);
break;
}
FD_ZERO(&readfds);
if (g.stdin_is_tty && !stdin_eof) {
FD_SET(STDIN_FILENO, &readfds);
}
FD_SET(g.fd_ptym, &readfds);
select_timeout.tv_sec = 1;
select_timeout.tv_usec = 100 * 1000;
r = select(g.fd_ptym + 1, &readfds, NULL, NULL, &select_timeout);
if (r == 0) {
/* timeout */
continue;
} else if (r < 0) {
if (errno == EINTR) {
continue;
} else {
fatal_sys("select error");
}
}
/*
* copy data from ptym to stdout
*/
if (FD_ISSET(g.fd_ptym, &readfds) ) {
while (true) {
nread = read_if_ready(g.fd_ptym, cache + ncache,
2 * BUFFSIZE - (cache - buf2));
if (nread <= 0) {
/* child exited? */
goto L_chk_sigchld;
}
write2(STDOUT_FILENO, fd_from_pty, cache + ncache, nread);
if (! given_up && g.opt.timeout != 0
&& labs(time(NULL) - last_time) >= g.opt.timeout) {
given_up = true;
}
/* regexec() does not like NULLs */
if (! given_up) {
for (i = 0; i < nread; ++i) {
if (cache[ncache + i] == 0) {
cache[ncache + i] = 0xff;
}
}
}
ncache += nread;
/* make it NULL-terminated so regexec() would be happy */
cache[ncache] = 0;
/* match password prompt and send the password */
if (! g.now_interactive && ! given_up) {
if (g.opt.auto_yesno && passwords_seen == 0
&& regexec(&g.opt.re_yesno, cache, 1, re_match, 0) == 0)
{
/*
* (yes/no)?
*/
char *yes = "yes\r";
write2(g.fd_ptym, fd_to_pty, yes, strlen(yes) );
ncache -= re_match[0].rm_eo;
cache += re_match[0].rm_eo;
} else if (regexec(&g.opt.re_prompt, cache, 1, re_match, 0) == 0) {
/*
* Password:
*/
++passwords_seen;
last_time = time(NULL);
if (g.opt.fatal_more_tries) {
if (g.opt.tries != 0 && passwords_seen > g.opt.tries) {
fatal(ERROR_MAX_TRIES, "still prompted for passwords after %d tries", g.opt.tries);
}
} else if (g.opt.tries != 0 && passwords_seen >= g.opt.tries) {
given_up = true;
}
write(g.fd_ptym, g.opt.password, strlen(g.opt.password));
write(g.fd_ptym, "\r", 1);
write(fd_to_pty, "********\r", strlen("********\r") );
ncache -= re_match[0].rm_eo;
cache += re_match[0].rm_eo;
}
} else {
cache = buf2;
ncache = 0;
}
if (cache + ncache >= buf2 + 2 * BUFFSIZE) {
if (ncache > BUFFSIZE) {
cache += ncache - BUFFSIZE;
ncache = BUFFSIZE;
}
memmove(buf2, cache, ncache);
cache = buf2;
}
}
}
/*
* copy data from stdin to ptym
*/
if (!stdin_eof && FD_ISSET(STDIN_FILENO, &readfds) ) {
if ((nread = read(STDIN_FILENO, buf1, BUFFSIZE)) < 0)
fatal_sys("read error from stdin");
else if (nread == 0) {
/* EOF on stdin means we're done */
stdin_eof = true;
} else {
g.now_interactive = true;
write2(g.fd_ptym, fd_to_pty, buf1, nread);
}
}
}
L_done:
/* the child has exited but there may be still some data for us
* to read */
while ((nread = read_if_ready(g.fd_ptym, buf2, BUFFSIZE) ) > 0) {
write2(STDOUT_FILENO, fd_from_pty, buf2, nread);
}
if (fd_to_pty >= 0) {
close(fd_to_pty);
}
if (fd_from_pty >= 0) {
close(fd_from_pty);
}
if (exit_code < 0) {
exit(ERROR_GENERAL);
} else {
exit(exit_code);
}
}
int
main(int argc, char *argv[])
{
char slave_name[32];
pid_t pid;
struct termios orig_termios;
struct winsize size;
startup();
getargs(argc, argv);
g.stdin_is_tty = isatty(STDIN_FILENO);
sig_handle(SIGCHLD, sig_child);
if (g.stdin_is_tty) {
if (tcgetattr(STDIN_FILENO, &orig_termios) < 0)
fatal_sys("tcgetattr error on stdin");
if (ioctl(STDIN_FILENO, TIOCGWINSZ, (char *) &size) < 0)
fatal_sys("TIOCGWINSZ error");
pid = pty_fork(&g.fd_ptym, slave_name, sizeof(slave_name),
&orig_termios, &size);
} else {
pid = pty_fork(&g.fd_ptym, slave_name, sizeof(slave_name),
NULL, NULL);
}
if (pid < 0) {
fatal_sys("fork error");
} else if (pid == 0) {
/*
* child
*/
if (g.opt.nohup_child) {
sig_handle(SIGHUP, SIG_IGN);
}
if (execvp(g.opt.command[0], g.opt.command) < 0)
fatal_sys("can't execute: %s", g.opt.command[0]);
}
/*
* parent
*/
/* stdout also needs to be checked. Or `passh ls -l | less' would not
* restore the saved tty settings. */
if (g.stdin_is_tty && isatty(STDOUT_FILENO) ) {
/* user's tty to raw mode */