Blame modules/mappers/mod_actions.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * mod_actions.c: executes scripts based on MIME type or HTTP method
Packit 90a5c9
 *
Packit 90a5c9
 * by Alexei Kosut; based on mod_cgi.c, mod_mime.c and mod_includes.c,
Packit 90a5c9
 * adapted by rst from original NCSA code by Rob McCool
Packit 90a5c9
 *
Packit 90a5c9
 * Usage instructions:
Packit 90a5c9
 *
Packit 90a5c9
 * Action mime/type /cgi-bin/script
Packit 90a5c9
 *
Packit 90a5c9
 * will activate /cgi-bin/script when a file of content type mime/type is
Packit 90a5c9
 * requested. It sends the URL and file path of the requested document using
Packit 90a5c9
 * the standard CGI PATH_INFO and PATH_TRANSLATED environment variables.
Packit 90a5c9
 *
Packit 90a5c9
 * Script PUT /cgi-bin/script
Packit 90a5c9
 *
Packit 90a5c9
 * will activate /cgi-bin/script when a request is received with the
Packit 90a5c9
 * HTTP method "PUT".  The available method names are defined in httpd.h.
Packit 90a5c9
 * If the method is GET, the script will only be activated if the requested
Packit 90a5c9
 * URI includes query information (stuff after a ?-mark).
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "http_main.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "util_script.h"
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    apr_table_t *action_types;       /* Added with Action... */
Packit 90a5c9
    const char *scripted[METHODS];   /* Added with Script... */
Packit 90a5c9
    int configured;  /* True if Action or Script has been
Packit 90a5c9
                      * called at least once
Packit 90a5c9
                      */
Packit 90a5c9
} action_dir_config;
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA actions_module;
Packit 90a5c9
Packit 90a5c9
static void *create_action_dir_config(apr_pool_t *p, char *dummy)
Packit 90a5c9
{
Packit 90a5c9
    action_dir_config *new =
Packit 90a5c9
    (action_dir_config *) apr_pcalloc(p, sizeof(action_dir_config));
Packit 90a5c9
Packit 90a5c9
    new->action_types = apr_table_make(p, 4);
Packit 90a5c9
Packit 90a5c9
    return new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *merge_action_dir_configs(apr_pool_t *p, void *basev, void *addv)
Packit 90a5c9
{
Packit 90a5c9
    action_dir_config *base = (action_dir_config *) basev;
Packit 90a5c9
    action_dir_config *add = (action_dir_config *) addv;
Packit 90a5c9
    action_dir_config *new = (action_dir_config *) apr_palloc(p,
Packit 90a5c9
                                  sizeof(action_dir_config));
Packit 90a5c9
    int i;
Packit 90a5c9
Packit 90a5c9
    new->action_types = apr_table_overlay(p, add->action_types,
Packit 90a5c9
                                       base->action_types);
Packit 90a5c9
Packit 90a5c9
    for (i = 0; i < METHODS; ++i) {
Packit 90a5c9
        new->scripted[i] = add->scripted[i] ? add->scripted[i]
Packit 90a5c9
                                            : base->scripted[i];
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    new->configured = (base->configured || add->configured);
Packit 90a5c9
    return new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *add_action(cmd_parms *cmd, void *m_v,
Packit 90a5c9
                              const char *type, const char *script,
Packit 90a5c9
                              const char *option)
Packit 90a5c9
{
Packit 90a5c9
    action_dir_config *m = (action_dir_config *)m_v;
Packit 90a5c9
Packit 90a5c9
    if (option && strcasecmp(option, "virtual")) {
Packit 90a5c9
        return apr_pstrcat(cmd->pool,
Packit 90a5c9
                           "unrecognized option '", option, "'", NULL);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    apr_table_setn(m->action_types, type,
Packit 90a5c9
                   apr_pstrcat(cmd->pool, option ? "1" : "0", script, NULL));
Packit 90a5c9
    m->configured = 1;
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_script(cmd_parms *cmd, void *m_v,
Packit 90a5c9
                              const char *method, const char *script)
Packit 90a5c9
{
Packit 90a5c9
    action_dir_config *m = (action_dir_config *)m_v;
Packit 90a5c9
    int methnum;
Packit 90a5c9
    if (cmd->pool == cmd->temp_pool) {
Packit 90a5c9
        /* In .htaccess, we can't globally register new methods. */
Packit 90a5c9
        methnum = ap_method_number_of(method);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* ap_method_register recognizes already registered methods,
Packit 90a5c9
         * so don't bother to check its previous existence explicitly.
Packit 90a5c9
         */
Packit 90a5c9
        methnum = ap_method_register(cmd->pool, method);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (methnum == M_TRACE) {
Packit 90a5c9
        return "TRACE not allowed for Script";
Packit 90a5c9
    }
Packit 90a5c9
    else if (methnum == M_INVALID) {
Packit 90a5c9
        return apr_pstrcat(cmd->pool, "Could not register method '", method,
Packit 90a5c9
                           "' for Script", NULL);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    m->scripted[methnum] = script;
Packit 90a5c9
    m->configured = 1;
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const command_rec action_cmds[] =
Packit 90a5c9
{
Packit 90a5c9
    AP_INIT_TAKE23("Action", add_action, NULL, OR_FILEINFO,
Packit 90a5c9
                  "a media type followed by a script name"),
Packit 90a5c9
    AP_INIT_TAKE2("Script", set_script, NULL, ACCESS_CONF | RSRC_CONF,
Packit 90a5c9
                  "a method followed by a script name"),
Packit 90a5c9
    {NULL}
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static int action_handler(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    action_dir_config *conf = (action_dir_config *)
Packit 90a5c9
        ap_get_module_config(r->per_dir_config, &actions_module);
Packit 90a5c9
    const char *t, *action;
Packit 90a5c9
    const char *script;
Packit 90a5c9
    int i;
Packit 90a5c9
Packit 90a5c9
    if (!conf->configured) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Note that this handler handles _all_ types, so handler is unchecked */
Packit 90a5c9
Packit 90a5c9
    /* Set allowed stuff */
Packit 90a5c9
    for (i = 0; i < METHODS; ++i) {
Packit 90a5c9
        if (conf->scripted[i])
Packit 90a5c9
            r->allowed |= (AP_METHOD_BIT << i);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* First, check for the method-handling scripts */
Packit 90a5c9
    if (r->method_number == M_GET) {
Packit 90a5c9
        if (r->args)
Packit 90a5c9
            script = conf->scripted[M_GET];
Packit 90a5c9
        else
Packit 90a5c9
            script = NULL;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        script = conf->scripted[r->method_number];
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Check for looping, which can happen if the CGI script isn't */
Packit 90a5c9
    if (script && r->prev && r->prev->prev)
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
Packit 90a5c9
    /* Second, check for actions (which override the method scripts) */
Packit 90a5c9
    action = r->handler ? r->handler :
Packit 90a5c9
        ap_field_noparam(r->pool, r->content_type);
Packit 90a5c9
Packit 90a5c9
    if (action && (t = apr_table_get(conf->action_types, action))) {
Packit 90a5c9
        int virtual = (*t++ == '0' ? 0 : 1);
Packit 90a5c9
        if (!virtual && r->finfo.filetype == APR_NOFILE) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00652)
Packit 90a5c9
                          "File does not exist: %s", r->filename);
Packit 90a5c9
            return HTTP_NOT_FOUND;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        script = t;
Packit 90a5c9
        /* propagate the handler name to the script
Packit 90a5c9
         * (will be REDIRECT_HANDLER there)
Packit 90a5c9
         */
Packit 90a5c9
        apr_table_setn(r->subprocess_env, "HANDLER", action);
Packit 90a5c9
        if (virtual) {
Packit 90a5c9
            apr_table_setn(r->notes, "virtual_script", "1");
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (script == NULL)
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
Packit 90a5c9
    ap_internal_redirect_handler(apr_pstrcat(r->pool, script,
Packit 90a5c9
                                             ap_escape_uri(r->pool, r->uri),
Packit 90a5c9
                                             r->args ? "?" : NULL,
Packit 90a5c9
                                             r->args, NULL), r);
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    ap_hook_handler(action_handler,NULL,NULL,APR_HOOK_LAST);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(actions) =
Packit 90a5c9
{
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    create_action_dir_config,   /* dir config creater */
Packit 90a5c9
    merge_action_dir_configs,   /* dir merger --- default is to override */
Packit 90a5c9
    NULL,                       /* server config */
Packit 90a5c9
    NULL,                       /* merge server config */
Packit 90a5c9
    action_cmds,                /* command apr_table_t */
Packit 90a5c9
    register_hooks              /* register hooks */
Packit 90a5c9
};