uidgid_get.c 3.8 KB

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