home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / linux / apache / contrib / modules / mod_block.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  5.1 KB  |  155 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1996 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54. /* mod_block.c - by Alexei Kosut <akosut@organic.com> June, 1996
  55.  *
  56.  * This module allows you to block access to your site that was
  57.  * referred to from a certain URL. This isn't a 100%-foolproof
  58.  * way of preventing access to your site, but if someone has a link
  59.  * to a page or image of yours, that you don't like, it's a pretty
  60.  * good way of blocking access from that location.
  61.  *
  62.  * Example:
  63.  * BlockReferer www.organic.com
  64.  * BlockReferer netscape.com/comprod/sales/index.html
  65.  */
  66.  
  67.  
  68. #include "httpd.h"
  69. #include "http_config.h"
  70.  
  71. module block_module;
  72.  
  73. typedef struct {
  74.     table *block_referer_list;
  75. } block_state;
  76.  
  77. void *make_block_state (pool *p, server_rec *s)
  78. {
  79.     block_state *cls =
  80.       (block_state *)palloc (p, sizeof (block_state));
  81.  
  82.     cls->block_referer_list = make_table(p, 1);
  83.     return (void *)cls;
  84. }
  85.  
  86. void *merge_block_state (pool *p, void *basev, void *overridesv)
  87. {
  88.    block_state *a = (block_state *)pcalloc (p, sizeof (block_state));
  89.    block_state *base = (block_state *)basev,
  90.      *overrides = (block_state *)overridesv;
  91.  
  92.    a->block_referer_list = overlay_tables (p, overrides->block_referer_list,
  93.                       base->block_referer_list);
  94.  
  95.    return a;
  96. }
  97.  
  98. char *add_referer_block (cmd_parms *parms, void *dummy, char *arg)
  99. {
  100.     block_state *cls = get_module_config (parms->server->module_config,
  101.                           &block_module);
  102.  
  103.     table_set(cls->block_referer_list, arg, arg);
  104.     
  105.     return NULL;
  106. }
  107.  
  108. command_rec block_cmds[] = {
  109. { "BlockReferer", add_referer_block, NULL, RSRC_CONF, ITERATE,
  110.     "referer hostnames to block" },
  111. { NULL }
  112. };
  113.  
  114. int proc_block_trans(request_rec *r)
  115. {
  116.     block_state *cls = get_module_config (r->server->module_config,
  117.                       &block_module);
  118.   
  119.     char *referer = table_get(r->headers_in, "Referer");
  120.     array_header *list_arr = table_elts(cls->block_referer_list);
  121.     table_entry *list = (table_entry *)list_arr->elts;
  122.     int i;
  123.  
  124.     if (!referer || !list)
  125.         return DECLINED;    
  126.  
  127.     for (i = 0; i < list_arr->nelts; ++i) {
  128.         char *str = list[i].key;
  129.  
  130.     if (str && strstr(referer, str))
  131.       return FORBIDDEN;
  132.     }
  133.  
  134.     return DECLINED;
  135. }
  136.  
  137.  
  138. module block_module = {
  139.    STANDARD_MODULE_STUFF,
  140.    NULL,            /* initializer */
  141.    NULL,            /* create per-dir config */
  142.    NULL,            /* merge per-dir config */
  143.    make_block_state,        /* server config */
  144.    merge_block_state,               /* merge server config */
  145.    block_cmds,            /* command table */
  146.    NULL,            /* handlers */
  147.    NULL,            /* filename translation */
  148.    NULL,            /* check_user_id */
  149.    NULL,            /* check auth */
  150.    proc_block_trans,        /* check access */
  151.    NULL,            /* type_checker */
  152.    NULL,            /* fixups */
  153.    NULL                /* logger */
  154. };
  155.