uidgid_get.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 FAST_FUNC 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. void FAST_FUNC xget_uidgid(struct bb_uidgid_t *u, const char *ug)
  73. {
  74. if (!get_uidgid(u, ug, 1))
  75. bb_error_msg_and_die("unknown user/group %s", ug);
  76. }
  77. /* chown-like:
  78. * "user" sets uid only,
  79. * ":group" sets gid only
  80. * "user:" sets uid and gid (to user's primary group id)
  81. * "user:group" sets uid and gid
  82. * ('unset' uid or gid is actually set to -1)
  83. */
  84. void FAST_FUNC parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group)
  85. {
  86. char *group;
  87. u->uid = -1;
  88. u->gid = -1;
  89. /* Check if there is a group name */
  90. group = strchr(user_group, '.'); /* deprecated? */
  91. if (!group)
  92. group = strchr(user_group, ':');
  93. else
  94. *group = ':'; /* replace '.' with ':' */
  95. /* Parse "user[:[group]]" */
  96. if (!group) { /* "user" */
  97. u->uid = get_ug_id(user_group, xuname2uid);
  98. } else if (group == user_group) { /* ":group" */
  99. u->gid = get_ug_id(group + 1, xgroup2gid);
  100. } else {
  101. if (!group[1]) /* "user:" */
  102. *group = '\0';
  103. xget_uidgid(u, user_group);
  104. }
  105. }
  106. #if 0
  107. #include <stdio.h>
  108. int main()
  109. {
  110. unsigned u;
  111. struct bb_uidgid_t ug;
  112. u = get_uidgid(&ug, "apache", 0);
  113. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  114. ug.uid = ug.gid = 1111;
  115. u = get_uidgid(&ug, "apache", 0);
  116. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  117. ug.uid = ug.gid = 1111;
  118. u = get_uidgid(&ug, "apache:users", 0);
  119. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  120. ug.uid = ug.gid = 1111;
  121. u = get_uidgid(&ug, "apache:users", 0);
  122. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  123. return 0;
  124. }
  125. #endif