From owner-svn-src-all@freebsd.org Tue Jan 7 18:42:55 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 9AC751EF4F4; Tue, 7 Jan 2020 18:42:55 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47sh8H3dZYz4SWW; Tue, 7 Jan 2020 18:42:55 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 77C623479; Tue, 7 Jan 2020 18:42:55 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 007IgtlW054736; Tue, 7 Jan 2020 18:42:55 GMT (envelope-from ian@FreeBSD.org) Received: (from ian@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 007IgrB1054727; Tue, 7 Jan 2020 18:42:53 GMT (envelope-from ian@FreeBSD.org) Message-Id: <202001071842.007IgrB1054727@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ian set sender to ian@FreeBSD.org using -f From: Ian Lepore Date: Tue, 7 Jan 2020 18:42:53 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r356461 - in stable/11/lib: libc/gen libutil X-SVN-Group: stable-11 X-SVN-Commit-Author: ian X-SVN-Commit-Paths: in stable/11/lib: libc/gen libutil X-SVN-Commit-Revision: 356461 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Jan 2020 18:42:55 -0000 Author: ian Date: Tue Jan 7 18:42:53 2020 New Revision: 356461 URL: https://svnweb.freebsd.org/changeset/base/356461 Log: MFC r336746, r336779 r336746: 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. r336779: Stop exporting __pw_scan and __pw_initpwd as freebsd-private libc functions for use in libutil, and instead compile the small amount of common code directly into libutil with a .PATH reachover. Modified: stable/11/lib/libc/gen/Symbol.map stable/11/lib/libc/gen/getpwent.c stable/11/lib/libc/gen/pw_scan.c stable/11/lib/libc/gen/pw_scan.h stable/11/lib/libutil/Makefile stable/11/lib/libutil/libutil.h stable/11/lib/libutil/pw_util.3 stable/11/lib/libutil/pw_util.c Directory Properties: stable/11/ (props changed) Modified: stable/11/lib/libc/gen/Symbol.map ============================================================================== --- stable/11/lib/libc/gen/Symbol.map Tue Jan 7 18:08:09 2020 (r356460) +++ stable/11/lib/libc/gen/Symbol.map Tue Jan 7 18:42:53 2020 (r356461) @@ -518,7 +518,6 @@ FBSDprivate_1.0 { __opendir2; __pause; _pause; - __pw_scan; /* Used by (at least) libutil */ __raise; _raise; __sleep; Modified: stable/11/lib/libc/gen/getpwent.c ============================================================================== --- stable/11/lib/libc/gen/getpwent.c Tue Jan 7 18:08:09 2020 (r356460) +++ stable/11/lib/libc/gen/getpwent.c Tue Jan 7 18:42:53 2020 (r356461) @@ -94,8 +94,6 @@ int __pw_match_entry(const char *, size_t, enum nss_lo const char *, uid_t); int __pw_parse_entry(char *, size_t, struct passwd *, int, int *errnop); -static void pwd_init(struct passwd *); - union key { const char *name; uid_t uid; @@ -525,7 +523,7 @@ getpwent_r(struct passwd *pwd, char *buffer, size_t bu }; int rv, ret_errno; - pwd_init(pwd); + __pw_initpwd(pwd); ret_errno = 0; *result = NULL; rv = _nsdispatch(result, dtab, NSDB_PASSWD, "getpwent_r", defaultsrc, @@ -564,7 +562,7 @@ getpwnam_r(const char *name, struct passwd *pwd, char }; int rv, ret_errno; - pwd_init(pwd); + __pw_initpwd(pwd); ret_errno = 0; *result = NULL; rv = _nsdispatch(result, dtab, NSDB_PASSWD, "getpwnam_r", defaultsrc, @@ -603,7 +601,7 @@ getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer }; int rv, ret_errno; - pwd_init(pwd); + __pw_initpwd(pwd); ret_errno = 0; *result = NULL; rv = _nsdispatch(result, dtab, NSDB_PASSWD, "getpwuid_r", defaultsrc, @@ -615,23 +613,6 @@ getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer } -static void -pwd_init(struct passwd *pwd) -{ - static char nul[] = ""; - - memset(pwd, 0, sizeof(*pwd)); - pwd->pw_uid = (uid_t)-1; /* Considered least likely to lead to */ - pwd->pw_gid = (gid_t)-1; /* a security issue. */ - pwd->pw_name = nul; - pwd->pw_passwd = nul; - pwd->pw_class = nul; - pwd->pw_gecos = nul; - pwd->pw_dir = nul; - pwd->pw_shell = nul; -} - - static struct passwd pwd; static char *pwd_storage; static size_t pwd_storage_size; @@ -1612,7 +1593,7 @@ compat_redispatch(struct compat_state *st, enum nss_lo for (i = 0; i < (int)(nitems(dtab) - 1); i++) dtab[i].mdata = (void *)lookup_how; more: - pwd_init(pwd); + __pw_initpwd(pwd); switch (lookup_how) { case nss_lt_all: rv = _nsdispatch(&discard, dtab, NSDB_PASSWD_COMPAT, Modified: stable/11/lib/libc/gen/pw_scan.c ============================================================================== --- stable/11/lib/libc/gen/pw_scan.c Tue Jan 7 18:08:09 2020 (r356460) +++ stable/11/lib/libc/gen/pw_scan.c Tue Jan 7 18:42:53 2020 (r356461) @@ -27,10 +27,10 @@ * SUCH DAMAGE. */ +#include #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)pw_scan.c 8.3 (Berkeley) 4/2/94"; +static char sccsid[] __unused = "@(#)pw_scan.c 8.3 (Berkeley) 4/2/94"; #endif /* LIBC_SCCS and not lint */ -#include __FBSDID("$FreeBSD$"); /* @@ -66,6 +66,22 @@ __FBSDID("$FreeBSD$"); * system lag behind. */ static int pw_big_ids_warning = 0; + +void +__pw_initpwd(struct passwd *pwd) +{ + static char nul[] = ""; + + memset(pwd, 0, sizeof(*pwd)); + pwd->pw_uid = (uid_t)-1; /* Considered least likely to lead to */ + pwd->pw_gid = (gid_t)-1; /* a security issue. */ + pwd->pw_name = nul; + pwd->pw_passwd = nul; + pwd->pw_class = nul; + pwd->pw_gecos = nul; + pwd->pw_dir = nul; + pwd->pw_shell = nul; +} int __pw_scan(char *bp, struct passwd *pw, int flags) Modified: stable/11/lib/libc/gen/pw_scan.h ============================================================================== --- stable/11/lib/libc/gen/pw_scan.h Tue Jan 7 18:08:09 2020 (r356460) +++ stable/11/lib/libc/gen/pw_scan.h Tue Jan 7 18:42:53 2020 (r356461) @@ -33,4 +33,5 @@ #define _PWSCAN_MASTER 0x01 #define _PWSCAN_WARN 0x02 +extern void __pw_initpwd(struct passwd *); extern int __pw_scan(char *, struct passwd *, int); Modified: stable/11/lib/libutil/Makefile ============================================================================== --- stable/11/lib/libutil/Makefile Tue Jan 7 18:08:09 2020 (r356460) +++ stable/11/lib/libutil/Makefile Tue Jan 7 18:42:53 2020 (r356461) @@ -6,6 +6,8 @@ SHLIBDIR?= /lib .include +.PATH: ${SRCTOP}/lib/libc/gen # for pw_scan.c + LIB= util SHLIB_MAJOR= 9 @@ -15,8 +17,8 @@ SRCS= _secure_path.c auth.c expand_number.c flopen.c f kinfo_getvmobject.c kld.c \ login_auth.c login_cap.c \ login_class.c login_crypt.c login_ok.c login_times.c login_tty.c \ - pidfile.c property.c pty.c pw_util.c quotafile.c realhostname.c \ - stub.c trimdomain.c uucplock.c + pidfile.c property.c pty.c pw_scan.c pw_util.c quotafile.c \ + realhostname.c stub.c trimdomain.c uucplock.c INCS= libutil.h login_cap.h CFLAGS+= -DLIBC_SCCS Modified: stable/11/lib/libutil/libutil.h ============================================================================== --- stable/11/lib/libutil/libutil.h Tue Jan 7 18:08:09 2020 (r356460) +++ stable/11/lib/libutil/libutil.h Tue Jan 7 18:42:53 2020 (r356461) @@ -152,6 +152,7 @@ int pw_edit(int _notsetuid); int pw_equal(const struct passwd *_pw1, const struct passwd *_pw2); void pw_fini(void); int pw_init(const char *_dir, const char *_master); +void pw_initpwd(struct passwd *_pw); char *pw_make(const struct passwd *_pw); char *pw_make_v7(const struct passwd *_pw); int pw_mkdb(const char *_user); Modified: stable/11/lib/libutil/pw_util.3 ============================================================================== --- stable/11/lib/libutil/pw_util.3 Tue Jan 7 18:08:09 2020 (r356460) +++ stable/11/lib/libutil/pw_util.3 Tue Jan 7 18:42:53 2020 (r356461) @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd July 02, 2015 +.Dd July 26, 2018 .Dt PW_UTIL 3 .Os .Sh NAME @@ -59,6 +59,8 @@ .Fn pw_fini "void" .Ft int .Fn pw_init "const char *dir" const char *master" +.Ft void +.Fn pw_initpwd "struct passwd *pw" .Ft "char *" .Fn pw_make "const struct passwd *pw" .Ft "char *" @@ -179,7 +181,7 @@ if any. .Pp The .Fn pw_init -initialize the static variable representing the path a password file. +initializes the static variable representing the path to a password file. .Fa dir is the directory where the password file is located. If set to @@ -192,6 +194,18 @@ If set to .Dv NULL? it will default to .Pa master.passwd +.Pp +The +.Fn pw_initpwd +function initializes the +.Vt passwd +struct to canonical values. +The entire structure is zeroed, then +.Va pw_uid +and +.Va pw_gid +are set to -1, and all string pointers are set to point at +an internally-defined zero-length string. .Pp The .Fn pw_make Modified: stable/11/lib/libutil/pw_util.c ============================================================================== --- stable/11/lib/libutil/pw_util.c Tue Jan 7 18:08:09 2020 (r356460) +++ stable/11/lib/libutil/pw_util.c Tue Jan 7 18:42:53 2020 (r356461) @@ -654,8 +654,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) { @@ -664,6 +672,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);