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/grp_pwd.h"
43 #include "private/ErrnoRestorer.h"
44
45 // Generated android_ids array
46 #include "generated_android_ids.h"
47
48 // 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.
54 #include "bionic/pthread_internal.h"
get_group_tls_buffer()55 static group_state_t* get_group_tls_buffer() {
56 return &__get_bionic_tls().group;
57 }
58
get_passwd_tls_buffer()59 static passwd_state_t* get_passwd_tls_buffer() {
60 return &__get_bionic_tls().passwd;
61 }
62
init_group_state(group_state_t * state)63 static void init_group_state(group_state_t* state) {
64 memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx));
65 state->group_.gr_mem = state->group_members_;
66 }
67
__group_state()68 static group_state_t* __group_state() {
69 group_state_t* result = get_group_tls_buffer();
70 if (result != nullptr) {
71 init_group_state(result);
72 }
73 return result;
74 }
75
do_getpw_r(int by_name,const char * name,uid_t uid,passwd * dst,char * buf,size_t byte_count,passwd ** result)76 static 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
getpwnam_r(const char * name,passwd * pwd,char * buf,size_t byte_count,passwd ** result)126 int getpwnam_r(const char* name, passwd* pwd,
127 char* buf, size_t byte_count, passwd** result) {
128 return do_getpw_r(1, name, -1, pwd, buf, byte_count, result);
129 }
130
getpwuid_r(uid_t uid,passwd * pwd,char * buf,size_t byte_count,passwd ** result)131 int getpwuid_r(uid_t uid, passwd* pwd,
132 char* buf, size_t byte_count, passwd** result) {
133 return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result);
134 }
135
android_iinfo_to_passwd(passwd_state_t * state,const android_id_info * iinfo)136 static 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
android_iinfo_to_group(group_state_t * state,const android_id_info * iinfo)151 static 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
android_id_to_passwd(passwd_state_t * state,unsigned id)162 static 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
android_name_to_passwd(passwd_state_t * state,const char * name)171 static 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
android_id_to_group(group_state_t * state,unsigned id)180 static 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
android_name_to_group(group_state_t * state,const char * name)189 static 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.
199 // 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']
204 // returns 0 and sets errno to ENOENT in case of error.
app_id_from_name(const char * name,bool is_group)205 static 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.
237 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 }
244 }
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.
272 if (appid >= AID_USER_OFFSET) {
273 errno = ENOENT;
274 return 0;
275 }
276
277 return (appid + userid*AID_USER_OFFSET);
278 }
279
print_app_name_from_uid(const uid_t uid,char * buffer,const int bufferlen)280 static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
281 const uid_t appid = uid % AID_USER_OFFSET;
282 const uid_t userid = uid / AID_USER_OFFSET;
283 if (appid >= AID_ISOLATED_START) {
284 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
285 } else if (appid < AID_APP_START) {
286 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 {
293 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
294 }
295 }
296
print_app_name_from_gid(const gid_t gid,char * buffer,const int bufferlen)297 static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
298 const uid_t appid = gid % AID_USER_OFFSET;
299 const uid_t userid = gid / AID_USER_OFFSET;
300 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);
304 } 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) {
307 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 {
314 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
315 }
316 }
317
318 // 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.
is_oem_id(id_t id)323 static 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
328 // Translate an OEM name to the corresponding user/group id.
oem_id_from_name(const char * name)329 static 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 }
334 if (!is_oem_id(id)) {
335 return 0;
336 }
337 return static_cast<id_t>(id);
338 }
339
oem_id_to_passwd(uid_t uid,passwd_state_t * state)340 static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
341 if (!is_oem_id(uid)) {
342 return NULL;
343 }
344
345 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid);
346 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
oem_id_to_group(gid_t gid,group_state_t * state)358 static group* oem_id_to_group(gid_t gid, group_state_t* state) {
359 if (!is_oem_id(gid)) {
360 return NULL;
361 }
362
363 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_),
364 "oem_%u", gid);
365
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.
374 // 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.
378 // returns a passwd structure (sets errno to ENOENT on failure).
app_id_to_passwd(uid_t uid,passwd_state_t * state)379 static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) {
380 if (uid < AID_APP_START) {
381 errno = ENOENT;
382 return NULL;
383 }
384
385 print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_));
386
387 const uid_t appid = uid % AID_USER_OFFSET;
388 if (appid < AID_APP_START) {
389 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).
app_id_to_group(gid_t gid,group_state_t * state)407 static group* app_id_to_group(gid_t gid, group_state_t* state) {
408 if (gid < AID_APP_START) {
409 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
getpwuid(uid_t uid)422 passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
423 passwd_state_t* state = get_passwd_tls_buffer();
424 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
getpwnam(const char * login)440 passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
441 passwd_state_t* state = get_passwd_tls_buffer();
442 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.
getgrouplist(const char *,gid_t group,gid_t * groups,int * ngroups)459 int 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
getlogin()468 char* getlogin() { // NOLINT: implementing bad function.
469 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
470 return (pw != NULL) ? pw->pw_name : NULL;
471 }
472
setpwent()473 void setpwent() {
474 passwd_state_t* state = get_passwd_tls_buffer();
475 if (state) {
476 state->getpwent_idx = 0;
477 }
478 }
479
endpwent()480 void endpwent() {
481 setpwent();
482 }
483
getpwent()484 passwd* getpwent() {
485 passwd_state_t* state = get_passwd_tls_buffer();
486 if (state == NULL) {
487 return NULL;
488 }
489 if (state->getpwent_idx < 0) {
490 return NULL;
491 }
492
493 size_t start = 0;
494 ssize_t end = android_id_count;
495 if (state->getpwent_idx < end) {
496 return android_iinfo_to_passwd(state, android_ids + state->getpwent_idx++);
497 }
498
499 start = end;
500 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
501
502 if (state->getpwent_idx < end) {
503 return oem_id_to_passwd(
504 state->getpwent_idx++ - start + AID_OEM_RESERVED_START, state);
505 }
506
507 start = end;
508 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_START + 1;
509
510 if (state->getpwent_idx < end) {
511 return oem_id_to_passwd(
512 state->getpwent_idx++ - start + AID_OEM_RESERVED_2_START, state);
513 }
514
515 start = end;
516 end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher users
517
518 if (state->getpwent_idx < end) {
519 return app_id_to_passwd(state->getpwent_idx++ - start + AID_APP_START, state);
520 }
521
522 // We are not reporting u1_a* and higher or we will be here forever
523 state->getpwent_idx = -1;
524 return NULL;
525 }
526
getgrgid_internal(gid_t gid,group_state_t * state)527 static group* getgrgid_internal(gid_t gid, group_state_t* state) {
528 group* grp = android_id_to_group(state, gid);
529 if (grp != NULL) {
530 return grp;
531 }
532 // Handle OEM range.
533 grp = oem_id_to_group(gid, state);
534 if (grp != NULL) {
535 return grp;
536 }
537 return app_id_to_group(gid, state);
538 }
539
getgrgid(gid_t gid)540 group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
541 group_state_t* state = __group_state();
542 if (state == NULL) {
543 return NULL;
544 }
545 return getgrgid_internal(gid, state);
546 }
547
getgrnam_internal(const char * name,group_state_t * state)548 static group* getgrnam_internal(const char* name, group_state_t* state) {
549 group* grp = android_name_to_group(state, name);
550 if (grp != NULL) {
551 return grp;
552 }
553 // Handle OEM range.
554 grp = oem_id_to_group(oem_id_from_name(name), state);
555 if (grp != NULL) {
556 return grp;
557 }
558 return app_id_to_group(app_id_from_name(name, true), state);
559 }
560
getgrnam(const char * name)561 group* getgrnam(const char* name) { // NOLINT: implementing bad function.
562 group_state_t* state = __group_state();
563 if (state == NULL) {
564 return NULL;
565 }
566 return getgrnam_internal(name, state);
567 }
568
getgroup_r(bool by_name,const char * name,gid_t gid,struct group * grp,char * buf,size_t buflen,struct group ** result)569 static int getgroup_r(bool by_name, const char* name, gid_t gid, struct group* grp, char* buf,
570 size_t buflen, struct group** result) {
571 ErrnoRestorer errno_restorer;
572 *result = NULL;
573 char* p = reinterpret_cast<char*>(
574 BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t)));
575 if (p + sizeof(group_state_t) > buf + buflen) {
576 return ERANGE;
577 }
578 group_state_t* state = reinterpret_cast<group_state_t*>(p);
579 init_group_state(state);
580 group* retval = (by_name ? getgrnam_internal(name, state) : getgrgid_internal(gid, state));
581 if (retval != NULL) {
582 *grp = *retval;
583 *result = grp;
584 return 0;
585 }
586 return errno;
587 }
588
getgrgid_r(gid_t gid,struct group * grp,char * buf,size_t buflen,struct group ** result)589 int getgrgid_r(gid_t gid, struct group* grp, char* buf, size_t buflen, struct group** result) {
590 return getgroup_r(false, NULL, gid, grp, buf, buflen, result);
591 }
592
getgrnam_r(const char * name,struct group * grp,char * buf,size_t buflen,struct group ** result)593 int getgrnam_r(const char* name, struct group* grp, char* buf, size_t buflen,
594 struct group **result) {
595 return getgroup_r(true, name, 0, grp, buf, buflen, result);
596 }
597
setgrent()598 void setgrent() {
599 group_state_t* state = get_group_tls_buffer();
600 if (state) {
601 state->getgrent_idx = 0;
602 }
603 }
604
endgrent()605 void endgrent() {
606 setgrent();
607 }
608
getgrent()609 group* getgrent() {
610 group_state_t* state = get_group_tls_buffer();
611 if (state == NULL) {
612 return NULL;
613 }
614 if (state->getgrent_idx < 0) {
615 return NULL;
616 }
617
618 size_t start = 0;
619 ssize_t end = android_id_count;
620 if (state->getgrent_idx < end) {
621 init_group_state(state);
622 return android_iinfo_to_group(state, android_ids + state->getgrent_idx++);
623 }
624
625 start = end;
626 end += AID_OEM_RESERVED_END - AID_OEM_RESERVED_START + 1;
627
628 if (state->getgrent_idx < end) {
629 init_group_state(state);
630 return oem_id_to_group(
631 state->getgrent_idx++ - start + AID_OEM_RESERVED_START, state);
632 }
633
634 start = end;
635 end += AID_OEM_RESERVED_2_END - AID_OEM_RESERVED_2_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_2_START, state);
641 }
642
643 start = end;
644 end += AID_USER_OFFSET - AID_APP_START; // Do not expose higher groups
645
646 if (state->getgrent_idx < end) {
647 init_group_state(state);
648 return app_id_to_group(state->getgrent_idx++ - start + AID_APP_START, state);
649 }
650
651 // We are not reporting u1_a* and higher or we will be here forever
652 state->getgrent_idx = -1;
653 return NULL;
654 }
655