Asterisk - The Open Source Telephony Project GIT-master-3dae2cf
Macros | Functions | Variables
tn_config.c File Reference
#include "asterisk.h"
#include <sys/stat.h>
#include "asterisk/cli.h"
#include "asterisk/module.h"
#include "asterisk/sorcery.h"
#include "stir_shaken.h"
Include dependency graph for tn_config.c:

Go to the source code of this file.

Macros

#define _TRACE_PREFIX_   "tc",__LINE__, ""
 
#define CONFIG_TYPE   "tn"
 
#define DEFAULT_attest_level   attest_level_NOT_SET
 
#define DEFAULT_check_tn_cert_public_url   check_tn_cert_public_url_NOT_SET
 
#define DEFAULT_private_key_file   NULL
 
#define DEFAULT_public_cert_url   NULL
 
#define DEFAULT_send_mky   send_mky_NOT_SET
 

Functions

static char * cli_tn_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * cli_tn_show_all (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static void * etn_alloc (const char *name)
 
 generate_acfg_common_sorcery_handlers (tn_cfg)
 
static struct ao2_containerget_tn_all (void)
 
static int init_tn (struct tn_cfg *cfg)
 
static void * tn_alloc (const char *name)
 
static int tn_apply (const struct ast_sorcery *sorcery, void *obj)
 
int tn_config_load (void)
 
int tn_config_reload (void)
 
int tn_config_unload (void)
 
static void tn_destructor (void *obj)
 
struct tn_cfgtn_get_cfg (const char *id)
 
struct tn_cfgtn_get_etn (const char *id, struct profile_cfg *eprofile)
 

Variables

static struct ast_cli_entry stir_shaken_certificate_cli []
 

Macro Definition Documentation

◆ _TRACE_PREFIX_

#define _TRACE_PREFIX_   "tc",__LINE__, ""

Definition at line 19 of file tn_config.c.

◆ CONFIG_TYPE

#define CONFIG_TYPE   "tn"

Definition at line 31 of file tn_config.c.

◆ DEFAULT_attest_level

#define DEFAULT_attest_level   attest_level_NOT_SET

Definition at line 36 of file tn_config.c.

◆ DEFAULT_check_tn_cert_public_url

#define DEFAULT_check_tn_cert_public_url   check_tn_cert_public_url_NOT_SET

Definition at line 33 of file tn_config.c.

◆ DEFAULT_private_key_file

#define DEFAULT_private_key_file   NULL

Definition at line 34 of file tn_config.c.

◆ DEFAULT_public_cert_url

#define DEFAULT_public_cert_url   NULL

Definition at line 35 of file tn_config.c.

◆ DEFAULT_send_mky

#define DEFAULT_send_mky   send_mky_NOT_SET

Definition at line 37 of file tn_config.c.

Function Documentation

◆ cli_tn_show()

static char * cli_tn_show ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 213 of file tn_config.c.

214{
215 struct tn_cfg *cfg;
216 struct config_object_cli_data data = {
217 .title = "TN",
218 .object_type = config_object_type_tn,
219 };
220
221 switch(cmd) {
222 case CLI_INIT:
223 e->command = "stir_shaken show tn";
224 e->usage =
225 "Usage: stir_shaken show tn <id>\n"
226 " Show the settings for a given TN\n";
227 return NULL;
228 case CLI_GENERATE:
229 if (a->pos == 3) {
231 } else {
232 return NULL;
233 }
234 }
235
236 if (a->argc != 4) {
237 return CLI_SHOWUSAGE;
238 }
239
240 cfg = tn_get_cfg(a->argv[3]);
241 config_object_cli_show(cfg, a, &data, 0);
242 ao2_cleanup(cfg);
243
244 return CLI_SUCCESS;
245}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
int config_object_cli_show(void *obj, void *arg, void *data, int flags)
Output configuration settings to the Asterisk CLI.
char * config_object_tab_complete_name(const char *word, struct ao2_container *container)
Tab completion for name matching with STIR/SHAKEN CLI commands.
@ config_object_type_tn
#define NULL
Definition: resample.c:96
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
TN configuration for stir/shaken.
static struct test_val a
static struct ao2_container * get_tn_all(void)
Definition: tn_config.c:44
struct tn_cfg * tn_get_cfg(const char *id)
Definition: tn_config.c:39

References a, ao2_cleanup, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, config_object_cli_show(), config_object_tab_complete_name(), config_object_type_tn, get_tn_all(), NULL, config_object_cli_data::title, tn_get_cfg(), and ast_cli_entry::usage.

◆ cli_tn_show_all()

static char * cli_tn_show_all ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 177 of file tn_config.c.

178{
179 struct ao2_container *container;
180 struct config_object_cli_data data = {
181 .title = "TN",
182 .object_type = config_object_type_tn,
183 };
184
185 switch(cmd) {
186 case CLI_INIT:
187 e->command = "stir_shaken show tns";
188 e->usage =
189 "Usage: stir_shaken show tns\n"
190 " Show all attestation TNs\n";
191 return NULL;
192 case CLI_GENERATE:
193 return NULL;
194 }
195
196 if (a->argc != 3) {
197 return CLI_SHOWUSAGE;
198 }
199
202 ast_cli(a->fd, "No stir/shaken TNs found\n");
204 return CLI_SUCCESS;
205 }
206
208 ao2_ref(container, -1);
209
210 return CLI_SUCCESS;
211}
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ao2_callback_data(container, flags, cb_fn, arg, data)
Definition: astobj2.h:1723
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
@ OBJ_NODATA
Definition: astobj2.h:1044
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
struct ao2_container * container
Definition: res_fax.c:501
Generic container type.

References a, ao2_callback_data, ao2_cleanup, ao2_container_count(), ao2_ref, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, config_object_cli_show(), config_object_type_tn, container, get_tn_all(), NULL, OBJ_NODATA, config_object_cli_data::title, and ast_cli_entry::usage.

◆ etn_alloc()

static void * etn_alloc ( const char *  name)
static

Definition at line 95 of file tn_config.c.

96{
97 struct tn_cfg *cfg;
98
100 if (!cfg) {
101 return NULL;
102 }
103
104 if (init_tn(cfg) != 0) {
105 ao2_cleanup(cfg);
106 cfg = NULL;
107 }
108 return cfg;
109}
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition: astobj2.h:367
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:404
static void tn_destructor(void *obj)
Definition: tn_config.c:52
static int init_tn(struct tn_cfg *cfg)
Definition: tn_config.c:60

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_alloc_options, ao2_cleanup, init_tn(), NULL, and tn_destructor().

Referenced by tn_get_etn().

◆ generate_acfg_common_sorcery_handlers()

generate_acfg_common_sorcery_handlers ( tn_cfg  )

◆ get_tn_all()

static struct ao2_container * get_tn_all ( void  )
static

Definition at line 44 of file tn_config.c.

45{
48}
struct ast_sorcery * get_sorcery(void)
Retrieve the stir/shaken sorcery context.
Definition: common_config.c:34
@ AST_RETRIEVE_FLAG_MULTIPLE
Return all matching objects.
Definition: sorcery.h:120
@ AST_RETRIEVE_FLAG_ALL
Perform no matching, return all objects.
Definition: sorcery.h:123
void * ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
Retrieve an object or multiple objects using specific fields.
Definition: sorcery.c:1897
#define CONFIG_TYPE
Definition: tn_config.c:31

References AST_RETRIEVE_FLAG_ALL, AST_RETRIEVE_FLAG_MULTIPLE, ast_sorcery_retrieve_by_fields(), CONFIG_TYPE, get_sorcery(), and NULL.

Referenced by cli_tn_show(), and cli_tn_show_all().

◆ init_tn()

static int init_tn ( struct tn_cfg cfg)
static

Definition at line 60 of file tn_config.c.

61{
62 if (ast_string_field_init(cfg, 1024)) {
63 return -1;
64 }
65
66 /*
67 * The memory for the commons actually comes from cfg
68 * due to the weirdness of the STRFLDSET macro used with
69 * sorcery. We just use a token amount of memory in
70 * this call so the initialize doesn't fail.
71 */
72 if (ast_string_field_init(&cfg->acfg_common, 8)) {
73 return -1;
74 }
75
76 return 0;
77}
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
struct attestation_cfg_common acfg_common

References tn_cfg::acfg_common, and ast_string_field_init.

Referenced by etn_alloc(), and tn_alloc().

◆ tn_alloc()

static void * tn_alloc ( const char *  name)
static

Definition at line 79 of file tn_config.c.

80{
81 struct tn_cfg *cfg;
82
83 cfg = ast_sorcery_generic_alloc(sizeof(*cfg), tn_destructor);
84 if (!cfg) {
85 return NULL;
86 }
87
88 if (init_tn(cfg) != 0) {
89 ao2_cleanup(cfg);
90 cfg = NULL;
91 }
92 return cfg;
93}
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728

References ao2_cleanup, ast_sorcery_generic_alloc(), init_tn(), NULL, and tn_destructor().

Referenced by tn_config_load().

◆ tn_apply()

static int tn_apply ( const struct ast_sorcery sorcery,
void *  obj 
)
static

Definition at line 164 of file tn_config.c.

165{
166 struct tn_cfg *cfg = obj;
167 const char *id = ast_sorcery_object_get_id(cfg);
168 int rc = 0;
169
170 if (as_check_common_config(id, &cfg->acfg_common) != 0) {
171 return -1;
172 }
173
174 return rc;
175}
int as_check_common_config(const char *id, struct attestation_cfg_common *acfg_common)
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317

References tn_cfg::acfg_common, as_check_common_config(), and ast_sorcery_object_get_id().

Referenced by tn_config_load().

◆ tn_config_load()

int tn_config_load ( void  )

Definition at line 268 of file tn_config.c.

269{
270 struct ast_sorcery *sorcery = get_sorcery();
271
272 ast_sorcery_apply_default(sorcery, CONFIG_TYPE, "config", "stir_shaken.conf,criteria=type=tn");
273
275 NULL, tn_apply)) {
276 ast_log(LOG_ERROR, "stir/shaken - failed to register '%s' sorcery object\n", CONFIG_TYPE);
278 }
279
281 OPT_NOOP_T, 0, 0);
282
284
286
289
291}
#define ast_log
Definition: astobj2.c:42
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
#define register_common_attestation_fields(sorcery, object, CONFIG_TYPE, nodoc)
@ OPT_NOOP_T
Type for a default handler that should do nothing.
#define LOG_ERROR
@ 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
static struct ast_sorcery * sorcery
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition: sorcery.h:837
void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
Inform any wizards of a specific object type to load persistent objects.
Definition: sorcery.c:1393
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object.
Definition: sorcery.h:955
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:476
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_cli_entry stir_shaken_certificate_cli[]
Definition: tn_config.c:248
static void * tn_alloc(const char *name)
Definition: tn_config.c:79
static int tn_apply(const struct ast_sorcery *sorcery, void *obj)
Definition: tn_config.c:164
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_cli_register_multiple, ast_log, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_sorcery_apply_default, ast_sorcery_load_object(), ast_sorcery_object_field_register, ast_sorcery_object_register, CONFIG_TYPE, get_sorcery(), LOG_ERROR, NULL, OPT_NOOP_T, register_common_attestation_fields, sorcery, stir_shaken_certificate_cli, tn_alloc(), and tn_apply().

Referenced by common_config_load().

◆ tn_config_reload()

int tn_config_reload ( void  )

Definition at line 253 of file tn_config.c.

254{
255 struct ast_sorcery *sorcery = get_sorcery();
258}
void ast_sorcery_force_reload_object(const struct ast_sorcery *sorcery, const char *type)
Inform any wizards of a specific object type to reload persistent objects even if no changes determin...
Definition: sorcery.c:1457

References AST_MODULE_LOAD_SUCCESS, ast_sorcery_force_reload_object(), CONFIG_TYPE, get_sorcery(), and sorcery.

Referenced by common_config_reload().

◆ tn_config_unload()

int tn_config_unload ( void  )

Definition at line 260 of file tn_config.c.

261{
264
265 return 0;
266}
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30

References ARRAY_LEN, ast_cli_unregister_multiple(), and stir_shaken_certificate_cli.

Referenced by common_config_unload().

◆ tn_destructor()

static void tn_destructor ( void *  obj)
static

Definition at line 52 of file tn_config.c.

53{
54 struct tn_cfg *cfg = obj;
55
58}
void acfg_cleanup(struct attestation_cfg_common *acfg_common)
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374

References acfg_cleanup(), tn_cfg::acfg_common, and ast_string_field_free_memory.

Referenced by etn_alloc(), and tn_alloc().

◆ tn_get_cfg()

struct tn_cfg * tn_get_cfg ( const char *  id)

Definition at line 39 of file tn_config.c.

40{
42}
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853

References ast_sorcery_retrieve_by_id(), CONFIG_TYPE, and get_sorcery().

Referenced by cli_tn_show().

◆ tn_get_etn()

struct tn_cfg * tn_get_etn ( const char *  id,
struct profile_cfg eprofile 
)

Definition at line 111 of file tn_config.c.

112{
113 const char *profile_id = eprofile ? ast_sorcery_object_get_id(eprofile) : "unknown";
114 RAII_VAR(struct tn_cfg *, tn,
117 RAII_VAR(struct tn_cfg *, etn, etn_alloc(id), ao2_cleanup);
118 enum attest_level_enum effective_al = attest_level_NOT_SET;
119 int rc = 0;
120 SCOPE_ENTER(3, "%s:%s: Getting effective TN\n", profile_id, S_OR(id, ""));
121
122 if (ast_strlen_zero(id) || !eprofile || !etn) {
123 SCOPE_EXIT_RTN_VALUE(NULL, "Missing params\n");
124 }
125
126 if (!tn) {
127 if (eprofile->unknown_tn_attest_level != attest_level_NOT_SET
128 && eprofile->unknown_tn_attest_level != attest_level_UNKNOWN) {
129 effective_al = eprofile->unknown_tn_attest_level;
130 ast_trace(-1, "%s:%s: TN not found. Using unknown_tn_attest_level %s\n",
131 profile_id, id, attest_level_to_str(effective_al));
132 } else {
133 SCOPE_EXIT_RTN_VALUE(NULL, "%s:%s: TN not found and unknown_tn_attest_level not set\n", profile_id, id);
134 }
135 }
136
137 /* Initialize with the acfg from the eprofile first */
138 rc = as_copy_cfg_common(id, &etn->acfg_common,
139 &eprofile->acfg_common);
140 if (rc != 0) {
141 SCOPE_EXIT_RTN_VALUE(NULL, "%s:%s: Couldn't copy from eprofile\n", profile_id, id);
142 }
143
144 /* Overwrite with anything in the TN itself */
145 if (tn) {
146 rc = as_copy_cfg_common(id, &etn->acfg_common,
147 &tn->acfg_common);
148 if (rc != 0) {
149 SCOPE_EXIT_RTN_VALUE(NULL, "%s:%s: Couldn't copy from tn\n", profile_id, id);
150 }
151 } else {
152 etn->acfg_common.attest_level = effective_al;
153 }
154
155 /*
156 * Unlike profile, we're not going to actually add a
157 * new object to sorcery because, although unlikely,
158 * the same TN could be used with multiple profiles.
159 */
160
161 SCOPE_EXIT_RTN_VALUE(ao2_bump(etn), "%s:%s: Done\n", profile_id, id);
162}
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
int as_copy_cfg_common(const char *id, struct attestation_cfg_common *cfg_dst, struct attestation_cfg_common *cfg_src)
#define SCOPE_EXIT_RTN_VALUE(__return_value,...)
#define SCOPE_ENTER(level,...)
#define ast_trace(level,...)
#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
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
struct attestation_cfg_common acfg_common
enum attest_level_enum unknown_tn_attest_level
static void * etn_alloc(const char *name)
Definition: tn_config.c:95
#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 profile_cfg::acfg_common, ao2_bump, ao2_cleanup, as_copy_cfg_common(), ast_sorcery_object_get_id(), ast_sorcery_retrieve_by_id(), ast_strlen_zero(), ast_trace, CONFIG_TYPE, etn_alloc(), get_sorcery(), NULL, RAII_VAR, S_OR, SCOPE_ENTER, SCOPE_EXIT_RTN_VALUE, and profile_cfg::unknown_tn_attest_level.

Referenced by ast_stir_shaken_as_ctx_create().

Variable Documentation

◆ stir_shaken_certificate_cli

struct ast_cli_entry stir_shaken_certificate_cli[]
static
Initial value:
= {
{ .handler = cli_tn_show , .summary = "Show stir/shaken TN configuration by id" ,},
{ .handler = cli_tn_show_all , .summary = "Show all stir/shaken attestation TN configurations" ,},
}
static char * cli_tn_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: tn_config.c:213
static char * cli_tn_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: tn_config.c:177

Definition at line 248 of file tn_config.c.

Referenced by tn_config_load(), and tn_config_unload().