Asterisk - The Open Source Telephony Project GIT-master-7e7a603
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#include "asterisk.h"
20
21#include <sys/stat.h>
22
23#include "asterisk/cli.h"
24#include "asterisk/module.h"
25#include "asterisk/sorcery.h"
26
27#include "stir_shaken.h"
28
29#define CONFIG_TYPE "tn"
30
31#define DEFAULT_check_tn_cert_public_url check_tn_cert_public_url_NO
32#define DEFAULT_private_key_file NULL
33#define DEFAULT_public_cert_url NULL
34#define DEFAULT_attest_level attest_level_NOT_SET
35#define DEFAULT_send_mky send_mky_NO
36
37struct tn_cfg *tn_get_cfg(const char *id)
38{
40}
41
42static struct ao2_container *get_tn_all(void)
43{
46}
47
48generate_sorcery_enum_from_str(tn_cfg, acfg_common., check_tn_cert_public_url, UNKNOWN)
49generate_sorcery_enum_to_str(tn_cfg, acfg_common., check_tn_cert_public_url)
50
51generate_sorcery_enum_from_str(tn_cfg, acfg_common., attest_level, UNKNOWN)
52generate_sorcery_enum_to_str(tn_cfg, acfg_common., attest_level)
53
54generate_sorcery_enum_from_str(tn_cfg, acfg_common., send_mky, UNKNOWN)
55generate_sorcery_enum_to_str(tn_cfg, acfg_common., send_mky)
56
57static void tn_destructor(void *obj)
58{
59 struct tn_cfg *cfg = obj;
60
63}
64
65static int init_tn(struct tn_cfg *cfg)
66{
67 if (ast_string_field_init(cfg, 1024)) {
68 return -1;
69 }
70
71 /*
72 * The memory for the commons actually comes from cfg
73 * due to the weirdness of the STRFLDSET macro used with
74 * sorcery. We just use a token amount of memory in
75 * this call so the initialize doesn't fail.
76 */
77 if (ast_string_field_init(&cfg->acfg_common, 8)) {
78 return -1;
79 }
80
81 return 0;
82}
83
84static void *tn_alloc(const char *name)
85{
86 struct tn_cfg *cfg;
87
88 cfg = ast_sorcery_generic_alloc(sizeof(*cfg), tn_destructor);
89 if (!cfg) {
90 return NULL;
91 }
92
93 if (init_tn(cfg) != 0) {
94 ao2_cleanup(cfg);
95 cfg = NULL;
96 }
97 return cfg;
98}
99
100static void *etn_alloc(const char *name)
101{
102 struct tn_cfg *cfg;
103
104 cfg = ao2_alloc_options(sizeof(*cfg), tn_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
105 if (!cfg) {
106 return NULL;
107 }
108
109 if (init_tn(cfg) != 0) {
110 ao2_cleanup(cfg);
111 cfg = NULL;
112 }
113 return cfg;
114}
115
116struct tn_cfg *tn_get_etn(const char *id, struct profile_cfg *eprofile)
117{
118 RAII_VAR(struct tn_cfg *, tn,
121 struct tn_cfg *etn = etn_alloc(id);
122 int rc = 0;
123
124 if (!tn || !eprofile || !etn) {
125 return NULL;
126 }
127
128 /* Initialize with the acfg from the eprofile first */
129 rc = as_copy_cfg_common(id, &etn->acfg_common,
130 &eprofile->acfg_common);
131 if (rc != 0) {
132 ao2_cleanup(etn);
133 return NULL;
134 }
135
136 /* Overwrite with anything in the TN itself */
137 rc = as_copy_cfg_common(id, &etn->acfg_common,
138 &tn->acfg_common);
139 if (rc != 0) {
140 ao2_cleanup(etn);
141 return NULL;
142 }
143
144 /*
145 * Unlike profile, we're not going to actually add a
146 * new object to sorcery because, although unlikely,
147 * the same TN could be used with multiple profiles.
148 */
149
150 return etn;
151}
152
153static int tn_apply(const struct ast_sorcery *sorcery, void *obj)
154{
155 struct tn_cfg *cfg = obj;
156 const char *id = ast_sorcery_object_get_id(cfg);
157 int rc = 0;
158
159 if (as_check_common_config(id, &cfg->acfg_common) != 0) {
160 return -1;
161 }
162
163 return rc;
164}
165
166static char *cli_tn_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
167{
168 struct ao2_container *container;
169 struct config_object_cli_data data = {
170 .title = "TN",
171 .object_type = config_object_type_tn,
172 };
173
174 switch(cmd) {
175 case CLI_INIT:
176 e->command = "stir_shaken show tns";
177 e->usage =
178 "Usage: stir_shaken show tns\n"
179 " Show all attestation TNs\n";
180 return NULL;
181 case CLI_GENERATE:
182 return NULL;
183 }
184
185 if (a->argc != 3) {
186 return CLI_SHOWUSAGE;
187 }
188
191 ast_cli(a->fd, "No stir/shaken TNs found\n");
193 return CLI_SUCCESS;
194 }
195
197 ao2_ref(container, -1);
198
199 return CLI_SUCCESS;
200}
201
202static char *cli_tn_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
203{
204 struct tn_cfg *cfg;
205 struct config_object_cli_data data = {
206 .title = "TN",
207 .object_type = config_object_type_tn,
208 };
209
210 switch(cmd) {
211 case CLI_INIT:
212 e->command = "stir_shaken show tn";
213 e->usage =
214 "Usage: stir_shaken show tn <id>\n"
215 " Show the settings for a given TN\n";
216 return NULL;
217 case CLI_GENERATE:
218 if (a->pos == 3) {
220 } else {
221 return NULL;
222 }
223 }
224
225 if (a->argc != 4) {
226 return CLI_SHOWUSAGE;
227 }
228
229 cfg = tn_get_cfg(a->argv[3]);
230 config_object_cli_show(cfg, a, &data, 0);
231 ao2_cleanup(cfg);
232
233 return CLI_SUCCESS;
234}
235
236
238 AST_CLI_DEFINE(cli_tn_show, "Show stir/shaken TN configuration by id"),
239 AST_CLI_DEFINE(cli_tn_show_all, "Show all stir/shaken attestation TN configurations"),
240};
241
243{
244 struct ast_sorcery *sorcery = get_sorcery();
247}
248
250{
253
254 return 0;
255}
256
258{
259 struct ast_sorcery *sorcery = get_sorcery();
260
261 ast_sorcery_apply_default(sorcery, CONFIG_TYPE, "config", "stir_shaken.conf,criteria=type=tn");
262
264 NULL, tn_apply)) {
265 ast_log(LOG_ERROR, "stir/shaken - failed to register '%s' sorcery object\n", CONFIG_TYPE);
267 }
268
270 OPT_NOOP_T, 0, 0);
271
273
275
278
280}
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
@ 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 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
generate_sorcery_enum_to_str(profile_cfg,, endpoint_behavior)
struct ao2_container * container
Definition: res_fax.c:501
@ UNKNOWN
Definition: res_pjsip.h:440
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
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
TN configuration for stir/shaken.
struct attestation_cfg_common acfg_common
static struct test_val a
generate_sorcery_enum_from_str(generate_sorcery_enum_to_str(tn_cfg, generate_sorcery_enum_to_str(acfg_common., check_tn_cert_public_url, UNKNOWN)
Definition: tn_config.c:48
static char * cli_tn_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: tn_config.c:202
struct tn_cfg * tn_get_etn(const char *id, struct profile_cfg *eprofile)
Definition: tn_config.c:116
int tn_config_load(void)
Definition: tn_config.c:257
static char * cli_tn_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: tn_config.c:166
static struct ast_cli_entry stir_shaken_certificate_cli[]
Definition: tn_config.c:237
static struct ao2_container * get_tn_all(void)
Definition: tn_config.c:42
int tn_config_unload(void)
Definition: tn_config.c:249
static void * etn_alloc(const char *name)
Definition: tn_config.c:100
static void * tn_alloc(const char *name)
Definition: tn_config.c:84
int tn_config_reload(void)
Definition: tn_config.c:242
struct tn_cfg * tn_get_cfg(const char *id)
Definition: tn_config.c:37
static int init_tn(struct tn_cfg *cfg)
Definition: tn_config.c:65
#define CONFIG_TYPE
Definition: tn_config.c:29
static int tn_apply(const struct ast_sorcery *sorcery, void *obj)
Definition: tn_config.c:153
#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