1 /****************************************************************************
2 *
3 * Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
4 * Copyright (C) 2003-2013 Sourcefire, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2 as
8 * published by the Free Software Foundation. You may not use, modify or
9 * distribute this program under any other version of the GNU General
10 * Public License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 ****************************************************************************/
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "hi_util_kmap.h"
31 #include "hi_cmd_lookup.h"
32
33 /*
34 * Function: http_cmd_lookup_init(CMD_LOOKUP **CmdLookup)
35 *
36 * Purpose: Initialize the cmd_lookup structure.
37 *
38 * We need to initialize the cmd_lookup structure for
39 * the HTTP command configuration. Don't want a NULL pointer
40 * flying around, when we have to look for HTTP commands.
41 *
42 * Arguments: CmdLookup => pointer to the pointer of the cmd
43 * lookup structure.
44 *
45 * Returns: int => return code indicating error or success
46 *
47 */
http_cmd_lookup_init(CMD_LOOKUP ** CmdLookup)48 int http_cmd_lookup_init(CMD_LOOKUP **CmdLookup)
49 {
50 KMAP *km = KMapNew((KMapUserFreeFunc)HttpInspectCleanupHttpMethodsConf);
51 *CmdLookup = km;
52 if(*CmdLookup == NULL)
53 {
54 return -1;
55 }
56
57 km->nocase = 1;
58
59 return 0;
60 }
61
62 /*
63 * Function: http_cmd_lookup_cleanup(CMD_LOOKUP **CmdLookup)
64 *
65 * Purpose: Free the cmd_lookup structure.
66 * We need to free the cmd_lookup structure.
67 *
68 * Arguments: CmdLookup => pointer to the pointer of the cmd
69 * lookup structure.
70 *
71 * Returns: int => return code indicating error or success
72 *
73 */
http_cmd_lookup_cleanup(CMD_LOOKUP ** CmdLookup)74 int http_cmd_lookup_cleanup(CMD_LOOKUP **CmdLookup)
75 {
76 KMAP *km;
77
78 if (CmdLookup == NULL)
79 return -1;
80
81 km = *CmdLookup;
82
83 if (km)
84 {
85 KMapDelete(km);
86 *CmdLookup = NULL;
87 }
88
89 return 0;
90 }
91
92 /*
93 * Function: http_cmd_lookup_add(CMD_LOOKUP *CmdLookup,
94 * char *ip, int len,
95 * HTTP_CMD_CONF *HTTPCmd)
96 *
97 * Purpose: Add a cmd configuration to the list.
98 * We add these keys like you would normally think to add
99 * them, because on low endian machines the least significant
100 * byte is compared first. This is what we want to compare
101 * IPs backward, doesn't work on high endian machines, but oh
102 * well. Our platform is Intel.
103 *
104 * Arguments: CmdLookup => a pointer to the lookup structure
105 * cmd => the http cmd
106 * len => Length of the cmd
107 * HTTPCmd => a pointer to the cmd configuration structure
108 *
109 * Returns: int => return code indicating error or success
110 *
111 */
http_cmd_lookup_add(CMD_LOOKUP * CmdLookup,char * cmd,int len,HTTP_CMD_CONF * HTTPCmd)112 int http_cmd_lookup_add(CMD_LOOKUP *CmdLookup, char *cmd, int len,
113 HTTP_CMD_CONF *HTTPCmd)
114 {
115 int iRet;
116
117 if(!CmdLookup || !HTTPCmd)
118 {
119 return -1;
120 }
121
122 iRet = KMapAdd(CmdLookup, (void *)cmd, len, (void *)HTTPCmd);
123 if (iRet)
124 {
125 /*
126 * This means the key has already been added.
127 */
128 return -1;
129 }
130
131 return 0;
132 }
133
134 /*
135 * Function: http_cmd_lookup_find(CMD_LOOKUP *CmdLookup,
136 * char *ip, int len,
137 * int *iError)
138 *
139 * Purpose: Find a cmd configuration given a IP.
140 * We look up a cmd configuration given an HTTP cmd and
141 * return a pointer to that cmd configuration if found.
142 *
143 * Arguments: CmdLookup => a pointer to the lookup structure
144 * cmd => the http cmd
145 * len => Length of the cmd
146 * iError => a pointer to an error code
147 *
148 * Returns: int => return code indicating error or success
149 *
150 * Returns: HTTP_CMD_CONF* => Pointer to cmd configuration structure
151 * matching IP if found, NULL otherwise.
152 *
153 */
http_cmd_lookup_find(CMD_LOOKUP * CmdLookup,const char * cmd,int len,int * iError)154 HTTP_CMD_CONF *http_cmd_lookup_find(CMD_LOOKUP *CmdLookup,
155 const char *cmd, int len, int *iError)
156 {
157 HTTP_CMD_CONF *HTTPCmd = NULL;
158
159 if(!iError)
160 {
161 return NULL;
162 }
163
164 if(!CmdLookup)
165 {
166 *iError = -1;
167 return NULL;
168 }
169
170 *iError = 0;
171
172 HTTPCmd = (HTTP_CMD_CONF *)KMapFind(CmdLookup,(void *)cmd,len);
173 if (!HTTPCmd)
174 {
175 *iError = -1;
176 }
177
178 return HTTPCmd;
179 }
180
181 /*
182 * Function: http_cmd_lookup_first(CMD_LOOKUP *CmdLookup,
183 * int *iError)
184 *
185 * Purpose: This lookups the first cmd configuration, so we can
186 * iterate through the configurations.
187 *
188 * Arguments: CmdLookup => pointer to the cmd lookup structure
189 * iError => pointer to the integer to set for errors
190 *
191 * Returns: HTTP_CMD_CONF* => Pointer to first cmd configuration structure
192 *
193 */
http_cmd_lookup_first(CMD_LOOKUP * CmdLookup,int * iError)194 HTTP_CMD_CONF *http_cmd_lookup_first(CMD_LOOKUP *CmdLookup,
195 int *iError)
196 {
197 HTTP_CMD_CONF *HTTPCmd;
198
199 if(!iError)
200 {
201 return NULL;
202 }
203
204 if(!CmdLookup)
205 {
206 *iError = -1;
207 return NULL;
208 }
209
210 *iError = 0;
211
212 HTTPCmd = (HTTP_CMD_CONF *)KMapFindFirst(CmdLookup);
213 if (!HTTPCmd)
214 {
215 *iError = -1;
216 }
217
218 return HTTPCmd;
219 }
220
221 /*
222 * Function: http_cmd_lookup_next(CMD_LOOKUP *CmdLookup,
223 * int *iError)
224 *
225 * Iterates to the next configuration, like a list it just returns
226 * the next config in the config list.
227 *
228 * Purpose: This lookups the next cmd configuration, so we can
229 * iterate through the configurations.
230 *
231 * Arguments: CmdLookup => pointer to the cmd lookup structure
232 * iError => pointer to the integer to set for errors
233 *
234 * Returns: HTTP_CMD_CONF* => Pointer to next cmd configuration structure
235 *
236 */
http_cmd_lookup_next(CMD_LOOKUP * CmdLookup,int * iError)237 HTTP_CMD_CONF *http_cmd_lookup_next(CMD_LOOKUP *CmdLookup,
238 int *iError)
239 {
240 HTTP_CMD_CONF *HTTPCmd;
241
242 if(!iError)
243 {
244 return NULL;
245 }
246
247 if(!CmdLookup)
248 {
249 *iError = -1;
250 return NULL;
251 }
252
253 *iError = 0;
254
255 HTTPCmd = (HTTP_CMD_CONF *)KMapFindNext(CmdLookup);
256 if (!HTTPCmd)
257 {
258 *iError = -1;
259 }
260
261 return HTTPCmd;
262 }
263