config_auth.c File Reference

#include "asterisk.h"
#include <pjsip.h>
#include <pjlib.h>
#include "asterisk/res_pjsip.h"
#include "asterisk/logger.h"
#include "asterisk/sorcery.h"
#include "asterisk/cli.h"
#include "include/res_pjsip_private.h"
#include "asterisk/res_pjsip_cli.h"

Include dependency graph for config_auth.c:

Go to the source code of this file.

Functions

const char * ast_sip_auth_type_to_str (enum ast_sip_auth_type type)
 Converts the given auth type to a string.
int ast_sip_destroy_sorcery_auth (void)
int ast_sip_for_each_auth (const struct ast_sip_auth_vector *vector, ao2_callback_fn on_auth, void *arg)
 For every auth in the array call the given 'on_auth' handler.
int ast_sip_format_auths_ami (const struct ast_sip_auth_vector *auths, struct ast_sip_ami *ami)
 Format auth details for AMI.
int ast_sip_initialize_sorcery_auth (void)
 Initialize sorcery with auth support.
static void * auth_alloc (const char *name)
static int auth_apply (const struct ast_sorcery *sorcery, void *obj)
static void auth_destroy (void *obj)
static int auth_type_handler (const struct aco_option *opt, struct ast_variable *var, void *obj)
static int auth_type_to_str (const void *obj, const intptr_t *args, char **buf)
static struct ao2_containercli_get_container (void)
static int cli_iterator (void *container, ao2_callback_fn callback, void *args)
static int cli_print_body (void *obj, void *arg, int flags)
static int cli_print_header (void *obj, void *arg, int flags)
static void * cli_retrieve_by_id (const char *id)
static int format_ami_auth_handler (void *obj, void *arg, int flags)
static int format_ami_endpoint_auth (const struct ast_sip_endpoint *endpoint, struct ast_sip_ami *ami)
static int sip_auth_to_ami (const struct ast_sip_auth *auth, struct ast_str **buf)

Variables

static const char * auth_types_map []
static struct ast_cli_entry cli_commands []
static struct
ast_sip_cli_formatter_entry
cli_formatter
static struct
ast_sip_endpoint_formatter 
endpoint_auth_formatter


Function Documentation

const char* ast_sip_auth_type_to_str ( enum ast_sip_auth_type  type  ) 

Converts the given auth type to a string.

Parameters:
type the auth type to convert
Return values:
a string representative of the auth type

Definition at line 72 of file config_auth.c.

References ARRAY_IN_BOUNDS, and auth_types_map.

Referenced by auth_type_to_str().

00073 {
00074    return ARRAY_IN_BOUNDS(type, auth_types_map) ?
00075       auth_types_map[type] : "";
00076 }

int ast_sip_destroy_sorcery_auth ( void   ) 

int ast_sip_for_each_auth ( const struct ast_sip_auth_vector *  array,
ao2_callback_fn  on_auth,
void *  arg 
)

For every auth in the array call the given 'on_auth' handler.

Parameters:
array an array of auths
on_auth callback for each auth
arg user data passed to handler
Return values:
0 Success, non-zero on failure

Definition at line 117 of file config_auth.c.

References ao2_cleanup, ast_sip_get_sorcery(), ast_sorcery_retrieve_by_id(), AST_VECTOR_GET, AST_VECTOR_SIZE, RAII_VAR, and SIP_SORCERY_AUTH_TYPE.

Referenced by ast_sip_format_auths_ami(), and cli_iterator().

00119 {
00120    int i;
00121 
00122    if (!vector || !AST_VECTOR_SIZE(vector)) {
00123       return 0;
00124    }
00125 
00126    for (i = 0; i < AST_VECTOR_SIZE(vector); ++i) {
00127       /* AST_VECTOR_GET is safe to use since the vector is immutable */
00128       RAII_VAR(struct ast_sip_auth *, auth, ast_sorcery_retrieve_by_id(
00129              ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE,
00130              AST_VECTOR_GET(vector,i)), ao2_cleanup);
00131 
00132       if (!auth) {
00133          continue;
00134       }
00135 
00136       if (on_auth(auth, arg, 0)) {
00137          return -1;
00138       }
00139    }
00140 
00141    return 0;
00142 }

int ast_sip_format_auths_ami ( const struct ast_sip_auth_vector *  auths,
struct ast_sip_ami ami 
)

Format auth details for AMI.

Parameters:
auths an auth array
ami ami variable container
Return values:
0 Success, non-zero on failure

Definition at line 177 of file config_auth.c.

References ast_sip_for_each_auth(), and format_ami_auth_handler().

Referenced by ami_outbound_registration_task(), and format_ami_endpoint_auth().

00179 {
00180    return ast_sip_for_each_auth(auths, format_ami_auth_handler, ami);
00181 }

int ast_sip_initialize_sorcery_auth ( void   ) 

Initialize sorcery with auth support.

Definition at line 290 of file config_auth.c.

References ao2_alloc, ARRAY_LEN, ast_cli_register_multiple(), ast_log, ast_sip_get_sorcery(), ast_sip_register_cli_formatter(), ast_sorcery_apply_default, ast_sorcery_object_field_register, ast_sorcery_object_field_register_custom, ast_sorcery_object_get_id(), ast_sorcery_object_register, auth_alloc(), auth_apply(), auth_type_handler(), auth_type_to_str(), cli_get_container(), cli_iterator(), cli_print_body(), cli_print_header(), cli_retrieve_by_id(), FLDSET, ast_sip_cli_formatter_entry::get_container, ast_sip_cli_formatter_entry::get_id, internal_sip_register_endpoint_formatter(), ast_sip_cli_formatter_entry::iterate, LOG_ERROR, ast_sip_cli_formatter_entry::name, NULL, OPT_NOOP_T, OPT_STRINGFIELD_T, OPT_UINT_T, ast_sip_cli_formatter_entry::print_body, ast_sip_cli_formatter_entry::print_header, ast_sip_cli_formatter_entry::retrieve_by_id, SIP_SORCERY_AUTH_TYPE, sorcery, and STRFLDSET.

Referenced by ast_res_pjsip_initialize_configuration().

00291 {
00292    struct ast_sorcery *sorcery = ast_sip_get_sorcery();
00293 
00294    ast_sorcery_apply_default(sorcery, SIP_SORCERY_AUTH_TYPE, "config", "pjsip.conf,criteria=type=auth");
00295 
00296    if (ast_sorcery_object_register(sorcery, SIP_SORCERY_AUTH_TYPE, auth_alloc, NULL, auth_apply)) {
00297       return -1;
00298    }
00299 
00300    ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "type", "",
00301          OPT_NOOP_T, 0, 0);
00302    ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "username",
00303          "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, auth_user));
00304    ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "password",
00305          "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, auth_pass));
00306    ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "md5_cred",
00307          "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, md5_creds));
00308    ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "realm",
00309          "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, realm));
00310    ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "nonce_lifetime",
00311          "32", OPT_UINT_T, 0, FLDSET(struct ast_sip_auth, nonce_lifetime));
00312    ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_AUTH_TYPE, "auth_type",
00313          "userpass", auth_type_handler, auth_type_to_str, NULL, 0, 0);
00314 
00315    internal_sip_register_endpoint_formatter(&endpoint_auth_formatter);
00316 
00317    cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
00318    if (!cli_formatter) {
00319       ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n");
00320       return -1;
00321    }
00322    cli_formatter->name = SIP_SORCERY_AUTH_TYPE;
00323    cli_formatter->print_header = cli_print_header;
00324    cli_formatter->print_body = cli_print_body;
00325    cli_formatter->get_container = cli_get_container;
00326    cli_formatter->iterate = cli_iterator;
00327    cli_formatter->get_id = ast_sorcery_object_get_id;
00328    cli_formatter->retrieve_by_id = cli_retrieve_by_id;
00329 
00330    ast_sip_register_cli_formatter(cli_formatter);
00331    ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
00332 
00333    return 0;
00334 }

static void* auth_alloc ( const char *  name  )  [static]

Definition at line 36 of file config_auth.c.

References ao2_cleanup, ast_sorcery_generic_alloc(), ast_string_field_init, auth_destroy(), and NULL.

Referenced by ast_sip_initialize_sorcery_auth().

00037 {
00038    struct ast_sip_auth *auth = ast_sorcery_generic_alloc(sizeof(*auth), auth_destroy);
00039 
00040    if (!auth) {
00041       return NULL;
00042    }
00043 
00044    if (ast_string_field_init(auth, 64)) {
00045       ao2_cleanup(auth);
00046       return NULL;
00047    }
00048 
00049    return auth;
00050 }

static int auth_apply ( const struct ast_sorcery sorcery,
void *  obj 
) [static]

Definition at line 85 of file config_auth.c.

References ast_log, AST_SIP_AUTH_TYPE_ARTIFICIAL, AST_SIP_AUTH_TYPE_MD5, AST_SIP_AUTH_TYPE_USER_PASS, ast_sorcery_object_get_id(), ast_strlen_zero, ast_sip_auth::auth_user, LOG_ERROR, ast_sip_auth::md5_creds, and ast_sip_auth::type.

Referenced by ast_sip_initialize_sorcery_auth().

00086 {
00087    struct ast_sip_auth *auth = obj;
00088    int res = 0;
00089 
00090    if (ast_strlen_zero(auth->auth_user)) {
00091       ast_log(LOG_ERROR, "No authentication username for auth '%s'\n",
00092             ast_sorcery_object_get_id(auth));
00093       return -1;
00094    }
00095 
00096    switch (auth->type) {
00097    case AST_SIP_AUTH_TYPE_MD5:
00098       if (ast_strlen_zero(auth->md5_creds)) {
00099          ast_log(LOG_ERROR, "'md5' authentication specified but no md5_cred "
00100                "specified for auth '%s'\n", ast_sorcery_object_get_id(auth));
00101          res = -1;
00102       } else if (strlen(auth->md5_creds) != PJSIP_MD5STRLEN) {
00103          ast_log(LOG_ERROR, "'md5' authentication requires digest of size '%d', but "
00104             "digest is '%d' in size for auth '%s'\n", PJSIP_MD5STRLEN, (int)strlen(auth->md5_creds),
00105             ast_sorcery_object_get_id(auth));
00106          res = -1;
00107       }
00108       break;
00109    case AST_SIP_AUTH_TYPE_USER_PASS:
00110    case AST_SIP_AUTH_TYPE_ARTIFICIAL:
00111       break;
00112    }
00113 
00114    return res;
00115 }

static void auth_destroy ( void *  obj  )  [static]

Definition at line 30 of file config_auth.c.

References ast_string_field_free_memory.

Referenced by auth_alloc().

00031 {
00032    struct ast_sip_auth *auth = obj;
00033    ast_string_field_free_memory(auth);
00034 }

static int auth_type_handler ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
) [static]

Definition at line 52 of file config_auth.c.

References ast_log, AST_SIP_AUTH_TYPE_MD5, AST_SIP_AUTH_TYPE_USER_PASS, LOG_WARNING, ast_variable::name, ast_sip_auth::type, and ast_variable::value.

Referenced by ast_sip_initialize_sorcery_auth().

00053 {
00054    struct ast_sip_auth *auth = obj;
00055    if (!strcasecmp(var->value, "userpass")) {
00056       auth->type = AST_SIP_AUTH_TYPE_USER_PASS;
00057    } else if (!strcasecmp(var->value, "md5")) {
00058       auth->type = AST_SIP_AUTH_TYPE_MD5;
00059    } else {
00060       ast_log(LOG_WARNING, "Unknown authentication storage type '%s' specified for %s\n",
00061             var->value, var->name);
00062       return -1;
00063    }
00064    return 0;
00065 }

static int auth_type_to_str ( const void *  obj,
const intptr_t *  args,
char **  buf 
) [static]

Definition at line 78 of file config_auth.c.

References ast_sip_auth_type_to_str(), ast_strdup, and ast_sip_auth::type.

Referenced by ast_sip_initialize_sorcery_auth().

00079 {
00080    const struct ast_sip_auth *auth = obj;
00081    *buf = ast_strdup(ast_sip_auth_type_to_str(auth->type));
00082    return 0;
00083 }

static struct ao2_container* cli_get_container ( void   )  [static, read]

static int cli_iterator ( void *  container,
ao2_callback_fn  callback,
void *  args 
) [static]

Definition at line 223 of file config_auth.c.

References ast_sip_for_each_auth().

Referenced by ast_sip_initialize_sorcery_auth(), cli_endpoint_gather_identifies(), and load_module().

00224 {
00225    return ast_sip_for_each_auth(container, callback, args);
00226 }

static int cli_print_body ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 248 of file config_auth.c.

References ast_assert, ast_sip_cli_print_sorcery_objectset(), ast_sorcery_object_get_id(), ast_str_append(), ast_sip_cli_context::auth_direction, ast_sip_auth::auth_user, CLI_INDENT_TO_SPACES, context, ast_sip_cli_context::indent_level, NULL, ast_sip_cli_context::output_buffer, ast_sip_cli_context::show_details, and ast_sip_cli_context::show_details_only_level_0.

Referenced by ast_sip_initialize_sorcery_auth(), ast_sip_initialize_sorcery_transport(), and load_module().

00249 {
00250    struct ast_sip_auth *auth = obj;
00251    struct ast_sip_cli_context *context = arg;
00252    char title[32];
00253 
00254    ast_assert(context->output_buffer != NULL);
00255 
00256    snprintf(title, sizeof(title), "%sAuth",
00257       context->auth_direction ? context->auth_direction : "");
00258 
00259    ast_str_append(&context->output_buffer, 0, "%*s:  %s/%s\n",
00260       CLI_INDENT_TO_SPACES(context->indent_level), title,
00261       ast_sorcery_object_get_id(auth), auth->auth_user);
00262 
00263    if (context->show_details
00264       || (context->show_details_only_level_0 && context->indent_level == 0)) {
00265       ast_str_append(&context->output_buffer, 0, "\n");
00266       ast_sip_cli_print_sorcery_objectset(auth, context, 0);
00267    }
00268 
00269    return 0;
00270 }

static int cli_print_header ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 233 of file config_auth.c.

References ast_assert, ast_str_append(), CLI_HEADER_FILLER, CLI_INDENT_TO_SPACES, CLI_MAX_WIDTH, context, ast_sip_cli_context::indent_level, NULL, and ast_sip_cli_context::output_buffer.

Referenced by ast_sip_initialize_sorcery_auth(), ast_sip_initialize_sorcery_transport(), and load_module().

00234 {
00235    struct ast_sip_cli_context *context = arg;
00236    int indent = CLI_INDENT_TO_SPACES(context->indent_level);
00237    int filler = CLI_MAX_WIDTH - indent - 20;
00238 
00239    ast_assert(context->output_buffer != NULL);
00240 
00241    ast_str_append(&context->output_buffer, 0,
00242       "%*s:  <AuthId/UserName%*.*s>\n", indent, "I/OAuth", filler, filler,
00243       CLI_HEADER_FILLER);
00244 
00245    return 0;
00246 }

static void* cli_retrieve_by_id ( const char *  id  )  [static]

static int format_ami_auth_handler ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 150 of file config_auth.c.

References ast_sip_ami::arg, ast_free, ast_sip_create_ami_event(), ast_sorcery_object_get_id(), ast_str_append(), ast_str_buffer(), astman_append(), buf, ast_sip_ami::count, RAII_VAR, ast_sip_ami::s, and sip_auth_to_ami().

Referenced by ast_sip_format_auths_ami().

00151 {
00152    const struct ast_sip_auth *auth = obj;
00153    struct ast_sip_ami *ami = arg;
00154    const struct ast_sip_endpoint *endpoint = ami->arg;
00155    RAII_VAR(struct ast_str *, buf,
00156        ast_sip_create_ami_event("AuthDetail", ami), ast_free);
00157 
00158    if (!buf) {
00159       return -1;
00160    }
00161 
00162    if (sip_auth_to_ami(auth, &buf)) {
00163       return -1;
00164    }
00165 
00166    if (endpoint) {
00167       ast_str_append(&buf, 0, "EndpointName: %s\r\n",
00168              ast_sorcery_object_get_id(endpoint));
00169    }
00170 
00171    astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
00172    ami->count++;
00173 
00174    return 0;
00175 }

static int format_ami_endpoint_auth ( const struct ast_sip_endpoint endpoint,
struct ast_sip_ami ami 
) [static]

Definition at line 183 of file config_auth.c.

References ast_sip_ami::arg, ast_sip_format_auths_ami(), ast_sip_endpoint::inbound_auths, and ast_sip_endpoint::outbound_auths.

00185 {
00186    ami->arg = (void *)endpoint;
00187    if (ast_sip_format_auths_ami(&endpoint->inbound_auths, ami)) {
00188       return -1;
00189    }
00190 
00191    return ast_sip_format_auths_ami(&endpoint->outbound_auths, ami);
00192 }

static int sip_auth_to_ami ( const struct ast_sip_auth auth,
struct ast_str **  buf 
) [static]

Definition at line 144 of file config_auth.c.

References ast_sip_sorcery_object_to_ami().

Referenced by format_ami_auth_handler().

00146 {
00147    return ast_sip_sorcery_object_to_ami(auth, buf);
00148 }


Variable Documentation

const char* auth_types_map[] [static]

Initial value:

 {
   [AST_SIP_AUTH_TYPE_USER_PASS] = "userpass",
   [AST_SIP_AUTH_TYPE_MD5] = "md5"
}

Definition at line 67 of file config_auth.c.

Referenced by ast_sip_auth_type_to_str().

struct ast_cli_entry cli_commands[] [static]

Definition at line 272 of file config_auth.c.

Definition at line 287 of file config_auth.c.

Initial value:

 {
   .format_ami = format_ami_endpoint_auth
}

Definition at line 194 of file config_auth.c.


Generated on Thu Apr 16 06:30:26 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6