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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
|
#include "myproxy_common.h" /* all needed headers included here */
#if defined(HAVE_LIBPAM)
#include "auth_pam.h"
#endif
struct authorization_func {
author_status_t (*get_status) (struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config);
char * (*create_server_data) (void);
char * (*create_client_data) (authorization_data_t *data,
void *extra_data,
size_t extra_data_len,
size_t *client_data_len);
int (*check_client) (authorization_data_t *client_auth_data,
struct myproxy_creds *creds,
char *client_name, myproxy_server_context_t *config);
author_method_t method;
char *name; /* arbitrary ASCII string without a colon (':') */
};
static struct authorization_func * _find_func(author_method_t method);
static authorization_data_t *
_find_data(author_method_t method, authorization_data_t **data);
/*
* Implementation of password-based authorization
*/
static author_status_t auth_passwd_get_status(struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config)
{
assert(creds);
assert(config);
if (myproxy_creds_exist(creds->username, creds->credname) == 1 &&
myproxy_creds_encrypted(creds) == 1) {
verror_clear();
return AUTHORIZEMETHOD_REQUIRED;
}
#if defined(HAVE_LIBPAM)
if (config->pam_policy) {
if (strcmp(config->pam_policy, "required") == 0) {
return AUTHORIZEMETHOD_REQUIRED;
}
if (strcmp(config->pam_policy, "sufficient") == 0) {
return AUTHORIZEMETHOD_SUFFICIENT;
}
}
#endif
return AUTHORIZEMETHOD_DISABLED;
}
static char *auth_passwd_create_server_data(void)
{
return strdup("Enter MyProxy pass phrase:");
}
static char *auth_passwd_create_client_data(authorization_data_t *data,
void *extra_data,
size_t extra_data_len,
size_t *client_data_len)
{
char *tmp;
tmp = malloc(extra_data_len + 1);
if (tmp == NULL)
return NULL;
memcpy(tmp, extra_data, extra_data_len);
tmp[extra_data_len] = '\0';
*client_data_len = extra_data_len + 1;
return tmp;
}
static int auth_passwd_check_client(authorization_data_t *client_auth_data,
struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config)
{
int exist=0, encrypted=0, cred_passphrase_match=0;
#if defined(HAVE_LIBPAM)
char* pam_policy = NULL;
char* pam_id = NULL;
int pam_required, pam_sufficient, pam_disabled;
#endif
/* 1. Gather some initial information. */
exist = myproxy_creds_exist(creds->username, creds->credname);
if (exist < 0) {
exist = 0; verror_clear(); /* may be in CA-only mode */
}
if (exist) {
encrypted = myproxy_creds_encrypted(creds);
if (encrypted < 0) {
return 0;
}
}
/* 2. Check whether the password the user gave matches the
* credential passphrase */
if (exist && (encrypted || creds->passphrase))
{
if (config)
config->usage.cred_pphrase_used = 1;
if (client_auth_data->client_data_len >= MIN_PASS_PHRASE_LEN &&
client_auth_data->client_data != NULL &&
myproxy_creds_verify_passphrase(creds,
client_auth_data->client_data) == 1){
cred_passphrase_match = 1;
myproxy_log("credential passphrase matched");
} else {
/* We always have to match the credential passphrase if it exists. */
verror_put_string("invalid credential passphrase");
return 0;
}
}
#if defined(HAVE_LIBPAM)
/* Tangent: figure out PAM configuration. */
pam_policy = config ? config->pam_policy : NULL;
pam_id = config ? config->pam_id : NULL;
/* Default value is "disabled". */
if (pam_policy == NULL) pam_policy = "disabled";
pam_required = (strcmp(pam_policy, "required" ) == 0 ? 1 : 0);
pam_sufficient = (strcmp(pam_policy, "sufficient") == 0 ? 1 : 0);
pam_disabled = (strcmp(pam_policy, "disabled" ) == 0 ? 1 : 0);
/* Note: if pam_policy is not recognized, it will fall through to
* the disabled case below, and a debug message will be printed. */
/* 3. If the passphrase matches the credentials, and PAM config is
* "sufficient", then we're done, and we don't need to check
* PAM, as long as a passphrase was actually entered. */
if (pam_sufficient && cred_passphrase_match)
{
myproxy_debug("Passphrase matches credentials, and PAM config is \"%s\"; "
"authentication succeeds without checking PAM.", pam_policy);
return cred_passphrase_match;
}
/* 4. If PAM is "required", *always* check it, regardless of
* whether the credential passphrase matches, so that any
* logging, pausing, etc. can occur. Also, if PAM is sufficient
* and we've gotten this far, it means that the credential
* passphrase is blank and therefore we need to check PAM. */
else if (pam_required || pam_sufficient)
{
char* auth_pam_result = NULL;
int pam_success = 0;
if (pam_id == NULL) pam_id = "myproxy";
myproxy_debug
("Checking passphrase via PAM. PAM policy: \"%s\"; PAM ID: \"%s\"",
pam_policy, pam_id);
auth_pam_result = auth_pam(creds->username,
client_auth_data->client_data, pam_id, NULL);
if (config)
config->usage.pam_used = 1;
if (auth_pam_result && strcmp("OK", auth_pam_result) == 0) {
pam_success = 1;
myproxy_log("PAM authentication succeeded for %s",
creds->username);
} else {
if (auth_pam_result) {
/* The Cyrus SASL convention is to prepend the error
message with "NO ". We can chop that off. */
if (strlen(auth_pam_result) > 3
&& strncmp(auth_pam_result, "NO ", 3) == 0)
{
verror_put_string("%s", auth_pam_result + 3);
}
else verror_put_string("%s", auth_pam_result);
}
else
verror_put_string("PAM authentication failed with unknown error for user %s", creds->username);
}
if (auth_pam_result != NULL) {
free(auth_pam_result);
}
return pam_success;
}
/* 5. If PAM is disabled, check only the credential passphrase. */
else
{
if (!pam_disabled) {
myproxy_log("Unknown PAM policy: \"%s\"; not using PAM.\n", pam_policy);
}
return cred_passphrase_match;
}
#else /* defined(HAVE_LIBPAM) */
return cred_passphrase_match;
#endif /* defined(HAVE_LIBPAM) */
}
static struct authorization_func authorization_passwd = {
auth_passwd_get_status,
auth_passwd_create_server_data,
auth_passwd_create_client_data,
auth_passwd_check_client,
AUTHORIZETYPE_PASSWD,
"password"
};
/*
* Implementation of certificate-based authorization
*/
static author_status_t auth_cert_get_status(struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config)
{
/* Just check here if this server allows renewal.
Other checks for credential existence or CA configuration
are done elsewhere. */
if (config->authorized_renewer_dns) {
return AUTHORIZEMETHOD_SUFFICIENT;
}
return AUTHORIZEMETHOD_DISABLED;
}
#define CHALLENGE_SIZE 16
static char *auth_cert_create_server_data(void)
{
unsigned char random[CHALLENGE_SIZE];
char *challenge;
int i;
/* RAND_bytes() will fail if the PRNG has not been seeded with
enough randomness to ensure an unpredictable byte sequence. */
if (RAND_bytes(random, sizeof(random)) == 0) {
verror_put_string("RAND_bytes failed");
ssl_error_to_verror();
return NULL;
}
challenge = malloc(CHALLENGE_SIZE * 2 + 1);
if (challenge == NULL) {
verror_put_string("malloc()");
verror_put_errno(errno);
return NULL;
}
for (i = 0; i < CHALLENGE_SIZE; i++) {
int dd = random[i] & 0x0f;
challenge[2*i+1] = dd<10 ? dd+'0' : dd-10+'a';
dd = random[i] >> 4;
challenge[2*i] = dd<10 ? dd+'0' : dd-10+'a';
}
challenge[CHALLENGE_SIZE * 2] = '\0';
return challenge;
}
/* the extra data parameter must contain a filename with a certificate to
authorization */
static char *auth_cert_create_client_data_ex(authorization_data_t *data,
void *extra_data,
size_t extra_data_len,
size_t *client_data_len,
const EVP_MD *md)
{
char * return_data = NULL;
SSL_CREDENTIALS *proxy = NULL;
unsigned char *signature = NULL;
unsigned int signature_len;
char *output = NULL;
char *p;
unsigned char *creds_buf = NULL;
int creds_buf_len;
proxy = ssl_credentials_new();
if (proxy == NULL)
return NULL;
if (ssl_proxy_load_from_file(proxy, (char *)extra_data, NULL) == SSL_ERROR) {
verror_prepend_string("ssl_proxy_load_from_file()");
goto end;
}
if (ssl_sign((unsigned char *)data->server_data,
strlen(data->server_data), proxy,
&signature, (int *)&signature_len, md) == SSL_ERROR) {
verror_prepend_string("ssl_sign()");
goto end;
}
if (ssl_creds_to_buffer(proxy, &creds_buf, &creds_buf_len) == SSL_ERROR) {
verror_prepend_string("ssl_creds_to_buffer()");
goto end;
}
*client_data_len = 4 + signature_len + creds_buf_len;
output = malloc(*client_data_len);
if (output == NULL) {
verror_put_string("malloc failed");
verror_put_errno(errno);
goto end;
}
p = output;
*(unsigned int*)p = htonl(signature_len);
p += 4;
memcpy(p, signature, signature_len);
p += signature_len;
memcpy(p, creds_buf, creds_buf_len);
return_data = output;
output = NULL;
end:
ssl_credentials_destroy(proxy);
if (signature)
free(signature);
if (output)
free(output);
if (creds_buf)
free(creds_buf);
return return_data;
}
static char *auth_cert_create_client_data(authorization_data_t *data,
void *extra_data,
size_t extra_data_len,
size_t *client_data_len)
{
return auth_cert_create_client_data_ex(data, extra_data, extra_data_len,
client_data_len, EVP_sha1());
}
static char *auth_cert256_create_client_data(authorization_data_t *data,
void *extra_data,
size_t extra_data_len,
size_t *client_data_len)
{
return auth_cert_create_client_data_ex(data, extra_data, extra_data_len,
client_data_len, EVP_sha256());
}
static int auth_cert_check_client_ex(authorization_data_t *auth_data,
struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config,
const EVP_MD *md)
{
SSL_CREDENTIALS *chain = NULL;
unsigned char *signature = NULL;
unsigned char *p;
unsigned int signature_len;
char * authorization_subject = NULL;
char * cred_subject = NULL;
int return_status = 0;
if (config)
config->usage.certauthz_used = 1;
p = (unsigned char *)auth_data->client_data;
signature_len = ntohl(*(unsigned int*)p);
p += 4;
signature = p;
p += signature_len;
if (ssl_creds_from_buffer(p, auth_data->client_data_len - 4 - signature_len,
&chain) == SSL_ERROR) {
verror_prepend_string("internal error: ssl_creds_from_buffer() failed");
goto end;
}
if (ssl_verify((unsigned char *)auth_data->server_data,
strlen(auth_data->server_data),
chain, signature, signature_len, md) == SSL_ERROR) {
verror_prepend_string("certificate verification failed");
goto end;
}
if (ssl_verify_gsi_chain(chain) == SSL_ERROR) {
verror_prepend_string("certificate chain verification failed");
goto end;
}
if (config->limited_proxy == 0) {
switch (ssl_limited_proxy_chain(chain)) {
case 1:
config->limited_proxy = 1;
myproxy_debug("client has a limited proxy chain");
break;
case 0:
break;
default:
verror_prepend_string("unable to determine if limited proxy is present");
goto end;
}
}
if (ssl_get_base_subject(chain, &authorization_subject) == SSL_ERROR) {
verror_prepend_string("internal error: ssl_get_base_subject() failed");
goto end;
}
if (creds->location) {
if (ssl_get_base_subject_file(creds->location, &cred_subject)) {
verror_put_string("internal error: ssl_get_base_subject_file() failed");
goto end;
}
} else {
if (user_dn_lookup(creds->username, &cred_subject, config)) {
verror_put_string("unknown username: %s ", creds->username);
goto end;
}
}
if (strcmp(authorization_subject, cred_subject) != 0) {
verror_prepend_string("certificate subject does not match credential to be renewed");
goto end;
}
myproxy_log("renewal authentication succeeded");
return_status = 1;
end:
if (chain)
ssl_credentials_destroy(chain);
if (authorization_subject)
free(authorization_subject);
if (cred_subject)
free(cred_subject);
return return_status;
}
static int auth_cert_check_client(authorization_data_t *auth_data,
struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config)
{
return auth_cert_check_client_ex(auth_data, creds, client_name, config,
EVP_sha1());
}
static int auth_cert256_check_client(authorization_data_t *auth_data,
struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config)
{
return auth_cert_check_client_ex(auth_data, creds, client_name, config,
EVP_sha256());
}
static struct authorization_func authorization_cert = {
auth_cert_get_status,
auth_cert_create_server_data,
auth_cert_create_client_data,
auth_cert_check_client,
AUTHORIZETYPE_CERT,
"X509_certificate"
};
static struct authorization_func authorization_cert256 = {
auth_cert_get_status,
auth_cert_create_server_data,
auth_cert256_create_client_data,
auth_cert256_check_client,
AUTHORIZETYPE_CERT256,
"X509_certificate_SHA256"
};
#if defined(HAVE_LIBSASL2)
/*
* Implementation of SASL-based authorization
*/
static author_status_t auth_sasl_get_status(struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config)
{
if (config->sasl_policy) {
if (strcmp(config->sasl_policy, "required") == 0) {
return AUTHORIZEMETHOD_REQUIRED;
}
if (strcmp(config->sasl_policy, "sufficient") == 0) {
return AUTHORIZEMETHOD_SUFFICIENT;
}
}
return AUTHORIZEMETHOD_DISABLED;
}
static char *auth_sasl_create_server_data(void)
{
char *challenge = strdup("SASL authorization negotiation server");
return challenge;
}
static char *auth_sasl_create_client_data(authorization_data_t *data,
void *extra_data,
size_t extra_data_len,
size_t *client_data_len)
{
char *tmp;
tmp = malloc(extra_data_len + 1);
if (tmp == NULL)
return NULL;
memcpy(tmp, extra_data, extra_data_len);
tmp[extra_data_len] = '\0';
*client_data_len = extra_data_len + 1;
return tmp;
}
static int auth_sasl_check_client(authorization_data_t *auth_data,
struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config)
{
if (config)
config->usage.sasl_used = 1;
if (myproxy_sasl_authenticated) {
myproxy_log("SASL authentication succeeded for %s",
creds->username);
}
return myproxy_sasl_authenticated;
}
static struct authorization_func authorization_sasl = {
auth_sasl_get_status,
auth_sasl_create_server_data,
auth_sasl_create_client_data,
auth_sasl_check_client,
AUTHORIZETYPE_SASL,
"SASL"
};
#endif /* defined(HAVE_LIBSASL2) */
static struct authorization_func *authorization_funcs[] = {
&authorization_passwd,
#if defined(HAVE_LIBSASL2)
&authorization_sasl,
#endif
&authorization_cert,
&authorization_cert256
};
static int num_funcs = sizeof(authorization_funcs) / sizeof(authorization_funcs[0]);
int
authorization_init_server(authorization_data_t ***data,
author_method_t methods[])
{
authorization_data_t **auth_data;
int i=0, j=0, num_methods=0;
auth_data = malloc(sizeof(authorization_data_t *) * (num_funcs + 1));
if (auth_data == NULL) {
verror_put_string("malloc() failed");
verror_put_errno(errno);
return -1;
}
memset(auth_data, 0, sizeof(authorization_data_t *) * (num_funcs + 1));
for (i = 0; methods[i] != AUTHORIZETYPE_NULL; i++) {
for (j = 0; j < num_funcs; j++) {
if (authorization_funcs[j]->method == methods[i]) {
auth_data[num_methods] = malloc(sizeof(authorization_data_t));
if (auth_data[num_methods] == NULL) {
verror_put_string("malloc() failed");
verror_put_errno(errno);
authorization_data_free(auth_data);
return -1;
}
auth_data[num_methods]->server_data =
authorization_funcs[j]->create_server_data();
auth_data[num_methods]->client_data = NULL;
auth_data[num_methods]->client_data_len = 0;
auth_data[num_methods]->method = authorization_funcs[j]->method;
num_methods++;
}
}
}
auth_data[num_methods] = NULL;
*data = auth_data;
return 0;
}
void
authorization_data_free_contents(authorization_data_t *data)
{
if (data == NULL)
return;
if (data->server_data) {
free (data->server_data);
data->server_data = NULL;
}
if (data->client_data) {
free (data->client_data);
data->client_data = NULL;
}
}
void
authorization_data_free(authorization_data_t **data)
{
authorization_data_t **p = data;
if (data == NULL)
return;
while (*p) {
authorization_data_free_contents(*p);
free(*p);
p++;
}
free(data);
}
authorization_data_t *
authorization_store_response(char *buffer,
size_t bufferlen,
author_method_t method,
authorization_data_t **data)
{
authorization_data_t *d;
d = _find_data(method, data);
if (d) {
if (d->client_data) free(d->client_data);
d->client_data = malloc (bufferlen);
if (d->client_data == NULL)
return NULL;
memcpy(d->client_data, buffer, bufferlen);
d->client_data_len = bufferlen;
}
return d;
}
static struct authorization_func *
_find_func(author_method_t method)
{
int i;
for (i = 0; i < num_funcs; i++)
if (authorization_funcs[i]->method == method)
return authorization_funcs[i];
return NULL;
}
static authorization_data_t *
_find_data(author_method_t method, authorization_data_t **data)
{
authorization_data_t **d = data;
if (data == NULL)
return NULL;
while (*d) {
if ((*d)->method == method)
return (*d);
d++;
}
return NULL;
}
char *
authorization_get_name(author_method_t method)
{
struct authorization_func *af = _find_func(method);
if (af == NULL)
return "unknown";
return(af->name);
}
author_method_t
authorization_get_method(char *name)
{
int i;
for (i = 0; i < num_funcs; i++)
if (strcmp(authorization_funcs[i]->name, name) == 0)
return authorization_funcs[i]->method;
return AUTHORIZETYPE_NULL;
}
author_status_t
authorization_get_status(author_method_t method,
struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config)
{
struct authorization_func *af = _find_func(method);
if (af == NULL) {
return AUTHORIZEMETHOD_DISABLED;
}
return (af->get_status(creds, client_name, config));
}
int
authorization_check(authorization_data_t *client_auth_data,
struct myproxy_creds *creds,
char *client_name)
{
return authorization_check_ex(client_auth_data, creds, client_name, NULL);
}
int
authorization_check_ex(authorization_data_t *client_auth_data,
struct myproxy_creds *creds,
char *client_name,
myproxy_server_context_t *config)
{
struct authorization_func *af = _find_func(client_auth_data->method);
if (af == NULL) {
verror_put_string("Not supported authorization method");
return -1;
}
return (af->check_client(client_auth_data, creds, client_name, config));
}
authorization_data_t *
authorization_create_response(authorization_data_t **data,
author_method_t method,
void *extra_data,
size_t extra_data_len)
{
authorization_data_t *d;
struct authorization_func *af = _find_func(method);
if (af == NULL) {
verror_put_string("Unsupported authorization method");
return NULL;
}
d = _find_data(method, data);
if (d == NULL) {
verror_put_string("Unable to perform %s negotiation with server.",
af->name);
return NULL;
}
if (d->client_data) free(d->client_data);
if ((d->client_data = af->create_client_data(d, extra_data, extra_data_len,
&d->client_data_len)) == NULL)
return NULL;
return d;
}
|