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