Asterisk - The Open Source Telephony Project  18.5.0
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 <[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"
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 
49 
51 static const size_t SECURITY_EVENT_BUF_INIT_LEN = 256;
52 
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 
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 
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 {
137  }
138 
139  if (!(security_stasis_sub = stasis_subscribe(ast_security_topic(), security_stasis_cb, NULL))) {
141  LOG_SECURITY = -1;
143  }
146 
147  ast_verb(3, "Security Logging Enabled\n");
148 
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 
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");
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:84
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
static void append_json(struct ast_str **str, struct ast_json *json, const struct ast_security_event_ie_type *ies, enum ie_required required)
Asterisk main include file. File version handling, generic pbx functions.
ast_security_event_type
Security event types.
Security Event Reporting API.
String manipulation functions.
struct stasis_topic * ast_security_topic(void)
A stasis_topic which publishes messages for security related issues.
const char * ast_security_event_get_name(const enum ast_security_event_type event_type)
Get the name of a security event sub-type.
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
struct ast_json * json
Definition: json.h:1025
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
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:1079
#define ast_log_dynamic_level(level,...)
Send a log message to a dynamically registered log level.
Definition: logger.h:439
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:1091
#define ast_assert(a)
Definition: utils.h:695
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96
Definitions to aid in the use of thread local storage.
#define ast_verb(level,...)
Definition: logger.h:463
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.
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:1065
void ast_logger_unregister_level(const char *name)
Unregister a previously registered logger level.
Definition: logger.c:2536
static const char LOG_SECURITY_NAME[]
ast_event_ie_type
Event Information Element types.
Definition: event_defs.h:68
Asterisk JSON abstraction layer.
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)
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
static struct stasis_subscription * security_stasis_sub
int ast_logger_register_level(const char *name)
Register a new logger level.
Definition: logger.c:2503
#define stasis_subscribe(topic, callback, data)
Definition: stasis.h:652
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static void security_event_stasis_cb(struct ast_json *json)
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
static const size_t SECURITY_EVENT_BUF_INIT_LEN
struct stasis_subscription * stasis_unsubscribe_and_join(struct stasis_subscription *subscription)
Cancel a subscription, blocking until the last message is processed.
Definition: stasis.c:1136
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static void security_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
This must stay at the end.
Support for logging to various files, console and syslog Configuration in file logger.conf.
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 ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
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:208
static int load_module(void)
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:1025
struct stasis_forward * sub
Definition: res_corosync.c:240
Abstract JSON element (object, array, string, int, ...).
static int unload_module(void)
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:861
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
Definition: json.c:322
static struct ast_threadstorage security_event_buf
static int LOG_SECURITY