Group.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <AK/ScopeGuard.h>
  8. #include <LibCore/Group.h>
  9. #include <LibCore/System.h>
  10. namespace Core {
  11. #ifndef AK_OS_BSD_GENERIC
  12. ErrorOr<void> Group::add_group(Group& group)
  13. {
  14. if (group.name().is_empty())
  15. return Error::from_string_literal("Group name can not be empty.");
  16. // A quick sanity check on group name
  17. if (strpbrk(group.name().characters(), "\\/!@#$%^&*()~+=`:\n"))
  18. return Error::from_string_literal("Group name has invalid characters.");
  19. // Disallow names starting with '_', '-' or other non-alpha characters.
  20. if (group.name().starts_with('_') || group.name().starts_with('-') || !is_ascii_alpha(group.name().characters()[0]))
  21. return Error::from_string_literal("Group name has invalid characters.");
  22. // Verify group name does not already exist
  23. if (TRY(name_exists(group.name())))
  24. return Error::from_string_literal("Group name already exists.");
  25. // Sort out the group id for the group
  26. if (group.id() > 0) {
  27. if (TRY(id_exists(group.id())))
  28. return Error::from_string_literal("Group ID already exists.");
  29. } else {
  30. gid_t group_id = 100;
  31. while (true) {
  32. if (!TRY(id_exists(group_id)))
  33. break;
  34. group_id++;
  35. }
  36. group.set_group_id(group_id);
  37. }
  38. auto gr = TRY(group.to_libc_group());
  39. FILE* file = fopen("/etc/group", "a");
  40. if (!file)
  41. return Error::from_errno(errno);
  42. ScopeGuard file_guard { [&] {
  43. fclose(file);
  44. } };
  45. if (putgrent(&gr, file) < 0)
  46. return Error::from_errno(errno);
  47. return {};
  48. }
  49. #endif
  50. Group::Group(String name, gid_t id, Vector<String> members)
  51. : m_name(move(name))
  52. , m_id(id)
  53. , m_members(move(members))
  54. {
  55. }
  56. ErrorOr<bool> Group::name_exists(StringView name)
  57. {
  58. return TRY(Core::System::getgrnam(name)).has_value();
  59. }
  60. ErrorOr<bool> Group::id_exists(gid_t id)
  61. {
  62. return TRY(Core::System::getgrgid(id)).has_value();
  63. }
  64. // NOTE: struct group returned from this function cannot outlive an instance of Group.
  65. ErrorOr<struct group> Group::to_libc_group()
  66. {
  67. struct group gr;
  68. gr.gr_name = const_cast<char*>(m_name.characters());
  69. gr.gr_passwd = const_cast<char*>("x");
  70. gr.gr_gid = m_id;
  71. gr.gr_mem = nullptr;
  72. // FIXME: A better solution would surely be not using a static here
  73. // NOTE: This now means that there cannot be multiple struct groups at the same time, because only one gr.gr_mem can ever be valid at the same time.
  74. // NOTE: Not using a static here would result in gr.gr_mem being freed up on exit from this function.
  75. static Vector<char*> members;
  76. members.clear_with_capacity();
  77. if (m_members.size() > 0) {
  78. TRY(members.try_ensure_capacity(m_members.size() + 1));
  79. for (auto member : m_members)
  80. members.unchecked_append(const_cast<char*>(member.characters()));
  81. members.unchecked_append(nullptr);
  82. gr.gr_mem = const_cast<char**>(members.data());
  83. }
  84. return gr;
  85. }
  86. }