/* vi: set sw=4 ts=4: */
-/* Copyright (C) 2014 Tito Ragusa <farmatito@tiscali.it>
+/*
+ * Copyright (C) 2014 Tito Ragusa <farmatito@tiscali.it>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
* exit using the atexit function to make valgrind happy.
* 2) the passwd/group files:
* a) must contain the expected number of fields (as per count of field
- * delimeters ":") or we will complain with a error message.
+ * delimiters ":") or we will complain with a error message.
* b) leading and trailing whitespace in fields is stripped.
- * c) some fields are not allowed to be empty (e.g. username, uid/gid,
- * homedir, shell) and in this case NULL is returned and errno is
- * set to EINVAL. This behaviour could be easily changed by
- * modifying PW_DEF, GR_DEF, SP_DEF strings (uppercase
- * makes a field mandatory).
+ * c) some fields are not allowed to be empty (e.g. username, uid/gid),
+ * and in this case NULL is returned and errno is set to EINVAL.
+ * This behaviour could be easily changed by modifying PW_DEF, GR_DEF,
+ * SP_DEF strings (uppercase makes a field mandatory).
* d) the string representing uid/gid must be convertible by strtoXX
* functions, or errno is set to EINVAL.
* e) leading and trailing whitespace in group member names is stripped.
* implemented, if you need a particular missing function it should be
* easy to write it by using the internal common code.
*/
-
#include "libbb.h"
struct const_passdb {
* I = uid,gid, l = long maybe empty, m = members,
* r = reserved
*/
-#define PW_DEF "SsIIsSS"
+#define PW_DEF "SsIIsss"
#define GR_DEF "SsIm"
#define SP_DEF "Ssllllllr"
offsetof(struct passwd, pw_uid), /* 2 I */
offsetof(struct passwd, pw_gid), /* 3 I */
offsetof(struct passwd, pw_gecos), /* 4 s */
- offsetof(struct passwd, pw_dir), /* 5 S */
- offsetof(struct passwd, pw_shell) /* 6 S */
+ offsetof(struct passwd, pw_dir), /* 5 s */
+ offsetof(struct passwd, pw_shell) /* 6 s */
},
sizeof(PW_DEF)-1, sizeof(struct passwd)
};
#if ENABLE_FEATURE_CLEAN_UP
static void free_static(void)
{
- free(S.db[0].malloced);
+ free(S.db[0].malloced);
free(S.db[1].malloced);
# if ENABLE_USE_BB_SHADOW
free(S.db[2].malloced);
/* Internal functions */
/* Divide the passwd/group/shadow record in fields
- * by substituting the given delimeter
+ * by substituting the given delimiter
* e.g. ':' or ',' with '\0'.
* Returns the number of fields found.
* Strips leading and trailing whitespace in fields.
static char *parse_common(FILE *fp, struct passdb *db,
const char *key, int field_pos)
{
- int count = 0;
char *buf;
while ((buf = xmalloc_fgetline(fp)) != NULL) {
- count++;
/* Skip empty lines, comment lines */
if (buf[0] == '\0' || buf[0] == '#')
goto free_and_next;
if (tokenize(buf, ':') != db->numfields) {
/* number of fields is wrong */
- bb_error_msg("bad record at %s:%u", db->filename, count);
+ bb_error_msg("%s: bad record", db->filename);
goto free_and_next;
}
return errno;
}
+static void* massage_data_for_non_r_func(struct passdb *db, char *buf)
+{
+ if (!buf)
+ return NULL;
+
+ free(db->malloced);
+ /* We enlarge buf and move string data up, freeing space
+ * for struct passwd/group/spwd at the beginning. This way,
+ * entire result of getXXnam is in a single malloced block.
+ * This enables easy creation of xmalloc_getpwnam() API.
+ */
+ db->malloced = buf = xrealloc(buf, db->size_of + S.string_size);
+ memmove(buf + db->size_of, buf, S.string_size);
+ return convert_to_struct(db, buf + db->size_of, buf);
+}
+
/****** getXXnam/id_r */
static int FAST_FUNC getXXnam_r(const char *name, uintptr_t db_and_field_pos,
}
#endif
+#ifdef UNUSED
/****** getXXent_r */
static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen,
*result = struct_buf;
return getXXent_r(0, buffer, buflen, result);
}
+#endif
-/****** getXXnam/id */
+/****** getXXent */
-static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
+static void* FAST_FUNC getXXent(uintptr_t db_idx)
{
char *buf;
- void *result;
- struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
-
- result = NULL;
+ struct passdb *db = &get_S()->db[db_idx];
if (!db->fp) {
db->fp = fopen_for_read(db->filename);
close_on_exec_on(fileno(db->fp));
}
- buf = parse_common(db->fp, db, name, db_and_field_pos & 3);
- if (buf) {
- free(db->malloced);
- /* We enlarge buf and move string data up, freeing space
- * for struct passwd/group/spwd at the beginning. This way,
- * entire result of getXXnam is in a single malloced block.
- * This enables easy creation of xmalloc_getpwnam() API.
- */
- db->malloced = buf = xrealloc(buf, db->size_of + S.string_size);
- memmove(buf + db->size_of, buf, S.string_size);
- result = convert_to_struct(db, buf + db->size_of, buf);
- }
- return result;
+ buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
+ return massage_data_for_non_r_func(db, buf);
+}
+
+struct passwd* FAST_FUNC getpwent(void)
+{
+ return getXXent(0);
+}
+
+/****** getXXnam/id */
+
+static void* FAST_FUNC getXXnam(const char *name, unsigned db_and_field_pos)
+{
+ char *buf;
+ struct passdb *db = &get_S()->db[db_and_field_pos >> 2];
+
+ buf = parse_file(db, name, db_and_field_pos & 3);
+ return massage_data_for_non_r_func(db, buf);
}
struct passwd* FAST_FUNC getpwnam(const char *name)