Blame apache2/msc_parsers.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 "msc_parsers.h"
Packit 284210
#include <ctype.h>
Packit 284210
Packit 284210
/**
Packit 284210
 *
Packit 284210
 */
Packit 284210
int parse_cookies_v0(modsec_rec *msr, char *_cookie_header,
Packit 284210
                     apr_table_t *cookies, const char *delim)
Packit 284210
{
Packit 284210
    char *attr_name = NULL, *attr_value = NULL;
Packit 284210
    char *cookie_header;
Packit 284210
    char *saveptr = NULL;
Packit 284210
    int cookie_count = 0;
Packit 284210
    char *p = NULL;
Packit 284210
Packit 284210
    if (_cookie_header == NULL) {
Packit 284210
        msr_log(msr, 1, "Cookie parser: Received null for argument.");
Packit 284210
        return -1;
Packit 284210
    }
Packit 284210
Packit 284210
    cookie_header = strdup(_cookie_header);
Packit 284210
    if (cookie_header == NULL) return -1;
Packit 284210
Packit 284210
    if(msr->txcfg->cookiev0_separator == NULL) {
Packit 284210
        p = apr_strtok(cookie_header, delim, &saveptr);
Packit 284210
    } else {
Packit 284210
        p = apr_strtok(cookie_header, msr->txcfg->cookiev0_separator, &saveptr);
Packit 284210
    }
Packit 284210
Packit 284210
    while(p != NULL) {
Packit 284210
        attr_name = NULL;
Packit 284210
        attr_value = NULL;
Packit 284210
Packit 284210
        /* ignore whitespace at the beginning of cookie name */
Packit 284210
        while(isspace(*p)) p++;
Packit 284210
        attr_name = p;
Packit 284210
Packit 284210
        attr_value = strstr(p, "=");
Packit 284210
        if (attr_value != NULL) {
Packit 284210
            /* terminate cookie name */
Packit 284210
            *attr_value = 0;
Packit 284210
            /* move over to the beginning of the value */
Packit 284210
            attr_value++;
Packit 284210
        }
Packit 284210
Packit 284210
        /* we ignore cookies with empty names */
Packit 284210
        if ((attr_name != NULL)&&(strlen(attr_name) != 0)) {
Packit 284210
            if (attr_value != NULL) {
Packit 284210
                if (msr->txcfg->debuglog_level >= 5) {
Packit 284210
                    msr_log(msr, 5, "Adding request cookie: name \"%s\", value \"%s\"",
Packit 284210
                            log_escape(msr->mp, attr_name), log_escape(msr->mp, attr_value));
Packit 284210
                }
Packit 284210
Packit 284210
                apr_table_add(cookies, attr_name, attr_value);
Packit 284210
            } else {
Packit 284210
                if (msr->txcfg->debuglog_level >= 5) {
Packit 284210
                    msr_log(msr, 5, "Adding request cookie: name \"%s\", value empty",
Packit 284210
                            log_escape(msr->mp, attr_name));
Packit 284210
                }
Packit 284210
Packit 284210
                apr_table_add(cookies, attr_name, "");
Packit 284210
            }
Packit 284210
Packit 284210
            cookie_count++;
Packit 284210
        }
Packit 284210
Packit 284210
        if(msr->txcfg->cookiev0_separator == NULL) {
Packit 284210
            p = apr_strtok(NULL, delim, &saveptr);
Packit 284210
        } else {
Packit 284210
            p = apr_strtok(NULL, msr->txcfg->cookiev0_separator, &saveptr);
Packit 284210
        }
Packit 284210
    }
Packit 284210
Packit 284210
    free(cookie_header);
Packit 284210
    return cookie_count;
Packit 284210
}
Packit 284210
Packit 284210
/**
Packit 284210
 *
Packit 284210
 */
Packit 284210
int parse_cookies_v1(modsec_rec *msr, char *_cookie_header,
Packit 284210
        apr_table_t *cookies)
Packit 284210
{
Packit 284210
    char *attr_name = NULL, *attr_value = NULL, *p = NULL;
Packit 284210
    char *prev_attr_name = NULL;
Packit 284210
    char *cookie_header = NULL;
Packit 284210
    int cookie_count = 0;
Packit 284210
Packit 284210
    if (_cookie_header == NULL) return -1;
Packit 284210
    // XXX Should it not match _v0 parser?
Packit 284210
    //if (_cookie_header == NULL) {
Packit 284210
    //    msr_log(msr, 1, "Cookie parser: Received null for argument.");
Packit 284210
    //    return -1;
Packit 284210
    //}
Packit 284210
Packit 284210
    cookie_header = strdup(_cookie_header);
Packit 284210
    if (cookie_header == NULL) return -1;
Packit 284210
Packit 284210
    p = cookie_header;
Packit 284210
    while(*p != 0) {
Packit 284210
        attr_name = NULL;
Packit 284210
        attr_value = NULL;
Packit 284210
Packit 284210
        /* attribute name */
Packit 284210
Packit 284210
        /* remove space from the beginning */
Packit 284210
        while((isspace(*p))&&(*p != 0)) p++;
Packit 284210
        attr_name = p;
Packit 284210
        while((*p != 0)&&(*p != '=')&&(*p != ';')&&(*p != ',')) p++;
Packit 284210
Packit 284210
        /* if we've reached the end of string */
Packit 284210
        if (*p == 0) goto add_cookie;
Packit 284210
Packit 284210
        /* if there is no cookie value supplied */
Packit 284210
        if ((*p == ';')||(*p == ',')) {
Packit 284210
            *p++ = 0; /* terminate the name */
Packit 284210
            goto add_cookie;
Packit 284210
        }
Packit 284210
Packit 284210
        /* terminate the attribute name,
Packit 284210
         * writing over the = character
Packit 284210
         */
Packit 284210
        *p++ = 0;
Packit 284210
Packit 284210
        /* attribute value */
Packit 284210
Packit 284210
        /* skip over the whitespace at the beginning */
Packit 284210
        while((isspace(*p))&&(*p != 0)) p++;
Packit 284210
Packit 284210
        /* no value supplied */
Packit 284210
        if (*p == 0) goto add_cookie;
Packit 284210
Packit 284210
        if (*p == '"') {
Packit 284210
            if (*++p == 0) goto add_cookie;
Packit 284210
            attr_value = p;
Packit 284210
            while((*p != 0)&&(*p != '"')) p++;
Packit 284210
            if (*p != 0) *p++ = 0;
Packit 284210
            else {
Packit 284210
                /* Do nothing about this. */
Packit 284210
            }
Packit 284210
        } else {
Packit 284210
            attr_value = p;
Packit 284210
            while((*p != 0)&&(*p != ',')&&(*p != ';')) p++;
Packit 284210
            if (*p != 0) *p++ = 0;
Packit 284210
Packit 284210
            /* remove the whitespace from the end of cookie value */
Packit 284210
            if (attr_value != NULL) {
Packit 284210
                char *t = attr_value;
Packit 284210
                int i = 0;
Packit 284210
Packit 284210
                while(*t != 0) {
Packit 284210
                    t++;
Packit 284210
                    i++;
Packit 284210
                }
Packit 284210
Packit 284210
                while((i-- > 0)&&(isspace(*(--t)))) *t = 0;
Packit 284210
            }
Packit 284210
        }
Packit 284210
Packit 284210
add_cookie:
Packit 284210
Packit 284210
        /* remove the whitespace from the end of cookie name */
Packit 284210
        if (attr_name != NULL) {
Packit 284210
            char *t = attr_name;
Packit 284210
            int i = 0;
Packit 284210
Packit 284210
            while(*t != 0) {
Packit 284210
                t++;
Packit 284210
                i++;
Packit 284210
            }
Packit 284210
Packit 284210
            while((i-- > 0)&&(isspace(*(--t)))) *t = 0;
Packit 284210
        }
Packit 284210
Packit 284210
        /* add the cookie to the list now */
Packit 284210
        if ((attr_name != NULL)&&(strlen(attr_name) != 0)) {
Packit 284210
Packit 284210
            /* handle special attribute names */
Packit 284210
            if (attr_name[0] == '$') {
Packit 284210
                if (prev_attr_name != NULL) {
Packit 284210
                    /* cookie keyword, we change the name we use
Packit 284210
                     * so they can have a unique name in the cookie table
Packit 284210
                     */
Packit 284210
                    attr_name = apr_psprintf(msr->mp, "$%s_%s", prev_attr_name, attr_name + 1);
Packit 284210
                }
Packit 284210
            }
Packit 284210
Packit 284210
            if (attr_value != NULL) {
Packit 284210
                if (msr->txcfg->debuglog_level >= 5) {
Packit 284210
                    msr_log(msr, 5, "Adding request cookie: name \"%s\", value \"%s\"",
Packit 284210
                            log_escape(msr->mp, attr_name), log_escape(msr->mp, attr_value));
Packit 284210
                }
Packit 284210
Packit 284210
                apr_table_add(cookies, attr_name, attr_value);
Packit 284210
            } else {
Packit 284210
                if (msr->txcfg->debuglog_level >= 5) {
Packit 284210
                    msr_log(msr, 5, "Adding request cookie: name \"%s\", value empty",
Packit 284210
                            log_escape(msr->mp, attr_name));
Packit 284210
                }
Packit 284210
Packit 284210
                apr_table_add(cookies, attr_name, "");
Packit 284210
            }
Packit 284210
Packit 284210
            cookie_count++;
Packit 284210
Packit 284210
            /* only keep the cookie names for later */
Packit 284210
            if (attr_name[0] != '$') prev_attr_name = attr_name;
Packit 284210
        }
Packit 284210
Packit 284210
        /* at this point the *p is either 0 (in which case we exit), or
Packit 284210
         * right after the current cookie ended - we need to look for
Packit 284210
         * the next cookie
Packit 284210
         */
Packit 284210
        while( (*p != 0)&&( (*p == ',')||(*p == ';')||(isspace(*p)) ) ) p++;
Packit 284210
    }
Packit 284210
Packit 284210
    free(cookie_header);
Packit 284210
    return cookie_count;
Packit 284210
}
Packit 284210
Packit 284210
/**
Packit 284210
 *
Packit 284210
 */
Packit 284210
int parse_arguments(modsec_rec *msr, const char *s, apr_size_t inputlength,
Packit 284210
        int argument_separator, const char *origin,
Packit 284210
        apr_table_t *arguments, int *invalid_count)
Packit 284210
{
Packit 284210
    msc_arg *arg;
Packit 284210
    apr_size_t i, j;
Packit 284210
    char *value = NULL;
Packit 284210
    char *buf;
Packit 284210
    int status;
Packit 284210
    int changed;
Packit 284210
Packit 284210
    if (s == NULL) return -1;
Packit 284210
    if (inputlength == 0) return 1;
Packit 284210
Packit 284210
    /* Check that adding one will not overflow */
Packit 284210
    if (inputlength + 1 <= 0) return -1;
Packit 284210
Packit 284210
    buf = (char *)malloc(inputlength + 1);
Packit 284210
    if (buf == NULL) return -1;
Packit 284210
Packit 284210
    arg = (msc_arg *)apr_pcalloc(msr->mp, sizeof(msc_arg));
Packit 284210
    arg->origin = origin;
Packit 284210
Packit 284210
    i = 0;
Packit 284210
    j = 0;
Packit 284210
    status = 0;
Packit 284210
    *invalid_count = 0;
Packit 284210
    while (i < inputlength) {
Packit 284210
        if (status == 0) {
Packit 284210
            /* parameter name */
Packit 284210
            arg->name_origin_offset = i;
Packit 284210
            while ((s[i] != '=') && (s[i] != argument_separator) && (i < inputlength)) {
Packit 284210
                buf[j] = s[i];
Packit 284210
                j++;
Packit 284210
                i++;
Packit 284210
            }
Packit 284210
            buf[j++] = '\0';
Packit 284210
            arg->name_origin_len = i - arg->name_origin_offset;
Packit 284210
        } else {
Packit 284210
            /* parameter value */
Packit 284210
            arg->value_origin_offset = i;
Packit 284210
            while ((s[i] != argument_separator) && (i < inputlength)) {
Packit 284210
                buf[j] = s[i];
Packit 284210
                j++;
Packit 284210
                i++;
Packit 284210
            }
Packit 284210
            buf[j++] = '\0';
Packit 284210
            arg->value_origin_len = i - arg->value_origin_offset;
Packit 284210
        }
Packit 284210
Packit 284210
        if (status == 0) {
Packit 284210
            arg->name_len = urldecode_nonstrict_inplace_ex((unsigned char *)buf, arg->name_origin_len, invalid_count, &changed);
Packit 284210
            arg->name = apr_pstrmemdup(msr->mp, buf, arg->name_len);
Packit 284210
Packit 284210
            if (s[i] == argument_separator) {
Packit 284210
                /* Empty parameter */
Packit 284210
                arg->value_len = 0;
Packit 284210
                arg->value = "";
Packit 284210
Packit 284210
                add_argument(msr, arguments, arg);
Packit 284210
Packit 284210
                arg = (msc_arg *)apr_pcalloc(msr->mp, sizeof(msc_arg));
Packit 284210
                arg->origin = origin;
Packit 284210
Packit 284210
                status = 0; /* unchanged */
Packit 284210
                j = 0;
Packit 284210
            } else {
Packit 284210
                status = 1;
Packit 284210
                value = &buf[j];
Packit 284210
            }
Packit 284210
        }
Packit 284210
        else {
Packit 284210
            arg->value_len = urldecode_nonstrict_inplace_ex((unsigned char *)value, arg->value_origin_len, invalid_count, &changed);
Packit 284210
            arg->value = apr_pstrmemdup(msr->mp, value, arg->value_len);
Packit 284210
Packit 284210
            add_argument(msr, arguments, arg);
Packit 284210
Packit 284210
            arg = (msc_arg *)apr_pcalloc(msr->mp, sizeof(msc_arg));
Packit 284210
            arg->origin = origin;
Packit 284210
Packit 284210
            status = 0;
Packit 284210
            j = 0;
Packit 284210
        }
Packit 284210
Packit 284210
        i++; /* skip over the separator */
Packit 284210
    }
Packit 284210
Packit 284210
    /* the last parameter was empty */
Packit 284210
    if (status == 1) {
Packit 284210
        arg->value_len = 0;
Packit 284210
        arg->value = "";
Packit 284210
Packit 284210
        add_argument(msr, arguments, arg);
Packit 284210
    }
Packit 284210
Packit 284210
    free(buf);
Packit 284210
Packit 284210
    return 1;
Packit 284210
}
Packit 284210
Packit 284210
/**
Packit 284210
 *
Packit 284210
 */
Packit 284210
void add_argument(modsec_rec *msr, apr_table_t *arguments, msc_arg *arg)
Packit 284210
{
Packit 284210
    if (msr->txcfg->debuglog_level >= 5) {
Packit 284210
        msr_log(msr, 5, "Adding request argument (%s): name \"%s\", value \"%s\"",
Packit 284210
                arg->origin, log_escape_ex(msr->mp, arg->name, arg->name_len),
Packit 284210
                log_escape_ex(msr->mp, arg->value, arg->value_len));
Packit 284210
    }
Packit 284210
Packit 284210
    apr_table_addn(arguments, log_escape_nq_ex(msr->mp, arg->name, arg->name_len), (void *)arg);
Packit 284210
}
Packit 284210