Group.cpp 3.0 KB

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