Asterisk - The Open Source Telephony Project  18.5.0
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 504 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 937 of file main/security_events.c.

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 924 of file main/security_events.c.

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().

926 {
927  struct ast_json *json_ip;
928 
929  json_ip = ast_json_ipaddr(addr->addr, addr->transport);
930  if (!json_ip) {
931  return -1;
932  }
933 
934  return ast_json_object_set(json, ast_event_get_ie_type_name(ie_type), json_ip);
935 }
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:404
const struct ast_sockaddr * addr
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:661
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
Abstract JSON element (object, array, string, int, ...).

◆ 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 942 of file main/security_events.c.

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().

944 {
945  int res = 0;
946 
947  switch (ie_type->ie_type) {
951  case AST_EVENT_IE_MODULE:
962  {
963  const char *str;
964  struct ast_json *json_string;
965 
966  str = *((const char **)(((const char *) sec) + ie_type->offset));
967 
968  if (req && !str) {
969  ast_log(LOG_WARNING, "Required IE '%d' (%s) for security event "
970  "type '%u' (%s) not present\n", ie_type->ie_type,
972  sec->event_type, ast_security_event_get_name(sec->event_type));
973  res = -1;
974  break;
975  }
976 
977  if (!str) {
978  break;
979  }
980 
981  json_string = ast_json_string_create(str);
982  if (!json_string) {
983  res = -1;
984  break;
985  }
986 
987  res = ast_json_object_set(json, ast_event_get_ie_type_name(ie_type->ie_type), json_string);
988  break;
989  }
992  {
993  struct ast_json *json_string;
994  uint32_t val;
995  val = *((const uint32_t *)(((const char *) sec) + ie_type->offset));
996 
997  json_string = ast_json_stringf("%u", val);
998  if (!json_string) {
999  res = -1;
1000  break;
1001  }
1002 
1003  res = ast_json_object_set(json, ast_event_get_ie_type_name(ie_type->ie_type), json_string);
1004  break;
1005  }
1009  {
1010  const struct ast_security_event_ip_addr *addr;
1011 
1012  addr = (const struct ast_security_event_ip_addr *)(((const char *) sec) + ie_type->offset);
1013 
1014  if (req && !addr->addr) {
1015  ast_log(LOG_WARNING, "Required IE '%d' (%s) for security event "
1016  "type '%u' (%s) not present\n", ie_type->ie_type,
1018  sec->event_type, ast_security_event_get_name(sec->event_type));
1019  res = -1;
1020  }
1021 
1022  if (addr->addr) {
1023  res = add_ip_json_object(json, ie_type->ie_type, addr);
1024  }
1025 
1026  break;
1027  }
1029  {
1030  const struct timeval *tval;
1031 
1032  tval = *((const struct timeval **)(((const char *) sec) + ie_type->offset));
1033 
1034  if (req && !tval) {
1035  ast_log(LOG_WARNING, "Required IE '%d' (%s) for security event "
1036  "type '%u' (%s) not present\n", ie_type->ie_type,
1038  sec->event_type, ast_security_event_get_name(sec->event_type));
1039  res = -1;
1040  }
1041 
1042  if (tval) {
1043  struct ast_json *json_tval = ast_json_timeval(*tval, NULL);
1044  if (!json_tval) {
1045  res = -1;
1046  break;
1047  }
1048  res = ast_json_object_set(json, ast_event_get_ie_type_name(ie_type->ie_type), json_tval);
1049  }
1050 
1051  break;
1052  }
1053  case AST_EVENT_IE_EVENT_TV:
1054  case AST_EVENT_IE_SEVERITY:
1055  /* Added automatically, nothing to do here. */
1056  break;
1057  default:
1058  ast_log(LOG_WARNING, "Unhandled IE type '%d' (%s), this security event "
1059  "will be missing data.\n", ie_type->ie_type,
1061  break;
1062  }
1063 
1064  return res;
1065 }
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)
Definition: ast_expr2.c:325
#define LOG_WARNING
Definition: logger.h:274
struct ast_json * ast_json_stringf(const char *format,...)
Create a JSON string, printf style.
Definition: json.c:283
#define NULL
Definition: resample.c:96
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:404
#define ast_log
Definition: astobj2.c:42
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:268
const char * str
const struct ast_sockaddr * addr
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
Definition: json.c:649
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 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
Abstract JSON element (object, array, string, int, ...).

◆ 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 1067 of file main/security_events.c.

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().

1068 {
1069  struct timeval tv = ast_tvnow();
1070  const char *severity_str;
1071  struct ast_json *json_temp;
1072  RAII_VAR(struct ast_json *, json_object, ast_json_object_create(), ast_json_unref);
1073 
1074  if (!json_object) {
1075  return NULL;
1076  }
1077 
1078  /* NOTE: Every time ast_json_object_set is used, json_temp becomes a stale pointer since the reference is taken.
1079  * This is true even if ast_json_object_set fails.
1080  */
1081 
1082  json_temp = ast_json_integer_create(sec->event_type);
1083  if (!json_temp || ast_json_object_set(json_object, "SecurityEvent", json_temp)) {
1084  return NULL;
1085  }
1086 
1087  json_temp = ast_json_stringf("%u", sec->version);
1088  if (!json_temp || ast_json_object_set(json_object, ast_event_get_ie_type_name(AST_EVENT_IE_EVENT_VERSION), json_temp)) {
1089  return NULL;
1090  }
1091 
1092  /* AST_EVENT_IE_EVENT_TV */
1093  json_temp = ast_json_timeval(tv, NULL);
1094  if (!json_temp || ast_json_object_set(json_object, ast_event_get_ie_type_name(AST_EVENT_IE_EVENT_TV), json_temp)) {
1095  return NULL;
1096  }
1097 
1098  /* AST_EVENT_IE_SERVICE */
1099  json_temp = ast_json_string_create(sec->service);
1100  if (!json_temp || ast_json_object_set(json_object, ast_event_get_ie_type_name(AST_EVENT_IE_SERVICE), json_temp)) {
1101  return NULL;
1102  }
1103 
1104  /* AST_EVENT_IE_SEVERITY */
1105  severity_str = S_OR(
1107  "Unknown"
1108  );
1109 
1110  json_temp = ast_json_string_create(severity_str);
1111  if (!json_temp || ast_json_object_set(json_object, ast_event_get_ie_type_name(AST_EVENT_IE_SEVERITY), json_temp)) {
1112  return NULL;
1113  }
1114 
1115  return ast_json_ref(json_object);
1116 }
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
enum ast_security_event_type event_type
The security event sub-type.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
const char * service
Service that generated the event.
static const struct @418 sec_events[AST_SECURITY_EVENT_NUM_TYPES]
struct ast_json * ast_json_stringf(const char *format,...)
Create a JSON string, printf style.
Definition: json.c:283
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
const char * ast_security_event_severity_get_name(const enum ast_security_event_severity severity)
Get the name of a security event severity.
#define NULL
Definition: resample.c:96
uint32_t version
security event version
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:404
#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:911
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:268
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
Definition: json.c:649
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:389
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
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
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.
Definition: json.c:317

◆ 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 402 of file main/security_events.c.

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

Referenced by security_event_to_ami_blob().

404 {
405  unsigned int i;
406 
407  if (!ies) {
408  return 0;
409  }
410 
411  for (i = 0; ies[i].ie_type != AST_EVENT_IE_END; i++) {
412  if (append_event_str_single(str, json, ies[i].ie_type)) {
413  return -1;
414  }
415  }
416 
417  return 0;
418 }
static int append_event_str_single(struct ast_str **str, struct ast_json *json, const enum ast_event_ie_type ie_type)

◆ 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 385 of file main/security_events.c.

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

Referenced by append_event_str_from_json().

387 {
388  const char *ie_type_key = ast_event_get_ie_type_name(ie_type);
389  struct ast_json *json_string = ast_json_object_get(json, ie_type_key);
390 
391  if (!json_string) {
392  return 0;
393  }
394 
395  if (ast_str_append(str, 0, "%s: %s\r\n", ie_type_key, S_OR(ast_json_string_get(json_string), "")) == -1) {
396  return -1;
397  }
398 
399  return 0;
400 }
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
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
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
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
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
Abstract JSON element (object, array, string, int, ...).

◆ 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 895 of file main/security_events.c.

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().

896 {
897  if (check_event_type(event_type)) {
898  return NULL;
899  }
900 
901  return sec_events[event_type].name;
902 }
static const struct @418 sec_events[AST_SECURITY_EVENT_NUM_TYPES]
#define NULL
Definition: resample.c:96
static int check_event_type(const enum ast_security_event_type event_type)

◆ 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 914 of file main/security_events.c.

References check_event_type(), NULL, and sec_events.

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

916 {
917  if (check_event_type(event_type)) {
918  return NULL;
919  }
920 
921  return sec_events[event_type].optional_ies;
922 }
static const struct @418 sec_events[AST_SECURITY_EVENT_NUM_TYPES]
#define NULL
Definition: resample.c:96
static int check_event_type(const enum ast_security_event_type event_type)

◆ 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 904 of file main/security_events.c.

References check_event_type(), NULL, and sec_events.

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

906 {
907  if (check_event_type(event_type)) {
908  return NULL;
909  }
910 
911  return sec_events[event_type].required_ies;
912 }
static const struct @418 sec_events[AST_SECURITY_EVENT_NUM_TYPES]
#define NULL
Definition: resample.c:96
static int check_event_type(const enum ast_security_event_type event_type)

◆ 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 1171 of file main/security_events.c.

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(), report_session_limit(), sip_report_auth_success(), sip_report_chal_sent(), sip_report_failed_acl(), sip_report_failed_challenge_response(), sip_report_inval_password(), sip_report_inval_transport(), sip_report_invalid_peer(), and sip_report_session_limit().

1172 {
1173  if ((unsigned int)sec->event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
1174  ast_log(LOG_ERROR, "Invalid security event type\n");
1175  return -1;
1176  }
1177 
1178  if (!sec_events[sec->event_type].name) {
1179  ast_log(LOG_WARNING, "Security event type %u not handled\n",
1180  sec->event_type);
1181  return -1;
1182  }
1183 
1184  if (sec->version != sec_events[sec->event_type].version) {
1185  ast_log(LOG_WARNING, "Security event %u version mismatch\n",
1186  sec->event_type);
1187  return -1;
1188  }
1189 
1190  if (handle_security_event(sec)) {
1191  ast_log(LOG_ERROR, "Failed to issue security event of type %s.\n",
1193  }
1194 
1195  return 0;
1196 }
enum ast_security_event_type event_type
The security event sub-type.
#define LOG_WARNING
Definition: logger.h:274
static const struct @418 sec_events[AST_SECURITY_EVENT_NUM_TYPES]
uint32_t version
security event version
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
static int handle_security_event(const struct ast_security_event_common *sec)
This must stay at the end.
const char * ast_security_event_get_name(const enum ast_security_event_type event_type)
Get the name of a security event sub-type.

◆ 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 871 of file main/security_events.c.

References ARRAY_LEN, NULL, and severities.

Referenced by alloc_security_event_json_object().

873 {
874  unsigned int i;
875 
876  for (i = 0; i < ARRAY_LEN(severities); i++) {
877  if (severities[i].severity == severity) {
878  return severities[i].str;
879  }
880  }
881 
882  return NULL;
883 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define NULL
Definition: resample.c:96
enum ast_security_event_severity severity
static const struct @419 severities[]

◆ 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 483 of file main/security_events.c.

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

Referenced by asterisk_daemon().

484 {
486 
487  security_topic = stasis_topic_create("security:all");
488  if (!security_topic) {
489  return -1;
490  }
491 
493  return -1;
494  }
495 
496 
497  return 0;
498 }
ast_security_event_type
Security event types.
static struct stasis_topic * security_topic
Security Topic.
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1501
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
struct stasis_topic * stasis_topic_create(const char *name)
Create a new topic.
Definition: stasis.c:618
static void security_stasis_cleanup(void)

◆ ast_security_topic()

struct stasis_topic* ast_security_topic ( void  )

A stasis_topic which publishes messages for security related issues.

Since
12
Return values

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

References security_topic.

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

381 {
382  return security_topic;
383 }
static struct stasis_topic * security_topic
Security Topic.

◆ check_event_type()

static int check_event_type ( const enum ast_security_event_type  event_type)
static

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

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().

886 {
887  if ((unsigned int)event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
888  ast_log(LOG_ERROR, "Invalid security event type %u\n", event_type);
889  return -1;
890  }
891 
892  return 0;
893 }
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
This must stay at the end.

◆ handle_security_event()

static int handle_security_event ( const struct ast_security_event_common sec)
static

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

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().

1119 {
1120  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
1121  RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
1122  RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
1123 
1124  const struct ast_security_event_ie_type *ies;
1125  unsigned int i;
1126 
1127  if (!ast_security_event_type()) {
1128  return -1;
1129  }
1130 
1131  json_object = alloc_security_event_json_object(sec);
1132  if (!json_object) {
1133  return -1;
1134  }
1135 
1136  for (ies = ast_security_event_get_required_ies(sec->event_type), i = 0;
1137  ies[i].ie_type != AST_EVENT_IE_END;
1138  i++) {
1139  if (add_json_object(json_object, sec, ies + i, REQUIRED)) {
1140  goto return_error;
1141  }
1142  }
1143 
1144  for (ies = ast_security_event_get_optional_ies(sec->event_type), i = 0;
1145  ies[i].ie_type != AST_EVENT_IE_END;
1146  i++) {
1147  if (add_json_object(json_object, sec, ies + i, NOT_REQUIRED)) {
1148  goto return_error;
1149  }
1150  }
1151 
1152  /* The json blob is ready. Throw it in the payload and send it out over stasis. */
1153  if (!(json_payload = ast_json_payload_create(json_object))) {
1154  goto return_error;
1155  }
1156 
1157  msg = stasis_message_create(ast_security_event_type(), json_payload);
1158 
1159  if (!msg) {
1160  goto return_error;
1161  }
1162 
1164 
1165  return 0;
1166 
1167 return_error:
1168  return -1;
1169 }
enum ast_security_event_type event_type
The security event sub-type.
ast_security_event_type
Security event types.
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:735
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
#define NULL
Definition: resample.c:96
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.
#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:911
static struct ast_json * alloc_security_event_json_object(const struct ast_security_event_common *sec)
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_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&#39;s subscribers.
Definition: stasis.c:1511
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.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Abstract JSON element (object, array, string, int, ...).
struct stasis_topic * ast_security_topic(void)
A stasis_topic which publishes messages for security related issues.

◆ security_event_to_ami()

static struct ast_manager_event_blob* security_event_to_ami ( struct stasis_message message)
static

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

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

456 {
457  struct ast_json_payload *payload = stasis_message_data(message);
458 
459  if (stasis_message_type(message) != ast_security_event_type()) {
460  return NULL;
461  }
462 
463  if (!payload) {
464  return NULL;
465  }
466 
467  return security_event_to_ami_blob(payload->json);
468 }
ast_security_event_type
Security event types.
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.
#define NULL
Definition: resample.c:96
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
static struct ast_manager_event_blob * security_event_to_ami_blob(struct ast_json *json)

◆ security_event_to_ami_blob()

static struct ast_manager_event_blob* security_event_to_ami_blob ( struct ast_json json)
static

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

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().

421 {
422  RAII_VAR(struct ast_str *, str, NULL, ast_free);
423  struct ast_json *event_type_json;
424  enum ast_security_event_type event_type;
425 
426  event_type_json = ast_json_object_get(json, "SecurityEvent");
427  event_type = ast_json_integer_get(event_type_json);
428 
429  ast_assert((unsigned int)event_type < AST_SECURITY_EVENT_NUM_TYPES);
430 
432  return NULL;
433  }
434 
435  if (append_event_str_from_json(&str, json,
437  ast_log(AST_LOG_ERROR, "Failed to issue a security event to AMI: "
438  "error occurred when adding required event fields.\n");
439  return NULL;
440  }
441 
442  if (append_event_str_from_json(&str, json,
444  ast_log(AST_LOG_ERROR, "Failed to issue a security event to AMI: "
445  "error occurred when adding optional event fields.\n");
446  return NULL;
447  }
448 
450  ast_security_event_get_name(event_type),
451  "%s",
453 }
ast_security_event_type
Security event types.
static int append_event_str_from_json(struct ast_str **str, struct ast_json *json, const struct ast_security_event_ie_type *ies)
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
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:9727
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.
#define ast_log
Definition: astobj2.c:42
#define AST_LOG_ERROR
Definition: logger.h:290
#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:911
const char * str
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static const size_t SECURITY_EVENT_BUF_INIT_LEN
#define ast_free(a)
Definition: astmm.h:182
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.
#define EVENT_FLAG_SECURITY
Definition: manager.h:89
This must stay at the end.
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_security_event_get_name(const enum ast_security_event_type event_type)
Get the name of a security event sub-type.
Abstract JSON element (object, array, string, int, ...).
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
Definition: json.c:322
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ security_stasis_cleanup()

static void security_stasis_cleanup ( void  )
static

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

References ao2_cleanup, NULL, and STASIS_MESSAGE_TYPE_CLEANUP.

Referenced by ast_security_stasis_init().

476 {
479 
481 }
ast_security_event_type
Security event types.
static struct stasis_topic * security_topic
Security Topic.
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1523
#define NULL
Definition: resample.c:96
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ STASIS_MESSAGE_TYPE_DEFN()

STASIS_MESSAGE_TYPE_DEFN ( ast_security_event_type  ,
to_ami = security_event_to_ami 
)

Message type for security events.

Referenced by security_event_to_ami().

Variable Documentation

◆ name

const char* name

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

◆ optional_ies

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

◆ required_ies

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

◆ sec_events

const { ... } 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 375 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 378 of file main/security_events.c.

Referenced by ast_security_topic().

◆ severities

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

Referenced by ast_security_event_severity_get_name().

◆ severity

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

◆ str

const char* str

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

Referenced by add_json_object(), and security_event_to_ami_blob().

◆ version

uint32_t version

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