sulogin.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini sulogin implementation for busybox
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. */
  7. //config:config SULOGIN
  8. //config: bool "sulogin"
  9. //config: default y
  10. //config: select FEATURE_SYSLOG
  11. //config: help
  12. //config: sulogin is invoked when the system goes into single user
  13. //config: mode (this is done through an entry in inittab).
  14. //applet:/* Needs to be run by root or be suid root - needs to change uid and gid: */
  15. //applet:IF_SULOGIN(APPLET(sulogin, BB_DIR_SBIN, BB_SUID_DROP))
  16. //kbuild:lib-$(CONFIG_SULOGIN) += sulogin.o
  17. //usage:#define sulogin_trivial_usage
  18. //usage: "[-t N] [TTY]"
  19. //usage:#define sulogin_full_usage "\n\n"
  20. //usage: "Single user login\n"
  21. //usage: "\n -t N Timeout"
  22. #include "libbb.h"
  23. #include <syslog.h>
  24. int sulogin_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  25. int sulogin_main(int argc UNUSED_PARAM, char **argv)
  26. {
  27. int timeout = 0;
  28. struct passwd *pwd;
  29. const char *shell;
  30. logmode = LOGMODE_BOTH;
  31. openlog(applet_name, 0, LOG_AUTH);
  32. opt_complementary = "t+"; /* -t N */
  33. getopt32(argv, "t:", &timeout);
  34. argv += optind;
  35. if (argv[0]) {
  36. close(0);
  37. close(1);
  38. dup(xopen(argv[0], O_RDWR));
  39. close(2);
  40. dup(0);
  41. }
  42. /* Malicious use like "sulogin /dev/sda"? */
  43. if (!isatty(0) || !isatty(1) || !isatty(2)) {
  44. logmode = LOGMODE_SYSLOG;
  45. bb_error_msg_and_die("not a tty");
  46. }
  47. /* Clear dangerous stuff, set PATH */
  48. sanitize_env_if_suid();
  49. pwd = getpwuid(0);
  50. if (!pwd) {
  51. goto auth_error;
  52. }
  53. while (1) {
  54. int r;
  55. r = ask_and_check_password_extended(pwd, timeout,
  56. "Give root password for system maintenance\n"
  57. "(or type Control-D for normal startup):"
  58. );
  59. if (r < 0) {
  60. /* ^D, ^C, timeout, or read error */
  61. bb_info_msg("Normal startup");
  62. return 0;
  63. }
  64. if (r > 0) {
  65. break;
  66. }
  67. bb_do_delay(LOGIN_FAIL_DELAY);
  68. bb_info_msg("Login incorrect");
  69. }
  70. bb_info_msg("System Maintenance Mode");
  71. IF_SELINUX(renew_current_security_context());
  72. shell = getenv("SUSHELL");
  73. if (!shell)
  74. shell = getenv("sushell");
  75. if (!shell)
  76. shell = pwd->pw_shell;
  77. /* Exec login shell with no additional parameters. Never returns. */
  78. run_shell(shell, 1, NULL, NULL);
  79. auth_error:
  80. bb_error_msg_and_die("no password entry for root");
  81. }