Asterisk - The Open Source Telephony Project GIT-master-d856a3e
res_security_log.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2009, Digium, Inc.
5 *
6 * Russell Bryant <russell@digium.com>
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 <russell@digium.com>
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"
39#include "asterisk/strings.h"
41#include "asterisk/stasis.h"
42#include "asterisk/json.h"
43
44static const char LOG_SECURITY_NAME[] = "SECURITY";
45
46static int LOG_SECURITY;
47
49
51static const size_t SECURITY_EVENT_BUF_INIT_LEN = 256;
52
56};
57
58static 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
80static 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
90static 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
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,
111 append_json(&str, json,
113
115}
116
117static void security_stasis_cb(void *data, struct stasis_subscription *sub,
118 struct stasis_message *message)
119{
121
123 return;
124 }
125
126 if (!payload) {
127 return;
128 }
129
131}
132
133static int load_module(void)
134{
137 }
138
141 LOG_SECURITY = -1;
143 }
146
147 ast_verb(3, "Security Logging Enabled\n");
148
150}
151
152static int unload_module(void)
153{
156 }
157
159
160 ast_verb(3, "Security Logging Disabled\n");
161
162 return 0;
163}
164
const char * str
Definition: app_jack.c:147
Asterisk main include file. File version handling, generic pbx functions.
const char * ast_event_get_ie_type_name(enum ast_event_ie_type ie_type)
Get the string representation of an information element type.
Definition: event.c:209
ast_event_ie_type
Event Information Element types.
Definition: event_defs.h:68
@ AST_EVENT_IE_END
Definition: event_defs.h:70
Support for logging to various files, console and syslog Configuration in file logger....
#define ast_log_dynamic_level(level,...)
Send a log message to a dynamically registered log level.
int ast_logger_register_level(const char *name)
Register a new logger level.
Definition: logger.c:2851
#define ast_verb(level,...)
void ast_logger_unregister_level(const char *name)
Unregister a previously registered logger level.
Definition: logger.c:2909
Asterisk JSON abstraction layer.
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
Definition: json.c:332
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
struct stasis_forward * sub
Definition: res_corosync.c:240
static int LOG_SECURITY
static struct stasis_subscription * security_stasis_sub
@ REQUIRED
@ NOT_REQUIRED
static struct ast_threadstorage security_event_buf
static const char LOG_SECURITY_NAME[]
static const size_t SECURITY_EVENT_BUF_INIT_LEN
static void security_event_stasis_cb(struct ast_json *json)
static int load_module(void)
static void append_json(struct ast_str **str, struct ast_json *json, const struct ast_security_event_ie_type *ies, enum ie_required required)
static int unload_module(void)
static void append_json_single(struct ast_str **str, struct ast_json *json, const enum ast_event_ie_type ie_type, enum ie_required required)
static void security_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
#define NULL
Definition: resample.c:96
Security Event Reporting API.
const struct ast_security_event_ie_type * ast_security_event_get_required_ies(const enum ast_security_event_type event_type)
Get the list of required IEs for a given security event sub-type.
const char * ast_security_event_get_name(const enum ast_security_event_type event_type)
Get the name of a security event sub-type.
const struct ast_security_event_ie_type * ast_security_event_get_optional_ies(const enum ast_security_event_type event_type)
Get the list of optional IEs for a given security event sub-type.
struct stasis_topic * ast_security_topic(void)
A stasis_topic which publishes messages for security related issues.
ast_security_event_type
Security event types.
@ AST_SECURITY_EVENT_NUM_TYPES
This must stay at the end.
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
@ STASIS_SUBSCRIPTION_FILTER_SELECTIVE
Definition: stasis.h:297
int stasis_subscription_accept_message_type(struct stasis_subscription *subscription, const struct stasis_message_type *type)
Indicate to a subscription that we are interested in a message type.
Definition: stasis.c:1024
int stasis_subscription_set_filter(struct stasis_subscription *subscription, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a subscription.
Definition: stasis.c:1078
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct stasis_subscription * stasis_unsubscribe_and_join(struct stasis_subscription *subscription)
Cancel a subscription, blocking until the last message is processed.
Definition: stasis.c:1135
#define stasis_subscribe(topic, callback, data)
Definition: stasis.h:649
String manipulation functions.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:909
struct ast_json * json
Definition: json.h:1083
Abstract JSON element (object, array, string, int, ...).
enum ast_event_ie_type ie_type
Support for dynamic strings.
Definition: strings.h:623
Definitions to aid in the use of thread local storage.
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:86
#define ast_assert(a)
Definition: utils.h:739