|
Lines 118-128
Link Here
|
| 118 |
|
118 |
|
| 119 |
/* Verify IP address integrity */ |
119 |
/* Verify IP address integrity */ |
| 120 |
static int |
120 |
static int |
| 121 |
verifyIP(char *ip, unsigned long *out) |
121 |
verifyIP(char *ip, unsigned long *mask, unsigned long *out) |
| 122 |
{ |
122 |
{ |
| 123 |
long a, b, c, d; |
123 |
long a, b, c, d; |
| 124 |
char *endptr; |
124 |
char *endptr; |
| 125 |
|
125 |
|
|
|
126 |
unsigned long parsedip; |
| 127 |
unsigned long max_addr = (255 << 24) | (255 << 16) | (255 << 8) | 255; |
| 128 |
|
| 126 |
if (ip == NULL) |
129 |
if (ip == NULL) |
| 127 |
return 0; |
130 |
return 0; |
| 128 |
a = strtol(ip, &endptr, 10); |
131 |
a = strtol(ip, &endptr, 10); |
|
Lines 137-150
Link Here
|
| 137 |
d = strtol(endptr, &endptr, 10); |
140 |
d = strtol(endptr, &endptr, 10); |
| 138 |
if (*endptr != '\0') |
141 |
if (*endptr != '\0') |
| 139 |
return 0; |
142 |
return 0; |
| 140 |
/* Both 0 and 255 are technically valid in nets that are larger |
143 |
if (!_validByte(a) || !_validByte(b) || !_validByte(c) || !_validByte(d)) |
| 141 |
than class C, but at least MS' TCP/IP stacks freak out if they see |
|
|
| 142 |
them. */ |
| 143 |
if (!_validByte(a) || !_validByte(b) || !_validByte(c) || |
| 144 |
!_validByte(d) || (d == 0) || (d == 255)) |
| 145 |
return 0; |
144 |
return 0; |
|
|
145 |
parsedip = (a << 24) | (b << 16) | (c << 8) | d; |
| 146 |
if (out) |
146 |
if (out) |
| 147 |
*out = (a << 24) | (b << 16) | (c << 8) | d; |
147 |
*out = parsedip; |
|
|
148 |
/* |
| 149 |
* The ip address must not be network or broadcast address. |
| 150 |
*/ |
| 151 |
if (mask && ((parsedip == (parsedip & *mask)) || |
| 152 |
(parsedip == ((parsedip & *mask) + max_addr - *mask)))) |
| 153 |
return 0; |
| 148 |
return 1; |
154 |
return 1; |
| 149 |
} |
155 |
} |
| 150 |
|
156 |
|
|
Lines 209-215
Link Here
|
| 209 |
{ |
215 |
{ |
| 210 |
unsigned long parsedgw; |
216 |
unsigned long parsedgw; |
| 211 |
|
217 |
|
| 212 |
if (!verifyIP(gw, &parsedgw)) |
218 |
if (!verifyIP(gw, mask, &parsedgw)) |
| 213 |
return 0; |
219 |
return 0; |
| 214 |
/* Gateway needs to be within the set of IPs reachable through the |
220 |
/* Gateway needs to be within the set of IPs reachable through the |
| 215 |
interface */ |
221 |
interface */ |
|
Lines 228-240
Link Here
|
| 228 |
|
234 |
|
| 229 |
if (!hostname[0]) |
235 |
if (!hostname[0]) |
| 230 |
feepout("Must specify a host name of some sort!"); |
236 |
feepout("Must specify a host name of some sort!"); |
| 231 |
else if (nameserver[0] && !verifyIP(nameserver, NULL) && |
237 |
else if (netmask[0] && !verifyNetmask(netmask, &parsednetmask)) |
|
|
238 |
feepout("Invalid netmask value"); |
| 239 |
else if (nameserver[0] && !verifyIP(nameserver, NULL, NULL) && |
| 232 |
!verifyIP6(nameserver)) |
240 |
!verifyIP6(nameserver)) |
| 233 |
feepout("Invalid name server IP address specified"); |
241 |
feepout("Invalid name server IP address specified"); |
| 234 |
else if (ipaddr[0] && !verifyIP(ipaddr, &parsedip)) |
242 |
else if (ipaddr[0] && !verifyIP(ipaddr, &parsednetmask, &parsedip)) |
| 235 |
feepout("Invalid IPv4 address"); |
243 |
feepout("Invalid IPv4 address"); |
| 236 |
else if (netmask[0] && !verifyNetmask(netmask, &parsednetmask)) |
|
|
| 237 |
feepout("Invalid netmask value"); |
| 238 |
else if (gateway[0] && strcmp(gateway, "NO") && |
244 |
else if (gateway[0] && strcmp(gateway, "NO") && |
| 239 |
!verifyGW(gateway, ipaddr[0] ? &parsedip : NULL, |
245 |
!verifyGW(gateway, ipaddr[0] ? &parsedip : NULL, |
| 240 |
netmask[0] ? &parsednetmask : NULL)) |
246 |
netmask[0] ? &parsednetmask : NULL)) |