This removes cache.c from pax in favor of using the new uid_from_user()
and gid_from_group() functions in libc. I've added explicit calls
to setpassent() and setgroupent() since they are no longer implicitly
called.
- todd
Index: bin/pax/Makefile
===================================================================
RCS file: /cvs/src/bin/pax/Makefile,v
retrieving revision 1.12
diff -u -p -u -r1.12 Makefile
--- bin/pax/Makefile 23 Aug 2016 06:00:28 -0000 1.12
+++ bin/pax/Makefile 10 Sep 2018 00:46:52 -0000
@@ -2,7 +2,7 @@
WARNINGS=Yes
PROG= pax
-SRCS= ar_io.c ar_subs.c buf_subs.c cache.c cpio.c file_subs.c ftree.c\
+SRCS= ar_io.c ar_subs.c buf_subs.c cpio.c file_subs.c ftree.c\
gen_subs.c getoldopt.c options.c pat_rep.c pax.c sel_subs.c tables.c\
tar.c tty_subs.c
MAN= pax.1 tar.1 cpio.1
Index: bin/pax/cache.c
===================================================================
RCS file: bin/pax/cache.c
diff -N bin/pax/cache.c
--- bin/pax/cache.c 26 Aug 2016 04:08:18 -0000 1.23
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,267 +0,0 @@
-/* $OpenBSD: cache.c,v 1.23 2016/08/26 04:08:18 guenther Exp $ */
-/* $NetBSD: cache.c,v 1.4 1995/03/21 09:07:10 cgd Exp $ */
-
-/*-
- * Copyright (c) 1992 Keith Muller.
- * Copyright (c) 1992, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Keith Muller of the University of California, San Diego.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <grp.h>
-#include <pwd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "pax.h"
-#include "extern.h"
-
-/*
- * Constants and data structures used to implement group and password file
- * caches. Traditional passwd/group cache routines perform quite poorly with
- * archives. The chances of hitting a valid lookup with an archive is quite a
- * bit worse than with files already resident on the file system. These misses
- * create a MAJOR performance cost. To address this problem, these routines
- * cache both hits and misses.
- *
- * NOTE: name lengths must be as large as those stored in ANY PROTOCOL and
- * as stored in the passwd and group files. CACHE SIZES MUST BE PRIME
- */
-#define UNMLEN 32 /* >= user name found in any protocol */
-#define GNMLEN 32 /* >= group name found in any protocol */
-#define UNM_SZ 317 /* size of uid_name() cache */
-#define GNM_SZ 317 /* size of gid_name() cache */
-#define VALID 1 /* entry and name are valid */
-#define INVALID 2 /* entry valid, name NOT valid */
-
-/*
- * Node structures used in the user, group, uid, and gid caches.
- */
-
-typedef struct uidc {
- int valid; /* is this a valid or a miss entry */
- char name[UNMLEN]; /* uid name */
- uid_t uid; /* cached uid */
-} UIDC;
-
-typedef struct gidc {
- int valid; /* is this a valid or a miss entry */
- char name[GNMLEN]; /* gid name */
- gid_t gid; /* cached gid */
-} GIDC;
-
-
-/*
- * routines that control user, group, uid and gid caches (for the archive
- * member print routine).
- * IMPORTANT:
- * these routines cache BOTH hits and misses, a major performance improvement
- */
-
-static int pwopn = 0; /* is password file open */
-static int gropn = 0; /* is group file open */
-static UIDC **usrtb = NULL; /* user name to uid cache */
-static GIDC **grptb = NULL; /* group name to gid cache */
-
-/*
- * usrtb_start
- * creates an empty usrtb
- * Return:
- * 0 if ok, -1 otherwise
- */
-
-int
-usrtb_start(void)
-{
- static int fail = 0;
-
- if (usrtb != NULL)
- return(0);
- if (fail)
- return(-1);
- if ((usrtb = calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
- ++fail;
- paxwarn(1, "Unable to allocate memory for user name cache
table");
- return(-1);
- }
- return(0);
-}
-
-/*
- * grptb_start
- * creates an empty grptb
- * Return:
- * 0 if ok, -1 otherwise
- */
-
-int
-grptb_start(void)
-{
- static int fail = 0;
-
- if (grptb != NULL)
- return(0);
- if (fail)
- return(-1);
- if ((grptb = calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
- ++fail;
- paxwarn(1,"Unable to allocate memory for group name cache
table");
- return(-1);
- }
- return(0);
-}
-
-/*
- * uid_name()
- * caches the uid for a given user name. We use a simple hash table.
- * Return
- * the uid (if any) for a user name, or a -1 if no match can be found
- */
-
-int
-uid_name(char *name, uid_t *uid)
-{
- struct passwd *pw;
- UIDC *ptr;
- int namelen;
-
- /*
- * return -1 for mangled names
- */
- if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
- return(-1);
- if ((usrtb == NULL) && (usrtb_start() < 0))
- return(-1);
-
- /*
- * look up in hash table, if found and valid return the uid,
- * if found and invalid, return a -1
- */
- ptr = usrtb[st_hash(name, namelen, UNM_SZ)];
- if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
- if (ptr->valid == INVALID)
- return(-1);
- *uid = ptr->uid;
- return(0);
- }
-
- if (!pwopn) {
- setpassent(1);
- ++pwopn;
- }
-
- if (ptr == NULL)
- ptr = usrtb[st_hash(name, namelen, UNM_SZ)] =
- malloc(sizeof(UIDC));
-
- /*
- * no match, look it up, if no match store it as an invalid entry,
- * or store the matching uid
- */
- if (ptr == NULL) {
- if ((pw = getpwnam(name)) == NULL)
- return(-1);
- *uid = pw->pw_uid;
- return(0);
- }
- (void)strlcpy(ptr->name, name, sizeof(ptr->name));
- if ((pw = getpwnam(name)) == NULL) {
- ptr->valid = INVALID;
- return(-1);
- }
- ptr->valid = VALID;
- *uid = ptr->uid = pw->pw_uid;
- return(0);
-}
-
-/*
- * gid_name()
- * caches the gid for a given group name. We use a simple hash table.
- * Return
- * the gid (if any) for a group name, or a -1 if no match can be found
- */
-
-int
-gid_name(char *name, gid_t *gid)
-{
- struct group *gr;
- GIDC *ptr;
- int namelen;
-
- /*
- * return -1 for mangled names
- */
- if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
- return(-1);
- if ((grptb == NULL) && (grptb_start() < 0))
- return(-1);
-
- /*
- * look up in hash table, if found and valid return the uid,
- * if found and invalid, return a -1
- */
- ptr = grptb[st_hash(name, namelen, GNM_SZ)];
- if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
- if (ptr->valid == INVALID)
- return(-1);
- *gid = ptr->gid;
- return(0);
- }
-
- if (!gropn) {
- setgroupent(1);
- ++gropn;
- }
- if (ptr == NULL)
- ptr = grptb[st_hash(name, namelen, GNM_SZ)] =
- malloc(sizeof(GIDC));
-
- /*
- * no match, look it up, if no match store it as an invalid entry,
- * or store the matching gid
- */
- if (ptr == NULL) {
- if ((gr = getgrnam(name)) == NULL)
- return(-1);
- *gid = gr->gr_gid;
- return(0);
- }
-
- (void)strlcpy(ptr->name, name, sizeof(ptr->name));
- if ((gr = getgrnam(name)) == NULL) {
- ptr->valid = INVALID;
- return(-1);
- }
- ptr->valid = VALID;
- *gid = ptr->gid = gr->gr_gid;
- return(0);
-}
Index: bin/pax/extern.h
===================================================================
RCS file: /cvs/src/bin/pax/extern.h,v
retrieving revision 1.58
diff -u -p -u -r1.58 extern.h
--- bin/pax/extern.h 12 Sep 2017 17:11:11 -0000 1.58
+++ bin/pax/extern.h 10 Sep 2018 00:46:52 -0000
@@ -96,14 +96,6 @@ int buf_fill(void);
int buf_flush(int);
/*
- * cache.c
- */
-int usrtb_start(void);
-int grptb_start(void);
-int uid_name(char *, uid_t *);
-int gid_name(char *, gid_t *);
-
-/*
* cpio.c
*/
int cpio_strd(void);
@@ -301,7 +293,6 @@ int tar_id(char *, int);
int tar_opt(void);
int tar_rd(ARCHD *, char *);
int tar_wr(ARCHD *);
-int ustar_strd(void);
int ustar_id(char *, int);
int ustar_rd(ARCHD *, char *);
int ustar_wr(ARCHD *);
Index: bin/pax/options.c
===================================================================
RCS file: /cvs/src/bin/pax/options.c,v
retrieving revision 1.101
diff -u -p -u -r1.101 options.c
--- bin/pax/options.c 26 Dec 2016 23:43:52 -0000 1.101
+++ bin/pax/options.c 10 Sep 2018 00:46:52 -0000
@@ -201,7 +201,7 @@ FSUB fsub[] = {
tar_opt},
/* 5: POSIX USTAR */
- {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
+ {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, no_op,
ustar_rd, tar_endrd, no_op, ustar_wr, tar_endwr, tar_trail,
tar_opt},
Index: bin/pax/pax.c
===================================================================
RCS file: /cvs/src/bin/pax/pax.c,v
retrieving revision 1.51
diff -u -p -u -r1.51 pax.c
--- bin/pax/pax.c 8 Dec 2017 17:04:14 -0000 1.51
+++ bin/pax/pax.c 10 Sep 2018 16:34:32 -0000
@@ -44,7 +44,9 @@
#include <errno.h>
#include <err.h>
#include <fcntl.h>
+#include <grp.h>
#include <paths.h>
+#include <pwd.h>
#include <stdio.h>
#include "pax.h"
@@ -248,6 +250,12 @@ main(int argc, char **argv)
memcpy(tempfile, tmpdir, tdlen);
tempbase = tempfile + tdlen;
*tempbase++ = '/';
+
+ /*
+ * keep passwd and group files open for faster lookups.
+ */
+ setpassent(1);
+ setgroupent(1);
/*
* parse options, determine operational mode, general init
Index: bin/pax/sel_subs.c
===================================================================
RCS file: /cvs/src/bin/pax/sel_subs.c,v
retrieving revision 1.26
diff -u -p -u -r1.26 sel_subs.c
--- bin/pax/sel_subs.c 26 Aug 2016 04:20:38 -0000 1.26
+++ bin/pax/sel_subs.c 10 Sep 2018 00:46:52 -0000
@@ -133,7 +133,6 @@ usr_add(char *str)
{
u_int indx;
USRT *pt;
- struct passwd *pw;
uid_t uid;
/*
@@ -156,11 +155,10 @@ usr_add(char *str)
*/
if ((str[0] == '\\') && (str[1] == '#'))
++str;
- if ((pw = getpwnam(str)) == NULL) {
+ if (uid_from_user(str, &uid) < 0) {
paxwarn(1, "Unable to find uid for user: %s", str);
return(-1);
}
- uid = (uid_t)pw->pw_uid;
} else
uid = (uid_t)strtoul(str+1, NULL, 10);
endpwent();
@@ -230,7 +228,6 @@ grp_add(char *str)
{
u_int indx;
GRPT *pt;
- struct group *gr;
gid_t gid;
/*
@@ -245,7 +242,7 @@ grp_add(char *str)
}
/*
- * figure out user spec
+ * figure out group spec
*/
if (str[0] != '#') {
/*
@@ -253,11 +250,10 @@ grp_add(char *str)
*/
if ((str[0] == '\\') && (str[1] == '#'))
++str;
- if ((gr = getgrnam(str)) == NULL) {
+ if (gid_from_group(str, &gid) < 0) {
paxwarn(1,"Cannot determine gid for group name: %s",
str);
return(-1);
}
- gid = (gid_t)gr->gr_gid;
} else
gid = (gid_t)strtoul(str+1, NULL, 10);
endgrent();
Index: bin/pax/tar.c
===================================================================
RCS file: /cvs/src/bin/pax/tar.c,v
retrieving revision 1.66
diff -u -p -u -r1.66 tar.c
--- bin/pax/tar.c 16 Sep 2017 07:42:34 -0000 1.66
+++ bin/pax/tar.c 10 Sep 2018 00:46:52 -0000
@@ -666,21 +666,6 @@ tar_wr(ARCHD *arcn)
*/
/*
- * ustar_strd()
- * initialization for ustar read
- * Return:
- * 0 if ok, -1 otherwise
- */
-
-int
-ustar_strd(void)
-{
- if ((usrtb_start() < 0) || (grptb_start() < 0))
- return(-1);
- return(0);
-}
-
-/*
* ustar_id()
* determine if a block given to us is a valid ustar header. We have to
* be on the lookout for those pesky blocks of all zero's
@@ -813,10 +798,10 @@ reset:
* the posix spec wants).
*/
hd->gname[sizeof(hd->gname) - 1] = '\0';
- if (Nflag || gid_name(hd->gname, &(arcn->sb.st_gid)) < 0)
+ if (Nflag || gid_from_group(hd->gname, &(arcn->sb.st_gid)) < 0)
arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT);
hd->uname[sizeof(hd->uname) - 1] = '\0';
- if (Nflag || uid_name(hd->uname, &(arcn->sb.st_uid)) < 0)
+ if (Nflag || uid_from_user(hd->uname, &(arcn->sb.st_uid)) < 0)
arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT);
/*
@@ -921,8 +906,8 @@ int
ustar_wr(ARCHD *arcn)
{
HD_USTAR *hd;
- char *pt, *name;
- char hdblk[sizeof(HD_USTAR)];
+ const char *name;
+ char *pt, hdblk[sizeof(HD_USTAR)];
/*
* check for those file system types ustar cannot store
@@ -1051,7 +1036,7 @@ ustar_wr(ARCHD *arcn)
*/
if (ul_oct(arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3)) {
if (uid_nobody == 0) {
- if (uid_name("nobody", &uid_nobody) == -1)
+ if (uid_from_user("nobody", &uid_nobody) == -1)
goto out;
}
if (uid_warn != arcn->sb.st_uid) {
@@ -1065,7 +1050,7 @@ ustar_wr(ARCHD *arcn)
}
if (ul_oct(arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3)) {
if (gid_nobody == 0) {
- if (gid_name("nobody", &gid_nobody) == -1)
+ if (gid_from_group("nobody", &gid_nobody) == -1)
goto out;
}
if (gid_warn != arcn->sb.st_gid) {