From d09a59a77e241d83788bdeedfd383261c900112e Mon Sep 17 00:00:00 2001 From: sw1tchbl4d3 Date: Thu, 5 May 2022 08:21:48 +0200 Subject: [PATCH] Add own readpassphrase function This removes the libbsd dependency. Fixed #10. --- Makefile | 2 +- README.md | 4 +--- rdo.c | 5 ++-- readpassphrase.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 readpassphrase.h diff --git a/Makefile b/Makefile index 26cc89a..654e5e6 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS = -Wall -Wextra -Werror -Wl,-z,now CFLAGS_RELEASE = ${CFLAGS} -O2 -s -D_FORTIFY_SOURCE=2 CFLAGS_DEBUG = ${CFLAGS} -O0 -g -fsanitize=undefined -LIBS = -lbsd -lcrypt +LIBS = -lcrypt CC = gcc all: rdo.c diff --git a/README.md b/README.md index 424e386..aad4fe0 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,7 @@ This project aims to be a very slim alternative to both sudo and doas. If you are on Arch Linux, you can download the package via the [AUR](https://aur.archlinux.org/packages/rdo/). -If you are using any other linux distro, or want to build it yourself, you will first need to install either `libbsd` or `libbsd-dev`, depending on how your package manager calls it. - -Then, you can clone and build rdo with the following set of commands: +You can clone and build rdo with the following set of commands: ```sh git clone https://codeberg.org/sw1tchbl4d3/rdo diff --git a/rdo.c b/rdo.c index b8b4456..397bc8d 100644 --- a/rdo.c +++ b/rdo.c @@ -7,7 +7,8 @@ #include #include #include -#include + +#include "readpassphrase.h" #include "sessions.h" #define VERSION "1.4" @@ -116,7 +117,7 @@ int main(int argc, char** argv) { tries = 0; while (tries < 3) { - if (!readpassphrase("(rdo) Password: ", password, sizeof(password), RPP_REQUIRE_TTY)) + if (!readpassphrase("(rdo) Password: ", password, sizeof(password))) err(1, "Could not get passphrase"); char* hashed_pw = crypt(password, shadowEntry->sp_pwdp); diff --git a/readpassphrase.h b/readpassphrase.h new file mode 100644 index 0000000..13eac35 --- /dev/null +++ b/readpassphrase.h @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +char* readpassphrase(const char* prompt, char* buf, size_t bufsz) { + char stdin_path[256]; + char tty_link_path[256]; + int n; + int ttyfd = -1; + + struct termios term; + + for (int i = 0; i < 3; i++) { + if (tcgetattr(i, &term) == 0) { + ttyfd = i; + break; + } + } + + if (ttyfd < 0) + return NULL; + + snprintf(tty_link_path, sizeof(tty_link_path), "/proc/self/fd/%d", ttyfd); + + n = readlink(tty_link_path, stdin_path, sizeof(stdin_path)); + if (n < 0) + return NULL; + + stdin_path[n] = '\0'; + + int fd = open(stdin_path, O_RDWR); + if (fd < 0) + return NULL; + + term.c_lflag &= ~ECHO; + tcsetattr(ttyfd, 0, &term); + term.c_lflag |= ECHO; + + if (write(fd, prompt, strlen(prompt)) < 0) { + tcsetattr(ttyfd, 0, &term); + return NULL; + } + + n = read(fd, buf, bufsz); + if (n < 0) { + tcsetattr(ttyfd, 0, &term); + printf("\n"); + return NULL; + } + + buf[n-1] = '\0'; + + close(fd); + tcsetattr(ttyfd, 0, &term); + + // NOTE: As we disabled echo, the enter sent by the user isn't displayed. + printf("\n"); + + return buf; +}