From 4c043852dc27aefa3b4502e6379b8a24ad398904 Mon Sep 17 00:00:00 2001 From: guenther Date: Thu, 25 Aug 2016 01:44:55 +0000 Subject: [PATCH] Replace name_{uid,gid}() with the libc routines user_from_uid() and group_from_gid(). Eliminate some superfluous strncpy() calls. ok millert@ --- bin/pax/ar_subs.c | 5 +- bin/pax/cache.c | 188 +-------------------------------------------- bin/pax/cache.h | 8 +- bin/pax/extern.h | 7 +- bin/pax/gen_subs.c | 13 ++-- bin/pax/options.c | 4 +- bin/pax/tar.c | 35 +++------ 7 files changed, 29 insertions(+), 231 deletions(-) diff --git a/bin/pax/ar_subs.c b/bin/pax/ar_subs.c index b95b1c2d317..3bf64ca35bc 100644 --- a/bin/pax/ar_subs.c +++ b/bin/pax/ar_subs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ar_subs.c,v 1.45 2015/03/19 05:14:24 guenther Exp $ */ +/* $OpenBSD: ar_subs.c,v 1.46 2016/08/25 01:44:55 guenther Exp $ */ /* $NetBSD: ar_subs.c,v 1.5 1995/03/21 09:07:06 cgd Exp $ */ /*- @@ -86,9 +86,6 @@ list(void) ((*frmt->st_rd)() < 0)) return; - if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0))) - return; - now = time(NULL); /* diff --git a/bin/pax/cache.c b/bin/pax/cache.c index a0d38cb6612..f3ecda5c2f2 100644 --- a/bin/pax/cache.c +++ b/bin/pax/cache.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cache.c,v 1.21 2014/05/24 18:51:00 guenther Exp $ */ +/* $OpenBSD: cache.c,v 1.22 2016/08/25 01:44:55 guenther Exp $ */ /* $NetBSD: cache.c,v 1.4 1995/03/21 09:07:10 cgd Exp $ */ /*- @@ -56,59 +56,9 @@ static int pwopn = 0; /* is password file open */ static int gropn = 0; /* is group file open */ -static UIDC **uidtb = NULL; /* uid to name cache */ -static GIDC **gidtb = NULL; /* gid to name cache */ static UIDC **usrtb = NULL; /* user name to uid cache */ static GIDC **grptb = NULL; /* group name to gid cache */ -/* - * uidtb_start - * creates an empty uidtb - * Return: - * 0 if ok, -1 otherwise - */ - -int -uidtb_start(void) -{ - static int fail = 0; - - if (uidtb != NULL) - return(0); - if (fail) - return(-1); - if ((uidtb = calloc(UID_SZ, sizeof(UIDC *))) == NULL) { - ++fail; - paxwarn(1, "Unable to allocate memory for user id cache table"); - return(-1); - } - return(0); -} - -/* - * gidtb_start - * creates an empty gidtb - * Return: - * 0 if ok, -1 otherwise - */ - -int -gidtb_start(void) -{ - static int fail = 0; - - if (gidtb != NULL) - return(0); - if (fail) - return(-1); - if ((gidtb = calloc(GID_SZ, sizeof(GIDC *))) == NULL) { - ++fail; - paxwarn(1, "Unable to allocate memory for group id cache table"); - return(-1); - } - return(0); -} - /* * usrtb_start * creates an empty usrtb @@ -157,138 +107,6 @@ grptb_start(void) return(0); } -/* - * name_uid() - * caches the name (if any) for the uid. If frc set, we always return the - * the stored name (if valid or invalid match). We use a simple hash table. - * Return - * Pointer to stored name (or a empty string) - */ - -char * -name_uid(uid_t uid, int frc) -{ - struct passwd *pw; - UIDC *ptr; - - if ((uidtb == NULL) && (uidtb_start() < 0)) - return(""); - - /* - * see if we have this uid cached - */ - ptr = uidtb[uid % UID_SZ]; - if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) { - /* - * have an entry for this uid - */ - if (frc || (ptr->valid == VALID)) - return(ptr->name); - return(""); - } - - /* - * No entry for this uid, we will add it - */ - if (!pwopn) { - setpassent(1); - ++pwopn; - } - if (ptr == NULL) - ptr = uidtb[uid % UID_SZ] = malloc(sizeof(UIDC)); - - if ((pw = getpwuid(uid)) == NULL) { - /* - * no match for this uid in the local password file - * a string that is the uid in numeric format - */ - if (ptr == NULL) - return(""); - ptr->uid = uid; - ptr->valid = INVALID; - (void)snprintf(ptr->name, sizeof(ptr->name), "%lu", - (unsigned long)uid); - if (frc == 0) - return(""); - } else { - /* - * there is an entry for this uid in the password file - */ - if (ptr == NULL) - return(pw->pw_name); - ptr->uid = uid; - (void)strlcpy(ptr->name, pw->pw_name, sizeof(ptr->name)); - ptr->valid = VALID; - } - return(ptr->name); -} - -/* - * name_gid() - * caches the name (if any) for the gid. If frc set, we always return the - * the stored name (if valid or invalid match). We use a simple hash table. - * Return - * Pointer to stored name (or a empty string) - */ - -char * -name_gid(gid_t gid, int frc) -{ - struct group *gr; - GIDC *ptr; - - if ((gidtb == NULL) && (gidtb_start() < 0)) - return(""); - - /* - * see if we have this gid cached - */ - ptr = gidtb[gid % GID_SZ]; - if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) { - /* - * have an entry for this gid - */ - if (frc || (ptr->valid == VALID)) - return(ptr->name); - return(""); - } - - /* - * No entry for this gid, we will add it - */ - if (!gropn) { - setgroupent(1); - ++gropn; - } - if (ptr == NULL) - ptr = gidtb[gid % GID_SZ] = malloc(sizeof(GIDC)); - - if ((gr = getgrgid(gid)) == NULL) { - /* - * no match for this gid in the local group file, put in - * a string that is the gid in numeric format - */ - if (ptr == NULL) - return(""); - ptr->gid = gid; - ptr->valid = INVALID; - (void)snprintf(ptr->name, sizeof(ptr->name), "%lu", - (unsigned long)gid); - if (frc == 0) - return(""); - } else { - /* - * there is an entry for this group in the group file - */ - if (ptr == NULL) - return(gr->gr_name); - ptr->gid = gid; - (void)strlcpy(ptr->name, gr->gr_name, sizeof(ptr->name)); - ptr->valid = VALID; - } - return(ptr->name); -} - /* * uid_name() * caches the uid for a given user name. We use a simple hash table. @@ -378,7 +196,7 @@ gid_name(char *name, gid_t *gid) * 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, GID_SZ)]; + ptr = grptb[st_hash(name, namelen, GNM_SZ)]; if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { if (ptr->valid == INVALID) return(-1); @@ -391,7 +209,7 @@ gid_name(char *name, gid_t *gid) ++gropn; } if (ptr == NULL) - ptr = grptb[st_hash(name, namelen, GID_SZ)] = + ptr = grptb[st_hash(name, namelen, GNM_SZ)] = malloc(sizeof(GIDC)); /* diff --git a/bin/pax/cache.h b/bin/pax/cache.h index def36e19b82..60f2f5cca7b 100644 --- a/bin/pax/cache.h +++ b/bin/pax/cache.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cache.h,v 1.7 2015/12/24 05:50:15 mmcc Exp $ */ +/* $OpenBSD: cache.h,v 1.8 2016/08/25 01:44:55 guenther Exp $ */ /* $NetBSD: cache.h,v 1.3 1995/03/21 09:07:12 cgd Exp $ */ /*- @@ -49,10 +49,8 @@ */ #define UNMLEN 32 /* >= user name found in any protocol */ #define GNMLEN 32 /* >= group name found in any protocol */ -#define UID_SZ 317 /* size of user_name/uid cache */ -#define UNM_SZ 317 /* size of user_name/uid cache */ -#define GID_SZ 251 /* size of gid cache */ -#define GNM_SZ 317 /* size of group name cache */ +#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 */ diff --git a/bin/pax/extern.h b/bin/pax/extern.h index 774487b900d..1207d79ff85 100644 --- a/bin/pax/extern.h +++ b/bin/pax/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.56 2016/08/23 06:00:28 guenther Exp $ */ +/* $OpenBSD: extern.h,v 1.57 2016/08/25 01:44:55 guenther Exp $ */ /* $NetBSD: extern.h,v 1.5 1996/03/26 23:54:16 mrg Exp $ */ /*- @@ -98,12 +98,8 @@ int buf_flush(int); /* * cache.c */ -int uidtb_start(void); -int gidtb_start(void); int usrtb_start(void); int grptb_start(void); -char * name_uid(uid_t, int); -char * name_gid(gid_t, int); int uid_name(char *, uid_t *); int gid_name(char *, gid_t *); @@ -306,7 +302,6 @@ int tar_opt(void); int tar_rd(ARCHD *, char *); int tar_wr(ARCHD *); int ustar_strd(void); -int ustar_stwr(void); int ustar_id(char *, int); int ustar_rd(ARCHD *, char *); int ustar_wr(ARCHD *); diff --git a/bin/pax/gen_subs.c b/bin/pax/gen_subs.c index 5e05a201b70..af56c16a7ae 100644 --- a/bin/pax/gen_subs.c +++ b/bin/pax/gen_subs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gen_subs.c,v 1.29 2016/08/14 04:47:52 guenther Exp $ */ +/* $OpenBSD: gen_subs.c,v 1.30 2016/08/25 01:44:55 guenther Exp $ */ /* $NetBSD: gen_subs.c,v 1.5 1995/03/21 09:07:26 cgd Exp $ */ /*- @@ -37,12 +37,15 @@ #include #include #include +#include +#include #include -#include -#include #include #include +#include +#include #include + #include "pax.h" #include "extern.h" @@ -104,8 +107,8 @@ ls_list(ARCHD *arcn, time_t now, FILE *fp) localtime(&(sbp->st_mtime))) == 0) f_date[0] = '\0'; (void)fprintf(fp, "%s%2u %-*.*s %-*.*s ", f_mode, sbp->st_nlink, - NAME_WIDTH, UT_NAMESIZE, name_uid(sbp->st_uid, 1), - NAME_WIDTH, UT_NAMESIZE, name_gid(sbp->st_gid, 1)); + NAME_WIDTH, UT_NAMESIZE, user_from_uid(sbp->st_uid, 0), + NAME_WIDTH, UT_NAMESIZE, group_from_gid(sbp->st_gid, 0)); /* * print device id's for devices, or sizes for other nodes diff --git a/bin/pax/options.c b/bin/pax/options.c index 6d4a599be74..d051af92a27 100644 --- a/bin/pax/options.c +++ b/bin/pax/options.c @@ -1,4 +1,4 @@ -/* $OpenBSD: options.c,v 1.97 2016/08/24 19:15:42 guenther Exp $ */ +/* $OpenBSD: options.c,v 1.98 2016/08/25 01:44:55 guenther Exp $ */ /* $NetBSD: options.c,v 1.6 1996/03/26 23:54:18 mrg Exp $ */ /*- @@ -126,7 +126,7 @@ FSUB fsub[] = { /* 5: POSIX USTAR */ {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd, - ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail, + ustar_rd, tar_endrd, no_op, ustar_wr, tar_endwr, tar_trail, tar_opt}, #ifdef SMALL diff --git a/bin/pax/tar.c b/bin/pax/tar.c index d6589107e61..bfccf280e14 100644 --- a/bin/pax/tar.c +++ b/bin/pax/tar.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tar.c,v 1.61 2016/08/14 18:30:33 guenther Exp $ */ +/* $OpenBSD: tar.c,v 1.62 2016/08/25 01:44:55 guenther Exp $ */ /* $NetBSD: tar.c,v 1.5 1995/03/21 09:07:49 cgd Exp $ */ /*- @@ -39,11 +39,14 @@ #include #include #include +#include #include -#include +#include #include -#include #include +#include +#include + #include "pax.h" #include "extern.h" #include "tar.h" @@ -678,21 +681,6 @@ ustar_strd(void) return(0); } -/* - * ustar_stwr() - * initialization for ustar write - * Return: - * 0 if ok, -1 otherwise - */ - -int -ustar_stwr(void) -{ - if ((uidtb_start() < 0) || (gidtb_start() < 0)) - return(-1); - return(0); -} - /* * ustar_id() * determine if a block given to us is a valid ustar header. We have to @@ -934,7 +922,7 @@ int ustar_wr(ARCHD *arcn) { HD_USTAR *hd; - char *pt; + char *pt, *name; char hdblk[sizeof(HD_USTAR)]; /* @@ -1094,11 +1082,10 @@ ustar_wr(ARCHD *arcn) ul_oct(arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3)) goto out; if (!Nflag) { - strncpy(hd->uname, name_uid(arcn->sb.st_uid, 0), sizeof(hd->uname)); - strncpy(hd->gname, name_gid(arcn->sb.st_gid, 0), sizeof(hd->gname)); - } else { - strncpy(hd->uname, "", sizeof(hd->uname)); - strncpy(hd->gname, "", sizeof(hd->gname)); + if ((name = user_from_uid(arcn->sb.st_uid, 1)) != NULL) + strncpy(hd->uname, name, sizeof(hd->uname)); + if ((name = group_from_gid(arcn->sb.st_gid, 1)) != NULL) + strncpy(hd->gname, name, sizeof(hd->gname)); } /* -- 2.20.1