summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1998-03-16 05:07:03 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1998-03-16 05:07:03 +0000
commitc6e6ff6ba816fbfd8168fcaa11020351a56bb992 (patch)
tree047c2ff5a5b9e94a0b77d503884e64395d8ca0cc /lib
parent9e9d3110171c8c9544bf65a861f4261fed7219ef (diff)
Use fgetln(3) instead of fgets(3) so we can easily recognize lines
that are too long and ignore them instead of corrupting later entries.
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/net/ethers.c30
-rw-r--r--lib/libc/net/gethostnamadr.c18
-rw-r--r--lib/libc/net/getnetent.314
-rw-r--r--lib/libc/net/getnetent.c21
-rw-r--r--lib/libc/net/getprotoent.c20
-rw-r--r--lib/libc/net/getservent.c20
-rw-r--r--lib/libc/net/res_init.c17
-rw-r--r--lib/libc/net/res_query.c15
8 files changed, 101 insertions, 54 deletions
diff --git a/lib/libc/net/ethers.c b/lib/libc/net/ethers.c
index 71163515a42..b2d18e9932d 100644
--- a/lib/libc/net/ethers.c
+++ b/lib/libc/net/ethers.c
@@ -6,7 +6,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: ethers.c,v 1.3 1996/08/19 08:28:36 tholo Exp $";
+static char rcsid[] = "$OpenBSD: ethers.c,v 1.4 1998/03/16 05:06:53 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
@@ -64,7 +64,8 @@ ether_ntohost(hostname, e)
struct ether_addr *e;
{
FILE *f;
- char buf[BUFSIZ];
+ char buf[BUFSIZ+1], *p;
+ size_t len;
struct ether_addr try;
#ifdef YP
@@ -79,9 +80,16 @@ ether_ntohost(hostname, e)
#endif
f = fopen(_PATH_ETHERS, "r");
- if (f==NULL)
+ if (f == NULL)
return -1;
- while (fgets(buf, sizeof buf, f)) {
+ while ((p = fgetln(f, &len)) != NULL) {
+ if (p[len-1] == '\n')
+ len--;
+ if (len > sizeof(buf) - 2)
+ continue;
+ memcpy(buf, p, len);
+ buf[len] = '\n'; /* code assumes newlines later on */
+ buf[len+1] = '\0';
#ifdef YP
/* A + in the file means try YP now. */
if (!strncmp(buf, "+\n", sizeof buf)) {
@@ -103,7 +111,7 @@ ether_ntohost(hostname, e)
}
#endif
if (ether_line(buf, &try, hostname) == 0 &&
- bcmp((char *)&try, (char *)e, sizeof try) == 0) {
+ memcmp((char *)&try, (char *)e, sizeof try) == 0) {
(void)fclose(f);
return 0;
}
@@ -119,8 +127,9 @@ ether_hostton(hostname, e)
struct ether_addr *e;
{
FILE *f;
- char buf[BUFSIZ];
+ char buf[BUFSIZ+1], *p;
char try[MAXHOSTNAMELEN];
+ size_t len;
#ifdef YP
int hostlen = strlen(hostname);
#endif
@@ -129,7 +138,14 @@ ether_hostton(hostname, e)
if (f==NULL)
return -1;
- while (fgets(buf, sizeof buf, f)) {
+ while ((p = fgetln(f, &len)) != NULL) {
+ if (p[len-1] == '\n')
+ len--;
+ if (len > sizeof(buf) - 2)
+ continue;
+ memcpy(buf, p, len);
+ buf[len] = '\n'; /* code assumes newlines later on */
+ buf[len+1] = '\0';
#ifdef YP
/* A + in the file means try YP now. */
if (!strncmp(buf, "+\n", sizeof buf)) {
diff --git a/lib/libc/net/gethostnamadr.c b/lib/libc/net/gethostnamadr.c
index 7fb148bf528..7321225863a 100644
--- a/lib/libc/net/gethostnamadr.c
+++ b/lib/libc/net/gethostnamadr.c
@@ -52,7 +52,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: gethostnamadr.c,v 1.29 1998/01/20 18:28:33 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: gethostnamadr.c,v 1.30 1998/03/16 05:06:55 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
@@ -721,22 +721,28 @@ _gethtent()
{
char *p;
register char *cp, **q;
- int af, len;
+ int af;
+ size_t len;
if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
h_errno = NETDB_INTERNAL;
return (NULL);
}
again:
- if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
+ if ((p = fgetln(hostf, &len)) == NULL) {
h_errno = HOST_NOT_FOUND;
return (NULL);
}
- if (*p == '#')
+ if (p[len-1] == '\n')
+ len--;
+ if (len >= sizeof(hostbuf) || len == 0)
goto again;
- if (!(cp = strpbrk(p, "#\n")))
+ p = memcpy(hostbuf, p, len);
+ hostbuf[len] = '\0';
+ if (*p == '#')
goto again;
- *cp = '\0';
+ if ((cp = strchr(p, '#')))
+ *cp = '\0';
if (!(cp = strpbrk(p, " \t")))
goto again;
*cp++ = '\0';
diff --git a/lib/libc/net/getnetent.3 b/lib/libc/net/getnetent.3
index a33ac0fac5c..5864b75839d 100644
--- a/lib/libc/net/getnetent.3
+++ b/lib/libc/net/getnetent.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: getnetent.3,v 1.4 1997/04/05 21:13:07 millert Exp $
+.\" $OpenBSD: getnetent.3,v 1.5 1998/03/16 05:06:56 millert Exp $
.\"
.\" Copyright (c) 1983, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -141,10 +141,8 @@ and
functions appeared in
.Bx 4.2 .
.Sh BUGS
-The data space used by
-these functions is static; if future use requires the data, it should be
-copied before any subsequent calls to these functions overwrite it.
-Only Internet network
-numbers are currently understood.
-Expecting network numbers to fit
-in no more than 32 bits is naive.
+The data space used by these functions is static; if future use
+requires the data, it should be copied before any subsequent calls
+to these functions overwrite it. Only Internet network numbers
+are currently understood. Expecting network numbers to fit in no
+more than 32 bits is naive.
diff --git a/lib/libc/net/getnetent.c b/lib/libc/net/getnetent.c
index e40fb50c0f6..8f618a1d5e3 100644
--- a/lib/libc/net/getnetent.c
+++ b/lib/libc/net/getnetent.c
@@ -32,7 +32,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: getnetent.c,v 1.7 1997/04/24 08:37:09 tholo Exp $";
+static char rcsid[] = "$OpenBSD: getnetent.c,v 1.8 1998/03/16 05:06:57 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
@@ -75,21 +75,24 @@ endnetent()
struct netent *
getnetent()
{
- char *p;
- register char *cp, **q;
+ char *p, *cp, **q;
+ size_t len;
if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
return (NULL);
again:
- p = fgets(line, BUFSIZ, netf);
- if (p == NULL)
+ if ((p = fgetln(netf, &len)) == NULL)
return (NULL);
- if (*p == '#')
+ if (p[len-1] == '\n')
+ len--;
+ if (len >= sizeof(line) || len == 0)
goto again;
- cp = strpbrk(p, "#\n");
- if (cp == NULL)
+ p = memcpy(line, p, len);
+ line[len] = '\0';
+ if (*p == '#')
goto again;
- *cp = '\0';
+ if ((cp = strchr(p, '#')) != NULL)
+ *cp = '\0';
net.n_name = p;
if (strlen(net.n_name) >= MAXHOSTNAMELEN-1)
net.n_name[MAXHOSTNAMELEN-1] = '\0';
diff --git a/lib/libc/net/getprotoent.c b/lib/libc/net/getprotoent.c
index 381feb6fafc..2bef526e7ad 100644
--- a/lib/libc/net/getprotoent.c
+++ b/lib/libc/net/getprotoent.c
@@ -32,7 +32,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: getprotoent.c,v 1.2 1996/08/19 08:28:52 tholo Exp $";
+static char rcsid[] = "$OpenBSD: getprotoent.c,v 1.3 1998/03/16 05:06:59 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
@@ -74,20 +74,24 @@ endprotoent()
struct protoent *
getprotoent()
{
- char *p;
- register char *cp, **q;
+ char *p, *cp, **q;
+ size_t len;
if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
return (NULL);
again:
- if ((p = fgets(line, BUFSIZ, protof)) == NULL)
+ if ((p = fgetln(protof, &len)) == NULL)
return (NULL);
- if (*p == '#')
+ if (p[len-1] == '\n')
+ len--;
+ if (len >= sizeof(line) || len == 0)
goto again;
- cp = strpbrk(p, "#\n");
- if (cp == NULL)
+ p = memcpy(line, p, len);
+ line[len] = '\0';
+ if (*p == '#')
goto again;
- *cp = '\0';
+ if ((cp = strchr(p, '#')) != NULL)
+ *cp = '\0';
proto.p_name = p;
cp = strpbrk(p, " \t");
if (cp == NULL)
diff --git a/lib/libc/net/getservent.c b/lib/libc/net/getservent.c
index feb97aa129a..7d8cb6d8cad 100644
--- a/lib/libc/net/getservent.c
+++ b/lib/libc/net/getservent.c
@@ -32,7 +32,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: getservent.c,v 1.3 1997/04/05 21:13:09 millert Exp $";
+static char rcsid[] = "$OpenBSD: getservent.c,v 1.4 1998/03/16 05:07:00 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
@@ -74,20 +74,24 @@ endservent()
struct servent *
getservent()
{
- char *p;
- register char *cp, **q;
+ char *p, *cp, **q;
+ size_t len;
if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
return (NULL);
again:
- if ((p = fgets(line, BUFSIZ, servf)) == NULL)
+ if ((p = fgetln(servf, &len)) == NULL)
return (NULL);
- if (*p == '#')
+ if (p[len-1] == '\n')
+ len--;
+ if (len >= sizeof(line) || len == 0)
goto again;
- cp = strpbrk(p, "#\n");
- if (cp == NULL)
+ p = memcpy(line, p, len);
+ line[len] = '\0';
+ if (*p == '#')
goto again;
- *cp = '\0';
+ if ((cp = strchr(p, '#')) != NULL)
+ *cp = '\0';
serv.s_name = p;
p = strpbrk(p, " \t");
if (p == NULL)
diff --git a/lib/libc/net/res_init.c b/lib/libc/net/res_init.c
index b4cee1d3073..df176b7fa1d 100644
--- a/lib/libc/net/res_init.c
+++ b/lib/libc/net/res_init.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: res_init.c,v 1.15 1997/07/15 18:33:50 flipk Exp $ */
+/* $OpenBSD: res_init.c,v 1.16 1998/03/16 05:07:01 millert Exp $ */
/*
* ++Copyright++ 1985, 1989, 1993
@@ -60,7 +60,7 @@
static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
static char rcsid[] = "$From: res_init.c,v 8.7 1996/09/28 06:51:07 vixie Exp $";
#else
-static char rcsid[] = "$OpenBSD: res_init.c,v 1.15 1997/07/15 18:33:50 flipk Exp $";
+static char rcsid[] = "$OpenBSD: res_init.c,v 1.16 1998/03/16 05:07:01 millert Exp $";
#endif
#endif /* LIBC_SCCS and not lint */
@@ -151,6 +151,7 @@ res_init()
int nserv = 0; /* number of nameserver records read from file */
int haveenv = 0;
int havesearch = 0;
+ size_t len;
#ifdef RESOLVSORT
int nsort = 0;
char *net;
@@ -241,9 +242,17 @@ res_init()
strncpy(_res.lookups, "bf", sizeof _res.lookups);
/* read the config file */
- while (fgets(buf, sizeof(buf), fp) != NULL) {
+ buf[0] = '\0';
+ while ((cp = fgetln(fp, &len)) != NULL) {
+ /* skip lines that are too long or zero length */
+ if (len >= sizeof(buf) || len == 0)
+ continue;
+ (void)memcpy(buf, cp, len);
+ buf[len] = '\0';
/* skip comments */
- if (*buf == ';' || *buf == '#')
+ if ((cp = strpbrk(buf, ";#")) != NULL)
+ *cp = '\0';
+ if (buf[0] == '\0')
continue;
/* read default domain name */
if (MATCH(buf, "domain")) {
diff --git a/lib/libc/net/res_query.c b/lib/libc/net/res_query.c
index 2e245b78cc6..a2a8fe000b5 100644
--- a/lib/libc/net/res_query.c
+++ b/lib/libc/net/res_query.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: res_query.c,v 1.10 1997/07/09 01:08:53 millert Exp $ */
+/* $OpenBSD: res_query.c,v 1.11 1998/03/16 05:07:02 millert Exp $ */
/*
* ++Copyright++ 1988, 1993
@@ -60,7 +60,7 @@
static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
static char rcsid[] = "$From: res_query.c,v 8.9 1996/09/22 00:13:28 vixie Exp $";
#else
-static char rcsid[] = "$OpenBSD: res_query.c,v 1.10 1997/07/09 01:08:53 millert Exp $";
+static char rcsid[] = "$OpenBSD: res_query.c,v 1.11 1998/03/16 05:07:02 millert Exp $";
#endif
#endif /* LIBC_SCCS and not lint */
@@ -359,6 +359,7 @@ hostalias(name)
char *file;
char buf[BUFSIZ];
static char abuf[MAXDNAME];
+ size_t len;
if (_res.options & RES_NOALIASES)
return (NULL);
@@ -366,8 +367,14 @@ hostalias(name)
if (issetugid() != 0 || file == NULL || (fp = fopen(file, "r")) == NULL)
return (NULL);
setbuf(fp, NULL);
- buf[sizeof(buf) - 1] = '\0';
- while (fgets(buf, sizeof(buf), fp)) {
+ while ((cp1 = fgetln(fp, &len)) != NULL) {
+ if (cp1[len-1] == '\n')
+ len--;
+ if (len >= sizeof(buf) || len == 0)
+ continue;
+ (void)memcpy(buf, cp1, len);
+ buf[len] = '\0';
+
for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
;
if (!*cp1)