aboutsummaryrefslogtreecommitdiff
path: root/rtty.c
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-15 13:39:10 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-15 13:39:22 +0200
commit668bd8418f5886b1e449381057e4f2849b200562 (patch)
tree0b97a8a1d5cadda1d21fe95f1873b5d2eb9cc044 /rtty.c
parent0c91ed54db29d1d5628b6473ef4387dde1b3c116 (diff)
downloadrtty-668bd8418f5886b1e449381057e4f2849b200562.tar.gz
Add +d (detect password prompt) flag
Also, rename -P flag to +p.
Diffstat (limited to 'rtty.c')
-rw-r--r--rtty.c61
1 files changed, 41 insertions, 20 deletions
diff --git a/rtty.c b/rtty.c
index 6a69e56..18eeae6 100644
--- a/rtty.c
+++ b/rtty.c
@@ -13,16 +13,17 @@
#define INIT "export TERM=tty43 EDITOR=ed PAGER='pr -ptl23'\n"
#define MAXBUF 2048
-char *getpw(void);
+char *getpw(char *);
void sigchld();
int
main(int argc, char *argv[])
{
- char bufin[MAXBUF], bufout[MAXBUF], *bi, *bo, in[30], **nargv, out[30];
- fd_set rfds0, rfds1;
- int fdin, fdout, i, n, offset, sshpass;
- struct timeval tv;
+ char bufin[MAXBUF], bufout[MAXBUF], *bi, *bo, in[30], **nargv,
+ out[30], pw[255];
+ fd_set rfds0, rfds1;
+ int detect, fdin, fdout, i, n, offset, sshpass;
+ struct timeval tv;
signal(SIGCHLD, sigchld);
@@ -33,17 +34,25 @@ main(int argc, char *argv[])
if(errno != EEXIST)
err(1, "mkfifo");
- /* Ask for password on -P. */
- sshpass = 0;
- if(strcmp(argv[1], "-P") == 0){
- sshpass = 1;
- setenv("SSHPASS", getpw(), 1);
+ /* Process rtty-specific flags. */
+ detect = sshpass = 0;
+ for(;argv[1][0] == '+';){
+ if(strchr(argv[1], 'd')) detect = 1;
+ if(strchr(argv[1], 'p')) sshpass = 1;
- /* Remove -P from argv. */
+ /* Remove flag argument from argv. */
argv[1] = argv[0];
argv++; argc--;
}
+ /* Ask for password on +p. */
+ if(sshpass){
+ printf("password: ");
+ getpw(pw);
+ setenv("SSHPASS", pw, 1);
+ sshpass = 1;
+ }
+
if(fork() == 0){
/* Redirect standard in, out. */
if(!(fdin = open(in, O_RDONLY)))
@@ -143,29 +152,41 @@ main(int argc, char *argv[])
printf("%s", bo);
fflush(stdout);
bufin[0] = 0;
+
+ if(!detect)
+ continue;
+
+ /*
+ * As the +d flag is given, rtty tries to detect
+ * password prompts and hide the user's input.
+ */
+ if(bo[strlen(bo)-1] == ':'
+ || strcmp(bo+strlen(bo)-2, ": ") == 0){
+ if(!(bo = strrchr(bufout, '\n')))
+ bo = bufout;
+ if(strstr(bo, "password")
+ || strstr(bo, "Password")
+ || strstr(bo, "passphrase")
+ || strstr(bo, "Passphrase")){
+ getpw(pw);
+ dprintf(fdin, "%s\n", pw);
+ }
+ }
}
}
}
char *
-getpw()
+getpw(char *pw)
{
- char *pw;
struct termios orig, term;
- /*
- * This pointer will never be freed, but eh... whatever.
- */
- if(!(pw = malloc(255)))
- err(1, "malloc");
-
tcgetattr(0, &orig);
tcgetattr(0, &term);
term.c_lflag &= ~ECHO;
tcsetattr(0, TCSANOW, &term);
- printf("password: ");
fgets(pw, 255, stdin);
pw[strcspn(pw, "\n")] = 0;