| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include <pjsip.h> |
| 36 |
|
| 37 |
#include "asterisk/res_pjsip.h" |
| 38 |
#include "asterisk/logger.h" |
| 39 |
#include "asterisk/module.h" |
| 40 |
#include "asterisk/strings.h" |
| 41 |
#include "asterisk/vector.h" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static pjsip_hdr_e get_auth_search_type(pjsip_rx_data *challenge) |
| 51 |
{ |
| 52 |
if (challenge->msg_info.msg->line.status.code == PJSIP_SC_UNAUTHORIZED) { |
| 53 |
return PJSIP_H_WWW_AUTHENTICATE; |
| 54 |
} else if (challenge->msg_info.msg->line.status.code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED) { |
| 55 |
return PJSIP_H_PROXY_AUTHENTICATE; |
| 56 |
} else { |
| 57 |
ast_log(LOG_ERROR, |
| 58 |
"Status code %d was received when it should have been 401 or 407.\n", |
| 59 |
challenge->msg_info.msg->line.status.code); |
| 60 |
return PJSIP_H_OTHER; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static const pjsip_auth_algorithm *get_supported_algorithm(pjsip_www_authenticate_hdr *auth_hdr) |
| 70 |
{ |
| 71 |
const pjsip_auth_algorithm *algo = NULL; |
| 72 |
|
| 73 |
algo = ast_sip_auth_get_algorithm_by_iana_name(&auth_hdr->challenge.digest.algorithm); |
| 74 |
if (!algo) { |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
|
| 78 |
if (ast_sip_auth_is_algorithm_supported(algo->algorithm_type)) { |
| 79 |
return algo; |
| 80 |
} |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
AST_VECTOR(cred_info_vector, pjsip_cred_info); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
static void get_creds_for_header(const char *id, const char *src_name, |
| 99 |
pjsip_www_authenticate_hdr *auth_hdr, size_t auth_object_count, |
| 100 |
const struct ast_sip_auth_objects_vector *auth_objects_vector, |
| 101 |
struct cred_info_vector *auth_creds, struct ast_str **realms) |
| 102 |
{ |
| 103 |
int exact_match_index = -1; |
| 104 |
int wildcard_match_index = -1; |
| 105 |
struct ast_sip_auth *found_auth = NULL; |
| 106 |
const pjsip_auth_algorithm *challenge_algorithm = |
| 107 |
get_supported_algorithm(auth_hdr); |
| 108 |
int i = 0; |
| 109 |
pjsip_cred_info auth_cred; |
| 110 |
const char *cred_data; |
| 111 |
int res = 0; |
| 112 |
SCOPE_ENTER(4, "%s:%s: Testing header realm: '" PJSTR_PRINTF_SPEC "' algorithm: '" |
| 113 |
PJSTR_PRINTF_SPEC "'\n", id, src_name, |
| 114 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.realm), |
| 115 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.algorithm)); |
| 116 |
|
| 117 |
if (!challenge_algorithm) { |
| 118 |
SCOPE_EXIT_RTN("%s:%s: Skipping header with realm '" PJSTR_PRINTF_SPEC "' " |
| 119 |
"and unsupported " PJSTR_PRINTF_SPEC "' algorithm \n", id, src_name, |
| 120 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.realm), |
| 121 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.algorithm)); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
for (i = 0; i < AST_VECTOR_SIZE(auth_creds); i++) { |
| 129 |
pjsip_cred_info auth_cred = AST_VECTOR_GET(auth_creds, i); |
| 130 |
if (pj_stricmp(&auth_cred.realm, &auth_hdr->challenge.common.realm) == 0) { |
| 131 |
SCOPE_EXIT_RTN("%s:%s: Skipping header with realm '" PJSTR_PRINTF_SPEC "' " |
| 132 |
"because we already have credentials for it\n", id, src_name, |
| 133 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.realm)); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if (*realms) { |
| 142 |
ast_str_append(realms, 0, PJSTR_PRINTF_SPEC "/" PJSTR_PRINTF_SPEC ", ", |
| 143 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.realm), |
| 144 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.algorithm)); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
ast_trace(-1, "%s:%s: Searching %zu auths to find matching ones for header with realm " |
| 157 |
"'" PJSTR_PRINTF_SPEC "' and algorithm '" PJSTR_PRINTF_SPEC "'\n", |
| 158 |
id, src_name, auth_object_count, |
| 159 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.realm), |
| 160 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.algorithm)); |
| 161 |
|
| 162 |
for (i = 0; i < auth_object_count; ++i) { |
| 163 |
struct ast_sip_auth *auth = AST_VECTOR_GET(auth_objects_vector, i); |
| 164 |
const char *auth_id = ast_sorcery_object_get_id(auth); |
| 165 |
SCOPE_ENTER(5, "%s:%s: Checking auth '%s' with realm '%s'\n", |
| 166 |
id, src_name, auth_id, auth->realm); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
if (!ast_sip_auth_is_algorithm_available(auth, &auth->supported_algorithms_uac, |
| 174 |
challenge_algorithm->algorithm_type)) { |
| 175 |
SCOPE_EXIT_EXPR(continue, "%s:%s: Skipping auth '%s' with realm '%s' because it doesn't support " |
| 176 |
" algorithm '" PJSTR_PRINTF_SPEC "'\n", id, src_name, |
| 177 |
auth_id, auth->realm, |
| 178 |
PJSTR_PRINTF_VAR(challenge_algorithm->iana_name)); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
if (pj_stricmp2(&auth_hdr->challenge.digest.realm, auth->realm) == 0) { |
| 189 |
exact_match_index = i; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
SCOPE_EXIT_EXPR(break, "%s:%s: Found matching auth '%s' with realm '%s'\n", |
| 195 |
id, src_name, auth_id, auth->realm); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
if (wildcard_match_index < 0 |
| 207 |
&& (ast_strlen_zero(auth->realm) || ast_strings_equal(auth->realm, "*"))) { |
| 208 |
ast_trace(-1, "%s:%s: Found wildcard auth '%s' for realm '" PJSTR_PRINTF_SPEC "'\n", |
| 209 |
id, src_name, auth_id, |
| 210 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.realm)); |
| 211 |
wildcard_match_index = i; |
| 212 |
} |
| 213 |
SCOPE_EXIT("%s:%s: Done checking auth '%s' with realm '%s'. " |
| 214 |
"Found exact? %s Found wildcard? %s\n", id, src_name, |
| 215 |
auth_id, auth->realm, exact_match_index >= 0 ? "yes" : "no", |
| 216 |
wildcard_match_index >= 0 ? "yes" : "no"); |
| 217 |
} |
| 218 |
|
| 219 |
if (exact_match_index < 0 && wildcard_match_index < 0) { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
SCOPE_EXIT_RTN("%s:%s: No auth matching realm or no wildcard found for realm '" PJSTR_PRINTF_SPEC "'\n", |
| 225 |
id, src_name, PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.realm)); |
| 226 |
} |
| 227 |
|
| 228 |
if (exact_match_index >= 0) { |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
found_auth = AST_VECTOR_GET(auth_objects_vector, exact_match_index); |
| 233 |
ast_trace(-1, "%s:%s: Using matched auth '%s' with realm '" PJSTR_PRINTF_SPEC "'\n", |
| 234 |
id, src_name, ast_sorcery_object_get_id(found_auth), |
| 235 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.realm)); |
| 236 |
} else { |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
found_auth = AST_VECTOR_GET(auth_objects_vector, wildcard_match_index); |
| 241 |
ast_trace(-1, "%s:%s: Using wildcard auth '%s' for realm '" PJSTR_PRINTF_SPEC "'\n", |
| 242 |
id, src_name, ast_sorcery_object_get_id(found_auth), |
| 243 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.realm)); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
memset(&auth_cred, 0, sizeof(auth_cred)); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
auth_cred.realm = auth_hdr->challenge.common.realm; |
| 257 |
pj_cstr(&auth_cred.username, found_auth->auth_user); |
| 258 |
pj_cstr(&auth_cred.scheme, "digest"); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
cred_data = SCOPE_CALL_WITH_RESULT(-1, const char *, ast_sip_auth_get_creds, |
| 265 |
found_auth, challenge_algorithm->algorithm_type, &auth_cred.data_type); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if (!cred_data) { |
| 272 |
SCOPE_EXIT_RTN("%s:%s: Shouldn't have happened\n", id, src_name); |
| 273 |
} |
| 274 |
|
| 275 |
pj_cstr(&auth_cred.data, cred_data); |
| 276 |
#ifdef HAVE_PJSIP_AUTH_NEW_DIGESTS |
| 277 |
if (auth_cred.data_type == PJSIP_CRED_DATA_DIGEST) { |
| 278 |
auth_cred.algorithm_type = challenge_algorithm->algorithm_type; |
| 279 |
} |
| 280 |
#endif |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
res = AST_VECTOR_APPEND(auth_creds, auth_cred); |
| 295 |
SCOPE_EXIT_RTN("%s:%s: %s credential for realm: '" PJSTR_PRINTF_SPEC "' algorithm: '" |
| 296 |
PJSTR_PRINTF_SPEC "'\n", id, src_name, |
| 297 |
res == 0 ? "Added" : "Failed to add", |
| 298 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.realm), |
| 299 |
PJSTR_PRINTF_VAR(auth_hdr->challenge.digest.algorithm)); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
static pj_status_t set_auth_creds(const char *id, pjsip_auth_clt_sess *auth_sess, |
| 340 |
const struct ast_sip_auth_objects_vector *auth_objects_vector, |
| 341 |
pjsip_rx_data *challenge, struct ast_str **realms) |
| 342 |
{ |
| 343 |
size_t auth_object_count; |
| 344 |
pjsip_www_authenticate_hdr *auth_hdr = NULL; |
| 345 |
pj_status_t res = PJ_SUCCESS; |
| 346 |
pjsip_hdr_e search_type; |
| 347 |
size_t cred_count = 0; |
| 348 |
pjsip_cred_info *creds_array; |
| 349 |
char *pj_err = NULL; |
| 350 |
const char *src_name = challenge->pkt_info.src_name; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
struct cred_info_vector auth_creds; |
| 363 |
SCOPE_ENTER(3, "%s:%s\n", id, src_name); |
| 364 |
|
| 365 |
search_type = get_auth_search_type(challenge); |
| 366 |
if (search_type == PJSIP_H_OTHER) { |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
SCOPE_EXIT_RTN_VALUE(PJ_ENOTSUP, "%s:%s: Status code %d was received when it should have been 401 or 407.\n", |
| 373 |
id, src_name, challenge->msg_info.msg->line.status.code); |
| 374 |
} |
| 375 |
|
| 376 |
auth_object_count = AST_VECTOR_SIZE(auth_objects_vector); |
| 377 |
if (auth_object_count == 0) { |
| 378 |
|
| 379 |
SCOPE_EXIT_RTN_VALUE(PJ_EINVAL, "%s:%s No auth objects available\n", id, src_name); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
if (AST_VECTOR_INIT(&auth_creds, 5) != 0) { |
| 393 |
SCOPE_EXIT_RTN_VALUE(PJ_ENOMEM); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
while ((auth_hdr = pjsip_msg_find_hdr(challenge->msg_info.msg, |
| 406 |
search_type, auth_hdr ? auth_hdr->next : NULL))) { |
| 407 |
|
| 408 |
get_creds_for_header(id, src_name, auth_hdr, auth_object_count, |
| 409 |
auth_objects_vector, &auth_creds, realms); |
| 410 |
|
| 411 |
} |
| 412 |
|
| 413 |
if (*realms && ast_str_strlen(*realms)) { |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
ast_str_truncate(*realms, ast_str_strlen(*realms) - 2); |
| 418 |
} |
| 419 |
|
| 420 |
if (AST_VECTOR_SIZE(&auth_creds) == 0) { |
| 421 |
|
| 422 |
res = PJSIP_ENOCREDENTIAL; |
| 423 |
goto cleanup; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
cred_count = AST_VECTOR_SIZE(&auth_creds); |
| 442 |
creds_array = AST_VECTOR_STEAL_ELEMENTS(&auth_creds); |
| 443 |
|
| 444 |
res = pjsip_auth_clt_set_credentials(auth_sess, cred_count, creds_array); |
| 445 |
ast_free(creds_array); |
| 446 |
|
| 447 |
cleanup: |
| 448 |
AST_VECTOR_FREE(&auth_creds); |
| 449 |
if (res != PJ_SUCCESS) { |
| 450 |
pj_err = ast_alloca(PJ_ERR_MSG_SIZE); |
| 451 |
pj_strerror(res, pj_err, PJ_ERR_MSG_SIZE); |
| 452 |
} |
| 453 |
SCOPE_EXIT_RTN_VALUE(res, "%s:%s: Set %zu credentials in auth session: %s\n", |
| 454 |
id, src_name, cred_count, S_OR(pj_err, "success")); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
static int digest_create_request_with_auth(const struct ast_sip_auth_vector *auth_ids_vector, |
| 471 |
pjsip_rx_data *challenge, pjsip_tx_data *old_request, pjsip_tx_data **new_request) |
| 472 |
{ |
| 473 |
pjsip_auth_clt_sess auth_sess; |
| 474 |
pjsip_cseq_hdr *cseq; |
| 475 |
pj_status_t status; |
| 476 |
struct ast_sip_auth_objects_vector auth_objects_vector; |
| 477 |
size_t auth_object_count = 0; |
| 478 |
pjsip_dialog *dlg = pjsip_rdata_get_dlg(challenge); |
| 479 |
struct ast_sip_endpoint *endpoint = (dlg ? ast_sip_dialog_get_endpoint(dlg) : NULL); |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
char *endpoint_id = endpoint ? ast_strdupa(ast_sorcery_object_get_id(endpoint)) : NULL; |
| 486 |
char *id = endpoint_id ?: "noendpoint"; |
| 487 |
char *src_name = challenge->pkt_info.src_name; |
| 488 |
struct ast_str *realms = NULL; |
| 489 |
int res = -1; |
| 490 |
char *pj_err = NULL; |
| 491 |
SCOPE_ENTER(3, "%s:%s\n", id, src_name); |
| 492 |
|
| 493 |
|
| 494 |
ao2_cleanup(endpoint); |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
memset(&auth_sess, 0, sizeof(auth_sess)); |
| 502 |
|
| 503 |
if (!auth_ids_vector || AST_VECTOR_SIZE(auth_ids_vector) == 0) { |
| 504 |
SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s:%s: There were no auth ids available\n", |
| 505 |
id, src_name); |
| 506 |
return -1; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
if (AST_VECTOR_INIT(&auth_objects_vector, AST_VECTOR_SIZE(auth_ids_vector)) != 0) { |
| 513 |
SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s:%s: Couldn't initialize auth object vector\n", |
| 514 |
id, src_name); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
ast_trace(-1, "%s:%s: Retrieving %d auth objects\n", id, src_name, |
| 527 |
(int)AST_VECTOR_SIZE(auth_ids_vector)); |
| 528 |
ast_sip_retrieve_auths_vector(auth_ids_vector, &auth_objects_vector); |
| 529 |
auth_object_count = AST_VECTOR_SIZE(&auth_objects_vector); |
| 530 |
if (auth_object_count == 0) { |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
res = -1; |
| 538 |
ast_trace(-1, "%s:%s: No auth objects found\n", id, src_name); |
| 539 |
goto cleanup; |
| 540 |
} |
| 541 |
ast_trace(-1, "%s:%s: Retrieved %d auth objects\n", id, src_name, |
| 542 |
(int)auth_object_count); |
| 543 |
|
| 544 |
status = pjsip_auth_clt_init(&auth_sess, ast_sip_get_pjsip_endpoint(), |
| 545 |
old_request->pool, 0); |
| 546 |
if (status != PJ_SUCCESS) { |
| 547 |
pj_err = ast_alloca(PJ_ERR_MSG_SIZE); |
| 548 |
pj_strerror(status, pj_err, PJ_ERR_MSG_SIZE); |
| 549 |
ast_log(LOG_ERROR, "%s:%s: Failed to initialize client authentication session: %s\n", |
| 550 |
id, src_name, pj_err); |
| 551 |
res = -1; |
| 552 |
goto cleanup; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
realms = ast_str_create(32); |
| 559 |
if (!realms) { |
| 560 |
res = -1; |
| 561 |
goto cleanup; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
status = SCOPE_CALL_WITH_RESULT(-1, pj_status_t, set_auth_creds, id, &auth_sess, &auth_objects_vector, challenge, &realms); |
| 569 |
if (status != PJ_SUCCESS && status != PJSIP_ENOCREDENTIAL) { |
| 570 |
pj_err = ast_alloca(PJ_ERR_MSG_SIZE); |
| 571 |
} |
| 572 |
|
| 573 |
switch (status) { |
| 574 |
case PJ_SUCCESS: |
| 575 |
break; |
| 576 |
case PJSIP_ENOCREDENTIAL: |
| 577 |
ast_log(LOG_WARNING, |
| 578 |
"%s:%s: No auth objects matching realm/algorithm(s) '%s' from challenge found.\n", |
| 579 |
id, src_name, realms ? ast_str_buffer(realms) : "<none>"); |
| 580 |
res = -1; |
| 581 |
goto cleanup; |
| 582 |
default: |
| 583 |
pj_strerror(status, pj_err, PJ_ERR_MSG_SIZE); |
| 584 |
ast_log(LOG_WARNING, "%s:%s: Failed to set authentication credentials: %s\n", |
| 585 |
id, src_name, pj_err); |
| 586 |
res = -1; |
| 587 |
goto cleanup; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
status = SCOPE_CALL_WITH_RESULT(-1, pj_status_t, pjsip_auth_clt_reinit_req, |
| 597 |
&auth_sess, challenge, old_request, new_request); |
| 598 |
if (status != PJ_SUCCESS) { |
| 599 |
pj_err = ast_alloca(PJ_ERR_MSG_SIZE); |
| 600 |
} |
| 601 |
|
| 602 |
switch (status) { |
| 603 |
case PJ_SUCCESS: |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
cseq = pjsip_msg_find_hdr((*new_request)->msg, PJSIP_H_CSEQ, NULL); |
| 611 |
ast_assert(cseq != NULL); |
| 612 |
++cseq->cseq; |
| 613 |
res = 0; |
| 614 |
ast_trace(-1, "%s:%s: Created new request with auth\n", id, src_name); |
| 615 |
goto cleanup; |
| 616 |
case PJSIP_ENOCREDENTIAL: |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
ast_log(LOG_WARNING, |
| 622 |
"%s:%s: No auth objects matching realm(s) '%s' from challenge found.\n", |
| 623 |
id, src_name, realms ? ast_str_buffer(realms) : "<none>"); |
| 624 |
break; |
| 625 |
case PJSIP_EAUTHSTALECOUNT: |
| 626 |
pj_strerror(status, pj_err, PJ_ERR_MSG_SIZE); |
| 627 |
ast_log(LOG_WARNING, |
| 628 |
"%s:%s: Unable to create request with auth: %s\n", |
| 629 |
id, src_name, pj_err); |
| 630 |
break; |
| 631 |
case PJSIP_EFAILEDCREDENTIAL: |
| 632 |
pj_strerror(status, pj_err, PJ_ERR_MSG_SIZE); |
| 633 |
ast_log(LOG_WARNING, "%s:%s: Authentication credentials not accepted by server. %s\n", |
| 634 |
id, src_name, pj_err); |
| 635 |
break; |
| 636 |
default: |
| 637 |
pj_strerror(status, pj_err, PJ_ERR_MSG_SIZE); |
| 638 |
ast_log(LOG_WARNING, "%s:%s: Unable to create request with auth: %s\n", |
| 639 |
id, src_name, pj_err); |
| 640 |
break; |
| 641 |
} |
| 642 |
res = -1; |
| 643 |
|
| 644 |
cleanup: |
| 645 |
#if defined(HAVE_PJSIP_AUTH_CLT_DEINIT) |
| 646 |
|
| 647 |
if (auth_sess.endpt) { |
| 648 |
pjsip_auth_clt_deinit(&auth_sess); |
| 649 |
} |
| 650 |
#endif |
| 651 |
|
| 652 |
ast_sip_cleanup_auth_objects_vector(&auth_objects_vector); |
| 653 |
AST_VECTOR_FREE(&auth_objects_vector); |
| 654 |
ast_free(realms); |
| 655 |
|
| 656 |
SCOPE_EXIT_RTN_VALUE(res, "%s:%s: result: %s\n", id, src_name, |
| 657 |
res == 0 ? "success" : "failure"); |
| 658 |
} |
| 659 |
|
| 660 |
static struct ast_sip_outbound_authenticator digest_authenticator = { |
| 661 |
.create_request_with_auth = digest_create_request_with_auth, |
| 662 |
}; |
| 663 |
|
| 664 |
static int load_module(void) |
| 665 |
{ |
| 666 |
if (ast_sip_register_outbound_authenticator(&digest_authenticator)) { |
| 667 |
return AST_MODULE_LOAD_DECLINE; |
| 668 |
} |
| 669 |
return AST_MODULE_LOAD_SUCCESS; |
| 670 |
} |
| 671 |
|
| 672 |
static int unload_module(void) |
| 673 |
{ |
| 674 |
ast_sip_unregister_outbound_authenticator(&digest_authenticator); |
| 675 |
return 0; |
| 676 |
} |
| 677 |
|
| 678 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP authentication resource", |
| 679 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 680 |
.load = load_module, |
| 681 |
.unload = unload_module, |
| 682 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND, |
| 683 |
.requires = "res_pjsip", |
| 684 |
); |