Debian Codegraph @

res/res_security_log.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2009, Digium, Inc.
5 *
6 * Russell Bryant <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*!
20 * \file
21 *
22 * \author Russell Bryant <[email protected]>
23 *
24 * \brief Security Event Logging
25 *
26 * \todo Make informational security events optional
27 * \todo Escape quotes in string payload IE contents
28 */
29
30 /*** MODULEINFO
31 <support_level>core</support_level>
32 ***/
33
34 #include "asterisk.h"
35
36 #include "asterisk/module.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/threadstorage.h"
39 #include "asterisk/strings.h"
40 #include "asterisk/security_events.h"
41 #include "asterisk/stasis.h"
42 #include "asterisk/json.h"
43
44 static const char LOG_SECURITY_NAME[] = "SECURITY";
45
46 static int LOG_SECURITY;
47
48 static struct stasis_subscription *security_stasis_sub;
49
50 AST_THREADSTORAGE(security_event_buf);
51 static const size_t SECURITY_EVENT_BUF_INIT_LEN = 256;
52
53 enum ie_required {
54 NOT_REQUIRED,
55 REQUIRED
56 };
57
58 static void append_json_single(struct ast_str **str, struct ast_json *json,
59 const enum ast_event_ie_type ie_type, enum ie_required required)
60 {
61 const char *ie_type_key = ast_event_get_ie_type_name(ie_type);
62
63 struct ast_json *json_string;
64
65 json_string = ast_json_object_get(json, ie_type_key);
66
67 if (!required && !json_string) {
68 /* Optional IE isn't present. Ignore. */
69 return;
70 }
71
72 /* At this point, it _better_ be there! */
73 ast_assert(json_string != NULL);
74
75 ast_str_append(str, 0, ",%s=\"%s\"",
76 ie_type_key,
77 ast_json_string_get(json_string));
78 }
79
80 static void append_json(struct ast_str **str, struct ast_json *json,
81 const struct ast_security_event_ie_type *ies, enum ie_required required)
82 {
83 unsigned int i;
84
85 for (i = 0; ies[i].ie_type != AST_EVENT_IE_END; i++) {
86 append_json_single(str, json, ies[i].ie_type, required);
87 }
88 }
89
90 static void security_event_stasis_cb(struct ast_json *json)
91 {
92 struct ast_str *str;
93 struct ast_json *event_type_json;
94 enum ast_security_event_type event_type;
95
96 event_type_json = ast_json_object_get(json, "SecurityEvent");
97 event_type = ast_json_integer_get(event_type_json);
98
99 ast_assert((unsigned int)event_type < AST_SECURITY_EVENT_NUM_TYPES);
100
101 if (!(str = ast_str_thread_get(&security_event_buf,
102 SECURITY_EVENT_BUF_INIT_LEN))) {
103 return;
104 }
105
106 ast_str_set(&str, 0, "SecurityEvent=\"%s\"",
107 ast_security_event_get_name(event_type));
108
109 append_json(&str, json,
110 ast_security_event_get_required_ies(event_type), REQUIRED);
111 append_json(&str, json,
112 ast_security_event_get_optional_ies(event_type), NOT_REQUIRED);
113
114 ast_log_dynamic_level(LOG_SECURITY, "%s\n", ast_str_buffer(str));
115 }
116
117 static void security_stasis_cb(void *data, struct stasis_subscription *sub,
118 struct stasis_message *message)
119 {
120 struct ast_json_payload *payload = stasis_message_data(message);
121
122 if (stasis_message_type(message) != ast_security_event_type()) {
123 return;
124 }
125
126 if (!payload) {
127 return;
128 }
129
130 security_event_stasis_cb(payload->json);
131 }
132
133 static int load_module(void)
134 {
135 if ((LOG_SECURITY = ast_logger_register_level(LOG_SECURITY_NAME)) == -1) {
136 return AST_MODULE_LOAD_DECLINE;
137 }
138
139 if (!(security_stasis_sub = stasis_subscribe(ast_security_topic(), security_stasis_cb, NULL))) {
140 ast_logger_unregister_level(LOG_SECURITY_NAME);
141 LOG_SECURITY = -1;
142 return AST_MODULE_LOAD_DECLINE;
143 }
144 stasis_subscription_accept_message_type(security_stasis_sub, ast_security_event_type());
145 stasis_subscription_set_filter(security_stasis_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
146
147 ast_verb(3, "Security Logging Enabled\n");
148
149 return AST_MODULE_LOAD_SUCCESS;
150 }
151
152 static int unload_module(void)
153 {
154 if (security_stasis_sub) {
155 security_stasis_sub = stasis_unsubscribe_and_join(security_stasis_sub);
156 }
157
158 ast_logger_unregister_level(LOG_SECURITY_NAME);
159
160 ast_verb(3, "Security Logging Disabled\n");
161
162 return 0;
163 }
164
165 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Security Event Logging");