From dcd47655d0a970daaf336a343dca1863e2389b40 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 6 Sep 2020 16:10:27 +0200 Subject: [PATCH] utmpupdate: Add a program for updating /var/run/utmp To keep track of ongoing terminal sessions, we now have a sort-of traditional /var/run/utmp file, like other Unix systems. Unlike other Unix systems however, ours is of course JSON. :^) The /bin/utmpupdate program is used to update the file, which is not writable by regular user accounts. This helper program is set-GID "utmp". --- Base/etc/fstab | 1 + Base/etc/group | 1 + Meta/build-root-filesystem.sh | 11 ++++- Userland/utmpupdate.cpp | 82 +++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 Userland/utmpupdate.cpp diff --git a/Base/etc/fstab b/Base/etc/fstab index 5e1598326c..4cff6ee8a6 100644 --- a/Base/etc/fstab +++ b/Base/etc/fstab @@ -7,6 +7,7 @@ /etc /etc bind bind,nodev,nosuid /home /home bind bind,nodev,nosuid /root /root bind bind,nodev,nosuid +/var /var bind bind,nodev,nosuid none /proc proc nosuid none /dev/pts devpts noexec,nosuid,ro diff --git a/Base/etc/group b/Base/etc/group index 374eeca8aa..55b63e71eb 100644 --- a/Base/etc/group +++ b/Base/etc/group @@ -3,6 +3,7 @@ wheel:x:1:anon tty:x:2: phys:x:3:window,anon audio:x:4:anon +utmp:x:5: lookup:x:10:protocol,anon protocol:x:11:webcontent,anon notify:x:12:anon diff --git a/Meta/build-root-filesystem.sh b/Meta/build-root-filesystem.sh index 2254c8bc9b..475df54f45 100755 --- a/Meta/build-root-filesystem.sh +++ b/Meta/build-root-filesystem.sh @@ -6,6 +6,7 @@ wheel_gid=1 tty_gid=2 phys_gid=3 audio_gid=4 +utmp_gid=5 window_uid=13 window_gid=13 @@ -51,11 +52,13 @@ chmod 4755 mnt/bin/ping chmod 4750 mnt/bin/reboot chmod 4750 mnt/bin/shutdown chmod 4750 mnt/bin/keymap +chown 0:$utmp_gid mnt/bin/utmpupdate +chmod 2755 mnt/bin/utmpupdate echo "done" printf "creating initial filesystem structure... " -for dir in bin etc proc mnt tmp boot mod; do +for dir in bin etc proc mnt tmp boot mod var/run; do mkdir -p mnt/$dir done chmod 700 mnt/boot @@ -63,6 +66,12 @@ chmod 700 mnt/mod chmod 1777 mnt/tmp echo "done" +printf "creating utmp file... " +touch mnt/var/run/utmp +chown 0:$utmp_gid mnt/var/run/utmp +chmod 664 mnt/var/run/utmp +echo "done" + printf "setting up device nodes... " mkdir -p mnt/dev mkdir -p mnt/dev/pts diff --git a/Userland/utmpupdate.cpp b/Userland/utmpupdate.cpp new file mode 100644 index 0000000000..ad02768834 --- /dev/null +++ b/Userland/utmpupdate.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include + +// utmpupdate -c /dev/pts/0 +// utmpupdate -d /dev/pts/0 + +int main(int argc, char** argv) +{ + pid_t pid = 0; + bool flag_create = false; + bool flag_delete = false; + const char* tty_name = nullptr; + const char* from = nullptr; + + Core::ArgsParser args_parser; + args_parser.add_option(flag_create, "Create entry", "create", 'c'); + args_parser.add_option(flag_delete, "Delete entry", "delete", 'd'); + args_parser.add_option(pid, "PID", "PID", 'p', "PID"); + args_parser.add_option(from, "From", "from", 'f', "From"); + args_parser.add_positional_argument(tty_name, "TTY name", "tty"); + + args_parser.parse(argc, argv); + + if (flag_create && flag_delete) { + warn() << "-c and -d are mutually exclusive"; + return 1; + } + + dbg() << "Updating utmp from UID=" << getuid() << " GID=" << getgid() << " EGID=" << getegid() << " PID=" << pid; + + auto file_or_error = Core::File::open("/var/run/utmp", Core::IODevice::ReadWrite); + if (file_or_error.is_error()) { + dbg() << "Error: " << file_or_error.error(); + return 1; + } + + auto& file = *file_or_error.value(); + + auto file_contents = file.read_all(); + auto previous_json = JsonValue::from_string(file_contents); + + JsonObject json; + + if (!previous_json.has_value() || !previous_json.value().is_object()) { + dbg() << "Error: Could not parse JSON"; + } else { + json = previous_json.value().as_object(); + } + + if (flag_create) { + JsonObject entry; + entry.set("pid", pid); + entry.set("uid", getuid()); + entry.set("from", from); + entry.set("login_at", Core::DateTime::now().to_string()); + json.set(tty_name, move(entry)); + } else { + ASSERT(flag_delete); + dbg() << "Removing " << tty_name << " from utmp"; + json.remove(tty_name); + } + + if (!file.seek(0)) { + dbg() << "Seek failed"; + return 1; + } + + if (!file.truncate(0)) { + dbg() << "Truncation failed"; + return 1; + } + + if (!file.write(json.to_string())) { + dbg() << "Write failed"; + return 1; + } + + return 0; +}