From 0e656b6985416a89ca240b56dd9177a6bc3db4ac Mon Sep 17 00:00:00 2001 From: serassio <> Date: Sun, 8 Oct 2006 19:10:34 +0000 Subject: [PATCH] Forward port of patches for Bug #1719 and Bug #1695 from 2.6. Bug #1719: Incorrect error message on invalid cache_peer specifications aborted with an assertion on the first request instead of rejecting the configuration as invalid. Bug #1695: http_port and other directives accept invalid ports This patch rejects invalid port specifications in http_port and numerous other directives as invalid. --- src/ACLIntRange.cc | 35 ++++++++++++------------ src/ACLMaxUserIP.cc | 4 +-- src/Parsing.cc | 42 ++++++++++++++++++++++++++--- src/Parsing.h | 7 +++-- src/cache_cf.cc | 66 ++++++++++++--------------------------------- 5 files changed, 79 insertions(+), 75 deletions(-) diff --git a/src/ACLIntRange.cc b/src/ACLIntRange.cc index b69f516929..60efec9d37 100644 --- a/src/ACLIntRange.cc +++ b/src/ACLIntRange.cc @@ -1,5 +1,5 @@ /* - * $Id: ACLIntRange.cc,v 1.8 2006/08/26 11:38:56 serassio Exp $ + * $Id: ACLIntRange.cc,v 1.9 2006/10/08 13:10:34 serassio Exp $ * * DEBUG: section 28 Access Control * AUTHOR: Robert Collins @@ -37,6 +37,7 @@ #include "squid.h" #include "ACLIntRange.h" #include "wordlist.h" +#include "Parsing.h" /* explicit instantiation required for some systems */ @@ -46,28 +47,26 @@ template cbdata_type List > void ACLIntRange::parse() { - char *t = NULL; + char *a; - while ((t = strtokFile())) { - int port = atoi(t); + while ((a = strtokFile())) { + char *b = strchr(a, '-'); + unsigned short port1, port2; - if (port > 0 && port < 65536) { - RangeType temp (0,0); - temp.start = port; - t = strchr(t, '-'); + if (b) + *b++ = '\0'; - if (t && *(++t)) { - port = atoi(t); + port1 = xatos(a); - if (port > 0 && port < 65536 && port > temp.start) { - temp.end = port+1; - } else { - debug(28, 0) ("ACLIntRange::parse: Invalid port range\n"); - self_destruct(); - } - } else - temp.end = temp.start+1; + if (b) + port2 = xatos(b); + else + port2 = port1; + if (port2 >= port1) { + RangeType temp (0,0); + temp.start = port1; + temp.end = port2+1; ranges.push_back(temp); } else { debug(28, 0) ("ACLIntRange::parse: Invalid port value\n"); diff --git a/src/ACLMaxUserIP.cc b/src/ACLMaxUserIP.cc index 4269219d7e..e02c1a9729 100644 --- a/src/ACLMaxUserIP.cc +++ b/src/ACLMaxUserIP.cc @@ -1,5 +1,5 @@ /* - * $Id: ACLMaxUserIP.cc,v 1.10 2006/04/23 11:10:31 robertc Exp $ + * $Id: ACLMaxUserIP.cc,v 1.11 2006/10/08 13:10:34 serassio Exp $ * * DEBUG: section 28 Access Control * AUTHOR: Duane Wessels @@ -102,7 +102,7 @@ ACLMaxUserIP::parse() if (!t) return; - maximum = atoi(t); + maximum = xatoi(t); debug(28, 5) ("aclParseUserMaxIP: Max IP address's %d\n", (int) maximum); diff --git a/src/Parsing.cc b/src/Parsing.cc index 1b8c036512..cb689ba92e 100644 --- a/src/Parsing.cc +++ b/src/Parsing.cc @@ -1,6 +1,6 @@ /* - * $Id: Parsing.cc,v 1.2 2005/11/21 23:06:51 wessels Exp $ + * $Id: Parsing.cc,v 1.3 2006/10/08 13:10:34 serassio Exp $ * * DEBUG: section 3 Configuration File Parsing * AUTHOR: Harvest Derived @@ -39,11 +39,11 @@ * These functions is the same as atoi/l/f, except that they check for errors */ -long -xatol(const char *token) +double +xatof(const char *token) { char *end; - long ret = strtol(token, &end, 10); + double ret = strtod(token, &end); if (ret == 0 && end == token) self_destruct(); @@ -57,6 +57,29 @@ xatoi(const char *token) return xatol(token); } +long +xatol(const char *token) +{ + char *end; + long ret = strtol(token, &end, 10); + + if (end == token || *end) + self_destruct(); + + return ret; +} + +unsigned short +xatos(const char *token) +{ + long port = xatol(token); + + if (port & ~0xFFFF) + self_destruct(); + + return port; +} + int GetInteger(void) { @@ -72,6 +95,17 @@ GetInteger(void) return i; } +u_short +GetShort(void) +{ + char *token = strtok(NULL, w_space); + + if (token == NULL) + self_destruct(); + + return xatos(token); +} + bool StringToInt(const char *s, int &result, const char **p, int base) { diff --git a/src/Parsing.h b/src/Parsing.h index 182251e9ac..91613bad54 100644 --- a/src/Parsing.h +++ b/src/Parsing.h @@ -1,6 +1,6 @@ /* - * $Id: Parsing.h,v 1.2 2005/11/21 23:06:51 wessels Exp $ + * $Id: Parsing.h,v 1.3 2006/10/08 13:10:34 serassio Exp $ * * DEBUG: section 3 Configuration File Parsing * AUTHOR: Harvest Derived @@ -38,9 +38,12 @@ #include "squid.h" -extern long xatol(const char *token); +extern double xatof(const char *token); extern int xatoi(const char *token); +extern long xatol(const char *token); +extern unsigned short xatos(const char *token); extern int GetInteger(void); +extern u_short GetShort(void); // on success, returns true and sets *p (if any) to the end of the integer extern bool StringToInt(const char *str, int &result, const char **p, int base); diff --git a/src/cache_cf.cc b/src/cache_cf.cc index 699d38a7c1..e137b429e1 100644 --- a/src/cache_cf.cc +++ b/src/cache_cf.cc @@ -1,6 +1,6 @@ /* - * $Id: cache_cf.cc,v 1.500 2006/07/02 16:53:46 serassio Exp $ + * $Id: cache_cf.cc,v 1.501 2006/10/08 13:10:34 serassio Exp $ * * DEBUG: section 3 Configuration File Parsing * AUTHOR: Harvest Derived @@ -162,22 +162,6 @@ self_destruct(void) LegacyParser.destruct(); } -/* - * These functions is the same as atoi/l/f, except that they check for errors - */ - -static double -xatof(const char *token) -{ - char *end; - double ret = strtod(token, &end); - - if (ret == 0 && end == token) - self_destruct(); - - return ret; -} - static void update_maxobjsize(void) { @@ -1495,7 +1479,6 @@ parse_peer(peer ** head) { char *token = NULL; peer *p; - int i; CBDATA_INIT_TYPE_FREECB(peer, peerDestroy); p = cbdataAlloc(peer); p->http_port = CACHE_HTTP_PORT; @@ -1516,13 +1499,12 @@ parse_peer(peer ** head) p->type = parseNeighborType(token); - i = GetInteger(); - - p->http_port = (u_short) i; + p->http_port = GetShort(); - i = GetInteger(); + if (!p->http_port) + self_destruct(); - p->icp.port = (u_short) i; + p->icp.port = GetShort(); while ((token = strtok(NULL, w_space))) { if (!strcasecmp(token, "proxy-only")) { @@ -1935,20 +1917,14 @@ static void parse_ushortlist(ushortlist ** P) { char *token; - int i; + u_short i; ushortlist *u; ushortlist **U; while ((token = strtok(NULL, w_space))) { - if (sscanf(token, "%d", &i) != 1) - self_destruct(); - - if (i < 0) - i = 0; - + i = GetShort(); u = xcalloc(1, sizeof(ushortlist)); - - u->i = (u_short) i; + u->i = i; for (U = P; *U; U = &(*U)->next) @@ -2435,14 +2411,7 @@ parse_ushort(u_short * var) void ConfigParser::ParseUShort(u_short *var) { - int i; - - i = GetInteger(); - - if (i < 0) - i = 0; - - *var = (u_short) i; + *var = GetShort(); } void @@ -2626,7 +2595,7 @@ parse_sockaddr_in_list_token(sockaddr_in_list ** head, char *token) /* host:port */ host = token; *t = '\0'; - port = (unsigned short) xatoi(t + 1); + port = xatos(t + 1); if (0 == port) self_destruct(); @@ -2715,16 +2684,15 @@ parse_http_port_specification(http_port_list * s, char *token) /* host:port */ host = token; *t = '\0'; - port = (unsigned short) atoi(t + 1); - - if (0 == port) - self_destruct(); - } else if ((port = atoi(token)) > 0) { - /* port */ + port = xatos(t + 1); } else { - self_destruct(); + /* port */ + port = xatos(token); } + if (port == 0) + self_destruct(); + s->s.sin_port = htons(port); if (NULL == host) @@ -2758,7 +2726,7 @@ parse_http_port_option(http_port_list * s, char *token) s->vport = -1; s->accel = 1; } else if (strncmp(token, "vport=", 6) == 0) { - s->vport = atoi(token + 6); + s->vport = xatos(token + 6); s->accel = 1; } else if (strncmp(token, "protocol=", 9) == 0) { s->protocol = xstrdup(token + 9); -- 2.47.2