summaryrefslogtreecommitdiff
path: root/channels/sip/security_events.c
blob: b93dffe994527bfba61b368e3bf02436a89bbe97 (plain)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012, Digium, Inc.
 *
 * Michael L. Young <elgueromexicano@gmail.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*!
 * \file
 *
 * \brief Generate security events in the SIP channel
 *
 * \author Michael L. Young <elgueromexicano@gmail.com>
 */

/*** MODULEINFO
	<support_level>extended</support_level>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "include/sip.h"
#include "include/security_events.h"

/*! \brief Determine transport type used to receive request*/

static enum ast_transport security_event_get_transport(const struct sip_pvt *p)
{
	return p->socket.type;
}

void sip_report_invalid_peer(const struct sip_pvt *p)
{
	char session_id[32];

	struct ast_security_event_inval_acct_id inval_acct_id = {
		.common.event_type = AST_SECURITY_EVENT_INVAL_ACCT_ID,
		.common.version    = AST_SECURITY_EVENT_INVAL_ACCT_ID_VERSION,
		.common.service    = "SIP",
		.common.account_id = p->exten,
		.common.local_addr = {
			.addr      = &p->ourip,
			.transport = security_event_get_transport(p)
		},
		.common.remote_addr = {
			.addr       = &p->sa,
			.transport = security_event_get_transport(p)
		},
		.common.session_id = session_id,
	};

	snprintf(session_id, sizeof(session_id), "%p", p);

	ast_security_event_report(AST_SEC_EVT(&inval_acct_id));
}

void sip_report_failed_acl(const struct sip_pvt *p, const char *aclname)
{
        char session_id[32];

        struct ast_security_event_failed_acl failed_acl_event = {
                .common.event_type  = AST_SECURITY_EVENT_FAILED_ACL,
                .common.version     = AST_SECURITY_EVENT_FAILED_ACL_VERSION,
                .common.service     = "SIP",
                .common.account_id  = p->exten,
                .common.local_addr  = {
                        .addr       = &p->ourip,
                        .transport  = security_event_get_transport(p)
                },
                .common.remote_addr = {
                        .addr       = &p->sa,
                        .transport  = security_event_get_transport(p)
                },
                .common.session_id  = session_id,
                .acl_name           = aclname,
        };

        snprintf(session_id, sizeof(session_id), "%p", p);

        ast_security_event_report(AST_SEC_EVT(&failed_acl_event));
}

void sip_report_inval_password(const struct sip_pvt *p, const char *response_challenge, const char *response_hash)
{
        char session_id[32];

        struct ast_security_event_inval_password inval_password = {
                .common.event_type  = AST_SECURITY_EVENT_INVAL_PASSWORD,
                .common.version     = AST_SECURITY_EVENT_INVAL_PASSWORD_VERSION,
                .common.service     = "SIP",
                .common.account_id  = p->exten,
                .common.local_addr  = {
                        .addr       = &p->ourip,
                        .transport  = security_event_get_transport(p)
                },
                .common.remote_addr = {
                        .addr       = &p->sa,
                        .transport  = security_event_get_transport(p)
                },
                .common.session_id  = session_id,

		.challenge	    = p->nonce,
		.received_challenge = response_challenge,
		.received_hash	    = response_hash,
        };

        snprintf(session_id, sizeof(session_id), "%p", p);

        ast_security_event_report(AST_SEC_EVT(&inval_password));
}

void sip_report_auth_success(const struct sip_pvt *p, uint32_t using_password)
{
        char session_id[32];

        struct ast_security_event_successful_auth successful_auth = {
                .common.event_type  = AST_SECURITY_EVENT_SUCCESSFUL_AUTH,
                .common.version     = AST_SECURITY_EVENT_SUCCESSFUL_AUTH_VERSION,
                .common.service     = "SIP",
                .common.account_id  = p->exten,
                .common.local_addr  = {
                        .addr       = &p->ourip,
                        .transport  = security_event_get_transport(p)
                },
                .common.remote_addr = {
                        .addr       = &p->sa,
                        .transport  = security_event_get_transport(p)
                },
                .common.session_id  = session_id,
                .using_password     = using_password,
        };

        snprintf(session_id, sizeof(session_id), "%p", p);

        ast_security_event_report(AST_SEC_EVT(&successful_auth));
}

void sip_report_session_limit(const struct sip_pvt *p)
{
        char session_id[32];

        struct ast_security_event_session_limit session_limit = {
                .common.event_type = AST_SECURITY_EVENT_SESSION_LIMIT,
                .common.version    = AST_SECURITY_EVENT_SESSION_LIMIT_VERSION,
                .common.service    = "SIP",
                .common.account_id = p->exten,
                .common.local_addr = {
                        .addr      = &p->ourip,
                        .transport = security_event_get_transport(p)
                },
                .common.remote_addr = {
                        .addr      = &p->sa,
                        .transport = security_event_get_transport(p)
                },
                .common.session_id = session_id,
        };

        snprintf(session_id, sizeof(session_id), "%p", p);

        ast_security_event_report(AST_SEC_EVT(&session_limit));
}

void sip_report_failed_challenge_response(const struct sip_pvt *p, const char *response, const char *expected_response)
{
	char session_id[32];
	char account_id[256];

	struct ast_security_event_chal_resp_failed chal_resp_failed = {
                .common.event_type = AST_SECURITY_EVENT_CHAL_RESP_FAILED,
                .common.version    = AST_SECURITY_EVENT_CHAL_RESP_FAILED_VERSION,
                .common.service    = "SIP",
                .common.account_id = account_id,
                .common.local_addr = {
                        .addr      = &p->ourip,
                        .transport = security_event_get_transport(p)
                },
                .common.remote_addr = {
                        .addr      = &p->sa,
                        .transport = security_event_get_transport(p)
                },
                .common.session_id = session_id,

                .challenge         = p->nonce,
                .response          = response,
                .expected_response = expected_response,
        };

	if (!ast_strlen_zero(p->from)) { /* When dialing, show account making call */
                ast_copy_string(account_id, p->from, sizeof(account_id));
        } else {
                ast_copy_string(account_id, p->exten, sizeof(account_id));
        }

        snprintf(session_id, sizeof(session_id), "%p", p);

        ast_security_event_report(AST_SEC_EVT(&chal_resp_failed));
}

void sip_report_chal_sent(const struct sip_pvt *p)
{
	char session_id[32];
	char account_id[256];

	struct ast_security_event_chal_sent chal_sent = {
                .common.event_type = AST_SECURITY_EVENT_CHAL_SENT,
                .common.version    = AST_SECURITY_EVENT_CHAL_SENT_VERSION,
                .common.service    = "SIP",
                .common.account_id = account_id,
                .common.local_addr = {
                        .addr      = &p->ourip,
                        .transport = security_event_get_transport(p)
                },
                .common.remote_addr = {
                        .addr      = &p->sa,
                        .transport = security_event_get_transport(p)
                },
                .common.session_id = session_id,

                .challenge         = p->nonce,
        };

	if (!ast_strlen_zero(p->from)) { /* When dialing, show account making call */
		ast_copy_string(account_id, p->from, sizeof(account_id));
	} else {
		ast_copy_string(account_id, p->exten, sizeof(account_id));
	}

        snprintf(session_id, sizeof(session_id), "%p", p);

        ast_security_event_report(AST_SEC_EVT(&chal_sent));
}

void sip_report_inval_transport(const struct sip_pvt *p, const char *transport)
{
        char session_id[32];

        struct ast_security_event_inval_transport inval_transport = {
                .common.event_type = AST_SECURITY_EVENT_INVAL_TRANSPORT,
                .common.version    = AST_SECURITY_EVENT_INVAL_TRANSPORT_VERSION,
                .common.service    = "SIP",
                .common.account_id = p->exten,
                .common.local_addr = {
                        .addr      = &p->ourip,
                        .transport = security_event_get_transport(p)
                },
                .common.remote_addr = {
                        .addr      = &p->sa,
                        .transport = security_event_get_transport(p)
                },
                .common.session_id = session_id,

                .transport         = transport,
        };

        snprintf(session_id, sizeof(session_id), "%p", p);

        ast_security_event_report(AST_SEC_EVT(&inval_transport));
}

int sip_report_security_event(const char *peer, struct ast_sockaddr *addr, const struct sip_pvt *p,
    const struct sip_request *req, const int res)
{

	struct sip_peer *peer_report;
	enum check_auth_result res_report = res;
	struct ast_str *buf;
	char *c;
	const char *authtoken;
	char *reqheader, *respheader;
	int result = 0;
	char aclname[256];
	struct digestkeys keys[] = {
		[K_RESP]  = { "response=", "" },
		[K_URI]   = { "uri=", "" },
		[K_USER]  = { "username=", "" },
		[K_NONCE] = { "nonce=", "" },
		[K_LAST]  = { NULL, NULL}
	};

	peer_report = sip_find_peer(peer, addr, TRUE, FINDPEERS, FALSE, p->socket.type);

	switch(res_report) {
	case AUTH_DONT_KNOW:
		break;
	case AUTH_SUCCESSFUL:
		if (peer_report) {
			if (ast_strlen_zero(peer_report->secret) && ast_strlen_zero(peer_report->md5secret)) {
				sip_report_auth_success(p, 0);
			} else {
				sip_report_auth_success(p, 1);
			}
		}
		break;
	case AUTH_CHALLENGE_SENT:
		sip_report_chal_sent(p);
		break;
	case AUTH_SECRET_FAILED:
	case AUTH_USERNAME_MISMATCH:
		sip_auth_headers(WWW_AUTH, &respheader, &reqheader);
		authtoken = sip_get_header(req, reqheader);
		buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN);
		ast_str_set(&buf, 0, "%s", authtoken);
		c = ast_str_buffer(buf);

		sip_digest_parser(c, keys);

		if (res_report == AUTH_SECRET_FAILED) {
			sip_report_inval_password(p, keys[K_NONCE].s, keys[K_RESP].s);
		} else {
			if (peer_report) {
				sip_report_failed_challenge_response(p, keys[K_USER].s, peer_report->username);
			}
		}
		break;
	case AUTH_NOT_FOUND:
		/* with sip_cfg.alwaysauthreject on, generates 2 events */
		sip_report_invalid_peer(p);
		break;
	case AUTH_UNKNOWN_DOMAIN:
		snprintf(aclname, sizeof(aclname), "domain_must_match");
		sip_report_failed_acl(p, aclname);
		break;
	case AUTH_PEER_NOT_DYNAMIC:
		snprintf(aclname, sizeof(aclname), "peer_not_dynamic");
		sip_report_failed_acl(p, aclname);
		break;
	case AUTH_ACL_FAILED:
		/* with sip_cfg.alwaysauthreject on, generates 2 events */
		snprintf(aclname, sizeof(aclname), "device_must_match_acl");
		sip_report_failed_acl(p, aclname);
		break;
	case AUTH_BAD_TRANSPORT:
		sip_report_inval_transport(p, sip_get_transport(req->socket.type));
		break;
	case AUTH_RTP_FAILED:
		break;
	case AUTH_SESSION_LIMIT:
		sip_report_session_limit(p);
		break;
	}

	if (peer_report) {
		sip_unref_peer(peer_report, "sip_report_security_event: sip_unref_peer: from handle_incoming");
	}

	return result;
}