Blame alp2/alp2_pp.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 <ctype.h>
Packit 284210
#include <stdio.h>
Packit 284210
#include <sys/param.h>
Packit 284210
Packit 284210
#include "alp2_pp.h"
Packit 284210
Packit 284210
/**
Packit 284210
 * Take the line in the buffer and replace the new line
Packit 284210
 * at the end with a NUL byte.
Packit 284210
 */
Packit 284210
char *alp2_pp_line_chomp(alp2_pp_t *pp) {
Packit 284210
    if (pp->line_pos == 0) {
Packit 284210
        pp->line_buf[0] = '\0';
Packit 284210
    }
Packit 284210
    else {
Packit 284210
        pp->line_buf[pp->line_pos - 1] = '\0';
Packit 284210
    }
Packit 284210
Packit 284210
    return &(pp->line_buf[0]);
Packit 284210
}
Packit 284210
Packit 284210
/**
Packit 284210
 * Look into the line buffer to determine if it
Packit 284210
 * contains a boundary line.
Packit 284210
 */
Packit 284210
static int alp2_pp_is_boundary_line(alp2_pp_t *alp_pp) {
Packit 284210
    char *new_boundary = NULL;
Packit 284210
    unsigned int id;
Packit 284210
    size_t i;
Packit 284210
Packit 284210
    /* A boundary line cannot be less than 14 characters long. */
Packit 284210
    if (alp_pp->line_pos < 15) {
Packit 284210
        return 0;
Packit 284210
    }
Packit 284210
Packit 284210
    /* The first two characters must both be dashes. */
Packit 284210
    if ((alp_pp->line_buf[0] != '-')||(alp_pp->line_buf[1] != '-')) {
Packit 284210
        return 0;
Packit 284210
    }
Packit 284210
Packit 284210
    /* Extract the boundary. */
Packit 284210
    i = 2; /* Start after the second dash. */
Packit 284210
    while((isxdigit(alp_pp->line_buf[i]))&&(i < alp_pp->line_pos)) {
Packit 284210
        i++;
Packit 284210
    }    
Packit 284210
Packit 284210
    /* The boundary cannot be shorter than 8 characters. */
Packit 284210
    if (i - 2 < 8) {
Packit 284210
        return 0;
Packit 284210
    }
Packit 284210
Packit 284210
    // TODO Memory leak; use a single parser buffer to avoid per-entry
Packit 284210
    //      allocation from the parser pool.
Packit 284210
    new_boundary = apr_pstrndup(alp_pp->mp, &(alp_pp->line_buf[2]), i - 2);
Packit 284210
Packit 284210
    /* Check if the rest of the line is valid. */
Packit 284210
    if (  (i + 5 < alp_pp->line_pos) /* Need at lest 5 more bytes. */
Packit 284210
        ||(alp_pp->line_buf[i + 0] != '-')
Packit 284210
        ||(alp_pp->line_buf[i + 1] < 'A')
Packit 284210
        ||(alp_pp->line_buf[i + 1] > 'Z')
Packit 284210
        ||(alp_pp->line_buf[i + 2] != '-')
Packit 284210
        ||(alp_pp->line_buf[i + 3] != '-')
Packit 284210
        ||(alp_pp->line_buf[i + 4] != '\n') )
Packit 284210
    {
Packit 284210
        return 0;
Packit 284210
    }
Packit 284210
Packit 284210
    id = alp_pp->line_buf[i + 1];
Packit 284210
Packit 284210
    /* Are we in a middle of an entry right now? */
Packit 284210
    if (alp_pp->current_entry == NULL) {
Packit 284210
        /* We will accept a new boundary. */
Packit 284210
        alp_pp->boundary = new_boundary;
Packit 284210
Packit 284210
        return id;
Packit 284210
    }
Packit 284210
    else {
Packit 284210
        /* The boundary must match the boundary of
Packit 284210
         * the entry we are currently working on.
Packit 284210
         */
Packit 284210
        if (strcmp(alp_pp->current_entry->boundary, new_boundary) != 0) {
Packit 284210
            return 0;
Packit 284210
        }
Packit 284210
        else {
Packit 284210
            return id;
Packit 284210
        }
Packit 284210
    }
Packit 284210
Packit 284210
    return 0;
Packit 284210
}
Packit 284210
Packit 284210
/**
Packit 284210
 * Process data belonging to a single part.
Packit 284210
 */
Packit 284210
static void alp2_pp_process_part_data(alp2_pp_t *alp_pp) {
Packit 284210
    if (alp_pp->current_part == NULL) {
Packit 284210
        return;
Packit 284210
    }
Packit 284210
    
Packit 284210
    /* Invoke part processor. */
Packit 284210
    if (alp_pp->callback != NULL) {
Packit 284210
        if (alp_pp->callback(alp_pp, ALP2_EVENT_PART_DATA) == 0) {
Packit 284210
            alp_pp->done = 1;
Packit 284210
        }
Packit 284210
    }
Packit 284210
                
Packit 284210
    /* Keep track of part size. */
Packit 284210
    alp_pp->current_part->size += alp_pp->line_pos;
Packit 284210
Packit 284210
    /* Update the MD5 hash calculation. */
Packit 284210
    if ((alp_pp->current_entry != NULL)&&(alp_pp->line_pos > 0)) {
Packit 284210
        apr_md5_update(alp_pp->current_entry->md5_context, &alp_pp->line_buf[0], alp_pp->line_pos - 1);
Packit 284210
    }
Packit 284210
}
Packit 284210
Packit 284210
/**
Packit 284210
 * Initialise parser.
Packit 284210
 */
Packit 284210
int alp2_pp_init(alp2_pp_t *alp_pp, void *user_data,
Packit 284210
    int (*callback)(alp2_pp_t *alp_pp, int event_type), apr_pool_t *mp)
Packit 284210
{
Packit 284210
    memset(alp_pp, 0, sizeof(alp2_pp_t));
Packit 284210
Packit 284210
    alp_pp->user_data = user_data;
Packit 284210
    alp_pp->callback = callback;
Packit 284210
    alp_pp->mp = mp; /* Use the parent pool directly. */
Packit 284210
Packit 284210
    /* Set-up the line buffer. */
Packit 284210
    alp_pp->line_buf = apr_pcalloc(mp, ALP2_MAX_LINE_SIZE);
Packit 284210
    alp_pp->line_size = ALP2_MAX_LINE_SIZE;
Packit 284210
    alp_pp->line_has_start = 1;
Packit 284210
    alp_pp->line_offset = 0;
Packit 284210
    
Packit 284210
    return 1;
Packit 284210
}
Packit 284210
Packit 284210
/**
Packit 284210
 * Process data the parser has stored in the input buffer.
Packit 284210
 */
Packit 284210
static apr_status_t alp2_pp_process_internal(alp2_pp_t *alp_pp) {
Packit 284210
    /* Do not proceed if we've previously 
Packit 284210
     * encountered a fatal error.
Packit 284210
     */
Packit 284210
    if (alp_pp->errored != 0) {
Packit 284210
        return ALP2_ERROR_FATAL;
Packit 284210
    }
Packit 284210
Packit 284210
    if (alp_pp->done) {
Packit 284210
        return ALP2_DONE;
Packit 284210
    }
Packit 284210
Packit 284210
    /* Go back straight away if we don't have anything to work with. */
Packit 284210
    if (alp_pp->input_len == 0) {
Packit 284210
        return ALP2_NEED_DATA;
Packit 284210
    }
Packit 284210
Packit 284210
    while (alp_pp->input_pos < alp_pp->input_len) {
Packit 284210
        int c;
Packit 284210
Packit 284210
        if (alp_pp->done) {
Packit 284210
            return ALP2_DONE;
Packit 284210
        }
Packit 284210
Packit 284210
        if (alp_pp->line_pos >= alp_pp->line_size) {
Packit 284210
            /* Our line buffer is full with the
Packit 284210
             * line incomplete.
Packit 284210
             */
Packit 284210
            alp2_pp_process_part_data(alp_pp);
Packit 284210
Packit 284210
            /* Reset line buffer . */
Packit 284210
            alp_pp->line_pos = 0;
Packit 284210
            alp_pp->line_has_start = 0;
Packit 284210
            alp_pp->line_offset = alp_pp->current_offset;
Packit 284210
        }
Packit 284210
Packit 284210
        /* Consume one byte. */
Packit 284210
        c = alp_pp->input_buf[alp_pp->input_pos];
Packit 284210
        alp_pp->input_pos++;
Packit 284210
        alp_pp->current_offset++;
Packit 284210
        
Packit 284210
        /* Copy the byte to the line buffer. */
Packit 284210
        alp_pp->line_buf[alp_pp->line_pos] = c;
Packit 284210
        alp_pp->line_pos++;
Packit 284210
       
Packit 284210
        /* Are we at the end of a line? */       
Packit 284210
        if (c == '\n') {
Packit 284210
            if (alp_pp->line_has_start) {
Packit 284210
                /* We have one complete line. */
Packit 284210
                
Packit 284210
                int id = alp2_pp_is_boundary_line(alp_pp);
Packit 284210
Packit 284210
                if (id != 0) {
Packit 284210
                    /* The line is a boundary. */
Packit 284210
                    
Packit 284210
                    /* Finish with the previous part, if any. */
Packit 284210
                    if (alp_pp->current_part != NULL) {
Packit 284210
                        /* Update the MD5 context. */
Packit 284210
                        apr_md5_update(alp_pp->current_entry->md5_context,
Packit 284210
                            &alp_pp->line_buf[0], alp_pp->line_pos - 1);
Packit 284210
                        
Packit 284210
                        /* Event PART_END. */
Packit 284210
                        if (alp_pp->callback != NULL) {
Packit 284210
                            if (alp_pp->callback(alp_pp, ALP2_EVENT_PART_END) == 0) {
Packit 284210
                                alp_pp->done = 1;
Packit 284210
                            }
Packit 284210
                        }
Packit 284210
                        
Packit 284210
                        /* Add part to the current entry. */
Packit 284210
                        *(alp2_pp_part_t **)apr_array_push(alp_pp->current_entry->parts)
Packit 284210
                            = alp_pp->current_part;
Packit 284210
Packit 284210
                        /* Delete part. */
Packit 284210
                        alp_pp->current_part = NULL;
Packit 284210
                        
Packit 284210
                        /* If the new part is part Z, then finish
Packit 284210
                         * with the current entry. */
Packit 284210
                        if (id == 'Z') {
Packit 284210
                            alp_pp->current_entry->size = alp_pp->current_offset - alp_pp->current_entry->offset;
Packit 284210
Packit 284210
                            /* Create the MD5 digest. */
Packit 284210
                            apr_md5_final(alp_pp->current_entry->md5_digest,
Packit 284210
                                alp_pp->current_entry->md5_context);
Packit 284210
Packit 284210
                            /* Event ENTRY_END. */
Packit 284210
                            if (alp_pp->callback != NULL) {
Packit 284210
                                if (alp_pp->callback(alp_pp, ALP2_EVENT_ENTRY_END) == 0) {
Packit 284210
                                    alp_pp->done = 1;
Packit 284210
                                }
Packit 284210
                            }
Packit 284210
Packit 284210
                            /* We are about to destroy our only reference to the per-entry
Packit 284210
                             * memory pool, but that is all right since we've passed all
Packit 284210
                             * responsibility for the entry to the higher-level handler.
Packit 284210
                             */
Packit 284210
                            alp_pp->current_entry = NULL;
Packit 284210
                        }
Packit 284210
                    }
Packit 284210
                    
Packit 284210
                    if (id != 'Z') {
Packit 284210
                        /* Create new entry if necessary. */
Packit 284210
                        if (alp_pp->current_entry == NULL) {
Packit 284210
                            apr_pool_t *new_pool = NULL;
Packit 284210
Packit 284210
                            /* Create a per-entry pool directly from the main memory pool. */
Packit 284210
                            apr_pool_create(&new_pool, apr_pool_parent_get(alp_pp->mp));
Packit 284210
Packit 284210
                            alp_pp->current_entry = apr_pcalloc(new_pool, sizeof(alp2_pp_entry_t));
Packit 284210
                            alp_pp->current_entry->mp = new_pool;
Packit 284210
                            alp_pp->current_entry->offset = alp_pp->line_offset;
Packit 284210
                            alp_pp->current_entry->boundary = apr_pstrdup(new_pool, alp_pp->boundary);
Packit 284210
                            alp_pp->boundary = NULL;
Packit 284210
Packit 284210
                            alp_pp->current_entry->parts = apr_array_make(alp_pp->current_entry->mp,
Packit 284210
                                16, sizeof(alp2_pp_part_t *));
Packit 284210
Packit 284210
                            /* Initialise the MD5 context. */
Packit 284210
                            alp_pp->current_entry->md5_context = apr_pcalloc(alp_pp->current_entry->mp,
Packit 284210
                                sizeof(apr_md5_ctx_t));
Packit 284210
                            apr_md5_init(alp_pp->current_entry->md5_context);
Packit 284210
Packit 284210
                            /* Start calculating the has with the first line. */
Packit 284210
                            apr_md5_update(alp_pp->current_entry->md5_context, &alp_pp->line_buf[0], alp_pp->line_pos - 1);
Packit 284210
Packit 284210
                            /* Event ENTRY_START. */
Packit 284210
                            if (alp_pp->callback != NULL) {
Packit 284210
                                if (alp_pp->callback(alp_pp, ALP2_EVENT_ENTRY_START) == 0) {
Packit 284210
                                    alp_pp->done = 1;
Packit 284210
                                }
Packit 284210
                            }
Packit 284210
                        }
Packit 284210
                    
Packit 284210
                        /* Create new part, but only if we are not
Packit 284210
                         * dealing with an entry terminator.
Packit 284210
                         */
Packit 284210
                        alp_pp->current_part = apr_pcalloc(alp_pp->current_entry->mp, sizeof(alp2_pp_part_t));
Packit 284210
                        alp_pp->current_part->id = id;
Packit 284210
                        alp_pp->current_part->offset = alp_pp->current_offset;
Packit 284210
Packit 284210
                        /* Event PART_START. */
Packit 284210
                        if (alp_pp->callback != NULL) {
Packit 284210
                            if (alp_pp->callback(alp_pp, ALP2_EVENT_PART_START) == 0) {
Packit 284210
                                alp_pp->done = 1;
Packit 284210
                            }
Packit 284210
                        }
Packit 284210
                    }
Packit 284210
                }
Packit 284210
                else {
Packit 284210
                    /* The line does not contain a boundary,
Packit 284210
                     * so process it as part data.
Packit 284210
                     */
Packit 284210
                    alp2_pp_process_part_data(alp_pp);
Packit 284210
                }
Packit 284210
            }
Packit 284210
            else {
Packit 284210
                /* We have a chunk of data that is not a line, which
Packit 284210
                 * probably means that our buffer was not big enough, either
Packit 284210
                 * because the line (is a line and it) was too big, or because
Packit 284210
                 * we are processing binary data. Ideally the latter.
Packit 284210
                 */
Packit 284210
                alp2_pp_process_part_data(alp_pp);
Packit 284210
            }
Packit 284210
            
Packit 284210
            /* Reset the line buffer. */
Packit 284210
            alp_pp->line_pos = 0;
Packit 284210
            alp_pp->line_has_start = 1;
Packit 284210
            alp_pp->line_offset = alp_pp->current_offset;
Packit 284210
        }
Packit 284210
    }
Packit 284210
Packit 284210
    if (alp_pp->done) {
Packit 284210
        return ALP2_DONE;
Packit 284210
    }
Packit 284210
    else {
Packit 284210
        return ALP2_NEED_DATA;
Packit 284210
    }
Packit 284210
}
Packit 284210
Packit 284210
/**
Packit 284210
 * Process the provided data.
Packit 284210
 */
Packit 284210
int alp2_pp_process(alp2_pp_t *alp_pp, const char *data, size_t len) {
Packit 284210
    /* Do not proceed if we've previously 
Packit 284210
     * encountered a fatal error.
Packit 284210
     */
Packit 284210
    if (alp_pp->errored != 0) {
Packit 284210
        return ALP2_ERROR_FATAL;
Packit 284210
    }
Packit 284210
    
Packit 284210
    /* Check that we've used up the existing buffer. */
Packit 284210
    if (alp_pp->input_pos < alp_pp->input_len) {
Packit 284210
        return ALP2_ERROR_INCORRECT_STATE;
Packit 284210
    }
Packit 284210
    
Packit 284210
    alp_pp->input_buf = data;
Packit 284210
    alp_pp->input_len = len;
Packit 284210
    alp_pp->input_pos = 0;
Packit 284210
    
Packit 284210
    return alp2_pp_process_internal(alp_pp);
Packit 284210
}
Packit 284210
Packit 284210
/**
Packit 284210
 * Clean-up the parser structures.
Packit 284210
 */
Packit 284210
void alp2_pp_terminate(alp2_pp_t *alp_pp) {
Packit 284210
    /* Nothing to do, but we may need
Packit 284210
     * to do something in the future.
Packit 284210
     */
Packit 284210
}