work on other platforms. Please report any problems and suggested fixes to
<dhcp-users@isc.org>.
+ Changes since 4.1.0 (bug fixes)
+
+- Validate the argument to the -p option.
+
Changes since 4.1.0b1
- A missing "else" in dhcrelay.c could have caused an interface not to
} else if (!strcmp(argv[i], "-p")) {
if (++i == argc)
usage();
- local_port = htons(atoi(argv[i]));
+ local_port = validate_port(argv[i]);
log_debug("binding to user-specified port %d",
ntohs(local_port));
} else if (!strcmp(argv[i], "-d")) {
/* inet.c
- Subroutines to manipulate internet addresses in a safely portable
+ Subroutines to manipulate internet addresses and ports in a safely portable
way... */
/*
- * Copyright (c) 2004,2005,2007 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 2004,2005,2007,2008 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1995-2003 by Internet Software Consortium
*
* Permission to use, copy, modify, and distribute this software for any
return ret;
}
+/* Validate that the string represents a valid port number and
+ * return it in network byte order
+ */
+
+u_int16_t
+validate_port(char *port) {
+ int local_port = 0;
+ int lower = 1;
+ int upper = 65535;
+ char *endptr;
+
+ errno = 0;
+ local_port = strtol(port, &endptr, 10);
+
+ if ((*endptr != '\0') || (errno == ERANGE) || (errno == EINVAL))
+ log_fatal ("Invalid port number specification: %s", port);
+
+ if (local_port < lower || local_port > upper)
+ log_fatal("Port number specified is out of range (%d-%d).",
+ lower, upper);
+
+ return htons(local_port);
+}
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
+#include <errno.h>
#include <netdb.h>
#else
const char *piaddr PROTO ((struct iaddr));
char *piaddrmask(struct iaddr *, struct iaddr *);
char *piaddrcidr(const struct iaddr *, unsigned int);
+u_int16_t validate_port(char *);
/* dhclient.c */
extern int nowait;
} else if (!strcmp(argv[i], "-p")) {
if (++i == argc)
usage();
- local_port = htons(atoi (argv[i]));
+ local_port = validate_port(argv[i]);
log_debug("binding to user-specified port %d",
ntohs(local_port));
} else if (!strcmp(argv[i], "-c")) {
if (!strcmp (argv [i], "-p")) {
if (++i == argc)
usage ();
- for (s = argv [i]; *s; s++)
- if (!isdigit ((unsigned char)*s))
- log_fatal ("%s: not a valid UDP port",
- argv [i]);
- status = atoi (argv [i]);
- if (status < 1 || status > 65535)
- log_fatal ("%s: not a valid UDP port",
- argv [i]);
- local_port = htons (status);
+ local_port = validate_port (argv [i]);
log_debug ("binding to user-specified port %d",
ntohs (local_port));
} else if (!strcmp (argv [i], "-f")) {
if (!local_port)
{
if ((s = getenv ("DHCPD_PORT"))) {
- local_port = htons (atoi (s));
+ local_port = validate_port (s);
log_debug ("binding to environment-specified port %d",
ntohs (local_port));
} else {