Import in latest mtree from NetBSD to fix a bug in parsing group files

that jmg fixed upstream.

MFC after:	1 week
Sponsored by:	DARPA, AFRL
This commit is contained in:
brooks 2014-10-28 17:52:26 +00:00
parent df22f3557c
commit d288d777a1
5 changed files with 116 additions and 98 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: create.c,v 1.72 2013/10/17 17:22:59 christos Exp $ */
/* $NetBSD: create.c,v 1.73 2014/04/24 17:22:41 christos Exp $ */
/*-
* Copyright (c) 1989, 1993
@ -38,7 +38,7 @@
#if 0
static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: create.c,v 1.72 2013/10/17 17:22:59 christos Exp $");
__RCSID("$NetBSD: create.c,v 1.73 2014/04/24 17:22:41 christos Exp $");
#endif
#endif /* not lint */
@ -91,13 +91,14 @@ static u_long flags;
#endif
static int dcmp(const FTSENT *FTS_CONST *, const FTSENT *FTS_CONST *);
static void output(int, int *, const char *, ...)
__attribute__((__format__(__printf__, 3, 4)));
static int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
static void statf(int, FTSENT *);
static void output(FILE *, int, int *, const char *, ...)
__printflike(4, 5);
static int statd(FILE *, FTS *, FTSENT *, uid_t *, gid_t *, mode_t *,
u_long *);
static void statf(FILE *, int, FTSENT *);
void
cwalk(void)
cwalk(FILE *fp)
{
FTS *t;
FTSENT *p;
@ -121,7 +122,7 @@ cwalk(void)
}
if (!nflag)
printf(
fprintf(fp,
"#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n"
"#\t date: %s",
user, host, fullpath, ctime(&clocktime));
@ -142,21 +143,21 @@ cwalk(void)
switch(p->fts_info) {
case FTS_D:
if (!bflag)
printf("\n");
fprintf(fp, "\n");
if (!nflag)
printf("# %s\n", p->fts_path);
statd(t, p, &uid, &gid, &mode, &flags);
statf(indent, p);
fprintf(fp, "# %s\n", p->fts_path);
statd(fp, t, p, &uid, &gid, &mode, &flags);
statf(fp, indent, p);
break;
case FTS_DP:
if (p->fts_level > 0)
if (!nflag)
printf("%*s# %s\n", indent, "",
fprintf(fp, "%*s# %s\n", indent, "",
p->fts_path);
if (p->fts_level > 0 || flavor == F_FREEBSD9) {
printf("%*s..\n", indent, "");
fprintf(fp, "%*s..\n", indent, "");
if (!bflag)
printf("\n");
fprintf(fp, "\n");
}
break;
case FTS_DNR:
@ -167,7 +168,7 @@ cwalk(void)
break;
default:
if (!dflag)
statf(indent, p);
statf(fp, indent, p);
break;
}
@ -178,7 +179,7 @@ cwalk(void)
}
static void
statf(int indent, FTSENT *p)
statf(FILE *fp, int indent, FTSENT *p)
{
u_int32_t len, val;
int fd, offset;
@ -187,51 +188,54 @@ statf(int indent, FTSENT *p)
char *digestbuf;
#endif
offset = printf("%*s%s%s", indent, "",
offset = fprintf(fp, "%*s%s%s", indent, "",
S_ISDIR(p->fts_statp->st_mode) ? "" : " ", vispath(p->fts_name));
if (offset > (INDENTNAMELEN + indent))
offset = MAXLINELEN;
else
offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
offset += fprintf(fp, "%*s",
(INDENTNAMELEN + indent) - offset, "");
if (!S_ISREG(p->fts_statp->st_mode) && (flavor == F_NETBSD6 || !dflag))
output(indent, &offset, "type=%s",
output(fp, indent, &offset, "type=%s",
inotype(p->fts_statp->st_mode));
if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
if (keys & F_UNAME &&
(name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
output(indent, &offset, "uname=%s", name);
output(fp, indent, &offset, "uname=%s", name);
if (keys & F_UID || (keys & F_UNAME && name == NULL))
output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
output(fp, indent, &offset, "uid=%u",
p->fts_statp->st_uid);
}
if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
if (keys & F_GNAME &&
(name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
output(indent, &offset, "gname=%s", name);
output(fp, indent, &offset, "gname=%s", name);
if (keys & F_GID || (keys & F_GNAME && name == NULL))
output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
output(fp, indent, &offset, "gid=%u",
p->fts_statp->st_gid);
}
if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
output(indent, &offset, "mode=%#o",
output(fp, indent, &offset, "mode=%#o",
p->fts_statp->st_mode & MBITS);
if (keys & F_DEV &&
(S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
output(indent, &offset, "device=%#jx",
output(fp, indent, &offset, "device=%#jx",
(uintmax_t)p->fts_statp->st_rdev);
if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
output(fp, indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
if (keys & F_SIZE &&
(flavor == F_FREEBSD9 || S_ISREG(p->fts_statp->st_mode)))
output(indent, &offset, "size=%ju",
output(fp, indent, &offset, "size=%ju",
(uintmax_t)p->fts_statp->st_size);
if (keys & F_TIME)
#if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
output(indent, &offset, "time=%jd.%09ld",
output(fp, indent, &offset, "time=%jd.%09ld",
(intmax_t)p->fts_statp->st_mtimespec.tv_sec,
p->fts_statp->st_mtimespec.tv_nsec);
#else
output(indent, &offset, "time=%jd.%09ld",
output(fp, indent, &offset, "time=%jd.%09ld",
(intmax_t)p->fts_statp->st_mtime, (long)0);
#endif
if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
@ -239,14 +243,14 @@ statf(int indent, FTSENT *p)
crc(fd, &val, &len))
mtree_err("%s: %s", p->fts_accpath, strerror(errno));
close(fd);
output(indent, &offset, "cksum=%lu", (long)val);
output(fp, indent, &offset, "cksum=%lu", (long)val);
}
#ifndef NO_MD5
if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL)
mtree_err("%s: MD5File failed: %s", p->fts_accpath,
strerror(errno));
output(indent, &offset, "%s=%s", MD5KEY, digestbuf);
output(fp, indent, &offset, "%s=%s", MD5KEY, digestbuf);
free(digestbuf);
}
#endif /* ! NO_MD5 */
@ -255,7 +259,7 @@ statf(int indent, FTSENT *p)
if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL)
mtree_err("%s: RMD160File failed: %s", p->fts_accpath,
strerror(errno));
output(indent, &offset, "%s=%s", RMD160KEY, digestbuf);
output(fp, indent, &offset, "%s=%s", RMD160KEY, digestbuf);
free(digestbuf);
}
#endif /* ! NO_RMD160 */
@ -264,7 +268,7 @@ statf(int indent, FTSENT *p)
if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL)
mtree_err("%s: SHA1File failed: %s", p->fts_accpath,
strerror(errno));
output(indent, &offset, "%s=%s", SHA1KEY, digestbuf);
output(fp, indent, &offset, "%s=%s", SHA1KEY, digestbuf);
free(digestbuf);
}
#endif /* ! NO_SHA1 */
@ -273,7 +277,7 @@ statf(int indent, FTSENT *p)
if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL)
mtree_err("%s: SHA256_File failed: %s", p->fts_accpath,
strerror(errno));
output(indent, &offset, "%s=%s", SHA256KEY, digestbuf);
output(fp, indent, &offset, "%s=%s", SHA256KEY, digestbuf);
free(digestbuf);
}
#ifdef SHA384_BLOCK_LENGTH
@ -281,7 +285,7 @@ statf(int indent, FTSENT *p)
if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL)
mtree_err("%s: SHA384_File failed: %s", p->fts_accpath,
strerror(errno));
output(indent, &offset, "%s=%s", SHA384KEY, digestbuf);
output(fp, indent, &offset, "%s=%s", SHA384KEY, digestbuf);
free(digestbuf);
}
#endif
@ -289,18 +293,18 @@ statf(int indent, FTSENT *p)
if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL)
mtree_err("%s: SHA512_File failed: %s", p->fts_accpath,
strerror(errno));
output(indent, &offset, "%s=%s", SHA512KEY, digestbuf);
output(fp, indent, &offset, "%s=%s", SHA512KEY, digestbuf);
free(digestbuf);
}
#endif /* ! NO_SHA2 */
if (keys & F_SLINK &&
(p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
output(indent, &offset, "link=%s",
output(fp, indent, &offset, "link=%s",
vispath(rlink(p->fts_accpath)));
#if HAVE_STRUCT_STAT_ST_FLAGS
if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
char *str = flags_to_string(p->fts_statp->st_flags, "none");
output(indent, &offset, "flags=%s", str);
output(fp, indent, &offset, "flags=%s", str);
free(str);
}
#endif
@ -324,8 +328,8 @@ statf(int indent, FTSENT *p)
#define MTREE_MAXS 16
static int
statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
u_long *pflags)
statd(FILE *fp, FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
u_long *pflags)
{
FTSENT *p;
gid_t sgid;
@ -398,33 +402,33 @@ statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
first) {
first = 0;
if (flavor != F_NETBSD6 && dflag)
printf("/set type=dir");
fprintf(fp, "/set type=dir");
else
printf("/set type=file");
fprintf(fp, "/set type=file");
if (keys & (F_UID | F_UNAME)) {
if (keys & F_UNAME &&
(name = user_from_uid(saveuid, 1)) != NULL)
printf(" uname=%s", name);
fprintf(fp, " uname=%s", name);
if (keys & F_UID || (keys & F_UNAME && name == NULL))
printf(" uid=%lu", (u_long)saveuid);
fprintf(fp, " uid=%lu", (u_long)saveuid);
}
if (keys & (F_GID | F_GNAME)) {
if (keys & F_GNAME &&
(name = group_from_gid(savegid, 1)) != NULL)
printf(" gname=%s", name);
fprintf(fp, " gname=%s", name);
if (keys & F_GID || (keys & F_GNAME && name == NULL))
printf(" gid=%lu", (u_long)savegid);
fprintf(fp, " gid=%lu", (u_long)savegid);
}
if (keys & F_MODE)
printf(" mode=%#lo", (u_long)savemode);
fprintf(fp, " mode=%#lo", (u_long)savemode);
if (keys & F_NLINK)
printf(" nlink=1");
fprintf(fp, " nlink=1");
if (keys & F_FLAGS) {
char *str = flags_to_string(saveflags, "none");
printf(" flags=%s", str);
fprintf(fp, " flags=%s", str);
free(str);
}
printf("\n");
fprintf(fp, "\n");
*puid = saveuid;
*pgid = savegid;
*pmode = savemode;
@ -455,7 +459,7 @@ dcmp(const FTSENT *FTS_CONST *a, const FTSENT *FTS_CONST *b)
}
void
output(int indent, int *offset, const char *fmt, ...)
output(FILE *fp, int indent, int *offset, const char *fmt, ...)
{
va_list ap;
char buf[1024];
@ -465,8 +469,8 @@ output(int indent, int *offset, const char *fmt, ...)
va_end(ap);
if (*offset + strlen(buf) > MAXLINELEN - 3) {
printf(" \\\n%*s", INDENTNAMELEN + indent, "");
fprintf(fp, " \\\n%*s", INDENTNAMELEN + indent, "");
*offset = INDENTNAMELEN + indent;
}
*offset += printf(" %s", buf) + 1;
*offset += fprintf(fp, " %s", buf) + 1;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.38 2013/02/03 19:15:17 christos Exp $ */
/* $NetBSD: extern.h,v 1.39 2014/04/24 17:22:41 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -63,8 +63,8 @@ void addtag(slist_t *, char *);
int check_excludes(const char *, const char *);
int compare(NODE *, FTSENT *);
int crc(int, u_int32_t *, u_int32_t *);
void cwalk(void);
void dump_nodes(const char *, NODE *, int);
void cwalk(FILE *);
void dump_nodes(FILE *, const char *, NODE *, int);
void init_excludes(void);
int matchtags(NODE *);
__dead __printflike(1,2) void mtree_err(const char *, ...);

View File

@ -1,4 +1,4 @@
/* $NetBSD: getid.c,v 1.8 2013/10/16 17:27:42 christos Exp $ */
/* $NetBSD: getid.c,v 1.10 2014/10/27 21:46:45 christos Exp $ */
/* from: NetBSD: getpwent.c,v 1.48 2000/10/03 03:22:26 enami Exp */
/* from: NetBSD: getgrent.c,v 1.41 2002/01/12 23:51:30 lukem Exp */
@ -65,7 +65,7 @@
#endif
#include <sys/cdefs.h>
__RCSID("$NetBSD: getid.c,v 1.8 2013/10/16 17:27:42 christos Exp $");
__RCSID("$NetBSD: getid.c,v 1.10 2014/10/27 21:46:45 christos Exp $");
#include <sys/param.h>
@ -206,7 +206,12 @@ grstart(void)
}
if (grfile[0] == '\0') /* sanity check */
return 0;
return (_gr_fp = fopen(grfile, "r")) ? 1 : 0;
_gr_fp = fopen(grfile, "r");
if (_gr_fp != NULL)
return 1;
warn("Can't open `%s'", grfile);
return 0;
}
@ -231,7 +236,7 @@ grscan(int search, gid_t gid, const char *name)
continue;
}
/* skip comments */
if (pwline[0] == '#')
if (grline[0] == '#')
continue;
if (grmatchline(search, gid, name))
return 1;
@ -350,7 +355,11 @@ pwstart(void)
}
if (pwfile[0] == '\0') /* sanity check */
return 0;
return (_pw_fp = fopen(pwfile, "r")) ? 1 : 0;
_pw_fp = fopen(pwfile, "r");
if (_pw_fp != NULL)
return 1;
warn("Can't open `%s'", pwfile);
return 0;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: mtree.c,v 1.48 2013/04/08 17:39:11 christos Exp $ */
/* $NetBSD: mtree.c,v 1.49 2014/04/24 17:22:41 christos Exp $ */
/*-
* Copyright (c) 1989, 1990, 1993
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1990, 1993\
#if 0
static char sccsid[] = "@(#)mtree.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: mtree.c,v 1.48 2013/04/08 17:39:11 christos Exp $");
__RCSID("$NetBSD: mtree.c,v 1.49 2014/04/24 17:22:41 christos Exp $");
#endif
#endif /* not lint */
@ -296,11 +296,11 @@ main(int argc, char **argv)
mtree_err("-l and -u flags are mutually exclusive");
if (cflag) {
cwalk();
cwalk(stdout);
exit(0);
}
if (Cflag || Dflag) {
dump_nodes("", spec(spec1), Dflag);
dump_nodes(stdout, "", spec(spec1), Dflag);
exit(0);
}
if (spec2 != NULL)

View File

@ -1,4 +1,4 @@
/* $NetBSD: spec.c,v 1.88 2013/10/17 17:22:59 christos Exp $ */
/* $NetBSD: spec.c,v 1.89 2014/04/24 17:22:41 christos Exp $ */
/*-
* Copyright (c) 1989, 1993
@ -67,7 +67,7 @@
#if 0
static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: spec.c,v 1.88 2013/10/17 17:22:59 christos Exp $");
__RCSID("$NetBSD: spec.c,v 1.89 2014/04/24 17:22:41 christos Exp $");
#endif
#endif /* not lint */
@ -102,7 +102,7 @@ static void set(char *, NODE *);
static void unset(char *, NODE *);
static void addchild(NODE *, NODE *);
static int nodecmp(const NODE *, const NODE *);
static int appendfield(int, const char *, ...) __printflike(2, 3);
static int appendfield(FILE *, int, const char *, ...) __printflike(3, 4);
#define REPLACEPTR(x,v) do { if ((x)) free((x)); (x) = (v); } while (0)
@ -290,21 +290,21 @@ free_nodes(NODE *root)
/*
* appendfield --
* Like printf(), but output a space either before or after
* Like fprintf(), but output a space either before or after
* the regular output, according to the pathlast flag.
*/
static int
appendfield(int pathlast, const char *fmt, ...)
appendfield(FILE *fp, int pathlast, const char *fmt, ...)
{
va_list ap;
int result;
va_start(ap, fmt);
if (!pathlast)
printf(" ");
fprintf(fp, " ");
result = vprintf(fmt, ap);
if (pathlast)
printf(" ");
fprintf(fp, " ");
va_end(ap);
return result;
}
@ -316,7 +316,7 @@ appendfield(int pathlast, const char *fmt, ...)
* it first.
*/
void
dump_nodes(const char *dir, NODE *root, int pathlast)
dump_nodes(FILE *fp, const char *dir, NODE *root, int pathlast)
{
NODE *cur;
char path[MAXPATHLEN];
@ -334,70 +334,75 @@ dump_nodes(const char *dir, NODE *root, int pathlast)
mtree_err("Pathname too long.");
if (!pathlast)
printf("%s", vispath(path));
fprintf(fp, "%s", vispath(path));
#define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f)))
if (MATCHFLAG(F_TYPE))
appendfield(pathlast, "type=%s", nodetype(cur->type));
appendfield(fp, pathlast, "type=%s",
nodetype(cur->type));
if (MATCHFLAG(F_UID | F_UNAME)) {
if (keys & F_UNAME &&
(name = user_from_uid(cur->st_uid, 1)) != NULL)
appendfield(pathlast, "uname=%s", name);
appendfield(fp, pathlast, "uname=%s", name);
else
appendfield(pathlast, "uid=%u", cur->st_uid);
appendfield(fp, pathlast, "uid=%u",
cur->st_uid);
}
if (MATCHFLAG(F_GID | F_GNAME)) {
if (keys & F_GNAME &&
(name = group_from_gid(cur->st_gid, 1)) != NULL)
appendfield(pathlast, "gname=%s", name);
appendfield(fp, pathlast, "gname=%s", name);
else
appendfield(pathlast, "gid=%u", cur->st_gid);
appendfield(fp, pathlast, "gid=%u",
cur->st_gid);
}
if (MATCHFLAG(F_MODE))
appendfield(pathlast, "mode=%#o", cur->st_mode);
appendfield(fp, pathlast, "mode=%#o", cur->st_mode);
if (MATCHFLAG(F_DEV) &&
(cur->type == F_BLOCK || cur->type == F_CHAR))
appendfield(pathlast, "device=%#jx",
appendfield(fp, pathlast, "device=%#jx",
(uintmax_t)cur->st_rdev);
if (MATCHFLAG(F_NLINK))
appendfield(pathlast, "nlink=%d", cur->st_nlink);
appendfield(fp, pathlast, "nlink=%d", cur->st_nlink);
if (MATCHFLAG(F_SLINK))
appendfield(pathlast, "link=%s", vispath(cur->slink));
appendfield(fp, pathlast, "link=%s",
vispath(cur->slink));
if (MATCHFLAG(F_SIZE))
appendfield(pathlast, "size=%ju",
appendfield(fp, pathlast, "size=%ju",
(uintmax_t)cur->st_size);
if (MATCHFLAG(F_TIME))
appendfield(pathlast, "time=%jd.%09ld",
appendfield(fp, pathlast, "time=%jd.%09ld",
(intmax_t)cur->st_mtimespec.tv_sec,
cur->st_mtimespec.tv_nsec);
if (MATCHFLAG(F_CKSUM))
appendfield(pathlast, "cksum=%lu", cur->cksum);
appendfield(fp, pathlast, "cksum=%lu", cur->cksum);
if (MATCHFLAG(F_MD5))
appendfield(pathlast, "%s=%s", MD5KEY, cur->md5digest);
appendfield(fp, pathlast, "%s=%s", MD5KEY,
cur->md5digest);
if (MATCHFLAG(F_RMD160))
appendfield(pathlast, "%s=%s", RMD160KEY,
appendfield(fp, pathlast, "%s=%s", RMD160KEY,
cur->rmd160digest);
if (MATCHFLAG(F_SHA1))
appendfield(pathlast, "%s=%s", SHA1KEY,
appendfield(fp, pathlast, "%s=%s", SHA1KEY,
cur->sha1digest);
if (MATCHFLAG(F_SHA256))
appendfield(pathlast, "%s=%s", SHA256KEY,
appendfield(fp, pathlast, "%s=%s", SHA256KEY,
cur->sha256digest);
if (MATCHFLAG(F_SHA384))
appendfield(pathlast, "%s=%s", SHA384KEY,
appendfield(fp, pathlast, "%s=%s", SHA384KEY,
cur->sha384digest);
if (MATCHFLAG(F_SHA512))
appendfield(pathlast, "%s=%s", SHA512KEY,
appendfield(fp, pathlast, "%s=%s", SHA512KEY,
cur->sha512digest);
if (MATCHFLAG(F_FLAGS)) {
str = flags_to_string(cur->st_flags, "none");
appendfield(pathlast, "flags=%s", str);
appendfield(fp, pathlast, "flags=%s", str);
free(str);
}
if (MATCHFLAG(F_IGN))
appendfield(pathlast, "ignore");
appendfield(fp, pathlast, "ignore");
if (MATCHFLAG(F_OPT))
appendfield(pathlast, "optional");
appendfield(fp, pathlast, "optional");
if (MATCHFLAG(F_TAGS)) {
/* don't output leading or trailing commas */
p = cur->tags;
@ -406,12 +411,12 @@ dump_nodes(const char *dir, NODE *root, int pathlast)
q = p + strlen(p);
while(q > p && q[-1] == ',')
q--;
appendfield(pathlast, "tags=%.*s", (int)(q - p), p);
appendfield(fp, pathlast, "tags=%.*s", (int)(q - p), p);
}
puts(pathlast ? vispath(path) : "");
if (cur->child)
dump_nodes(path, cur->child, pathlast);
dump_nodes(fp, path, cur->child, pathlast);
}
}