Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions
internal.h File Reference

Internal API's for res_ari. More...

#include "asterisk/http.h"
#include "asterisk/json.h"
#include "asterisk/stringfields.h"
Include dependency graph for internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_ari_conf
 All configuration options for ARI. More...
 
struct  ast_ari_conf_general
 Global configuration options for ARI. More...
 
struct  ast_ari_conf_user
 Per-user configuration options. More...
 

Functions

void ari_handle_websocket (struct ast_websocket_server *ws_server, struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers)
 Wrapper for invoking the websocket code for an incoming connection. More...
 
int ast_ari_cli_register (void)
 Register CLI commands for ARI. More...
 
void ast_ari_cli_unregister (void)
 Unregister CLI commands for ARI. More...
 
#define ARI_AUTH_REALM_LEN   80
 
#define ARI_PASSWORD_LEN   256
 User's password mx length. More...
 
enum  ast_ari_password_format { ARI_PASSWORD_FORMAT_PLAIN, ARI_PASSWORD_FORMAT_CRYPT }
 Password format. More...
 
int ast_ari_config_init (void)
 Initialize the ARI configuration. More...
 
int ast_ari_config_reload (void)
 Reload the ARI configuration. More...
 
void ast_ari_config_destroy (void)
 Destroy the ARI configuration. More...
 
struct ast_ari_confast_ari_config_get (void)
 Get the current ARI configuration. More...
 
struct ast_ari_conf_userast_ari_config_validate_user (const char *username, const char *password)
 Validated a user's credentials. More...
 

Detailed Description

Internal API's for res_ari.

Author
David M. Lee, II [email protected][email protected]digi[email protected]um.co[email protected]m

Definition in file internal.h.

Macro Definition Documentation

◆ ARI_AUTH_REALM_LEN

#define ARI_AUTH_REALM_LEN   80

Max length for auth_realm field

Definition at line 62 of file internal.h.

Referenced by ast_ari_config_init().

◆ ARI_PASSWORD_LEN

#define ARI_PASSWORD_LEN   256

User's password mx length.

If 256 seems like a lot, a crypt SHA-512 has over 106 characters.

Definition at line 93 of file internal.h.

Referenced by ast_ari_config_init().

Enumeration Type Documentation

◆ ast_ari_password_format

Password format.

Enumerator
ARI_PASSWORD_FORMAT_PLAIN 

Plaintext password.

ARI_PASSWORD_FORMAT_CRYPT 

crypt(3) password

Definition at line 81 of file internal.h.

81  {
82  /*! \brief Plaintext password */
84  /*! crypt(3) password */
86 };
Plaintext password.
Definition: internal.h:83

Function Documentation

◆ ari_handle_websocket()

void ari_handle_websocket ( struct ast_websocket_server ws_server,
struct ast_tcptls_session_instance ser,
const char *  uri,
enum ast_http_method  method,
struct ast_variable get_params,
struct ast_variable headers 
)

Wrapper for invoking the websocket code for an incoming connection.

Parameters
ws_serverWebSocket server to invoke.
serHTTP session.
uriRequested URI.
methodRequested HTTP method.
get_paramsParsed query parameters.
headersParsed HTTP headers.

Definition at line 191 of file ari_websockets.c.

References ast_websocket_uri_cb(), and ast_http_uri::data.

Referenced by ast_ari_invoke().

195 {
196  struct ast_http_uri fake_urih = {
197  .data = ws_server,
198  };
199  ast_websocket_uri_cb(ser, &fake_urih, uri, method, get_params,
200  headers);
201 }
const char * method
Definition: res_pjsip.c:4335
int AST_OPTIONAL_API_NAME() ast_websocket_uri_cb(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers)
Definition of a URI handler.
Definition: http.h:100
const char * uri
Definition: http.h:103
void * data
Definition: http.h:114

◆ ast_ari_cli_register()

int ast_ari_cli_register ( void  )

Register CLI commands for ARI.

Returns
0 on success.
Non-zero on error.

Definition at line 434 of file res/ari/cli.c.

References ARRAY_LEN, and ast_cli_register_multiple.

Referenced by load_module().

434  {
436 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static struct ast_cli_entry cli_ari[]
Definition: res/ari/cli.c:424

◆ ast_ari_cli_unregister()

void ast_ari_cli_unregister ( void  )

Unregister CLI commands for ARI.

Definition at line 438 of file res/ari/cli.c.

References ARRAY_LEN, and ast_cli_unregister_multiple().

Referenced by unload_module().

438  {
440 }
#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 cli_ari[]
Definition: res/ari/cli.c:424

◆ ast_ari_config_destroy()

void ast_ari_config_destroy ( void  )

Destroy the ARI configuration.

Definition at line 383 of file res/ari/config.c.

References aco_info_destroy(), and ao2_global_obj_release.

Referenced by unload_module().

384 {
385  aco_info_destroy(&cfg_info);
387 }
void aco_info_destroy(struct aco_info *info)
Destroy an initialized aco_info struct.
#define ao2_global_obj_release(holder)
Definition: astobj2.h:865

◆ ast_ari_config_get()

struct ast_ari_conf* ast_ari_config_get ( void  )

Get the current ARI configuration.

This is an immutable object, so don't modify it. It is AO2 managed, so ao2_cleanup() when you're done with it.

Returns
ARI configuration object.
NULL on error.

Definition at line 227 of file res/ari/config.c.

References ao2_global_obj_ref, ast_log, CONF_FILENAME, and LOG_ERROR.

Referenced by ari_show(), ari_show_user(), ari_show_users(), ast_ari_callback(), ast_ari_config_validate_user(), ast_ari_json_format(), ast_ari_websocket_session_create(), complete_ari_user(), is_enabled(), origin_allowed(), and process_config().

228 {
229  struct ast_ari_conf *res = ao2_global_obj_ref(confs);
230  if (!res) {
232  "Error obtaining config from " CONF_FILENAME "\n");
233  }
234  return res;
235 }
All configuration options for ARI.
Definition: internal.h:54
#define ao2_global_obj_ref(holder)
Definition: astobj2.h:925
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
#define CONF_FILENAME

◆ ast_ari_config_init()

int ast_ari_config_init ( void  )

Initialize the ARI configuration.

Definition at line 337 of file res/ari/config.c.

References ACO_EXACT, aco_info_destroy(), aco_info_init(), aco_option_register, aco_option_register_custom, ARI_AUTH_REALM_LEN, ARI_PASSWORD_LEN, AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT_STR, channelvars_handler(), enabled, encoding_format_handler(), FLDSET, NULL, OPT_BOOL_T, OPT_CHAR_ARRAY_T, OPT_INT_T, OPT_NOOP_T, OPT_STRINGFIELD_T, PARSE_IN_RANGE, password, password_format_handler(), process_config(), ast_ari_conf_user::read_only, and STRFLDSET.

Referenced by load_module().

338 {
339  if (aco_info_init(&cfg_info)) {
340  aco_info_destroy(&cfg_info);
341  return -1;
342  }
343 
344  /* ARI general category options */
345  aco_option_register(&cfg_info, "enabled", ACO_EXACT, general_options,
346  "yes", OPT_BOOL_T, 1,
348  aco_option_register_custom(&cfg_info, "pretty", ACO_EXACT,
350  aco_option_register(&cfg_info, "auth_realm", ACO_EXACT, general_options,
351  "Asterisk REST Interface", OPT_CHAR_ARRAY_T, 0,
352  FLDSET(struct ast_ari_conf_general, auth_realm),
354  aco_option_register(&cfg_info, "allowed_origins", ACO_EXACT, general_options,
355  "", OPT_STRINGFIELD_T, 0,
356  STRFLDSET(struct ast_ari_conf_general, allowed_origins));
357  aco_option_register(&cfg_info, "websocket_write_timeout", ACO_EXACT, general_options,
359  FLDSET(struct ast_ari_conf_general, write_timeout), 1, INT_MAX);
360  aco_option_register_custom(&cfg_info, "channelvars", ACO_EXACT, general_options,
361  "", channelvars_handler, 0);
362 
363  /* ARI type=user category options */
364  aco_option_register(&cfg_info, "type", ACO_EXACT, global_user, NULL,
365  OPT_NOOP_T, 0, 0);
366  aco_option_register(&cfg_info, "read_only", ACO_EXACT, global_user,
367  "no", OPT_BOOL_T, 1,
368  FLDSET(struct ast_ari_conf_user, read_only));
369  aco_option_register(&cfg_info, "password", ACO_EXACT, global_user,
370  "", OPT_CHAR_ARRAY_T, 0,
372  aco_option_register_custom(&cfg_info, "password_format", ACO_EXACT,
373  global_user, "plain", password_format_handler, 0);
374 
375  return process_config(0);
376 }
static int password_format_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Parses the ast_ari_password_format enum from a config file.
#define aco_option_register_custom(info, name, matchtype, types, default_val, handler, flags)
Register a config option.
static struct aco_type * general_options[]
#define aco_option_register(info, name, matchtype, types, default_val, opt_type, flags,...)
Register a config option.
static int process_config(int reload)
Load (or reload) configuration.
#define ARI_PASSWORD_LEN
User's password mx length.
Definition: internal.h:93
Type for a default handler that should do nothing.
#define NULL
Definition: resample.c:96
static struct ast_str * password
Definition: cdr_mysql.c:77
Type for default option handler for character array strings.
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
Global configuration options for ARI.
Definition: internal.h:65
static int channelvars_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
int aco_info_init(struct aco_info *info)
Initialize an aco_info structure.
#define ARI_AUTH_REALM_LEN
Definition: internal.h:62
Per-user configuration options.
Definition: internal.h:96
void aco_info_destroy(struct aco_info *info)
Destroy an initialized aco_info struct.
Type for default option handler for bools (ast_true/ast_false)
#define STRFLDSET(type,...)
Convert a struct and a list of stringfield fields to an argument list of field offsets.
#define AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT_STR
Default websocket write timeout, in ms (as a string)
Type for default option handler for stringfields.
Type for default option handler for signed integers.
static int encoding_format_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Encoding format handler converts from boolean to enum.
static struct aco_type * global_user[]
static int enabled
Definition: dnsmgr.c:91

◆ ast_ari_config_reload()

int ast_ari_config_reload ( void  )

Reload the ARI configuration.

Definition at line 378 of file res/ari/config.c.

References process_config().

Referenced by reload_module().

379 {
380  return process_config(1);
381 }
static int process_config(int reload)
Load (or reload) configuration.

◆ ast_ari_config_validate_user()

struct ast_ari_conf_user* ast_ari_config_validate_user ( const char *  username,
const char *  password 
)

Validated a user's credentials.

Parameters
usernameName of the user.
passwordUser's password.
Returns
User object.
NULL if username or password is invalid.

Definition at line 237 of file res/ari/config.c.

References ao2_cleanup, ao2_find, ao2_ref, ARI_PASSWORD_FORMAT_CRYPT, ARI_PASSWORD_FORMAT_PLAIN, ast_ari_config_get(), ast_crypt_validate(), ast_log, ast_strlen_zero, LOG_WARNING, NULL, OBJ_SEARCH_KEY, RAII_VAR, and user.

Referenced by authenticate_api_key(), and authenticate_user().

239 {
242  int is_valid = 0;
243 
245  if (!conf) {
246  return NULL;
247  }
248 
249  user = ao2_find(conf->users, username, OBJ_SEARCH_KEY);
250  if (!user) {
251  return NULL;
252  }
253 
254  if (ast_strlen_zero(user->password)) {
256  "User '%s' missing password; authentication failed\n",
257  user->username);
258  return NULL;
259  }
260 
261  switch (user->password_format) {
263  is_valid = strcmp(password, user->password) == 0;
264  break;
266  is_valid = ast_crypt_validate(password, user->password);
267  break;
268  }
269 
270  if (!is_valid) {
271  return NULL;
272  }
273 
274  ao2_ref(user, +1);
275  return user;
276 }
static char user[512]
struct ast_ari_conf * ast_ari_config_get(void)
Get the current ARI configuration.
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
#define LOG_WARNING
Definition: logger.h:274
All configuration options for ARI.
Definition: internal.h:54
Plaintext password.
Definition: internal.h:83
#define NULL
Definition: resample.c:96
static struct ast_str * password
Definition: cdr_mysql.c:77
#define ast_strlen_zero(foo)
Definition: strings.h:52
All configuration options for statsd client.
Definition: res_statsd.c:95
#define ast_log
Definition: astobj2.c:42
#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:911
#define ao2_ref(o, delta)
Definition: astobj2.h:464
Per-user configuration options.
Definition: internal.h:96
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
structure to hold users read from users.conf
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
int ast_crypt_validate(const char *key, const char *expected)
Asterisk wrapper around crypt(3) for validating passwords.
Definition: crypt.c:136