From 30c7de068b50de999b0b73a22e859dd2e9c23204 Mon Sep 17 00:00:00 2001 From: Bernhard Reutner-Fischer Date: Fri, 28 Oct 2005 11:21:40 +0000 Subject: - move GETXXKEY_R_FUNC into an internal helper file to allow for compiling with IMA. --- libpwdgrp/pwd_grp.c | 89 +++++++++++++++++----------------- libpwdgrp/pwd_grp_internal.c | 111 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 46 deletions(-) create mode 100644 libpwdgrp/pwd_grp_internal.c diff --git a/libpwdgrp/pwd_grp.c b/libpwdgrp/pwd_grp.c index 3661834..1878200 100644 --- a/libpwdgrp/pwd_grp.c +++ b/libpwdgrp/pwd_grp.c @@ -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. */ /**********************************************************************/ + #ifdef L_fgetpwent_r 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 DO_GETXXKEY_R_KEYTYPE const char *__restrict #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD +#include "pwd_grp_internal.c" #endif #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 DO_GETXXKEY_R_KEYTYPE const char *__restrict #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP +#include "pwd_grp_internal.c" #endif #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 DO_GETXXKEY_R_KEYTYPE const char *__restrict #define DO_GETXXKEY_R_PATHNAME _PATH_SHADOW +#include "pwd_grp_internal.c" #endif #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 DO_GETXXKEY_R_KEYTYPE uid_t #define DO_GETXXKEY_R_PATHNAME _PATH_PASSWD +#include "pwd_grp_internal.c" #endif #ifdef L_getgrgid_r @@ -260,47 +265,10 @@ int sgetspent_r(const char *string, struct spwd *result_buf, #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key) #define DO_GETXXKEY_R_KEYTYPE gid_t #define DO_GETXXKEY_R_PATHNAME _PATH_GROUP +#include "pwd_grp_internal.c" #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 struct passwd *getpwuid(uid_t uid) @@ -440,22 +408,37 @@ int getpw(uid_t uid, char *buf) #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*/; void setpwent(void) { + LOCK; if (pwf) { rewind(pwf); } + UNLOCK; } void endpwent(void) { + LOCK; if (pwf) { fclose(pwf); pwf = NULL; } + UNLOCK; } @@ -465,6 +448,7 @@ int getpwent_r(struct passwd *__restrict resultbuf, { int rv; + LOCK; *result = NULL; /* In case of error... */ if (!pwf) { @@ -480,6 +464,7 @@ int getpwent_r(struct passwd *__restrict resultbuf, } ERR: + UNLOCK; return rv; } @@ -490,17 +475,21 @@ int getpwent_r(struct passwd *__restrict resultbuf, static FILE *grf /*= NULL*/; void setgrent(void) { + LOCK; if (grf) { rewind(grf); } + UNLOCK; } void endgrent(void) { + LOCK; if (grf) { fclose(grf); grf = NULL; } + UNLOCK; } int getgrent_r(struct group *__restrict resultbuf, @@ -509,6 +498,7 @@ int getgrent_r(struct group *__restrict resultbuf, { int rv; + LOCK; *result = NULL; /* In case of error... */ if (!grf) { @@ -524,6 +514,7 @@ int getgrent_r(struct group *__restrict resultbuf, } ERR: + UNLOCK; return rv; } @@ -534,17 +525,21 @@ int getgrent_r(struct group *__restrict resultbuf, static FILE *spf /*= NULL*/; void setspent(void) { + LOCK; if (spf) { rewind(spf); } + UNLOCK; } void endspent(void) { + LOCK; if (spf) { fclose(spf); spf = NULL; } + UNLOCK; } int getspent_r(struct spwd *resultbuf, char *buffer, @@ -552,6 +547,7 @@ int getspent_r(struct spwd *resultbuf, char *buffer, { int rv; + LOCK; *result = NULL; /* In case of error... */ if (!spf) { @@ -567,6 +563,7 @@ int getspent_r(struct spwd *resultbuf, char *buffer, } ERR: + UNLOCK; return rv; } @@ -632,7 +629,7 @@ struct spwd *sgetspent(const char *string) int initgroups(const char *user, gid_t gid) { - FILE *grf; + FILE *grfile; gid_t *group_list; int num_groups, rv; char **m; @@ -643,13 +640,13 @@ int initgroups(const char *user, gid_t gid) /* We alloc space for 8 gids at a time. */ 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; 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. */ if (group.gr_gid != gid) { 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); DO_CLOSE: - fclose(grf); + fclose(grfile); } /* 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 -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_min), /* 3 - 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; } - for (i=0 ; i < sizeof(sp_off) ; i++) { + for (i=0 ; i < sizeof(_sp_off) ; i++) { 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; } if (fprintf(stream, f, x) < 0) { diff --git a/libpwdgrp/pwd_grp_internal.c b/libpwdgrp/pwd_grp_internal.c new file mode 100644 index 0000000..d935b86 --- /dev/null +++ b/libpwdgrp/pwd_grp_internal.c @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#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 + -- cgit v1.1