1
0
Fork 0
forked from soccera/rdo

Add own readpassphrase function

This removes the libbsd dependency.
Fixed #10.
This commit is contained in:
sw1tchbl4d3 2022-05-05 08:21:48 +02:00
parent c82d7e777e
commit d09a59a77e
4 changed files with 66 additions and 6 deletions

View file

@ -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

View file

@ -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

5
rdo.c
View file

@ -7,7 +7,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bsd/readpassphrase.h>
#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);

61
readpassphrase.h Normal file
View file

@ -0,0 +1,61 @@
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
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;
}