aboutsummaryrefslogtreecommitdiff
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
parent2fd0c583436f6a38692a125b4f8eb2b2e9e9a046 (diff)
downloadrtty-3726eebdf7b5fb37e9bb18609778a903c783c3ef.tar.gz
Add -P option (ask for password)
-rw-r--r--INTRO13
-rw-r--r--rtty.124
-rw-r--r--rtty.c63
3 files changed, 67 insertions, 33 deletions
diff --git a/INTRO b/INTRO
index 788587c..3dec1e9 100644
--- a/INTRO
+++ b/INTRO
@@ -16,16 +16,3 @@ a brief overview:
Editor: ed(1)
Pager: cat(1), pr(1)
History: fc (see sh(1))
-
-
- Supplying passwords to rtty
-
-If compiled with -DSSHPASS, rtty will run sshpass(1) with the -e
-flag, expecting a password to be set in the SSHPASS environment
-variable.
-
-If you often need to log into remote servers without public key
-authentication, or if you cannot use ssh-agent(1) to avoid entering
-your key's passphrase interactively, consider compiling a special
-version of rtty -- I suggest the name rttyp -- and installing it
-alongside the normal version of rtty.
diff --git a/rtty.1 b/rtty.1
index 21c2dd4..7f91f29 100644
--- a/rtty.1
+++ b/rtty.1
@@ -6,6 +6,7 @@
.Nd limited but responsive remote shell
.Sh SYNOPSIS
.Nm
+.Op Fl P
.Op Ar ...
.Sh DESCRIPTION
.Pp
@@ -22,7 +23,22 @@ uses the default line-editing capabilities of the terminal,
user input is only sent to the remote server
once a full line has been input.
.Pp
-All arguments are passed to
+If the
+.Fl P
+flag is provided,
+.Nm
+will manually ask the user for a password and run
+.Xr ssh 1
+using
+.Xr sshpass 1 .
+Otherwise,
+.Xr ssh 1
+is launched in batch mode,
+without the possibility of interactive password entry.
+.Pp
+All arguments except
+.Fl P
+are passed to
.Xr ssh 1 .
.Sh AUTHORS
.Pp
@@ -32,12 +48,6 @@ is written by John Ankarström
.Sh BUGS
.Pp
.Nm
-does not support interactive password input;
-it is recommended that public key authentication be used,
-in combination with
-.Xr ssh-agent 1 .
-.Pp
-.Nm
exits immediately on SIGINT;
it is best used in conjunction with
.Xr dtach 1
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);
+}