HTP  0.3
htp_multipart.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2009-2010, Open Information Security Foundation
3  * Copyright (c) 2009-2012, Qualys, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Qualys, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  ***************************************************************************/
31 
37 #ifndef _HTP_MULTIPART_H
38 #define _HTP_MULTIPART_H
39 
40 typedef struct htp_mpartp_t htp_mpartp_t;
42 
43 #include "bstr.h"
44 #include "dslib.h"
45 #include "htp.h"
46 
47 #define MULTIPART_PART_UNKNOWN 0
48 #define MULTIPART_PART_TEXT 1
49 #define MULTIPART_PART_FILE 2
50 #define MULTIPART_PART_PREAMBLE 3
51 #define MULTIPART_PART_EPILOGUE 4
52 
53 #define MULTIPART_MODE_LINE 0
54 #define MULTIPART_MODE_DATA 1
55 
56 #define MULTIPART_STATE_DATA 1
57 #define MULTIPART_STATE_BOUNDARY 2
58 #define MULTIPART_STATE_BOUNDARY_IS_LAST1 3
59 #define MULTIPART_STATE_BOUNDARY_IS_LAST2 4
60 #define MULTIPART_STATE_BOUNDARY_EAT_LF 5
61 
62 #define MULTIPART_DEFAULT_FILE_EXTRACT_LIMIT 16
63 
64 #define HTP_MULTIPART_MIME_TYPE "multipart/form-data"
65 
66 #ifndef CR
67 #define CR '\r'
68 #endif
69 
70 #ifndef LF
71 #define LF '\n'
72 #endif
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
81 
83  int type;
84 
86  size_t len;
87 
90 
93 
96 
98 };
99 
100 struct htp_mpartp_t {
102 
104  char *boundary;
105 
107  size_t boundary_len;
108 
111 
114 
117 
120  char *extract_dir;
122 
123  // Parsing callbacks
124  int (*handle_data)(htp_mpartp_t *mpartp, unsigned char *data, size_t len, int line_end);
125  int (*handle_boundary)(htp_mpartp_t *mpartp);
126 
127  // Internal parsing fields
128  // TODO Consider prefixing them with an underscore.
129  int state;
130  size_t bpos;
131  unsigned char *current_data;
134  size_t current_len;
138  unsigned char first_boundary_byte;
139  size_t boundarypos;
140  int cr_aside;
141 };
142 
143 htp_mpartp_t *htp_mpartp_create(htp_connp_t *connp, char *boundary);
144 void htp_mpartp_destroy(htp_mpartp_t **mpartp);
145 
146 int htp_mpartp_parse(htp_mpartp_t *mpartp, unsigned char *data, size_t len);
147 int htp_mpartp_finalize(htp_mpartp_t *mpartp);
148 
150 int htp_mpart_part_receive_data(htp_mpart_part_t *part, unsigned char *data, size_t len, int line);
153 
154 int htp_mpartp_extract_boundary(bstr *content_type, char **boundary);
155 
156 int htp_mpartp_run_request_file_data_hook(htp_mpart_part_t *part, unsigned char *data, size_t len);
157 
158 #ifdef __cplusplus
159 }
160 #endif
161 
162 #endif /* _HTP_MULTIPART_H */
163 
164