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