uidgid_get.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright (c) 2001-2006, Gerrit Pape
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. The name of the author may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  14. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  16. EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  19. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  20. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  21. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "libbb.h"
  25. /* Always sets uid and gid */
  26. int get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok)
  27. {
  28. struct passwd *pwd;
  29. struct group *gr;
  30. char *user, *group;
  31. unsigned n;
  32. user = (char*)ug;
  33. group = strchr(ug, ':');
  34. if (group) {
  35. int sz = (++group) - ug;
  36. user = alloca(sz);
  37. /* copies sz-1 bytes, stores terminating '\0' */
  38. safe_strncpy(user, ug, sz);
  39. }
  40. if (numeric_ok) {
  41. n = bb_strtou(user, NULL, 10);
  42. if (!errno) {
  43. u->uid = n;
  44. pwd = getpwuid(n);
  45. /* If we have e.g. "500" string without user */
  46. /* with uid 500 in /etc/passwd, we set gid == uid */
  47. u->gid = pwd ? pwd->pw_gid : n;
  48. goto skip;
  49. }
  50. }
  51. /* Either it is not numeric, or caller disallows numeric username */
  52. pwd = getpwnam(user);
  53. if (!pwd)
  54. return 0;
  55. u->uid = pwd->pw_uid;
  56. u->gid = pwd->pw_gid;
  57. skip:
  58. if (group) {
  59. if (numeric_ok) {
  60. n = bb_strtou(group, NULL, 10);
  61. if (!errno) {
  62. u->gid = n;
  63. return 1;
  64. }
  65. }
  66. gr = getgrnam(group);
  67. if (!gr) return 0;
  68. u->gid = gr->gr_gid;
  69. }
  70. return 1;
  71. }
  72. /* chown-like:
  73. * "user" sets uid only,
  74. * ":group" sets gid only
  75. * "user:" sets uid and gid (to user's primary group id)
  76. * "user:group" sets uid and gid
  77. * ('unset' uid or gid is actually set to -1)
  78. */
  79. void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group)
  80. {
  81. char *group;
  82. u->uid = -1;
  83. u->gid = -1;
  84. /* Check if there is a group name */
  85. group = strchr(user_group, '.'); /* deprecated? */
  86. if (!group)
  87. group = strchr(user_group, ':');
  88. else
  89. *group = ':'; /* replace '.' with ':' */
  90. /* Parse "user[:[group]]" */
  91. if (!group) { /* "user" */
  92. u->uid = get_ug_id(user_group, xuname2uid);
  93. } else if (group == user_group) { /* ":group" */
  94. u->gid = get_ug_id(group + 1, xgroup2gid);
  95. } else {
  96. if (!group[1]) /* "user:" */
  97. *group = '\0';
  98. if (!get_uidgid(u, user_group, 1))
  99. bb_error_msg_and_die("unknown user/group %s", user_group);
  100. }
  101. }
  102. #if 0
  103. #include <stdio.h>
  104. int main()
  105. {
  106. unsigned u;
  107. struct bb_uidgid_t ug;
  108. u = get_uidgid(&ug, "apache", 0);
  109. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  110. ug.uid = ug.gid = 1111;
  111. u = get_uidgid(&ug, "apache", 0);
  112. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  113. ug.uid = ug.gid = 1111;
  114. u = get_uidgid(&ug, "apache:users", 0);
  115. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  116. ug.uid = ug.gid = 1111;
  117. u = get_uidgid(&ug, "apache:users", 0);
  118. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  119. return 0;
  120. }
  121. #endif