Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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 |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <ctype.h> |
| 30 | #include <errno.h> |
| 31 | #include <grp.h> |
| 32 | #include <mntent.h> |
| 33 | #include <netdb.h> |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 34 | #include <pthread.h> |
| 35 | #include <pwd.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
Mark Salyzyn | 56b2768 | 2015-03-31 16:55:42 -0700 | [diff] [blame] | 38 | #include <string.h> |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 39 | #include <unistd.h> |
| 40 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 41 | #include "private/android_filesystem_config.h" |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 42 | #include "private/ErrnoRestorer.h" |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 43 | #include "private/libc_logging.h" |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 44 | #include "private/ThreadLocalBuffer.h" |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 45 | |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 46 | // POSIX seems to envisage an implementation where the <pwd.h> functions are |
| 47 | // implemented by brute-force searching with getpwent(3), and the <grp.h> |
| 48 | // functions are implemented similarly with getgrent(3). This means that it's |
| 49 | // okay for all the <grp.h> functions to share state, and all the <passwd.h> |
| 50 | // functions to share state, but <grp.h> functions can't clobber <passwd.h> |
| 51 | // functions' state and vice versa. |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 52 | |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 53 | struct group_state_t { |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 54 | group group_; |
| 55 | char* group_members_[2]; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 56 | char group_name_buffer_[32]; |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 59 | struct passwd_state_t { |
| 60 | passwd passwd_; |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 61 | char name_buffer_[32]; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 62 | char dir_buffer_[32]; |
| 63 | char sh_buffer_[32]; |
| 64 | }; |
| 65 | |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 66 | static ThreadLocalBuffer<group_state_t> g_group_tls_buffer; |
| 67 | static ThreadLocalBuffer<passwd_state_t> g_passwd_tls_buffer; |
| 68 | |
| 69 | static group_state_t* __group_state() { |
| 70 | group_state_t* result = g_group_tls_buffer.get(); |
| 71 | if (result != nullptr) { |
| 72 | memset(result, 0, sizeof(group_state_t)); |
| 73 | result->group_.gr_mem = result->group_members_; |
| 74 | } |
| 75 | return result; |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 78 | static int do_getpw_r(int by_name, const char* name, uid_t uid, |
| 79 | passwd* dst, char* buf, size_t byte_count, |
| 80 | passwd** result) { |
| 81 | // getpwnam_r and getpwuid_r don't modify errno, but library calls we |
| 82 | // make might. |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 83 | ErrnoRestorer errno_restorer; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 84 | *result = NULL; |
| 85 | |
| 86 | // Our implementation of getpwnam(3) and getpwuid(3) use thread-local |
| 87 | // storage, so we can call them as long as we copy everything out |
| 88 | // before returning. |
| 89 | const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above. |
| 90 | |
| 91 | // POSIX allows failure to find a match to be considered a non-error. |
| 92 | // Reporting success (0) but with *result NULL is glibc's behavior. |
| 93 | if (src == NULL) { |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 94 | return (errno == ENOENT) ? 0 : errno; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Work out where our strings will go in 'buf', and whether we've got |
| 98 | // enough space. |
| 99 | size_t required_byte_count = 0; |
| 100 | dst->pw_name = buf; |
| 101 | required_byte_count += strlen(src->pw_name) + 1; |
| 102 | dst->pw_dir = buf + required_byte_count; |
| 103 | required_byte_count += strlen(src->pw_dir) + 1; |
| 104 | dst->pw_shell = buf + required_byte_count; |
| 105 | required_byte_count += strlen(src->pw_shell) + 1; |
| 106 | if (byte_count < required_byte_count) { |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 107 | return ERANGE; |
| 108 | } |
| 109 | |
| 110 | // Copy the strings. |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 111 | snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 112 | |
Calin Juravle | c768874 | 2014-05-09 21:50:53 +0100 | [diff] [blame] | 113 | // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic. |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 114 | dst->pw_passwd = NULL; |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 115 | #if defined(__LP64__) |
Calin Juravle | c768874 | 2014-05-09 21:50:53 +0100 | [diff] [blame] | 116 | dst->pw_gecos = NULL; |
| 117 | #endif |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 118 | |
| 119 | // Copy the integral fields. |
| 120 | dst->pw_gid = src->pw_gid; |
| 121 | dst->pw_uid = src->pw_uid; |
| 122 | |
| 123 | *result = dst; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int getpwnam_r(const char* name, passwd* pwd, |
| 128 | char* buf, size_t byte_count, passwd** result) { |
| 129 | return do_getpw_r(1, name, -1, pwd, buf, byte_count, result); |
| 130 | } |
| 131 | |
| 132 | int getpwuid_r(uid_t uid, passwd* pwd, |
| 133 | char* buf, size_t byte_count, passwd** result) { |
| 134 | return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result); |
| 135 | } |
| 136 | |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 137 | static passwd* android_iinfo_to_passwd(passwd_state_t* state, |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 138 | const android_id_info* iinfo) { |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 139 | snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 140 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); |
| 141 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); |
| 142 | |
| 143 | passwd* pw = &state->passwd_; |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 144 | pw->pw_name = state->name_buffer_; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 145 | pw->pw_uid = iinfo->aid; |
| 146 | pw->pw_gid = iinfo->aid; |
| 147 | pw->pw_dir = state->dir_buffer_; |
| 148 | pw->pw_shell = state->sh_buffer_; |
| 149 | return pw; |
| 150 | } |
| 151 | |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 152 | static group* android_iinfo_to_group(group_state_t* state, |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 153 | const android_id_info* iinfo) { |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 154 | snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name); |
| 155 | |
| 156 | group* gr = &state->group_; |
| 157 | gr->gr_name = state->group_name_buffer_; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 158 | gr->gr_gid = iinfo->aid; |
| 159 | gr->gr_mem[0] = gr->gr_name; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 160 | return gr; |
| 161 | } |
| 162 | |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 163 | static passwd* android_id_to_passwd(passwd_state_t* state, unsigned id) { |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 164 | for (size_t n = 0; n < android_id_count; ++n) { |
| 165 | if (android_ids[n].aid == id) { |
| 166 | return android_iinfo_to_passwd(state, android_ids + n); |
| 167 | } |
| 168 | } |
| 169 | return NULL; |
| 170 | } |
| 171 | |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 172 | static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) { |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 173 | for (size_t n = 0; n < android_id_count; ++n) { |
| 174 | if (!strcmp(android_ids[n].name, name)) { |
| 175 | return android_iinfo_to_passwd(state, android_ids + n); |
| 176 | } |
| 177 | } |
| 178 | return NULL; |
| 179 | } |
| 180 | |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 181 | static group* android_id_to_group(group_state_t* state, unsigned id) { |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 182 | for (size_t n = 0; n < android_id_count; ++n) { |
| 183 | if (android_ids[n].aid == id) { |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 184 | return android_iinfo_to_group(state, android_ids + n); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 190 | static group* android_name_to_group(group_state_t* state, const char* name) { |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 191 | for (size_t n = 0; n < android_id_count; ++n) { |
| 192 | if (!strcmp(android_ids[n].name, name)) { |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 193 | return android_iinfo_to_group(state, android_ids + n); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | // Translate a user/group name to the corresponding user/group id. |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 200 | // all_a1234 -> 0 * AID_USER + AID_SHARED_GID_START + 1234 (group name only) |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 201 | // u0_a1234 -> 0 * AID_USER + AID_APP + 1234 |
| 202 | // u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000 |
| 203 | // u1_system -> 1 * AID_USER + android_ids['system'] |
| 204 | // returns 0 and sets errno to ENOENT in case of error |
Elliott Hughes | c56af08 | 2015-01-22 11:02:59 -0800 | [diff] [blame] | 205 | static id_t app_id_from_name(const char* name, bool is_group) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 206 | char* end; |
| 207 | unsigned long userid; |
| 208 | bool is_shared_gid = false; |
| 209 | |
| 210 | if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') { |
| 211 | end = const_cast<char*>(name+3); |
| 212 | userid = 0; |
| 213 | is_shared_gid = true; |
| 214 | } else if (name[0] == 'u' && isdigit(name[1])) { |
| 215 | userid = strtoul(name+1, &end, 10); |
| 216 | } else { |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 217 | errno = ENOENT; |
| 218 | return 0; |
| 219 | } |
| 220 | |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 221 | if (end[0] != '_' || end[1] == 0) { |
| 222 | errno = ENOENT; |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | unsigned long appid = 0; |
| 227 | if (end[1] == 'a' && isdigit(end[2])) { |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 228 | if (is_shared_gid) { |
| 229 | // end will point to \0 if the strtoul below succeeds. |
| 230 | appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START; |
| 231 | if (appid > AID_SHARED_GID_END) { |
| 232 | errno = ENOENT; |
| 233 | return 0; |
| 234 | } |
| 235 | } else { |
| 236 | // end will point to \0 if the strtoul below succeeds. |
| 237 | appid = strtoul(end+2, &end, 10) + AID_APP; |
| 238 | } |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 239 | } else if (end[1] == 'i' && isdigit(end[2])) { |
| 240 | // end will point to \0 if the strtoul below succeeds. |
| 241 | appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START; |
| 242 | } else { |
| 243 | for (size_t n = 0; n < android_id_count; n++) { |
| 244 | if (!strcmp(android_ids[n].name, end + 1)) { |
| 245 | appid = android_ids[n].aid; |
| 246 | // Move the end pointer to the null terminator. |
| 247 | end += strlen(android_ids[n].name) + 1; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Check that the entire string was consumed by one of the 3 cases above. |
| 253 | if (end[0] != 0) { |
| 254 | errno = ENOENT; |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | // Check that user id won't overflow. |
| 259 | if (userid > 1000) { |
| 260 | errno = ENOENT; |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | // Check that app id is within range. |
| 265 | if (appid >= AID_USER) { |
| 266 | errno = ENOENT; |
| 267 | return 0; |
| 268 | } |
| 269 | |
Elliott Hughes | c56af08 | 2015-01-22 11:02:59 -0800 | [diff] [blame] | 270 | return (appid + userid*AID_USER); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 273 | static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) { |
| 274 | const uid_t appid = uid % AID_USER; |
| 275 | const uid_t userid = uid / AID_USER; |
Kenny Root | 8a05a01 | 2012-09-13 14:31:50 -0700 | [diff] [blame] | 276 | if (appid >= AID_ISOLATED_START) { |
| 277 | snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); |
Kenny Root | 8a05a01 | 2012-09-13 14:31:50 -0700 | [diff] [blame] | 278 | } else if (appid < AID_APP) { |
| 279 | for (size_t n = 0; n < android_id_count; n++) { |
| 280 | if (android_ids[n].aid == appid) { |
| 281 | snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); |
| 282 | return; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 285 | } else { |
Kenny Root | 8a05a01 | 2012-09-13 14:31:50 -0700 | [diff] [blame] | 286 | snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 290 | static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) { |
| 291 | const uid_t appid = gid % AID_USER; |
| 292 | const uid_t userid = gid / AID_USER; |
| 293 | if (appid >= AID_ISOLATED_START) { |
| 294 | snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); |
| 295 | } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) { |
| 296 | snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START); |
| 297 | } else if (appid < AID_APP) { |
| 298 | for (size_t n = 0; n < android_id_count; n++) { |
| 299 | if (android_ids[n].aid == appid) { |
| 300 | snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); |
| 301 | return; |
| 302 | } |
| 303 | } |
| 304 | } else { |
| 305 | snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP); |
| 306 | } |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 309 | // Translate a uid into the corresponding name. |
| 310 | // 0 to AID_APP-1 -> "system", "radio", etc. |
| 311 | // AID_APP to AID_ISOLATED_START-1 -> u0_a1234 |
| 312 | // AID_ISOLATED_START to AID_USER-1 -> u0_i1234 |
| 313 | // AID_USER+ -> u1_radio, u1_a1234, u2_i1234, etc. |
| 314 | // returns a passwd structure (sets errno to ENOENT on failure). |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 315 | static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) { |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 316 | if (uid < AID_APP) { |
| 317 | errno = ENOENT; |
| 318 | return NULL; |
| 319 | } |
| 320 | |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 321 | print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_)); |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 322 | |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 323 | const uid_t appid = uid % AID_USER; |
Kenny Root | 2a54e5e | 2012-09-13 10:52:52 -0700 | [diff] [blame] | 324 | if (appid < AID_APP) { |
| 325 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); |
| 326 | } else { |
| 327 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data"); |
| 328 | } |
| 329 | |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 330 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); |
| 331 | |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 332 | passwd* pw = &state->passwd_; |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 333 | pw->pw_name = state->name_buffer_; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 334 | pw->pw_dir = state->dir_buffer_; |
| 335 | pw->pw_shell = state->sh_buffer_; |
| 336 | pw->pw_uid = uid; |
| 337 | pw->pw_gid = uid; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 338 | return pw; |
| 339 | } |
| 340 | |
| 341 | // Translate a gid into the corresponding app_<gid> |
| 342 | // group structure (sets errno to ENOENT on failure). |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 343 | static group* app_id_to_group(gid_t gid, group_state_t* state) { |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 344 | if (gid < AID_APP) { |
| 345 | errno = ENOENT; |
| 346 | return NULL; |
| 347 | } |
| 348 | |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 349 | print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_)); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 350 | |
| 351 | group* gr = &state->group_; |
| 352 | gr->gr_name = state->group_name_buffer_; |
| 353 | gr->gr_gid = gid; |
| 354 | gr->gr_mem[0] = gr->gr_name; |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 355 | return gr; |
| 356 | } |
| 357 | |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 358 | passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function. |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 359 | passwd_state_t* state = g_passwd_tls_buffer.get(); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 360 | if (state == NULL) { |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | passwd* pw = android_id_to_passwd(state, uid); |
| 365 | if (pw != NULL) { |
| 366 | return pw; |
| 367 | } |
| 368 | return app_id_to_passwd(uid, state); |
| 369 | } |
| 370 | |
| 371 | passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 372 | passwd_state_t* state = g_passwd_tls_buffer.get(); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 373 | if (state == NULL) { |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | passwd* pw = android_name_to_passwd(state, login); |
| 378 | if (pw != NULL) { |
| 379 | return pw; |
| 380 | } |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 381 | return app_id_to_passwd(app_id_from_name(login, false), state); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Elliott Hughes | 6fa26e2 | 2012-10-22 16:04:56 -0700 | [diff] [blame] | 384 | // All users are in just one group, the one passed in. |
| 385 | int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) { |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 386 | if (*ngroups < 1) { |
| 387 | *ngroups = 1; |
| 388 | return -1; |
| 389 | } |
| 390 | groups[0] = group; |
| 391 | return (*ngroups = 1); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | char* getlogin() { // NOLINT: implementing bad function. |
| 395 | passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function. |
| 396 | return (pw != NULL) ? pw->pw_name : NULL; |
| 397 | } |
| 398 | |
| 399 | group* getgrgid(gid_t gid) { // NOLINT: implementing bad function. |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 400 | group_state_t* state = __group_state(); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 401 | if (state == NULL) { |
| 402 | return NULL; |
| 403 | } |
| 404 | |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 405 | group* gr = android_id_to_group(state, gid); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 406 | if (gr != NULL) { |
| 407 | return gr; |
| 408 | } |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 409 | return app_id_to_group(gid, state); |
| 410 | } |
| 411 | |
| 412 | group* getgrnam(const char* name) { // NOLINT: implementing bad function. |
Elliott Hughes | 7874f1d | 2014-12-18 13:36:25 -0800 | [diff] [blame] | 413 | group_state_t* state = __group_state(); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 414 | if (state == NULL) { |
| 415 | return NULL; |
| 416 | } |
| 417 | |
Elliott Hughes | 8b5df39 | 2015-01-21 16:19:07 -0800 | [diff] [blame] | 418 | if (android_name_to_group(state, name) != 0) { |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 419 | return &state->group_; |
| 420 | } |
Yabin Cui | a04c79b | 2014-11-18 16:14:54 -0800 | [diff] [blame] | 421 | return app_id_to_group(app_id_from_name(name, true), state); |
Elliott Hughes | de727ca | 2012-08-13 15:45:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Elliott Hughes | 6fa26e2 | 2012-10-22 16:04:56 -0700 | [diff] [blame] | 424 | // We don't have an /etc/networks, so all inputs return NULL. |
| 425 | netent* getnetbyname(const char* /*name*/) { |
| 426 | return NULL; |
| 427 | } |
| 428 | |
| 429 | // We don't have an /etc/networks, so all inputs return NULL. |
| 430 | netent* getnetbyaddr(uint32_t /*net*/, int /*type*/) { |
| 431 | return NULL; |
| 432 | } |
| 433 | |
| 434 | // We don't have an /etc/protocols, so all inputs return NULL. |
| 435 | protoent* getprotobyname(const char* /*name*/) { |
| 436 | return NULL; |
| 437 | } |
| 438 | |
| 439 | // We don't have an /etc/protocols, so all inputs return NULL. |
| 440 | protoent* getprotobynumber(int /*proto*/) { |
| 441 | return NULL; |
| 442 | } |
| 443 | |
Elliott Hughes | 0e44bc3 | 2014-02-24 15:55:31 -0800 | [diff] [blame] | 444 | // Portable code should use sysconf(_SC_PAGE_SIZE) directly instead. |
Bernhard Rosenkraenzer | 9ae59c0 | 2013-09-18 23:29:08 +0200 | [diff] [blame] | 445 | int getpagesize() { |
Elliott Hughes | 91570ce | 2014-07-10 12:34:23 -0700 | [diff] [blame] | 446 | // We dont use sysconf(3) here because that drags in stdio, which makes static binaries fat. |
| 447 | return PAGE_SIZE; |
Bernhard Rosenkraenzer | 9ae59c0 | 2013-09-18 23:29:08 +0200 | [diff] [blame] | 448 | } |