| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "asterisk.h" |
| 20 |
|
| 21 |
#include <pjsip.h> |
| 22 |
#include <pjlib.h> |
| 23 |
#include "asterisk/res_pjsip.h" |
| 24 |
#include "asterisk/logger.h" |
| 25 |
#include "asterisk/sorcery.h" |
| 26 |
#include "asterisk/cli.h" |
| 27 |
#include "asterisk/vector.h" |
| 28 |
#include "include/res_pjsip_private.h" |
| 29 |
#include "asterisk/res_pjsip_cli.h" |
| 30 |
|
| 31 |
#ifndef HAVE_PJSIP_AUTH_NEW_DIGESTS |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#ifdef HAVE_OPENSSL |
| 39 |
#include "openssl/md5.h" |
| 40 |
#include "openssl/sha.h" |
| 41 |
#else |
| 42 |
#define MD5_DIGEST_LENGTH 16 |
| 43 |
#define SHA256_DIGEST_LENGTH 32 |
| 44 |
#endif |
| 45 |
|
| 46 |
const pjsip_auth_algorithm pjsip_auth_algorithms[] = { |
| 47 |
|
| 48 |
|
| 49 |
{ PJSIP_AUTH_ALGORITHM_NOT_SET, {"", 0}, "", |
| 50 |
0, 0}, |
| 51 |
{ PJSIP_AUTH_ALGORITHM_MD5, {"MD5", 3}, "MD5", |
| 52 |
MD5_DIGEST_LENGTH, MD5_DIGEST_LENGTH * 2}, |
| 53 |
{ PJSIP_AUTH_ALGORITHM_SHA256, {"SHA-256", 7}, "SHA256", |
| 54 |
SHA256_DIGEST_LENGTH, SHA256_DIGEST_LENGTH * 2}, |
| 55 |
{ PJSIP_AUTH_ALGORITHM_SHA512_256, {"SHA-512-256", 11}, "SHA512-256", |
| 56 |
SHA256_DIGEST_LENGTH, SHA256_DIGEST_LENGTH * 2}, |
| 57 |
{ PJSIP_AUTH_ALGORITHM_AKAV1_MD5, {"AKAv1-MD5", 9}, "", |
| 58 |
MD5_DIGEST_LENGTH, MD5_DIGEST_LENGTH * 2}, |
| 59 |
{ PJSIP_AUTH_ALGORITHM_AKAV1_MD5, {"AKAv2-MD5", 9}, "", |
| 60 |
MD5_DIGEST_LENGTH, MD5_DIGEST_LENGTH * 2}, |
| 61 |
{ PJSIP_AUTH_ALGORITHM_COUNT, {"", 0}, "", |
| 62 |
0, 0}, |
| 63 |
}; |
| 64 |
#endif |
| 65 |
|
| 66 |
const pjsip_auth_algorithm *ast_sip_auth_get_algorithm_by_type( |
| 67 |
pjsip_auth_algorithm_type algorithm_type) |
| 68 |
{ |
| 69 |
#ifdef HAVE_PJSIP_AUTH_NEW_DIGESTS |
| 70 |
return pjsip_auth_get_algorithm_by_type(algorithm_type); |
| 71 |
#else |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
if (algorithm_type == PJSIP_AUTH_ALGORITHM_MD5) { |
| 77 |
return &pjsip_auth_algorithms[algorithm_type]; |
| 78 |
} |
| 79 |
return NULL; |
| 80 |
#endif |
| 81 |
} |
| 82 |
|
| 83 |
const pjsip_auth_algorithm *ast_sip_auth_get_algorithm_by_iana_name( |
| 84 |
const pj_str_t *iana_name) |
| 85 |
{ |
| 86 |
#ifdef HAVE_PJSIP_AUTH_NEW_DIGESTS |
| 87 |
return pjsip_auth_get_algorithm_by_iana_name(iana_name); |
| 88 |
#else |
| 89 |
if (!iana_name) { |
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
if (iana_name->slen == 0 || pj_stricmp2(iana_name, "MD5") == 0) { |
| 98 |
return &pjsip_auth_algorithms[PJSIP_AUTH_ALGORITHM_MD5]; |
| 99 |
} |
| 100 |
return NULL; |
| 101 |
#endif |
| 102 |
} |
| 103 |
|
| 104 |
pj_bool_t ast_sip_auth_is_algorithm_supported( |
| 105 |
pjsip_auth_algorithm_type algorithm_type) |
| 106 |
{ |
| 107 |
#ifdef HAVE_PJSIP_AUTH_NEW_DIGESTS |
| 108 |
return pjsip_auth_is_algorithm_supported(algorithm_type); |
| 109 |
#else |
| 110 |
return algorithm_type == PJSIP_AUTH_ALGORITHM_MD5; |
| 111 |
#endif |
| 112 |
} |
| 113 |
|
| 114 |
static void auth_destroy(void *obj) |
| 115 |
{ |
| 116 |
struct ast_sip_auth *auth = obj; |
| 117 |
int i = 0; |
| 118 |
|
| 119 |
ast_string_field_free_memory(auth); |
| 120 |
|
| 121 |
for (i = PJSIP_AUTH_ALGORITHM_NOT_SET + 1; i < PJSIP_AUTH_ALGORITHM_COUNT; i++) { |
| 122 |
ast_free(auth->password_digests[i]); |
| 123 |
} |
| 124 |
|
| 125 |
AST_VECTOR_FREE(&auth->supported_algorithms_uac); |
| 126 |
AST_VECTOR_FREE(&auth->supported_algorithms_uas); |
| 127 |
} |
| 128 |
|
| 129 |
static void *auth_alloc(const char *name) |
| 130 |
{ |
| 131 |
struct ast_sip_auth *auth = ast_sorcery_generic_alloc(sizeof(*auth), auth_destroy); |
| 132 |
|
| 133 |
if (!auth) { |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
if (ast_string_field_init(auth, 64)) { |
| 138 |
ao2_cleanup(auth); |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
|
| 142 |
AST_VECTOR_INIT(&auth->supported_algorithms_uac, 0); |
| 143 |
AST_VECTOR_INIT(&auth->supported_algorithms_uas, 0); |
| 144 |
|
| 145 |
return auth; |
| 146 |
} |
| 147 |
|
| 148 |
static int auth_type_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 149 |
{ |
| 150 |
struct ast_sip_auth *auth = obj; |
| 151 |
if (!strcasecmp(var->value, "userpass")) { |
| 152 |
auth->type = AST_SIP_AUTH_TYPE_USER_PASS; |
| 153 |
} else if (!strcasecmp(var->value, "md5")) { |
| 154 |
auth->type = AST_SIP_AUTH_TYPE_MD5; |
| 155 |
} else if (!strcasecmp(var->value, "digest")) { |
| 156 |
auth->type = AST_SIP_AUTH_TYPE_DIGEST; |
| 157 |
} else if (!strcasecmp(var->value, "google_oauth")) { |
| 158 |
#ifdef HAVE_PJSIP_OAUTH_AUTHENTICATION |
| 159 |
auth->type = AST_SIP_AUTH_TYPE_GOOGLE_OAUTH; |
| 160 |
#else |
| 161 |
ast_log(LOG_WARNING, "OAuth support is not available in the version of PJSIP in use\n"); |
| 162 |
return -1; |
| 163 |
#endif |
| 164 |
} else { |
| 165 |
ast_log(LOG_WARNING, "Unknown authentication storage type '%s' specified for %s\n", |
| 166 |
var->value, var->name); |
| 167 |
return -1; |
| 168 |
} |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
static const char *auth_types_map[] = { |
| 173 |
[AST_SIP_AUTH_TYPE_USER_PASS] = "userpass", |
| 174 |
[AST_SIP_AUTH_TYPE_MD5] = "md5", |
| 175 |
[AST_SIP_AUTH_TYPE_DIGEST] = "digest", |
| 176 |
[AST_SIP_AUTH_TYPE_GOOGLE_OAUTH] = "google_oauth" |
| 177 |
}; |
| 178 |
|
| 179 |
const char *ast_sip_auth_type_to_str(enum ast_sip_auth_type type) |
| 180 |
{ |
| 181 |
return ARRAY_IN_BOUNDS(type, auth_types_map) ? |
| 182 |
auth_types_map[type] : ""; |
| 183 |
} |
| 184 |
|
| 185 |
static int auth_type_to_str(const void *obj, const intptr_t *args, char **buf) |
| 186 |
{ |
| 187 |
const struct ast_sip_auth *auth = obj; |
| 188 |
*buf = ast_strdup(ast_sip_auth_type_to_str(auth->type)); |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
int ast_sip_auth_digest_algorithms_vector_init(const char *id, |
| 193 |
struct pjsip_auth_algorithm_type_vector *algorithms, const char *agent_type, |
| 194 |
const char *value) |
| 195 |
{ |
| 196 |
char *iana_names = ast_strdupa(value); |
| 197 |
pj_str_t val; |
| 198 |
int res = 0; |
| 199 |
|
| 200 |
ast_assert(algorithms != NULL); |
| 201 |
|
| 202 |
while ((val.ptr = ast_strip(strsep(&iana_names, ",")))) { |
| 203 |
const pjsip_auth_algorithm *algo; |
| 204 |
|
| 205 |
if (ast_strlen_zero(val.ptr)) { |
| 206 |
continue; |
| 207 |
} |
| 208 |
val.slen = strlen(val.ptr); |
| 209 |
|
| 210 |
algo = ast_sip_auth_get_algorithm_by_iana_name(&val); |
| 211 |
if (!algo) { |
| 212 |
ast_log(LOG_WARNING, "%s: Unknown %s digest algorithm '%s' specified\n", |
| 213 |
id, agent_type, val.ptr); |
| 214 |
res = -1; |
| 215 |
continue; |
| 216 |
} |
| 217 |
if (!ast_sip_auth_is_algorithm_supported(algo->algorithm_type)) { |
| 218 |
ast_log(LOG_WARNING, "%s: %s digest algorithm '%s' is not supported by the version of OpenSSL in use\n", |
| 219 |
id, agent_type, val.ptr); |
| 220 |
res = -1; |
| 221 |
continue; |
| 222 |
} |
| 223 |
|
| 224 |
if (AST_VECTOR_APPEND(algorithms, algo->algorithm_type)) { |
| 225 |
AST_VECTOR_FREE(algorithms); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
} |
| 229 |
return res; |
| 230 |
} |
| 231 |
|
| 232 |
static int uac_algorithms_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 233 |
{ |
| 234 |
struct ast_sip_auth *auth = obj; |
| 235 |
|
| 236 |
return ast_sip_auth_digest_algorithms_vector_init(ast_sorcery_object_get_id(auth), |
| 237 |
&auth->supported_algorithms_uac, "UAC", var->value); |
| 238 |
} |
| 239 |
|
| 240 |
static int uas_algorithms_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 241 |
{ |
| 242 |
struct ast_sip_auth *auth = obj; |
| 243 |
|
| 244 |
return ast_sip_auth_digest_algorithms_vector_init(ast_sorcery_object_get_id(auth), |
| 245 |
&auth->supported_algorithms_uas, "UAS", var->value); |
| 246 |
} |
| 247 |
|
| 248 |
int ast_sip_auth_digest_algorithms_vector_to_str( |
| 249 |
const struct pjsip_auth_algorithm_type_vector *algorithms, char **buf) |
| 250 |
{ |
| 251 |
struct ast_str *str = NULL; |
| 252 |
int i = 0; |
| 253 |
|
| 254 |
if (!algorithms || !AST_VECTOR_SIZE(algorithms)) { |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
str = ast_str_alloca(256); |
| 259 |
if (!str) { |
| 260 |
return -1; |
| 261 |
} |
| 262 |
|
| 263 |
for (i = 0; i < AST_VECTOR_SIZE(algorithms); ++i) { |
| 264 |
const pjsip_auth_algorithm *algo = ast_sip_auth_get_algorithm_by_type( |
| 265 |
AST_VECTOR_GET(algorithms, i)); |
| 266 |
ast_str_append(&str, 0, "%s" PJSTR_PRINTF_SPEC, i > 0 ? "," : "", |
| 267 |
PJSTR_PRINTF_VAR(algo->iana_name)); |
| 268 |
} |
| 269 |
|
| 270 |
*buf = ast_strdup(ast_str_buffer(str)); |
| 271 |
|
| 272 |
return *buf ? 0 : -1; |
| 273 |
} |
| 274 |
|
| 275 |
static int uac_algorithms_to_str(const void *obj, const intptr_t *args, char **buf) |
| 276 |
{ |
| 277 |
const struct ast_sip_auth *auth = obj; |
| 278 |
return ast_sip_auth_digest_algorithms_vector_to_str(&auth->supported_algorithms_uac, buf); |
| 279 |
} |
| 280 |
|
| 281 |
static int uas_algorithms_to_str(const void *obj, const intptr_t *args, char **buf) |
| 282 |
{ |
| 283 |
const struct ast_sip_auth *auth = obj; |
| 284 |
return ast_sip_auth_digest_algorithms_vector_to_str(&auth->supported_algorithms_uas, buf); |
| 285 |
} |
| 286 |
|
| 287 |
static int password_digest_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 288 |
{ |
| 289 |
struct ast_sip_auth *auth = obj; |
| 290 |
const char *auth_name = ast_sorcery_object_get_id(auth); |
| 291 |
char *value = ast_strdupa(var->value); |
| 292 |
char *unparsed_digest = NULL; |
| 293 |
|
| 294 |
while ((unparsed_digest = ast_strsep(&value, ',', AST_STRSEP_TRIM))) { |
| 295 |
const pjsip_auth_algorithm *algo; |
| 296 |
char *iana_name; |
| 297 |
char *digest; |
| 298 |
struct ast_sip_auth_password_digest *pw; |
| 299 |
pj_str_t pj_iana_name; |
| 300 |
|
| 301 |
if (ast_strlen_zero(unparsed_digest)) { |
| 302 |
continue; |
| 303 |
} |
| 304 |
|
| 305 |
if (strchr(unparsed_digest, ':') != NULL) { |
| 306 |
iana_name = ast_strsep(&unparsed_digest, ':', AST_STRSEP_TRIM); |
| 307 |
} else { |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
iana_name = "MD5"; |
| 313 |
} |
| 314 |
digest = unparsed_digest; |
| 315 |
|
| 316 |
pj_iana_name = pj_str(iana_name); |
| 317 |
|
| 318 |
algo = ast_sip_auth_get_algorithm_by_iana_name(&pj_iana_name); |
| 319 |
if (!algo) { |
| 320 |
ast_log(LOG_WARNING, "%s: Unknown password_digest algorithm '%s' specified\n", |
| 321 |
auth_name, iana_name); |
| 322 |
return -1; |
| 323 |
} |
| 324 |
if (!ast_sip_auth_is_algorithm_supported(algo->algorithm_type)) { |
| 325 |
ast_log(LOG_WARNING, "%s: password_digest algorithm '%s' is not supported by the version of OpenSSL in use\n", |
| 326 |
auth_name, iana_name); |
| 327 |
return -1; |
| 328 |
} |
| 329 |
if (strlen(digest) != algo->digest_str_length) { |
| 330 |
ast_log(LOG_WARNING, "%s: password_digest algorithm '%s' length (%d) must be %d\n", |
| 331 |
auth_name, iana_name, (int)strlen(digest), (int)algo->digest_str_length); |
| 332 |
return -1; |
| 333 |
} |
| 334 |
|
| 335 |
pw = ast_calloc(1, sizeof(*pw) + strlen(digest) + 1); |
| 336 |
if (!pw) { |
| 337 |
return -1; |
| 338 |
} |
| 339 |
pw->algorithm_type = algo->algorithm_type; |
| 340 |
strcpy(pw->digest, digest); |
| 341 |
auth->password_digests[pw->algorithm_type] = pw; |
| 342 |
} |
| 343 |
|
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
static int password_digest_to_str(const void *obj, const intptr_t *args, char **buf) |
| 348 |
{ |
| 349 |
const struct ast_sip_auth *auth = obj; |
| 350 |
struct ast_str *str = ast_str_alloca(256); |
| 351 |
int i = 0; |
| 352 |
int count = 0; |
| 353 |
|
| 354 |
for (i = PJSIP_AUTH_ALGORITHM_NOT_SET + 1; i < PJSIP_AUTH_ALGORITHM_COUNT; i++) { |
| 355 |
struct ast_sip_auth_password_digest *pw = |
| 356 |
auth->password_digests[i]; |
| 357 |
const pjsip_auth_algorithm *algorithm; |
| 358 |
|
| 359 |
if (!pw) { |
| 360 |
continue; |
| 361 |
} |
| 362 |
|
| 363 |
algorithm = ast_sip_auth_get_algorithm_by_type(pw->algorithm_type); |
| 364 |
|
| 365 |
ast_str_append(&str, 0, "%s" PJSTR_PRINTF_SPEC ":%s", count > 0 ? "," : "", |
| 366 |
PJSTR_PRINTF_VAR(algorithm->iana_name), pw->digest); |
| 367 |
count++; |
| 368 |
} |
| 369 |
|
| 370 |
*buf = ast_strdup(ast_str_buffer(str)); |
| 371 |
|
| 372 |
return 0; |
| 373 |
} |
| 374 |
|
| 375 |
static int md5cred_to_str(const void *obj, const intptr_t *args, char **buf) |
| 376 |
{ |
| 377 |
const struct ast_sip_auth *auth = obj; |
| 378 |
|
| 379 |
if (auth->password_digests[PJSIP_AUTH_ALGORITHM_MD5]) { |
| 380 |
*buf = ast_strdup(auth->password_digests[PJSIP_AUTH_ALGORITHM_MD5]->digest); |
| 381 |
} |
| 382 |
|
| 383 |
return 0; |
| 384 |
} |
| 385 |
|
| 386 |
int ast_sip_auth_is_algorithm_available(const struct ast_sip_auth *auth, |
| 387 |
const struct pjsip_auth_algorithm_type_vector *algorithms, |
| 388 |
pjsip_auth_algorithm_type algorithm_type) |
| 389 |
{ |
| 390 |
int i; |
| 391 |
|
| 392 |
if (!algorithms) { |
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
for (i = 0; i < AST_VECTOR_SIZE(algorithms); ++i) { |
| 397 |
if (AST_VECTOR_GET(algorithms, i) == algorithm_type) { |
| 398 |
if (auth->password_digests[algorithm_type] || !ast_strlen_zero(auth->auth_pass)) { |
| 399 |
return 1; |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
const char *ast_sip_auth_get_creds(const struct ast_sip_auth *auth, |
| 408 |
const pjsip_auth_algorithm_type algorithm_type, int *cred_type) |
| 409 |
{ |
| 410 |
struct ast_sip_auth_password_digest *pw_digest = |
| 411 |
auth->password_digests[algorithm_type]; |
| 412 |
|
| 413 |
if (pw_digest) { |
| 414 |
*cred_type = PJSIP_CRED_DATA_DIGEST; |
| 415 |
return pw_digest->digest; |
| 416 |
} |
| 417 |
|
| 418 |
*cred_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 419 |
return auth->auth_pass; |
| 420 |
} |
| 421 |
|
| 422 |
static int check_algorithm(const struct ast_sip_auth *auth, |
| 423 |
const pjsip_auth_algorithm_type algorithm_type, const char *which_supported) |
| 424 |
{ |
| 425 |
const pjsip_auth_algorithm *algo = ast_sip_auth_get_algorithm_by_type(algorithm_type); |
| 426 |
struct ast_sip_auth_password_digest *pw_digest = |
| 427 |
auth->password_digests[algorithm_type]; |
| 428 |
|
| 429 |
if (!pw_digest && ast_strlen_zero(auth->auth_pass)) { |
| 430 |
ast_log(LOG_ERROR, "%s: No plain text or digest password found for algorithm " |
| 431 |
PJSTR_PRINTF_SPEC " in supported_algorithms_%s\n", |
| 432 |
ast_sorcery_object_get_id(auth), PJSTR_PRINTF_VAR(algo->iana_name), which_supported); |
| 433 |
return -1; |
| 434 |
} |
| 435 |
|
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
static int auth_apply(const struct ast_sorcery *sorcery, void *obj) |
| 440 |
{ |
| 441 |
struct ast_sip_auth *auth = obj; |
| 442 |
const char *id = ast_sorcery_object_get_id(auth); |
| 443 |
int i = 0; |
| 444 |
int res = 0; |
| 445 |
|
| 446 |
if (ast_strlen_zero(auth->auth_user)) { |
| 447 |
ast_log(LOG_ERROR, "%s: No authentication username\n", id); |
| 448 |
return -1; |
| 449 |
} |
| 450 |
|
| 451 |
if (auth->type == AST_SIP_AUTH_TYPE_GOOGLE_OAUTH) { |
| 452 |
if (ast_strlen_zero(auth->refresh_token) |
| 453 |
|| ast_strlen_zero(auth->oauth_clientid) |
| 454 |
|| ast_strlen_zero(auth->oauth_secret)) { |
| 455 |
ast_log(LOG_ERROR, "%s: 'google_oauth' authentication specified but refresh_token," |
| 456 |
" oauth_clientid, or oauth_secret not specified\n", id); |
| 457 |
res = -1; |
| 458 |
} |
| 459 |
return res; |
| 460 |
} |
| 461 |
|
| 462 |
if (AST_VECTOR_SIZE(&auth->supported_algorithms_uas) == 0) { |
| 463 |
char *default_algo_uas = ast_alloca(AST_SIP_AUTH_MAX_SUPPORTED_ALGORITHMS_LENGTH + 1); |
| 464 |
ast_sip_get_default_auth_algorithms_uas(default_algo_uas, AST_SIP_AUTH_MAX_SUPPORTED_ALGORITHMS_LENGTH); |
| 465 |
ast_sip_auth_digest_algorithms_vector_init(id, &auth->supported_algorithms_uas, "UAS", default_algo_uas); |
| 466 |
} |
| 467 |
if (AST_VECTOR_SIZE(&auth->supported_algorithms_uac) == 0) { |
| 468 |
char *default_algo_uac = ast_alloca(AST_SIP_AUTH_MAX_SUPPORTED_ALGORITHMS_LENGTH + 1); |
| 469 |
ast_sip_get_default_auth_algorithms_uac(default_algo_uac, AST_SIP_AUTH_MAX_SUPPORTED_ALGORITHMS_LENGTH); |
| 470 |
ast_sip_auth_digest_algorithms_vector_init(id, &auth->supported_algorithms_uac, "UAC", default_algo_uac); |
| 471 |
} |
| 472 |
|
| 473 |
for (i = 0; i < AST_VECTOR_SIZE(&auth->supported_algorithms_uas); i++) { |
| 474 |
res += check_algorithm(auth, AST_VECTOR_GET(&auth->supported_algorithms_uas, i), "uas"); |
| 475 |
} |
| 476 |
|
| 477 |
for (i = 0; i < AST_VECTOR_SIZE(&auth->supported_algorithms_uac); i++) { |
| 478 |
res += check_algorithm(auth, AST_VECTOR_GET(&auth->supported_algorithms_uac, i), "uac"); |
| 479 |
} |
| 480 |
|
| 481 |
return res; |
| 482 |
} |
| 483 |
|
| 484 |
int ast_sip_for_each_auth(const struct ast_sip_auth_vector *vector, |
| 485 |
ao2_callback_fn on_auth, void *arg) |
| 486 |
{ |
| 487 |
int i; |
| 488 |
|
| 489 |
if (!vector || !AST_VECTOR_SIZE(vector)) { |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
for (i = 0; i < AST_VECTOR_SIZE(vector); ++i) { |
| 494 |
|
| 495 |
RAII_VAR(struct ast_sip_auth *, auth, ast_sorcery_retrieve_by_id( |
| 496 |
ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, |
| 497 |
AST_VECTOR_GET(vector,i)), ao2_cleanup); |
| 498 |
|
| 499 |
if (!auth) { |
| 500 |
continue; |
| 501 |
} |
| 502 |
|
| 503 |
if (on_auth(auth, arg, 0)) { |
| 504 |
return -1; |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
return 0; |
| 509 |
} |
| 510 |
|
| 511 |
static int sip_auth_to_ami(const struct ast_sip_auth *auth, |
| 512 |
struct ast_str **buf) |
| 513 |
{ |
| 514 |
return ast_sip_sorcery_object_to_ami(auth, buf); |
| 515 |
} |
| 516 |
|
| 517 |
static int format_ami_auth_handler(void *obj, void *arg, int flags) |
| 518 |
{ |
| 519 |
const struct ast_sip_auth *auth = obj; |
| 520 |
struct ast_sip_ami *ami = arg; |
| 521 |
const struct ast_sip_endpoint *endpoint = ami->arg; |
| 522 |
RAII_VAR(struct ast_str *, buf, |
| 523 |
ast_sip_create_ami_event("AuthDetail", ami), ast_free); |
| 524 |
|
| 525 |
if (!buf) { |
| 526 |
return -1; |
| 527 |
} |
| 528 |
|
| 529 |
if (sip_auth_to_ami(auth, &buf)) { |
| 530 |
return -1; |
| 531 |
} |
| 532 |
|
| 533 |
if (endpoint) { |
| 534 |
ast_str_append(&buf, 0, "EndpointName: %s\r\n", |
| 535 |
ast_sorcery_object_get_id(endpoint)); |
| 536 |
} |
| 537 |
|
| 538 |
astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); |
| 539 |
ami->count++; |
| 540 |
|
| 541 |
return 0; |
| 542 |
} |
| 543 |
|
| 544 |
int ast_sip_format_auths_ami(const struct ast_sip_auth_vector *auths, |
| 545 |
struct ast_sip_ami *ami) |
| 546 |
{ |
| 547 |
return ast_sip_for_each_auth(auths, format_ami_auth_handler, ami); |
| 548 |
} |
| 549 |
|
| 550 |
static int format_ami_endpoint_auth(const struct ast_sip_endpoint *endpoint, |
| 551 |
struct ast_sip_ami *ami) |
| 552 |
{ |
| 553 |
ami->arg = (void *)endpoint; |
| 554 |
if (ast_sip_format_auths_ami(&endpoint->inbound_auths, ami)) { |
| 555 |
return -1; |
| 556 |
} |
| 557 |
|
| 558 |
return ast_sip_format_auths_ami(&endpoint->outbound_auths, ami); |
| 559 |
} |
| 560 |
|
| 561 |
static struct ast_sip_endpoint_formatter endpoint_auth_formatter = { |
| 562 |
.format_ami = format_ami_endpoint_auth |
| 563 |
}; |
| 564 |
|
| 565 |
static struct ao2_container *cli_get_auths(void) |
| 566 |
{ |
| 567 |
struct ao2_container *auths; |
| 568 |
|
| 569 |
auths = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "auth", |
| 570 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 571 |
|
| 572 |
return auths; |
| 573 |
} |
| 574 |
|
| 575 |
static int format_ami_authlist_handler(void *obj, void *arg, int flags) |
| 576 |
{ |
| 577 |
struct ast_sip_auth *auth = obj; |
| 578 |
struct ast_sip_ami *ami = arg; |
| 579 |
struct ast_str *buf; |
| 580 |
|
| 581 |
buf = ast_sip_create_ami_event("AuthList", ami); |
| 582 |
if (!buf) { |
| 583 |
return CMP_STOP; |
| 584 |
} |
| 585 |
|
| 586 |
sip_auth_to_ami(auth, &buf); |
| 587 |
|
| 588 |
astman_append(ami->s, "%s\r\n", ast_str_buffer(buf)); |
| 589 |
ami->count++; |
| 590 |
|
| 591 |
ast_free(buf); |
| 592 |
|
| 593 |
return 0; |
| 594 |
} |
| 595 |
|
| 596 |
static int ami_show_auths(struct mansession *s, const struct message *m) |
| 597 |
{ |
| 598 |
struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), }; |
| 599 |
struct ao2_container *auths; |
| 600 |
|
| 601 |
auths = cli_get_auths(); |
| 602 |
if (!auths) { |
| 603 |
astman_send_error(s, m, "Could not get Auths\n"); |
| 604 |
return 0; |
| 605 |
} |
| 606 |
|
| 607 |
if (!ao2_container_count(auths)) { |
| 608 |
astman_send_error(s, m, "No Auths found\n"); |
| 609 |
ao2_ref(auths, -1); |
| 610 |
return 0; |
| 611 |
} |
| 612 |
|
| 613 |
astman_send_listack(s, m, "A listing of Auths follows, presented as AuthList events", |
| 614 |
"start"); |
| 615 |
|
| 616 |
ao2_callback(auths, OBJ_NODATA, format_ami_authlist_handler, &ami); |
| 617 |
|
| 618 |
astman_send_list_complete_start(s, m, "AuthListComplete", ami.count); |
| 619 |
astman_send_list_complete_end(s); |
| 620 |
|
| 621 |
ao2_ref(auths, -1); |
| 622 |
|
| 623 |
return 0; |
| 624 |
} |
| 625 |
|
| 626 |
static struct ao2_container *cli_get_container(const char *regex) |
| 627 |
{ |
| 628 |
RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup); |
| 629 |
struct ao2_container *s_container; |
| 630 |
|
| 631 |
container = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "auth", regex); |
| 632 |
if (!container) { |
| 633 |
return NULL; |
| 634 |
} |
| 635 |
|
| 636 |
s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, |
| 637 |
ast_sorcery_object_id_sort, ast_sorcery_object_id_compare); |
| 638 |
if (!s_container) { |
| 639 |
return NULL; |
| 640 |
} |
| 641 |
|
| 642 |
if (ao2_container_dup(s_container, container, 0)) { |
| 643 |
ao2_ref(s_container, -1); |
| 644 |
return NULL; |
| 645 |
} |
| 646 |
|
| 647 |
return s_container; |
| 648 |
} |
| 649 |
|
| 650 |
static int cli_iterator(void *container, ao2_callback_fn callback, void *args) |
| 651 |
{ |
| 652 |
return ast_sip_for_each_auth(container, callback, args); |
| 653 |
} |
| 654 |
|
| 655 |
static void *cli_retrieve_by_id(const char *id) |
| 656 |
{ |
| 657 |
return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, id); |
| 658 |
} |
| 659 |
|
| 660 |
static int cli_print_header(void *obj, void *arg, int flags) |
| 661 |
{ |
| 662 |
struct ast_sip_cli_context *context = arg; |
| 663 |
int indent = CLI_INDENT_TO_SPACES(context->indent_level); |
| 664 |
int filler = CLI_MAX_WIDTH - indent - 20; |
| 665 |
|
| 666 |
ast_assert(context->output_buffer != NULL); |
| 667 |
|
| 668 |
ast_str_append(&context->output_buffer, 0, |
| 669 |
"%*s: <AuthId/UserName%*.*s>\n", indent, "I/OAuth", filler, filler, |
| 670 |
CLI_HEADER_FILLER); |
| 671 |
|
| 672 |
return 0; |
| 673 |
} |
| 674 |
|
| 675 |
static int cli_print_body(void *obj, void *arg, int flags) |
| 676 |
{ |
| 677 |
struct ast_sip_auth *auth = obj; |
| 678 |
struct ast_sip_cli_context *context = arg; |
| 679 |
char title[32]; |
| 680 |
|
| 681 |
ast_assert(context->output_buffer != NULL); |
| 682 |
|
| 683 |
snprintf(title, sizeof(title), "%sAuth", |
| 684 |
context->auth_direction ? context->auth_direction : ""); |
| 685 |
|
| 686 |
ast_str_append(&context->output_buffer, 0, "%*s: %s/%s\n", |
| 687 |
CLI_INDENT_TO_SPACES(context->indent_level), title, |
| 688 |
ast_sorcery_object_get_id(auth), auth->auth_user); |
| 689 |
|
| 690 |
if (context->show_details |
| 691 |
|| (context->show_details_only_level_0 && context->indent_level == 0)) { |
| 692 |
ast_str_append(&context->output_buffer, 0, "\n"); |
| 693 |
ast_sip_cli_print_sorcery_objectset(auth, context, 0); |
| 694 |
} |
| 695 |
|
| 696 |
return 0; |
| 697 |
} |
| 698 |
|
| 699 |
static struct ast_cli_entry cli_commands[] = { |
| 700 |
AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Auths", |
| 701 |
.command = "pjsip list auths", |
| 702 |
.usage = "Usage: pjsip list auths [ like <pattern> ]\n" |
| 703 |
" List the configured PJSIP Auths\n" |
| 704 |
" Optional regular expression pattern is used to filter the list.\n"), |
| 705 |
AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Auths", |
| 706 |
.command = "pjsip show auths", |
| 707 |
.usage = "Usage: pjsip show auths [ like <pattern> ]\n" |
| 708 |
" Show the configured PJSIP Auths\n" |
| 709 |
" Optional regular expression pattern is used to filter the list.\n"), |
| 710 |
AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Auth", |
| 711 |
.command = "pjsip show auth", |
| 712 |
.usage = "Usage: pjsip show auth <id>\n" |
| 713 |
" Show the configured PJSIP Auth\n"), |
| 714 |
}; |
| 715 |
|
| 716 |
static struct ast_sip_cli_formatter_entry *cli_formatter; |
| 717 |
|
| 718 |
static void global_loaded(const char *object_type) |
| 719 |
{ |
| 720 |
ast_sorcery_force_reload_object(ast_sip_get_sorcery(), "auth"); |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
static struct ast_sorcery_observer global_observer = { |
| 725 |
.loaded = global_loaded, |
| 726 |
}; |
| 727 |
|
| 728 |
|
| 729 |
int ast_sip_initialize_sorcery_auth(void) |
| 730 |
{ |
| 731 |
struct ast_sorcery *sorcery = ast_sip_get_sorcery(); |
| 732 |
|
| 733 |
ast_sorcery_apply_default(sorcery, SIP_SORCERY_AUTH_TYPE, "config", "pjsip.conf,criteria=type=auth"); |
| 734 |
|
| 735 |
if (ast_sorcery_object_register(sorcery, SIP_SORCERY_AUTH_TYPE, auth_alloc, NULL, auth_apply)) { |
| 736 |
return -1; |
| 737 |
} |
| 738 |
|
| 739 |
ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "type", "", |
| 740 |
OPT_NOOP_T, 0, 0); |
| 741 |
ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "username", |
| 742 |
"", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, auth_user)); |
| 743 |
ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "password", |
| 744 |
"", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, auth_pass)); |
| 745 |
ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "refresh_token", |
| 746 |
"", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, refresh_token)); |
| 747 |
ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "oauth_clientid", |
| 748 |
"", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, oauth_clientid)); |
| 749 |
ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "oauth_secret", |
| 750 |
"", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, oauth_secret)); |
| 751 |
ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_AUTH_TYPE, "md5_cred", |
| 752 |
NULL, password_digest_handler, md5cred_to_str, NULL, 0, 0); |
| 753 |
ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "realm", |
| 754 |
"", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_auth, realm)); |
| 755 |
ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "nonce_lifetime", |
| 756 |
"32", OPT_UINT_T, 0, FLDSET(struct ast_sip_auth, nonce_lifetime)); |
| 757 |
ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_AUTH_TYPE, "auth_type", |
| 758 |
"userpass", auth_type_handler, auth_type_to_str, NULL, 0, 0); |
| 759 |
ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_AUTH_TYPE, "password_digest", |
| 760 |
NULL, password_digest_handler, password_digest_to_str, NULL, 0, 0); |
| 761 |
ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_AUTH_TYPE, "supported_algorithms_uac", |
| 762 |
"", uac_algorithms_handler, uac_algorithms_to_str, NULL, 0, 0); |
| 763 |
ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_AUTH_TYPE, "supported_algorithms_uas", |
| 764 |
"", uas_algorithms_handler, uas_algorithms_to_str, NULL, 0, 0); |
| 765 |
|
| 766 |
ast_sip_register_endpoint_formatter(&endpoint_auth_formatter); |
| 767 |
|
| 768 |
cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL); |
| 769 |
if (!cli_formatter) { |
| 770 |
ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n"); |
| 771 |
return -1; |
| 772 |
} |
| 773 |
cli_formatter->name = SIP_SORCERY_AUTH_TYPE; |
| 774 |
cli_formatter->print_header = cli_print_header; |
| 775 |
cli_formatter->print_body = cli_print_body; |
| 776 |
cli_formatter->get_container = cli_get_container; |
| 777 |
cli_formatter->iterate = cli_iterator; |
| 778 |
cli_formatter->get_id = ast_sorcery_object_get_id; |
| 779 |
cli_formatter->retrieve_by_id = cli_retrieve_by_id; |
| 780 |
|
| 781 |
ast_sip_register_cli_formatter(cli_formatter); |
| 782 |
ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands)); |
| 783 |
|
| 784 |
if (ast_manager_register_xml("PJSIPShowAuths", EVENT_FLAG_SYSTEM, ami_show_auths)) { |
| 785 |
return -1; |
| 786 |
} |
| 787 |
|
| 788 |
ast_sorcery_observer_add(sorcery, "global", &global_observer); |
| 789 |
return 0; |
| 790 |
} |
| 791 |
|
| 792 |
int ast_sip_destroy_sorcery_auth(void) |
| 793 |
{ |
| 794 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "global", &global_observer); |
| 795 |
|
| 796 |
ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands)); |
| 797 |
ast_sip_unregister_cli_formatter(cli_formatter); |
| 798 |
ast_sip_unregister_endpoint_formatter(&endpoint_auth_formatter); |
| 799 |
|
| 800 |
ast_manager_unregister("PJSIPShowAuths"); |
| 801 |
|
| 802 |
return 0; |
| 803 |
} |