blob: db0f7778d160ca6548acf0a549ba8d755a07401d [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"
42#include "private/ErrnoRestorer.h"
43#include "private/libc_logging.h"
44#include "private/ThreadLocalBuffer.h"
45
46// POSIX seems to envisage an implementation where the <pwd.h> functions are
47// implemented by brute-force searching with getpwent(3), and the <grp.h>
48// functions are implemented similarly with getgrent(3). This means that it's
49// okay for all the <grp.h> functions to share state, and all the <passwd.h>
50// functions to share state, but <grp.h> functions can't clobber <passwd.h>
51// functions' state and vice versa.
52
53struct group_state_t {
54 group group_;
55 char* group_members_[2];
56 char group_name_buffer_[32];
Mark Salyzyn722ab052016-04-06 10:35:48 -070057 // Must be last so init_group_state can run a simple memset for the above
58 ssize_t getgrent_idx;
Mark Salyzynb38347a2016-04-05 09:09:46 -070059};
60
61struct passwd_state_t {
62 passwd passwd_;
63 char name_buffer_[32];
64 char dir_buffer_[32];
65 char sh_buffer_[32];
Mark Salyzyn722ab052016-04-06 10:35:48 -070066 ssize_t getpwent_idx;
Mark Salyzynb38347a2016-04-05 09:09:46 -070067};
68
69static ThreadLocalBuffer<group_state_t> g_group_tls_buffer;
70static ThreadLocalBuffer<passwd_state_t> g_passwd_tls_buffer;
71
72static void init_group_state(group_state_t* state) {
Mark Salyzyn722ab052016-04-06 10:35:48 -070073 memset(state, 0, sizeof(group_state_t) - sizeof(state->getgrent_idx));
Mark Salyzynb38347a2016-04-05 09:09:46 -070074 state->group_.gr_mem = state->group_members_;
75}
76
77static group_state_t* __group_state() {
78 group_state_t* result = g_group_tls_buffer.get();
79 if (result != nullptr) {
80 init_group_state(result);
81 }
82 return result;
83}
84
85static int do_getpw_r(int by_name, const char* name, uid_t uid,
86 passwd* dst, char* buf, size_t byte_count,
87 passwd** result) {
88 // getpwnam_r and getpwuid_r don't modify errno, but library calls we
89 // make might.
90 ErrnoRestorer errno_restorer;
91 *result = NULL;
92
93 // Our implementation of getpwnam(3) and getpwuid(3) use thread-local
94 // storage, so we can call them as long as we copy everything out
95 // before returning.
96 const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above.
97
98 // POSIX allows failure to find a match to be considered a non-error.
99 // Reporting success (0) but with *result NULL is glibc's behavior.
100 if (src == NULL) {
101 return (errno == ENOENT) ? 0 : errno;
102 }
103
104 // Work out where our strings will go in 'buf', and whether we've got
105 // enough space.
106 size_t required_byte_count = 0;
107 dst->pw_name = buf;
108 required_byte_count += strlen(src->pw_name) + 1;
109 dst->pw_dir = buf + required_byte_count;
110 required_byte_count += strlen(src->pw_dir) + 1;
111 dst->pw_shell = buf + required_byte_count;
112 required_byte_count += strlen(src->pw_shell) + 1;
113 if (byte_count < required_byte_count) {
114 return ERANGE;
115 }
116
117 // Copy the strings.
118 snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell);
119
120 // pw_passwd and pw_gecos are non-POSIX and unused (always NULL) in bionic.
121 // Note: On LP32, we define pw_gecos to pw_passwd since they're both NULL.
122 dst->pw_passwd = NULL;
123#if defined(__LP64__)
124 dst->pw_gecos = NULL;
125#endif
126
127 // Copy the integral fields.
128 dst->pw_gid = src->pw_gid;
129 dst->pw_uid = src->pw_uid;
130
131 *result = dst;
132 return 0;
133}
134
135int getpwnam_r(const char* name, passwd* pwd,
136 char* buf, size_t byte_count, passwd** result) {
137 return do_getpw_r(1, name, -1, pwd, buf, byte_count, result);
138}
139
140int getpwuid_r(uid_t uid, passwd* pwd,
141 char* buf, size_t byte_count, passwd** result) {
142 return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result);
143}
144
145static passwd* android_iinfo_to_passwd(passwd_state_t* state,
146 const android_id_info* iinfo) {
147 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "%s", iinfo->name);
148 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
149 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
150
151 passwd* pw = &state->passwd_;
152 pw->pw_name = state->name_buffer_;
153 pw->pw_uid = iinfo->aid;
154 pw->pw_gid = iinfo->aid;
155 pw->pw_dir = state->dir_buffer_;
156 pw->pw_shell = state->sh_buffer_;
157 return pw;
158}
159
160static group* android_iinfo_to_group(group_state_t* state,
161 const android_id_info* iinfo) {
162 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_), "%s", iinfo->name);
163
164 group* gr = &state->group_;
165 gr->gr_name = state->group_name_buffer_;
166 gr->gr_gid = iinfo->aid;
167 gr->gr_mem[0] = gr->gr_name;
168 return gr;
169}
170
171static passwd* android_id_to_passwd(passwd_state_t* state, unsigned id) {
172 for (size_t n = 0; n < android_id_count; ++n) {
173 if (android_ids[n].aid == id) {
174 return android_iinfo_to_passwd(state, android_ids + n);
175 }
176 }
177 return NULL;
178}
179
180static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) {
181 for (size_t n = 0; n < android_id_count; ++n) {
182 if (!strcmp(android_ids[n].name, name)) {
183 return android_iinfo_to_passwd(state, android_ids + n);
184 }
185 }
186 return NULL;
187}
188
189static group* android_id_to_group(group_state_t* state, unsigned id) {
190 for (size_t n = 0; n < android_id_count; ++n) {
191 if (android_ids[n].aid == id) {
192 return android_iinfo_to_group(state, android_ids + n);
193 }
194 }
195 return NULL;
196}
197
198static group* android_name_to_group(group_state_t* state, const char* name) {
199 for (size_t n = 0; n < android_id_count; ++n) {
200 if (!strcmp(android_ids[n].name, name)) {
201 return android_iinfo_to_group(state, android_ids + n);
202 }
203 }
204 return NULL;
205}
206
207// Translate a user/group name to the corresponding user/group id.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700208// all_a1234 -> 0 * AID_USER_OFFSET + AID_SHARED_GID_START + 1234 (group name only)
209// u0_a1234_cache -> 0 * AID_USER_OFFSET + AID_CACHE_GID_START + 1234 (group name only)
210// u0_a1234 -> 0 * AID_USER_OFFSET + AID_APP_START + 1234
211// u2_i1000 -> 2 * AID_USER_OFFSET + AID_ISOLATED_START + 1000
212// u1_system -> 1 * AID_USER_OFFSET + android_ids['system']
Mark Salyzynb38347a2016-04-05 09:09:46 -0700213// returns 0 and sets errno to ENOENT in case of error.
214static id_t app_id_from_name(const char* name, bool is_group) {
215 char* end;
216 unsigned long userid;
217 bool is_shared_gid = false;
218
219 if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') {
220 end = const_cast<char*>(name+3);
221 userid = 0;
222 is_shared_gid = true;
223 } else if (name[0] == 'u' && isdigit(name[1])) {
224 userid = strtoul(name+1, &end, 10);
225 } else {
226 errno = ENOENT;
227 return 0;
228 }
229
230 if (end[0] != '_' || end[1] == 0) {
231 errno = ENOENT;
232 return 0;
233 }
234
235 unsigned long appid = 0;
236 if (end[1] == 'a' && isdigit(end[2])) {
237 if (is_shared_gid) {
238 // end will point to \0 if the strtoul below succeeds.
239 appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START;
240 if (appid > AID_SHARED_GID_END) {
241 errno = ENOENT;
242 return 0;
243 }
244 } else {
245 // end will point to \0 if the strtoul below succeeds.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700246 appid = strtoul(end+2, &end, 10);
247 if (is_group && !strcmp(end, "_cache")) {
248 end += 6;
249 appid += AID_CACHE_GID_START;
250 } else {
251 appid += AID_APP_START;
252 }
Mark Salyzynb38347a2016-04-05 09:09:46 -0700253 }
254 } else if (end[1] == 'i' && isdigit(end[2])) {
255 // end will point to \0 if the strtoul below succeeds.
256 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
257 } else {
258 for (size_t n = 0; n < android_id_count; n++) {
259 if (!strcmp(android_ids[n].name, end + 1)) {
260 appid = android_ids[n].aid;
261 // Move the end pointer to the null terminator.
262 end += strlen(android_ids[n].name) + 1;
263 break;
264 }
265 }
266 }
267
268 // Check that the entire string was consumed by one of the 3 cases above.
269 if (end[0] != 0) {
270 errno = ENOENT;
271 return 0;
272 }
273
274 // Check that user id won't overflow.
275 if (userid > 1000) {
276 errno = ENOENT;
277 return 0;
278 }
279
280 // Check that app id is within range.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700281 if (appid >= AID_USER_OFFSET) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700282 errno = ENOENT;
283 return 0;
284 }
285
Jeff Sharkey934bc862016-12-13 14:03:19 -0700286 return (appid + userid*AID_USER_OFFSET);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700287}
288
289static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700290 const uid_t appid = uid % AID_USER_OFFSET;
291 const uid_t userid = uid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700292 if (appid >= AID_ISOLATED_START) {
293 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700294 } else if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700295 for (size_t n = 0; n < android_id_count; n++) {
296 if (android_ids[n].aid == appid) {
297 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
298 return;
299 }
300 }
301 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700302 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700303 }
304}
305
306static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700307 const uid_t appid = gid % AID_USER_OFFSET;
308 const uid_t userid = gid / AID_USER_OFFSET;
Mark Salyzynb38347a2016-04-05 09:09:46 -0700309 if (appid >= AID_ISOLATED_START) {
310 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
311 } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) {
312 snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
Jeff Sharkey934bc862016-12-13 14:03:19 -0700313 } else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) {
314 snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START);
315 } else if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700316 for (size_t n = 0; n < android_id_count; n++) {
317 if (android_ids[n].aid == appid) {
318 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
319 return;
320 }
321 }
322 } else {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700323 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700324 }
325}
326
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700327// oem_XXXX -> uid
328// Supported ranges:
329// AID_OEM_RESERVED_START to AID_OEM_RESERVED_END (2900-2999)
330// AID_OEM_RESERVED_2_START to AID_OEM_RESERVED_2_END (5000-5999)
331// Check OEM id is within range.
332static bool is_oem_id(id_t id) {
333 return (((id >= AID_OEM_RESERVED_START) && (id <= AID_OEM_RESERVED_END)) ||
334 ((id >= AID_OEM_RESERVED_2_START) && (id <= AID_OEM_RESERVED_2_END)));
335}
336
Mark Salyzynb38347a2016-04-05 09:09:46 -0700337// Translate an OEM name to the corresponding user/group id.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700338static id_t oem_id_from_name(const char* name) {
339 unsigned int id;
340 if (sscanf(name, "oem_%u", &id) != 1) {
341 return 0;
342 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700343 if (!is_oem_id(id)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700344 return 0;
345 }
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700346 return static_cast<id_t>(id);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700347}
348
349static passwd* oem_id_to_passwd(uid_t uid, passwd_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700350 if (!is_oem_id(uid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700351 return NULL;
352 }
353
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700354 snprintf(state->name_buffer_, sizeof(state->name_buffer_), "oem_%u", uid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700355 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
356 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
357
358 passwd* pw = &state->passwd_;
359 pw->pw_name = state->name_buffer_;
360 pw->pw_dir = state->dir_buffer_;
361 pw->pw_shell = state->sh_buffer_;
362 pw->pw_uid = uid;
363 pw->pw_gid = uid;
364 return pw;
365}
366
367static group* oem_id_to_group(gid_t gid, group_state_t* state) {
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700368 if (!is_oem_id(gid)) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700369 return NULL;
370 }
371
372 snprintf(state->group_name_buffer_, sizeof(state->group_name_buffer_),
Mark Salyzyn8d387ee2016-04-05 09:24:59 -0700373 "oem_%u", gid);
Mark Salyzynb38347a2016-04-05 09:09:46 -0700374
375 group* gr = &state->group_;
376 gr->gr_name = state->group_name_buffer_;
377 gr->gr_gid = gid;
378 gr->gr_mem[0] = gr->gr_name;
379 return gr;
380}
381
382// Translate a uid into the corresponding name.
Jeff Sharkey934bc862016-12-13 14:03:19 -0700383// 0 to AID_APP_START-1 -> "system", "radio", etc.
384// AID_APP_START to AID_ISOLATED_START-1 -> u0_a1234
385// AID_ISOLATED_START to AID_USER_OFFSET-1 -> u0_i1234
386// AID_USER_OFFSET+ -> u1_radio, u1_a1234, u2_i1234, etc.
Mark Salyzynb38347a2016-04-05 09:09:46 -0700387// returns a passwd structure (sets errno to ENOENT on failure).
388static passwd* app_id_to_passwd(uid_t uid, passwd_state_t* state) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700389 if (uid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700390 errno = ENOENT;
391 return NULL;
392 }
393
394 print_app_name_from_uid(uid, state->name_buffer_, sizeof(state->name_buffer_));
395
Jeff Sharkey934bc862016-12-13 14:03:19 -0700396 const uid_t appid = uid % AID_USER_OFFSET;
397 if (appid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700398 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
399 } else {
400 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
401 }
402
403 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
404
405 passwd* pw = &state->passwd_;
406 pw->pw_name = state->name_buffer_;
407 pw->pw_dir = state->dir_buffer_;
408 pw->pw_shell = state->sh_buffer_;
409 pw->pw_uid = uid;
410 pw->pw_gid = uid;
411 return pw;
412}
413
414// Translate a gid into the corresponding app_<gid>
415// group structure (sets errno to ENOENT on failure).
416static group* app_id_to_group(gid_t gid, group_state_t* state) {
Jeff Sharkey934bc862016-12-13 14:03:19 -0700417 if (gid < AID_APP_START) {
Mark Salyzynb38347a2016-04-05 09:09:46 -0700418 errno = ENOENT;
419 return NULL;
420 }
421
422 print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_));
423
424 group* gr = &state->group_;
425 gr->gr_name = state->group_name_buffer_;
426 gr->gr_gid = gid;
427 gr->gr_mem[0] = gr->gr_name;
428 return gr;
429}
430
431passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
432 passwd_state_t* state = g_passwd_tls_buffer.get();
433 if (state == NULL) {
434 return NULL;
435 }
436
437 passwd* pw = android_id_to_passwd(state, uid);
438 if (pw != NULL) {
439 return pw;
440 }
441 // Handle OEM range.
442 pw = oem_id_to_passwd(uid, state);
443 if (pw != NULL) {
444 return pw;
445 }
446 return app_id_to_passwd(uid, state);
447}
448
449passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
450 passwd_state_t* state = g_passwd_tls_buffer.get();
451 if (state == NULL) {
452 return NULL;
453 }
454
455 passwd* pw = android_name_to_passwd(state, login);
456 if (pw != NULL) {
457 return pw;
458 }
459 // Handle OEM range.
460 pw = oem_id_to_passwd(oem_id_from_name(login), state);
461 if (pw != NULL) {
462 return pw;
463 }
464 return app_id_to_passwd(app_id_from_name(login, false), state);
465}
466
467// All users are in just one group, the one passed in.
468int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
469 if (*ngroups < 1) {
470 *ngroups = 1;
471 return -1;
472 }
473 groups[0] = group;
474 return (*ngroups = 1);
475}
476
477char* getlogin() { // NOLINT: implementing bad function.
478 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
479 return (pw != NULL) ? pw->pw_name : NULL;
480}
481
Mark Salyzyn722ab052016-04-06 10:35:48 -0700482void setpwent() {
483 passwd_state_t* state = g_passwd_tls_buffer.get();
484 if (state) {
485 state->getpwent_idx = 0;
486 }
487}
488
489void endpwent() {
490 setpwent();
491}
492
493passwd* getpwent() {
494 passwd_state_t* state = g_passwd_tls_buffer.get();
495 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*>(
583 BIONIC_ALIGN(reinterpret_cast<uintptr_t>(buf), sizeof(uintptr_t)));
584 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() {
608 group_state_t* state = g_group_tls_buffer.get();
609 if (state) {
610 state->getgrent_idx = 0;
611 }
612}
613
614void endgrent() {
615 setgrent();
616}
617
618group* getgrent() {
619 group_state_t* state = g_group_tls_buffer.get();
620 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}