1 /*
2 * ModSecurity for Apache 2.x, http://www.modsecurity.org/
3 * Copyright (c) 2004-2013 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4 *
5 * You may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * If any of the files related to licensing are missing or if you have any
11 * other questions related to licensing please contact Trustwave Holdings, Inc.
12 * directly using the email address security@modsecurity.org.
13 */
14 
15 #ifndef _UTIL_H_
16 #define _UTIL_H_
17 
18 #include <sys/types.h>
19 #include <apr_file_info.h>
20 
21 #ifndef APR_WSTICKY
22 /* Add extra flags added to APR in 0.9.5 */
23 #define APR_USETID      0x8000 /**< Set user id */
24 #define APR_GSETID      0x4000 /**< Set group id */
25 #define APR_WSTICKY     0x2000 /**< Sticky bit */
26 #endif
27 
28 #include "modsecurity.h"
29 #include "re.h"
30 #include "msc_tree.h"
31 
32 #ifdef WIN32
33 #include <ws2tcpip.h>
34 // This is a trick: for ModSecurity modules this will declare inet_pton,
35 // but for mymodule.cpp (IIS module) this will skip, because we include
36 // windows.h before including msc_util.h
37 // Without the trick we have redefinition conflict.
38 //
39 #if !(NTDDI_VERSION >= NTDDI_VISTA)
40 int DSOLOCAL inet_pton(int family, const char *src, void *dst);
41 #endif
42 #endif
43 
44 #define UNICODE_ERROR_CHARACTERS_MISSING    -1
45 #define UNICODE_ERROR_INVALID_ENCODING      -2
46 #define UNICODE_ERROR_OVERLONG_CHARACTER    -3
47 #define UNICODE_ERROR_RESTRICTED_CHARACTER  -4
48 #define UNICODE_ERROR_DECODING_ERROR        -5
49 
50 #ifdef LINUX_S390
51 int DSOLOCAL swap_int32(int x);
52 #endif
53 
54 
55 char DSOLOCAL *utf8_unicode_inplace_ex(apr_pool_t *mp, unsigned char *input, long int input_len, int *changed);
56 
57 char DSOLOCAL *m_strcasestr(const char *haystack, const char *needle);
58 
59 int DSOLOCAL normalize_path_inplace(unsigned char *input, int len, int win, int *changed);
60 
61 int DSOLOCAL parse_boolean(const char *input);
62 
63 char DSOLOCAL *remove_quotes(apr_pool_t *mptmp, const char *input, int input_len);
64 
65 char DSOLOCAL *parse_pm_content(const char *op_parm, unsigned short int op_len, msre_rule *rule, char **error_msg);
66 
67 char DSOLOCAL *remove_escape(apr_pool_t *mptmp, const char *input, int input_len);
68 
69 int DSOLOCAL parse_name_eq_value(apr_pool_t *mp, const char *input, char **name, char **value);
70 
71 char DSOLOCAL *url_encode(apr_pool_t *mp, char *input, unsigned int input_len, int *changed);
72 
73 char DSOLOCAL *strnurlencat(char *destination, char *source, unsigned int maxlen);
74 
75 char DSOLOCAL *file_dirname(apr_pool_t *p, const char *filename);
76 
77 char DSOLOCAL *file_basename(apr_pool_t *p, const char *filename);
78 
79 int DSOLOCAL sql_hex2bytes_inplace(unsigned char *data, int len);
80 
81 int DSOLOCAL hex2bytes_inplace(unsigned char *data, int len);
82 
83 char DSOLOCAL *bytes2hex(apr_pool_t *pool, unsigned char *data, int len);
84 
85 int DSOLOCAL is_token_char(unsigned char c);
86 
87 int DSOLOCAL remove_lf_crlf_inplace(char *text);
88 
89 char DSOLOCAL *guess_tmp_dir(apr_pool_t *p);
90 
91 char DSOLOCAL *current_logtime(apr_pool_t *mp);
92 
93 char DSOLOCAL *current_filetime(apr_pool_t *mp);
94 
95 int DSOLOCAL msc_mkstemp_ex(char *templat, int mode);
96 
97 int DSOLOCAL msc_mkstemp(char *templat);
98 
99 char DSOLOCAL *strtolower_inplace(unsigned char *str);
100 
101 char DSOLOCAL *log_escape_re(apr_pool_t *p, const char *text);
102 
103 char DSOLOCAL *log_escape(apr_pool_t *p, const char *text);
104 
105 char DSOLOCAL *log_escape_nq(apr_pool_t *p, const char *text);
106 
107 char DSOLOCAL *log_escape_ex(apr_pool_t *p, const char *text, unsigned long int text_length);
108 
109 char DSOLOCAL *log_escape_nq_ex(apr_pool_t *p, const char *text, unsigned long int text_length);
110 
111 char DSOLOCAL *log_escape_hex(apr_pool_t *mp, const unsigned char *text, unsigned long int text_length);
112 
113 char DSOLOCAL *log_escape_raw(apr_pool_t *mp, const unsigned char *text, unsigned long int text_length);
114 
115 char DSOLOCAL *log_escape_nul(apr_pool_t *mp, const unsigned char *text, unsigned long int text_length);
116 
117 int DSOLOCAL decode_base64_ext(char *plain_text, const unsigned char *input, int input_len);
118 
119 int DSOLOCAL convert_to_int(const char c);
120 
121 int DSOLOCAL set_match_to_tx(modsec_rec *msr, int capture, const char *match, int tx_n);
122 
123 int DSOLOCAL js_decode_nonstrict_inplace(unsigned char *input, long int input_len);
124 
125 int DSOLOCAL urldecode_uni_nonstrict_inplace_ex(unsigned char *input, long int input_length, int * changed);
126 
127 int DSOLOCAL urldecode_nonstrict_inplace_ex(unsigned char *input, long int input_length, int *invalid_count, int *changed);
128 
129 int DSOLOCAL html_entities_decode_inplace(apr_pool_t *mp, unsigned char *input, int len);
130 
131 int DSOLOCAL ansi_c_sequences_decode_inplace(unsigned char *input, int len);
132 
133 char DSOLOCAL *modsec_build(apr_pool_t *mp);
134 
135 int DSOLOCAL is_empty_string(const char *string);
136 
137 char DSOLOCAL *resolve_relative_path(apr_pool_t *pool, const char *parent_filename, const char *filename);
138 
139 int DSOLOCAL css_decode_inplace(unsigned char *input, long int input_len);
140 
141 apr_fileperms_t DSOLOCAL mode2fileperms(int mode);
142 
143 char DSOLOCAL *construct_single_var(modsec_rec *msr, char *name);
144 
145 char DSOLOCAL *format_all_performance_variables(modsec_rec *msr, apr_pool_t *mp);
146 
147 unsigned char DSOLOCAL is_netmask_v4(char *ip_strv4);
148 
149 unsigned char DSOLOCAL is_netmask_v6(char *ip_strv6);
150 
151 int DSOLOCAL msc_headers_to_buffer(const apr_array_header_t *arr, char *buffer, int max_length);
152 
153 int DSOLOCAL ip_tree_from_file(TreeRoot **rtree, char *uri,
154     apr_pool_t *mp, char **error_msg);
155 
156 int DSOLOCAL tree_contains_ip(apr_pool_t *mp, TreeRoot *rtree,
157     const char *value, modsec_rec *msr, char **error_msg);
158 
159 int DSOLOCAL ip_tree_from_param(apr_pool_t *pool,
160     char *param, TreeRoot **rtree, char **error_msg);
161 
162 #ifdef WITH_CURL
163 int ip_tree_from_uri(TreeRoot **rtree, char *uri,
164     apr_pool_t *mp, char **error_msg);
165 #endif
166 
167 int read_line(char *buff, int size, FILE *fp);
168 
169 size_t msc_curl_write_memory_cb(apr_pool_t *mp, void *contents, size_t size,
170     size_t nmemb, void *userp, char **error_msg);
171 
172 struct msc_curl_memory_buffer_t
173 {
174    char *memory;
175    size_t size;
176 };
177 
178 #ifdef WIN32
179 char *strtok_r(char *str, const char *delim, char **nextp);
180 #endif
181 
182 #endif
183