1. Michał Górny
  2. mirage2iso

Commits

Michał Górny  committed e47608d

Add support for disabling character output through termios.

  • Participants
  • Parent commits d50bf64
  • Branches master

Comments (0)

Files changed (3)

File Makefile.in

View file
  • Ignore whitespace
 PROG = mirage2iso
 OBJS = mirage-getopt.o mirage-wrapper.o mirage-password.o
 
-CONFIGTESTS = check-getopt.o check-sysexits.o check-mmapio.o check-assuan
-CONFIGIN = check-getopt.c check-sysexits.c check-mmapio.c check-assuan.c
+CONFIGTESTS = check-getopt.o check-sysexits.o check-mmapio.o check-termios.o check-assuan
+CONFIGIN = check-getopt.c check-sysexits.c check-mmapio.c check-termios.c check-assuan.c
 
 DESTDIR = 
 PREFIX = /usr/local
 check-mmapio.c:
 	printf '#define _POSIX_C_SOURCE 200112L\n#include <unistd.h>\n#include <sys/types.h>\n#include <sys/mman.h>\nint main(void) { return (ftruncate(0, 0) || mmap(0, 0, 0, 0, 0, 0) == MAP_FAILED); }\n' > $@
 
+check-termios.c:
+	printf '#define _POSIX_C_SOURCE 200112L\n#include <stdio.h>\n#include <termios.h>\n#include <unistd.h>\nint main(void) { struct termios term; return tcgetattr(fileno(stdin), &term); }\n' > $@
+
 check-assuan.c:
 	printf '#define _ISO_C99_SOURCE 1\n#include <assuan.h>\nint main(void) { assuan_context_t ctx; return assuan_pipe_connect(&ctx, "/bin/true", 0, 0); }\n' > $@
 

File configure

View file
  • Ignore whitespace
 	conf_check check-getopt.o NO_GETOPT_LONG 'getopt_long()'
 	conf_check check-sysexits.o NO_SYSEXITS '<sysexits.h>'
 	conf_check check-mmapio.o NO_MMAPIO 'mmap() & ftruncate()'
+	conf_check check-termios.o NO_TERMIOS 'termios'
 	conf_check check-assuan NO_ASSUAN 'assuan'
 }
 

File mirage-password.c

View file
  • Ignore whitespace
 
 #include "mirage-config.h"
 
-#define _ISOC99_SOURCE 1
+#ifndef NO_TERMIOS
+#	define _POSIX_C_SOURCE 200112L
+#else
+#	define _ISOC99_SOURCE 1
+#endif
 
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <string.h>
 
+#ifndef NO_TERMIOS
+#	include <termios.h>
+#	include <unistd.h>
+#endif
+
 #ifndef NO_ASSUAN
 #	include <stddef.h>
 #	include <assuan.h>
 
 #endif
 
+static const bool mirage_echo(const bool newstate) {
+#ifndef NO_TERMIOS
+	const int fd = fileno(stdin);
+	struct termios term;
+
+	/* noecho state should have eaten a newline */
+	if (newstate)
+		fprintf(stderr, "\n");
+
+	if (tcgetattr(fd, &term) == -1)
+		perror("tcgetattr() failed");
+	else {
+		if (newstate)
+			term.c_lflag |= ECHO;
+		else
+			term.c_lflag &= !ECHO;
+
+		if (tcsetattr(fd, TCSANOW, &term) == -1)
+			perror("tcsetattr() failed");
+		else
+			return true;
+	}
+#endif
+
+	return false;
+}
+
 static const mirage_tristate_t mirage_input_password_stdio(void) {
 	if (!buf) {
 		buf = malloc(password_bufsize);
 			return error;
 		}
 
+		/* disable the echo before the prompt as we may output error */
+		const bool echooff = mirage_echo(false);
+
 		fprintf(stderr, "Please input password to the encrypted image: ");
 
 		if (!fgets(buf, password_bufsize, stdin)) {
 			fprintf(stderr, "Password input failed\n");
 			mirage_forget_password();
+			if (echooff)
+				mirage_echo(true);
 			return error;
 		}
+		if (echooff)
+			mirage_echo(true);
 
 		/* remove trailing newline */
 		const int len = strlen(buf);