su.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <errno.h>
  17. #include <error.h>
  18. #include <getopt.h>
  19. #include <paths.h>
  20. #include <pwd.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <private/android_filesystem_config.h>
  26. void pwtoid(const char* tok, uid_t* uid, gid_t* gid) {
  27. struct passwd* pw = getpwnam(tok);
  28. if (pw) {
  29. if (uid) *uid = pw->pw_uid;
  30. if (gid) *gid = pw->pw_gid;
  31. } else {
  32. char* end;
  33. errno = 0;
  34. uid_t tmpid = strtoul(tok, &end, 10);
  35. if (errno != 0 || end == tok) error(1, errno, "invalid uid/gid '%s'", tok);
  36. if (uid) *uid = tmpid;
  37. if (gid) *gid = tmpid;
  38. }
  39. }
  40. void extract_uidgids(const char* uidgids, uid_t* uid, gid_t* gid, gid_t* gids, int* gids_count) {
  41. char *clobberablegids;
  42. char *nexttok;
  43. char *tok;
  44. int gids_found;
  45. if (!uidgids || !*uidgids) {
  46. *gid = *uid = 0;
  47. *gids_count = 0;
  48. return;
  49. }
  50. clobberablegids = strdup(uidgids);
  51. strcpy(clobberablegids, uidgids);
  52. nexttok = clobberablegids;
  53. tok = strsep(&nexttok, ",");
  54. pwtoid(tok, uid, gid);
  55. tok = strsep(&nexttok, ",");
  56. if (!tok) {
  57. /* gid is already set above */
  58. *gids_count = 0;
  59. free(clobberablegids);
  60. return;
  61. }
  62. pwtoid(tok, NULL, gid);
  63. gids_found = 0;
  64. while ((gids_found < *gids_count) && (tok = strsep(&nexttok, ","))) {
  65. pwtoid(tok, NULL, gids);
  66. gids_found++;
  67. gids++;
  68. }
  69. if (nexttok && gids_found == *gids_count) {
  70. fprintf(stderr, "too many group ids\n");
  71. }
  72. *gids_count = gids_found;
  73. free(clobberablegids);
  74. }
  75. int main(int argc, char** argv) {
  76. uid_t current_uid = getuid();
  77. if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed");
  78. // Handle -h and --help.
  79. ++argv;
  80. if (*argv && (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0)) {
  81. fprintf(stderr,
  82. "usage: su [WHO [COMMAND...]]\n"
  83. "\n"
  84. "Switch to WHO (default 'root') and run the given COMMAND (default sh).\n"
  85. "\n"
  86. "WHO is a comma-separated list of user, group, and supplementary groups\n"
  87. "in that order.\n"
  88. "\n");
  89. return 0;
  90. }
  91. // The default user is root.
  92. uid_t uid = 0;
  93. gid_t gid = 0;
  94. // If there are any arguments, the first argument is the uid/gid/supplementary groups.
  95. if (*argv) {
  96. gid_t gids[10];
  97. int gids_count = sizeof(gids)/sizeof(gids[0]);
  98. extract_uidgids(*argv, &uid, &gid, gids, &gids_count);
  99. if (gids_count) {
  100. if (setgroups(gids_count, gids)) {
  101. error(1, errno, "setgroups failed");
  102. }
  103. }
  104. ++argv;
  105. }
  106. if (setgid(gid)) error(1, errno, "setgid failed");
  107. if (setuid(uid)) error(1, errno, "setuid failed");
  108. // Reset parts of the environment.
  109. setenv("PATH", _PATH_DEFPATH, 1);
  110. unsetenv("IFS");
  111. struct passwd* pw = getpwuid(uid);
  112. if (pw) {
  113. setenv("LOGNAME", pw->pw_name, 1);
  114. setenv("USER", pw->pw_name, 1);
  115. } else {
  116. unsetenv("LOGNAME");
  117. unsetenv("USER");
  118. }
  119. // Set up the arguments for exec.
  120. char* exec_args[argc + 1]; // Having too much space is fine.
  121. size_t i = 0;
  122. for (; *argv != NULL; ++i) {
  123. exec_args[i] = *argv++;
  124. }
  125. // Default to the standard shell.
  126. if (i == 0) exec_args[i++] = const_cast<char*>("/system/bin/sh");
  127. exec_args[i] = NULL;
  128. execvp(exec_args[0], exec_args);
  129. error(1, errno, "failed to exec %s", exec_args[0]);
  130. }