usermod.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
  3. * Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibCore/Account.h>
  8. #include <LibCore/ArgsParser.h>
  9. #include <LibCore/System.h>
  10. #include <LibFileSystem/FileSystem.h>
  11. #include <LibMain/Main.h>
  12. #include <pwd.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. static Optional<gid_t> group_string_to_gid(StringView group)
  17. {
  18. auto maybe_gid = group.to_uint<gid_t>();
  19. auto maybe_group_or_error = maybe_gid.has_value()
  20. ? Core::System::getgrgid(maybe_gid.value())
  21. : Core::System::getgrnam(group);
  22. if (maybe_group_or_error.is_error()) {
  23. warnln("Error resolving group '{}': {}", group, maybe_group_or_error.release_error());
  24. return {};
  25. }
  26. auto maybe_group = maybe_group_or_error.release_value();
  27. if (!maybe_group.has_value()) {
  28. warnln("Group '{}' does not exist", group);
  29. return {};
  30. }
  31. return maybe_group->gr_gid;
  32. }
  33. ErrorOr<int> serenity_main(Main::Arguments arguments)
  34. {
  35. if (geteuid() != 0) {
  36. warnln("Not running as root :^(");
  37. return 1;
  38. }
  39. TRY(Core::System::setegid(0));
  40. TRY(Core::System::pledge("stdio wpath rpath cpath fattr tty"));
  41. TRY(Core::System::unveil("/etc", "rwc"));
  42. uid_t uid = 0;
  43. bool append_extra_gids = false;
  44. Optional<gid_t> gid;
  45. bool lock = false;
  46. bool remove_extra_gids = false;
  47. bool unlock = false;
  48. StringView new_home_directory;
  49. bool move_home = false;
  50. StringView shell;
  51. StringView gecos;
  52. StringView username;
  53. Vector<gid_t> extra_gids;
  54. auto args_parser = Core::ArgsParser();
  55. args_parser.set_general_help("Modify a user account");
  56. args_parser.add_option(append_extra_gids, "Append the supplementary groups specified with the -G option to the user", "append", 'a');
  57. args_parser.add_option(uid, "The new numerical value of the user's ID", "uid", 'u', "uid");
  58. args_parser.add_option(Core::ArgsParser::Option {
  59. .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
  60. .help_string = "The group name or number of the user's new initial login group",
  61. .long_name = "gid",
  62. .short_name = 'g',
  63. .value_name = "group",
  64. .accept_value = [&gid](StringView group) {
  65. if (auto maybe_gid = group_string_to_gid(group); maybe_gid.has_value())
  66. gid = move(maybe_gid);
  67. return gid.has_value();
  68. },
  69. });
  70. args_parser.add_option(Core::ArgsParser::Option {
  71. .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
  72. .help_string = "Set the user's supplementary groups. Groups are specified with a comma-separated list. Group names or numbers may be used",
  73. .long_name = "groups",
  74. .short_name = 'G',
  75. .value_name = "groups",
  76. .accept_value = [&extra_gids](StringView comma_separated_groups) {
  77. auto groups = comma_separated_groups.split_view(',');
  78. for (auto group : groups) {
  79. if (auto gid = group_string_to_gid(group); gid.has_value())
  80. extra_gids.append(gid.value());
  81. }
  82. return true;
  83. },
  84. });
  85. args_parser.add_option(lock, "Lock password", "lock", 'L');
  86. args_parser.add_option(remove_extra_gids, "Remove the supplementary groups specified with the -G option from the user", "remove", 'r');
  87. args_parser.add_option(unlock, "Unlock password", "unlock", 'U');
  88. args_parser.add_option(new_home_directory, "The user's new login directory", "home", 'd', "new-home");
  89. args_parser.add_option(move_home, "Move the content of the user's home directory to the new location", "move", 'm');
  90. args_parser.add_option(shell, "The name of the user's new login shell", "shell", 's', "path-to-shell");
  91. args_parser.add_option(gecos, "Change the GECOS field of the user", "gecos", 'n', "general-info");
  92. args_parser.add_positional_argument(username, "Username of the account to modify", "username");
  93. args_parser.parse(arguments);
  94. if (extra_gids.is_empty() && (append_extra_gids || remove_extra_gids)) {
  95. warnln("The -a and -r options can only be used with the -G option");
  96. args_parser.print_usage(stderr, arguments.strings[0]);
  97. return 1;
  98. }
  99. if (append_extra_gids && remove_extra_gids) {
  100. warnln("The -a and -r options are mutually exclusive");
  101. args_parser.print_usage(stderr, arguments.strings[0]);
  102. return 1;
  103. }
  104. if (lock && unlock) {
  105. warnln("The -L and -U options are mutually exclusive");
  106. args_parser.print_usage(stderr, arguments.strings[0]);
  107. return 1;
  108. }
  109. auto account_or_error = Core::Account::from_name(username);
  110. if (account_or_error.is_error()) {
  111. warnln("Core::Account::from_name: {}", account_or_error.error());
  112. return 1;
  113. }
  114. // target_account is the account we are modifying.
  115. auto& target_account = account_or_error.value();
  116. if (move_home) {
  117. TRY(Core::System::unveil(target_account.home_directory(), "c"sv));
  118. TRY(Core::System::unveil(new_home_directory, "wc"sv));
  119. }
  120. unveil(nullptr, nullptr);
  121. if (uid) {
  122. if (getpwuid(uid)) {
  123. warnln("uid {} already exists", uid);
  124. return 1;
  125. }
  126. target_account.set_uid(uid);
  127. }
  128. if (gid.has_value())
  129. target_account.set_gid(gid.value());
  130. if (lock) {
  131. target_account.set_password_enabled(false);
  132. }
  133. if (unlock) {
  134. target_account.set_password_enabled(true);
  135. }
  136. if (!new_home_directory.is_empty()) {
  137. if (move_home) {
  138. auto maybe_error = Core::System::rename(target_account.home_directory(), new_home_directory);
  139. if (maybe_error.is_error()) {
  140. if (maybe_error.error().code() == EXDEV) {
  141. auto result = FileSystem::copy_file_or_directory(
  142. new_home_directory, target_account.home_directory(),
  143. FileSystem::RecursionMode::Allowed,
  144. FileSystem::LinkMode::Disallowed,
  145. FileSystem::AddDuplicateFileMarker::No);
  146. if (result.is_error()) {
  147. warnln("usermod: could not move directory {} : {}", target_account.home_directory().characters(), static_cast<Error const&>(result.error()));
  148. return 1;
  149. }
  150. maybe_error = Core::System::unlink(target_account.home_directory());
  151. if (maybe_error.is_error())
  152. warnln("usermod: unlink {} : {}", target_account.home_directory(), maybe_error.error().code());
  153. } else {
  154. warnln("usermod: could not move directory {} : {}", target_account.home_directory(), maybe_error.error().code());
  155. }
  156. }
  157. }
  158. target_account.set_home_directory(new_home_directory);
  159. }
  160. if (!shell.is_empty()) {
  161. target_account.set_shell(shell);
  162. }
  163. if (!gecos.is_empty()) {
  164. target_account.set_gecos(gecos);
  165. }
  166. if (append_extra_gids) {
  167. for (auto gid : target_account.extra_gids())
  168. extra_gids.append(gid);
  169. }
  170. if (remove_extra_gids) {
  171. Vector<gid_t> current_extra_gids = target_account.extra_gids();
  172. for (auto gid : extra_gids)
  173. current_extra_gids.remove_all_matching([gid](auto current_gid) { return current_gid == gid; });
  174. extra_gids = move(current_extra_gids);
  175. }
  176. if (!extra_gids.is_empty() || remove_extra_gids) {
  177. target_account.set_extra_gids(extra_gids);
  178. }
  179. TRY(Core::System::pledge("stdio wpath rpath cpath fattr"));
  180. TRY(target_account.sync());
  181. return 0;
  182. }