blob: 50fcbce1763f7d5d6ac814e2fd149fe8dc05f268 [file] [log] [blame]
Mark Salyzynb38347a2016-04-05 09:09:46 -07001/*
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"
Josh Gao5e2285d2017-02-22 12:19:05 -080042#include "private/grp_pwd.h"
Mark Salyzynb38347a2016-04-05 09:09:46 -070043#include "private/ErrnoRestorer.h"
Mark Salyzynb38347a2016-04-05 09:09:46 -070044
Elliott Hughes3f6eee92016-12-13 23:47:25 +000045// Generated android_ids array
46#include "generated_android_ids.h"
47
Mark Salyzynb38347a2016-04-05 09:09:46 -070048// POSIX seems to envisage an implementation where the <pwd.h> functions are
49// implemented by brute-force searching with getpwent(3), and the <grp.h>
50// functions are implemented similarly with getgrent(3). This means that it's
51// okay for all the <grp.h> functions to share state, and all the <passwd.h>
52// functions to share state, but <grp.h> functions can't clobber <passwd.h>
53// functions' state and vice versa.
Josh Gao5e2285d2017-02-22 12:19:05 -080054#include "bionic/pthread_internal.h"
55static group_state_t* get_group_tls_buffer() {
56 return &__get_bionic_tls().group;
57}
Mark Salyzynb38347a2016-04-05 09:09:46 -070058
Josh Gao5e2285d2017-02-22 12:19:05 -080059static passwd_state_t* get_passwd_tls_buffer() {
60 return &__get_bionic_tls().passwd;
61}
Mark Salyzynb38347a2016-04-05 09:09:46 -070062
63static void init_group_state(group_state_t* state) {
Mark Salyzyn722ab052016-04-06 10:35:48 -070064 memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx));
Mark Salyzynb38347a2016-04-05 09:09:46 -070065 state->group_.gr_mem = state->group_members_;
66}
67
68static group_state_t* __group_state() {
Josh Gao5e2285d2017-02-22 12:19:05 -080069 group_state_t* result = get_group_tls_buffer();
Mark Salyzynb38347a2016-04-05 09:09:46 -070070 if (result != nullptr) {
71 init_group_state(result);
72 }
73 return result;
74}
75
76static int do_getpw_r(int by_name, const char* name, uid_t uid,
77 passwd* dst, char* buf, size_t byte_count,
78 passwd** result) {
79 // getpwnam_r and getpwuid_r don't modify errno, but library calls we
80 // make might.
81 ErrnoRestorer errno_restorer;
82 *result = NULL;
83
84 // Our implementation of getpwnam(3) and getpwuid(3) use thread-local
85 // storage, so we can call them as long as we copy everything out
86 // before returning.
87 const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above.
88
89 // POSIX allows failure to find a match to be considered a non-error.
90 // Reporting success (0) but with *result NULL is glibc's behavior.
91 if (src == NULL) {
92 return (errno == ENOENT) ? 0 : errno;
93 }
94
95 // Work out where our strings will go in 'buf', and whether we've got
96 // enough space.
97 size_t required_byte_count = 0;
98 dst->pw_name = buf;
99 required_byte_count += strlen(src->pw_name) + 1;
100 dst->pw_dir = buf + required_byte_count;
101 required_byte_count += strlen(src->pw_dir) + 1;
102 dst->pw_shell = buf + required_byte_count;
103 required_byte_count += strlen(src->pw_shell) + 1;
104 if (byte_count < required_byte_count) {
105 return ERANGE;
106 }
107
108 // Copy the strings.
109 snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell);
110
111 // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic.
112 // Note: On LP32, we define pw_gecos to pw_passwd since they're both NULL.
113 dst->pw_passwd = NULL;
114#if defined(__LP64__)
115 dst->pw_gecos = NULL;
116#endif
117
118 // Copy the integral fields.
119 dst->pw_gid = src->pw_gid;
120 dst->pw_uid = src->pw_uid;
121
122 *result = dst;
123 return 0;
124}
125
126int 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
131int 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
136static passwd* android_iinfo_to_passwd(passwd_state_t* state,
137 const android_id_info* iinfo) {
138 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name);
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_;
143 pw->pw_name = state->name_buffer_;
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
151static group* android_iinfo_to_group(group_state_t* state,
152 const android_id_info* iinfo) {
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_;
157 gr->gr_gid = iinfo->aid;
158 gr->gr_mem[0] = gr->gr_name;
159 return gr;
160}
161
162static passwd* android_id_to_passwd(passwd_state_t* state, unsigned id) {
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
171static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) {
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
180static group* android_id_to_group(group_state_t* state, unsigned id) {
181 for (size_t n = 0; n < android_id_count; ++n) {
182 if (android_ids[n].aid == id) {
183 return android_iinfo_to_group(state, android_ids + n);
184 }
185 }
186 return NULL;
187}
188
189static group* android_name_to_group(group_state_t* state, const char* name) {
190 for (size_t n = 0; n < android_id_count; ++n) {
191 if (!strcmp(android_ids[n].name, name)) {
192 return android_iinfo_to_group(state, android_ids + n);
193 }
194 }
195 return NULL;
196}
197
198// Translate a user/group name to the corresponding user/group id.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700199// all_a1234 -> 0 * AID_USER_OFFSET + AID_SHARED_GID_START + 1234 (group name only)
200// u0_a1234_cache -> 0 * AID_USER_OFFSET + AID_CACHE_GID_START + 1234 (group name only)
201// u0_a1234 -> 0 * AID_USER_OFFSET + AID_APP_START + 1234
202// u2_i1000 -> 2 * AID_USER_OFFSET + AID_ISOLATED_START + 1000
203// u1_system -> 1 * AID_USER_OFFSET + android_ids['system']
Mark Salyzynb38347a2016-04-05 09:09:46 -0700204// returns 0 and sets errno to ENOENT in case of error.
205static id_t app_id_from_name(const char* name, bool is_group) {
206 char* end;
207 unsigned long userid;
208 bool is_shared_gid = false;
209
210 if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') {
211 end = const_cast<char*>(name+3);
212 userid = 0;
213 is_shared_gid = true;
214 } else if (name[0] == 'u' && isdigit(name[1])) {
215 userid = strtoul(name+1, &end, 10);
216 } else {
217 errno = ENOENT;
218 return 0;
219 }
220
221 if (end[0] != '_' || end[1] == 0) {
222 errno = ENOENT;
223 return 0;
224 }
225
226 unsigned long appid = 0;
227 if (end[1] == 'a' && isdigit(end[2])) {
228 if (is_shared_gid) {
229 // end will point to \0 if the strtoul below succeeds.
230 appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START;
231 if (appid > AID_SHARED_GID_END) {
232 errno = ENOENT;
233 return 0;
234 }
235 } else {
236 // end will point to \0 if the strtoul below succeeds.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700237 appid = strtoul(end+2, &end, 10);
238 if (is_group && !strcmp(end, "_cache")) {
239 end += 6;
240 appid += AID_CACHE_GID_START;
241 } else {
242 appid += AID_APP_START;
243 }
Mark Salyzynb38347a2016-04-05 09:09:46 -0700244 }
245 } else if (end[1] == 'i' && isdigit(end[2])) {
246 // end will point to \0 if the strtoul below succeeds.
247 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
248 } else {
249 for (size_t n = 0; n < android_id_count; n++) {
250 if (!strcmp(android_ids[n].name, end + 1)) {
251 appid = android_ids[n].aid;
252 // Move the end pointer to the null terminator.
253 end += strlen(android_ids[n].name) + 1;
254 break;
255 }
256 }
257 }
258
259 // Check that the entire string was consumed by one of the 3 cases above.
260 if (end[0] != 0) {
261 errno = ENOENT;
262 return 0;
263 }
264
265 // Check that user id won't overflow.
266 if (userid > 1000) {
267 errno = ENOENT;
268 return 0;
269 }
270
271 // Check that app id is within range.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700272 if (appid >= AID_USER_OFFSET) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700273 errno = ENOENT;
274 return 0;
275 }
276
Jeff Sharkey934bc862016-12-13 14:03:19 -0700277 return (appid + userid*AID_USER_OFFSET);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700278}
279
280static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700281 const uid_t appid = uid % AID_USER_OFFSET;
282 const uid_t userid = uid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700283 if (appid >= AID_ISOLATED_START) {
284 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700285 } else if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700286 for (size_t n = 0; n < android_id_count; n++) {
287 if (android_ids[n].aid == appid) {
288 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
289 return;
290 }
291 }
292 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700293 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700294 }
295}
296
297static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700298 const uid_t appid = gid % AID_USER_OFFSET;
299 const uid_t userid = gid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700300 if (appid >= AID_ISOLATED_START) {
301 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
302 } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) {
303 snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700304 } else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) {
305 snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START);
306 } else if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700307 for (size_t n = 0; n < android_id_count; n++) {
308 if (android_ids[n].aid == appid) {
309 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
310 return;
311 }
312 }
313 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700314 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700315 }
316}
317
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700318// oem_XXXX -> uid
319// Supported ranges:
320// AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999)
321// AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999)
322// Check OEM id is within range.
323static bool is_oem_id(id_t id) {
324 return (((id >= AID_OEM_RESERVED_START) && (id <= AID_OEM_RESERVED_END)) ||
325 ((id >= AID_OEM_RESERVED_2_START) && (id <= AID_OEM_RESERVED_2_END)));
326}
327
Mark Salyzynb38347a2016-04-05 09:09:46 -0700328// Translate an OEM name to the corresponding user/group id.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700329static id_t oem_id_from_name(const char* name) {
330 unsigned int id;
331 if (sscanf(name, "oem_%u", &id) != 1) {
332 return 0;
333 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700334 if (!is_oem_id(id)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700335 return 0;
336 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700337 return static_cast<id_t>(id);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700338}
339
340static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700341 if (!is_oem_id(uid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700342 return NULL;
343 }
344
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700345 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700346 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
347 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
348
349 passwd* pw = &state->passwd_;
350 pw->pw_name = state->name_buffer_;
351 pw->pw_dir = state->dir_buffer_;
352 pw->pw_shell = state->sh_buffer_;
353 pw->pw_uid = uid;
354 pw->pw_gid = uid;
355 return pw;
356}
357
358static group* oem_id_to_group(gid_t gid, group_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700359 if (!is_oem_id(gid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700360 return NULL;
361 }
362
363 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_),
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700364 "oem_%u", gid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700365
366 group* gr = &state->group_;
367 gr->gr_name = state->group_name_buffer_;
368 gr->gr_gid = gid;
369 gr->gr_mem[0] = gr->gr_name;
370 return gr;
371}
372
373// Translate a uid into the corresponding name.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700374// 0 to AID_APP_START-1 -> "system", "radio", etc.
375// AID_APP_START to AID_ISOLATED_START-1 -> u0_a1234
376// AID_ISOLATED_START to AID_USER_OFFSET-1 -> u0_i1234
377// AID_USER_OFFSET+ -> u1_radio, u1_a1234, u2_i1234, etc.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700378// returns a passwd structure (sets errno to ENOENT on failure).
379static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700380 if (uid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700381 errno = ENOENT;
382 return NULL;
383 }
384
385 print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_));
386
Jeff Sharkey934bc862016-12-13 14:03:19 -0700387 const uid_t appid = uid % AID_USER_OFFSET;
388 if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700389 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
390 } else {
391 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
392 }
393
394 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
395
396 passwd* pw = &state->passwd_;
397 pw->pw_name = state->name_buffer_;
398 pw->pw_dir = state->dir_buffer_;
399 pw->pw_shell = state->sh_buffer_;
400 pw->pw_uid = uid;
401 pw->pw_gid = uid;
402 return pw;
403}
404
405// Translate a gid into the corresponding app_<gid>
406// group structure (sets errno to ENOENT on failure).
407static group* app_id_to_group(gid_t gid, group_state_t* state) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700408 if (gid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700409 errno = ENOENT;
410 return NULL;
411 }
412
413 print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_));
414
415 group* gr = &state->group_;
416 gr->gr_name = state->group_name_buffer_;
417 gr->gr_gid = gid;
418 gr->gr_mem[0] = gr->gr_name;
419 return gr;
420}
421
422passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
Josh Gao5e2285d2017-02-22 12:19:05 -0800423 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzynb38347a2016-04-05 09:09:46 -0700424 if (state == NULL) {
425 return NULL;
426 }
427
428 passwd* pw = android_id_to_passwd(state, uid);
429 if (pw != NULL) {
430 return pw;
431 }
432 // Handle OEM range.
433 pw = oem_id_to_passwd(uid, state);
434 if (pw != NULL) {
435 return pw;
436 }
437 return app_id_to_passwd(uid, state);
438}
439
440passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
Josh Gao5e2285d2017-02-22 12:19:05 -0800441 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzynb38347a2016-04-05 09:09:46 -0700442 if (state == NULL) {
443 return NULL;
444 }
445
446 passwd* pw = android_name_to_passwd(state, login);
447 if (pw != NULL) {
448 return pw;
449 }
450 // Handle OEM range.
451 pw = oem_id_to_passwd(oem_id_from_name(login), state);
452 if (pw != NULL) {
453 return pw;
454 }
455 return app_id_to_passwd(app_id_from_name(login, false), state);
456}
457
458// All users are in just one group, the one passed in.
459int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
460 if (*ngroups < 1) {
461 *ngroups = 1;
462 return -1;
463 }
464 groups[0] = group;
465 return (*ngroups = 1);
466}
467
468char* getlogin() { // NOLINT: implementing bad function.
469 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
Elliott Hughes06bd5862017-07-28 16:27:49 -0700470 return pw ? pw->pw_name : nullptr;
471}
472
473int getlogin_r(char* buf, size_t size) {
474 char* login = getlogin();
475 if (login == nullptr) return errno;
476 size_t login_length = strlen(login) + 1;
477 if (login_length > size) return ERANGE;
478 memcpy(buf, login, login_length);
479 return 0;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700480}
481
Mark Salyzyn722ab052016-04-06 10:35:48 -0700482void setpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800483 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700484 if (state) {
485 state->getpwent_idx = 0;
486 }
487}
488
489void endpwent() {
490 setpwent();
491}
492
493passwd* getpwent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800494 passwd_state_t* state = get_passwd_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700495 if (state == NULL) {
496 return NULL;
497 }
498 if (state->getpwent_idx < 0) {
499 return NULL;
500 }
501
502 size_t start = 0;
503 ssize_t end = android_id_count;
504 if (state->getpwent_idx < end) {
505 return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++);
506 }
507
508 start = end;
509 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
510
511 if (state->getpwent_idx < end) {
512 return oem_id_to_passwd(
513 state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state);
514 }
515
516 start = end;
517 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
518
519 if (state->getpwent_idx < end) {
520 return oem_id_to_passwd(
521 state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state);
522 }
523
524 start = end;
Jeff Sharkey934bc862016-12-13 14:03:19 -0700525 end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher users
Mark Salyzyn722ab052016-04-06 10:35:48 -0700526
527 if (state->getpwent_idx < end) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700528 return app_id_to_passwd(state->getpwent_idx++ - start + AID_APP_START, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700529 }
530
531 // We are not reporting u1_a* and higher or we will be here forever
532 state->getpwent_idx = -1;
533 return NULL;
534}
535
Mark Salyzynb38347a2016-04-05 09:09:46 -0700536static group* getgrgid_internal(gid_t gid, group_state_t* state) {
537 group* grp = android_id_to_group(state, gid);
538 if (grp != NULL) {
539 return grp;
540 }
541 // Handle OEM range.
542 grp = oem_id_to_group(gid, state);
543 if (grp != NULL) {
544 return grp;
545 }
546 return app_id_to_group(gid, state);
547}
548
549group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
550 group_state_t* state = __group_state();
551 if (state == NULL) {
552 return NULL;
553 }
554 return getgrgid_internal(gid, state);
555}
556
557static group* getgrnam_internal(const char* name, group_state_t* state) {
558 group* grp = android_name_to_group(state, name);
559 if (grp != NULL) {
560 return grp;
561 }
562 // Handle OEM range.
563 grp = oem_id_to_group(oem_id_from_name(name), state);
564 if (grp != NULL) {
565 return grp;
566 }
567 return app_id_to_group(app_id_from_name(name, true), state);
568}
569
570group* getgrnam(const char* name) { // NOLINT: implementing bad function.
571 group_state_t* state = __group_state();
572 if (state == NULL) {
573 return NULL;
574 }
575 return getgrnam_internal(name, state);
576}
577
578static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf,
579 size_t buflen, struct group** result) {
580 ErrnoRestorer errno_restorer;
581 *result = NULL;
582 char* p = reinterpret_cast<char*>(
Dan Alberta613d0d2017-10-05 16:39:33 -0700583 __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t)));
Mark Salyzynb38347a2016-04-05 09:09:46 -0700584 if (p + sizeof(group_state_t) > buf + buflen) {
585 return ERANGE;
586 }
587 group_state_t* state = reinterpret_cast<group_state_t*>(p);
588 init_group_state(state);
589 group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state));
590 if (retval != NULL) {
591 *grp = *retval;
592 *result = grp;
593 return 0;
594 }
595 return errno;
596}
597
598int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) {
599 return getgroup_r(false, NULL, gid, grp, buf, buflen, result);
600}
601
602int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen,
603 struct group **result) {
604 return getgroup_r(true, name, 0, grp, buf, buflen, result);
605}
Mark Salyzyn722ab052016-04-06 10:35:48 -0700606
607void setgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800608 group_state_t* state = get_group_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700609 if (state) {
610 state->getgrent_idx = 0;
611 }
612}
613
614void endgrent() {
615 setgrent();
616}
617
618group* getgrent() {
Josh Gao5e2285d2017-02-22 12:19:05 -0800619 group_state_t* state = get_group_tls_buffer();
Mark Salyzyn722ab052016-04-06 10:35:48 -0700620 if (state == NULL) {
621 return NULL;
622 }
623 if (state->getgrent_idx < 0) {
624 return NULL;
625 }
626
627 size_t start = 0;
628 ssize_t end = android_id_count;
629 if (state->getgrent_idx < end) {
630 init_group_state(state);
631 return android_iinfo_to_group(state, android_ids + state->getgrent_idx++);
632 }
633
634 start = end;
635 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
636
637 if (state->getgrent_idx < end) {
638 init_group_state(state);
639 return oem_id_to_group(
640 state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state);
641 }
642
643 start = end;
644 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
645
646 if (state->getgrent_idx < end) {
647 init_group_state(state);
648 return oem_id_to_group(
649 state->getgrent_idx++ - start + AID_OEM_RESERVED_2_START, state);
650 }
651
652 start = end;
Jeff Sharkey934bc862016-12-13 14:03:19 -0700653 end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher groups
Mark Salyzyn722ab052016-04-06 10:35:48 -0700654
655 if (state->getgrent_idx < end) {
656 init_group_state(state);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700657 return app_id_to_group(state->getgrent_idx++ - start + AID_APP_START, state);
Mark Salyzyn722ab052016-04-06 10:35:48 -0700658 }
659
660 // We are not reporting u1_a* and higher or we will be here forever
661 state->getgrent_idx = -1;
662 return NULL;
663}