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 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
|
/* -*-pgsql-c-*- */
/*
* $Header: /cvsroot/pgpool/pgpool/pool_auth.c,v 1.9 2006/04/17 03:59:25 t-ishii Exp $
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2006 PgPool Global Development Group
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that copyright notice and this permission
* notice appear in supporting documentation, and that the name of the
* author not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. The author makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* pool_auth.c: authenticaton stuff
*
*/
#include "pool.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_PARAM_H
#include <param.h>
#endif
#include <errno.h>
#include <string.h>
static POOL_STATUS pool_send_auth_ok(POOL_CONNECTION *frontend, int pid, int key, int protoMajor);
static int do_clear_text_password(POOL_CONNECTION *backend, POOL_CONNECTION *frontend, int reauth, int protoMajor);
static int do_crypt(POOL_CONNECTION *backend, POOL_CONNECTION *frontend, int reauth, int protoMajor);
static int do_md5(POOL_CONNECTION *backend, POOL_CONNECTION *frontend, int reauth, int protoMajor);
/*
* do authentication against backend. if success return 0 otherwise non 0.
*/
int pool_do_auth(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *cp)
{
int status;
signed char kind;
int pid, pid1;
int key, key1;
int protoMajor;
int length;
protoMajor = MAJOR(cp);
kind = pool_read_kind(cp);
if (kind < 0)
{
return -1;
}
/* error response? */
if (kind == 'E')
{
/* we assume error response at this stage is likely version
* protocol mismatch (v3 frontend vs. v2 backend). So we throw
* a V2 protocol error response in the hope that v3 frontend
* will negotiate again using v2 protocol.
*/
pool_log("pool_do_auth: maybe protocol version mismatch (current version %d)", protoMajor);
ErrorResponse(frontend, cp);
return -1;
}
else if (kind != 'R')
{
pool_error("pool_do_auth: expect \"R\" got %c", kind);
return -1;
}
/*
* message length (v3 only) */
if (protoMajor == PROTO_MAJOR_V3 && pool_read_message_length(cp) < 0)
{
return -1;
}
/*
* read authentication request kind.
*
* 0: authentication ok
* 1: kerberos v4
* 2: kerberos v5
* 3: clear text password
* 4: crypt password
* 5: md5 password
* 6: scm credential
*
* in replication mode, we only supports kind = 0, 3. this is because to "salt"
* cannot be replicated among master and secondary.
* in non replication mode, we supports kind = 0, 3, 4, 5
*/
status = pool_read(MASTER(cp), &pid, sizeof(pid));
if (status < 0)
{
pool_error("pool_do_auth: read authentication kind failed");
return -1;
}
pid = ntohl(pid);
if (DUAL_MODE)
{
status = pool_read(SECONDARY(cp), &pid1, sizeof(pid1));
if (status < 0)
{
pool_error("pool_do_auth: read authentication kind from secondary failed");
return -1;
}
pid1 = ntohl(pid1);
if (pid != pid1)
{
pool_error("pool_do_auth: authentication kind does not match between master(%d) and secondary(%d)", pid, pid1);
return -1;
}
}
/* trust? */
if (pid == 0)
{
int msglen;
pool_write(frontend, "R", 1);
if (protoMajor == PROTO_MAJOR_V3)
{
msglen = htonl(8);
pool_write(frontend, &msglen, sizeof(msglen));
}
msglen = htonl(0);
if (pool_write_and_flush(frontend, &msglen, sizeof(msglen)) < 0)
{
return -1;
}
MASTER(cp)->auth_kind = 0;
}
/* clear text password authentication? */
else if (pid == 3)
{
pool_debug("trying clear text password authentication");
pid = do_clear_text_password(MASTER(cp), frontend, 0, protoMajor);
if (pid >= 0 && DUAL_MODE)
{
pid = do_clear_text_password(SECONDARY(cp), frontend, 0, protoMajor);
}
}
/* crypt authentication? */
else if (pid == 4)
{
pool_debug("trying crypt authentication");
pid = do_crypt(MASTER(cp), frontend, 0, protoMajor);
if (pid >= 0 && DUAL_MODE)
{
pid = do_crypt(SECONDARY(cp), frontend, 0, protoMajor);
}
}
/* md5 authentication? */
else if (pid == 5)
{
pool_debug("trying md5 authentication");
pid = do_md5(MASTER(cp), frontend, 0, protoMajor);
if (pid >= 0 && DUAL_MODE)
{
pid = do_md5(SECONDARY(cp), frontend, 0, protoMajor);
}
}
if (pid != 0)
{
pool_error("pool_do_auth: backend does not return authenticaton ok");
return -1;
}
/*
* authentication ok. now read pid and secret key from the
* backend
*/
for (;;)
{
kind = pool_read_kind(cp);
if (kind < 0)
{
pool_error("pool_do_auth: failed to read kind before backendkeydata");
return -1;
}
else if (kind == 'K')
break;
if (protoMajor == PROTO_MAJOR_V3)
{
switch (kind)
{
case 'S':
/* process parameter status */
if (ParameterStatus(frontend, cp) != POOL_CONTINUE)
return -1;
pool_flush(frontend);
break;
case 'N':
/* process notice message */
if (SimpleForwardToFrontend(kind, frontend, cp))
return -1;
pool_flush(frontend);
break;
/* process error message */
case 'E':
SimpleForwardToFrontend(kind, frontend, cp);
pool_flush(frontend);
return -1;
break;
default:
pool_error("pool_do_auth: unknown response \"%c\" before processing BackendKeyData",
kind);
return -1;
break;
}
}
else
{
/* V2 case */
switch (kind)
{
case 'N':
/* process notice message */
if (NoticeResponse(frontend, cp) != POOL_CONTINUE)
return -1;
break;
/* process error message */
case 'E':
ErrorResponse(frontend, cp);
return -1;
break;
default:
pool_error("pool_do_auth: unknown response \"%c\" before processing V2 BackendKeyData",
kind);
return -1;
break;
}
}
}
/*
* message length (V3 only)
*/
if (protoMajor == PROTO_MAJOR_V3)
{
if (kind != 'K')
{
pool_error("pool_do_auth: expect \"K\" got %c", kind);
return -1;
}
if ((length = pool_read_message_length(cp)) != 12)
{
pool_error("pool_do_auth: invalid messages length(%d) for BackendKeyData", length);
return -1;
}
}
/*
* OK, read pid and secret key
*/
/* pid */
pool_read(MASTER(cp), &pid, sizeof(pid));
MASTER_CONNECTION(cp)->pid = pid;
/* key */
pool_read(MASTER(cp), &key, sizeof(key));
MASTER_CONNECTION(cp)->key = key;
if (DUAL_MODE)
{
pool_read(SECONDARY(cp), &pid1, sizeof(pid1));
SECONDARY_CONNECTION(cp)->pid = pid;
/* key */
pool_read(SECONDARY(cp), &key1, sizeof(key1));
SECONDARY_CONNECTION(cp)->key = key;
}
return (pool_send_auth_ok(frontend, pid, key, protoMajor));
}
/*
* do re-authentication for reused connection. if success return 0 otherwise non 0.
*/
int pool_do_reauth(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *cp)
{
int status;
int protoMajor;
protoMajor = MAJOR(cp);
switch(MASTER(cp)->auth_kind)
{
case 0:
/* trust */
status = 0;
break;
case 3:
/* clear text password */
status = do_clear_text_password(MASTER(cp), frontend, 1, protoMajor);
break;
case 4:
/* crypt password */
status = do_crypt(MASTER(cp), frontend, 1, protoMajor);
break;
case 5:
/* md5 password */
status = do_md5(MASTER(cp), frontend, 1, protoMajor);
break;
default:
pool_error("pool_do_reauth: unknown authentication request code %d",
MASTER(cp)->auth_kind);
return -1;
}
if (status == 0)
{
int msglen;
pool_write(frontend, "R", 1);
if (protoMajor == PROTO_MAJOR_V3)
{
msglen = htonl(8);
pool_write(frontend, &msglen, sizeof(msglen));
}
msglen = htonl(0);
if (pool_write_and_flush(frontend, &msglen, sizeof(msglen)) < 0)
{
return -1;
}
}
else
{
pool_debug("pool_do_reauth: authentication failed");
return -1;
}
return (pool_send_auth_ok(frontend, MASTER_CONNECTION(cp)->pid, MASTER_CONNECTION(cp)->key, protoMajor) != POOL_CONTINUE);
}
/*
* send authentication ok to frontend. if success return 0 otherwise non 0.
*/
static POOL_STATUS pool_send_auth_ok(POOL_CONNECTION *frontend, int pid, int key, int protoMajor)
{
char kind;
int len;
#ifdef NOT_USED
if (protoMajor == PROTO_MAJOR_V2)
{
/* return "Authentication OK" to the frontend */
kind = 'R';
pool_write(frontend, &kind, 1);
len = htonl(0);
if (pool_write_and_flush(frontend, &len, sizeof(len)) < 0)
{
return -1;
}
}
#endif
/* send backend key data */
kind = 'K';
pool_write(frontend, &kind, 1);
if (protoMajor == PROTO_MAJOR_V3)
{
len = htonl(12);
pool_write(frontend, &len, sizeof(len));
}
pool_debug("pool_send_auth_ok: send pid %d to frontend", ntohl(pid));
pool_write(frontend, &pid, sizeof(pid));
if (pool_write_and_flush(frontend, &key, sizeof(key)) < 0)
{
return -1;
}
return 0;
}
/*
* perform clear text password authetication
*/
static int do_clear_text_password(POOL_CONNECTION *backend, POOL_CONNECTION *frontend, int reauth, int protoMajor)
{
static int size;
static char password[MAX_PASSWORD_SIZE];
char response;
int kind;
int len;
/* master? */
if (!backend->issecondary_backend)
{
pool_write(frontend, "R", 1); /* authenticaton */
if (protoMajor == PROTO_MAJOR_V3)
{
len = htonl(8);
pool_write(frontend, &len, sizeof(len));
}
kind = htonl(3); /* clear text password authentication */
pool_write_and_flush(frontend, &kind, sizeof(kind)); /* indicating clear text password authentication */
/* read password packet */
if (protoMajor == PROTO_MAJOR_V2)
{
if (pool_read(frontend, &size, sizeof(size)))
{
pool_error("do_clear_text_password: failed to read password packet size");
return -1;
}
}
else
{
char k;
if (pool_read(frontend, &k, sizeof(k)))
{
pool_error("do_clear_text_password: failed to read password packet \"p\"");
return -1;
}
if (k != 'p')
{
pool_error("do_clear_text_password: password packet does not start with \"p\"");
return -1;
}
if (pool_read(frontend, &size, sizeof(size)))
{
pool_error("do_clear_text_password: failed to read password packet size");
return -1;
}
}
if ((ntohl(size) - 4) > sizeof(password))
{
pool_error("do_clear_text_password: password is too long (size: %d)", ntohl(size) - 4);
return -1;
}
if (pool_read(frontend, password, ntohl(size) - 4))
{
pool_error("do_clear_text_password: failed to read password (size: %d)", ntohl(size) - 4);
return -1;
}
}
/* connection reusing? */
if (reauth)
{
if ((ntohl(size) - 4) != backend->pwd_size)
{
pool_debug("do_clear_text_password; password size does not match in re-authetication");
return -1;
}
if (memcmp(password, backend->password, backend->pwd_size) != 0)
{
pool_debug("do_clear_text_password; password does not match in re-authetication");
return -1;
}
return 0;
}
/* send password packet to backend */
if (protoMajor == PROTO_MAJOR_V3)
pool_write(backend, "p", 1);
pool_write(backend, &size, sizeof(size));
pool_write_and_flush(backend, password, ntohl(size) -4);
if (pool_read(backend, &response, sizeof(response)))
{
pool_error("do_clear_text_password: failed to read authentication response");
return -1;
}
if (response != 'R')
{
pool_debug("do_clear_text_password: backend does not return R while processing clear text password authentication");
return -1;
}
if (protoMajor == PROTO_MAJOR_V3)
{
if (pool_read(backend, &len, sizeof(len)))
{
pool_error("do_clear_text_password: failed to read authentication packet size");
return -1;
}
if (ntohl(len) != 8)
{
pool_error("do_clear_text_password: incorrect authentication packet size (%d)", ntohl(len));
return -1;
}
}
/* expect to read "Authentication OK" response. kind should be 0... */
if (pool_read(backend, &kind, sizeof(kind)))
{
pool_debug("do_clear_text_password: failed to read Authentication OK response");
return -1;
}
/* if authenticated, save info */
if (!reauth && kind == 0)
{
if (!backend->issecondary_backend)
{
int msglen;
pool_write(frontend, "R", 1);
if (protoMajor == PROTO_MAJOR_V3)
{
msglen = htonl(8);
pool_write(frontend, &msglen, sizeof(msglen));
}
msglen = htonl(0);
if (pool_write_and_flush(frontend, &msglen, sizeof(msglen)) < 0)
{
return -1;
}
}
backend->auth_kind = 3;
backend->pwd_size = ntohl(size) - 4;
memcpy(backend->password, password, backend->pwd_size);
}
return kind;
}
/*
* perform crypt authetication
*/
static int do_crypt(POOL_CONNECTION *backend, POOL_CONNECTION *frontend, int reauth, int protoMajor)
{
char salt[2];
static int size;
static char password[MAX_PASSWORD_SIZE];
char response;
int kind;
int len;
if (!reauth)
{
/* read salt */
if (pool_read(backend, salt, sizeof(salt)))
{
pool_error("do_crypt: failed to read salt");
return -1;
}
}
else
{
memcpy(salt, backend->salt, sizeof(salt));
}
/* master? */
if (!backend->issecondary_backend)
{
pool_write(frontend, "R", 1); /* authenticaton */
if (protoMajor == PROTO_MAJOR_V3)
{
len = htonl(10);
pool_write(frontend, &len, sizeof(len));
}
kind = htonl(4); /* crypt authentication */
pool_write(frontend, &kind, sizeof(kind)); /* indicating crypt authentication */
pool_write_and_flush(frontend, salt, sizeof(salt)); /* salt */
/* read password packet */
if (protoMajor == PROTO_MAJOR_V2)
{
if (pool_read(frontend, &size, sizeof(size)))
{
pool_error("do_crypt: failed to read password packet size");
return -1;
}
}
else
{
char k;
if (pool_read(frontend, &k, sizeof(k)))
{
pool_error("do_crypt_password: failed to read password packet \"p\"");
return -1;
}
if (k != 'p')
{
pool_error("do_crypt_password: password packet does not start with \"p\"");
return -1;
}
if (pool_read(frontend, &size, sizeof(size)))
{
pool_error("do_crypt_password: failed to read password packet size");
return -1;
}
}
if ((ntohl(size) - 4) > sizeof(password))
{
pool_error("do_crypt: password is too long(size: %d)", ntohl(size) - 4);
return -1;
}
if (pool_read(frontend, password, ntohl(size) - 4))
{
pool_error("do_crypt: failed to read password (size: %d)", ntohl(size) - 4);
return -1;
}
}
/* connection reusing? */
if (reauth)
{
pool_debug("size: %d saved_size: %d", (ntohl(size) - 4), backend->pwd_size);
if ((ntohl(size) - 4) != backend->pwd_size)
{
pool_debug("do_crypt: password size does not match in re-authetication");
return -1;
}
if (memcmp(password, backend->password, backend->pwd_size) != 0)
{
pool_debug("do_crypt: password does not match in re-authetication");
return -1;
}
return 0;
}
/* send password packet to backend */
if (protoMajor == PROTO_MAJOR_V3)
pool_write(backend, "p", 1);
pool_write(backend, &size, sizeof(size));
pool_write_and_flush(backend, password, ntohl(size) -4);
if (pool_read(backend, &response, sizeof(response)))
{
pool_error("do_crypt: failed to read authentication response");
return -1;
}
if (response != 'R')
{
pool_debug("do_crypt: backend does not return R while processing crypt authentication(%02x) secondary: %d", response, backend->issecondary_backend);
return -1;
}
if (protoMajor == PROTO_MAJOR_V3)
{
if (pool_read(backend, &len, sizeof(len)))
{
pool_error("do_clear_text_password: failed to read authentication packet size");
return -1;
}
if (ntohl(len) != 8)
{
pool_error("do_clear_text_password: incorrect authentication packet size (%d)", ntohl(len));
return -1;
}
}
/* expect to read "Authentication OK" response. kind should be 0... */
if (pool_read(backend, &kind, sizeof(kind)))
{
pool_debug("do_crypt: failed to read Authentication OK response");
return -1;
}
/* if authenticated, save info */
if (!reauth && kind == 0)
{
int msglen;
pool_write(frontend, "R", 1);
if (protoMajor == PROTO_MAJOR_V3)
{
msglen = htonl(8);
pool_write(frontend, &msglen, sizeof(msglen));
}
msglen = htonl(0);
if (pool_write_and_flush(frontend, &msglen, sizeof(msglen)) < 0)
{
return -1;
}
backend->auth_kind = 4;
backend->pwd_size = ntohl(size) - 4;
memcpy(backend->password, password, backend->pwd_size);
memcpy(backend->salt, salt, sizeof(salt));
}
return kind;
}
/*
* perform MD5 authetication
*/
static int do_md5(POOL_CONNECTION *backend, POOL_CONNECTION *frontend, int reauth, int protoMajor)
{
char salt[4];
static int size;
static char password[MAX_PASSWORD_SIZE];
char response;
int kind;
int len;
if (!reauth)
{
/* read salt */
if (pool_read(backend, salt, sizeof(salt)))
{
pool_error("do_md5: failed to read salt");
return -1;
}
pool_debug("master: %d salt: %hhx%hhx%hhx%hhx", !backend->issecondary_backend,
salt[0], salt[1], salt[2], salt[3]);
}
else
{
memcpy(salt, backend->salt, sizeof(salt));
}
/* master? */
if (!backend->issecondary_backend)
{
pool_write(frontend, "R", 1); /* authenticaton */
if (protoMajor == PROTO_MAJOR_V3)
{
len = htonl(12);
pool_write(frontend, &len, sizeof(len));
}
kind = htonl(5);
pool_write(frontend, &kind, sizeof(kind)); /* indicating MD5 */
pool_write_and_flush(frontend, salt, sizeof(salt)); /* salt */
/* read password packet */
if (protoMajor == PROTO_MAJOR_V2)
{
if (pool_read(frontend, &size, sizeof(size)))
{
pool_error("do_md5: failed to read password packet size");
return -1;
}
}
else
{
char k;
if (pool_read(frontend, &k, sizeof(k)))
{
pool_error("do_md5_password: failed to read password packet \"p\"");
return -1;
}
if (k != 'p')
{
pool_error("do_md5_password: password packet does not start with \"p\"");
return -1;
}
if (pool_read(frontend, &size, sizeof(size)))
{
pool_error("do_md5_password: failed to read password packet size");
return -1;
}
}
if ((ntohl(size) - 4) > sizeof(password))
{
pool_error("do_md5: password is too long(size: %d)", ntohl(size) - 4);
return -1;
}
if (pool_read(frontend, password, ntohl(size) - 4))
{
pool_error("do_md5: failed to read password (size: %d)", ntohl(size) - 4);
return -1;
}
}
/* connection reusing? */
if (reauth)
{
if ((ntohl(size) - 4) != backend->pwd_size)
{
pool_debug("do_md5; password size does not match in re-authetication");
return -1;
}
if (memcmp(password, backend->password, backend->pwd_size) != 0)
{
pool_debug("do_md5; password does not match in re-authetication");
return -1;
}
return 0;
}
/* send password packet to backend */
if (protoMajor == PROTO_MAJOR_V3)
pool_write(backend, "p", 1);
pool_write(backend, &size, sizeof(size));
pool_write_and_flush(backend, password, ntohl(size) -4);
if (pool_read(backend, &response, sizeof(response)))
{
pool_error("do_md5: failed to read authentication response");
return -1;
}
if (response != 'R')
{
pool_debug("do_md5: backend does not return R while processing MD5 authentication %c", response);
return -1;
}
if (protoMajor == PROTO_MAJOR_V3)
{
if (pool_read(backend, &len, sizeof(len)))
{
pool_error("do_md5: failed to read authentication packet size");
return -1;
}
if (ntohl(len) != 8)
{
pool_error("do_clear_text_password: incorrect authentication packet size (%d)", ntohl(len));
return -1;
}
}
/* expect to read "Authentication OK" response. kind should be 0... */
if (pool_read(backend, &kind, sizeof(kind)))
{
pool_debug("do_md5: failed to read Authentication OK response");
return -1;
}
/* if authenticated, save info */
if (!reauth && kind == 0)
{
int msglen;
pool_write(frontend, "R", 1);
if (protoMajor == PROTO_MAJOR_V3)
{
msglen = htonl(8);
pool_write(frontend, &msglen, sizeof(msglen));
}
msglen = htonl(0);
if (pool_write_and_flush(frontend, &msglen, sizeof(msglen)) < 0)
{
return -1;
}
backend->auth_kind = 5;
backend->pwd_size = ntohl(size) - 4;
memcpy(backend->password, password, backend->pwd_size);
memcpy(backend->salt, salt, sizeof(salt));
}
return kind;
}
/*
* read message length (V3 only)
*/
int pool_read_message_length(POOL_CONNECTION_POOL *cp)
{
int status;
int length, length1;
status = pool_read(MASTER(cp), &length, sizeof(length));
if (status < 0)
{
pool_error("pool_read_message_length: error while reading message length");
return -1;
}
length = ntohl(length);
pool_debug("pool_read_message_length: lenghth: %d", length);
if (DUAL_MODE)
{
status = pool_read(SECONDARY(cp), &length1, sizeof(length1));
if (status < 0)
{
pool_error("pool_read_message_length: error while reading message length from secondary backend");
return -1;
}
length1 = ntohl(length1);
if (length != length1)
{
pool_error("pool_read_message_length: length does not match between backends master(%d) secondary(%d)",
length, length1);
return -1;
}
}
if (length < 0)
{
pool_error("pool_read_message_length: invalid message length (%d)", length);
return -1;
}
return length;
}
/*
* read message length2 (V3 only)
* unlike pool_read_message_length, this returns an array of message length.
* the array is in the static storage, thus it will be destroyed by subsequent calls.
*/
int *pool_read_message_length2(POOL_CONNECTION_POOL *cp)
{
int status;
int length, length1;
static int length_array[MAX_CONNECTION_SLOTS];
status = pool_read(MASTER(cp), &length, sizeof(length));
if (status < 0)
{
pool_error("pool_read_message_length2: error while reading message length");
return NULL;
}
length = ntohl(length);
pool_debug("pool_read_message_length2: master lenghth: %d", length);
if (DUAL_MODE)
{
status = pool_read(SECONDARY(cp), &length1, sizeof(length1));
if (status < 0)
{
pool_error("pool_read_message_length2: error while reading message length from secondary backend");
return NULL;
}
length1 = ntohl(length1);
if (length != length1)
{
pool_debug("pool_read_message_length2: length does not match between backends master(%d) secondary(%d)",
length, length1);
}
}
else
{
length1 = 0;
}
if (length < 0 || length1 < 0)
{
pool_error("pool_read_message_length2: invalid message length (%d) length1 (%d)", length, length1);
return NULL;
}
length_array[0] = length;
length_array[1] = length1;
return &length_array[0];
}
signed char pool_read_kind(POOL_CONNECTION_POOL *cp)
{
int status;
char kind, kind1;
status = pool_read(MASTER(cp), &kind, sizeof(kind));
if (status < 0)
{
pool_error("read_kind: error while reading message kind");
return -1;
}
if (DUAL_MODE)
{
status = pool_read(SECONDARY(cp), &kind1, sizeof(kind1));
if (status < 0)
{
pool_error("read_kind: error while reading message kind from secondary backend");
return -1;
}
if (kind != kind1)
{
pool_error("read_kind: kind does not match between backends master(%d) secondary(%d)",
kind, kind1);
return -1;
}
}
return kind;
}
|