summaryrefslogtreecommitdiffstats
path: root/src/ck-sysdeps-unix.c
diff options
context:
space:
mode:
authorWilliam Jon McCann <mccann@jhu.edu>2007-08-17 12:59:41 -0400
committerWilliam Jon McCann <mccann@jhu.edu>2007-08-17 12:59:41 -0400
commit3423ce3039033e8bf54860a2b955fdf8c1f21be7 (patch)
treef9da817b4799d19e28c673e1e26d7a42f94fcde9 /src/ck-sysdeps-unix.c
parentbec4b4163ae65fa271845da6b748456346987c26 (diff)
add a vt monitor test, move getfd to sysdeps, add a check for the root user
Diffstat (limited to 'src/ck-sysdeps-unix.c')
-rw-r--r--src/ck-sysdeps-unix.c114
1 files changed, 113 insertions, 1 deletions
diff --git a/src/ck-sysdeps-unix.c b/src/ck-sysdeps-unix.c
index aadca48..4bad4b6 100644
--- a/src/ck-sysdeps-unix.c
+++ b/src/ck-sysdeps-unix.c
@@ -25,10 +25,15 @@
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
+#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
-#include <errno.h>
+#include <sys/ioctl.h>
+
+#ifdef __linux__
+#include <linux/kd.h>
+#endif
#ifdef HAVE_GETPEERUCRED
#include <ucred.h>
@@ -96,3 +101,110 @@ ck_get_socket_peer_credentials (int socket_fd,
return ret;
}
+
+
+/*
+ * getfd.c
+ *
+ * Get an fd for use with kbd/console ioctls.
+ * We try several things because opening /dev/console will fail
+ * if someone else used X (which does a chown on /dev/console).
+ */
+
+gboolean
+ck_fd_is_a_console (int fd)
+{
+ char arg;
+ int kb_ok;
+
+ arg = 0;
+
+#ifdef __linux__
+ kb_ok = (ioctl (fd, KDGKBTYPE, &arg) == 0
+ && ((arg == KB_101) || (arg == KB_84)));
+#else
+ kb_ok = 1;
+#endif
+
+ return (isatty (fd) && kb_ok);
+}
+
+static int
+open_a_console (char *fnam)
+{
+ int fd;
+
+ fd = open (fnam, O_RDONLY | O_NOCTTY);
+ if (fd < 0 && errno == EACCES)
+ fd = open (fnam, O_WRONLY | O_NOCTTY);
+
+ if (fd < 0)
+ return -1;
+
+ if (! ck_fd_is_a_console (fd)) {
+ close (fd);
+ fd = -1;
+ }
+
+ return fd;
+}
+
+int
+ck_get_a_console_fd (void)
+{
+ int fd;
+
+#ifdef _PATH_TTY
+ fd = open_a_console (_PATH_TTY);
+ if (fd >= 0)
+ return fd;
+#endif
+
+ fd = open_a_console ("/dev/tty");
+ if (fd >= 0)
+ return fd;
+
+#ifdef _PATH_CONSOLE
+ fd = open_a_console (_PATH_CONSOLE);
+ if (fd >= 0)
+ return fd;
+#endif
+
+ fd = open_a_console ("/dev/console");
+ if (fd >= 0)
+ return fd;
+
+ for (fd = 0; fd < 3; fd++) {
+ if (ck_fd_is_a_console (fd)) {
+ return fd;
+ }
+ }
+
+ return -1;
+}
+
+gboolean
+ck_is_root_user (void)
+{
+#ifndef G_OS_WIN32
+ uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
+ gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
+
+#ifdef HAVE_GETRESUID
+ if (getresuid (&ruid, &euid, &suid) != 0 ||
+ getresgid (&rgid, &egid, &sgid) != 0)
+#endif /* HAVE_GETRESUID */
+ {
+ suid = ruid = getuid ();
+ sgid = rgid = getgid ();
+ euid = geteuid ();
+ egid = getegid ();
+ }
+
+ if (ruid == 0) {
+ return TRUE;
+ }
+
+#endif
+ return FALSE;
+}