21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

if (errno != EINTR && errno != EAGAIN) return -1;<br />

}<br />

for (;;) {<br />

he = gethostbyaddr((char *)&addr->sin_addr, sizeof(addr->sin_addr), AF_INET);<br />

if (he) break;<br />

if (h_errno = = HOST_NOT_FOUND) {<br />

endhostent( );<br />

return SPC_ERROR_NOREVERSE;<br />

}<br />

if (h_errno != TRY_AGAIN) {<br />

endhostent( );<br />

return -1;<br />

}<br />

}<br />

hostname = strdup(he->h_name);<br />

for (;;) {<br />

if ((he = gethostbyname(hostname)) != 0) break;<br />

if (h_errno = = HOST_NOT_FOUND) {<br />

endhostent( );<br />

free(hostname);<br />

return SPC_ERROR_NOHOSTNAME;<br />

}<br />

if (h_errno != TRY_AGAIN) {<br />

endhostent( );<br />

free(hostname);<br />

return -1;<br />

}<br />

}<br />

/* Check all IP addresses returned for the hostname. If one matches, return<br />

* 0 to indicate that the address is not likely being spoofed.<br />

*/<br />

for (i = 0; he->h_addr_list[i]; i++)<br />

if (*(in_addr_t *)he->h_addr_list[i] = = addr->sin_addr.s_addr) {<br />

*name = hostname;<br />

endhostent( );<br />

return 0;<br />

}<br />

/* No matches. Spoofing very likely */<br />

free(hostname);<br />

endhostent( );<br />

return SPC_ERROR_BADHOSTNAME;<br />

}<br />

The next code listing contains several worker functions as well as the function spc_<br />

host_init( ), which requires a single argument that is the name of a file from which<br />

access restriction information is to be read. The access restriction information is read<br />

from the file and stored in an in-memory list, which is then used by spc_host_check( )<br />

(we’ll describe that function shortly).<br />

Restricting Access Based on Hostname or IP Address | 381<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!