Blame ext/mod_tfn_reverse.c

Packit 284210
/*
Packit 284210
* ModSecurity for Apache 2.x, http://www.modsecurity.org/
Packit 284210
* Copyright (c) 2004-2013 Trustwave Holdings, Inc. (http://www.trustwave.com/)
Packit 284210
*
Packit 284210
* You may not use this file except in compliance with
Packit 284210
* the License.  You may obtain a copy of the License at
Packit 284210
*
Packit 284210
*     http://www.apache.org/licenses/LICENSE-2.0
Packit 284210
*
Packit 284210
* If any of the files related to licensing are missing or if you have any
Packit 284210
* other questions related to licensing please contact Trustwave Holdings, Inc.
Packit 284210
* directly using the email address security@modsecurity.org.
Packit 284210
*/
Packit 284210
Packit 284210
#include "httpd.h"
Packit 284210
#include "http_core.h"
Packit 284210
#include "http_config.h"
Packit 284210
#include "http_log.h"
Packit 284210
#include "http_protocol.h"
Packit 284210
#include "ap_config.h"
Packit 284210
#include "apr_optional.h"
Packit 284210
Packit 284210
#include "modsecurity.h"
Packit 284210
Packit 284210
/**
Packit 284210
 * This function will be invoked by
Packit 284210
 * ModSecurity to transform input.
Packit 284210
 */
Packit 284210
static int reverse(apr_pool_t *mptmp, unsigned char *input,
Packit 284210
    long int input_len, char **rval, long int *rval_len)
Packit 284210
{
Packit 284210
    /* Transformation functions can choose to do their
Packit 284210
     * thing in-place, overwriting the existing content. This
Packit 284210
     * is normally possible only if the transformed content
Packit 284210
     * is of equal length or shorter.
Packit 284210
     *
Packit 284210
     * If you need to expand the content use the temporary
Packit 284210
     * memory pool mptmp to allocate the space.
Packit 284210
     */
Packit 284210
Packit 284210
    /* Reverse the string in place, but only if it's long enough. */
Packit 284210
    if (input_len > 1) {
Packit 284210
        long int i = 0;
Packit 284210
        long int j = input_len - 1;
Packit 284210
        while(i < j) {
Packit 284210
            char c = input[i];
Packit 284210
            input[i] = input[j];
Packit 284210
            input[j] = c;
Packit 284210
            i++;
Packit 284210
            j--;
Packit 284210
        }
Packit 284210
    }
Packit 284210
Packit 284210
    /* Tell ModSecurity about the content
Packit 284210
     * we have generated. In this case we
Packit 284210
     * merely point back to the input buffer.
Packit 284210
     */
Packit 284210
    *rval = (char *)input;
Packit 284210
    *rval_len = input_len;
Packit 284210
Packit 284210
    /* Must return 1 if the content was
Packit 284210
     * changed, or 0 otherwise.
Packit 284210
     */
Packit 284210
    return 1; 
Packit 284210
}
Packit 284210
Packit 284210
static int hook_pre_config(apr_pool_t *mp, apr_pool_t *mp_log, apr_pool_t *mp_temp) {
Packit 284210
    void (*fn)(const char *name, void *fn);
Packit 284210
Packit 284210
    /* Look for the registration function
Packit 284210
     * exported by ModSecurity.
Packit 284210
     */
Packit 284210
    fn = APR_RETRIEVE_OPTIONAL_FN(modsec_register_tfn);
Packit 284210
    if (fn) {
Packit 284210
        /* Use it to register our new
Packit 284210
         * transformation function under the
Packit 284210
         * name "reverse".
Packit 284210
         */
Packit 284210
        fn("reverse", (void *)reverse);
Packit 284210
    } else {
Packit 284210
        ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
Packit 284210
            "mod_tfn_reverse: Unable to find modsec_register_tfn.");
Packit 284210
    }
Packit 284210
Packit 284210
    return OK;
Packit 284210
}
Packit 284210
Packit 284210
static void register_hooks(apr_pool_t *p) {
Packit 284210
    ap_hook_pre_config(hook_pre_config, NULL, NULL, APR_HOOK_LAST);
Packit 284210
}
Packit 284210
Packit 284210
/* Dispatch list for API hooks */
Packit 284210
module AP_MODULE_DECLARE_DATA tfn_reverse_module = {
Packit 284210
    STANDARD20_MODULE_STUFF, 
Packit 284210
    NULL,                  /* create per-dir    config structures */
Packit 284210
    NULL,                  /* merge  per-dir    config structures */
Packit 284210
    NULL,                  /* create per-server config structures */
Packit 284210
    NULL,                  /* merge  per-server config structures */
Packit 284210
    NULL,                  /* table of config file commands       */
Packit 284210
    register_hooks         /* register hooks                      */
Packit 284210
};
Packit 284210