/* Copyright (C) 2003 Manuel Novoa III
*
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Licensed under GPL v2, or later. See file LICENSE in this tarball.
*/
/* Nov 6, 2003 Initial version.
* lenient. See the various glibc difference comments below.
*
* TODO:
- * Move to dynamic allocation of (currently staticly allocated)
+ * Move to dynamic allocation of (currently statically allocated)
* buffers; especially for the group-related functions since
* large group member lists will cause error returns.
*
*/
+#include "libbb.h"
#include <features.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
-#include "busybox.h"
-#include "pwd_.h"
-#include "grp_.h"
-#include "shadow_.h"
+
+//#include "pwd_.h"
+//#include "grp_.h"
+//#include "shadow_.h"
#ifndef _PATH_SHADOW
#define _PATH_SHADOW "/etc/shadow"
#endif
/**********************************************************************/
-/* Sizes for staticly allocated buffers. */
+/* Sizes for statically 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 */
* 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,
#endif
/**********************************************************************/
/* For the various fget??ent funcs, return NULL on failure and a
- * pointer to the appropriate struct (staticly allocated) on success.
+ * pointer to the appropriate struct (statically allocated) on success.
*/
/**********************************************************************/
#ifdef L_fgetpwent
#ifdef L_getpwnam_r
#define GETXXKEY_R_FUNC getpwnam_r
-#define GETXXKEY_R_PARSER __parsepwent
+#define GETXXKEY_R_PARSER __parsepwent
#define GETXXKEY_R_ENTTYPE struct passwd
#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
#define GETXXKEY_R_FUNC getgrnam_r
-#define GETXXKEY_R_PARSER __parsegrent
+#define GETXXKEY_R_PARSER __parsegrent
#define GETXXKEY_R_ENTTYPE struct group
#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
#define GETXXKEY_R_FUNC getspnam_r
-#define GETXXKEY_R_PARSER __parsespent
+#define GETXXKEY_R_PARSER __parsespent
#define GETXXKEY_R_ENTTYPE struct spwd
#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
#define GETXXKEY_R_FUNC getpwuid_r
-#define GETXXKEY_R_PARSER __parsepwent
+#define GETXXKEY_R_PARSER __parsepwent
#define GETXXKEY_R_ENTTYPE struct passwd
#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
#define GETXXKEY_R_FUNC getgrgid_r
-#define GETXXKEY_R_PARSER __parsegrent
+#define GETXXKEY_R_PARSER __parsegrent
#define GETXXKEY_R_ENTTYPE struct group
#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
#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;
}
-int getpwent_r(struct passwd *__restrict resultbuf,
+int getpwent_r(struct passwd *__restrict resultbuf,
char *__restrict buffer, size_t buflen,
struct passwd **__restrict result)
{
int rv;
+ LOCK;
*result = NULL; /* In case of error... */
if (!pwf) {
}
ERR:
+ UNLOCK;
return rv;
}
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,
{
int rv;
+ LOCK;
*result = NULL; /* In case of error... */
if (!grf) {
}
ERR:
+ UNLOCK;
return rv;
}
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,
+int getspent_r(struct spwd *resultbuf, char *buffer,
size_t buflen, struct spwd **result)
{
int rv;
+ LOCK;
*result = NULL; /* In case of error... */
if (!spf) {
}
ERR:
+ UNLOCK;
return rv;
}
int initgroups(const char *user, gid_t gid)
{
- FILE *grf;
+ FILE *grfile;
gid_t *group_list;
int num_groups, rv;
char **m;
/* 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++) {
rv = setgroups(num_groups, group_list);
DO_CLOSE:
- fclose(grf);
+ fclose(grfile);
}
/* group_list will be NULL if initial malloc failed, which may trigger
do {
if (!*m) {
- if (fputc_unlocked('\n', f) >= 0) {
+ if (fputc('\n', f) >= 0) {
rv = 0;
}
break;
/**********************************************************************/
#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_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 */
+ 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 */
};
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) {
goto DO_UNLOCK;
}
- if (fputc_unlocked('\n', stream) > 0) {
+ if (fputc('\n', stream) > 0) {
rv = 0;
}
#endif
/**********************************************************************/
-/* Internal uClibc functions. */
+/* Internal uClibc functions. */
/**********************************************************************/
#ifdef L___parsepwent
static const unsigned char pw_off[] = {
- offsetof(struct passwd, pw_name), /* 0 */
+ 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_gid), /* 3 - not a char ptr */
offsetof(struct passwd, pw_gecos), /* 4 */
- offsetof(struct passwd, pw_dir), /* 5 */
- offsetof(struct passwd, pw_shell) /* 6 */
+ offsetof(struct passwd, pw_dir), /* 5 */
+ offsetof(struct passwd, pw_shell) /* 6 */
};
int __parsepwent(void *data, char *line)
do {
p = ((char *) ((struct passwd *) data)) + pw_off[i];
- if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
+ if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
*((char **) p) = line;
if (i==6) {
return 0;
#ifdef L___parsegrent
static const unsigned char gr_off[] = {
- offsetof(struct group, gr_name), /* 0 */
+ offsetof(struct group, gr_name), /* 0 */
offsetof(struct group, gr_passwd), /* 1 */
offsetof(struct group, gr_gid) /* 2 - not a char ptr */
};
if (!--i) break;
while (*++p) {}
} while (1);
- }
+ }
*members = NULL;
return 0;
offsetof(struct spwd, sp_namp), /* 0 */
offsetof(struct spwd, sp_pwdp), /* 1 */
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_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 */
- offsetof(struct spwd, sp_flag) /* 8 - 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 */
+ offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */
};
int __parsespent(void *data, char * line)
} else {
skip = 0;
do {
- if (!fgets_unlocked(line_buff, buflen, f)) {
- if (feof_unlocked(f)) {
+ if (!fgets(line_buff, buflen, f)) {
+ if (feof(f)) {
rv = ENOENT;
}
break;