Asterisk - The Open Source Telephony Project GIT-master-66c01d8
Macros | Enumerations | Functions | Variables
main/security_events.c File Reference

Security Event Reporting Helpers. More...

#include "asterisk.h"
#include "asterisk/utils.h"
#include "asterisk/strings.h"
#include "asterisk/network.h"
#include "asterisk/event.h"
#include "asterisk/security_events.h"
#include "asterisk/netsock2.h"
#include "asterisk/stasis.h"
#include "asterisk/json.h"
#include "asterisk/astobj2.h"
Include dependency graph for main/security_events.c:

Go to the source code of this file.

Macros

#define MAX_SECURITY_IES   12
 
#define SEC_EVT_FIELD(e, field)   (offsetof(struct ast_security_event_##e, field))
 

Enumerations

enum  ie_required { NOT_REQUIRED , REQUIRED , NOT_REQUIRED , REQUIRED }
 

Functions

static int add_ip_json_object (struct ast_json *json, enum ast_event_ie_type ie_type, const struct ast_security_event_ip_addr *addr)
 
static int add_json_object (struct ast_json *json, const struct ast_security_event_common *sec, const struct ast_security_event_ie_type *ie_type, enum ie_required req)
 
static struct ast_jsonalloc_security_event_json_object (const struct ast_security_event_common *sec)
 
static int append_event_str_from_json (struct ast_str **str, struct ast_json *json, const struct ast_security_event_ie_type *ies)
 
static int append_event_str_single (struct ast_str **str, struct ast_json *json, const enum ast_event_ie_type ie_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. More...
 
const struct ast_security_event_ie_typeast_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. More...
 
const struct ast_security_event_ie_typeast_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. More...
 
int ast_security_event_report (const struct ast_security_event_common *sec)
 Report a security event. More...
 
const char * ast_security_event_severity_get_name (const enum ast_security_event_severity severity)
 Get the name of a security event severity. More...
 
int ast_security_stasis_init (void)
 initializes stasis topic/event types for ast_security_topic and ast_security_event_type More...
 
struct stasis_topicast_security_topic (void)
 A stasis_topic which publishes messages for security related issues. More...
 
static int check_event_type (const enum ast_security_event_type event_type)
 
static int handle_security_event (const struct ast_security_event_common *sec)
 
static struct ast_manager_event_blobsecurity_event_to_ami (struct stasis_message *message)
 
static struct ast_manager_event_blobsecurity_event_to_ami_blob (struct ast_json *json)
 
static void security_stasis_cleanup (void)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_security_event_type,.to_ami=security_event_to_ami,)
 Message type for security events. More...
 

Variables

struct {
   const char *   name
 
   struct ast_security_event_ie_type   optional_ies [MAX_SECURITY_IES]
 
   struct ast_security_event_ie_type   required_ies [MAX_SECURITY_IES]
 
   enum ast_security_event_severity   severity
 
   uint32_t   version
 
sec_events [AST_SECURITY_EVENT_NUM_TYPES]
 
static const size_t SECURITY_EVENT_BUF_INIT_LEN = 256
 
static struct stasis_topicsecurity_topic
 Security Topic. More...
 
struct {
   enum ast_security_event_severity   severity
 
   const char *   str
 
severities []
 

Detailed Description

Security Event Reporting Helpers.

Author
Russell Bryant russe.nosp@m.ll@d.nosp@m.igium.nosp@m..com

Definition in file main/security_events.c.

Macro Definition Documentation

◆ MAX_SECURITY_IES

#define MAX_SECURITY_IES   12

Definition at line 549 of file main/security_events.c.

◆ SEC_EVT_FIELD

#define SEC_EVT_FIELD (   e,
  field 
)    (offsetof(struct ast_security_event_##e, field))

Enumeration Type Documentation

◆ ie_required

Enumerator
NOT_REQUIRED 
REQUIRED 
NOT_REQUIRED 
REQUIRED 

Definition at line 982 of file main/security_events.c.

982 {
985};
@ NOT_REQUIRED

Function Documentation

◆ add_ip_json_object()

static int add_ip_json_object ( struct ast_json json,
enum ast_event_ie_type  ie_type,
const struct ast_security_event_ip_addr addr 
)
static

Definition at line 969 of file main/security_events.c.

971{
972 struct ast_json *json_ip;
973
974 json_ip = ast_json_ipaddr(addr->addr, addr->transport);
975 if (!json_ip) {
976 return -1;
977 }
978
979 return ast_json_object_set(json, ast_event_get_ie_type_name(ie_type), json_ip);
980}
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
struct ast_json * ast_json_ipaddr(const struct ast_sockaddr *addr, enum ast_transport transport_type)
Construct an IP address as JSON.
Definition: json.c:682
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:414
Abstract JSON element (object, array, string, int, ...).
const struct ast_sockaddr * addr

References ast_security_event_ip_addr::addr, ast_event_get_ie_type_name(), ast_json_ipaddr(), ast_json_object_set(), and ast_security_event_ip_addr::transport.

Referenced by add_json_object().

◆ add_json_object()

static int add_json_object ( struct ast_json json,
const struct ast_security_event_common sec,
const struct ast_security_event_ie_type ie_type,
enum ie_required  req 
)
static

Definition at line 987 of file main/security_events.c.

989{
990 int res = 0;
991
992 switch (ie_type->ie_type) {
1007 {
1008 const char *str;
1009 struct ast_json *json_string;
1010
1011 str = *((const char **)(((const char *) sec) + ie_type->offset));
1012
1013 if (req && !str) {
1014 ast_log(LOG_WARNING, "Required IE '%d' (%s) for security event "
1015 "type '%u' (%s) not present\n", ie_type->ie_type,
1017 sec->event_type, ast_security_event_get_name(sec->event_type));
1018 res = -1;
1019 break;
1020 }
1021
1022 if (!str) {
1023 break;
1024 }
1025
1026 json_string = ast_json_string_create(str);
1027 if (!json_string) {
1028 res = -1;
1029 break;
1030 }
1031
1032 res = ast_json_object_set(json, ast_event_get_ie_type_name(ie_type->ie_type), json_string);
1033 break;
1034 }
1037 {
1038 struct ast_json *json_string;
1039 uint32_t val;
1040 val = *((const uint32_t *)(((const char *) sec) + ie_type->offset));
1041
1042 json_string = ast_json_stringf("%u", val);
1043 if (!json_string) {
1044 res = -1;
1045 break;
1046 }
1047
1048 res = ast_json_object_set(json, ast_event_get_ie_type_name(ie_type->ie_type), json_string);
1049 break;
1050 }
1054 {
1055 const struct ast_security_event_ip_addr *addr;
1056
1057 addr = (const struct ast_security_event_ip_addr *)(((const char *) sec) + ie_type->offset);
1058
1059 if (req && !addr->addr) {
1060 ast_log(LOG_WARNING, "Required IE '%d' (%s) for security event "
1061 "type '%u' (%s) not present\n", ie_type->ie_type,
1063 sec->event_type, ast_security_event_get_name(sec->event_type));
1064 res = -1;
1065 }
1066
1067 if (addr->addr) {
1068 res = add_ip_json_object(json, ie_type->ie_type, addr);
1069 }
1070
1071 break;
1072 }
1074 {
1075 const struct timeval *tval;
1076
1077 tval = *((const struct timeval **)(((const char *) sec) + ie_type->offset));
1078
1079 if (req && !tval) {
1080 ast_log(LOG_WARNING, "Required IE '%d' (%s) for security event "
1081 "type '%u' (%s) not present\n", ie_type->ie_type,
1083 sec->event_type, ast_security_event_get_name(sec->event_type));
1084 res = -1;
1085 }
1086
1087 if (tval) {
1088 struct ast_json *json_tval = ast_json_timeval(*tval, NULL);
1089 if (!json_tval) {
1090 res = -1;
1091 break;
1092 }
1093 res = ast_json_object_set(json, ast_event_get_ie_type_name(ie_type->ie_type), json_tval);
1094 }
1095
1096 break;
1097 }
1100 /* Added automatically, nothing to do here. */
1101 break;
1102 default:
1103 ast_log(LOG_WARNING, "Unhandled IE type '%d' (%s), this security event "
1104 "will be missing data.\n", ie_type->ie_type,
1106 break;
1107 }
1108
1109 return res;
1110}
#define ast_log
Definition: astobj2.c:42
@ AST_EVENT_IE_EVENT_VERSION
Definition: event_defs.h:274
@ AST_EVENT_IE_REMOTE_ADDR
Definition: event_defs.h:282
@ AST_EVENT_IE_ATTEMPTED_TRANSPORT
Definition: event_defs.h:295
@ AST_EVENT_IE_MODULE
Definition: event_defs.h:276
@ AST_EVENT_IE_EVENT_TV
Definition: event_defs.h:283
@ AST_EVENT_IE_ACCOUNT_ID
Definition: event_defs.h:277
@ AST_EVENT_IE_SESSION_ID
Definition: event_defs.h:278
@ AST_EVENT_IE_CHALLENGE
Definition: event_defs.h:289
@ AST_EVENT_IE_LOCAL_ADDR
Definition: event_defs.h:281
@ AST_EVENT_IE_RECEIVED_HASH
Definition: event_defs.h:293
@ AST_EVENT_IE_ACL_NAME
Definition: event_defs.h:280
@ AST_EVENT_IE_AUTH_METHOD
Definition: event_defs.h:286
@ AST_EVENT_IE_SEVERITY
Definition: event_defs.h:287
@ AST_EVENT_IE_RECEIVED_CHALLENGE
Definition: event_defs.h:292
@ AST_EVENT_IE_REQUEST_TYPE
Definition: event_defs.h:284
@ AST_EVENT_IE_EXPECTED_RESPONSE
Definition: event_defs.h:291
@ AST_EVENT_IE_EXPECTED_ADDR
Definition: event_defs.h:288
@ AST_EVENT_IE_USING_PASSWORD
Definition: event_defs.h:294
@ AST_EVENT_IE_RESPONSE
Definition: event_defs.h:290
@ AST_EVENT_IE_SESSION_TV
Definition: event_defs.h:279
@ AST_EVENT_IE_REQUEST_PARAMS
Definition: event_defs.h:285
@ AST_EVENT_IE_SERVICE
Definition: event_defs.h:275
#define LOG_WARNING
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
Definition: json.c:670
struct ast_json * ast_json_stringf(const char *format,...)
Create a JSON string, printf style.
Definition: json.c:293
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 int add_ip_json_object(struct ast_json *json, enum ast_event_ie_type ie_type, const struct ast_security_event_ip_addr *addr)
const char * str
#define NULL
Definition: resample.c:96
size_t offset
For internal usage.
enum ast_event_ie_type ie_type
Definition: ast_expr2.c:325

References add_ip_json_object(), ast_security_event_ip_addr::addr, ast_event_get_ie_type_name(), AST_EVENT_IE_ACCOUNT_ID, AST_EVENT_IE_ACL_NAME, AST_EVENT_IE_ATTEMPTED_TRANSPORT, AST_EVENT_IE_AUTH_METHOD, AST_EVENT_IE_CHALLENGE, AST_EVENT_IE_EVENT_TV, AST_EVENT_IE_EVENT_VERSION, AST_EVENT_IE_EXPECTED_ADDR, AST_EVENT_IE_EXPECTED_RESPONSE, AST_EVENT_IE_LOCAL_ADDR, AST_EVENT_IE_MODULE, AST_EVENT_IE_RECEIVED_CHALLENGE, AST_EVENT_IE_RECEIVED_HASH, AST_EVENT_IE_REMOTE_ADDR, AST_EVENT_IE_REQUEST_PARAMS, AST_EVENT_IE_REQUEST_TYPE, AST_EVENT_IE_RESPONSE, AST_EVENT_IE_SERVICE, AST_EVENT_IE_SESSION_ID, AST_EVENT_IE_SESSION_TV, AST_EVENT_IE_SEVERITY, AST_EVENT_IE_USING_PASSWORD, ast_json_object_set(), ast_json_string_create(), ast_json_stringf(), ast_json_timeval(), ast_log, ast_security_event_get_name(), ast_security_event_ie_type::ie_type, LOG_WARNING, NULL, ast_security_event_ie_type::offset, and str.

Referenced by handle_security_event().

◆ alloc_security_event_json_object()

static struct ast_json * alloc_security_event_json_object ( const struct ast_security_event_common sec)
static

Definition at line 1112 of file main/security_events.c.

1113{
1114 struct timeval tv = ast_tvnow();
1115 const char *severity_str;
1116 struct ast_json *json_temp;
1117 RAII_VAR(struct ast_json *, json_object, ast_json_object_create(), ast_json_unref);
1118
1119 if (!json_object) {
1120 return NULL;
1121 }
1122
1123 /* NOTE: Every time ast_json_object_set is used, json_temp becomes a stale pointer since the reference is taken.
1124 * This is true even if ast_json_object_set fails.
1125 */
1126
1127 json_temp = ast_json_integer_create(sec->event_type);
1128 if (!json_temp || ast_json_object_set(json_object, "SecurityEvent", json_temp)) {
1129 return NULL;
1130 }
1131
1132 json_temp = ast_json_stringf("%u", sec->version);
1133 if (!json_temp || ast_json_object_set(json_object, ast_event_get_ie_type_name(AST_EVENT_IE_EVENT_VERSION), json_temp)) {
1134 return NULL;
1135 }
1136
1137 /* AST_EVENT_IE_EVENT_TV */
1138 json_temp = ast_json_timeval(tv, NULL);
1139 if (!json_temp || ast_json_object_set(json_object, ast_event_get_ie_type_name(AST_EVENT_IE_EVENT_TV), json_temp)) {
1140 return NULL;
1141 }
1142
1143 /* AST_EVENT_IE_SERVICE */
1144 json_temp = ast_json_string_create(sec->service);
1145 if (!json_temp || ast_json_object_set(json_object, ast_event_get_ie_type_name(AST_EVENT_IE_SERVICE), json_temp)) {
1146 return NULL;
1147 }
1148
1149 /* AST_EVENT_IE_SEVERITY */
1150 severity_str = S_OR(
1152 "Unknown"
1153 );
1154
1155 json_temp = ast_json_string_create(severity_str);
1156 if (!json_temp || ast_json_object_set(json_object, ast_event_get_ie_type_name(AST_EVENT_IE_SEVERITY), json_temp)) {
1157 return NULL;
1158 }
1159
1160 return ast_json_ref(json_object);
1161}
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:399
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.
Definition: json.c:327
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
const char * ast_security_event_severity_get_name(const enum ast_security_event_severity severity)
Get the name of a security event severity.
static const struct @392 sec_events[AST_SECURITY_EVENT_NUM_TYPES]
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
enum ast_security_event_type event_type
The security event sub-type.
uint32_t version
security event version
const char * service
Service that generated the event.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

References ast_event_get_ie_type_name(), AST_EVENT_IE_EVENT_TV, AST_EVENT_IE_EVENT_VERSION, AST_EVENT_IE_SERVICE, AST_EVENT_IE_SEVERITY, ast_json_integer_create(), ast_json_object_create(), ast_json_object_set(), ast_json_ref(), ast_json_string_create(), ast_json_stringf(), ast_json_timeval(), ast_json_unref(), ast_security_event_severity_get_name(), ast_tvnow(), ast_security_event_common::event_type, NULL, RAII_VAR, S_OR, sec_events, ast_security_event_common::service, and ast_security_event_common::version.

Referenced by handle_security_event().

◆ append_event_str_from_json()

static int append_event_str_from_json ( struct ast_str **  str,
struct ast_json json,
const struct ast_security_event_ie_type ies 
)
static

Definition at line 447 of file main/security_events.c.

449{
450 unsigned int i;
451
452 if (!ies) {
453 return 0;
454 }
455
456 for (i = 0; ies[i].ie_type != AST_EVENT_IE_END; i++) {
457 if (append_event_str_single(str, json, ies[i].ie_type)) {
458 return -1;
459 }
460 }
461
462 return 0;
463}
@ AST_EVENT_IE_END
Definition: event_defs.h:70
static int append_event_str_single(struct ast_str **str, struct ast_json *json, const enum ast_event_ie_type ie_type)

References append_event_str_single(), AST_EVENT_IE_END, ast_security_event_ie_type::ie_type, and str.

Referenced by security_event_to_ami_blob().

◆ append_event_str_single()

static int append_event_str_single ( struct ast_str **  str,
struct ast_json json,
const enum ast_event_ie_type  ie_type 
)
static

Definition at line 430 of file main/security_events.c.

432{
433 const char *ie_type_key = ast_event_get_ie_type_name(ie_type);
434 struct ast_json *json_string = ast_json_object_get(json, ie_type_key);
435
436 if (!json_string) {
437 return 0;
438 }
439
440 if (ast_str_append(str, 0, "%s: %s\r\n", ie_type_key, S_OR(ast_json_string_get(json_string), "")) == -1) {
441 return -1;
442 }
443
444 return 0;
445}
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
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

References ast_event_get_ie_type_name(), ast_json_object_get(), ast_json_string_get(), ast_str_append(), S_OR, and str.

Referenced by append_event_str_from_json().

◆ ast_security_event_get_name()

const char * ast_security_event_get_name ( const enum ast_security_event_type  event_type)

Get the name of a security event sub-type.

Parameters
[in]event_typesecurity event sub-type
Return values
NULLif event_type is invalid
non-NULLthe name of the security event type
Since
1.8

Definition at line 940 of file main/security_events.c.

941{
942 if (check_event_type(event_type)) {
943 return NULL;
944 }
945
946 return sec_events[event_type].name;
947}
static int check_event_type(const enum ast_security_event_type event_type)

References check_event_type(), NULL, and sec_events.

Referenced by add_json_object(), ast_security_event_report(), gen_events(), security_event_stasis_cb(), and security_event_to_ami_blob().

◆ ast_security_event_get_optional_ies()

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.

Parameters
[in]event_typesecurity event sub-type
Return values
NULLinvalid event_type
non-NULLAn array terminated with the value AST_EVENT_IE_END
Since
1.8

Definition at line 959 of file main/security_events.c.

961{
962 if (check_event_type(event_type)) {
963 return NULL;
964 }
965
966 return sec_events[event_type].optional_ies;
967}

References check_event_type(), NULL, and sec_events.

Referenced by handle_security_event(), security_event_stasis_cb(), and security_event_to_ami_blob().

◆ ast_security_event_get_required_ies()

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.

Parameters
[in]event_typesecurity event sub-type
Return values
NULLinvalid event_type
non-NULLAn array terminated with the value AST_EVENT_IE_END
Since
1.8

Definition at line 949 of file main/security_events.c.

951{
952 if (check_event_type(event_type)) {
953 return NULL;
954 }
955
956 return sec_events[event_type].required_ies;
957}

References check_event_type(), NULL, and sec_events.

Referenced by handle_security_event(), security_event_stasis_cb(), and security_event_to_ami_blob().

◆ ast_security_event_report()

int ast_security_event_report ( const struct ast_security_event_common sec)

Report a security event.

Parameters
[in]secsecurity event data. Callers of this function should never declare an instance of ast_security_event_common directly. The argument should be an instance of a specific security event descriptor which has ast_security_event_common at the very beginning.
Return values
0success
non-zerofailure

Definition at line 1216 of file main/security_events.c.

1217{
1218 if ((unsigned int)sec->event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
1219 ast_log(LOG_ERROR, "Invalid security event type\n");
1220 return -1;
1221 }
1222
1223 if (!sec_events[sec->event_type].name) {
1224 ast_log(LOG_WARNING, "Security event type %u not handled\n",
1225 sec->event_type);
1226 return -1;
1227 }
1228
1229 if (sec->version != sec_events[sec->event_type].version) {
1230 ast_log(LOG_WARNING, "Security event %u version mismatch\n",
1231 sec->event_type);
1232 return -1;
1233 }
1234
1235 if (handle_security_event(sec)) {
1236 ast_log(LOG_ERROR, "Failed to issue security event of type %s.\n",
1238 }
1239
1240 return 0;
1241}
#define LOG_ERROR
static int handle_security_event(const struct ast_security_event_common *sec)
@ AST_SECURITY_EVENT_NUM_TYPES
This must stay at the end.

References ast_log, ast_security_event_get_name(), AST_SECURITY_EVENT_NUM_TYPES, ast_security_event_common::event_type, handle_security_event(), LOG_ERROR, LOG_WARNING, sec_events, and ast_security_event_common::version.

Referenced by ast_sip_report_auth_challenge_sent(), ast_sip_report_auth_failed_challenge_response(), ast_sip_report_auth_success(), ast_sip_report_failed_acl(), ast_sip_report_invalid_endpoint(), ast_sip_report_mem_limit(), ast_sip_report_req_no_support(), evt_gen_auth_method_not_allowed(), evt_gen_chal_resp_failed(), evt_gen_chal_sent(), evt_gen_failed_acl(), evt_gen_inval_acct_id(), evt_gen_inval_password(), evt_gen_inval_transport(), evt_gen_load_avg(), evt_gen_mem_limit(), evt_gen_req_bad_format(), evt_gen_req_no_support(), evt_gen_req_not_allowed(), evt_gen_session_limit(), evt_gen_successful_auth(), evt_gen_unexpected_addr(), report_auth_success(), report_failed_acl(), report_failed_challenge_response(), report_inval_password(), report_invalid_user(), report_req_bad_format(), report_req_not_allowed(), and report_session_limit().

◆ ast_security_event_severity_get_name()

const char * ast_security_event_severity_get_name ( const enum ast_security_event_severity  severity)

Get the name of a security event severity.

Parameters
[in]severitysecurity event severity
Return values
NULLif severity is invalid
non-NULLthe name of the security event severity
Since
1.8

Definition at line 916 of file main/security_events.c.

918{
919 unsigned int i;
920
921 for (i = 0; i < ARRAY_LEN(severities); i++) {
922 if (severities[i].severity == severity) {
923 return severities[i].str;
924 }
925 }
926
927 return NULL;
928}
static const struct @393 severities[]
enum ast_security_event_severity severity
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, NULL, severities, and severity.

Referenced by alloc_security_event_json_object().

◆ ast_security_stasis_init()

int ast_security_stasis_init ( void  )

initializes stasis topic/event types for ast_security_topic and ast_security_event_type

Since
12
Return values
0on success
-1on failure

Definition at line 528 of file main/security_events.c.

529{
531
532 security_topic = stasis_topic_create("security:all");
533 if (!security_topic) {
534 return -1;
535 }
536
538 return -1;
539 }
540
541
542 return 0;
543}
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
static struct stasis_topic * security_topic
Security Topic.
static void security_stasis_cleanup(void)
ast_security_event_type
Security event types.
struct stasis_topic * stasis_topic_create(const char *name)
Create a new topic.
Definition: stasis.c:644
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1493

References ast_register_cleanup(), security_stasis_cleanup(), security_topic, STASIS_MESSAGE_TYPE_INIT, and stasis_topic_create().

Referenced by asterisk_daemon().

◆ ast_security_topic()

struct stasis_topic * ast_security_topic ( void  )

A stasis_topic which publishes messages for security related issues.

Since
12
Returns
stasis_topic for security related issues.
Return values
NULLon error

Definition at line 425 of file main/security_events.c.

426{
427 return security_topic;
428}

References security_topic.

Referenced by acl_change_stasis_subscribe(), ast_res_pjsip_initialize_configuration(), common_config_load(), handle_security_event(), load_module(), manager_subscriptions_init(), publish_acl_change(), and rtp_reload().

◆ check_event_type()

static int check_event_type ( const enum ast_security_event_type  event_type)
static

Definition at line 930 of file main/security_events.c.

931{
932 if ((unsigned int)event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
933 ast_log(LOG_ERROR, "Invalid security event type %u\n", event_type);
934 return -1;
935 }
936
937 return 0;
938}

References ast_log, AST_SECURITY_EVENT_NUM_TYPES, and LOG_ERROR.

Referenced by ast_security_event_get_name(), ast_security_event_get_optional_ies(), and ast_security_event_get_required_ies().

◆ handle_security_event()

static int handle_security_event ( const struct ast_security_event_common sec)
static

Definition at line 1163 of file main/security_events.c.

1164{
1165 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
1166 RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
1167 RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
1168
1169 const struct ast_security_event_ie_type *ies;
1170 unsigned int i;
1171
1172 if (!ast_security_event_type()) {
1173 return -1;
1174 }
1175
1176 json_object = alloc_security_event_json_object(sec);
1177 if (!json_object) {
1178 return -1;
1179 }
1180
1181 for (ies = ast_security_event_get_required_ies(sec->event_type), i = 0;
1182 ies[i].ie_type != AST_EVENT_IE_END;
1183 i++) {
1184 if (add_json_object(json_object, sec, ies + i, REQUIRED)) {
1185 goto return_error;
1186 }
1187 }
1188
1189 for (ies = ast_security_event_get_optional_ies(sec->event_type), i = 0;
1190 ies[i].ie_type != AST_EVENT_IE_END;
1191 i++) {
1192 if (add_json_object(json_object, sec, ies + i, NOT_REQUIRED)) {
1193 goto return_error;
1194 }
1195 }
1196
1197 /* The json blob is ready. Throw it in the payload and send it out over stasis. */
1198 if (!(json_payload = ast_json_payload_create(json_object))) {
1199 goto return_error;
1200 }
1201
1202 msg = stasis_message_create(ast_security_event_type(), json_payload);
1203
1204 if (!msg) {
1205 goto return_error;
1206 }
1207
1209
1210 return 0;
1211
1212return_error:
1213 return -1;
1214}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
struct ast_json_payload * ast_json_payload_create(struct ast_json *json)
Create an ao2 object to pass json blobs as data payloads for stasis.
Definition: json.c:756
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.
static struct ast_json * alloc_security_event_json_object(const struct ast_security_event_common *sec)
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.
static int add_json_object(struct ast_json *json, const struct ast_security_event_common *sec, const struct ast_security_event_ie_type *ie_type, enum ie_required req)
struct stasis_topic * ast_security_topic(void)
A stasis_topic which publishes messages for security related issues.
struct stasis_message * stasis_message_create(struct stasis_message_type *type, void *data)
Create a new message.
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic's subscribers.
Definition: stasis.c:1538

References add_json_object(), alloc_security_event_json_object(), ao2_cleanup, AST_EVENT_IE_END, ast_json_payload_create(), ast_json_unref(), ast_security_event_get_optional_ies(), ast_security_event_get_required_ies(), ast_security_topic(), ast_security_event_common::event_type, ast_security_event_ie_type::ie_type, NOT_REQUIRED, NULL, RAII_VAR, REQUIRED, stasis_message_create(), and stasis_publish().

Referenced by ast_security_event_report().

◆ security_event_to_ami()

static struct ast_manager_event_blob * security_event_to_ami ( struct stasis_message message)
static

Definition at line 500 of file main/security_events.c.

501{
503
505 return NULL;
506 }
507
508 if (!payload) {
509 return NULL;
510 }
511
512 return security_event_to_ami_blob(payload->json);
513}
static struct ast_manager_event_blob * security_event_to_ami_blob(struct ast_json *json)
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct ast_json * json
Definition: json.h:1083

References ast_json_payload::json, NULL, security_event_to_ami_blob(), stasis_message_data(), and stasis_message_type().

◆ security_event_to_ami_blob()

static struct ast_manager_event_blob * security_event_to_ami_blob ( struct ast_json json)
static

Definition at line 465 of file main/security_events.c.

466{
467 RAII_VAR(struct ast_str *, str, NULL, ast_free);
468 struct ast_json *event_type_json;
469 enum ast_security_event_type event_type;
470
471 event_type_json = ast_json_object_get(json, "SecurityEvent");
472 event_type = ast_json_integer_get(event_type_json);
473
474 ast_assert((unsigned int)event_type < AST_SECURITY_EVENT_NUM_TYPES);
475
477 return NULL;
478 }
479
482 ast_log(AST_LOG_ERROR, "Failed to issue a security event to AMI: "
483 "error occurred when adding required event fields.\n");
484 return NULL;
485 }
486
489 ast_log(AST_LOG_ERROR, "Failed to issue a security event to AMI: "
490 "error occurred when adding optional event fields.\n");
491 return NULL;
492 }
493
495 ast_security_event_get_name(event_type),
496 "%s",
498}
#define ast_free(a)
Definition: astmm.h:180
#define AST_LOG_ERROR
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
Definition: json.c:332
static int append_event_str_from_json(struct ast_str **str, struct ast_json *json, const struct ast_security_event_ie_type *ies)
static const size_t SECURITY_EVENT_BUF_INIT_LEN
#define EVENT_FLAG_SECURITY
Definition: manager.h:93
struct ast_manager_event_blob * ast_manager_event_blob_create(int event_flags, const char *manager_event, const char *extra_fields_fmt,...)
Construct a ast_manager_event_blob.
Definition: manager.c:10230
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
Support for dynamic strings.
Definition: strings.h:623
#define ast_assert(a)
Definition: utils.h:739

References append_event_str_from_json(), ast_assert, ast_free, ast_json_integer_get(), ast_json_object_get(), ast_log, AST_LOG_ERROR, ast_manager_event_blob_create(), ast_security_event_get_name(), ast_security_event_get_optional_ies(), ast_security_event_get_required_ies(), AST_SECURITY_EVENT_NUM_TYPES, ast_str_buffer(), ast_str_create, EVENT_FLAG_SECURITY, NULL, RAII_VAR, SECURITY_EVENT_BUF_INIT_LEN, and str.

Referenced by security_event_to_ami().

◆ security_stasis_cleanup()

static void security_stasis_cleanup ( void  )
static

Definition at line 520 of file main/security_events.c.

521{
524
526}
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1515

References ao2_cleanup, NULL, security_topic, and STASIS_MESSAGE_TYPE_CLEANUP.

Referenced by ast_security_stasis_init().

◆ STASIS_MESSAGE_TYPE_DEFN()

STASIS_MESSAGE_TYPE_DEFN ( ast_security_event_type  ,
to_ami = security_event_to_ami 
)

Message type for security events.

Variable Documentation

◆ name

const char* name

Definition at line 546 of file main/security_events.c.

◆ optional_ies

Definition at line 551 of file main/security_events.c.

◆ required_ies

Definition at line 550 of file main/security_events.c.

◆ 

const struct { ... } sec_events[AST_SECURITY_EVENT_NUM_TYPES]

◆ SECURITY_EVENT_BUF_INIT_LEN

const size_t SECURITY_EVENT_BUF_INIT_LEN = 256
static

Definition at line 420 of file main/security_events.c.

Referenced by security_event_to_ami_blob().

◆ security_topic

struct stasis_topic* security_topic
static

Security Topic.

Definition at line 423 of file main/security_events.c.

Referenced by ast_security_stasis_init(), ast_security_topic(), and security_stasis_cleanup().

◆ 

const struct { ... } severities[]
Initial value:
= {
{ AST_SECURITY_EVENT_SEVERITY_INFO, "Informational" },
}
@ AST_SECURITY_EVENT_SEVERITY_ERROR
Something has gone wrong.
@ AST_SECURITY_EVENT_SEVERITY_INFO
Informational event, not something that has gone wrong.

Referenced by ast_security_event_severity_get_name().

◆ severity

◆ str

const char* str

◆ version

uint32_t version

Definition at line 547 of file main/security_events.c.