passwd.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
  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/Types.h>
  27. #include <LibCore/Account.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/File.h>
  30. #include <LibCore/GetPassword.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. int main(int argc, char** argv)
  34. {
  35. if (geteuid() != 0) {
  36. fprintf(stderr, "Not running as root :^(\n");
  37. return 1;
  38. }
  39. if (pledge("stdio wpath rpath cpath tty", nullptr) < 0) {
  40. perror("pledge");
  41. return 1;
  42. }
  43. if (unveil("/etc/passwd", "rwc") < 0) {
  44. perror("unveil");
  45. return 1;
  46. }
  47. if (unveil("/etc/group", "rwc") < 0) {
  48. perror("unveil");
  49. return 1;
  50. }
  51. unveil(nullptr, nullptr);
  52. bool del = false;
  53. bool lock = false;
  54. bool unlock = false;
  55. const char* username = nullptr;
  56. auto args_parser = Core::ArgsParser();
  57. args_parser.add_option(del, "Delete password", "delete", 'd');
  58. args_parser.add_option(lock, "Lock password", "lock", 'l');
  59. args_parser.add_option(unlock, "Unlock password", "unlock", 'u');
  60. args_parser.add_positional_argument(username, "Username", "username", Core::ArgsParser::Required::No);
  61. args_parser.parse(argc, argv);
  62. uid_t current_uid = getuid();
  63. auto account_or_error = (username) ? Core::Account::from_name(username) : Core::Account::from_uid(current_uid);
  64. if (account_or_error.is_error()) {
  65. fprintf(stderr, "Core::Account::%s: %s\n", (username) ? "from_name" : "from_uid", account_or_error.error().characters());
  66. return 1;
  67. }
  68. // target_account is the account we are changing the password of.
  69. auto target_account = account_or_error.value();
  70. if (current_uid != 0 && current_uid != target_account.uid()) {
  71. fprintf(stderr, "You can't modify passwd for %s\n", username);
  72. return 1;
  73. }
  74. if (del) {
  75. target_account.delete_password();
  76. } else if (lock) {
  77. target_account.set_password_enabled(false);
  78. } else if (unlock) {
  79. target_account.set_password_enabled(true);
  80. } else {
  81. auto new_password = Core::get_password("New password: ");
  82. if (new_password.is_error()) {
  83. fprintf(stderr, "%s\n", strerror(new_password.error()));
  84. return 1;
  85. }
  86. target_account.set_password(new_password.value().characters());
  87. }
  88. if (!target_account.sync()) {
  89. perror("Core::Account::Sync");
  90. }
  91. return 0;
  92. }