aboutsummaryrefslogtreecommitdiff
path: root/rtty.c
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-15 00:42:55 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-15 00:45:09 +0200
commit3726eebdf7b5fb37e9bb18609778a903c783c3ef (patch)
tree4a20271f94a827deeb3db1bf6a0c025e8bf5fec6 /rtty.c
parent2fd0c583436f6a38692a125b4f8eb2b2e9e9a046 (diff)
downloadrtty-3726eebdf7b5fb37e9bb18609778a903c783c3ef.tar.gz
Add -P option (ask for password)
Diffstat (limited to 'rtty.c')
-rw-r--r--rtty.c63
1 files changed, 50 insertions, 13 deletions
diff --git a/rtty.c b/rtty.c
index 6088dc7..c7a616d 100644
--- a/rtty.c
+++ b/rtty.c
@@ -4,18 +4,17 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/select.h>
#include <sys/stat.h>
+#include <termios.h>
#include <unistd.h>
#define INIT "export TERM=tty43 EDITOR=ed PAGER='pr -ptl23'\n"
#define MAXBUF 2048
-void
-sigchld()
-{
- exit(0);
-}
+char *getpw(void);
+void sigchld();
int
main(int argc, char *argv[])
@@ -34,6 +33,13 @@ main(int argc, char *argv[])
if(errno != EEXIST)
err(1, "mkfifo");
+ /* Ask for password on -P. */
+ if(strcmp(argv[1], "-P") == 0){
+ printf("password: ");
+ setenv("SSHPASS", getpw(), 1);
+ argv++; argc--;
+ }
+
if(fork() == 0){
/* Redirect standard in, out. */
if(!(fdin = open(in, O_RDONLY)))
@@ -47,22 +53,21 @@ main(int argc, char *argv[])
if(!(nargv = malloc(sizeof(char *)*(argc+10))))
err(1, "malloc");
offset = -1;
-#ifdef SSHPASS
- nargv[++offset] = "sshpass";
- nargv[++offset] = "-ePass";
-#endif
+ if(strcmp(argv[0], "-P") == 0){
+ nargv[++offset] = "sshpass";
+ nargv[++offset] = "-ePass";
+ }
nargv[++offset] = "ssh";
nargv[++offset] = "-tt";
-#ifndef SSHPASS
- nargv[++offset] = "-oBatchMode=yes";
-#endif
+ if(strcmp(argv[0], "-P") != 0)
+ nargv[++offset] = "-oBatchMode=yes";
for(i = 1; i < argc; i++)
nargv[i+offset] = argv[i];
nargv[argc+offset] = NULL;
/* Exec into ssh. */
execvp(nargv[0], nargv);
- err(1, "execvp");
+ err(1, "%s", nargv[0]);
}
if(!(fdin = open(in, O_WRONLY)))
@@ -138,3 +143,35 @@ main(int argc, char *argv[])
}
}
+
+char *
+getpw()
+{
+ 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);
+
+ fgets(pw, 255, stdin);
+ pw[strcspn(pw, "\n")] = 0;
+
+ printf("\n");
+ tcsetattr(0, TCSAFLUSH, &orig);
+
+ return pw;
+}
+
+void
+sigchld()
+{
+ exit(0);
+}