home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / mod_acce.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-29  |  7.6 KB  |  252 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 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.  
  55. /*
  56.  * Security options etc.
  57.  * 
  58.  * Module derived from code originally written by Rob McCool
  59.  * 
  60.  */
  61.  
  62. #include "httpd.h"
  63. #include "http_core.h"
  64. #include "http_config.h"
  65. #include "http_log.h"
  66.  
  67. typedef struct {
  68.     char *from;
  69.     int limited;
  70. } allowdeny;
  71.  
  72. /* things in the 'order' array */
  73. #define DENY_THEN_ALLOW 0
  74. #define ALLOW_THEN_DENY 1
  75. #define MUTUAL_FAILURE 2
  76.  
  77. typedef struct {
  78.     int order[METHODS];
  79.     array_header *allows;
  80.     array_header *denys;
  81. } access_dir_conf;
  82.  
  83. module access_module;
  84.  
  85. void *create_access_dir_config (pool *p, char *dummy)
  86. {
  87.     access_dir_conf *conf =
  88.         (access_dir_conf *)pcalloc(p, sizeof(access_dir_conf));
  89.     int i;
  90.     
  91.     for (i = 0; i < METHODS; ++i) conf->order[i] = DENY_THEN_ALLOW;
  92.     conf->allows = make_array (p, 1, sizeof (allowdeny));
  93.     conf->denys = make_array (p, 1, sizeof (allowdeny));
  94.     
  95.     return (void *)conf;
  96. }
  97.  
  98. char *order (cmd_parms *cmd, void *dv, char *arg)
  99. {
  100.     access_dir_conf *d = (access_dir_conf *)dv;
  101.     int i, order;
  102.   
  103.     if (!strcasecmp (arg, "allow,deny")) order = ALLOW_THEN_DENY;
  104.     else if (!strcasecmp (arg, "deny,allow")) order = DENY_THEN_ALLOW;
  105.     else if (!strcasecmp (arg, "mutual-failure")) order = MUTUAL_FAILURE;
  106.     else return "unknown order";
  107.  
  108.     for (i = 0; i < METHODS; ++i) 
  109.         if (cmd->limited & (1 << i))
  110.         d->order[i] = order;
  111.     
  112.     return NULL;
  113. }
  114.  
  115. char *allow_cmd (cmd_parms *cmd, void *dv, char *from, char *where)
  116. {
  117.     access_dir_conf *d = (access_dir_conf *)dv;
  118.     allowdeny *a;
  119.   
  120.     if (strcasecmp (from, "from"))
  121.         return "allow and deny must be followed by 'from'";
  122.     
  123.     a = (allowdeny *)push_array (cmd->info ? d->allows : d->denys);
  124.     a->from = pstrdup (cmd->pool, where);
  125.     a->limited = cmd->limited;
  126.     return NULL;
  127. }
  128.  
  129. static char its_an_allow;
  130.  
  131. command_rec access_cmds[] = {
  132. { "order", order, NULL, OR_LIMIT, TAKE1,
  133.     "'allow,deny', 'deny,allow', or 'mutual-failure'" },
  134. { "allow", allow_cmd, &its_an_allow, OR_LIMIT, ITERATE2,
  135.     "'from' followed by hostnames or IP-address wildcards" },
  136. { "deny", allow_cmd, NULL, OR_LIMIT, ITERATE2,
  137.     "'from' followed by hostnames or IP-address wildcards" },
  138. {NULL}
  139. };
  140.  
  141. int in_domain(const char *domain, const char *what) {
  142.     int dl=strlen(domain);
  143.     int wl=strlen(what);
  144.  
  145.     if((wl-dl) >= 0) {
  146.         if (strcmp(domain,&what[wl-dl]) != 0) return 0;
  147.  
  148.     /* Make sure we matched an *entire* subdomain --- if the user
  149.      * said 'allow from good.com', we don't want people from nogood.com
  150.      * to be able to get in.
  151.      */
  152.     
  153.     if (wl == dl) return 1;    /* matched whole thing */
  154.     else return (domain[0] == '.' || what[wl - dl - 1] == '.');
  155.     } else
  156.         return 0;
  157. }
  158.  
  159. int in_ip(char *domain, char *what) {
  160.  
  161.     /* Check a similar screw case to the one checked above ---
  162.      * "allow from 204.26.2" shouldn't let in people from 204.26.23
  163.      */
  164.     
  165.     int l = strlen(domain);
  166.     if (strncmp(domain,what,l) != 0) return 0;
  167.     if (domain[l - 1] == '.') return 1;
  168.     return (what[l] == '\0' || what[l] == '.');
  169. }
  170.  
  171. int find_allowdeny (request_rec *r, array_header *a, int method)
  172. {
  173.     allowdeny *ap = (allowdeny *)a->elts;
  174.     int mmask = (1 << method);
  175.     int i, gothost=0;
  176.     const char *remotehost=NULL;
  177.  
  178.     for (i = 0; i < a->nelts; ++i) {
  179.         if (!(mmask & ap[i].limited))
  180.         continue;
  181.     if (!strcmp (ap[i].from, "all"))
  182.         return 1;
  183.     if (!gothost)
  184.     {
  185.         remotehost = get_remote_host(r->connection, r->per_dir_config,
  186.                      REMOTE_HOST);
  187.         gothost = 1;
  188.     }
  189.         if (remotehost != NULL && isalpha(remotehost[0]))
  190.             if (in_domain(ap[i].from, remotehost))
  191.                 return 1;
  192.         if (in_ip (ap[i].from, r->connection->remote_ip))
  193.             return 1;
  194.     }
  195.  
  196.     return 0;
  197. }
  198.  
  199. int check_dir_access (request_rec *r)
  200. {
  201.     int method = r->method_number;
  202.     access_dir_conf *a =
  203.         (access_dir_conf *)
  204.        get_module_config (r->per_dir_config, &access_module);
  205.     int ret = OK;
  206.                         
  207.     if (a->order[method] == ALLOW_THEN_DENY) {
  208.         ret = FORBIDDEN;
  209.         if (find_allowdeny (r, a->allows, method))
  210.             ret = OK;
  211.         if (find_allowdeny (r, a->denys, method))
  212.             ret = FORBIDDEN;
  213.     } else if (a->order[method] == DENY_THEN_ALLOW) {
  214.         if (find_allowdeny (r, a->denys, method))
  215.             ret = FORBIDDEN;
  216.         if (find_allowdeny (r, a->allows, method))
  217.             ret = OK;
  218.     }
  219.     else {
  220.         if (find_allowdeny(r, a->allows, method) 
  221.         && !find_allowdeny(r, a->denys, method))
  222.         ret = OK;
  223.     else
  224.         ret = FORBIDDEN;
  225.     }
  226.  
  227.     if (ret == FORBIDDEN)
  228.     log_reason ("Client denied by server configuration", r->filename, r);
  229.  
  230.     return ret;
  231. }
  232.  
  233.  
  234.  
  235. module access_module = {
  236.    STANDARD_MODULE_STUFF,
  237.    NULL,            /* initializer */
  238.    create_access_dir_config,    /* dir config creater */
  239.    NULL,            /* dir merger --- default is to override */
  240.    NULL,            /* server config */
  241.    NULL,            /* merge server config */
  242.    access_cmds,
  243.    NULL,            /* handlers */
  244.    NULL,            /* filename translation */
  245.    NULL,            /* check_user_id */
  246.    NULL,            /* check auth */
  247.    check_dir_access,        /* check access */
  248.    NULL,            /* type_checker */
  249.    NULL,            /* fixups */
  250.    NULL                /* logger */
  251. };
  252.