Asterisk - The Open Source Telephony Project GIT-master-f36a736
tn_config.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2023, Sangoma Technologies Corporation
5 *
6 * George Joseph <gjoseph@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#define _TRACE_PREFIX_ "tc",__LINE__, ""
20
21#include "asterisk.h"
22
23#include <sys/stat.h>
24
25#include "asterisk/cli.h"
26#include "asterisk/module.h"
27#include "asterisk/sorcery.h"
28
29#include "stir_shaken.h"
30
31#define CONFIG_TYPE "tn"
32
33#define DEFAULT_check_tn_cert_public_url check_tn_cert_public_url_NOT_SET
34#define DEFAULT_private_key_file NULL
35#define DEFAULT_public_cert_url NULL
36#define DEFAULT_attest_level attest_level_NOT_SET
37#define DEFAULT_send_mky send_mky_NOT_SET
38
39struct tn_cfg *tn_get_cfg(const char *id)
40{
42}
43
44static struct ao2_container *get_tn_all(void)
45{
48}
49
51
52static void tn_destructor(void *obj)
53{
54 struct tn_cfg *cfg = obj;
55
58}
59
60static int init_tn(struct tn_cfg *cfg)
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}
78
79static void *tn_alloc(const char *name)
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}
94
95static void *etn_alloc(const char *name)
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}
110
111struct tn_cfg *tn_get_etn(const char *id, struct profile_cfg *eprofile)
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}
163
164static int tn_apply(const struct ast_sorcery *sorcery, void *obj)
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}
176
177static char *cli_tn_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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}
212
213static char *cli_tn_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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}
246
247
249 AST_CLI_DEFINE(cli_tn_show, "Show stir/shaken TN configuration by id"),
250 AST_CLI_DEFINE(cli_tn_show_all, "Show all stir/shaken attestation TN configurations"),
251};
252
254{
255 struct ast_sorcery *sorcery = get_sorcery();
258}
259
261{
264
265 return 0;
266}
267
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}
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition: astobj2.h:367
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#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
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:404
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
@ OBJ_NODATA
Definition: astobj2.h:1044
int as_check_common_config(const char *id, struct attestation_cfg_common *acfg_common)
void acfg_cleanup(struct attestation_cfg_common *acfg_common)
int as_copy_cfg_common(const char *id, struct attestation_cfg_common *cfg_dst, struct attestation_cfg_common *cfg_src)
Standard Command Line Interface.
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
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.
struct ast_sorcery * get_sorcery(void)
Retrieve the stir/shaken sorcery context.
Definition: common_config.c:34
@ config_object_type_tn
#define register_common_attestation_fields(sorcery, object, CONFIG_TYPE, nodoc)
@ OPT_NOOP_T
Type for a default handler that should do nothing.
static const char name[]
Definition: format_mp3.c:68
#define SCOPE_EXIT_RTN_VALUE(__return_value,...)
#define SCOPE_ENTER(level,...)
#define ast_trace(level,...)
#define LOG_ERROR
Asterisk module definitions.
@ 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
struct ao2_container * container
Definition: res_fax.c:501
static struct ast_sorcery * sorcery
#define NULL
Definition: resample.c:96
Sorcery Data Access Layer API.
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317
@ 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_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
#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
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object.
Definition: sorcery.h:955
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
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:476
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 ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374
#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
Generic container type.
descriptor for a cli entry.
Definition: cli.h:171
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
Full structure for sorcery.
Definition: sorcery.c:230
Profile configuration for stir/shaken.
struct attestation_cfg_common acfg_common
enum attest_level_enum unknown_tn_attest_level
TN configuration for stir/shaken.
struct attestation_cfg_common acfg_common
static struct test_val a
static char * cli_tn_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: tn_config.c:213
struct tn_cfg * tn_get_etn(const char *id, struct profile_cfg *eprofile)
Definition: tn_config.c:111
int tn_config_load(void)
Definition: tn_config.c:268
static char * cli_tn_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: tn_config.c:177
static struct ast_cli_entry stir_shaken_certificate_cli[]
Definition: tn_config.c:248
static struct ao2_container * get_tn_all(void)
Definition: tn_config.c:44
int tn_config_unload(void)
Definition: tn_config.c:260
static void * etn_alloc(const char *name)
Definition: tn_config.c:95
static void * tn_alloc(const char *name)
Definition: tn_config.c:79
int tn_config_reload(void)
Definition: tn_config.c:253
static void tn_destructor(void *obj)
Definition: tn_config.c:52
struct tn_cfg * tn_get_cfg(const char *id)
Definition: tn_config.c:39
static int init_tn(struct tn_cfg *cfg)
Definition: tn_config.c:60
#define CONFIG_TYPE
Definition: tn_config.c:31
generate_acfg_common_sorcery_handlers(tn_cfg)
static int tn_apply(const struct ast_sorcery *sorcery, void *obj)
Definition: tn_config.c:164
#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
#define ARRAY_LEN(a)
Definition: utils.h:666