blob: 3f24d1bd5f06880d1d30d4106ce10cdc6f222d59 [file] [log] [blame]
Elliott Hughesde727ca2012-08-13 15:45:36 -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 <netdb.h>
34#include <private/android_filesystem_config.h>
Elliott Hughes1e980b62013-01-17 18:36:06 -080035#include <private/debug_format.h>
Elliott Hughesde727ca2012-08-13 15:45:36 -070036#include <private/logd.h>
37#include <pthread.h>
38#include <pwd.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <unistd.h>
42
43// Thread-specific state for the non-reentrant functions.
44static pthread_once_t stubs_once = PTHREAD_ONCE_INIT;
45static pthread_key_t stubs_key;
46struct stubs_state_t {
47 passwd passwd_;
48 group group_;
49 char* group_members_[2];
50 char app_name_buffer_[32];
51 char group_name_buffer_[32];
52 char dir_buffer_[32];
53 char sh_buffer_[32];
54};
55
56static int do_getpw_r(int by_name, const char* name, uid_t uid,
57 passwd* dst, char* buf, size_t byte_count,
58 passwd** result) {
59 // getpwnam_r and getpwuid_r don't modify errno, but library calls we
60 // make might.
61 int old_errno = errno;
62 *result = NULL;
63
64 // Our implementation of getpwnam(3) and getpwuid(3) use thread-local
65 // storage, so we can call them as long as we copy everything out
66 // before returning.
67 const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above.
68
69 // POSIX allows failure to find a match to be considered a non-error.
70 // Reporting success (0) but with *result NULL is glibc's behavior.
71 if (src == NULL) {
72 int rc = (errno == ENOENT) ? 0 : errno;
73 errno = old_errno;
74 return rc;
75 }
76
77 // Work out where our strings will go in 'buf', and whether we've got
78 // enough space.
79 size_t required_byte_count = 0;
80 dst->pw_name = buf;
81 required_byte_count += strlen(src->pw_name) + 1;
82 dst->pw_dir = buf + required_byte_count;
83 required_byte_count += strlen(src->pw_dir) + 1;
84 dst->pw_shell = buf + required_byte_count;
85 required_byte_count += strlen(src->pw_shell) + 1;
86 if (byte_count < required_byte_count) {
87 errno = old_errno;
88 return ERANGE;
89 }
90
91 // Copy the strings.
92 snprintf(buf, byte_count, "%s%c%s%c%s",
93 src->pw_name, 0, src->pw_dir, 0, src->pw_shell);
94
95 // pw_passwd is non-POSIX and unused (always NULL) in bionic.
96 // pw_gecos is non-POSIX and missing in bionic.
97 dst->pw_passwd = NULL;
98
99 // Copy the integral fields.
100 dst->pw_gid = src->pw_gid;
101 dst->pw_uid = src->pw_uid;
102
103 *result = dst;
104 errno = old_errno;
105 return 0;
106}
107
108int getpwnam_r(const char* name, passwd* pwd,
109 char* buf, size_t byte_count, passwd** result) {
110 return do_getpw_r(1, name, -1, pwd, buf, byte_count, result);
111}
112
113int getpwuid_r(uid_t uid, passwd* pwd,
114 char* buf, size_t byte_count, passwd** result) {
115 return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result);
116}
117
118static stubs_state_t* stubs_state_alloc() {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700119 stubs_state_t* s = static_cast<stubs_state_t*>(calloc(1, sizeof(*s)));
Elliott Hughesde727ca2012-08-13 15:45:36 -0700120 if (s != NULL) {
121 s->group_.gr_mem = s->group_members_;
122 }
123 return s;
124}
125
126static void stubs_state_free(void* ptr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700127 stubs_state_t* state = static_cast<stubs_state_t*>(ptr);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700128 free(state);
129}
130
131static void __stubs_key_init() {
132 pthread_key_create(&stubs_key, stubs_state_free);
133}
134
135static stubs_state_t* __stubs_state() {
136 pthread_once(&stubs_once, __stubs_key_init);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700137 stubs_state_t* s = static_cast<stubs_state_t*>(pthread_getspecific(stubs_key));
Elliott Hughesde727ca2012-08-13 15:45:36 -0700138 if (s == NULL) {
139 s = stubs_state_alloc();
140 if (s == NULL) {
141 errno = ENOMEM; // Just in case.
142 } else {
143 if (pthread_setspecific(stubs_key, s) != 0) {
144 stubs_state_free(s);
145 errno = ENOMEM;
146 s = NULL;
147 }
148 }
149 }
150 return s;
151}
152
153static passwd* android_iinfo_to_passwd(stubs_state_t* state,
154 const android_id_info* iinfo) {
155 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
156 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
157
158 passwd* pw = &state->passwd_;
159 pw->pw_name = (char*) iinfo->name;
160 pw->pw_uid = iinfo->aid;
161 pw->pw_gid = iinfo->aid;
162 pw->pw_dir = state->dir_buffer_;
163 pw->pw_shell = state->sh_buffer_;
164 return pw;
165}
166
167static group* android_iinfo_to_group(group* gr,
168 const android_id_info* iinfo) {
169 gr->gr_name = (char*) iinfo->name;
170 gr->gr_gid = iinfo->aid;
171 gr->gr_mem[0] = gr->gr_name;
172 gr->gr_mem[1] = NULL;
173 return gr;
174}
175
176static passwd* android_id_to_passwd(stubs_state_t* state, unsigned id) {
177 for (size_t n = 0; n < android_id_count; ++n) {
178 if (android_ids[n].aid == id) {
179 return android_iinfo_to_passwd(state, android_ids + n);
180 }
181 }
182 return NULL;
183}
184
185static passwd* android_name_to_passwd(stubs_state_t* state, const char* name) {
186 for (size_t n = 0; n < android_id_count; ++n) {
187 if (!strcmp(android_ids[n].name, name)) {
188 return android_iinfo_to_passwd(state, android_ids + n);
189 }
190 }
191 return NULL;
192}
193
194static group* android_id_to_group(group* gr, unsigned id) {
195 for (size_t n = 0; n < android_id_count; ++n) {
196 if (android_ids[n].aid == id) {
197 return android_iinfo_to_group(gr, android_ids + n);
198 }
199 }
200 return NULL;
201}
202
203static group* android_name_to_group(group* gr, const char* name) {
204 for (size_t n = 0; n < android_id_count; ++n) {
205 if (!strcmp(android_ids[n].name, name)) {
206 return android_iinfo_to_group(gr, android_ids + n);
207 }
208 }
209 return NULL;
210}
211
212// Translate a user/group name to the corresponding user/group id.
213// u0_a1234 -> 0 * AID_USER + AID_APP + 1234
214// u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000
215// u1_system -> 1 * AID_USER + android_ids['system']
216// returns 0 and sets errno to ENOENT in case of error
217static unsigned app_id_from_name(const char* name) {
218 if (name[0] != 'u' || !isdigit(name[1])) {
219 errno = ENOENT;
220 return 0;
221 }
222
223 char* end;
224 unsigned long userid = strtoul(name+1, &end, 10);
225 if (end[0] != '_' || end[1] == 0) {
226 errno = ENOENT;
227 return 0;
228 }
229
230 unsigned long appid = 0;
231 if (end[1] == 'a' && isdigit(end[2])) {
232 // end will point to \0 if the strtoul below succeeds.
233 appid = strtoul(end+2, &end, 10) + AID_APP;
234 } else if (end[1] == 'i' && isdigit(end[2])) {
235 // end will point to \0 if the strtoul below succeeds.
236 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
237 } else {
238 for (size_t n = 0; n < android_id_count; n++) {
239 if (!strcmp(android_ids[n].name, end + 1)) {
240 appid = android_ids[n].aid;
241 // Move the end pointer to the null terminator.
242 end += strlen(android_ids[n].name) + 1;
243 }
244 }
245 }
246
247 // Check that the entire string was consumed by one of the 3 cases above.
248 if (end[0] != 0) {
249 errno = ENOENT;
250 return 0;
251 }
252
253 // Check that user id won't overflow.
254 if (userid > 1000) {
255 errno = ENOENT;
256 return 0;
257 }
258
259 // Check that app id is within range.
260 if (appid >= AID_USER) {
261 errno = ENOENT;
262 return 0;
263 }
264
265 return (unsigned)(appid + userid*AID_USER);
266}
267
Kenny Root2a54e5e2012-09-13 10:52:52 -0700268static void print_app_name_from_appid_userid(const uid_t appid,
269 const uid_t userid, char* buffer, const int bufferlen) {
Kenny Root8a05a012012-09-13 14:31:50 -0700270 if (appid >= AID_ISOLATED_START) {
271 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
272 } else if (userid == 0 && appid >= AID_SHARED_GID_START) {
273 snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
274 } else if (appid < AID_APP) {
275 for (size_t n = 0; n < android_id_count; n++) {
276 if (android_ids[n].aid == appid) {
277 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
278 return;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700279 }
280 }
Elliott Hughesde727ca2012-08-13 15:45:36 -0700281 } else {
Kenny Root8a05a012012-09-13 14:31:50 -0700282 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700283 }
284}
285
Kenny Root2a54e5e2012-09-13 10:52:52 -0700286static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
287 const uid_t appid = uid % AID_USER;
288 const uid_t userid = uid / AID_USER;
289 return print_app_name_from_appid_userid(appid, userid, buffer, bufferlen);
290}
291
Elliott Hughesde727ca2012-08-13 15:45:36 -0700292// Translate a uid into the corresponding name.
293// 0 to AID_APP-1 -> "system", "radio", etc.
294// AID_APP to AID_ISOLATED_START-1 -> u0_a1234
295// AID_ISOLATED_START to AID_USER-1 -> u0_i1234
296// AID_USER+ -> u1_radio, u1_a1234, u2_i1234, etc.
297// returns a passwd structure (sets errno to ENOENT on failure).
298static passwd* app_id_to_passwd(uid_t uid, stubs_state_t* state) {
299 passwd* pw = &state->passwd_;
300
301 if (uid < AID_APP) {
302 errno = ENOENT;
303 return NULL;
304 }
305
Kenny Root2a54e5e2012-09-13 10:52:52 -0700306 const uid_t appid = uid % AID_USER;
307 const uid_t userid = uid / AID_USER;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700308
Kenny Root2a54e5e2012-09-13 10:52:52 -0700309 print_app_name_from_appid_userid(appid, userid, state->app_name_buffer_,
310 sizeof(state->app_name_buffer_));
311
312 if (appid < AID_APP) {
313 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
314 } else {
315 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
316 }
317
Elliott Hughesde727ca2012-08-13 15:45:36 -0700318 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
319
320 pw->pw_name = state->app_name_buffer_;
321 pw->pw_dir = state->dir_buffer_;
322 pw->pw_shell = state->sh_buffer_;
323 pw->pw_uid = uid;
324 pw->pw_gid = uid;
325
326 return pw;
327}
328
329// Translate a gid into the corresponding app_<gid>
330// group structure (sets errno to ENOENT on failure).
331static group* app_id_to_group(gid_t gid, stubs_state_t* state) {
332 if (gid < AID_APP) {
333 errno = ENOENT;
334 return NULL;
335 }
336
Kenny Root2a54e5e2012-09-13 10:52:52 -0700337 print_app_name_from_uid(gid, state->group_name_buffer_,
338 sizeof(state->group_name_buffer_));
Elliott Hughesde727ca2012-08-13 15:45:36 -0700339
340 group* gr = &state->group_;
341 gr->gr_name = state->group_name_buffer_;
342 gr->gr_gid = gid;
343 gr->gr_mem[0] = gr->gr_name;
344 gr->gr_mem[1] = NULL;
345 return gr;
346}
347
348
349passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
350 stubs_state_t* state = __stubs_state();
351 if (state == NULL) {
352 return NULL;
353 }
354
355 passwd* pw = android_id_to_passwd(state, uid);
356 if (pw != NULL) {
357 return pw;
358 }
359 return app_id_to_passwd(uid, state);
360}
361
362passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
363 stubs_state_t* state = __stubs_state();
364 if (state == NULL) {
365 return NULL;
366 }
367
368 passwd* pw = android_name_to_passwd(state, login);
369 if (pw != NULL) {
370 return pw;
371 }
372 return app_id_to_passwd(app_id_from_name(login), state);
373}
374
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700375// All users are in just one group, the one passed in.
376int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700377 if (*ngroups < 1) {
378 *ngroups = 1;
379 return -1;
380 }
381 groups[0] = group;
382 return (*ngroups = 1);
383}
384
385char* getlogin() { // NOLINT: implementing bad function.
386 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
387 return (pw != NULL) ? pw->pw_name : NULL;
388}
389
390group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
391 stubs_state_t* state = __stubs_state();
392 if (state == NULL) {
393 return NULL;
394 }
395
396 group* gr = android_id_to_group(&state->group_, gid);
397 if (gr != NULL) {
398 return gr;
399 }
400
401 return app_id_to_group(gid, state);
402}
403
404group* getgrnam(const char* name) { // NOLINT: implementing bad function.
405 stubs_state_t* state = __stubs_state();
406 if (state == NULL) {
407 return NULL;
408 }
409
410 if (android_name_to_group(&state->group_, name) != 0) {
411 return &state->group_;
412 }
413
414 return app_id_to_group(app_id_from_name(name), state);
415}
416
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700417// We don't have an /etc/networks, so all inputs return NULL.
418netent* getnetbyname(const char* /*name*/) {
419 return NULL;
420}
421
422// We don't have an /etc/networks, so all inputs return NULL.
423netent* getnetbyaddr(uint32_t /*net*/, int /*type*/) {
424 return NULL;
425}
426
427// We don't have an /etc/protocols, so all inputs return NULL.
428protoent* getprotobyname(const char* /*name*/) {
429 return NULL;
430}
431
432// We don't have an /etc/protocols, so all inputs return NULL.
433protoent* getprotobynumber(int /*proto*/) {
434 return NULL;
435}
436
Elliott Hughesde727ca2012-08-13 15:45:36 -0700437static void unimplemented_stub(const char* function) {
438 const char* fmt = "%s(3) is not implemented on Android\n";
Elliott Hughes1e980b62013-01-17 18:36:06 -0800439 __libc_format_log(ANDROID_LOG_WARN, "libc", fmt, function);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700440 fprintf(stderr, fmt, function);
441}
442
443#define UNIMPLEMENTED unimplemented_stub(__PRETTY_FUNCTION__)
444
Elliott Hughesde727ca2012-08-13 15:45:36 -0700445void endpwent() {
446 UNIMPLEMENTED;
447}
448
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700449mntent* getmntent(FILE* /*f*/) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700450 UNIMPLEMENTED;
451 return NULL;
452}
453
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700454char* ttyname(int /*fd*/) { // NOLINT: implementing bad function.
Elliott Hughesde727ca2012-08-13 15:45:36 -0700455 UNIMPLEMENTED;
456 return NULL;
457}
458
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700459int ttyname_r(int /*fd*/, char* /*buf*/, size_t /*buflen*/) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700460 UNIMPLEMENTED;
461 return -ERANGE;
462}
463
Elliott Hughesde727ca2012-08-13 15:45:36 -0700464char* getusershell() {
465 UNIMPLEMENTED;
466 return NULL;
467}
468
469void setusershell() {
470 UNIMPLEMENTED;
471}
472
473void endusershell() {
474 UNIMPLEMENTED;
475}