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 FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug)
  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. n = bb_strtou(user, NULL, 10);
  41. if (!errno) {
  42. u->uid = n;
  43. pwd = getpwuid(n);
  44. /* If we have e.g. "500" string without user */
  45. /* with uid 500 in /etc/passwd, we set gid == uid */
  46. u->gid = pwd ? pwd->pw_gid : n;
  47. goto skip;
  48. }
  49. /* it is not numeric */
  50. pwd = getpwnam(user);
  51. if (!pwd)
  52. return 0;
  53. u->uid = pwd->pw_uid;
  54. u->gid = pwd->pw_gid;
  55. skip:
  56. if (group) {
  57. n = bb_strtou(group, NULL, 10);
  58. if (!errno) {
  59. u->gid = n;
  60. return 1;
  61. }
  62. gr = getgrnam(group);
  63. if (!gr)
  64. return 0;
  65. u->gid = gr->gr_gid;
  66. }
  67. return 1;
  68. }
  69. void FAST_FUNC xget_uidgid(struct bb_uidgid_t *u, const char *ug)
  70. {
  71. if (!get_uidgid(u, ug))
  72. bb_error_msg_and_die("unknown user/group %s", ug);
  73. }
  74. /* chown-like:
  75. * "user" sets uid only,
  76. * ":group" sets gid only
  77. * "user:" sets uid and gid (to user's primary group id)
  78. * "user:group" sets uid and gid
  79. * ('unset' uid or gid retains the value it has on entry)
  80. */
  81. void FAST_FUNC parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group)
  82. {
  83. char *group;
  84. u->uid = u->gid = (gid_t)-1L;
  85. /* Check if there is a group name */
  86. group = strchr(user_group, '.'); /* deprecated? */
  87. if (!group)
  88. group = strchr(user_group, ':');
  89. else
  90. *group = ':'; /* replace '.' with ':' */
  91. /* Parse "user[:[group]]" */
  92. if (!group) { /* "user" */
  93. u->uid = get_ug_id(user_group, xuname2uid);
  94. } else if (group == user_group) { /* ":group" */
  95. u->gid = get_ug_id(group + 1, xgroup2gid);
  96. } else {
  97. if (!group[1]) /* "user:" */
  98. *group = '\0';
  99. xget_uidgid(u, 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");
  109. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  110. ug.uid = ug.gid = 1111;
  111. u = get_uidgid(&ug, "apache");
  112. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  113. ug.uid = ug.gid = 1111;
  114. u = get_uidgid(&ug, "apache:users");
  115. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  116. ug.uid = ug.gid = 1111;
  117. u = get_uidgid(&ug, "apache:users");
  118. printf("%u = %u:%u\n", u, ug.uid, ug.gid);
  119. return 0;
  120. }
  121. #endif