Switch PJSIP auth to use a vector.

Since Asterisk has a vector API now, places where arrays are manually
resized don't really make sense any more. Since the auth work in PJSIP
was freshly-written, it was easy to reform it to use a vector.

Review: https://reviewboard.asterisk.org/r/3044



git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@403499 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Mark Michelson 2013-12-09 16:10:05 +00:00
parent 8042f4cdd2
commit b18ed67d16
8 changed files with 94 additions and 100 deletions

View File

@ -38,6 +38,8 @@
#include <pjlib.h>
/* Needed for ast_rtp_dtls_cfg struct */
#include "asterisk/rtp_engine.h"
/* Needed for AST_VECTOR macro */
#include "asterisk/vector.h"
/* Forward declarations of PJSIP stuff */
struct pjsip_rx_data;
@ -259,12 +261,7 @@ struct ast_sip_auth {
enum ast_sip_auth_type type;
};
struct ast_sip_auth_array {
/*! Array of Sorcery IDs of auth sections */
const char **names;
/*! Number of credentials in the array */
unsigned int num;
};
AST_VECTOR(ast_sip_auth_vector, const char *);
/*!
* \brief Different methods by which incoming requests can be matched to endpoints
@ -555,9 +552,9 @@ struct ast_sip_endpoint {
/*! Call pickup configuration */
struct ast_sip_endpoint_pickup_configuration pickup;
/*! Inbound authentication credentials */
struct ast_sip_auth_array inbound_auths;
struct ast_sip_auth_vector inbound_auths;
/*! Outbound authentication credentials */
struct ast_sip_auth_array outbound_auths;
struct ast_sip_auth_vector outbound_auths;
/*! DTMF mode to use with this endpoint */
enum ast_sip_dtmf_mode dtmf;
/*! Method(s) by which the endpoint should be identified. */
@ -577,21 +574,21 @@ struct ast_sip_endpoint {
};
/*!
* \brief Initialize an auth array with the configured values.
* \brief Initialize an auth vector with the configured values.
*
* \param array Array to initialize
* \param vector Vector to initialize
* \param auth_names Comma-separated list of names to set in the array
* \retval 0 Success
* \retval non-zero Failure
*/
int ast_sip_auth_array_init(struct ast_sip_auth_array *array, const char *auth_names);
int ast_sip_auth_vector_init(struct ast_sip_auth_vector *vector, const char *auth_names);
/*!
* \brief Free contents of an auth array.
* \brief Free contents of an auth vector.
*
* \param array Array whose contents are to be freed
* \param array Vector whose contents are to be freed
*/
void ast_sip_auth_array_destroy(struct ast_sip_auth_array *array);
void ast_sip_auth_vector_destroy(struct ast_sip_auth_vector *vector);
/*!
* \brief Possible returns from ast_sip_check_authentication
@ -643,14 +640,14 @@ struct ast_sip_outbound_authenticator {
/*!
* \brief Create a new request with authentication credentials
*
* \param auths An array of IDs of auth sorcery objects
* \param auths A vector of IDs of auth sorcery objects
* \param challenge The SIP response with authentication challenge(s)
* \param tsx The transaction in which the challenge was received
* \param new_request The new SIP request with challenge response(s)
* \retval 0 Successfully created new request
* \retval -1 Failed to create a new request
*/
int (*create_request_with_auth)(const struct ast_sip_auth_array *auths, struct pjsip_rx_data *challenge,
int (*create_request_with_auth)(const struct ast_sip_auth_vector *auths, struct pjsip_rx_data *challenge,
struct pjsip_transaction *tsx, struct pjsip_tx_data **new_request);
};
@ -1263,7 +1260,7 @@ enum ast_sip_check_auth_result ast_sip_check_authentication(struct ast_sip_endpo
* callback in the \ref ast_sip_outbound_authenticator structure for details about
* the parameters and return values.
*/
int ast_sip_create_request_with_auth(const struct ast_sip_auth_array *auths, pjsip_rx_data *challenge,
int ast_sip_create_request_with_auth(const struct ast_sip_auth_vector *auths, pjsip_rx_data *challenge,
pjsip_transaction *tsx, pjsip_tx_data **new_request);
/*!
@ -1307,7 +1304,7 @@ int ast_sip_add_body(pjsip_tx_data *tdata, const struct ast_sip_body *body);
/*!
* \brief Add a multipart body to an outbound SIP message
*
* This will treat each part of the input array as part of a multipart body and
* This will treat each part of the input vector as part of a multipart body and
* add each part to the SIP message.
*
* \param tdata The message to add the body to
@ -1374,10 +1371,10 @@ struct ao2_container *ast_sip_get_endpoints(void);
/*!
* \brief Retrieve relevant SIP auth structures from sorcery
*
* \param auths Array of sorcery IDs of auth credentials to retrieve
* \param auths Vector of sorcery IDs of auth credentials to retrieve
* \param[out] out The retrieved auths are stored here
*/
int ast_sip_retrieve_auths(const struct ast_sip_auth_array *auths, struct ast_sip_auth **out);
int ast_sip_retrieve_auths(const struct ast_sip_auth_vector *auths, struct ast_sip_auth **out);
/*!
* \brief Clean up retrieved auth structures from memory
@ -1385,8 +1382,8 @@ int ast_sip_retrieve_auths(const struct ast_sip_auth_array *auths, struct ast_si
* Call this function once you have completed operating on auths
* retrieved from \ref ast_sip_retrieve_auths
*
* \param auths An array of auth structures to clean up
* \param num_auths The number of auths in the array
* \param auths An vector of auth structures to clean up
* \param num_auths The number of auths in the vector
*/
void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths);
@ -1578,7 +1575,7 @@ int ast_sip_for_each_aor(const char *aors, ao2_callback_fn on_aor, void *arg);
* \param arg user data passed to handler
* \retval 0 Success, non-zero on failure
*/
int ast_sip_for_each_auth(const struct ast_sip_auth_array *array,
int ast_sip_for_each_auth(const struct ast_sip_auth_vector *array,
ao2_callback_fn on_auth, void *arg);
/*!
@ -1596,7 +1593,7 @@ const char *ast_sip_auth_type_to_str(enum ast_sip_auth_type type);
* \param buf the string buffer to write the object data
* \retval 0 Success, non-zero on failure
*/
int ast_sip_auths_to_str(const struct ast_sip_auth_array *auths, char **buf);
int ast_sip_auths_to_str(const struct ast_sip_auth_vector *auths, char **buf);
/*
* \brief AMI variable container
@ -1675,7 +1672,7 @@ int ast_sip_format_endpoint_ami(struct ast_sip_endpoint *endpoint,
* \param ami ami variable container
* \retval 0 Success, non-zero on failure
*/
int ast_sip_format_auths_ami(const struct ast_sip_auth_array *auths,
int ast_sip_format_auths_ami(const struct ast_sip_auth_vector *auths,
struct ast_sip_ami *ami);
#endif /* _RES_PJSIP_H */

View File

@ -1194,7 +1194,7 @@ void ast_sip_unregister_outbound_authenticator(struct ast_sip_outbound_authentic
ast_module_unref(ast_module_info->self);
}
int ast_sip_create_request_with_auth(const struct ast_sip_auth_array *auths, pjsip_rx_data *challenge,
int ast_sip_create_request_with_auth(const struct ast_sip_auth_vector *auths, pjsip_rx_data *challenge,
pjsip_transaction *tsx, pjsip_tx_data **new_request)
{
if (!registered_outbound_authenticator) {

View File

@ -118,19 +118,20 @@ static int auth_apply(const struct ast_sorcery *sorcery, void *obj)
return res;
}
int ast_sip_for_each_auth(const struct ast_sip_auth_array *array,
int ast_sip_for_each_auth(const struct ast_sip_auth_vector *vector,
ao2_callback_fn on_auth, void *arg)
{
int i;
if (!array || !array->num) {
if (!vector || !AST_VECTOR_SIZE(vector)) {
return 0;
}
for (i = 0; i < array->num; ++i) {
for (i = 0; i < AST_VECTOR_SIZE(vector); ++i) {
/* AST_VECTOR_GET is safe to use since the vector is immutable */
RAII_VAR(struct ast_sip_auth *, auth, ast_sorcery_retrieve_by_id(
ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE,
array->names[i]), ao2_cleanup);
AST_VECTOR_GET(vector,i)), ao2_cleanup);
if (!auth) {
continue;
@ -175,7 +176,7 @@ static int format_ami_auth_handler(void *obj, void *arg, int flags)
return 0;
}
int ast_sip_format_auths_ami(const struct ast_sip_auth_array *auths,
int ast_sip_format_auths_ami(const struct ast_sip_auth_vector *auths,
struct ast_sip_ami *ami)
{
return ast_sip_for_each_auth(auths, format_ami_auth_handler, ami);

View File

@ -331,55 +331,47 @@ static int timers_to_str(const void *obj, const intptr_t *args, char **buf)
return 0;
}
void ast_sip_auth_array_destroy(struct ast_sip_auth_array *auths)
void ast_sip_auth_vector_destroy(struct ast_sip_auth_vector *auths)
{
int i;
size_t size;
if (!auths) {
return;
}
for (i = 0; i < auths->num; ++i) {
ast_free((char *) auths->names[i]);
size = AST_VECTOR_SIZE(auths);
for (i = 0; i < size; ++i) {
const char *name = AST_VECTOR_REMOVE_UNORDERED(auths, 0);
ast_free((char *) name);
}
ast_free(auths->names);
auths->names = NULL;
auths->num = 0;
AST_VECTOR_FREE(auths);
}
#define AUTH_INCREMENT 4
int ast_sip_auth_array_init(struct ast_sip_auth_array *auths, const char *value)
int ast_sip_auth_vector_init(struct ast_sip_auth_vector *auths, const char *value)
{
char *auth_names = ast_strdupa(value);
char *val;
int num_alloced = 0;
const char **alloced_auths;
ast_assert(auths != NULL);
ast_assert(auths->names == NULL);
ast_assert(!auths->num);
ast_assert(AST_VECTOR_SIZE(auths) == 0);
AST_VECTOR_INIT(auths, 1);
while ((val = strsep(&auth_names, ","))) {
if (auths->num >= num_alloced) {
num_alloced += AUTH_INCREMENT;
alloced_auths = ast_realloc(auths->names, num_alloced * sizeof(char *));
if (!alloced_auths) {
goto failure;
}
auths->names = alloced_auths;
}
val = ast_strdup(val);
if (!val) {
goto failure;
}
auths->names[auths->num] = val;
++auths->num;
if (AST_VECTOR_APPEND(auths, val)) {
goto failure;
}
}
return 0;
failure:
ast_sip_auth_array_destroy(auths);
ast_sip_auth_vector_destroy(auths);
return -1;
}
@ -387,19 +379,19 @@ static int inbound_auth_handler(const struct aco_option *opt, struct ast_variabl
{
struct ast_sip_endpoint *endpoint = obj;
return ast_sip_auth_array_init(&endpoint->inbound_auths, var->value);
return ast_sip_auth_vector_init(&endpoint->inbound_auths, var->value);
}
static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
{
struct ast_sip_endpoint *endpoint = obj;
return ast_sip_auth_array_init(&endpoint->outbound_auths, var->value);
return ast_sip_auth_vector_init(&endpoint->outbound_auths, var->value);
}
int ast_sip_auths_to_str(const struct ast_sip_auth_array *auths, char **buf)
int ast_sip_auths_to_str(const struct ast_sip_auth_vector *auths, char **buf)
{
if (!auths || !auths->num) {
if (!auths || !AST_VECTOR_SIZE(auths)) {
return 0;
}
@ -407,7 +399,8 @@ int ast_sip_auths_to_str(const struct ast_sip_auth_array *auths, char **buf)
return -1;
}
ast_join_delim(*buf, MAX_OBJECT_FIELD, auths->names, auths->num, ',');
/* I feel like accessing the vector's elem array directly is cheating...*/
ast_join_delim(*buf, MAX_OBJECT_FIELD, auths->elems, AST_VECTOR_SIZE(auths), ',');
return 0;
}
@ -1171,7 +1164,7 @@ static int ami_show_endpoint(struct mansession *s, const struct message *m)
return 0;
}
static int format_str_append_auth(const struct ast_sip_auth_array *auths,
static int format_str_append_auth(const struct ast_sip_auth_vector *auths,
struct ast_str **buf)
{
char *str = NULL;
@ -1473,8 +1466,8 @@ static void endpoint_destructor(void* obj)
subscription_configuration_destroy(&endpoint->subscription);
info_configuration_destroy(&endpoint->info);
media_configuration_destroy(&endpoint->media);
ast_sip_auth_array_destroy(&endpoint->inbound_auths);
ast_sip_auth_array_destroy(&endpoint->outbound_auths);
ast_sip_auth_vector_destroy(&endpoint->inbound_auths);
ast_sip_auth_vector_destroy(&endpoint->outbound_auths);
ast_party_id_free(&endpoint->id.self);
endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups);
endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
@ -1535,14 +1528,16 @@ struct ao2_container *ast_sip_get_endpoints(void)
return endpoints;
}
int ast_sip_retrieve_auths(const struct ast_sip_auth_array *auths, struct ast_sip_auth **out)
int ast_sip_retrieve_auths(const struct ast_sip_auth_vector *auths, struct ast_sip_auth **out)
{
int i;
for (i = 0; i < auths->num; ++i) {
out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, auths->names[i]);
for (i = 0; i < AST_VECTOR_SIZE(auths); ++i) {
/* Using AST_VECTOR_GET is safe since the vector is immutable */
const char *name = AST_VECTOR_GET(auths, i);
out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, name);
if (!out[i]) {
ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", auths->names[i]);
ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", name);
return -1;
}
}

View File

@ -227,7 +227,7 @@ static int create_artificial_endpoint(void)
return -1;
}
artificial_endpoint->inbound_auths.num = 1;
AST_VECTOR_INIT(&artificial_endpoint->inbound_auths, 1);
return 0;
}

View File

@ -41,7 +41,7 @@ AO2_GLOBAL_OBJ_STATIC(entity_id);
*/
static int digest_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
{
return endpoint->inbound_auths.num > 0;
return AST_VECTOR_SIZE(&endpoint->inbound_auths) > 0;
}
static void auth_store_cleanup(void *data)
@ -384,12 +384,15 @@ static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint
enum ast_sip_check_auth_result res;
int i;
int failures = 0;
size_t auth_size;
RAII_VAR(struct ast_sip_endpoint *, artificial_endpoint,
ast_sip_get_artificial_endpoint(), ao2_cleanup);
auths = ast_alloca(endpoint->inbound_auths.num * sizeof(*auths));
verify_res = ast_alloca(endpoint->inbound_auths.num * sizeof(*verify_res));
auth_size = AST_VECTOR_SIZE(&endpoint->inbound_auths);
auths = ast_alloca(auth_size * sizeof(*auths));
verify_res = ast_alloca(auth_size * sizeof(*verify_res));
if (!auths) {
return AST_SIP_AUTHENTICATION_ERROR;
@ -402,7 +405,7 @@ static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint
goto cleanup;
}
for (i = 0; i < endpoint->inbound_auths.num; ++i) {
for (i = 0; i < auth_size; ++i) {
if (ast_strlen_zero(auths[i]->realm)) {
ast_string_field_set(auths[i], realm, "asterisk");
}
@ -416,18 +419,18 @@ static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint
}
}
for (i = 0; i < endpoint->inbound_auths.num; ++i) {
for (i = 0; i < auth_size; ++i) {
challenge(auths[i]->realm, tdata, rdata, verify_res[i] == AUTH_STALE);
}
if (failures == endpoint->inbound_auths.num) {
if (failures == auth_size) {
res = AST_SIP_AUTHENTICATION_FAILED;
} else {
res = AST_SIP_AUTHENTICATION_CHALLENGE;
}
cleanup:
ast_sip_cleanup_auths(auths, endpoint->inbound_auths.num);
ast_sip_cleanup_auths(auths, auth_size);
return res;
}

View File

@ -50,15 +50,16 @@ static pjsip_www_authenticate_hdr *get_auth_header(pjsip_rx_data *challenge) {
}
static int set_outbound_authentication_credentials(pjsip_auth_clt_sess *auth_sess,
const struct ast_sip_auth_array *array, pjsip_rx_data *challenge)
const struct ast_sip_auth_vector *auth_vector, pjsip_rx_data *challenge)
{
struct ast_sip_auth **auths = ast_alloca(array->num * sizeof(*auths));
pjsip_cred_info *auth_creds = ast_alloca(array->num * sizeof(*auth_creds));
size_t auth_size = AST_VECTOR_SIZE(auth_vector);
struct ast_sip_auth **auths = ast_alloca(auth_size * sizeof(*auths));
pjsip_cred_info *auth_creds = ast_alloca(auth_size * sizeof(*auth_creds));
pjsip_www_authenticate_hdr *auth_hdr = NULL;
int res = 0;
int i;
if (ast_sip_retrieve_auths(array, auths)) {
if (ast_sip_retrieve_auths(auth_vector, auths)) {
res = -1;
goto cleanup;
}
@ -70,7 +71,7 @@ static int set_outbound_authentication_credentials(pjsip_auth_clt_sess *auth_ses
goto cleanup;
}
for (i = 0; i < array->num; ++i) {
for (i = 0; i < auth_size; ++i) {
if (ast_strlen_zero(auths[i]->realm)) {
auth_creds[i].realm = auth_hdr->challenge.common.realm;
} else {
@ -93,14 +94,14 @@ static int set_outbound_authentication_credentials(pjsip_auth_clt_sess *auth_ses
}
}
pjsip_auth_clt_set_credentials(auth_sess, array->num, auth_creds);
pjsip_auth_clt_set_credentials(auth_sess, auth_size, auth_creds);
cleanup:
ast_sip_cleanup_auths(auths, array->num);
ast_sip_cleanup_auths(auths, auth_size);
return res;
}
static int digest_create_request_with_auth(const struct ast_sip_auth_array *auths, pjsip_rx_data *challenge,
static int digest_create_request_with_auth(const struct ast_sip_auth_vector *auths, pjsip_rx_data *challenge,
pjsip_transaction *tsx, pjsip_tx_data **new_request)
{
pjsip_auth_clt_sess auth_sess;

View File

@ -192,9 +192,7 @@ struct sip_outbound_registration_client_state {
/*! \brief Serializer for stuff and things */
struct ast_taskprocessor *serializer;
/*! \brief Configured authentication credentials */
struct ast_sip_auth_array outbound_auths;
/*! \brief Number of configured auths */
size_t num_outbound_auths;
struct ast_sip_auth_vector outbound_auths;
/*! \brief Registration should be destroyed after completion of transaction */
unsigned int destroy:1;
};
@ -235,9 +233,7 @@ struct sip_outbound_registration {
/*! \brief Outbound registration state */
struct sip_outbound_registration_state *state;
/*! \brief Configured authentication credentials */
struct ast_sip_auth_array outbound_auths;
/*! \brief Number of configured auths */
size_t num_outbound_auths;
struct ast_sip_auth_vector outbound_auths;
};
/*! \brief Helper function which cancels the timer on a client */
@ -334,7 +330,7 @@ static int handle_client_state_destruction(void *data)
pjsip_regc_destroy(client_state->client);
client_state->status = SIP_REGISTRATION_STOPPED;
ast_sip_auth_array_destroy(&client_state->outbound_auths);
ast_sip_auth_vector_destroy(&client_state->outbound_auths);
return 0;
}
@ -564,7 +560,7 @@ static void sip_outbound_registration_destroy(void *obj)
struct sip_outbound_registration *registration = obj;
ao2_cleanup(registration->state);
ast_sip_auth_array_destroy(&registration->outbound_auths);
ast_sip_auth_vector_destroy(&registration->outbound_auths);
ast_string_field_free_memory(registration);
}
@ -657,13 +653,14 @@ static int can_reuse_registration(struct sip_outbound_registration *existing, st
if (strcmp(existing->server_uri, applied->server_uri) || strcmp(existing->client_uri, applied->client_uri) ||
strcmp(existing->transport, applied->transport) || strcmp(existing->contact_user, applied->contact_user) ||
strcmp(existing->outbound_proxy, applied->outbound_proxy) || existing->num_outbound_auths != applied->num_outbound_auths ||
strcmp(existing->outbound_proxy, applied->outbound_proxy) ||
AST_VECTOR_SIZE(&existing->outbound_auths) != AST_VECTOR_SIZE(&applied->outbound_auths) ||
existing->auth_rejection_permanent != applied->auth_rejection_permanent) {
return 0;
}
for (i = 0; i < existing->num_outbound_auths; ++i) {
if (strcmp(existing->outbound_auths.names[i], applied->outbound_auths.names[i])) {
for (i = 0; i < AST_VECTOR_SIZE(&existing->outbound_auths); ++i) {
if (strcmp(AST_VECTOR_GET(&existing->outbound_auths, i), AST_VECTOR_GET(&applied->outbound_auths, i))) {
return 0;
}
}
@ -764,13 +761,13 @@ static int sip_outbound_registration_perform(void *data)
size_t i;
/* Just in case the client state is being reused for this registration, free the auth information */
ast_sip_auth_array_destroy(&registration->state->client_state->outbound_auths);
ast_sip_auth_vector_destroy(&registration->state->client_state->outbound_auths);
registration->state->client_state->outbound_auths.names = ast_calloc(registration->outbound_auths.num, sizeof(char *));
for (i = 0; i < registration->outbound_auths.num; ++i) {
registration->state->client_state->outbound_auths.names[i] = ast_strdup(registration->outbound_auths.names[i]);
AST_VECTOR_INIT(&registration->state->client_state->outbound_auths, AST_VECTOR_SIZE(&registration->outbound_auths));
for (i = 0; i < AST_VECTOR_SIZE(&registration->outbound_auths); ++i) {
const char *name = ast_strdup(AST_VECTOR_GET(&registration->outbound_auths, i));
AST_VECTOR_APPEND(&registration->state->client_state->outbound_auths, name);
}
registration->state->client_state->outbound_auths.num = registration->outbound_auths.num;
registration->state->client_state->retry_interval = registration->retry_interval;
registration->state->client_state->forbidden_retry_interval = registration->forbidden_retry_interval;
registration->state->client_state->max_retries = registration->max_retries;
@ -808,7 +805,7 @@ static int outbound_auth_handler(const struct aco_option *opt, struct ast_variab
{
struct sip_outbound_registration *registration = obj;
return ast_sip_auth_array_init(&registration->outbound_auths, var->value);
return ast_sip_auth_vector_init(&registration->outbound_auths, var->value);
}
static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)