Asterisk - The Open Source Telephony Project  21.4.1
tn_config.c
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 
37 struct tn_cfg *tn_get_cfg(const char *id)
38 {
39  return ast_sorcery_retrieve_by_id(get_sorcery(), CONFIG_TYPE, id);
40 }
41 
42 static struct ao2_container *get_tn_all(void)
43 {
44  return ast_sorcery_retrieve_by_fields(get_sorcery(), CONFIG_TYPE,
46 }
47 
48 generate_sorcery_enum_from_str(tn_cfg, acfg_common., check_tn_cert_public_url, UNKNOWN)
49 generate_sorcery_enum_to_str(tn_cfg, acfg_common., check_tn_cert_public_url)
50 
51 generate_sorcery_enum_from_str(tn_cfg, acfg_common., attest_level, UNKNOWN)
52 generate_sorcery_enum_to_str(tn_cfg, acfg_common., attest_level)
53 
54 generate_sorcery_enum_from_str(tn_cfg, acfg_common., send_mky, UNKNOWN)
55 generate_sorcery_enum_to_str(tn_cfg, acfg_common., send_mky)
56 
57 static void tn_destructor(void *obj)
58 {
59  struct tn_cfg *cfg = obj;
60 
62  acfg_cleanup(&cfg->acfg_common);
63 }
64 
65 static 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 
84 static 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 
100 static 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 
116 struct tn_cfg *tn_get_etn(const char *id, struct profile_cfg *eprofile)
117 {
118  RAII_VAR(struct tn_cfg *, tn,
119  ast_sorcery_retrieve_by_id(get_sorcery(), CONFIG_TYPE, S_OR(id, "")),
120  ao2_cleanup);
121  struct tn_cfg *etn = etn_alloc(id);
122  int rc = 0;
123 
124  if (!tn || !eprofile || !etn) {
125  ao2_cleanup(etn);
126  return NULL;
127  }
128 
129  /* Initialize with the acfg from the eprofile first */
130  rc = as_copy_cfg_common(id, &etn->acfg_common,
131  &eprofile->acfg_common);
132  if (rc != 0) {
133  ao2_cleanup(etn);
134  return NULL;
135  }
136 
137  /* Overwrite with anything in the TN itself */
138  rc = as_copy_cfg_common(id, &etn->acfg_common,
139  &tn->acfg_common);
140  if (rc != 0) {
141  ao2_cleanup(etn);
142  return NULL;
143  }
144 
145  /*
146  * Unlike profile, we're not going to actually add a
147  * new object to sorcery because, although unlikely,
148  * the same TN could be used with multiple profiles.
149  */
150 
151  return etn;
152 }
153 
154 static int tn_apply(const struct ast_sorcery *sorcery, void *obj)
155 {
156  struct tn_cfg *cfg = obj;
157  const char *id = ast_sorcery_object_get_id(cfg);
158  int rc = 0;
159 
160  if (as_check_common_config(id, &cfg->acfg_common) != 0) {
161  return -1;
162  }
163 
164  return rc;
165 }
166 
167 static char *cli_tn_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
168 {
169  struct ao2_container *container;
170  struct config_object_cli_data data = {
171  .title = "TN",
172  .object_type = config_object_type_tn,
173  };
174 
175  switch(cmd) {
176  case CLI_INIT:
177  e->command = "stir_shaken show tns";
178  e->usage =
179  "Usage: stir_shaken show tns\n"
180  " Show all attestation TNs\n";
181  return NULL;
182  case CLI_GENERATE:
183  return NULL;
184  }
185 
186  if (a->argc != 3) {
187  return CLI_SHOWUSAGE;
188  }
189 
190  container = get_tn_all();
191  if (!container || ao2_container_count(container) == 0) {
192  ast_cli(a->fd, "No stir/shaken TNs found\n");
193  ao2_cleanup(container);
194  return CLI_SUCCESS;
195  }
196 
197  ao2_callback_data(container, OBJ_NODATA, config_object_cli_show, a,&data);
198  ao2_ref(container, -1);
199 
200  return CLI_SUCCESS;
201 }
202 
203 static char *cli_tn_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
204 {
205  struct tn_cfg *cfg;
206  struct config_object_cli_data data = {
207  .title = "TN",
208  .object_type = config_object_type_tn,
209  };
210 
211  switch(cmd) {
212  case CLI_INIT:
213  e->command = "stir_shaken show tn";
214  e->usage =
215  "Usage: stir_shaken show tn <id>\n"
216  " Show the settings for a given TN\n";
217  return NULL;
218  case CLI_GENERATE:
219  if (a->pos == 3) {
220  return config_object_tab_complete_name(a->word, get_tn_all());
221  } else {
222  return NULL;
223  }
224  }
225 
226  if (a->argc != 4) {
227  return CLI_SHOWUSAGE;
228  }
229 
230  cfg = tn_get_cfg(a->argv[3]);
231  config_object_cli_show(cfg, a, &data, 0);
232  ao2_cleanup(cfg);
233 
234  return CLI_SUCCESS;
235 }
236 
237 
238 static struct ast_cli_entry stir_shaken_certificate_cli[] = {
239  AST_CLI_DEFINE(cli_tn_show, "Show stir/shaken TN configuration by id"),
240  AST_CLI_DEFINE(cli_tn_show_all, "Show all stir/shaken attestation TN configurations"),
241 };
242 
243 int tn_config_reload(void)
244 {
245  struct ast_sorcery *sorcery = get_sorcery();
246  ast_sorcery_force_reload_object(sorcery, CONFIG_TYPE);
248 }
249 
250 int tn_config_unload(void)
251 {
252  ast_cli_unregister_multiple(stir_shaken_certificate_cli,
253  ARRAY_LEN(stir_shaken_certificate_cli));
254 
255  return 0;
256 }
257 
258 int tn_config_load(void)
259 {
260  struct ast_sorcery *sorcery = get_sorcery();
261 
262  ast_sorcery_apply_default(sorcery, CONFIG_TYPE, "config", "stir_shaken.conf,criteria=type=tn");
263 
264  if (ast_sorcery_object_register(sorcery, CONFIG_TYPE, tn_alloc,
265  NULL, tn_apply)) {
266  ast_log(LOG_ERROR, "stir/shaken - failed to register '%s' sorcery object\n", CONFIG_TYPE);
268  }
269 
270  ast_sorcery_object_field_register(sorcery, CONFIG_TYPE, "type", "",
271  OPT_NOOP_T, 0, 0);
272 
273  register_common_attestation_fields(sorcery, tn_cfg, CONFIG_TYPE,);
274 
275  ast_sorcery_load_object(sorcery, CONFIG_TYPE);
276 
277  ast_cli_register_multiple(stir_shaken_certificate_cli,
278  ARRAY_LEN(stir_shaken_certificate_cli));
279 
281 }
Asterisk main include file. File version handling, generic pbx functions.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
TN configuration for stir/shaken.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
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
descriptor for a cli entry.
Definition: cli.h:171
Perform no matching, return all objects.
Definition: sorcery.h:123
Full structure for sorcery.
Definition: sorcery.c:230
Type for a default handler that should do nothing.
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
Return all matching objects.
Definition: sorcery.h:120
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_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_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317
struct ao2_container * container
Definition: res_fax.c:501
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition: sorcery.h:837
char * command
Definition: cli.h:186
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
const char * usage
Definition: cli.h:177
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_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object.
Definition: sorcery.h:955
Standard Command Line Interface.
#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
Profile configuration for stir/shaken.
Generic container type.
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728
Asterisk module definitions.
#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 ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374
Sorcery Data Access Layer API.