Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -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 <pthread.h> |
| 34 | #include <pwd.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <unistd.h> |
| 39 | |
| 40 | #include "private/android_filesystem_config.h" |
| 41 | #include "private/bionic_macros.h" |
| 42 | #include "private/ErrnoRestorer.h" |
| 43 | #include "private/libc_logging.h" |
| 44 | #include "private/ThreadLocalBuffer.h" |
| 45 | |
Elliott Hughes | 3f6eee9 | 2016-12-13 23:47:25 +0000 | [diff] [blame^] | 46 | // Generated android_ids array |
| 47 | #include "generated_android_ids.h" |
| 48 | |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 49 | // POSIX seems to envisage an implementation where the <pwd.h> functions are |
| 50 | // implemented by brute-force searching with getpwent(3), and the <grp.h> |
| 51 | // functions are implemented similarly with getgrent(3). This means that it's |
| 52 | // okay for all the <grp.h> functions to share state, and all the <passwd.h> |
| 53 | // functions to share state, but <grp.h> functions can't clobber <passwd.h> |
| 54 | // functions' state and vice versa. |
| 55 | |
| 56 | struct group_state_t { |
| 57 | group group_; |
| 58 | char* group_members_[2]; |
| 59 | char group_name_buffer_[32]; |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 60 | // Must be last so init_group_state can run a simple memset for the above |
| 61 | ssize_t getgrent_idx; |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct passwd_state_t { |
| 65 | passwd passwd_; |
| 66 | char name_buffer_[32]; |
| 67 | char dir_buffer_[32]; |
| 68 | char sh_buffer_[32]; |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 69 | ssize_t getpwent_idx; |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static ThreadLocalBuffer<group_state_t> g_group_tls_buffer; |
| 73 | static ThreadLocalBuffer<passwd_state_t> g_passwd_tls_buffer; |
| 74 | |
| 75 | static void init_group_state(group_state_t* state) { |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 76 | memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx)); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 77 | state->group_.gr_mem = state->group_members_; |
| 78 | } |
| 79 | |
| 80 | static group_state_t* __group_state() { |
| 81 | group_state_t* result = g_group_tls_buffer.get(); |
| 82 | if (result != nullptr) { |
| 83 | init_group_state(result); |
| 84 | } |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | static int do_getpw_r(int by_name, const char* name, uid_t uid, |
| 89 | passwd* dst, char* buf, size_t byte_count, |
| 90 | passwd** result) { |
| 91 | // getpwnam_r and getpwuid_r don't modify errno, but library calls we |
| 92 | // make might. |
| 93 | ErrnoRestorer errno_restorer; |
| 94 | *result = NULL; |
| 95 | |
| 96 | // Our implementation of getpwnam(3) and getpwuid(3) use thread-local |
| 97 | // storage, so we can call them as long as we copy everything out |
| 98 | // before returning. |
| 99 | const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above. |
| 100 | |
| 101 | // POSIX allows failure to find a match to be considered a non-error. |
| 102 | // Reporting success (0) but with *result NULL is glibc's behavior. |
| 103 | if (src == NULL) { |
| 104 | return (errno == ENOENT) ? 0 : errno; |
| 105 | } |
| 106 | |
| 107 | // Work out where our strings will go in 'buf', and whether we've got |
| 108 | // enough space. |
| 109 | size_t required_byte_count = 0; |
| 110 | dst->pw_name = buf; |
| 111 | required_byte_count += strlen(src->pw_name) + 1; |
| 112 | dst->pw_dir = buf + required_byte_count; |
| 113 | required_byte_count += strlen(src->pw_dir) + 1; |
| 114 | dst->pw_shell = buf + required_byte_count; |
| 115 | required_byte_count += strlen(src->pw_shell) + 1; |
| 116 | if (byte_count < required_byte_count) { |
| 117 | return ERANGE; |
| 118 | } |
| 119 | |
| 120 | // Copy the strings. |
| 121 | snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell); |
| 122 | |
| 123 | // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic. |
| 124 | // Note: On LP32, we define pw_gecos to pw_passwd since they're both NULL. |
| 125 | dst->pw_passwd = NULL; |
| 126 | #if defined(__LP64__) |
| 127 | dst->pw_gecos = NULL; |
| 128 | #endif |
| 129 | |
| 130 | // Copy the integral fields. |
| 131 | dst->pw_gid = src->pw_gid; |
| 132 | dst->pw_uid = src->pw_uid; |
| 133 | |
| 134 | *result = dst; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int getpwnam_r(const char* name, passwd* pwd, |
| 139 | char* buf, size_t byte_count, passwd** result) { |
| 140 | return do_getpw_r(1, name, -1, pwd, buf, byte_count, result); |
| 141 | } |
| 142 | |
| 143 | int getpwuid_r(uid_t uid, passwd* pwd, |
| 144 | char* buf, size_t byte_count, passwd** result) { |
| 145 | return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result); |
| 146 | } |
| 147 | |
| 148 | static passwd* android_iinfo_to_passwd(passwd_state_t* state, |
| 149 | const android_id_info* iinfo) { |
| 150 | snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name); |
| 151 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); |
| 152 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); |
| 153 | |
| 154 | passwd* pw = &state->passwd_; |
| 155 | pw->pw_name = state->name_buffer_; |
| 156 | pw->pw_uid = iinfo->aid; |
| 157 | pw->pw_gid = iinfo->aid; |
| 158 | pw->pw_dir = state->dir_buffer_; |
| 159 | pw->pw_shell = state->sh_buffer_; |
| 160 | return pw; |
| 161 | } |
| 162 | |
| 163 | static group* android_iinfo_to_group(group_state_t* state, |
| 164 | const android_id_info* iinfo) { |
| 165 | snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name); |
| 166 | |
| 167 | group* gr = &state->group_; |
| 168 | gr->gr_name = state->group_name_buffer_; |
| 169 | gr->gr_gid = iinfo->aid; |
| 170 | gr->gr_mem[0] = gr->gr_name; |
| 171 | return gr; |
| 172 | } |
| 173 | |
| 174 | static passwd* android_id_to_passwd(passwd_state_t* state, unsigned id) { |
| 175 | for (size_t n = 0; n < android_id_count; ++n) { |
| 176 | if (android_ids[n].aid == id) { |
| 177 | return android_iinfo_to_passwd(state, android_ids + n); |
| 178 | } |
| 179 | } |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) { |
| 184 | for (size_t n = 0; n < android_id_count; ++n) { |
| 185 | if (!strcmp(android_ids[n].name, name)) { |
| 186 | return android_iinfo_to_passwd(state, android_ids + n); |
| 187 | } |
| 188 | } |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | static group* android_id_to_group(group_state_t* state, unsigned id) { |
| 193 | for (size_t n = 0; n < android_id_count; ++n) { |
| 194 | if (android_ids[n].aid == id) { |
| 195 | return android_iinfo_to_group(state, android_ids + n); |
| 196 | } |
| 197 | } |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | static group* android_name_to_group(group_state_t* state, const char* name) { |
| 202 | for (size_t n = 0; n < android_id_count; ++n) { |
| 203 | if (!strcmp(android_ids[n].name, name)) { |
| 204 | return android_iinfo_to_group(state, android_ids + n); |
| 205 | } |
| 206 | } |
| 207 | return NULL; |
| 208 | } |
| 209 | |
| 210 | // Translate a user/group name to the corresponding user/group id. |
| 211 | // all_a1234 -> 0 * AID_USER + AID_SHARED_GID_START + 1234 (group name only) |
| 212 | // u0_a1234 -> 0 * AID_USER + AID_APP + 1234 |
| 213 | // u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000 |
| 214 | // u1_system -> 1 * AID_USER + android_ids['system'] |
| 215 | // returns 0 and sets errno to ENOENT in case of error. |
| 216 | static id_t app_id_from_name(const char* name, bool is_group) { |
| 217 | char* end; |
| 218 | unsigned long userid; |
| 219 | bool is_shared_gid = false; |
| 220 | |
| 221 | if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') { |
| 222 | end = const_cast<char*>(name+3); |
| 223 | userid = 0; |
| 224 | is_shared_gid = true; |
| 225 | } else if (name[0] == 'u' && isdigit(name[1])) { |
| 226 | userid = strtoul(name+1, &end, 10); |
| 227 | } else { |
| 228 | errno = ENOENT; |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | if (end[0] != '_' || end[1] == 0) { |
| 233 | errno = ENOENT; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | unsigned long appid = 0; |
| 238 | if (end[1] == 'a' && isdigit(end[2])) { |
| 239 | if (is_shared_gid) { |
| 240 | // end will point to \0 if the strtoul below succeeds. |
| 241 | appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START; |
| 242 | if (appid > AID_SHARED_GID_END) { |
| 243 | errno = ENOENT; |
| 244 | return 0; |
| 245 | } |
| 246 | } else { |
| 247 | // end will point to \0 if the strtoul below succeeds. |
| 248 | appid = strtoul(end+2, &end, 10) + AID_APP; |
| 249 | } |
| 250 | } else if (end[1] == 'i' && isdigit(end[2])) { |
| 251 | // end will point to \0 if the strtoul below succeeds. |
| 252 | appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START; |
| 253 | } else { |
| 254 | for (size_t n = 0; n < android_id_count; n++) { |
| 255 | if (!strcmp(android_ids[n].name, end + 1)) { |
| 256 | appid = android_ids[n].aid; |
| 257 | // Move the end pointer to the null terminator. |
| 258 | end += strlen(android_ids[n].name) + 1; |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // Check that the entire string was consumed by one of the 3 cases above. |
| 265 | if (end[0] != 0) { |
| 266 | errno = ENOENT; |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | // Check that user id won't overflow. |
| 271 | if (userid > 1000) { |
| 272 | errno = ENOENT; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | // Check that app id is within range. |
| 277 | if (appid >= AID_USER) { |
| 278 | errno = ENOENT; |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | return (appid + userid*AID_USER); |
| 283 | } |
| 284 | |
| 285 | static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) { |
| 286 | const uid_t appid = uid % AID_USER; |
| 287 | const uid_t userid = uid / AID_USER; |
| 288 | if (appid >= AID_ISOLATED_START) { |
| 289 | snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); |
| 290 | } else if (appid < AID_APP) { |
| 291 | for (size_t n = 0; n < android_id_count; n++) { |
| 292 | if (android_ids[n].aid == appid) { |
| 293 | snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); |
| 294 | return; |
| 295 | } |
| 296 | } |
| 297 | } else { |
| 298 | snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) { |
| 303 | const uid_t appid = gid % AID_USER; |
| 304 | const uid_t userid = gid / AID_USER; |
| 305 | if (appid >= AID_ISOLATED_START) { |
| 306 | snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); |
| 307 | } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) { |
| 308 | snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START); |
| 309 | } else if (appid < AID_APP) { |
| 310 | for (size_t n = 0; n < android_id_count; n++) { |
| 311 | if (android_ids[n].aid == appid) { |
| 312 | snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name); |
| 313 | return; |
| 314 | } |
| 315 | } |
| 316 | } else { |
| 317 | snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP); |
| 318 | } |
| 319 | } |
| 320 | |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 321 | // oem_XXXX -> uid |
| 322 | // Supported ranges: |
| 323 | // AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999) |
| 324 | // AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999) |
| 325 | // Check OEM id is within range. |
| 326 | static bool is_oem_id(id_t id) { |
| 327 | return (((id >= AID_OEM_RESERVED_START) && (id <= AID_OEM_RESERVED_END)) || |
| 328 | ((id >= AID_OEM_RESERVED_2_START) && (id <= AID_OEM_RESERVED_2_END))); |
| 329 | } |
| 330 | |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 331 | // Translate an OEM name to the corresponding user/group id. |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 332 | static id_t oem_id_from_name(const char* name) { |
| 333 | unsigned int id; |
| 334 | if (sscanf(name, "oem_%u", &id) != 1) { |
| 335 | return 0; |
| 336 | } |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 337 | if (!is_oem_id(id)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 338 | return 0; |
| 339 | } |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 340 | return static_cast<id_t>(id); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) { |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 344 | if (!is_oem_id(uid)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 345 | return NULL; |
| 346 | } |
| 347 | |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 348 | snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 349 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); |
| 350 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); |
| 351 | |
| 352 | passwd* pw = &state->passwd_; |
| 353 | pw->pw_name = state->name_buffer_; |
| 354 | pw->pw_dir = state->dir_buffer_; |
| 355 | pw->pw_shell = state->sh_buffer_; |
| 356 | pw->pw_uid = uid; |
| 357 | pw->pw_gid = uid; |
| 358 | return pw; |
| 359 | } |
| 360 | |
| 361 | static group* oem_id_to_group(gid_t gid, group_state_t* state) { |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 362 | if (!is_oem_id(gid)) { |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), |
Mark Salyzyn | 8d387ee | 2016-04-05 09:24:59 -0700 | [diff] [blame] | 367 | "oem_%u", gid); |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 368 | |
| 369 | group* gr = &state->group_; |
| 370 | gr->gr_name = state->group_name_buffer_; |
| 371 | gr->gr_gid = gid; |
| 372 | gr->gr_mem[0] = gr->gr_name; |
| 373 | return gr; |
| 374 | } |
| 375 | |
| 376 | // Translate a uid into the corresponding name. |
| 377 | // 0 to AID_APP-1 -> "system", "radio", etc. |
| 378 | // AID_APP to AID_ISOLATED_START-1 -> u0_a1234 |
| 379 | // AID_ISOLATED_START to AID_USER-1 -> u0_i1234 |
| 380 | // AID_USER+ -> u1_radio, u1_a1234, u2_i1234, etc. |
| 381 | // returns a passwd structure (sets errno to ENOENT on failure). |
| 382 | static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) { |
| 383 | if (uid < AID_APP) { |
| 384 | errno = ENOENT; |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_)); |
| 389 | |
| 390 | const uid_t appid = uid % AID_USER; |
| 391 | if (appid < AID_APP) { |
| 392 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/"); |
| 393 | } else { |
| 394 | snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data"); |
| 395 | } |
| 396 | |
| 397 | snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh"); |
| 398 | |
| 399 | passwd* pw = &state->passwd_; |
| 400 | pw->pw_name = state->name_buffer_; |
| 401 | pw->pw_dir = state->dir_buffer_; |
| 402 | pw->pw_shell = state->sh_buffer_; |
| 403 | pw->pw_uid = uid; |
| 404 | pw->pw_gid = uid; |
| 405 | return pw; |
| 406 | } |
| 407 | |
| 408 | // Translate a gid into the corresponding app_<gid> |
| 409 | // group structure (sets errno to ENOENT on failure). |
| 410 | static group* app_id_to_group(gid_t gid, group_state_t* state) { |
| 411 | if (gid < AID_APP) { |
| 412 | errno = ENOENT; |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_)); |
| 417 | |
| 418 | group* gr = &state->group_; |
| 419 | gr->gr_name = state->group_name_buffer_; |
| 420 | gr->gr_gid = gid; |
| 421 | gr->gr_mem[0] = gr->gr_name; |
| 422 | return gr; |
| 423 | } |
| 424 | |
| 425 | passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function. |
| 426 | passwd_state_t* state = g_passwd_tls_buffer.get(); |
| 427 | if (state == NULL) { |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | passwd* pw = android_id_to_passwd(state, uid); |
| 432 | if (pw != NULL) { |
| 433 | return pw; |
| 434 | } |
| 435 | // Handle OEM range. |
| 436 | pw = oem_id_to_passwd(uid, state); |
| 437 | if (pw != NULL) { |
| 438 | return pw; |
| 439 | } |
| 440 | return app_id_to_passwd(uid, state); |
| 441 | } |
| 442 | |
| 443 | passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. |
| 444 | passwd_state_t* state = g_passwd_tls_buffer.get(); |
| 445 | if (state == NULL) { |
| 446 | return NULL; |
| 447 | } |
| 448 | |
| 449 | passwd* pw = android_name_to_passwd(state, login); |
| 450 | if (pw != NULL) { |
| 451 | return pw; |
| 452 | } |
| 453 | // Handle OEM range. |
| 454 | pw = oem_id_to_passwd(oem_id_from_name(login), state); |
| 455 | if (pw != NULL) { |
| 456 | return pw; |
| 457 | } |
| 458 | return app_id_to_passwd(app_id_from_name(login, false), state); |
| 459 | } |
| 460 | |
| 461 | // All users are in just one group, the one passed in. |
| 462 | int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) { |
| 463 | if (*ngroups < 1) { |
| 464 | *ngroups = 1; |
| 465 | return -1; |
| 466 | } |
| 467 | groups[0] = group; |
| 468 | return (*ngroups = 1); |
| 469 | } |
| 470 | |
| 471 | char* getlogin() { // NOLINT: implementing bad function. |
| 472 | passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function. |
| 473 | return (pw != NULL) ? pw->pw_name : NULL; |
| 474 | } |
| 475 | |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 476 | void setpwent() { |
| 477 | passwd_state_t* state = g_passwd_tls_buffer.get(); |
| 478 | if (state) { |
| 479 | state->getpwent_idx = 0; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | void endpwent() { |
| 484 | setpwent(); |
| 485 | } |
| 486 | |
| 487 | passwd* getpwent() { |
| 488 | passwd_state_t* state = g_passwd_tls_buffer.get(); |
| 489 | if (state == NULL) { |
| 490 | return NULL; |
| 491 | } |
| 492 | if (state->getpwent_idx < 0) { |
| 493 | return NULL; |
| 494 | } |
| 495 | |
| 496 | size_t start = 0; |
| 497 | ssize_t end = android_id_count; |
| 498 | if (state->getpwent_idx < end) { |
| 499 | return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++); |
| 500 | } |
| 501 | |
| 502 | start = end; |
| 503 | end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1; |
| 504 | |
| 505 | if (state->getpwent_idx < end) { |
| 506 | return oem_id_to_passwd( |
| 507 | state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state); |
| 508 | } |
| 509 | |
| 510 | start = end; |
| 511 | end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1; |
| 512 | |
| 513 | if (state->getpwent_idx < end) { |
| 514 | return oem_id_to_passwd( |
| 515 | state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state); |
| 516 | } |
| 517 | |
| 518 | start = end; |
| 519 | end += AID_USER - AID_APP; // Do not expose higher users |
| 520 | |
| 521 | if (state->getpwent_idx < end) { |
| 522 | return app_id_to_passwd(state->getpwent_idx++ - start + AID_APP, state); |
| 523 | } |
| 524 | |
| 525 | // We are not reporting u1_a* and higher or we will be here forever |
| 526 | state->getpwent_idx = -1; |
| 527 | return NULL; |
| 528 | } |
| 529 | |
Mark Salyzyn | b38347a | 2016-04-05 09:09:46 -0700 | [diff] [blame] | 530 | static group* getgrgid_internal(gid_t gid, group_state_t* state) { |
| 531 | group* grp = android_id_to_group(state, gid); |
| 532 | if (grp != NULL) { |
| 533 | return grp; |
| 534 | } |
| 535 | // Handle OEM range. |
| 536 | grp = oem_id_to_group(gid, state); |
| 537 | if (grp != NULL) { |
| 538 | return grp; |
| 539 | } |
| 540 | return app_id_to_group(gid, state); |
| 541 | } |
| 542 | |
| 543 | group* getgrgid(gid_t gid) { // NOLINT: implementing bad function. |
| 544 | group_state_t* state = __group_state(); |
| 545 | if (state == NULL) { |
| 546 | return NULL; |
| 547 | } |
| 548 | return getgrgid_internal(gid, state); |
| 549 | } |
| 550 | |
| 551 | static group* getgrnam_internal(const char* name, group_state_t* state) { |
| 552 | group* grp = android_name_to_group(state, name); |
| 553 | if (grp != NULL) { |
| 554 | return grp; |
| 555 | } |
| 556 | // Handle OEM range. |
| 557 | grp = oem_id_to_group(oem_id_from_name(name), state); |
| 558 | if (grp != NULL) { |
| 559 | return grp; |
| 560 | } |
| 561 | return app_id_to_group(app_id_from_name(name, true), state); |
| 562 | } |
| 563 | |
| 564 | group* getgrnam(const char* name) { // NOLINT: implementing bad function. |
| 565 | group_state_t* state = __group_state(); |
| 566 | if (state == NULL) { |
| 567 | return NULL; |
| 568 | } |
| 569 | return getgrnam_internal(name, state); |
| 570 | } |
| 571 | |
| 572 | static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf, |
| 573 | size_t buflen, struct group** result) { |
| 574 | ErrnoRestorer errno_restorer; |
| 575 | *result = NULL; |
| 576 | char* p = reinterpret_cast<char*>( |
| 577 | BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t))); |
| 578 | if (p + sizeof(group_state_t) > buf + buflen) { |
| 579 | return ERANGE; |
| 580 | } |
| 581 | group_state_t* state = reinterpret_cast<group_state_t*>(p); |
| 582 | init_group_state(state); |
| 583 | group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state)); |
| 584 | if (retval != NULL) { |
| 585 | *grp = *retval; |
| 586 | *result = grp; |
| 587 | return 0; |
| 588 | } |
| 589 | return errno; |
| 590 | } |
| 591 | |
| 592 | int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) { |
| 593 | return getgroup_r(false, NULL, gid, grp, buf, buflen, result); |
| 594 | } |
| 595 | |
| 596 | int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen, |
| 597 | struct group **result) { |
| 598 | return getgroup_r(true, name, 0, grp, buf, buflen, result); |
| 599 | } |
Mark Salyzyn | 722ab05 | 2016-04-06 10:35:48 -0700 | [diff] [blame] | 600 | |
| 601 | void setgrent() { |
| 602 | group_state_t* state = g_group_tls_buffer.get(); |
| 603 | if (state) { |
| 604 | state->getgrent_idx = 0; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | void endgrent() { |
| 609 | setgrent(); |
| 610 | } |
| 611 | |
| 612 | group* getgrent() { |
| 613 | group_state_t* state = g_group_tls_buffer.get(); |
| 614 | if (state == NULL) { |
| 615 | return NULL; |
| 616 | } |
| 617 | if (state->getgrent_idx < 0) { |
| 618 | return NULL; |
| 619 | } |
| 620 | |
| 621 | size_t start = 0; |
| 622 | ssize_t end = android_id_count; |
| 623 | if (state->getgrent_idx < end) { |
| 624 | init_group_state(state); |
| 625 | return android_iinfo_to_group(state, android_ids + state->getgrent_idx++); |
| 626 | } |
| 627 | |
| 628 | start = end; |
| 629 | end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1; |
| 630 | |
| 631 | if (state->getgrent_idx < end) { |
| 632 | init_group_state(state); |
| 633 | return oem_id_to_group( |
| 634 | state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state); |
| 635 | } |
| 636 | |
| 637 | start = end; |
| 638 | end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1; |
| 639 | |
| 640 | if (state->getgrent_idx < end) { |
| 641 | init_group_state(state); |
| 642 | return oem_id_to_group( |
| 643 | state->getgrent_idx++ - start + AID_OEM_RESERVED_2_START, state); |
| 644 | } |
| 645 | |
| 646 | start = end; |
| 647 | end += AID_USER - AID_APP; // Do not expose higher groups |
| 648 | |
| 649 | if (state->getgrent_idx < end) { |
| 650 | init_group_state(state); |
| 651 | return app_id_to_group(state->getgrent_idx++ - start + AID_APP, state); |
| 652 | } |
| 653 | |
| 654 | // We are not reporting u1_a* and higher or we will be here forever |
| 655 | state->getgrent_idx = -1; |
| 656 | return NULL; |
| 657 | } |