/* mod_auth_everyone
 * Everyone authn+authz module for Apache 2
 *
 * Joe Presbrey <presbrey@csail.mit.edu>
 *
 * $Id: mod_auth_everyone.c 28614 2010-03-19 20:54:09Z presbrey $
 */

#include "apr_strings.h"

#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_request.h"

module AP_MODULE_DECLARE_DATA auth_everyone_module;

static int auth_everyone(request_rec *r)
{
    int m = r->method_number;
    register int x;
    const char *t, *w;
    const apr_array_header_t *reqs_arr = ap_requires(r);
    require_line *reqs;

    if (!reqs_arr) {
        return DECLINED;
    }
    reqs = (require_line *)reqs_arr->elts;

    for (x = 0; x < reqs_arr->nelts; x++) {
        if (!(reqs[x].method_mask & (AP_METHOD_BIT << m))) {
            continue;
        }

        t = reqs[x].requirement;
        w = ap_getword_white(r->pool, &t);
        if (!strcasecmp(w, "everyone")) {
            return OK;
        }
    }

    return DECLINED;
}

static void register_hooks(apr_pool_t *p)
{
    ap_hook_check_user_id(auth_everyone,NULL,NULL,APR_HOOK_LAST-1);
    ap_hook_auth_checker(auth_everyone,NULL,NULL,APR_HOOK_LAST-1);
}

module AP_MODULE_DECLARE_DATA auth_everyone_module =
{
    STANDARD20_MODULE_STUFF,
    NULL,                            /* dir config creater */
    NULL,                            /* dir merger --- default is to override */
    NULL,                            /* server config */
    NULL,                            /* merge server config */
    NULL,                            /* command apr_table_t */
    register_hooks                   /* register hooks */
};
