From 4309fe26d920af031c5c517ddeb46a032619d4cc Mon Sep 17 00:00:00 2001 From: sw1tchbl4d3 Date: Sun, 5 Feb 2023 10:21:01 +0100 Subject: [PATCH] clean up bsd compatibility functions --- rdo.c | 4 ++-- sessions.h | 61 ++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/rdo.c b/rdo.c index 1b463c3..3d9c2e1 100644 --- a/rdo.c +++ b/rdo.c @@ -100,7 +100,7 @@ int main(int argc, char** argv) { 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]); 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 (!read_pw_from_stdin) - setsession(getppid(), ts_ttl, ruid); + setsession(ts_ttl); runprog(&argv[read_pw_from_stdin+1]); } diff --git a/sessions.h b/sessions.h index 1916cfa..a1d6991 100644 --- a/sessions.h +++ b/sessions.h @@ -8,6 +8,10 @@ #include #include +#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) { char path[255], fc[1024]; char* ptr = fc; @@ -78,27 +82,46 @@ int ensuredir() { return 0; } -void setsession(int pid, int ts_ttl, int ruid) { +#ifdef RDO_USE_IOCTL + +void setsession(int ts_ttl) { if (ts_ttl <= 0) return; -#ifdef TIOCSETVERAUTH - (void)pid; - (void)ruid; - int ttyfd = open("/dev/tty", O_RDWR); ioctl(ttyfd, TIOCSETVERAUTH, &ts_ttl); 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 + +void setsession(int ts_ttl) { + if (ts_ttl <= 0) + return; + unsigned long long startts; 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; - 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); if (fd < 0) { @@ -115,32 +138,23 @@ void setsession(int pid, int ts_ttl, int ruid) { } close(fd); -#endif - return; } -int getsession(int pid, int ts_ttl, int ruid) { +int getsession(int ts_ttl) { if (ts_ttl <= 0) 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; 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; - 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); if (fd < 0) { @@ -166,5 +180,6 @@ int getsession(int pid, int ts_ttl, int ruid) { } return 0; -#endif } + +#endif