utmpupdate.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/ByteBuffer.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/JsonValue.h>
  29. #include <LibCore/ArgsParser.h>
  30. #include <LibCore/DateTime.h>
  31. #include <LibCore/File.h>
  32. int main(int argc, char** argv)
  33. {
  34. if (pledge("stdio wpath cpath", nullptr) < 0) {
  35. perror("pledge");
  36. return 1;
  37. }
  38. if (unveil("/var/run/utmp", "rwc") < 0) {
  39. perror("unveil");
  40. return 1;
  41. }
  42. unveil(nullptr, nullptr);
  43. pid_t pid = 0;
  44. bool flag_create = false;
  45. bool flag_delete = false;
  46. const char* tty_name = nullptr;
  47. const char* from = nullptr;
  48. Core::ArgsParser args_parser;
  49. args_parser.add_option(flag_create, "Create entry", "create", 'c');
  50. args_parser.add_option(flag_delete, "Delete entry", "delete", 'd');
  51. args_parser.add_option(pid, "PID", "PID", 'p', "PID");
  52. args_parser.add_option(from, "From", "from", 'f', "From");
  53. args_parser.add_positional_argument(tty_name, "TTY name", "tty");
  54. args_parser.parse(argc, argv);
  55. if (flag_create && flag_delete) {
  56. warnln("-c and -d are mutually exclusive");
  57. return 1;
  58. }
  59. dbg() << "Updating utmp from UID=" << getuid() << " GID=" << getgid() << " EGID=" << getegid() << " PID=" << pid;
  60. auto file_or_error = Core::File::open("/var/run/utmp", Core::IODevice::ReadWrite);
  61. if (file_or_error.is_error()) {
  62. dbg() << "Error: " << file_or_error.error();
  63. return 1;
  64. }
  65. auto& file = *file_or_error.value();
  66. auto file_contents = file.read_all();
  67. auto previous_json = JsonValue::from_string(file_contents);
  68. JsonObject json;
  69. if (!previous_json.has_value() || !previous_json.value().is_object()) {
  70. dbg() << "Error: Could not parse JSON";
  71. } else {
  72. json = previous_json.value().as_object();
  73. }
  74. if (flag_create) {
  75. JsonObject entry;
  76. entry.set("pid", pid);
  77. entry.set("uid", getuid());
  78. entry.set("from", from);
  79. entry.set("login_at", time(nullptr));
  80. json.set(tty_name, move(entry));
  81. } else {
  82. ASSERT(flag_delete);
  83. dbg() << "Removing " << tty_name << " from utmp";
  84. json.remove(tty_name);
  85. }
  86. if (!file.seek(0)) {
  87. dbg() << "Seek failed";
  88. return 1;
  89. }
  90. if (!file.truncate(0)) {
  91. dbg() << "Truncation failed";
  92. return 1;
  93. }
  94. if (!file.write(json.to_string())) {
  95. dbg() << "Write failed";
  96. return 1;
  97. }
  98. return 0;
  99. }