home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / modules / standard / mod_access.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  10.4 KB  |  411 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58. /*
  59.  * Security options etc.
  60.  * 
  61.  * Module derived from code originally written by Rob McCool
  62.  * 
  63.  */
  64.  
  65. #include "httpd.h"
  66. #include "http_core.h"
  67. #include "http_config.h"
  68. #include "http_log.h"
  69. #include "http_request.h"
  70.  
  71. enum allowdeny_type {
  72.     T_ENV,
  73.     T_ALL,
  74.     T_IP,
  75.     T_HOST,
  76.     T_FAIL
  77. };
  78.  
  79. typedef struct {
  80.     int limited;
  81.     union {
  82.     char *from;
  83.     struct {
  84.         unsigned long net;
  85.         unsigned long mask;
  86.     } ip;
  87.     } x;
  88.     enum allowdeny_type type;
  89. } allowdeny;
  90.  
  91. /* things in the 'order' array */
  92. #define DENY_THEN_ALLOW 0
  93. #define ALLOW_THEN_DENY 1
  94. #define MUTUAL_FAILURE 2
  95.  
  96. typedef struct {
  97.     int order[METHODS];
  98.     array_header *allows;
  99.     array_header *denys;
  100. } access_dir_conf;
  101.  
  102. module MODULE_VAR_EXPORT access_module;
  103.  
  104. static void *create_access_dir_config(pool *p, char *dummy)
  105. {
  106.     access_dir_conf *conf =
  107.     (access_dir_conf *) ap_pcalloc(p, sizeof(access_dir_conf));
  108.     int i;
  109.  
  110.     for (i = 0; i < METHODS; ++i)
  111.     conf->order[i] = DENY_THEN_ALLOW;
  112.     conf->allows = ap_make_array(p, 1, sizeof(allowdeny));
  113.     conf->denys = ap_make_array(p, 1, sizeof(allowdeny));
  114.  
  115.     return (void *) conf;
  116. }
  117.  
  118. static const char *order(cmd_parms *cmd, void *dv, char *arg)
  119. {
  120.     access_dir_conf *d = (access_dir_conf *) dv;
  121.     int i, o;
  122.  
  123.     if (!strcasecmp(arg, "allow,deny"))
  124.     o = ALLOW_THEN_DENY;
  125.     else if (!strcasecmp(arg, "deny,allow"))
  126.     o = DENY_THEN_ALLOW;
  127.     else if (!strcasecmp(arg, "mutual-failure"))
  128.     o = MUTUAL_FAILURE;
  129.     else
  130.     return "unknown order";
  131.  
  132.     for (i = 0; i < METHODS; ++i)
  133.     if (cmd->limited & (1 << i))
  134.         d->order[i] = o;
  135.  
  136.     return NULL;
  137. }
  138.  
  139. static int is_ip(const char *host)
  140. {
  141.     while ((*host == '.') || ap_isdigit(*host))
  142.     host++;
  143.     return (*host == '\0');
  144. }
  145.  
  146. static const char *allow_cmd(cmd_parms *cmd, void *dv, char *from, char *where)
  147. {
  148.     access_dir_conf *d = (access_dir_conf *) dv;
  149.     allowdeny *a;
  150.     char *s;
  151.  
  152.     if (strcasecmp(from, "from"))
  153.     return "allow and deny must be followed by 'from'";
  154.  
  155.     a = (allowdeny *) ap_push_array(cmd->info ? d->allows : d->denys);
  156.     a->x.from = where;
  157.     a->limited = cmd->limited;
  158.  
  159.     if (!strncasecmp(where, "env=", 4)) {
  160.     a->type = T_ENV;
  161.     a->x.from += 4;
  162.  
  163.     }
  164.     else if (!strcasecmp(where, "all")) {
  165.     a->type = T_ALL;
  166.  
  167.     }
  168.     else if ((s = strchr(where, '/'))) {
  169.     unsigned long mask;
  170.  
  171.     a->type = T_IP;
  172.     /* trample on where, we won't be using it any more */
  173.     *s++ = '\0';
  174.  
  175.     if (!is_ip(where)
  176.         || (a->x.ip.net = ap_inet_addr(where)) == INADDR_NONE) {
  177.         a->type = T_FAIL;
  178.         return "syntax error in network portion of network/netmask";
  179.     }
  180.  
  181.     /* is_ip just tests if it matches [\d.]+ */
  182.     if (!is_ip(s)) {
  183.         a->type = T_FAIL;
  184.         return "syntax error in mask portion of network/netmask";
  185.     }
  186.     /* is it in /a.b.c.d form? */
  187.     if (strchr(s, '.')) {
  188.         mask = ap_inet_addr(s);
  189.         if (mask == INADDR_NONE) {
  190.         a->type = T_FAIL;
  191.         return "syntax error in mask portion of network/netmask";
  192.         }
  193.     }
  194.     else {
  195.         /* assume it's in /nnn form */
  196.         mask = atoi(s);
  197.         if (mask > 32 || mask <= 0) {
  198.         a->type = T_FAIL;
  199.         return "invalid mask in network/netmask";
  200.         }
  201.         mask = 0xFFFFFFFFUL << (32 - mask);
  202.         mask = htonl(mask);
  203.     }
  204.     a->x.ip.mask = mask;
  205.  
  206.     }
  207.     else if (ap_isdigit(*where) && is_ip(where)) {
  208.     /* legacy syntax for ip addrs: a.b.c. ==> a.b.c.0/24 for example */
  209.     int shift;
  210.     char *t;
  211.     int octet;
  212.  
  213.     a->type = T_IP;
  214.     /* parse components */
  215.     s = where;
  216.     a->x.ip.net = 0;
  217.     a->x.ip.mask = 0;
  218.     shift = 24;
  219.     while (*s) {
  220.         t = s;
  221.         if (!ap_isdigit(*t)) {
  222.         a->type = T_FAIL;
  223.         return "invalid ip address";
  224.         }
  225.         while (ap_isdigit(*t)) {
  226.         ++t;
  227.         }
  228.         if (*t == '.') {
  229.         *t++ = 0;
  230.         }
  231.         else if (*t) {
  232.         a->type = T_FAIL;
  233.         return "invalid ip address";
  234.         }
  235.         if (shift < 0) {
  236.         return "invalid ip address, only 4 octets allowed";
  237.         }
  238.         octet = atoi(s);
  239.         if (octet < 0 || octet > 255) {
  240.         a->type = T_FAIL;
  241.         return "each octet must be between 0 and 255 inclusive";
  242.         }
  243.         a->x.ip.net |= octet << shift;
  244.         a->x.ip.mask |= 0xFFUL << shift;
  245.         s = t;
  246.         shift -= 8;
  247.     }
  248.     a->x.ip.net = ntohl(a->x.ip.net);
  249.     a->x.ip.mask = ntohl(a->x.ip.mask);
  250.     }
  251.     else {
  252.     a->type = T_HOST;
  253.     }
  254.  
  255.     return NULL;
  256. }
  257.  
  258. static char its_an_allow;
  259.  
  260. static const command_rec access_cmds[] =
  261. {
  262.     {"order", order, NULL, OR_LIMIT, TAKE1,
  263.      "'allow,deny', 'deny,allow', or 'mutual-failure'"},
  264.     {"allow", allow_cmd, &its_an_allow, OR_LIMIT, ITERATE2,
  265.      "'from' followed by hostnames or IP-address wildcards"},
  266.     {"deny", allow_cmd, NULL, OR_LIMIT, ITERATE2,
  267.      "'from' followed by hostnames or IP-address wildcards"},
  268.     {NULL}
  269. };
  270.  
  271. static int in_domain(const char *domain, const char *what)
  272. {
  273.     int dl = strlen(domain);
  274.     int wl = strlen(what);
  275.  
  276.     if ((wl - dl) >= 0) {
  277.     if (strcasecmp(domain, &what[wl - dl]) != 0)
  278.         return 0;
  279.  
  280.     /* Make sure we matched an *entire* subdomain --- if the user
  281.      * said 'allow from good.com', we don't want people from nogood.com
  282.      * to be able to get in.
  283.      */
  284.  
  285.     if (wl == dl)
  286.         return 1;        /* matched whole thing */
  287.     else
  288.         return (domain[0] == '.' || what[wl - dl - 1] == '.');
  289.     }
  290.     else
  291.     return 0;
  292. }
  293.  
  294. static int find_allowdeny(request_rec *r, array_header *a, int method)
  295. {
  296.     allowdeny *ap = (allowdeny *) a->elts;
  297.     int mmask = (1 << method);
  298.     int i;
  299.     int gothost = 0;
  300.     const char *remotehost = NULL;
  301.  
  302.     for (i = 0; i < a->nelts; ++i) {
  303.     if (!(mmask & ap[i].limited))
  304.         continue;
  305.  
  306.     switch (ap[i].type) {
  307.     case T_ENV:
  308.         if (ap_table_get(r->subprocess_env, ap[i].x.from)) {
  309.         return 1;
  310.         }
  311.         break;
  312.  
  313.     case T_ALL:
  314.         return 1;
  315.  
  316.     case T_IP:
  317.         if (ap[i].x.ip.net != INADDR_NONE
  318.         && (r->connection->remote_addr.sin_addr.s_addr
  319.             & ap[i].x.ip.mask) == ap[i].x.ip.net) {
  320.         return 1;
  321.         }
  322.         break;
  323.  
  324.     case T_HOST:
  325.         if (!gothost) {
  326.         remotehost = ap_get_remote_host(r->connection, r->per_dir_config,
  327.                         REMOTE_DOUBLE_REV);
  328.  
  329.         if ((remotehost == NULL) || is_ip(remotehost))
  330.             gothost = 1;
  331.         else
  332.             gothost = 2;
  333.         }
  334.  
  335.         if ((gothost == 2) && in_domain(ap[i].x.from, remotehost))
  336.         return 1;
  337.         break;
  338.  
  339.     case T_FAIL:
  340.         /* do nothing? */
  341.         break;
  342.     }
  343.     }
  344.  
  345.     return 0;
  346. }
  347.  
  348. static int check_dir_access(request_rec *r)
  349. {
  350.     int method = r->method_number;
  351.     access_dir_conf *a =
  352.     (access_dir_conf *)
  353.     ap_get_module_config(r->per_dir_config, &access_module);
  354.     int ret = OK;
  355.  
  356.     if (a->order[method] == ALLOW_THEN_DENY) {
  357.     ret = FORBIDDEN;
  358.     if (find_allowdeny(r, a->allows, method))
  359.         ret = OK;
  360.     if (find_allowdeny(r, a->denys, method))
  361.         ret = FORBIDDEN;
  362.     }
  363.     else if (a->order[method] == DENY_THEN_ALLOW) {
  364.     if (find_allowdeny(r, a->denys, method))
  365.         ret = FORBIDDEN;
  366.     if (find_allowdeny(r, a->allows, method))
  367.         ret = OK;
  368.     }
  369.     else {
  370.     if (find_allowdeny(r, a->allows, method)
  371.         && !find_allowdeny(r, a->denys, method))
  372.         ret = OK;
  373.     else
  374.         ret = FORBIDDEN;
  375.     }
  376.  
  377.     if (ret == FORBIDDEN
  378.     && (ap_satisfies(r) != SATISFY_ANY || !ap_some_auth_required(r))) {
  379.     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
  380.           "client denied by server configuration: %s",
  381.           r->filename);
  382.     }
  383.  
  384.     return ret;
  385. }
  386.  
  387.  
  388.  
  389. module MODULE_VAR_EXPORT access_module =
  390. {
  391.     STANDARD_MODULE_STUFF,
  392.     NULL,            /* initializer */
  393.     create_access_dir_config,    /* dir config creater */
  394.     NULL,            /* dir merger --- default is to override */
  395.     NULL,            /* server config */
  396.     NULL,            /* merge server config */
  397.     access_cmds,
  398.     NULL,            /* handlers */
  399.     NULL,            /* filename translation */
  400.     NULL,            /* check_user_id */
  401.     NULL,            /* check auth */
  402.     check_dir_access,        /* check access */
  403.     NULL,            /* type_checker */
  404.     NULL,            /* fixups */
  405.     NULL,            /* logger */
  406.     NULL,            /* header parser */
  407.     NULL,            /* child_init */
  408.     NULL,            /* child_exit */
  409.     NULL            /* post read-request */
  410. };
  411.