*/
#include "libbb.h"
-#include <features.h>
#include <assert.h>
#ifndef _PATH_SHADOW
/**********************************************************************/
/* Prototypes for internal functions. */
-static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
- char *__restrict line_buff, size_t buflen, FILE *f);
+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 bb__parsepwent(void *pw, char *line);
-static int bb__parsegrent(void *gr, char *line);
+static int FAST_FUNC bb__parsepwent(void *pw, char *line);
+static int FAST_FUNC bb__parsegrent(void *gr, char *line);
#if ENABLE_USE_BB_SHADOW
-static int bb__parsespent(void *sp, char *line);
+static int FAST_FUNC bb__parsespent(void *sp, char *line);
#endif
/**********************************************************************/
}
#if ENABLE_USE_BB_SHADOW
+#ifdef UNUSED_FOR_NOW
int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
char *__restrict buffer, size_t buflen,
struct spwd **__restrict result)
return rv;
}
#endif
+#endif
/**********************************************************************/
/* For the various fget??ent funcs, return NULL on failure and a
* TODO: audit & stop using these in bbox, they pull in static buffers */
/**********************************************************************/
-#if 0
+#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
struct passwd *fgetpwent(FILE *stream)
{
struct statics *S;
#endif
#if ENABLE_USE_BB_SHADOW
-#if 0
+#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
struct spwd *fgetspent(FILE *stream)
{
struct statics *S;
}
#endif
+#ifdef UNUSED_FOR_NOW
int sgetspent_r(const char *string, struct spwd *result_buf,
char *buffer, size_t buflen, struct spwd **result)
{
*result = NULL;
if (buflen < PWD_BUFFER_SIZE) {
- DO_ERANGE:
- errno=rv;
+ DO_ERANGE:
+ errno = rv;
goto DONE;
}
return rv;
}
#endif
+#endif /* ENABLE_USE_BB_SHADOW */
/**********************************************************************/
}
#endif
-/* This one doesn't use static buffers */
-int getpw(uid_t uid, char *buf)
-{
- struct passwd resultbuf;
- struct passwd *result;
- char buffer[PWD_BUFFER_SIZE];
-
- if (!buf) {
- errno = EINVAL;
- } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) {
- if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n",
- resultbuf.pw_name, resultbuf.pw_passwd,
- (unsigned long)(resultbuf.pw_uid),
- (unsigned long)(resultbuf.pw_gid),
- resultbuf.pw_gecos, resultbuf.pw_dir,
- resultbuf.pw_shell) >= 0
- ) {
- return 0;
- }
- }
-
- return -1;
-}
-
/**********************************************************************/
/* FIXME: we don't have such CONFIG_xx - ?! */
*result = NULL; /* In case of error... */
if (!pwf) {
- pwf = fopen(_PATH_PASSWD, "r");
+ pwf = fopen_for_read(_PATH_PASSWD);
if (!pwf) {
rv = errno;
goto ERR;
*result = NULL; /* In case of error... */
if (!grf) {
- grf = fopen(_PATH_GROUP, "r");
+ grf = fopen_for_read(_PATH_GROUP);
if (!grf) {
rv = errno;
goto ERR;
return rv;
}
+#ifdef UNUSED_FOR_NOW
#if ENABLE_USE_BB_SHADOW
static FILE *spf /*= NULL*/;
void setspent(void)
*result = NULL; /* In case of error... */
if (!spf) {
- spf = fopen(_PATH_SHADOW, "r");
+ spf = fopen_for_read(_PATH_SHADOW);
if (!spf) {
rv = errno;
goto ERR;
return rv;
}
#endif
+#endif /* UNUSED_FOR_NOW */
-#if 0
+#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
struct passwd *getpwent(void)
{
static char line_buff[PWD_BUFFER_SIZE];
getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
return result;
}
-#endif
-#if 0 //ENABLE_USE_BB_SHADOW
+#if ENABLE_USE_BB_SHADOW
struct spwd *getspent(void)
{
static char line_buff[PWD_BUFFER_SIZE];
return result;
}
#endif
+#endif /* UNUSED_SINCE_WE_AVOID_STATIC_BUFS */
-int initgroups(const char *user, gid_t gid)
+static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
{
FILE *grfile;
gid_t *group_list;
- int num_groups, rv;
- char **m;
+ int ngroups;
struct group group;
char buff[PWD_BUFFER_SIZE];
- rv = -1;
- grfile = fopen(_PATH_GROUP, "r");
- if (grfile != NULL) {
-
- /* We alloc space for 8 gids at a time. */
- group_list = xmalloc(8 * sizeof(gid_t *));
- *group_list = gid;
- num_groups = 1;
+ /* We alloc space for 8 gids at a time. */
+ group_list = xmalloc(8 * sizeof(group_list[0]));
+ group_list[0] = gid;
+ ngroups = 1;
+ grfile = fopen_for_read(_PATH_GROUP);
+ if (grfile) {
while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
+ char **m;
assert(group.gr_mem); /* Must have at least a NULL terminator. */
- if (group.gr_gid != gid) {
- for (m = group.gr_mem; *m; m++) {
- if (!strcmp(*m, user)) {
- if (!(num_groups & 7)) {
- gid_t *tmp = xrealloc(group_list,
- (num_groups+8) * sizeof(gid_t *));
- group_list = tmp;
- }
- group_list[num_groups++] = group.gr_gid;
- break;
- }
- }
+ if (group.gr_gid == gid)
+ continue;
+ for (m = group.gr_mem; *m; m++) {
+ if (strcmp(*m, user) != 0)
+ continue;
+ group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
+ group_list[ngroups++] = group.gr_gid;
+ break;
}
}
-
- rv = setgroups(num_groups, group_list);
- free(group_list);
fclose(grfile);
}
+ *ngroups_ptr = ngroups;
+ return group_list;
+}
- return rv;
+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;
+}
+
+#ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
{
int rv = -1;
+#if 0
+ /* glibc does this check */
if (!p || !f) {
errno = EINVAL;
- } else {
- /* 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;
- }
+ return rv;
+ }
+#endif
+
+ /* 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;
}
return rv;
int putgrent(const struct group *__restrict p, FILE *__restrict f)
{
- static const char format[] ALIGN1 = ",%s";
-
- char **m;
- const char *fmt;
int rv = -1;
- if (!p || !f) { /* Sigh... glibc checks. */
+#if 0
+ /* glibc does this check */
+ if (!p || !f) {
errno = EINVAL;
- } else {
- if (fprintf(f, "%s:%s:%lu:",
- p->gr_name, p->gr_passwd,
- (unsigned long)(p->gr_gid)) >= 0
- ) {
+ return rv;
+ }
+#endif
- fmt = format + 1;
+ if (fprintf(f, "%s:%s:%lu:",
+ p->gr_name, p->gr_passwd,
+ (unsigned long)(p->gr_gid)) >= 0
+ ) {
+ static const char format[] ALIGN1 = ",%s";
- assert(p->gr_mem);
- m = p->gr_mem;
+ char **m;
+ const char *fmt;
- do {
- if (!*m) {
- if (fputc('\n', f) >= 0) {
- rv = 0;
- }
- break;
- }
- if (fprintf(f, fmt, *m) < 0) {
- break;
- }
- ++m;
- fmt = format;
- } while (1);
+ fmt = format + 1;
- }
+ assert(p->gr_mem);
+ m = p->gr_mem;
+ while (1) {
+ if (!*m) {
+ if (fputc('\n', f) >= 0) {
+ rv = 0;
+ }
+ break;
+ }
+ if (fprintf(f, fmt, *m) < 0) {
+ break;
+ }
+ m++;
+ fmt = format;
+ }
}
return rv;
}
+#endif
#if ENABLE_USE_BB_SHADOW
-static const unsigned char _sp_off[] ALIGN1 = {
+#ifdef UNUSED_FOR_NOW
+static const unsigned char put_sp_off[] ALIGN1 = {
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 */
int putspent(const struct spwd *p, FILE *stream)
{
- static const char ld_format[] ALIGN1 = "%ld:";
-
- const char *f;
+ const char *fmt;
long x;
int i;
int rv = -1;
goto DO_UNLOCK;
}
- for (i = 0; i < sizeof(_sp_off); i++) {
- f = ld_format;
- x = *(const long *)(((const char *) p) + _sp_off[i]);
+ for (i = 0; i < sizeof(put_sp_off); i++) {
+ fmt = "%ld:";
+ x = *(long *)((char *)p + put_sp_off[i]);
if (x == -1) {
- f += 3;
+ fmt += 3;
}
- if (fprintf(stream, f, x) < 0) {
+ if (fprintf(stream, fmt, x) < 0) {
goto DO_UNLOCK;
}
}
rv = 0;
}
-DO_UNLOCK:
+ DO_UNLOCK:
return rv;
}
#endif
+#endif /* USE_BB_SHADOW */
/**********************************************************************/
-/* Internal uClibc functions. */
+/* Internal functions */
/**********************************************************************/
static const unsigned char pw_off[] ALIGN1 = {
offsetof(struct passwd, pw_shell) /* 6 */
};
-static int bb__parsepwent(void *data, char *line)
+static int FAST_FUNC bb__parsepwent(void *data, char *line)
{
char *endptr;
char *p;
int i;
i = 0;
- do {
- p = ((char *) ((struct passwd *) data)) + pw_off[i];
+ while (1) {
+ p = (char *) data + pw_off[i];
- if ((i & 6) ^ 2) { /* i!=2 and i!=3 */
+ if (i < 2 || i > 3) {
*((char **) p) = line;
- if (i==6) {
+ if (i == 6) {
return 0;
}
/* NOTE: glibc difference - glibc allows omission of
}
}
- *line++ = 0;
- ++i;
- } while (1);
+ *line++ = '\0';
+ i++;
+ } /* while (1) */
return -1;
}
offsetof(struct group, gr_gid) /* 2 - not a char ptr */
};
-static int bb__parsegrent(void *data, char *line)
+static int FAST_FUNC bb__parsegrent(void *data, char *line)
{
char *endptr;
char *p;
end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
i = 0;
- do {
- p = ((char *) ((struct group *) data)) + gr_off[i];
+ while (1) {
+ p = (char *) data + gr_off[i];
if (i < 2) {
*((char **) p) = line;
if (!line) {
break;
}
- *line++ = 0;
- ++i;
+ *line++ = '\0';
+ i++;
} else {
*((gid_t *) p) = strtoul(line, &endptr, 10);
if (p[1]) { /* We have a member list to process. */
/* Overwrite the last ':' with a ',' before counting.
- * This allows us to test for initial ',' and adds
- * one ',' so that the ',' count equals the member
- * count. */
+ * This allows us to (1) test for initial ','
+ * and (2) adds one ',' so that the number of commas
+ * equals the member count. */
*p = ',';
do {
/* NOTE: glibc difference - glibc allows and trims leading
if (--i) {
p = endptr; /* Pointing to char prior to first member. */
- do {
+ while (1) {
*members++ = ++p;
- if (!--i) break;
- while (*++p) {}
- } while (1);
+ if (!--i)
+ break;
+ while (*++p)
+ continue;
+ }
}
*members = NULL;
return 0;
}
- } while (1);
+ } /* while (1) */
ERR:
return -1;
#if ENABLE_USE_BB_SHADOW
static const unsigned char sp_off[] ALIGN1 = {
- 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_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_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 */
};
-static int bb__parsespent(void *data, char * line)
+static int FAST_FUNC bb__parsespent(void *data, char *line)
{
char *endptr;
char *p;
int i;
i = 0;
- do {
- p = ((char *) ((struct spwd *) data)) + sp_off[i];
+ while (1) {
+ p = (char *) data + sp_off[i];
if (i < 2) {
*((char **) p) = line;
line = strchr(line, ':');
if (!line) {
- break;
+ break; /* error */
}
} else {
- *((long *) p) = (long) strtoul(line, &endptr, 10);
-
+ *((long *) p) = strtoul(line, &endptr, 10);
if (endptr == line) {
- *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL)));
+ *((long *) p) = -1L;
}
-
line = endptr;
-
if (i == 8) {
- if (!*endptr) {
- return 0;
+ if (*line != '\0') {
+ break; /* error */
}
- break;
+ return 0; /* all ok */
}
-
- if (*endptr != ':') {
- break;
+ if (*line != ':') {
+ break; /* error */
}
-
}
-
- *line++ = 0;
- ++i;
- } while (1);
+ *line++ = '\0';
+ i++;
+ }
return EINVAL;
}
/**********************************************************************/
-/* Reads until if EOF, or until if finds a line which fits in the buffer
+/* Reads until EOF, or until it finds a line which fits in the buffer
* and for which the parser function succeeds.
*
- * Returns 0 on success and ENOENT for end-of-file (glibc concession).
+ * Returns 0 on success and ENOENT for end-of-file (glibc convention).
*/
-
-static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data,
- char *__restrict line_buff, size_t buflen, FILE *f)
+static int bb__pgsreader(
+ int FAST_FUNC (*parserfunc)(void *d, char *line),
+ void *data,
+ char *__restrict line_buff,
+ size_t buflen,
+ FILE *f)
{
- int line_len;
int skip;
int rv = ERANGE;
if (buflen < PWD_BUFFER_SIZE) {
errno = rv;
- } else {
- skip = 0;
- do {
- if (!fgets(line_buff, buflen, f)) {
- if (feof(f)) {
- rv = ENOENT;
- }
- break;
- }
+ return rv;
+ }
- line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */
- if (line_buff[line_len] == '\n') {
- line_buff[line_len] = 0;
- } else if (line_len + 2 == buflen) { /* line too long */
- ++skip;
- continue;
+ skip = 0;
+ while (1) {
+ if (!fgets(line_buff, buflen, f)) {
+ if (feof(f)) {
+ rv = ENOENT;
}
+ break;
+ }
- if (skip) {
- --skip;
+ {
+ 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;
continue;
- }
+ } /* else: a last line in the file, and it has no '\n' */
+ }
- /* 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 && (*line_buff != '#') && !isspace(*line_buff)) {
- 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;
- }
+ if (skip) {
+ /* This "line" is a remainder of overlong line, ignore */
+ skip = 0;
+ continue;
+ }
- if (!parserfunc(data, line_buff)) {
- rv = 0;
- break;
- }
+ /* 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;
}
- } while (1);
-
- }
+ if (parserfunc(data, line_buff) == 0) {
+ rv = 0;
+ break;
+ }
+ }
+ } /* while (1) */
return rv;
}