commit: 2b28bce631481b27039e6952a8b1b76731ff9e37
parent 049f48f0307dd5404ec42c43f4c75b91c0b0a66d
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 3 Nov 2022 02:03:54 +0100
login: fchown current TTY to new user
Diffstat:
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/login.c b/login.c
@@ -14,12 +14,15 @@
#include <assert.h> // assert
#include <errno.h> // errno
+#include <grp.h> // getgrnam
#include <pwd.h> // getpwnam
#include <stdbool.h> // bool
#include <stdio.h> // fprintf, perror
#include <stdlib.h> // abort, setenv
#include <string.h> // strcmp, explicit_bzero
-#include <unistd.h> // getuid, getopt, opt*, chdir, setuid, setgid
+#include <unistd.h> // getuid, getopt, opt*, chdir, setuid, setgid, fchown
+
+#define TTY_GROUP "tty"
extern char **environ;
char *envclear[] = {NULL};
@@ -168,6 +171,22 @@ main(int argc, char *argv[])
if(pwent != NULL)
{
+ int tty_gid = pwent->pw_gid;
+ struct group *tty_group = getgrnam(TTY_GROUP);
+ if(tty_group == NULL)
+ {
+ perror("login: getgrnam");
+ }
+ else
+ {
+ tty_gid = tty_group->gr_gid;
+ }
+
+ /* considers that STDIN_FILENO is close enough to the current tty */
+ if(fchown(STDIN_FILENO, pwent->pw_uid, tty_gid) < 0)
+ {
+ perror("login: fchown");
+ }
if(setgid(pwent->pw_gid) < 0)
{
perror("login: setgid");