su.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini su implementation for busybox
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. */
  7. //config:config SU
  8. //config: bool "su"
  9. //config: default y
  10. //config: select FEATURE_SYSLOG
  11. //config: help
  12. //config: su is used to become another user during a login session.
  13. //config: Invoked without a username, su defaults to becoming the super user.
  14. //config:
  15. //config: Note that Busybox binary must be setuid root for this applet to
  16. //config: work properly.
  17. //config:
  18. //config:config FEATURE_SU_SYSLOG
  19. //config: bool "Enable su to write to syslog"
  20. //config: default y
  21. //config: depends on SU
  22. //config:
  23. //config:config FEATURE_SU_CHECKS_SHELLS
  24. //config: bool "Enable su to check user's shell to be listed in /etc/shells"
  25. //config: depends on SU
  26. //config: default y
  27. //applet:/* Needs to be run by root or be suid root - needs to change uid and gid: */
  28. //applet:IF_SU(APPLET(su, BB_DIR_BIN, BB_SUID_REQUIRE))
  29. //kbuild:lib-$(CONFIG_SU) += su.o
  30. //usage:#define su_trivial_usage
  31. //usage: "[OPTIONS] [-] [USER]"
  32. //usage:#define su_full_usage "\n\n"
  33. //usage: "Run shell under USER (by default, root)\n"
  34. //usage: "\n -,-l Clear environment, run shell as login shell"
  35. //usage: "\n -p,-m Do not set new $HOME, $SHELL, $USER, $LOGNAME"
  36. //usage: "\n -c CMD Command to pass to 'sh -c'"
  37. //usage: "\n -s SH Shell to use instead of user's default"
  38. #include "libbb.h"
  39. #include <syslog.h>
  40. #if ENABLE_FEATURE_SU_CHECKS_SHELLS
  41. /* Return 1 if SHELL is a restricted shell (one not returned by
  42. * getusershell), else 0, meaning it is a standard shell. */
  43. static int restricted_shell(const char *shell)
  44. {
  45. char *line;
  46. int result = 1;
  47. /*setusershell(); - getusershell does it itself*/
  48. while ((line = getusershell()) != NULL) {
  49. if (/* *line != '#' && */ strcmp(line, shell) == 0) {
  50. result = 0;
  51. break;
  52. }
  53. }
  54. if (ENABLE_FEATURE_CLEAN_UP)
  55. endusershell();
  56. return result;
  57. }
  58. #endif
  59. #define SU_OPT_mp (3)
  60. #define SU_OPT_l (4)
  61. int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  62. int su_main(int argc UNUSED_PARAM, char **argv)
  63. {
  64. unsigned flags;
  65. char *opt_shell = NULL;
  66. char *opt_command = NULL;
  67. const char *opt_username = "root";
  68. struct passwd *pw;
  69. uid_t cur_uid = getuid();
  70. const char *tty;
  71. #if ENABLE_FEATURE_UTMP
  72. char user_buf[64];
  73. #endif
  74. const char *old_user;
  75. flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
  76. //argc -= optind;
  77. argv += optind;
  78. if (argv[0] && LONE_DASH(argv[0])) {
  79. flags |= SU_OPT_l;
  80. argv++;
  81. }
  82. /* get user if specified */
  83. if (argv[0]) {
  84. opt_username = argv[0];
  85. argv++;
  86. }
  87. if (ENABLE_FEATURE_SU_SYSLOG) {
  88. /* The utmp entry (via getlogin) is probably the best way to
  89. * identify the user, especially if someone su's from a su-shell.
  90. * But getlogin can fail -- usually due to lack of utmp entry.
  91. * in this case resort to getpwuid. */
  92. #if ENABLE_FEATURE_UTMP
  93. old_user = user_buf;
  94. if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
  95. #endif
  96. {
  97. pw = getpwuid(cur_uid);
  98. old_user = pw ? xstrdup(pw->pw_name) : "";
  99. }
  100. tty = xmalloc_ttyname(2);
  101. if (!tty) {
  102. tty = "none";
  103. }
  104. openlog(applet_name, 0, LOG_AUTH);
  105. }
  106. pw = xgetpwnam(opt_username);
  107. if (cur_uid == 0 || ask_and_check_password(pw) > 0) {
  108. if (ENABLE_FEATURE_SU_SYSLOG)
  109. syslog(LOG_NOTICE, "%c %s %s:%s",
  110. '+', tty, old_user, opt_username);
  111. } else {
  112. if (ENABLE_FEATURE_SU_SYSLOG)
  113. syslog(LOG_NOTICE, "%c %s %s:%s",
  114. '-', tty, old_user, opt_username);
  115. bb_do_delay(LOGIN_FAIL_DELAY);
  116. bb_error_msg_and_die("incorrect password");
  117. }
  118. if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
  119. closelog();
  120. }
  121. if (!opt_shell && (flags & SU_OPT_mp)) {
  122. /* -s SHELL is not given, but "preserve env" opt is */
  123. opt_shell = getenv("SHELL");
  124. }
  125. #if ENABLE_FEATURE_SU_CHECKS_SHELLS
  126. if (opt_shell && cur_uid != 0 && pw->pw_shell && restricted_shell(pw->pw_shell)) {
  127. /* The user being su'd to has a nonstandard shell, and so is
  128. * probably a uucp account or has restricted access. Don't
  129. * compromise the account by allowing access with a standard
  130. * shell. */
  131. bb_error_msg("using restricted shell");
  132. opt_shell = NULL; /* ignore -s PROG */
  133. }
  134. /* else: user can run whatever he wants via "su -s PROG USER".
  135. * This is safe since PROG is run under user's uid/gid. */
  136. #endif
  137. if (!opt_shell)
  138. opt_shell = pw->pw_shell;
  139. change_identity(pw);
  140. setup_environment(opt_shell,
  141. ((flags & SU_OPT_l) / SU_OPT_l * SETUP_ENV_CLEARENV)
  142. + (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV)
  143. + (!(flags & SU_OPT_l) * SETUP_ENV_NO_CHDIR),
  144. pw);
  145. IF_SELINUX(set_current_security_context(NULL);)
  146. /* Never returns */
  147. run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv);
  148. /* return EXIT_FAILURE; - not reached */
  149. }