summaryrefslogtreecommitdiff
path: root/usr.sbin/mtree
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2005-08-10 00:42:10 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2005-08-10 00:42:10 +0000
commit86039bd4136a48050ac1fe7521180bf967f22569 (patch)
treec78d9187ac59ca9a7eea478d6d25107aa4e776a8 /usr.sbin/mtree
parentbbdd33e5ecee3792bd6e1f7f5fe3492acf7fa763 (diff)
Call open(2) with O_NONBLOCK and O_NOFOLLOW to make sure mtree can't
be halted by a FIFO or special device. Use fgetln() instead of fgets() to handle arbitrarily long lines. If a line starts with a comment char, don't check for line continuation char. Adapted from a diff by Solar Designer. OK deraadt@ henning@
Diffstat (limited to 'usr.sbin/mtree')
-rw-r--r--usr.sbin/mtree/compare.c6
-rw-r--r--usr.sbin/mtree/create.c12
-rw-r--r--usr.sbin/mtree/extern.h5
-rw-r--r--usr.sbin/mtree/mtree.h5
-rw-r--r--usr.sbin/mtree/spec.c46
5 files changed, 42 insertions, 32 deletions
diff --git a/usr.sbin/mtree/compare.c b/usr.sbin/mtree/compare.c
index cac8e967cad..6d0017dde53 100644
--- a/usr.sbin/mtree/compare.c
+++ b/usr.sbin/mtree/compare.c
@@ -1,5 +1,5 @@
/* $NetBSD: compare.c,v 1.11 1996/09/05 09:56:48 mycroft Exp $ */
-/* $OpenBSD: compare.c,v 1.20 2004/11/21 19:36:04 otto Exp $ */
+/* $OpenBSD: compare.c,v 1.21 2005/08/10 00:42:09 millert Exp $ */
/*-
* Copyright (c) 1989, 1993
@@ -34,7 +34,7 @@
#if 0
static const char sccsid[] = "@(#)compare.c 8.1 (Berkeley) 6/6/93";
#else
-static const char rcsid[] = "$OpenBSD: compare.c,v 1.20 2004/11/21 19:36:04 otto Exp $";
+static const char rcsid[] = "$OpenBSD: compare.c,v 1.21 2005/08/10 00:42:09 millert Exp $";
#endif
#endif /* not lint */
@@ -226,7 +226,7 @@ typeerr: LABEL;
}
}
if (s->flags & F_CKSUM) {
- if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
+ if ((fd = open(p->fts_accpath, MTREE_O_FLAGS, 0)) < 0) {
LABEL;
(void)printf("%scksum: %s: %s\n",
tab, p->fts_accpath, strerror(errno));
diff --git a/usr.sbin/mtree/create.c b/usr.sbin/mtree/create.c
index 5e86da1a0dd..28a05d5a4db 100644
--- a/usr.sbin/mtree/create.c
+++ b/usr.sbin/mtree/create.c
@@ -1,5 +1,5 @@
/* $NetBSD: create.c,v 1.11 1996/09/05 09:24:19 mycroft Exp $ */
-/* $OpenBSD: create.c,v 1.24 2004/11/21 19:36:04 otto Exp $ */
+/* $OpenBSD: create.c,v 1.25 2005/08/10 00:42:09 millert Exp $ */
/*-
* Copyright (c) 1989, 1993
@@ -34,7 +34,7 @@
#if 0
static const char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
#else
-static const char rcsid[] = "$OpenBSD: create.c,v 1.24 2004/11/21 19:36:04 otto Exp $";
+static const char rcsid[] = "$OpenBSD: create.c,v 1.25 2005/08/10 00:42:09 millert Exp $";
#endif
#endif /* not lint */
@@ -70,7 +70,8 @@ static gid_t gid;
static uid_t uid;
static mode_t mode;
-static void output(int, int *, const char *, ...);
+static void output(int, int *, const char *, ...)
+ __attribute__((__format__ (printf, 3, 4)));
static int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *);
static void statf(int, FTSENT *);
@@ -145,7 +146,8 @@ statf(int indent, FTSENT *p)
escaped_name = malloc(esc_len);
if (escaped_name == NULL)
error("statf: %s", strerror(errno));
- strnvis(escaped_name, p->fts_name, esc_len, VIS_WHITE | VIS_OCTAL);
+ strnvis(escaped_name, p->fts_name, esc_len,
+ VIS_WHITE | VIS_OCTAL | VIS_GLOB);
if (iflag || S_ISDIR(p->fts_statp->st_mode))
offset = printf("%*s%s", indent, "", escaped_name);
@@ -196,7 +198,7 @@ statf(int indent, FTSENT *p)
p->fts_statp->st_mtimespec.tv_sec,
p->fts_statp->st_mtimespec.tv_nsec);
if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
- if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
+ if ((fd = open(p->fts_accpath, MTREE_O_FLAGS, 0)) < 0 ||
crc(fd, &val, &len))
error("%s: %s", p->fts_accpath, strerror(errno));
(void)close(fd);
diff --git a/usr.sbin/mtree/extern.h b/usr.sbin/mtree/extern.h
index 4954adb6749..c855c1a4188 100644
--- a/usr.sbin/mtree/extern.h
+++ b/usr.sbin/mtree/extern.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: extern.h,v 1.7 2004/11/21 19:36:04 otto Exp $ */
+/* $OpenBSD: extern.h,v 1.8 2005/08/10 00:42:09 millert Exp $ */
/* $NetBSD: extern.h,v 1.3 1995/03/07 21:12:07 cgd Exp $ */
/*-
@@ -39,7 +39,8 @@ int compare(char *, struct _node *, struct _ftsent *);
int dsort(const struct _ftsent **, const struct _ftsent **);
int crc(int, u_int32_t *, u_int32_t *);
void cwalk(void);
-void error(const char *, ...);
+void error(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
char *inotype(u_int);
u_int parsekey(char *, int *);
char *rlink(char *);
diff --git a/usr.sbin/mtree/mtree.h b/usr.sbin/mtree/mtree.h
index 452541ce4fb..7fe3b325ecf 100644
--- a/usr.sbin/mtree/mtree.h
+++ b/usr.sbin/mtree/mtree.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: mtree.h,v 1.10 2003/06/02 23:36:54 millert Exp $ */
+/* $OpenBSD: mtree.h,v 1.11 2005/08/10 00:42:09 millert Exp $ */
/* $NetBSD: mtree.h,v 1.7 1995/03/07 21:26:27 cgd Exp $ */
/*-
@@ -35,6 +35,9 @@
#include <string.h>
#include <stdlib.h>
+#define MTREE_O_FLAGS \
+ (O_RDONLY | O_NONBLOCK | O_NOFOLLOW)
+
#define KEYDEFAULT \
(F_GID | F_MODE | F_NLINK | F_SIZE | F_SLINK | F_TIME | F_UID)
diff --git a/usr.sbin/mtree/spec.c b/usr.sbin/mtree/spec.c
index 388410dd82f..c0d75bfc78e 100644
--- a/usr.sbin/mtree/spec.c
+++ b/usr.sbin/mtree/spec.c
@@ -1,5 +1,5 @@
/* $NetBSD: spec.c,v 1.6 1995/03/07 21:12:12 cgd Exp $ */
-/* $OpenBSD: spec.c,v 1.22 2004/08/01 18:32:20 deraadt Exp $ */
+/* $OpenBSD: spec.c,v 1.23 2005/08/10 00:42:09 millert Exp $ */
/*-
* Copyright (c) 1989, 1993
@@ -34,7 +34,7 @@
#if 0
static const char sccsid[] = "@(#)spec.c 8.1 (Berkeley) 6/6/93";
#else
-static const char rcsid[] = "$OpenBSD: spec.c,v 1.22 2004/08/01 18:32:20 deraadt Exp $";
+static const char rcsid[] = "$OpenBSD: spec.c,v 1.23 2005/08/10 00:42:09 millert Exp $";
#endif
#endif /* not lint */
@@ -62,38 +62,41 @@ spec(void)
char *p;
NODE ginfo, *root;
int c_cur, c_next;
- char buf[2048];
+ char *buf, *tbuf = NULL;
size_t len;
centry = last = root = NULL;
bzero(&ginfo, sizeof(ginfo));
c_cur = c_next = 0;
- for (lineno = 1; fgets(buf, sizeof(buf), stdin);
+ for (lineno = 1; (buf = fgetln(stdin, &len));
++lineno, c_cur = c_next, c_next = 0) {
- /* Skip empty lines. */
- if (buf[0] == '\n')
- continue;
-
- /* Find end of line. */
- if ((p = strchr(buf, '\n')) == NULL)
- error("line %d too long", lineno);
-
- /* See if next line is continuation line. */
- if (p[-1] == '\\') {
- --p;
- c_next = 1;
- }
-
/* Null-terminate the line. */
- *p = '\0';
+ if (buf[len - 1] == '\n') {
+ buf[--len] = '\0';
+ } else {
+ /* EOF with no newline. */
+ tbuf = malloc(len + 1);
+ memcpy(tbuf, buf, len);
+ tbuf[len] = '\0';
+ buf = tbuf;
+ }
/* Skip leading whitespace. */
- for (p = buf; *p && isspace(*p); ++p);
+ for (p = buf; isspace((unsigned char)*p); p++)
+ ;
/* If nothing but whitespace or comment char, continue. */
- if (!*p || *p == '#')
+ if (*p == '\0' || *p == '#')
continue;
+ /* See if next line is continuation line. */
+ if (buf[len - 1] == '\\') {
+ if (--len == 0)
+ continue;
+ buf[len] = '\0';
+ c_next = 1;
+ }
+
#ifdef DEBUG
(void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
#endif
@@ -164,6 +167,7 @@ noparent: error("no parent node");
last = last->next = centry;
}
}
+ free(tbuf);
return (root);
}