utmpupdate.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/JsonObject.h>
  27. #include <AK/JsonValue.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/DateTime.h>
  30. #include <LibCore/File.h>
  31. int main(int argc, char** argv)
  32. {
  33. if (pledge("stdio wpath cpath", nullptr) < 0) {
  34. perror("pledge");
  35. return 1;
  36. }
  37. if (unveil("/var/run/utmp", "rwc") < 0) {
  38. perror("unveil");
  39. return 1;
  40. }
  41. unveil(nullptr, nullptr);
  42. pid_t pid = 0;
  43. bool flag_create = false;
  44. bool flag_delete = false;
  45. const char* tty_name = nullptr;
  46. const char* from = nullptr;
  47. Core::ArgsParser args_parser;
  48. args_parser.add_option(flag_create, "Create entry", "create", 'c');
  49. args_parser.add_option(flag_delete, "Delete entry", "delete", 'd');
  50. args_parser.add_option(pid, "PID", "PID", 'p', "PID");
  51. args_parser.add_option(from, "From", "from", 'f', "From");
  52. args_parser.add_positional_argument(tty_name, "TTY name", "tty");
  53. args_parser.parse(argc, argv);
  54. if (flag_create && flag_delete) {
  55. warn() << "-c and -d are mutually exclusive";
  56. return 1;
  57. }
  58. dbg() << "Updating utmp from UID=" << getuid() << " GID=" << getgid() << " EGID=" << getegid() << " PID=" << pid;
  59. auto file_or_error = Core::File::open("/var/run/utmp", Core::IODevice::ReadWrite);
  60. if (file_or_error.is_error()) {
  61. dbg() << "Error: " << file_or_error.error();
  62. return 1;
  63. }
  64. auto& file = *file_or_error.value();
  65. auto file_contents = file.read_all();
  66. auto previous_json = JsonValue::from_string(file_contents);
  67. JsonObject json;
  68. if (!previous_json.has_value() || !previous_json.value().is_object()) {
  69. dbg() << "Error: Could not parse JSON";
  70. } else {
  71. json = previous_json.value().as_object();
  72. }
  73. if (flag_create) {
  74. JsonObject entry;
  75. entry.set("pid", pid);
  76. entry.set("uid", getuid());
  77. entry.set("from", from);
  78. entry.set("login_at", time(nullptr));
  79. json.set(tty_name, move(entry));
  80. } else {
  81. ASSERT(flag_delete);
  82. dbg() << "Removing " << tty_name << " from utmp";
  83. json.remove(tty_name);
  84. }
  85. if (!file.seek(0)) {
  86. dbg() << "Seek failed";
  87. return 1;
  88. }
  89. if (!file.truncate(0)) {
  90. dbg() << "Truncation failed";
  91. return 1;
  92. }
  93. if (!file.write(json.to_string())) {
  94. dbg() << "Write failed";
  95. return 1;
  96. }
  97. return 0;
  98. }