Asterisk - The Open Source Telephony Project  18.5.0
store.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2020, Sangoma Technologies Corporation
5  *
6  * Kevin Harwell <[email protected]>
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/sorcery.h"
25 
26 #include "stir_shaken.h"
27 #include "store.h"
29 
30 #define CONFIG_TYPE "store"
31 
32 #define VARIABLE_SUBSTITUTE "${CERTIFICATE}"
33 
35  SORCERY_OBJECT(details);
37  /*! Path to a directory containing certificates */
39  /*! URL to the public certificate */
41  );
42 };
43 
44 static struct stir_shaken_store *stir_shaken_store_get(const char *id)
45 {
47 }
48 
50 {
53 }
54 
55 static void stir_shaken_store_destructor(void *obj)
56 {
57  struct stir_shaken_store *cfg = obj;
58 
60 }
61 
62 static void *stir_shaken_store_alloc(const char *name)
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 }
78 
79 static int stir_shaken_store_apply(const struct ast_sorcery *sorcery, void *obj)
80 {
81  return 0;
82 }
83 
84 static char *stir_shaken_store_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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 }
113 
115  AST_CLI_DEFINE(stir_shaken_store_show, "Show stir/shaken store configuration by id"),
116 };
117 
118 static int on_load_path(const struct aco_option *opt, struct ast_variable *var, void *obj)
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 }
135 
136 static int path_to_str(const void *obj, const intptr_t *args, char **buf)
137 {
138  const struct stir_shaken_store *cfg = obj;
139 
140  *buf = ast_strdup(cfg->path);
141 
142  return 0;
143 }
144 
145 static int on_load_public_cert_url(const struct aco_option *opt, struct ast_variable *var, void *obj)
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 }
162 
163 static int public_cert_url_to_str(const void *obj, const intptr_t *args, char **buf)
164 {
165  const struct stir_shaken_store *cfg = obj;
166 
167  *buf = ast_strdup(cfg->public_cert_url);
168 
169  return 0;
170 }
171 
173 {
174  ast_cli_unregister_multiple(stir_shaken_store_cli,
175  ARRAY_LEN(stir_shaken_store_cli));
176 
177  return 0;
178 }
179 
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 
198  ast_cli_register_multiple(stir_shaken_store_cli,
199  ARRAY_LEN(stir_shaken_store_cli));
200 
201  return 0;
202 }
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
Asterisk main include file. File version handling, generic pbx functions.
#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
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int public_cert_url_to_str(const void *obj, const intptr_t *args, char **buf)
Definition: store.c:163
SORCERY_OBJECT(details)
descriptor for a cli entry.
Definition: cli.h:171
const int argc
Definition: cli.h:160
static struct ast_cli_entry stir_shaken_store_cli[]
Definition: store.c:114
Structure for variables, used for configurations and for channel variables.
static void * stir_shaken_store_alloc(const char *name)
Definition: store.c:62
#define var
Definition: ast_expr2f.c:614
Perform no matching, return all objects.
Definition: sorcery.h:123
Definition: cli.h:152
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 AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
Definition: stringfields.h:337
Return all matching objects.
Definition: sorcery.h:120
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
const char * args
#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
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
static int on_load_path(const struct aco_option *opt, struct ast_variable *var, void *obj)
Definition: store.c:118
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_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_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:353
#define AST_STRING_FIELD(name)
Declare a string field.
Definition: stringfields.h:299
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static char * stir_shaken_store_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: store.c:84
int stir_shaken_cli_show(void *obj, void *arg, int flags)
Output configuration settings to the Asterisk CLI.
Definition: stir_shaken.c:35
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition: sorcery.h:838
const char *const * argv
Definition: cli.h:161
#define LOG_ERROR
Definition: logger.h:285
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define VARIABLE_SUBSTITUTE
Definition: store.c:32
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:477
const ast_string_field public_cert_url
Definition: store.c:41
static const char name[]
Definition: cdr_mysql.c:74
char * command
Definition: cli.h:186
const ast_string_field path
Definition: store.c:41
struct ast_sorcery * ast_stir_shaken_sorcery(void)
Retrieve the stir/shaken sorcery context.
const char * word
Definition: cli.h:163
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
#define CLI_SUCCESS
Definition: cli.h:44
static void stir_shaken_store_destructor(void *obj)
Definition: store.c:55
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Standard Command Line Interface.
int stir_shaken_store_unload(void)
Unload time cleanup for the stir/shaken &#39;store&#39; configuration.
Definition: store.c:172
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
static int path_to_str(const void *obj, const intptr_t *args, char **buf)
Definition: store.c:136
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Definition: strings.h:94
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
int stir_shaken_store_load(void)
Load time initialization for the stir/shaken &#39;store&#39; configuration.
Definition: store.c:180
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368
Sorcery Data Access Layer API.
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514
static struct test_val a