2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2010-08-16 23:44:46 +05:30
|
|
|
/* Copyright (C) 2003 Manuel Novoa III
|
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
|
|
|
*/
|
|
|
|
|
2010-08-16 23:44:46 +05:30
|
|
|
/* Nov 6, 2003 Initial version.
|
2004-07-15 18:23:49 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* NOTE: This implementation is quite strict about requiring all
|
2004-07-15 18:23:49 +05:30
|
|
|
* field seperators. It also does not allow leading whitespace
|
|
|
|
* except when processing the numeric fields. glibc is more
|
|
|
|
* lenient. See the various glibc difference comments below.
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* TODO:
|
2006-03-04 00:32:50 +05:30
|
|
|
* Move to dynamic allocation of (currently statically allocated)
|
2004-07-15 18:23:49 +05:30
|
|
|
* buffers; especially for the group-related functions since
|
|
|
|
* large group member lists will cause error returns.
|
|
|
|
*/
|
|
|
|
|
2006-06-19 01:50:07 +05:30
|
|
|
#include "libbb.h"
|
2004-07-15 18:23:49 +05:30
|
|
|
#include <assert.h>
|
2006-04-05 21:51:37 +05:30
|
|
|
|
2004-07-15 18:23:49 +05:30
|
|
|
/**********************************************************************/
|
2006-03-04 00:32:50 +05:30
|
|
|
/* Sizes for statically allocated buffers. */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
#define PWD_BUFFER_SIZE 256
|
|
|
|
#define GRP_BUFFER_SIZE 256
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* Prototypes for internal functions. */
|
|
|
|
|
2010-03-31 16:07:43 +05:30
|
|
|
static int bb__pgsreader(
|
|
|
|
int FAST_FUNC (*parserfunc)(void *d, char *line),
|
|
|
|
void *data,
|
|
|
|
char *__restrict line_buff,
|
|
|
|
size_t buflen,
|
|
|
|
FILE *f);
|
|
|
|
|
|
|
|
static int FAST_FUNC bb__parsepwent(void *pw, char *line);
|
|
|
|
static int FAST_FUNC bb__parsegrent(void *gr, char *line);
|
2006-12-29 03:03:30 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2010-03-31 16:07:43 +05:30
|
|
|
static int FAST_FUNC bb__parsespent(void *sp, char *line);
|
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 {
|
|
|
|
/* Smaller things first */
|
2012-01-06 20:54:56 +05:30
|
|
|
/* It's ok to use one buffer for getpwuid and getpwnam. Manpage says:
|
|
|
|
* "The return value may point to a static area, and may be overwritten
|
|
|
|
* by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
|
|
|
|
*/
|
|
|
|
struct passwd getpw_resultbuf;
|
|
|
|
struct group getgr_resultbuf;
|
|
|
|
|
|
|
|
char getpw_buffer[PWD_BUFFER_SIZE];
|
|
|
|
char getgr_buffer[GRP_BUFFER_SIZE];
|
2007-06-18 15:38:27 +05:30
|
|
|
#if 0 //ENABLE_USE_BB_SHADOW
|
2012-01-06 20:54:56 +05:30
|
|
|
struct spwd getsp_resultbuf;
|
|
|
|
char getsp_buffer[PWD_BUFFER_SIZE];
|
2007-06-18 15:38:27 +05:30
|
|
|
#endif
|
|
|
|
// Not converted - too small to bother
|
|
|
|
//pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
//FILE *pwf /*= NULL*/;
|
|
|
|
//FILE *grf /*= NULL*/;
|
|
|
|
//FILE *spf /*= NULL*/;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct statics *ptr_to_statics;
|
|
|
|
|
|
|
|
static struct statics *get_S(void)
|
|
|
|
{
|
|
|
|
if (!ptr_to_statics)
|
|
|
|
ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
|
|
|
|
return ptr_to_statics;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Always use in this order, get_S() must be called first */
|
|
|
|
#define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
|
|
|
|
#define BUFFER(name) (S->name##_buffer)
|
|
|
|
|
2004-07-15 18:23:49 +05:30
|
|
|
/**********************************************************************/
|
|
|
|
/* For the various fget??ent_r funcs, return
|
|
|
|
*
|
|
|
|
* 0: success
|
|
|
|
* ENOENT: end-of-file encountered
|
|
|
|
* ERANGE: buflen too small
|
2006-12-31 02:41:57 +05:30
|
|
|
* other error values possible. See bb__pgsreader.
|
2004-07-15 18:23:49 +05:30
|
|
|
*
|
|
|
|
* Also, *result == resultbuf on success and NULL on failure.
|
|
|
|
*
|
|
|
|
* NOTE: glibc difference - For the ENOENT case, glibc also sets errno.
|
|
|
|
* We do not, as it really isn't an error if we reach the end-of-file.
|
|
|
|
* Doing so is analogous to having fgetc() set errno on EOF.
|
|
|
|
*/
|
|
|
|
/**********************************************************************/
|
2005-10-28 16:51:40 +05:30
|
|
|
|
2004-07-15 18:23:49 +05:30
|
|
|
int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
|
|
|
|
char *__restrict buffer, size_t buflen,
|
|
|
|
struct passwd **__restrict result)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (!rv) {
|
2004-07-15 18:23:49 +05:30
|
|
|
*result = resultbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
|
|
|
|
char *__restrict buffer, size_t buflen,
|
|
|
|
struct group **__restrict result)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (!rv) {
|
2004-07-15 18:23:49 +05:30
|
|
|
*result = resultbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2006-12-29 03:03:30 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2010-03-31 18:13:58 +05:30
|
|
|
#ifdef UNUSED_FOR_NOW
|
2004-07-15 18:23:49 +05:30
|
|
|
int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
|
|
|
|
char *__restrict buffer, size_t buflen,
|
|
|
|
struct spwd **__restrict result)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (!rv) {
|
2004-07-15 18:23:49 +05:30
|
|
|
*result = resultbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
2010-03-31 18:13:58 +05:30
|
|
|
#endif
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* For the various fget??ent funcs, return NULL on failure and a
|
2006-03-04 00:32:50 +05:30
|
|
|
* pointer to the appropriate struct (statically allocated) on success.
|
2007-03-13 18:31:14 +05:30
|
|
|
* TODO: audit & stop using these in bbox, they pull in static buffers */
|
2004-07-15 18:23:49 +05:30
|
|
|
/**********************************************************************/
|
|
|
|
|
2010-03-31 16:08:17 +05:30
|
|
|
#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
|
2004-07-15 18:23:49 +05:30
|
|
|
struct passwd *fgetpwent(FILE *stream)
|
|
|
|
{
|
2007-06-18 15:38:27 +05:30
|
|
|
struct statics *S;
|
2012-01-06 20:54:56 +05:30
|
|
|
struct passwd *resultbuf = RESULTBUF(getpw);
|
|
|
|
char *buffer = BUFFER(getpw);
|
2004-07-15 18:23:49 +05:30
|
|
|
struct passwd *result;
|
|
|
|
|
2012-01-06 20:54:56 +05:30
|
|
|
fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
|
2004-07-15 18:23:49 +05:30
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct group *fgetgrent(FILE *stream)
|
|
|
|
{
|
2007-06-18 15:38:27 +05:30
|
|
|
struct statics *S;
|
2012-01-06 20:54:56 +05:30
|
|
|
struct group *resultbuf = RESULTBUF(getgr);
|
|
|
|
char *buffer = BUFFER(getgr);
|
2004-07-15 18:23:49 +05:30
|
|
|
struct group *result;
|
|
|
|
|
2012-01-06 20:54:56 +05:30
|
|
|
fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
|
2004-07-15 18:23:49 +05:30
|
|
|
return result;
|
|
|
|
}
|
2007-03-13 18:31:14 +05:30
|
|
|
#endif
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2006-12-29 03:03:30 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2010-03-31 18:13:58 +05:30
|
|
|
#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
|
2004-07-15 18:23:49 +05:30
|
|
|
struct spwd *fgetspent(FILE *stream)
|
|
|
|
{
|
2007-06-18 15:38:27 +05:30
|
|
|
struct statics *S;
|
2012-01-06 20:54:56 +05:30
|
|
|
struct spwd *resultbuf = RESULTBUF(getsp);
|
|
|
|
char *buffer = BUFFER(getsp);
|
2004-07-15 18:23:49 +05:30
|
|
|
struct spwd *result;
|
|
|
|
|
2012-01-06 20:54:56 +05:30
|
|
|
fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
|
2004-07-15 18:23:49 +05:30
|
|
|
return result;
|
|
|
|
}
|
2007-03-13 18:31:14 +05:30
|
|
|
#endif
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2010-03-31 18:13:58 +05:30
|
|
|
#ifdef UNUSED_FOR_NOW
|
2004-07-15 18:23:49 +05:30
|
|
|
int sgetspent_r(const char *string, struct spwd *result_buf,
|
|
|
|
char *buffer, size_t buflen, struct spwd **result)
|
|
|
|
{
|
|
|
|
int rv = ERANGE;
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
|
|
|
if (buflen < PWD_BUFFER_SIZE) {
|
2010-03-31 14:01:51 +05:30
|
|
|
DO_ERANGE:
|
2010-03-31 16:07:43 +05:30
|
|
|
errno = rv;
|
2004-07-15 18:23:49 +05:30
|
|
|
goto DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string != buffer) {
|
|
|
|
if (strlen(string) >= buflen) {
|
|
|
|
goto DO_ERANGE;
|
|
|
|
}
|
|
|
|
strcpy(buffer, string);
|
|
|
|
}
|
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
rv = bb__parsespent(result_buf, buffer);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (!rv) {
|
2004-07-15 18:23:49 +05:30
|
|
|
*result = result_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
DONE:
|
|
|
|
return rv;
|
|
|
|
}
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
2010-03-31 18:13:58 +05:30
|
|
|
#endif /* ENABLE_USE_BB_SHADOW */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
#define GETXXKEY_R_FUNC getpwnam_r
|
|
|
|
#define GETXXKEY_R_PARSER bb__parsepwent
|
2006-12-29 03:03:30 +05:30
|
|
|
#define GETXXKEY_R_ENTTYPE struct passwd
|
|
|
|
#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
|
2006-12-31 02:41:57 +05:30
|
|
|
#define GETXXKEY_R_KEYTYPE const char *__restrict
|
|
|
|
#define GETXXKEY_R_PATHNAME _PATH_PASSWD
|
2005-10-28 16:51:40 +05:30
|
|
|
#include "pwd_grp_internal.c"
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
#define GETXXKEY_R_FUNC getgrnam_r
|
|
|
|
#define GETXXKEY_R_PARSER bb__parsegrent
|
2006-12-29 03:03:30 +05:30
|
|
|
#define GETXXKEY_R_ENTTYPE struct group
|
|
|
|
#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
|
2006-12-31 02:41:57 +05:30
|
|
|
#define GETXXKEY_R_KEYTYPE const char *__restrict
|
|
|
|
#define GETXXKEY_R_PATHNAME _PATH_GROUP
|
2005-10-28 16:51:40 +05:30
|
|
|
#include "pwd_grp_internal.c"
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2006-12-29 03:03:30 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2006-12-31 02:41:57 +05:30
|
|
|
#define GETXXKEY_R_FUNC getspnam_r
|
|
|
|
#define GETXXKEY_R_PARSER bb__parsespent
|
2006-12-29 03:03:30 +05:30
|
|
|
#define GETXXKEY_R_ENTTYPE struct spwd
|
|
|
|
#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
|
2006-12-31 02:41:57 +05:30
|
|
|
#define GETXXKEY_R_KEYTYPE const char *__restrict
|
|
|
|
#define GETXXKEY_R_PATHNAME _PATH_SHADOW
|
2005-10-28 16:51:40 +05:30
|
|
|
#include "pwd_grp_internal.c"
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
#define GETXXKEY_R_FUNC getpwuid_r
|
|
|
|
#define GETXXKEY_R_PARSER bb__parsepwent
|
2006-12-29 03:03:30 +05:30
|
|
|
#define GETXXKEY_R_ENTTYPE struct passwd
|
|
|
|
#define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
|
2006-12-31 02:41:57 +05:30
|
|
|
#define GETXXKEY_R_KEYTYPE uid_t
|
|
|
|
#define GETXXKEY_R_PATHNAME _PATH_PASSWD
|
2005-10-28 16:51:40 +05:30
|
|
|
#include "pwd_grp_internal.c"
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
#define GETXXKEY_R_FUNC getgrgid_r
|
|
|
|
#define GETXXKEY_R_PARSER bb__parsegrent
|
2006-12-29 03:03:30 +05:30
|
|
|
#define GETXXKEY_R_ENTTYPE struct group
|
|
|
|
#define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
|
2006-12-31 02:41:57 +05:30
|
|
|
#define GETXXKEY_R_KEYTYPE gid_t
|
|
|
|
#define GETXXKEY_R_PATHNAME _PATH_GROUP
|
2005-10-28 16:51:40 +05:30
|
|
|
#include "pwd_grp_internal.c"
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
/**********************************************************************/
|
2007-03-13 18:31:14 +05:30
|
|
|
/* TODO: audit & stop using these in bbox, they pull in static buffers */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2007-03-13 18:31:14 +05:30
|
|
|
/* This one has many users */
|
2004-07-15 18:23:49 +05:30
|
|
|
struct passwd *getpwuid(uid_t uid)
|
|
|
|
{
|
2007-06-18 15:38:27 +05:30
|
|
|
struct statics *S;
|
2012-01-06 20:54:56 +05:30
|
|
|
struct passwd *resultbuf = RESULTBUF(getpw);
|
|
|
|
char *buffer = BUFFER(getpw);
|
2004-07-15 18:23:49 +05:30
|
|
|
struct passwd *result;
|
|
|
|
|
2012-01-06 20:54:56 +05:30
|
|
|
getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
|
2004-07-15 18:23:49 +05:30
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-03-13 18:31:14 +05:30
|
|
|
/* This one has many users */
|
2004-07-15 18:23:49 +05:30
|
|
|
struct group *getgrgid(gid_t gid)
|
|
|
|
{
|
2007-06-18 15:38:27 +05:30
|
|
|
struct statics *S;
|
2012-01-06 20:54:56 +05:30
|
|
|
struct group *resultbuf = RESULTBUF(getgr);
|
|
|
|
char *buffer = BUFFER(getgr);
|
2004-07-15 18:23:49 +05:30
|
|
|
struct group *result;
|
|
|
|
|
2012-01-06 20:54:56 +05:30
|
|
|
getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
|
2004-07-15 18:23:49 +05:30
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-12-29 03:03:30 +05:30
|
|
|
#if 0 //ENABLE_USE_BB_SHADOW
|
2004-07-15 18:23:49 +05:30
|
|
|
/* This function is non-standard and is currently not built. It seems
|
|
|
|
* to have been created as a reentrant version of the non-standard
|
|
|
|
* functions getspuid. Why getspuid was added, I do not know. */
|
|
|
|
int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
|
|
|
|
char *__restrict buffer, size_t buflen,
|
|
|
|
struct spwd **__restrict result)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
struct passwd *pp;
|
|
|
|
struct passwd password;
|
|
|
|
char pwd_buff[PWD_BUFFER_SIZE];
|
|
|
|
|
|
|
|
*result = NULL;
|
2006-12-29 03:03:30 +05:30
|
|
|
rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
|
|
|
|
if (!rv) {
|
2004-07-15 18:23:49 +05:30
|
|
|
rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function is non-standard and is currently not built.
|
|
|
|
* Why it was added, I do not know. */
|
|
|
|
struct spwd *getspuid(uid_t uid)
|
|
|
|
{
|
2007-06-18 15:38:27 +05:30
|
|
|
struct statics *S;
|
2012-01-06 20:54:56 +05:30
|
|
|
struct spwd *resultbuf = RESULTBUF(getsp);
|
|
|
|
char *buffer = BUFFER(getsp);
|
2004-07-15 18:23:49 +05:30
|
|
|
struct spwd *result;
|
|
|
|
|
2012-01-06 20:54:56 +05:30
|
|
|
getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
|
2004-07-15 18:23:49 +05:30
|
|
|
return result;
|
|
|
|
}
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2007-03-13 18:31:14 +05:30
|
|
|
/* This one has many users */
|
2004-07-15 18:23:49 +05:30
|
|
|
struct passwd *getpwnam(const char *name)
|
|
|
|
{
|
2007-06-18 15:38:27 +05:30
|
|
|
struct statics *S;
|
2012-01-06 20:54:56 +05:30
|
|
|
struct passwd *resultbuf = RESULTBUF(getpw);
|
|
|
|
char *buffer = BUFFER(getpw);
|
2004-07-15 18:23:49 +05:30
|
|
|
struct passwd *result;
|
|
|
|
|
2012-01-06 20:54:56 +05:30
|
|
|
getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
|
2004-07-15 18:23:49 +05:30
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-03-13 18:31:14 +05:30
|
|
|
/* This one has many users */
|
2004-07-15 18:23:49 +05:30
|
|
|
struct group *getgrnam(const char *name)
|
|
|
|
{
|
2007-06-18 15:38:27 +05:30
|
|
|
struct statics *S;
|
2012-01-06 20:54:56 +05:30
|
|
|
struct group *resultbuf = RESULTBUF(getgr);
|
|
|
|
char *buffer = BUFFER(getgr);
|
2004-07-15 18:23:49 +05:30
|
|
|
struct group *result;
|
|
|
|
|
2012-01-06 20:54:56 +05:30
|
|
|
getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
|
2004-07-15 18:23:49 +05:30
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-03-13 18:31:14 +05:30
|
|
|
#if 0 //ENABLE_USE_BB_SHADOW
|
2004-07-15 18:23:49 +05:30
|
|
|
struct spwd *getspnam(const char *name)
|
|
|
|
{
|
2007-06-18 15:38:27 +05:30
|
|
|
struct statics *S;
|
2012-01-06 20:54:56 +05:30
|
|
|
struct spwd *resultbuf = RESULTBUF(getsp);
|
|
|
|
char *buffer = BUFFER(getsp);
|
2004-07-15 18:23:49 +05:30
|
|
|
struct spwd *result;
|
|
|
|
|
2012-01-06 20:54:56 +05:30
|
|
|
getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
|
2004-07-15 18:23:49 +05:30
|
|
|
return result;
|
|
|
|
}
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
/* FIXME: we don't have such CONFIG_xx - ?! */
|
|
|
|
|
2005-10-28 16:51:40 +05:30
|
|
|
#if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
|
|
|
|
static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
# define LOCK pthread_mutex_lock(&mylock)
|
|
|
|
# define UNLOCK pthread_mutex_unlock(&mylock);
|
|
|
|
#else
|
|
|
|
# define LOCK ((void) 0)
|
|
|
|
# define UNLOCK ((void) 0)
|
|
|
|
#endif
|
|
|
|
|
2004-07-15 18:23:49 +05:30
|
|
|
static FILE *pwf /*= NULL*/;
|
|
|
|
void setpwent(void)
|
|
|
|
{
|
2005-10-28 16:51:40 +05:30
|
|
|
LOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
if (pwf) {
|
|
|
|
rewind(pwf);
|
|
|
|
}
|
2005-10-28 16:51:40 +05:30
|
|
|
UNLOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void endpwent(void)
|
|
|
|
{
|
2005-10-28 16:51:40 +05:30
|
|
|
LOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
if (pwf) {
|
|
|
|
fclose(pwf);
|
|
|
|
pwf = NULL;
|
|
|
|
}
|
2005-10-28 16:51:40 +05:30
|
|
|
UNLOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-25 05:38:53 +05:30
|
|
|
int getpwent_r(struct passwd *__restrict resultbuf,
|
2004-07-15 18:23:49 +05:30
|
|
|
char *__restrict buffer, size_t buflen,
|
|
|
|
struct passwd **__restrict result)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2005-10-28 16:51:40 +05:30
|
|
|
LOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
*result = NULL; /* In case of error... */
|
|
|
|
|
|
|
|
if (!pwf) {
|
2008-07-22 04:35:26 +05:30
|
|
|
pwf = fopen_for_read(_PATH_PASSWD);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (!pwf) {
|
2004-07-15 18:23:49 +05:30
|
|
|
rv = errno;
|
|
|
|
goto ERR;
|
|
|
|
}
|
2012-01-06 20:53:42 +05:30
|
|
|
close_on_exec_on(fileno(pwf));
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (!rv) {
|
2004-07-15 18:23:49 +05:30
|
|
|
*result = resultbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR:
|
2005-10-28 16:51:40 +05:30
|
|
|
UNLOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *grf /*= NULL*/;
|
|
|
|
void setgrent(void)
|
|
|
|
{
|
2005-10-28 16:51:40 +05:30
|
|
|
LOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
if (grf) {
|
|
|
|
rewind(grf);
|
|
|
|
}
|
2005-10-28 16:51:40 +05:30
|
|
|
UNLOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void endgrent(void)
|
|
|
|
{
|
2005-10-28 16:51:40 +05:30
|
|
|
LOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
if (grf) {
|
|
|
|
fclose(grf);
|
|
|
|
grf = NULL;
|
|
|
|
}
|
2005-10-28 16:51:40 +05:30
|
|
|
UNLOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int getgrent_r(struct group *__restrict resultbuf,
|
|
|
|
char *__restrict buffer, size_t buflen,
|
|
|
|
struct group **__restrict result)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2005-10-28 16:51:40 +05:30
|
|
|
LOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
*result = NULL; /* In case of error... */
|
|
|
|
|
|
|
|
if (!grf) {
|
2008-07-22 04:35:26 +05:30
|
|
|
grf = fopen_for_read(_PATH_GROUP);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (!grf) {
|
2004-07-15 18:23:49 +05:30
|
|
|
rv = errno;
|
|
|
|
goto ERR;
|
|
|
|
}
|
2012-01-06 20:53:42 +05:30
|
|
|
close_on_exec_on(fileno(grf));
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (!rv) {
|
2004-07-15 18:23:49 +05:30
|
|
|
*result = resultbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR:
|
2005-10-28 16:51:40 +05:30
|
|
|
UNLOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-03-31 18:13:58 +05:30
|
|
|
#ifdef UNUSED_FOR_NOW
|
2006-12-29 03:03:30 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2004-07-15 18:23:49 +05:30
|
|
|
static FILE *spf /*= NULL*/;
|
|
|
|
void setspent(void)
|
|
|
|
{
|
2005-10-28 16:51:40 +05:30
|
|
|
LOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
if (spf) {
|
|
|
|
rewind(spf);
|
|
|
|
}
|
2005-10-28 16:51:40 +05:30
|
|
|
UNLOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void endspent(void)
|
|
|
|
{
|
2005-10-28 16:51:40 +05:30
|
|
|
LOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
if (spf) {
|
|
|
|
fclose(spf);
|
|
|
|
spf = NULL;
|
|
|
|
}
|
2005-10-28 16:51:40 +05:30
|
|
|
UNLOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2006-01-25 05:38:53 +05:30
|
|
|
int getspent_r(struct spwd *resultbuf, char *buffer,
|
2004-07-15 18:23:49 +05:30
|
|
|
size_t buflen, struct spwd **result)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2005-10-28 16:51:40 +05:30
|
|
|
LOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
*result = NULL; /* In case of error... */
|
|
|
|
|
|
|
|
if (!spf) {
|
2008-07-22 04:35:26 +05:30
|
|
|
spf = fopen_for_read(_PATH_SHADOW);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (!spf) {
|
2004-07-15 18:23:49 +05:30
|
|
|
rv = errno;
|
|
|
|
goto ERR;
|
|
|
|
}
|
2012-01-06 20:53:42 +05:30
|
|
|
close_on_exec_on(fileno(spf));
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
2006-12-31 02:41:57 +05:30
|
|
|
rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (!rv) {
|
2004-07-15 18:23:49 +05:30
|
|
|
*result = resultbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR:
|
2005-10-28 16:51:40 +05:30
|
|
|
UNLOCK;
|
2004-07-15 18:23:49 +05:30
|
|
|
return rv;
|
|
|
|
}
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
2010-03-31 18:13:58 +05:30
|
|
|
#endif /* UNUSED_FOR_NOW */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2010-03-31 16:08:17 +05:30
|
|
|
#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
|
2004-07-15 18:23:49 +05:30
|
|
|
struct passwd *getpwent(void)
|
|
|
|
{
|
|
|
|
static char line_buff[PWD_BUFFER_SIZE];
|
|
|
|
static struct passwd pwd;
|
|
|
|
struct passwd *result;
|
|
|
|
|
|
|
|
getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct group *getgrent(void)
|
|
|
|
{
|
|
|
|
static char line_buff[GRP_BUFFER_SIZE];
|
|
|
|
static struct group gr;
|
|
|
|
struct group *result;
|
|
|
|
|
|
|
|
getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-03-31 18:13:58 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2004-07-15 18:23:49 +05:30
|
|
|
struct spwd *getspent(void)
|
|
|
|
{
|
|
|
|
static char line_buff[PWD_BUFFER_SIZE];
|
|
|
|
static struct spwd spwd;
|
|
|
|
struct spwd *result;
|
|
|
|
|
|
|
|
getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct spwd *sgetspent(const char *string)
|
|
|
|
{
|
|
|
|
static char line_buff[PWD_BUFFER_SIZE];
|
|
|
|
static struct spwd spwd;
|
|
|
|
struct spwd *result;
|
|
|
|
|
|
|
|
sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
|
|
|
|
return result;
|
|
|
|
}
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
2010-03-31 18:13:58 +05:30
|
|
|
#endif /* UNUSED_SINCE_WE_AVOID_STATIC_BUFS */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2008-09-18 06:26:24 +05:30
|
|
|
static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
2005-10-28 16:51:40 +05:30
|
|
|
FILE *grfile;
|
2004-07-15 18:23:49 +05:30
|
|
|
gid_t *group_list;
|
2008-09-18 06:26:24 +05:30
|
|
|
int ngroups;
|
2004-07-15 18:23:49 +05:30
|
|
|
struct group group;
|
|
|
|
char buff[PWD_BUFFER_SIZE];
|
|
|
|
|
2008-09-18 06:26:24 +05:30
|
|
|
/* We alloc space for 8 gids at a time. */
|
|
|
|
group_list = xmalloc(8 * sizeof(group_list[0]));
|
|
|
|
group_list[0] = gid;
|
|
|
|
ngroups = 1;
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2008-09-18 06:26:24 +05:30
|
|
|
grfile = fopen_for_read(_PATH_GROUP);
|
|
|
|
if (grfile) {
|
2006-12-31 02:41:57 +05:30
|
|
|
while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
|
2008-09-18 06:26:24 +05:30
|
|
|
char **m;
|
2004-07-15 18:23:49 +05:30
|
|
|
assert(group.gr_mem); /* Must have at least a NULL terminator. */
|
2008-09-18 06:26:24 +05:30
|
|
|
if (group.gr_gid == gid)
|
|
|
|
continue;
|
|
|
|
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;
|
|
|
|
break;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
}
|
2005-10-28 16:51:40 +05:30
|
|
|
fclose(grfile);
|
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
|
|
|
|
2008-09-18 06:26:24 +05:30
|
|
|
int initgroups(const char *user, gid_t gid)
|
|
|
|
{
|
|
|
|
int ngroups;
|
|
|
|
gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
|
|
|
|
|
|
|
|
ngroups = setgroups(ngroups, group_list);
|
|
|
|
free(group_list);
|
|
|
|
return ngroups;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-03-31 16:08:17 +05:30
|
|
|
#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
|
2004-07-15 18:23:49 +05:30
|
|
|
int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
|
|
|
|
{
|
|
|
|
int rv = -1;
|
|
|
|
|
2010-03-31 18:13:58 +05:30
|
|
|
#if 0
|
|
|
|
/* glibc does this check */
|
2004-07-15 18:23:49 +05:30
|
|
|
if (!p || !f) {
|
2008-03-20 20:42:58 +05:30
|
|
|
errno = EINVAL;
|
2010-03-31 16:07:43 +05:30
|
|
|
return rv;
|
|
|
|
}
|
2010-03-31 18:13:58 +05:30
|
|
|
#endif
|
2010-03-31 16:07:43 +05:30
|
|
|
|
|
|
|
/* No extra thread locking is needed above what fprintf does. */
|
|
|
|
if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
|
|
|
|
p->pw_name, p->pw_passwd,
|
|
|
|
(unsigned long)(p->pw_uid),
|
|
|
|
(unsigned long)(p->pw_gid),
|
|
|
|
p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
|
|
|
|
) {
|
|
|
|
rv = 0;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int putgrent(const struct group *__restrict p, FILE *__restrict f)
|
|
|
|
{
|
|
|
|
int rv = -1;
|
|
|
|
|
2010-03-31 18:13:58 +05:30
|
|
|
#if 0
|
|
|
|
/* glibc does this check */
|
|
|
|
if (!p || !f) {
|
2008-03-20 20:42:58 +05:30
|
|
|
errno = EINVAL;
|
2010-03-31 16:07:43 +05:30
|
|
|
return rv;
|
|
|
|
}
|
2010-03-31 18:13:58 +05:30
|
|
|
#endif
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2010-03-31 16:07:43 +05:30
|
|
|
if (fprintf(f, "%s:%s:%lu:",
|
|
|
|
p->gr_name, p->gr_passwd,
|
|
|
|
(unsigned long)(p->gr_gid)) >= 0
|
|
|
|
) {
|
|
|
|
static const char format[] ALIGN1 = ",%s";
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2010-03-31 16:07:43 +05:30
|
|
|
char **m;
|
|
|
|
const char *fmt;
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2010-03-31 16:07:43 +05:30
|
|
|
fmt = format + 1;
|
|
|
|
|
|
|
|
assert(p->gr_mem);
|
|
|
|
m = p->gr_mem;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (!*m) {
|
|
|
|
if (fputc('\n', f) >= 0) {
|
|
|
|
rv = 0;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2010-03-31 16:07:43 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fprintf(f, fmt, *m) < 0) {
|
|
|
|
break;
|
2010-03-31 14:01:51 +05:30
|
|
|
}
|
2010-03-31 16:07:43 +05:30
|
|
|
m++;
|
|
|
|
fmt = format;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2010-03-31 16:08:17 +05:30
|
|
|
#endif
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2006-12-29 03:03:30 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2010-04-02 15:42:43 +05:30
|
|
|
#ifdef UNUSED_FOR_NOW
|
2010-03-31 16:07:43 +05:30
|
|
|
static const unsigned char put_sp_off[] ALIGN1 = {
|
2007-08-13 02:28:27 +05:30
|
|
|
offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
|
|
|
|
offsetof(struct spwd, sp_min), /* 3 - not a char ptr */
|
|
|
|
offsetof(struct spwd, sp_max), /* 4 - not a char ptr */
|
|
|
|
offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */
|
|
|
|
offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */
|
|
|
|
offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */
|
2004-07-15 18:23:49 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
int putspent(const struct spwd *p, FILE *stream)
|
|
|
|
{
|
2010-03-31 16:07:43 +05:30
|
|
|
const char *fmt;
|
2007-04-14 04:52:00 +05:30
|
|
|
long x;
|
2004-07-15 18:23:49 +05:30
|
|
|
int i;
|
|
|
|
int rv = -1;
|
|
|
|
|
|
|
|
/* Unlike putpwent and putgrent, glibc does not check the args. */
|
|
|
|
if (fprintf(stream, "%s:%s:", p->sp_namp,
|
|
|
|
(p->sp_pwdp ? p->sp_pwdp : "")) < 0
|
2006-12-29 03:03:30 +05:30
|
|
|
) {
|
2004-07-15 18:23:49 +05:30
|
|
|
goto DO_UNLOCK;
|
|
|
|
}
|
|
|
|
|
2010-03-31 16:07:43 +05:30
|
|
|
for (i = 0; i < sizeof(put_sp_off); i++) {
|
|
|
|
fmt = "%ld:";
|
|
|
|
x = *(long *)((char *)p + put_sp_off[i]);
|
2006-12-29 03:03:30 +05:30
|
|
|
if (x == -1) {
|
2010-03-31 16:07:43 +05:30
|
|
|
fmt += 3;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2010-03-31 16:07:43 +05:30
|
|
|
if (fprintf(stream, fmt, x) < 0) {
|
2004-07-15 18:23:49 +05:30
|
|
|
goto DO_UNLOCK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
|
|
|
|
goto DO_UNLOCK;
|
|
|
|
}
|
|
|
|
|
2005-10-08 07:53:22 +05:30
|
|
|
if (fputc('\n', stream) > 0) {
|
2004-07-15 18:23:49 +05:30
|
|
|
rv = 0;
|
|
|
|
}
|
|
|
|
|
2010-03-31 14:01:51 +05:30
|
|
|
DO_UNLOCK:
|
2004-07-15 18:23:49 +05:30
|
|
|
return rv;
|
|
|
|
}
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
2010-03-31 18:13:58 +05:30
|
|
|
#endif /* USE_BB_SHADOW */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
/**********************************************************************/
|
2010-03-31 18:13:58 +05:30
|
|
|
/* Internal functions */
|
2004-07-15 18:23:49 +05:30
|
|
|
/**********************************************************************/
|
|
|
|
|
2007-08-13 02:28:27 +05:30
|
|
|
static const unsigned char pw_off[] ALIGN1 = {
|
|
|
|
offsetof(struct passwd, pw_name), /* 0 */
|
|
|
|
offsetof(struct passwd, pw_passwd), /* 1 */
|
|
|
|
offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */
|
|
|
|
offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */
|
|
|
|
offsetof(struct passwd, pw_gecos), /* 4 */
|
|
|
|
offsetof(struct passwd, pw_dir), /* 5 */
|
|
|
|
offsetof(struct passwd, pw_shell) /* 6 */
|
2004-07-15 18:23:49 +05:30
|
|
|
};
|
|
|
|
|
2010-03-31 16:07:43 +05:30
|
|
|
static int FAST_FUNC bb__parsepwent(void *data, char *line)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = 0;
|
2010-03-31 14:01:51 +05:30
|
|
|
while (1) {
|
2009-09-23 20:47:53 +05:30
|
|
|
p = (char *) data + pw_off[i];
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2010-03-31 16:07:43 +05:30
|
|
|
if (i < 2 || i > 3) {
|
2004-07-15 18:23:49 +05:30
|
|
|
*((char **) p) = line;
|
2010-03-31 16:07:43 +05:30
|
|
|
if (i == 6) {
|
2004-07-15 18:23:49 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* NOTE: glibc difference - glibc allows omission of
|
|
|
|
* ':' seperators after the gid field if all remaining
|
|
|
|
* entries are empty. We require all separators. */
|
2006-12-29 03:03:30 +05:30
|
|
|
line = strchr(line, ':');
|
|
|
|
if (!line) {
|
2004-07-15 18:23:49 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned long t = strtoul(line, &endptr, 10);
|
|
|
|
/* Make sure we had at least one digit, and that the
|
|
|
|
* failing char is the next field seperator ':'. See
|
|
|
|
* glibc difference note above. */
|
|
|
|
/* TODO: Also check for leading whitespace? */
|
|
|
|
if ((endptr == line) || (*endptr != ':')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line = endptr;
|
|
|
|
if (i & 1) { /* i == 3 -- gid */
|
|
|
|
*((gid_t *) p) = t;
|
|
|
|
} else { /* i == 2 -- uid */
|
|
|
|
*((uid_t *) p) = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-31 16:07:43 +05:30
|
|
|
*line++ = '\0';
|
|
|
|
i++;
|
2010-03-31 14:01:51 +05:30
|
|
|
} /* while (1) */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
|
2007-08-13 02:28:27 +05:30
|
|
|
static const unsigned char gr_off[] ALIGN1 = {
|
|
|
|
offsetof(struct group, gr_name), /* 0 */
|
|
|
|
offsetof(struct group, gr_passwd), /* 1 */
|
|
|
|
offsetof(struct group, gr_gid) /* 2 - not a char ptr */
|
2004-07-15 18:23:49 +05:30
|
|
|
};
|
|
|
|
|
2010-03-31 16:07:43 +05:30
|
|
|
static int FAST_FUNC bb__parsegrent(void *data, char *line)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
char **members;
|
|
|
|
char *end_of_buf;
|
|
|
|
|
|
|
|
end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
|
|
|
|
i = 0;
|
2010-03-31 14:01:51 +05:30
|
|
|
while (1) {
|
2009-09-23 20:47:53 +05:30
|
|
|
p = (char *) data + gr_off[i];
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
if (i < 2) {
|
|
|
|
*((char **) p) = line;
|
2006-12-29 03:03:30 +05:30
|
|
|
line = strchr(line, ':');
|
|
|
|
if (!line) {
|
2004-07-15 18:23:49 +05:30
|
|
|
break;
|
|
|
|
}
|
2010-03-31 16:07:43 +05:30
|
|
|
*line++ = '\0';
|
|
|
|
i++;
|
2004-07-15 18:23:49 +05:30
|
|
|
} else {
|
|
|
|
*((gid_t *) p) = strtoul(line, &endptr, 10);
|
|
|
|
|
|
|
|
/* NOTE: glibc difference - glibc allows omission of the
|
|
|
|
* trailing colon when there is no member list. We treat
|
|
|
|
* this as an error. */
|
|
|
|
|
|
|
|
/* Make sure we had at least one digit, and that the
|
|
|
|
* failing char is the next field seperator ':'. See
|
|
|
|
* glibc difference note above. */
|
|
|
|
if ((endptr == line) || (*endptr != ':')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 1; /* Count terminating NULL ptr. */
|
|
|
|
p = endptr;
|
|
|
|
|
|
|
|
if (p[1]) { /* We have a member list to process. */
|
|
|
|
/* Overwrite the last ':' with a ',' before counting.
|
2010-03-31 18:13:58 +05:30
|
|
|
* This allows us to (1) test for initial ','
|
|
|
|
* and (2) adds one ',' so that the number of commas
|
|
|
|
* equals the member count. */
|
2004-07-15 18:23:49 +05:30
|
|
|
*p = ',';
|
|
|
|
do {
|
|
|
|
/* NOTE: glibc difference - glibc allows and trims leading
|
|
|
|
* (but not trailing) space. We treat this as an error. */
|
|
|
|
/* NOTE: glibc difference - glibc allows consecutive and
|
|
|
|
* trailing commas, and ignores "empty string" users. We
|
|
|
|
* treat this as an error. */
|
|
|
|
if (*p == ',') {
|
|
|
|
++i;
|
|
|
|
*p = 0; /* nul-terminate each member string. */
|
|
|
|
if (!*++p || (*p == ',') || isspace(*p)) {
|
|
|
|
goto ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (*++p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now align (p+1), rounding up. */
|
|
|
|
/* Assumes sizeof(char **) is a power of 2. */
|
|
|
|
members = (char **)( (((intptr_t) p) + sizeof(char **))
|
|
|
|
& ~((intptr_t)(sizeof(char **) - 1)) );
|
|
|
|
|
|
|
|
if (((char *)(members + i)) > end_of_buf) { /* No space. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
((struct group *) data)->gr_mem = members;
|
|
|
|
|
|
|
|
if (--i) {
|
|
|
|
p = endptr; /* Pointing to char prior to first member. */
|
2010-03-31 14:01:51 +05:30
|
|
|
while (1) {
|
2004-07-15 18:23:49 +05:30
|
|
|
*members++ = ++p;
|
2010-03-31 14:01:51 +05:30
|
|
|
if (!--i)
|
|
|
|
break;
|
|
|
|
while (*++p)
|
|
|
|
continue;
|
|
|
|
}
|
2006-01-25 05:38:53 +05:30
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
*members = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-03-31 14:01:51 +05:30
|
|
|
} /* while (1) */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
ERR:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
2006-12-31 02:41:57 +05:30
|
|
|
|
2006-12-29 03:03:30 +05:30
|
|
|
#if ENABLE_USE_BB_SHADOW
|
2007-08-13 02:28:27 +05:30
|
|
|
static const unsigned char sp_off[] ALIGN1 = {
|
2010-03-31 16:07:43 +05:30
|
|
|
offsetof(struct spwd, sp_namp), /* 0: char* */
|
|
|
|
offsetof(struct spwd, sp_pwdp), /* 1: char* */
|
|
|
|
offsetof(struct spwd, sp_lstchg), /* 2: long */
|
|
|
|
offsetof(struct spwd, sp_min), /* 3: long */
|
|
|
|
offsetof(struct spwd, sp_max), /* 4: long */
|
|
|
|
offsetof(struct spwd, sp_warn), /* 5: long */
|
|
|
|
offsetof(struct spwd, sp_inact), /* 6: long */
|
|
|
|
offsetof(struct spwd, sp_expire), /* 7: long */
|
|
|
|
offsetof(struct spwd, sp_flag) /* 8: unsigned long */
|
2004-07-15 18:23:49 +05:30
|
|
|
};
|
|
|
|
|
2010-03-31 16:07:43 +05:30
|
|
|
static int FAST_FUNC bb__parsespent(void *data, char *line)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = 0;
|
2009-09-23 20:47:53 +05:30
|
|
|
while (1) {
|
|
|
|
p = (char *) data + sp_off[i];
|
2004-07-15 18:23:49 +05:30
|
|
|
if (i < 2) {
|
|
|
|
*((char **) p) = line;
|
2006-12-29 03:03:30 +05:30
|
|
|
line = strchr(line, ':');
|
|
|
|
if (!line) {
|
2010-03-31 16:07:43 +05:30
|
|
|
break; /* error */
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
} else {
|
2009-09-23 20:47:53 +05:30
|
|
|
*((long *) p) = strtoul(line, &endptr, 10);
|
2004-07-15 18:23:49 +05:30
|
|
|
if (endptr == line) {
|
2010-03-31 16:07:43 +05:30
|
|
|
*((long *) p) = -1L;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
line = endptr;
|
|
|
|
if (i == 8) {
|
2010-03-31 16:07:43 +05:30
|
|
|
if (*line != '\0') {
|
|
|
|
break; /* error */
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2010-03-31 16:07:43 +05:30
|
|
|
return 0; /* all ok */
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2010-03-31 16:07:43 +05:30
|
|
|
if (*line != ':') {
|
|
|
|
break; /* error */
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
|
|
|
}
|
2009-09-23 20:47:53 +05:30
|
|
|
*line++ = '\0';
|
2010-03-31 16:07:43 +05:30
|
|
|
i++;
|
2009-09-23 20:47:53 +05:30
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2006-12-29 03:03:30 +05:30
|
|
|
#endif
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
|
2010-04-02 15:42:43 +05:30
|
|
|
/* Reads until EOF, or until it finds a line which fits in the buffer
|
2004-07-15 18:23:49 +05:30
|
|
|
* and for which the parser function succeeds.
|
|
|
|
*
|
2010-04-02 15:42:43 +05:30
|
|
|
* Returns 0 on success and ENOENT for end-of-file (glibc convention).
|
2004-07-15 18:23:49 +05:30
|
|
|
*/
|
2010-03-31 16:07:43 +05:30
|
|
|
static int bb__pgsreader(
|
|
|
|
int FAST_FUNC (*parserfunc)(void *d, char *line),
|
|
|
|
void *data,
|
|
|
|
char *__restrict line_buff,
|
|
|
|
size_t buflen,
|
|
|
|
FILE *f)
|
2004-07-15 18:23:49 +05:30
|
|
|
{
|
|
|
|
int skip;
|
|
|
|
int rv = ERANGE;
|
|
|
|
|
|
|
|
if (buflen < PWD_BUFFER_SIZE) {
|
2006-12-29 03:03:30 +05:30
|
|
|
errno = rv;
|
2010-03-31 13:54:37 +05:30
|
|
|
return rv;
|
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2010-03-31 13:54:37 +05:30
|
|
|
skip = 0;
|
|
|
|
while (1) {
|
|
|
|
if (!fgets(line_buff, buflen, f)) {
|
|
|
|
if (feof(f)) {
|
|
|
|
rv = ENOENT;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2010-03-31 13:54:37 +05:30
|
|
|
break;
|
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2010-03-31 13:54:37 +05:30
|
|
|
{
|
|
|
|
int line_len = strlen(line_buff) - 1;
|
|
|
|
if (line_len >= 0 && line_buff[line_len] == '\n') {
|
|
|
|
line_buff[line_len] = '\0';
|
|
|
|
} else
|
|
|
|
if (line_len + 2 == buflen) {
|
|
|
|
/* A start (or continuation) of overlong line */
|
|
|
|
skip = 1;
|
2004-07-15 18:23:49 +05:30
|
|
|
continue;
|
2010-03-31 13:54:37 +05:30
|
|
|
} /* else: a last line in the file, and it has no '\n' */
|
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2010-03-31 13:54:37 +05:30
|
|
|
if (skip) {
|
|
|
|
/* This "line" is a remainder of overlong line, ignore */
|
|
|
|
skip = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2004-07-15 18:23:49 +05:30
|
|
|
|
2010-03-31 13:54:37 +05:30
|
|
|
/* NOTE: glibc difference - glibc strips leading whitespace from
|
|
|
|
* records. We do not allow leading whitespace. */
|
|
|
|
|
|
|
|
/* Skip empty lines, comment lines, and lines with leading
|
|
|
|
* whitespace. */
|
|
|
|
if (line_buff[0] != '\0' && line_buff[0] != '#' && !isspace(line_buff[0])) {
|
|
|
|
if (parserfunc == bb__parsegrent) {
|
|
|
|
/* Do evil group hack:
|
|
|
|
* The group entry parsing function needs to know where
|
|
|
|
* the end of the buffer is so that it can construct the
|
|
|
|
* group member ptr table. */
|
|
|
|
((struct group *) data)->gr_name = line_buff + buflen;
|
2004-07-15 18:23:49 +05:30
|
|
|
}
|
2010-03-31 13:54:37 +05:30
|
|
|
if (parserfunc(data, line_buff) == 0) {
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* while (1) */
|
2004-07-15 18:23:49 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|