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