clean up bsd compatibility functions

This commit is contained in:
sw1tchbl4d3 2023-02-05 10:21:01 +01:00
parent ca8a3ee603
commit 4309fe26d9
2 changed files with 40 additions and 25 deletions

4
rdo.c
View file

@ -100,7 +100,7 @@ int main(int argc, char** argv) {
fclose(fp); fclose(fp);
if (getsession(getppid(), ts_ttl, ruid) == 0 && !read_pw_from_stdin) if (getsession(ts_ttl) == 0 && !read_pw_from_stdin)
runprog(&argv[1]); runprog(&argv[1]);
struct passwd* pw = getpwuid(ruid); struct passwd* pw = getpwuid(ruid);
@ -146,7 +146,7 @@ int main(int argc, char** argv) {
if (strcmp(given_hashed_pw, user_hashed_pw) == 0) { if (strcmp(given_hashed_pw, user_hashed_pw) == 0) {
if (!read_pw_from_stdin) if (!read_pw_from_stdin)
setsession(getppid(), ts_ttl, ruid); setsession(ts_ttl);
runprog(&argv[read_pw_from_stdin+1]); runprog(&argv[read_pw_from_stdin+1]);
} }

View file

@ -8,6 +8,10 @@
#include <fcntl.h> #include <fcntl.h>
#include <time.h> #include <time.h>
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__BSD__) || defined(BSD)
#define RDO_USE_IOCTL 1
#endif
int getpstartts(int pid, unsigned long long* startts) { int getpstartts(int pid, unsigned long long* startts) {
char path[255], fc[1024]; char path[255], fc[1024];
char* ptr = fc; char* ptr = fc;
@ -78,27 +82,46 @@ int ensuredir() {
return 0; return 0;
} }
void setsession(int pid, int ts_ttl, int ruid) { #ifdef RDO_USE_IOCTL
void setsession(int ts_ttl) {
if (ts_ttl <= 0) if (ts_ttl <= 0)
return; return;
#ifdef TIOCSETVERAUTH
(void)pid;
(void)ruid;
int ttyfd = open("/dev/tty", O_RDWR); int ttyfd = open("/dev/tty", O_RDWR);
ioctl(ttyfd, TIOCSETVERAUTH, &ts_ttl); ioctl(ttyfd, TIOCSETVERAUTH, &ts_ttl);
close(ttyfd); close(ttyfd);
return;
}
int getsession(int ts_ttl) {
if (ts_ttl <= 0)
return -1;
int ttyfd = open("/dev/tty", O_RDWR);
int ret = ioctl(ttyfd, TIOCCHKVERAUTH);
close(ttyfd);
return ret;
}
#else #else
void setsession(int ts_ttl) {
if (ts_ttl <= 0)
return;
unsigned long long startts; unsigned long long startts;
char path[1024], ts_str[32]; char path[1024], ts_str[32];
int ppid = getppid();
int ruid = getuid();
if (ensuredir() < 0 || getpstartts(pid, &startts) < 0) if (ensuredir() < 0 || getpstartts(ppid, &startts) < 0)
return; return;
snprintf(path, sizeof(path), "/var/run/rdo/%d-%d-%llu", ruid, pid, startts); snprintf(path, sizeof(path), "/var/run/rdo/%d-%d-%llu", ruid, ppid, startts);
int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0700); int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0700);
if (fd < 0) { if (fd < 0) {
@ -115,32 +138,23 @@ void setsession(int pid, int ts_ttl, int ruid) {
} }
close(fd); close(fd);
#endif
return; return;
} }
int getsession(int pid, int ts_ttl, int ruid) { int getsession(int ts_ttl) {
if (ts_ttl <= 0) if (ts_ttl <= 0)
return -1; return -1;
#ifdef TIOCCHKVERAUTH
(void)pid;
(void)ruid;
int ttyfd = open("/dev/tty", O_RDWR);
int ret = ioctl(ttyfd, TIOCCHKVERAUTH);
close(ttyfd);
return ret;
#else
unsigned long long startts, current; unsigned long long startts, current;
char path[1024], ts_str[32]; char path[1024], ts_str[32];
if (ensuredir() < 0 || getpstartts(pid, &startts) < 0) int ppid = getppid();
int ruid = getuid();
if (ensuredir() < 0 || getpstartts(ppid, &startts) < 0)
return -1; return -1;
snprintf(path, sizeof(path), "/var/run/rdo/%d-%d-%llu", ruid, pid, startts); snprintf(path, sizeof(path), "/var/run/rdo/%d-%d-%llu", ruid, ppid, startts);
int fd = open(path, O_RDONLY); int fd = open(path, O_RDONLY);
if (fd < 0) { if (fd < 0) {
@ -166,5 +180,6 @@ int getsession(int pid, int ts_ttl, int ruid) {
} }
return 0; return 0;
#endif
} }
#endif