blob: d140890130c34595f052ebbd16d94d0567e72925 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 */
Elliott Hughes52d62332012-07-27 17:40:29 -070028
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080029#include <ctype.h>
Elliott Hughes52d62332012-07-27 17:40:29 -070030#include <errno.h>
31#include <grp.h>
32#include <mntent.h>
33#include <netdb.h>
34#include <private/android_filesystem_config.h>
35#include <private/logd.h>
36#include <pthread.h>
37#include <pwd.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Brian Carlstrom1f8e2672011-05-27 00:52:21 -070042static int do_getpw_r(int by_name, const char* name, uid_t uid,
43 struct passwd* dst, char* buf, size_t byte_count, struct passwd** result)
44{
45 /*
46 * getpwnam_r and getpwuid_r don't modify errno, but library calls we
47 * make might.
48 */
49 int old_errno = errno;
50 int rc = 0;
51 *result = NULL;
52
53 const struct passwd* src = by_name ? getpwnam(name) : getpwuid(uid);
54
55 /*
56 * POSIX allows failure to find a match to be considered a non-error.
57 * Reporting success (0) but with *result NULL is glibc's behavior.
58 */
59 if (src == NULL) {
60 rc = (errno == ENOENT) ? 0 : errno;
61 goto failure;
62 }
63
64 /*
65 * Work out where our strings will go in 'buf', and whether we've got
66 * enough space.
67 */
68 size_t required_byte_count = 0;
69 dst->pw_name = buf;
70 required_byte_count += strlen(src->pw_name) + 1;
71 dst->pw_dir = buf + required_byte_count;
72 required_byte_count += strlen(src->pw_dir) + 1;
73 dst->pw_shell = buf + required_byte_count;
74 required_byte_count += strlen(src->pw_shell) + 1;
75 if (byte_count < required_byte_count) {
76 rc = ERANGE;
77 goto failure;
78 }
79
80 /* Copy the strings. */
81 snprintf(buf, byte_count, "%s%c%s%c%s",
82 src->pw_name, 0, src->pw_dir, 0, src->pw_shell);
83
84 /*
85 * pw_passwd is non-POSIX and unused (always NULL) in bionic.
86 * pw_gecos is non-POSIX and missing in bionic.
87 */
88 dst->pw_passwd = NULL;
89
90 /* Copy the integral fields. */
91 dst->pw_gid = src->pw_gid;
92 dst->pw_uid = src->pw_uid;
93
94success:
95 rc = 0;
96 *result = dst;
97failure:
98 errno = old_errno;
99 return rc;
100}
101
102int getpwnam_r(const char* name, struct passwd* pwd,
103 char* buf, size_t byte_count, struct passwd** result)
104{
105 return do_getpw_r(1, name, -1, pwd, buf, byte_count, result);
106}
107
108int getpwuid_r(uid_t uid, struct passwd* pwd,
109 char* buf, size_t byte_count, struct passwd** result)
110{
111 return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result);
112}
113
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114/** Thread-specific state for the stubs functions
115 **/
116
Jim Huangc9409452010-10-15 02:21:14 +0800117static pthread_once_t the_once = PTHREAD_ONCE_INIT;
118static pthread_key_t the_key;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119
120typedef struct {
121 struct passwd passwd;
122 struct group group;
123 char* group_members[2];
124 char app_name_buffer[32];
125 char group_name_buffer[32];
126} stubs_state_t;
127
128static void
129stubs_state_free( void* _s )
130{
131 stubs_state_t* s = _s;
132 free(s);
133}
134
135static stubs_state_t*
136stubs_state_alloc( void )
137{
138 stubs_state_t* s = calloc(1, sizeof *s);
139
140 if (s != NULL) {
141 s->group.gr_mem = s->group_members;
142 }
143 return s;
144}
145
146static void __stubs_key_init(void)
147{
148 pthread_key_create( &the_key, stubs_state_free );
149}
150
151static stubs_state_t*
152__stubs_state(void)
153{
154 stubs_state_t* s;
155
156 pthread_once(&the_once, __stubs_key_init);
157 s = pthread_getspecific(the_key);
158 if (s == NULL) {
159 s = stubs_state_alloc();
160 if (s == NULL) {
161 errno = ENOMEM; /* just in case */
162 } else {
163 if ( pthread_setspecific(the_key, s) != 0 ) {
164 stubs_state_free(s);
165 errno = ENOMEM;
166 s = NULL;
167 }
168 }
169 }
170 return s;
171}
172
173static struct passwd*
174android_iinfo_to_passwd( struct passwd *pw,
175 struct android_id_info *iinfo )
176{
177 pw->pw_name = (char*)iinfo->name;
178 pw->pw_uid = iinfo->aid;
179 pw->pw_gid = iinfo->aid;
180 pw->pw_dir = "/";
181 pw->pw_shell = "/system/bin/sh";
182 return pw;
183}
184
185static struct group*
186android_iinfo_to_group( struct group *gr,
187 struct android_id_info *iinfo )
188{
189 gr->gr_name = (char*) iinfo->name;
190 gr->gr_gid = iinfo->aid;
191 gr->gr_mem[0] = gr->gr_name;
192 gr->gr_mem[1] = NULL;
193 return gr;
194}
195
196static struct passwd *
197android_id_to_passwd( struct passwd *pw, unsigned id)
198{
199 struct android_id_info *iinfo = android_ids;
200 unsigned n;
201 for (n = 0; n < android_id_count; n++) {
202 if (iinfo[n].aid == id) {
203 return android_iinfo_to_passwd(pw, iinfo + n);
204 }
205 }
206 return NULL;
207}
208
209static struct passwd*
210android_name_to_passwd(struct passwd *pw, const char *name)
211{
212 struct android_id_info *iinfo = android_ids;
213 unsigned n;
214 for (n = 0; n < android_id_count; n++) {
215 if (!strcmp(iinfo[n].name, name)) {
216 return android_iinfo_to_passwd(pw, iinfo + n);
217 }
218 }
219 return NULL;
220}
221
222static struct group*
223android_id_to_group( struct group *gr, unsigned id )
224{
225 struct android_id_info *iinfo = android_ids;
226 unsigned n;
227 for (n = 0; n < android_id_count; n++) {
228 if (iinfo[n].aid == id) {
229 return android_iinfo_to_group(gr, iinfo + n);
230 }
231 }
232 return NULL;
233}
234
235static struct group*
236android_name_to_group( struct group *gr, const char *name )
237{
238 struct android_id_info *iinfo = android_ids;
239 unsigned n;
240 for (n = 0; n < android_id_count; n++) {
241 if (!strcmp(iinfo[n].name, name)) {
242 return android_iinfo_to_group(gr, iinfo + n);
243 }
244 }
245 return NULL;
246}
247
Amith Yamasanibf9441e2012-08-12 19:51:34 -0700248/* translate a user/group name to the corresponding user/group id.
249 * u0_a1234 -> 0 * AID_USER + AID_APP + 1234
250 * u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000
251 * u1_system -> 1 * AID_USER + android_ids['system']
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800252 * returns 0 and sets errno to ENOENT in case of error
253 */
254static unsigned
255app_id_from_name( const char* name )
256{
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800257 unsigned long userid;
Amith Yamasanibf9441e2012-08-12 19:51:34 -0700258 unsigned long appid = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800259 char* end;
260
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800261 if (name[0] != 'u' || !isdigit(name[1]))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800262 goto FAIL;
263
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800264 userid = strtoul(name+1, &end, 10);
Amith Yamasanibf9441e2012-08-12 19:51:34 -0700265 if (end[0] != '_' || end[1] == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800266 goto FAIL;
267
Amith Yamasanibf9441e2012-08-12 19:51:34 -0700268 if (end[1] == 'a' && isdigit(end[2])) {
269 /* end will point to \0 if the strtoul below succeeds. */
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800270 appid = strtoul(end+2, &end, 10) + AID_APP;
Amith Yamasanibf9441e2012-08-12 19:51:34 -0700271 } else if (end[1] == 'i' && isdigit(end[2])) {
272 /* end will point to \0 if the strtoul below succeeds. */
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800273 appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
Amith Yamasanibf9441e2012-08-12 19:51:34 -0700274 } else {
275 for (size_t n = 0; n < android_id_count; n++) {
276 if (!strcmp(android_ids[n].name, end + 1)) {
277 appid = android_ids[n].aid;
278 /* move the end pointer to the null terminator */
279 end += 1 + strlen(android_ids[n].name);
280 }
281 }
282 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283
Amith Yamasanibf9441e2012-08-12 19:51:34 -0700284 /* check if the entire string was consumed by the 3 cases above */
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800285 if (end[0] != 0)
286 goto FAIL;
287
288 /* check that user id won't overflow */
289 if (userid > 1000)
290 goto FAIL;
291
292 /* check that app id is within range */
Amith Yamasanibf9441e2012-08-12 19:51:34 -0700293 if (appid >= AID_USER)
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800294 goto FAIL;
295
296 return (unsigned)(appid + userid*AID_USER);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800297
298FAIL:
299 errno = ENOENT;
300 return 0;
301}
302
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800303static void
304print_app_uid_name(uid_t uid, char* buffer, int bufferlen)
305{
306 uid_t appid;
307 uid_t userid;
308
309 appid = uid % AID_USER;
310 userid = uid / AID_USER;
311
312 if (appid < AID_ISOLATED_START) {
Amith Yamasanibf9441e2012-08-12 19:51:34 -0700313 if (appid < AID_APP) {
314 for (size_t n = 0; n < android_id_count; n++) {
315 if (android_ids[n].aid == appid) {
316 snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
317 return;
318 }
319 }
320 }
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800321 snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP);
322 } else {
323 snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
324 }
325}
326
Amith Yamasanibf9441e2012-08-12 19:51:34 -0700327/* translate a uid into the corresponding name.
328 * 0 to AID_APP-1 -> "system", "radio", etc.
329 * AID_APP to AID_ISOLATED_START-1 -> u0_a1234
330 * AID_ISOLATED_START to AID_USER-1 -> u0_i1234
331 * AID_USER+ -> u1_radio, u1_a1234, u2_i1234, etc.
332 * returns a passwd structure (sets errno to ENOENT on failure)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333 */
334static struct passwd*
335app_id_to_passwd(uid_t uid, stubs_state_t* state)
336{
337 struct passwd* pw = &state->passwd;
338
339 if (uid < AID_APP) {
340 errno = ENOENT;
341 return NULL;
342 }
343
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800344 print_app_uid_name(uid, state->app_name_buffer, sizeof state->app_name_buffer);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800345
346 pw->pw_name = state->app_name_buffer;
347 pw->pw_dir = "/data";
348 pw->pw_shell = "/system/bin/sh";
349 pw->pw_uid = uid;
350 pw->pw_gid = uid;
351
352 return pw;
353}
354
355/* translate a gid into the corresponding app_<gid>
356 * group structure (sets errno to ENOENT on failure)
357 */
358static struct group*
359app_id_to_group(gid_t gid, stubs_state_t* state)
360{
361 struct group* gr = &state->group;
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800362 int appid;
363 int userid;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364
365 if (gid < AID_APP) {
366 errno = ENOENT;
367 return NULL;
368 }
369
Dianne Hackborn058d6d82012-02-09 16:14:28 -0800370 print_app_uid_name(gid, state->group_name_buffer, sizeof state->group_name_buffer);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800371
372 gr->gr_name = state->group_name_buffer;
373 gr->gr_gid = gid;
374 gr->gr_mem[0] = gr->gr_name;
375 gr->gr_mem[1] = NULL;
376
377 return gr;
378}
379
380
381struct passwd*
382getpwuid(uid_t uid)
383{
384 stubs_state_t* state = __stubs_state();
385 struct passwd* pw;
386
387 if (state == NULL)
388 return NULL;
389
390 pw = &state->passwd;
391
392 if ( android_id_to_passwd(pw, uid) != NULL )
393 return pw;
394
395 return app_id_to_passwd(uid, state);
396}
397
398struct passwd*
399getpwnam(const char *login)
400{
401 stubs_state_t* state = __stubs_state();
402
403 if (state == NULL)
404 return NULL;
405
406 if (android_name_to_passwd(&state->passwd, login) != NULL)
407 return &state->passwd;
408
409 return app_id_to_passwd( app_id_from_name(login), state );
410}
411
412int
413getgrouplist (const char *user, gid_t group,
414 gid_t *groups, int *ngroups)
415{
416 if (*ngroups < 1) {
417 *ngroups = 1;
418 return -1;
419 }
420 groups[0] = group;
421 return (*ngroups = 1);
422}
423
424char*
425getlogin(void)
426{
427 struct passwd *pw = getpwuid(getuid());
428
429 if(pw) {
430 return pw->pw_name;
431 } else {
432 return NULL;
433 }
434}
435
436struct group*
437getgrgid(gid_t gid)
438{
439 stubs_state_t* state = __stubs_state();
440 struct group* gr;
441
442 if (state == NULL)
443 return NULL;
444
445 gr = android_id_to_group(&state->group, gid);
446 if (gr != NULL)
447 return gr;
448
449 return app_id_to_group(gid, state);
450}
451
452struct group*
453getgrnam(const char *name)
454{
455 stubs_state_t* state = __stubs_state();
456 unsigned id;
457
458 if (state == NULL)
459 return NULL;
460
461 if (android_name_to_group(&state->group, name) != 0)
462 return &state->group;
463
464 return app_id_to_group( app_id_from_name(name), state );
465}
466
Elliott Hughes52d62332012-07-27 17:40:29 -0700467static void unimplemented_stub(const char* function) {
468 const char* fmt = "%s(3) is not implemented on Android\n";
469 __libc_android_log_print(ANDROID_LOG_WARN, "libc", fmt, function);
470 fprintf(stderr, fmt, function);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800471}
472
Elliott Hughes52d62332012-07-27 17:40:29 -0700473#define UNIMPLEMENTED unimplemented_stub(__PRETTY_FUNCTION__)
474
475struct netent* getnetbyname(const char* name) {
476 UNIMPLEMENTED;
477 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800478}
479
Elliott Hughes52d62332012-07-27 17:40:29 -0700480void endpwent(void) {
481 UNIMPLEMENTED;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800482}
483
Elliott Hughes52d62332012-07-27 17:40:29 -0700484struct mntent* getmntent(FILE* f) {
485 UNIMPLEMENTED;
486 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800487}
488
Elliott Hughes52d62332012-07-27 17:40:29 -0700489char* ttyname(int fd) {
490 UNIMPLEMENTED;
491 return NULL;
Colin Crossfc10b242010-01-13 17:48:34 -0800492}
493
Elliott Hughes52d62332012-07-27 17:40:29 -0700494int ttyname_r(int fd, char* buf, size_t buflen) {
495 UNIMPLEMENTED;
496 return -ERANGE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800497}
498
Elliott Hughes52d62332012-07-27 17:40:29 -0700499struct netent* getnetbyaddr(uint32_t net, int type) {
500 UNIMPLEMENTED;
501 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800502}
503
Elliott Hughes52d62332012-07-27 17:40:29 -0700504struct protoent* getprotobyname(const char* name) {
505 UNIMPLEMENTED;
506 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800507}
Colin Crossfc10b242010-01-13 17:48:34 -0800508
Elliott Hughes52d62332012-07-27 17:40:29 -0700509struct protoent* getprotobynumber(int proto) {
510 UNIMPLEMENTED;
511 return NULL;
Colin Crossfc10b242010-01-13 17:48:34 -0800512}
513
Elliott Hughes52d62332012-07-27 17:40:29 -0700514char* getusershell(void) {
515 UNIMPLEMENTED;
516 return NULL;
Colin Crossfc10b242010-01-13 17:48:34 -0800517}
518
Elliott Hughes52d62332012-07-27 17:40:29 -0700519void setusershell(void) {
520 UNIMPLEMENTED;
521}
522
523void endusershell(void) {
524 UNIMPLEMENTED;
Colin Crossfc10b242010-01-13 17:48:34 -0800525}