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