HTP  0.3
htp_multipart.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  * Copyright 2009-2010 Open Information Security Foundation
00003  * Copyright 2010-2011 Qualys, Inc.
00004  *
00005  * Licensed to You under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  ***************************************************************************/
00017 
00023 #ifndef _HTP_MULTIPART_H
00024 #define _HTP_MULTIPART_H
00025 
00026 typedef struct htp_mpartp_t htp_mpartp_t;
00027 typedef struct htp_mpart_part_t htp_mpart_part_t;
00028 
00029 #include "bstr.h"
00030 #include "dslib.h"
00031 #include "htp.h"
00032 
00033 #define MULTIPART_PART_UNKNOWN                  0
00034 #define MULTIPART_PART_TEXT                     1
00035 #define MULTIPART_PART_FILE                     2
00036 #define MULTIPART_PART_PREAMBLE                 3
00037 #define MULTIPART_PART_EPILOGUE                 4
00038 
00039 #define MULTIPART_MODE_LINE                     0
00040 #define MULTIPART_MODE_DATA                     1
00041 
00042 #define MULTIPART_STATE_DATA                    1
00043 #define MULTIPART_STATE_BOUNDARY                2
00044 #define MULTIPART_STATE_BOUNDARY_IS_LAST1       3
00045 #define MULTIPART_STATE_BOUNDARY_IS_LAST2       4
00046 #define MULTIPART_STATE_BOUNDARY_EAT_LF         5
00047 
00048 #define MULTIPART_DEFAULT_FILE_EXTRACT_LIMIT    16
00049 
00050 #define HTP_MULTIPART_MIME_TYPE             "multipart/form-data"
00051 
00052 #ifndef CR
00053 #define CR '\r'
00054 #endif
00055 
00056 #ifndef LF
00057 #define LF '\n'
00058 #endif
00059 
00060 #ifdef __cplusplus
00061 extern "C" {
00062 #endif
00063 
00064 struct htp_mpart_part_t {
00066     htp_mpartp_t *mpartp;
00067 
00069     int type;   
00070 
00072     size_t len;
00073    
00075     bstr *name;   
00076 
00078     bstr *value;
00079 
00081     table_t *headers;
00082 
00083     htp_file_t *file;
00084 };
00085 
00086 struct htp_mpartp_t {
00087     htp_connp_t *connp;
00088 
00090     char *boundary;
00091 
00093     size_t boundary_len;
00094     
00096     int boundary_count;
00097 
00099     int seen_last_boundary;
00100 
00102     list_t *parts;
00103 
00104     int extract_files;
00105     int extract_limit;
00106     char *extract_dir;
00107     int file_count;
00108 
00109     // Parsing callbacks
00110     int (*handle_data)(htp_mpartp_t *mpartp, unsigned char *data, size_t len, int line_end);
00111     int (*handle_boundary)(htp_mpartp_t *mpartp);
00112 
00113     // Internal parsing fields
00114     // TODO Consider prefixing them with an underscore.
00115     int state;
00116     size_t bpos;
00117     unsigned char *current_data;
00118     htp_mpart_part_t *current_part;
00119     int current_mode;
00120     size_t current_len;
00121     bstr_builder_t *boundary_pieces;
00122     bstr_builder_t *part_pieces;
00123     int pieces_form_line;
00124     unsigned char first_boundary_byte;
00125     size_t boundarypos;
00126     int cr_aside;
00127 };
00128 
00129 htp_mpartp_t *htp_mpartp_create(htp_connp_t *connp, char *boundary);
00130 void htp_mpartp_destroy(htp_mpartp_t **mpartp);
00131 
00132 int htp_mpartp_parse(htp_mpartp_t *mpartp, unsigned char *data, size_t len);
00133 int htp_mpartp_finalize(htp_mpartp_t *mpartp);
00134 
00135 htp_mpart_part_t *htp_mpart_part_create(htp_mpartp_t *mpartp);
00136 int htp_mpart_part_receive_data(htp_mpart_part_t *part, unsigned char *data, size_t len, int line);
00137 int htp_mpart_part_finalize_data(htp_mpart_part_t *part);
00138 void htp_mpart_part_destroy(htp_mpart_part_t *part);
00139 
00140 int htp_mpartp_extract_boundary(bstr *content_type, char **boundary);
00141 
00142 int htp_mpartp_run_request_file_data_hook(htp_mpart_part_t *part, unsigned char *data, size_t len);
00143 
00144 #ifdef __cplusplus
00145 }
00146 #endif
00147 
00148 #endif  /* _HTP_MULTIPART_H */
00149 
00150