2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2015-01-03 19:45:47 +05:30
|
|
|
/* Copyright (C) 2014 Tito Ragusa <farmatito@tiscali.it>
|
2004-07-15 18:23:49 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2004-07-15 18:23:49 +05:30
|
|
|
*/
|
2015-01-03 02:07:59 +05:30
|
|
|
/* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY!!
|
2004-07-15 18:23:49 +05:30
|
|
|
*
|
2015-01-03 02:07:59 +05:30
|
|
|
* Rewrite of some parts. Main differences are:
|
2004-07-15 18:23:49 +05:30
|
|
|
*
|
2015-01-03 02:07:59 +05:30
|
|
|
* 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically
|
2015-01-05 19:39:04 +05:30
|
|
|
* allocated.
|
2015-01-03 02:07:59 +05:30
|
|
|
* If ENABLE_FEATURE_CLEAN_UP is set the buffers are freed at program
|
|
|
|
* exit using the atexit function to make valgrind happy.
|
|
|
|
* 2) the passwd/group files:
|
|
|
|
* a) must contain the expected number of fields (as per count of field
|
|
|
|
* delimeters ":") or we will complain with a error message.
|
2015-01-05 19:39:04 +05:30
|
|
|
* b) leading and trailing whitespace in fields is stripped.
|
2015-01-03 02:07:59 +05:30
|
|
|
* c) some fields are not allowed to be empty (e.g. username, uid/gid,
|
|
|
|
* homedir, shell) and in this case NULL is returned and errno is
|
|
|
|
* set to EINVAL. This behaviour could be easily changed by
|
|
|
|
* modifying PW_DEF, GR_DEF, SP_DEF strings (uppercase
|
|
|
|
* makes a field mandatory).
|
|
|
|
* d) the string representing uid/gid must be convertible by strtoXX
|
2015-01-03 19:45:47 +05:30
|
|
|
* functions, or errno is set to EINVAL.
|
2015-01-05 19:39:04 +05:30
|
|
|
* e) leading and trailing whitespace in group member names is stripped.
|
2015-01-03 19:45:47 +05:30
|
|
|
* 3) the internal function for getgrouplist uses dynamically allocated buffer.
|
|
|
|
* 4) at the moment only the functions really used by busybox code are
|
2015-01-03 02:07:59 +05:30
|
|
|
* implemented, if you need a particular missing function it should be
|
|
|
|
* easy to write it by using the internal common code.
|
2004-07-15 18:23:49 +05:30
|
|
|
*/
|
|
|
|
|
2006-06-19 01:50:07 +05:30
|
|
|
#include "libbb.h"
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
struct const_passdb {
|
|
|
|
const char *filename;
|
2015-01-05 19:39:04 +05:30
|
|
|
char def[7 + 2*ENABLE_USE_BB_SHADOW];
|
|
|
|
uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
|
2015-01-03 02:07:59 +05:30
|
|
|
uint8_t numfields;
|
2015-01-04 01:17:47 +05:30
|
|
|
uint8_t size_of;
|
2015-01-03 02:07:59 +05:30
|
|
|
};
|
|
|
|
struct passdb {
|
|
|
|
const char *filename;
|
2015-01-05 19:39:04 +05:30
|
|
|
char def[7 + 2*ENABLE_USE_BB_SHADOW];
|
|
|
|
uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
|
2015-01-03 02:07:59 +05:30
|
|
|
uint8_t numfields;
|
2015-01-04 01:17:47 +05:30
|
|
|
uint8_t size_of;
|
2015-01-03 02:07:59 +05:30
|
|
|
FILE *fp;
|
2015-01-03 20:24:04 +05:30
|
|
|
char *malloced;
|
2015-01-03 02:07:59 +05:30
|
|
|
};
|
2015-01-04 01:17:47 +05:30
|
|
|
/* Note: for shadow db, def[] will not contain terminating NUL,
|
2015-01-03 23:42:49 +05:30
|
|
|
* but convert_to_struct() logic detects def[] end by "less than SP?",
|
|
|
|
* not by "is it NUL?" condition; and off[0] happens to be zero
|
|
|
|
* for every db anyway, so there _is_ in fact a terminating NUL there.
|
|
|
|
*/
|
2010-03-31 16:07:43 +05:30
|
|
|
|
2015-01-03 20:24:04 +05:30
|
|
|
/* S = string not empty, s = string maybe empty,
|
|
|
|
* I = uid,gid, l = long maybe empty, m = members,
|
|
|
|
* r = reserved
|
|
|
|
*/
|
|
|
|
#define PW_DEF "SsIIsSS"
|
|
|
|
#define GR_DEF "SsIm"
|
|
|
|
#define SP_DEF "Ssllllllr"
|
|
|
|
|
|
|
|
static const struct const_passdb const_pw_db = {
|
|
|
|
_PATH_PASSWD, PW_DEF,
|
|
|
|
{
|
|
|
|
offsetof(struct passwd, pw_name), /* 0 S */
|
|
|
|
offsetof(struct passwd, pw_passwd), /* 1 s */
|
|
|
|
offsetof(struct passwd, pw_uid), /* 2 I */
|
|
|
|
offsetof(struct passwd, pw_gid), /* 3 I */
|
|
|
|
offsetof(struct passwd, pw_gecos), /* 4 s */
|
|
|
|
offsetof(struct passwd, pw_dir), /* 5 S */
|
|
|
|
offsetof(struct passwd, pw_shell) /* 6 S */
|
|
|
|
},
|
2015-01-04 01:17:47 +05:30
|
|
|
sizeof(PW_DEF)-1, sizeof(struct passwd)
|
2015-01-03 20:24:04 +05:30
|
|
|
};
|
|
|
|
static const struct const_passdb const_gr_db = {
|
|
|
|
_PATH_GROUP, GR_DEF,
|
|
|
|
{
|
|
|
|
offsetof(struct group, gr_name), /* 0 S */
|
|
|
|
offsetof(struct group, gr_passwd), /* 1 s */
|
|
|
|
offsetof(struct group, gr_gid), /* 2 I */
|
|
|
|
offsetof(struct group, gr_mem) /* 3 m (char **) */
|
|
|
|
},
|
2015-01-04 01:17:47 +05:30
|
|
|
sizeof(GR_DEF)-1, sizeof(struct group)
|
2015-01-03 20:24:04 +05:30
|
|
|
};
|
2006-12-29 03:03:30 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2015-01-03 20:24:04 +05:30
|
|
|
static const struct const_passdb const_sp_db = {
|
|
|
|
_PATH_SHADOW, SP_DEF,
|
|
|
|
{
|
|
|
|
offsetof(struct spwd, sp_namp), /* 0 S Login name */
|
|
|
|
offsetof(struct spwd, sp_pwdp), /* 1 s Encrypted password */
|
|
|
|
offsetof(struct spwd, sp_lstchg), /* 2 l */
|
|
|
|
offsetof(struct spwd, sp_min), /* 3 l */
|
|
|
|
offsetof(struct spwd, sp_max), /* 4 l */
|
|
|
|
offsetof(struct spwd, sp_warn), /* 5 l */
|
|
|
|
offsetof(struct spwd, sp_inact), /* 6 l */
|
|
|
|
offsetof(struct spwd, sp_expire), /* 7 l */
|
|
|
|
offsetof(struct spwd, sp_flag) /* 8 r Reserved */
|
|
|
|
},
|
2015-01-04 01:17:47 +05:30
|
|
|
sizeof(SP_DEF)-1, sizeof(struct spwd)
|
2015-01-03 20:24:04 +05:30
|
|
|
};
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
|
|
|
|
2007-06-18 15:38:27 +05:30
|
|
|
/* We avoid having big global data. */
|
|
|
|
struct statics {
|
2015-01-05 19:39:04 +05:30
|
|
|
/* We use same buffer (db[0].malloced) for getpwuid and getpwnam.
|
2015-01-03 19:45:47 +05:30
|
|
|
* Manpage says:
|
2012-01-06 20:54:56 +05:30
|
|
|
* "The return value may point to a static area, and may be overwritten
|
|
|
|
* by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
|
|
|
|
*/
|
2015-01-03 02:07:59 +05:30
|
|
|
struct passdb db[2 + ENABLE_USE_BB_SHADOW];
|
|
|
|
char *tokenize_end;
|
2015-01-04 01:17:47 +05:30
|
|
|
unsigned string_size;
|
2007-06-18 15:38:27 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static struct statics *ptr_to_statics;
|
2015-01-03 02:07:59 +05:30
|
|
|
#define S (*ptr_to_statics)
|
|
|
|
#define has_S (ptr_to_statics)
|
2007-06-18 15:38:27 +05:30
|
|
|
|
2015-01-03 20:39:05 +05:30
|
|
|
#if ENABLE_FEATURE_CLEAN_UP
|
|
|
|
static void free_static(void)
|
|
|
|
{
|
|
|
|
free(S.db[0].malloced);
|
|
|
|
free(S.db[1].malloced);
|
|
|
|
# if ENABLE_USE_BB_SHADOW
|
2015-01-03 23:42:49 +05:30
|
|
|
free(S.db[2].malloced);
|
2015-01-03 20:39:05 +05:30
|
|
|
# endif
|
|
|
|
free(ptr_to_statics);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-06-18 15:38:27 +05:30
|
|
|
static struct statics *get_S(void)
|
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
if (!ptr_to_statics) {
|
|
|
|
ptr_to_statics = xzalloc(sizeof(S));
|
|
|
|
memcpy(&S.db[0], &const_pw_db, sizeof(const_pw_db));
|
|
|
|
memcpy(&S.db[1], &const_gr_db, sizeof(const_gr_db));
|
|
|
|
#if ENABLE_USE_BB_SHADOW
|
|
|
|
memcpy(&S.db[2], &const_sp_db, sizeof(const_sp_db));
|
2015-01-03 20:39:05 +05:30
|
|
|
#endif
|
|
|
|
#if ENABLE_FEATURE_CLEAN_UP
|
|
|
|
atexit(free_static);
|
2015-01-03 02:07:59 +05:30
|
|
|
#endif
|
|
|
|
}
|
2007-06-18 15:38:27 +05:30
|
|
|
return ptr_to_statics;
|
|
|
|
}
|
|
|
|
|
2015-01-03 19:45:47 +05:30
|
|
|
/* Internal functions */
|
2005-10-28 16:51:40 +05:30
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
/* Divide the passwd/group/shadow record in fields
|
|
|
|
* by substituting the given delimeter
|
|
|
|
* e.g. ':' or ',' with '\0'.
|
2015-01-03 19:45:47 +05:30
|
|
|
* Returns the number of fields found.
|
2015-01-03 02:07:59 +05:30
|
|
|
* Strips leading and trailing whitespace in fields.
|
|
|
|
*/
|
|
|
|
static int tokenize(char *buffer, int ch)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
char *p = buffer;
|
|
|
|
char *s = p;
|
|
|
|
int num_fields = 0;
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
for (;;) {
|
|
|
|
if (isblank(*s)) {
|
|
|
|
overlapping_strcpy(s, skip_whitespace(s));
|
|
|
|
}
|
|
|
|
if (*p == ch || *p == '\0') {
|
|
|
|
char *end = p;
|
|
|
|
while (p != s && isblank(p[-1]))
|
|
|
|
p--;
|
|
|
|
if (p != end)
|
|
|
|
overlapping_strcpy(p, end);
|
|
|
|
num_fields++;
|
|
|
|
if (*end == '\0') {
|
|
|
|
S.tokenize_end = p + 1;
|
|
|
|
return num_fields;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
s = p + 1;
|
|
|
|
}
|
|
|
|
p++;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
/* Returns !NULL on success and matching line broken up in fields by '\0' in buf.
|
|
|
|
* We require the expected number of fields to be found.
|
|
|
|
*/
|
2015-01-04 06:32:39 +05:30
|
|
|
static char *parse_common(FILE *fp, struct passdb *db,
|
2015-01-03 02:07:59 +05:30
|
|
|
const char *key, int field_pos)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
while ((buf = xmalloc_fgetline(fp)) != NULL) {
|
|
|
|
/* Skip empty lines, comment lines */
|
|
|
|
if (buf[0] == '\0' || buf[0] == '#')
|
|
|
|
goto free_and_next;
|
2015-01-04 06:32:39 +05:30
|
|
|
if (tokenize(buf, ':') != db->numfields) {
|
2015-01-03 02:07:59 +05:30
|
|
|
/* number of fields is wrong */
|
2015-01-06 05:52:36 +05:30
|
|
|
bb_error_msg("%s: bad record", db->filename);
|
2015-01-03 02:07:59 +05:30
|
|
|
goto free_and_next;
|
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-05 19:39:04 +05:30
|
|
|
if (field_pos == -1) {
|
2015-01-03 02:07:59 +05:30
|
|
|
/* no key specified: sequential read, return a record */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strcmp(key, nth_string(buf, field_pos)) == 0) {
|
|
|
|
/* record found */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free_and_next:
|
|
|
|
free(buf);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2015-01-04 01:33:39 +05:30
|
|
|
S.string_size = S.tokenize_end - buf;
|
|
|
|
/*
|
|
|
|
* Ugly hack: group db requires additional buffer space
|
|
|
|
* for members[] array. If there is only one group, we need space
|
|
|
|
* for 3 pointers: alignment padding, group name, NULL.
|
|
|
|
* +1 for every additional group.
|
|
|
|
*/
|
2015-01-04 06:32:39 +05:30
|
|
|
if (buf && db->numfields == sizeof(GR_DEF)-1) { /* if we read group file... */
|
2015-01-04 01:33:39 +05:30
|
|
|
int cnt = 3;
|
|
|
|
char *p = buf;
|
|
|
|
while (p < S.tokenize_end)
|
|
|
|
if (*p++ == ',')
|
|
|
|
cnt++;
|
|
|
|
S.string_size += cnt * sizeof(char*);
|
|
|
|
//bb_error_msg("+%d words = %u key:%s buf:'%s'", cnt, S.string_size, key, buf);
|
|
|
|
buf = xrealloc(buf, S.string_size);
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
return buf;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2015-01-04 06:32:39 +05:30
|
|
|
static char *parse_file(struct passdb *db,
|
2015-01-03 02:07:59 +05:30
|
|
|
const char *key, int field_pos)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
char *buf = NULL;
|
2015-01-04 06:32:39 +05:30
|
|
|
FILE *fp = fopen_for_read(db->filename);
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
if (fp) {
|
2015-01-04 06:32:39 +05:30
|
|
|
buf = parse_common(fp, db, key, field_pos);
|
2015-01-03 02:07:59 +05:30
|
|
|
fclose(fp);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 02:07:59 +05:30
|
|
|
return buf;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
/* Convert passwd/group/shadow file record in buffer to a struct */
|
2015-01-03 20:24:04 +05:30
|
|
|
static void *convert_to_struct(struct passdb *db,
|
2015-01-03 02:07:59 +05:30
|
|
|
char *buffer, void *result)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 20:24:04 +05:30
|
|
|
const char *def = db->def;
|
|
|
|
const uint8_t *off = db->off;
|
|
|
|
|
2015-01-04 01:17:47 +05:30
|
|
|
/* For consistency, zero out all fields */
|
|
|
|
memset(result, 0, db->size_of);
|
2015-01-03 20:24:04 +05:30
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
for (;;) {
|
|
|
|
void *member = (char*)result + (*off++);
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
if ((*def | 0x20) == 's') { /* s or S */
|
|
|
|
*(char **)member = (char*)buffer;
|
|
|
|
if (!buffer[0] && (*def == 'S')) {
|
|
|
|
errno = EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*def == 'I') {
|
|
|
|
*(int *)member = bb_strtou(buffer, NULL, 10);
|
|
|
|
}
|
2006-12-29 03:03:30 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2015-01-03 02:07:59 +05:30
|
|
|
if (*def == 'l') {
|
|
|
|
long n = -1;
|
|
|
|
if (buffer[0])
|
|
|
|
n = bb_strtol(buffer, NULL, 10);
|
|
|
|
*(long *)member = n;
|
|
|
|
}
|
2007-03-13 18:31:14 +05:30
|
|
|
#endif
|
2015-01-03 02:07:59 +05:30
|
|
|
if (*def == 'm') {
|
|
|
|
char **members;
|
|
|
|
int i = tokenize(buffer, ',');
|
|
|
|
|
|
|
|
/* Store members[] after buffer's end.
|
|
|
|
* This is safe ONLY because there is a hack
|
|
|
|
* in parse_common() which allocates additional space
|
|
|
|
* at the end of malloced buffer!
|
|
|
|
*/
|
|
|
|
members = (char **)
|
2015-01-03 23:42:49 +05:30
|
|
|
( ((intptr_t)S.tokenize_end + sizeof(members[0]))
|
|
|
|
& -(intptr_t)sizeof(members[0])
|
2015-01-03 02:07:59 +05:30
|
|
|
);
|
|
|
|
((struct group *)result)->gr_mem = members;
|
|
|
|
while (--i >= 0) {
|
2015-01-04 01:46:18 +05:30
|
|
|
if (buffer[0]) {
|
|
|
|
*members++ = buffer;
|
|
|
|
// bb_error_msg("member[]='%s'", buffer);
|
|
|
|
}
|
2015-01-03 02:07:59 +05:30
|
|
|
buffer += strlen(buffer) + 1;
|
|
|
|
}
|
|
|
|
*members = NULL;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 02:07:59 +05:30
|
|
|
/* def "r" does nothing */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
def++;
|
2015-01-04 01:17:47 +05:30
|
|
|
if ((unsigned char)*def <= (unsigned char)' ')
|
2015-01-03 02:07:59 +05:30
|
|
|
break;
|
|
|
|
buffer += strlen(buffer) + 1;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
if (errno)
|
|
|
|
result = NULL;
|
2004-07-15 18:23:49 +05:30
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-04 07:04:52 +05:30
|
|
|
static int massage_data_for_r_func(struct passdb *db,
|
2015-01-03 20:24:04 +05:30
|
|
|
char *buffer, size_t buflen,
|
2015-01-04 07:04:52 +05:30
|
|
|
void **result,
|
|
|
|
char *buf)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-04 07:04:52 +05:30
|
|
|
void *result_buf = *result;
|
|
|
|
*result = NULL;
|
2015-01-03 02:07:59 +05:30
|
|
|
if (buf) {
|
2015-01-04 07:04:52 +05:30
|
|
|
if (S.string_size > buflen) {
|
2015-01-03 02:07:59 +05:30
|
|
|
errno = ERANGE;
|
|
|
|
} else {
|
2015-01-04 07:04:52 +05:30
|
|
|
memcpy(buffer, buf, S.string_size);
|
|
|
|
*result = convert_to_struct(db, buffer, result_buf);
|
2015-01-03 02:07:59 +05:30
|
|
|
}
|
|
|
|
free(buf);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 02:07:59 +05:30
|
|
|
/* "The reentrant functions return zero on success.
|
|
|
|
* In case of error, an error number is returned."
|
|
|
|
* NB: not finding the record is also a "success" here:
|
|
|
|
*/
|
|
|
|
return errno;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2015-02-08 01:51:02 +05:30
|
|
|
static void* massage_data_for_non_r_func(struct passdb *db, char *buf)
|
|
|
|
{
|
|
|
|
if (!buf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
free(db->malloced);
|
|
|
|
/* We enlarge buf and move string data up, freeing space
|
|
|
|
* for struct passwd/group/spwd at the beginning. This way,
|
|
|
|
* entire result of getXXnam is in a single malloced block.
|
|
|
|
* This enables easy creation of xmalloc_getpwnam() API.
|
|
|
|
*/
|
|
|
|
db->malloced = buf = xrealloc(buf, db->size_of + S.string_size);
|
|
|
|
memmove(buf + db->size_of, buf, S.string_size);
|
|
|
|
return convert_to_struct(db, buf + db->size_of, buf);
|
|
|
|
}
|
|
|
|
|
2015-01-04 07:04:52 +05:30
|
|
|
/****** getXXnam/id_r */
|
|
|
|
|
|
|
|
static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
void *result)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
|
|
|
|
|
|
|
|
buf = parse_file(db, name, 0 /*db_and_field_pos & 3*/);
|
|
|
|
/* "db_and_field_pos & 3" is commented out since so far we don't implement
|
|
|
|
* getXXXid_r() functions which would use that to pass 2 here */
|
|
|
|
|
|
|
|
return massage_data_for_r_func(db, buffer, buflen, result, buf);
|
|
|
|
}
|
|
|
|
|
2015-01-03 20:24:04 +05:30
|
|
|
int FAST_FUNC getpwnam_r(const char *name, struct passwd *struct_buf,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
struct passwd **result)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
/* Why the "store buffer address in result" trick?
|
|
|
|
* This way, getXXnam_r has the same ABI signature as getpwnam_r,
|
2015-01-03 03:01:07 +05:30
|
|
|
* hopefully compiler can optimize tail call better in this case.
|
2015-01-03 02:07:59 +05:30
|
|
|
*/
|
|
|
|
*result = struct_buf;
|
|
|
|
return getXXnam_r(name, (0 << 2) + 0, buffer, buflen, result);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 02:07:59 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2015-01-03 03:01:07 +05:30
|
|
|
int FAST_FUNC getspnam_r(const char *name, struct spwd *struct_buf, char *buffer, size_t buflen,
|
2015-01-03 19:45:47 +05:30
|
|
|
struct spwd **result)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
*result = struct_buf;
|
|
|
|
return getXXnam_r(name, (2 << 2) + 0, buffer, buflen, result);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-02-08 01:51:02 +05:30
|
|
|
#ifdef UNUSED
|
2015-01-03 02:07:59 +05:30
|
|
|
/****** getXXent_r */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-04 07:04:52 +05:30
|
|
|
static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen,
|
|
|
|
void *result)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
char *buf;
|
2015-01-04 06:32:39 +05:30
|
|
|
struct passdb *db = &get_S()->db[db_idx];
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
if (!db->fp) {
|
|
|
|
db->fp = fopen_for_read(db->filename);
|
|
|
|
if (!db->fp) {
|
|
|
|
return errno;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 02:07:59 +05:30
|
|
|
close_on_exec_on(fileno(db->fp));
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2015-01-05 19:39:04 +05:30
|
|
|
buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
|
|
|
|
if (!buf && !errno)
|
|
|
|
errno = ENOENT;
|
2015-01-04 07:04:52 +05:30
|
|
|
return massage_data_for_r_func(db, buffer, buflen, result, buf);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2015-01-04 07:04:52 +05:30
|
|
|
int FAST_FUNC getpwent_r(struct passwd *struct_buf, char *buffer, size_t buflen,
|
|
|
|
struct passwd **result)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-04 07:04:52 +05:30
|
|
|
*result = struct_buf;
|
|
|
|
return getXXent_r(0, buffer, buflen, result);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-02-08 01:51:02 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
/****** getXXent */
|
|
|
|
|
|
|
|
static void* FAST_FUNC getXXent(uintptr_t db_idx)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
struct passdb *db = &get_S()->db[db_idx];
|
|
|
|
|
|
|
|
if (!db->fp) {
|
|
|
|
db->fp = fopen_for_read(db->filename);
|
|
|
|
if (!db->fp) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
close_on_exec_on(fileno(db->fp));
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
|
|
|
|
return massage_data_for_non_r_func(db, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct passwd* FAST_FUNC getpwent(void)
|
|
|
|
{
|
|
|
|
return getXXent(0);
|
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
/****** getXXnam/id */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 03:01:07 +05:30
|
|
|
static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
char *buf;
|
2015-01-04 06:32:39 +05:30
|
|
|
struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-02-20 02:32:59 +05:30
|
|
|
buf = parse_file(db, name, db_and_field_pos & 3);
|
2015-02-08 01:51:02 +05:30
|
|
|
return massage_data_for_non_r_func(db, buf);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2015-01-03 03:01:07 +05:30
|
|
|
struct passwd* FAST_FUNC getpwnam(const char *name)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
return getXXnam(name, (0 << 2) + 0);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 03:01:07 +05:30
|
|
|
struct group* FAST_FUNC getgrnam(const char *name)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
return getXXnam(name, (1 << 2) + 0);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 03:01:07 +05:30
|
|
|
struct passwd* FAST_FUNC getpwuid(uid_t id)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
return getXXnam(utoa(id), (0 << 2) + 2);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 03:01:07 +05:30
|
|
|
struct group* FAST_FUNC getgrgid(gid_t id)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
return getXXnam(utoa(id), (1 << 2) + 2);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
/****** end/setXXend */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 03:01:07 +05:30
|
|
|
void FAST_FUNC endpwent(void)
|
2015-01-03 02:07:59 +05:30
|
|
|
{
|
|
|
|
if (has_S && S.db[0].fp) {
|
|
|
|
fclose(S.db[0].fp);
|
|
|
|
S.db[0].fp = NULL;
|
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 03:01:07 +05:30
|
|
|
void FAST_FUNC setpwent(void)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
if (has_S && S.db[0].fp) {
|
|
|
|
rewind(S.db[0].fp);
|
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 03:01:07 +05:30
|
|
|
void FAST_FUNC endgrent(void)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
if (has_S && S.db[1].fp) {
|
|
|
|
fclose(S.db[1].fp);
|
|
|
|
S.db[1].fp = NULL;
|
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
/****** initgroups and getgrouplist */
|
|
|
|
|
|
|
|
static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
|
|
|
|
const char *user, gid_t gid)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2015-01-03 02:07:59 +05:30
|
|
|
FILE *fp;
|
2004-07-15 18:23:49 +05:30
|
|
|
gid_t *group_list;
|
2008-09-18 06:26:24 +05:30
|
|
|
int ngroups;
|
2015-01-03 02:07:59 +05:30
|
|
|
|
2008-09-18 06:26:24 +05:30
|
|
|
/* We alloc space for 8 gids at a time. */
|
2015-01-03 19:45:47 +05:30
|
|
|
group_list = xzalloc(8 * sizeof(group_list[0]));
|
2008-09-18 06:26:24 +05:30
|
|
|
group_list[0] = gid;
|
|
|
|
ngroups = 1;
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 02:07:59 +05:30
|
|
|
fp = fopen_for_read(_PATH_GROUP);
|
|
|
|
if (fp) {
|
2015-01-04 06:32:39 +05:30
|
|
|
struct passdb *db = &get_S()->db[1];
|
2015-01-03 02:07:59 +05:30
|
|
|
char *buf;
|
2015-01-05 19:39:04 +05:30
|
|
|
while ((buf = parse_common(fp, db, NULL, -1)) != NULL) {
|
2008-09-18 06:26:24 +05:30
|
|
|
char **m;
|
2015-01-03 02:07:59 +05:30
|
|
|
struct group group;
|
2015-01-04 06:32:39 +05:30
|
|
|
if (!convert_to_struct(db, buf, &group))
|
2015-01-03 02:07:59 +05:30
|
|
|
goto next;
|
2008-09-18 06:26:24 +05:30
|
|
|
if (group.gr_gid == gid)
|
2015-01-03 02:07:59 +05:30
|
|
|
goto next;
|
2008-09-18 06:26:24 +05:30
|
|
|
for (m = group.gr_mem; *m; m++) {
|
|
|
|
if (strcmp(*m, user) != 0)
|
|
|
|
continue;
|
2010-03-31 16:07:43 +05:30
|
|
|
group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
|
2008-09-18 06:26:24 +05:30
|
|
|
group_list[ngroups++] = group.gr_gid;
|
2015-01-05 19:39:04 +05:30
|
|
|
goto next;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 02:07:59 +05:30
|
|
|
next:
|
|
|
|
free(buf);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2015-01-03 02:07:59 +05:30
|
|
|
fclose(fp);
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2008-09-18 06:26:24 +05:30
|
|
|
*ngroups_ptr = ngroups;
|
|
|
|
return group_list;
|
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2015-01-03 03:01:07 +05:30
|
|
|
int FAST_FUNC initgroups(const char *user, gid_t gid)
|
2008-09-18 06:26:24 +05:30
|
|
|
{
|
|
|
|
int ngroups;
|
|
|
|
gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
|
|
|
|
|
|
|
|
ngroups = setgroups(ngroups, group_list);
|
|
|
|
free(group_list);
|
|
|
|
return ngroups;
|
|
|
|
}
|
|
|
|
|
2015-01-03 03:01:07 +05:30
|
|
|
int FAST_FUNC getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
|
2008-09-18 06:26:24 +05:30
|
|
|
{
|
|
|
|
int ngroups_old = *ngroups;
|
|
|
|
gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
|
|
|
|
|
|
|
|
if (*ngroups <= ngroups_old) {
|
|
|
|
ngroups_old = *ngroups;
|
|
|
|
memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
|
|
|
|
} else {
|
|
|
|
ngroups_old = -1;
|
|
|
|
}
|
|
|
|
free(group_list);
|
|
|
|
return ngroups_old;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|