Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
store.c File Reference
#include "asterisk.h"
#include <sys/stat.h>
#include "asterisk/cli.h"
#include "asterisk/sorcery.h"
#include "stir_shaken.h"
#include "store.h"
#include "asterisk/res_stir_shaken.h"
Include dependency graph for store.c:

Go to the source code of this file.

Data Structures

struct  stir_shaken_store
 

Macros

#define CONFIG_TYPE   "store"
 
#define VARIABLE_SUBSTITUTE   "${CERTIFICATE}"
 

Functions

static int on_load_path (const struct aco_option *opt, struct ast_variable *var, void *obj)
 
static int on_load_public_cert_url (const struct aco_option *opt, struct ast_variable *var, void *obj)
 
static int path_to_str (const void *obj, const intptr_t *args, char **buf)
 
static int public_cert_url_to_str (const void *obj, const intptr_t *args, char **buf)
 
static void * stir_shaken_store_alloc (const char *name)
 
static int stir_shaken_store_apply (const struct ast_sorcery *sorcery, void *obj)
 
static void stir_shaken_store_destructor (void *obj)
 
static struct stir_shaken_storestir_shaken_store_get (const char *id)
 
static struct ao2_containerstir_shaken_store_get_all (void)
 
int stir_shaken_store_load (void)
 Load time initialization for the stir/shaken 'store' configuration. More...
 
static char * stir_shaken_store_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
int stir_shaken_store_unload (void)
 Unload time cleanup for the stir/shaken 'store' configuration. More...
 

Variables

static struct ast_cli_entry stir_shaken_store_cli []
 

Macro Definition Documentation

◆ CONFIG_TYPE

#define CONFIG_TYPE   "store"

◆ VARIABLE_SUBSTITUTE

#define VARIABLE_SUBSTITUTE   "${CERTIFICATE}"

Definition at line 32 of file store.c.

Referenced by on_load_public_cert_url().

Function Documentation

◆ on_load_path()

static int on_load_path ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Definition at line 118 of file store.c.

References ast_log, ast_string_field_set, LOG_ERROR, stir_shaken_store::path, and ast_variable::value.

Referenced by stir_shaken_store_load().

119 {
120  struct stir_shaken_store *cfg = obj;
121  struct stat statbuf;
122 
123  if (stat(var->value, &statbuf)) {
124  ast_log(LOG_ERROR, "stir/shaken - path '%s' not found\n", var->value);
125  return -1;
126  }
127 
128  if (!S_ISDIR(statbuf.st_mode)) {
129  ast_log(LOG_ERROR, "stir/shaken - path '%s' is not a directory\n", var->value);
130  return -1;
131  }
132 
133  return ast_string_field_set(cfg, path, var->value);
134 }
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514

◆ on_load_public_cert_url()

static int on_load_public_cert_url ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Definition at line 145 of file store.c.

References ast_begins_with(), ast_log, ast_string_field_set, LOG_ERROR, stir_shaken_store::public_cert_url, ast_variable::value, and VARIABLE_SUBSTITUTE.

Referenced by stir_shaken_store_load().

146 {
147  struct stir_shaken_store *cfg = obj;
148 
149  if (!ast_begins_with(var->value, "http")) {
150  ast_log(LOG_ERROR, "stir/shaken - public_cert_url scheme must be 'http[s]'\n");
151  return -1;
152  }
153 
154  if (!strstr(var->value, VARIABLE_SUBSTITUTE)) {
155  ast_log(LOG_ERROR, "stir/shaken - public_cert_url must contain variable '%s' "
156  "used for substitution\n", VARIABLE_SUBSTITUTE);
157  return -1;
158  }
159 
160  return ast_string_field_set(cfg, public_cert_url, var->value);
161 }
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
#define VARIABLE_SUBSTITUTE
Definition: store.c:32
const ast_string_field public_cert_url
Definition: store.c:41
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Definition: strings.h:94
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514

◆ path_to_str()

static int path_to_str ( const void *  obj,
const intptr_t *  args,
char **  buf 
)
static

Definition at line 136 of file store.c.

References ast_strdup, and stir_shaken_store::path.

Referenced by stir_shaken_store_load().

137 {
138  const struct stir_shaken_store *cfg = obj;
139 
140  *buf = ast_strdup(cfg->path);
141 
142  return 0;
143 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
const ast_string_field path
Definition: store.c:41

◆ public_cert_url_to_str()

static int public_cert_url_to_str ( const void *  obj,
const intptr_t *  args,
char **  buf 
)
static

Definition at line 163 of file store.c.

References ast_strdup, and stir_shaken_store::public_cert_url.

Referenced by stir_shaken_store_load().

164 {
165  const struct stir_shaken_store *cfg = obj;
166 
167  *buf = ast_strdup(cfg->public_cert_url);
168 
169  return 0;
170 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
const ast_string_field public_cert_url
Definition: store.c:41

◆ stir_shaken_store_alloc()

static void* stir_shaken_store_alloc ( const char *  name)
static

Definition at line 62 of file store.c.

References ao2_ref, ast_sorcery_generic_alloc(), ast_string_field_init, NULL, and stir_shaken_store_destructor().

Referenced by stir_shaken_store_load().

63 {
64  struct stir_shaken_store *cfg;
65 
67  if (!cfg) {
68  return NULL;
69  }
70 
71  if (ast_string_field_init(cfg, 512)) {
72  ao2_ref(cfg, -1);
73  return NULL;
74  }
75 
76  return cfg;
77 }
#define NULL
Definition: resample.c:96
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:353
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static void stir_shaken_store_destructor(void *obj)
Definition: store.c:55
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728

◆ stir_shaken_store_apply()

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

Definition at line 79 of file store.c.

Referenced by stir_shaken_store_load().

80 {
81  return 0;
82 }

◆ stir_shaken_store_destructor()

static void stir_shaken_store_destructor ( void *  obj)
static

Definition at line 55 of file store.c.

References ast_string_field_free_memory.

Referenced by stir_shaken_store_alloc().

56 {
57  struct stir_shaken_store *cfg = obj;
58 
60 }
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368

◆ stir_shaken_store_get()

static struct stir_shaken_store* stir_shaken_store_get ( const char *  id)
static

Definition at line 44 of file store.c.

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

Referenced by stir_shaken_store_show().

45 {
47 }
#define CONFIG_TYPE
Definition: store.c:30
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
struct ast_sorcery * ast_stir_shaken_sorcery(void)
Retrieve the stir/shaken sorcery context.

◆ stir_shaken_store_get_all()

static struct ao2_container* stir_shaken_store_get_all ( void  )
static

Definition at line 49 of file store.c.

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

Referenced by stir_shaken_store_show().

50 {
53 }
Perform no matching, return all objects.
Definition: sorcery.h:123
Return all matching objects.
Definition: sorcery.h:120
#define NULL
Definition: resample.c:96
#define CONFIG_TYPE
Definition: store.c:30
struct ast_sorcery * ast_stir_shaken_sorcery(void)
Retrieve the stir/shaken sorcery context.
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

◆ stir_shaken_store_load()

int stir_shaken_store_load ( void  )

Load time initialization for the stir/shaken 'store' configuration.

Return values
0on success, -1 on error

Definition at line 180 of file store.c.

References ARRAY_LEN, ast_cli_register_multiple, ast_log, ast_sorcery_apply_default, ast_sorcery_object_field_register, ast_sorcery_object_field_register_custom, ast_sorcery_object_register, ast_stir_shaken_sorcery(), CONFIG_TYPE, LOG_ERROR, NULL, on_load_path(), on_load_public_cert_url(), OPT_NOOP_T, path_to_str(), public_cert_url_to_str(), sorcery, stir_shaken_store_alloc(), and stir_shaken_store_apply().

Referenced by load_module().

181 {
183 
184  ast_sorcery_apply_default(sorcery, CONFIG_TYPE, "config", "stir_shaken.conf,criteria=type=store");
185 
188  ast_log(LOG_ERROR, "stir/shaken - failed to register '%s' sorcery object\n", CONFIG_TYPE);
189  return -1;
190  }
191 
192  ast_sorcery_object_field_register(sorcery, CONFIG_TYPE, "type", "", OPT_NOOP_T, 0, 0);
194  on_load_path, path_to_str, NULL, 0, 0);
195  ast_sorcery_object_field_register_custom(sorcery, CONFIG_TYPE, "public_cert_url", "",
197 
200 
201  return 0;
202 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static int public_cert_url_to_str(const void *obj, const intptr_t *args, char **buf)
Definition: store.c:163
static struct ast_cli_entry stir_shaken_store_cli[]
Definition: store.c:114
static void * stir_shaken_store_alloc(const char *name)
Definition: store.c:62
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
static int on_load_public_cert_url(const struct aco_option *opt, struct ast_variable *var, void *obj)
Definition: store.c:145
#define NULL
Definition: resample.c:96
#define CONFIG_TYPE
Definition: store.c:30
static int stir_shaken_store_apply(const struct ast_sorcery *sorcery, void *obj)
Definition: store.c:79
static int on_load_path(const struct aco_option *opt, struct ast_variable *var, void *obj)
Definition: store.c:118
#define ast_log
Definition: astobj2.c:42
#define ast_sorcery_object_field_register_custom(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags,...)
Register a field within an object with custom handlers.
Definition: sorcery.h:1005
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition: sorcery.h:838
#define LOG_ERROR
Definition: logger.h:285
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:477
struct ast_sorcery * ast_stir_shaken_sorcery(void)
Retrieve the stir/shaken sorcery context.
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object.
Definition: sorcery.h:955
static struct ast_sorcery * sorcery
static int path_to_str(const void *obj, const intptr_t *args, char **buf)
Definition: store.c:136

◆ stir_shaken_store_show()

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

Definition at line 84 of file store.c.

References ao2_cleanup, ast_cli_args::argc, ast_cli_args::argv, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, ast_cli_args::pos, stir_shaken_cli_show(), stir_shaken_store_get(), stir_shaken_store_get_all(), stir_shaken_tab_complete_name(), ast_cli_entry::usage, and ast_cli_args::word.

85 {
86  struct stir_shaken_store *cfg;
87 
88  switch(cmd) {
89  case CLI_INIT:
90  e->command = "stir_shaken show store";
91  e->usage =
92  "Usage: stir_shaken show store <id>\n"
93  " Show the store stir/shaken settings for a given id\n";
94  return NULL;
95  case CLI_GENERATE:
96  if (a->pos == 3) {
98  } else {
99  return NULL;
100  };
101  }
102 
103  if (a->argc != 4) {
104  return CLI_SHOWUSAGE;
105  }
106 
107  cfg = stir_shaken_store_get(a->argv[3]);
108  stir_shaken_cli_show(cfg, a, 0);
109  ao2_cleanup(cfg);
110 
111  return CLI_SUCCESS;
112 }
const int argc
Definition: cli.h:160
Definition: cli.h:152
#define NULL
Definition: resample.c:96
char * stir_shaken_tab_complete_name(const char *word, struct ao2_container *container)
Tab completion for name matching with STIR/SHAKEN CLI commands.
Definition: stir_shaken.c:66
int stir_shaken_cli_show(void *obj, void *arg, int flags)
Output configuration settings to the Asterisk CLI.
Definition: stir_shaken.c:35
const char *const * argv
Definition: cli.h:161
#define CLI_SHOWUSAGE
Definition: cli.h:45
char * command
Definition: cli.h:186
const char * word
Definition: cli.h:163
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static struct ao2_container * stir_shaken_store_get_all(void)
Definition: store.c:49
const int pos
Definition: cli.h:164
static struct stir_shaken_store * stir_shaken_store_get(const char *id)
Definition: store.c:44

◆ stir_shaken_store_unload()

int stir_shaken_store_unload ( void  )

Unload time cleanup for the stir/shaken 'store' configuration.

Return values
0on success, -1 on error

Definition at line 172 of file store.c.

References ARRAY_LEN, and ast_cli_unregister_multiple().

Referenced by unload_module().

173 {
176 
177  return 0;
178 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
static struct ast_cli_entry stir_shaken_store_cli[]
Definition: store.c:114

Variable Documentation

◆ stir_shaken_store_cli

struct ast_cli_entry stir_shaken_store_cli[]
static
Initial value:
= {
{ .handler = stir_shaken_store_show , .summary = "Show stir/shaken store configuration by id" ,},
}
static char * stir_shaken_store_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: store.c:84

Definition at line 114 of file store.c.