summaryrefslogtreecommitdiffstats
path: root/libutil/pw_util.c
diff options
context:
space:
mode:
authorIan Lepore <ian@FreeBSD.org>2018-07-26 18:34:38 +0000
committerIan Lepore <ian@FreeBSD.org>2018-07-26 18:34:38 +0000
commitcec26ff1cefd6235cfbefd07302a918e7217b0b1 (patch)
tree19369a3f8c77d6cc7690ba6ec168bea78d936c00 /libutil/pw_util.c
parent699a28cfd315d63c32b4c309d0b6cbd9197a6ee4 (diff)
downloadpw-darwin-cec26ff1cefd6235cfbefd07302a918e7217b0b1.tar.gz
pw-darwin-cec26ff1cefd6235cfbefd07302a918e7217b0b1.tar.zst
pw-darwin-cec26ff1cefd6235cfbefd07302a918e7217b0b1.zip
Make pw_scan(3) more compatible with getpwent(3) et. al. when processing
data from /etc/passwd rather than /etc/master.passwd. The libc getpwent(3) and related functions automatically read master.passwd when run by root, or passwd when run by a non-root user. When run by non- root, getpwent() copes with the missing data by setting the corresponding fields in the passwd struct to known values (zeroes for numbers, or a pointer to an empty string for literals). When libutil's pw_scan(3) was used to parse a line without the root-accessible data, it was leaving garbage in the corresponding fields. These changes rename the static pw_init() function used by getpwent() and friends to __pw_initpwd(), and move it into pw_scan.c so that common init code can be shared between libc and libutil. pw_scan(3) now calls __pw_initpwd() before __pw_scan(), just like the getpwent() family does, so that reading an arbitrary passwd file in either format and parsing it with pw_scan(3) returns the same results as getpwent(3) would. This also adds a new pw_initpwd(3) function to libutil, so that code which creates passwd structs from scratch in some manner that doesn't involve pw_scan() can initialize the struct to the values expected by lots of existing code, which doesn't expect to encounter NULL pointers or garbage values in some fields.
Diffstat (limited to 'libutil/pw_util.c')
-rw-r--r--libutil/pw_util.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/libutil/pw_util.c b/libutil/pw_util.c
index 112b6d8..1659c97 100644
--- a/libutil/pw_util.c
+++ b/libutil/pw_util.c
@@ -652,8 +652,16 @@ pw_dup(const struct passwd *pw)
#include "pw_scan.h"
/*
- * Wrapper around an internal libc function
+ * Wrapper around some internal libc functions.
*/
+
+void
+pw_initpwd(struct passwd *pw)
+{
+
+ __pw_initpwd(pw);
+}
+
struct passwd *
pw_scan(const char *line, int flags)
{
@@ -662,6 +670,7 @@ pw_scan(const char *line, int flags)
if ((bp = strdup(line)) == NULL)
return (NULL);
+ __pw_initpwd(&pw);
if (!__pw_scan(bp, &pw, flags)) {
free(bp);
return (NULL);