-
-#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;
- 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)
-{
- int rv = -1;
-
-#if 0
- /* glibc does this check */
- if (!p || !f) {
- errno = EINVAL;
- return rv;
- }
-#endif
-
- if (fprintf(f, "%s:%s:%lu:",
- p->gr_name, p->gr_passwd,
- (unsigned long)(p->gr_gid)) >= 0
- ) {
- static const char format[] ALIGN1 = ",%s";
-
- char **m;
- const char *fmt;
-
- 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
-#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 */
- 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)
-{
- const char *fmt;
- long x;
- 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
- ) {
- goto DO_UNLOCK;
- }
-
- for (i = 0; i < sizeof(put_sp_off); i++) {
- fmt = "%ld:";
- x = *(long *)((char *)p + put_sp_off[i]);
- if (x == -1) {
- fmt += 3;
- }
- if (fprintf(stream, fmt, x) < 0) {
- goto DO_UNLOCK;
- }
- }
-
- if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
- goto DO_UNLOCK;
- }
-
- if (fputc('\n', stream) > 0) {
- rv = 0;
- }
-
- DO_UNLOCK:
- return rv;
-}
-#endif
-#endif /* USE_BB_SHADOW */
-
-/**********************************************************************/
-/* Internal functions */
-/**********************************************************************/
-
-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 */
-};
-
-static int FAST_FUNC bb__parsepwent(void *data, char *line)
-{
- char *endptr;
- char *p;
- int i;
-
- i = 0;
- while (1) {
- p = (char *) data + pw_off[i];
-
- if (i < 2 || i > 3) {
- *((char **) p) = line;
- if (i == 6) {
- return 0;
- }
- /* NOTE: glibc difference - glibc allows omission of
- * ':' seperators after the gid field if all remaining
- * entries are empty. We require all separators. */
- line = strchr(line, ':');
- if (!line) {
- 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;
- }
- }
-
- *line++ = '\0';
- i++;
- } /* while (1) */
-
- return -1;
-}
-
-/**********************************************************************/
-
-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 */
-};
-
-static int FAST_FUNC bb__parsegrent(void *data, char *line)
-{
- char *endptr;
- char *p;
- int i;
- char **members;
- char *end_of_buf;
-
- end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */
- i = 0;
- while (1) {
- p = (char *) data + gr_off[i];
-
- if (i < 2) {
- *((char **) p) = line;
- line = strchr(line, ':');
- if (!line) {
- break;
- }
- *line++ = '\0';
- i++;
- } 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.
- * 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
- * (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. */
- while (1) {
- *members++ = ++p;
- if (!--i)
- break;
- while (*++p)
- continue;
- }
- }
- *members = NULL;
-
- return 0;
- }
- } /* while (1) */
-
- ERR:
- return -1;
-}
-
-/**********************************************************************/
-
-#if ENABLE_USE_BB_SHADOW
-static const unsigned char sp_off[] ALIGN1 = {
- 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 FAST_FUNC bb__parsespent(void *data, char *line)
-{
- char *endptr;
- char *p;
- int i;
-
- i = 0;
- while (1) {
- p = (char *) data + sp_off[i];
- if (i < 2) {
- *((char **) p) = line;
- line = strchr(line, ':');
- if (!line) {
- break; /* error */
- }
- } else {
- *((long *) p) = strtoul(line, &endptr, 10);
- if (endptr == line) {
- *((long *) p) = -1L;
- }
- line = endptr;
- if (i == 8) {
- if (*line != '\0') {
- break; /* error */
- }
- return 0; /* all ok */
- }
- if (*line != ':') {
- break; /* error */
- }
- }
- *line++ = '\0';
- i++;
- }
-
- return EINVAL;
-}
-#endif
-
-/**********************************************************************/
-
-/* 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 convention).
- */
-static int bb__pgsreader(
- int FAST_FUNC (*parserfunc)(void *d, char *line),
- void *data,
- char *__restrict line_buff,
- size_t buflen,
- FILE *f)
-{
- int skip;
- int rv = ERANGE;
-
- if (buflen < PWD_BUFFER_SIZE) {
- errno = rv;
- return rv;
- }
-
- skip = 0;
- while (1) {
- if (!fgets(line_buff, buflen, f)) {
- if (feof(f)) {
- rv = ENOENT;
- }
- break;
- }
-
- {
- 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' */
- }
-
- if (skip) {
- /* This "line" is a remainder of overlong line, ignore */
- skip = 0;
- continue;
- }
-
- /* 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;
- }
- if (parserfunc(data, line_buff) == 0) {
- rv = 0;
- break;
- }
- }
- } /* while (1) */
-
- return rv;
-}