1
0
mirror of https://github.com/sheumann/hush.git synced 2024-12-29 13:30:02 +00:00

- move GETXXKEY_R_FUNC into an internal helper file to allow for compiling

with IMA.
This commit is contained in:
Bernhard Reutner-Fischer 2005-10-28 11:21:40 +00:00
parent 6973abcfa8
commit 30c7de068b
2 changed files with 154 additions and 46 deletions

View File

@ -74,6 +74,7 @@ extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
* Doing so is analogous to having fgetc() set errno on EOF. * Doing so is analogous to having fgetc() set errno on EOF.
*/ */
/**********************************************************************/ /**********************************************************************/
#ifdef L_fgetpwent_r #ifdef L_fgetpwent_r
int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf, int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
@ -224,6 +225,7 @@ int sgetspent_r(const char *string, struct spwd *result_buf,
#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key)) #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
#define DO_GETXXKEY_R_KEYTYPE const char *__restrict #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
#define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
#include "pwd_grp_internal.c"
#endif #endif
#ifdef L_getgrnam_r #ifdef L_getgrnam_r
@ -233,6 +235,7 @@ int sgetspent_r(const char *string, struct spwd *result_buf,
#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key)) #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
#define DO_GETXXKEY_R_KEYTYPE const char *__restrict #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
#define DO_GETXXKEY_R_PATHNAME _PATH_GROUP #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
#include "pwd_grp_internal.c"
#endif #endif
#ifdef L_getspnam_r #ifdef L_getspnam_r
@ -242,6 +245,7 @@ int sgetspent_r(const char *string, struct spwd *result_buf,
#define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key)) #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
#define DO_GETXXKEY_R_KEYTYPE const char *__restrict #define DO_GETXXKEY_R_KEYTYPE const char *__restrict
#define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW
#include "pwd_grp_internal.c"
#endif #endif
#ifdef L_getpwuid_r #ifdef L_getpwuid_r
@ -251,6 +255,7 @@ int sgetspent_r(const char *string, struct spwd *result_buf,
#define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key) #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
#define DO_GETXXKEY_R_KEYTYPE uid_t #define DO_GETXXKEY_R_KEYTYPE uid_t
#define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD
#include "pwd_grp_internal.c"
#endif #endif
#ifdef L_getgrgid_r #ifdef L_getgrgid_r
@ -260,46 +265,9 @@ int sgetspent_r(const char *string, struct spwd *result_buf,
#define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key) #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
#define DO_GETXXKEY_R_KEYTYPE gid_t #define DO_GETXXKEY_R_KEYTYPE gid_t
#define DO_GETXXKEY_R_PATHNAME _PATH_GROUP #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP
#include "pwd_grp_internal.c"
#endif #endif
/**********************************************************************/
#ifdef GETXXKEY_R_FUNC
int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
GETXXKEY_R_ENTTYPE *__restrict resultbuf,
char *__restrict buffer, size_t buflen,
GETXXKEY_R_ENTTYPE **__restrict result)
{
FILE *stream;
int rv;
*result = NULL;
if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
rv = errno;
} else {
do {
if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
buffer, buflen, stream))
) {
if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
*result = resultbuf;
break;
}
} else {
if (rv == ENOENT) { /* end-of-file encountered. */
rv = 0;
}
break;
}
} while (1);
fclose(stream);
}
return rv;
}
#endif
/**********************************************************************/ /**********************************************************************/
#ifdef L_getpwuid #ifdef L_getpwuid
@ -440,22 +408,37 @@ int getpw(uid_t uid, char *buf)
#endif #endif
/**********************************************************************/ /**********************************************************************/
#ifdef L_getpwent_r
#if defined(L_getpwent_r) || defined(L_getgrent_r) || defined(L_getspent_r)
#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
#endif
#ifdef L_getpwent_r
static FILE *pwf /*= NULL*/; static FILE *pwf /*= NULL*/;
void setpwent(void) void setpwent(void)
{ {
LOCK;
if (pwf) { if (pwf) {
rewind(pwf); rewind(pwf);
} }
UNLOCK;
} }
void endpwent(void) void endpwent(void)
{ {
LOCK;
if (pwf) { if (pwf) {
fclose(pwf); fclose(pwf);
pwf = NULL; pwf = NULL;
} }
UNLOCK;
} }
@ -465,6 +448,7 @@ int getpwent_r(struct passwd *__restrict resultbuf,
{ {
int rv; int rv;
LOCK;
*result = NULL; /* In case of error... */ *result = NULL; /* In case of error... */
if (!pwf) { if (!pwf) {
@ -480,6 +464,7 @@ int getpwent_r(struct passwd *__restrict resultbuf,
} }
ERR: ERR:
UNLOCK;
return rv; return rv;
} }
@ -490,17 +475,21 @@ int getpwent_r(struct passwd *__restrict resultbuf,
static FILE *grf /*= NULL*/; static FILE *grf /*= NULL*/;
void setgrent(void) void setgrent(void)
{ {
LOCK;
if (grf) { if (grf) {
rewind(grf); rewind(grf);
} }
UNLOCK;
} }
void endgrent(void) void endgrent(void)
{ {
LOCK;
if (grf) { if (grf) {
fclose(grf); fclose(grf);
grf = NULL; grf = NULL;
} }
UNLOCK;
} }
int getgrent_r(struct group *__restrict resultbuf, int getgrent_r(struct group *__restrict resultbuf,
@ -509,6 +498,7 @@ int getgrent_r(struct group *__restrict resultbuf,
{ {
int rv; int rv;
LOCK;
*result = NULL; /* In case of error... */ *result = NULL; /* In case of error... */
if (!grf) { if (!grf) {
@ -524,6 +514,7 @@ int getgrent_r(struct group *__restrict resultbuf,
} }
ERR: ERR:
UNLOCK;
return rv; return rv;
} }
@ -534,17 +525,21 @@ int getgrent_r(struct group *__restrict resultbuf,
static FILE *spf /*= NULL*/; static FILE *spf /*= NULL*/;
void setspent(void) void setspent(void)
{ {
LOCK;
if (spf) { if (spf) {
rewind(spf); rewind(spf);
} }
UNLOCK;
} }
void endspent(void) void endspent(void)
{ {
LOCK;
if (spf) { if (spf) {
fclose(spf); fclose(spf);
spf = NULL; spf = NULL;
} }
UNLOCK;
} }
int getspent_r(struct spwd *resultbuf, char *buffer, int getspent_r(struct spwd *resultbuf, char *buffer,
@ -552,6 +547,7 @@ int getspent_r(struct spwd *resultbuf, char *buffer,
{ {
int rv; int rv;
LOCK;
*result = NULL; /* In case of error... */ *result = NULL; /* In case of error... */
if (!spf) { if (!spf) {
@ -567,6 +563,7 @@ int getspent_r(struct spwd *resultbuf, char *buffer,
} }
ERR: ERR:
UNLOCK;
return rv; return rv;
} }
@ -632,7 +629,7 @@ struct spwd *sgetspent(const char *string)
int initgroups(const char *user, gid_t gid) int initgroups(const char *user, gid_t gid)
{ {
FILE *grf; FILE *grfile;
gid_t *group_list; gid_t *group_list;
int num_groups, rv; int num_groups, rv;
char **m; char **m;
@ -643,13 +640,13 @@ int initgroups(const char *user, gid_t gid)
/* We alloc space for 8 gids at a time. */ /* We alloc space for 8 gids at a time. */
if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL) if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL)
&& ((grf = fopen(_PATH_GROUP, "r")) != NULL) && ((grfile = fopen(_PATH_GROUP, "r")) != NULL)
) { ) {
*group_list = gid; *group_list = gid;
num_groups = 1; num_groups = 1;
while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grf)) { while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grfile)) {
assert(group.gr_mem); /* Must have at least a NULL terminator. */ assert(group.gr_mem); /* Must have at least a NULL terminator. */
if (group.gr_gid != gid) { if (group.gr_gid != gid) {
for (m=group.gr_mem ; *m ; m++) { for (m=group.gr_mem ; *m ; m++) {
@ -673,7 +670,7 @@ int initgroups(const char *user, gid_t gid)
rv = setgroups(num_groups, group_list); rv = setgroups(num_groups, group_list);
DO_CLOSE: DO_CLOSE:
fclose(grf); fclose(grfile);
} }
/* group_list will be NULL if initial malloc failed, which may trigger /* group_list will be NULL if initial malloc failed, which may trigger
@ -756,7 +753,7 @@ int putgrent(const struct group *__restrict p, FILE *__restrict f)
/**********************************************************************/ /**********************************************************************/
#ifdef L_putspent #ifdef L_putspent
static const unsigned char sp_off[] = { static const unsigned char _sp_off[] = {
offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */
offsetof(struct spwd, sp_min), /* 3 - 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_max), /* 4 - not a char ptr */
@ -780,9 +777,9 @@ int putspent(const struct spwd *p, FILE *stream)
goto DO_UNLOCK; goto DO_UNLOCK;
} }
for (i=0 ; i < sizeof(sp_off) ; i++) { for (i=0 ; i < sizeof(_sp_off) ; i++) {
f = ld_format; f = ld_format;
if ((x = *(const long int *)(((const char *) p) + sp_off[i])) == -1) { if ((x = *(const long int *)(((const char *) p) + _sp_off[i])) == -1) {
f += 3; f += 3;
} }
if (fprintf(stream, f, x) < 0) { if (fprintf(stream, f, x) < 0) {

View File

@ -0,0 +1,111 @@
/* Copyright (C) 2003 Manuel Novoa III
*
* Licensed under GPL v2, or later. See file LICENSE in this tarball.
*/
/* Nov 6, 2003 Initial version.
*
* NOTE: This implementation is quite strict about requiring all
* 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.
*
* TODO:
* Move to dynamic allocation of (currently staticly allocated)
* buffers; especially for the group-related functions since
* large group member lists will cause error returns.
*
*/
#include <features.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include "busybox.h"
#include "pwd_.h"
#include "grp_.h"
#include "shadow_.h"
#ifndef _PATH_SHADOW
#define _PATH_SHADOW "/etc/shadow"
#endif
#ifndef _PATH_PASSWD
#define _PATH_PASSWD "/etc/passwd"
#endif
#ifndef _PATH_GROUP
#define _PATH_GROUP "/etc/group"
#endif
/**********************************************************************/
/* Sizes for staticly allocated buffers. */
/* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
* _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
#define PWD_BUFFER_SIZE 256
#define GRP_BUFFER_SIZE 256
/**********************************************************************/
/* Prototypes for internal functions. */
extern int __parsepwent(void *pw, char *line);
extern int __parsegrent(void *gr, char *line);
extern int __parsespent(void *sp, char *line);
extern int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data,
char *__restrict line_buff, size_t buflen, FILE *f);
#ifndef GETXXKEY_R_FUNC
#error GETXXKEY_R_FUNC is not defined!
#endif
/**********************************************************************/
#ifdef GETXXKEY_R_FUNC
int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
GETXXKEY_R_ENTTYPE *__restrict resultbuf,
char *__restrict buffer, size_t buflen,
GETXXKEY_R_ENTTYPE **__restrict result)
{
FILE *stream;
int rv;
*result = NULL;
if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
rv = errno;
} else {
do {
if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
buffer, buflen, stream))
) {
if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
*result = resultbuf;
break;
}
} else {
if (rv == ENOENT) { /* end-of-file encountered. */
rv = 0;
}
break;
}
} while (1);
fclose(stream);
}
return rv;
}
#endif
/**********************************************************************/
#undef GETXXKEY_R_FUNC
#undef GETXXKEY_R_PARSER
#undef GETXXKEY_R_ENTTYPE
#undef GETXXKEY_R_TEST
#undef DO_GETXXKEY_R_KEYTYPE
#undef DO_GETXXKEY_R_PATHNAME