Wed Jan 8 2020 09:49:50

Asterisk developer's documentation


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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 398102 $");
37 
38 #include "asterisk/module.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/event.h"
41 #include "asterisk/threadstorage.h"
42 #include "asterisk/strings.h"
44 
45 static const char LOG_SECURITY_NAME[] = "SECURITY";
46 
47 static int LOG_SECURITY;
48 
50 
52 static const size_t SECURITY_EVENT_BUF_INIT_LEN = 256;
53 
57 };
58 
59 static int ie_is_present(const struct ast_event *event,
60  const enum ast_event_ie_type ie_type)
61 {
62  return (ast_event_get_ie_raw(event, ie_type) != NULL);
63 }
64 
65 static void append_ie(struct ast_str **str, const struct ast_event *event,
66  const enum ast_event_ie_type ie_type, enum ie_required required)
67 {
68  if (!required && !ie_is_present(event, ie_type)) {
69  /* Optional IE isn't present. Ignore. */
70  return;
71  }
72 
73  /* At this point, it _better_ be there! */
74  ast_assert(ie_is_present(event, ie_type));
75 
76  switch (ast_event_get_ie_pltype(ie_type)) {
78  ast_str_append(str, 0, ",%s=\"%u\"",
80  ast_event_get_ie_uint(event, ie_type));
81  break;
83  ast_str_append(str, 0, ",%s=\"%s\"",
85  ast_event_get_ie_str(event, ie_type));
86  break;
88  ast_str_append(str, 0, ",%s=\"%u\"",
90  ast_event_get_ie_bitflags(event, ie_type));
91  break;
95  ast_log(LOG_WARNING, "Unexpected payload type for IE '%s'\n",
97  break;
98  }
99 }
100 
101 static void append_ies(struct ast_str **str, const struct ast_event *event,
102  const struct ast_security_event_ie_type *ies, enum ie_required required)
103 {
104  unsigned int i;
105 
106  for (i = 0; ies[i].ie_type != AST_EVENT_IE_END; i++) {
107  append_ie(str, event, ies[i].ie_type, required);
108  }
109 }
110 
111 static void security_event_cb(const struct ast_event *event, void *data)
112 {
113  struct ast_str *str;
114  enum ast_security_event_type event_type;
115 
118  return;
119  }
120 
121  /* Note that the event type is guaranteed to be valid here. */
123  ast_assert(event_type >= 0 && event_type < AST_SECURITY_EVENT_NUM_TYPES);
124 
125  ast_str_set(&str, 0, "%s=\"%s\"",
127  ast_security_event_get_name(event_type));
128 
129  append_ies(&str, event,
131  append_ies(&str, event,
133 
135 }
136 
137 static int load_module(void)
138 {
141  }
142 
143  if (!(security_event_sub = ast_event_subscribe(AST_EVENT_SECURITY,
144  security_event_cb, "Security Event Logger",
145  NULL, AST_EVENT_IE_END))) {
147  LOG_SECURITY = -1;
149  }
150 
151  ast_verb(3, "Security Logging Enabled\n");
152 
154 }
155 
156 static int unload_module(void)
157 {
158  if (security_event_sub) {
159  security_event_sub = ast_event_unsubscribe(security_event_sub);
160  }
161 
163 
164  ast_verb(3, "Security Logging Disabled\n");
165 
166  return 0;
167 }
168 
169 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Security Event Logging");
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:81
An event.
Definition: event.c:85
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Security Event Reporting API.
Asterisk main include file. File version handling, generic pbx functions.
ast_security_event_type
Security event types.
static int ie_is_present(const struct ast_event *event, const enum ast_event_ie_type ie_type)
uint32_t ast_event_get_ie_bitflags(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has a bitflags payload.
Definition: event.c:1084
String manipulation functions.
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.
static struct ast_event_sub * security_event_sub
#define LOG_WARNING
Definition: logger.h:144
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
#define ast_log_dynamic_level(level,...)
Send a log message to a dynamically registered log level.
Definition: logger.h:229
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:900
#define ast_assert(a)
Definition: utils.h:738
enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type)
Get the payload type for a given information element type.
Definition: event.c:314
const char * str
Definition: app_jack.c:144
Definitions to aid in the use of thread local storage.
#define ast_verb(level,...)
Definition: logger.h:243
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:874
void ast_logger_unregister_level(const char *name)
Unregister a previously registered logger level.
Definition: logger.c:1670
static const char LOG_SECURITY_NAME[]
ast_event_ie_type
Event Information Element types.
Definition: event_defs.h:62
int ast_logger_register_level(const char *name)
Register a new logger level.
Definition: logger.c:1627
const void * ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has a raw payload.
Definition: event.c:1111
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
Event subscription.
Definition: event.c:124
enum ast_event_ie_type ie_type
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static const size_t SECURITY_EVENT_BUF_INIT_LEN
Support for logging to various files, console and syslog Configuration in file logger.conf.
uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has an integer payload.
Definition: event.c:1075
static void security_event_cb(const struct ast_event *event, void *data)
struct ast_event_sub * ast_event_subscribe(enum ast_event_type event_type, ast_event_cb_t cb, const char *description, void *userdata,...)
Subscribe to events.
Definition: event.c:909
ie_required
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:304
static void append_ie(struct ast_str **str, const struct ast_event *event, const enum ast_event_ie_type ie_type, enum ie_required required)
static int load_module(void)
static int unload_module(void)
static void append_ies(struct ast_str **str, const struct ast_event *event, const struct ast_security_event_ie_type *ies, enum ie_required required)
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:669
const char * ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
Get the value of an information element that has a string payload.
Definition: event.c:1102
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
const char * ast_security_event_get_name(const enum ast_security_event_type event_type)
Get the name of a security event sub-type.
static struct ast_threadstorage security_event_buf
struct ast_event_sub * ast_event_unsubscribe(struct ast_event_sub *event_sub)
Un-subscribe from events.
Definition: event.c:987
static int LOG_SECURITY
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180