ISAPI filter to allow access only to hosts with selected names

This article describes an ISAPI filter to allow access only to hosts listed in an external datasource. This sample uses a file (hostdb.txt) to keep track of authorized hostnames, but you might modify this sample to access a database which holds host info.

For each URL request, the filter first looks in a cache of recently allowed hosts, and when that fails, it looks in the hostdb.txt file. This shows an efficient way to authorize connections: a cache allows the filter to quickly allow or disallow host access, and because each request comes in through the filter, speed is critical.

The project is a standard appwizard generated ISAPI filter. There are 5 parameters that can be changed to fine tune the filter: the maximum number of cached hosts, the position after which a cached entry will be moved to the front of the list (to make the search time shorter!), the name of the file that contains the host list and the names of the html files that indicates to the user that its IP address could not be resolved to a host name or that the access was denied for this host. All this parameters are #define directives in the hostnamefilter.h header file.

The filter could be improved in several ways: using a database instead of a file for host information (you should consider using stored procedures to search and/or to cache!), load parameters from registry, automatic selection of the number of cached hosts and the list reorder parameter, etc.

The full source code is provided, you will have to compile it in order to get a working filter. Once you have compiled the project you will need to take the following steps to install:

  1. Run REGEDT32.EXE and modify the server's registry as follows. Select the Filter DLLs key in HKEY_LOCAL_MACHINE\CurrentControlSet\Services\W3SVC\Parameters. Add a local path to hostnamefilter.dll, usually C:\WinNT\System32\InetSrv\hostnamefilter.dll. The filter entries are separated by commas. The order is important, if you have other filters with the same priority, the first one listed will receive the requests first.
  2. Copy the hostnamefilter.dll file to the directory you specified in the registry.
  3. Make sure the System account have execute rights on the filter dll file.
  4. Edit the hostdb.txt file so it contains valid hostnames. The format of the file is:
    host1
    host2.foo.com
    *.edu
    *.foo.net

  5. Copy the hostdb.txt file to the directory you specified in the hostnamefilter.h header file for the host database.
  6. Copy the NoName.htm file to the directory you specified in the hostnamefilter.h header file for the page to indicate that the IP address could not be resolved to a name.
  7. Copy the NoAccess.htm file to the directory you specified in the hostnamefilter.h header file for the page to indicate that the access is denied for this host.
  8. Make sure the System account have read rights on the NoName.htm, NoAccess.htm and hostdb.txt files.
  9. Restart the WWW service.

Download Source Code


Last updated: 23 November 1998



Comments

  • Host Addition

    Posted by Kutilu on 03/15/2010 01:37am

    Hi, Thanks for your wonderful article. But currently iam wondering why i need to restart my IIS in order to have filter recognize the changes done in my hostdb file. Can you explain me what do you mean by storing the hostfile in cache? That is not browser cache right? - Rajee

    Reply
  • how to change the register value?

    Posted by Legacy on 11/07/2002 12:00am

    Originally posted by: yanny

    hi,
    About the register key HKEY_LOCAL_MACHINE\CurrentControlSet\Services\W3SVC\Paramet,
    i don't know how to deal with, is that add a path, or a string? and how to name it? then set the key of the name to the system path?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • Agile development principles have gone from something used only by cutting-edge teams to a mainstream approach used by teams large and small. If you're not using agile methods already though, or if you've only been exposed to agile on small projects here and there, you may wonder if agile can ever work in your environment. Read this eBook to learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds