blob: 8ddc326e0d4723bd93bc98af670aa14b8c18044a [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>
Elliott Hughesde727ca2012-08-13 15:45:36 -070034#include <pthread.h>
35#include <pwd.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <unistd.h>
39
Elliott Hughes3e898472013-02-12 16:40:24 +000040#include "private/android_filesystem_config.h"
41#include "private/debug_format.h"
42#include "private/ErrnoRestorer.h"
43#include "private/logd.h"
44
Elliott Hughesde727ca2012-08-13 15:45:36 -070045// Thread-specific state for the non-reentrant functions.
46static pthread_once_t stubs_once = PTHREAD_ONCE_INIT;
47static pthread_key_t stubs_key;
48struct stubs_state_t {
49 passwd passwd_;
50 group group_;
51 char* group_members_[2];
52 char app_name_buffer_[32];
53 char group_name_buffer_[32];
54 char dir_buffer_[32];
55 char sh_buffer_[32];
56};
57
58static int do_getpw_r(int by_name, const char* name, uid_t uid,
59 passwd* dst, char* buf, size_t byte_count,
60 passwd** result) {
61 // getpwnam_r and getpwuid_r don't modify errno, but library calls we
62 // make might.
Elliott Hughes3e898472013-02-12 16:40:24 +000063 ErrnoRestorer errno_restorer;
Elliott Hughesde727ca2012-08-13 15:45:36 -070064 *result = NULL;
65
66 // Our implementation of getpwnam(3) and getpwuid(3) use thread-local
67 // storage, so we can call them as long as we copy everything out
68 // before returning.
69 const passwd* src = by_name ? getpwnam(name) : getpwuid(uid); // NOLINT: see above.
70
71 // POSIX allows failure to find a match to be considered a non-error.
72 // Reporting success (0) but with *result NULL is glibc's behavior.
73 if (src == NULL) {
Elliott Hughes3e898472013-02-12 16:40:24 +000074 return (errno == ENOENT) ? 0 : errno;
Elliott Hughesde727ca2012-08-13 15:45:36 -070075 }
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) {
Elliott Hughesde727ca2012-08-13 15:45:36 -070087 return ERANGE;
88 }
89
90 // Copy the strings.
Elliott Hughes3e898472013-02-12 16:40:24 +000091 snprintf(buf, byte_count, "%s%c%s%c%s", src->pw_name, 0, src->pw_dir, 0, src->pw_shell);
Elliott Hughesde727ca2012-08-13 15:45:36 -070092
93 // pw_passwd is non-POSIX and unused (always NULL) in bionic.
94 // pw_gecos is non-POSIX and missing in bionic.
95 dst->pw_passwd = NULL;
96
97 // Copy the integral fields.
98 dst->pw_gid = src->pw_gid;
99 dst->pw_uid = src->pw_uid;
100
101 *result = dst;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700102 return 0;
103}
104
105int getpwnam_r(const char* name, passwd* pwd,
106 char* buf, size_t byte_count, passwd** result) {
107 return do_getpw_r(1, name, -1, pwd, buf, byte_count, result);
108}
109
110int getpwuid_r(uid_t uid, passwd* pwd,
111 char* buf, size_t byte_count, passwd** result) {
112 return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result);
113}
114
115static stubs_state_t* stubs_state_alloc() {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700116 stubs_state_t* s = static_cast<stubs_state_t*>(calloc(1, sizeof(*s)));
Elliott Hughesde727ca2012-08-13 15:45:36 -0700117 if (s != NULL) {
118 s->group_.gr_mem = s->group_members_;
119 }
120 return s;
121}
122
123static void stubs_state_free(void* ptr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700124 stubs_state_t* state = static_cast<stubs_state_t*>(ptr);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700125 free(state);
126}
127
128static void __stubs_key_init() {
129 pthread_key_create(&stubs_key, stubs_state_free);
130}
131
132static stubs_state_t* __stubs_state() {
133 pthread_once(&stubs_once, __stubs_key_init);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700134 stubs_state_t* s = static_cast<stubs_state_t*>(pthread_getspecific(stubs_key));
Elliott Hughesde727ca2012-08-13 15:45:36 -0700135 if (s == NULL) {
136 s = stubs_state_alloc();
137 if (s == NULL) {
138 errno = ENOMEM; // Just in case.
139 } else {
140 if (pthread_setspecific(stubs_key, s) != 0) {
141 stubs_state_free(s);
142 errno = ENOMEM;
143 s = NULL;
144 }
145 }
146 }
147 return s;
148}
149
150static passwd* android_iinfo_to_passwd(stubs_state_t* state,
151 const android_id_info* iinfo) {
152 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
153 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
154
155 passwd* pw = &state->passwd_;
156 pw->pw_name = (char*) iinfo->name;
157 pw->pw_uid = iinfo->aid;
158 pw->pw_gid = iinfo->aid;
159 pw->pw_dir = state->dir_buffer_;
160 pw->pw_shell = state->sh_buffer_;
161 return pw;
162}
163
164static group* android_iinfo_to_group(group* gr,
165 const android_id_info* iinfo) {
166 gr->gr_name = (char*) iinfo->name;
167 gr->gr_gid = iinfo->aid;
168 gr->gr_mem[0] = gr->gr_name;
169 gr->gr_mem[1] = NULL;
170 return gr;
171}
172
173static passwd* android_id_to_passwd(stubs_state_t* state, unsigned id) {
174 for (size_t n = 0; n < android_id_count; ++n) {
175 if (android_ids[n].aid == id) {
176 return android_iinfo_to_passwd(state, android_ids + n);
177 }
178 }
179 return NULL;
180}
181
182static passwd* android_name_to_passwd(stubs_state_t* state, const char* name) {
183 for (size_t n = 0; n < android_id_count; ++n) {
184 if (!strcmp(android_ids[n].name, name)) {
185 return android_iinfo_to_passwd(state, android_ids + n);
186 }
187 }
188 return NULL;
189}
190
191static group* android_id_to_group(group* gr, unsigned id) {
192 for (size_t n = 0; n < android_id_count; ++n) {
193 if (android_ids[n].aid == id) {
194 return android_iinfo_to_group(gr, android_ids + n);
195 }
196 }
197 return NULL;
198}
199
200static group* android_name_to_group(group* gr, const char* name) {
201 for (size_t n = 0; n < android_id_count; ++n) {
202 if (!strcmp(android_ids[n].name, name)) {
203 return android_iinfo_to_group(gr, android_ids + n);
204 }
205 }
206 return NULL;
207}
208
209// Translate a user/group name to the corresponding user/group id.
210// u0_a1234 -> 0 * AID_USER + AID_APP + 1234
211// u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000
212// u1_system -> 1 * AID_USER + android_ids['system']
213// returns 0 and sets errno to ENOENT in case of error
214static unsigned app_id_from_name(const char* name) {
215 if (name[0] != 'u' || !isdigit(name[1])) {
216 errno = ENOENT;
217 return 0;
218 }
219
220 char* end;
221 unsigned long userid = strtoul(name+1, &end, 10);
222 if (end[0] != '_' || end[1] == 0) {
223 errno = ENOENT;
224 return 0;
225 }
226
227 unsigned long appid = 0;
228 if (end[1] == 'a' && isdigit(end[2])) {
229 // end will point to \0 if the strtoul below succeeds.
230 appid = strtoul(end+2, &end, 10) + AID_APP;
231 } else if (end[1] == 'i' && isdigit(end[2])) {
232 // end will point to \0 if the strtoul below succeeds.
233 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
234 } else {
235 for (size_t n = 0; n < android_id_count; n++) {
236 if (!strcmp(android_ids[n].name, end + 1)) {
237 appid = android_ids[n].aid;
238 // Move the end pointer to the null terminator.
239 end += strlen(android_ids[n].name) + 1;
240 }
241 }
242 }
243
244 // Check that the entire string was consumed by one of the 3 cases above.
245 if (end[0] != 0) {
246 errno = ENOENT;
247 return 0;
248 }
249
250 // Check that user id won't overflow.
251 if (userid > 1000) {
252 errno = ENOENT;
253 return 0;
254 }
255
256 // Check that app id is within range.
257 if (appid >= AID_USER) {
258 errno = ENOENT;
259 return 0;
260 }
261
262 return (unsigned)(appid + userid*AID_USER);
263}
264
Kenny Root2a54e5e2012-09-13 10:52:52 -0700265static void print_app_name_from_appid_userid(const uid_t appid,
266 const uid_t userid, char* buffer, const int bufferlen) {
Kenny Root8a05a012012-09-13 14:31:50 -0700267 if (appid >= AID_ISOLATED_START) {
268 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
269 } else if (userid == 0 && appid >= AID_SHARED_GID_START) {
270 snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
271 } else if (appid < AID_APP) {
272 for (size_t n = 0; n < android_id_count; n++) {
273 if (android_ids[n].aid == appid) {
274 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
275 return;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700276 }
277 }
Elliott Hughesde727ca2012-08-13 15:45:36 -0700278 } else {
Kenny Root8a05a012012-09-13 14:31:50 -0700279 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700280 }
281}
282
Kenny Root2a54e5e2012-09-13 10:52:52 -0700283static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
284 const uid_t appid = uid % AID_USER;
285 const uid_t userid = uid / AID_USER;
286 return print_app_name_from_appid_userid(appid, userid, buffer, bufferlen);
287}
288
Elliott Hughesde727ca2012-08-13 15:45:36 -0700289// Translate a uid into the corresponding name.
290// 0 to AID_APP-1 -> "system", "radio", etc.
291// AID_APP to AID_ISOLATED_START-1 -> u0_a1234
292// AID_ISOLATED_START to AID_USER-1 -> u0_i1234
293// AID_USER+ -> u1_radio, u1_a1234, u2_i1234, etc.
294// returns a passwd structure (sets errno to ENOENT on failure).
295static passwd* app_id_to_passwd(uid_t uid, stubs_state_t* state) {
296 passwd* pw = &state->passwd_;
297
298 if (uid < AID_APP) {
299 errno = ENOENT;
300 return NULL;
301 }
302
Kenny Root2a54e5e2012-09-13 10:52:52 -0700303 const uid_t appid = uid % AID_USER;
304 const uid_t userid = uid / AID_USER;
Elliott Hughesde727ca2012-08-13 15:45:36 -0700305
Kenny Root2a54e5e2012-09-13 10:52:52 -0700306 print_app_name_from_appid_userid(appid, userid, state->app_name_buffer_,
307 sizeof(state->app_name_buffer_));
308
309 if (appid < AID_APP) {
310 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
311 } else {
312 snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/data");
313 }
314
Elliott Hughesde727ca2012-08-13 15:45:36 -0700315 snprintf(state->sh_buffer_, sizeof(state->sh_buffer_), "/system/bin/sh");
316
317 pw->pw_name = state->app_name_buffer_;
318 pw->pw_dir = state->dir_buffer_;
319 pw->pw_shell = state->sh_buffer_;
320 pw->pw_uid = uid;
321 pw->pw_gid = uid;
322
323 return pw;
324}
325
326// Translate a gid into the corresponding app_<gid>
327// group structure (sets errno to ENOENT on failure).
328static group* app_id_to_group(gid_t gid, stubs_state_t* state) {
329 if (gid < AID_APP) {
330 errno = ENOENT;
331 return NULL;
332 }
333
Kenny Root2a54e5e2012-09-13 10:52:52 -0700334 print_app_name_from_uid(gid, state->group_name_buffer_,
335 sizeof(state->group_name_buffer_));
Elliott Hughesde727ca2012-08-13 15:45:36 -0700336
337 group* gr = &state->group_;
338 gr->gr_name = state->group_name_buffer_;
339 gr->gr_gid = gid;
340 gr->gr_mem[0] = gr->gr_name;
341 gr->gr_mem[1] = NULL;
342 return gr;
343}
344
345
346passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
347 stubs_state_t* state = __stubs_state();
348 if (state == NULL) {
349 return NULL;
350 }
351
352 passwd* pw = android_id_to_passwd(state, uid);
353 if (pw != NULL) {
354 return pw;
355 }
356 return app_id_to_passwd(uid, state);
357}
358
359passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
360 stubs_state_t* state = __stubs_state();
361 if (state == NULL) {
362 return NULL;
363 }
364
365 passwd* pw = android_name_to_passwd(state, login);
366 if (pw != NULL) {
367 return pw;
368 }
369 return app_id_to_passwd(app_id_from_name(login), state);
370}
371
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700372// All users are in just one group, the one passed in.
373int getgrouplist(const char* /*user*/, gid_t group, gid_t* groups, int* ngroups) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700374 if (*ngroups < 1) {
375 *ngroups = 1;
376 return -1;
377 }
378 groups[0] = group;
379 return (*ngroups = 1);
380}
381
382char* getlogin() { // NOLINT: implementing bad function.
383 passwd *pw = getpwuid(getuid()); // NOLINT: implementing bad function in terms of bad function.
384 return (pw != NULL) ? pw->pw_name : NULL;
385}
386
387group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
388 stubs_state_t* state = __stubs_state();
389 if (state == NULL) {
390 return NULL;
391 }
392
393 group* gr = android_id_to_group(&state->group_, gid);
394 if (gr != NULL) {
395 return gr;
396 }
397
398 return app_id_to_group(gid, state);
399}
400
401group* getgrnam(const char* name) { // NOLINT: implementing bad function.
402 stubs_state_t* state = __stubs_state();
403 if (state == NULL) {
404 return NULL;
405 }
406
407 if (android_name_to_group(&state->group_, name) != 0) {
408 return &state->group_;
409 }
410
411 return app_id_to_group(app_id_from_name(name), state);
412}
413
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700414// We don't have an /etc/networks, so all inputs return NULL.
415netent* getnetbyname(const char* /*name*/) {
416 return NULL;
417}
418
419// We don't have an /etc/networks, so all inputs return NULL.
420netent* getnetbyaddr(uint32_t /*net*/, int /*type*/) {
421 return NULL;
422}
423
424// We don't have an /etc/protocols, so all inputs return NULL.
425protoent* getprotobyname(const char* /*name*/) {
426 return NULL;
427}
428
429// We don't have an /etc/protocols, so all inputs return NULL.
430protoent* getprotobynumber(int /*proto*/) {
431 return NULL;
432}
433
Elliott Hughesde727ca2012-08-13 15:45:36 -0700434static void unimplemented_stub(const char* function) {
435 const char* fmt = "%s(3) is not implemented on Android\n";
Elliott Hughes1e980b62013-01-17 18:36:06 -0800436 __libc_format_log(ANDROID_LOG_WARN, "libc", fmt, function);
Elliott Hughesde727ca2012-08-13 15:45:36 -0700437 fprintf(stderr, fmt, function);
438}
439
440#define UNIMPLEMENTED unimplemented_stub(__PRETTY_FUNCTION__)
441
Elliott Hughesde727ca2012-08-13 15:45:36 -0700442void endpwent() {
443 UNIMPLEMENTED;
444}
445
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700446mntent* getmntent(FILE* /*f*/) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700447 UNIMPLEMENTED;
448 return NULL;
449}
450
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700451char* ttyname(int /*fd*/) { // NOLINT: implementing bad function.
Elliott Hughesde727ca2012-08-13 15:45:36 -0700452 UNIMPLEMENTED;
453 return NULL;
454}
455
Elliott Hughes6fa26e22012-10-22 16:04:56 -0700456int ttyname_r(int /*fd*/, char* /*buf*/, size_t /*buflen*/) {
Elliott Hughesde727ca2012-08-13 15:45:36 -0700457 UNIMPLEMENTED;
458 return -ERANGE;
459}
460
Elliott Hughesde727ca2012-08-13 15:45:36 -0700461char* getusershell() {
462 UNIMPLEMENTED;
463 return NULL;
464}
465
466void setusershell() {
467 UNIMPLEMENTED;
468}
469
470void endusershell() {
471 UNIMPLEMENTED;
472}