| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 39 |
struct tn_cfg *tn_get_cfg(const char *id) |
| 40 |
{ |
| 41 |
return ast_sorcery_retrieve_by_id(get_sorcery(), CONFIG_TYPE, id); |
| 42 |
} |
| 43 |
|
| 44 |
static struct ao2_container *get_tn_all(void) |
| 45 |
{ |
| 46 |
return ast_sorcery_retrieve_by_fields(get_sorcery(), CONFIG_TYPE, |
| 47 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 48 |
} |
| 49 |
|
| 50 |
generate_acfg_common_sorcery_handlers(tn_cfg); |
| 51 |
|
| 52 |
static void tn_destructor(void *obj) |
| 53 |
{ |
| 54 |
struct tn_cfg *cfg = obj; |
| 55 |
|
| 56 |
ast_string_field_free_memory(cfg); |
| 57 |
acfg_cleanup(&cfg->acfg_common); |
| 58 |
} |
| 59 |
|
| 60 |
static int init_tn(struct tn_cfg *cfg) |
| 61 |
{ |
| 62 |
if (ast_string_field_init(cfg, 1024)) { |
| 63 |
return -1; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
if (ast_string_field_init(&cfg->acfg_common, 8)) { |
| 73 |
return -1; |
| 74 |
} |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
static 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 |
|
| 95 |
static void *etn_alloc(const char *name) |
| 96 |
{ |
| 97 |
struct tn_cfg *cfg; |
| 98 |
|
| 99 |
cfg = ao2_alloc_options(sizeof(*cfg), tn_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 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 |
|
| 111 |
struct 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, |
| 115 |
ast_sorcery_retrieve_by_id(get_sorcery(), CONFIG_TYPE, S_OR(id, "")), |
| 116 |
ao2_cleanup); |
| 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 |
|
| 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 |
|
| 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 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
SCOPE_EXIT_RTN_VALUE(ao2_bump(etn), "%s:%s: Done\n", profile_id, id); |
| 162 |
} |
| 163 |
|
| 164 |
static 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 |
|
| 177 |
static 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 |
|
| 200 |
container = get_tn_all(); |
| 201 |
if (!container || ao2_container_count(container) == 0) { |
| 202 |
ast_cli(a->fd, "No stir/shaken TNs found\n"); |
| 203 |
ao2_cleanup(container); |
| 204 |
return CLI_SUCCESS; |
| 205 |
} |
| 206 |
|
| 207 |
ao2_callback_data(container, OBJ_NODATA, config_object_cli_show, a,&data); |
| 208 |
ao2_ref(container, -1); |
| 209 |
|
| 210 |
return CLI_SUCCESS; |
| 211 |
} |
| 212 |
|
| 213 |
static 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) { |
| 230 |
return config_object_tab_complete_name(a->word, get_tn_all()); |
| 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 |
|
| 248 |
static struct ast_cli_entry stir_shaken_certificate_cli[] = { |
| 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 |
|
| 253 |
int tn_config_reload(void) |
| 254 |
{ |
| 255 |
struct ast_sorcery *sorcery = get_sorcery(); |
| 256 |
ast_sorcery_force_reload_object(sorcery, CONFIG_TYPE); |
| 257 |
return AST_MODULE_LOAD_SUCCESS; |
| 258 |
} |
| 259 |
|
| 260 |
int tn_config_unload(void) |
| 261 |
{ |
| 262 |
ast_cli_unregister_multiple(stir_shaken_certificate_cli, |
| 263 |
ARRAY_LEN(stir_shaken_certificate_cli)); |
| 264 |
|
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
int tn_config_load(void) |
| 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 |
|
| 274 |
if (ast_sorcery_object_register(sorcery, CONFIG_TYPE, tn_alloc, |
| 275 |
NULL, tn_apply)) { |
| 276 |
ast_log(LOG_ERROR, "stir/shaken - failed to register '%s' sorcery object\n", CONFIG_TYPE); |
| 277 |
return AST_MODULE_LOAD_DECLINE; |
| 278 |
} |
| 279 |
|
| 280 |
ast_sorcery_object_field_register(sorcery, CONFIG_TYPE, "type", "", |
| 281 |
OPT_NOOP_T, 0, 0); |
| 282 |
|
| 283 |
register_common_attestation_fields(sorcery, tn_cfg, CONFIG_TYPE,); |
| 284 |
|
| 285 |
ast_sorcery_load_object(sorcery, CONFIG_TYPE); |
| 286 |
|
| 287 |
ast_cli_register_multiple(stir_shaken_certificate_cli, |
| 288 |
ARRAY_LEN(stir_shaken_certificate_cli)); |
| 289 |
|
| 290 |
return AST_MODULE_LOAD_SUCCESS; |
| 291 |
} |