/
mod_sanity.c
315 lines (280 loc) · 7.7 KB
/
mod_sanity.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Sanity Checks Module
*
* Copyright (C) 2006 iptelorg GbmH
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "mod_sanity.h"
#include "sanity.h"
#include "api.h"
#include "../../sr_module.h"
#include "../../ut.h"
#include "../../error.h"
#include "../../kemi.h"
MODULE_VERSION
#define PROXY_REQUIRE_DEF ""
str pr_str = STR_STATIC_INIT(PROXY_REQUIRE_DEF);
int default_msg_checks = SANITY_DEFAULT_CHECKS;
int default_uri_checks = SANITY_DEFAULT_URI_CHECKS;
int _sanity_drop = 1;
strl* proxyrequire_list = NULL;
sl_api_t slb;
static int mod_init(void);
static int sanity_fixup(void** param, int param_no);
static int w_sanity_check(struct sip_msg* _msg, char* _foo, char* _bar);
static int bind_sanity(sanity_api_t* api);
/*
* Exported functions
*/
static cmd_export_t cmds[] = {
{"sanity_check", (cmd_function)w_sanity_check, 0, 0,
REQUEST_ROUTE|ONREPLY_ROUTE},
{"sanity_check", (cmd_function)w_sanity_check, 1, sanity_fixup,
REQUEST_ROUTE|ONREPLY_ROUTE},
{"sanity_check", (cmd_function)w_sanity_check, 2, sanity_fixup,
REQUEST_ROUTE|ONREPLY_ROUTE},
{"bind_sanity", (cmd_function)bind_sanity, 0, 0, 0},
{0, 0, 0, 0}
};
/*
* Exported parameters
*/
static param_export_t params[] = {
{"default_checks", PARAM_INT, &default_msg_checks },
{"uri_checks", PARAM_INT, &default_uri_checks },
{"proxy_require", PARAM_STR, &pr_str },
{"autodrop", PARAM_INT, &_sanity_drop },
{0, 0, 0}
};
/*
* Module description
*/
struct module_exports exports = {
"sanity", /* Module name */
cmds, /* Exported functions */
0, /* RPC methods */
params, /* Exported parameters */
mod_init, /* Initialization function */
0, /* Response function */
0, /* Destroy function */
0, /* OnCancel function */
0 /* Child init function */
};
/*
* initialize module
*/
static int mod_init(void) {
strl* ptr;
DBG("sanity initializing\n");
/* bind the SL API */
if (sl_load_api(&slb)!=0) {
LM_ERR("cannot bind to SL API\n");
return -1;
}
DBG("parsing proxy requires string:\n");
ptr = parse_str_list(&pr_str);
proxyrequire_list = ptr;
while (ptr != NULL) {
DBG("string: '%.*s', next: %p\n", ptr->string.len, ptr->string.s, ptr->next);
ptr = ptr->next;
}
return 0;
}
static int sanity_fixup(void** param, int param_no) {
int checks;
str in;
if (param_no == 1) {
in.s = (char*)*param;
in.len = strlen(in.s);
if (str2int(&in, (unsigned int*)&checks) < 0) {
LOG(L_ERR, "sanity: failed to convert input integer\n");
return E_UNSPEC;
}
if ((checks < 1) || (checks >= (SANITY_MAX_CHECKS))) {
LOG(L_ERR, "sanity: input parameter (%i) outside of valid range <1-%i)\n", checks, SANITY_MAX_CHECKS);
return E_UNSPEC;
}
*param = (void*)(long)checks;
}
if (param_no == 2) {
in.s = (char*)*param;
in.len = strlen(in.s);
if (str2int(&in, (unsigned int*)&checks) < 0) {
LOG(L_ERR, "sanity: failed to convert second integer argument\n");
return E_UNSPEC;
}
if ((checks < 1) || (checks >= (SANITY_URI_MAX_CHECKS))) {
LOG(L_ERR, "sanity: second input parameter (%i) outside of valid range <1-%i\n", checks, SANITY_URI_MAX_CHECKS);
return E_UNSPEC;
}
*param = (void*)(long)checks;
}
return 0;
}
/**
* perform SIP message sanity check
* @param _msg - SIP message structure
* @param msg_checks - bitmask of sanity tests to perform over message
* @param uri_checks - bitmask of sanity tests to perform over uri
* @return -1 on error, 0 on tests failure, 1 on success
*/
int sanity_check(struct sip_msg* _msg, int msg_checks, int uri_checks)
{
int ret;
ret = SANITY_CHECK_PASSED;
if (SANITY_RURI_SIP_VERSION & msg_checks &&
(ret = check_ruri_sip_version(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_RURI_SCHEME & msg_checks &&
(ret = check_ruri_scheme(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_REQUIRED_HEADERS & msg_checks &&
(ret = check_required_headers(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_VIA_SIP_VERSION & msg_checks &&
(ret = check_via_sip_version(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_VIA_PROTOCOL & msg_checks &&
(ret = check_via_protocol(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_CSEQ_METHOD & msg_checks &&
(ret = check_cseq_method(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_CSEQ_VALUE & msg_checks &&
(ret = check_cseq_value(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_CL & msg_checks &&
(ret = check_cl(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_EXPIRES_VALUE & msg_checks &&
(ret = check_expires_value(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_PROXY_REQUIRE & msg_checks &&
(ret = check_proxy_require(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_PARSE_URIS & msg_checks &&
(ret = check_parse_uris(_msg, uri_checks)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_CHECK_DIGEST & msg_checks &&
(ret = check_digest(_msg, uri_checks)) != SANITY_CHECK_PASSED) {
goto done;
}
if (SANITY_CHECK_DUPTAGS & msg_checks &&
(ret = check_duptags(_msg)) != SANITY_CHECK_PASSED) {
goto done;
}
done:
return ret;
}
/**
* do default checks
*/
int sanity_check_defaults(struct sip_msg* msg)
{
return sanity_check(msg, default_msg_checks, default_uri_checks);
}
/**
* wrapper for sanity_check() to be used from config file
*/
static int w_sanity_check(struct sip_msg* _msg, char* _number, char* _arg) {
int ret, check, arg;
if (_number == NULL) {
check = default_msg_checks;
}
else {
check = (int)(long)_number;
}
if (_arg == NULL) {
arg = default_uri_checks;
}
else {
arg = (int)(long)_arg;
}
ret = sanity_check(_msg, check, arg);
DBG("sanity checks result: %d\n", ret);
if(_sanity_drop!=0)
return ret;
return (ret==SANITY_CHECK_FAILED)?-1:ret;
}
/**
*
*/
static int ki_sanity_check(sip_msg_t *msg, int mflags, int uflags)
{
int ret;
ret = sanity_check(msg, mflags, uflags);
return (ret==SANITY_CHECK_FAILED)?-1:ret;
}
/**
*
*/
static int ki_sanity_check_defaults(sip_msg_t *msg)
{
int ret;
ret = sanity_check(msg, default_msg_checks, default_uri_checks);
return (ret==SANITY_CHECK_FAILED)?-1:ret;
}
/**
* load sanity module API
*/
static int bind_sanity(sanity_api_t* api)
{
if (!api) {
ERR("Invalid parameter value\n");
return -1;
}
api->check = sanity_check;
api->check_defaults = sanity_check_defaults;
return 0;
}
/**
*
*/
static sr_kemi_t sr_kemi_sanity_exports[] = {
{ str_init("sanity"), str_init("sanity_check"),
SR_KEMIP_INT, ki_sanity_check,
{ SR_KEMIP_INT, SR_KEMIP_INT, SR_KEMIP_NONE,
SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
},
{ str_init("sanity"), str_init("sanity_check_defaults"),
SR_KEMIP_INT, ki_sanity_check_defaults,
{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
},
{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
};
/**
*
*/
int mod_register(char *path, int *dlflags, void *p1, void *p2)
{
sr_kemi_modules_add(sr_kemi_sanity_exports);
return 0;
}