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 | |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 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 | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 28 | #include "libbb.h" |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 30 | /* Always sets uid and gid */ |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 31 | int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug) |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 32 | { |
| 33 | struct passwd *pwd; |
| 34 | struct group *gr; |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 35 | char *user, *group; |
| 36 | unsigned n; |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 38 | user = (char*)ug; |
| 39 | group = strchr(ug, ':'); |
| 40 | if (group) { |
| 41 | int sz = (++group) - ug; |
| 42 | user = alloca(sz); |
| 43 | /* copies sz-1 bytes, stores terminating '\0' */ |
| 44 | safe_strncpy(user, ug, sz); |
| 45 | } |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 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; |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 54 | } |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 55 | /* it is not numeric */ |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 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) { |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 64 | n = bb_strtou(group, NULL, 10); |
| 65 | if (!errno) { |
| 66 | u->gid = n; |
| 67 | return 1; |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 68 | } |
| 69 | gr = getgrnam(group); |
Denys Vlasenko | 4566e17 | 2011-05-16 00:01:08 +0200 | [diff] [blame] | 70 | if (!gr) |
| 71 | return 0; |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 72 | u->gid = gr->gr_gid; |
| 73 | } |
| 74 | return 1; |
| 75 | } |
Bernhard Reutner-Fischer | d73cbd3 | 2008-07-21 14:41:33 +0000 | [diff] [blame] | 76 | void FAST_FUNC xget_uidgid(struct bb_uidgid_t *u, const char *ug) |
| 77 | { |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 78 | if (!get_uidgid(u, ug)) |
Denis Vlasenko | b74a2db | 2008-07-21 21:34:51 +0000 | [diff] [blame] | 79 | bb_error_msg_and_die("unknown user/group %s", ug); |
Bernhard Reutner-Fischer | d73cbd3 | 2008-07-21 14:41:33 +0000 | [diff] [blame] | 80 | } |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 82 | /* chown-like: |
| 83 | * "user" sets uid only, |
| 84 | * ":group" sets gid only |
| 85 | * "user:" sets uid and gid (to user's primary group id) |
| 86 | * "user:group" sets uid and gid |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 87 | * ('unset' uid or gid retains the value it has on entry) |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 88 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 89 | void FAST_FUNC parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group) |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 90 | { |
| 91 | char *group; |
| 92 | |
Denys Vlasenko | 3d0805e | 2015-10-19 04:37:19 +0200 | [diff] [blame] | 93 | u->uid = u->gid = (gid_t)-1L; |
| 94 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 95 | /* Check if there is a group name */ |
| 96 | group = strchr(user_group, '.'); /* deprecated? */ |
| 97 | if (!group) |
| 98 | group = strchr(user_group, ':'); |
| 99 | else |
| 100 | *group = ':'; /* replace '.' with ':' */ |
| 101 | |
| 102 | /* Parse "user[:[group]]" */ |
| 103 | if (!group) { /* "user" */ |
| 104 | u->uid = get_ug_id(user_group, xuname2uid); |
| 105 | } else if (group == user_group) { /* ":group" */ |
| 106 | u->gid = get_ug_id(group + 1, xgroup2gid); |
| 107 | } else { |
| 108 | if (!group[1]) /* "user:" */ |
| 109 | *group = '\0'; |
Bernhard Reutner-Fischer | d73cbd3 | 2008-07-21 14:41:33 +0000 | [diff] [blame] | 110 | xget_uidgid(u, user_group); |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 114 | #if 0 |
| 115 | #include <stdio.h> |
| 116 | int main() |
| 117 | { |
| 118 | unsigned u; |
| 119 | struct bb_uidgid_t ug; |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 120 | u = get_uidgid(&ug, "apache"); |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 121 | printf("%u = %u:%u\n", u, ug.uid, ug.gid); |
| 122 | ug.uid = ug.gid = 1111; |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 123 | u = get_uidgid(&ug, "apache"); |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 124 | printf("%u = %u:%u\n", u, ug.uid, ug.gid); |
| 125 | ug.uid = ug.gid = 1111; |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 126 | u = get_uidgid(&ug, "apache:users"); |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 127 | printf("%u = %u:%u\n", u, ug.uid, ug.gid); |
| 128 | ug.uid = ug.gid = 1111; |
Denys Vlasenko | 526d858 | 2015-10-19 04:27:17 +0200 | [diff] [blame] | 129 | u = get_uidgid(&ug, "apache:users"); |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 130 | printf("%u = %u:%u\n", u, ug.uid, ug.gid); |
| 131 | return 0; |
| 132 | } |
| 133 | #endif |